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 "async_smb.h"
23 #include "libsmb/clirap.h"
25 /***********************************************************
26 Common function for pushing stings, used by smb_bytes_push_str()
27 and trans_bytes_push_str(). Only difference is the align_odd
29 ***********************************************************/
31 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
32 const char *str, size_t str_len,
34 size_t *pconverted_size)
38 size_t converted_size;
44 buflen = talloc_get_size(buf);
46 if (align_odd && ucs2 && (buflen % 2 == 0)) {
48 * We're pushing into an SMB buffer, align odd
50 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
58 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
59 ucs2 ? CH_UTF16LE : CH_DOS,
60 str, str_len, &converted,
61 &converted_size, true)) {
65 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
66 buflen + converted_size);
68 TALLOC_FREE(converted);
72 memcpy(buf + buflen, converted, converted_size);
74 TALLOC_FREE(converted);
76 if (pconverted_size) {
77 *pconverted_size = converted_size;
83 /***********************************************************
84 Push a string into an SMB buffer, with odd byte alignment
85 if it's a UCS2 string.
86 ***********************************************************/
88 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
89 const char *str, size_t str_len,
90 size_t *pconverted_size)
92 return internal_bytes_push_str(buf, ucs2, str, str_len,
93 true, pconverted_size);
96 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
97 const uint8_t *bytes, size_t num_bytes)
104 buflen = talloc_get_size(buf);
106 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
107 buflen + 1 + num_bytes);
111 buf[buflen] = prefix;
112 memcpy(&buf[buflen+1], bytes, num_bytes);
116 /***********************************************************
117 Same as smb_bytes_push_str(), but without the odd byte
118 align for ucs2 (we're pushing into a param or data block).
119 static for now, although this will probably change when
120 other modules use async trans calls.
121 ***********************************************************/
123 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
124 const char *str, size_t str_len,
125 size_t *pconverted_size)
127 return internal_bytes_push_str(buf, ucs2, str, str_len,
128 false, pconverted_size);
131 struct cli_setpathinfo_state {
136 static void cli_setpathinfo_done(struct tevent_req *subreq);
138 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
139 struct tevent_context *ev,
140 struct cli_state *cli,
146 struct tevent_req *req, *subreq;
147 struct cli_setpathinfo_state *state;
149 req = tevent_req_create(mem_ctx, &state,
150 struct cli_setpathinfo_state);
155 /* Setup setup word. */
156 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
158 /* Setup param array. */
159 state->param = TALLOC_ZERO_ARRAY(state, uint8_t, 6);
160 if (tevent_req_nomem(state->param, req)) {
161 return tevent_req_post(req, ev);
163 SSVAL(state->param, 0, level);
165 state->param = trans2_bytes_push_str(
166 state->param, cli_ucs2(cli), path, strlen(path)+1, NULL);
167 if (tevent_req_nomem(state->param, req)) {
168 return tevent_req_post(req, ev);
171 subreq = cli_trans_send(
172 state, /* mem ctx. */
174 cli, /* cli_state. */
175 SMBtrans2, /* cmd. */
176 NULL, /* pipe name. */
180 &state->setup, /* setup. */
181 1, /* num setup uint16_t words. */
182 0, /* max returned setup. */
183 state->param, /* param. */
184 talloc_get_size(state->param), /* num param. */
185 2, /* max returned param. */
187 data_len, /* num data. */
188 0); /* max returned data. */
190 if (tevent_req_nomem(subreq, req)) {
191 return tevent_req_post(req, ev);
193 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
197 static void cli_setpathinfo_done(struct tevent_req *subreq)
199 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
200 NULL, 0, NULL, NULL, 0, NULL);
201 tevent_req_simple_finish_ntstatus(subreq, status);
204 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
206 return tevent_req_simple_recv_ntstatus(req);
209 NTSTATUS cli_setpathinfo(struct cli_state *cli,
215 TALLOC_CTX *frame = talloc_stackframe();
216 struct tevent_context *ev;
217 struct tevent_req *req;
218 NTSTATUS status = NT_STATUS_NO_MEMORY;
220 if (cli_has_async_calls(cli)) {
222 * Can't use sync call while an async call is in flight
224 status = NT_STATUS_INVALID_PARAMETER;
227 ev = tevent_context_init(frame);
231 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
235 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
238 status = cli_setpathinfo_recv(req);
244 /****************************************************************************
245 Hard/Symlink a file (UNIX extensions).
246 Creates new name (sym)linked to oldname.
247 ****************************************************************************/
249 struct cli_posix_link_internal_state {
253 static void cli_posix_link_internal_done(struct tevent_req *subreq);
255 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
256 struct event_context *ev,
257 struct cli_state *cli,
262 struct tevent_req *req = NULL, *subreq = NULL;
263 struct cli_posix_link_internal_state *state = NULL;
265 req = tevent_req_create(mem_ctx, &state,
266 struct cli_posix_link_internal_state);
271 /* Setup data array. */
272 state->data = talloc_array(state, uint8_t, 0);
273 if (tevent_req_nomem(state->data, req)) {
274 return tevent_req_post(req, ev);
276 state->data = trans2_bytes_push_str(
277 state->data, cli_ucs2(cli), oldname, strlen(oldname)+1, NULL);
279 subreq = cli_setpathinfo_send(
280 state, ev, cli, level, newname,
281 state->data, talloc_get_size(state->data));
282 if (tevent_req_nomem(subreq, req)) {
283 return tevent_req_post(req, ev);
285 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
289 static void cli_posix_link_internal_done(struct tevent_req *subreq)
291 NTSTATUS status = cli_setpathinfo_recv(subreq);
292 tevent_req_simple_finish_ntstatus(subreq, status);
295 /****************************************************************************
296 Symlink a file (UNIX extensions).
297 ****************************************************************************/
299 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
300 struct event_context *ev,
301 struct cli_state *cli,
305 return cli_posix_link_internal_send(
306 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
309 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
311 return tevent_req_simple_recv_ntstatus(req);
314 NTSTATUS cli_posix_symlink(struct cli_state *cli,
318 TALLOC_CTX *frame = talloc_stackframe();
319 struct event_context *ev = NULL;
320 struct tevent_req *req = NULL;
321 NTSTATUS status = NT_STATUS_OK;
323 if (cli_has_async_calls(cli)) {
325 * Can't use sync call while an async call is in flight
327 status = NT_STATUS_INVALID_PARAMETER;
331 ev = event_context_init(frame);
333 status = NT_STATUS_NO_MEMORY;
337 req = cli_posix_symlink_send(frame,
343 status = NT_STATUS_NO_MEMORY;
347 if (!tevent_req_poll(req, ev)) {
348 status = map_nt_error_from_unix(errno);
352 status = cli_posix_symlink_recv(req);
356 if (!NT_STATUS_IS_OK(status)) {
357 cli_set_error(cli, status);
362 /****************************************************************************
363 Read a POSIX symlink.
364 ****************************************************************************/
366 struct readlink_state {
371 static void cli_posix_readlink_done(struct tevent_req *subreq);
373 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
374 struct event_context *ev,
375 struct cli_state *cli,
379 struct tevent_req *req = NULL, *subreq = NULL;
380 struct readlink_state *state = NULL;
381 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
383 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
389 * Len is in bytes, we need it in UCS2 units.
391 if ((2*len < len) || (maxbytelen < len)) {
392 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
393 return tevent_req_post(req, ev);
396 subreq = cli_qpathinfo_send(state, ev, cli, fname,
397 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
398 if (tevent_req_nomem(subreq, req)) {
399 return tevent_req_post(req, ev);
401 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
405 static void cli_posix_readlink_done(struct tevent_req *subreq)
407 struct tevent_req *req = tevent_req_callback_data(
408 subreq, struct tevent_req);
409 struct readlink_state *state = tevent_req_data(
410 req, struct readlink_state);
413 status = cli_qpathinfo_recv(subreq, state, &state->data,
416 if (!NT_STATUS_IS_OK(status)) {
417 tevent_req_nterror(req, status);
421 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
423 if (state->data[state->num_data-1] != '\0') {
424 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
427 tevent_req_done(req);
430 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
431 char *retpath, size_t len)
434 char *converted = NULL;
435 size_t converted_size = 0;
436 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
438 if (tevent_req_is_nterror(req, &status)) {
441 /* The returned data is a pushed string, not raw data. */
442 if (!convert_string_talloc(state,
443 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
450 return NT_STATUS_NO_MEMORY;
453 len = MIN(len,converted_size);
455 return NT_STATUS_DATA_ERROR;
457 memcpy(retpath, converted, len);
461 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
462 char *linkpath, size_t len)
464 TALLOC_CTX *frame = talloc_stackframe();
465 struct event_context *ev = NULL;
466 struct tevent_req *req = NULL;
467 NTSTATUS status = NT_STATUS_OK;
469 if (cli_has_async_calls(cli)) {
471 * Can't use sync call while an async call is in flight
473 status = NT_STATUS_INVALID_PARAMETER;
477 ev = event_context_init(frame);
479 status = NT_STATUS_NO_MEMORY;
483 req = cli_posix_readlink_send(frame,
489 status = NT_STATUS_NO_MEMORY;
493 if (!tevent_req_poll(req, ev)) {
494 status = map_nt_error_from_unix(errno);
498 status = cli_posix_readlink_recv(req, cli, linkpath, len);
502 if (!NT_STATUS_IS_OK(status)) {
503 cli_set_error(cli, status);
508 /****************************************************************************
509 Hard link a file (UNIX extensions).
510 ****************************************************************************/
512 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
513 struct event_context *ev,
514 struct cli_state *cli,
518 return cli_posix_link_internal_send(
519 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
522 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
524 return tevent_req_simple_recv_ntstatus(req);
527 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
531 TALLOC_CTX *frame = talloc_stackframe();
532 struct event_context *ev = NULL;
533 struct tevent_req *req = NULL;
534 NTSTATUS status = NT_STATUS_OK;
536 if (cli_has_async_calls(cli)) {
538 * Can't use sync call while an async call is in flight
540 status = NT_STATUS_INVALID_PARAMETER;
544 ev = event_context_init(frame);
546 status = NT_STATUS_NO_MEMORY;
550 req = cli_posix_hardlink_send(frame,
556 status = NT_STATUS_NO_MEMORY;
560 if (!tevent_req_poll(req, ev)) {
561 status = map_nt_error_from_unix(errno);
565 status = cli_posix_hardlink_recv(req);
569 if (!NT_STATUS_IS_OK(status)) {
570 cli_set_error(cli, status);
575 /****************************************************************************
576 Map standard UNIX permissions onto wire representations.
577 ****************************************************************************/
579 uint32_t unix_perms_to_wire(mode_t perms)
581 unsigned int ret = 0;
583 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
584 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
585 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
586 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
587 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
588 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
589 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
590 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
591 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
593 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
596 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
599 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
604 /****************************************************************************
605 Map wire permissions to standard UNIX.
606 ****************************************************************************/
608 mode_t wire_perms_to_unix(uint32_t perms)
610 mode_t ret = (mode_t)0;
612 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
613 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
614 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
615 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
616 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
617 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
618 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
619 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
620 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
622 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
625 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
628 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
633 /****************************************************************************
634 Return the file type from the wire filetype for UNIX extensions.
635 ****************************************************************************/
637 static mode_t unix_filetype_from_wire(uint32_t wire_type)
645 case UNIX_TYPE_SYMLINK:
649 case UNIX_TYPE_CHARDEV:
653 case UNIX_TYPE_BLKDEV:
661 case UNIX_TYPE_SOCKET:
669 /****************************************************************************
670 Do a POSIX getfacl (UNIX extensions).
671 ****************************************************************************/
673 struct getfacl_state {
678 static void cli_posix_getfacl_done(struct tevent_req *subreq);
680 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
681 struct event_context *ev,
682 struct cli_state *cli,
685 struct tevent_req *req = NULL, *subreq = NULL;
686 struct getfacl_state *state = NULL;
688 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
692 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
694 if (tevent_req_nomem(subreq, req)) {
695 return tevent_req_post(req, ev);
697 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
701 static void cli_posix_getfacl_done(struct tevent_req *subreq)
703 struct tevent_req *req = tevent_req_callback_data(
704 subreq, struct tevent_req);
705 struct getfacl_state *state = tevent_req_data(
706 req, struct getfacl_state);
709 status = cli_qpathinfo_recv(subreq, state, &state->data,
712 if (!NT_STATUS_IS_OK(status)) {
713 tevent_req_nterror(req, status);
716 tevent_req_done(req);
719 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
724 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
727 if (tevent_req_is_nterror(req, &status)) {
730 *prb_size = (size_t)state->num_data;
731 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
735 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
741 TALLOC_CTX *frame = talloc_stackframe();
742 struct event_context *ev = NULL;
743 struct tevent_req *req = NULL;
744 NTSTATUS status = NT_STATUS_OK;
746 if (cli_has_async_calls(cli)) {
748 * Can't use sync call while an async call is in flight
750 status = NT_STATUS_INVALID_PARAMETER;
754 ev = event_context_init(frame);
756 status = NT_STATUS_NO_MEMORY;
760 req = cli_posix_getfacl_send(frame,
765 status = NT_STATUS_NO_MEMORY;
769 if (!tevent_req_poll(req, ev)) {
770 status = map_nt_error_from_unix(errno);
774 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
778 if (!NT_STATUS_IS_OK(status)) {
779 cli_set_error(cli, status);
784 /****************************************************************************
785 Stat a file (UNIX extensions).
786 ****************************************************************************/
793 static void cli_posix_stat_done(struct tevent_req *subreq);
795 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
796 struct event_context *ev,
797 struct cli_state *cli,
800 struct tevent_req *req = NULL, *subreq = NULL;
801 struct stat_state *state = NULL;
803 req = tevent_req_create(mem_ctx, &state, struct stat_state);
807 subreq = cli_qpathinfo_send(state, ev, cli, fname,
808 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
809 if (tevent_req_nomem(subreq, req)) {
810 return tevent_req_post(req, ev);
812 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
816 static void cli_posix_stat_done(struct tevent_req *subreq)
818 struct tevent_req *req = tevent_req_callback_data(
819 subreq, struct tevent_req);
820 struct stat_state *state = tevent_req_data(req, struct stat_state);
823 status = cli_qpathinfo_recv(subreq, state, &state->data,
826 if (!NT_STATUS_IS_OK(status)) {
827 tevent_req_nterror(req, status);
830 tevent_req_done(req);
833 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
834 SMB_STRUCT_STAT *sbuf)
836 struct stat_state *state = tevent_req_data(req, struct stat_state);
839 if (tevent_req_is_nterror(req, &status)) {
843 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
844 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
845 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
846 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
848 /* assume 512 byte blocks */
849 sbuf->st_ex_blocks /= 512;
851 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
852 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
853 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
855 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
856 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
857 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
858 #if defined(HAVE_MAKEDEV)
860 uint32_t dev_major = IVAL(state->data,60);
861 uint32_t dev_minor = IVAL(state->data,68);
862 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
865 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
866 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
867 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
872 NTSTATUS cli_posix_stat(struct cli_state *cli,
874 SMB_STRUCT_STAT *sbuf)
876 TALLOC_CTX *frame = talloc_stackframe();
877 struct event_context *ev = NULL;
878 struct tevent_req *req = NULL;
879 NTSTATUS status = NT_STATUS_OK;
881 if (cli_has_async_calls(cli)) {
883 * Can't use sync call while an async call is in flight
885 status = NT_STATUS_INVALID_PARAMETER;
889 ev = event_context_init(frame);
891 status = NT_STATUS_NO_MEMORY;
895 req = cli_posix_stat_send(frame,
900 status = NT_STATUS_NO_MEMORY;
904 if (!tevent_req_poll(req, ev)) {
905 status = map_nt_error_from_unix(errno);
909 status = cli_posix_stat_recv(req, sbuf);
913 if (!NT_STATUS_IS_OK(status)) {
914 cli_set_error(cli, status);
919 /****************************************************************************
920 Chmod or chown a file internal (UNIX extensions).
921 ****************************************************************************/
923 struct cli_posix_chown_chmod_internal_state {
927 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
929 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
930 struct event_context *ev,
931 struct cli_state *cli,
937 struct tevent_req *req = NULL, *subreq = NULL;
938 struct cli_posix_chown_chmod_internal_state *state = NULL;
940 req = tevent_req_create(mem_ctx, &state,
941 struct cli_posix_chown_chmod_internal_state);
946 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
947 memset(&state->data[40], '\0', 60);
948 SIVAL(state->data,40,uid);
949 SIVAL(state->data,48,gid);
950 SIVAL(state->data,84,mode);
952 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
953 fname, state->data, sizeof(state->data));
954 if (tevent_req_nomem(subreq, req)) {
955 return tevent_req_post(req, ev);
957 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
962 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
964 NTSTATUS status = cli_setpathinfo_recv(subreq);
965 tevent_req_simple_finish_ntstatus(subreq, status);
968 /****************************************************************************
969 chmod a file (UNIX extensions).
970 ****************************************************************************/
972 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
973 struct event_context *ev,
974 struct cli_state *cli,
978 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
980 unix_perms_to_wire(mode),
985 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
987 return tevent_req_simple_recv_ntstatus(req);
990 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
992 TALLOC_CTX *frame = talloc_stackframe();
993 struct event_context *ev = NULL;
994 struct tevent_req *req = NULL;
995 NTSTATUS status = NT_STATUS_OK;
997 if (cli_has_async_calls(cli)) {
999 * Can't use sync call while an async call is in flight
1001 status = NT_STATUS_INVALID_PARAMETER;
1005 ev = event_context_init(frame);
1007 status = NT_STATUS_NO_MEMORY;
1011 req = cli_posix_chmod_send(frame,
1017 status = NT_STATUS_NO_MEMORY;
1021 if (!tevent_req_poll(req, ev)) {
1022 status = map_nt_error_from_unix(errno);
1026 status = cli_posix_chmod_recv(req);
1030 if (!NT_STATUS_IS_OK(status)) {
1031 cli_set_error(cli, status);
1036 /****************************************************************************
1037 chown a file (UNIX extensions).
1038 ****************************************************************************/
1040 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1041 struct event_context *ev,
1042 struct cli_state *cli,
1047 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1054 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1056 return tevent_req_simple_recv_ntstatus(req);
1059 NTSTATUS cli_posix_chown(struct cli_state *cli,
1064 TALLOC_CTX *frame = talloc_stackframe();
1065 struct event_context *ev = NULL;
1066 struct tevent_req *req = NULL;
1067 NTSTATUS status = NT_STATUS_OK;
1069 if (cli_has_async_calls(cli)) {
1071 * Can't use sync call while an async call is in flight
1073 status = NT_STATUS_INVALID_PARAMETER;
1077 ev = event_context_init(frame);
1079 status = NT_STATUS_NO_MEMORY;
1083 req = cli_posix_chown_send(frame,
1090 status = NT_STATUS_NO_MEMORY;
1094 if (!tevent_req_poll(req, ev)) {
1095 status = map_nt_error_from_unix(errno);
1099 status = cli_posix_chown_recv(req);
1103 if (!NT_STATUS_IS_OK(status)) {
1104 cli_set_error(cli, status);
1109 /****************************************************************************
1111 ****************************************************************************/
1113 static void cli_rename_done(struct tevent_req *subreq);
1115 struct cli_rename_state {
1119 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1120 struct event_context *ev,
1121 struct cli_state *cli,
1122 const char *fname_src,
1123 const char *fname_dst)
1125 struct tevent_req *req = NULL, *subreq = NULL;
1126 struct cli_rename_state *state = NULL;
1127 uint8_t additional_flags = 0;
1128 uint8_t *bytes = NULL;
1130 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1135 SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
1137 bytes = talloc_array(state, uint8_t, 1);
1138 if (tevent_req_nomem(bytes, req)) {
1139 return tevent_req_post(req, ev);
1142 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1143 strlen(fname_src)+1, NULL);
1144 if (tevent_req_nomem(bytes, req)) {
1145 return tevent_req_post(req, ev);
1148 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1149 talloc_get_size(bytes)+1);
1150 if (tevent_req_nomem(bytes, req)) {
1151 return tevent_req_post(req, ev);
1154 bytes[talloc_get_size(bytes)-1] = 4;
1155 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1156 strlen(fname_dst)+1, NULL);
1157 if (tevent_req_nomem(bytes, req)) {
1158 return tevent_req_post(req, ev);
1161 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1162 1, state->vwv, talloc_get_size(bytes), bytes);
1163 if (tevent_req_nomem(subreq, req)) {
1164 return tevent_req_post(req, ev);
1166 tevent_req_set_callback(subreq, cli_rename_done, req);
1170 static void cli_rename_done(struct tevent_req *subreq)
1172 struct tevent_req *req = tevent_req_callback_data(
1173 subreq, struct tevent_req);
1176 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1177 TALLOC_FREE(subreq);
1178 if (!NT_STATUS_IS_OK(status)) {
1179 tevent_req_nterror(req, status);
1182 tevent_req_done(req);
1185 NTSTATUS cli_rename_recv(struct tevent_req *req)
1187 return tevent_req_simple_recv_ntstatus(req);
1190 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1192 TALLOC_CTX *frame = talloc_stackframe();
1193 struct event_context *ev;
1194 struct tevent_req *req;
1195 NTSTATUS status = NT_STATUS_OK;
1197 if (cli_has_async_calls(cli)) {
1199 * Can't use sync call while an async call is in flight
1201 status = NT_STATUS_INVALID_PARAMETER;
1205 ev = event_context_init(frame);
1207 status = NT_STATUS_NO_MEMORY;
1211 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1213 status = NT_STATUS_NO_MEMORY;
1217 if (!tevent_req_poll(req, ev)) {
1218 status = map_nt_error_from_unix(errno);
1222 status = cli_rename_recv(req);
1226 if (!NT_STATUS_IS_OK(status)) {
1227 cli_set_error(cli, status);
1232 /****************************************************************************
1234 ****************************************************************************/
1236 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1238 struct cli_ntrename_internal_state {
1242 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1243 struct event_context *ev,
1244 struct cli_state *cli,
1245 const char *fname_src,
1246 const char *fname_dst,
1247 uint16_t rename_flag)
1249 struct tevent_req *req = NULL, *subreq = NULL;
1250 struct cli_ntrename_internal_state *state = NULL;
1251 uint8_t additional_flags = 0;
1252 uint8_t *bytes = NULL;
1254 req = tevent_req_create(mem_ctx, &state,
1255 struct cli_ntrename_internal_state);
1260 SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
1261 SSVAL(state->vwv+1, 0, rename_flag);
1263 bytes = talloc_array(state, uint8_t, 1);
1264 if (tevent_req_nomem(bytes, req)) {
1265 return tevent_req_post(req, ev);
1268 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1269 strlen(fname_src)+1, NULL);
1270 if (tevent_req_nomem(bytes, req)) {
1271 return tevent_req_post(req, ev);
1274 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1275 talloc_get_size(bytes)+1);
1276 if (tevent_req_nomem(bytes, req)) {
1277 return tevent_req_post(req, ev);
1280 bytes[talloc_get_size(bytes)-1] = 4;
1281 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1282 strlen(fname_dst)+1, NULL);
1283 if (tevent_req_nomem(bytes, req)) {
1284 return tevent_req_post(req, ev);
1287 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1288 4, state->vwv, talloc_get_size(bytes), bytes);
1289 if (tevent_req_nomem(subreq, req)) {
1290 return tevent_req_post(req, ev);
1292 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1296 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1298 struct tevent_req *req = tevent_req_callback_data(
1299 subreq, struct tevent_req);
1302 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1303 TALLOC_FREE(subreq);
1304 if (!NT_STATUS_IS_OK(status)) {
1305 tevent_req_nterror(req, status);
1308 tevent_req_done(req);
1311 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1313 return tevent_req_simple_recv_ntstatus(req);
1316 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1317 struct event_context *ev,
1318 struct cli_state *cli,
1319 const char *fname_src,
1320 const char *fname_dst)
1322 return cli_ntrename_internal_send(mem_ctx,
1327 RENAME_FLAG_RENAME);
1330 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1332 return cli_ntrename_internal_recv(req);
1335 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1337 TALLOC_CTX *frame = talloc_stackframe();
1338 struct event_context *ev;
1339 struct tevent_req *req;
1340 NTSTATUS status = NT_STATUS_OK;
1342 if (cli_has_async_calls(cli)) {
1344 * Can't use sync call while an async call is in flight
1346 status = NT_STATUS_INVALID_PARAMETER;
1350 ev = event_context_init(frame);
1352 status = NT_STATUS_NO_MEMORY;
1356 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1358 status = NT_STATUS_NO_MEMORY;
1362 if (!tevent_req_poll(req, ev)) {
1363 status = map_nt_error_from_unix(errno);
1367 status = cli_ntrename_recv(req);
1371 if (!NT_STATUS_IS_OK(status)) {
1372 cli_set_error(cli, status);
1377 /****************************************************************************
1379 ****************************************************************************/
1381 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1382 struct event_context *ev,
1383 struct cli_state *cli,
1384 const char *fname_src,
1385 const char *fname_dst)
1387 return cli_ntrename_internal_send(mem_ctx,
1392 RENAME_FLAG_HARD_LINK);
1395 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1397 return cli_ntrename_internal_recv(req);
1400 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1402 TALLOC_CTX *frame = talloc_stackframe();
1403 struct event_context *ev;
1404 struct tevent_req *req;
1405 NTSTATUS status = NT_STATUS_OK;
1407 if (cli_has_async_calls(cli)) {
1409 * Can't use sync call while an async call is in flight
1411 status = NT_STATUS_INVALID_PARAMETER;
1415 ev = event_context_init(frame);
1417 status = NT_STATUS_NO_MEMORY;
1421 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1423 status = NT_STATUS_NO_MEMORY;
1427 if (!tevent_req_poll(req, ev)) {
1428 status = map_nt_error_from_unix(errno);
1432 status = cli_nt_hardlink_recv(req);
1436 if (!NT_STATUS_IS_OK(status)) {
1437 cli_set_error(cli, status);
1442 /****************************************************************************
1444 ****************************************************************************/
1446 static void cli_unlink_done(struct tevent_req *subreq);
1448 struct cli_unlink_state {
1452 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1453 struct event_context *ev,
1454 struct cli_state *cli,
1456 uint16_t mayhave_attrs)
1458 struct tevent_req *req = NULL, *subreq = NULL;
1459 struct cli_unlink_state *state = NULL;
1460 uint8_t additional_flags = 0;
1461 uint8_t *bytes = NULL;
1463 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1468 SSVAL(state->vwv+0, 0, mayhave_attrs);
1470 bytes = talloc_array(state, uint8_t, 1);
1471 if (tevent_req_nomem(bytes, req)) {
1472 return tevent_req_post(req, ev);
1475 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1476 strlen(fname)+1, NULL);
1478 if (tevent_req_nomem(bytes, req)) {
1479 return tevent_req_post(req, ev);
1482 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1483 1, state->vwv, talloc_get_size(bytes), bytes);
1484 if (tevent_req_nomem(subreq, req)) {
1485 return tevent_req_post(req, ev);
1487 tevent_req_set_callback(subreq, cli_unlink_done, req);
1491 static void cli_unlink_done(struct tevent_req *subreq)
1493 struct tevent_req *req = tevent_req_callback_data(
1494 subreq, struct tevent_req);
1497 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1498 TALLOC_FREE(subreq);
1499 if (!NT_STATUS_IS_OK(status)) {
1500 tevent_req_nterror(req, status);
1503 tevent_req_done(req);
1506 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1508 return tevent_req_simple_recv_ntstatus(req);
1511 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1513 TALLOC_CTX *frame = talloc_stackframe();
1514 struct event_context *ev;
1515 struct tevent_req *req;
1516 NTSTATUS status = NT_STATUS_OK;
1518 if (cli_has_async_calls(cli)) {
1520 * Can't use sync call while an async call is in flight
1522 status = NT_STATUS_INVALID_PARAMETER;
1526 ev = event_context_init(frame);
1528 status = NT_STATUS_NO_MEMORY;
1532 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1534 status = NT_STATUS_NO_MEMORY;
1538 if (!tevent_req_poll(req, ev)) {
1539 status = map_nt_error_from_unix(errno);
1543 status = cli_unlink_recv(req);
1547 if (!NT_STATUS_IS_OK(status)) {
1548 cli_set_error(cli, status);
1553 /****************************************************************************
1555 ****************************************************************************/
1557 static void cli_mkdir_done(struct tevent_req *subreq);
1559 struct cli_mkdir_state {
1563 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1564 struct event_context *ev,
1565 struct cli_state *cli,
1568 struct tevent_req *req = NULL, *subreq = NULL;
1569 struct cli_mkdir_state *state = NULL;
1570 uint8_t additional_flags = 0;
1571 uint8_t *bytes = NULL;
1573 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1578 bytes = talloc_array(state, uint8_t, 1);
1579 if (tevent_req_nomem(bytes, req)) {
1580 return tevent_req_post(req, ev);
1583 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1584 strlen(dname)+1, NULL);
1586 if (tevent_req_nomem(bytes, req)) {
1587 return tevent_req_post(req, ev);
1590 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1591 0, NULL, talloc_get_size(bytes), bytes);
1592 if (tevent_req_nomem(subreq, req)) {
1593 return tevent_req_post(req, ev);
1595 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1599 static void cli_mkdir_done(struct tevent_req *subreq)
1601 struct tevent_req *req = tevent_req_callback_data(
1602 subreq, struct tevent_req);
1605 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1606 TALLOC_FREE(subreq);
1607 if (!NT_STATUS_IS_OK(status)) {
1608 tevent_req_nterror(req, status);
1611 tevent_req_done(req);
1614 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1616 return tevent_req_simple_recv_ntstatus(req);
1619 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1621 TALLOC_CTX *frame = talloc_stackframe();
1622 struct event_context *ev;
1623 struct tevent_req *req;
1624 NTSTATUS status = NT_STATUS_OK;
1626 if (cli_has_async_calls(cli)) {
1628 * Can't use sync call while an async call is in flight
1630 status = NT_STATUS_INVALID_PARAMETER;
1634 ev = event_context_init(frame);
1636 status = NT_STATUS_NO_MEMORY;
1640 req = cli_mkdir_send(frame, ev, cli, dname);
1642 status = NT_STATUS_NO_MEMORY;
1646 if (!tevent_req_poll(req, ev)) {
1647 status = map_nt_error_from_unix(errno);
1651 status = cli_mkdir_recv(req);
1655 if (!NT_STATUS_IS_OK(status)) {
1656 cli_set_error(cli, status);
1661 /****************************************************************************
1663 ****************************************************************************/
1665 static void cli_rmdir_done(struct tevent_req *subreq);
1667 struct cli_rmdir_state {
1671 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1672 struct event_context *ev,
1673 struct cli_state *cli,
1676 struct tevent_req *req = NULL, *subreq = NULL;
1677 struct cli_rmdir_state *state = NULL;
1678 uint8_t additional_flags = 0;
1679 uint8_t *bytes = NULL;
1681 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1686 bytes = talloc_array(state, uint8_t, 1);
1687 if (tevent_req_nomem(bytes, req)) {
1688 return tevent_req_post(req, ev);
1691 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1692 strlen(dname)+1, NULL);
1694 if (tevent_req_nomem(bytes, req)) {
1695 return tevent_req_post(req, ev);
1698 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1699 0, NULL, talloc_get_size(bytes), bytes);
1700 if (tevent_req_nomem(subreq, req)) {
1701 return tevent_req_post(req, ev);
1703 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1707 static void cli_rmdir_done(struct tevent_req *subreq)
1709 struct tevent_req *req = tevent_req_callback_data(
1710 subreq, struct tevent_req);
1713 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1714 TALLOC_FREE(subreq);
1715 if (!NT_STATUS_IS_OK(status)) {
1716 tevent_req_nterror(req, status);
1719 tevent_req_done(req);
1722 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1724 return tevent_req_simple_recv_ntstatus(req);
1727 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1729 TALLOC_CTX *frame = talloc_stackframe();
1730 struct event_context *ev;
1731 struct tevent_req *req;
1732 NTSTATUS status = NT_STATUS_OK;
1734 if (cli_has_async_calls(cli)) {
1736 * Can't use sync call while an async call is in flight
1738 status = NT_STATUS_INVALID_PARAMETER;
1742 ev = event_context_init(frame);
1744 status = NT_STATUS_NO_MEMORY;
1748 req = cli_rmdir_send(frame, ev, cli, dname);
1750 status = NT_STATUS_NO_MEMORY;
1754 if (!tevent_req_poll(req, ev)) {
1755 status = map_nt_error_from_unix(errno);
1759 status = cli_rmdir_recv(req);
1763 if (!NT_STATUS_IS_OK(status)) {
1764 cli_set_error(cli, status);
1769 /****************************************************************************
1770 Set or clear the delete on close flag.
1771 ****************************************************************************/
1779 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1781 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1782 NULL, 0, NULL, NULL, 0, NULL);
1783 tevent_req_simple_finish_ntstatus(subreq, status);
1786 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1787 struct event_context *ev,
1788 struct cli_state *cli,
1792 struct tevent_req *req = NULL, *subreq = NULL;
1793 struct doc_state *state = NULL;
1795 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1800 /* Setup setup word. */
1801 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1803 /* Setup param array. */
1804 SSVAL(state->param,0,fnum);
1805 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1807 /* Setup data array. */
1808 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1810 subreq = cli_trans_send(state, /* mem ctx. */
1811 ev, /* event ctx. */
1812 cli, /* cli_state. */
1813 SMBtrans2, /* cmd. */
1814 NULL, /* pipe name. */
1818 &state->setup, /* setup. */
1819 1, /* num setup uint16_t words. */
1820 0, /* max returned setup. */
1821 state->param, /* param. */
1823 2, /* max returned param. */
1824 state->data, /* data. */
1826 0); /* max returned data. */
1828 if (tevent_req_nomem(subreq, req)) {
1829 return tevent_req_post(req, ev);
1831 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1835 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1837 return tevent_req_simple_recv_ntstatus(req);
1840 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1842 TALLOC_CTX *frame = talloc_stackframe();
1843 struct event_context *ev = NULL;
1844 struct tevent_req *req = NULL;
1845 NTSTATUS status = NT_STATUS_OK;
1847 if (cli_has_async_calls(cli)) {
1849 * Can't use sync call while an async call is in flight
1851 status = NT_STATUS_INVALID_PARAMETER;
1855 ev = event_context_init(frame);
1857 status = NT_STATUS_NO_MEMORY;
1861 req = cli_nt_delete_on_close_send(frame,
1867 status = NT_STATUS_NO_MEMORY;
1871 if (!tevent_req_poll(req, ev)) {
1872 status = map_nt_error_from_unix(errno);
1876 status = cli_nt_delete_on_close_recv(req);
1880 if (!NT_STATUS_IS_OK(status)) {
1881 cli_set_error(cli, status);
1886 struct cli_ntcreate_state {
1891 static void cli_ntcreate_done(struct tevent_req *subreq);
1893 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1894 struct event_context *ev,
1895 struct cli_state *cli,
1897 uint32_t CreatFlags,
1898 uint32_t DesiredAccess,
1899 uint32_t FileAttributes,
1900 uint32_t ShareAccess,
1901 uint32_t CreateDisposition,
1902 uint32_t CreateOptions,
1903 uint8_t SecurityFlags)
1905 struct tevent_req *req, *subreq;
1906 struct cli_ntcreate_state *state;
1909 size_t converted_len;
1911 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1918 SCVAL(vwv+0, 0, 0xFF);
1923 if (cli->use_oplocks) {
1924 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1926 SIVAL(vwv+3, 1, CreatFlags);
1927 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1928 SIVAL(vwv+7, 1, DesiredAccess);
1929 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1930 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1931 SIVAL(vwv+13, 1, FileAttributes);
1932 SIVAL(vwv+15, 1, ShareAccess);
1933 SIVAL(vwv+17, 1, CreateDisposition);
1934 SIVAL(vwv+19, 1, CreateOptions);
1935 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1936 SCVAL(vwv+23, 1, SecurityFlags);
1938 bytes = talloc_array(state, uint8_t, 0);
1939 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1940 fname, strlen(fname)+1,
1943 /* sigh. this copes with broken netapp filer behaviour */
1944 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1946 if (tevent_req_nomem(bytes, req)) {
1947 return tevent_req_post(req, ev);
1950 SSVAL(vwv+2, 1, converted_len);
1952 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1953 talloc_get_size(bytes), bytes);
1954 if (tevent_req_nomem(subreq, req)) {
1955 return tevent_req_post(req, ev);
1957 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1961 static void cli_ntcreate_done(struct tevent_req *subreq)
1963 struct tevent_req *req = tevent_req_callback_data(
1964 subreq, struct tevent_req);
1965 struct cli_ntcreate_state *state = tevent_req_data(
1966 req, struct cli_ntcreate_state);
1974 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1975 &num_bytes, &bytes);
1976 TALLOC_FREE(subreq);
1977 if (!NT_STATUS_IS_OK(status)) {
1978 tevent_req_nterror(req, status);
1981 state->fnum = SVAL(vwv+2, 1);
1982 tevent_req_done(req);
1985 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1987 struct cli_ntcreate_state *state = tevent_req_data(
1988 req, struct cli_ntcreate_state);
1991 if (tevent_req_is_nterror(req, &status)) {
1994 *pfnum = state->fnum;
1995 return NT_STATUS_OK;
1998 NTSTATUS cli_ntcreate(struct cli_state *cli,
2000 uint32_t CreatFlags,
2001 uint32_t DesiredAccess,
2002 uint32_t FileAttributes,
2003 uint32_t ShareAccess,
2004 uint32_t CreateDisposition,
2005 uint32_t CreateOptions,
2006 uint8_t SecurityFlags,
2009 TALLOC_CTX *frame = talloc_stackframe();
2010 struct event_context *ev;
2011 struct tevent_req *req;
2012 NTSTATUS status = NT_STATUS_OK;
2014 if (cli_has_async_calls(cli)) {
2016 * Can't use sync call while an async call is in flight
2018 status = NT_STATUS_INVALID_PARAMETER;
2022 ev = event_context_init(frame);
2024 status = NT_STATUS_NO_MEMORY;
2028 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2029 DesiredAccess, FileAttributes, ShareAccess,
2030 CreateDisposition, CreateOptions,
2033 status = NT_STATUS_NO_MEMORY;
2037 if (!tevent_req_poll(req, ev)) {
2038 status = map_nt_error_from_unix(errno);
2042 status = cli_ntcreate_recv(req, pfid);
2045 if (!NT_STATUS_IS_OK(status)) {
2046 cli_set_error(cli, status);
2051 /****************************************************************************
2053 WARNING: if you open with O_WRONLY then getattrE won't work!
2054 ****************************************************************************/
2056 struct cli_open_state {
2062 static void cli_open_done(struct tevent_req *subreq);
2064 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2065 struct event_context *ev,
2066 struct cli_state *cli, const char *fname,
2067 int flags, int share_mode,
2068 struct tevent_req **psmbreq)
2070 struct tevent_req *req, *subreq;
2071 struct cli_open_state *state;
2073 unsigned accessmode;
2074 uint8_t additional_flags;
2077 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2083 if (flags & O_CREAT) {
2086 if (!(flags & O_EXCL)) {
2087 if (flags & O_TRUNC)
2093 accessmode = (share_mode<<4);
2095 if ((flags & O_ACCMODE) == O_RDWR) {
2097 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2102 if ((flags & O_SYNC) == O_SYNC) {
2103 accessmode |= (1<<14);
2107 if (share_mode == DENY_FCB) {
2111 SCVAL(state->vwv + 0, 0, 0xFF);
2112 SCVAL(state->vwv + 0, 1, 0);
2113 SSVAL(state->vwv + 1, 0, 0);
2114 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2115 SSVAL(state->vwv + 3, 0, accessmode);
2116 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
2117 SSVAL(state->vwv + 5, 0, 0);
2118 SIVAL(state->vwv + 6, 0, 0);
2119 SSVAL(state->vwv + 8, 0, openfn);
2120 SIVAL(state->vwv + 9, 0, 0);
2121 SIVAL(state->vwv + 11, 0, 0);
2122 SIVAL(state->vwv + 13, 0, 0);
2124 additional_flags = 0;
2126 if (cli->use_oplocks) {
2127 /* if using oplocks then ask for a batch oplock via
2128 core and extended methods */
2130 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2131 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2134 bytes = talloc_array(state, uint8_t, 0);
2135 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2136 strlen(fname)+1, NULL);
2138 if (tevent_req_nomem(bytes, req)) {
2139 return tevent_req_post(req, ev);
2142 state->bytes.iov_base = (void *)bytes;
2143 state->bytes.iov_len = talloc_get_size(bytes);
2145 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2146 15, state->vwv, 1, &state->bytes);
2147 if (subreq == NULL) {
2151 tevent_req_set_callback(subreq, cli_open_done, req);
2156 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2157 struct cli_state *cli, const char *fname,
2158 int flags, int share_mode)
2160 struct tevent_req *req, *subreq;
2163 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2169 status = cli_smb_req_send(subreq);
2170 if (!NT_STATUS_IS_OK(status)) {
2171 tevent_req_nterror(req, status);
2172 return tevent_req_post(req, ev);
2177 static void cli_open_done(struct tevent_req *subreq)
2179 struct tevent_req *req = tevent_req_callback_data(
2180 subreq, struct tevent_req);
2181 struct cli_open_state *state = tevent_req_data(
2182 req, struct cli_open_state);
2188 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2190 TALLOC_FREE(subreq);
2191 if (!NT_STATUS_IS_OK(status)) {
2192 tevent_req_nterror(req, status);
2195 state->fnum = SVAL(vwv+2, 0);
2196 tevent_req_done(req);
2199 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2201 struct cli_open_state *state = tevent_req_data(
2202 req, struct cli_open_state);
2205 if (tevent_req_is_nterror(req, &status)) {
2208 *pfnum = state->fnum;
2209 return NT_STATUS_OK;
2212 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2213 int share_mode, uint16_t *pfnum)
2215 TALLOC_CTX *frame = talloc_stackframe();
2216 struct event_context *ev;
2217 struct tevent_req *req;
2218 NTSTATUS status = NT_STATUS_OK;
2220 if (cli_has_async_calls(cli)) {
2222 * Can't use sync call while an async call is in flight
2224 status = NT_STATUS_INVALID_PARAMETER;
2228 ev = event_context_init(frame);
2230 status = NT_STATUS_NO_MEMORY;
2234 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2236 status = NT_STATUS_NO_MEMORY;
2240 if (!tevent_req_poll(req, ev)) {
2241 status = map_nt_error_from_unix(errno);
2245 status = cli_open_recv(req, pfnum);
2248 if (!NT_STATUS_IS_OK(status)) {
2249 cli_set_error(cli, status);
2254 /****************************************************************************
2256 ****************************************************************************/
2258 struct cli_close_state {
2262 static void cli_close_done(struct tevent_req *subreq);
2264 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2265 struct event_context *ev,
2266 struct cli_state *cli,
2268 struct tevent_req **psubreq)
2270 struct tevent_req *req, *subreq;
2271 struct cli_close_state *state;
2273 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2278 SSVAL(state->vwv+0, 0, fnum);
2279 SIVALS(state->vwv+1, 0, -1);
2281 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2283 if (subreq == NULL) {
2287 tevent_req_set_callback(subreq, cli_close_done, req);
2292 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2293 struct event_context *ev,
2294 struct cli_state *cli,
2297 struct tevent_req *req, *subreq;
2300 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2305 status = cli_smb_req_send(subreq);
2306 if (!NT_STATUS_IS_OK(status)) {
2307 tevent_req_nterror(req, status);
2308 return tevent_req_post(req, ev);
2313 static void cli_close_done(struct tevent_req *subreq)
2315 struct tevent_req *req = tevent_req_callback_data(
2316 subreq, struct tevent_req);
2319 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2320 TALLOC_FREE(subreq);
2321 if (!NT_STATUS_IS_OK(status)) {
2322 tevent_req_nterror(req, status);
2325 tevent_req_done(req);
2328 NTSTATUS cli_close_recv(struct tevent_req *req)
2330 return tevent_req_simple_recv_ntstatus(req);
2333 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2335 TALLOC_CTX *frame = talloc_stackframe();
2336 struct event_context *ev;
2337 struct tevent_req *req;
2338 NTSTATUS status = NT_STATUS_OK;
2340 if (cli_has_async_calls(cli)) {
2342 * Can't use sync call while an async call is in flight
2344 status = NT_STATUS_INVALID_PARAMETER;
2348 ev = event_context_init(frame);
2350 status = NT_STATUS_NO_MEMORY;
2354 req = cli_close_send(frame, ev, cli, fnum);
2356 status = NT_STATUS_NO_MEMORY;
2360 if (!tevent_req_poll(req, ev)) {
2361 status = map_nt_error_from_unix(errno);
2365 status = cli_close_recv(req);
2368 if (!NT_STATUS_IS_OK(status)) {
2369 cli_set_error(cli, status);
2374 /****************************************************************************
2375 Truncate a file to a specified size
2376 ****************************************************************************/
2378 struct ftrunc_state {
2384 static void cli_ftruncate_done(struct tevent_req *subreq)
2386 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2387 NULL, 0, NULL, NULL, 0, NULL);
2388 tevent_req_simple_finish_ntstatus(subreq, status);
2391 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2392 struct event_context *ev,
2393 struct cli_state *cli,
2397 struct tevent_req *req = NULL, *subreq = NULL;
2398 struct ftrunc_state *state = NULL;
2400 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2405 /* Setup setup word. */
2406 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2408 /* Setup param array. */
2409 SSVAL(state->param,0,fnum);
2410 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2411 SSVAL(state->param,4,0);
2413 /* Setup data array. */
2414 SBVAL(state->data, 0, size);
2416 subreq = cli_trans_send(state, /* mem ctx. */
2417 ev, /* event ctx. */
2418 cli, /* cli_state. */
2419 SMBtrans2, /* cmd. */
2420 NULL, /* pipe name. */
2424 &state->setup, /* setup. */
2425 1, /* num setup uint16_t words. */
2426 0, /* max returned setup. */
2427 state->param, /* param. */
2429 2, /* max returned param. */
2430 state->data, /* data. */
2432 0); /* max returned data. */
2434 if (tevent_req_nomem(subreq, req)) {
2435 return tevent_req_post(req, ev);
2437 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2441 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2443 return tevent_req_simple_recv_ntstatus(req);
2446 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2448 TALLOC_CTX *frame = talloc_stackframe();
2449 struct event_context *ev = NULL;
2450 struct tevent_req *req = NULL;
2451 NTSTATUS status = NT_STATUS_OK;
2453 if (cli_has_async_calls(cli)) {
2455 * Can't use sync call while an async call is in flight
2457 status = NT_STATUS_INVALID_PARAMETER;
2461 ev = event_context_init(frame);
2463 status = NT_STATUS_NO_MEMORY;
2467 req = cli_ftruncate_send(frame,
2473 status = NT_STATUS_NO_MEMORY;
2477 if (!tevent_req_poll(req, ev)) {
2478 status = map_nt_error_from_unix(errno);
2482 status = cli_ftruncate_recv(req);
2486 if (!NT_STATUS_IS_OK(status)) {
2487 cli_set_error(cli, status);
2492 /****************************************************************************
2493 send a lock with a specified locktype
2494 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2495 ****************************************************************************/
2497 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2498 uint32_t offset, uint32_t len,
2499 int timeout, unsigned char locktype)
2506 SCVAL(vwv + 0, 0, 0xff);
2507 SCVAL(vwv + 0, 1, 0);
2508 SSVAL(vwv + 1, 0, 0);
2509 SSVAL(vwv + 2, 0, fnum);
2510 SCVAL(vwv + 3, 0, locktype);
2511 SCVAL(vwv + 3, 1, 0);
2512 SIVALS(vwv + 4, 0, timeout);
2513 SSVAL(vwv + 6, 0, 0);
2514 SSVAL(vwv + 7, 0, 1);
2516 SSVAL(bytes, 0, cli->pid);
2517 SIVAL(bytes, 2, offset);
2518 SIVAL(bytes, 6, len);
2520 saved_timeout = cli->timeout;
2523 cli->timeout = (timeout == -1)
2524 ? 0x7FFFFFFF : (timeout + 2*1000);
2527 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2528 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2530 cli->timeout = saved_timeout;
2535 /****************************************************************************
2537 note that timeout is in units of 2 milliseconds
2538 ****************************************************************************/
2540 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2541 uint32_t offset, uint32_t len, int timeout,
2542 enum brl_type lock_type)
2546 status = cli_locktype(cli, fnum, offset, len, timeout,
2547 (lock_type == READ_LOCK? 1 : 0));
2548 cli_set_error(cli, status);
2549 return NT_STATUS_IS_OK(status);
2552 /****************************************************************************
2554 ****************************************************************************/
2556 struct cli_unlock_state {
2561 static void cli_unlock_done(struct tevent_req *subreq);
2563 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2564 struct event_context *ev,
2565 struct cli_state *cli,
2571 struct tevent_req *req = NULL, *subreq = NULL;
2572 struct cli_unlock_state *state = NULL;
2573 uint8_t additional_flags = 0;
2575 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2580 SCVAL(state->vwv+0, 0, 0xFF);
2581 SSVAL(state->vwv+2, 0, fnum);
2582 SCVAL(state->vwv+3, 0, 0);
2583 SIVALS(state->vwv+4, 0, 0);
2584 SSVAL(state->vwv+6, 0, 1);
2585 SSVAL(state->vwv+7, 0, 0);
2587 SSVAL(state->data, 0, cli->pid);
2588 SIVAL(state->data, 2, offset);
2589 SIVAL(state->data, 6, len);
2591 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2592 8, state->vwv, 10, state->data);
2593 if (tevent_req_nomem(subreq, req)) {
2594 return tevent_req_post(req, ev);
2596 tevent_req_set_callback(subreq, cli_unlock_done, req);
2600 static void cli_unlock_done(struct tevent_req *subreq)
2602 struct tevent_req *req = tevent_req_callback_data(
2603 subreq, struct tevent_req);
2606 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2607 TALLOC_FREE(subreq);
2608 if (!NT_STATUS_IS_OK(status)) {
2609 tevent_req_nterror(req, status);
2612 tevent_req_done(req);
2615 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2617 return tevent_req_simple_recv_ntstatus(req);
2620 NTSTATUS cli_unlock(struct cli_state *cli,
2625 TALLOC_CTX *frame = talloc_stackframe();
2626 struct event_context *ev;
2627 struct tevent_req *req;
2628 NTSTATUS status = NT_STATUS_OK;
2630 if (cli_has_async_calls(cli)) {
2632 * Can't use sync call while an async call is in flight
2634 status = NT_STATUS_INVALID_PARAMETER;
2638 ev = event_context_init(frame);
2640 status = NT_STATUS_NO_MEMORY;
2644 req = cli_unlock_send(frame, ev, cli,
2647 status = NT_STATUS_NO_MEMORY;
2651 if (!tevent_req_poll(req, ev)) {
2652 status = map_nt_error_from_unix(errno);
2656 status = cli_unlock_recv(req);
2660 if (!NT_STATUS_IS_OK(status)) {
2661 cli_set_error(cli, status);
2666 /****************************************************************************
2667 Lock a file with 64 bit offsets.
2668 ****************************************************************************/
2670 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2671 uint64_t offset, uint64_t len, int timeout,
2672 enum brl_type lock_type)
2676 int saved_timeout = cli->timeout;
2680 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2681 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2684 ltype = (lock_type == READ_LOCK? 1 : 0);
2685 ltype |= LOCKING_ANDX_LARGE_FILES;
2687 SCVAL(vwv + 0, 0, 0xff);
2688 SCVAL(vwv + 0, 1, 0);
2689 SSVAL(vwv + 1, 0, 0);
2690 SSVAL(vwv + 2, 0, fnum);
2691 SCVAL(vwv + 3, 0, ltype);
2692 SCVAL(vwv + 3, 1, 0);
2693 SIVALS(vwv + 4, 0, timeout);
2694 SSVAL(vwv + 6, 0, 0);
2695 SSVAL(vwv + 7, 0, 1);
2697 SIVAL(bytes, 0, cli->pid);
2698 SOFF_T_R(bytes, 4, offset);
2699 SOFF_T_R(bytes, 12, len);
2701 saved_timeout = cli->timeout;
2704 cli->timeout = (timeout == -1)
2705 ? 0x7FFFFFFF : (timeout + 2*1000);
2708 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2709 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2711 cli->timeout = saved_timeout;
2713 cli_set_error(cli, status);
2714 return NT_STATUS_IS_OK(status);
2717 /****************************************************************************
2718 Unlock a file with 64 bit offsets.
2719 ****************************************************************************/
2721 struct cli_unlock64_state {
2726 static void cli_unlock64_done(struct tevent_req *subreq);
2728 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2729 struct event_context *ev,
2730 struct cli_state *cli,
2736 struct tevent_req *req = NULL, *subreq = NULL;
2737 struct cli_unlock64_state *state = NULL;
2738 uint8_t additional_flags = 0;
2740 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2745 SCVAL(state->vwv+0, 0, 0xff);
2746 SSVAL(state->vwv+2, 0, fnum);
2747 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2748 SIVALS(state->vwv+4, 0, 0);
2749 SSVAL(state->vwv+6, 0, 1);
2750 SSVAL(state->vwv+7, 0, 0);
2752 SIVAL(state->data, 0, cli->pid);
2753 SOFF_T_R(state->data, 4, offset);
2754 SOFF_T_R(state->data, 12, len);
2756 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2757 8, state->vwv, 20, state->data);
2758 if (tevent_req_nomem(subreq, req)) {
2759 return tevent_req_post(req, ev);
2761 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2765 static void cli_unlock64_done(struct tevent_req *subreq)
2767 struct tevent_req *req = tevent_req_callback_data(
2768 subreq, struct tevent_req);
2771 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2772 TALLOC_FREE(subreq);
2773 if (!NT_STATUS_IS_OK(status)) {
2774 tevent_req_nterror(req, status);
2777 tevent_req_done(req);
2780 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2782 return tevent_req_simple_recv_ntstatus(req);
2785 NTSTATUS cli_unlock64(struct cli_state *cli,
2790 TALLOC_CTX *frame = talloc_stackframe();
2791 struct event_context *ev;
2792 struct tevent_req *req;
2793 NTSTATUS status = NT_STATUS_OK;
2795 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2796 return cli_unlock(cli, fnum, offset, len);
2799 if (cli_has_async_calls(cli)) {
2801 * Can't use sync call while an async call is in flight
2803 status = NT_STATUS_INVALID_PARAMETER;
2807 ev = event_context_init(frame);
2809 status = NT_STATUS_NO_MEMORY;
2813 req = cli_unlock64_send(frame, ev, cli,
2816 status = NT_STATUS_NO_MEMORY;
2820 if (!tevent_req_poll(req, ev)) {
2821 status = map_nt_error_from_unix(errno);
2825 status = cli_unlock64_recv(req);
2829 if (!NT_STATUS_IS_OK(status)) {
2830 cli_set_error(cli, status);
2835 /****************************************************************************
2836 Get/unlock a POSIX lock on a file - internal function.
2837 ****************************************************************************/
2839 struct posix_lock_state {
2842 uint8_t data[POSIX_LOCK_DATA_SIZE];
2845 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2847 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2848 NULL, 0, NULL, NULL, 0, NULL);
2849 tevent_req_simple_finish_ntstatus(subreq, status);
2852 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2853 struct event_context *ev,
2854 struct cli_state *cli,
2859 enum brl_type lock_type)
2861 struct tevent_req *req = NULL, *subreq = NULL;
2862 struct posix_lock_state *state = NULL;
2864 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2869 /* Setup setup word. */
2870 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2872 /* Setup param array. */
2873 SSVAL(&state->param, 0, fnum);
2874 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2876 /* Setup data array. */
2877 switch (lock_type) {
2879 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2880 POSIX_LOCK_TYPE_READ);
2883 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2884 POSIX_LOCK_TYPE_WRITE);
2887 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2888 POSIX_LOCK_TYPE_UNLOCK);
2895 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2896 POSIX_LOCK_FLAG_WAIT);
2898 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2899 POSIX_LOCK_FLAG_NOWAIT);
2902 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
2903 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2904 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2906 subreq = cli_trans_send(state, /* mem ctx. */
2907 ev, /* event ctx. */
2908 cli, /* cli_state. */
2909 SMBtrans2, /* cmd. */
2910 NULL, /* pipe name. */
2914 &state->setup, /* setup. */
2915 1, /* num setup uint16_t words. */
2916 0, /* max returned setup. */
2917 state->param, /* param. */
2919 2, /* max returned param. */
2920 state->data, /* data. */
2921 POSIX_LOCK_DATA_SIZE, /* num data. */
2922 0); /* max returned data. */
2924 if (tevent_req_nomem(subreq, req)) {
2925 return tevent_req_post(req, ev);
2927 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
2931 /****************************************************************************
2933 ****************************************************************************/
2935 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
2936 struct event_context *ev,
2937 struct cli_state *cli,
2942 enum brl_type lock_type)
2944 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2945 wait_lock, lock_type);
2948 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
2950 return tevent_req_simple_recv_ntstatus(req);
2953 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2954 uint64_t offset, uint64_t len,
2955 bool wait_lock, enum brl_type lock_type)
2957 TALLOC_CTX *frame = talloc_stackframe();
2958 struct event_context *ev = NULL;
2959 struct tevent_req *req = NULL;
2960 NTSTATUS status = NT_STATUS_OK;
2962 if (cli_has_async_calls(cli)) {
2964 * Can't use sync call while an async call is in flight
2966 status = NT_STATUS_INVALID_PARAMETER;
2970 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2971 status = NT_STATUS_INVALID_PARAMETER;
2975 ev = event_context_init(frame);
2977 status = NT_STATUS_NO_MEMORY;
2981 req = cli_posix_lock_send(frame,
2990 status = NT_STATUS_NO_MEMORY;
2994 if (!tevent_req_poll(req, ev)) {
2995 status = map_nt_error_from_unix(errno);
2999 status = cli_posix_lock_recv(req);
3003 if (!NT_STATUS_IS_OK(status)) {
3004 cli_set_error(cli, status);
3009 /****************************************************************************
3010 POSIX Unlock a file.
3011 ****************************************************************************/
3013 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3014 struct event_context *ev,
3015 struct cli_state *cli,
3020 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3021 false, UNLOCK_LOCK);
3024 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3026 return tevent_req_simple_recv_ntstatus(req);
3029 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3031 TALLOC_CTX *frame = talloc_stackframe();
3032 struct event_context *ev = NULL;
3033 struct tevent_req *req = NULL;
3034 NTSTATUS status = NT_STATUS_OK;
3036 if (cli_has_async_calls(cli)) {
3038 * Can't use sync call while an async call is in flight
3040 status = NT_STATUS_INVALID_PARAMETER;
3044 ev = event_context_init(frame);
3046 status = NT_STATUS_NO_MEMORY;
3050 req = cli_posix_unlock_send(frame,
3057 status = NT_STATUS_NO_MEMORY;
3061 if (!tevent_req_poll(req, ev)) {
3062 status = map_nt_error_from_unix(errno);
3066 status = cli_posix_unlock_recv(req);
3070 if (!NT_STATUS_IS_OK(status)) {
3071 cli_set_error(cli, status);
3076 /****************************************************************************
3077 Do a SMBgetattrE call.
3078 ****************************************************************************/
3080 static void cli_getattrE_done(struct tevent_req *subreq);
3082 struct cli_getattrE_state {
3092 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3093 struct event_context *ev,
3094 struct cli_state *cli,
3097 struct tevent_req *req = NULL, *subreq = NULL;
3098 struct cli_getattrE_state *state = NULL;
3099 uint8_t additional_flags = 0;
3101 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3106 state->zone_offset = cli->serverzone;
3107 SSVAL(state->vwv+0,0,fnum);
3109 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3110 1, state->vwv, 0, NULL);
3111 if (tevent_req_nomem(subreq, req)) {
3112 return tevent_req_post(req, ev);
3114 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3118 static void cli_getattrE_done(struct tevent_req *subreq)
3120 struct tevent_req *req = tevent_req_callback_data(
3121 subreq, struct tevent_req);
3122 struct cli_getattrE_state *state = tevent_req_data(
3123 req, struct cli_getattrE_state);
3125 uint16_t *vwv = NULL;
3129 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3131 TALLOC_FREE(subreq);
3132 if (!NT_STATUS_IS_OK(status)) {
3133 tevent_req_nterror(req, status);
3137 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3138 state->attr = SVAL(vwv+10,0);
3139 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3140 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3141 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3143 tevent_req_done(req);
3146 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3149 time_t *change_time,
3150 time_t *access_time,
3153 struct cli_getattrE_state *state = tevent_req_data(
3154 req, struct cli_getattrE_state);
3157 if (tevent_req_is_nterror(req, &status)) {
3161 *attr = state->attr;
3164 *size = state->size;
3167 *change_time = state->change_time;
3170 *access_time = state->access_time;
3173 *write_time = state->write_time;
3175 return NT_STATUS_OK;
3178 NTSTATUS cli_getattrE(struct cli_state *cli,
3182 time_t *change_time,
3183 time_t *access_time,
3186 TALLOC_CTX *frame = talloc_stackframe();
3187 struct event_context *ev = NULL;
3188 struct tevent_req *req = NULL;
3189 NTSTATUS status = NT_STATUS_OK;
3191 if (cli_has_async_calls(cli)) {
3193 * Can't use sync call while an async call is in flight
3195 status = NT_STATUS_INVALID_PARAMETER;
3199 ev = event_context_init(frame);
3201 status = NT_STATUS_NO_MEMORY;
3205 req = cli_getattrE_send(frame, ev, cli, fnum);
3207 status = NT_STATUS_NO_MEMORY;
3211 if (!tevent_req_poll(req, ev)) {
3212 status = map_nt_error_from_unix(errno);
3216 status = cli_getattrE_recv(req,
3225 if (!NT_STATUS_IS_OK(status)) {
3226 cli_set_error(cli, status);
3231 /****************************************************************************
3233 ****************************************************************************/
3235 static void cli_getatr_done(struct tevent_req *subreq);
3237 struct cli_getatr_state {
3244 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3245 struct event_context *ev,
3246 struct cli_state *cli,
3249 struct tevent_req *req = NULL, *subreq = NULL;
3250 struct cli_getatr_state *state = NULL;
3251 uint8_t additional_flags = 0;
3252 uint8_t *bytes = NULL;
3254 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3259 state->zone_offset = cli->serverzone;
3261 bytes = talloc_array(state, uint8_t, 1);
3262 if (tevent_req_nomem(bytes, req)) {
3263 return tevent_req_post(req, ev);
3266 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3267 strlen(fname)+1, NULL);
3269 if (tevent_req_nomem(bytes, req)) {
3270 return tevent_req_post(req, ev);
3273 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3274 0, NULL, talloc_get_size(bytes), bytes);
3275 if (tevent_req_nomem(subreq, req)) {
3276 return tevent_req_post(req, ev);
3278 tevent_req_set_callback(subreq, cli_getatr_done, req);
3282 static void cli_getatr_done(struct tevent_req *subreq)
3284 struct tevent_req *req = tevent_req_callback_data(
3285 subreq, struct tevent_req);
3286 struct cli_getatr_state *state = tevent_req_data(
3287 req, struct cli_getatr_state);
3289 uint16_t *vwv = NULL;
3293 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3295 TALLOC_FREE(subreq);
3296 if (!NT_STATUS_IS_OK(status)) {
3297 tevent_req_nterror(req, status);
3301 state->attr = SVAL(vwv+0,0);
3302 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3303 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3305 tevent_req_done(req);
3308 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3313 struct cli_getatr_state *state = tevent_req_data(
3314 req, struct cli_getatr_state);
3317 if (tevent_req_is_nterror(req, &status)) {
3321 *attr = state->attr;
3324 *size = state->size;
3327 *write_time = state->write_time;
3329 return NT_STATUS_OK;
3332 NTSTATUS cli_getatr(struct cli_state *cli,
3338 TALLOC_CTX *frame = talloc_stackframe();
3339 struct event_context *ev = NULL;
3340 struct tevent_req *req = NULL;
3341 NTSTATUS status = NT_STATUS_OK;
3343 if (cli_has_async_calls(cli)) {
3345 * Can't use sync call while an async call is in flight
3347 status = NT_STATUS_INVALID_PARAMETER;
3351 ev = event_context_init(frame);
3353 status = NT_STATUS_NO_MEMORY;
3357 req = cli_getatr_send(frame, ev, cli, fname);
3359 status = NT_STATUS_NO_MEMORY;
3363 if (!tevent_req_poll(req, ev)) {
3364 status = map_nt_error_from_unix(errno);
3368 status = cli_getatr_recv(req,
3375 if (!NT_STATUS_IS_OK(status)) {
3376 cli_set_error(cli, status);
3381 /****************************************************************************
3382 Do a SMBsetattrE call.
3383 ****************************************************************************/
3385 static void cli_setattrE_done(struct tevent_req *subreq);
3387 struct cli_setattrE_state {
3391 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3392 struct event_context *ev,
3393 struct cli_state *cli,
3399 struct tevent_req *req = NULL, *subreq = NULL;
3400 struct cli_setattrE_state *state = NULL;
3401 uint8_t additional_flags = 0;
3403 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3408 SSVAL(state->vwv+0, 0, fnum);
3409 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3411 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3413 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3416 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3417 7, state->vwv, 0, NULL);
3418 if (tevent_req_nomem(subreq, req)) {
3419 return tevent_req_post(req, ev);
3421 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3425 static void cli_setattrE_done(struct tevent_req *subreq)
3427 struct tevent_req *req = tevent_req_callback_data(
3428 subreq, struct tevent_req);
3431 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3432 TALLOC_FREE(subreq);
3433 if (!NT_STATUS_IS_OK(status)) {
3434 tevent_req_nterror(req, status);
3437 tevent_req_done(req);
3440 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3442 return tevent_req_simple_recv_ntstatus(req);
3445 NTSTATUS cli_setattrE(struct cli_state *cli,
3451 TALLOC_CTX *frame = talloc_stackframe();
3452 struct event_context *ev = NULL;
3453 struct tevent_req *req = NULL;
3454 NTSTATUS status = NT_STATUS_OK;
3456 if (cli_has_async_calls(cli)) {
3458 * Can't use sync call while an async call is in flight
3460 status = NT_STATUS_INVALID_PARAMETER;
3464 ev = event_context_init(frame);
3466 status = NT_STATUS_NO_MEMORY;
3470 req = cli_setattrE_send(frame, ev,
3478 status = NT_STATUS_NO_MEMORY;
3482 if (!tevent_req_poll(req, ev)) {
3483 status = map_nt_error_from_unix(errno);
3487 status = cli_setattrE_recv(req);
3491 if (!NT_STATUS_IS_OK(status)) {
3492 cli_set_error(cli, status);
3497 /****************************************************************************
3498 Do a SMBsetatr call.
3499 ****************************************************************************/
3501 static void cli_setatr_done(struct tevent_req *subreq);
3503 struct cli_setatr_state {
3507 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3508 struct event_context *ev,
3509 struct cli_state *cli,
3514 struct tevent_req *req = NULL, *subreq = NULL;
3515 struct cli_setatr_state *state = NULL;
3516 uint8_t additional_flags = 0;
3517 uint8_t *bytes = NULL;
3519 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3524 SSVAL(state->vwv+0, 0, attr);
3525 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
3527 bytes = talloc_array(state, uint8_t, 1);
3528 if (tevent_req_nomem(bytes, req)) {
3529 return tevent_req_post(req, ev);
3532 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3533 strlen(fname)+1, NULL);
3534 if (tevent_req_nomem(bytes, req)) {
3535 return tevent_req_post(req, ev);
3537 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3538 talloc_get_size(bytes)+1);
3539 if (tevent_req_nomem(bytes, req)) {
3540 return tevent_req_post(req, ev);
3543 bytes[talloc_get_size(bytes)-1] = 4;
3544 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3546 if (tevent_req_nomem(bytes, req)) {
3547 return tevent_req_post(req, ev);
3550 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3551 8, state->vwv, talloc_get_size(bytes), bytes);
3552 if (tevent_req_nomem(subreq, req)) {
3553 return tevent_req_post(req, ev);
3555 tevent_req_set_callback(subreq, cli_setatr_done, req);
3559 static void cli_setatr_done(struct tevent_req *subreq)
3561 struct tevent_req *req = tevent_req_callback_data(
3562 subreq, struct tevent_req);
3565 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3566 TALLOC_FREE(subreq);
3567 if (!NT_STATUS_IS_OK(status)) {
3568 tevent_req_nterror(req, status);
3571 tevent_req_done(req);
3574 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3576 return tevent_req_simple_recv_ntstatus(req);
3579 NTSTATUS cli_setatr(struct cli_state *cli,
3584 TALLOC_CTX *frame = talloc_stackframe();
3585 struct event_context *ev = NULL;
3586 struct tevent_req *req = NULL;
3587 NTSTATUS status = NT_STATUS_OK;
3589 if (cli_has_async_calls(cli)) {
3591 * Can't use sync call while an async call is in flight
3593 status = NT_STATUS_INVALID_PARAMETER;
3597 ev = event_context_init(frame);
3599 status = NT_STATUS_NO_MEMORY;
3603 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3605 status = NT_STATUS_NO_MEMORY;
3609 if (!tevent_req_poll(req, ev)) {
3610 status = map_nt_error_from_unix(errno);
3614 status = cli_setatr_recv(req);
3618 if (!NT_STATUS_IS_OK(status)) {
3619 cli_set_error(cli, status);
3624 /****************************************************************************
3625 Check for existance of a dir.
3626 ****************************************************************************/
3628 static void cli_chkpath_done(struct tevent_req *subreq);
3630 struct cli_chkpath_state {
3634 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3635 struct event_context *ev,
3636 struct cli_state *cli,
3639 struct tevent_req *req = NULL, *subreq = NULL;
3640 struct cli_chkpath_state *state = NULL;
3641 uint8_t additional_flags = 0;
3642 uint8_t *bytes = NULL;
3644 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3649 bytes = talloc_array(state, uint8_t, 1);
3650 if (tevent_req_nomem(bytes, req)) {
3651 return tevent_req_post(req, ev);
3654 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3655 strlen(fname)+1, NULL);
3657 if (tevent_req_nomem(bytes, req)) {
3658 return tevent_req_post(req, ev);
3661 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3662 0, NULL, talloc_get_size(bytes), bytes);
3663 if (tevent_req_nomem(subreq, req)) {
3664 return tevent_req_post(req, ev);
3666 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3670 static void cli_chkpath_done(struct tevent_req *subreq)
3672 struct tevent_req *req = tevent_req_callback_data(
3673 subreq, struct tevent_req);
3676 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3677 TALLOC_FREE(subreq);
3678 if (!NT_STATUS_IS_OK(status)) {
3679 tevent_req_nterror(req, status);
3682 tevent_req_done(req);
3685 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3687 return tevent_req_simple_recv_ntstatus(req);
3690 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3692 TALLOC_CTX *frame = talloc_stackframe();
3693 struct event_context *ev = NULL;
3694 struct tevent_req *req = NULL;
3696 NTSTATUS status = NT_STATUS_OK;
3698 if (cli_has_async_calls(cli)) {
3700 * Can't use sync call while an async call is in flight
3702 status = NT_STATUS_INVALID_PARAMETER;
3706 path2 = talloc_strdup(frame, path);
3708 status = NT_STATUS_NO_MEMORY;
3711 trim_char(path2,'\0','\\');
3713 path2 = talloc_strdup(frame, "\\");
3715 status = NT_STATUS_NO_MEMORY;
3720 ev = event_context_init(frame);
3722 status = NT_STATUS_NO_MEMORY;
3726 req = cli_chkpath_send(frame, ev, cli, path2);
3728 status = NT_STATUS_NO_MEMORY;
3732 if (!tevent_req_poll(req, ev)) {
3733 status = map_nt_error_from_unix(errno);
3737 status = cli_chkpath_recv(req);
3741 if (!NT_STATUS_IS_OK(status)) {
3742 cli_set_error(cli, status);
3747 /****************************************************************************
3749 ****************************************************************************/
3751 static void cli_dskattr_done(struct tevent_req *subreq);
3753 struct cli_dskattr_state {
3759 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3760 struct event_context *ev,
3761 struct cli_state *cli)
3763 struct tevent_req *req = NULL, *subreq = NULL;
3764 struct cli_dskattr_state *state = NULL;
3765 uint8_t additional_flags = 0;
3767 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3772 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3774 if (tevent_req_nomem(subreq, req)) {
3775 return tevent_req_post(req, ev);
3777 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3781 static void cli_dskattr_done(struct tevent_req *subreq)
3783 struct tevent_req *req = tevent_req_callback_data(
3784 subreq, struct tevent_req);
3785 struct cli_dskattr_state *state = tevent_req_data(
3786 req, struct cli_dskattr_state);
3788 uint16_t *vwv = NULL;
3792 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3794 TALLOC_FREE(subreq);
3795 if (!NT_STATUS_IS_OK(status)) {
3796 tevent_req_nterror(req, status);
3799 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3800 state->total = SVAL(vwv+0, 0);
3801 state->avail = SVAL(vwv+3, 0);
3802 tevent_req_done(req);
3805 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3807 struct cli_dskattr_state *state = tevent_req_data(
3808 req, struct cli_dskattr_state);
3811 if (tevent_req_is_nterror(req, &status)) {
3814 *bsize = state->bsize;
3815 *total = state->total;
3816 *avail = state->avail;
3817 return NT_STATUS_OK;
3820 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3822 TALLOC_CTX *frame = talloc_stackframe();
3823 struct event_context *ev = NULL;
3824 struct tevent_req *req = NULL;
3825 NTSTATUS status = NT_STATUS_OK;
3827 if (cli_has_async_calls(cli)) {
3829 * Can't use sync call while an async call is in flight
3831 status = NT_STATUS_INVALID_PARAMETER;
3835 ev = event_context_init(frame);
3837 status = NT_STATUS_NO_MEMORY;
3841 req = cli_dskattr_send(frame, ev, cli);
3843 status = NT_STATUS_NO_MEMORY;
3847 if (!tevent_req_poll(req, ev)) {
3848 status = map_nt_error_from_unix(errno);
3852 status = cli_dskattr_recv(req, bsize, total, avail);
3856 if (!NT_STATUS_IS_OK(status)) {
3857 cli_set_error(cli, status);
3862 /****************************************************************************
3863 Create and open a temporary file.
3864 ****************************************************************************/
3866 static void cli_ctemp_done(struct tevent_req *subreq);
3868 struct ctemp_state {
3874 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3875 struct event_context *ev,
3876 struct cli_state *cli,
3879 struct tevent_req *req = NULL, *subreq = NULL;
3880 struct ctemp_state *state = NULL;
3881 uint8_t additional_flags = 0;
3882 uint8_t *bytes = NULL;
3884 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3889 SSVAL(state->vwv,0,0);
3890 SIVALS(state->vwv+1,0,-1);
3892 bytes = talloc_array(state, uint8_t, 1);
3893 if (tevent_req_nomem(bytes, req)) {
3894 return tevent_req_post(req, ev);
3897 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3898 strlen(path)+1, NULL);
3899 if (tevent_req_nomem(bytes, req)) {
3900 return tevent_req_post(req, ev);
3903 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3904 3, state->vwv, talloc_get_size(bytes), bytes);
3905 if (tevent_req_nomem(subreq, req)) {
3906 return tevent_req_post(req, ev);
3908 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3912 static void cli_ctemp_done(struct tevent_req *subreq)
3914 struct tevent_req *req = tevent_req_callback_data(
3915 subreq, struct tevent_req);
3916 struct ctemp_state *state = tevent_req_data(
3917 req, struct ctemp_state);
3921 uint32_t num_bytes = 0;
3922 uint8_t *bytes = NULL;
3925 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3926 &num_bytes, &bytes);
3927 TALLOC_FREE(subreq);
3928 if (!NT_STATUS_IS_OK(status)) {
3929 tevent_req_nterror(req, status);
3933 state->fnum = SVAL(vwv+0, 0);
3935 /* From W2K3, the result is just the ASCII name */
3936 if (num_bytes < 2) {
3937 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
3941 if (pull_string_talloc(state,
3948 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
3951 tevent_req_done(req);
3954 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
3959 struct ctemp_state *state = tevent_req_data(req,
3960 struct ctemp_state);
3963 if (tevent_req_is_nterror(req, &status)) {
3966 *pfnum = state->fnum;
3967 *outfile = talloc_strdup(ctx, state->ret_path);
3969 return NT_STATUS_NO_MEMORY;
3971 return NT_STATUS_OK;
3974 NTSTATUS cli_ctemp(struct cli_state *cli,
3980 TALLOC_CTX *frame = talloc_stackframe();
3981 struct event_context *ev;
3982 struct tevent_req *req;
3983 NTSTATUS status = NT_STATUS_OK;
3985 if (cli_has_async_calls(cli)) {
3987 * Can't use sync call while an async call is in flight
3989 status = NT_STATUS_INVALID_PARAMETER;
3993 ev = event_context_init(frame);
3995 status = NT_STATUS_NO_MEMORY;
3999 req = cli_ctemp_send(frame, ev, cli, path);
4001 status = NT_STATUS_NO_MEMORY;
4005 if (!tevent_req_poll(req, ev)) {
4006 status = map_nt_error_from_unix(errno);
4010 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4014 if (!NT_STATUS_IS_OK(status)) {
4015 cli_set_error(cli, status);
4021 send a raw ioctl - used by the torture code
4023 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4028 SSVAL(vwv+0, 0, fnum);
4029 SSVAL(vwv+1, 0, code>>16);
4030 SSVAL(vwv+2, 0, (code&0xFFFF));
4032 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4033 NULL, 0, NULL, NULL, NULL, NULL);
4034 if (!NT_STATUS_IS_OK(status)) {
4037 *blob = data_blob_null;
4038 return NT_STATUS_OK;
4041 /*********************************************************
4042 Set an extended attribute utility fn.
4043 *********************************************************/
4045 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4046 uint8_t *param, unsigned int param_len,
4047 const char *ea_name,
4048 const char *ea_val, size_t ea_len)
4051 unsigned int data_len = 0;
4052 uint8_t *data = NULL;
4054 size_t ea_namelen = strlen(ea_name);
4057 SSVAL(setup, 0, setup_val);
4059 if (ea_namelen == 0 && ea_len == 0) {
4061 data = (uint8_t *)SMB_MALLOC(data_len);
4063 return NT_STATUS_NO_MEMORY;
4066 SIVAL(p,0,data_len);
4068 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4069 data = (uint8_t *)SMB_MALLOC(data_len);
4071 return NT_STATUS_NO_MEMORY;
4074 SIVAL(p,0,data_len);
4076 SCVAL(p, 0, 0); /* EA flags. */
4077 SCVAL(p, 1, ea_namelen);
4078 SSVAL(p, 2, ea_len);
4079 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4080 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4083 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4085 param, param_len, 2,
4086 data, data_len, cli->max_xmit,
4088 NULL, 0, NULL, /* rsetup */
4089 NULL, 0, NULL, /* rparam */
4090 NULL, 0, NULL); /* rdata */
4095 /*********************************************************
4096 Set an extended attribute on a pathname.
4097 *********************************************************/
4099 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4100 const char *ea_name, const char *ea_val,
4103 unsigned int param_len = 0;
4105 size_t srclen = 2*(strlen(path)+1);
4109 param = SMB_MALLOC_ARRAY(uint8_t, 6+srclen+2);
4111 return NT_STATUS_NO_MEMORY;
4113 memset(param, '\0', 6);
4114 SSVAL(param,0,SMB_INFO_SET_EA);
4115 p = (char *)(¶m[6]);
4117 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4118 param_len = PTR_DIFF(p, param);
4120 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4121 ea_name, ea_val, ea_len);
4126 /*********************************************************
4127 Set an extended attribute on an fnum.
4128 *********************************************************/
4130 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4131 const char *ea_name, const char *ea_val,
4136 memset(param, 0, 6);
4137 SSVAL(param,0,fnum);
4138 SSVAL(param,2,SMB_INFO_SET_EA);
4140 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4141 ea_name, ea_val, ea_len);
4144 /*********************************************************
4145 Get an extended attribute list utility fn.
4146 *********************************************************/
4148 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4150 size_t *pnum_eas, struct ea_struct **pea_list)
4152 struct ea_struct *ea_list = NULL;
4157 if (rdata_len < 4) {
4161 ea_size = (size_t)IVAL(rdata,0);
4162 if (ea_size > rdata_len) {
4167 /* No EA's present. */
4176 /* Validate the EA list and count it. */
4177 for (num_eas = 0; ea_size >= 4; num_eas++) {
4178 unsigned int ea_namelen = CVAL(p,1);
4179 unsigned int ea_valuelen = SVAL(p,2);
4180 if (ea_namelen == 0) {
4183 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4186 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4187 p += 4 + ea_namelen + 1 + ea_valuelen;
4196 *pnum_eas = num_eas;
4198 /* Caller only wants number of EA's. */
4202 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
4207 ea_size = (size_t)IVAL(rdata,0);
4210 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4211 struct ea_struct *ea = &ea_list[num_eas];
4212 fstring unix_ea_name;
4213 unsigned int ea_namelen = CVAL(p,1);
4214 unsigned int ea_valuelen = SVAL(p,2);
4216 ea->flags = CVAL(p,0);
4217 unix_ea_name[0] = '\0';
4218 pull_ascii_fstring(unix_ea_name, p + 4);
4219 ea->name = talloc_strdup(ea_list, unix_ea_name);
4223 /* Ensure the value is null terminated (in case it's a string). */
4224 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4225 if (!ea->value.data) {
4229 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4231 ea->value.data[ea_valuelen] = 0;
4233 p += 4 + ea_namelen + 1 + ea_valuelen;
4236 *pea_list = ea_list;
4240 TALLOC_FREE(ea_list);
4244 /*********************************************************
4245 Get an extended attribute list from a pathname.
4246 *********************************************************/
4248 struct cli_get_ea_list_path_state {
4253 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4255 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4256 struct tevent_context *ev,
4257 struct cli_state *cli,
4260 struct tevent_req *req, *subreq;
4261 struct cli_get_ea_list_path_state *state;
4263 req = tevent_req_create(mem_ctx, &state,
4264 struct cli_get_ea_list_path_state);
4268 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4269 SMB_INFO_QUERY_ALL_EAS, 4,
4271 if (tevent_req_nomem(subreq, req)) {
4272 return tevent_req_post(req, ev);
4274 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4278 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4280 struct tevent_req *req = tevent_req_callback_data(
4281 subreq, struct tevent_req);
4282 struct cli_get_ea_list_path_state *state = tevent_req_data(
4283 req, struct cli_get_ea_list_path_state);
4286 status = cli_qpathinfo_recv(subreq, state, &state->data,
4288 TALLOC_FREE(subreq);
4289 if (!NT_STATUS_IS_OK(status)) {
4290 tevent_req_nterror(req, status);
4293 tevent_req_done(req);
4296 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4297 size_t *pnum_eas, struct ea_struct **peas)
4299 struct cli_get_ea_list_path_state *state = tevent_req_data(
4300 req, struct cli_get_ea_list_path_state);
4303 if (tevent_req_is_nterror(req, &status)) {
4306 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4308 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4310 return NT_STATUS_OK;
4313 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4316 struct ea_struct **pea_list)
4318 TALLOC_CTX *frame = talloc_stackframe();
4319 struct event_context *ev = NULL;
4320 struct tevent_req *req = NULL;
4321 NTSTATUS status = NT_STATUS_NO_MEMORY;
4323 if (cli_has_async_calls(cli)) {
4325 * Can't use sync call while an async call is in flight
4327 status = NT_STATUS_INVALID_PARAMETER;
4330 ev = event_context_init(frame);
4334 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4338 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4341 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4344 if (!NT_STATUS_IS_OK(status)) {
4345 cli_set_error(cli, status);
4350 /****************************************************************************
4351 Convert open "flags" arg to uint32_t on wire.
4352 ****************************************************************************/
4354 static uint32_t open_flags_to_wire(int flags)
4356 int open_mode = flags & O_ACCMODE;
4359 switch (open_mode) {
4361 ret |= SMB_O_WRONLY;
4368 ret |= SMB_O_RDONLY;
4372 if (flags & O_CREAT) {
4375 if (flags & O_EXCL) {
4378 if (flags & O_TRUNC) {
4382 if (flags & O_SYNC) {
4386 if (flags & O_APPEND) {
4387 ret |= SMB_O_APPEND;
4389 #if defined(O_DIRECT)
4390 if (flags & O_DIRECT) {
4391 ret |= SMB_O_DIRECT;
4394 #if defined(O_DIRECTORY)
4395 if (flags & O_DIRECTORY) {
4396 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4397 ret |= SMB_O_DIRECTORY;
4403 /****************************************************************************
4404 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4405 ****************************************************************************/
4407 struct posix_open_state {
4411 uint16_t fnum; /* Out */
4414 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4416 struct tevent_req *req = tevent_req_callback_data(
4417 subreq, struct tevent_req);
4418 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4423 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4424 NULL, 0, NULL, &data, 12, &num_data);
4425 TALLOC_FREE(subreq);
4426 if (!NT_STATUS_IS_OK(status)) {
4427 tevent_req_nterror(req, status);
4430 state->fnum = SVAL(data,2);
4431 tevent_req_done(req);
4434 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4435 struct event_context *ev,
4436 struct cli_state *cli,
4442 struct tevent_req *req = NULL, *subreq = NULL;
4443 struct posix_open_state *state = NULL;
4444 uint32_t wire_flags = open_flags_to_wire(flags);
4446 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4451 /* Setup setup word. */
4452 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4454 /* Setup param array. */
4455 state->param = talloc_array(state, uint8_t, 6);
4456 if (tevent_req_nomem(state->param, req)) {
4457 return tevent_req_post(req, ev);
4459 memset(state->param, '\0', 6);
4460 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4462 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4463 strlen(fname)+1, NULL);
4465 if (tevent_req_nomem(state->param, req)) {
4466 return tevent_req_post(req, ev);
4469 /* Setup data words. */
4471 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4472 wire_flags |= SMB_O_DIRECTORY;
4475 SIVAL(state->data,0,0); /* No oplock. */
4476 SIVAL(state->data,4,wire_flags);
4477 SIVAL(state->data,8,unix_perms_to_wire(mode));
4478 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4479 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4481 subreq = cli_trans_send(state, /* mem ctx. */
4482 ev, /* event ctx. */
4483 cli, /* cli_state. */
4484 SMBtrans2, /* cmd. */
4485 NULL, /* pipe name. */
4489 &state->setup, /* setup. */
4490 1, /* num setup uint16_t words. */
4491 0, /* max returned setup. */
4492 state->param, /* param. */
4493 talloc_get_size(state->param),/* num param. */
4494 2, /* max returned param. */
4495 state->data, /* data. */
4497 12); /* max returned data. */
4499 if (tevent_req_nomem(subreq, req)) {
4500 return tevent_req_post(req, ev);
4502 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4506 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4507 struct event_context *ev,
4508 struct cli_state *cli,
4513 return cli_posix_open_internal_send(mem_ctx, ev,
4514 cli, fname, flags, mode, false);
4517 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4519 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4522 if (tevent_req_is_nterror(req, &status)) {
4525 *pfnum = state->fnum;
4526 return NT_STATUS_OK;
4529 /****************************************************************************
4530 Open - POSIX semantics. Doesn't request oplock.
4531 ****************************************************************************/
4533 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4534 int flags, mode_t mode, uint16_t *pfnum)
4537 TALLOC_CTX *frame = talloc_stackframe();
4538 struct event_context *ev = NULL;
4539 struct tevent_req *req = NULL;
4540 NTSTATUS status = NT_STATUS_OK;
4542 if (cli_has_async_calls(cli)) {
4544 * Can't use sync call while an async call is in flight
4546 status = NT_STATUS_INVALID_PARAMETER;
4550 ev = event_context_init(frame);
4552 status = NT_STATUS_NO_MEMORY;
4556 req = cli_posix_open_send(frame,
4563 status = NT_STATUS_NO_MEMORY;
4567 if (!tevent_req_poll(req, ev)) {
4568 status = map_nt_error_from_unix(errno);
4572 status = cli_posix_open_recv(req, pfnum);
4576 if (!NT_STATUS_IS_OK(status)) {
4577 cli_set_error(cli, status);
4582 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4583 struct event_context *ev,
4584 struct cli_state *cli,
4588 return cli_posix_open_internal_send(mem_ctx, ev,
4589 cli, fname, O_CREAT, mode, true);
4592 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4594 return tevent_req_simple_recv_ntstatus(req);
4597 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4599 TALLOC_CTX *frame = talloc_stackframe();
4600 struct event_context *ev = NULL;
4601 struct tevent_req *req = NULL;
4602 NTSTATUS status = NT_STATUS_OK;
4604 if (cli_has_async_calls(cli)) {
4606 * Can't use sync call while an async call is in flight
4608 status = NT_STATUS_INVALID_PARAMETER;
4612 ev = event_context_init(frame);
4614 status = NT_STATUS_NO_MEMORY;
4618 req = cli_posix_mkdir_send(frame,
4624 status = NT_STATUS_NO_MEMORY;
4628 if (!tevent_req_poll(req, ev)) {
4629 status = map_nt_error_from_unix(errno);
4633 status = cli_posix_mkdir_recv(req);
4637 if (!NT_STATUS_IS_OK(status)) {
4638 cli_set_error(cli, status);
4643 /****************************************************************************
4644 unlink or rmdir - POSIX semantics.
4645 ****************************************************************************/
4647 struct cli_posix_unlink_internal_state {
4651 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4653 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4654 struct event_context *ev,
4655 struct cli_state *cli,
4659 struct tevent_req *req = NULL, *subreq = NULL;
4660 struct cli_posix_unlink_internal_state *state = NULL;
4662 req = tevent_req_create(mem_ctx, &state,
4663 struct cli_posix_unlink_internal_state);
4668 /* Setup data word. */
4669 SSVAL(state->data, 0, level);
4671 subreq = cli_setpathinfo_send(state, ev, cli,
4672 SMB_POSIX_PATH_UNLINK,
4674 state->data, sizeof(state->data));
4675 if (tevent_req_nomem(subreq, req)) {
4676 return tevent_req_post(req, ev);
4678 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4682 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4684 NTSTATUS status = cli_setpathinfo_recv(subreq);
4685 tevent_req_simple_finish_ntstatus(subreq, status);
4688 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4689 struct event_context *ev,
4690 struct cli_state *cli,
4693 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4694 SMB_POSIX_UNLINK_FILE_TARGET);
4697 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4699 return tevent_req_simple_recv_ntstatus(req);
4702 /****************************************************************************
4703 unlink - POSIX semantics.
4704 ****************************************************************************/
4706 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4708 TALLOC_CTX *frame = talloc_stackframe();
4709 struct event_context *ev = NULL;
4710 struct tevent_req *req = NULL;
4711 NTSTATUS status = NT_STATUS_OK;
4713 if (cli_has_async_calls(cli)) {
4715 * Can't use sync call while an async call is in flight
4717 status = NT_STATUS_INVALID_PARAMETER;
4721 ev = event_context_init(frame);
4723 status = NT_STATUS_NO_MEMORY;
4727 req = cli_posix_unlink_send(frame,
4732 status = NT_STATUS_NO_MEMORY;
4736 if (!tevent_req_poll(req, ev)) {
4737 status = map_nt_error_from_unix(errno);
4741 status = cli_posix_unlink_recv(req);
4745 if (!NT_STATUS_IS_OK(status)) {
4746 cli_set_error(cli, status);
4751 /****************************************************************************
4752 rmdir - POSIX semantics.
4753 ****************************************************************************/
4755 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4756 struct event_context *ev,
4757 struct cli_state *cli,
4760 return cli_posix_unlink_internal_send(
4761 mem_ctx, ev, cli, fname,
4762 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4765 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4767 return tevent_req_simple_recv_ntstatus(req);
4770 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4772 TALLOC_CTX *frame = talloc_stackframe();
4773 struct event_context *ev = NULL;
4774 struct tevent_req *req = NULL;
4775 NTSTATUS status = NT_STATUS_OK;
4777 if (cli_has_async_calls(cli)) {
4779 * Can't use sync call while an async call is in flight
4781 status = NT_STATUS_INVALID_PARAMETER;
4785 ev = event_context_init(frame);
4787 status = NT_STATUS_NO_MEMORY;
4791 req = cli_posix_rmdir_send(frame,
4796 status = NT_STATUS_NO_MEMORY;
4800 if (!tevent_req_poll(req, ev)) {
4801 status = map_nt_error_from_unix(errno);
4805 status = cli_posix_rmdir_recv(req, frame);
4809 if (!NT_STATUS_IS_OK(status)) {
4810 cli_set_error(cli, status);
4815 /****************************************************************************
4817 ****************************************************************************/
4819 struct cli_notify_state {
4821 uint32_t num_changes;
4822 struct notify_change *changes;
4825 static void cli_notify_done(struct tevent_req *subreq);
4827 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4828 struct tevent_context *ev,
4829 struct cli_state *cli, uint16_t fnum,
4830 uint32_t buffer_size,
4831 uint32_t completion_filter, bool recursive)
4833 struct tevent_req *req, *subreq;
4834 struct cli_notify_state *state;
4836 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4841 SIVAL(state->setup, 0, completion_filter);
4842 SSVAL(state->setup, 4, fnum);
4843 SSVAL(state->setup, 6, recursive);
4845 subreq = cli_trans_send(
4846 state, /* mem ctx. */
4847 ev, /* event ctx. */
4848 cli, /* cli_state. */
4849 SMBnttrans, /* cmd. */
4850 NULL, /* pipe name. */
4852 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4854 (uint16_t *)state->setup, /* setup. */
4855 4, /* num setup uint16_t words. */
4856 0, /* max returned setup. */
4859 buffer_size, /* max returned param. */
4862 0); /* max returned data. */
4864 if (tevent_req_nomem(subreq, req)) {
4865 return tevent_req_post(req, ev);
4867 tevent_req_set_callback(subreq, cli_notify_done, req);
4871 static void cli_notify_done(struct tevent_req *subreq)
4873 struct tevent_req *req = tevent_req_callback_data(
4874 subreq, struct tevent_req);
4875 struct cli_notify_state *state = tevent_req_data(
4876 req, struct cli_notify_state);
4879 uint32_t i, ofs, num_params;
4882 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4883 ¶ms, 0, &num_params, NULL, 0, NULL);
4884 TALLOC_FREE(subreq);
4885 if (!NT_STATUS_IS_OK(status)) {
4886 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4887 tevent_req_nterror(req, status);
4891 state->num_changes = 0;
4894 while (num_params - ofs > 12) {
4895 uint32_t len = IVAL(params, ofs);
4896 state->num_changes += 1;
4898 if ((len == 0) || (ofs+len >= num_params)) {
4904 state->changes = talloc_array(state, struct notify_change,
4905 state->num_changes);
4906 if (tevent_req_nomem(state->changes, req)) {
4907 TALLOC_FREE(params);
4913 for (i=0; i<state->num_changes; i++) {
4914 uint32_t next = IVAL(params, ofs);
4915 uint32_t len = IVAL(params, ofs+8);
4919 if ((next != 0) && (len+12 != next)) {
4920 TALLOC_FREE(params);
4922 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4926 state->changes[i].action = IVAL(params, ofs+4);
4927 ret = clistr_pull_talloc(params, (char *)params, flags2,
4928 &name, params+ofs+12, len,
4929 STR_TERMINATE|STR_UNICODE);
4931 TALLOC_FREE(params);
4932 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4935 state->changes[i].name = name;
4939 TALLOC_FREE(params);
4940 tevent_req_done(req);
4943 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4944 uint32_t *pnum_changes,
4945 struct notify_change **pchanges)
4947 struct cli_notify_state *state = tevent_req_data(
4948 req, struct cli_notify_state);
4951 if (tevent_req_is_nterror(req, &status)) {
4955 *pnum_changes = state->num_changes;
4956 *pchanges = talloc_move(mem_ctx, &state->changes);
4957 return NT_STATUS_OK;
4960 struct cli_qpathinfo_state {
4969 static void cli_qpathinfo_done(struct tevent_req *subreq);
4971 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
4972 struct tevent_context *ev,
4973 struct cli_state *cli, const char *fname,
4974 uint16_t level, uint32_t min_rdata,
4977 struct tevent_req *req, *subreq;
4978 struct cli_qpathinfo_state *state;
4980 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
4984 state->min_rdata = min_rdata;
4985 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
4987 state->param = talloc_zero_array(state, uint8_t, 6);
4988 if (tevent_req_nomem(state->param, req)) {
4989 return tevent_req_post(req, ev);
4991 SSVAL(state->param, 0, level);
4992 state->param = trans2_bytes_push_str(
4993 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
4994 if (tevent_req_nomem(state->param, req)) {
4995 return tevent_req_post(req, ev);
4998 subreq = cli_trans_send(
4999 state, /* mem ctx. */
5000 ev, /* event ctx. */
5001 cli, /* cli_state. */
5002 SMBtrans2, /* cmd. */
5003 NULL, /* pipe name. */
5007 state->setup, /* setup. */
5008 1, /* num setup uint16_t words. */
5009 0, /* max returned setup. */
5010 state->param, /* param. */
5011 talloc_get_size(state->param), /* num param. */
5012 2, /* max returned param. */
5015 max_rdata); /* max returned data. */
5017 if (tevent_req_nomem(subreq, req)) {
5018 return tevent_req_post(req, ev);
5020 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5024 static void cli_qpathinfo_done(struct tevent_req *subreq)
5026 struct tevent_req *req = tevent_req_callback_data(
5027 subreq, struct tevent_req);
5028 struct cli_qpathinfo_state *state = tevent_req_data(
5029 req, struct cli_qpathinfo_state);
5032 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5034 &state->rdata, state->min_rdata,
5036 if (!NT_STATUS_IS_OK(status)) {
5037 tevent_req_nterror(req, status);
5040 tevent_req_done(req);
5043 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5044 uint8_t **rdata, uint32_t *num_rdata)
5046 struct cli_qpathinfo_state *state = tevent_req_data(
5047 req, struct cli_qpathinfo_state);
5050 if (tevent_req_is_nterror(req, &status)) {
5053 if (rdata != NULL) {
5054 *rdata = talloc_move(mem_ctx, &state->rdata);
5056 TALLOC_FREE(state->rdata);
5058 if (num_rdata != NULL) {
5059 *num_rdata = state->num_rdata;
5061 return NT_STATUS_OK;
5064 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5065 const char *fname, uint16_t level, uint32_t min_rdata,
5067 uint8_t **rdata, uint32_t *num_rdata)
5069 TALLOC_CTX *frame = talloc_stackframe();
5070 struct event_context *ev;
5071 struct tevent_req *req;
5072 NTSTATUS status = NT_STATUS_NO_MEMORY;
5074 if (cli_has_async_calls(cli)) {
5076 * Can't use sync call while an async call is in flight
5078 status = NT_STATUS_INVALID_PARAMETER;
5081 ev = event_context_init(frame);
5085 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5090 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5093 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5096 if (!NT_STATUS_IS_OK(status)) {
5097 cli_set_error(cli, status);
5102 struct cli_qfileinfo_state {
5111 static void cli_qfileinfo_done(struct tevent_req *subreq);
5113 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5114 struct tevent_context *ev,
5115 struct cli_state *cli, uint16_t fnum,
5116 uint16_t level, uint32_t min_rdata,
5119 struct tevent_req *req, *subreq;
5120 struct cli_qfileinfo_state *state;
5122 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5126 state->min_rdata = min_rdata;
5127 SSVAL(state->param, 0, fnum);
5128 SSVAL(state->param, 2, level);
5129 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5131 subreq = cli_trans_send(
5132 state, /* mem ctx. */
5133 ev, /* event ctx. */
5134 cli, /* cli_state. */
5135 SMBtrans2, /* cmd. */
5136 NULL, /* pipe name. */
5140 state->setup, /* setup. */
5141 1, /* num setup uint16_t words. */
5142 0, /* max returned setup. */
5143 state->param, /* param. */
5144 sizeof(state->param), /* num param. */
5145 2, /* max returned param. */
5148 max_rdata); /* max returned data. */
5150 if (tevent_req_nomem(subreq, req)) {
5151 return tevent_req_post(req, ev);
5153 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5157 static void cli_qfileinfo_done(struct tevent_req *subreq)
5159 struct tevent_req *req = tevent_req_callback_data(
5160 subreq, struct tevent_req);
5161 struct cli_qfileinfo_state *state = tevent_req_data(
5162 req, struct cli_qfileinfo_state);
5165 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5167 &state->rdata, state->min_rdata,
5169 if (!NT_STATUS_IS_OK(status)) {
5170 tevent_req_nterror(req, status);
5173 tevent_req_done(req);
5176 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5177 uint8_t **rdata, uint32_t *num_rdata)
5179 struct cli_qfileinfo_state *state = tevent_req_data(
5180 req, struct cli_qfileinfo_state);
5183 if (tevent_req_is_nterror(req, &status)) {
5186 if (rdata != NULL) {
5187 *rdata = talloc_move(mem_ctx, &state->rdata);
5189 TALLOC_FREE(state->rdata);
5191 if (num_rdata != NULL) {
5192 *num_rdata = state->num_rdata;
5194 return NT_STATUS_OK;
5197 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5198 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5200 uint8_t **rdata, uint32_t *num_rdata)
5202 TALLOC_CTX *frame = talloc_stackframe();
5203 struct event_context *ev;
5204 struct tevent_req *req;
5205 NTSTATUS status = NT_STATUS_NO_MEMORY;
5207 if (cli_has_async_calls(cli)) {
5209 * Can't use sync call while an async call is in flight
5211 status = NT_STATUS_INVALID_PARAMETER;
5214 ev = event_context_init(frame);
5218 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5223 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5226 status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
5229 if (!NT_STATUS_IS_OK(status)) {
5230 cli_set_error(cli, status);
5235 struct cli_flush_state {
5239 static void cli_flush_done(struct tevent_req *subreq);
5241 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5242 struct event_context *ev,
5243 struct cli_state *cli,
5246 struct tevent_req *req, *subreq;
5247 struct cli_flush_state *state;
5249 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5253 SSVAL(state->vwv + 0, 0, fnum);
5255 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5257 if (tevent_req_nomem(subreq, req)) {
5258 return tevent_req_post(req, ev);
5260 tevent_req_set_callback(subreq, cli_flush_done, req);
5264 static void cli_flush_done(struct tevent_req *subreq)
5266 struct tevent_req *req = tevent_req_callback_data(
5267 subreq, struct tevent_req);
5270 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5271 TALLOC_FREE(subreq);
5272 if (!NT_STATUS_IS_OK(status)) {
5273 tevent_req_nterror(req, status);
5276 tevent_req_done(req);
5279 NTSTATUS cli_flush_recv(struct tevent_req *req)
5281 return tevent_req_simple_recv_ntstatus(req);
5284 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5286 TALLOC_CTX *frame = talloc_stackframe();
5287 struct event_context *ev;
5288 struct tevent_req *req;
5289 NTSTATUS status = NT_STATUS_NO_MEMORY;
5291 if (cli_has_async_calls(cli)) {
5293 * Can't use sync call while an async call is in flight
5295 status = NT_STATUS_INVALID_PARAMETER;
5298 ev = event_context_init(frame);
5302 req = cli_flush_send(frame, ev, cli, fnum);
5306 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5309 status = cli_flush_recv(req);
5312 if (!NT_STATUS_IS_OK(status)) {
5313 cli_set_error(cli, status);
5318 struct cli_shadow_copy_data_state {
5325 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5327 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5328 struct tevent_context *ev,
5329 struct cli_state *cli,
5333 struct tevent_req *req, *subreq;
5334 struct cli_shadow_copy_data_state *state;
5337 req = tevent_req_create(mem_ctx, &state,
5338 struct cli_shadow_copy_data_state);
5342 state->get_names = get_names;
5343 ret_size = get_names ? cli->max_xmit : 16;
5345 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5346 SSVAL(state->setup + 2, 0, fnum);
5347 SCVAL(state->setup + 3, 0, 0); /* isFsctl */
5348 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5350 subreq = cli_trans_send(
5351 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5352 state->setup, ARRAY_SIZE(state->setup), 0,
5355 if (tevent_req_nomem(subreq, req)) {
5356 return tevent_req_post(req, ev);
5358 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5362 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5364 struct tevent_req *req = tevent_req_callback_data(
5365 subreq, struct tevent_req);
5366 struct cli_shadow_copy_data_state *state = tevent_req_data(
5367 req, struct cli_shadow_copy_data_state);
5370 status = cli_trans_recv(subreq, state, NULL,
5371 NULL, 0, NULL, /* setup */
5372 NULL, 0, NULL, /* param */
5373 &state->data, 12, &state->num_data);
5374 TALLOC_FREE(subreq);
5375 if (!NT_STATUS_IS_OK(status)) {
5376 tevent_req_nterror(req, status);
5379 tevent_req_done(req);
5382 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5383 char ***pnames, int *pnum_names)
5385 struct cli_shadow_copy_data_state *state = tevent_req_data(
5386 req, struct cli_shadow_copy_data_state);
5392 if (tevent_req_is_nterror(req, &status)) {
5395 num_names = IVAL(state->data, 4);
5396 dlength = IVAL(state->data, 8);
5398 if (!state->get_names) {
5399 *pnum_names = num_names;
5400 return NT_STATUS_OK;
5403 if (dlength+12 > state->num_data) {
5404 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5406 names = talloc_array(mem_ctx, char *, num_names);
5407 if (names == NULL) {
5408 return NT_STATUS_NO_MEMORY;
5411 for (i=0; i<num_names; i++) {
5414 size_t converted_size;
5416 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5417 ret = convert_string_talloc(
5418 names, CH_UTF16LE, CH_UNIX,
5419 src, 2 * sizeof(SHADOW_COPY_LABEL),
5420 &names[i], &converted_size, True);
5423 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5426 *pnum_names = num_names;
5428 return NT_STATUS_OK;
5431 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5432 uint16_t fnum, bool get_names,
5433 char ***pnames, int *pnum_names)
5435 TALLOC_CTX *frame = talloc_stackframe();
5436 struct event_context *ev;
5437 struct tevent_req *req;
5438 NTSTATUS status = NT_STATUS_NO_MEMORY;
5440 if (cli_has_async_calls(cli)) {
5442 * Can't use sync call while an async call is in flight
5444 status = NT_STATUS_INVALID_PARAMETER;
5447 ev = event_context_init(frame);
5451 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5455 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5458 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5461 if (!NT_STATUS_IS_OK(status)) {
5462 cli_set_error(cli, status);