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 "async_smb.h"
24 #include "libsmb/clirap.h"
28 /***********************************************************
29 Common function for pushing stings, used by smb_bytes_push_str()
30 and trans_bytes_push_str(). Only difference is the align_odd
32 ***********************************************************/
34 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
35 const char *str, size_t str_len,
37 size_t *pconverted_size)
41 size_t converted_size;
47 buflen = talloc_get_size(buf);
49 if (align_odd && ucs2 && (buflen % 2 == 0)) {
51 * We're pushing into an SMB buffer, align odd
53 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
61 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
62 ucs2 ? CH_UTF16LE : CH_DOS,
63 str, str_len, &converted,
68 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
69 buflen + converted_size);
71 TALLOC_FREE(converted);
75 memcpy(buf + buflen, converted, converted_size);
77 TALLOC_FREE(converted);
79 if (pconverted_size) {
80 *pconverted_size = converted_size;
86 /***********************************************************
87 Push a string into an SMB buffer, with odd byte alignment
88 if it's a UCS2 string.
89 ***********************************************************/
91 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
92 const char *str, size_t str_len,
93 size_t *pconverted_size)
95 return internal_bytes_push_str(buf, ucs2, str, str_len,
96 true, pconverted_size);
99 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
100 const uint8_t *bytes, size_t num_bytes)
107 buflen = talloc_get_size(buf);
109 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
110 buflen + 1 + num_bytes);
114 buf[buflen] = prefix;
115 memcpy(&buf[buflen+1], bytes, num_bytes);
119 /***********************************************************
120 Same as smb_bytes_push_str(), but without the odd byte
121 align for ucs2 (we're pushing into a param or data block).
122 static for now, although this will probably change when
123 other modules use async trans calls.
124 ***********************************************************/
126 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
127 const char *str, size_t str_len,
128 size_t *pconverted_size)
130 return internal_bytes_push_str(buf, ucs2, str, str_len,
131 false, pconverted_size);
134 struct cli_setpathinfo_state {
139 static void cli_setpathinfo_done(struct tevent_req *subreq);
141 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
142 struct tevent_context *ev,
143 struct cli_state *cli,
149 struct tevent_req *req, *subreq;
150 struct cli_setpathinfo_state *state;
152 req = tevent_req_create(mem_ctx, &state,
153 struct cli_setpathinfo_state);
158 /* Setup setup word. */
159 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
161 /* Setup param array. */
162 state->param = TALLOC_ZERO_ARRAY(state, uint8_t, 6);
163 if (tevent_req_nomem(state->param, req)) {
164 return tevent_req_post(req, ev);
166 SSVAL(state->param, 0, level);
168 state->param = trans2_bytes_push_str(
169 state->param, cli_ucs2(cli), path, strlen(path)+1, NULL);
170 if (tevent_req_nomem(state->param, req)) {
171 return tevent_req_post(req, ev);
174 subreq = cli_trans_send(
175 state, /* mem ctx. */
177 cli, /* cli_state. */
178 SMBtrans2, /* cmd. */
179 NULL, /* pipe name. */
183 &state->setup, /* setup. */
184 1, /* num setup uint16_t words. */
185 0, /* max returned setup. */
186 state->param, /* param. */
187 talloc_get_size(state->param), /* num param. */
188 2, /* max returned param. */
190 data_len, /* num data. */
191 0); /* max returned data. */
193 if (tevent_req_nomem(subreq, req)) {
194 return tevent_req_post(req, ev);
196 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
200 static void cli_setpathinfo_done(struct tevent_req *subreq)
202 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
203 NULL, 0, NULL, NULL, 0, NULL);
204 tevent_req_simple_finish_ntstatus(subreq, status);
207 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
209 return tevent_req_simple_recv_ntstatus(req);
212 NTSTATUS cli_setpathinfo(struct cli_state *cli,
218 TALLOC_CTX *frame = talloc_stackframe();
219 struct tevent_context *ev;
220 struct tevent_req *req;
221 NTSTATUS status = NT_STATUS_NO_MEMORY;
223 if (cli_has_async_calls(cli)) {
225 * Can't use sync call while an async call is in flight
227 status = NT_STATUS_INVALID_PARAMETER;
230 ev = tevent_context_init(frame);
234 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
238 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
241 status = cli_setpathinfo_recv(req);
247 /****************************************************************************
248 Hard/Symlink a file (UNIX extensions).
249 Creates new name (sym)linked to oldname.
250 ****************************************************************************/
252 struct cli_posix_link_internal_state {
256 static void cli_posix_link_internal_done(struct tevent_req *subreq);
258 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
259 struct event_context *ev,
260 struct cli_state *cli,
265 struct tevent_req *req = NULL, *subreq = NULL;
266 struct cli_posix_link_internal_state *state = NULL;
268 req = tevent_req_create(mem_ctx, &state,
269 struct cli_posix_link_internal_state);
274 /* Setup data array. */
275 state->data = talloc_array(state, uint8_t, 0);
276 if (tevent_req_nomem(state->data, req)) {
277 return tevent_req_post(req, ev);
279 state->data = trans2_bytes_push_str(
280 state->data, cli_ucs2(cli), oldname, strlen(oldname)+1, NULL);
282 subreq = cli_setpathinfo_send(
283 state, ev, cli, level, newname,
284 state->data, talloc_get_size(state->data));
285 if (tevent_req_nomem(subreq, req)) {
286 return tevent_req_post(req, ev);
288 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
292 static void cli_posix_link_internal_done(struct tevent_req *subreq)
294 NTSTATUS status = cli_setpathinfo_recv(subreq);
295 tevent_req_simple_finish_ntstatus(subreq, status);
298 /****************************************************************************
299 Symlink a file (UNIX extensions).
300 ****************************************************************************/
302 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
303 struct event_context *ev,
304 struct cli_state *cli,
308 return cli_posix_link_internal_send(
309 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
312 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
314 return tevent_req_simple_recv_ntstatus(req);
317 NTSTATUS cli_posix_symlink(struct cli_state *cli,
321 TALLOC_CTX *frame = talloc_stackframe();
322 struct event_context *ev = NULL;
323 struct tevent_req *req = NULL;
324 NTSTATUS status = NT_STATUS_OK;
326 if (cli_has_async_calls(cli)) {
328 * Can't use sync call while an async call is in flight
330 status = NT_STATUS_INVALID_PARAMETER;
334 ev = event_context_init(frame);
336 status = NT_STATUS_NO_MEMORY;
340 req = cli_posix_symlink_send(frame,
346 status = NT_STATUS_NO_MEMORY;
350 if (!tevent_req_poll(req, ev)) {
351 status = map_nt_error_from_unix(errno);
355 status = cli_posix_symlink_recv(req);
359 if (!NT_STATUS_IS_OK(status)) {
360 cli_set_error(cli, status);
365 /****************************************************************************
366 Read a POSIX symlink.
367 ****************************************************************************/
369 struct readlink_state {
374 static void cli_posix_readlink_done(struct tevent_req *subreq);
376 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
377 struct event_context *ev,
378 struct cli_state *cli,
382 struct tevent_req *req = NULL, *subreq = NULL;
383 struct readlink_state *state = NULL;
384 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
386 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
392 * Len is in bytes, we need it in UCS2 units.
394 if ((2*len < len) || (maxbytelen < len)) {
395 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
396 return tevent_req_post(req, ev);
399 subreq = cli_qpathinfo_send(state, ev, cli, fname,
400 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
401 if (tevent_req_nomem(subreq, req)) {
402 return tevent_req_post(req, ev);
404 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
408 static void cli_posix_readlink_done(struct tevent_req *subreq)
410 struct tevent_req *req = tevent_req_callback_data(
411 subreq, struct tevent_req);
412 struct readlink_state *state = tevent_req_data(
413 req, struct readlink_state);
416 status = cli_qpathinfo_recv(subreq, state, &state->data,
419 if (!NT_STATUS_IS_OK(status)) {
420 tevent_req_nterror(req, status);
424 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
426 if (state->data[state->num_data-1] != '\0') {
427 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
430 tevent_req_done(req);
433 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
434 char *retpath, size_t len)
437 char *converted = NULL;
438 size_t converted_size = 0;
439 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
441 if (tevent_req_is_nterror(req, &status)) {
444 /* The returned data is a pushed string, not raw data. */
445 if (!convert_string_talloc(state,
446 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
452 return NT_STATUS_NO_MEMORY;
455 len = MIN(len,converted_size);
457 return NT_STATUS_DATA_ERROR;
459 memcpy(retpath, converted, len);
463 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
464 char *linkpath, size_t len)
466 TALLOC_CTX *frame = talloc_stackframe();
467 struct event_context *ev = NULL;
468 struct tevent_req *req = NULL;
469 NTSTATUS status = NT_STATUS_OK;
471 if (cli_has_async_calls(cli)) {
473 * Can't use sync call while an async call is in flight
475 status = NT_STATUS_INVALID_PARAMETER;
479 ev = event_context_init(frame);
481 status = NT_STATUS_NO_MEMORY;
485 req = cli_posix_readlink_send(frame,
491 status = NT_STATUS_NO_MEMORY;
495 if (!tevent_req_poll(req, ev)) {
496 status = map_nt_error_from_unix(errno);
500 status = cli_posix_readlink_recv(req, cli, linkpath, len);
504 if (!NT_STATUS_IS_OK(status)) {
505 cli_set_error(cli, status);
510 /****************************************************************************
511 Hard link a file (UNIX extensions).
512 ****************************************************************************/
514 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
515 struct event_context *ev,
516 struct cli_state *cli,
520 return cli_posix_link_internal_send(
521 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
524 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
526 return tevent_req_simple_recv_ntstatus(req);
529 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
533 TALLOC_CTX *frame = talloc_stackframe();
534 struct event_context *ev = NULL;
535 struct tevent_req *req = NULL;
536 NTSTATUS status = NT_STATUS_OK;
538 if (cli_has_async_calls(cli)) {
540 * Can't use sync call while an async call is in flight
542 status = NT_STATUS_INVALID_PARAMETER;
546 ev = event_context_init(frame);
548 status = NT_STATUS_NO_MEMORY;
552 req = cli_posix_hardlink_send(frame,
558 status = NT_STATUS_NO_MEMORY;
562 if (!tevent_req_poll(req, ev)) {
563 status = map_nt_error_from_unix(errno);
567 status = cli_posix_hardlink_recv(req);
571 if (!NT_STATUS_IS_OK(status)) {
572 cli_set_error(cli, status);
577 /****************************************************************************
578 Map standard UNIX permissions onto wire representations.
579 ****************************************************************************/
581 uint32_t unix_perms_to_wire(mode_t perms)
583 unsigned int ret = 0;
585 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
586 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
587 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
588 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
589 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
590 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
591 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
592 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
593 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
595 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
598 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
601 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
606 /****************************************************************************
607 Map wire permissions to standard UNIX.
608 ****************************************************************************/
610 mode_t wire_perms_to_unix(uint32_t perms)
612 mode_t ret = (mode_t)0;
614 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
615 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
616 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
617 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
618 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
619 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
620 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
621 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
622 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
624 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
627 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
630 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
635 /****************************************************************************
636 Return the file type from the wire filetype for UNIX extensions.
637 ****************************************************************************/
639 static mode_t unix_filetype_from_wire(uint32_t wire_type)
647 case UNIX_TYPE_SYMLINK:
651 case UNIX_TYPE_CHARDEV:
655 case UNIX_TYPE_BLKDEV:
663 case UNIX_TYPE_SOCKET:
671 /****************************************************************************
672 Do a POSIX getfacl (UNIX extensions).
673 ****************************************************************************/
675 struct getfacl_state {
680 static void cli_posix_getfacl_done(struct tevent_req *subreq);
682 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
683 struct event_context *ev,
684 struct cli_state *cli,
687 struct tevent_req *req = NULL, *subreq = NULL;
688 struct getfacl_state *state = NULL;
690 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
694 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
696 if (tevent_req_nomem(subreq, req)) {
697 return tevent_req_post(req, ev);
699 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
703 static void cli_posix_getfacl_done(struct tevent_req *subreq)
705 struct tevent_req *req = tevent_req_callback_data(
706 subreq, struct tevent_req);
707 struct getfacl_state *state = tevent_req_data(
708 req, struct getfacl_state);
711 status = cli_qpathinfo_recv(subreq, state, &state->data,
714 if (!NT_STATUS_IS_OK(status)) {
715 tevent_req_nterror(req, status);
718 tevent_req_done(req);
721 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
726 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
729 if (tevent_req_is_nterror(req, &status)) {
732 *prb_size = (size_t)state->num_data;
733 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
737 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
743 TALLOC_CTX *frame = talloc_stackframe();
744 struct event_context *ev = NULL;
745 struct tevent_req *req = NULL;
746 NTSTATUS status = NT_STATUS_OK;
748 if (cli_has_async_calls(cli)) {
750 * Can't use sync call while an async call is in flight
752 status = NT_STATUS_INVALID_PARAMETER;
756 ev = event_context_init(frame);
758 status = NT_STATUS_NO_MEMORY;
762 req = cli_posix_getfacl_send(frame,
767 status = NT_STATUS_NO_MEMORY;
771 if (!tevent_req_poll(req, ev)) {
772 status = map_nt_error_from_unix(errno);
776 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
780 if (!NT_STATUS_IS_OK(status)) {
781 cli_set_error(cli, status);
786 /****************************************************************************
787 Stat a file (UNIX extensions).
788 ****************************************************************************/
795 static void cli_posix_stat_done(struct tevent_req *subreq);
797 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
798 struct event_context *ev,
799 struct cli_state *cli,
802 struct tevent_req *req = NULL, *subreq = NULL;
803 struct stat_state *state = NULL;
805 req = tevent_req_create(mem_ctx, &state, struct stat_state);
809 subreq = cli_qpathinfo_send(state, ev, cli, fname,
810 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
811 if (tevent_req_nomem(subreq, req)) {
812 return tevent_req_post(req, ev);
814 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
818 static void cli_posix_stat_done(struct tevent_req *subreq)
820 struct tevent_req *req = tevent_req_callback_data(
821 subreq, struct tevent_req);
822 struct stat_state *state = tevent_req_data(req, struct stat_state);
825 status = cli_qpathinfo_recv(subreq, state, &state->data,
828 if (!NT_STATUS_IS_OK(status)) {
829 tevent_req_nterror(req, status);
832 tevent_req_done(req);
835 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
836 SMB_STRUCT_STAT *sbuf)
838 struct stat_state *state = tevent_req_data(req, struct stat_state);
841 if (tevent_req_is_nterror(req, &status)) {
845 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
846 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
847 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
848 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
850 /* assume 512 byte blocks */
851 sbuf->st_ex_blocks /= 512;
853 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
854 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
855 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
857 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
858 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
859 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
860 #if defined(HAVE_MAKEDEV)
862 uint32_t dev_major = IVAL(state->data,60);
863 uint32_t dev_minor = IVAL(state->data,68);
864 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
867 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
868 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
869 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
874 NTSTATUS cli_posix_stat(struct cli_state *cli,
876 SMB_STRUCT_STAT *sbuf)
878 TALLOC_CTX *frame = talloc_stackframe();
879 struct event_context *ev = NULL;
880 struct tevent_req *req = NULL;
881 NTSTATUS status = NT_STATUS_OK;
883 if (cli_has_async_calls(cli)) {
885 * Can't use sync call while an async call is in flight
887 status = NT_STATUS_INVALID_PARAMETER;
891 ev = event_context_init(frame);
893 status = NT_STATUS_NO_MEMORY;
897 req = cli_posix_stat_send(frame,
902 status = NT_STATUS_NO_MEMORY;
906 if (!tevent_req_poll(req, ev)) {
907 status = map_nt_error_from_unix(errno);
911 status = cli_posix_stat_recv(req, sbuf);
915 if (!NT_STATUS_IS_OK(status)) {
916 cli_set_error(cli, status);
921 /****************************************************************************
922 Chmod or chown a file internal (UNIX extensions).
923 ****************************************************************************/
925 struct cli_posix_chown_chmod_internal_state {
929 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
931 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
932 struct event_context *ev,
933 struct cli_state *cli,
939 struct tevent_req *req = NULL, *subreq = NULL;
940 struct cli_posix_chown_chmod_internal_state *state = NULL;
942 req = tevent_req_create(mem_ctx, &state,
943 struct cli_posix_chown_chmod_internal_state);
948 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
949 memset(&state->data[40], '\0', 60);
950 SIVAL(state->data,40,uid);
951 SIVAL(state->data,48,gid);
952 SIVAL(state->data,84,mode);
954 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
955 fname, state->data, sizeof(state->data));
956 if (tevent_req_nomem(subreq, req)) {
957 return tevent_req_post(req, ev);
959 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
964 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
966 NTSTATUS status = cli_setpathinfo_recv(subreq);
967 tevent_req_simple_finish_ntstatus(subreq, status);
970 /****************************************************************************
971 chmod a file (UNIX extensions).
972 ****************************************************************************/
974 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
975 struct event_context *ev,
976 struct cli_state *cli,
980 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
982 unix_perms_to_wire(mode),
987 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
989 return tevent_req_simple_recv_ntstatus(req);
992 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
994 TALLOC_CTX *frame = talloc_stackframe();
995 struct event_context *ev = NULL;
996 struct tevent_req *req = NULL;
997 NTSTATUS status = NT_STATUS_OK;
999 if (cli_has_async_calls(cli)) {
1001 * Can't use sync call while an async call is in flight
1003 status = NT_STATUS_INVALID_PARAMETER;
1007 ev = event_context_init(frame);
1009 status = NT_STATUS_NO_MEMORY;
1013 req = cli_posix_chmod_send(frame,
1019 status = NT_STATUS_NO_MEMORY;
1023 if (!tevent_req_poll(req, ev)) {
1024 status = map_nt_error_from_unix(errno);
1028 status = cli_posix_chmod_recv(req);
1032 if (!NT_STATUS_IS_OK(status)) {
1033 cli_set_error(cli, status);
1038 /****************************************************************************
1039 chown a file (UNIX extensions).
1040 ****************************************************************************/
1042 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1043 struct event_context *ev,
1044 struct cli_state *cli,
1049 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1056 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1058 return tevent_req_simple_recv_ntstatus(req);
1061 NTSTATUS cli_posix_chown(struct cli_state *cli,
1066 TALLOC_CTX *frame = talloc_stackframe();
1067 struct event_context *ev = NULL;
1068 struct tevent_req *req = NULL;
1069 NTSTATUS status = NT_STATUS_OK;
1071 if (cli_has_async_calls(cli)) {
1073 * Can't use sync call while an async call is in flight
1075 status = NT_STATUS_INVALID_PARAMETER;
1079 ev = event_context_init(frame);
1081 status = NT_STATUS_NO_MEMORY;
1085 req = cli_posix_chown_send(frame,
1092 status = NT_STATUS_NO_MEMORY;
1096 if (!tevent_req_poll(req, ev)) {
1097 status = map_nt_error_from_unix(errno);
1101 status = cli_posix_chown_recv(req);
1105 if (!NT_STATUS_IS_OK(status)) {
1106 cli_set_error(cli, status);
1111 /****************************************************************************
1113 ****************************************************************************/
1115 static void cli_rename_done(struct tevent_req *subreq);
1117 struct cli_rename_state {
1121 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1122 struct event_context *ev,
1123 struct cli_state *cli,
1124 const char *fname_src,
1125 const char *fname_dst)
1127 struct tevent_req *req = NULL, *subreq = NULL;
1128 struct cli_rename_state *state = NULL;
1129 uint8_t additional_flags = 0;
1130 uint8_t *bytes = NULL;
1132 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1137 SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
1139 bytes = talloc_array(state, uint8_t, 1);
1140 if (tevent_req_nomem(bytes, req)) {
1141 return tevent_req_post(req, ev);
1144 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1145 strlen(fname_src)+1, NULL);
1146 if (tevent_req_nomem(bytes, req)) {
1147 return tevent_req_post(req, ev);
1150 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1151 talloc_get_size(bytes)+1);
1152 if (tevent_req_nomem(bytes, req)) {
1153 return tevent_req_post(req, ev);
1156 bytes[talloc_get_size(bytes)-1] = 4;
1157 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1158 strlen(fname_dst)+1, NULL);
1159 if (tevent_req_nomem(bytes, req)) {
1160 return tevent_req_post(req, ev);
1163 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1164 1, state->vwv, talloc_get_size(bytes), bytes);
1165 if (tevent_req_nomem(subreq, req)) {
1166 return tevent_req_post(req, ev);
1168 tevent_req_set_callback(subreq, cli_rename_done, req);
1172 static void cli_rename_done(struct tevent_req *subreq)
1174 struct tevent_req *req = tevent_req_callback_data(
1175 subreq, struct tevent_req);
1178 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1179 TALLOC_FREE(subreq);
1180 if (!NT_STATUS_IS_OK(status)) {
1181 tevent_req_nterror(req, status);
1184 tevent_req_done(req);
1187 NTSTATUS cli_rename_recv(struct tevent_req *req)
1189 return tevent_req_simple_recv_ntstatus(req);
1192 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1194 TALLOC_CTX *frame = talloc_stackframe();
1195 struct event_context *ev;
1196 struct tevent_req *req;
1197 NTSTATUS status = NT_STATUS_OK;
1199 if (cli_has_async_calls(cli)) {
1201 * Can't use sync call while an async call is in flight
1203 status = NT_STATUS_INVALID_PARAMETER;
1207 ev = event_context_init(frame);
1209 status = NT_STATUS_NO_MEMORY;
1213 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1215 status = NT_STATUS_NO_MEMORY;
1219 if (!tevent_req_poll(req, ev)) {
1220 status = map_nt_error_from_unix(errno);
1224 status = cli_rename_recv(req);
1228 if (!NT_STATUS_IS_OK(status)) {
1229 cli_set_error(cli, status);
1234 /****************************************************************************
1236 ****************************************************************************/
1238 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1240 struct cli_ntrename_internal_state {
1244 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1245 struct event_context *ev,
1246 struct cli_state *cli,
1247 const char *fname_src,
1248 const char *fname_dst,
1249 uint16_t rename_flag)
1251 struct tevent_req *req = NULL, *subreq = NULL;
1252 struct cli_ntrename_internal_state *state = NULL;
1253 uint8_t additional_flags = 0;
1254 uint8_t *bytes = NULL;
1256 req = tevent_req_create(mem_ctx, &state,
1257 struct cli_ntrename_internal_state);
1262 SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
1263 SSVAL(state->vwv+1, 0, rename_flag);
1265 bytes = talloc_array(state, uint8_t, 1);
1266 if (tevent_req_nomem(bytes, req)) {
1267 return tevent_req_post(req, ev);
1270 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1271 strlen(fname_src)+1, NULL);
1272 if (tevent_req_nomem(bytes, req)) {
1273 return tevent_req_post(req, ev);
1276 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1277 talloc_get_size(bytes)+1);
1278 if (tevent_req_nomem(bytes, req)) {
1279 return tevent_req_post(req, ev);
1282 bytes[talloc_get_size(bytes)-1] = 4;
1283 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1284 strlen(fname_dst)+1, NULL);
1285 if (tevent_req_nomem(bytes, req)) {
1286 return tevent_req_post(req, ev);
1289 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1290 4, state->vwv, talloc_get_size(bytes), bytes);
1291 if (tevent_req_nomem(subreq, req)) {
1292 return tevent_req_post(req, ev);
1294 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1298 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1300 struct tevent_req *req = tevent_req_callback_data(
1301 subreq, struct tevent_req);
1304 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1305 TALLOC_FREE(subreq);
1306 if (!NT_STATUS_IS_OK(status)) {
1307 tevent_req_nterror(req, status);
1310 tevent_req_done(req);
1313 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1315 return tevent_req_simple_recv_ntstatus(req);
1318 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1319 struct event_context *ev,
1320 struct cli_state *cli,
1321 const char *fname_src,
1322 const char *fname_dst)
1324 return cli_ntrename_internal_send(mem_ctx,
1329 RENAME_FLAG_RENAME);
1332 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1334 return cli_ntrename_internal_recv(req);
1337 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1339 TALLOC_CTX *frame = talloc_stackframe();
1340 struct event_context *ev;
1341 struct tevent_req *req;
1342 NTSTATUS status = NT_STATUS_OK;
1344 if (cli_has_async_calls(cli)) {
1346 * Can't use sync call while an async call is in flight
1348 status = NT_STATUS_INVALID_PARAMETER;
1352 ev = event_context_init(frame);
1354 status = NT_STATUS_NO_MEMORY;
1358 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1360 status = NT_STATUS_NO_MEMORY;
1364 if (!tevent_req_poll(req, ev)) {
1365 status = map_nt_error_from_unix(errno);
1369 status = cli_ntrename_recv(req);
1373 if (!NT_STATUS_IS_OK(status)) {
1374 cli_set_error(cli, status);
1379 /****************************************************************************
1381 ****************************************************************************/
1383 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1384 struct event_context *ev,
1385 struct cli_state *cli,
1386 const char *fname_src,
1387 const char *fname_dst)
1389 return cli_ntrename_internal_send(mem_ctx,
1394 RENAME_FLAG_HARD_LINK);
1397 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1399 return cli_ntrename_internal_recv(req);
1402 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1404 TALLOC_CTX *frame = talloc_stackframe();
1405 struct event_context *ev;
1406 struct tevent_req *req;
1407 NTSTATUS status = NT_STATUS_OK;
1409 if (cli_has_async_calls(cli)) {
1411 * Can't use sync call while an async call is in flight
1413 status = NT_STATUS_INVALID_PARAMETER;
1417 ev = event_context_init(frame);
1419 status = NT_STATUS_NO_MEMORY;
1423 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1425 status = NT_STATUS_NO_MEMORY;
1429 if (!tevent_req_poll(req, ev)) {
1430 status = map_nt_error_from_unix(errno);
1434 status = cli_nt_hardlink_recv(req);
1438 if (!NT_STATUS_IS_OK(status)) {
1439 cli_set_error(cli, status);
1444 /****************************************************************************
1446 ****************************************************************************/
1448 static void cli_unlink_done(struct tevent_req *subreq);
1450 struct cli_unlink_state {
1454 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1455 struct event_context *ev,
1456 struct cli_state *cli,
1458 uint16_t mayhave_attrs)
1460 struct tevent_req *req = NULL, *subreq = NULL;
1461 struct cli_unlink_state *state = NULL;
1462 uint8_t additional_flags = 0;
1463 uint8_t *bytes = NULL;
1465 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1470 SSVAL(state->vwv+0, 0, mayhave_attrs);
1472 bytes = talloc_array(state, uint8_t, 1);
1473 if (tevent_req_nomem(bytes, req)) {
1474 return tevent_req_post(req, ev);
1477 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1478 strlen(fname)+1, NULL);
1480 if (tevent_req_nomem(bytes, req)) {
1481 return tevent_req_post(req, ev);
1484 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1485 1, state->vwv, talloc_get_size(bytes), bytes);
1486 if (tevent_req_nomem(subreq, req)) {
1487 return tevent_req_post(req, ev);
1489 tevent_req_set_callback(subreq, cli_unlink_done, req);
1493 static void cli_unlink_done(struct tevent_req *subreq)
1495 struct tevent_req *req = tevent_req_callback_data(
1496 subreq, struct tevent_req);
1499 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1500 TALLOC_FREE(subreq);
1501 if (!NT_STATUS_IS_OK(status)) {
1502 tevent_req_nterror(req, status);
1505 tevent_req_done(req);
1508 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1510 return tevent_req_simple_recv_ntstatus(req);
1513 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1515 TALLOC_CTX *frame = talloc_stackframe();
1516 struct event_context *ev;
1517 struct tevent_req *req;
1518 NTSTATUS status = NT_STATUS_OK;
1520 if (cli_has_async_calls(cli)) {
1522 * Can't use sync call while an async call is in flight
1524 status = NT_STATUS_INVALID_PARAMETER;
1528 ev = event_context_init(frame);
1530 status = NT_STATUS_NO_MEMORY;
1534 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1536 status = NT_STATUS_NO_MEMORY;
1540 if (!tevent_req_poll(req, ev)) {
1541 status = map_nt_error_from_unix(errno);
1545 status = cli_unlink_recv(req);
1549 if (!NT_STATUS_IS_OK(status)) {
1550 cli_set_error(cli, status);
1555 /****************************************************************************
1557 ****************************************************************************/
1559 static void cli_mkdir_done(struct tevent_req *subreq);
1561 struct cli_mkdir_state {
1565 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1566 struct event_context *ev,
1567 struct cli_state *cli,
1570 struct tevent_req *req = NULL, *subreq = NULL;
1571 struct cli_mkdir_state *state = NULL;
1572 uint8_t additional_flags = 0;
1573 uint8_t *bytes = NULL;
1575 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1580 bytes = talloc_array(state, uint8_t, 1);
1581 if (tevent_req_nomem(bytes, req)) {
1582 return tevent_req_post(req, ev);
1585 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1586 strlen(dname)+1, NULL);
1588 if (tevent_req_nomem(bytes, req)) {
1589 return tevent_req_post(req, ev);
1592 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1593 0, NULL, talloc_get_size(bytes), bytes);
1594 if (tevent_req_nomem(subreq, req)) {
1595 return tevent_req_post(req, ev);
1597 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1601 static void cli_mkdir_done(struct tevent_req *subreq)
1603 struct tevent_req *req = tevent_req_callback_data(
1604 subreq, struct tevent_req);
1607 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1608 TALLOC_FREE(subreq);
1609 if (!NT_STATUS_IS_OK(status)) {
1610 tevent_req_nterror(req, status);
1613 tevent_req_done(req);
1616 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1618 return tevent_req_simple_recv_ntstatus(req);
1621 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1623 TALLOC_CTX *frame = talloc_stackframe();
1624 struct event_context *ev;
1625 struct tevent_req *req;
1626 NTSTATUS status = NT_STATUS_OK;
1628 if (cli_has_async_calls(cli)) {
1630 * Can't use sync call while an async call is in flight
1632 status = NT_STATUS_INVALID_PARAMETER;
1636 ev = event_context_init(frame);
1638 status = NT_STATUS_NO_MEMORY;
1642 req = cli_mkdir_send(frame, ev, cli, dname);
1644 status = NT_STATUS_NO_MEMORY;
1648 if (!tevent_req_poll(req, ev)) {
1649 status = map_nt_error_from_unix(errno);
1653 status = cli_mkdir_recv(req);
1657 if (!NT_STATUS_IS_OK(status)) {
1658 cli_set_error(cli, status);
1663 /****************************************************************************
1665 ****************************************************************************/
1667 static void cli_rmdir_done(struct tevent_req *subreq);
1669 struct cli_rmdir_state {
1673 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1674 struct event_context *ev,
1675 struct cli_state *cli,
1678 struct tevent_req *req = NULL, *subreq = NULL;
1679 struct cli_rmdir_state *state = NULL;
1680 uint8_t additional_flags = 0;
1681 uint8_t *bytes = NULL;
1683 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1688 bytes = talloc_array(state, uint8_t, 1);
1689 if (tevent_req_nomem(bytes, req)) {
1690 return tevent_req_post(req, ev);
1693 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1694 strlen(dname)+1, NULL);
1696 if (tevent_req_nomem(bytes, req)) {
1697 return tevent_req_post(req, ev);
1700 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1701 0, NULL, talloc_get_size(bytes), bytes);
1702 if (tevent_req_nomem(subreq, req)) {
1703 return tevent_req_post(req, ev);
1705 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1709 static void cli_rmdir_done(struct tevent_req *subreq)
1711 struct tevent_req *req = tevent_req_callback_data(
1712 subreq, struct tevent_req);
1715 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1716 TALLOC_FREE(subreq);
1717 if (!NT_STATUS_IS_OK(status)) {
1718 tevent_req_nterror(req, status);
1721 tevent_req_done(req);
1724 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1726 return tevent_req_simple_recv_ntstatus(req);
1729 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1731 TALLOC_CTX *frame = talloc_stackframe();
1732 struct event_context *ev;
1733 struct tevent_req *req;
1734 NTSTATUS status = NT_STATUS_OK;
1736 if (cli_has_async_calls(cli)) {
1738 * Can't use sync call while an async call is in flight
1740 status = NT_STATUS_INVALID_PARAMETER;
1744 ev = event_context_init(frame);
1746 status = NT_STATUS_NO_MEMORY;
1750 req = cli_rmdir_send(frame, ev, cli, dname);
1752 status = NT_STATUS_NO_MEMORY;
1756 if (!tevent_req_poll(req, ev)) {
1757 status = map_nt_error_from_unix(errno);
1761 status = cli_rmdir_recv(req);
1765 if (!NT_STATUS_IS_OK(status)) {
1766 cli_set_error(cli, status);
1771 /****************************************************************************
1772 Set or clear the delete on close flag.
1773 ****************************************************************************/
1781 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1783 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1784 NULL, 0, NULL, NULL, 0, NULL);
1785 tevent_req_simple_finish_ntstatus(subreq, status);
1788 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1789 struct event_context *ev,
1790 struct cli_state *cli,
1794 struct tevent_req *req = NULL, *subreq = NULL;
1795 struct doc_state *state = NULL;
1797 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1802 /* Setup setup word. */
1803 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1805 /* Setup param array. */
1806 SSVAL(state->param,0,fnum);
1807 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1809 /* Setup data array. */
1810 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1812 subreq = cli_trans_send(state, /* mem ctx. */
1813 ev, /* event ctx. */
1814 cli, /* cli_state. */
1815 SMBtrans2, /* cmd. */
1816 NULL, /* pipe name. */
1820 &state->setup, /* setup. */
1821 1, /* num setup uint16_t words. */
1822 0, /* max returned setup. */
1823 state->param, /* param. */
1825 2, /* max returned param. */
1826 state->data, /* data. */
1828 0); /* max returned data. */
1830 if (tevent_req_nomem(subreq, req)) {
1831 return tevent_req_post(req, ev);
1833 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1837 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1839 return tevent_req_simple_recv_ntstatus(req);
1842 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1844 TALLOC_CTX *frame = talloc_stackframe();
1845 struct event_context *ev = NULL;
1846 struct tevent_req *req = NULL;
1847 NTSTATUS status = NT_STATUS_OK;
1849 if (cli_has_async_calls(cli)) {
1851 * Can't use sync call while an async call is in flight
1853 status = NT_STATUS_INVALID_PARAMETER;
1857 ev = event_context_init(frame);
1859 status = NT_STATUS_NO_MEMORY;
1863 req = cli_nt_delete_on_close_send(frame,
1869 status = NT_STATUS_NO_MEMORY;
1873 if (!tevent_req_poll(req, ev)) {
1874 status = map_nt_error_from_unix(errno);
1878 status = cli_nt_delete_on_close_recv(req);
1882 if (!NT_STATUS_IS_OK(status)) {
1883 cli_set_error(cli, status);
1888 struct cli_ntcreate_state {
1893 static void cli_ntcreate_done(struct tevent_req *subreq);
1895 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1896 struct event_context *ev,
1897 struct cli_state *cli,
1899 uint32_t CreatFlags,
1900 uint32_t DesiredAccess,
1901 uint32_t FileAttributes,
1902 uint32_t ShareAccess,
1903 uint32_t CreateDisposition,
1904 uint32_t CreateOptions,
1905 uint8_t SecurityFlags)
1907 struct tevent_req *req, *subreq;
1908 struct cli_ntcreate_state *state;
1911 size_t converted_len;
1913 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1920 SCVAL(vwv+0, 0, 0xFF);
1925 if (cli->use_oplocks) {
1926 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1928 SIVAL(vwv+3, 1, CreatFlags);
1929 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1930 SIVAL(vwv+7, 1, DesiredAccess);
1931 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1932 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1933 SIVAL(vwv+13, 1, FileAttributes);
1934 SIVAL(vwv+15, 1, ShareAccess);
1935 SIVAL(vwv+17, 1, CreateDisposition);
1936 SIVAL(vwv+19, 1, CreateOptions);
1937 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1938 SCVAL(vwv+23, 1, SecurityFlags);
1940 bytes = talloc_array(state, uint8_t, 0);
1941 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1942 fname, strlen(fname)+1,
1945 /* sigh. this copes with broken netapp filer behaviour */
1946 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1948 if (tevent_req_nomem(bytes, req)) {
1949 return tevent_req_post(req, ev);
1952 SSVAL(vwv+2, 1, converted_len);
1954 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1955 talloc_get_size(bytes), bytes);
1956 if (tevent_req_nomem(subreq, req)) {
1957 return tevent_req_post(req, ev);
1959 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1963 static void cli_ntcreate_done(struct tevent_req *subreq)
1965 struct tevent_req *req = tevent_req_callback_data(
1966 subreq, struct tevent_req);
1967 struct cli_ntcreate_state *state = tevent_req_data(
1968 req, struct cli_ntcreate_state);
1976 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1977 &num_bytes, &bytes);
1978 TALLOC_FREE(subreq);
1979 if (!NT_STATUS_IS_OK(status)) {
1980 tevent_req_nterror(req, status);
1983 state->fnum = SVAL(vwv+2, 1);
1984 tevent_req_done(req);
1987 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1989 struct cli_ntcreate_state *state = tevent_req_data(
1990 req, struct cli_ntcreate_state);
1993 if (tevent_req_is_nterror(req, &status)) {
1996 *pfnum = state->fnum;
1997 return NT_STATUS_OK;
2000 NTSTATUS cli_ntcreate(struct cli_state *cli,
2002 uint32_t CreatFlags,
2003 uint32_t DesiredAccess,
2004 uint32_t FileAttributes,
2005 uint32_t ShareAccess,
2006 uint32_t CreateDisposition,
2007 uint32_t CreateOptions,
2008 uint8_t SecurityFlags,
2011 TALLOC_CTX *frame = talloc_stackframe();
2012 struct event_context *ev;
2013 struct tevent_req *req;
2014 NTSTATUS status = NT_STATUS_OK;
2016 if (cli_has_async_calls(cli)) {
2018 * Can't use sync call while an async call is in flight
2020 status = NT_STATUS_INVALID_PARAMETER;
2024 ev = event_context_init(frame);
2026 status = NT_STATUS_NO_MEMORY;
2030 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2031 DesiredAccess, FileAttributes, ShareAccess,
2032 CreateDisposition, CreateOptions,
2035 status = NT_STATUS_NO_MEMORY;
2039 if (!tevent_req_poll(req, ev)) {
2040 status = map_nt_error_from_unix(errno);
2044 status = cli_ntcreate_recv(req, pfid);
2047 if (!NT_STATUS_IS_OK(status)) {
2048 cli_set_error(cli, status);
2053 /****************************************************************************
2055 WARNING: if you open with O_WRONLY then getattrE won't work!
2056 ****************************************************************************/
2058 struct cli_open_state {
2064 static void cli_open_done(struct tevent_req *subreq);
2066 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2067 struct event_context *ev,
2068 struct cli_state *cli, const char *fname,
2069 int flags, int share_mode,
2070 struct tevent_req **psmbreq)
2072 struct tevent_req *req, *subreq;
2073 struct cli_open_state *state;
2075 unsigned accessmode;
2076 uint8_t additional_flags;
2079 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2085 if (flags & O_CREAT) {
2088 if (!(flags & O_EXCL)) {
2089 if (flags & O_TRUNC)
2095 accessmode = (share_mode<<4);
2097 if ((flags & O_ACCMODE) == O_RDWR) {
2099 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2104 if ((flags & O_SYNC) == O_SYNC) {
2105 accessmode |= (1<<14);
2109 if (share_mode == DENY_FCB) {
2113 SCVAL(state->vwv + 0, 0, 0xFF);
2114 SCVAL(state->vwv + 0, 1, 0);
2115 SSVAL(state->vwv + 1, 0, 0);
2116 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2117 SSVAL(state->vwv + 3, 0, accessmode);
2118 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
2119 SSVAL(state->vwv + 5, 0, 0);
2120 SIVAL(state->vwv + 6, 0, 0);
2121 SSVAL(state->vwv + 8, 0, openfn);
2122 SIVAL(state->vwv + 9, 0, 0);
2123 SIVAL(state->vwv + 11, 0, 0);
2124 SIVAL(state->vwv + 13, 0, 0);
2126 additional_flags = 0;
2128 if (cli->use_oplocks) {
2129 /* if using oplocks then ask for a batch oplock via
2130 core and extended methods */
2132 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2133 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2136 bytes = talloc_array(state, uint8_t, 0);
2137 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2138 strlen(fname)+1, NULL);
2140 if (tevent_req_nomem(bytes, req)) {
2141 return tevent_req_post(req, ev);
2144 state->bytes.iov_base = (void *)bytes;
2145 state->bytes.iov_len = talloc_get_size(bytes);
2147 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2148 15, state->vwv, 1, &state->bytes);
2149 if (subreq == NULL) {
2153 tevent_req_set_callback(subreq, cli_open_done, req);
2158 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2159 struct cli_state *cli, const char *fname,
2160 int flags, int share_mode)
2162 struct tevent_req *req, *subreq;
2165 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2171 status = cli_smb_req_send(subreq);
2172 if (!NT_STATUS_IS_OK(status)) {
2173 tevent_req_nterror(req, status);
2174 return tevent_req_post(req, ev);
2179 static void cli_open_done(struct tevent_req *subreq)
2181 struct tevent_req *req = tevent_req_callback_data(
2182 subreq, struct tevent_req);
2183 struct cli_open_state *state = tevent_req_data(
2184 req, struct cli_open_state);
2190 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2192 TALLOC_FREE(subreq);
2193 if (!NT_STATUS_IS_OK(status)) {
2194 tevent_req_nterror(req, status);
2197 state->fnum = SVAL(vwv+2, 0);
2198 tevent_req_done(req);
2201 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2203 struct cli_open_state *state = tevent_req_data(
2204 req, struct cli_open_state);
2207 if (tevent_req_is_nterror(req, &status)) {
2210 *pfnum = state->fnum;
2211 return NT_STATUS_OK;
2214 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2215 int share_mode, uint16_t *pfnum)
2217 TALLOC_CTX *frame = talloc_stackframe();
2218 struct event_context *ev;
2219 struct tevent_req *req;
2220 NTSTATUS status = NT_STATUS_OK;
2222 if (cli_has_async_calls(cli)) {
2224 * Can't use sync call while an async call is in flight
2226 status = NT_STATUS_INVALID_PARAMETER;
2230 ev = event_context_init(frame);
2232 status = NT_STATUS_NO_MEMORY;
2236 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2238 status = NT_STATUS_NO_MEMORY;
2242 if (!tevent_req_poll(req, ev)) {
2243 status = map_nt_error_from_unix(errno);
2247 status = cli_open_recv(req, pfnum);
2250 if (!NT_STATUS_IS_OK(status)) {
2251 cli_set_error(cli, status);
2256 /****************************************************************************
2258 ****************************************************************************/
2260 struct cli_close_state {
2264 static void cli_close_done(struct tevent_req *subreq);
2266 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2267 struct event_context *ev,
2268 struct cli_state *cli,
2270 struct tevent_req **psubreq)
2272 struct tevent_req *req, *subreq;
2273 struct cli_close_state *state;
2275 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2280 SSVAL(state->vwv+0, 0, fnum);
2281 SIVALS(state->vwv+1, 0, -1);
2283 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2285 if (subreq == NULL) {
2289 tevent_req_set_callback(subreq, cli_close_done, req);
2294 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2295 struct event_context *ev,
2296 struct cli_state *cli,
2299 struct tevent_req *req, *subreq;
2302 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2307 status = cli_smb_req_send(subreq);
2308 if (!NT_STATUS_IS_OK(status)) {
2309 tevent_req_nterror(req, status);
2310 return tevent_req_post(req, ev);
2315 static void cli_close_done(struct tevent_req *subreq)
2317 struct tevent_req *req = tevent_req_callback_data(
2318 subreq, struct tevent_req);
2321 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2322 TALLOC_FREE(subreq);
2323 if (!NT_STATUS_IS_OK(status)) {
2324 tevent_req_nterror(req, status);
2327 tevent_req_done(req);
2330 NTSTATUS cli_close_recv(struct tevent_req *req)
2332 return tevent_req_simple_recv_ntstatus(req);
2335 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2337 TALLOC_CTX *frame = talloc_stackframe();
2338 struct event_context *ev;
2339 struct tevent_req *req;
2340 NTSTATUS status = NT_STATUS_OK;
2342 if (cli_has_async_calls(cli)) {
2344 * Can't use sync call while an async call is in flight
2346 status = NT_STATUS_INVALID_PARAMETER;
2350 ev = event_context_init(frame);
2352 status = NT_STATUS_NO_MEMORY;
2356 req = cli_close_send(frame, ev, cli, fnum);
2358 status = NT_STATUS_NO_MEMORY;
2362 if (!tevent_req_poll(req, ev)) {
2363 status = map_nt_error_from_unix(errno);
2367 status = cli_close_recv(req);
2370 if (!NT_STATUS_IS_OK(status)) {
2371 cli_set_error(cli, status);
2376 /****************************************************************************
2377 Truncate a file to a specified size
2378 ****************************************************************************/
2380 struct ftrunc_state {
2386 static void cli_ftruncate_done(struct tevent_req *subreq)
2388 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2389 NULL, 0, NULL, NULL, 0, NULL);
2390 tevent_req_simple_finish_ntstatus(subreq, status);
2393 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2394 struct event_context *ev,
2395 struct cli_state *cli,
2399 struct tevent_req *req = NULL, *subreq = NULL;
2400 struct ftrunc_state *state = NULL;
2402 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2407 /* Setup setup word. */
2408 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2410 /* Setup param array. */
2411 SSVAL(state->param,0,fnum);
2412 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2413 SSVAL(state->param,4,0);
2415 /* Setup data array. */
2416 SBVAL(state->data, 0, size);
2418 subreq = cli_trans_send(state, /* mem ctx. */
2419 ev, /* event ctx. */
2420 cli, /* cli_state. */
2421 SMBtrans2, /* cmd. */
2422 NULL, /* pipe name. */
2426 &state->setup, /* setup. */
2427 1, /* num setup uint16_t words. */
2428 0, /* max returned setup. */
2429 state->param, /* param. */
2431 2, /* max returned param. */
2432 state->data, /* data. */
2434 0); /* max returned data. */
2436 if (tevent_req_nomem(subreq, req)) {
2437 return tevent_req_post(req, ev);
2439 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2443 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2445 return tevent_req_simple_recv_ntstatus(req);
2448 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2450 TALLOC_CTX *frame = talloc_stackframe();
2451 struct event_context *ev = NULL;
2452 struct tevent_req *req = NULL;
2453 NTSTATUS status = NT_STATUS_OK;
2455 if (cli_has_async_calls(cli)) {
2457 * Can't use sync call while an async call is in flight
2459 status = NT_STATUS_INVALID_PARAMETER;
2463 ev = event_context_init(frame);
2465 status = NT_STATUS_NO_MEMORY;
2469 req = cli_ftruncate_send(frame,
2475 status = NT_STATUS_NO_MEMORY;
2479 if (!tevent_req_poll(req, ev)) {
2480 status = map_nt_error_from_unix(errno);
2484 status = cli_ftruncate_recv(req);
2488 if (!NT_STATUS_IS_OK(status)) {
2489 cli_set_error(cli, status);
2494 /****************************************************************************
2495 send a lock with a specified locktype
2496 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2497 ****************************************************************************/
2499 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2500 uint32_t offset, uint32_t len,
2501 int timeout, unsigned char locktype)
2508 SCVAL(vwv + 0, 0, 0xff);
2509 SCVAL(vwv + 0, 1, 0);
2510 SSVAL(vwv + 1, 0, 0);
2511 SSVAL(vwv + 2, 0, fnum);
2512 SCVAL(vwv + 3, 0, locktype);
2513 SCVAL(vwv + 3, 1, 0);
2514 SIVALS(vwv + 4, 0, timeout);
2515 SSVAL(vwv + 6, 0, 0);
2516 SSVAL(vwv + 7, 0, 1);
2518 SSVAL(bytes, 0, cli->pid);
2519 SIVAL(bytes, 2, offset);
2520 SIVAL(bytes, 6, len);
2522 saved_timeout = cli->timeout;
2525 cli->timeout = (timeout == -1)
2526 ? 0x7FFFFFFF : (timeout + 2*1000);
2529 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2530 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2532 cli->timeout = saved_timeout;
2537 /****************************************************************************
2539 note that timeout is in units of 2 milliseconds
2540 ****************************************************************************/
2542 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2543 uint32_t offset, uint32_t len, int timeout,
2544 enum brl_type lock_type)
2548 status = cli_locktype(cli, fnum, offset, len, timeout,
2549 (lock_type == READ_LOCK? 1 : 0));
2550 cli_set_error(cli, status);
2551 return NT_STATUS_IS_OK(status);
2554 /****************************************************************************
2556 ****************************************************************************/
2558 struct cli_unlock_state {
2563 static void cli_unlock_done(struct tevent_req *subreq);
2565 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2566 struct event_context *ev,
2567 struct cli_state *cli,
2573 struct tevent_req *req = NULL, *subreq = NULL;
2574 struct cli_unlock_state *state = NULL;
2575 uint8_t additional_flags = 0;
2577 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2582 SCVAL(state->vwv+0, 0, 0xFF);
2583 SSVAL(state->vwv+2, 0, fnum);
2584 SCVAL(state->vwv+3, 0, 0);
2585 SIVALS(state->vwv+4, 0, 0);
2586 SSVAL(state->vwv+6, 0, 1);
2587 SSVAL(state->vwv+7, 0, 0);
2589 SSVAL(state->data, 0, cli->pid);
2590 SIVAL(state->data, 2, offset);
2591 SIVAL(state->data, 6, len);
2593 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2594 8, state->vwv, 10, state->data);
2595 if (tevent_req_nomem(subreq, req)) {
2596 return tevent_req_post(req, ev);
2598 tevent_req_set_callback(subreq, cli_unlock_done, req);
2602 static void cli_unlock_done(struct tevent_req *subreq)
2604 struct tevent_req *req = tevent_req_callback_data(
2605 subreq, struct tevent_req);
2608 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2609 TALLOC_FREE(subreq);
2610 if (!NT_STATUS_IS_OK(status)) {
2611 tevent_req_nterror(req, status);
2614 tevent_req_done(req);
2617 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2619 return tevent_req_simple_recv_ntstatus(req);
2622 NTSTATUS cli_unlock(struct cli_state *cli,
2627 TALLOC_CTX *frame = talloc_stackframe();
2628 struct event_context *ev;
2629 struct tevent_req *req;
2630 NTSTATUS status = NT_STATUS_OK;
2632 if (cli_has_async_calls(cli)) {
2634 * Can't use sync call while an async call is in flight
2636 status = NT_STATUS_INVALID_PARAMETER;
2640 ev = event_context_init(frame);
2642 status = NT_STATUS_NO_MEMORY;
2646 req = cli_unlock_send(frame, ev, cli,
2649 status = NT_STATUS_NO_MEMORY;
2653 if (!tevent_req_poll(req, ev)) {
2654 status = map_nt_error_from_unix(errno);
2658 status = cli_unlock_recv(req);
2662 if (!NT_STATUS_IS_OK(status)) {
2663 cli_set_error(cli, status);
2668 /****************************************************************************
2669 Lock a file with 64 bit offsets.
2670 ****************************************************************************/
2672 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2673 uint64_t offset, uint64_t len, int timeout,
2674 enum brl_type lock_type)
2678 int saved_timeout = cli->timeout;
2682 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2683 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2686 ltype = (lock_type == READ_LOCK? 1 : 0);
2687 ltype |= LOCKING_ANDX_LARGE_FILES;
2689 SCVAL(vwv + 0, 0, 0xff);
2690 SCVAL(vwv + 0, 1, 0);
2691 SSVAL(vwv + 1, 0, 0);
2692 SSVAL(vwv + 2, 0, fnum);
2693 SCVAL(vwv + 3, 0, ltype);
2694 SCVAL(vwv + 3, 1, 0);
2695 SIVALS(vwv + 4, 0, timeout);
2696 SSVAL(vwv + 6, 0, 0);
2697 SSVAL(vwv + 7, 0, 1);
2699 SIVAL(bytes, 0, cli->pid);
2700 SOFF_T_R(bytes, 4, offset);
2701 SOFF_T_R(bytes, 12, len);
2703 saved_timeout = cli->timeout;
2706 cli->timeout = (timeout == -1)
2707 ? 0x7FFFFFFF : (timeout + 2*1000);
2710 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2711 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2713 cli->timeout = saved_timeout;
2715 cli_set_error(cli, status);
2716 return NT_STATUS_IS_OK(status);
2719 /****************************************************************************
2720 Unlock a file with 64 bit offsets.
2721 ****************************************************************************/
2723 struct cli_unlock64_state {
2728 static void cli_unlock64_done(struct tevent_req *subreq);
2730 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2731 struct event_context *ev,
2732 struct cli_state *cli,
2738 struct tevent_req *req = NULL, *subreq = NULL;
2739 struct cli_unlock64_state *state = NULL;
2740 uint8_t additional_flags = 0;
2742 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2747 SCVAL(state->vwv+0, 0, 0xff);
2748 SSVAL(state->vwv+2, 0, fnum);
2749 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2750 SIVALS(state->vwv+4, 0, 0);
2751 SSVAL(state->vwv+6, 0, 1);
2752 SSVAL(state->vwv+7, 0, 0);
2754 SIVAL(state->data, 0, cli->pid);
2755 SOFF_T_R(state->data, 4, offset);
2756 SOFF_T_R(state->data, 12, len);
2758 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2759 8, state->vwv, 20, state->data);
2760 if (tevent_req_nomem(subreq, req)) {
2761 return tevent_req_post(req, ev);
2763 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2767 static void cli_unlock64_done(struct tevent_req *subreq)
2769 struct tevent_req *req = tevent_req_callback_data(
2770 subreq, struct tevent_req);
2773 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2774 TALLOC_FREE(subreq);
2775 if (!NT_STATUS_IS_OK(status)) {
2776 tevent_req_nterror(req, status);
2779 tevent_req_done(req);
2782 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2784 return tevent_req_simple_recv_ntstatus(req);
2787 NTSTATUS cli_unlock64(struct cli_state *cli,
2792 TALLOC_CTX *frame = talloc_stackframe();
2793 struct event_context *ev;
2794 struct tevent_req *req;
2795 NTSTATUS status = NT_STATUS_OK;
2797 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2798 return cli_unlock(cli, fnum, offset, len);
2801 if (cli_has_async_calls(cli)) {
2803 * Can't use sync call while an async call is in flight
2805 status = NT_STATUS_INVALID_PARAMETER;
2809 ev = event_context_init(frame);
2811 status = NT_STATUS_NO_MEMORY;
2815 req = cli_unlock64_send(frame, ev, cli,
2818 status = NT_STATUS_NO_MEMORY;
2822 if (!tevent_req_poll(req, ev)) {
2823 status = map_nt_error_from_unix(errno);
2827 status = cli_unlock64_recv(req);
2831 if (!NT_STATUS_IS_OK(status)) {
2832 cli_set_error(cli, status);
2837 /****************************************************************************
2838 Get/unlock a POSIX lock on a file - internal function.
2839 ****************************************************************************/
2841 struct posix_lock_state {
2844 uint8_t data[POSIX_LOCK_DATA_SIZE];
2847 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2849 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2850 NULL, 0, NULL, NULL, 0, NULL);
2851 tevent_req_simple_finish_ntstatus(subreq, status);
2854 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2855 struct event_context *ev,
2856 struct cli_state *cli,
2861 enum brl_type lock_type)
2863 struct tevent_req *req = NULL, *subreq = NULL;
2864 struct posix_lock_state *state = NULL;
2866 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2871 /* Setup setup word. */
2872 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2874 /* Setup param array. */
2875 SSVAL(&state->param, 0, fnum);
2876 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2878 /* Setup data array. */
2879 switch (lock_type) {
2881 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2882 POSIX_LOCK_TYPE_READ);
2885 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2886 POSIX_LOCK_TYPE_WRITE);
2889 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2890 POSIX_LOCK_TYPE_UNLOCK);
2897 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2898 POSIX_LOCK_FLAG_WAIT);
2900 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2901 POSIX_LOCK_FLAG_NOWAIT);
2904 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
2905 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2906 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2908 subreq = cli_trans_send(state, /* mem ctx. */
2909 ev, /* event ctx. */
2910 cli, /* cli_state. */
2911 SMBtrans2, /* cmd. */
2912 NULL, /* pipe name. */
2916 &state->setup, /* setup. */
2917 1, /* num setup uint16_t words. */
2918 0, /* max returned setup. */
2919 state->param, /* param. */
2921 2, /* max returned param. */
2922 state->data, /* data. */
2923 POSIX_LOCK_DATA_SIZE, /* num data. */
2924 0); /* max returned data. */
2926 if (tevent_req_nomem(subreq, req)) {
2927 return tevent_req_post(req, ev);
2929 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
2933 /****************************************************************************
2935 ****************************************************************************/
2937 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
2938 struct event_context *ev,
2939 struct cli_state *cli,
2944 enum brl_type lock_type)
2946 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2947 wait_lock, lock_type);
2950 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
2952 return tevent_req_simple_recv_ntstatus(req);
2955 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2956 uint64_t offset, uint64_t len,
2957 bool wait_lock, enum brl_type lock_type)
2959 TALLOC_CTX *frame = talloc_stackframe();
2960 struct event_context *ev = NULL;
2961 struct tevent_req *req = NULL;
2962 NTSTATUS status = NT_STATUS_OK;
2964 if (cli_has_async_calls(cli)) {
2966 * Can't use sync call while an async call is in flight
2968 status = NT_STATUS_INVALID_PARAMETER;
2972 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2973 status = NT_STATUS_INVALID_PARAMETER;
2977 ev = event_context_init(frame);
2979 status = NT_STATUS_NO_MEMORY;
2983 req = cli_posix_lock_send(frame,
2992 status = NT_STATUS_NO_MEMORY;
2996 if (!tevent_req_poll(req, ev)) {
2997 status = map_nt_error_from_unix(errno);
3001 status = cli_posix_lock_recv(req);
3005 if (!NT_STATUS_IS_OK(status)) {
3006 cli_set_error(cli, status);
3011 /****************************************************************************
3012 POSIX Unlock a file.
3013 ****************************************************************************/
3015 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3016 struct event_context *ev,
3017 struct cli_state *cli,
3022 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3023 false, UNLOCK_LOCK);
3026 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3028 return tevent_req_simple_recv_ntstatus(req);
3031 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3033 TALLOC_CTX *frame = talloc_stackframe();
3034 struct event_context *ev = NULL;
3035 struct tevent_req *req = NULL;
3036 NTSTATUS status = NT_STATUS_OK;
3038 if (cli_has_async_calls(cli)) {
3040 * Can't use sync call while an async call is in flight
3042 status = NT_STATUS_INVALID_PARAMETER;
3046 ev = event_context_init(frame);
3048 status = NT_STATUS_NO_MEMORY;
3052 req = cli_posix_unlock_send(frame,
3059 status = NT_STATUS_NO_MEMORY;
3063 if (!tevent_req_poll(req, ev)) {
3064 status = map_nt_error_from_unix(errno);
3068 status = cli_posix_unlock_recv(req);
3072 if (!NT_STATUS_IS_OK(status)) {
3073 cli_set_error(cli, status);
3078 /****************************************************************************
3079 Do a SMBgetattrE call.
3080 ****************************************************************************/
3082 static void cli_getattrE_done(struct tevent_req *subreq);
3084 struct cli_getattrE_state {
3094 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3095 struct event_context *ev,
3096 struct cli_state *cli,
3099 struct tevent_req *req = NULL, *subreq = NULL;
3100 struct cli_getattrE_state *state = NULL;
3101 uint8_t additional_flags = 0;
3103 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3108 state->zone_offset = cli->serverzone;
3109 SSVAL(state->vwv+0,0,fnum);
3111 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3112 1, state->vwv, 0, NULL);
3113 if (tevent_req_nomem(subreq, req)) {
3114 return tevent_req_post(req, ev);
3116 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3120 static void cli_getattrE_done(struct tevent_req *subreq)
3122 struct tevent_req *req = tevent_req_callback_data(
3123 subreq, struct tevent_req);
3124 struct cli_getattrE_state *state = tevent_req_data(
3125 req, struct cli_getattrE_state);
3127 uint16_t *vwv = NULL;
3131 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3133 TALLOC_FREE(subreq);
3134 if (!NT_STATUS_IS_OK(status)) {
3135 tevent_req_nterror(req, status);
3139 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3140 state->attr = SVAL(vwv+10,0);
3141 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3142 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3143 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3145 tevent_req_done(req);
3148 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3151 time_t *change_time,
3152 time_t *access_time,
3155 struct cli_getattrE_state *state = tevent_req_data(
3156 req, struct cli_getattrE_state);
3159 if (tevent_req_is_nterror(req, &status)) {
3163 *attr = state->attr;
3166 *size = state->size;
3169 *change_time = state->change_time;
3172 *access_time = state->access_time;
3175 *write_time = state->write_time;
3177 return NT_STATUS_OK;
3180 NTSTATUS cli_getattrE(struct cli_state *cli,
3184 time_t *change_time,
3185 time_t *access_time,
3188 TALLOC_CTX *frame = talloc_stackframe();
3189 struct event_context *ev = NULL;
3190 struct tevent_req *req = NULL;
3191 NTSTATUS status = NT_STATUS_OK;
3193 if (cli_has_async_calls(cli)) {
3195 * Can't use sync call while an async call is in flight
3197 status = NT_STATUS_INVALID_PARAMETER;
3201 ev = event_context_init(frame);
3203 status = NT_STATUS_NO_MEMORY;
3207 req = cli_getattrE_send(frame, ev, cli, fnum);
3209 status = NT_STATUS_NO_MEMORY;
3213 if (!tevent_req_poll(req, ev)) {
3214 status = map_nt_error_from_unix(errno);
3218 status = cli_getattrE_recv(req,
3227 if (!NT_STATUS_IS_OK(status)) {
3228 cli_set_error(cli, status);
3233 /****************************************************************************
3235 ****************************************************************************/
3237 static void cli_getatr_done(struct tevent_req *subreq);
3239 struct cli_getatr_state {
3246 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3247 struct event_context *ev,
3248 struct cli_state *cli,
3251 struct tevent_req *req = NULL, *subreq = NULL;
3252 struct cli_getatr_state *state = NULL;
3253 uint8_t additional_flags = 0;
3254 uint8_t *bytes = NULL;
3256 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3261 state->zone_offset = cli->serverzone;
3263 bytes = talloc_array(state, uint8_t, 1);
3264 if (tevent_req_nomem(bytes, req)) {
3265 return tevent_req_post(req, ev);
3268 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3269 strlen(fname)+1, NULL);
3271 if (tevent_req_nomem(bytes, req)) {
3272 return tevent_req_post(req, ev);
3275 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3276 0, NULL, talloc_get_size(bytes), bytes);
3277 if (tevent_req_nomem(subreq, req)) {
3278 return tevent_req_post(req, ev);
3280 tevent_req_set_callback(subreq, cli_getatr_done, req);
3284 static void cli_getatr_done(struct tevent_req *subreq)
3286 struct tevent_req *req = tevent_req_callback_data(
3287 subreq, struct tevent_req);
3288 struct cli_getatr_state *state = tevent_req_data(
3289 req, struct cli_getatr_state);
3291 uint16_t *vwv = NULL;
3295 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3297 TALLOC_FREE(subreq);
3298 if (!NT_STATUS_IS_OK(status)) {
3299 tevent_req_nterror(req, status);
3303 state->attr = SVAL(vwv+0,0);
3304 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3305 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3307 tevent_req_done(req);
3310 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3315 struct cli_getatr_state *state = tevent_req_data(
3316 req, struct cli_getatr_state);
3319 if (tevent_req_is_nterror(req, &status)) {
3323 *attr = state->attr;
3326 *size = state->size;
3329 *write_time = state->write_time;
3331 return NT_STATUS_OK;
3334 NTSTATUS cli_getatr(struct cli_state *cli,
3340 TALLOC_CTX *frame = talloc_stackframe();
3341 struct event_context *ev = NULL;
3342 struct tevent_req *req = NULL;
3343 NTSTATUS status = NT_STATUS_OK;
3345 if (cli_has_async_calls(cli)) {
3347 * Can't use sync call while an async call is in flight
3349 status = NT_STATUS_INVALID_PARAMETER;
3353 ev = event_context_init(frame);
3355 status = NT_STATUS_NO_MEMORY;
3359 req = cli_getatr_send(frame, ev, cli, fname);
3361 status = NT_STATUS_NO_MEMORY;
3365 if (!tevent_req_poll(req, ev)) {
3366 status = map_nt_error_from_unix(errno);
3370 status = cli_getatr_recv(req,
3377 if (!NT_STATUS_IS_OK(status)) {
3378 cli_set_error(cli, status);
3383 /****************************************************************************
3384 Do a SMBsetattrE call.
3385 ****************************************************************************/
3387 static void cli_setattrE_done(struct tevent_req *subreq);
3389 struct cli_setattrE_state {
3393 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3394 struct event_context *ev,
3395 struct cli_state *cli,
3401 struct tevent_req *req = NULL, *subreq = NULL;
3402 struct cli_setattrE_state *state = NULL;
3403 uint8_t additional_flags = 0;
3405 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3410 SSVAL(state->vwv+0, 0, fnum);
3411 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3413 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3415 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3418 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3419 7, state->vwv, 0, NULL);
3420 if (tevent_req_nomem(subreq, req)) {
3421 return tevent_req_post(req, ev);
3423 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3427 static void cli_setattrE_done(struct tevent_req *subreq)
3429 struct tevent_req *req = tevent_req_callback_data(
3430 subreq, struct tevent_req);
3433 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3434 TALLOC_FREE(subreq);
3435 if (!NT_STATUS_IS_OK(status)) {
3436 tevent_req_nterror(req, status);
3439 tevent_req_done(req);
3442 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3444 return tevent_req_simple_recv_ntstatus(req);
3447 NTSTATUS cli_setattrE(struct cli_state *cli,
3453 TALLOC_CTX *frame = talloc_stackframe();
3454 struct event_context *ev = NULL;
3455 struct tevent_req *req = NULL;
3456 NTSTATUS status = NT_STATUS_OK;
3458 if (cli_has_async_calls(cli)) {
3460 * Can't use sync call while an async call is in flight
3462 status = NT_STATUS_INVALID_PARAMETER;
3466 ev = event_context_init(frame);
3468 status = NT_STATUS_NO_MEMORY;
3472 req = cli_setattrE_send(frame, ev,
3480 status = NT_STATUS_NO_MEMORY;
3484 if (!tevent_req_poll(req, ev)) {
3485 status = map_nt_error_from_unix(errno);
3489 status = cli_setattrE_recv(req);
3493 if (!NT_STATUS_IS_OK(status)) {
3494 cli_set_error(cli, status);
3499 /****************************************************************************
3500 Do a SMBsetatr call.
3501 ****************************************************************************/
3503 static void cli_setatr_done(struct tevent_req *subreq);
3505 struct cli_setatr_state {
3509 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3510 struct event_context *ev,
3511 struct cli_state *cli,
3516 struct tevent_req *req = NULL, *subreq = NULL;
3517 struct cli_setatr_state *state = NULL;
3518 uint8_t additional_flags = 0;
3519 uint8_t *bytes = NULL;
3521 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3526 SSVAL(state->vwv+0, 0, attr);
3527 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
3529 bytes = talloc_array(state, uint8_t, 1);
3530 if (tevent_req_nomem(bytes, req)) {
3531 return tevent_req_post(req, ev);
3534 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3535 strlen(fname)+1, NULL);
3536 if (tevent_req_nomem(bytes, req)) {
3537 return tevent_req_post(req, ev);
3539 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3540 talloc_get_size(bytes)+1);
3541 if (tevent_req_nomem(bytes, req)) {
3542 return tevent_req_post(req, ev);
3545 bytes[talloc_get_size(bytes)-1] = 4;
3546 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3548 if (tevent_req_nomem(bytes, req)) {
3549 return tevent_req_post(req, ev);
3552 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3553 8, state->vwv, talloc_get_size(bytes), bytes);
3554 if (tevent_req_nomem(subreq, req)) {
3555 return tevent_req_post(req, ev);
3557 tevent_req_set_callback(subreq, cli_setatr_done, req);
3561 static void cli_setatr_done(struct tevent_req *subreq)
3563 struct tevent_req *req = tevent_req_callback_data(
3564 subreq, struct tevent_req);
3567 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3568 TALLOC_FREE(subreq);
3569 if (!NT_STATUS_IS_OK(status)) {
3570 tevent_req_nterror(req, status);
3573 tevent_req_done(req);
3576 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3578 return tevent_req_simple_recv_ntstatus(req);
3581 NTSTATUS cli_setatr(struct cli_state *cli,
3586 TALLOC_CTX *frame = talloc_stackframe();
3587 struct event_context *ev = NULL;
3588 struct tevent_req *req = NULL;
3589 NTSTATUS status = NT_STATUS_OK;
3591 if (cli_has_async_calls(cli)) {
3593 * Can't use sync call while an async call is in flight
3595 status = NT_STATUS_INVALID_PARAMETER;
3599 ev = event_context_init(frame);
3601 status = NT_STATUS_NO_MEMORY;
3605 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3607 status = NT_STATUS_NO_MEMORY;
3611 if (!tevent_req_poll(req, ev)) {
3612 status = map_nt_error_from_unix(errno);
3616 status = cli_setatr_recv(req);
3620 if (!NT_STATUS_IS_OK(status)) {
3621 cli_set_error(cli, status);
3626 /****************************************************************************
3627 Check for existance of a dir.
3628 ****************************************************************************/
3630 static void cli_chkpath_done(struct tevent_req *subreq);
3632 struct cli_chkpath_state {
3636 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3637 struct event_context *ev,
3638 struct cli_state *cli,
3641 struct tevent_req *req = NULL, *subreq = NULL;
3642 struct cli_chkpath_state *state = NULL;
3643 uint8_t additional_flags = 0;
3644 uint8_t *bytes = NULL;
3646 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3651 bytes = talloc_array(state, uint8_t, 1);
3652 if (tevent_req_nomem(bytes, req)) {
3653 return tevent_req_post(req, ev);
3656 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3657 strlen(fname)+1, NULL);
3659 if (tevent_req_nomem(bytes, req)) {
3660 return tevent_req_post(req, ev);
3663 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3664 0, NULL, talloc_get_size(bytes), bytes);
3665 if (tevent_req_nomem(subreq, req)) {
3666 return tevent_req_post(req, ev);
3668 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3672 static void cli_chkpath_done(struct tevent_req *subreq)
3674 struct tevent_req *req = tevent_req_callback_data(
3675 subreq, struct tevent_req);
3678 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3679 TALLOC_FREE(subreq);
3680 if (!NT_STATUS_IS_OK(status)) {
3681 tevent_req_nterror(req, status);
3684 tevent_req_done(req);
3687 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3689 return tevent_req_simple_recv_ntstatus(req);
3692 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3694 TALLOC_CTX *frame = talloc_stackframe();
3695 struct event_context *ev = NULL;
3696 struct tevent_req *req = NULL;
3698 NTSTATUS status = NT_STATUS_OK;
3700 if (cli_has_async_calls(cli)) {
3702 * Can't use sync call while an async call is in flight
3704 status = NT_STATUS_INVALID_PARAMETER;
3708 path2 = talloc_strdup(frame, path);
3710 status = NT_STATUS_NO_MEMORY;
3713 trim_char(path2,'\0','\\');
3715 path2 = talloc_strdup(frame, "\\");
3717 status = NT_STATUS_NO_MEMORY;
3722 ev = event_context_init(frame);
3724 status = NT_STATUS_NO_MEMORY;
3728 req = cli_chkpath_send(frame, ev, cli, path2);
3730 status = NT_STATUS_NO_MEMORY;
3734 if (!tevent_req_poll(req, ev)) {
3735 status = map_nt_error_from_unix(errno);
3739 status = cli_chkpath_recv(req);
3743 if (!NT_STATUS_IS_OK(status)) {
3744 cli_set_error(cli, status);
3749 /****************************************************************************
3751 ****************************************************************************/
3753 static void cli_dskattr_done(struct tevent_req *subreq);
3755 struct cli_dskattr_state {
3761 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3762 struct event_context *ev,
3763 struct cli_state *cli)
3765 struct tevent_req *req = NULL, *subreq = NULL;
3766 struct cli_dskattr_state *state = NULL;
3767 uint8_t additional_flags = 0;
3769 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3774 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3776 if (tevent_req_nomem(subreq, req)) {
3777 return tevent_req_post(req, ev);
3779 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3783 static void cli_dskattr_done(struct tevent_req *subreq)
3785 struct tevent_req *req = tevent_req_callback_data(
3786 subreq, struct tevent_req);
3787 struct cli_dskattr_state *state = tevent_req_data(
3788 req, struct cli_dskattr_state);
3790 uint16_t *vwv = NULL;
3794 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3796 TALLOC_FREE(subreq);
3797 if (!NT_STATUS_IS_OK(status)) {
3798 tevent_req_nterror(req, status);
3801 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3802 state->total = SVAL(vwv+0, 0);
3803 state->avail = SVAL(vwv+3, 0);
3804 tevent_req_done(req);
3807 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3809 struct cli_dskattr_state *state = tevent_req_data(
3810 req, struct cli_dskattr_state);
3813 if (tevent_req_is_nterror(req, &status)) {
3816 *bsize = state->bsize;
3817 *total = state->total;
3818 *avail = state->avail;
3819 return NT_STATUS_OK;
3822 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3824 TALLOC_CTX *frame = talloc_stackframe();
3825 struct event_context *ev = NULL;
3826 struct tevent_req *req = NULL;
3827 NTSTATUS status = NT_STATUS_OK;
3829 if (cli_has_async_calls(cli)) {
3831 * Can't use sync call while an async call is in flight
3833 status = NT_STATUS_INVALID_PARAMETER;
3837 ev = event_context_init(frame);
3839 status = NT_STATUS_NO_MEMORY;
3843 req = cli_dskattr_send(frame, ev, cli);
3845 status = NT_STATUS_NO_MEMORY;
3849 if (!tevent_req_poll(req, ev)) {
3850 status = map_nt_error_from_unix(errno);
3854 status = cli_dskattr_recv(req, bsize, total, avail);
3858 if (!NT_STATUS_IS_OK(status)) {
3859 cli_set_error(cli, status);
3864 /****************************************************************************
3865 Create and open a temporary file.
3866 ****************************************************************************/
3868 static void cli_ctemp_done(struct tevent_req *subreq);
3870 struct ctemp_state {
3876 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3877 struct event_context *ev,
3878 struct cli_state *cli,
3881 struct tevent_req *req = NULL, *subreq = NULL;
3882 struct ctemp_state *state = NULL;
3883 uint8_t additional_flags = 0;
3884 uint8_t *bytes = NULL;
3886 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3891 SSVAL(state->vwv,0,0);
3892 SIVALS(state->vwv+1,0,-1);
3894 bytes = talloc_array(state, uint8_t, 1);
3895 if (tevent_req_nomem(bytes, req)) {
3896 return tevent_req_post(req, ev);
3899 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3900 strlen(path)+1, NULL);
3901 if (tevent_req_nomem(bytes, req)) {
3902 return tevent_req_post(req, ev);
3905 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3906 3, state->vwv, talloc_get_size(bytes), bytes);
3907 if (tevent_req_nomem(subreq, req)) {
3908 return tevent_req_post(req, ev);
3910 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3914 static void cli_ctemp_done(struct tevent_req *subreq)
3916 struct tevent_req *req = tevent_req_callback_data(
3917 subreq, struct tevent_req);
3918 struct ctemp_state *state = tevent_req_data(
3919 req, struct ctemp_state);
3923 uint32_t num_bytes = 0;
3924 uint8_t *bytes = NULL;
3927 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3928 &num_bytes, &bytes);
3929 TALLOC_FREE(subreq);
3930 if (!NT_STATUS_IS_OK(status)) {
3931 tevent_req_nterror(req, status);
3935 state->fnum = SVAL(vwv+0, 0);
3937 /* From W2K3, the result is just the ASCII name */
3938 if (num_bytes < 2) {
3939 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
3943 if (pull_string_talloc(state,
3950 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
3953 tevent_req_done(req);
3956 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
3961 struct ctemp_state *state = tevent_req_data(req,
3962 struct ctemp_state);
3965 if (tevent_req_is_nterror(req, &status)) {
3968 *pfnum = state->fnum;
3969 *outfile = talloc_strdup(ctx, state->ret_path);
3971 return NT_STATUS_NO_MEMORY;
3973 return NT_STATUS_OK;
3976 NTSTATUS cli_ctemp(struct cli_state *cli,
3982 TALLOC_CTX *frame = talloc_stackframe();
3983 struct event_context *ev;
3984 struct tevent_req *req;
3985 NTSTATUS status = NT_STATUS_OK;
3987 if (cli_has_async_calls(cli)) {
3989 * Can't use sync call while an async call is in flight
3991 status = NT_STATUS_INVALID_PARAMETER;
3995 ev = event_context_init(frame);
3997 status = NT_STATUS_NO_MEMORY;
4001 req = cli_ctemp_send(frame, ev, cli, path);
4003 status = NT_STATUS_NO_MEMORY;
4007 if (!tevent_req_poll(req, ev)) {
4008 status = map_nt_error_from_unix(errno);
4012 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4016 if (!NT_STATUS_IS_OK(status)) {
4017 cli_set_error(cli, status);
4023 send a raw ioctl - used by the torture code
4025 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4030 SSVAL(vwv+0, 0, fnum);
4031 SSVAL(vwv+1, 0, code>>16);
4032 SSVAL(vwv+2, 0, (code&0xFFFF));
4034 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4035 NULL, 0, NULL, NULL, NULL, NULL);
4036 if (!NT_STATUS_IS_OK(status)) {
4039 *blob = data_blob_null;
4040 return NT_STATUS_OK;
4043 /*********************************************************
4044 Set an extended attribute utility fn.
4045 *********************************************************/
4047 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4048 uint8_t *param, unsigned int param_len,
4049 const char *ea_name,
4050 const char *ea_val, size_t ea_len)
4053 unsigned int data_len = 0;
4054 uint8_t *data = NULL;
4056 size_t ea_namelen = strlen(ea_name);
4059 SSVAL(setup, 0, setup_val);
4061 if (ea_namelen == 0 && ea_len == 0) {
4063 data = (uint8_t *)SMB_MALLOC(data_len);
4065 return NT_STATUS_NO_MEMORY;
4068 SIVAL(p,0,data_len);
4070 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4071 data = (uint8_t *)SMB_MALLOC(data_len);
4073 return NT_STATUS_NO_MEMORY;
4076 SIVAL(p,0,data_len);
4078 SCVAL(p, 0, 0); /* EA flags. */
4079 SCVAL(p, 1, ea_namelen);
4080 SSVAL(p, 2, ea_len);
4081 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4082 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4085 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4087 param, param_len, 2,
4088 data, data_len, cli->max_xmit,
4090 NULL, 0, NULL, /* rsetup */
4091 NULL, 0, NULL, /* rparam */
4092 NULL, 0, NULL); /* rdata */
4097 /*********************************************************
4098 Set an extended attribute on a pathname.
4099 *********************************************************/
4101 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4102 const char *ea_name, const char *ea_val,
4105 unsigned int param_len = 0;
4107 size_t srclen = 2*(strlen(path)+1);
4111 param = SMB_MALLOC_ARRAY(uint8_t, 6+srclen+2);
4113 return NT_STATUS_NO_MEMORY;
4115 memset(param, '\0', 6);
4116 SSVAL(param,0,SMB_INFO_SET_EA);
4117 p = (char *)(¶m[6]);
4119 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4120 param_len = PTR_DIFF(p, param);
4122 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4123 ea_name, ea_val, ea_len);
4128 /*********************************************************
4129 Set an extended attribute on an fnum.
4130 *********************************************************/
4132 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4133 const char *ea_name, const char *ea_val,
4138 memset(param, 0, 6);
4139 SSVAL(param,0,fnum);
4140 SSVAL(param,2,SMB_INFO_SET_EA);
4142 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4143 ea_name, ea_val, ea_len);
4146 /*********************************************************
4147 Get an extended attribute list utility fn.
4148 *********************************************************/
4150 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4152 size_t *pnum_eas, struct ea_struct **pea_list)
4154 struct ea_struct *ea_list = NULL;
4159 if (rdata_len < 4) {
4163 ea_size = (size_t)IVAL(rdata,0);
4164 if (ea_size > rdata_len) {
4169 /* No EA's present. */
4178 /* Validate the EA list and count it. */
4179 for (num_eas = 0; ea_size >= 4; num_eas++) {
4180 unsigned int ea_namelen = CVAL(p,1);
4181 unsigned int ea_valuelen = SVAL(p,2);
4182 if (ea_namelen == 0) {
4185 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4188 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4189 p += 4 + ea_namelen + 1 + ea_valuelen;
4198 *pnum_eas = num_eas;
4200 /* Caller only wants number of EA's. */
4204 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
4209 ea_size = (size_t)IVAL(rdata,0);
4212 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4213 struct ea_struct *ea = &ea_list[num_eas];
4214 fstring unix_ea_name;
4215 unsigned int ea_namelen = CVAL(p,1);
4216 unsigned int ea_valuelen = SVAL(p,2);
4218 ea->flags = CVAL(p,0);
4219 unix_ea_name[0] = '\0';
4220 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4221 ea->name = talloc_strdup(ea_list, unix_ea_name);
4225 /* Ensure the value is null terminated (in case it's a string). */
4226 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4227 if (!ea->value.data) {
4231 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4233 ea->value.data[ea_valuelen] = 0;
4235 p += 4 + ea_namelen + 1 + ea_valuelen;
4238 *pea_list = ea_list;
4242 TALLOC_FREE(ea_list);
4246 /*********************************************************
4247 Get an extended attribute list from a pathname.
4248 *********************************************************/
4250 struct cli_get_ea_list_path_state {
4255 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4257 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4258 struct tevent_context *ev,
4259 struct cli_state *cli,
4262 struct tevent_req *req, *subreq;
4263 struct cli_get_ea_list_path_state *state;
4265 req = tevent_req_create(mem_ctx, &state,
4266 struct cli_get_ea_list_path_state);
4270 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4271 SMB_INFO_QUERY_ALL_EAS, 4,
4273 if (tevent_req_nomem(subreq, req)) {
4274 return tevent_req_post(req, ev);
4276 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4280 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4282 struct tevent_req *req = tevent_req_callback_data(
4283 subreq, struct tevent_req);
4284 struct cli_get_ea_list_path_state *state = tevent_req_data(
4285 req, struct cli_get_ea_list_path_state);
4288 status = cli_qpathinfo_recv(subreq, state, &state->data,
4290 TALLOC_FREE(subreq);
4291 if (!NT_STATUS_IS_OK(status)) {
4292 tevent_req_nterror(req, status);
4295 tevent_req_done(req);
4298 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4299 size_t *pnum_eas, struct ea_struct **peas)
4301 struct cli_get_ea_list_path_state *state = tevent_req_data(
4302 req, struct cli_get_ea_list_path_state);
4305 if (tevent_req_is_nterror(req, &status)) {
4308 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4310 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4312 return NT_STATUS_OK;
4315 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4318 struct ea_struct **pea_list)
4320 TALLOC_CTX *frame = talloc_stackframe();
4321 struct event_context *ev = NULL;
4322 struct tevent_req *req = NULL;
4323 NTSTATUS status = NT_STATUS_NO_MEMORY;
4325 if (cli_has_async_calls(cli)) {
4327 * Can't use sync call while an async call is in flight
4329 status = NT_STATUS_INVALID_PARAMETER;
4332 ev = event_context_init(frame);
4336 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4340 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4343 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4346 if (!NT_STATUS_IS_OK(status)) {
4347 cli_set_error(cli, status);
4352 /****************************************************************************
4353 Convert open "flags" arg to uint32_t on wire.
4354 ****************************************************************************/
4356 static uint32_t open_flags_to_wire(int flags)
4358 int open_mode = flags & O_ACCMODE;
4361 switch (open_mode) {
4363 ret |= SMB_O_WRONLY;
4370 ret |= SMB_O_RDONLY;
4374 if (flags & O_CREAT) {
4377 if (flags & O_EXCL) {
4380 if (flags & O_TRUNC) {
4384 if (flags & O_SYNC) {
4388 if (flags & O_APPEND) {
4389 ret |= SMB_O_APPEND;
4391 #if defined(O_DIRECT)
4392 if (flags & O_DIRECT) {
4393 ret |= SMB_O_DIRECT;
4396 #if defined(O_DIRECTORY)
4397 if (flags & O_DIRECTORY) {
4398 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4399 ret |= SMB_O_DIRECTORY;
4405 /****************************************************************************
4406 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4407 ****************************************************************************/
4409 struct posix_open_state {
4413 uint16_t fnum; /* Out */
4416 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4418 struct tevent_req *req = tevent_req_callback_data(
4419 subreq, struct tevent_req);
4420 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4425 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4426 NULL, 0, NULL, &data, 12, &num_data);
4427 TALLOC_FREE(subreq);
4428 if (!NT_STATUS_IS_OK(status)) {
4429 tevent_req_nterror(req, status);
4432 state->fnum = SVAL(data,2);
4433 tevent_req_done(req);
4436 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4437 struct event_context *ev,
4438 struct cli_state *cli,
4444 struct tevent_req *req = NULL, *subreq = NULL;
4445 struct posix_open_state *state = NULL;
4446 uint32_t wire_flags = open_flags_to_wire(flags);
4448 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4453 /* Setup setup word. */
4454 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4456 /* Setup param array. */
4457 state->param = talloc_array(state, uint8_t, 6);
4458 if (tevent_req_nomem(state->param, req)) {
4459 return tevent_req_post(req, ev);
4461 memset(state->param, '\0', 6);
4462 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4464 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4465 strlen(fname)+1, NULL);
4467 if (tevent_req_nomem(state->param, req)) {
4468 return tevent_req_post(req, ev);
4471 /* Setup data words. */
4473 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4474 wire_flags |= SMB_O_DIRECTORY;
4477 SIVAL(state->data,0,0); /* No oplock. */
4478 SIVAL(state->data,4,wire_flags);
4479 SIVAL(state->data,8,unix_perms_to_wire(mode));
4480 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4481 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4483 subreq = cli_trans_send(state, /* mem ctx. */
4484 ev, /* event ctx. */
4485 cli, /* cli_state. */
4486 SMBtrans2, /* cmd. */
4487 NULL, /* pipe name. */
4491 &state->setup, /* setup. */
4492 1, /* num setup uint16_t words. */
4493 0, /* max returned setup. */
4494 state->param, /* param. */
4495 talloc_get_size(state->param),/* num param. */
4496 2, /* max returned param. */
4497 state->data, /* data. */
4499 12); /* max returned data. */
4501 if (tevent_req_nomem(subreq, req)) {
4502 return tevent_req_post(req, ev);
4504 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4508 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4509 struct event_context *ev,
4510 struct cli_state *cli,
4515 return cli_posix_open_internal_send(mem_ctx, ev,
4516 cli, fname, flags, mode, false);
4519 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4521 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4524 if (tevent_req_is_nterror(req, &status)) {
4527 *pfnum = state->fnum;
4528 return NT_STATUS_OK;
4531 /****************************************************************************
4532 Open - POSIX semantics. Doesn't request oplock.
4533 ****************************************************************************/
4535 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4536 int flags, mode_t mode, uint16_t *pfnum)
4539 TALLOC_CTX *frame = talloc_stackframe();
4540 struct event_context *ev = NULL;
4541 struct tevent_req *req = NULL;
4542 NTSTATUS status = NT_STATUS_OK;
4544 if (cli_has_async_calls(cli)) {
4546 * Can't use sync call while an async call is in flight
4548 status = NT_STATUS_INVALID_PARAMETER;
4552 ev = event_context_init(frame);
4554 status = NT_STATUS_NO_MEMORY;
4558 req = cli_posix_open_send(frame,
4565 status = NT_STATUS_NO_MEMORY;
4569 if (!tevent_req_poll(req, ev)) {
4570 status = map_nt_error_from_unix(errno);
4574 status = cli_posix_open_recv(req, pfnum);
4578 if (!NT_STATUS_IS_OK(status)) {
4579 cli_set_error(cli, status);
4584 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4585 struct event_context *ev,
4586 struct cli_state *cli,
4590 return cli_posix_open_internal_send(mem_ctx, ev,
4591 cli, fname, O_CREAT, mode, true);
4594 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4596 return tevent_req_simple_recv_ntstatus(req);
4599 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4601 TALLOC_CTX *frame = talloc_stackframe();
4602 struct event_context *ev = NULL;
4603 struct tevent_req *req = NULL;
4604 NTSTATUS status = NT_STATUS_OK;
4606 if (cli_has_async_calls(cli)) {
4608 * Can't use sync call while an async call is in flight
4610 status = NT_STATUS_INVALID_PARAMETER;
4614 ev = event_context_init(frame);
4616 status = NT_STATUS_NO_MEMORY;
4620 req = cli_posix_mkdir_send(frame,
4626 status = NT_STATUS_NO_MEMORY;
4630 if (!tevent_req_poll(req, ev)) {
4631 status = map_nt_error_from_unix(errno);
4635 status = cli_posix_mkdir_recv(req);
4639 if (!NT_STATUS_IS_OK(status)) {
4640 cli_set_error(cli, status);
4645 /****************************************************************************
4646 unlink or rmdir - POSIX semantics.
4647 ****************************************************************************/
4649 struct cli_posix_unlink_internal_state {
4653 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4655 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4656 struct event_context *ev,
4657 struct cli_state *cli,
4661 struct tevent_req *req = NULL, *subreq = NULL;
4662 struct cli_posix_unlink_internal_state *state = NULL;
4664 req = tevent_req_create(mem_ctx, &state,
4665 struct cli_posix_unlink_internal_state);
4670 /* Setup data word. */
4671 SSVAL(state->data, 0, level);
4673 subreq = cli_setpathinfo_send(state, ev, cli,
4674 SMB_POSIX_PATH_UNLINK,
4676 state->data, sizeof(state->data));
4677 if (tevent_req_nomem(subreq, req)) {
4678 return tevent_req_post(req, ev);
4680 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4684 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4686 NTSTATUS status = cli_setpathinfo_recv(subreq);
4687 tevent_req_simple_finish_ntstatus(subreq, status);
4690 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4691 struct event_context *ev,
4692 struct cli_state *cli,
4695 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4696 SMB_POSIX_UNLINK_FILE_TARGET);
4699 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4701 return tevent_req_simple_recv_ntstatus(req);
4704 /****************************************************************************
4705 unlink - POSIX semantics.
4706 ****************************************************************************/
4708 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4710 TALLOC_CTX *frame = talloc_stackframe();
4711 struct event_context *ev = NULL;
4712 struct tevent_req *req = NULL;
4713 NTSTATUS status = NT_STATUS_OK;
4715 if (cli_has_async_calls(cli)) {
4717 * Can't use sync call while an async call is in flight
4719 status = NT_STATUS_INVALID_PARAMETER;
4723 ev = event_context_init(frame);
4725 status = NT_STATUS_NO_MEMORY;
4729 req = cli_posix_unlink_send(frame,
4734 status = NT_STATUS_NO_MEMORY;
4738 if (!tevent_req_poll(req, ev)) {
4739 status = map_nt_error_from_unix(errno);
4743 status = cli_posix_unlink_recv(req);
4747 if (!NT_STATUS_IS_OK(status)) {
4748 cli_set_error(cli, status);
4753 /****************************************************************************
4754 rmdir - POSIX semantics.
4755 ****************************************************************************/
4757 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4758 struct event_context *ev,
4759 struct cli_state *cli,
4762 return cli_posix_unlink_internal_send(
4763 mem_ctx, ev, cli, fname,
4764 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4767 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4769 return tevent_req_simple_recv_ntstatus(req);
4772 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4774 TALLOC_CTX *frame = talloc_stackframe();
4775 struct event_context *ev = NULL;
4776 struct tevent_req *req = NULL;
4777 NTSTATUS status = NT_STATUS_OK;
4779 if (cli_has_async_calls(cli)) {
4781 * Can't use sync call while an async call is in flight
4783 status = NT_STATUS_INVALID_PARAMETER;
4787 ev = event_context_init(frame);
4789 status = NT_STATUS_NO_MEMORY;
4793 req = cli_posix_rmdir_send(frame,
4798 status = NT_STATUS_NO_MEMORY;
4802 if (!tevent_req_poll(req, ev)) {
4803 status = map_nt_error_from_unix(errno);
4807 status = cli_posix_rmdir_recv(req, frame);
4811 if (!NT_STATUS_IS_OK(status)) {
4812 cli_set_error(cli, status);
4817 /****************************************************************************
4819 ****************************************************************************/
4821 struct cli_notify_state {
4823 uint32_t num_changes;
4824 struct notify_change *changes;
4827 static void cli_notify_done(struct tevent_req *subreq);
4829 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4830 struct tevent_context *ev,
4831 struct cli_state *cli, uint16_t fnum,
4832 uint32_t buffer_size,
4833 uint32_t completion_filter, bool recursive)
4835 struct tevent_req *req, *subreq;
4836 struct cli_notify_state *state;
4838 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4843 SIVAL(state->setup, 0, completion_filter);
4844 SSVAL(state->setup, 4, fnum);
4845 SSVAL(state->setup, 6, recursive);
4847 subreq = cli_trans_send(
4848 state, /* mem ctx. */
4849 ev, /* event ctx. */
4850 cli, /* cli_state. */
4851 SMBnttrans, /* cmd. */
4852 NULL, /* pipe name. */
4854 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4856 (uint16_t *)state->setup, /* setup. */
4857 4, /* num setup uint16_t words. */
4858 0, /* max returned setup. */
4861 buffer_size, /* max returned param. */
4864 0); /* max returned data. */
4866 if (tevent_req_nomem(subreq, req)) {
4867 return tevent_req_post(req, ev);
4869 tevent_req_set_callback(subreq, cli_notify_done, req);
4873 static void cli_notify_done(struct tevent_req *subreq)
4875 struct tevent_req *req = tevent_req_callback_data(
4876 subreq, struct tevent_req);
4877 struct cli_notify_state *state = tevent_req_data(
4878 req, struct cli_notify_state);
4881 uint32_t i, ofs, num_params;
4884 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4885 ¶ms, 0, &num_params, NULL, 0, NULL);
4886 TALLOC_FREE(subreq);
4887 if (!NT_STATUS_IS_OK(status)) {
4888 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4889 tevent_req_nterror(req, status);
4893 state->num_changes = 0;
4896 while (num_params - ofs > 12) {
4897 uint32_t len = IVAL(params, ofs);
4898 state->num_changes += 1;
4900 if ((len == 0) || (ofs+len >= num_params)) {
4906 state->changes = talloc_array(state, struct notify_change,
4907 state->num_changes);
4908 if (tevent_req_nomem(state->changes, req)) {
4909 TALLOC_FREE(params);
4915 for (i=0; i<state->num_changes; i++) {
4916 uint32_t next = IVAL(params, ofs);
4917 uint32_t len = IVAL(params, ofs+8);
4921 if ((next != 0) && (len+12 != next)) {
4922 TALLOC_FREE(params);
4924 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4928 state->changes[i].action = IVAL(params, ofs+4);
4929 ret = clistr_pull_talloc(params, (char *)params, flags2,
4930 &name, params+ofs+12, len,
4931 STR_TERMINATE|STR_UNICODE);
4933 TALLOC_FREE(params);
4934 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4937 state->changes[i].name = name;
4941 TALLOC_FREE(params);
4942 tevent_req_done(req);
4945 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4946 uint32_t *pnum_changes,
4947 struct notify_change **pchanges)
4949 struct cli_notify_state *state = tevent_req_data(
4950 req, struct cli_notify_state);
4953 if (tevent_req_is_nterror(req, &status)) {
4957 *pnum_changes = state->num_changes;
4958 *pchanges = talloc_move(mem_ctx, &state->changes);
4959 return NT_STATUS_OK;
4962 struct cli_qpathinfo_state {
4971 static void cli_qpathinfo_done(struct tevent_req *subreq);
4973 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
4974 struct tevent_context *ev,
4975 struct cli_state *cli, const char *fname,
4976 uint16_t level, uint32_t min_rdata,
4979 struct tevent_req *req, *subreq;
4980 struct cli_qpathinfo_state *state;
4982 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
4986 state->min_rdata = min_rdata;
4987 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
4989 state->param = talloc_zero_array(state, uint8_t, 6);
4990 if (tevent_req_nomem(state->param, req)) {
4991 return tevent_req_post(req, ev);
4993 SSVAL(state->param, 0, level);
4994 state->param = trans2_bytes_push_str(
4995 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
4996 if (tevent_req_nomem(state->param, req)) {
4997 return tevent_req_post(req, ev);
5000 subreq = cli_trans_send(
5001 state, /* mem ctx. */
5002 ev, /* event ctx. */
5003 cli, /* cli_state. */
5004 SMBtrans2, /* cmd. */
5005 NULL, /* pipe name. */
5009 state->setup, /* setup. */
5010 1, /* num setup uint16_t words. */
5011 0, /* max returned setup. */
5012 state->param, /* param. */
5013 talloc_get_size(state->param), /* num param. */
5014 2, /* max returned param. */
5017 max_rdata); /* max returned data. */
5019 if (tevent_req_nomem(subreq, req)) {
5020 return tevent_req_post(req, ev);
5022 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5026 static void cli_qpathinfo_done(struct tevent_req *subreq)
5028 struct tevent_req *req = tevent_req_callback_data(
5029 subreq, struct tevent_req);
5030 struct cli_qpathinfo_state *state = tevent_req_data(
5031 req, struct cli_qpathinfo_state);
5034 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5036 &state->rdata, state->min_rdata,
5038 if (!NT_STATUS_IS_OK(status)) {
5039 tevent_req_nterror(req, status);
5042 tevent_req_done(req);
5045 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5046 uint8_t **rdata, uint32_t *num_rdata)
5048 struct cli_qpathinfo_state *state = tevent_req_data(
5049 req, struct cli_qpathinfo_state);
5052 if (tevent_req_is_nterror(req, &status)) {
5055 if (rdata != NULL) {
5056 *rdata = talloc_move(mem_ctx, &state->rdata);
5058 TALLOC_FREE(state->rdata);
5060 if (num_rdata != NULL) {
5061 *num_rdata = state->num_rdata;
5063 return NT_STATUS_OK;
5066 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5067 const char *fname, uint16_t level, uint32_t min_rdata,
5069 uint8_t **rdata, uint32_t *num_rdata)
5071 TALLOC_CTX *frame = talloc_stackframe();
5072 struct event_context *ev;
5073 struct tevent_req *req;
5074 NTSTATUS status = NT_STATUS_NO_MEMORY;
5076 if (cli_has_async_calls(cli)) {
5078 * Can't use sync call while an async call is in flight
5080 status = NT_STATUS_INVALID_PARAMETER;
5083 ev = event_context_init(frame);
5087 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5092 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5095 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5098 if (!NT_STATUS_IS_OK(status)) {
5099 cli_set_error(cli, status);
5104 struct cli_qfileinfo_state {
5113 static void cli_qfileinfo_done(struct tevent_req *subreq);
5115 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5116 struct tevent_context *ev,
5117 struct cli_state *cli, uint16_t fnum,
5118 uint16_t level, uint32_t min_rdata,
5121 struct tevent_req *req, *subreq;
5122 struct cli_qfileinfo_state *state;
5124 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5128 state->min_rdata = min_rdata;
5129 SSVAL(state->param, 0, fnum);
5130 SSVAL(state->param, 2, level);
5131 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5133 subreq = cli_trans_send(
5134 state, /* mem ctx. */
5135 ev, /* event ctx. */
5136 cli, /* cli_state. */
5137 SMBtrans2, /* cmd. */
5138 NULL, /* pipe name. */
5142 state->setup, /* setup. */
5143 1, /* num setup uint16_t words. */
5144 0, /* max returned setup. */
5145 state->param, /* param. */
5146 sizeof(state->param), /* num param. */
5147 2, /* max returned param. */
5150 max_rdata); /* max returned data. */
5152 if (tevent_req_nomem(subreq, req)) {
5153 return tevent_req_post(req, ev);
5155 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5159 static void cli_qfileinfo_done(struct tevent_req *subreq)
5161 struct tevent_req *req = tevent_req_callback_data(
5162 subreq, struct tevent_req);
5163 struct cli_qfileinfo_state *state = tevent_req_data(
5164 req, struct cli_qfileinfo_state);
5167 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5169 &state->rdata, state->min_rdata,
5171 if (!NT_STATUS_IS_OK(status)) {
5172 tevent_req_nterror(req, status);
5175 tevent_req_done(req);
5178 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5179 uint8_t **rdata, uint32_t *num_rdata)
5181 struct cli_qfileinfo_state *state = tevent_req_data(
5182 req, struct cli_qfileinfo_state);
5185 if (tevent_req_is_nterror(req, &status)) {
5188 if (rdata != NULL) {
5189 *rdata = talloc_move(mem_ctx, &state->rdata);
5191 TALLOC_FREE(state->rdata);
5193 if (num_rdata != NULL) {
5194 *num_rdata = state->num_rdata;
5196 return NT_STATUS_OK;
5199 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5200 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5202 uint8_t **rdata, uint32_t *num_rdata)
5204 TALLOC_CTX *frame = talloc_stackframe();
5205 struct event_context *ev;
5206 struct tevent_req *req;
5207 NTSTATUS status = NT_STATUS_NO_MEMORY;
5209 if (cli_has_async_calls(cli)) {
5211 * Can't use sync call while an async call is in flight
5213 status = NT_STATUS_INVALID_PARAMETER;
5216 ev = event_context_init(frame);
5220 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5225 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5228 status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
5231 if (!NT_STATUS_IS_OK(status)) {
5232 cli_set_error(cli, status);
5237 struct cli_flush_state {
5241 static void cli_flush_done(struct tevent_req *subreq);
5243 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5244 struct event_context *ev,
5245 struct cli_state *cli,
5248 struct tevent_req *req, *subreq;
5249 struct cli_flush_state *state;
5251 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5255 SSVAL(state->vwv + 0, 0, fnum);
5257 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5259 if (tevent_req_nomem(subreq, req)) {
5260 return tevent_req_post(req, ev);
5262 tevent_req_set_callback(subreq, cli_flush_done, req);
5266 static void cli_flush_done(struct tevent_req *subreq)
5268 struct tevent_req *req = tevent_req_callback_data(
5269 subreq, struct tevent_req);
5272 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5273 TALLOC_FREE(subreq);
5274 if (!NT_STATUS_IS_OK(status)) {
5275 tevent_req_nterror(req, status);
5278 tevent_req_done(req);
5281 NTSTATUS cli_flush_recv(struct tevent_req *req)
5283 return tevent_req_simple_recv_ntstatus(req);
5286 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5288 TALLOC_CTX *frame = talloc_stackframe();
5289 struct event_context *ev;
5290 struct tevent_req *req;
5291 NTSTATUS status = NT_STATUS_NO_MEMORY;
5293 if (cli_has_async_calls(cli)) {
5295 * Can't use sync call while an async call is in flight
5297 status = NT_STATUS_INVALID_PARAMETER;
5300 ev = event_context_init(frame);
5304 req = cli_flush_send(frame, ev, cli, fnum);
5308 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5311 status = cli_flush_recv(req);
5314 if (!NT_STATUS_IS_OK(status)) {
5315 cli_set_error(cli, status);
5320 struct cli_shadow_copy_data_state {
5327 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5329 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5330 struct tevent_context *ev,
5331 struct cli_state *cli,
5335 struct tevent_req *req, *subreq;
5336 struct cli_shadow_copy_data_state *state;
5339 req = tevent_req_create(mem_ctx, &state,
5340 struct cli_shadow_copy_data_state);
5344 state->get_names = get_names;
5345 ret_size = get_names ? cli->max_xmit : 16;
5347 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5348 SSVAL(state->setup + 2, 0, fnum);
5349 SCVAL(state->setup + 3, 0, 0); /* isFsctl */
5350 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5352 subreq = cli_trans_send(
5353 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5354 state->setup, ARRAY_SIZE(state->setup), 0,
5357 if (tevent_req_nomem(subreq, req)) {
5358 return tevent_req_post(req, ev);
5360 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5364 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5366 struct tevent_req *req = tevent_req_callback_data(
5367 subreq, struct tevent_req);
5368 struct cli_shadow_copy_data_state *state = tevent_req_data(
5369 req, struct cli_shadow_copy_data_state);
5372 status = cli_trans_recv(subreq, state, NULL,
5373 NULL, 0, NULL, /* setup */
5374 NULL, 0, NULL, /* param */
5375 &state->data, 12, &state->num_data);
5376 TALLOC_FREE(subreq);
5377 if (!NT_STATUS_IS_OK(status)) {
5378 tevent_req_nterror(req, status);
5381 tevent_req_done(req);
5384 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5385 char ***pnames, int *pnum_names)
5387 struct cli_shadow_copy_data_state *state = tevent_req_data(
5388 req, struct cli_shadow_copy_data_state);
5394 if (tevent_req_is_nterror(req, &status)) {
5397 num_names = IVAL(state->data, 4);
5398 dlength = IVAL(state->data, 8);
5400 if (!state->get_names) {
5401 *pnum_names = num_names;
5402 return NT_STATUS_OK;
5405 if (dlength+12 > state->num_data) {
5406 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5408 names = talloc_array(mem_ctx, char *, num_names);
5409 if (names == NULL) {
5410 return NT_STATUS_NO_MEMORY;
5413 for (i=0; i<num_names; i++) {
5416 size_t converted_size;
5418 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5419 ret = convert_string_talloc(
5420 names, CH_UTF16LE, CH_UNIX,
5421 src, 2 * sizeof(SHADOW_COPY_LABEL),
5422 &names[i], &converted_size);
5425 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5428 *pnum_names = num_names;
5430 return NT_STATUS_OK;
5433 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5434 uint16_t fnum, bool get_names,
5435 char ***pnames, int *pnum_names)
5437 TALLOC_CTX *frame = talloc_stackframe();
5438 struct event_context *ev;
5439 struct tevent_req *req;
5440 NTSTATUS status = NT_STATUS_NO_MEMORY;
5442 if (cli_has_async_calls(cli)) {
5444 * Can't use sync call while an async call is in flight
5446 status = NT_STATUS_INVALID_PARAMETER;
5449 ev = event_context_init(frame);
5453 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5457 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5460 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5463 if (!NT_STATUS_IS_OK(status)) {
5464 cli_set_error(cli, status);