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 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
1818 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1819 SCVAL(vwv+23, 1, SecurityFlags);
1821 bytes = talloc_array(state, uint8_t, 0);
1822 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1823 fname, strlen(fname)+1,
1826 /* sigh. this copes with broken netapp filer behaviour */
1827 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1829 if (tevent_req_nomem(bytes, req)) {
1830 return tevent_req_post(req, ev);
1833 SSVAL(vwv+2, 1, converted_len);
1835 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1836 talloc_get_size(bytes), bytes);
1837 if (tevent_req_nomem(subreq, req)) {
1838 return tevent_req_post(req, ev);
1840 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1844 static void cli_ntcreate_done(struct tevent_req *subreq)
1846 struct tevent_req *req = tevent_req_callback_data(
1847 subreq, struct tevent_req);
1848 struct cli_ntcreate_state *state = tevent_req_data(
1849 req, struct cli_ntcreate_state);
1857 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1858 &num_bytes, &bytes);
1859 TALLOC_FREE(subreq);
1860 if (tevent_req_nterror(req, status)) {
1863 state->fnum = SVAL(vwv+2, 1);
1864 tevent_req_done(req);
1867 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1869 struct cli_ntcreate_state *state = tevent_req_data(
1870 req, struct cli_ntcreate_state);
1873 if (tevent_req_is_nterror(req, &status)) {
1876 *pfnum = state->fnum;
1877 return NT_STATUS_OK;
1880 NTSTATUS cli_ntcreate(struct cli_state *cli,
1882 uint32_t CreatFlags,
1883 uint32_t DesiredAccess,
1884 uint32_t FileAttributes,
1885 uint32_t ShareAccess,
1886 uint32_t CreateDisposition,
1887 uint32_t CreateOptions,
1888 uint8_t SecurityFlags,
1891 TALLOC_CTX *frame = talloc_stackframe();
1892 struct event_context *ev;
1893 struct tevent_req *req;
1894 NTSTATUS status = NT_STATUS_OK;
1896 if (cli_has_async_calls(cli)) {
1898 * Can't use sync call while an async call is in flight
1900 status = NT_STATUS_INVALID_PARAMETER;
1904 ev = event_context_init(frame);
1906 status = NT_STATUS_NO_MEMORY;
1910 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1911 DesiredAccess, FileAttributes, ShareAccess,
1912 CreateDisposition, CreateOptions,
1915 status = NT_STATUS_NO_MEMORY;
1919 if (!tevent_req_poll(req, ev)) {
1920 status = map_nt_error_from_unix(errno);
1924 status = cli_ntcreate_recv(req, pfid);
1930 struct cli_nttrans_create_state {
1934 static void cli_nttrans_create_done(struct tevent_req *subreq);
1936 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
1937 struct event_context *ev,
1938 struct cli_state *cli,
1940 uint32_t CreatFlags,
1941 uint32_t DesiredAccess,
1942 uint32_t FileAttributes,
1943 uint32_t ShareAccess,
1944 uint32_t CreateDisposition,
1945 uint32_t CreateOptions,
1946 uint8_t SecurityFlags,
1947 struct security_descriptor *secdesc,
1948 struct ea_struct *eas,
1951 struct tevent_req *req, *subreq;
1952 struct cli_nttrans_create_state *state;
1954 uint8_t *secdesc_buf;
1957 size_t converted_len;
1959 req = tevent_req_create(mem_ctx,
1960 &state, struct cli_nttrans_create_state);
1965 if (secdesc != NULL) {
1966 status = marshall_sec_desc(talloc_tos(), secdesc,
1967 &secdesc_buf, &secdesc_len);
1968 if (tevent_req_nterror(req, status)) {
1969 DEBUG(10, ("marshall_sec_desc failed: %s\n",
1970 nt_errstr(status)));
1971 return tevent_req_post(req, ev);
1982 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
1983 return tevent_req_post(req, ev);
1986 param = talloc_array(state, uint8_t, 53);
1987 if (tevent_req_nomem(param, req)) {
1988 return tevent_req_post(req, ev);
1991 param = trans2_bytes_push_str(param, cli_ucs2(cli),
1992 fname, strlen(fname),
1994 if (tevent_req_nomem(param, req)) {
1995 return tevent_req_post(req, ev);
1998 SIVAL(param, 0, CreatFlags);
1999 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
2000 SIVAL(param, 8, DesiredAccess);
2001 SIVAL(param, 12, 0x0); /* AllocationSize */
2002 SIVAL(param, 16, 0x0); /* AllocationSize */
2003 SIVAL(param, 20, FileAttributes);
2004 SIVAL(param, 24, ShareAccess);
2005 SIVAL(param, 28, CreateDisposition);
2006 SIVAL(param, 32, CreateOptions |
2007 (cli->backup_intent ? FILE_OPEN_FOR_BACKUP_INTENT : 0));
2008 SIVAL(param, 36, secdesc_len);
2009 SIVAL(param, 40, 0); /* EA length*/
2010 SIVAL(param, 44, converted_len);
2011 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
2012 SCVAL(param, 52, SecurityFlags);
2014 subreq = cli_trans_send(state, ev, cli, SMBnttrans,
2015 NULL, -1, /* name, fid */
2016 NT_TRANSACT_CREATE, 0,
2017 NULL, 0, 0, /* setup */
2018 param, talloc_get_size(param), 128, /* param */
2019 secdesc_buf, secdesc_len, 0); /* data */
2020 if (tevent_req_nomem(subreq, req)) {
2021 return tevent_req_post(req, ev);
2023 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2027 static void cli_nttrans_create_done(struct tevent_req *subreq)
2029 struct tevent_req *req = tevent_req_callback_data(
2030 subreq, struct tevent_req);
2031 struct cli_nttrans_create_state *state = tevent_req_data(
2032 req, struct cli_nttrans_create_state);
2037 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2038 NULL, 0, NULL, /* rsetup */
2039 ¶m, 69, &num_param,
2041 if (tevent_req_nterror(req, status)) {
2044 state->fnum = SVAL(param, 2);
2046 tevent_req_done(req);
2049 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req, uint16_t *fnum)
2051 struct cli_nttrans_create_state *state = tevent_req_data(
2052 req, struct cli_nttrans_create_state);
2055 if (tevent_req_is_nterror(req, &status)) {
2058 *fnum = state->fnum;
2059 return NT_STATUS_OK;
2062 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2064 uint32_t CreatFlags,
2065 uint32_t DesiredAccess,
2066 uint32_t FileAttributes,
2067 uint32_t ShareAccess,
2068 uint32_t CreateDisposition,
2069 uint32_t CreateOptions,
2070 uint8_t SecurityFlags,
2071 struct security_descriptor *secdesc,
2072 struct ea_struct *eas,
2076 TALLOC_CTX *frame = talloc_stackframe();
2077 struct event_context *ev;
2078 struct tevent_req *req;
2079 NTSTATUS status = NT_STATUS_NO_MEMORY;
2081 if (cli_has_async_calls(cli)) {
2083 * Can't use sync call while an async call is in flight
2085 status = NT_STATUS_INVALID_PARAMETER;
2088 ev = event_context_init(frame);
2092 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2093 DesiredAccess, FileAttributes,
2094 ShareAccess, CreateDisposition,
2095 CreateOptions, SecurityFlags,
2096 secdesc, eas, num_eas);
2100 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2103 status = cli_nttrans_create_recv(req, pfid);
2109 /****************************************************************************
2111 WARNING: if you open with O_WRONLY then getattrE won't work!
2112 ****************************************************************************/
2114 struct cli_open_state {
2115 struct tevent_context *ev;
2116 struct cli_state *cli;
2122 uint8_t additional_flags;
2126 static void cli_open_done(struct tevent_req *subreq);
2127 static void cli_open_ntcreate_done(struct tevent_req *subreq);
2129 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2130 struct event_context *ev,
2131 struct cli_state *cli, const char *fname,
2132 int flags, int share_mode,
2133 struct tevent_req **psmbreq)
2135 struct tevent_req *req, *subreq;
2136 struct cli_open_state *state;
2139 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2145 state->fname = fname;
2147 if (flags & O_CREAT) {
2148 state->openfn |= (1<<4);
2150 if (!(flags & O_EXCL)) {
2151 if (flags & O_TRUNC)
2152 state->openfn |= (1<<1);
2154 state->openfn |= (1<<0);
2157 state->dos_deny = (share_mode<<4);
2159 if ((flags & O_ACCMODE) == O_RDWR) {
2160 state->dos_deny |= 2;
2161 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2162 state->dos_deny |= 1;
2166 if ((flags & O_SYNC) == O_SYNC) {
2167 state->dos_deny |= (1<<14);
2171 if (share_mode == DENY_FCB) {
2172 state->dos_deny = 0xFF;
2175 SCVAL(state->vwv + 0, 0, 0xFF);
2176 SCVAL(state->vwv + 0, 1, 0);
2177 SSVAL(state->vwv + 1, 0, 0);
2178 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2179 SSVAL(state->vwv + 3, 0, state->dos_deny);
2180 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2181 SSVAL(state->vwv + 5, 0, 0);
2182 SIVAL(state->vwv + 6, 0, 0);
2183 SSVAL(state->vwv + 8, 0, state->openfn);
2184 SIVAL(state->vwv + 9, 0, 0);
2185 SIVAL(state->vwv + 11, 0, 0);
2186 SIVAL(state->vwv + 13, 0, 0);
2188 if (cli->use_oplocks) {
2189 /* if using oplocks then ask for a batch oplock via
2190 core and extended methods */
2191 state->additional_flags =
2192 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2193 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2196 bytes = talloc_array(state, uint8_t, 0);
2197 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2198 strlen(fname)+1, NULL);
2200 if (tevent_req_nomem(bytes, req)) {
2201 return tevent_req_post(req, ev);
2204 state->bytes.iov_base = (void *)bytes;
2205 state->bytes.iov_len = talloc_get_size(bytes);
2207 subreq = cli_smb_req_create(state, ev, cli, SMBopenX,
2208 state->additional_flags,
2209 15, state->vwv, 1, &state->bytes);
2210 if (subreq == NULL) {
2214 tevent_req_set_callback(subreq, cli_open_done, req);
2219 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2220 struct cli_state *cli, const char *fname,
2221 int flags, int share_mode)
2223 struct tevent_req *req, *subreq;
2226 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2232 status = cli_smb_req_send(subreq);
2233 if (tevent_req_nterror(req, status)) {
2234 return tevent_req_post(req, ev);
2239 static void cli_open_done(struct tevent_req *subreq)
2241 struct tevent_req *req = tevent_req_callback_data(
2242 subreq, struct tevent_req);
2243 struct cli_open_state *state = tevent_req_data(
2244 req, struct cli_open_state);
2249 uint32_t access_mask, share_mode, create_disposition, create_options;
2251 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2253 TALLOC_FREE(subreq);
2255 if (NT_STATUS_IS_OK(status)) {
2256 state->fnum = SVAL(vwv+2, 0);
2257 tevent_req_done(req);
2261 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
2262 tevent_req_nterror(req, status);
2267 * For the new shiny OS/X Lion SMB server, try a ntcreate
2271 if (!map_open_params_to_ntcreate(state->fname, state->dos_deny,
2272 state->openfn, &access_mask,
2273 &share_mode, &create_disposition,
2274 &create_options, NULL)) {
2275 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
2279 subreq = cli_ntcreate_send(state, state->ev, state->cli,
2280 state->fname, 0, access_mask,
2281 0, share_mode, create_disposition,
2283 if (tevent_req_nomem(subreq, req)) {
2286 tevent_req_set_callback(subreq, cli_open_ntcreate_done, req);
2289 static void cli_open_ntcreate_done(struct tevent_req *subreq)
2291 struct tevent_req *req = tevent_req_callback_data(
2292 subreq, struct tevent_req);
2293 struct cli_open_state *state = tevent_req_data(
2294 req, struct cli_open_state);
2297 status = cli_ntcreate_recv(subreq, &state->fnum);
2298 TALLOC_FREE(subreq);
2299 if (tevent_req_nterror(req, status)) {
2302 tevent_req_done(req);
2305 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2307 struct cli_open_state *state = tevent_req_data(
2308 req, struct cli_open_state);
2311 if (tevent_req_is_nterror(req, &status)) {
2314 *pfnum = state->fnum;
2315 return NT_STATUS_OK;
2318 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2319 int share_mode, uint16_t *pfnum)
2321 TALLOC_CTX *frame = talloc_stackframe();
2322 struct event_context *ev;
2323 struct tevent_req *req;
2324 NTSTATUS status = NT_STATUS_OK;
2326 if (cli_has_async_calls(cli)) {
2328 * Can't use sync call while an async call is in flight
2330 status = NT_STATUS_INVALID_PARAMETER;
2334 ev = event_context_init(frame);
2336 status = NT_STATUS_NO_MEMORY;
2340 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2342 status = NT_STATUS_NO_MEMORY;
2346 if (!tevent_req_poll(req, ev)) {
2347 status = map_nt_error_from_unix(errno);
2351 status = cli_open_recv(req, pfnum);
2357 /****************************************************************************
2359 ****************************************************************************/
2361 struct cli_close_state {
2365 static void cli_close_done(struct tevent_req *subreq);
2367 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2368 struct event_context *ev,
2369 struct cli_state *cli,
2371 struct tevent_req **psubreq)
2373 struct tevent_req *req, *subreq;
2374 struct cli_close_state *state;
2376 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2381 SSVAL(state->vwv+0, 0, fnum);
2382 SIVALS(state->vwv+1, 0, -1);
2384 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2386 if (subreq == NULL) {
2390 tevent_req_set_callback(subreq, cli_close_done, req);
2395 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2396 struct event_context *ev,
2397 struct cli_state *cli,
2400 struct tevent_req *req, *subreq;
2403 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2408 status = cli_smb_req_send(subreq);
2409 if (tevent_req_nterror(req, status)) {
2410 return tevent_req_post(req, ev);
2415 static void cli_close_done(struct tevent_req *subreq)
2417 struct tevent_req *req = tevent_req_callback_data(
2418 subreq, struct tevent_req);
2421 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2422 TALLOC_FREE(subreq);
2423 if (tevent_req_nterror(req, status)) {
2426 tevent_req_done(req);
2429 NTSTATUS cli_close_recv(struct tevent_req *req)
2431 return tevent_req_simple_recv_ntstatus(req);
2434 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2436 TALLOC_CTX *frame = talloc_stackframe();
2437 struct event_context *ev;
2438 struct tevent_req *req;
2439 NTSTATUS status = NT_STATUS_OK;
2441 if (cli_has_async_calls(cli)) {
2443 * Can't use sync call while an async call is in flight
2445 status = NT_STATUS_INVALID_PARAMETER;
2449 ev = event_context_init(frame);
2451 status = NT_STATUS_NO_MEMORY;
2455 req = cli_close_send(frame, ev, cli, fnum);
2457 status = NT_STATUS_NO_MEMORY;
2461 if (!tevent_req_poll(req, ev)) {
2462 status = map_nt_error_from_unix(errno);
2466 status = cli_close_recv(req);
2472 /****************************************************************************
2473 Truncate a file to a specified size
2474 ****************************************************************************/
2476 struct ftrunc_state {
2482 static void cli_ftruncate_done(struct tevent_req *subreq)
2484 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2485 NULL, 0, NULL, NULL, 0, NULL);
2486 tevent_req_simple_finish_ntstatus(subreq, status);
2489 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2490 struct event_context *ev,
2491 struct cli_state *cli,
2495 struct tevent_req *req = NULL, *subreq = NULL;
2496 struct ftrunc_state *state = NULL;
2498 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2503 /* Setup setup word. */
2504 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2506 /* Setup param array. */
2507 SSVAL(state->param,0,fnum);
2508 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2509 SSVAL(state->param,4,0);
2511 /* Setup data array. */
2512 SBVAL(state->data, 0, size);
2514 subreq = cli_trans_send(state, /* mem ctx. */
2515 ev, /* event ctx. */
2516 cli, /* cli_state. */
2517 SMBtrans2, /* cmd. */
2518 NULL, /* pipe name. */
2522 &state->setup, /* setup. */
2523 1, /* num setup uint16_t words. */
2524 0, /* max returned setup. */
2525 state->param, /* param. */
2527 2, /* max returned param. */
2528 state->data, /* data. */
2530 0); /* max returned data. */
2532 if (tevent_req_nomem(subreq, req)) {
2533 return tevent_req_post(req, ev);
2535 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2539 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2541 return tevent_req_simple_recv_ntstatus(req);
2544 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2546 TALLOC_CTX *frame = talloc_stackframe();
2547 struct event_context *ev = NULL;
2548 struct tevent_req *req = NULL;
2549 NTSTATUS status = NT_STATUS_OK;
2551 if (cli_has_async_calls(cli)) {
2553 * Can't use sync call while an async call is in flight
2555 status = NT_STATUS_INVALID_PARAMETER;
2559 ev = event_context_init(frame);
2561 status = NT_STATUS_NO_MEMORY;
2565 req = cli_ftruncate_send(frame,
2571 status = NT_STATUS_NO_MEMORY;
2575 if (!tevent_req_poll(req, ev)) {
2576 status = map_nt_error_from_unix(errno);
2580 status = cli_ftruncate_recv(req);
2587 /****************************************************************************
2588 send a lock with a specified locktype
2589 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2590 ****************************************************************************/
2592 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2593 uint32_t offset, uint32_t len,
2594 int timeout, unsigned char locktype)
2599 unsigned int set_timeout = 0;
2600 unsigned int saved_timeout = 0;
2602 SCVAL(vwv + 0, 0, 0xff);
2603 SCVAL(vwv + 0, 1, 0);
2604 SSVAL(vwv + 1, 0, 0);
2605 SSVAL(vwv + 2, 0, fnum);
2606 SCVAL(vwv + 3, 0, locktype);
2607 SCVAL(vwv + 3, 1, 0);
2608 SIVALS(vwv + 4, 0, timeout);
2609 SSVAL(vwv + 6, 0, 0);
2610 SSVAL(vwv + 7, 0, 1);
2612 SSVAL(bytes, 0, cli_getpid(cli));
2613 SIVAL(bytes, 2, offset);
2614 SIVAL(bytes, 6, len);
2617 if (timeout == -1) {
2618 set_timeout = 0x7FFFFFFF;
2620 set_timeout = timeout + 2*1000;
2622 saved_timeout = cli_set_timeout(cli, set_timeout);
2625 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2626 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2628 if (saved_timeout != 0) {
2629 cli_set_timeout(cli, saved_timeout);
2635 /****************************************************************************
2637 note that timeout is in units of 2 milliseconds
2638 ****************************************************************************/
2640 NTSTATUS cli_lock32(struct cli_state *cli, uint16_t fnum,
2641 uint32_t offset, uint32_t len, int timeout,
2642 enum brl_type lock_type)
2646 status = cli_locktype(cli, fnum, offset, len, timeout,
2647 (lock_type == READ_LOCK? 1 : 0));
2651 /****************************************************************************
2653 ****************************************************************************/
2655 struct cli_unlock_state {
2660 static void cli_unlock_done(struct tevent_req *subreq);
2662 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2663 struct event_context *ev,
2664 struct cli_state *cli,
2670 struct tevent_req *req = NULL, *subreq = NULL;
2671 struct cli_unlock_state *state = NULL;
2672 uint8_t additional_flags = 0;
2674 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2679 SCVAL(state->vwv+0, 0, 0xFF);
2680 SSVAL(state->vwv+2, 0, fnum);
2681 SCVAL(state->vwv+3, 0, 0);
2682 SIVALS(state->vwv+4, 0, 0);
2683 SSVAL(state->vwv+6, 0, 1);
2684 SSVAL(state->vwv+7, 0, 0);
2686 SSVAL(state->data, 0, cli_getpid(cli));
2687 SIVAL(state->data, 2, offset);
2688 SIVAL(state->data, 6, len);
2690 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2691 8, state->vwv, 10, state->data);
2692 if (tevent_req_nomem(subreq, req)) {
2693 return tevent_req_post(req, ev);
2695 tevent_req_set_callback(subreq, cli_unlock_done, req);
2699 static void cli_unlock_done(struct tevent_req *subreq)
2701 struct tevent_req *req = tevent_req_callback_data(
2702 subreq, struct tevent_req);
2705 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2706 TALLOC_FREE(subreq);
2707 if (tevent_req_nterror(req, status)) {
2710 tevent_req_done(req);
2713 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2715 return tevent_req_simple_recv_ntstatus(req);
2718 NTSTATUS cli_unlock(struct cli_state *cli,
2723 TALLOC_CTX *frame = talloc_stackframe();
2724 struct event_context *ev;
2725 struct tevent_req *req;
2726 NTSTATUS status = NT_STATUS_OK;
2728 if (cli_has_async_calls(cli)) {
2730 * Can't use sync call while an async call is in flight
2732 status = NT_STATUS_INVALID_PARAMETER;
2736 ev = event_context_init(frame);
2738 status = NT_STATUS_NO_MEMORY;
2742 req = cli_unlock_send(frame, ev, cli,
2745 status = NT_STATUS_NO_MEMORY;
2749 if (!tevent_req_poll(req, ev)) {
2750 status = map_nt_error_from_unix(errno);
2754 status = cli_unlock_recv(req);
2761 /****************************************************************************
2762 Lock a file with 64 bit offsets.
2763 ****************************************************************************/
2765 NTSTATUS cli_lock64(struct cli_state *cli, uint16_t fnum,
2766 uint64_t offset, uint64_t len, int timeout,
2767 enum brl_type lock_type)
2771 unsigned int set_timeout = 0;
2772 unsigned int saved_timeout = 0;
2776 if (! (cli_state_capabilities(cli) & CAP_LARGE_FILES)) {
2777 return cli_lock32(cli, fnum, offset, len, timeout, lock_type);
2780 ltype = (lock_type == READ_LOCK? 1 : 0);
2781 ltype |= LOCKING_ANDX_LARGE_FILES;
2783 SCVAL(vwv + 0, 0, 0xff);
2784 SCVAL(vwv + 0, 1, 0);
2785 SSVAL(vwv + 1, 0, 0);
2786 SSVAL(vwv + 2, 0, fnum);
2787 SCVAL(vwv + 3, 0, ltype);
2788 SCVAL(vwv + 3, 1, 0);
2789 SIVALS(vwv + 4, 0, timeout);
2790 SSVAL(vwv + 6, 0, 0);
2791 SSVAL(vwv + 7, 0, 1);
2793 SIVAL(bytes, 0, cli_getpid(cli));
2794 SOFF_T_R(bytes, 4, offset);
2795 SOFF_T_R(bytes, 12, len);
2798 if (timeout == -1) {
2799 set_timeout = 0x7FFFFFFF;
2801 set_timeout = timeout + 2*1000;
2803 saved_timeout = cli_set_timeout(cli, set_timeout);
2806 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2807 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2809 if (saved_timeout != 0) {
2810 cli_set_timeout(cli, saved_timeout);
2816 /****************************************************************************
2817 Unlock a file with 64 bit offsets.
2818 ****************************************************************************/
2820 struct cli_unlock64_state {
2825 static void cli_unlock64_done(struct tevent_req *subreq);
2827 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2828 struct event_context *ev,
2829 struct cli_state *cli,
2835 struct tevent_req *req = NULL, *subreq = NULL;
2836 struct cli_unlock64_state *state = NULL;
2837 uint8_t additional_flags = 0;
2839 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2844 SCVAL(state->vwv+0, 0, 0xff);
2845 SSVAL(state->vwv+2, 0, fnum);
2846 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2847 SIVALS(state->vwv+4, 0, 0);
2848 SSVAL(state->vwv+6, 0, 1);
2849 SSVAL(state->vwv+7, 0, 0);
2851 SIVAL(state->data, 0, cli_getpid(cli));
2852 SOFF_T_R(state->data, 4, offset);
2853 SOFF_T_R(state->data, 12, len);
2855 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2856 8, state->vwv, 20, state->data);
2857 if (tevent_req_nomem(subreq, req)) {
2858 return tevent_req_post(req, ev);
2860 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2864 static void cli_unlock64_done(struct tevent_req *subreq)
2866 struct tevent_req *req = tevent_req_callback_data(
2867 subreq, struct tevent_req);
2870 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2871 TALLOC_FREE(subreq);
2872 if (tevent_req_nterror(req, status)) {
2875 tevent_req_done(req);
2878 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2880 return tevent_req_simple_recv_ntstatus(req);
2883 NTSTATUS cli_unlock64(struct cli_state *cli,
2888 TALLOC_CTX *frame = talloc_stackframe();
2889 struct event_context *ev;
2890 struct tevent_req *req;
2891 NTSTATUS status = NT_STATUS_OK;
2893 if (! (cli_state_capabilities(cli) & CAP_LARGE_FILES)) {
2894 return cli_unlock(cli, fnum, offset, len);
2897 if (cli_has_async_calls(cli)) {
2899 * Can't use sync call while an async call is in flight
2901 status = NT_STATUS_INVALID_PARAMETER;
2905 ev = event_context_init(frame);
2907 status = NT_STATUS_NO_MEMORY;
2911 req = cli_unlock64_send(frame, ev, cli,
2914 status = NT_STATUS_NO_MEMORY;
2918 if (!tevent_req_poll(req, ev)) {
2919 status = map_nt_error_from_unix(errno);
2923 status = cli_unlock64_recv(req);
2930 /****************************************************************************
2931 Get/unlock a POSIX lock on a file - internal function.
2932 ****************************************************************************/
2934 struct posix_lock_state {
2937 uint8_t data[POSIX_LOCK_DATA_SIZE];
2940 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2942 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2943 NULL, 0, NULL, NULL, 0, NULL);
2944 tevent_req_simple_finish_ntstatus(subreq, status);
2947 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2948 struct event_context *ev,
2949 struct cli_state *cli,
2954 enum brl_type lock_type)
2956 struct tevent_req *req = NULL, *subreq = NULL;
2957 struct posix_lock_state *state = NULL;
2959 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2964 /* Setup setup word. */
2965 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2967 /* Setup param array. */
2968 SSVAL(&state->param, 0, fnum);
2969 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2971 /* Setup data array. */
2972 switch (lock_type) {
2974 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2975 POSIX_LOCK_TYPE_READ);
2978 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2979 POSIX_LOCK_TYPE_WRITE);
2982 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2983 POSIX_LOCK_TYPE_UNLOCK);
2990 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2991 POSIX_LOCK_FLAG_WAIT);
2993 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2994 POSIX_LOCK_FLAG_NOWAIT);
2997 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli_getpid(cli));
2998 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2999 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3001 subreq = cli_trans_send(state, /* mem ctx. */
3002 ev, /* event ctx. */
3003 cli, /* cli_state. */
3004 SMBtrans2, /* cmd. */
3005 NULL, /* pipe name. */
3009 &state->setup, /* setup. */
3010 1, /* num setup uint16_t words. */
3011 0, /* max returned setup. */
3012 state->param, /* param. */
3014 2, /* max returned param. */
3015 state->data, /* data. */
3016 POSIX_LOCK_DATA_SIZE, /* num data. */
3017 0); /* max returned data. */
3019 if (tevent_req_nomem(subreq, req)) {
3020 return tevent_req_post(req, ev);
3022 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3026 /****************************************************************************
3028 ****************************************************************************/
3030 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3031 struct event_context *ev,
3032 struct cli_state *cli,
3037 enum brl_type lock_type)
3039 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3040 wait_lock, lock_type);
3043 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3045 return tevent_req_simple_recv_ntstatus(req);
3048 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3049 uint64_t offset, uint64_t len,
3050 bool wait_lock, enum brl_type lock_type)
3052 TALLOC_CTX *frame = talloc_stackframe();
3053 struct event_context *ev = NULL;
3054 struct tevent_req *req = NULL;
3055 NTSTATUS status = NT_STATUS_OK;
3057 if (cli_has_async_calls(cli)) {
3059 * Can't use sync call while an async call is in flight
3061 status = NT_STATUS_INVALID_PARAMETER;
3065 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3066 status = NT_STATUS_INVALID_PARAMETER;
3070 ev = event_context_init(frame);
3072 status = NT_STATUS_NO_MEMORY;
3076 req = cli_posix_lock_send(frame,
3085 status = NT_STATUS_NO_MEMORY;
3089 if (!tevent_req_poll(req, ev)) {
3090 status = map_nt_error_from_unix(errno);
3094 status = cli_posix_lock_recv(req);
3101 /****************************************************************************
3102 POSIX Unlock a file.
3103 ****************************************************************************/
3105 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3106 struct event_context *ev,
3107 struct cli_state *cli,
3112 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3113 false, UNLOCK_LOCK);
3116 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3118 return tevent_req_simple_recv_ntstatus(req);
3121 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3123 TALLOC_CTX *frame = talloc_stackframe();
3124 struct event_context *ev = NULL;
3125 struct tevent_req *req = NULL;
3126 NTSTATUS status = NT_STATUS_OK;
3128 if (cli_has_async_calls(cli)) {
3130 * Can't use sync call while an async call is in flight
3132 status = NT_STATUS_INVALID_PARAMETER;
3136 ev = event_context_init(frame);
3138 status = NT_STATUS_NO_MEMORY;
3142 req = cli_posix_unlock_send(frame,
3149 status = NT_STATUS_NO_MEMORY;
3153 if (!tevent_req_poll(req, ev)) {
3154 status = map_nt_error_from_unix(errno);
3158 status = cli_posix_unlock_recv(req);
3165 /****************************************************************************
3166 Do a SMBgetattrE call.
3167 ****************************************************************************/
3169 static void cli_getattrE_done(struct tevent_req *subreq);
3171 struct cli_getattrE_state {
3181 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3182 struct event_context *ev,
3183 struct cli_state *cli,
3186 struct tevent_req *req = NULL, *subreq = NULL;
3187 struct cli_getattrE_state *state = NULL;
3188 uint8_t additional_flags = 0;
3190 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3195 state->zone_offset = cli_state_server_time_zone(cli);
3196 SSVAL(state->vwv+0,0,fnum);
3198 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3199 1, state->vwv, 0, NULL);
3200 if (tevent_req_nomem(subreq, req)) {
3201 return tevent_req_post(req, ev);
3203 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3207 static void cli_getattrE_done(struct tevent_req *subreq)
3209 struct tevent_req *req = tevent_req_callback_data(
3210 subreq, struct tevent_req);
3211 struct cli_getattrE_state *state = tevent_req_data(
3212 req, struct cli_getattrE_state);
3214 uint16_t *vwv = NULL;
3218 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3220 TALLOC_FREE(subreq);
3221 if (tevent_req_nterror(req, status)) {
3225 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3226 state->attr = SVAL(vwv+10,0);
3227 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3228 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3229 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3231 tevent_req_done(req);
3234 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3237 time_t *change_time,
3238 time_t *access_time,
3241 struct cli_getattrE_state *state = tevent_req_data(
3242 req, struct cli_getattrE_state);
3245 if (tevent_req_is_nterror(req, &status)) {
3249 *attr = state->attr;
3252 *size = state->size;
3255 *change_time = state->change_time;
3258 *access_time = state->access_time;
3261 *write_time = state->write_time;
3263 return NT_STATUS_OK;
3266 NTSTATUS cli_getattrE(struct cli_state *cli,
3270 time_t *change_time,
3271 time_t *access_time,
3274 TALLOC_CTX *frame = talloc_stackframe();
3275 struct event_context *ev = NULL;
3276 struct tevent_req *req = NULL;
3277 NTSTATUS status = NT_STATUS_OK;
3279 if (cli_has_async_calls(cli)) {
3281 * Can't use sync call while an async call is in flight
3283 status = NT_STATUS_INVALID_PARAMETER;
3287 ev = event_context_init(frame);
3289 status = NT_STATUS_NO_MEMORY;
3293 req = cli_getattrE_send(frame, ev, cli, fnum);
3295 status = NT_STATUS_NO_MEMORY;
3299 if (!tevent_req_poll(req, ev)) {
3300 status = map_nt_error_from_unix(errno);
3304 status = cli_getattrE_recv(req,
3316 /****************************************************************************
3318 ****************************************************************************/
3320 static void cli_getatr_done(struct tevent_req *subreq);
3322 struct cli_getatr_state {
3329 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3330 struct event_context *ev,
3331 struct cli_state *cli,
3334 struct tevent_req *req = NULL, *subreq = NULL;
3335 struct cli_getatr_state *state = NULL;
3336 uint8_t additional_flags = 0;
3337 uint8_t *bytes = NULL;
3339 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3344 state->zone_offset = cli_state_server_time_zone(cli);
3346 bytes = talloc_array(state, uint8_t, 1);
3347 if (tevent_req_nomem(bytes, req)) {
3348 return tevent_req_post(req, ev);
3351 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3352 strlen(fname)+1, NULL);
3354 if (tevent_req_nomem(bytes, req)) {
3355 return tevent_req_post(req, ev);
3358 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3359 0, NULL, talloc_get_size(bytes), bytes);
3360 if (tevent_req_nomem(subreq, req)) {
3361 return tevent_req_post(req, ev);
3363 tevent_req_set_callback(subreq, cli_getatr_done, req);
3367 static void cli_getatr_done(struct tevent_req *subreq)
3369 struct tevent_req *req = tevent_req_callback_data(
3370 subreq, struct tevent_req);
3371 struct cli_getatr_state *state = tevent_req_data(
3372 req, struct cli_getatr_state);
3374 uint16_t *vwv = NULL;
3378 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3380 TALLOC_FREE(subreq);
3381 if (tevent_req_nterror(req, status)) {
3385 state->attr = SVAL(vwv+0,0);
3386 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3387 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3389 tevent_req_done(req);
3392 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3397 struct cli_getatr_state *state = tevent_req_data(
3398 req, struct cli_getatr_state);
3401 if (tevent_req_is_nterror(req, &status)) {
3405 *attr = state->attr;
3408 *size = state->size;
3411 *write_time = state->write_time;
3413 return NT_STATUS_OK;
3416 NTSTATUS cli_getatr(struct cli_state *cli,
3422 TALLOC_CTX *frame = talloc_stackframe();
3423 struct event_context *ev = NULL;
3424 struct tevent_req *req = NULL;
3425 NTSTATUS status = NT_STATUS_OK;
3427 if (cli_has_async_calls(cli)) {
3429 * Can't use sync call while an async call is in flight
3431 status = NT_STATUS_INVALID_PARAMETER;
3435 ev = event_context_init(frame);
3437 status = NT_STATUS_NO_MEMORY;
3441 req = cli_getatr_send(frame, ev, cli, fname);
3443 status = NT_STATUS_NO_MEMORY;
3447 if (!tevent_req_poll(req, ev)) {
3448 status = map_nt_error_from_unix(errno);
3452 status = cli_getatr_recv(req,
3462 /****************************************************************************
3463 Do a SMBsetattrE call.
3464 ****************************************************************************/
3466 static void cli_setattrE_done(struct tevent_req *subreq);
3468 struct cli_setattrE_state {
3472 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3473 struct event_context *ev,
3474 struct cli_state *cli,
3480 struct tevent_req *req = NULL, *subreq = NULL;
3481 struct cli_setattrE_state *state = NULL;
3482 uint8_t additional_flags = 0;
3484 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3489 SSVAL(state->vwv+0, 0, fnum);
3490 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3491 cli_state_server_time_zone(cli));
3492 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3493 cli_state_server_time_zone(cli));
3494 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3495 cli_state_server_time_zone(cli));
3497 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3498 7, state->vwv, 0, NULL);
3499 if (tevent_req_nomem(subreq, req)) {
3500 return tevent_req_post(req, ev);
3502 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3506 static void cli_setattrE_done(struct tevent_req *subreq)
3508 struct tevent_req *req = tevent_req_callback_data(
3509 subreq, struct tevent_req);
3512 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3513 TALLOC_FREE(subreq);
3514 if (tevent_req_nterror(req, status)) {
3517 tevent_req_done(req);
3520 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3522 return tevent_req_simple_recv_ntstatus(req);
3525 NTSTATUS cli_setattrE(struct cli_state *cli,
3531 TALLOC_CTX *frame = talloc_stackframe();
3532 struct event_context *ev = NULL;
3533 struct tevent_req *req = NULL;
3534 NTSTATUS status = NT_STATUS_OK;
3536 if (cli_has_async_calls(cli)) {
3538 * Can't use sync call while an async call is in flight
3540 status = NT_STATUS_INVALID_PARAMETER;
3544 ev = event_context_init(frame);
3546 status = NT_STATUS_NO_MEMORY;
3550 req = cli_setattrE_send(frame, ev,
3558 status = NT_STATUS_NO_MEMORY;
3562 if (!tevent_req_poll(req, ev)) {
3563 status = map_nt_error_from_unix(errno);
3567 status = cli_setattrE_recv(req);
3574 /****************************************************************************
3575 Do a SMBsetatr call.
3576 ****************************************************************************/
3578 static void cli_setatr_done(struct tevent_req *subreq);
3580 struct cli_setatr_state {
3584 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3585 struct event_context *ev,
3586 struct cli_state *cli,
3591 struct tevent_req *req = NULL, *subreq = NULL;
3592 struct cli_setatr_state *state = NULL;
3593 uint8_t additional_flags = 0;
3594 uint8_t *bytes = NULL;
3596 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3601 SSVAL(state->vwv+0, 0, attr);
3602 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli_state_server_time_zone(cli));
3604 bytes = talloc_array(state, uint8_t, 1);
3605 if (tevent_req_nomem(bytes, req)) {
3606 return tevent_req_post(req, ev);
3609 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3610 strlen(fname)+1, NULL);
3611 if (tevent_req_nomem(bytes, req)) {
3612 return tevent_req_post(req, ev);
3614 bytes = talloc_realloc(state, bytes, uint8_t,
3615 talloc_get_size(bytes)+1);
3616 if (tevent_req_nomem(bytes, req)) {
3617 return tevent_req_post(req, ev);
3620 bytes[talloc_get_size(bytes)-1] = 4;
3621 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3623 if (tevent_req_nomem(bytes, req)) {
3624 return tevent_req_post(req, ev);
3627 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3628 8, state->vwv, talloc_get_size(bytes), bytes);
3629 if (tevent_req_nomem(subreq, req)) {
3630 return tevent_req_post(req, ev);
3632 tevent_req_set_callback(subreq, cli_setatr_done, req);
3636 static void cli_setatr_done(struct tevent_req *subreq)
3638 struct tevent_req *req = tevent_req_callback_data(
3639 subreq, struct tevent_req);
3642 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3643 TALLOC_FREE(subreq);
3644 if (tevent_req_nterror(req, status)) {
3647 tevent_req_done(req);
3650 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3652 return tevent_req_simple_recv_ntstatus(req);
3655 NTSTATUS cli_setatr(struct cli_state *cli,
3660 TALLOC_CTX *frame = talloc_stackframe();
3661 struct event_context *ev = NULL;
3662 struct tevent_req *req = NULL;
3663 NTSTATUS status = NT_STATUS_OK;
3665 if (cli_has_async_calls(cli)) {
3667 * Can't use sync call while an async call is in flight
3669 status = NT_STATUS_INVALID_PARAMETER;
3673 ev = event_context_init(frame);
3675 status = NT_STATUS_NO_MEMORY;
3679 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3681 status = NT_STATUS_NO_MEMORY;
3685 if (!tevent_req_poll(req, ev)) {
3686 status = map_nt_error_from_unix(errno);
3690 status = cli_setatr_recv(req);
3697 /****************************************************************************
3698 Check for existance of a dir.
3699 ****************************************************************************/
3701 static void cli_chkpath_done(struct tevent_req *subreq);
3703 struct cli_chkpath_state {
3707 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3708 struct event_context *ev,
3709 struct cli_state *cli,
3712 struct tevent_req *req = NULL, *subreq = NULL;
3713 struct cli_chkpath_state *state = NULL;
3714 uint8_t additional_flags = 0;
3715 uint8_t *bytes = NULL;
3717 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3722 bytes = talloc_array(state, uint8_t, 1);
3723 if (tevent_req_nomem(bytes, req)) {
3724 return tevent_req_post(req, ev);
3727 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3728 strlen(fname)+1, NULL);
3730 if (tevent_req_nomem(bytes, req)) {
3731 return tevent_req_post(req, ev);
3734 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3735 0, NULL, talloc_get_size(bytes), bytes);
3736 if (tevent_req_nomem(subreq, req)) {
3737 return tevent_req_post(req, ev);
3739 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3743 static void cli_chkpath_done(struct tevent_req *subreq)
3745 struct tevent_req *req = tevent_req_callback_data(
3746 subreq, struct tevent_req);
3749 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3750 TALLOC_FREE(subreq);
3751 if (tevent_req_nterror(req, status)) {
3754 tevent_req_done(req);
3757 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3759 return tevent_req_simple_recv_ntstatus(req);
3762 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3764 TALLOC_CTX *frame = talloc_stackframe();
3765 struct event_context *ev = NULL;
3766 struct tevent_req *req = NULL;
3768 NTSTATUS status = NT_STATUS_OK;
3770 if (cli_has_async_calls(cli)) {
3772 * Can't use sync call while an async call is in flight
3774 status = NT_STATUS_INVALID_PARAMETER;
3778 path2 = talloc_strdup(frame, path);
3780 status = NT_STATUS_NO_MEMORY;
3783 trim_char(path2,'\0','\\');
3785 path2 = talloc_strdup(frame, "\\");
3787 status = NT_STATUS_NO_MEMORY;
3792 ev = event_context_init(frame);
3794 status = NT_STATUS_NO_MEMORY;
3798 req = cli_chkpath_send(frame, ev, cli, path2);
3800 status = NT_STATUS_NO_MEMORY;
3804 if (!tevent_req_poll(req, ev)) {
3805 status = map_nt_error_from_unix(errno);
3809 status = cli_chkpath_recv(req);
3816 /****************************************************************************
3818 ****************************************************************************/
3820 static void cli_dskattr_done(struct tevent_req *subreq);
3822 struct cli_dskattr_state {
3828 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3829 struct event_context *ev,
3830 struct cli_state *cli)
3832 struct tevent_req *req = NULL, *subreq = NULL;
3833 struct cli_dskattr_state *state = NULL;
3834 uint8_t additional_flags = 0;
3836 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3841 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3843 if (tevent_req_nomem(subreq, req)) {
3844 return tevent_req_post(req, ev);
3846 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3850 static void cli_dskattr_done(struct tevent_req *subreq)
3852 struct tevent_req *req = tevent_req_callback_data(
3853 subreq, struct tevent_req);
3854 struct cli_dskattr_state *state = tevent_req_data(
3855 req, struct cli_dskattr_state);
3857 uint16_t *vwv = NULL;
3861 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3863 TALLOC_FREE(subreq);
3864 if (tevent_req_nterror(req, status)) {
3867 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3868 state->total = SVAL(vwv+0, 0);
3869 state->avail = SVAL(vwv+3, 0);
3870 tevent_req_done(req);
3873 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3875 struct cli_dskattr_state *state = tevent_req_data(
3876 req, struct cli_dskattr_state);
3879 if (tevent_req_is_nterror(req, &status)) {
3882 *bsize = state->bsize;
3883 *total = state->total;
3884 *avail = state->avail;
3885 return NT_STATUS_OK;
3888 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3890 TALLOC_CTX *frame = talloc_stackframe();
3891 struct event_context *ev = NULL;
3892 struct tevent_req *req = NULL;
3893 NTSTATUS status = NT_STATUS_OK;
3895 if (cli_has_async_calls(cli)) {
3897 * Can't use sync call while an async call is in flight
3899 status = NT_STATUS_INVALID_PARAMETER;
3903 ev = event_context_init(frame);
3905 status = NT_STATUS_NO_MEMORY;
3909 req = cli_dskattr_send(frame, ev, cli);
3911 status = NT_STATUS_NO_MEMORY;
3915 if (!tevent_req_poll(req, ev)) {
3916 status = map_nt_error_from_unix(errno);
3920 status = cli_dskattr_recv(req, bsize, total, avail);
3927 /****************************************************************************
3928 Create and open a temporary file.
3929 ****************************************************************************/
3931 static void cli_ctemp_done(struct tevent_req *subreq);
3933 struct ctemp_state {
3939 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3940 struct event_context *ev,
3941 struct cli_state *cli,
3944 struct tevent_req *req = NULL, *subreq = NULL;
3945 struct ctemp_state *state = NULL;
3946 uint8_t additional_flags = 0;
3947 uint8_t *bytes = NULL;
3949 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3954 SSVAL(state->vwv,0,0);
3955 SIVALS(state->vwv+1,0,-1);
3957 bytes = talloc_array(state, uint8_t, 1);
3958 if (tevent_req_nomem(bytes, req)) {
3959 return tevent_req_post(req, ev);
3962 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3963 strlen(path)+1, NULL);
3964 if (tevent_req_nomem(bytes, req)) {
3965 return tevent_req_post(req, ev);
3968 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3969 3, state->vwv, talloc_get_size(bytes), bytes);
3970 if (tevent_req_nomem(subreq, req)) {
3971 return tevent_req_post(req, ev);
3973 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3977 static void cli_ctemp_done(struct tevent_req *subreq)
3979 struct tevent_req *req = tevent_req_callback_data(
3980 subreq, struct tevent_req);
3981 struct ctemp_state *state = tevent_req_data(
3982 req, struct ctemp_state);
3986 uint32_t num_bytes = 0;
3987 uint8_t *bytes = NULL;
3990 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3991 &num_bytes, &bytes);
3992 TALLOC_FREE(subreq);
3993 if (tevent_req_nterror(req, status)) {
3997 state->fnum = SVAL(vwv+0, 0);
3999 /* From W2K3, the result is just the ASCII name */
4000 if (num_bytes < 2) {
4001 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4005 if (pull_string_talloc(state,
4012 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4015 tevent_req_done(req);
4018 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4023 struct ctemp_state *state = tevent_req_data(req,
4024 struct ctemp_state);
4027 if (tevent_req_is_nterror(req, &status)) {
4030 *pfnum = state->fnum;
4031 *outfile = talloc_strdup(ctx, state->ret_path);
4033 return NT_STATUS_NO_MEMORY;
4035 return NT_STATUS_OK;
4038 NTSTATUS cli_ctemp(struct cli_state *cli,
4044 TALLOC_CTX *frame = talloc_stackframe();
4045 struct event_context *ev;
4046 struct tevent_req *req;
4047 NTSTATUS status = NT_STATUS_OK;
4049 if (cli_has_async_calls(cli)) {
4051 * Can't use sync call while an async call is in flight
4053 status = NT_STATUS_INVALID_PARAMETER;
4057 ev = event_context_init(frame);
4059 status = NT_STATUS_NO_MEMORY;
4063 req = cli_ctemp_send(frame, ev, cli, path);
4065 status = NT_STATUS_NO_MEMORY;
4069 if (!tevent_req_poll(req, ev)) {
4070 status = map_nt_error_from_unix(errno);
4074 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4082 send a raw ioctl - used by the torture code
4084 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4089 SSVAL(vwv+0, 0, fnum);
4090 SSVAL(vwv+1, 0, code>>16);
4091 SSVAL(vwv+2, 0, (code&0xFFFF));
4093 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4094 NULL, 0, NULL, NULL, NULL, NULL);
4095 if (!NT_STATUS_IS_OK(status)) {
4098 *blob = data_blob_null;
4099 return NT_STATUS_OK;
4102 /*********************************************************
4103 Set an extended attribute utility fn.
4104 *********************************************************/
4106 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4107 uint8_t *param, unsigned int param_len,
4108 const char *ea_name,
4109 const char *ea_val, size_t ea_len)
4112 unsigned int data_len = 0;
4113 uint8_t *data = NULL;
4115 size_t ea_namelen = strlen(ea_name);
4118 SSVAL(setup, 0, setup_val);
4120 if (ea_namelen == 0 && ea_len == 0) {
4122 data = (uint8_t *)SMB_MALLOC(data_len);
4124 return NT_STATUS_NO_MEMORY;
4127 SIVAL(p,0,data_len);
4129 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4130 data = (uint8_t *)SMB_MALLOC(data_len);
4132 return NT_STATUS_NO_MEMORY;
4135 SIVAL(p,0,data_len);
4137 SCVAL(p, 0, 0); /* EA flags. */
4138 SCVAL(p, 1, ea_namelen);
4139 SSVAL(p, 2, ea_len);
4140 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4141 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4144 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4146 param, param_len, 2,
4147 data, data_len, CLI_BUFFER_SIZE,
4149 NULL, 0, NULL, /* rsetup */
4150 NULL, 0, NULL, /* rparam */
4151 NULL, 0, NULL); /* rdata */
4156 /*********************************************************
4157 Set an extended attribute on a pathname.
4158 *********************************************************/
4160 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4161 const char *ea_name, const char *ea_val,
4164 unsigned int param_len = 0;
4167 TALLOC_CTX *frame = talloc_stackframe();
4169 param = talloc_array(talloc_tos(), uint8_t, 6);
4171 return NT_STATUS_NO_MEMORY;
4173 SSVAL(param,0,SMB_INFO_SET_EA);
4177 param = trans2_bytes_push_str(param, cli_ucs2(cli),
4178 path, strlen(path)+1,
4180 param_len = talloc_get_size(param);
4182 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4183 ea_name, ea_val, ea_len);
4188 /*********************************************************
4189 Set an extended attribute on an fnum.
4190 *********************************************************/
4192 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4193 const char *ea_name, const char *ea_val,
4198 memset(param, 0, 6);
4199 SSVAL(param,0,fnum);
4200 SSVAL(param,2,SMB_INFO_SET_EA);
4202 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4203 ea_name, ea_val, ea_len);
4206 /*********************************************************
4207 Get an extended attribute list utility fn.
4208 *********************************************************/
4210 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4212 size_t *pnum_eas, struct ea_struct **pea_list)
4214 struct ea_struct *ea_list = NULL;
4219 if (rdata_len < 4) {
4223 ea_size = (size_t)IVAL(rdata,0);
4224 if (ea_size > rdata_len) {
4229 /* No EA's present. */
4238 /* Validate the EA list and count it. */
4239 for (num_eas = 0; ea_size >= 4; num_eas++) {
4240 unsigned int ea_namelen = CVAL(p,1);
4241 unsigned int ea_valuelen = SVAL(p,2);
4242 if (ea_namelen == 0) {
4245 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4248 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4249 p += 4 + ea_namelen + 1 + ea_valuelen;
4258 *pnum_eas = num_eas;
4260 /* Caller only wants number of EA's. */
4264 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4269 ea_size = (size_t)IVAL(rdata,0);
4272 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4273 struct ea_struct *ea = &ea_list[num_eas];
4274 fstring unix_ea_name;
4275 unsigned int ea_namelen = CVAL(p,1);
4276 unsigned int ea_valuelen = SVAL(p,2);
4278 ea->flags = CVAL(p,0);
4279 unix_ea_name[0] = '\0';
4280 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4281 ea->name = talloc_strdup(ea_list, unix_ea_name);
4285 /* Ensure the value is null terminated (in case it's a string). */
4286 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4287 if (!ea->value.data) {
4291 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4293 ea->value.data[ea_valuelen] = 0;
4295 p += 4 + ea_namelen + 1 + ea_valuelen;
4298 *pea_list = ea_list;
4302 TALLOC_FREE(ea_list);
4306 /*********************************************************
4307 Get an extended attribute list from a pathname.
4308 *********************************************************/
4310 struct cli_get_ea_list_path_state {
4315 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4317 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4318 struct tevent_context *ev,
4319 struct cli_state *cli,
4322 struct tevent_req *req, *subreq;
4323 struct cli_get_ea_list_path_state *state;
4325 req = tevent_req_create(mem_ctx, &state,
4326 struct cli_get_ea_list_path_state);
4330 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4331 SMB_INFO_QUERY_ALL_EAS, 4,
4333 if (tevent_req_nomem(subreq, req)) {
4334 return tevent_req_post(req, ev);
4336 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4340 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4342 struct tevent_req *req = tevent_req_callback_data(
4343 subreq, struct tevent_req);
4344 struct cli_get_ea_list_path_state *state = tevent_req_data(
4345 req, struct cli_get_ea_list_path_state);
4348 status = cli_qpathinfo_recv(subreq, state, &state->data,
4350 TALLOC_FREE(subreq);
4351 if (tevent_req_nterror(req, status)) {
4354 tevent_req_done(req);
4357 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4358 size_t *pnum_eas, struct ea_struct **peas)
4360 struct cli_get_ea_list_path_state *state = tevent_req_data(
4361 req, struct cli_get_ea_list_path_state);
4364 if (tevent_req_is_nterror(req, &status)) {
4367 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4369 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4371 return NT_STATUS_OK;
4374 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4377 struct ea_struct **pea_list)
4379 TALLOC_CTX *frame = talloc_stackframe();
4380 struct event_context *ev = NULL;
4381 struct tevent_req *req = NULL;
4382 NTSTATUS status = NT_STATUS_NO_MEMORY;
4384 if (cli_has_async_calls(cli)) {
4386 * Can't use sync call while an async call is in flight
4388 status = NT_STATUS_INVALID_PARAMETER;
4391 ev = event_context_init(frame);
4395 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4399 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4402 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4408 /****************************************************************************
4409 Convert open "flags" arg to uint32_t on wire.
4410 ****************************************************************************/
4412 static uint32_t open_flags_to_wire(int flags)
4414 int open_mode = flags & O_ACCMODE;
4417 switch (open_mode) {
4419 ret |= SMB_O_WRONLY;
4426 ret |= SMB_O_RDONLY;
4430 if (flags & O_CREAT) {
4433 if (flags & O_EXCL) {
4436 if (flags & O_TRUNC) {
4440 if (flags & O_SYNC) {
4444 if (flags & O_APPEND) {
4445 ret |= SMB_O_APPEND;
4447 #if defined(O_DIRECT)
4448 if (flags & O_DIRECT) {
4449 ret |= SMB_O_DIRECT;
4452 #if defined(O_DIRECTORY)
4453 if (flags & O_DIRECTORY) {
4454 ret |= SMB_O_DIRECTORY;
4460 /****************************************************************************
4461 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4462 ****************************************************************************/
4464 struct posix_open_state {
4468 uint16_t fnum; /* Out */
4471 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4473 struct tevent_req *req = tevent_req_callback_data(
4474 subreq, struct tevent_req);
4475 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4480 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4481 NULL, 0, NULL, &data, 12, &num_data);
4482 TALLOC_FREE(subreq);
4483 if (tevent_req_nterror(req, status)) {
4486 state->fnum = SVAL(data,2);
4487 tevent_req_done(req);
4490 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4491 struct event_context *ev,
4492 struct cli_state *cli,
4498 struct tevent_req *req = NULL, *subreq = NULL;
4499 struct posix_open_state *state = NULL;
4500 uint32_t wire_flags = open_flags_to_wire(flags);
4502 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4507 /* Setup setup word. */
4508 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4510 /* Setup param array. */
4511 state->param = talloc_array(state, uint8_t, 6);
4512 if (tevent_req_nomem(state->param, req)) {
4513 return tevent_req_post(req, ev);
4515 memset(state->param, '\0', 6);
4516 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4518 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4519 strlen(fname)+1, NULL);
4521 if (tevent_req_nomem(state->param, req)) {
4522 return tevent_req_post(req, ev);
4525 /* Setup data words. */
4527 wire_flags |= SMB_O_DIRECTORY;
4530 SIVAL(state->data,0,0); /* No oplock. */
4531 SIVAL(state->data,4,wire_flags);
4532 SIVAL(state->data,8,unix_perms_to_wire(mode));
4533 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4534 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4536 subreq = cli_trans_send(state, /* mem ctx. */
4537 ev, /* event ctx. */
4538 cli, /* cli_state. */
4539 SMBtrans2, /* cmd. */
4540 NULL, /* pipe name. */
4544 &state->setup, /* setup. */
4545 1, /* num setup uint16_t words. */
4546 0, /* max returned setup. */
4547 state->param, /* param. */
4548 talloc_get_size(state->param),/* num param. */
4549 2, /* max returned param. */
4550 state->data, /* data. */
4552 12); /* max returned data. */
4554 if (tevent_req_nomem(subreq, req)) {
4555 return tevent_req_post(req, ev);
4557 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4561 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4562 struct event_context *ev,
4563 struct cli_state *cli,
4568 return cli_posix_open_internal_send(mem_ctx, ev,
4569 cli, fname, flags, mode, false);
4572 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4574 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4577 if (tevent_req_is_nterror(req, &status)) {
4580 *pfnum = state->fnum;
4581 return NT_STATUS_OK;
4584 /****************************************************************************
4585 Open - POSIX semantics. Doesn't request oplock.
4586 ****************************************************************************/
4588 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4589 int flags, mode_t mode, uint16_t *pfnum)
4592 TALLOC_CTX *frame = talloc_stackframe();
4593 struct event_context *ev = NULL;
4594 struct tevent_req *req = NULL;
4595 NTSTATUS status = NT_STATUS_OK;
4597 if (cli_has_async_calls(cli)) {
4599 * Can't use sync call while an async call is in flight
4601 status = NT_STATUS_INVALID_PARAMETER;
4605 ev = event_context_init(frame);
4607 status = NT_STATUS_NO_MEMORY;
4611 req = cli_posix_open_send(frame,
4618 status = NT_STATUS_NO_MEMORY;
4622 if (!tevent_req_poll(req, ev)) {
4623 status = map_nt_error_from_unix(errno);
4627 status = cli_posix_open_recv(req, pfnum);
4634 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4635 struct event_context *ev,
4636 struct cli_state *cli,
4640 return cli_posix_open_internal_send(mem_ctx, ev,
4641 cli, fname, O_CREAT, mode, true);
4644 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4646 return tevent_req_simple_recv_ntstatus(req);
4649 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4651 TALLOC_CTX *frame = talloc_stackframe();
4652 struct event_context *ev = NULL;
4653 struct tevent_req *req = NULL;
4654 NTSTATUS status = NT_STATUS_OK;
4656 if (cli_has_async_calls(cli)) {
4658 * Can't use sync call while an async call is in flight
4660 status = NT_STATUS_INVALID_PARAMETER;
4664 ev = event_context_init(frame);
4666 status = NT_STATUS_NO_MEMORY;
4670 req = cli_posix_mkdir_send(frame,
4676 status = NT_STATUS_NO_MEMORY;
4680 if (!tevent_req_poll(req, ev)) {
4681 status = map_nt_error_from_unix(errno);
4685 status = cli_posix_mkdir_recv(req);
4692 /****************************************************************************
4693 unlink or rmdir - POSIX semantics.
4694 ****************************************************************************/
4696 struct cli_posix_unlink_internal_state {
4700 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4702 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4703 struct event_context *ev,
4704 struct cli_state *cli,
4708 struct tevent_req *req = NULL, *subreq = NULL;
4709 struct cli_posix_unlink_internal_state *state = NULL;
4711 req = tevent_req_create(mem_ctx, &state,
4712 struct cli_posix_unlink_internal_state);
4717 /* Setup data word. */
4718 SSVAL(state->data, 0, level);
4720 subreq = cli_setpathinfo_send(state, ev, cli,
4721 SMB_POSIX_PATH_UNLINK,
4723 state->data, sizeof(state->data));
4724 if (tevent_req_nomem(subreq, req)) {
4725 return tevent_req_post(req, ev);
4727 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4731 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4733 NTSTATUS status = cli_setpathinfo_recv(subreq);
4734 tevent_req_simple_finish_ntstatus(subreq, status);
4737 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4738 struct event_context *ev,
4739 struct cli_state *cli,
4742 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4743 SMB_POSIX_UNLINK_FILE_TARGET);
4746 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4748 return tevent_req_simple_recv_ntstatus(req);
4751 /****************************************************************************
4752 unlink - POSIX semantics.
4753 ****************************************************************************/
4755 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4757 TALLOC_CTX *frame = talloc_stackframe();
4758 struct event_context *ev = NULL;
4759 struct tevent_req *req = NULL;
4760 NTSTATUS status = NT_STATUS_OK;
4762 if (cli_has_async_calls(cli)) {
4764 * Can't use sync call while an async call is in flight
4766 status = NT_STATUS_INVALID_PARAMETER;
4770 ev = event_context_init(frame);
4772 status = NT_STATUS_NO_MEMORY;
4776 req = cli_posix_unlink_send(frame,
4781 status = NT_STATUS_NO_MEMORY;
4785 if (!tevent_req_poll(req, ev)) {
4786 status = map_nt_error_from_unix(errno);
4790 status = cli_posix_unlink_recv(req);
4797 /****************************************************************************
4798 rmdir - POSIX semantics.
4799 ****************************************************************************/
4801 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4802 struct event_context *ev,
4803 struct cli_state *cli,
4806 return cli_posix_unlink_internal_send(
4807 mem_ctx, ev, cli, fname,
4808 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4811 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4813 return tevent_req_simple_recv_ntstatus(req);
4816 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4818 TALLOC_CTX *frame = talloc_stackframe();
4819 struct event_context *ev = NULL;
4820 struct tevent_req *req = NULL;
4821 NTSTATUS status = NT_STATUS_OK;
4823 if (cli_has_async_calls(cli)) {
4825 * Can't use sync call while an async call is in flight
4827 status = NT_STATUS_INVALID_PARAMETER;
4831 ev = event_context_init(frame);
4833 status = NT_STATUS_NO_MEMORY;
4837 req = cli_posix_rmdir_send(frame,
4842 status = NT_STATUS_NO_MEMORY;
4846 if (!tevent_req_poll(req, ev)) {
4847 status = map_nt_error_from_unix(errno);
4851 status = cli_posix_rmdir_recv(req, frame);
4858 /****************************************************************************
4860 ****************************************************************************/
4862 struct cli_notify_state {
4864 uint32_t num_changes;
4865 struct notify_change *changes;
4868 static void cli_notify_done(struct tevent_req *subreq);
4870 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4871 struct tevent_context *ev,
4872 struct cli_state *cli, uint16_t fnum,
4873 uint32_t buffer_size,
4874 uint32_t completion_filter, bool recursive)
4876 struct tevent_req *req, *subreq;
4877 struct cli_notify_state *state;
4879 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4884 SIVAL(state->setup, 0, completion_filter);
4885 SSVAL(state->setup, 4, fnum);
4886 SSVAL(state->setup, 6, recursive);
4888 subreq = cli_trans_send(
4889 state, /* mem ctx. */
4890 ev, /* event ctx. */
4891 cli, /* cli_state. */
4892 SMBnttrans, /* cmd. */
4893 NULL, /* pipe name. */
4895 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4897 (uint16_t *)state->setup, /* setup. */
4898 4, /* num setup uint16_t words. */
4899 0, /* max returned setup. */
4902 buffer_size, /* max returned param. */
4905 0); /* max returned data. */
4907 if (tevent_req_nomem(subreq, req)) {
4908 return tevent_req_post(req, ev);
4910 tevent_req_set_callback(subreq, cli_notify_done, req);
4914 static void cli_notify_done(struct tevent_req *subreq)
4916 struct tevent_req *req = tevent_req_callback_data(
4917 subreq, struct tevent_req);
4918 struct cli_notify_state *state = tevent_req_data(
4919 req, struct cli_notify_state);
4922 uint32_t i, ofs, num_params;
4925 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4926 ¶ms, 0, &num_params, NULL, 0, NULL);
4927 TALLOC_FREE(subreq);
4928 if (tevent_req_nterror(req, status)) {
4929 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4933 state->num_changes = 0;
4936 while (num_params - ofs > 12) {
4937 uint32_t len = IVAL(params, ofs);
4938 state->num_changes += 1;
4940 if ((len == 0) || (ofs+len >= num_params)) {
4946 state->changes = talloc_array(state, struct notify_change,
4947 state->num_changes);
4948 if (tevent_req_nomem(state->changes, req)) {
4949 TALLOC_FREE(params);
4955 for (i=0; i<state->num_changes; i++) {
4956 uint32_t next = IVAL(params, ofs);
4957 uint32_t len = IVAL(params, ofs+8);
4961 if ((next != 0) && (len+12 != next)) {
4962 TALLOC_FREE(params);
4964 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4968 state->changes[i].action = IVAL(params, ofs+4);
4969 ret = clistr_pull_talloc(params, (char *)params, flags2,
4970 &name, params+ofs+12, len,
4971 STR_TERMINATE|STR_UNICODE);
4973 TALLOC_FREE(params);
4974 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4977 state->changes[i].name = name;
4981 TALLOC_FREE(params);
4982 tevent_req_done(req);
4985 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4986 uint32_t *pnum_changes,
4987 struct notify_change **pchanges)
4989 struct cli_notify_state *state = tevent_req_data(
4990 req, struct cli_notify_state);
4993 if (tevent_req_is_nterror(req, &status)) {
4997 *pnum_changes = state->num_changes;
4998 *pchanges = talloc_move(mem_ctx, &state->changes);
4999 return NT_STATUS_OK;
5002 struct cli_qpathinfo_state {
5011 static void cli_qpathinfo_done(struct tevent_req *subreq);
5013 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5014 struct tevent_context *ev,
5015 struct cli_state *cli, const char *fname,
5016 uint16_t level, uint32_t min_rdata,
5019 struct tevent_req *req, *subreq;
5020 struct cli_qpathinfo_state *state;
5022 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5026 state->min_rdata = min_rdata;
5027 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5029 state->param = talloc_zero_array(state, uint8_t, 6);
5030 if (tevent_req_nomem(state->param, req)) {
5031 return tevent_req_post(req, ev);
5033 SSVAL(state->param, 0, level);
5034 state->param = trans2_bytes_push_str(
5035 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
5036 if (tevent_req_nomem(state->param, req)) {
5037 return tevent_req_post(req, ev);
5040 subreq = cli_trans_send(
5041 state, /* mem ctx. */
5042 ev, /* event ctx. */
5043 cli, /* cli_state. */
5044 SMBtrans2, /* cmd. */
5045 NULL, /* pipe name. */
5049 state->setup, /* setup. */
5050 1, /* num setup uint16_t words. */
5051 0, /* max returned setup. */
5052 state->param, /* param. */
5053 talloc_get_size(state->param), /* num param. */
5054 2, /* max returned param. */
5057 max_rdata); /* max returned data. */
5059 if (tevent_req_nomem(subreq, req)) {
5060 return tevent_req_post(req, ev);
5062 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5066 static void cli_qpathinfo_done(struct tevent_req *subreq)
5068 struct tevent_req *req = tevent_req_callback_data(
5069 subreq, struct tevent_req);
5070 struct cli_qpathinfo_state *state = tevent_req_data(
5071 req, struct cli_qpathinfo_state);
5074 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5076 &state->rdata, state->min_rdata,
5078 if (tevent_req_nterror(req, status)) {
5081 tevent_req_done(req);
5084 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5085 uint8_t **rdata, uint32_t *num_rdata)
5087 struct cli_qpathinfo_state *state = tevent_req_data(
5088 req, struct cli_qpathinfo_state);
5091 if (tevent_req_is_nterror(req, &status)) {
5094 if (rdata != NULL) {
5095 *rdata = talloc_move(mem_ctx, &state->rdata);
5097 TALLOC_FREE(state->rdata);
5099 if (num_rdata != NULL) {
5100 *num_rdata = state->num_rdata;
5102 return NT_STATUS_OK;
5105 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5106 const char *fname, uint16_t level, uint32_t min_rdata,
5108 uint8_t **rdata, uint32_t *num_rdata)
5110 TALLOC_CTX *frame = talloc_stackframe();
5111 struct event_context *ev;
5112 struct tevent_req *req;
5113 NTSTATUS status = NT_STATUS_NO_MEMORY;
5115 if (cli_has_async_calls(cli)) {
5117 * Can't use sync call while an async call is in flight
5119 status = NT_STATUS_INVALID_PARAMETER;
5122 ev = event_context_init(frame);
5126 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5131 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5134 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5140 struct cli_qfileinfo_state {
5144 uint16_t recv_flags2;
5150 static void cli_qfileinfo_done(struct tevent_req *subreq);
5152 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5153 struct tevent_context *ev,
5154 struct cli_state *cli, uint16_t fnum,
5155 uint16_t level, uint32_t min_rdata,
5158 struct tevent_req *req, *subreq;
5159 struct cli_qfileinfo_state *state;
5161 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5165 state->min_rdata = min_rdata;
5166 SSVAL(state->param, 0, fnum);
5167 SSVAL(state->param, 2, level);
5168 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5170 subreq = cli_trans_send(
5171 state, /* mem ctx. */
5172 ev, /* event ctx. */
5173 cli, /* cli_state. */
5174 SMBtrans2, /* cmd. */
5175 NULL, /* pipe name. */
5179 state->setup, /* setup. */
5180 1, /* num setup uint16_t words. */
5181 0, /* max returned setup. */
5182 state->param, /* param. */
5183 sizeof(state->param), /* num param. */
5184 2, /* max returned param. */
5187 max_rdata); /* max returned data. */
5189 if (tevent_req_nomem(subreq, req)) {
5190 return tevent_req_post(req, ev);
5192 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5196 static void cli_qfileinfo_done(struct tevent_req *subreq)
5198 struct tevent_req *req = tevent_req_callback_data(
5199 subreq, struct tevent_req);
5200 struct cli_qfileinfo_state *state = tevent_req_data(
5201 req, struct cli_qfileinfo_state);
5204 status = cli_trans_recv(subreq, state,
5205 &state->recv_flags2,
5208 &state->rdata, state->min_rdata,
5210 if (tevent_req_nterror(req, status)) {
5213 tevent_req_done(req);
5216 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5217 uint16_t *recv_flags2,
5218 uint8_t **rdata, uint32_t *num_rdata)
5220 struct cli_qfileinfo_state *state = tevent_req_data(
5221 req, struct cli_qfileinfo_state);
5224 if (tevent_req_is_nterror(req, &status)) {
5228 if (recv_flags2 != NULL) {
5229 *recv_flags2 = state->recv_flags2;
5231 if (rdata != NULL) {
5232 *rdata = talloc_move(mem_ctx, &state->rdata);
5234 TALLOC_FREE(state->rdata);
5236 if (num_rdata != NULL) {
5237 *num_rdata = state->num_rdata;
5239 return NT_STATUS_OK;
5242 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5243 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5244 uint32_t max_rdata, uint16_t *recv_flags2,
5245 uint8_t **rdata, uint32_t *num_rdata)
5247 TALLOC_CTX *frame = talloc_stackframe();
5248 struct event_context *ev;
5249 struct tevent_req *req;
5250 NTSTATUS status = NT_STATUS_NO_MEMORY;
5252 if (cli_has_async_calls(cli)) {
5254 * Can't use sync call while an async call is in flight
5256 status = NT_STATUS_INVALID_PARAMETER;
5259 ev = event_context_init(frame);
5263 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5268 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5271 status = cli_qfileinfo_recv(req, mem_ctx, recv_flags2, rdata, num_rdata);
5277 struct cli_flush_state {
5281 static void cli_flush_done(struct tevent_req *subreq);
5283 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5284 struct event_context *ev,
5285 struct cli_state *cli,
5288 struct tevent_req *req, *subreq;
5289 struct cli_flush_state *state;
5291 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5295 SSVAL(state->vwv + 0, 0, fnum);
5297 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5299 if (tevent_req_nomem(subreq, req)) {
5300 return tevent_req_post(req, ev);
5302 tevent_req_set_callback(subreq, cli_flush_done, req);
5306 static void cli_flush_done(struct tevent_req *subreq)
5308 struct tevent_req *req = tevent_req_callback_data(
5309 subreq, struct tevent_req);
5312 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5313 TALLOC_FREE(subreq);
5314 if (tevent_req_nterror(req, status)) {
5317 tevent_req_done(req);
5320 NTSTATUS cli_flush_recv(struct tevent_req *req)
5322 return tevent_req_simple_recv_ntstatus(req);
5325 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5327 TALLOC_CTX *frame = talloc_stackframe();
5328 struct event_context *ev;
5329 struct tevent_req *req;
5330 NTSTATUS status = NT_STATUS_NO_MEMORY;
5332 if (cli_has_async_calls(cli)) {
5334 * Can't use sync call while an async call is in flight
5336 status = NT_STATUS_INVALID_PARAMETER;
5339 ev = event_context_init(frame);
5343 req = cli_flush_send(frame, ev, cli, fnum);
5347 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5350 status = cli_flush_recv(req);
5356 struct cli_shadow_copy_data_state {
5363 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5365 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5366 struct tevent_context *ev,
5367 struct cli_state *cli,
5371 struct tevent_req *req, *subreq;
5372 struct cli_shadow_copy_data_state *state;
5375 req = tevent_req_create(mem_ctx, &state,
5376 struct cli_shadow_copy_data_state);
5380 state->get_names = get_names;
5381 ret_size = get_names ? CLI_BUFFER_SIZE : 16;
5383 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5384 SSVAL(state->setup + 2, 0, fnum);
5385 SCVAL(state->setup + 3, 0, 0); /* isFsctl */
5386 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5388 subreq = cli_trans_send(
5389 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5390 state->setup, ARRAY_SIZE(state->setup), 0,
5393 if (tevent_req_nomem(subreq, req)) {
5394 return tevent_req_post(req, ev);
5396 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5400 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5402 struct tevent_req *req = tevent_req_callback_data(
5403 subreq, struct tevent_req);
5404 struct cli_shadow_copy_data_state *state = tevent_req_data(
5405 req, struct cli_shadow_copy_data_state);
5408 status = cli_trans_recv(subreq, state, NULL,
5409 NULL, 0, NULL, /* setup */
5410 NULL, 0, NULL, /* param */
5411 &state->data, 12, &state->num_data);
5412 TALLOC_FREE(subreq);
5413 if (tevent_req_nterror(req, status)) {
5416 tevent_req_done(req);
5419 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5420 char ***pnames, int *pnum_names)
5422 struct cli_shadow_copy_data_state *state = tevent_req_data(
5423 req, struct cli_shadow_copy_data_state);
5429 if (tevent_req_is_nterror(req, &status)) {
5432 num_names = IVAL(state->data, 4);
5433 dlength = IVAL(state->data, 8);
5435 if (!state->get_names) {
5436 *pnum_names = num_names;
5437 return NT_STATUS_OK;
5440 if (dlength+12 > state->num_data) {
5441 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5443 names = talloc_array(mem_ctx, char *, num_names);
5444 if (names == NULL) {
5445 return NT_STATUS_NO_MEMORY;
5448 for (i=0; i<num_names; i++) {
5451 size_t converted_size;
5453 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5454 ret = convert_string_talloc(
5455 names, CH_UTF16LE, CH_UNIX,
5456 src, 2 * sizeof(SHADOW_COPY_LABEL),
5457 &names[i], &converted_size);
5460 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5463 *pnum_names = num_names;
5465 return NT_STATUS_OK;
5468 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5469 uint16_t fnum, bool get_names,
5470 char ***pnames, int *pnum_names)
5472 TALLOC_CTX *frame = talloc_stackframe();
5473 struct event_context *ev;
5474 struct tevent_req *req;
5475 NTSTATUS status = NT_STATUS_NO_MEMORY;
5477 if (cli_has_async_calls(cli)) {
5479 * Can't use sync call while an async call is in flight
5481 status = NT_STATUS_INVALID_PARAMETER;
5484 ev = event_context_init(frame);
5488 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5492 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5495 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);