2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "async_smb.h"
26 #include "libsmb/clirap.h"
29 #include "libcli/security/secdesc.h"
31 /***********************************************************
32 Common function for pushing stings, used by smb_bytes_push_str()
33 and trans_bytes_push_str(). Only difference is the align_odd
35 ***********************************************************/
37 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
38 const char *str, size_t str_len,
40 size_t *pconverted_size)
44 size_t converted_size;
50 buflen = talloc_get_size(buf);
53 ((align_odd && (buflen % 2 == 0)) ||
54 (!align_odd && (buflen % 2 == 1)))) {
56 * We're pushing into an SMB buffer, align odd
58 buf = talloc_realloc(NULL, buf, uint8_t, buflen + 1);
66 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
67 ucs2 ? CH_UTF16LE : CH_DOS,
68 str, str_len, &converted,
73 buf = talloc_realloc(NULL, buf, uint8_t,
74 buflen + converted_size);
76 TALLOC_FREE(converted);
80 memcpy(buf + buflen, converted, converted_size);
82 TALLOC_FREE(converted);
84 if (pconverted_size) {
85 *pconverted_size = converted_size;
91 /***********************************************************
92 Push a string into an SMB buffer, with odd byte alignment
93 if it's a UCS2 string.
94 ***********************************************************/
96 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
97 const char *str, size_t str_len,
98 size_t *pconverted_size)
100 return internal_bytes_push_str(buf, ucs2, str, str_len,
101 true, pconverted_size);
104 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
105 const uint8_t *bytes, size_t num_bytes)
112 buflen = talloc_get_size(buf);
114 buf = talloc_realloc(NULL, buf, uint8_t,
115 buflen + 1 + num_bytes);
119 buf[buflen] = prefix;
120 memcpy(&buf[buflen+1], bytes, num_bytes);
124 /***********************************************************
125 Same as smb_bytes_push_str(), but without the odd byte
126 align for ucs2 (we're pushing into a param or data block).
127 static for now, although this will probably change when
128 other modules use async trans calls.
129 ***********************************************************/
131 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
132 const char *str, size_t str_len,
133 size_t *pconverted_size)
135 return internal_bytes_push_str(buf, ucs2, str, str_len,
136 false, pconverted_size);
139 struct cli_setpathinfo_state {
144 static void cli_setpathinfo_done(struct tevent_req *subreq);
146 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
147 struct tevent_context *ev,
148 struct cli_state *cli,
154 struct tevent_req *req, *subreq;
155 struct cli_setpathinfo_state *state;
157 req = tevent_req_create(mem_ctx, &state,
158 struct cli_setpathinfo_state);
163 /* Setup setup word. */
164 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
166 /* Setup param array. */
167 state->param = talloc_zero_array(state, uint8_t, 6);
168 if (tevent_req_nomem(state->param, req)) {
169 return tevent_req_post(req, ev);
171 SSVAL(state->param, 0, level);
173 state->param = trans2_bytes_push_str(
174 state->param, cli_ucs2(cli), path, strlen(path)+1, NULL);
175 if (tevent_req_nomem(state->param, req)) {
176 return tevent_req_post(req, ev);
179 subreq = cli_trans_send(
180 state, /* mem ctx. */
182 cli, /* cli_state. */
183 SMBtrans2, /* cmd. */
184 NULL, /* pipe name. */
188 &state->setup, /* setup. */
189 1, /* num setup uint16_t words. */
190 0, /* max returned setup. */
191 state->param, /* param. */
192 talloc_get_size(state->param), /* num param. */
193 2, /* max returned param. */
195 data_len, /* num data. */
196 0); /* max returned data. */
198 if (tevent_req_nomem(subreq, req)) {
199 return tevent_req_post(req, ev);
201 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
205 static void cli_setpathinfo_done(struct tevent_req *subreq)
207 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
208 NULL, 0, NULL, NULL, 0, NULL);
209 tevent_req_simple_finish_ntstatus(subreq, status);
212 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
214 return tevent_req_simple_recv_ntstatus(req);
217 NTSTATUS cli_setpathinfo(struct cli_state *cli,
223 TALLOC_CTX *frame = talloc_stackframe();
224 struct tevent_context *ev;
225 struct tevent_req *req;
226 NTSTATUS status = NT_STATUS_NO_MEMORY;
228 if (cli_has_async_calls(cli)) {
230 * Can't use sync call while an async call is in flight
232 status = NT_STATUS_INVALID_PARAMETER;
235 ev = tevent_context_init(frame);
239 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
243 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
246 status = cli_setpathinfo_recv(req);
252 /****************************************************************************
253 Hard/Symlink a file (UNIX extensions).
254 Creates new name (sym)linked to oldname.
255 ****************************************************************************/
257 struct cli_posix_link_internal_state {
261 static void cli_posix_link_internal_done(struct tevent_req *subreq);
263 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
264 struct event_context *ev,
265 struct cli_state *cli,
270 struct tevent_req *req = NULL, *subreq = NULL;
271 struct cli_posix_link_internal_state *state = NULL;
273 req = tevent_req_create(mem_ctx, &state,
274 struct cli_posix_link_internal_state);
279 /* Setup data array. */
280 state->data = talloc_array(state, uint8_t, 0);
281 if (tevent_req_nomem(state->data, req)) {
282 return tevent_req_post(req, ev);
284 state->data = trans2_bytes_push_str(
285 state->data, cli_ucs2(cli), oldname, strlen(oldname)+1, NULL);
287 subreq = cli_setpathinfo_send(
288 state, ev, cli, level, newname,
289 state->data, talloc_get_size(state->data));
290 if (tevent_req_nomem(subreq, req)) {
291 return tevent_req_post(req, ev);
293 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
297 static void cli_posix_link_internal_done(struct tevent_req *subreq)
299 NTSTATUS status = cli_setpathinfo_recv(subreq);
300 tevent_req_simple_finish_ntstatus(subreq, status);
303 /****************************************************************************
304 Symlink a file (UNIX extensions).
305 ****************************************************************************/
307 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
308 struct event_context *ev,
309 struct cli_state *cli,
313 return cli_posix_link_internal_send(
314 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
317 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
319 return tevent_req_simple_recv_ntstatus(req);
322 NTSTATUS cli_posix_symlink(struct cli_state *cli,
326 TALLOC_CTX *frame = talloc_stackframe();
327 struct event_context *ev = NULL;
328 struct tevent_req *req = NULL;
329 NTSTATUS status = NT_STATUS_OK;
331 if (cli_has_async_calls(cli)) {
333 * Can't use sync call while an async call is in flight
335 status = NT_STATUS_INVALID_PARAMETER;
339 ev = event_context_init(frame);
341 status = NT_STATUS_NO_MEMORY;
345 req = cli_posix_symlink_send(frame,
351 status = NT_STATUS_NO_MEMORY;
355 if (!tevent_req_poll(req, ev)) {
356 status = map_nt_error_from_unix(errno);
360 status = cli_posix_symlink_recv(req);
367 /****************************************************************************
368 Read a POSIX symlink.
369 ****************************************************************************/
371 struct readlink_state {
376 static void cli_posix_readlink_done(struct tevent_req *subreq);
378 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
379 struct event_context *ev,
380 struct cli_state *cli,
384 struct tevent_req *req = NULL, *subreq = NULL;
385 struct readlink_state *state = NULL;
386 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
388 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
394 * Len is in bytes, we need it in UCS2 units.
396 if ((2*len < len) || (maxbytelen < len)) {
397 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
398 return tevent_req_post(req, ev);
401 subreq = cli_qpathinfo_send(state, ev, cli, fname,
402 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
403 if (tevent_req_nomem(subreq, req)) {
404 return tevent_req_post(req, ev);
406 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
410 static void cli_posix_readlink_done(struct tevent_req *subreq)
412 struct tevent_req *req = tevent_req_callback_data(
413 subreq, struct tevent_req);
414 struct readlink_state *state = tevent_req_data(
415 req, struct readlink_state);
418 status = cli_qpathinfo_recv(subreq, state, &state->data,
421 if (tevent_req_nterror(req, status)) {
425 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
427 if (state->data[state->num_data-1] != '\0') {
428 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
431 tevent_req_done(req);
434 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
435 char *retpath, size_t len)
438 char *converted = NULL;
439 size_t converted_size = 0;
440 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
442 if (tevent_req_is_nterror(req, &status)) {
445 /* The returned data is a pushed string, not raw data. */
446 if (!convert_string_talloc(state,
447 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
453 return NT_STATUS_NO_MEMORY;
456 len = MIN(len,converted_size);
458 return NT_STATUS_DATA_ERROR;
460 memcpy(retpath, converted, len);
464 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
465 char *linkpath, size_t len)
467 TALLOC_CTX *frame = talloc_stackframe();
468 struct event_context *ev = NULL;
469 struct tevent_req *req = NULL;
470 NTSTATUS status = NT_STATUS_OK;
472 if (cli_has_async_calls(cli)) {
474 * Can't use sync call while an async call is in flight
476 status = NT_STATUS_INVALID_PARAMETER;
480 ev = event_context_init(frame);
482 status = NT_STATUS_NO_MEMORY;
486 req = cli_posix_readlink_send(frame,
492 status = NT_STATUS_NO_MEMORY;
496 if (!tevent_req_poll(req, ev)) {
497 status = map_nt_error_from_unix(errno);
501 status = cli_posix_readlink_recv(req, cli, linkpath, len);
508 /****************************************************************************
509 Hard link a file (UNIX extensions).
510 ****************************************************************************/
512 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
513 struct event_context *ev,
514 struct cli_state *cli,
518 return cli_posix_link_internal_send(
519 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
522 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
524 return tevent_req_simple_recv_ntstatus(req);
527 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
531 TALLOC_CTX *frame = talloc_stackframe();
532 struct event_context *ev = NULL;
533 struct tevent_req *req = NULL;
534 NTSTATUS status = NT_STATUS_OK;
536 if (cli_has_async_calls(cli)) {
538 * Can't use sync call while an async call is in flight
540 status = NT_STATUS_INVALID_PARAMETER;
544 ev = event_context_init(frame);
546 status = NT_STATUS_NO_MEMORY;
550 req = cli_posix_hardlink_send(frame,
556 status = NT_STATUS_NO_MEMORY;
560 if (!tevent_req_poll(req, ev)) {
561 status = map_nt_error_from_unix(errno);
565 status = cli_posix_hardlink_recv(req);
572 /****************************************************************************
573 Do a POSIX getfacl (UNIX extensions).
574 ****************************************************************************/
576 struct getfacl_state {
581 static void cli_posix_getfacl_done(struct tevent_req *subreq);
583 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
584 struct event_context *ev,
585 struct cli_state *cli,
588 struct tevent_req *req = NULL, *subreq = NULL;
589 struct getfacl_state *state = NULL;
591 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
595 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
597 if (tevent_req_nomem(subreq, req)) {
598 return tevent_req_post(req, ev);
600 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
604 static void cli_posix_getfacl_done(struct tevent_req *subreq)
606 struct tevent_req *req = tevent_req_callback_data(
607 subreq, struct tevent_req);
608 struct getfacl_state *state = tevent_req_data(
609 req, struct getfacl_state);
612 status = cli_qpathinfo_recv(subreq, state, &state->data,
615 if (tevent_req_nterror(req, status)) {
618 tevent_req_done(req);
621 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
626 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
629 if (tevent_req_is_nterror(req, &status)) {
632 *prb_size = (size_t)state->num_data;
633 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
637 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
643 TALLOC_CTX *frame = talloc_stackframe();
644 struct event_context *ev = NULL;
645 struct tevent_req *req = NULL;
646 NTSTATUS status = NT_STATUS_OK;
648 if (cli_has_async_calls(cli)) {
650 * Can't use sync call while an async call is in flight
652 status = NT_STATUS_INVALID_PARAMETER;
656 ev = event_context_init(frame);
658 status = NT_STATUS_NO_MEMORY;
662 req = cli_posix_getfacl_send(frame,
667 status = NT_STATUS_NO_MEMORY;
671 if (!tevent_req_poll(req, ev)) {
672 status = map_nt_error_from_unix(errno);
676 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
683 /****************************************************************************
684 Stat a file (UNIX extensions).
685 ****************************************************************************/
692 static void cli_posix_stat_done(struct tevent_req *subreq);
694 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
695 struct event_context *ev,
696 struct cli_state *cli,
699 struct tevent_req *req = NULL, *subreq = NULL;
700 struct stat_state *state = NULL;
702 req = tevent_req_create(mem_ctx, &state, struct stat_state);
706 subreq = cli_qpathinfo_send(state, ev, cli, fname,
707 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
708 if (tevent_req_nomem(subreq, req)) {
709 return tevent_req_post(req, ev);
711 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
715 static void cli_posix_stat_done(struct tevent_req *subreq)
717 struct tevent_req *req = tevent_req_callback_data(
718 subreq, struct tevent_req);
719 struct stat_state *state = tevent_req_data(req, struct stat_state);
722 status = cli_qpathinfo_recv(subreq, state, &state->data,
725 if (tevent_req_nterror(req, status)) {
728 tevent_req_done(req);
731 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
732 SMB_STRUCT_STAT *sbuf)
734 struct stat_state *state = tevent_req_data(req, struct stat_state);
737 if (tevent_req_is_nterror(req, &status)) {
741 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
742 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
743 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
744 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
746 /* assume 512 byte blocks */
747 sbuf->st_ex_blocks /= 512;
749 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
750 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
751 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
753 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
754 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
755 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
756 #if defined(HAVE_MAKEDEV)
758 uint32_t dev_major = IVAL(state->data,60);
759 uint32_t dev_minor = IVAL(state->data,68);
760 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
763 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
764 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
765 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
770 NTSTATUS cli_posix_stat(struct cli_state *cli,
772 SMB_STRUCT_STAT *sbuf)
774 TALLOC_CTX *frame = talloc_stackframe();
775 struct event_context *ev = NULL;
776 struct tevent_req *req = NULL;
777 NTSTATUS status = NT_STATUS_OK;
779 if (cli_has_async_calls(cli)) {
781 * Can't use sync call while an async call is in flight
783 status = NT_STATUS_INVALID_PARAMETER;
787 ev = event_context_init(frame);
789 status = NT_STATUS_NO_MEMORY;
793 req = cli_posix_stat_send(frame,
798 status = NT_STATUS_NO_MEMORY;
802 if (!tevent_req_poll(req, ev)) {
803 status = map_nt_error_from_unix(errno);
807 status = cli_posix_stat_recv(req, sbuf);
814 /****************************************************************************
815 Chmod or chown a file internal (UNIX extensions).
816 ****************************************************************************/
818 struct cli_posix_chown_chmod_internal_state {
822 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
824 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
825 struct event_context *ev,
826 struct cli_state *cli,
832 struct tevent_req *req = NULL, *subreq = NULL;
833 struct cli_posix_chown_chmod_internal_state *state = NULL;
835 req = tevent_req_create(mem_ctx, &state,
836 struct cli_posix_chown_chmod_internal_state);
841 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
842 memset(&state->data[40], '\0', 60);
843 SIVAL(state->data,40,uid);
844 SIVAL(state->data,48,gid);
845 SIVAL(state->data,84,mode);
847 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
848 fname, state->data, sizeof(state->data));
849 if (tevent_req_nomem(subreq, req)) {
850 return tevent_req_post(req, ev);
852 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
857 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
859 NTSTATUS status = cli_setpathinfo_recv(subreq);
860 tevent_req_simple_finish_ntstatus(subreq, status);
863 /****************************************************************************
864 chmod a file (UNIX extensions).
865 ****************************************************************************/
867 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
868 struct event_context *ev,
869 struct cli_state *cli,
873 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
875 unix_perms_to_wire(mode),
880 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
882 return tevent_req_simple_recv_ntstatus(req);
885 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
887 TALLOC_CTX *frame = talloc_stackframe();
888 struct event_context *ev = NULL;
889 struct tevent_req *req = NULL;
890 NTSTATUS status = NT_STATUS_OK;
892 if (cli_has_async_calls(cli)) {
894 * Can't use sync call while an async call is in flight
896 status = NT_STATUS_INVALID_PARAMETER;
900 ev = event_context_init(frame);
902 status = NT_STATUS_NO_MEMORY;
906 req = cli_posix_chmod_send(frame,
912 status = NT_STATUS_NO_MEMORY;
916 if (!tevent_req_poll(req, ev)) {
917 status = map_nt_error_from_unix(errno);
921 status = cli_posix_chmod_recv(req);
928 /****************************************************************************
929 chown a file (UNIX extensions).
930 ****************************************************************************/
932 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
933 struct event_context *ev,
934 struct cli_state *cli,
939 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
946 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
948 return tevent_req_simple_recv_ntstatus(req);
951 NTSTATUS cli_posix_chown(struct cli_state *cli,
956 TALLOC_CTX *frame = talloc_stackframe();
957 struct event_context *ev = NULL;
958 struct tevent_req *req = NULL;
959 NTSTATUS status = NT_STATUS_OK;
961 if (cli_has_async_calls(cli)) {
963 * Can't use sync call while an async call is in flight
965 status = NT_STATUS_INVALID_PARAMETER;
969 ev = event_context_init(frame);
971 status = NT_STATUS_NO_MEMORY;
975 req = cli_posix_chown_send(frame,
982 status = NT_STATUS_NO_MEMORY;
986 if (!tevent_req_poll(req, ev)) {
987 status = map_nt_error_from_unix(errno);
991 status = cli_posix_chown_recv(req);
998 /****************************************************************************
1000 ****************************************************************************/
1002 static void cli_rename_done(struct tevent_req *subreq);
1004 struct cli_rename_state {
1008 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1009 struct event_context *ev,
1010 struct cli_state *cli,
1011 const char *fname_src,
1012 const char *fname_dst)
1014 struct tevent_req *req = NULL, *subreq = NULL;
1015 struct cli_rename_state *state = NULL;
1016 uint8_t additional_flags = 0;
1017 uint8_t *bytes = NULL;
1019 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1024 SSVAL(state->vwv+0, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1026 bytes = talloc_array(state, uint8_t, 1);
1027 if (tevent_req_nomem(bytes, req)) {
1028 return tevent_req_post(req, ev);
1031 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1032 strlen(fname_src)+1, NULL);
1033 if (tevent_req_nomem(bytes, req)) {
1034 return tevent_req_post(req, ev);
1037 bytes = talloc_realloc(state, bytes, uint8_t,
1038 talloc_get_size(bytes)+1);
1039 if (tevent_req_nomem(bytes, req)) {
1040 return tevent_req_post(req, ev);
1043 bytes[talloc_get_size(bytes)-1] = 4;
1044 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1045 strlen(fname_dst)+1, NULL);
1046 if (tevent_req_nomem(bytes, req)) {
1047 return tevent_req_post(req, ev);
1050 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1051 1, state->vwv, talloc_get_size(bytes), bytes);
1052 if (tevent_req_nomem(subreq, req)) {
1053 return tevent_req_post(req, ev);
1055 tevent_req_set_callback(subreq, cli_rename_done, req);
1059 static void cli_rename_done(struct tevent_req *subreq)
1061 struct tevent_req *req = tevent_req_callback_data(
1062 subreq, struct tevent_req);
1065 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1066 TALLOC_FREE(subreq);
1067 if (tevent_req_nterror(req, status)) {
1070 tevent_req_done(req);
1073 NTSTATUS cli_rename_recv(struct tevent_req *req)
1075 return tevent_req_simple_recv_ntstatus(req);
1078 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1080 TALLOC_CTX *frame = talloc_stackframe();
1081 struct event_context *ev;
1082 struct tevent_req *req;
1083 NTSTATUS status = NT_STATUS_OK;
1085 if (cli_has_async_calls(cli)) {
1087 * Can't use sync call while an async call is in flight
1089 status = NT_STATUS_INVALID_PARAMETER;
1093 ev = event_context_init(frame);
1095 status = NT_STATUS_NO_MEMORY;
1099 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1101 status = NT_STATUS_NO_MEMORY;
1105 if (!tevent_req_poll(req, ev)) {
1106 status = map_nt_error_from_unix(errno);
1110 status = cli_rename_recv(req);
1117 /****************************************************************************
1119 ****************************************************************************/
1121 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1123 struct cli_ntrename_internal_state {
1127 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1128 struct event_context *ev,
1129 struct cli_state *cli,
1130 const char *fname_src,
1131 const char *fname_dst,
1132 uint16_t rename_flag)
1134 struct tevent_req *req = NULL, *subreq = NULL;
1135 struct cli_ntrename_internal_state *state = NULL;
1136 uint8_t additional_flags = 0;
1137 uint8_t *bytes = NULL;
1139 req = tevent_req_create(mem_ctx, &state,
1140 struct cli_ntrename_internal_state);
1145 SSVAL(state->vwv+0, 0 ,FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
1146 SSVAL(state->vwv+1, 0, rename_flag);
1148 bytes = talloc_array(state, uint8_t, 1);
1149 if (tevent_req_nomem(bytes, req)) {
1150 return tevent_req_post(req, ev);
1153 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1154 strlen(fname_src)+1, NULL);
1155 if (tevent_req_nomem(bytes, req)) {
1156 return tevent_req_post(req, ev);
1159 bytes = talloc_realloc(state, bytes, uint8_t,
1160 talloc_get_size(bytes)+1);
1161 if (tevent_req_nomem(bytes, req)) {
1162 return tevent_req_post(req, ev);
1165 bytes[talloc_get_size(bytes)-1] = 4;
1166 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1167 strlen(fname_dst)+1, NULL);
1168 if (tevent_req_nomem(bytes, req)) {
1169 return tevent_req_post(req, ev);
1172 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1173 4, state->vwv, talloc_get_size(bytes), bytes);
1174 if (tevent_req_nomem(subreq, req)) {
1175 return tevent_req_post(req, ev);
1177 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1181 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1183 struct tevent_req *req = tevent_req_callback_data(
1184 subreq, struct tevent_req);
1187 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1188 TALLOC_FREE(subreq);
1189 if (tevent_req_nterror(req, status)) {
1192 tevent_req_done(req);
1195 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1197 return tevent_req_simple_recv_ntstatus(req);
1200 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1201 struct event_context *ev,
1202 struct cli_state *cli,
1203 const char *fname_src,
1204 const char *fname_dst)
1206 return cli_ntrename_internal_send(mem_ctx,
1211 RENAME_FLAG_RENAME);
1214 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1216 return cli_ntrename_internal_recv(req);
1219 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1221 TALLOC_CTX *frame = talloc_stackframe();
1222 struct event_context *ev;
1223 struct tevent_req *req;
1224 NTSTATUS status = NT_STATUS_OK;
1226 if (cli_has_async_calls(cli)) {
1228 * Can't use sync call while an async call is in flight
1230 status = NT_STATUS_INVALID_PARAMETER;
1234 ev = event_context_init(frame);
1236 status = NT_STATUS_NO_MEMORY;
1240 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1242 status = NT_STATUS_NO_MEMORY;
1246 if (!tevent_req_poll(req, ev)) {
1247 status = map_nt_error_from_unix(errno);
1251 status = cli_ntrename_recv(req);
1258 /****************************************************************************
1260 ****************************************************************************/
1262 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1263 struct event_context *ev,
1264 struct cli_state *cli,
1265 const char *fname_src,
1266 const char *fname_dst)
1268 return cli_ntrename_internal_send(mem_ctx,
1273 RENAME_FLAG_HARD_LINK);
1276 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1278 return cli_ntrename_internal_recv(req);
1281 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1283 TALLOC_CTX *frame = talloc_stackframe();
1284 struct event_context *ev;
1285 struct tevent_req *req;
1286 NTSTATUS status = NT_STATUS_OK;
1288 if (cli_has_async_calls(cli)) {
1290 * Can't use sync call while an async call is in flight
1292 status = NT_STATUS_INVALID_PARAMETER;
1296 ev = event_context_init(frame);
1298 status = NT_STATUS_NO_MEMORY;
1302 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1304 status = NT_STATUS_NO_MEMORY;
1308 if (!tevent_req_poll(req, ev)) {
1309 status = map_nt_error_from_unix(errno);
1313 status = cli_nt_hardlink_recv(req);
1320 /****************************************************************************
1322 ****************************************************************************/
1324 static void cli_unlink_done(struct tevent_req *subreq);
1326 struct cli_unlink_state {
1330 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1331 struct event_context *ev,
1332 struct cli_state *cli,
1334 uint16_t mayhave_attrs)
1336 struct tevent_req *req = NULL, *subreq = NULL;
1337 struct cli_unlink_state *state = NULL;
1338 uint8_t additional_flags = 0;
1339 uint8_t *bytes = NULL;
1341 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1346 SSVAL(state->vwv+0, 0, mayhave_attrs);
1348 bytes = talloc_array(state, uint8_t, 1);
1349 if (tevent_req_nomem(bytes, req)) {
1350 return tevent_req_post(req, ev);
1353 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1354 strlen(fname)+1, NULL);
1356 if (tevent_req_nomem(bytes, req)) {
1357 return tevent_req_post(req, ev);
1360 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1361 1, state->vwv, talloc_get_size(bytes), bytes);
1362 if (tevent_req_nomem(subreq, req)) {
1363 return tevent_req_post(req, ev);
1365 tevent_req_set_callback(subreq, cli_unlink_done, req);
1369 static void cli_unlink_done(struct tevent_req *subreq)
1371 struct tevent_req *req = tevent_req_callback_data(
1372 subreq, struct tevent_req);
1375 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1376 TALLOC_FREE(subreq);
1377 if (tevent_req_nterror(req, status)) {
1380 tevent_req_done(req);
1383 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1385 return tevent_req_simple_recv_ntstatus(req);
1388 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1390 TALLOC_CTX *frame = talloc_stackframe();
1391 struct event_context *ev;
1392 struct tevent_req *req;
1393 NTSTATUS status = NT_STATUS_OK;
1395 if (cli_has_async_calls(cli)) {
1397 * Can't use sync call while an async call is in flight
1399 status = NT_STATUS_INVALID_PARAMETER;
1403 ev = event_context_init(frame);
1405 status = NT_STATUS_NO_MEMORY;
1409 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1411 status = NT_STATUS_NO_MEMORY;
1415 if (!tevent_req_poll(req, ev)) {
1416 status = map_nt_error_from_unix(errno);
1420 status = cli_unlink_recv(req);
1427 /****************************************************************************
1429 ****************************************************************************/
1431 static void cli_mkdir_done(struct tevent_req *subreq);
1433 struct cli_mkdir_state {
1437 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1438 struct event_context *ev,
1439 struct cli_state *cli,
1442 struct tevent_req *req = NULL, *subreq = NULL;
1443 struct cli_mkdir_state *state = NULL;
1444 uint8_t additional_flags = 0;
1445 uint8_t *bytes = NULL;
1447 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1452 bytes = talloc_array(state, uint8_t, 1);
1453 if (tevent_req_nomem(bytes, req)) {
1454 return tevent_req_post(req, ev);
1457 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1458 strlen(dname)+1, NULL);
1460 if (tevent_req_nomem(bytes, req)) {
1461 return tevent_req_post(req, ev);
1464 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1465 0, NULL, talloc_get_size(bytes), bytes);
1466 if (tevent_req_nomem(subreq, req)) {
1467 return tevent_req_post(req, ev);
1469 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1473 static void cli_mkdir_done(struct tevent_req *subreq)
1475 struct tevent_req *req = tevent_req_callback_data(
1476 subreq, struct tevent_req);
1479 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1480 TALLOC_FREE(subreq);
1481 if (tevent_req_nterror(req, status)) {
1484 tevent_req_done(req);
1487 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1489 return tevent_req_simple_recv_ntstatus(req);
1492 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1494 TALLOC_CTX *frame = talloc_stackframe();
1495 struct event_context *ev;
1496 struct tevent_req *req;
1497 NTSTATUS status = NT_STATUS_OK;
1499 if (cli_has_async_calls(cli)) {
1501 * Can't use sync call while an async call is in flight
1503 status = NT_STATUS_INVALID_PARAMETER;
1507 ev = event_context_init(frame);
1509 status = NT_STATUS_NO_MEMORY;
1513 req = cli_mkdir_send(frame, ev, cli, dname);
1515 status = NT_STATUS_NO_MEMORY;
1519 if (!tevent_req_poll(req, ev)) {
1520 status = map_nt_error_from_unix(errno);
1524 status = cli_mkdir_recv(req);
1531 /****************************************************************************
1533 ****************************************************************************/
1535 static void cli_rmdir_done(struct tevent_req *subreq);
1537 struct cli_rmdir_state {
1541 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1542 struct event_context *ev,
1543 struct cli_state *cli,
1546 struct tevent_req *req = NULL, *subreq = NULL;
1547 struct cli_rmdir_state *state = NULL;
1548 uint8_t additional_flags = 0;
1549 uint8_t *bytes = NULL;
1551 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1556 bytes = talloc_array(state, uint8_t, 1);
1557 if (tevent_req_nomem(bytes, req)) {
1558 return tevent_req_post(req, ev);
1561 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1562 strlen(dname)+1, NULL);
1564 if (tevent_req_nomem(bytes, req)) {
1565 return tevent_req_post(req, ev);
1568 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1569 0, NULL, talloc_get_size(bytes), bytes);
1570 if (tevent_req_nomem(subreq, req)) {
1571 return tevent_req_post(req, ev);
1573 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1577 static void cli_rmdir_done(struct tevent_req *subreq)
1579 struct tevent_req *req = tevent_req_callback_data(
1580 subreq, struct tevent_req);
1583 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1584 TALLOC_FREE(subreq);
1585 if (tevent_req_nterror(req, status)) {
1588 tevent_req_done(req);
1591 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1593 return tevent_req_simple_recv_ntstatus(req);
1596 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1598 TALLOC_CTX *frame = talloc_stackframe();
1599 struct event_context *ev;
1600 struct tevent_req *req;
1601 NTSTATUS status = NT_STATUS_OK;
1603 if (cli_has_async_calls(cli)) {
1605 * Can't use sync call while an async call is in flight
1607 status = NT_STATUS_INVALID_PARAMETER;
1611 ev = event_context_init(frame);
1613 status = NT_STATUS_NO_MEMORY;
1617 req = cli_rmdir_send(frame, ev, cli, dname);
1619 status = NT_STATUS_NO_MEMORY;
1623 if (!tevent_req_poll(req, ev)) {
1624 status = map_nt_error_from_unix(errno);
1628 status = cli_rmdir_recv(req);
1635 /****************************************************************************
1636 Set or clear the delete on close flag.
1637 ****************************************************************************/
1645 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1647 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1648 NULL, 0, NULL, NULL, 0, NULL);
1649 tevent_req_simple_finish_ntstatus(subreq, status);
1652 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1653 struct event_context *ev,
1654 struct cli_state *cli,
1658 struct tevent_req *req = NULL, *subreq = NULL;
1659 struct doc_state *state = NULL;
1661 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1666 /* Setup setup word. */
1667 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1669 /* Setup param array. */
1670 SSVAL(state->param,0,fnum);
1671 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1673 /* Setup data array. */
1674 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1676 subreq = cli_trans_send(state, /* mem ctx. */
1677 ev, /* event ctx. */
1678 cli, /* cli_state. */
1679 SMBtrans2, /* cmd. */
1680 NULL, /* pipe name. */
1684 &state->setup, /* setup. */
1685 1, /* num setup uint16_t words. */
1686 0, /* max returned setup. */
1687 state->param, /* param. */
1689 2, /* max returned param. */
1690 state->data, /* data. */
1692 0); /* max returned data. */
1694 if (tevent_req_nomem(subreq, req)) {
1695 return tevent_req_post(req, ev);
1697 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1701 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1703 return tevent_req_simple_recv_ntstatus(req);
1706 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1708 TALLOC_CTX *frame = talloc_stackframe();
1709 struct event_context *ev = NULL;
1710 struct tevent_req *req = NULL;
1711 NTSTATUS status = NT_STATUS_OK;
1713 if (cli_has_async_calls(cli)) {
1715 * Can't use sync call while an async call is in flight
1717 status = NT_STATUS_INVALID_PARAMETER;
1721 ev = event_context_init(frame);
1723 status = NT_STATUS_NO_MEMORY;
1727 req = cli_nt_delete_on_close_send(frame,
1733 status = NT_STATUS_NO_MEMORY;
1737 if (!tevent_req_poll(req, ev)) {
1738 status = map_nt_error_from_unix(errno);
1742 status = cli_nt_delete_on_close_recv(req);
1749 struct cli_ntcreate_state {
1754 static void cli_ntcreate_done(struct tevent_req *subreq);
1756 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1757 struct event_context *ev,
1758 struct cli_state *cli,
1760 uint32_t CreatFlags,
1761 uint32_t DesiredAccess,
1762 uint32_t FileAttributes,
1763 uint32_t ShareAccess,
1764 uint32_t CreateDisposition,
1765 uint32_t CreateOptions,
1766 uint8_t SecurityFlags)
1768 struct tevent_req *req, *subreq;
1769 struct cli_ntcreate_state *state;
1772 size_t converted_len;
1774 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1781 SCVAL(vwv+0, 0, 0xFF);
1786 if (cli->use_oplocks) {
1787 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1789 SIVAL(vwv+3, 1, CreatFlags);
1790 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1791 SIVAL(vwv+7, 1, DesiredAccess);
1792 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1793 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1794 SIVAL(vwv+13, 1, FileAttributes);
1795 SIVAL(vwv+15, 1, ShareAccess);
1796 SIVAL(vwv+17, 1, CreateDisposition);
1797 SIVAL(vwv+19, 1, CreateOptions);
1798 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1799 SCVAL(vwv+23, 1, SecurityFlags);
1801 bytes = talloc_array(state, uint8_t, 0);
1802 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1803 fname, strlen(fname)+1,
1806 /* sigh. this copes with broken netapp filer behaviour */
1807 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1809 if (tevent_req_nomem(bytes, req)) {
1810 return tevent_req_post(req, ev);
1813 SSVAL(vwv+2, 1, converted_len);
1815 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1816 talloc_get_size(bytes), bytes);
1817 if (tevent_req_nomem(subreq, req)) {
1818 return tevent_req_post(req, ev);
1820 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1824 static void cli_ntcreate_done(struct tevent_req *subreq)
1826 struct tevent_req *req = tevent_req_callback_data(
1827 subreq, struct tevent_req);
1828 struct cli_ntcreate_state *state = tevent_req_data(
1829 req, struct cli_ntcreate_state);
1837 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1838 &num_bytes, &bytes);
1839 TALLOC_FREE(subreq);
1840 if (tevent_req_nterror(req, status)) {
1843 state->fnum = SVAL(vwv+2, 1);
1844 tevent_req_done(req);
1847 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1849 struct cli_ntcreate_state *state = tevent_req_data(
1850 req, struct cli_ntcreate_state);
1853 if (tevent_req_is_nterror(req, &status)) {
1856 *pfnum = state->fnum;
1857 return NT_STATUS_OK;
1860 NTSTATUS cli_ntcreate(struct cli_state *cli,
1862 uint32_t CreatFlags,
1863 uint32_t DesiredAccess,
1864 uint32_t FileAttributes,
1865 uint32_t ShareAccess,
1866 uint32_t CreateDisposition,
1867 uint32_t CreateOptions,
1868 uint8_t SecurityFlags,
1871 TALLOC_CTX *frame = talloc_stackframe();
1872 struct event_context *ev;
1873 struct tevent_req *req;
1874 NTSTATUS status = NT_STATUS_OK;
1876 if (cli_has_async_calls(cli)) {
1878 * Can't use sync call while an async call is in flight
1880 status = NT_STATUS_INVALID_PARAMETER;
1884 ev = event_context_init(frame);
1886 status = NT_STATUS_NO_MEMORY;
1890 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1891 DesiredAccess, FileAttributes, ShareAccess,
1892 CreateDisposition, CreateOptions,
1895 status = NT_STATUS_NO_MEMORY;
1899 if (!tevent_req_poll(req, ev)) {
1900 status = map_nt_error_from_unix(errno);
1904 status = cli_ntcreate_recv(req, pfid);
1910 struct cli_nttrans_create_state {
1914 static void cli_nttrans_create_done(struct tevent_req *subreq);
1916 struct tevent_req *cli_nttrans_create_send(TALLOC_CTX *mem_ctx,
1917 struct event_context *ev,
1918 struct cli_state *cli,
1920 uint32_t CreatFlags,
1921 uint32_t DesiredAccess,
1922 uint32_t FileAttributes,
1923 uint32_t ShareAccess,
1924 uint32_t CreateDisposition,
1925 uint32_t CreateOptions,
1926 uint8_t SecurityFlags,
1927 struct security_descriptor *secdesc,
1928 struct ea_struct *eas,
1931 struct tevent_req *req, *subreq;
1932 struct cli_nttrans_create_state *state;
1934 uint8_t *secdesc_buf;
1937 size_t converted_len;
1939 req = tevent_req_create(mem_ctx,
1940 &state, struct cli_nttrans_create_state);
1945 if (secdesc != NULL) {
1946 status = marshall_sec_desc(talloc_tos(), secdesc,
1947 &secdesc_buf, &secdesc_len);
1948 if (tevent_req_nterror(req, status)) {
1949 DEBUG(10, ("marshall_sec_desc failed: %s\n",
1950 nt_errstr(status)));
1951 return tevent_req_post(req, ev);
1962 tevent_req_nterror(req, NT_STATUS_NOT_IMPLEMENTED);
1963 return tevent_req_post(req, ev);
1966 param = talloc_array(state, uint8_t, 53);
1967 if (tevent_req_nomem(param, req)) {
1968 return tevent_req_post(req, ev);
1971 param = trans2_bytes_push_str(param, cli_ucs2(cli),
1972 fname, strlen(fname),
1974 if (tevent_req_nomem(param, req)) {
1975 return tevent_req_post(req, ev);
1978 SIVAL(param, 0, CreatFlags);
1979 SIVAL(param, 4, 0x0); /* RootDirectoryFid */
1980 SIVAL(param, 8, DesiredAccess);
1981 SIVAL(param, 12, 0x0); /* AllocationSize */
1982 SIVAL(param, 16, 0x0); /* AllocationSize */
1983 SIVAL(param, 20, FileAttributes);
1984 SIVAL(param, 24, ShareAccess);
1985 SIVAL(param, 28, CreateDisposition);
1986 SIVAL(param, 32, CreateOptions);
1987 SIVAL(param, 36, secdesc_len);
1988 SIVAL(param, 40, 0); /* EA length*/
1989 SIVAL(param, 44, converted_len);
1990 SIVAL(param, 48, 0x02); /* ImpersonationLevel */
1991 SCVAL(param, 52, SecurityFlags);
1993 subreq = cli_trans_send(state, ev, cli, SMBnttrans,
1994 NULL, -1, /* name, fid */
1995 NT_TRANSACT_CREATE, 0,
1996 NULL, 0, 0, /* setup */
1997 param, talloc_get_size(param), 128, /* param */
1998 secdesc_buf, secdesc_len, 0); /* data */
1999 if (tevent_req_nomem(subreq, req)) {
2000 return tevent_req_post(req, ev);
2002 tevent_req_set_callback(subreq, cli_nttrans_create_done, req);
2006 static void cli_nttrans_create_done(struct tevent_req *subreq)
2008 struct tevent_req *req = tevent_req_callback_data(
2009 subreq, struct tevent_req);
2010 struct cli_nttrans_create_state *state = tevent_req_data(
2011 req, struct cli_nttrans_create_state);
2016 status = cli_trans_recv(subreq, talloc_tos(), NULL,
2017 NULL, 0, NULL, /* rsetup */
2018 ¶m, 69, &num_param,
2020 if (tevent_req_nterror(req, status)) {
2023 state->fnum = SVAL(param, 2);
2025 tevent_req_done(req);
2028 NTSTATUS cli_nttrans_create_recv(struct tevent_req *req, uint16_t *fnum)
2030 struct cli_nttrans_create_state *state = tevent_req_data(
2031 req, struct cli_nttrans_create_state);
2034 if (tevent_req_is_nterror(req, &status)) {
2037 *fnum = state->fnum;
2038 return NT_STATUS_OK;
2041 NTSTATUS cli_nttrans_create(struct cli_state *cli,
2043 uint32_t CreatFlags,
2044 uint32_t DesiredAccess,
2045 uint32_t FileAttributes,
2046 uint32_t ShareAccess,
2047 uint32_t CreateDisposition,
2048 uint32_t CreateOptions,
2049 uint8_t SecurityFlags,
2050 struct security_descriptor *secdesc,
2051 struct ea_struct *eas,
2055 TALLOC_CTX *frame = talloc_stackframe();
2056 struct event_context *ev;
2057 struct tevent_req *req;
2058 NTSTATUS status = NT_STATUS_NO_MEMORY;
2060 if (cli_has_async_calls(cli)) {
2062 * Can't use sync call while an async call is in flight
2064 status = NT_STATUS_INVALID_PARAMETER;
2067 ev = event_context_init(frame);
2071 req = cli_nttrans_create_send(frame, ev, cli, fname, CreatFlags,
2072 DesiredAccess, FileAttributes,
2073 ShareAccess, CreateDisposition,
2074 CreateOptions, SecurityFlags,
2075 secdesc, eas, num_eas);
2079 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
2082 status = cli_nttrans_create_recv(req, pfid);
2088 /****************************************************************************
2090 WARNING: if you open with O_WRONLY then getattrE won't work!
2091 ****************************************************************************/
2093 struct cli_open_state {
2099 static void cli_open_done(struct tevent_req *subreq);
2101 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2102 struct event_context *ev,
2103 struct cli_state *cli, const char *fname,
2104 int flags, int share_mode,
2105 struct tevent_req **psmbreq)
2107 struct tevent_req *req, *subreq;
2108 struct cli_open_state *state;
2110 unsigned accessmode;
2111 uint8_t additional_flags;
2114 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2120 if (flags & O_CREAT) {
2123 if (!(flags & O_EXCL)) {
2124 if (flags & O_TRUNC)
2130 accessmode = (share_mode<<4);
2132 if ((flags & O_ACCMODE) == O_RDWR) {
2134 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2139 if ((flags & O_SYNC) == O_SYNC) {
2140 accessmode |= (1<<14);
2144 if (share_mode == DENY_FCB) {
2148 SCVAL(state->vwv + 0, 0, 0xFF);
2149 SCVAL(state->vwv + 0, 1, 0);
2150 SSVAL(state->vwv + 1, 0, 0);
2151 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2152 SSVAL(state->vwv + 3, 0, accessmode);
2153 SSVAL(state->vwv + 4, 0, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
2154 SSVAL(state->vwv + 5, 0, 0);
2155 SIVAL(state->vwv + 6, 0, 0);
2156 SSVAL(state->vwv + 8, 0, openfn);
2157 SIVAL(state->vwv + 9, 0, 0);
2158 SIVAL(state->vwv + 11, 0, 0);
2159 SIVAL(state->vwv + 13, 0, 0);
2161 additional_flags = 0;
2163 if (cli->use_oplocks) {
2164 /* if using oplocks then ask for a batch oplock via
2165 core and extended methods */
2167 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2168 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2171 bytes = talloc_array(state, uint8_t, 0);
2172 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2173 strlen(fname)+1, NULL);
2175 if (tevent_req_nomem(bytes, req)) {
2176 return tevent_req_post(req, ev);
2179 state->bytes.iov_base = (void *)bytes;
2180 state->bytes.iov_len = talloc_get_size(bytes);
2182 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2183 15, state->vwv, 1, &state->bytes);
2184 if (subreq == NULL) {
2188 tevent_req_set_callback(subreq, cli_open_done, req);
2193 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2194 struct cli_state *cli, const char *fname,
2195 int flags, int share_mode)
2197 struct tevent_req *req, *subreq;
2200 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2206 status = cli_smb_req_send(subreq);
2207 if (tevent_req_nterror(req, status)) {
2208 return tevent_req_post(req, ev);
2213 static void cli_open_done(struct tevent_req *subreq)
2215 struct tevent_req *req = tevent_req_callback_data(
2216 subreq, struct tevent_req);
2217 struct cli_open_state *state = tevent_req_data(
2218 req, struct cli_open_state);
2224 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2226 TALLOC_FREE(subreq);
2227 if (tevent_req_nterror(req, status)) {
2230 state->fnum = SVAL(vwv+2, 0);
2231 tevent_req_done(req);
2234 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2236 struct cli_open_state *state = tevent_req_data(
2237 req, struct cli_open_state);
2240 if (tevent_req_is_nterror(req, &status)) {
2243 *pfnum = state->fnum;
2244 return NT_STATUS_OK;
2247 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2248 int share_mode, uint16_t *pfnum)
2250 TALLOC_CTX *frame = talloc_stackframe();
2251 struct event_context *ev;
2252 struct tevent_req *req;
2253 NTSTATUS status = NT_STATUS_OK;
2255 if (cli_has_async_calls(cli)) {
2257 * Can't use sync call while an async call is in flight
2259 status = NT_STATUS_INVALID_PARAMETER;
2263 ev = event_context_init(frame);
2265 status = NT_STATUS_NO_MEMORY;
2269 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2271 status = NT_STATUS_NO_MEMORY;
2275 if (!tevent_req_poll(req, ev)) {
2276 status = map_nt_error_from_unix(errno);
2280 status = cli_open_recv(req, pfnum);
2286 /****************************************************************************
2288 ****************************************************************************/
2290 struct cli_close_state {
2294 static void cli_close_done(struct tevent_req *subreq);
2296 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2297 struct event_context *ev,
2298 struct cli_state *cli,
2300 struct tevent_req **psubreq)
2302 struct tevent_req *req, *subreq;
2303 struct cli_close_state *state;
2305 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2310 SSVAL(state->vwv+0, 0, fnum);
2311 SIVALS(state->vwv+1, 0, -1);
2313 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2315 if (subreq == NULL) {
2319 tevent_req_set_callback(subreq, cli_close_done, req);
2324 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2325 struct event_context *ev,
2326 struct cli_state *cli,
2329 struct tevent_req *req, *subreq;
2332 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2337 status = cli_smb_req_send(subreq);
2338 if (tevent_req_nterror(req, status)) {
2339 return tevent_req_post(req, ev);
2344 static void cli_close_done(struct tevent_req *subreq)
2346 struct tevent_req *req = tevent_req_callback_data(
2347 subreq, struct tevent_req);
2350 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2351 TALLOC_FREE(subreq);
2352 if (tevent_req_nterror(req, status)) {
2355 tevent_req_done(req);
2358 NTSTATUS cli_close_recv(struct tevent_req *req)
2360 return tevent_req_simple_recv_ntstatus(req);
2363 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2365 TALLOC_CTX *frame = talloc_stackframe();
2366 struct event_context *ev;
2367 struct tevent_req *req;
2368 NTSTATUS status = NT_STATUS_OK;
2370 if (cli_has_async_calls(cli)) {
2372 * Can't use sync call while an async call is in flight
2374 status = NT_STATUS_INVALID_PARAMETER;
2378 ev = event_context_init(frame);
2380 status = NT_STATUS_NO_MEMORY;
2384 req = cli_close_send(frame, ev, cli, fnum);
2386 status = NT_STATUS_NO_MEMORY;
2390 if (!tevent_req_poll(req, ev)) {
2391 status = map_nt_error_from_unix(errno);
2395 status = cli_close_recv(req);
2401 /****************************************************************************
2402 Truncate a file to a specified size
2403 ****************************************************************************/
2405 struct ftrunc_state {
2411 static void cli_ftruncate_done(struct tevent_req *subreq)
2413 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2414 NULL, 0, NULL, NULL, 0, NULL);
2415 tevent_req_simple_finish_ntstatus(subreq, status);
2418 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2419 struct event_context *ev,
2420 struct cli_state *cli,
2424 struct tevent_req *req = NULL, *subreq = NULL;
2425 struct ftrunc_state *state = NULL;
2427 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2432 /* Setup setup word. */
2433 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2435 /* Setup param array. */
2436 SSVAL(state->param,0,fnum);
2437 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2438 SSVAL(state->param,4,0);
2440 /* Setup data array. */
2441 SBVAL(state->data, 0, size);
2443 subreq = cli_trans_send(state, /* mem ctx. */
2444 ev, /* event ctx. */
2445 cli, /* cli_state. */
2446 SMBtrans2, /* cmd. */
2447 NULL, /* pipe name. */
2451 &state->setup, /* setup. */
2452 1, /* num setup uint16_t words. */
2453 0, /* max returned setup. */
2454 state->param, /* param. */
2456 2, /* max returned param. */
2457 state->data, /* data. */
2459 0); /* max returned data. */
2461 if (tevent_req_nomem(subreq, req)) {
2462 return tevent_req_post(req, ev);
2464 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2468 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2470 return tevent_req_simple_recv_ntstatus(req);
2473 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2475 TALLOC_CTX *frame = talloc_stackframe();
2476 struct event_context *ev = NULL;
2477 struct tevent_req *req = NULL;
2478 NTSTATUS status = NT_STATUS_OK;
2480 if (cli_has_async_calls(cli)) {
2482 * Can't use sync call while an async call is in flight
2484 status = NT_STATUS_INVALID_PARAMETER;
2488 ev = event_context_init(frame);
2490 status = NT_STATUS_NO_MEMORY;
2494 req = cli_ftruncate_send(frame,
2500 status = NT_STATUS_NO_MEMORY;
2504 if (!tevent_req_poll(req, ev)) {
2505 status = map_nt_error_from_unix(errno);
2509 status = cli_ftruncate_recv(req);
2516 /****************************************************************************
2517 send a lock with a specified locktype
2518 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2519 ****************************************************************************/
2521 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2522 uint32_t offset, uint32_t len,
2523 int timeout, unsigned char locktype)
2530 SCVAL(vwv + 0, 0, 0xff);
2531 SCVAL(vwv + 0, 1, 0);
2532 SSVAL(vwv + 1, 0, 0);
2533 SSVAL(vwv + 2, 0, fnum);
2534 SCVAL(vwv + 3, 0, locktype);
2535 SCVAL(vwv + 3, 1, 0);
2536 SIVALS(vwv + 4, 0, timeout);
2537 SSVAL(vwv + 6, 0, 0);
2538 SSVAL(vwv + 7, 0, 1);
2540 SSVAL(bytes, 0, cli->pid);
2541 SIVAL(bytes, 2, offset);
2542 SIVAL(bytes, 6, len);
2544 saved_timeout = cli->timeout;
2547 cli->timeout = (timeout == -1)
2548 ? 0x7FFFFFFF : (timeout + 2*1000);
2551 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2552 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2554 cli->timeout = saved_timeout;
2559 /****************************************************************************
2561 note that timeout is in units of 2 milliseconds
2562 ****************************************************************************/
2564 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2565 uint32_t offset, uint32_t len, int timeout,
2566 enum brl_type lock_type)
2570 status = cli_locktype(cli, fnum, offset, len, timeout,
2571 (lock_type == READ_LOCK? 1 : 0));
2572 return NT_STATUS_IS_OK(status);
2575 /****************************************************************************
2577 ****************************************************************************/
2579 struct cli_unlock_state {
2584 static void cli_unlock_done(struct tevent_req *subreq);
2586 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2587 struct event_context *ev,
2588 struct cli_state *cli,
2594 struct tevent_req *req = NULL, *subreq = NULL;
2595 struct cli_unlock_state *state = NULL;
2596 uint8_t additional_flags = 0;
2598 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2603 SCVAL(state->vwv+0, 0, 0xFF);
2604 SSVAL(state->vwv+2, 0, fnum);
2605 SCVAL(state->vwv+3, 0, 0);
2606 SIVALS(state->vwv+4, 0, 0);
2607 SSVAL(state->vwv+6, 0, 1);
2608 SSVAL(state->vwv+7, 0, 0);
2610 SSVAL(state->data, 0, cli->pid);
2611 SIVAL(state->data, 2, offset);
2612 SIVAL(state->data, 6, len);
2614 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2615 8, state->vwv, 10, state->data);
2616 if (tevent_req_nomem(subreq, req)) {
2617 return tevent_req_post(req, ev);
2619 tevent_req_set_callback(subreq, cli_unlock_done, req);
2623 static void cli_unlock_done(struct tevent_req *subreq)
2625 struct tevent_req *req = tevent_req_callback_data(
2626 subreq, struct tevent_req);
2629 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2630 TALLOC_FREE(subreq);
2631 if (tevent_req_nterror(req, status)) {
2634 tevent_req_done(req);
2637 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2639 return tevent_req_simple_recv_ntstatus(req);
2642 NTSTATUS cli_unlock(struct cli_state *cli,
2647 TALLOC_CTX *frame = talloc_stackframe();
2648 struct event_context *ev;
2649 struct tevent_req *req;
2650 NTSTATUS status = NT_STATUS_OK;
2652 if (cli_has_async_calls(cli)) {
2654 * Can't use sync call while an async call is in flight
2656 status = NT_STATUS_INVALID_PARAMETER;
2660 ev = event_context_init(frame);
2662 status = NT_STATUS_NO_MEMORY;
2666 req = cli_unlock_send(frame, ev, cli,
2669 status = NT_STATUS_NO_MEMORY;
2673 if (!tevent_req_poll(req, ev)) {
2674 status = map_nt_error_from_unix(errno);
2678 status = cli_unlock_recv(req);
2685 /****************************************************************************
2686 Lock a file with 64 bit offsets.
2687 ****************************************************************************/
2689 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2690 uint64_t offset, uint64_t len, int timeout,
2691 enum brl_type lock_type)
2695 int saved_timeout = cli->timeout;
2699 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2700 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2703 ltype = (lock_type == READ_LOCK? 1 : 0);
2704 ltype |= LOCKING_ANDX_LARGE_FILES;
2706 SCVAL(vwv + 0, 0, 0xff);
2707 SCVAL(vwv + 0, 1, 0);
2708 SSVAL(vwv + 1, 0, 0);
2709 SSVAL(vwv + 2, 0, fnum);
2710 SCVAL(vwv + 3, 0, ltype);
2711 SCVAL(vwv + 3, 1, 0);
2712 SIVALS(vwv + 4, 0, timeout);
2713 SSVAL(vwv + 6, 0, 0);
2714 SSVAL(vwv + 7, 0, 1);
2716 SIVAL(bytes, 0, cli->pid);
2717 SOFF_T_R(bytes, 4, offset);
2718 SOFF_T_R(bytes, 12, len);
2720 saved_timeout = cli->timeout;
2723 cli->timeout = (timeout == -1)
2724 ? 0x7FFFFFFF : (timeout + 2*1000);
2727 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2728 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2730 cli->timeout = saved_timeout;
2732 return NT_STATUS_IS_OK(status);
2735 /****************************************************************************
2736 Unlock a file with 64 bit offsets.
2737 ****************************************************************************/
2739 struct cli_unlock64_state {
2744 static void cli_unlock64_done(struct tevent_req *subreq);
2746 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2747 struct event_context *ev,
2748 struct cli_state *cli,
2754 struct tevent_req *req = NULL, *subreq = NULL;
2755 struct cli_unlock64_state *state = NULL;
2756 uint8_t additional_flags = 0;
2758 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2763 SCVAL(state->vwv+0, 0, 0xff);
2764 SSVAL(state->vwv+2, 0, fnum);
2765 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2766 SIVALS(state->vwv+4, 0, 0);
2767 SSVAL(state->vwv+6, 0, 1);
2768 SSVAL(state->vwv+7, 0, 0);
2770 SIVAL(state->data, 0, cli->pid);
2771 SOFF_T_R(state->data, 4, offset);
2772 SOFF_T_R(state->data, 12, len);
2774 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2775 8, state->vwv, 20, state->data);
2776 if (tevent_req_nomem(subreq, req)) {
2777 return tevent_req_post(req, ev);
2779 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2783 static void cli_unlock64_done(struct tevent_req *subreq)
2785 struct tevent_req *req = tevent_req_callback_data(
2786 subreq, struct tevent_req);
2789 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2790 TALLOC_FREE(subreq);
2791 if (tevent_req_nterror(req, status)) {
2794 tevent_req_done(req);
2797 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2799 return tevent_req_simple_recv_ntstatus(req);
2802 NTSTATUS cli_unlock64(struct cli_state *cli,
2807 TALLOC_CTX *frame = talloc_stackframe();
2808 struct event_context *ev;
2809 struct tevent_req *req;
2810 NTSTATUS status = NT_STATUS_OK;
2812 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2813 return cli_unlock(cli, fnum, offset, len);
2816 if (cli_has_async_calls(cli)) {
2818 * Can't use sync call while an async call is in flight
2820 status = NT_STATUS_INVALID_PARAMETER;
2824 ev = event_context_init(frame);
2826 status = NT_STATUS_NO_MEMORY;
2830 req = cli_unlock64_send(frame, ev, cli,
2833 status = NT_STATUS_NO_MEMORY;
2837 if (!tevent_req_poll(req, ev)) {
2838 status = map_nt_error_from_unix(errno);
2842 status = cli_unlock64_recv(req);
2849 /****************************************************************************
2850 Get/unlock a POSIX lock on a file - internal function.
2851 ****************************************************************************/
2853 struct posix_lock_state {
2856 uint8_t data[POSIX_LOCK_DATA_SIZE];
2859 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2861 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2862 NULL, 0, NULL, NULL, 0, NULL);
2863 tevent_req_simple_finish_ntstatus(subreq, status);
2866 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2867 struct event_context *ev,
2868 struct cli_state *cli,
2873 enum brl_type lock_type)
2875 struct tevent_req *req = NULL, *subreq = NULL;
2876 struct posix_lock_state *state = NULL;
2878 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2883 /* Setup setup word. */
2884 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2886 /* Setup param array. */
2887 SSVAL(&state->param, 0, fnum);
2888 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2890 /* Setup data array. */
2891 switch (lock_type) {
2893 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2894 POSIX_LOCK_TYPE_READ);
2897 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2898 POSIX_LOCK_TYPE_WRITE);
2901 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2902 POSIX_LOCK_TYPE_UNLOCK);
2909 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2910 POSIX_LOCK_FLAG_WAIT);
2912 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2913 POSIX_LOCK_FLAG_NOWAIT);
2916 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
2917 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2918 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2920 subreq = cli_trans_send(state, /* mem ctx. */
2921 ev, /* event ctx. */
2922 cli, /* cli_state. */
2923 SMBtrans2, /* cmd. */
2924 NULL, /* pipe name. */
2928 &state->setup, /* setup. */
2929 1, /* num setup uint16_t words. */
2930 0, /* max returned setup. */
2931 state->param, /* param. */
2933 2, /* max returned param. */
2934 state->data, /* data. */
2935 POSIX_LOCK_DATA_SIZE, /* num data. */
2936 0); /* max returned data. */
2938 if (tevent_req_nomem(subreq, req)) {
2939 return tevent_req_post(req, ev);
2941 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
2945 /****************************************************************************
2947 ****************************************************************************/
2949 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
2950 struct event_context *ev,
2951 struct cli_state *cli,
2956 enum brl_type lock_type)
2958 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2959 wait_lock, lock_type);
2962 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
2964 return tevent_req_simple_recv_ntstatus(req);
2967 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2968 uint64_t offset, uint64_t len,
2969 bool wait_lock, enum brl_type lock_type)
2971 TALLOC_CTX *frame = talloc_stackframe();
2972 struct event_context *ev = NULL;
2973 struct tevent_req *req = NULL;
2974 NTSTATUS status = NT_STATUS_OK;
2976 if (cli_has_async_calls(cli)) {
2978 * Can't use sync call while an async call is in flight
2980 status = NT_STATUS_INVALID_PARAMETER;
2984 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2985 status = NT_STATUS_INVALID_PARAMETER;
2989 ev = event_context_init(frame);
2991 status = NT_STATUS_NO_MEMORY;
2995 req = cli_posix_lock_send(frame,
3004 status = NT_STATUS_NO_MEMORY;
3008 if (!tevent_req_poll(req, ev)) {
3009 status = map_nt_error_from_unix(errno);
3013 status = cli_posix_lock_recv(req);
3020 /****************************************************************************
3021 POSIX Unlock a file.
3022 ****************************************************************************/
3024 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3025 struct event_context *ev,
3026 struct cli_state *cli,
3031 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3032 false, UNLOCK_LOCK);
3035 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3037 return tevent_req_simple_recv_ntstatus(req);
3040 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3042 TALLOC_CTX *frame = talloc_stackframe();
3043 struct event_context *ev = NULL;
3044 struct tevent_req *req = NULL;
3045 NTSTATUS status = NT_STATUS_OK;
3047 if (cli_has_async_calls(cli)) {
3049 * Can't use sync call while an async call is in flight
3051 status = NT_STATUS_INVALID_PARAMETER;
3055 ev = event_context_init(frame);
3057 status = NT_STATUS_NO_MEMORY;
3061 req = cli_posix_unlock_send(frame,
3068 status = NT_STATUS_NO_MEMORY;
3072 if (!tevent_req_poll(req, ev)) {
3073 status = map_nt_error_from_unix(errno);
3077 status = cli_posix_unlock_recv(req);
3084 /****************************************************************************
3085 Do a SMBgetattrE call.
3086 ****************************************************************************/
3088 static void cli_getattrE_done(struct tevent_req *subreq);
3090 struct cli_getattrE_state {
3100 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3101 struct event_context *ev,
3102 struct cli_state *cli,
3105 struct tevent_req *req = NULL, *subreq = NULL;
3106 struct cli_getattrE_state *state = NULL;
3107 uint8_t additional_flags = 0;
3109 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3114 state->zone_offset = cli->serverzone;
3115 SSVAL(state->vwv+0,0,fnum);
3117 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3118 1, state->vwv, 0, NULL);
3119 if (tevent_req_nomem(subreq, req)) {
3120 return tevent_req_post(req, ev);
3122 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3126 static void cli_getattrE_done(struct tevent_req *subreq)
3128 struct tevent_req *req = tevent_req_callback_data(
3129 subreq, struct tevent_req);
3130 struct cli_getattrE_state *state = tevent_req_data(
3131 req, struct cli_getattrE_state);
3133 uint16_t *vwv = NULL;
3137 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3139 TALLOC_FREE(subreq);
3140 if (tevent_req_nterror(req, status)) {
3144 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3145 state->attr = SVAL(vwv+10,0);
3146 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3147 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3148 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3150 tevent_req_done(req);
3153 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3156 time_t *change_time,
3157 time_t *access_time,
3160 struct cli_getattrE_state *state = tevent_req_data(
3161 req, struct cli_getattrE_state);
3164 if (tevent_req_is_nterror(req, &status)) {
3168 *attr = state->attr;
3171 *size = state->size;
3174 *change_time = state->change_time;
3177 *access_time = state->access_time;
3180 *write_time = state->write_time;
3182 return NT_STATUS_OK;
3185 NTSTATUS cli_getattrE(struct cli_state *cli,
3189 time_t *change_time,
3190 time_t *access_time,
3193 TALLOC_CTX *frame = talloc_stackframe();
3194 struct event_context *ev = NULL;
3195 struct tevent_req *req = NULL;
3196 NTSTATUS status = NT_STATUS_OK;
3198 if (cli_has_async_calls(cli)) {
3200 * Can't use sync call while an async call is in flight
3202 status = NT_STATUS_INVALID_PARAMETER;
3206 ev = event_context_init(frame);
3208 status = NT_STATUS_NO_MEMORY;
3212 req = cli_getattrE_send(frame, ev, cli, fnum);
3214 status = NT_STATUS_NO_MEMORY;
3218 if (!tevent_req_poll(req, ev)) {
3219 status = map_nt_error_from_unix(errno);
3223 status = cli_getattrE_recv(req,
3235 /****************************************************************************
3237 ****************************************************************************/
3239 static void cli_getatr_done(struct tevent_req *subreq);
3241 struct cli_getatr_state {
3248 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3249 struct event_context *ev,
3250 struct cli_state *cli,
3253 struct tevent_req *req = NULL, *subreq = NULL;
3254 struct cli_getatr_state *state = NULL;
3255 uint8_t additional_flags = 0;
3256 uint8_t *bytes = NULL;
3258 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3263 state->zone_offset = cli->serverzone;
3265 bytes = talloc_array(state, uint8_t, 1);
3266 if (tevent_req_nomem(bytes, req)) {
3267 return tevent_req_post(req, ev);
3270 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3271 strlen(fname)+1, NULL);
3273 if (tevent_req_nomem(bytes, req)) {
3274 return tevent_req_post(req, ev);
3277 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3278 0, NULL, talloc_get_size(bytes), bytes);
3279 if (tevent_req_nomem(subreq, req)) {
3280 return tevent_req_post(req, ev);
3282 tevent_req_set_callback(subreq, cli_getatr_done, req);
3286 static void cli_getatr_done(struct tevent_req *subreq)
3288 struct tevent_req *req = tevent_req_callback_data(
3289 subreq, struct tevent_req);
3290 struct cli_getatr_state *state = tevent_req_data(
3291 req, struct cli_getatr_state);
3293 uint16_t *vwv = NULL;
3297 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3299 TALLOC_FREE(subreq);
3300 if (tevent_req_nterror(req, status)) {
3304 state->attr = SVAL(vwv+0,0);
3305 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3306 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3308 tevent_req_done(req);
3311 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3316 struct cli_getatr_state *state = tevent_req_data(
3317 req, struct cli_getatr_state);
3320 if (tevent_req_is_nterror(req, &status)) {
3324 *attr = state->attr;
3327 *size = state->size;
3330 *write_time = state->write_time;
3332 return NT_STATUS_OK;
3335 NTSTATUS cli_getatr(struct cli_state *cli,
3341 TALLOC_CTX *frame = talloc_stackframe();
3342 struct event_context *ev = NULL;
3343 struct tevent_req *req = NULL;
3344 NTSTATUS status = NT_STATUS_OK;
3346 if (cli_has_async_calls(cli)) {
3348 * Can't use sync call while an async call is in flight
3350 status = NT_STATUS_INVALID_PARAMETER;
3354 ev = event_context_init(frame);
3356 status = NT_STATUS_NO_MEMORY;
3360 req = cli_getatr_send(frame, ev, cli, fname);
3362 status = NT_STATUS_NO_MEMORY;
3366 if (!tevent_req_poll(req, ev)) {
3367 status = map_nt_error_from_unix(errno);
3371 status = cli_getatr_recv(req,
3381 /****************************************************************************
3382 Do a SMBsetattrE call.
3383 ****************************************************************************/
3385 static void cli_setattrE_done(struct tevent_req *subreq);
3387 struct cli_setattrE_state {
3391 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3392 struct event_context *ev,
3393 struct cli_state *cli,
3399 struct tevent_req *req = NULL, *subreq = NULL;
3400 struct cli_setattrE_state *state = NULL;
3401 uint8_t additional_flags = 0;
3403 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3408 SSVAL(state->vwv+0, 0, fnum);
3409 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3411 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3413 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3416 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3417 7, state->vwv, 0, NULL);
3418 if (tevent_req_nomem(subreq, req)) {
3419 return tevent_req_post(req, ev);
3421 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3425 static void cli_setattrE_done(struct tevent_req *subreq)
3427 struct tevent_req *req = tevent_req_callback_data(
3428 subreq, struct tevent_req);
3431 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3432 TALLOC_FREE(subreq);
3433 if (tevent_req_nterror(req, status)) {
3436 tevent_req_done(req);
3439 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3441 return tevent_req_simple_recv_ntstatus(req);
3444 NTSTATUS cli_setattrE(struct cli_state *cli,
3450 TALLOC_CTX *frame = talloc_stackframe();
3451 struct event_context *ev = NULL;
3452 struct tevent_req *req = NULL;
3453 NTSTATUS status = NT_STATUS_OK;
3455 if (cli_has_async_calls(cli)) {
3457 * Can't use sync call while an async call is in flight
3459 status = NT_STATUS_INVALID_PARAMETER;
3463 ev = event_context_init(frame);
3465 status = NT_STATUS_NO_MEMORY;
3469 req = cli_setattrE_send(frame, ev,
3477 status = NT_STATUS_NO_MEMORY;
3481 if (!tevent_req_poll(req, ev)) {
3482 status = map_nt_error_from_unix(errno);
3486 status = cli_setattrE_recv(req);
3493 /****************************************************************************
3494 Do a SMBsetatr call.
3495 ****************************************************************************/
3497 static void cli_setatr_done(struct tevent_req *subreq);
3499 struct cli_setatr_state {
3503 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3504 struct event_context *ev,
3505 struct cli_state *cli,
3510 struct tevent_req *req = NULL, *subreq = NULL;
3511 struct cli_setatr_state *state = NULL;
3512 uint8_t additional_flags = 0;
3513 uint8_t *bytes = NULL;
3515 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3520 SSVAL(state->vwv+0, 0, attr);
3521 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
3523 bytes = talloc_array(state, uint8_t, 1);
3524 if (tevent_req_nomem(bytes, req)) {
3525 return tevent_req_post(req, ev);
3528 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3529 strlen(fname)+1, NULL);
3530 if (tevent_req_nomem(bytes, req)) {
3531 return tevent_req_post(req, ev);
3533 bytes = talloc_realloc(state, bytes, uint8_t,
3534 talloc_get_size(bytes)+1);
3535 if (tevent_req_nomem(bytes, req)) {
3536 return tevent_req_post(req, ev);
3539 bytes[talloc_get_size(bytes)-1] = 4;
3540 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3542 if (tevent_req_nomem(bytes, req)) {
3543 return tevent_req_post(req, ev);
3546 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3547 8, state->vwv, talloc_get_size(bytes), bytes);
3548 if (tevent_req_nomem(subreq, req)) {
3549 return tevent_req_post(req, ev);
3551 tevent_req_set_callback(subreq, cli_setatr_done, req);
3555 static void cli_setatr_done(struct tevent_req *subreq)
3557 struct tevent_req *req = tevent_req_callback_data(
3558 subreq, struct tevent_req);
3561 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3562 TALLOC_FREE(subreq);
3563 if (tevent_req_nterror(req, status)) {
3566 tevent_req_done(req);
3569 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3571 return tevent_req_simple_recv_ntstatus(req);
3574 NTSTATUS cli_setatr(struct cli_state *cli,
3579 TALLOC_CTX *frame = talloc_stackframe();
3580 struct event_context *ev = NULL;
3581 struct tevent_req *req = NULL;
3582 NTSTATUS status = NT_STATUS_OK;
3584 if (cli_has_async_calls(cli)) {
3586 * Can't use sync call while an async call is in flight
3588 status = NT_STATUS_INVALID_PARAMETER;
3592 ev = event_context_init(frame);
3594 status = NT_STATUS_NO_MEMORY;
3598 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3600 status = NT_STATUS_NO_MEMORY;
3604 if (!tevent_req_poll(req, ev)) {
3605 status = map_nt_error_from_unix(errno);
3609 status = cli_setatr_recv(req);
3616 /****************************************************************************
3617 Check for existance of a dir.
3618 ****************************************************************************/
3620 static void cli_chkpath_done(struct tevent_req *subreq);
3622 struct cli_chkpath_state {
3626 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3627 struct event_context *ev,
3628 struct cli_state *cli,
3631 struct tevent_req *req = NULL, *subreq = NULL;
3632 struct cli_chkpath_state *state = NULL;
3633 uint8_t additional_flags = 0;
3634 uint8_t *bytes = NULL;
3636 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3641 bytes = talloc_array(state, uint8_t, 1);
3642 if (tevent_req_nomem(bytes, req)) {
3643 return tevent_req_post(req, ev);
3646 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3647 strlen(fname)+1, NULL);
3649 if (tevent_req_nomem(bytes, req)) {
3650 return tevent_req_post(req, ev);
3653 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3654 0, NULL, talloc_get_size(bytes), bytes);
3655 if (tevent_req_nomem(subreq, req)) {
3656 return tevent_req_post(req, ev);
3658 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3662 static void cli_chkpath_done(struct tevent_req *subreq)
3664 struct tevent_req *req = tevent_req_callback_data(
3665 subreq, struct tevent_req);
3668 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3669 TALLOC_FREE(subreq);
3670 if (tevent_req_nterror(req, status)) {
3673 tevent_req_done(req);
3676 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3678 return tevent_req_simple_recv_ntstatus(req);
3681 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3683 TALLOC_CTX *frame = talloc_stackframe();
3684 struct event_context *ev = NULL;
3685 struct tevent_req *req = NULL;
3687 NTSTATUS status = NT_STATUS_OK;
3689 if (cli_has_async_calls(cli)) {
3691 * Can't use sync call while an async call is in flight
3693 status = NT_STATUS_INVALID_PARAMETER;
3697 path2 = talloc_strdup(frame, path);
3699 status = NT_STATUS_NO_MEMORY;
3702 trim_char(path2,'\0','\\');
3704 path2 = talloc_strdup(frame, "\\");
3706 status = NT_STATUS_NO_MEMORY;
3711 ev = event_context_init(frame);
3713 status = NT_STATUS_NO_MEMORY;
3717 req = cli_chkpath_send(frame, ev, cli, path2);
3719 status = NT_STATUS_NO_MEMORY;
3723 if (!tevent_req_poll(req, ev)) {
3724 status = map_nt_error_from_unix(errno);
3728 status = cli_chkpath_recv(req);
3735 /****************************************************************************
3737 ****************************************************************************/
3739 static void cli_dskattr_done(struct tevent_req *subreq);
3741 struct cli_dskattr_state {
3747 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3748 struct event_context *ev,
3749 struct cli_state *cli)
3751 struct tevent_req *req = NULL, *subreq = NULL;
3752 struct cli_dskattr_state *state = NULL;
3753 uint8_t additional_flags = 0;
3755 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3760 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3762 if (tevent_req_nomem(subreq, req)) {
3763 return tevent_req_post(req, ev);
3765 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3769 static void cli_dskattr_done(struct tevent_req *subreq)
3771 struct tevent_req *req = tevent_req_callback_data(
3772 subreq, struct tevent_req);
3773 struct cli_dskattr_state *state = tevent_req_data(
3774 req, struct cli_dskattr_state);
3776 uint16_t *vwv = NULL;
3780 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3782 TALLOC_FREE(subreq);
3783 if (tevent_req_nterror(req, status)) {
3786 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3787 state->total = SVAL(vwv+0, 0);
3788 state->avail = SVAL(vwv+3, 0);
3789 tevent_req_done(req);
3792 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3794 struct cli_dskattr_state *state = tevent_req_data(
3795 req, struct cli_dskattr_state);
3798 if (tevent_req_is_nterror(req, &status)) {
3801 *bsize = state->bsize;
3802 *total = state->total;
3803 *avail = state->avail;
3804 return NT_STATUS_OK;
3807 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3809 TALLOC_CTX *frame = talloc_stackframe();
3810 struct event_context *ev = NULL;
3811 struct tevent_req *req = NULL;
3812 NTSTATUS status = NT_STATUS_OK;
3814 if (cli_has_async_calls(cli)) {
3816 * Can't use sync call while an async call is in flight
3818 status = NT_STATUS_INVALID_PARAMETER;
3822 ev = event_context_init(frame);
3824 status = NT_STATUS_NO_MEMORY;
3828 req = cli_dskattr_send(frame, ev, cli);
3830 status = NT_STATUS_NO_MEMORY;
3834 if (!tevent_req_poll(req, ev)) {
3835 status = map_nt_error_from_unix(errno);
3839 status = cli_dskattr_recv(req, bsize, total, avail);
3846 /****************************************************************************
3847 Create and open a temporary file.
3848 ****************************************************************************/
3850 static void cli_ctemp_done(struct tevent_req *subreq);
3852 struct ctemp_state {
3858 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3859 struct event_context *ev,
3860 struct cli_state *cli,
3863 struct tevent_req *req = NULL, *subreq = NULL;
3864 struct ctemp_state *state = NULL;
3865 uint8_t additional_flags = 0;
3866 uint8_t *bytes = NULL;
3868 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3873 SSVAL(state->vwv,0,0);
3874 SIVALS(state->vwv+1,0,-1);
3876 bytes = talloc_array(state, uint8_t, 1);
3877 if (tevent_req_nomem(bytes, req)) {
3878 return tevent_req_post(req, ev);
3881 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3882 strlen(path)+1, NULL);
3883 if (tevent_req_nomem(bytes, req)) {
3884 return tevent_req_post(req, ev);
3887 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3888 3, state->vwv, talloc_get_size(bytes), bytes);
3889 if (tevent_req_nomem(subreq, req)) {
3890 return tevent_req_post(req, ev);
3892 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3896 static void cli_ctemp_done(struct tevent_req *subreq)
3898 struct tevent_req *req = tevent_req_callback_data(
3899 subreq, struct tevent_req);
3900 struct ctemp_state *state = tevent_req_data(
3901 req, struct ctemp_state);
3905 uint32_t num_bytes = 0;
3906 uint8_t *bytes = NULL;
3909 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3910 &num_bytes, &bytes);
3911 TALLOC_FREE(subreq);
3912 if (tevent_req_nterror(req, status)) {
3916 state->fnum = SVAL(vwv+0, 0);
3918 /* From W2K3, the result is just the ASCII name */
3919 if (num_bytes < 2) {
3920 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
3924 if (pull_string_talloc(state,
3931 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
3934 tevent_req_done(req);
3937 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
3942 struct ctemp_state *state = tevent_req_data(req,
3943 struct ctemp_state);
3946 if (tevent_req_is_nterror(req, &status)) {
3949 *pfnum = state->fnum;
3950 *outfile = talloc_strdup(ctx, state->ret_path);
3952 return NT_STATUS_NO_MEMORY;
3954 return NT_STATUS_OK;
3957 NTSTATUS cli_ctemp(struct cli_state *cli,
3963 TALLOC_CTX *frame = talloc_stackframe();
3964 struct event_context *ev;
3965 struct tevent_req *req;
3966 NTSTATUS status = NT_STATUS_OK;
3968 if (cli_has_async_calls(cli)) {
3970 * Can't use sync call while an async call is in flight
3972 status = NT_STATUS_INVALID_PARAMETER;
3976 ev = event_context_init(frame);
3978 status = NT_STATUS_NO_MEMORY;
3982 req = cli_ctemp_send(frame, ev, cli, path);
3984 status = NT_STATUS_NO_MEMORY;
3988 if (!tevent_req_poll(req, ev)) {
3989 status = map_nt_error_from_unix(errno);
3993 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4001 send a raw ioctl - used by the torture code
4003 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4008 SSVAL(vwv+0, 0, fnum);
4009 SSVAL(vwv+1, 0, code>>16);
4010 SSVAL(vwv+2, 0, (code&0xFFFF));
4012 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4013 NULL, 0, NULL, NULL, NULL, NULL);
4014 if (!NT_STATUS_IS_OK(status)) {
4017 *blob = data_blob_null;
4018 return NT_STATUS_OK;
4021 /*********************************************************
4022 Set an extended attribute utility fn.
4023 *********************************************************/
4025 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4026 uint8_t *param, unsigned int param_len,
4027 const char *ea_name,
4028 const char *ea_val, size_t ea_len)
4031 unsigned int data_len = 0;
4032 uint8_t *data = NULL;
4034 size_t ea_namelen = strlen(ea_name);
4037 SSVAL(setup, 0, setup_val);
4039 if (ea_namelen == 0 && ea_len == 0) {
4041 data = (uint8_t *)SMB_MALLOC(data_len);
4043 return NT_STATUS_NO_MEMORY;
4046 SIVAL(p,0,data_len);
4048 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4049 data = (uint8_t *)SMB_MALLOC(data_len);
4051 return NT_STATUS_NO_MEMORY;
4054 SIVAL(p,0,data_len);
4056 SCVAL(p, 0, 0); /* EA flags. */
4057 SCVAL(p, 1, ea_namelen);
4058 SSVAL(p, 2, ea_len);
4059 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4060 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4063 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4065 param, param_len, 2,
4066 data, data_len, cli->max_xmit,
4068 NULL, 0, NULL, /* rsetup */
4069 NULL, 0, NULL, /* rparam */
4070 NULL, 0, NULL); /* rdata */
4075 /*********************************************************
4076 Set an extended attribute on a pathname.
4077 *********************************************************/
4079 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4080 const char *ea_name, const char *ea_val,
4083 unsigned int param_len = 0;
4085 size_t srclen = 2*(strlen(path)+1);
4089 param = SMB_MALLOC_ARRAY(uint8_t, 6+srclen+2);
4091 return NT_STATUS_NO_MEMORY;
4093 memset(param, '\0', 6);
4094 SSVAL(param,0,SMB_INFO_SET_EA);
4095 p = (char *)(¶m[6]);
4097 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4098 param_len = PTR_DIFF(p, param);
4100 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4101 ea_name, ea_val, ea_len);
4106 /*********************************************************
4107 Set an extended attribute on an fnum.
4108 *********************************************************/
4110 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4111 const char *ea_name, const char *ea_val,
4116 memset(param, 0, 6);
4117 SSVAL(param,0,fnum);
4118 SSVAL(param,2,SMB_INFO_SET_EA);
4120 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4121 ea_name, ea_val, ea_len);
4124 /*********************************************************
4125 Get an extended attribute list utility fn.
4126 *********************************************************/
4128 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4130 size_t *pnum_eas, struct ea_struct **pea_list)
4132 struct ea_struct *ea_list = NULL;
4137 if (rdata_len < 4) {
4141 ea_size = (size_t)IVAL(rdata,0);
4142 if (ea_size > rdata_len) {
4147 /* No EA's present. */
4156 /* Validate the EA list and count it. */
4157 for (num_eas = 0; ea_size >= 4; num_eas++) {
4158 unsigned int ea_namelen = CVAL(p,1);
4159 unsigned int ea_valuelen = SVAL(p,2);
4160 if (ea_namelen == 0) {
4163 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4166 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4167 p += 4 + ea_namelen + 1 + ea_valuelen;
4176 *pnum_eas = num_eas;
4178 /* Caller only wants number of EA's. */
4182 ea_list = talloc_array(ctx, struct ea_struct, num_eas);
4187 ea_size = (size_t)IVAL(rdata,0);
4190 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4191 struct ea_struct *ea = &ea_list[num_eas];
4192 fstring unix_ea_name;
4193 unsigned int ea_namelen = CVAL(p,1);
4194 unsigned int ea_valuelen = SVAL(p,2);
4196 ea->flags = CVAL(p,0);
4197 unix_ea_name[0] = '\0';
4198 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4199 ea->name = talloc_strdup(ea_list, unix_ea_name);
4203 /* Ensure the value is null terminated (in case it's a string). */
4204 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4205 if (!ea->value.data) {
4209 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4211 ea->value.data[ea_valuelen] = 0;
4213 p += 4 + ea_namelen + 1 + ea_valuelen;
4216 *pea_list = ea_list;
4220 TALLOC_FREE(ea_list);
4224 /*********************************************************
4225 Get an extended attribute list from a pathname.
4226 *********************************************************/
4228 struct cli_get_ea_list_path_state {
4233 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4235 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4236 struct tevent_context *ev,
4237 struct cli_state *cli,
4240 struct tevent_req *req, *subreq;
4241 struct cli_get_ea_list_path_state *state;
4243 req = tevent_req_create(mem_ctx, &state,
4244 struct cli_get_ea_list_path_state);
4248 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4249 SMB_INFO_QUERY_ALL_EAS, 4,
4251 if (tevent_req_nomem(subreq, req)) {
4252 return tevent_req_post(req, ev);
4254 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4258 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4260 struct tevent_req *req = tevent_req_callback_data(
4261 subreq, struct tevent_req);
4262 struct cli_get_ea_list_path_state *state = tevent_req_data(
4263 req, struct cli_get_ea_list_path_state);
4266 status = cli_qpathinfo_recv(subreq, state, &state->data,
4268 TALLOC_FREE(subreq);
4269 if (tevent_req_nterror(req, status)) {
4272 tevent_req_done(req);
4275 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4276 size_t *pnum_eas, struct ea_struct **peas)
4278 struct cli_get_ea_list_path_state *state = tevent_req_data(
4279 req, struct cli_get_ea_list_path_state);
4282 if (tevent_req_is_nterror(req, &status)) {
4285 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4287 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4289 return NT_STATUS_OK;
4292 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4295 struct ea_struct **pea_list)
4297 TALLOC_CTX *frame = talloc_stackframe();
4298 struct event_context *ev = NULL;
4299 struct tevent_req *req = NULL;
4300 NTSTATUS status = NT_STATUS_NO_MEMORY;
4302 if (cli_has_async_calls(cli)) {
4304 * Can't use sync call while an async call is in flight
4306 status = NT_STATUS_INVALID_PARAMETER;
4309 ev = event_context_init(frame);
4313 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4317 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4320 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4326 /****************************************************************************
4327 Convert open "flags" arg to uint32_t on wire.
4328 ****************************************************************************/
4330 static uint32_t open_flags_to_wire(int flags)
4332 int open_mode = flags & O_ACCMODE;
4335 switch (open_mode) {
4337 ret |= SMB_O_WRONLY;
4344 ret |= SMB_O_RDONLY;
4348 if (flags & O_CREAT) {
4351 if (flags & O_EXCL) {
4354 if (flags & O_TRUNC) {
4358 if (flags & O_SYNC) {
4362 if (flags & O_APPEND) {
4363 ret |= SMB_O_APPEND;
4365 #if defined(O_DIRECT)
4366 if (flags & O_DIRECT) {
4367 ret |= SMB_O_DIRECT;
4370 #if defined(O_DIRECTORY)
4371 if (flags & O_DIRECTORY) {
4372 ret |= SMB_O_DIRECTORY;
4378 /****************************************************************************
4379 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4380 ****************************************************************************/
4382 struct posix_open_state {
4386 uint16_t fnum; /* Out */
4389 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4391 struct tevent_req *req = tevent_req_callback_data(
4392 subreq, struct tevent_req);
4393 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4398 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4399 NULL, 0, NULL, &data, 12, &num_data);
4400 TALLOC_FREE(subreq);
4401 if (tevent_req_nterror(req, status)) {
4404 state->fnum = SVAL(data,2);
4405 tevent_req_done(req);
4408 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4409 struct event_context *ev,
4410 struct cli_state *cli,
4416 struct tevent_req *req = NULL, *subreq = NULL;
4417 struct posix_open_state *state = NULL;
4418 uint32_t wire_flags = open_flags_to_wire(flags);
4420 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4425 /* Setup setup word. */
4426 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4428 /* Setup param array. */
4429 state->param = talloc_array(state, uint8_t, 6);
4430 if (tevent_req_nomem(state->param, req)) {
4431 return tevent_req_post(req, ev);
4433 memset(state->param, '\0', 6);
4434 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4436 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4437 strlen(fname)+1, NULL);
4439 if (tevent_req_nomem(state->param, req)) {
4440 return tevent_req_post(req, ev);
4443 /* Setup data words. */
4445 wire_flags |= SMB_O_DIRECTORY;
4448 SIVAL(state->data,0,0); /* No oplock. */
4449 SIVAL(state->data,4,wire_flags);
4450 SIVAL(state->data,8,unix_perms_to_wire(mode));
4451 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4452 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4454 subreq = cli_trans_send(state, /* mem ctx. */
4455 ev, /* event ctx. */
4456 cli, /* cli_state. */
4457 SMBtrans2, /* cmd. */
4458 NULL, /* pipe name. */
4462 &state->setup, /* setup. */
4463 1, /* num setup uint16_t words. */
4464 0, /* max returned setup. */
4465 state->param, /* param. */
4466 talloc_get_size(state->param),/* num param. */
4467 2, /* max returned param. */
4468 state->data, /* data. */
4470 12); /* max returned data. */
4472 if (tevent_req_nomem(subreq, req)) {
4473 return tevent_req_post(req, ev);
4475 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4479 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4480 struct event_context *ev,
4481 struct cli_state *cli,
4486 return cli_posix_open_internal_send(mem_ctx, ev,
4487 cli, fname, flags, mode, false);
4490 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4492 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4495 if (tevent_req_is_nterror(req, &status)) {
4498 *pfnum = state->fnum;
4499 return NT_STATUS_OK;
4502 /****************************************************************************
4503 Open - POSIX semantics. Doesn't request oplock.
4504 ****************************************************************************/
4506 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4507 int flags, mode_t mode, uint16_t *pfnum)
4510 TALLOC_CTX *frame = talloc_stackframe();
4511 struct event_context *ev = NULL;
4512 struct tevent_req *req = NULL;
4513 NTSTATUS status = NT_STATUS_OK;
4515 if (cli_has_async_calls(cli)) {
4517 * Can't use sync call while an async call is in flight
4519 status = NT_STATUS_INVALID_PARAMETER;
4523 ev = event_context_init(frame);
4525 status = NT_STATUS_NO_MEMORY;
4529 req = cli_posix_open_send(frame,
4536 status = NT_STATUS_NO_MEMORY;
4540 if (!tevent_req_poll(req, ev)) {
4541 status = map_nt_error_from_unix(errno);
4545 status = cli_posix_open_recv(req, pfnum);
4552 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4553 struct event_context *ev,
4554 struct cli_state *cli,
4558 return cli_posix_open_internal_send(mem_ctx, ev,
4559 cli, fname, O_CREAT, mode, true);
4562 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4564 return tevent_req_simple_recv_ntstatus(req);
4567 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4569 TALLOC_CTX *frame = talloc_stackframe();
4570 struct event_context *ev = NULL;
4571 struct tevent_req *req = NULL;
4572 NTSTATUS status = NT_STATUS_OK;
4574 if (cli_has_async_calls(cli)) {
4576 * Can't use sync call while an async call is in flight
4578 status = NT_STATUS_INVALID_PARAMETER;
4582 ev = event_context_init(frame);
4584 status = NT_STATUS_NO_MEMORY;
4588 req = cli_posix_mkdir_send(frame,
4594 status = NT_STATUS_NO_MEMORY;
4598 if (!tevent_req_poll(req, ev)) {
4599 status = map_nt_error_from_unix(errno);
4603 status = cli_posix_mkdir_recv(req);
4610 /****************************************************************************
4611 unlink or rmdir - POSIX semantics.
4612 ****************************************************************************/
4614 struct cli_posix_unlink_internal_state {
4618 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4620 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4621 struct event_context *ev,
4622 struct cli_state *cli,
4626 struct tevent_req *req = NULL, *subreq = NULL;
4627 struct cli_posix_unlink_internal_state *state = NULL;
4629 req = tevent_req_create(mem_ctx, &state,
4630 struct cli_posix_unlink_internal_state);
4635 /* Setup data word. */
4636 SSVAL(state->data, 0, level);
4638 subreq = cli_setpathinfo_send(state, ev, cli,
4639 SMB_POSIX_PATH_UNLINK,
4641 state->data, sizeof(state->data));
4642 if (tevent_req_nomem(subreq, req)) {
4643 return tevent_req_post(req, ev);
4645 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4649 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4651 NTSTATUS status = cli_setpathinfo_recv(subreq);
4652 tevent_req_simple_finish_ntstatus(subreq, status);
4655 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4656 struct event_context *ev,
4657 struct cli_state *cli,
4660 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4661 SMB_POSIX_UNLINK_FILE_TARGET);
4664 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4666 return tevent_req_simple_recv_ntstatus(req);
4669 /****************************************************************************
4670 unlink - POSIX semantics.
4671 ****************************************************************************/
4673 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4675 TALLOC_CTX *frame = talloc_stackframe();
4676 struct event_context *ev = NULL;
4677 struct tevent_req *req = NULL;
4678 NTSTATUS status = NT_STATUS_OK;
4680 if (cli_has_async_calls(cli)) {
4682 * Can't use sync call while an async call is in flight
4684 status = NT_STATUS_INVALID_PARAMETER;
4688 ev = event_context_init(frame);
4690 status = NT_STATUS_NO_MEMORY;
4694 req = cli_posix_unlink_send(frame,
4699 status = NT_STATUS_NO_MEMORY;
4703 if (!tevent_req_poll(req, ev)) {
4704 status = map_nt_error_from_unix(errno);
4708 status = cli_posix_unlink_recv(req);
4715 /****************************************************************************
4716 rmdir - POSIX semantics.
4717 ****************************************************************************/
4719 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4720 struct event_context *ev,
4721 struct cli_state *cli,
4724 return cli_posix_unlink_internal_send(
4725 mem_ctx, ev, cli, fname,
4726 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4729 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4731 return tevent_req_simple_recv_ntstatus(req);
4734 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4736 TALLOC_CTX *frame = talloc_stackframe();
4737 struct event_context *ev = NULL;
4738 struct tevent_req *req = NULL;
4739 NTSTATUS status = NT_STATUS_OK;
4741 if (cli_has_async_calls(cli)) {
4743 * Can't use sync call while an async call is in flight
4745 status = NT_STATUS_INVALID_PARAMETER;
4749 ev = event_context_init(frame);
4751 status = NT_STATUS_NO_MEMORY;
4755 req = cli_posix_rmdir_send(frame,
4760 status = NT_STATUS_NO_MEMORY;
4764 if (!tevent_req_poll(req, ev)) {
4765 status = map_nt_error_from_unix(errno);
4769 status = cli_posix_rmdir_recv(req, frame);
4776 /****************************************************************************
4778 ****************************************************************************/
4780 struct cli_notify_state {
4782 uint32_t num_changes;
4783 struct notify_change *changes;
4786 static void cli_notify_done(struct tevent_req *subreq);
4788 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4789 struct tevent_context *ev,
4790 struct cli_state *cli, uint16_t fnum,
4791 uint32_t buffer_size,
4792 uint32_t completion_filter, bool recursive)
4794 struct tevent_req *req, *subreq;
4795 struct cli_notify_state *state;
4797 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4802 SIVAL(state->setup, 0, completion_filter);
4803 SSVAL(state->setup, 4, fnum);
4804 SSVAL(state->setup, 6, recursive);
4806 subreq = cli_trans_send(
4807 state, /* mem ctx. */
4808 ev, /* event ctx. */
4809 cli, /* cli_state. */
4810 SMBnttrans, /* cmd. */
4811 NULL, /* pipe name. */
4813 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4815 (uint16_t *)state->setup, /* setup. */
4816 4, /* num setup uint16_t words. */
4817 0, /* max returned setup. */
4820 buffer_size, /* max returned param. */
4823 0); /* max returned data. */
4825 if (tevent_req_nomem(subreq, req)) {
4826 return tevent_req_post(req, ev);
4828 tevent_req_set_callback(subreq, cli_notify_done, req);
4832 static void cli_notify_done(struct tevent_req *subreq)
4834 struct tevent_req *req = tevent_req_callback_data(
4835 subreq, struct tevent_req);
4836 struct cli_notify_state *state = tevent_req_data(
4837 req, struct cli_notify_state);
4840 uint32_t i, ofs, num_params;
4843 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4844 ¶ms, 0, &num_params, NULL, 0, NULL);
4845 TALLOC_FREE(subreq);
4846 if (tevent_req_nterror(req, status)) {
4847 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4851 state->num_changes = 0;
4854 while (num_params - ofs > 12) {
4855 uint32_t len = IVAL(params, ofs);
4856 state->num_changes += 1;
4858 if ((len == 0) || (ofs+len >= num_params)) {
4864 state->changes = talloc_array(state, struct notify_change,
4865 state->num_changes);
4866 if (tevent_req_nomem(state->changes, req)) {
4867 TALLOC_FREE(params);
4873 for (i=0; i<state->num_changes; i++) {
4874 uint32_t next = IVAL(params, ofs);
4875 uint32_t len = IVAL(params, ofs+8);
4879 if ((next != 0) && (len+12 != next)) {
4880 TALLOC_FREE(params);
4882 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4886 state->changes[i].action = IVAL(params, ofs+4);
4887 ret = clistr_pull_talloc(params, (char *)params, flags2,
4888 &name, params+ofs+12, len,
4889 STR_TERMINATE|STR_UNICODE);
4891 TALLOC_FREE(params);
4892 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4895 state->changes[i].name = name;
4899 TALLOC_FREE(params);
4900 tevent_req_done(req);
4903 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4904 uint32_t *pnum_changes,
4905 struct notify_change **pchanges)
4907 struct cli_notify_state *state = tevent_req_data(
4908 req, struct cli_notify_state);
4911 if (tevent_req_is_nterror(req, &status)) {
4915 *pnum_changes = state->num_changes;
4916 *pchanges = talloc_move(mem_ctx, &state->changes);
4917 return NT_STATUS_OK;
4920 struct cli_qpathinfo_state {
4929 static void cli_qpathinfo_done(struct tevent_req *subreq);
4931 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
4932 struct tevent_context *ev,
4933 struct cli_state *cli, const char *fname,
4934 uint16_t level, uint32_t min_rdata,
4937 struct tevent_req *req, *subreq;
4938 struct cli_qpathinfo_state *state;
4940 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
4944 state->min_rdata = min_rdata;
4945 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
4947 state->param = talloc_zero_array(state, uint8_t, 6);
4948 if (tevent_req_nomem(state->param, req)) {
4949 return tevent_req_post(req, ev);
4951 SSVAL(state->param, 0, level);
4952 state->param = trans2_bytes_push_str(
4953 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
4954 if (tevent_req_nomem(state->param, req)) {
4955 return tevent_req_post(req, ev);
4958 subreq = cli_trans_send(
4959 state, /* mem ctx. */
4960 ev, /* event ctx. */
4961 cli, /* cli_state. */
4962 SMBtrans2, /* cmd. */
4963 NULL, /* pipe name. */
4967 state->setup, /* setup. */
4968 1, /* num setup uint16_t words. */
4969 0, /* max returned setup. */
4970 state->param, /* param. */
4971 talloc_get_size(state->param), /* num param. */
4972 2, /* max returned param. */
4975 max_rdata); /* max returned data. */
4977 if (tevent_req_nomem(subreq, req)) {
4978 return tevent_req_post(req, ev);
4980 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
4984 static void cli_qpathinfo_done(struct tevent_req *subreq)
4986 struct tevent_req *req = tevent_req_callback_data(
4987 subreq, struct tevent_req);
4988 struct cli_qpathinfo_state *state = tevent_req_data(
4989 req, struct cli_qpathinfo_state);
4992 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4994 &state->rdata, state->min_rdata,
4996 if (tevent_req_nterror(req, status)) {
4999 tevent_req_done(req);
5002 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5003 uint8_t **rdata, uint32_t *num_rdata)
5005 struct cli_qpathinfo_state *state = tevent_req_data(
5006 req, struct cli_qpathinfo_state);
5009 if (tevent_req_is_nterror(req, &status)) {
5012 if (rdata != NULL) {
5013 *rdata = talloc_move(mem_ctx, &state->rdata);
5015 TALLOC_FREE(state->rdata);
5017 if (num_rdata != NULL) {
5018 *num_rdata = state->num_rdata;
5020 return NT_STATUS_OK;
5023 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5024 const char *fname, uint16_t level, uint32_t min_rdata,
5026 uint8_t **rdata, uint32_t *num_rdata)
5028 TALLOC_CTX *frame = talloc_stackframe();
5029 struct event_context *ev;
5030 struct tevent_req *req;
5031 NTSTATUS status = NT_STATUS_NO_MEMORY;
5033 if (cli_has_async_calls(cli)) {
5035 * Can't use sync call while an async call is in flight
5037 status = NT_STATUS_INVALID_PARAMETER;
5040 ev = event_context_init(frame);
5044 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5049 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5052 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5058 struct cli_qfileinfo_state {
5067 static void cli_qfileinfo_done(struct tevent_req *subreq);
5069 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5070 struct tevent_context *ev,
5071 struct cli_state *cli, uint16_t fnum,
5072 uint16_t level, uint32_t min_rdata,
5075 struct tevent_req *req, *subreq;
5076 struct cli_qfileinfo_state *state;
5078 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5082 state->min_rdata = min_rdata;
5083 SSVAL(state->param, 0, fnum);
5084 SSVAL(state->param, 2, level);
5085 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5087 subreq = cli_trans_send(
5088 state, /* mem ctx. */
5089 ev, /* event ctx. */
5090 cli, /* cli_state. */
5091 SMBtrans2, /* cmd. */
5092 NULL, /* pipe name. */
5096 state->setup, /* setup. */
5097 1, /* num setup uint16_t words. */
5098 0, /* max returned setup. */
5099 state->param, /* param. */
5100 sizeof(state->param), /* num param. */
5101 2, /* max returned param. */
5104 max_rdata); /* max returned data. */
5106 if (tevent_req_nomem(subreq, req)) {
5107 return tevent_req_post(req, ev);
5109 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5113 static void cli_qfileinfo_done(struct tevent_req *subreq)
5115 struct tevent_req *req = tevent_req_callback_data(
5116 subreq, struct tevent_req);
5117 struct cli_qfileinfo_state *state = tevent_req_data(
5118 req, struct cli_qfileinfo_state);
5121 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5123 &state->rdata, state->min_rdata,
5125 if (tevent_req_nterror(req, status)) {
5128 tevent_req_done(req);
5131 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5132 uint8_t **rdata, uint32_t *num_rdata)
5134 struct cli_qfileinfo_state *state = tevent_req_data(
5135 req, struct cli_qfileinfo_state);
5138 if (tevent_req_is_nterror(req, &status)) {
5141 if (rdata != NULL) {
5142 *rdata = talloc_move(mem_ctx, &state->rdata);
5144 TALLOC_FREE(state->rdata);
5146 if (num_rdata != NULL) {
5147 *num_rdata = state->num_rdata;
5149 return NT_STATUS_OK;
5152 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5153 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5155 uint8_t **rdata, uint32_t *num_rdata)
5157 TALLOC_CTX *frame = talloc_stackframe();
5158 struct event_context *ev;
5159 struct tevent_req *req;
5160 NTSTATUS status = NT_STATUS_NO_MEMORY;
5162 if (cli_has_async_calls(cli)) {
5164 * Can't use sync call while an async call is in flight
5166 status = NT_STATUS_INVALID_PARAMETER;
5169 ev = event_context_init(frame);
5173 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5178 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5181 status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
5187 struct cli_flush_state {
5191 static void cli_flush_done(struct tevent_req *subreq);
5193 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5194 struct event_context *ev,
5195 struct cli_state *cli,
5198 struct tevent_req *req, *subreq;
5199 struct cli_flush_state *state;
5201 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5205 SSVAL(state->vwv + 0, 0, fnum);
5207 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5209 if (tevent_req_nomem(subreq, req)) {
5210 return tevent_req_post(req, ev);
5212 tevent_req_set_callback(subreq, cli_flush_done, req);
5216 static void cli_flush_done(struct tevent_req *subreq)
5218 struct tevent_req *req = tevent_req_callback_data(
5219 subreq, struct tevent_req);
5222 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5223 TALLOC_FREE(subreq);
5224 if (tevent_req_nterror(req, status)) {
5227 tevent_req_done(req);
5230 NTSTATUS cli_flush_recv(struct tevent_req *req)
5232 return tevent_req_simple_recv_ntstatus(req);
5235 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5237 TALLOC_CTX *frame = talloc_stackframe();
5238 struct event_context *ev;
5239 struct tevent_req *req;
5240 NTSTATUS status = NT_STATUS_NO_MEMORY;
5242 if (cli_has_async_calls(cli)) {
5244 * Can't use sync call while an async call is in flight
5246 status = NT_STATUS_INVALID_PARAMETER;
5249 ev = event_context_init(frame);
5253 req = cli_flush_send(frame, ev, cli, fnum);
5257 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5260 status = cli_flush_recv(req);
5266 struct cli_shadow_copy_data_state {
5273 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5275 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5276 struct tevent_context *ev,
5277 struct cli_state *cli,
5281 struct tevent_req *req, *subreq;
5282 struct cli_shadow_copy_data_state *state;
5285 req = tevent_req_create(mem_ctx, &state,
5286 struct cli_shadow_copy_data_state);
5290 state->get_names = get_names;
5291 ret_size = get_names ? cli->max_xmit : 16;
5293 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5294 SSVAL(state->setup + 2, 0, fnum);
5295 SCVAL(state->setup + 3, 0, 0); /* isFsctl */
5296 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5298 subreq = cli_trans_send(
5299 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5300 state->setup, ARRAY_SIZE(state->setup), 0,
5303 if (tevent_req_nomem(subreq, req)) {
5304 return tevent_req_post(req, ev);
5306 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5310 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5312 struct tevent_req *req = tevent_req_callback_data(
5313 subreq, struct tevent_req);
5314 struct cli_shadow_copy_data_state *state = tevent_req_data(
5315 req, struct cli_shadow_copy_data_state);
5318 status = cli_trans_recv(subreq, state, NULL,
5319 NULL, 0, NULL, /* setup */
5320 NULL, 0, NULL, /* param */
5321 &state->data, 12, &state->num_data);
5322 TALLOC_FREE(subreq);
5323 if (tevent_req_nterror(req, status)) {
5326 tevent_req_done(req);
5329 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5330 char ***pnames, int *pnum_names)
5332 struct cli_shadow_copy_data_state *state = tevent_req_data(
5333 req, struct cli_shadow_copy_data_state);
5339 if (tevent_req_is_nterror(req, &status)) {
5342 num_names = IVAL(state->data, 4);
5343 dlength = IVAL(state->data, 8);
5345 if (!state->get_names) {
5346 *pnum_names = num_names;
5347 return NT_STATUS_OK;
5350 if (dlength+12 > state->num_data) {
5351 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5353 names = talloc_array(mem_ctx, char *, num_names);
5354 if (names == NULL) {
5355 return NT_STATUS_NO_MEMORY;
5358 for (i=0; i<num_names; i++) {
5361 size_t converted_size;
5363 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5364 ret = convert_string_talloc(
5365 names, CH_UTF16LE, CH_UNIX,
5366 src, 2 * sizeof(SHADOW_COPY_LABEL),
5367 &names[i], &converted_size);
5370 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5373 *pnum_names = num_names;
5375 return NT_STATUS_OK;
5378 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5379 uint16_t fnum, bool get_names,
5380 char ***pnames, int *pnum_names)
5382 TALLOC_CTX *frame = talloc_stackframe();
5383 struct event_context *ev;
5384 struct tevent_req *req;
5385 NTSTATUS status = NT_STATUS_NO_MEMORY;
5387 if (cli_has_async_calls(cli)) {
5389 * Can't use sync call while an async call is in flight
5391 status = NT_STATUS_INVALID_PARAMETER;
5394 ev = event_context_init(frame);
5398 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5402 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5405 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);