2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "libsmb/clirap.h"
29 #include "libcli/security/secdesc.h"
31 /***********************************************************
32 Common function for pushing stings, used by smb_bytes_push_str()
33 and trans_bytes_push_str(). Only difference is the align_odd
35 ***********************************************************/
37 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
38 const char *str, size_t str_len,
40 size_t *pconverted_size)
44 size_t converted_size;
50 buflen = talloc_get_size(buf);
53 ((align_odd && (buflen % 2 == 0)) ||
54 (!align_odd && (buflen % 2 == 1)))) {
56 * We're pushing into an SMB buffer, align odd
58 buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
66 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
67 ucs2 ? CH_UTF16LE : CH_DOS,
68 str, str_len, &converted,
73 buf = talloc_realloc(NULL, buf, uint8_t,
74 buflen + converted_size);
76 TALLOC_FREE(converted);
80 memcpy(buf + buflen, converted, converted_size);
82 TALLOC_FREE(converted);
84 if (pconverted_size) {
85 *pconverted_size = converted_size;
91 /***********************************************************
92 Push a string into an SMB buffer, with odd byte alignment
93 if it's a UCS2 string.
94 ***********************************************************/
96 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
97 const char *str, size_t str_len,
98 size_t *pconverted_size)
100 return internal_bytes_push_str(buf, ucs2, str, str_len,
101 true, pconverted_size);
104 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
105 const uint8_t *bytes, size_t num_bytes)
112 buflen = talloc_get_size(buf);
114 buf = talloc_realloc(NULL, buf, uint8_t,
115 buflen + 1 + num_bytes);
119 buf[buflen] = prefix;
120 memcpy(&buf[buflen+1], bytes, num_bytes);
124 /***********************************************************
125 Same as smb_bytes_push_str(), but without the odd byte
126 align for ucs2 (we're pushing into a param or data block).
127 static for now, although this will probably change when
128 other modules use async trans calls.
129 ***********************************************************/
131 uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
132 const char *str, size_t str_len,
133 size_t *pconverted_size)
135 return internal_bytes_push_str(buf, ucs2, str, str_len,
136 false, pconverted_size);
139 uint8_t *trans2_bytes_push_bytes(uint8_t *buf,
140 const uint8_t *bytes, size_t num_bytes)
147 buflen = talloc_get_size(buf);
149 buf = talloc_realloc(NULL, buf, uint8_t,
154 memcpy(&buf[buflen], bytes, num_bytes);
158 struct cli_setpathinfo_state {
163 static void cli_setpathinfo_done(struct tevent_req *subreq);
165 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
166 struct tevent_context *ev,
167 struct cli_state *cli,
173 struct tevent_req *req, *subreq;
174 struct cli_setpathinfo_state *state;
176 req = tevent_req_create(mem_ctx, &state,
177 struct cli_setpathinfo_state);
182 /* Setup setup word. */
183 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
185 /* Setup param array. */
186 state->param = talloc_zero_array(state, uint8_t, 6);
187 if (tevent_req_nomem(state->param, req)) {
188 return tevent_req_post(req, ev);
190 SSVAL(state->param, 0, level);
192 state->param = trans2_bytes_push_str(
193 state->param, cli_ucs2(cli), path, strlen(path)+1, NULL);
194 if (tevent_req_nomem(state->param, req)) {
195 return tevent_req_post(req, ev);
198 subreq = cli_trans_send(
199 state, /* mem ctx. */
201 cli, /* cli_state. */
202 SMBtrans2, /* cmd. */
203 NULL, /* pipe name. */
207 &state->setup, /* setup. */
208 1, /* num setup uint16_t words. */
209 0, /* max returned setup. */
210 state->param, /* param. */
211 talloc_get_size(state->param), /* num param. */
212 2, /* max returned param. */
214 data_len, /* num data. */
215 0); /* max returned data. */
217 if (tevent_req_nomem(subreq, req)) {
218 return tevent_req_post(req, ev);
220 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
224 static void cli_setpathinfo_done(struct tevent_req *subreq)
226 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
227 NULL, 0, NULL, NULL, 0, NULL);
228 tevent_req_simple_finish_ntstatus(subreq, status);
231 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
233 return tevent_req_simple_recv_ntstatus(req);
236 NTSTATUS cli_setpathinfo(struct cli_state *cli,
242 TALLOC_CTX *frame = talloc_stackframe();
243 struct tevent_context *ev;
244 struct tevent_req *req;
245 NTSTATUS status = NT_STATUS_NO_MEMORY;
247 if (cli_has_async_calls(cli)) {
249 * Can't use sync call while an async call is in flight
251 status = NT_STATUS_INVALID_PARAMETER;
254 ev = tevent_context_init(frame);
258 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
262 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
265 status = cli_setpathinfo_recv(req);
271 /****************************************************************************
272 Hard/Symlink a file (UNIX extensions).
273 Creates new name (sym)linked to oldname.
274 ****************************************************************************/
276 struct cli_posix_link_internal_state {
280 static void cli_posix_link_internal_done(struct tevent_req *subreq);
282 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
283 struct event_context *ev,
284 struct cli_state *cli,
289 struct tevent_req *req = NULL, *subreq = NULL;
290 struct cli_posix_link_internal_state *state = NULL;
292 req = tevent_req_create(mem_ctx, &state,
293 struct cli_posix_link_internal_state);
298 /* Setup data array. */
299 state->data = talloc_array(state, uint8_t, 0);
300 if (tevent_req_nomem(state->data, req)) {
301 return tevent_req_post(req, ev);
303 state->data = trans2_bytes_push_str(
304 state->data, cli_ucs2(cli), oldname, strlen(oldname)+1, NULL);
306 subreq = cli_setpathinfo_send(
307 state, ev, cli, level, newname,
308 state->data, talloc_get_size(state->data));
309 if (tevent_req_nomem(subreq, req)) {
310 return tevent_req_post(req, ev);
312 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
316 static void cli_posix_link_internal_done(struct tevent_req *subreq)
318 NTSTATUS status = cli_setpathinfo_recv(subreq);
319 tevent_req_simple_finish_ntstatus(subreq, status);
322 /****************************************************************************
323 Symlink a file (UNIX extensions).
324 ****************************************************************************/
326 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
327 struct event_context *ev,
328 struct cli_state *cli,
332 return cli_posix_link_internal_send(
333 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
336 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
338 return tevent_req_simple_recv_ntstatus(req);
341 NTSTATUS cli_posix_symlink(struct cli_state *cli,
345 TALLOC_CTX *frame = talloc_stackframe();
346 struct event_context *ev = NULL;
347 struct tevent_req *req = NULL;
348 NTSTATUS status = NT_STATUS_OK;
350 if (cli_has_async_calls(cli)) {
352 * Can't use sync call while an async call is in flight
354 status = NT_STATUS_INVALID_PARAMETER;
358 ev = event_context_init(frame);
360 status = NT_STATUS_NO_MEMORY;
364 req = cli_posix_symlink_send(frame,
370 status = NT_STATUS_NO_MEMORY;
374 if (!tevent_req_poll(req, ev)) {
375 status = map_nt_error_from_unix(errno);
379 status = cli_posix_symlink_recv(req);
386 /****************************************************************************
387 Read a POSIX symlink.
388 ****************************************************************************/
390 struct readlink_state {
395 static void cli_posix_readlink_done(struct tevent_req *subreq);
397 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
398 struct event_context *ev,
399 struct cli_state *cli,
403 struct tevent_req *req = NULL, *subreq = NULL;
404 struct readlink_state *state = NULL;
405 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
407 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
413 * Len is in bytes, we need it in UCS2 units.
415 if ((2*len < len) || (maxbytelen < len)) {
416 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
417 return tevent_req_post(req, ev);
420 subreq = cli_qpathinfo_send(state, ev, cli, fname,
421 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
422 if (tevent_req_nomem(subreq, req)) {
423 return tevent_req_post(req, ev);
425 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
429 static void cli_posix_readlink_done(struct tevent_req *subreq)
431 struct tevent_req *req = tevent_req_callback_data(
432 subreq, struct tevent_req);
433 struct readlink_state *state = tevent_req_data(
434 req, struct readlink_state);
437 status = cli_qpathinfo_recv(subreq, state, &state->data,
440 if (tevent_req_nterror(req, status)) {
444 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
446 if (state->data[state->num_data-1] != '\0') {
447 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
450 tevent_req_done(req);
453 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
454 char *retpath, size_t len)
457 char *converted = NULL;
458 size_t converted_size = 0;
459 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
461 if (tevent_req_is_nterror(req, &status)) {
464 /* The returned data is a pushed string, not raw data. */
465 if (!convert_string_talloc(state,
466 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
472 return NT_STATUS_NO_MEMORY;
475 len = MIN(len,converted_size);
477 return NT_STATUS_DATA_ERROR;
479 memcpy(retpath, converted, len);
483 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
484 char *linkpath, size_t len)
486 TALLOC_CTX *frame = talloc_stackframe();
487 struct event_context *ev = NULL;
488 struct tevent_req *req = NULL;
489 NTSTATUS status = NT_STATUS_OK;
491 if (cli_has_async_calls(cli)) {
493 * Can't use sync call while an async call is in flight
495 status = NT_STATUS_INVALID_PARAMETER;
499 ev = event_context_init(frame);
501 status = NT_STATUS_NO_MEMORY;
505 req = cli_posix_readlink_send(frame,
511 status = NT_STATUS_NO_MEMORY;
515 if (!tevent_req_poll(req, ev)) {
516 status = map_nt_error_from_unix(errno);
520 status = cli_posix_readlink_recv(req, cli, linkpath, len);
527 /****************************************************************************
528 Hard link a file (UNIX extensions).
529 ****************************************************************************/
531 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
532 struct event_context *ev,
533 struct cli_state *cli,
537 return cli_posix_link_internal_send(
538 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
541 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
543 return tevent_req_simple_recv_ntstatus(req);
546 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
550 TALLOC_CTX *frame = talloc_stackframe();
551 struct event_context *ev = NULL;
552 struct tevent_req *req = NULL;
553 NTSTATUS status = NT_STATUS_OK;
555 if (cli_has_async_calls(cli)) {
557 * Can't use sync call while an async call is in flight
559 status = NT_STATUS_INVALID_PARAMETER;
563 ev = event_context_init(frame);
565 status = NT_STATUS_NO_MEMORY;
569 req = cli_posix_hardlink_send(frame,
575 status = NT_STATUS_NO_MEMORY;
579 if (!tevent_req_poll(req, ev)) {
580 status = map_nt_error_from_unix(errno);
584 status = cli_posix_hardlink_recv(req);
591 /****************************************************************************
592 Do a POSIX getfacl (UNIX extensions).
593 ****************************************************************************/
595 struct getfacl_state {
600 static void cli_posix_getfacl_done(struct tevent_req *subreq);
602 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
603 struct event_context *ev,
604 struct cli_state *cli,
607 struct tevent_req *req = NULL, *subreq = NULL;
608 struct getfacl_state *state = NULL;
610 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
614 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
616 if (tevent_req_nomem(subreq, req)) {
617 return tevent_req_post(req, ev);
619 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
623 static void cli_posix_getfacl_done(struct tevent_req *subreq)
625 struct tevent_req *req = tevent_req_callback_data(
626 subreq, struct tevent_req);
627 struct getfacl_state *state = tevent_req_data(
628 req, struct getfacl_state);
631 status = cli_qpathinfo_recv(subreq, state, &state->data,
634 if (tevent_req_nterror(req, status)) {
637 tevent_req_done(req);
640 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
645 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
648 if (tevent_req_is_nterror(req, &status)) {
651 *prb_size = (size_t)state->num_data;
652 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
656 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
662 TALLOC_CTX *frame = talloc_stackframe();
663 struct event_context *ev = NULL;
664 struct tevent_req *req = NULL;
665 NTSTATUS status = NT_STATUS_OK;
667 if (cli_has_async_calls(cli)) {
669 * Can't use sync call while an async call is in flight
671 status = NT_STATUS_INVALID_PARAMETER;
675 ev = event_context_init(frame);
677 status = NT_STATUS_NO_MEMORY;
681 req = cli_posix_getfacl_send(frame,
686 status = NT_STATUS_NO_MEMORY;
690 if (!tevent_req_poll(req, ev)) {
691 status = map_nt_error_from_unix(errno);
695 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
702 /****************************************************************************
703 Stat a file (UNIX extensions).
704 ****************************************************************************/
711 static void cli_posix_stat_done(struct tevent_req *subreq);
713 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
714 struct event_context *ev,
715 struct cli_state *cli,
718 struct tevent_req *req = NULL, *subreq = NULL;
719 struct stat_state *state = NULL;
721 req = tevent_req_create(mem_ctx, &state, struct stat_state);
725 subreq = cli_qpathinfo_send(state, ev, cli, fname,
726 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
727 if (tevent_req_nomem(subreq, req)) {
728 return tevent_req_post(req, ev);
730 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
734 static void cli_posix_stat_done(struct tevent_req *subreq)
736 struct tevent_req *req = tevent_req_callback_data(
737 subreq, struct tevent_req);
738 struct stat_state *state = tevent_req_data(req, struct stat_state);
741 status = cli_qpathinfo_recv(subreq, state, &state->data,
744 if (tevent_req_nterror(req, status)) {
747 tevent_req_done(req);
750 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
751 SMB_STRUCT_STAT *sbuf)
753 struct stat_state *state = tevent_req_data(req, struct stat_state);
756 if (tevent_req_is_nterror(req, &status)) {
760 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
761 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
762 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
763 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
765 /* assume 512 byte blocks */
766 sbuf->st_ex_blocks /= 512;
768 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
769 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
770 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
772 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
773 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
774 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
775 #if defined(HAVE_MAKEDEV)
777 uint32_t dev_major = IVAL(state->data,60);
778 uint32_t dev_minor = IVAL(state->data,68);
779 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
782 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
783 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
784 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
789 NTSTATUS cli_posix_stat(struct cli_state *cli,
791 SMB_STRUCT_STAT *sbuf)
793 TALLOC_CTX *frame = talloc_stackframe();
794 struct event_context *ev = NULL;
795 struct tevent_req *req = NULL;
796 NTSTATUS status = NT_STATUS_OK;
798 if (cli_has_async_calls(cli)) {
800 * Can't use sync call while an async call is in flight
802 status = NT_STATUS_INVALID_PARAMETER;
806 ev = event_context_init(frame);
808 status = NT_STATUS_NO_MEMORY;
812 req = cli_posix_stat_send(frame,
817 status = NT_STATUS_NO_MEMORY;
821 if (!tevent_req_poll(req, ev)) {
822 status = map_nt_error_from_unix(errno);
826 status = cli_posix_stat_recv(req, sbuf);
833 /****************************************************************************
834 Chmod or chown a file internal (UNIX extensions).
835 ****************************************************************************/
837 struct cli_posix_chown_chmod_internal_state {
841 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
843 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
844 struct event_context *ev,
845 struct cli_state *cli,
851 struct tevent_req *req = NULL, *subreq = NULL;
852 struct cli_posix_chown_chmod_internal_state *state = NULL;
854 req = tevent_req_create(mem_ctx, &state,
855 struct cli_posix_chown_chmod_internal_state);
860 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
861 memset(&state->data[40], '\0', 60);
862 SIVAL(state->data,40,uid);
863 SIVAL(state->data,48,gid);
864 SIVAL(state->data,84,mode);
866 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
867 fname, state->data, sizeof(state->data));
868 if (tevent_req_nomem(subreq, req)) {
869 return tevent_req_post(req, ev);
871 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
876 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
878 NTSTATUS status = cli_setpathinfo_recv(subreq);
879 tevent_req_simple_finish_ntstatus(subreq, status);
882 /****************************************************************************
883 chmod a file (UNIX extensions).
884 ****************************************************************************/
886 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
887 struct event_context *ev,
888 struct cli_state *cli,
892 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
894 unix_perms_to_wire(mode),
899 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
901 return tevent_req_simple_recv_ntstatus(req);
904 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
906 TALLOC_CTX *frame = talloc_stackframe();
907 struct event_context *ev = NULL;
908 struct tevent_req *req = NULL;
909 NTSTATUS status = NT_STATUS_OK;
911 if (cli_has_async_calls(cli)) {
913 * Can't use sync call while an async call is in flight
915 status = NT_STATUS_INVALID_PARAMETER;
919 ev = event_context_init(frame);
921 status = NT_STATUS_NO_MEMORY;
925 req = cli_posix_chmod_send(frame,
931 status = NT_STATUS_NO_MEMORY;
935 if (!tevent_req_poll(req, ev)) {
936 status = map_nt_error_from_unix(errno);
940 status = cli_posix_chmod_recv(req);
947 /****************************************************************************
948 chown a file (UNIX extensions).
949 ****************************************************************************/
951 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
952 struct event_context *ev,
953 struct cli_state *cli,
958 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
965 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
967 return tevent_req_simple_recv_ntstatus(req);
970 NTSTATUS cli_posix_chown(struct cli_state *cli,
975 TALLOC_CTX *frame = talloc_stackframe();
976 struct event_context *ev = NULL;
977 struct tevent_req *req = NULL;
978 NTSTATUS status = NT_STATUS_OK;
980 if (cli_has_async_calls(cli)) {
982 * Can't use sync call while an async call is in flight
984 status = NT_STATUS_INVALID_PARAMETER;
988 ev = event_context_init(frame);
990 status = NT_STATUS_NO_MEMORY;
994 req = cli_posix_chown_send(frame,
1001 status = NT_STATUS_NO_MEMORY;
1005 if (!tevent_req_poll(req, ev)) {
1006 status = map_nt_error_from_unix(errno);
1010 status = cli_posix_chown_recv(req);
1017 /****************************************************************************
1019 ****************************************************************************/
1021 static void cli_rename_done(struct tevent_req *subreq);
1023 struct cli_rename_state {
1027 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1028 struct event_context *ev,
1029 struct cli_state *cli,
1030 const char *fname_src,
1031 const char *fname_dst)
1033 struct tevent_req *req = NULL, *subreq = NULL;
1034 struct cli_rename_state *state = NULL;
1035 uint8_t additional_flags = 0;
1036 uint8_t *bytes = NULL;
1038 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1043 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1045 bytes = talloc_array(state, uint8_t, 1);
1046 if (tevent_req_nomem(bytes, req)) {
1047 return tevent_req_post(req, ev);
1050 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1051 strlen(fname_src)+1, NULL);
1052 if (tevent_req_nomem(bytes, req)) {
1053 return tevent_req_post(req, ev);
1056 bytes = talloc_realloc(state, bytes, uint8_t,
1057 talloc_get_size(bytes)+1);
1058 if (tevent_req_nomem(bytes, req)) {
1059 return tevent_req_post(req, ev);
1062 bytes[talloc_get_size(bytes)-1] = 4;
1063 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1064 strlen(fname_dst)+1, NULL);
1065 if (tevent_req_nomem(bytes, req)) {
1066 return tevent_req_post(req, ev);
1069 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1070 1, state->vwv, talloc_get_size(bytes), bytes);
1071 if (tevent_req_nomem(subreq, req)) {
1072 return tevent_req_post(req, ev);
1074 tevent_req_set_callback(subreq, cli_rename_done, req);
1078 static void cli_rename_done(struct tevent_req *subreq)
1080 struct tevent_req *req = tevent_req_callback_data(
1081 subreq, struct tevent_req);
1084 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1085 TALLOC_FREE(subreq);
1086 if (tevent_req_nterror(req, status)) {
1089 tevent_req_done(req);
1092 NTSTATUS cli_rename_recv(struct tevent_req *req)
1094 return tevent_req_simple_recv_ntstatus(req);
1097 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1099 TALLOC_CTX *frame = talloc_stackframe();
1100 struct event_context *ev;
1101 struct tevent_req *req;
1102 NTSTATUS status = NT_STATUS_OK;
1104 if (cli_has_async_calls(cli)) {
1106 * Can't use sync call while an async call is in flight
1108 status = NT_STATUS_INVALID_PARAMETER;
1112 ev = event_context_init(frame);
1114 status = NT_STATUS_NO_MEMORY;
1118 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1120 status = NT_STATUS_NO_MEMORY;
1124 if (!tevent_req_poll(req, ev)) {
1125 status = map_nt_error_from_unix(errno);
1129 status = cli_rename_recv(req);
1136 /****************************************************************************
1138 ****************************************************************************/
1140 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1142 struct cli_ntrename_internal_state {
1146 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1147 struct event_context *ev,
1148 struct cli_state *cli,
1149 const char *fname_src,
1150 const char *fname_dst,
1151 uint16_t rename_flag)
1153 struct tevent_req *req = NULL, *subreq = NULL;
1154 struct cli_ntrename_internal_state *state = NULL;
1155 uint8_t additional_flags = 0;
1156 uint8_t *bytes = NULL;
1158 req = tevent_req_create(mem_ctx, &state,
1159 struct cli_ntrename_internal_state);
1164 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1165 SSVAL(state->vwv+1, 0, rename_flag);
1167 bytes = talloc_array(state, uint8_t, 1);
1168 if (tevent_req_nomem(bytes, req)) {
1169 return tevent_req_post(req, ev);
1172 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1173 strlen(fname_src)+1, NULL);
1174 if (tevent_req_nomem(bytes, req)) {
1175 return tevent_req_post(req, ev);
1178 bytes = talloc_realloc(state, bytes, uint8_t,
1179 talloc_get_size(bytes)+1);
1180 if (tevent_req_nomem(bytes, req)) {
1181 return tevent_req_post(req, ev);
1184 bytes[talloc_get_size(bytes)-1] = 4;
1185 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1186 strlen(fname_dst)+1, NULL);
1187 if (tevent_req_nomem(bytes, req)) {
1188 return tevent_req_post(req, ev);
1191 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1192 4, state->vwv, talloc_get_size(bytes), bytes);
1193 if (tevent_req_nomem(subreq, req)) {
1194 return tevent_req_post(req, ev);
1196 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1200 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1202 struct tevent_req *req = tevent_req_callback_data(
1203 subreq, struct tevent_req);
1206 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1207 TALLOC_FREE(subreq);
1208 if (tevent_req_nterror(req, status)) {
1211 tevent_req_done(req);
1214 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1216 return tevent_req_simple_recv_ntstatus(req);
1219 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1220 struct event_context *ev,
1221 struct cli_state *cli,
1222 const char *fname_src,
1223 const char *fname_dst)
1225 return cli_ntrename_internal_send(mem_ctx,
1230 RENAME_FLAG_RENAME);
1233 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1235 return cli_ntrename_internal_recv(req);
1238 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1240 TALLOC_CTX *frame = talloc_stackframe();
1241 struct event_context *ev;
1242 struct tevent_req *req;
1243 NTSTATUS status = NT_STATUS_OK;
1245 if (cli_has_async_calls(cli)) {
1247 * Can't use sync call while an async call is in flight
1249 status = NT_STATUS_INVALID_PARAMETER;
1253 ev = event_context_init(frame);
1255 status = NT_STATUS_NO_MEMORY;
1259 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1261 status = NT_STATUS_NO_MEMORY;
1265 if (!tevent_req_poll(req, ev)) {
1266 status = map_nt_error_from_unix(errno);
1270 status = cli_ntrename_recv(req);
1277 /****************************************************************************
1279 ****************************************************************************/
1281 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1282 struct event_context *ev,
1283 struct cli_state *cli,
1284 const char *fname_src,
1285 const char *fname_dst)
1287 return cli_ntrename_internal_send(mem_ctx,
1292 RENAME_FLAG_HARD_LINK);
1295 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1297 return cli_ntrename_internal_recv(req);
1300 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1302 TALLOC_CTX *frame = talloc_stackframe();
1303 struct event_context *ev;
1304 struct tevent_req *req;
1305 NTSTATUS status = NT_STATUS_OK;
1307 if (cli_has_async_calls(cli)) {
1309 * Can't use sync call while an async call is in flight
1311 status = NT_STATUS_INVALID_PARAMETER;
1315 ev = event_context_init(frame);
1317 status = NT_STATUS_NO_MEMORY;
1321 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1323 status = NT_STATUS_NO_MEMORY;
1327 if (!tevent_req_poll(req, ev)) {
1328 status = map_nt_error_from_unix(errno);
1332 status = cli_nt_hardlink_recv(req);
1339 /****************************************************************************
1341 ****************************************************************************/
1343 static void cli_unlink_done(struct tevent_req *subreq);
1345 struct cli_unlink_state {
1349 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1350 struct event_context *ev,
1351 struct cli_state *cli,
1353 uint16_t mayhave_attrs)
1355 struct tevent_req *req = NULL, *subreq = NULL;
1356 struct cli_unlink_state *state = NULL;
1357 uint8_t additional_flags = 0;
1358 uint8_t *bytes = NULL;
1360 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1365 SSVAL(state->vwv+0, 0, mayhave_attrs);
1367 bytes = talloc_array(state, uint8_t, 1);
1368 if (tevent_req_nomem(bytes, req)) {
1369 return tevent_req_post(req, ev);
1372 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1373 strlen(fname)+1, NULL);
1375 if (tevent_req_nomem(bytes, req)) {
1376 return tevent_req_post(req, ev);
1379 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1380 1, state->vwv, talloc_get_size(bytes), bytes);
1381 if (tevent_req_nomem(subreq, req)) {
1382 return tevent_req_post(req, ev);
1384 tevent_req_set_callback(subreq, cli_unlink_done, req);
1388 static void cli_unlink_done(struct tevent_req *subreq)
1390 struct tevent_req *req = tevent_req_callback_data(
1391 subreq, struct tevent_req);
1394 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1395 TALLOC_FREE(subreq);
1396 if (tevent_req_nterror(req, status)) {
1399 tevent_req_done(req);
1402 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1404 return tevent_req_simple_recv_ntstatus(req);
1407 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1409 TALLOC_CTX *frame = talloc_stackframe();
1410 struct event_context *ev;
1411 struct tevent_req *req;
1412 NTSTATUS status = NT_STATUS_OK;
1414 if (cli_has_async_calls(cli)) {
1416 * Can't use sync call while an async call is in flight
1418 status = NT_STATUS_INVALID_PARAMETER;
1422 ev = event_context_init(frame);
1424 status = NT_STATUS_NO_MEMORY;
1428 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1430 status = NT_STATUS_NO_MEMORY;
1434 if (!tevent_req_poll(req, ev)) {
1435 status = map_nt_error_from_unix(errno);
1439 status = cli_unlink_recv(req);
1446 /****************************************************************************
1448 ****************************************************************************/
1450 static void cli_mkdir_done(struct tevent_req *subreq);
1452 struct cli_mkdir_state {
1456 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1457 struct event_context *ev,
1458 struct cli_state *cli,
1461 struct tevent_req *req = NULL, *subreq = NULL;
1462 struct cli_mkdir_state *state = NULL;
1463 uint8_t additional_flags = 0;
1464 uint8_t *bytes = NULL;
1466 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1471 bytes = talloc_array(state, uint8_t, 1);
1472 if (tevent_req_nomem(bytes, req)) {
1473 return tevent_req_post(req, ev);
1476 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1477 strlen(dname)+1, NULL);
1479 if (tevent_req_nomem(bytes, req)) {
1480 return tevent_req_post(req, ev);
1483 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1484 0, NULL, talloc_get_size(bytes), bytes);
1485 if (tevent_req_nomem(subreq, req)) {
1486 return tevent_req_post(req, ev);
1488 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1492 static void cli_mkdir_done(struct tevent_req *subreq)
1494 struct tevent_req *req = tevent_req_callback_data(
1495 subreq, struct tevent_req);
1498 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1499 TALLOC_FREE(subreq);
1500 if (tevent_req_nterror(req, status)) {
1503 tevent_req_done(req);
1506 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1508 return tevent_req_simple_recv_ntstatus(req);
1511 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
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_mkdir_send(frame, ev, cli, dname);
1534 status = NT_STATUS_NO_MEMORY;
1538 if (!tevent_req_poll(req, ev)) {
1539 status = map_nt_error_from_unix(errno);
1543 status = cli_mkdir_recv(req);
1550 /****************************************************************************
1552 ****************************************************************************/
1554 static void cli_rmdir_done(struct tevent_req *subreq);
1556 struct cli_rmdir_state {
1560 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1561 struct event_context *ev,
1562 struct cli_state *cli,
1565 struct tevent_req *req = NULL, *subreq = NULL;
1566 struct cli_rmdir_state *state = NULL;
1567 uint8_t additional_flags = 0;
1568 uint8_t *bytes = NULL;
1570 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1575 bytes = talloc_array(state, uint8_t, 1);
1576 if (tevent_req_nomem(bytes, req)) {
1577 return tevent_req_post(req, ev);
1580 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1581 strlen(dname)+1, NULL);
1583 if (tevent_req_nomem(bytes, req)) {
1584 return tevent_req_post(req, ev);
1587 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1588 0, NULL, talloc_get_size(bytes), bytes);
1589 if (tevent_req_nomem(subreq, req)) {
1590 return tevent_req_post(req, ev);
1592 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1596 static void cli_rmdir_done(struct tevent_req *subreq)
1598 struct tevent_req *req = tevent_req_callback_data(
1599 subreq, struct tevent_req);
1602 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1603 TALLOC_FREE(subreq);
1604 if (tevent_req_nterror(req, status)) {
1607 tevent_req_done(req);
1610 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1612 return tevent_req_simple_recv_ntstatus(req);
1615 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1617 TALLOC_CTX *frame = talloc_stackframe();
1618 struct event_context *ev;
1619 struct tevent_req *req;
1620 NTSTATUS status = NT_STATUS_OK;
1622 if (cli_has_async_calls(cli)) {
1624 * Can't use sync call while an async call is in flight
1626 status = NT_STATUS_INVALID_PARAMETER;
1630 ev = event_context_init(frame);
1632 status = NT_STATUS_NO_MEMORY;
1636 req = cli_rmdir_send(frame, ev, cli, dname);
1638 status = NT_STATUS_NO_MEMORY;
1642 if (!tevent_req_poll(req, ev)) {
1643 status = map_nt_error_from_unix(errno);
1647 status = cli_rmdir_recv(req);
1654 /****************************************************************************
1655 Set or clear the delete on close flag.
1656 ****************************************************************************/
1664 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1666 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1667 NULL, 0, NULL, NULL, 0, NULL);
1668 tevent_req_simple_finish_ntstatus(subreq, status);
1671 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1672 struct event_context *ev,
1673 struct cli_state *cli,
1677 struct tevent_req *req = NULL, *subreq = NULL;
1678 struct doc_state *state = NULL;
1680 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1685 /* Setup setup word. */
1686 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1688 /* Setup param array. */
1689 SSVAL(state->param,0,fnum);
1690 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1692 /* Setup data array. */
1693 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1695 subreq = cli_trans_send(state, /* mem ctx. */
1696 ev, /* event ctx. */
1697 cli, /* cli_state. */
1698 SMBtrans2, /* cmd. */
1699 NULL, /* pipe name. */
1703 &state->setup, /* setup. */
1704 1, /* num setup uint16_t words. */
1705 0, /* max returned setup. */
1706 state->param, /* param. */
1708 2, /* max returned param. */
1709 state->data, /* data. */
1711 0); /* max returned data. */
1713 if (tevent_req_nomem(subreq, req)) {
1714 return tevent_req_post(req, ev);
1716 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1720 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1722 return tevent_req_simple_recv_ntstatus(req);
1725 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1727 TALLOC_CTX *frame = talloc_stackframe();
1728 struct event_context *ev = NULL;
1729 struct tevent_req *req = NULL;
1730 NTSTATUS status = NT_STATUS_OK;
1732 if (cli_has_async_calls(cli)) {
1734 * Can't use sync call while an async call is in flight
1736 status = NT_STATUS_INVALID_PARAMETER;
1740 ev = event_context_init(frame);
1742 status = NT_STATUS_NO_MEMORY;
1746 req = cli_nt_delete_on_close_send(frame,
1752 status = NT_STATUS_NO_MEMORY;
1756 if (!tevent_req_poll(req, ev)) {
1757 status = map_nt_error_from_unix(errno);
1761 status = cli_nt_delete_on_close_recv(req);
1768 struct cli_ntcreate_state {
1773 static void cli_ntcreate_done(struct tevent_req *subreq);
1775 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1776 struct event_context *ev,
1777 struct cli_state *cli,
1779 uint32_t CreatFlags,
1780 uint32_t DesiredAccess,
1781 uint32_t FileAttributes,
1782 uint32_t ShareAccess,
1783 uint32_t CreateDisposition,
1784 uint32_t CreateOptions,
1785 uint8_t SecurityFlags)
1787 struct tevent_req *req, *subreq;
1788 struct cli_ntcreate_state *state;
1791 size_t converted_len;
1793 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1800 SCVAL(vwv+0, 0, 0xFF);
1805 if (cli->use_oplocks) {
1806 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1808 SIVAL(vwv+3, 1, CreatFlags);
1809 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1810 SIVAL(vwv+7, 1, DesiredAccess);
1811 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1812 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1813 SIVAL(vwv+13, 1, FileAttributes);
1814 SIVAL(vwv+15, 1, ShareAccess);
1815 SIVAL(vwv+17, 1, CreateDisposition);
1816 SIVAL(vwv+19, 1, CreateOptions);
1817 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1818 SCVAL(vwv+23, 1, SecurityFlags);
1820 bytes = talloc_array(state, uint8_t, 0);
1821 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1822 fname, strlen(fname)+1,
1825 /* sigh. this copes with broken netapp filer behaviour */
1826 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1828 if (tevent_req_nomem(bytes, req)) {
1829 return tevent_req_post(req, ev);
1832 SSVAL(vwv+2, 1, converted_len);
1834 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1835 talloc_get_size(bytes), bytes);
1836 if (tevent_req_nomem(subreq, req)) {
1837 return tevent_req_post(req, ev);
1839 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1843 static void cli_ntcreate_done(struct tevent_req *subreq)
1845 struct tevent_req *req = tevent_req_callback_data(
1846 subreq, struct tevent_req);
1847 struct cli_ntcreate_state *state = tevent_req_data(
1848 req, struct cli_ntcreate_state);
1856 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1857 &num_bytes, &bytes);
1858 TALLOC_FREE(subreq);
1859 if (tevent_req_nterror(req, status)) {
1862 state->fnum = SVAL(vwv+2, 1);
1863 tevent_req_done(req);
1866 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1868 struct cli_ntcreate_state *state = tevent_req_data(
1869 req, struct cli_ntcreate_state);
1872 if (tevent_req_is_nterror(req, &status)) {
1875 *pfnum = state->fnum;
1876 return NT_STATUS_OK;
1879 NTSTATUS cli_ntcreate(struct cli_state *cli,
1881 uint32_t CreatFlags,
1882 uint32_t DesiredAccess,
1883 uint32_t FileAttributes,
1884 uint32_t ShareAccess,
1885 uint32_t CreateDisposition,
1886 uint32_t CreateOptions,
1887 uint8_t SecurityFlags,
1890 TALLOC_CTX *frame = talloc_stackframe();
1891 struct event_context *ev;
1892 struct tevent_req *req;
1893 NTSTATUS status = NT_STATUS_OK;
1895 if (cli_has_async_calls(cli)) {
1897 * Can't use sync call while an async call is in flight
1899 status = NT_STATUS_INVALID_PARAMETER;
1903 ev = event_context_init(frame);
1905 status = NT_STATUS_NO_MEMORY;
1909 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1910 DesiredAccess, FileAttributes, ShareAccess,
1911 CreateDisposition, CreateOptions,
1914 status = NT_STATUS_NO_MEMORY;
1918 if (!tevent_req_poll(req, ev)) {
1919 status = map_nt_error_from_unix(errno);
1923 status = cli_ntcreate_recv(req, pfid);
1929 struct cli_nttrans_create_state {
1933 static void cli_nttrans_create_done(struct tevent_req *subreq);
1935 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
1936 struct event_context *ev,
1937 struct cli_state *cli,
1939 uint32_t CreatFlags,
1940 uint32_t DesiredAccess,
1941 uint32_t FileAttributes,
1942 uint32_t ShareAccess,
1943 uint32_t CreateDisposition,
1944 uint32_t CreateOptions,
1945 uint8_t SecurityFlags,
1946 struct security_descriptor *secdesc,
1947 struct ea_struct *eas,
1950 struct tevent_req *req, *subreq;
1951 struct cli_nttrans_create_state *state;
1953 uint8_t *secdesc_buf;
1956 size_t converted_len;
1958 req = tevent_req_create(mem_ctx,
1959 &state, struct cli_nttrans_create_state);
1964 if (secdesc != NULL) {
1965 status = marshall_sec_desc(talloc_tos(), secdesc,
1966 &secdesc_buf, &secdesc_len);
1967 if (tevent_req_nterror(req, status)) {
1968 DEBUG(10, ("marshall_sec_desc failed: %s\n",
1969 nt_errstr(status)));
1970 return tevent_req_post(req, ev);
1981 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
1982 return tevent_req_post(req, ev);
1985 param = talloc_array(state, uint8_t, 53);
1986 if (tevent_req_nomem(param, req)) {
1987 return tevent_req_post(req, ev);
1990 param = trans2_bytes_push_str(param, cli_ucs2(cli),
1991 fname, strlen(fname),
1993 if (tevent_req_nomem(param, req)) {
1994 return tevent_req_post(req, ev);
1997 SIVAL(param, 0, CreatFlags);
1998 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
1999 SIVAL(param, 8, DesiredAccess);
2000 SIVAL(param, 12, 0x0); /* AllocationSize */
2001 SIVAL(param, 16, 0x0); /* AllocationSize */
2002 SIVAL(param, 20, FileAttributes);
2003 SIVAL(param, 24, ShareAccess);
2004 SIVAL(param, 28, CreateDisposition);
2005 SIVAL(param, 32, CreateOptions);
2006 SIVAL(param, 36, secdesc_len);
2007 SIVAL(param, 40, 0); /* EA length*/
2008 SIVAL(param, 44, converted_len);
2009 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2010 SCVAL(param, 52, SecurityFlags);
2012 subreq = cli_trans_send(state, ev, cli, SMBnttrans,
2013 NULL, -1, /* name, fid */
2014 NT_TRANSACT_CREATE, 0,
2015 NULL, 0, 0, /* setup */
2016 param, talloc_get_size(param), 128, /* param */
2017 secdesc_buf, secdesc_len, 0); /* data */
2018 if (tevent_req_nomem(subreq, req)) {
2019 return tevent_req_post(req, ev);
2021 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2025 static void cli_nttrans_create_done(struct tevent_req *subreq)
2027 struct tevent_req *req = tevent_req_callback_data(
2028 subreq, struct tevent_req);
2029 struct cli_nttrans_create_state *state = tevent_req_data(
2030 req, struct cli_nttrans_create_state);
2035 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2036 NULL, 0, NULL, /* rsetup */
2037 ¶m, 69, &num_param,
2039 if (tevent_req_nterror(req, status)) {
2042 state->fnum = SVAL(param, 2);
2044 tevent_req_done(req);
2047 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req, uint16_t *fnum)
2049 struct cli_nttrans_create_state *state = tevent_req_data(
2050 req, struct cli_nttrans_create_state);
2053 if (tevent_req_is_nterror(req, &status)) {
2056 *fnum = state->fnum;
2057 return NT_STATUS_OK;
2060 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2062 uint32_t CreatFlags,
2063 uint32_t DesiredAccess,
2064 uint32_t FileAttributes,
2065 uint32_t ShareAccess,
2066 uint32_t CreateDisposition,
2067 uint32_t CreateOptions,
2068 uint8_t SecurityFlags,
2069 struct security_descriptor *secdesc,
2070 struct ea_struct *eas,
2074 TALLOC_CTX *frame = talloc_stackframe();
2075 struct event_context *ev;
2076 struct tevent_req *req;
2077 NTSTATUS status = NT_STATUS_NO_MEMORY;
2079 if (cli_has_async_calls(cli)) {
2081 * Can't use sync call while an async call is in flight
2083 status = NT_STATUS_INVALID_PARAMETER;
2086 ev = event_context_init(frame);
2090 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2091 DesiredAccess, FileAttributes,
2092 ShareAccess, CreateDisposition,
2093 CreateOptions, SecurityFlags,
2094 secdesc, eas, num_eas);
2098 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2101 status = cli_nttrans_create_recv(req, pfid);
2107 /****************************************************************************
2109 WARNING: if you open with O_WRONLY then getattrE won't work!
2110 ****************************************************************************/
2112 struct cli_open_state {
2113 struct tevent_context *ev;
2114 struct cli_state *cli;
2120 uint8_t additional_flags;
2124 static void cli_open_done(struct tevent_req *subreq);
2125 static void cli_open_ntcreate_done(struct tevent_req *subreq);
2127 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2128 struct event_context *ev,
2129 struct cli_state *cli, const char *fname,
2130 int flags, int share_mode,
2131 struct tevent_req **psmbreq)
2133 struct tevent_req *req, *subreq;
2134 struct cli_open_state *state;
2137 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2143 state->fname = fname;
2145 if (flags & O_CREAT) {
2146 state->openfn |= (1<<4);
2148 if (!(flags & O_EXCL)) {
2149 if (flags & O_TRUNC)
2150 state->openfn |= (1<<1);
2152 state->openfn |= (1<<0);
2155 state->dos_deny = (share_mode<<4);
2157 if ((flags & O_ACCMODE) == O_RDWR) {
2158 state->dos_deny |= 2;
2159 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2160 state->dos_deny |= 1;
2164 if ((flags & O_SYNC) == O_SYNC) {
2165 state->dos_deny |= (1<<14);
2169 if (share_mode == DENY_FCB) {
2170 state->dos_deny = 0xFF;
2173 SCVAL(state->vwv + 0, 0, 0xFF);
2174 SCVAL(state->vwv + 0, 1, 0);
2175 SSVAL(state->vwv + 1, 0, 0);
2176 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2177 SSVAL(state->vwv + 3, 0, state->dos_deny);
2178 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2179 SSVAL(state->vwv + 5, 0, 0);
2180 SIVAL(state->vwv + 6, 0, 0);
2181 SSVAL(state->vwv + 8, 0, state->openfn);
2182 SIVAL(state->vwv + 9, 0, 0);
2183 SIVAL(state->vwv + 11, 0, 0);
2184 SIVAL(state->vwv + 13, 0, 0);
2186 if (cli->use_oplocks) {
2187 /* if using oplocks then ask for a batch oplock via
2188 core and extended methods */
2189 state->additional_flags =
2190 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2191 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2194 bytes = talloc_array(state, uint8_t, 0);
2195 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2196 strlen(fname)+1, NULL);
2198 if (tevent_req_nomem(bytes, req)) {
2199 return tevent_req_post(req, ev);
2202 state->bytes.iov_base = (void *)bytes;
2203 state->bytes.iov_len = talloc_get_size(bytes);
2205 subreq = cli_smb_req_create(state, ev, cli, SMBopenX,
2206 state->additional_flags,
2207 15, state->vwv, 1, &state->bytes);
2208 if (subreq == NULL) {
2212 tevent_req_set_callback(subreq, cli_open_done, req);
2217 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2218 struct cli_state *cli, const char *fname,
2219 int flags, int share_mode)
2221 struct tevent_req *req, *subreq;
2224 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2230 status = cli_smb_req_send(subreq);
2231 if (tevent_req_nterror(req, status)) {
2232 return tevent_req_post(req, ev);
2237 static void cli_open_done(struct tevent_req *subreq)
2239 struct tevent_req *req = tevent_req_callback_data(
2240 subreq, struct tevent_req);
2241 struct cli_open_state *state = tevent_req_data(
2242 req, struct cli_open_state);
2247 uint32_t access_mask, share_mode, create_disposition, create_options;
2249 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2251 TALLOC_FREE(subreq);
2253 if (NT_STATUS_IS_OK(status)) {
2254 state->fnum = SVAL(vwv+2, 0);
2255 tevent_req_done(req);
2259 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
2260 tevent_req_nterror(req, status);
2265 * For the new shiny OS/X Lion SMB server, try a ntcreate
2269 if (!map_open_params_to_ntcreate(state->fname, state->dos_deny,
2270 state->openfn, &access_mask,
2271 &share_mode, &create_disposition,
2272 &create_options, NULL)) {
2273 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
2277 subreq = cli_ntcreate_send(state, state->ev, state->cli,
2278 state->fname, 0, access_mask,
2279 0, share_mode, create_disposition,
2281 if (tevent_req_nomem(subreq, req)) {
2284 tevent_req_set_callback(subreq, cli_open_ntcreate_done, req);
2287 static void cli_open_ntcreate_done(struct tevent_req *subreq)
2289 struct tevent_req *req = tevent_req_callback_data(
2290 subreq, struct tevent_req);
2291 struct cli_open_state *state = tevent_req_data(
2292 req, struct cli_open_state);
2295 status = cli_ntcreate_recv(subreq, &state->fnum);
2296 TALLOC_FREE(subreq);
2297 if (tevent_req_nterror(req, status)) {
2300 tevent_req_done(req);
2303 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2305 struct cli_open_state *state = tevent_req_data(
2306 req, struct cli_open_state);
2309 if (tevent_req_is_nterror(req, &status)) {
2312 *pfnum = state->fnum;
2313 return NT_STATUS_OK;
2316 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2317 int share_mode, uint16_t *pfnum)
2319 TALLOC_CTX *frame = talloc_stackframe();
2320 struct event_context *ev;
2321 struct tevent_req *req;
2322 NTSTATUS status = NT_STATUS_OK;
2324 if (cli_has_async_calls(cli)) {
2326 * Can't use sync call while an async call is in flight
2328 status = NT_STATUS_INVALID_PARAMETER;
2332 ev = event_context_init(frame);
2334 status = NT_STATUS_NO_MEMORY;
2338 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2340 status = NT_STATUS_NO_MEMORY;
2344 if (!tevent_req_poll(req, ev)) {
2345 status = map_nt_error_from_unix(errno);
2349 status = cli_open_recv(req, pfnum);
2355 /****************************************************************************
2357 ****************************************************************************/
2359 struct cli_close_state {
2363 static void cli_close_done(struct tevent_req *subreq);
2365 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2366 struct event_context *ev,
2367 struct cli_state *cli,
2369 struct tevent_req **psubreq)
2371 struct tevent_req *req, *subreq;
2372 struct cli_close_state *state;
2374 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2379 SSVAL(state->vwv+0, 0, fnum);
2380 SIVALS(state->vwv+1, 0, -1);
2382 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2384 if (subreq == NULL) {
2388 tevent_req_set_callback(subreq, cli_close_done, req);
2393 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2394 struct event_context *ev,
2395 struct cli_state *cli,
2398 struct tevent_req *req, *subreq;
2401 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2406 status = cli_smb_req_send(subreq);
2407 if (tevent_req_nterror(req, status)) {
2408 return tevent_req_post(req, ev);
2413 static void cli_close_done(struct tevent_req *subreq)
2415 struct tevent_req *req = tevent_req_callback_data(
2416 subreq, struct tevent_req);
2419 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2420 TALLOC_FREE(subreq);
2421 if (tevent_req_nterror(req, status)) {
2424 tevent_req_done(req);
2427 NTSTATUS cli_close_recv(struct tevent_req *req)
2429 return tevent_req_simple_recv_ntstatus(req);
2432 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2434 TALLOC_CTX *frame = talloc_stackframe();
2435 struct event_context *ev;
2436 struct tevent_req *req;
2437 NTSTATUS status = NT_STATUS_OK;
2439 if (cli_has_async_calls(cli)) {
2441 * Can't use sync call while an async call is in flight
2443 status = NT_STATUS_INVALID_PARAMETER;
2447 ev = event_context_init(frame);
2449 status = NT_STATUS_NO_MEMORY;
2453 req = cli_close_send(frame, ev, cli, fnum);
2455 status = NT_STATUS_NO_MEMORY;
2459 if (!tevent_req_poll(req, ev)) {
2460 status = map_nt_error_from_unix(errno);
2464 status = cli_close_recv(req);
2470 /****************************************************************************
2471 Truncate a file to a specified size
2472 ****************************************************************************/
2474 struct ftrunc_state {
2480 static void cli_ftruncate_done(struct tevent_req *subreq)
2482 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2483 NULL, 0, NULL, NULL, 0, NULL);
2484 tevent_req_simple_finish_ntstatus(subreq, status);
2487 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2488 struct event_context *ev,
2489 struct cli_state *cli,
2493 struct tevent_req *req = NULL, *subreq = NULL;
2494 struct ftrunc_state *state = NULL;
2496 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2501 /* Setup setup word. */
2502 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2504 /* Setup param array. */
2505 SSVAL(state->param,0,fnum);
2506 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2507 SSVAL(state->param,4,0);
2509 /* Setup data array. */
2510 SBVAL(state->data, 0, size);
2512 subreq = cli_trans_send(state, /* mem ctx. */
2513 ev, /* event ctx. */
2514 cli, /* cli_state. */
2515 SMBtrans2, /* cmd. */
2516 NULL, /* pipe name. */
2520 &state->setup, /* setup. */
2521 1, /* num setup uint16_t words. */
2522 0, /* max returned setup. */
2523 state->param, /* param. */
2525 2, /* max returned param. */
2526 state->data, /* data. */
2528 0); /* max returned data. */
2530 if (tevent_req_nomem(subreq, req)) {
2531 return tevent_req_post(req, ev);
2533 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2537 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2539 return tevent_req_simple_recv_ntstatus(req);
2542 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2544 TALLOC_CTX *frame = talloc_stackframe();
2545 struct event_context *ev = NULL;
2546 struct tevent_req *req = NULL;
2547 NTSTATUS status = NT_STATUS_OK;
2549 if (cli_has_async_calls(cli)) {
2551 * Can't use sync call while an async call is in flight
2553 status = NT_STATUS_INVALID_PARAMETER;
2557 ev = event_context_init(frame);
2559 status = NT_STATUS_NO_MEMORY;
2563 req = cli_ftruncate_send(frame,
2569 status = NT_STATUS_NO_MEMORY;
2573 if (!tevent_req_poll(req, ev)) {
2574 status = map_nt_error_from_unix(errno);
2578 status = cli_ftruncate_recv(req);
2585 /****************************************************************************
2586 send a lock with a specified locktype
2587 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2588 ****************************************************************************/
2590 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2591 uint32_t offset, uint32_t len,
2592 int timeout, unsigned char locktype)
2597 unsigned int set_timeout = 0;
2598 unsigned int saved_timeout = 0;
2600 SCVAL(vwv + 0, 0, 0xff);
2601 SCVAL(vwv + 0, 1, 0);
2602 SSVAL(vwv + 1, 0, 0);
2603 SSVAL(vwv + 2, 0, fnum);
2604 SCVAL(vwv + 3, 0, locktype);
2605 SCVAL(vwv + 3, 1, 0);
2606 SIVALS(vwv + 4, 0, timeout);
2607 SSVAL(vwv + 6, 0, 0);
2608 SSVAL(vwv + 7, 0, 1);
2610 SSVAL(bytes, 0, cli_getpid(cli));
2611 SIVAL(bytes, 2, offset);
2612 SIVAL(bytes, 6, len);
2615 if (timeout == -1) {
2616 set_timeout = 0x7FFFFFFF;
2618 set_timeout = timeout + 2*1000;
2620 saved_timeout = cli_set_timeout(cli, set_timeout);
2623 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2624 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2626 if (saved_timeout != 0) {
2627 cli_set_timeout(cli, saved_timeout);
2633 /****************************************************************************
2635 note that timeout is in units of 2 milliseconds
2636 ****************************************************************************/
2638 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
2639 uint32_t offset, uint32_t len, int timeout,
2640 enum brl_type lock_type)
2644 status = cli_locktype(cli, fnum, offset, len, timeout,
2645 (lock_type == READ_LOCK? 1 : 0));
2649 /****************************************************************************
2651 ****************************************************************************/
2653 struct cli_unlock_state {
2658 static void cli_unlock_done(struct tevent_req *subreq);
2660 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2661 struct event_context *ev,
2662 struct cli_state *cli,
2668 struct tevent_req *req = NULL, *subreq = NULL;
2669 struct cli_unlock_state *state = NULL;
2670 uint8_t additional_flags = 0;
2672 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2677 SCVAL(state->vwv+0, 0, 0xFF);
2678 SSVAL(state->vwv+2, 0, fnum);
2679 SCVAL(state->vwv+3, 0, 0);
2680 SIVALS(state->vwv+4, 0, 0);
2681 SSVAL(state->vwv+6, 0, 1);
2682 SSVAL(state->vwv+7, 0, 0);
2684 SSVAL(state->data, 0, cli_getpid(cli));
2685 SIVAL(state->data, 2, offset);
2686 SIVAL(state->data, 6, len);
2688 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2689 8, state->vwv, 10, state->data);
2690 if (tevent_req_nomem(subreq, req)) {
2691 return tevent_req_post(req, ev);
2693 tevent_req_set_callback(subreq, cli_unlock_done, req);
2697 static void cli_unlock_done(struct tevent_req *subreq)
2699 struct tevent_req *req = tevent_req_callback_data(
2700 subreq, struct tevent_req);
2703 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2704 TALLOC_FREE(subreq);
2705 if (tevent_req_nterror(req, status)) {
2708 tevent_req_done(req);
2711 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2713 return tevent_req_simple_recv_ntstatus(req);
2716 NTSTATUS cli_unlock(struct cli_state *cli,
2721 TALLOC_CTX *frame = talloc_stackframe();
2722 struct event_context *ev;
2723 struct tevent_req *req;
2724 NTSTATUS status = NT_STATUS_OK;
2726 if (cli_has_async_calls(cli)) {
2728 * Can't use sync call while an async call is in flight
2730 status = NT_STATUS_INVALID_PARAMETER;
2734 ev = event_context_init(frame);
2736 status = NT_STATUS_NO_MEMORY;
2740 req = cli_unlock_send(frame, ev, cli,
2743 status = NT_STATUS_NO_MEMORY;
2747 if (!tevent_req_poll(req, ev)) {
2748 status = map_nt_error_from_unix(errno);
2752 status = cli_unlock_recv(req);
2759 /****************************************************************************
2760 Lock a file with 64 bit offsets.
2761 ****************************************************************************/
2763 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
2764 uint64_t offset, uint64_t len, int timeout,
2765 enum brl_type lock_type)
2769 unsigned int set_timeout = 0;
2770 unsigned int saved_timeout = 0;
2774 if (! (cli_state_capabilities(cli) & CAP_LARGE_FILES)) {
2775 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
2778 ltype = (lock_type == READ_LOCK? 1 : 0);
2779 ltype |= LOCKING_ANDX_LARGE_FILES;
2781 SCVAL(vwv + 0, 0, 0xff);
2782 SCVAL(vwv + 0, 1, 0);
2783 SSVAL(vwv + 1, 0, 0);
2784 SSVAL(vwv + 2, 0, fnum);
2785 SCVAL(vwv + 3, 0, ltype);
2786 SCVAL(vwv + 3, 1, 0);
2787 SIVALS(vwv + 4, 0, timeout);
2788 SSVAL(vwv + 6, 0, 0);
2789 SSVAL(vwv + 7, 0, 1);
2791 SIVAL(bytes, 0, cli_getpid(cli));
2792 SOFF_T_R(bytes, 4, offset);
2793 SOFF_T_R(bytes, 12, len);
2796 if (timeout == -1) {
2797 set_timeout = 0x7FFFFFFF;
2799 set_timeout = timeout + 2*1000;
2801 saved_timeout = cli_set_timeout(cli, set_timeout);
2804 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2805 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2807 if (saved_timeout != 0) {
2808 cli_set_timeout(cli, saved_timeout);
2814 /****************************************************************************
2815 Unlock a file with 64 bit offsets.
2816 ****************************************************************************/
2818 struct cli_unlock64_state {
2823 static void cli_unlock64_done(struct tevent_req *subreq);
2825 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2826 struct event_context *ev,
2827 struct cli_state *cli,
2833 struct tevent_req *req = NULL, *subreq = NULL;
2834 struct cli_unlock64_state *state = NULL;
2835 uint8_t additional_flags = 0;
2837 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2842 SCVAL(state->vwv+0, 0, 0xff);
2843 SSVAL(state->vwv+2, 0, fnum);
2844 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2845 SIVALS(state->vwv+4, 0, 0);
2846 SSVAL(state->vwv+6, 0, 1);
2847 SSVAL(state->vwv+7, 0, 0);
2849 SIVAL(state->data, 0, cli_getpid(cli));
2850 SOFF_T_R(state->data, 4, offset);
2851 SOFF_T_R(state->data, 12, len);
2853 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2854 8, state->vwv, 20, state->data);
2855 if (tevent_req_nomem(subreq, req)) {
2856 return tevent_req_post(req, ev);
2858 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2862 static void cli_unlock64_done(struct tevent_req *subreq)
2864 struct tevent_req *req = tevent_req_callback_data(
2865 subreq, struct tevent_req);
2868 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2869 TALLOC_FREE(subreq);
2870 if (tevent_req_nterror(req, status)) {
2873 tevent_req_done(req);
2876 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2878 return tevent_req_simple_recv_ntstatus(req);
2881 NTSTATUS cli_unlock64(struct cli_state *cli,
2886 TALLOC_CTX *frame = talloc_stackframe();
2887 struct event_context *ev;
2888 struct tevent_req *req;
2889 NTSTATUS status = NT_STATUS_OK;
2891 if (! (cli_state_capabilities(cli) & CAP_LARGE_FILES)) {
2892 return cli_unlock(cli, fnum, offset, len);
2895 if (cli_has_async_calls(cli)) {
2897 * Can't use sync call while an async call is in flight
2899 status = NT_STATUS_INVALID_PARAMETER;
2903 ev = event_context_init(frame);
2905 status = NT_STATUS_NO_MEMORY;
2909 req = cli_unlock64_send(frame, ev, cli,
2912 status = NT_STATUS_NO_MEMORY;
2916 if (!tevent_req_poll(req, ev)) {
2917 status = map_nt_error_from_unix(errno);
2921 status = cli_unlock64_recv(req);
2928 /****************************************************************************
2929 Get/unlock a POSIX lock on a file - internal function.
2930 ****************************************************************************/
2932 struct posix_lock_state {
2935 uint8_t data[POSIX_LOCK_DATA_SIZE];
2938 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2940 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2941 NULL, 0, NULL, NULL, 0, NULL);
2942 tevent_req_simple_finish_ntstatus(subreq, status);
2945 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2946 struct event_context *ev,
2947 struct cli_state *cli,
2952 enum brl_type lock_type)
2954 struct tevent_req *req = NULL, *subreq = NULL;
2955 struct posix_lock_state *state = NULL;
2957 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2962 /* Setup setup word. */
2963 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2965 /* Setup param array. */
2966 SSVAL(&state->param, 0, fnum);
2967 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2969 /* Setup data array. */
2970 switch (lock_type) {
2972 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2973 POSIX_LOCK_TYPE_READ);
2976 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2977 POSIX_LOCK_TYPE_WRITE);
2980 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2981 POSIX_LOCK_TYPE_UNLOCK);
2988 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2989 POSIX_LOCK_FLAG_WAIT);
2991 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2992 POSIX_LOCK_FLAG_NOWAIT);
2995 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
2996 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2997 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2999 subreq = cli_trans_send(state, /* mem ctx. */
3000 ev, /* event ctx. */
3001 cli, /* cli_state. */
3002 SMBtrans2, /* cmd. */
3003 NULL, /* pipe name. */
3007 &state->setup, /* setup. */
3008 1, /* num setup uint16_t words. */
3009 0, /* max returned setup. */
3010 state->param, /* param. */
3012 2, /* max returned param. */
3013 state->data, /* data. */
3014 POSIX_LOCK_DATA_SIZE, /* num data. */
3015 0); /* max returned data. */
3017 if (tevent_req_nomem(subreq, req)) {
3018 return tevent_req_post(req, ev);
3020 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3024 /****************************************************************************
3026 ****************************************************************************/
3028 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3029 struct event_context *ev,
3030 struct cli_state *cli,
3035 enum brl_type lock_type)
3037 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3038 wait_lock, lock_type);
3041 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3043 return tevent_req_simple_recv_ntstatus(req);
3046 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3047 uint64_t offset, uint64_t len,
3048 bool wait_lock, enum brl_type lock_type)
3050 TALLOC_CTX *frame = talloc_stackframe();
3051 struct event_context *ev = NULL;
3052 struct tevent_req *req = NULL;
3053 NTSTATUS status = NT_STATUS_OK;
3055 if (cli_has_async_calls(cli)) {
3057 * Can't use sync call while an async call is in flight
3059 status = NT_STATUS_INVALID_PARAMETER;
3063 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3064 status = NT_STATUS_INVALID_PARAMETER;
3068 ev = event_context_init(frame);
3070 status = NT_STATUS_NO_MEMORY;
3074 req = cli_posix_lock_send(frame,
3083 status = NT_STATUS_NO_MEMORY;
3087 if (!tevent_req_poll(req, ev)) {
3088 status = map_nt_error_from_unix(errno);
3092 status = cli_posix_lock_recv(req);
3099 /****************************************************************************
3100 POSIX Unlock a file.
3101 ****************************************************************************/
3103 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3104 struct event_context *ev,
3105 struct cli_state *cli,
3110 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3111 false, UNLOCK_LOCK);
3114 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3116 return tevent_req_simple_recv_ntstatus(req);
3119 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3121 TALLOC_CTX *frame = talloc_stackframe();
3122 struct event_context *ev = NULL;
3123 struct tevent_req *req = NULL;
3124 NTSTATUS status = NT_STATUS_OK;
3126 if (cli_has_async_calls(cli)) {
3128 * Can't use sync call while an async call is in flight
3130 status = NT_STATUS_INVALID_PARAMETER;
3134 ev = event_context_init(frame);
3136 status = NT_STATUS_NO_MEMORY;
3140 req = cli_posix_unlock_send(frame,
3147 status = NT_STATUS_NO_MEMORY;
3151 if (!tevent_req_poll(req, ev)) {
3152 status = map_nt_error_from_unix(errno);
3156 status = cli_posix_unlock_recv(req);
3163 /****************************************************************************
3164 Do a SMBgetattrE call.
3165 ****************************************************************************/
3167 static void cli_getattrE_done(struct tevent_req *subreq);
3169 struct cli_getattrE_state {
3179 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3180 struct event_context *ev,
3181 struct cli_state *cli,
3184 struct tevent_req *req = NULL, *subreq = NULL;
3185 struct cli_getattrE_state *state = NULL;
3186 uint8_t additional_flags = 0;
3188 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3193 state->zone_offset = cli->serverzone;
3194 SSVAL(state->vwv+0,0,fnum);
3196 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3197 1, state->vwv, 0, NULL);
3198 if (tevent_req_nomem(subreq, req)) {
3199 return tevent_req_post(req, ev);
3201 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3205 static void cli_getattrE_done(struct tevent_req *subreq)
3207 struct tevent_req *req = tevent_req_callback_data(
3208 subreq, struct tevent_req);
3209 struct cli_getattrE_state *state = tevent_req_data(
3210 req, struct cli_getattrE_state);
3212 uint16_t *vwv = NULL;
3216 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3218 TALLOC_FREE(subreq);
3219 if (tevent_req_nterror(req, status)) {
3223 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3224 state->attr = SVAL(vwv+10,0);
3225 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3226 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3227 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3229 tevent_req_done(req);
3232 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3235 time_t *change_time,
3236 time_t *access_time,
3239 struct cli_getattrE_state *state = tevent_req_data(
3240 req, struct cli_getattrE_state);
3243 if (tevent_req_is_nterror(req, &status)) {
3247 *attr = state->attr;
3250 *size = state->size;
3253 *change_time = state->change_time;
3256 *access_time = state->access_time;
3259 *write_time = state->write_time;
3261 return NT_STATUS_OK;
3264 NTSTATUS cli_getattrE(struct cli_state *cli,
3268 time_t *change_time,
3269 time_t *access_time,
3272 TALLOC_CTX *frame = talloc_stackframe();
3273 struct event_context *ev = NULL;
3274 struct tevent_req *req = NULL;
3275 NTSTATUS status = NT_STATUS_OK;
3277 if (cli_has_async_calls(cli)) {
3279 * Can't use sync call while an async call is in flight
3281 status = NT_STATUS_INVALID_PARAMETER;
3285 ev = event_context_init(frame);
3287 status = NT_STATUS_NO_MEMORY;
3291 req = cli_getattrE_send(frame, ev, cli, fnum);
3293 status = NT_STATUS_NO_MEMORY;
3297 if (!tevent_req_poll(req, ev)) {
3298 status = map_nt_error_from_unix(errno);
3302 status = cli_getattrE_recv(req,
3314 /****************************************************************************
3316 ****************************************************************************/
3318 static void cli_getatr_done(struct tevent_req *subreq);
3320 struct cli_getatr_state {
3327 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3328 struct event_context *ev,
3329 struct cli_state *cli,
3332 struct tevent_req *req = NULL, *subreq = NULL;
3333 struct cli_getatr_state *state = NULL;
3334 uint8_t additional_flags = 0;
3335 uint8_t *bytes = NULL;
3337 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3342 state->zone_offset = cli->serverzone;
3344 bytes = talloc_array(state, uint8_t, 1);
3345 if (tevent_req_nomem(bytes, req)) {
3346 return tevent_req_post(req, ev);
3349 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3350 strlen(fname)+1, NULL);
3352 if (tevent_req_nomem(bytes, req)) {
3353 return tevent_req_post(req, ev);
3356 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3357 0, NULL, talloc_get_size(bytes), bytes);
3358 if (tevent_req_nomem(subreq, req)) {
3359 return tevent_req_post(req, ev);
3361 tevent_req_set_callback(subreq, cli_getatr_done, req);
3365 static void cli_getatr_done(struct tevent_req *subreq)
3367 struct tevent_req *req = tevent_req_callback_data(
3368 subreq, struct tevent_req);
3369 struct cli_getatr_state *state = tevent_req_data(
3370 req, struct cli_getatr_state);
3372 uint16_t *vwv = NULL;
3376 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3378 TALLOC_FREE(subreq);
3379 if (tevent_req_nterror(req, status)) {
3383 state->attr = SVAL(vwv+0,0);
3384 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3385 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3387 tevent_req_done(req);
3390 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3395 struct cli_getatr_state *state = tevent_req_data(
3396 req, struct cli_getatr_state);
3399 if (tevent_req_is_nterror(req, &status)) {
3403 *attr = state->attr;
3406 *size = state->size;
3409 *write_time = state->write_time;
3411 return NT_STATUS_OK;
3414 NTSTATUS cli_getatr(struct cli_state *cli,
3420 TALLOC_CTX *frame = talloc_stackframe();
3421 struct event_context *ev = NULL;
3422 struct tevent_req *req = NULL;
3423 NTSTATUS status = NT_STATUS_OK;
3425 if (cli_has_async_calls(cli)) {
3427 * Can't use sync call while an async call is in flight
3429 status = NT_STATUS_INVALID_PARAMETER;
3433 ev = event_context_init(frame);
3435 status = NT_STATUS_NO_MEMORY;
3439 req = cli_getatr_send(frame, ev, cli, fname);
3441 status = NT_STATUS_NO_MEMORY;
3445 if (!tevent_req_poll(req, ev)) {
3446 status = map_nt_error_from_unix(errno);
3450 status = cli_getatr_recv(req,
3460 /****************************************************************************
3461 Do a SMBsetattrE call.
3462 ****************************************************************************/
3464 static void cli_setattrE_done(struct tevent_req *subreq);
3466 struct cli_setattrE_state {
3470 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3471 struct event_context *ev,
3472 struct cli_state *cli,
3478 struct tevent_req *req = NULL, *subreq = NULL;
3479 struct cli_setattrE_state *state = NULL;
3480 uint8_t additional_flags = 0;
3482 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3487 SSVAL(state->vwv+0, 0, fnum);
3488 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3490 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3492 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3495 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3496 7, state->vwv, 0, NULL);
3497 if (tevent_req_nomem(subreq, req)) {
3498 return tevent_req_post(req, ev);
3500 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3504 static void cli_setattrE_done(struct tevent_req *subreq)
3506 struct tevent_req *req = tevent_req_callback_data(
3507 subreq, struct tevent_req);
3510 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3511 TALLOC_FREE(subreq);
3512 if (tevent_req_nterror(req, status)) {
3515 tevent_req_done(req);
3518 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3520 return tevent_req_simple_recv_ntstatus(req);
3523 NTSTATUS cli_setattrE(struct cli_state *cli,
3529 TALLOC_CTX *frame = talloc_stackframe();
3530 struct event_context *ev = NULL;
3531 struct tevent_req *req = NULL;
3532 NTSTATUS status = NT_STATUS_OK;
3534 if (cli_has_async_calls(cli)) {
3536 * Can't use sync call while an async call is in flight
3538 status = NT_STATUS_INVALID_PARAMETER;
3542 ev = event_context_init(frame);
3544 status = NT_STATUS_NO_MEMORY;
3548 req = cli_setattrE_send(frame, ev,
3556 status = NT_STATUS_NO_MEMORY;
3560 if (!tevent_req_poll(req, ev)) {
3561 status = map_nt_error_from_unix(errno);
3565 status = cli_setattrE_recv(req);
3572 /****************************************************************************
3573 Do a SMBsetatr call.
3574 ****************************************************************************/
3576 static void cli_setatr_done(struct tevent_req *subreq);
3578 struct cli_setatr_state {
3582 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3583 struct event_context *ev,
3584 struct cli_state *cli,
3589 struct tevent_req *req = NULL, *subreq = NULL;
3590 struct cli_setatr_state *state = NULL;
3591 uint8_t additional_flags = 0;
3592 uint8_t *bytes = NULL;
3594 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3599 SSVAL(state->vwv+0, 0, attr);
3600 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
3602 bytes = talloc_array(state, uint8_t, 1);
3603 if (tevent_req_nomem(bytes, req)) {
3604 return tevent_req_post(req, ev);
3607 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3608 strlen(fname)+1, NULL);
3609 if (tevent_req_nomem(bytes, req)) {
3610 return tevent_req_post(req, ev);
3612 bytes = talloc_realloc(state, bytes, uint8_t,
3613 talloc_get_size(bytes)+1);
3614 if (tevent_req_nomem(bytes, req)) {
3615 return tevent_req_post(req, ev);
3618 bytes[talloc_get_size(bytes)-1] = 4;
3619 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3621 if (tevent_req_nomem(bytes, req)) {
3622 return tevent_req_post(req, ev);
3625 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3626 8, state->vwv, talloc_get_size(bytes), bytes);
3627 if (tevent_req_nomem(subreq, req)) {
3628 return tevent_req_post(req, ev);
3630 tevent_req_set_callback(subreq, cli_setatr_done, req);
3634 static void cli_setatr_done(struct tevent_req *subreq)
3636 struct tevent_req *req = tevent_req_callback_data(
3637 subreq, struct tevent_req);
3640 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3641 TALLOC_FREE(subreq);
3642 if (tevent_req_nterror(req, status)) {
3645 tevent_req_done(req);
3648 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3650 return tevent_req_simple_recv_ntstatus(req);
3653 NTSTATUS cli_setatr(struct cli_state *cli,
3658 TALLOC_CTX *frame = talloc_stackframe();
3659 struct event_context *ev = NULL;
3660 struct tevent_req *req = NULL;
3661 NTSTATUS status = NT_STATUS_OK;
3663 if (cli_has_async_calls(cli)) {
3665 * Can't use sync call while an async call is in flight
3667 status = NT_STATUS_INVALID_PARAMETER;
3671 ev = event_context_init(frame);
3673 status = NT_STATUS_NO_MEMORY;
3677 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3679 status = NT_STATUS_NO_MEMORY;
3683 if (!tevent_req_poll(req, ev)) {
3684 status = map_nt_error_from_unix(errno);
3688 status = cli_setatr_recv(req);
3695 /****************************************************************************
3696 Check for existance of a dir.
3697 ****************************************************************************/
3699 static void cli_chkpath_done(struct tevent_req *subreq);
3701 struct cli_chkpath_state {
3705 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3706 struct event_context *ev,
3707 struct cli_state *cli,
3710 struct tevent_req *req = NULL, *subreq = NULL;
3711 struct cli_chkpath_state *state = NULL;
3712 uint8_t additional_flags = 0;
3713 uint8_t *bytes = NULL;
3715 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3720 bytes = talloc_array(state, uint8_t, 1);
3721 if (tevent_req_nomem(bytes, req)) {
3722 return tevent_req_post(req, ev);
3725 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3726 strlen(fname)+1, NULL);
3728 if (tevent_req_nomem(bytes, req)) {
3729 return tevent_req_post(req, ev);
3732 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3733 0, NULL, talloc_get_size(bytes), bytes);
3734 if (tevent_req_nomem(subreq, req)) {
3735 return tevent_req_post(req, ev);
3737 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3741 static void cli_chkpath_done(struct tevent_req *subreq)
3743 struct tevent_req *req = tevent_req_callback_data(
3744 subreq, struct tevent_req);
3747 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3748 TALLOC_FREE(subreq);
3749 if (tevent_req_nterror(req, status)) {
3752 tevent_req_done(req);
3755 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3757 return tevent_req_simple_recv_ntstatus(req);
3760 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3762 TALLOC_CTX *frame = talloc_stackframe();
3763 struct event_context *ev = NULL;
3764 struct tevent_req *req = NULL;
3766 NTSTATUS status = NT_STATUS_OK;
3768 if (cli_has_async_calls(cli)) {
3770 * Can't use sync call while an async call is in flight
3772 status = NT_STATUS_INVALID_PARAMETER;
3776 path2 = talloc_strdup(frame, path);
3778 status = NT_STATUS_NO_MEMORY;
3781 trim_char(path2,'\0','\\');
3783 path2 = talloc_strdup(frame, "\\");
3785 status = NT_STATUS_NO_MEMORY;
3790 ev = event_context_init(frame);
3792 status = NT_STATUS_NO_MEMORY;
3796 req = cli_chkpath_send(frame, ev, cli, path2);
3798 status = NT_STATUS_NO_MEMORY;
3802 if (!tevent_req_poll(req, ev)) {
3803 status = map_nt_error_from_unix(errno);
3807 status = cli_chkpath_recv(req);
3814 /****************************************************************************
3816 ****************************************************************************/
3818 static void cli_dskattr_done(struct tevent_req *subreq);
3820 struct cli_dskattr_state {
3826 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3827 struct event_context *ev,
3828 struct cli_state *cli)
3830 struct tevent_req *req = NULL, *subreq = NULL;
3831 struct cli_dskattr_state *state = NULL;
3832 uint8_t additional_flags = 0;
3834 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3839 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3841 if (tevent_req_nomem(subreq, req)) {
3842 return tevent_req_post(req, ev);
3844 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3848 static void cli_dskattr_done(struct tevent_req *subreq)
3850 struct tevent_req *req = tevent_req_callback_data(
3851 subreq, struct tevent_req);
3852 struct cli_dskattr_state *state = tevent_req_data(
3853 req, struct cli_dskattr_state);
3855 uint16_t *vwv = NULL;
3859 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3861 TALLOC_FREE(subreq);
3862 if (tevent_req_nterror(req, status)) {
3865 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3866 state->total = SVAL(vwv+0, 0);
3867 state->avail = SVAL(vwv+3, 0);
3868 tevent_req_done(req);
3871 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3873 struct cli_dskattr_state *state = tevent_req_data(
3874 req, struct cli_dskattr_state);
3877 if (tevent_req_is_nterror(req, &status)) {
3880 *bsize = state->bsize;
3881 *total = state->total;
3882 *avail = state->avail;
3883 return NT_STATUS_OK;
3886 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3888 TALLOC_CTX *frame = talloc_stackframe();
3889 struct event_context *ev = NULL;
3890 struct tevent_req *req = NULL;
3891 NTSTATUS status = NT_STATUS_OK;
3893 if (cli_has_async_calls(cli)) {
3895 * Can't use sync call while an async call is in flight
3897 status = NT_STATUS_INVALID_PARAMETER;
3901 ev = event_context_init(frame);
3903 status = NT_STATUS_NO_MEMORY;
3907 req = cli_dskattr_send(frame, ev, cli);
3909 status = NT_STATUS_NO_MEMORY;
3913 if (!tevent_req_poll(req, ev)) {
3914 status = map_nt_error_from_unix(errno);
3918 status = cli_dskattr_recv(req, bsize, total, avail);
3925 /****************************************************************************
3926 Create and open a temporary file.
3927 ****************************************************************************/
3929 static void cli_ctemp_done(struct tevent_req *subreq);
3931 struct ctemp_state {
3937 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3938 struct event_context *ev,
3939 struct cli_state *cli,
3942 struct tevent_req *req = NULL, *subreq = NULL;
3943 struct ctemp_state *state = NULL;
3944 uint8_t additional_flags = 0;
3945 uint8_t *bytes = NULL;
3947 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3952 SSVAL(state->vwv,0,0);
3953 SIVALS(state->vwv+1,0,-1);
3955 bytes = talloc_array(state, uint8_t, 1);
3956 if (tevent_req_nomem(bytes, req)) {
3957 return tevent_req_post(req, ev);
3960 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3961 strlen(path)+1, NULL);
3962 if (tevent_req_nomem(bytes, req)) {
3963 return tevent_req_post(req, ev);
3966 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3967 3, state->vwv, talloc_get_size(bytes), bytes);
3968 if (tevent_req_nomem(subreq, req)) {
3969 return tevent_req_post(req, ev);
3971 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3975 static void cli_ctemp_done(struct tevent_req *subreq)
3977 struct tevent_req *req = tevent_req_callback_data(
3978 subreq, struct tevent_req);
3979 struct ctemp_state *state = tevent_req_data(
3980 req, struct ctemp_state);
3984 uint32_t num_bytes = 0;
3985 uint8_t *bytes = NULL;
3988 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3989 &num_bytes, &bytes);
3990 TALLOC_FREE(subreq);
3991 if (tevent_req_nterror(req, status)) {
3995 state->fnum = SVAL(vwv+0, 0);
3997 /* From W2K3, the result is just the ASCII name */
3998 if (num_bytes < 2) {
3999 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4003 if (pull_string_talloc(state,
4010 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4013 tevent_req_done(req);
4016 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4021 struct ctemp_state *state = tevent_req_data(req,
4022 struct ctemp_state);
4025 if (tevent_req_is_nterror(req, &status)) {
4028 *pfnum = state->fnum;
4029 *outfile = talloc_strdup(ctx, state->ret_path);
4031 return NT_STATUS_NO_MEMORY;
4033 return NT_STATUS_OK;
4036 NTSTATUS cli_ctemp(struct cli_state *cli,
4042 TALLOC_CTX *frame = talloc_stackframe();
4043 struct event_context *ev;
4044 struct tevent_req *req;
4045 NTSTATUS status = NT_STATUS_OK;
4047 if (cli_has_async_calls(cli)) {
4049 * Can't use sync call while an async call is in flight
4051 status = NT_STATUS_INVALID_PARAMETER;
4055 ev = event_context_init(frame);
4057 status = NT_STATUS_NO_MEMORY;
4061 req = cli_ctemp_send(frame, ev, cli, path);
4063 status = NT_STATUS_NO_MEMORY;
4067 if (!tevent_req_poll(req, ev)) {
4068 status = map_nt_error_from_unix(errno);
4072 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4080 send a raw ioctl - used by the torture code
4082 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4087 SSVAL(vwv+0, 0, fnum);
4088 SSVAL(vwv+1, 0, code>>16);
4089 SSVAL(vwv+2, 0, (code&0xFFFF));
4091 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4092 NULL, 0, NULL, NULL, NULL, NULL);
4093 if (!NT_STATUS_IS_OK(status)) {
4096 *blob = data_blob_null;
4097 return NT_STATUS_OK;
4100 /*********************************************************
4101 Set an extended attribute utility fn.
4102 *********************************************************/
4104 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4105 uint8_t *param, unsigned int param_len,
4106 const char *ea_name,
4107 const char *ea_val, size_t ea_len)
4110 unsigned int data_len = 0;
4111 uint8_t *data = NULL;
4113 size_t ea_namelen = strlen(ea_name);
4116 SSVAL(setup, 0, setup_val);
4118 if (ea_namelen == 0 && ea_len == 0) {
4120 data = (uint8_t *)SMB_MALLOC(data_len);
4122 return NT_STATUS_NO_MEMORY;
4125 SIVAL(p,0,data_len);
4127 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4128 data = (uint8_t *)SMB_MALLOC(data_len);
4130 return NT_STATUS_NO_MEMORY;
4133 SIVAL(p,0,data_len);
4135 SCVAL(p, 0, 0); /* EA flags. */
4136 SCVAL(p, 1, ea_namelen);
4137 SSVAL(p, 2, ea_len);
4138 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4139 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4142 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4144 param, param_len, 2,
4145 data, data_len, CLI_BUFFER_SIZE,
4147 NULL, 0, NULL, /* rsetup */
4148 NULL, 0, NULL, /* rparam */
4149 NULL, 0, NULL); /* rdata */
4154 /*********************************************************
4155 Set an extended attribute on a pathname.
4156 *********************************************************/
4158 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4159 const char *ea_name, const char *ea_val,
4162 unsigned int param_len = 0;
4165 TALLOC_CTX *frame = talloc_stackframe();
4167 param = talloc_array(talloc_tos(), uint8_t, 6);
4169 return NT_STATUS_NO_MEMORY;
4171 SSVAL(param,0,SMB_INFO_SET_EA);
4175 param = trans2_bytes_push_str(param, cli_ucs2(cli),
4176 path, strlen(path)+1,
4178 param_len = talloc_get_size(param);
4180 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4181 ea_name, ea_val, ea_len);
4186 /*********************************************************
4187 Set an extended attribute on an fnum.
4188 *********************************************************/
4190 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4191 const char *ea_name, const char *ea_val,
4196 memset(param, 0, 6);
4197 SSVAL(param,0,fnum);
4198 SSVAL(param,2,SMB_INFO_SET_EA);
4200 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4201 ea_name, ea_val, ea_len);
4204 /*********************************************************
4205 Get an extended attribute list utility fn.
4206 *********************************************************/
4208 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4210 size_t *pnum_eas, struct ea_struct **pea_list)
4212 struct ea_struct *ea_list = NULL;
4217 if (rdata_len < 4) {
4221 ea_size = (size_t)IVAL(rdata,0);
4222 if (ea_size > rdata_len) {
4227 /* No EA's present. */
4236 /* Validate the EA list and count it. */
4237 for (num_eas = 0; ea_size >= 4; num_eas++) {
4238 unsigned int ea_namelen = CVAL(p,1);
4239 unsigned int ea_valuelen = SVAL(p,2);
4240 if (ea_namelen == 0) {
4243 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4246 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4247 p += 4 + ea_namelen + 1 + ea_valuelen;
4256 *pnum_eas = num_eas;
4258 /* Caller only wants number of EA's. */
4262 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4267 ea_size = (size_t)IVAL(rdata,0);
4270 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4271 struct ea_struct *ea = &ea_list[num_eas];
4272 fstring unix_ea_name;
4273 unsigned int ea_namelen = CVAL(p,1);
4274 unsigned int ea_valuelen = SVAL(p,2);
4276 ea->flags = CVAL(p,0);
4277 unix_ea_name[0] = '\0';
4278 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4279 ea->name = talloc_strdup(ea_list, unix_ea_name);
4283 /* Ensure the value is null terminated (in case it's a string). */
4284 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4285 if (!ea->value.data) {
4289 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4291 ea->value.data[ea_valuelen] = 0;
4293 p += 4 + ea_namelen + 1 + ea_valuelen;
4296 *pea_list = ea_list;
4300 TALLOC_FREE(ea_list);
4304 /*********************************************************
4305 Get an extended attribute list from a pathname.
4306 *********************************************************/
4308 struct cli_get_ea_list_path_state {
4313 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4315 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4316 struct tevent_context *ev,
4317 struct cli_state *cli,
4320 struct tevent_req *req, *subreq;
4321 struct cli_get_ea_list_path_state *state;
4323 req = tevent_req_create(mem_ctx, &state,
4324 struct cli_get_ea_list_path_state);
4328 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4329 SMB_INFO_QUERY_ALL_EAS, 4,
4331 if (tevent_req_nomem(subreq, req)) {
4332 return tevent_req_post(req, ev);
4334 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4338 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4340 struct tevent_req *req = tevent_req_callback_data(
4341 subreq, struct tevent_req);
4342 struct cli_get_ea_list_path_state *state = tevent_req_data(
4343 req, struct cli_get_ea_list_path_state);
4346 status = cli_qpathinfo_recv(subreq, state, &state->data,
4348 TALLOC_FREE(subreq);
4349 if (tevent_req_nterror(req, status)) {
4352 tevent_req_done(req);
4355 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4356 size_t *pnum_eas, struct ea_struct **peas)
4358 struct cli_get_ea_list_path_state *state = tevent_req_data(
4359 req, struct cli_get_ea_list_path_state);
4362 if (tevent_req_is_nterror(req, &status)) {
4365 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4367 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4369 return NT_STATUS_OK;
4372 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4375 struct ea_struct **pea_list)
4377 TALLOC_CTX *frame = talloc_stackframe();
4378 struct event_context *ev = NULL;
4379 struct tevent_req *req = NULL;
4380 NTSTATUS status = NT_STATUS_NO_MEMORY;
4382 if (cli_has_async_calls(cli)) {
4384 * Can't use sync call while an async call is in flight
4386 status = NT_STATUS_INVALID_PARAMETER;
4389 ev = event_context_init(frame);
4393 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4397 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4400 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4406 /****************************************************************************
4407 Convert open "flags" arg to uint32_t on wire.
4408 ****************************************************************************/
4410 static uint32_t open_flags_to_wire(int flags)
4412 int open_mode = flags & O_ACCMODE;
4415 switch (open_mode) {
4417 ret |= SMB_O_WRONLY;
4424 ret |= SMB_O_RDONLY;
4428 if (flags & O_CREAT) {
4431 if (flags & O_EXCL) {
4434 if (flags & O_TRUNC) {
4438 if (flags & O_SYNC) {
4442 if (flags & O_APPEND) {
4443 ret |= SMB_O_APPEND;
4445 #if defined(O_DIRECT)
4446 if (flags & O_DIRECT) {
4447 ret |= SMB_O_DIRECT;
4450 #if defined(O_DIRECTORY)
4451 if (flags & O_DIRECTORY) {
4452 ret |= SMB_O_DIRECTORY;
4458 /****************************************************************************
4459 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4460 ****************************************************************************/
4462 struct posix_open_state {
4466 uint16_t fnum; /* Out */
4469 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4471 struct tevent_req *req = tevent_req_callback_data(
4472 subreq, struct tevent_req);
4473 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4478 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4479 NULL, 0, NULL, &data, 12, &num_data);
4480 TALLOC_FREE(subreq);
4481 if (tevent_req_nterror(req, status)) {
4484 state->fnum = SVAL(data,2);
4485 tevent_req_done(req);
4488 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4489 struct event_context *ev,
4490 struct cli_state *cli,
4496 struct tevent_req *req = NULL, *subreq = NULL;
4497 struct posix_open_state *state = NULL;
4498 uint32_t wire_flags = open_flags_to_wire(flags);
4500 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4505 /* Setup setup word. */
4506 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4508 /* Setup param array. */
4509 state->param = talloc_array(state, uint8_t, 6);
4510 if (tevent_req_nomem(state->param, req)) {
4511 return tevent_req_post(req, ev);
4513 memset(state->param, '\0', 6);
4514 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4516 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4517 strlen(fname)+1, NULL);
4519 if (tevent_req_nomem(state->param, req)) {
4520 return tevent_req_post(req, ev);
4523 /* Setup data words. */
4525 wire_flags |= SMB_O_DIRECTORY;
4528 SIVAL(state->data,0,0); /* No oplock. */
4529 SIVAL(state->data,4,wire_flags);
4530 SIVAL(state->data,8,unix_perms_to_wire(mode));
4531 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4532 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4534 subreq = cli_trans_send(state, /* mem ctx. */
4535 ev, /* event ctx. */
4536 cli, /* cli_state. */
4537 SMBtrans2, /* cmd. */
4538 NULL, /* pipe name. */
4542 &state->setup, /* setup. */
4543 1, /* num setup uint16_t words. */
4544 0, /* max returned setup. */
4545 state->param, /* param. */
4546 talloc_get_size(state->param),/* num param. */
4547 2, /* max returned param. */
4548 state->data, /* data. */
4550 12); /* max returned data. */
4552 if (tevent_req_nomem(subreq, req)) {
4553 return tevent_req_post(req, ev);
4555 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4559 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4560 struct event_context *ev,
4561 struct cli_state *cli,
4566 return cli_posix_open_internal_send(mem_ctx, ev,
4567 cli, fname, flags, mode, false);
4570 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4572 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4575 if (tevent_req_is_nterror(req, &status)) {
4578 *pfnum = state->fnum;
4579 return NT_STATUS_OK;
4582 /****************************************************************************
4583 Open - POSIX semantics. Doesn't request oplock.
4584 ****************************************************************************/
4586 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4587 int flags, mode_t mode, uint16_t *pfnum)
4590 TALLOC_CTX *frame = talloc_stackframe();
4591 struct event_context *ev = NULL;
4592 struct tevent_req *req = NULL;
4593 NTSTATUS status = NT_STATUS_OK;
4595 if (cli_has_async_calls(cli)) {
4597 * Can't use sync call while an async call is in flight
4599 status = NT_STATUS_INVALID_PARAMETER;
4603 ev = event_context_init(frame);
4605 status = NT_STATUS_NO_MEMORY;
4609 req = cli_posix_open_send(frame,
4616 status = NT_STATUS_NO_MEMORY;
4620 if (!tevent_req_poll(req, ev)) {
4621 status = map_nt_error_from_unix(errno);
4625 status = cli_posix_open_recv(req, pfnum);
4632 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4633 struct event_context *ev,
4634 struct cli_state *cli,
4638 return cli_posix_open_internal_send(mem_ctx, ev,
4639 cli, fname, O_CREAT, mode, true);
4642 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4644 return tevent_req_simple_recv_ntstatus(req);
4647 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4649 TALLOC_CTX *frame = talloc_stackframe();
4650 struct event_context *ev = NULL;
4651 struct tevent_req *req = NULL;
4652 NTSTATUS status = NT_STATUS_OK;
4654 if (cli_has_async_calls(cli)) {
4656 * Can't use sync call while an async call is in flight
4658 status = NT_STATUS_INVALID_PARAMETER;
4662 ev = event_context_init(frame);
4664 status = NT_STATUS_NO_MEMORY;
4668 req = cli_posix_mkdir_send(frame,
4674 status = NT_STATUS_NO_MEMORY;
4678 if (!tevent_req_poll(req, ev)) {
4679 status = map_nt_error_from_unix(errno);
4683 status = cli_posix_mkdir_recv(req);
4690 /****************************************************************************
4691 unlink or rmdir - POSIX semantics.
4692 ****************************************************************************/
4694 struct cli_posix_unlink_internal_state {
4698 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4700 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4701 struct event_context *ev,
4702 struct cli_state *cli,
4706 struct tevent_req *req = NULL, *subreq = NULL;
4707 struct cli_posix_unlink_internal_state *state = NULL;
4709 req = tevent_req_create(mem_ctx, &state,
4710 struct cli_posix_unlink_internal_state);
4715 /* Setup data word. */
4716 SSVAL(state->data, 0, level);
4718 subreq = cli_setpathinfo_send(state, ev, cli,
4719 SMB_POSIX_PATH_UNLINK,
4721 state->data, sizeof(state->data));
4722 if (tevent_req_nomem(subreq, req)) {
4723 return tevent_req_post(req, ev);
4725 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4729 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4731 NTSTATUS status = cli_setpathinfo_recv(subreq);
4732 tevent_req_simple_finish_ntstatus(subreq, status);
4735 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4736 struct event_context *ev,
4737 struct cli_state *cli,
4740 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4741 SMB_POSIX_UNLINK_FILE_TARGET);
4744 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4746 return tevent_req_simple_recv_ntstatus(req);
4749 /****************************************************************************
4750 unlink - POSIX semantics.
4751 ****************************************************************************/
4753 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4755 TALLOC_CTX *frame = talloc_stackframe();
4756 struct event_context *ev = NULL;
4757 struct tevent_req *req = NULL;
4758 NTSTATUS status = NT_STATUS_OK;
4760 if (cli_has_async_calls(cli)) {
4762 * Can't use sync call while an async call is in flight
4764 status = NT_STATUS_INVALID_PARAMETER;
4768 ev = event_context_init(frame);
4770 status = NT_STATUS_NO_MEMORY;
4774 req = cli_posix_unlink_send(frame,
4779 status = NT_STATUS_NO_MEMORY;
4783 if (!tevent_req_poll(req, ev)) {
4784 status = map_nt_error_from_unix(errno);
4788 status = cli_posix_unlink_recv(req);
4795 /****************************************************************************
4796 rmdir - POSIX semantics.
4797 ****************************************************************************/
4799 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4800 struct event_context *ev,
4801 struct cli_state *cli,
4804 return cli_posix_unlink_internal_send(
4805 mem_ctx, ev, cli, fname,
4806 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4809 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4811 return tevent_req_simple_recv_ntstatus(req);
4814 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4816 TALLOC_CTX *frame = talloc_stackframe();
4817 struct event_context *ev = NULL;
4818 struct tevent_req *req = NULL;
4819 NTSTATUS status = NT_STATUS_OK;
4821 if (cli_has_async_calls(cli)) {
4823 * Can't use sync call while an async call is in flight
4825 status = NT_STATUS_INVALID_PARAMETER;
4829 ev = event_context_init(frame);
4831 status = NT_STATUS_NO_MEMORY;
4835 req = cli_posix_rmdir_send(frame,
4840 status = NT_STATUS_NO_MEMORY;
4844 if (!tevent_req_poll(req, ev)) {
4845 status = map_nt_error_from_unix(errno);
4849 status = cli_posix_rmdir_recv(req, frame);
4856 /****************************************************************************
4858 ****************************************************************************/
4860 struct cli_notify_state {
4862 uint32_t num_changes;
4863 struct notify_change *changes;
4866 static void cli_notify_done(struct tevent_req *subreq);
4868 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4869 struct tevent_context *ev,
4870 struct cli_state *cli, uint16_t fnum,
4871 uint32_t buffer_size,
4872 uint32_t completion_filter, bool recursive)
4874 struct tevent_req *req, *subreq;
4875 struct cli_notify_state *state;
4877 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4882 SIVAL(state->setup, 0, completion_filter);
4883 SSVAL(state->setup, 4, fnum);
4884 SSVAL(state->setup, 6, recursive);
4886 subreq = cli_trans_send(
4887 state, /* mem ctx. */
4888 ev, /* event ctx. */
4889 cli, /* cli_state. */
4890 SMBnttrans, /* cmd. */
4891 NULL, /* pipe name. */
4893 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4895 (uint16_t *)state->setup, /* setup. */
4896 4, /* num setup uint16_t words. */
4897 0, /* max returned setup. */
4900 buffer_size, /* max returned param. */
4903 0); /* max returned data. */
4905 if (tevent_req_nomem(subreq, req)) {
4906 return tevent_req_post(req, ev);
4908 tevent_req_set_callback(subreq, cli_notify_done, req);
4912 static void cli_notify_done(struct tevent_req *subreq)
4914 struct tevent_req *req = tevent_req_callback_data(
4915 subreq, struct tevent_req);
4916 struct cli_notify_state *state = tevent_req_data(
4917 req, struct cli_notify_state);
4920 uint32_t i, ofs, num_params;
4923 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4924 ¶ms, 0, &num_params, NULL, 0, NULL);
4925 TALLOC_FREE(subreq);
4926 if (tevent_req_nterror(req, status)) {
4927 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4931 state->num_changes = 0;
4934 while (num_params - ofs > 12) {
4935 uint32_t len = IVAL(params, ofs);
4936 state->num_changes += 1;
4938 if ((len == 0) || (ofs+len >= num_params)) {
4944 state->changes = talloc_array(state, struct notify_change,
4945 state->num_changes);
4946 if (tevent_req_nomem(state->changes, req)) {
4947 TALLOC_FREE(params);
4953 for (i=0; i<state->num_changes; i++) {
4954 uint32_t next = IVAL(params, ofs);
4955 uint32_t len = IVAL(params, ofs+8);
4959 if ((next != 0) && (len+12 != next)) {
4960 TALLOC_FREE(params);
4962 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4966 state->changes[i].action = IVAL(params, ofs+4);
4967 ret = clistr_pull_talloc(params, (char *)params, flags2,
4968 &name, params+ofs+12, len,
4969 STR_TERMINATE|STR_UNICODE);
4971 TALLOC_FREE(params);
4972 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4975 state->changes[i].name = name;
4979 TALLOC_FREE(params);
4980 tevent_req_done(req);
4983 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4984 uint32_t *pnum_changes,
4985 struct notify_change **pchanges)
4987 struct cli_notify_state *state = tevent_req_data(
4988 req, struct cli_notify_state);
4991 if (tevent_req_is_nterror(req, &status)) {
4995 *pnum_changes = state->num_changes;
4996 *pchanges = talloc_move(mem_ctx, &state->changes);
4997 return NT_STATUS_OK;
5000 struct cli_qpathinfo_state {
5009 static void cli_qpathinfo_done(struct tevent_req *subreq);
5011 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5012 struct tevent_context *ev,
5013 struct cli_state *cli, const char *fname,
5014 uint16_t level, uint32_t min_rdata,
5017 struct tevent_req *req, *subreq;
5018 struct cli_qpathinfo_state *state;
5020 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5024 state->min_rdata = min_rdata;
5025 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5027 state->param = talloc_zero_array(state, uint8_t, 6);
5028 if (tevent_req_nomem(state->param, req)) {
5029 return tevent_req_post(req, ev);
5031 SSVAL(state->param, 0, level);
5032 state->param = trans2_bytes_push_str(
5033 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
5034 if (tevent_req_nomem(state->param, req)) {
5035 return tevent_req_post(req, ev);
5038 subreq = cli_trans_send(
5039 state, /* mem ctx. */
5040 ev, /* event ctx. */
5041 cli, /* cli_state. */
5042 SMBtrans2, /* cmd. */
5043 NULL, /* pipe name. */
5047 state->setup, /* setup. */
5048 1, /* num setup uint16_t words. */
5049 0, /* max returned setup. */
5050 state->param, /* param. */
5051 talloc_get_size(state->param), /* num param. */
5052 2, /* max returned param. */
5055 max_rdata); /* max returned data. */
5057 if (tevent_req_nomem(subreq, req)) {
5058 return tevent_req_post(req, ev);
5060 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5064 static void cli_qpathinfo_done(struct tevent_req *subreq)
5066 struct tevent_req *req = tevent_req_callback_data(
5067 subreq, struct tevent_req);
5068 struct cli_qpathinfo_state *state = tevent_req_data(
5069 req, struct cli_qpathinfo_state);
5072 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5074 &state->rdata, state->min_rdata,
5076 if (tevent_req_nterror(req, status)) {
5079 tevent_req_done(req);
5082 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5083 uint8_t **rdata, uint32_t *num_rdata)
5085 struct cli_qpathinfo_state *state = tevent_req_data(
5086 req, struct cli_qpathinfo_state);
5089 if (tevent_req_is_nterror(req, &status)) {
5092 if (rdata != NULL) {
5093 *rdata = talloc_move(mem_ctx, &state->rdata);
5095 TALLOC_FREE(state->rdata);
5097 if (num_rdata != NULL) {
5098 *num_rdata = state->num_rdata;
5100 return NT_STATUS_OK;
5103 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5104 const char *fname, uint16_t level, uint32_t min_rdata,
5106 uint8_t **rdata, uint32_t *num_rdata)
5108 TALLOC_CTX *frame = talloc_stackframe();
5109 struct event_context *ev;
5110 struct tevent_req *req;
5111 NTSTATUS status = NT_STATUS_NO_MEMORY;
5113 if (cli_has_async_calls(cli)) {
5115 * Can't use sync call while an async call is in flight
5117 status = NT_STATUS_INVALID_PARAMETER;
5120 ev = event_context_init(frame);
5124 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5129 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5132 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5138 struct cli_qfileinfo_state {
5142 uint16_t recv_flags2;
5148 static void cli_qfileinfo_done(struct tevent_req *subreq);
5150 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5151 struct tevent_context *ev,
5152 struct cli_state *cli, uint16_t fnum,
5153 uint16_t level, uint32_t min_rdata,
5156 struct tevent_req *req, *subreq;
5157 struct cli_qfileinfo_state *state;
5159 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5163 state->min_rdata = min_rdata;
5164 SSVAL(state->param, 0, fnum);
5165 SSVAL(state->param, 2, level);
5166 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5168 subreq = cli_trans_send(
5169 state, /* mem ctx. */
5170 ev, /* event ctx. */
5171 cli, /* cli_state. */
5172 SMBtrans2, /* cmd. */
5173 NULL, /* pipe name. */
5177 state->setup, /* setup. */
5178 1, /* num setup uint16_t words. */
5179 0, /* max returned setup. */
5180 state->param, /* param. */
5181 sizeof(state->param), /* num param. */
5182 2, /* max returned param. */
5185 max_rdata); /* max returned data. */
5187 if (tevent_req_nomem(subreq, req)) {
5188 return tevent_req_post(req, ev);
5190 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5194 static void cli_qfileinfo_done(struct tevent_req *subreq)
5196 struct tevent_req *req = tevent_req_callback_data(
5197 subreq, struct tevent_req);
5198 struct cli_qfileinfo_state *state = tevent_req_data(
5199 req, struct cli_qfileinfo_state);
5202 status = cli_trans_recv(subreq, state,
5203 &state->recv_flags2,
5206 &state->rdata, state->min_rdata,
5208 if (tevent_req_nterror(req, status)) {
5211 tevent_req_done(req);
5214 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5215 uint16_t *recv_flags2,
5216 uint8_t **rdata, uint32_t *num_rdata)
5218 struct cli_qfileinfo_state *state = tevent_req_data(
5219 req, struct cli_qfileinfo_state);
5222 if (tevent_req_is_nterror(req, &status)) {
5226 if (recv_flags2 != NULL) {
5227 *recv_flags2 = state->recv_flags2;
5229 if (rdata != NULL) {
5230 *rdata = talloc_move(mem_ctx, &state->rdata);
5232 TALLOC_FREE(state->rdata);
5234 if (num_rdata != NULL) {
5235 *num_rdata = state->num_rdata;
5237 return NT_STATUS_OK;
5240 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5241 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5242 uint32_t max_rdata, uint16_t *recv_flags2,
5243 uint8_t **rdata, uint32_t *num_rdata)
5245 TALLOC_CTX *frame = talloc_stackframe();
5246 struct event_context *ev;
5247 struct tevent_req *req;
5248 NTSTATUS status = NT_STATUS_NO_MEMORY;
5250 if (cli_has_async_calls(cli)) {
5252 * Can't use sync call while an async call is in flight
5254 status = NT_STATUS_INVALID_PARAMETER;
5257 ev = event_context_init(frame);
5261 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5266 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5269 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5275 struct cli_flush_state {
5279 static void cli_flush_done(struct tevent_req *subreq);
5281 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5282 struct event_context *ev,
5283 struct cli_state *cli,
5286 struct tevent_req *req, *subreq;
5287 struct cli_flush_state *state;
5289 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5293 SSVAL(state->vwv + 0, 0, fnum);
5295 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5297 if (tevent_req_nomem(subreq, req)) {
5298 return tevent_req_post(req, ev);
5300 tevent_req_set_callback(subreq, cli_flush_done, req);
5304 static void cli_flush_done(struct tevent_req *subreq)
5306 struct tevent_req *req = tevent_req_callback_data(
5307 subreq, struct tevent_req);
5310 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5311 TALLOC_FREE(subreq);
5312 if (tevent_req_nterror(req, status)) {
5315 tevent_req_done(req);
5318 NTSTATUS cli_flush_recv(struct tevent_req *req)
5320 return tevent_req_simple_recv_ntstatus(req);
5323 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5325 TALLOC_CTX *frame = talloc_stackframe();
5326 struct event_context *ev;
5327 struct tevent_req *req;
5328 NTSTATUS status = NT_STATUS_NO_MEMORY;
5330 if (cli_has_async_calls(cli)) {
5332 * Can't use sync call while an async call is in flight
5334 status = NT_STATUS_INVALID_PARAMETER;
5337 ev = event_context_init(frame);
5341 req = cli_flush_send(frame, ev, cli, fnum);
5345 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5348 status = cli_flush_recv(req);
5354 struct cli_shadow_copy_data_state {
5361 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5363 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5364 struct tevent_context *ev,
5365 struct cli_state *cli,
5369 struct tevent_req *req, *subreq;
5370 struct cli_shadow_copy_data_state *state;
5373 req = tevent_req_create(mem_ctx, &state,
5374 struct cli_shadow_copy_data_state);
5378 state->get_names = get_names;
5379 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
5381 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5382 SSVAL(state->setup + 2, 0, fnum);
5383 SCVAL(state->setup + 3, 0, 0); /* isFsctl */
5384 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5386 subreq = cli_trans_send(
5387 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5388 state->setup, ARRAY_SIZE(state->setup), 0,
5391 if (tevent_req_nomem(subreq, req)) {
5392 return tevent_req_post(req, ev);
5394 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5398 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5400 struct tevent_req *req = tevent_req_callback_data(
5401 subreq, struct tevent_req);
5402 struct cli_shadow_copy_data_state *state = tevent_req_data(
5403 req, struct cli_shadow_copy_data_state);
5406 status = cli_trans_recv(subreq, state, NULL,
5407 NULL, 0, NULL, /* setup */
5408 NULL, 0, NULL, /* param */
5409 &state->data, 12, &state->num_data);
5410 TALLOC_FREE(subreq);
5411 if (tevent_req_nterror(req, status)) {
5414 tevent_req_done(req);
5417 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5418 char ***pnames, int *pnum_names)
5420 struct cli_shadow_copy_data_state *state = tevent_req_data(
5421 req, struct cli_shadow_copy_data_state);
5427 if (tevent_req_is_nterror(req, &status)) {
5430 num_names = IVAL(state->data, 4);
5431 dlength = IVAL(state->data, 8);
5433 if (!state->get_names) {
5434 *pnum_names = num_names;
5435 return NT_STATUS_OK;
5438 if (dlength+12 > state->num_data) {
5439 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5441 names = talloc_array(mem_ctx, char *, num_names);
5442 if (names == NULL) {
5443 return NT_STATUS_NO_MEMORY;
5446 for (i=0; i<num_names; i++) {
5449 size_t converted_size;
5451 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5452 ret = convert_string_talloc(
5453 names, CH_UTF16LE, CH_UNIX,
5454 src, 2 * sizeof(SHADOW_COPY_LABEL),
5455 &names[i], &converted_size);
5458 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5461 *pnum_names = num_names;
5463 return NT_STATUS_OK;
5466 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5467 uint16_t fnum, bool get_names,
5468 char ***pnames, int *pnum_names)
5470 TALLOC_CTX *frame = talloc_stackframe();
5471 struct event_context *ev;
5472 struct tevent_req *req;
5473 NTSTATUS status = NT_STATUS_NO_MEMORY;
5475 if (cli_has_async_calls(cli)) {
5477 * Can't use sync call while an async call is in flight
5479 status = NT_STATUS_INVALID_PARAMETER;
5482 ev = event_context_init(frame);
5486 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5490 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5493 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);