2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "async_smb.h"
24 /***********************************************************
25 Common function for pushing stings, used by smb_bytes_push_str()
26 and trans_bytes_push_str(). Only difference is the align_odd
28 ***********************************************************/
30 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
31 const char *str, size_t str_len,
33 size_t *pconverted_size)
37 size_t converted_size;
43 buflen = talloc_get_size(buf);
45 if (align_odd && ucs2 && (buflen % 2 == 0)) {
47 * We're pushing into an SMB buffer, align odd
49 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
57 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
58 ucs2 ? CH_UTF16LE : CH_DOS,
59 str, str_len, &converted,
60 &converted_size, true)) {
64 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
65 buflen + converted_size);
67 TALLOC_FREE(converted);
71 memcpy(buf + buflen, converted, converted_size);
73 TALLOC_FREE(converted);
75 if (pconverted_size) {
76 *pconverted_size = converted_size;
82 /***********************************************************
83 Push a string into an SMB buffer, with odd byte alignment
84 if it's a UCS2 string.
85 ***********************************************************/
87 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
88 const char *str, size_t str_len,
89 size_t *pconverted_size)
91 return internal_bytes_push_str(buf, ucs2, str, str_len,
92 true, pconverted_size);
95 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
96 const uint8_t *bytes, size_t num_bytes)
103 buflen = talloc_get_size(buf);
105 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
106 buflen + 1 + num_bytes);
110 buf[buflen] = prefix;
111 memcpy(&buf[buflen+1], bytes, num_bytes);
115 /***********************************************************
116 Same as smb_bytes_push_str(), but without the odd byte
117 align for ucs2 (we're pushing into a param or data block).
118 static for now, although this will probably change when
119 other modules use async trans calls.
120 ***********************************************************/
122 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
123 const char *str, size_t str_len,
124 size_t *pconverted_size)
126 return internal_bytes_push_str(buf, ucs2, str, str_len,
127 false, pconverted_size);
130 struct cli_setpathinfo_state {
135 static void cli_setpathinfo_done(struct tevent_req *subreq);
137 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
138 struct tevent_context *ev,
139 struct cli_state *cli,
145 struct tevent_req *req, *subreq;
146 struct cli_setpathinfo_state *state;
148 req = tevent_req_create(mem_ctx, &state,
149 struct cli_setpathinfo_state);
154 /* Setup setup word. */
155 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
157 /* Setup param array. */
158 state->param = TALLOC_ZERO_ARRAY(state, uint8_t, 6);
159 if (tevent_req_nomem(state->param, req)) {
160 return tevent_req_post(req, ev);
162 SSVAL(state->param, 0, level);
164 state->param = trans2_bytes_push_str(
165 state->param, cli_ucs2(cli), path, strlen(path)+1, NULL);
166 if (tevent_req_nomem(state->param, req)) {
167 return tevent_req_post(req, ev);
170 subreq = cli_trans_send(
171 state, /* mem ctx. */
173 cli, /* cli_state. */
174 SMBtrans2, /* cmd. */
175 NULL, /* pipe name. */
179 &state->setup, /* setup. */
180 1, /* num setup uint16_t words. */
181 0, /* max returned setup. */
182 state->param, /* param. */
183 talloc_get_size(state->param), /* num param. */
184 2, /* max returned param. */
186 data_len, /* num data. */
187 0); /* max returned data. */
189 if (tevent_req_nomem(subreq, req)) {
190 return tevent_req_post(req, ev);
192 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
196 static void cli_setpathinfo_done(struct tevent_req *subreq)
198 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
199 NULL, 0, NULL, NULL, 0, NULL);
200 tevent_req_simple_finish_ntstatus(subreq, status);
203 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
205 return tevent_req_simple_recv_ntstatus(req);
208 /****************************************************************************
209 Hard/Symlink a file (UNIX extensions).
210 Creates new name (sym)linked to oldname.
211 ****************************************************************************/
219 static void cli_posix_link_internal_done(struct tevent_req *subreq)
221 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
222 NULL, 0, NULL, NULL, 0, NULL);
223 tevent_req_simple_finish_ntstatus(subreq, status);
226 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
227 struct event_context *ev,
228 struct cli_state *cli,
233 struct tevent_req *req = NULL, *subreq = NULL;
234 struct link_state *state = NULL;
236 req = tevent_req_create(mem_ctx, &state, struct link_state);
241 /* Setup setup word. */
242 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
244 /* Setup param array. */
245 state->param = talloc_array(state, uint8_t, 6);
246 if (tevent_req_nomem(state->param, req)) {
247 return tevent_req_post(req, ev);
249 memset(state->param, '\0', 6);
250 SSVAL(state->param,0,hardlink ? SMB_SET_FILE_UNIX_HLINK : SMB_SET_FILE_UNIX_LINK);
252 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), newname,
253 strlen(newname)+1, NULL);
255 if (tevent_req_nomem(state->param, req)) {
256 return tevent_req_post(req, ev);
259 /* Setup data array. */
260 state->data = talloc_array(state, uint8_t, 0);
261 if (tevent_req_nomem(state->data, req)) {
262 return tevent_req_post(req, ev);
264 state->data = trans2_bytes_push_str(state->data, cli_ucs2(cli), oldname,
265 strlen(oldname)+1, NULL);
267 subreq = cli_trans_send(state, /* mem ctx. */
269 cli, /* cli_state. */
270 SMBtrans2, /* cmd. */
271 NULL, /* pipe name. */
275 &state->setup, /* setup. */
276 1, /* num setup uint16_t words. */
277 0, /* max returned setup. */
278 state->param, /* param. */
279 talloc_get_size(state->param), /* num param. */
280 2, /* max returned param. */
281 state->data, /* data. */
282 talloc_get_size(state->data), /* num data. */
283 0); /* max returned data. */
285 if (tevent_req_nomem(subreq, req)) {
286 return tevent_req_post(req, ev);
288 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
292 /****************************************************************************
293 Symlink a file (UNIX extensions).
294 ****************************************************************************/
296 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
297 struct event_context *ev,
298 struct cli_state *cli,
302 return cli_posix_link_internal_send(mem_ctx, ev, cli,
303 oldname, newname, false);
306 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
310 if (tevent_req_is_nterror(req, &status)) {
316 NTSTATUS cli_posix_symlink(struct cli_state *cli,
320 TALLOC_CTX *frame = talloc_stackframe();
321 struct event_context *ev = NULL;
322 struct tevent_req *req = NULL;
323 NTSTATUS status = NT_STATUS_OK;
325 if (cli_has_async_calls(cli)) {
327 * Can't use sync call while an async call is in flight
329 status = NT_STATUS_INVALID_PARAMETER;
333 ev = event_context_init(frame);
335 status = NT_STATUS_NO_MEMORY;
339 req = cli_posix_symlink_send(frame,
345 status = NT_STATUS_NO_MEMORY;
349 if (!tevent_req_poll(req, ev)) {
350 status = map_nt_error_from_unix(errno);
354 status = cli_posix_symlink_recv(req);
358 if (!NT_STATUS_IS_OK(status)) {
359 cli_set_error(cli, status);
364 /****************************************************************************
365 Read a POSIX symlink.
366 ****************************************************************************/
368 struct readlink_state {
373 static void cli_posix_readlink_done(struct tevent_req *subreq);
375 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
376 struct event_context *ev,
377 struct cli_state *cli,
381 struct tevent_req *req = NULL, *subreq = NULL;
382 struct readlink_state *state = NULL;
383 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
385 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
391 * Len is in bytes, we need it in UCS2 units.
393 if ((2*len < len) || (maxbytelen < len)) {
394 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
395 return tevent_req_post(req, ev);
398 subreq = cli_qpathinfo_send(state, ev, cli, fname,
399 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
400 if (tevent_req_nomem(subreq, req)) {
401 return tevent_req_post(req, ev);
403 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
407 static void cli_posix_readlink_done(struct tevent_req *subreq)
409 struct tevent_req *req = tevent_req_callback_data(
410 subreq, struct tevent_req);
411 struct readlink_state *state = tevent_req_data(
412 req, struct readlink_state);
415 status = cli_qpathinfo_recv(subreq, state, &state->data,
418 if (!NT_STATUS_IS_OK(status)) {
419 tevent_req_nterror(req, status);
423 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
425 if (state->data[state->num_data-1] != '\0') {
426 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
429 tevent_req_done(req);
432 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
433 char *retpath, size_t len)
436 char *converted = NULL;
437 size_t converted_size = 0;
438 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
440 if (tevent_req_is_nterror(req, &status)) {
443 /* The returned data is a pushed string, not raw data. */
444 if (!convert_string_talloc(state,
445 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
452 return NT_STATUS_NO_MEMORY;
455 len = MIN(len,converted_size);
457 return NT_STATUS_DATA_ERROR;
459 memcpy(retpath, converted, len);
463 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
464 char *linkpath, size_t len)
466 TALLOC_CTX *frame = talloc_stackframe();
467 struct event_context *ev = NULL;
468 struct tevent_req *req = NULL;
469 NTSTATUS status = NT_STATUS_OK;
471 if (cli_has_async_calls(cli)) {
473 * Can't use sync call while an async call is in flight
475 status = NT_STATUS_INVALID_PARAMETER;
479 ev = event_context_init(frame);
481 status = NT_STATUS_NO_MEMORY;
485 req = cli_posix_readlink_send(frame,
491 status = NT_STATUS_NO_MEMORY;
495 if (!tevent_req_poll(req, ev)) {
496 status = map_nt_error_from_unix(errno);
500 status = cli_posix_readlink_recv(req, cli, linkpath, len);
504 if (!NT_STATUS_IS_OK(status)) {
505 cli_set_error(cli, status);
510 /****************************************************************************
511 Hard link a file (UNIX extensions).
512 ****************************************************************************/
514 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
515 struct event_context *ev,
516 struct cli_state *cli,
520 return cli_posix_link_internal_send(mem_ctx, ev, cli,
521 oldname, newname, true);
524 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
528 if (tevent_req_is_nterror(req, &status)) {
534 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
538 TALLOC_CTX *frame = talloc_stackframe();
539 struct event_context *ev = NULL;
540 struct tevent_req *req = NULL;
541 NTSTATUS status = NT_STATUS_OK;
543 if (cli_has_async_calls(cli)) {
545 * Can't use sync call while an async call is in flight
547 status = NT_STATUS_INVALID_PARAMETER;
551 ev = event_context_init(frame);
553 status = NT_STATUS_NO_MEMORY;
557 req = cli_posix_hardlink_send(frame,
563 status = NT_STATUS_NO_MEMORY;
567 if (!tevent_req_poll(req, ev)) {
568 status = map_nt_error_from_unix(errno);
572 status = cli_posix_hardlink_recv(req);
576 if (!NT_STATUS_IS_OK(status)) {
577 cli_set_error(cli, status);
582 /****************************************************************************
583 Map standard UNIX permissions onto wire representations.
584 ****************************************************************************/
586 uint32_t unix_perms_to_wire(mode_t perms)
588 unsigned int ret = 0;
590 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
591 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
592 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
593 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
594 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
595 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
596 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
597 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
598 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
600 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
603 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
606 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
611 /****************************************************************************
612 Map wire permissions to standard UNIX.
613 ****************************************************************************/
615 mode_t wire_perms_to_unix(uint32_t perms)
617 mode_t ret = (mode_t)0;
619 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
620 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
621 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
622 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
623 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
624 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
625 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
626 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
627 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
629 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
632 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
635 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
640 /****************************************************************************
641 Return the file type from the wire filetype for UNIX extensions.
642 ****************************************************************************/
644 static mode_t unix_filetype_from_wire(uint32_t wire_type)
652 case UNIX_TYPE_SYMLINK:
656 case UNIX_TYPE_CHARDEV:
660 case UNIX_TYPE_BLKDEV:
668 case UNIX_TYPE_SOCKET:
676 /****************************************************************************
677 Do a POSIX getfacl (UNIX extensions).
678 ****************************************************************************/
680 struct getfacl_state {
685 static void cli_posix_getfacl_done(struct tevent_req *subreq);
687 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
688 struct event_context *ev,
689 struct cli_state *cli,
692 struct tevent_req *req = NULL, *subreq = NULL;
693 struct getfacl_state *state = NULL;
695 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
699 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
701 if (tevent_req_nomem(subreq, req)) {
702 return tevent_req_post(req, ev);
704 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
708 static void cli_posix_getfacl_done(struct tevent_req *subreq)
710 struct tevent_req *req = tevent_req_callback_data(
711 subreq, struct tevent_req);
712 struct getfacl_state *state = tevent_req_data(
713 req, struct getfacl_state);
716 status = cli_qpathinfo_recv(subreq, state, &state->data,
719 if (!NT_STATUS_IS_OK(status)) {
720 tevent_req_nterror(req, status);
723 tevent_req_done(req);
726 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
731 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
734 if (tevent_req_is_nterror(req, &status)) {
737 *prb_size = (size_t)state->num_data;
738 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
742 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
748 TALLOC_CTX *frame = talloc_stackframe();
749 struct event_context *ev = NULL;
750 struct tevent_req *req = NULL;
751 NTSTATUS status = NT_STATUS_OK;
753 if (cli_has_async_calls(cli)) {
755 * Can't use sync call while an async call is in flight
757 status = NT_STATUS_INVALID_PARAMETER;
761 ev = event_context_init(frame);
763 status = NT_STATUS_NO_MEMORY;
767 req = cli_posix_getfacl_send(frame,
772 status = NT_STATUS_NO_MEMORY;
776 if (!tevent_req_poll(req, ev)) {
777 status = map_nt_error_from_unix(errno);
781 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
785 if (!NT_STATUS_IS_OK(status)) {
786 cli_set_error(cli, status);
791 /****************************************************************************
792 Stat a file (UNIX extensions).
793 ****************************************************************************/
800 static void cli_posix_stat_done(struct tevent_req *subreq);
802 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
803 struct event_context *ev,
804 struct cli_state *cli,
807 struct tevent_req *req = NULL, *subreq = NULL;
808 struct stat_state *state = NULL;
810 req = tevent_req_create(mem_ctx, &state, struct stat_state);
814 subreq = cli_qpathinfo_send(state, ev, cli, fname,
815 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
816 if (tevent_req_nomem(subreq, req)) {
817 return tevent_req_post(req, ev);
819 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
823 static void cli_posix_stat_done(struct tevent_req *subreq)
825 struct tevent_req *req = tevent_req_callback_data(
826 subreq, struct tevent_req);
827 struct stat_state *state = tevent_req_data(req, struct stat_state);
830 status = cli_qpathinfo_recv(subreq, state, &state->data,
833 if (!NT_STATUS_IS_OK(status)) {
834 tevent_req_nterror(req, status);
837 tevent_req_done(req);
840 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
841 SMB_STRUCT_STAT *sbuf)
843 struct stat_state *state = tevent_req_data(req, struct stat_state);
846 if (tevent_req_is_nterror(req, &status)) {
850 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
851 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
852 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
853 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
855 /* assume 512 byte blocks */
856 sbuf->st_ex_blocks /= 512;
858 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
859 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
860 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
862 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
863 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
864 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
865 #if defined(HAVE_MAKEDEV)
867 uint32_t dev_major = IVAL(state->data,60);
868 uint32_t dev_minor = IVAL(state->data,68);
869 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
872 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
873 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
874 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
879 NTSTATUS cli_posix_stat(struct cli_state *cli,
881 SMB_STRUCT_STAT *sbuf)
883 TALLOC_CTX *frame = talloc_stackframe();
884 struct event_context *ev = NULL;
885 struct tevent_req *req = NULL;
886 NTSTATUS status = NT_STATUS_OK;
888 if (cli_has_async_calls(cli)) {
890 * Can't use sync call while an async call is in flight
892 status = NT_STATUS_INVALID_PARAMETER;
896 ev = event_context_init(frame);
898 status = NT_STATUS_NO_MEMORY;
902 req = cli_posix_stat_send(frame,
907 status = NT_STATUS_NO_MEMORY;
911 if (!tevent_req_poll(req, ev)) {
912 status = map_nt_error_from_unix(errno);
916 status = cli_posix_stat_recv(req, sbuf);
920 if (!NT_STATUS_IS_OK(status)) {
921 cli_set_error(cli, status);
926 /****************************************************************************
927 Chmod or chown a file internal (UNIX extensions).
928 ****************************************************************************/
936 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
938 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
939 NULL, 0, NULL, NULL, 0, NULL);
940 tevent_req_simple_finish_ntstatus(subreq, status);
943 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
944 struct event_context *ev,
945 struct cli_state *cli,
951 struct tevent_req *req = NULL, *subreq = NULL;
952 struct ch_state *state = NULL;
954 req = tevent_req_create(mem_ctx, &state, struct ch_state);
959 /* Setup setup word. */
960 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
962 /* Setup param array. */
963 state->param = talloc_array(state, uint8_t, 6);
964 if (tevent_req_nomem(state->param, req)) {
965 return tevent_req_post(req, ev);
967 memset(state->param, '\0', 6);
968 SSVAL(state->param,0,SMB_SET_FILE_UNIX_BASIC);
970 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
971 strlen(fname)+1, NULL);
973 if (tevent_req_nomem(state->param, req)) {
974 return tevent_req_post(req, ev);
977 /* Setup data array. */
978 state->data = talloc_array(state, uint8_t, 100);
979 if (tevent_req_nomem(state->data, req)) {
980 return tevent_req_post(req, ev);
982 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
983 memset(&state->data[40], '\0', 60);
984 SIVAL(state->data,40,uid);
985 SIVAL(state->data,48,gid);
986 SIVAL(state->data,84,mode);
988 subreq = cli_trans_send(state, /* mem ctx. */
990 cli, /* cli_state. */
991 SMBtrans2, /* cmd. */
992 NULL, /* pipe name. */
996 &state->setup, /* setup. */
997 1, /* num setup uint16_t words. */
998 0, /* max returned setup. */
999 state->param, /* param. */
1000 talloc_get_size(state->param), /* num param. */
1001 2, /* max returned param. */
1002 state->data, /* data. */
1003 talloc_get_size(state->data), /* num data. */
1004 0); /* max returned data. */
1006 if (tevent_req_nomem(subreq, req)) {
1007 return tevent_req_post(req, ev);
1009 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done, req);
1013 /****************************************************************************
1014 chmod a file (UNIX extensions).
1015 ****************************************************************************/
1017 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
1018 struct event_context *ev,
1019 struct cli_state *cli,
1023 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1025 unix_perms_to_wire(mode),
1030 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
1034 if (tevent_req_is_nterror(req, &status)) {
1037 return NT_STATUS_OK;
1040 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
1042 TALLOC_CTX *frame = talloc_stackframe();
1043 struct event_context *ev = NULL;
1044 struct tevent_req *req = NULL;
1045 NTSTATUS status = NT_STATUS_OK;
1047 if (cli_has_async_calls(cli)) {
1049 * Can't use sync call while an async call is in flight
1051 status = NT_STATUS_INVALID_PARAMETER;
1055 ev = event_context_init(frame);
1057 status = NT_STATUS_NO_MEMORY;
1061 req = cli_posix_chmod_send(frame,
1067 status = NT_STATUS_NO_MEMORY;
1071 if (!tevent_req_poll(req, ev)) {
1072 status = map_nt_error_from_unix(errno);
1076 status = cli_posix_chmod_recv(req);
1080 if (!NT_STATUS_IS_OK(status)) {
1081 cli_set_error(cli, status);
1086 /****************************************************************************
1087 chown a file (UNIX extensions).
1088 ****************************************************************************/
1090 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1091 struct event_context *ev,
1092 struct cli_state *cli,
1097 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1104 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1108 if (tevent_req_is_nterror(req, &status)) {
1111 return NT_STATUS_OK;
1114 NTSTATUS cli_posix_chown(struct cli_state *cli,
1119 TALLOC_CTX *frame = talloc_stackframe();
1120 struct event_context *ev = NULL;
1121 struct tevent_req *req = NULL;
1122 NTSTATUS status = NT_STATUS_OK;
1124 if (cli_has_async_calls(cli)) {
1126 * Can't use sync call while an async call is in flight
1128 status = NT_STATUS_INVALID_PARAMETER;
1132 ev = event_context_init(frame);
1134 status = NT_STATUS_NO_MEMORY;
1138 req = cli_posix_chown_send(frame,
1145 status = NT_STATUS_NO_MEMORY;
1149 if (!tevent_req_poll(req, ev)) {
1150 status = map_nt_error_from_unix(errno);
1154 status = cli_posix_chown_recv(req);
1158 if (!NT_STATUS_IS_OK(status)) {
1159 cli_set_error(cli, status);
1164 /****************************************************************************
1166 ****************************************************************************/
1168 static void cli_rename_done(struct tevent_req *subreq);
1170 struct cli_rename_state {
1174 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1175 struct event_context *ev,
1176 struct cli_state *cli,
1177 const char *fname_src,
1178 const char *fname_dst)
1180 struct tevent_req *req = NULL, *subreq = NULL;
1181 struct cli_rename_state *state = NULL;
1182 uint8_t additional_flags = 0;
1183 uint8_t *bytes = NULL;
1185 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1190 SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
1192 bytes = talloc_array(state, uint8_t, 1);
1193 if (tevent_req_nomem(bytes, req)) {
1194 return tevent_req_post(req, ev);
1197 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1198 strlen(fname_src)+1, NULL);
1199 if (tevent_req_nomem(bytes, req)) {
1200 return tevent_req_post(req, ev);
1203 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1204 talloc_get_size(bytes)+1);
1205 if (tevent_req_nomem(bytes, req)) {
1206 return tevent_req_post(req, ev);
1209 bytes[talloc_get_size(bytes)-1] = 4;
1210 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1211 strlen(fname_dst)+1, NULL);
1212 if (tevent_req_nomem(bytes, req)) {
1213 return tevent_req_post(req, ev);
1216 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1217 1, state->vwv, talloc_get_size(bytes), bytes);
1218 if (tevent_req_nomem(subreq, req)) {
1219 return tevent_req_post(req, ev);
1221 tevent_req_set_callback(subreq, cli_rename_done, req);
1225 static void cli_rename_done(struct tevent_req *subreq)
1227 struct tevent_req *req = tevent_req_callback_data(
1228 subreq, struct tevent_req);
1231 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1232 TALLOC_FREE(subreq);
1233 if (!NT_STATUS_IS_OK(status)) {
1234 tevent_req_nterror(req, status);
1237 tevent_req_done(req);
1240 NTSTATUS cli_rename_recv(struct tevent_req *req)
1242 return tevent_req_simple_recv_ntstatus(req);
1245 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1247 TALLOC_CTX *frame = talloc_stackframe();
1248 struct event_context *ev;
1249 struct tevent_req *req;
1250 NTSTATUS status = NT_STATUS_OK;
1252 if (cli_has_async_calls(cli)) {
1254 * Can't use sync call while an async call is in flight
1256 status = NT_STATUS_INVALID_PARAMETER;
1260 ev = event_context_init(frame);
1262 status = NT_STATUS_NO_MEMORY;
1266 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1268 status = NT_STATUS_NO_MEMORY;
1272 if (!tevent_req_poll(req, ev)) {
1273 status = map_nt_error_from_unix(errno);
1277 status = cli_rename_recv(req);
1281 if (!NT_STATUS_IS_OK(status)) {
1282 cli_set_error(cli, status);
1287 /****************************************************************************
1289 ****************************************************************************/
1291 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1293 struct cli_ntrename_internal_state {
1297 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1298 struct event_context *ev,
1299 struct cli_state *cli,
1300 const char *fname_src,
1301 const char *fname_dst,
1302 uint16_t rename_flag)
1304 struct tevent_req *req = NULL, *subreq = NULL;
1305 struct cli_ntrename_internal_state *state = NULL;
1306 uint8_t additional_flags = 0;
1307 uint8_t *bytes = NULL;
1309 req = tevent_req_create(mem_ctx, &state,
1310 struct cli_ntrename_internal_state);
1315 SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
1316 SSVAL(state->vwv+1, 0, rename_flag);
1318 bytes = talloc_array(state, uint8_t, 1);
1319 if (tevent_req_nomem(bytes, req)) {
1320 return tevent_req_post(req, ev);
1323 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1324 strlen(fname_src)+1, NULL);
1325 if (tevent_req_nomem(bytes, req)) {
1326 return tevent_req_post(req, ev);
1329 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1330 talloc_get_size(bytes)+1);
1331 if (tevent_req_nomem(bytes, req)) {
1332 return tevent_req_post(req, ev);
1335 bytes[talloc_get_size(bytes)-1] = 4;
1336 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1337 strlen(fname_dst)+1, NULL);
1338 if (tevent_req_nomem(bytes, req)) {
1339 return tevent_req_post(req, ev);
1342 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1343 4, state->vwv, talloc_get_size(bytes), bytes);
1344 if (tevent_req_nomem(subreq, req)) {
1345 return tevent_req_post(req, ev);
1347 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1351 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1353 struct tevent_req *req = tevent_req_callback_data(
1354 subreq, struct tevent_req);
1357 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1358 TALLOC_FREE(subreq);
1359 if (!NT_STATUS_IS_OK(status)) {
1360 tevent_req_nterror(req, status);
1363 tevent_req_done(req);
1366 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1368 return tevent_req_simple_recv_ntstatus(req);
1371 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1372 struct event_context *ev,
1373 struct cli_state *cli,
1374 const char *fname_src,
1375 const char *fname_dst)
1377 return cli_ntrename_internal_send(mem_ctx,
1382 RENAME_FLAG_RENAME);
1385 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1387 return cli_ntrename_internal_recv(req);
1390 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1392 TALLOC_CTX *frame = talloc_stackframe();
1393 struct event_context *ev;
1394 struct tevent_req *req;
1395 NTSTATUS status = NT_STATUS_OK;
1397 if (cli_has_async_calls(cli)) {
1399 * Can't use sync call while an async call is in flight
1401 status = NT_STATUS_INVALID_PARAMETER;
1405 ev = event_context_init(frame);
1407 status = NT_STATUS_NO_MEMORY;
1411 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1413 status = NT_STATUS_NO_MEMORY;
1417 if (!tevent_req_poll(req, ev)) {
1418 status = map_nt_error_from_unix(errno);
1422 status = cli_ntrename_recv(req);
1426 if (!NT_STATUS_IS_OK(status)) {
1427 cli_set_error(cli, status);
1432 /****************************************************************************
1434 ****************************************************************************/
1436 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1437 struct event_context *ev,
1438 struct cli_state *cli,
1439 const char *fname_src,
1440 const char *fname_dst)
1442 return cli_ntrename_internal_send(mem_ctx,
1447 RENAME_FLAG_HARD_LINK);
1450 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1452 return cli_ntrename_internal_recv(req);
1455 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1457 TALLOC_CTX *frame = talloc_stackframe();
1458 struct event_context *ev;
1459 struct tevent_req *req;
1460 NTSTATUS status = NT_STATUS_OK;
1462 if (cli_has_async_calls(cli)) {
1464 * Can't use sync call while an async call is in flight
1466 status = NT_STATUS_INVALID_PARAMETER;
1470 ev = event_context_init(frame);
1472 status = NT_STATUS_NO_MEMORY;
1476 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1478 status = NT_STATUS_NO_MEMORY;
1482 if (!tevent_req_poll(req, ev)) {
1483 status = map_nt_error_from_unix(errno);
1487 status = cli_nt_hardlink_recv(req);
1491 if (!NT_STATUS_IS_OK(status)) {
1492 cli_set_error(cli, status);
1497 /****************************************************************************
1499 ****************************************************************************/
1501 static void cli_unlink_done(struct tevent_req *subreq);
1503 struct cli_unlink_state {
1507 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1508 struct event_context *ev,
1509 struct cli_state *cli,
1511 uint16_t mayhave_attrs)
1513 struct tevent_req *req = NULL, *subreq = NULL;
1514 struct cli_unlink_state *state = NULL;
1515 uint8_t additional_flags = 0;
1516 uint8_t *bytes = NULL;
1518 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1523 SSVAL(state->vwv+0, 0, mayhave_attrs);
1525 bytes = talloc_array(state, uint8_t, 1);
1526 if (tevent_req_nomem(bytes, req)) {
1527 return tevent_req_post(req, ev);
1530 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1531 strlen(fname)+1, NULL);
1533 if (tevent_req_nomem(bytes, req)) {
1534 return tevent_req_post(req, ev);
1537 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1538 1, state->vwv, talloc_get_size(bytes), bytes);
1539 if (tevent_req_nomem(subreq, req)) {
1540 return tevent_req_post(req, ev);
1542 tevent_req_set_callback(subreq, cli_unlink_done, req);
1546 static void cli_unlink_done(struct tevent_req *subreq)
1548 struct tevent_req *req = tevent_req_callback_data(
1549 subreq, struct tevent_req);
1552 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1553 TALLOC_FREE(subreq);
1554 if (!NT_STATUS_IS_OK(status)) {
1555 tevent_req_nterror(req, status);
1558 tevent_req_done(req);
1561 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1563 return tevent_req_simple_recv_ntstatus(req);
1566 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1568 TALLOC_CTX *frame = talloc_stackframe();
1569 struct event_context *ev;
1570 struct tevent_req *req;
1571 NTSTATUS status = NT_STATUS_OK;
1573 if (cli_has_async_calls(cli)) {
1575 * Can't use sync call while an async call is in flight
1577 status = NT_STATUS_INVALID_PARAMETER;
1581 ev = event_context_init(frame);
1583 status = NT_STATUS_NO_MEMORY;
1587 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1589 status = NT_STATUS_NO_MEMORY;
1593 if (!tevent_req_poll(req, ev)) {
1594 status = map_nt_error_from_unix(errno);
1598 status = cli_unlink_recv(req);
1602 if (!NT_STATUS_IS_OK(status)) {
1603 cli_set_error(cli, status);
1608 /****************************************************************************
1610 ****************************************************************************/
1612 static void cli_mkdir_done(struct tevent_req *subreq);
1614 struct cli_mkdir_state {
1618 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1619 struct event_context *ev,
1620 struct cli_state *cli,
1623 struct tevent_req *req = NULL, *subreq = NULL;
1624 struct cli_mkdir_state *state = NULL;
1625 uint8_t additional_flags = 0;
1626 uint8_t *bytes = NULL;
1628 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1633 bytes = talloc_array(state, uint8_t, 1);
1634 if (tevent_req_nomem(bytes, req)) {
1635 return tevent_req_post(req, ev);
1638 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1639 strlen(dname)+1, NULL);
1641 if (tevent_req_nomem(bytes, req)) {
1642 return tevent_req_post(req, ev);
1645 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1646 0, NULL, talloc_get_size(bytes), bytes);
1647 if (tevent_req_nomem(subreq, req)) {
1648 return tevent_req_post(req, ev);
1650 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1654 static void cli_mkdir_done(struct tevent_req *subreq)
1656 struct tevent_req *req = tevent_req_callback_data(
1657 subreq, struct tevent_req);
1660 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1661 TALLOC_FREE(subreq);
1662 if (!NT_STATUS_IS_OK(status)) {
1663 tevent_req_nterror(req, status);
1666 tevent_req_done(req);
1669 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1671 return tevent_req_simple_recv_ntstatus(req);
1674 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1676 TALLOC_CTX *frame = talloc_stackframe();
1677 struct event_context *ev;
1678 struct tevent_req *req;
1679 NTSTATUS status = NT_STATUS_OK;
1681 if (cli_has_async_calls(cli)) {
1683 * Can't use sync call while an async call is in flight
1685 status = NT_STATUS_INVALID_PARAMETER;
1689 ev = event_context_init(frame);
1691 status = NT_STATUS_NO_MEMORY;
1695 req = cli_mkdir_send(frame, ev, cli, dname);
1697 status = NT_STATUS_NO_MEMORY;
1701 if (!tevent_req_poll(req, ev)) {
1702 status = map_nt_error_from_unix(errno);
1706 status = cli_mkdir_recv(req);
1710 if (!NT_STATUS_IS_OK(status)) {
1711 cli_set_error(cli, status);
1716 /****************************************************************************
1718 ****************************************************************************/
1720 static void cli_rmdir_done(struct tevent_req *subreq);
1722 struct cli_rmdir_state {
1726 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1727 struct event_context *ev,
1728 struct cli_state *cli,
1731 struct tevent_req *req = NULL, *subreq = NULL;
1732 struct cli_rmdir_state *state = NULL;
1733 uint8_t additional_flags = 0;
1734 uint8_t *bytes = NULL;
1736 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1741 bytes = talloc_array(state, uint8_t, 1);
1742 if (tevent_req_nomem(bytes, req)) {
1743 return tevent_req_post(req, ev);
1746 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1747 strlen(dname)+1, NULL);
1749 if (tevent_req_nomem(bytes, req)) {
1750 return tevent_req_post(req, ev);
1753 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1754 0, NULL, talloc_get_size(bytes), bytes);
1755 if (tevent_req_nomem(subreq, req)) {
1756 return tevent_req_post(req, ev);
1758 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1762 static void cli_rmdir_done(struct tevent_req *subreq)
1764 struct tevent_req *req = tevent_req_callback_data(
1765 subreq, struct tevent_req);
1768 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1769 TALLOC_FREE(subreq);
1770 if (!NT_STATUS_IS_OK(status)) {
1771 tevent_req_nterror(req, status);
1774 tevent_req_done(req);
1777 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1779 return tevent_req_simple_recv_ntstatus(req);
1782 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1784 TALLOC_CTX *frame = talloc_stackframe();
1785 struct event_context *ev;
1786 struct tevent_req *req;
1787 NTSTATUS status = NT_STATUS_OK;
1789 if (cli_has_async_calls(cli)) {
1791 * Can't use sync call while an async call is in flight
1793 status = NT_STATUS_INVALID_PARAMETER;
1797 ev = event_context_init(frame);
1799 status = NT_STATUS_NO_MEMORY;
1803 req = cli_rmdir_send(frame, ev, cli, dname);
1805 status = NT_STATUS_NO_MEMORY;
1809 if (!tevent_req_poll(req, ev)) {
1810 status = map_nt_error_from_unix(errno);
1814 status = cli_rmdir_recv(req);
1818 if (!NT_STATUS_IS_OK(status)) {
1819 cli_set_error(cli, status);
1824 /****************************************************************************
1825 Set or clear the delete on close flag.
1826 ****************************************************************************/
1834 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1836 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1837 NULL, 0, NULL, NULL, 0, NULL);
1838 tevent_req_simple_finish_ntstatus(subreq, status);
1841 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1842 struct event_context *ev,
1843 struct cli_state *cli,
1847 struct tevent_req *req = NULL, *subreq = NULL;
1848 struct doc_state *state = NULL;
1850 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1855 /* Setup setup word. */
1856 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1858 /* Setup param array. */
1859 SSVAL(state->param,0,fnum);
1860 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1862 /* Setup data array. */
1863 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1865 subreq = cli_trans_send(state, /* mem ctx. */
1866 ev, /* event ctx. */
1867 cli, /* cli_state. */
1868 SMBtrans2, /* cmd. */
1869 NULL, /* pipe name. */
1873 &state->setup, /* setup. */
1874 1, /* num setup uint16_t words. */
1875 0, /* max returned setup. */
1876 state->param, /* param. */
1878 2, /* max returned param. */
1879 state->data, /* data. */
1881 0); /* max returned data. */
1883 if (tevent_req_nomem(subreq, req)) {
1884 return tevent_req_post(req, ev);
1886 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1890 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1894 if (tevent_req_is_nterror(req, &status)) {
1897 return NT_STATUS_OK;
1900 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1902 TALLOC_CTX *frame = talloc_stackframe();
1903 struct event_context *ev = NULL;
1904 struct tevent_req *req = NULL;
1905 NTSTATUS status = NT_STATUS_OK;
1907 if (cli_has_async_calls(cli)) {
1909 * Can't use sync call while an async call is in flight
1911 status = NT_STATUS_INVALID_PARAMETER;
1915 ev = event_context_init(frame);
1917 status = NT_STATUS_NO_MEMORY;
1921 req = cli_nt_delete_on_close_send(frame,
1927 status = NT_STATUS_NO_MEMORY;
1931 if (!tevent_req_poll(req, ev)) {
1932 status = map_nt_error_from_unix(errno);
1936 status = cli_nt_delete_on_close_recv(req);
1940 if (!NT_STATUS_IS_OK(status)) {
1941 cli_set_error(cli, status);
1946 struct cli_ntcreate_state {
1951 static void cli_ntcreate_done(struct tevent_req *subreq);
1953 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1954 struct event_context *ev,
1955 struct cli_state *cli,
1957 uint32_t CreatFlags,
1958 uint32_t DesiredAccess,
1959 uint32_t FileAttributes,
1960 uint32_t ShareAccess,
1961 uint32_t CreateDisposition,
1962 uint32_t CreateOptions,
1963 uint8_t SecurityFlags)
1965 struct tevent_req *req, *subreq;
1966 struct cli_ntcreate_state *state;
1969 size_t converted_len;
1971 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1978 SCVAL(vwv+0, 0, 0xFF);
1983 if (cli->use_oplocks) {
1984 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1986 SIVAL(vwv+3, 1, CreatFlags);
1987 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1988 SIVAL(vwv+7, 1, DesiredAccess);
1989 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1990 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1991 SIVAL(vwv+13, 1, FileAttributes);
1992 SIVAL(vwv+15, 1, ShareAccess);
1993 SIVAL(vwv+17, 1, CreateDisposition);
1994 SIVAL(vwv+19, 1, CreateOptions);
1995 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1996 SCVAL(vwv+23, 1, SecurityFlags);
1998 bytes = talloc_array(state, uint8_t, 0);
1999 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
2000 fname, strlen(fname)+1,
2003 /* sigh. this copes with broken netapp filer behaviour */
2004 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
2006 if (tevent_req_nomem(bytes, req)) {
2007 return tevent_req_post(req, ev);
2010 SSVAL(vwv+2, 1, converted_len);
2012 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
2013 talloc_get_size(bytes), bytes);
2014 if (tevent_req_nomem(subreq, req)) {
2015 return tevent_req_post(req, ev);
2017 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
2021 static void cli_ntcreate_done(struct tevent_req *subreq)
2023 struct tevent_req *req = tevent_req_callback_data(
2024 subreq, struct tevent_req);
2025 struct cli_ntcreate_state *state = tevent_req_data(
2026 req, struct cli_ntcreate_state);
2034 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
2035 &num_bytes, &bytes);
2036 TALLOC_FREE(subreq);
2037 if (!NT_STATUS_IS_OK(status)) {
2038 tevent_req_nterror(req, status);
2041 state->fnum = SVAL(vwv+2, 1);
2042 tevent_req_done(req);
2045 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
2047 struct cli_ntcreate_state *state = tevent_req_data(
2048 req, struct cli_ntcreate_state);
2051 if (tevent_req_is_nterror(req, &status)) {
2054 *pfnum = state->fnum;
2055 return NT_STATUS_OK;
2058 NTSTATUS cli_ntcreate(struct cli_state *cli,
2060 uint32_t CreatFlags,
2061 uint32_t DesiredAccess,
2062 uint32_t FileAttributes,
2063 uint32_t ShareAccess,
2064 uint32_t CreateDisposition,
2065 uint32_t CreateOptions,
2066 uint8_t SecurityFlags,
2069 TALLOC_CTX *frame = talloc_stackframe();
2070 struct event_context *ev;
2071 struct tevent_req *req;
2072 NTSTATUS status = NT_STATUS_OK;
2074 if (cli_has_async_calls(cli)) {
2076 * Can't use sync call while an async call is in flight
2078 status = NT_STATUS_INVALID_PARAMETER;
2082 ev = event_context_init(frame);
2084 status = NT_STATUS_NO_MEMORY;
2088 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2089 DesiredAccess, FileAttributes, ShareAccess,
2090 CreateDisposition, CreateOptions,
2093 status = NT_STATUS_NO_MEMORY;
2097 if (!tevent_req_poll(req, ev)) {
2098 status = map_nt_error_from_unix(errno);
2102 status = cli_ntcreate_recv(req, pfid);
2105 if (!NT_STATUS_IS_OK(status)) {
2106 cli_set_error(cli, status);
2111 /****************************************************************************
2113 WARNING: if you open with O_WRONLY then getattrE won't work!
2114 ****************************************************************************/
2116 struct cli_open_state {
2122 static void cli_open_done(struct tevent_req *subreq);
2124 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2125 struct event_context *ev,
2126 struct cli_state *cli, const char *fname,
2127 int flags, int share_mode,
2128 struct tevent_req **psmbreq)
2130 struct tevent_req *req, *subreq;
2131 struct cli_open_state *state;
2133 unsigned accessmode;
2134 uint8_t additional_flags;
2137 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2143 if (flags & O_CREAT) {
2146 if (!(flags & O_EXCL)) {
2147 if (flags & O_TRUNC)
2153 accessmode = (share_mode<<4);
2155 if ((flags & O_ACCMODE) == O_RDWR) {
2157 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2162 if ((flags & O_SYNC) == O_SYNC) {
2163 accessmode |= (1<<14);
2167 if (share_mode == DENY_FCB) {
2171 SCVAL(state->vwv + 0, 0, 0xFF);
2172 SCVAL(state->vwv + 0, 1, 0);
2173 SSVAL(state->vwv + 1, 0, 0);
2174 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2175 SSVAL(state->vwv + 3, 0, accessmode);
2176 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
2177 SSVAL(state->vwv + 5, 0, 0);
2178 SIVAL(state->vwv + 6, 0, 0);
2179 SSVAL(state->vwv + 8, 0, openfn);
2180 SIVAL(state->vwv + 9, 0, 0);
2181 SIVAL(state->vwv + 11, 0, 0);
2182 SIVAL(state->vwv + 13, 0, 0);
2184 additional_flags = 0;
2186 if (cli->use_oplocks) {
2187 /* if using oplocks then ask for a batch oplock via
2188 core and extended methods */
2190 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2191 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2194 bytes = talloc_array(state, uint8_t, 0);
2195 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2196 strlen(fname)+1, NULL);
2198 if (tevent_req_nomem(bytes, req)) {
2199 return tevent_req_post(req, ev);
2202 state->bytes.iov_base = (void *)bytes;
2203 state->bytes.iov_len = talloc_get_size(bytes);
2205 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2206 15, state->vwv, 1, &state->bytes);
2207 if (subreq == NULL) {
2211 tevent_req_set_callback(subreq, cli_open_done, req);
2216 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2217 struct cli_state *cli, const char *fname,
2218 int flags, int share_mode)
2220 struct tevent_req *req, *subreq;
2223 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2229 status = cli_smb_req_send(subreq);
2230 if (!NT_STATUS_IS_OK(status)) {
2231 tevent_req_nterror(req, status);
2232 return tevent_req_post(req, ev);
2237 static void cli_open_done(struct tevent_req *subreq)
2239 struct tevent_req *req = tevent_req_callback_data(
2240 subreq, struct tevent_req);
2241 struct cli_open_state *state = tevent_req_data(
2242 req, struct cli_open_state);
2248 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2250 TALLOC_FREE(subreq);
2251 if (!NT_STATUS_IS_OK(status)) {
2252 tevent_req_nterror(req, status);
2255 state->fnum = SVAL(vwv+2, 0);
2256 tevent_req_done(req);
2259 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2261 struct cli_open_state *state = tevent_req_data(
2262 req, struct cli_open_state);
2265 if (tevent_req_is_nterror(req, &status)) {
2268 *pfnum = state->fnum;
2269 return NT_STATUS_OK;
2272 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2273 int share_mode, uint16_t *pfnum)
2275 TALLOC_CTX *frame = talloc_stackframe();
2276 struct event_context *ev;
2277 struct tevent_req *req;
2278 NTSTATUS status = NT_STATUS_OK;
2280 if (cli_has_async_calls(cli)) {
2282 * Can't use sync call while an async call is in flight
2284 status = NT_STATUS_INVALID_PARAMETER;
2288 ev = event_context_init(frame);
2290 status = NT_STATUS_NO_MEMORY;
2294 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2296 status = NT_STATUS_NO_MEMORY;
2300 if (!tevent_req_poll(req, ev)) {
2301 status = map_nt_error_from_unix(errno);
2305 status = cli_open_recv(req, pfnum);
2308 if (!NT_STATUS_IS_OK(status)) {
2309 cli_set_error(cli, status);
2314 /****************************************************************************
2316 ****************************************************************************/
2318 struct cli_close_state {
2322 static void cli_close_done(struct tevent_req *subreq);
2324 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2325 struct event_context *ev,
2326 struct cli_state *cli,
2328 struct tevent_req **psubreq)
2330 struct tevent_req *req, *subreq;
2331 struct cli_close_state *state;
2333 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2338 SSVAL(state->vwv+0, 0, fnum);
2339 SIVALS(state->vwv+1, 0, -1);
2341 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2343 if (subreq == NULL) {
2347 tevent_req_set_callback(subreq, cli_close_done, req);
2352 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2353 struct event_context *ev,
2354 struct cli_state *cli,
2357 struct tevent_req *req, *subreq;
2360 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2365 status = cli_smb_req_send(subreq);
2366 if (!NT_STATUS_IS_OK(status)) {
2367 tevent_req_nterror(req, status);
2368 return tevent_req_post(req, ev);
2373 static void cli_close_done(struct tevent_req *subreq)
2375 struct tevent_req *req = tevent_req_callback_data(
2376 subreq, struct tevent_req);
2379 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2380 TALLOC_FREE(subreq);
2381 if (!NT_STATUS_IS_OK(status)) {
2382 tevent_req_nterror(req, status);
2385 tevent_req_done(req);
2388 NTSTATUS cli_close_recv(struct tevent_req *req)
2390 return tevent_req_simple_recv_ntstatus(req);
2393 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2395 TALLOC_CTX *frame = talloc_stackframe();
2396 struct event_context *ev;
2397 struct tevent_req *req;
2398 NTSTATUS status = NT_STATUS_OK;
2400 if (cli_has_async_calls(cli)) {
2402 * Can't use sync call while an async call is in flight
2404 status = NT_STATUS_INVALID_PARAMETER;
2408 ev = event_context_init(frame);
2410 status = NT_STATUS_NO_MEMORY;
2414 req = cli_close_send(frame, ev, cli, fnum);
2416 status = NT_STATUS_NO_MEMORY;
2420 if (!tevent_req_poll(req, ev)) {
2421 status = map_nt_error_from_unix(errno);
2425 status = cli_close_recv(req);
2428 if (!NT_STATUS_IS_OK(status)) {
2429 cli_set_error(cli, status);
2434 /****************************************************************************
2435 Truncate a file to a specified size
2436 ****************************************************************************/
2438 struct ftrunc_state {
2444 static void cli_ftruncate_done(struct tevent_req *subreq)
2446 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2447 NULL, 0, NULL, NULL, 0, NULL);
2448 tevent_req_simple_finish_ntstatus(subreq, status);
2451 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2452 struct event_context *ev,
2453 struct cli_state *cli,
2457 struct tevent_req *req = NULL, *subreq = NULL;
2458 struct ftrunc_state *state = NULL;
2460 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2465 /* Setup setup word. */
2466 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2468 /* Setup param array. */
2469 SSVAL(state->param,0,fnum);
2470 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2471 SSVAL(state->param,4,0);
2473 /* Setup data array. */
2474 SBVAL(state->data, 0, size);
2476 subreq = cli_trans_send(state, /* mem ctx. */
2477 ev, /* event ctx. */
2478 cli, /* cli_state. */
2479 SMBtrans2, /* cmd. */
2480 NULL, /* pipe name. */
2484 &state->setup, /* setup. */
2485 1, /* num setup uint16_t words. */
2486 0, /* max returned setup. */
2487 state->param, /* param. */
2489 2, /* max returned param. */
2490 state->data, /* data. */
2492 0); /* max returned data. */
2494 if (tevent_req_nomem(subreq, req)) {
2495 return tevent_req_post(req, ev);
2497 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2501 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2505 if (tevent_req_is_nterror(req, &status)) {
2508 return NT_STATUS_OK;
2511 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2513 TALLOC_CTX *frame = talloc_stackframe();
2514 struct event_context *ev = NULL;
2515 struct tevent_req *req = NULL;
2516 NTSTATUS status = NT_STATUS_OK;
2518 if (cli_has_async_calls(cli)) {
2520 * Can't use sync call while an async call is in flight
2522 status = NT_STATUS_INVALID_PARAMETER;
2526 ev = event_context_init(frame);
2528 status = NT_STATUS_NO_MEMORY;
2532 req = cli_ftruncate_send(frame,
2538 status = NT_STATUS_NO_MEMORY;
2542 if (!tevent_req_poll(req, ev)) {
2543 status = map_nt_error_from_unix(errno);
2547 status = cli_ftruncate_recv(req);
2551 if (!NT_STATUS_IS_OK(status)) {
2552 cli_set_error(cli, status);
2557 /****************************************************************************
2558 send a lock with a specified locktype
2559 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2560 ****************************************************************************/
2562 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2563 uint32_t offset, uint32_t len,
2564 int timeout, unsigned char locktype)
2567 int saved_timeout = cli->timeout;
2569 memset(cli->outbuf,'\0',smb_size);
2570 memset(cli->inbuf,'\0', smb_size);
2572 cli_set_message(cli->outbuf,8,0,True);
2574 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2575 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2576 cli_setup_packet(cli);
2578 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2579 SSVAL(cli->outbuf,smb_vwv2,fnum);
2580 SCVAL(cli->outbuf,smb_vwv3,locktype);
2581 SIVALS(cli->outbuf, smb_vwv4, timeout);
2582 SSVAL(cli->outbuf,smb_vwv6,0);
2583 SSVAL(cli->outbuf,smb_vwv7,1);
2585 p = smb_buf(cli->outbuf);
2586 SSVAL(p, 0, cli->pid);
2587 SIVAL(p, 2, offset);
2592 cli_setup_bcc(cli, p);
2597 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
2600 if (!cli_receive_smb(cli)) {
2601 cli->timeout = saved_timeout;
2602 return NT_STATUS_UNSUCCESSFUL;
2605 cli->timeout = saved_timeout;
2607 return cli_nt_error(cli);
2610 /****************************************************************************
2612 note that timeout is in units of 2 milliseconds
2613 ****************************************************************************/
2615 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2616 uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
2619 int saved_timeout = cli->timeout;
2621 memset(cli->outbuf,'\0',smb_size);
2622 memset(cli->inbuf,'\0', smb_size);
2624 cli_set_message(cli->outbuf,8,0,True);
2626 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2627 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2628 cli_setup_packet(cli);
2630 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2631 SSVAL(cli->outbuf,smb_vwv2,fnum);
2632 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
2633 SIVALS(cli->outbuf, smb_vwv4, timeout);
2634 SSVAL(cli->outbuf,smb_vwv6,0);
2635 SSVAL(cli->outbuf,smb_vwv7,1);
2637 p = smb_buf(cli->outbuf);
2638 SSVAL(p, 0, cli->pid);
2639 SIVAL(p, 2, offset);
2644 cli_setup_bcc(cli, p);
2649 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
2652 if (!cli_receive_smb(cli)) {
2653 cli->timeout = saved_timeout;
2657 cli->timeout = saved_timeout;
2659 if (cli_is_error(cli)) {
2666 /****************************************************************************
2668 ****************************************************************************/
2670 struct cli_unlock_state {
2675 static void cli_unlock_done(struct tevent_req *subreq);
2677 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2678 struct event_context *ev,
2679 struct cli_state *cli,
2685 struct tevent_req *req = NULL, *subreq = NULL;
2686 struct cli_unlock_state *state = NULL;
2687 uint8_t additional_flags = 0;
2689 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2694 SCVAL(state->vwv+0, 0, 0xFF);
2695 SSVAL(state->vwv+2, 0, fnum);
2696 SCVAL(state->vwv+3, 0, 0);
2697 SIVALS(state->vwv+4, 0, 0);
2698 SSVAL(state->vwv+6, 0, 1);
2699 SSVAL(state->vwv+7, 0, 0);
2701 SSVAL(state->data, 0, cli->pid);
2702 SIVAL(state->data, 2, offset);
2703 SIVAL(state->data, 6, len);
2705 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2706 8, state->vwv, 10, state->data);
2707 if (tevent_req_nomem(subreq, req)) {
2708 return tevent_req_post(req, ev);
2710 tevent_req_set_callback(subreq, cli_unlock_done, req);
2714 static void cli_unlock_done(struct tevent_req *subreq)
2716 struct tevent_req *req = tevent_req_callback_data(
2717 subreq, struct tevent_req);
2720 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2721 TALLOC_FREE(subreq);
2722 if (!NT_STATUS_IS_OK(status)) {
2723 tevent_req_nterror(req, status);
2726 tevent_req_done(req);
2729 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2731 return tevent_req_simple_recv_ntstatus(req);
2734 NTSTATUS cli_unlock(struct cli_state *cli,
2739 TALLOC_CTX *frame = talloc_stackframe();
2740 struct event_context *ev;
2741 struct tevent_req *req;
2742 NTSTATUS status = NT_STATUS_OK;
2744 if (cli_has_async_calls(cli)) {
2746 * Can't use sync call while an async call is in flight
2748 status = NT_STATUS_INVALID_PARAMETER;
2752 ev = event_context_init(frame);
2754 status = NT_STATUS_NO_MEMORY;
2758 req = cli_unlock_send(frame, ev, cli,
2761 status = NT_STATUS_NO_MEMORY;
2765 if (!tevent_req_poll(req, ev)) {
2766 status = map_nt_error_from_unix(errno);
2770 status = cli_unlock_recv(req);
2774 if (!NT_STATUS_IS_OK(status)) {
2775 cli_set_error(cli, status);
2780 /****************************************************************************
2781 Lock a file with 64 bit offsets.
2782 ****************************************************************************/
2784 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2785 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
2788 int saved_timeout = cli->timeout;
2791 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2792 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2795 ltype = (lock_type == READ_LOCK? 1 : 0);
2796 ltype |= LOCKING_ANDX_LARGE_FILES;
2798 memset(cli->outbuf,'\0',smb_size);
2799 memset(cli->inbuf,'\0', smb_size);
2801 cli_set_message(cli->outbuf,8,0,True);
2803 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2804 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2805 cli_setup_packet(cli);
2807 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2808 SSVAL(cli->outbuf,smb_vwv2,fnum);
2809 SCVAL(cli->outbuf,smb_vwv3,ltype);
2810 SIVALS(cli->outbuf, smb_vwv4, timeout);
2811 SSVAL(cli->outbuf,smb_vwv6,0);
2812 SSVAL(cli->outbuf,smb_vwv7,1);
2814 p = smb_buf(cli->outbuf);
2815 SIVAL(p, 0, cli->pid);
2816 SOFF_T_R(p, 4, offset);
2817 SOFF_T_R(p, 12, len);
2820 cli_setup_bcc(cli, p);
2824 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
2827 if (!cli_receive_smb(cli)) {
2828 cli->timeout = saved_timeout;
2832 cli->timeout = saved_timeout;
2834 if (cli_is_error(cli)) {
2841 /****************************************************************************
2842 Unlock a file with 64 bit offsets.
2843 ****************************************************************************/
2845 struct cli_unlock64_state {
2850 static void cli_unlock64_done(struct tevent_req *subreq);
2852 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2853 struct event_context *ev,
2854 struct cli_state *cli,
2860 struct tevent_req *req = NULL, *subreq = NULL;
2861 struct cli_unlock64_state *state = NULL;
2862 uint8_t additional_flags = 0;
2864 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2869 SCVAL(state->vwv+0, 0, 0xff);
2870 SSVAL(state->vwv+2, 0, fnum);
2871 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2872 SIVALS(state->vwv+4, 0, 0);
2873 SSVAL(state->vwv+6, 0, 1);
2874 SSVAL(state->vwv+7, 0, 0);
2876 SIVAL(state->data, 0, cli->pid);
2877 SOFF_T_R(state->data, 4, offset);
2878 SOFF_T_R(state->data, 12, len);
2880 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2881 8, state->vwv, 20, state->data);
2882 if (tevent_req_nomem(subreq, req)) {
2883 return tevent_req_post(req, ev);
2885 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2889 static void cli_unlock64_done(struct tevent_req *subreq)
2891 struct tevent_req *req = tevent_req_callback_data(
2892 subreq, struct tevent_req);
2895 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2896 TALLOC_FREE(subreq);
2897 if (!NT_STATUS_IS_OK(status)) {
2898 tevent_req_nterror(req, status);
2901 tevent_req_done(req);
2904 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2906 return tevent_req_simple_recv_ntstatus(req);
2909 NTSTATUS cli_unlock64(struct cli_state *cli,
2914 TALLOC_CTX *frame = talloc_stackframe();
2915 struct event_context *ev;
2916 struct tevent_req *req;
2917 NTSTATUS status = NT_STATUS_OK;
2919 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2920 return cli_unlock(cli, fnum, offset, len);
2923 if (cli_has_async_calls(cli)) {
2925 * Can't use sync call while an async call is in flight
2927 status = NT_STATUS_INVALID_PARAMETER;
2931 ev = event_context_init(frame);
2933 status = NT_STATUS_NO_MEMORY;
2937 req = cli_unlock64_send(frame, ev, cli,
2940 status = NT_STATUS_NO_MEMORY;
2944 if (!tevent_req_poll(req, ev)) {
2945 status = map_nt_error_from_unix(errno);
2949 status = cli_unlock64_recv(req);
2953 if (!NT_STATUS_IS_OK(status)) {
2954 cli_set_error(cli, status);
2959 /****************************************************************************
2960 Get/unlock a POSIX lock on a file - internal function.
2961 ****************************************************************************/
2963 struct posix_lock_state {
2966 uint8_t data[POSIX_LOCK_DATA_SIZE];
2969 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2971 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2972 NULL, 0, NULL, NULL, 0, NULL);
2973 tevent_req_simple_finish_ntstatus(subreq, status);
2976 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2977 struct event_context *ev,
2978 struct cli_state *cli,
2983 enum brl_type lock_type)
2985 struct tevent_req *req = NULL, *subreq = NULL;
2986 struct posix_lock_state *state = NULL;
2988 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2993 /* Setup setup word. */
2994 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2996 /* Setup param array. */
2997 SSVAL(&state->param, 0, fnum);
2998 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
3000 /* Setup data array. */
3001 switch (lock_type) {
3003 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3004 POSIX_LOCK_TYPE_READ);
3007 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3008 POSIX_LOCK_TYPE_WRITE);
3011 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
3012 POSIX_LOCK_TYPE_UNLOCK);
3019 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
3020 POSIX_LOCK_FLAG_WAIT);
3022 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
3023 POSIX_LOCK_FLAG_NOWAIT);
3026 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
3027 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
3028 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
3030 subreq = cli_trans_send(state, /* mem ctx. */
3031 ev, /* event ctx. */
3032 cli, /* cli_state. */
3033 SMBtrans2, /* cmd. */
3034 NULL, /* pipe name. */
3038 &state->setup, /* setup. */
3039 1, /* num setup uint16_t words. */
3040 0, /* max returned setup. */
3041 state->param, /* param. */
3043 2, /* max returned param. */
3044 state->data, /* data. */
3045 POSIX_LOCK_DATA_SIZE, /* num data. */
3046 0); /* max returned data. */
3048 if (tevent_req_nomem(subreq, req)) {
3049 return tevent_req_post(req, ev);
3051 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
3055 /****************************************************************************
3057 ****************************************************************************/
3059 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3060 struct event_context *ev,
3061 struct cli_state *cli,
3066 enum brl_type lock_type)
3068 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3069 wait_lock, lock_type);
3072 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3076 if (tevent_req_is_nterror(req, &status)) {
3079 return NT_STATUS_OK;
3082 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3083 uint64_t offset, uint64_t len,
3084 bool wait_lock, enum brl_type lock_type)
3086 TALLOC_CTX *frame = talloc_stackframe();
3087 struct event_context *ev = NULL;
3088 struct tevent_req *req = NULL;
3089 NTSTATUS status = NT_STATUS_OK;
3091 if (cli_has_async_calls(cli)) {
3093 * Can't use sync call while an async call is in flight
3095 status = NT_STATUS_INVALID_PARAMETER;
3099 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3100 status = NT_STATUS_INVALID_PARAMETER;
3104 ev = event_context_init(frame);
3106 status = NT_STATUS_NO_MEMORY;
3110 req = cli_posix_lock_send(frame,
3119 status = NT_STATUS_NO_MEMORY;
3123 if (!tevent_req_poll(req, ev)) {
3124 status = map_nt_error_from_unix(errno);
3128 status = cli_posix_lock_recv(req);
3132 if (!NT_STATUS_IS_OK(status)) {
3133 cli_set_error(cli, status);
3138 /****************************************************************************
3139 POSIX Unlock a file.
3140 ****************************************************************************/
3142 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3143 struct event_context *ev,
3144 struct cli_state *cli,
3149 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3150 false, UNLOCK_LOCK);
3153 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3157 if (tevent_req_is_nterror(req, &status)) {
3160 return NT_STATUS_OK;
3163 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3165 TALLOC_CTX *frame = talloc_stackframe();
3166 struct event_context *ev = NULL;
3167 struct tevent_req *req = NULL;
3168 NTSTATUS status = NT_STATUS_OK;
3170 if (cli_has_async_calls(cli)) {
3172 * Can't use sync call while an async call is in flight
3174 status = NT_STATUS_INVALID_PARAMETER;
3178 ev = event_context_init(frame);
3180 status = NT_STATUS_NO_MEMORY;
3184 req = cli_posix_unlock_send(frame,
3191 status = NT_STATUS_NO_MEMORY;
3195 if (!tevent_req_poll(req, ev)) {
3196 status = map_nt_error_from_unix(errno);
3200 status = cli_posix_unlock_recv(req);
3204 if (!NT_STATUS_IS_OK(status)) {
3205 cli_set_error(cli, status);
3210 /****************************************************************************
3211 Do a SMBgetattrE call.
3212 ****************************************************************************/
3214 static void cli_getattrE_done(struct tevent_req *subreq);
3216 struct cli_getattrE_state {
3226 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3227 struct event_context *ev,
3228 struct cli_state *cli,
3231 struct tevent_req *req = NULL, *subreq = NULL;
3232 struct cli_getattrE_state *state = NULL;
3233 uint8_t additional_flags = 0;
3235 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3240 state->zone_offset = cli->serverzone;
3241 SSVAL(state->vwv+0,0,fnum);
3243 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3244 1, state->vwv, 0, NULL);
3245 if (tevent_req_nomem(subreq, req)) {
3246 return tevent_req_post(req, ev);
3248 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3252 static void cli_getattrE_done(struct tevent_req *subreq)
3254 struct tevent_req *req = tevent_req_callback_data(
3255 subreq, struct tevent_req);
3256 struct cli_getattrE_state *state = tevent_req_data(
3257 req, struct cli_getattrE_state);
3259 uint16_t *vwv = NULL;
3263 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3265 TALLOC_FREE(subreq);
3266 if (!NT_STATUS_IS_OK(status)) {
3267 tevent_req_nterror(req, status);
3271 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3272 state->attr = SVAL(vwv+10,0);
3273 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3274 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3275 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3277 tevent_req_done(req);
3280 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3283 time_t *change_time,
3284 time_t *access_time,
3287 struct cli_getattrE_state *state = tevent_req_data(
3288 req, struct cli_getattrE_state);
3291 if (tevent_req_is_nterror(req, &status)) {
3295 *attr = state->attr;
3298 *size = state->size;
3301 *change_time = state->change_time;
3304 *access_time = state->access_time;
3307 *write_time = state->write_time;
3309 return NT_STATUS_OK;
3312 NTSTATUS cli_getattrE(struct cli_state *cli,
3316 time_t *change_time,
3317 time_t *access_time,
3320 TALLOC_CTX *frame = talloc_stackframe();
3321 struct event_context *ev = NULL;
3322 struct tevent_req *req = NULL;
3323 NTSTATUS status = NT_STATUS_OK;
3325 if (cli_has_async_calls(cli)) {
3327 * Can't use sync call while an async call is in flight
3329 status = NT_STATUS_INVALID_PARAMETER;
3333 ev = event_context_init(frame);
3335 status = NT_STATUS_NO_MEMORY;
3339 req = cli_getattrE_send(frame, ev, cli, fnum);
3341 status = NT_STATUS_NO_MEMORY;
3345 if (!tevent_req_poll(req, ev)) {
3346 status = map_nt_error_from_unix(errno);
3350 status = cli_getattrE_recv(req,
3359 if (!NT_STATUS_IS_OK(status)) {
3360 cli_set_error(cli, status);
3365 /****************************************************************************
3367 ****************************************************************************/
3369 static void cli_getatr_done(struct tevent_req *subreq);
3371 struct cli_getatr_state {
3378 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3379 struct event_context *ev,
3380 struct cli_state *cli,
3383 struct tevent_req *req = NULL, *subreq = NULL;
3384 struct cli_getatr_state *state = NULL;
3385 uint8_t additional_flags = 0;
3386 uint8_t *bytes = NULL;
3388 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3393 state->zone_offset = cli->serverzone;
3395 bytes = talloc_array(state, uint8_t, 1);
3396 if (tevent_req_nomem(bytes, req)) {
3397 return tevent_req_post(req, ev);
3400 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3401 strlen(fname)+1, NULL);
3403 if (tevent_req_nomem(bytes, req)) {
3404 return tevent_req_post(req, ev);
3407 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3408 0, NULL, talloc_get_size(bytes), bytes);
3409 if (tevent_req_nomem(subreq, req)) {
3410 return tevent_req_post(req, ev);
3412 tevent_req_set_callback(subreq, cli_getatr_done, req);
3416 static void cli_getatr_done(struct tevent_req *subreq)
3418 struct tevent_req *req = tevent_req_callback_data(
3419 subreq, struct tevent_req);
3420 struct cli_getatr_state *state = tevent_req_data(
3421 req, struct cli_getatr_state);
3423 uint16_t *vwv = NULL;
3427 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3429 TALLOC_FREE(subreq);
3430 if (!NT_STATUS_IS_OK(status)) {
3431 tevent_req_nterror(req, status);
3435 state->attr = SVAL(vwv+0,0);
3436 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3437 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3439 tevent_req_done(req);
3442 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3447 struct cli_getatr_state *state = tevent_req_data(
3448 req, struct cli_getatr_state);
3451 if (tevent_req_is_nterror(req, &status)) {
3455 *attr = state->attr;
3458 *size = state->size;
3461 *write_time = state->write_time;
3463 return NT_STATUS_OK;
3466 NTSTATUS cli_getatr(struct cli_state *cli,
3472 TALLOC_CTX *frame = talloc_stackframe();
3473 struct event_context *ev = NULL;
3474 struct tevent_req *req = NULL;
3475 NTSTATUS status = NT_STATUS_OK;
3477 if (cli_has_async_calls(cli)) {
3479 * Can't use sync call while an async call is in flight
3481 status = NT_STATUS_INVALID_PARAMETER;
3485 ev = event_context_init(frame);
3487 status = NT_STATUS_NO_MEMORY;
3491 req = cli_getatr_send(frame, ev, cli, fname);
3493 status = NT_STATUS_NO_MEMORY;
3497 if (!tevent_req_poll(req, ev)) {
3498 status = map_nt_error_from_unix(errno);
3502 status = cli_getatr_recv(req,
3509 if (!NT_STATUS_IS_OK(status)) {
3510 cli_set_error(cli, status);
3515 /****************************************************************************
3516 Do a SMBsetattrE call.
3517 ****************************************************************************/
3519 static void cli_setattrE_done(struct tevent_req *subreq);
3521 struct cli_setattrE_state {
3525 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3526 struct event_context *ev,
3527 struct cli_state *cli,
3533 struct tevent_req *req = NULL, *subreq = NULL;
3534 struct cli_setattrE_state *state = NULL;
3535 uint8_t additional_flags = 0;
3537 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3542 SSVAL(state->vwv+0, 0, fnum);
3543 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3545 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3547 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3550 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3551 7, state->vwv, 0, NULL);
3552 if (tevent_req_nomem(subreq, req)) {
3553 return tevent_req_post(req, ev);
3555 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3559 static void cli_setattrE_done(struct tevent_req *subreq)
3561 struct tevent_req *req = tevent_req_callback_data(
3562 subreq, struct tevent_req);
3565 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3566 TALLOC_FREE(subreq);
3567 if (!NT_STATUS_IS_OK(status)) {
3568 tevent_req_nterror(req, status);
3571 tevent_req_done(req);
3574 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3576 return tevent_req_simple_recv_ntstatus(req);
3579 NTSTATUS cli_setattrE(struct cli_state *cli,
3585 TALLOC_CTX *frame = talloc_stackframe();
3586 struct event_context *ev = NULL;
3587 struct tevent_req *req = NULL;
3588 NTSTATUS status = NT_STATUS_OK;
3590 if (cli_has_async_calls(cli)) {
3592 * Can't use sync call while an async call is in flight
3594 status = NT_STATUS_INVALID_PARAMETER;
3598 ev = event_context_init(frame);
3600 status = NT_STATUS_NO_MEMORY;
3604 req = cli_setattrE_send(frame, ev,
3612 status = NT_STATUS_NO_MEMORY;
3616 if (!tevent_req_poll(req, ev)) {
3617 status = map_nt_error_from_unix(errno);
3621 status = cli_setattrE_recv(req);
3625 if (!NT_STATUS_IS_OK(status)) {
3626 cli_set_error(cli, status);
3631 /****************************************************************************
3632 Do a SMBsetatr call.
3633 ****************************************************************************/
3635 static void cli_setatr_done(struct tevent_req *subreq);
3637 struct cli_setatr_state {
3641 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3642 struct event_context *ev,
3643 struct cli_state *cli,
3648 struct tevent_req *req = NULL, *subreq = NULL;
3649 struct cli_setatr_state *state = NULL;
3650 uint8_t additional_flags = 0;
3651 uint8_t *bytes = NULL;
3653 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3658 SSVAL(state->vwv+0, 0, attr);
3659 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
3661 bytes = talloc_array(state, uint8_t, 1);
3662 if (tevent_req_nomem(bytes, req)) {
3663 return tevent_req_post(req, ev);
3666 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3667 strlen(fname)+1, NULL);
3668 if (tevent_req_nomem(bytes, req)) {
3669 return tevent_req_post(req, ev);
3671 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3672 talloc_get_size(bytes)+1);
3673 if (tevent_req_nomem(bytes, req)) {
3674 return tevent_req_post(req, ev);
3677 bytes[talloc_get_size(bytes)-1] = 4;
3678 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3680 if (tevent_req_nomem(bytes, req)) {
3681 return tevent_req_post(req, ev);
3684 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3685 8, state->vwv, talloc_get_size(bytes), bytes);
3686 if (tevent_req_nomem(subreq, req)) {
3687 return tevent_req_post(req, ev);
3689 tevent_req_set_callback(subreq, cli_setatr_done, req);
3693 static void cli_setatr_done(struct tevent_req *subreq)
3695 struct tevent_req *req = tevent_req_callback_data(
3696 subreq, struct tevent_req);
3699 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3700 TALLOC_FREE(subreq);
3701 if (!NT_STATUS_IS_OK(status)) {
3702 tevent_req_nterror(req, status);
3705 tevent_req_done(req);
3708 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3710 return tevent_req_simple_recv_ntstatus(req);
3713 NTSTATUS cli_setatr(struct cli_state *cli,
3718 TALLOC_CTX *frame = talloc_stackframe();
3719 struct event_context *ev = NULL;
3720 struct tevent_req *req = NULL;
3721 NTSTATUS status = NT_STATUS_OK;
3723 if (cli_has_async_calls(cli)) {
3725 * Can't use sync call while an async call is in flight
3727 status = NT_STATUS_INVALID_PARAMETER;
3731 ev = event_context_init(frame);
3733 status = NT_STATUS_NO_MEMORY;
3737 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3739 status = NT_STATUS_NO_MEMORY;
3743 if (!tevent_req_poll(req, ev)) {
3744 status = map_nt_error_from_unix(errno);
3748 status = cli_setatr_recv(req);
3752 if (!NT_STATUS_IS_OK(status)) {
3753 cli_set_error(cli, status);
3758 /****************************************************************************
3759 Check for existance of a dir.
3760 ****************************************************************************/
3762 static void cli_chkpath_done(struct tevent_req *subreq);
3764 struct cli_chkpath_state {
3768 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3769 struct event_context *ev,
3770 struct cli_state *cli,
3773 struct tevent_req *req = NULL, *subreq = NULL;
3774 struct cli_chkpath_state *state = NULL;
3775 uint8_t additional_flags = 0;
3776 uint8_t *bytes = NULL;
3778 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3783 bytes = talloc_array(state, uint8_t, 1);
3784 if (tevent_req_nomem(bytes, req)) {
3785 return tevent_req_post(req, ev);
3788 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3789 strlen(fname)+1, NULL);
3791 if (tevent_req_nomem(bytes, req)) {
3792 return tevent_req_post(req, ev);
3795 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3796 0, NULL, talloc_get_size(bytes), bytes);
3797 if (tevent_req_nomem(subreq, req)) {
3798 return tevent_req_post(req, ev);
3800 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3804 static void cli_chkpath_done(struct tevent_req *subreq)
3806 struct tevent_req *req = tevent_req_callback_data(
3807 subreq, struct tevent_req);
3810 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3811 TALLOC_FREE(subreq);
3812 if (!NT_STATUS_IS_OK(status)) {
3813 tevent_req_nterror(req, status);
3816 tevent_req_done(req);
3819 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3821 return tevent_req_simple_recv_ntstatus(req);
3824 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3826 TALLOC_CTX *frame = talloc_stackframe();
3827 struct event_context *ev = NULL;
3828 struct tevent_req *req = NULL;
3830 NTSTATUS status = NT_STATUS_OK;
3832 if (cli_has_async_calls(cli)) {
3834 * Can't use sync call while an async call is in flight
3836 status = NT_STATUS_INVALID_PARAMETER;
3840 path2 = talloc_strdup(frame, path);
3842 status = NT_STATUS_NO_MEMORY;
3845 trim_char(path2,'\0','\\');
3847 path2 = talloc_strdup(frame, "\\");
3849 status = NT_STATUS_NO_MEMORY;
3854 ev = event_context_init(frame);
3856 status = NT_STATUS_NO_MEMORY;
3860 req = cli_chkpath_send(frame, ev, cli, path2);
3862 status = NT_STATUS_NO_MEMORY;
3866 if (!tevent_req_poll(req, ev)) {
3867 status = map_nt_error_from_unix(errno);
3871 status = cli_chkpath_recv(req);
3875 if (!NT_STATUS_IS_OK(status)) {
3876 cli_set_error(cli, status);
3881 /****************************************************************************
3883 ****************************************************************************/
3885 static void cli_dskattr_done(struct tevent_req *subreq);
3887 struct cli_dskattr_state {
3893 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3894 struct event_context *ev,
3895 struct cli_state *cli)
3897 struct tevent_req *req = NULL, *subreq = NULL;
3898 struct cli_dskattr_state *state = NULL;
3899 uint8_t additional_flags = 0;
3901 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3906 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3908 if (tevent_req_nomem(subreq, req)) {
3909 return tevent_req_post(req, ev);
3911 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3915 static void cli_dskattr_done(struct tevent_req *subreq)
3917 struct tevent_req *req = tevent_req_callback_data(
3918 subreq, struct tevent_req);
3919 struct cli_dskattr_state *state = tevent_req_data(
3920 req, struct cli_dskattr_state);
3922 uint16_t *vwv = NULL;
3926 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3928 TALLOC_FREE(subreq);
3929 if (!NT_STATUS_IS_OK(status)) {
3930 tevent_req_nterror(req, status);
3933 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3934 state->total = SVAL(vwv+0, 0);
3935 state->avail = SVAL(vwv+3, 0);
3936 tevent_req_done(req);
3939 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3941 struct cli_dskattr_state *state = tevent_req_data(
3942 req, struct cli_dskattr_state);
3945 if (tevent_req_is_nterror(req, &status)) {
3948 *bsize = state->bsize;
3949 *total = state->total;
3950 *avail = state->avail;
3951 return NT_STATUS_OK;
3954 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3956 TALLOC_CTX *frame = talloc_stackframe();
3957 struct event_context *ev = NULL;
3958 struct tevent_req *req = NULL;
3959 NTSTATUS status = NT_STATUS_OK;
3961 if (cli_has_async_calls(cli)) {
3963 * Can't use sync call while an async call is in flight
3965 status = NT_STATUS_INVALID_PARAMETER;
3969 ev = event_context_init(frame);
3971 status = NT_STATUS_NO_MEMORY;
3975 req = cli_dskattr_send(frame, ev, cli);
3977 status = NT_STATUS_NO_MEMORY;
3981 if (!tevent_req_poll(req, ev)) {
3982 status = map_nt_error_from_unix(errno);
3986 status = cli_dskattr_recv(req, bsize, total, avail);
3990 if (!NT_STATUS_IS_OK(status)) {
3991 cli_set_error(cli, status);
3996 /****************************************************************************
3997 Create and open a temporary file.
3998 ****************************************************************************/
4000 static void cli_ctemp_done(struct tevent_req *subreq);
4002 struct ctemp_state {
4008 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
4009 struct event_context *ev,
4010 struct cli_state *cli,
4013 struct tevent_req *req = NULL, *subreq = NULL;
4014 struct ctemp_state *state = NULL;
4015 uint8_t additional_flags = 0;
4016 uint8_t *bytes = NULL;
4018 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
4023 SSVAL(state->vwv,0,0);
4024 SIVALS(state->vwv+1,0,-1);
4026 bytes = talloc_array(state, uint8_t, 1);
4027 if (tevent_req_nomem(bytes, req)) {
4028 return tevent_req_post(req, ev);
4031 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
4032 strlen(path)+1, NULL);
4033 if (tevent_req_nomem(bytes, req)) {
4034 return tevent_req_post(req, ev);
4037 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
4038 3, state->vwv, talloc_get_size(bytes), bytes);
4039 if (tevent_req_nomem(subreq, req)) {
4040 return tevent_req_post(req, ev);
4042 tevent_req_set_callback(subreq, cli_ctemp_done, req);
4046 static void cli_ctemp_done(struct tevent_req *subreq)
4048 struct tevent_req *req = tevent_req_callback_data(
4049 subreq, struct tevent_req);
4050 struct ctemp_state *state = tevent_req_data(
4051 req, struct ctemp_state);
4055 uint32_t num_bytes = 0;
4056 uint8_t *bytes = NULL;
4059 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
4060 &num_bytes, &bytes);
4061 TALLOC_FREE(subreq);
4062 if (!NT_STATUS_IS_OK(status)) {
4063 tevent_req_nterror(req, status);
4067 state->fnum = SVAL(vwv+0, 0);
4069 /* From W2K3, the result is just the ASCII name */
4070 if (num_bytes < 2) {
4071 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4075 if (pull_string_talloc(state,
4082 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4085 tevent_req_done(req);
4088 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4093 struct ctemp_state *state = tevent_req_data(req,
4094 struct ctemp_state);
4097 if (tevent_req_is_nterror(req, &status)) {
4100 *pfnum = state->fnum;
4101 *outfile = talloc_strdup(ctx, state->ret_path);
4103 return NT_STATUS_NO_MEMORY;
4105 return NT_STATUS_OK;
4108 NTSTATUS cli_ctemp(struct cli_state *cli,
4114 TALLOC_CTX *frame = talloc_stackframe();
4115 struct event_context *ev;
4116 struct tevent_req *req;
4117 NTSTATUS status = NT_STATUS_OK;
4119 if (cli_has_async_calls(cli)) {
4121 * Can't use sync call while an async call is in flight
4123 status = NT_STATUS_INVALID_PARAMETER;
4127 ev = event_context_init(frame);
4129 status = NT_STATUS_NO_MEMORY;
4133 req = cli_ctemp_send(frame, ev, cli, path);
4135 status = NT_STATUS_NO_MEMORY;
4139 if (!tevent_req_poll(req, ev)) {
4140 status = map_nt_error_from_unix(errno);
4144 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4148 if (!NT_STATUS_IS_OK(status)) {
4149 cli_set_error(cli, status);
4155 send a raw ioctl - used by the torture code
4157 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4161 struct tevent_req *result_parent;
4163 SSVAL(vwv+0, 0, fnum);
4164 SSVAL(vwv+1, 0, code>>16);
4165 SSVAL(vwv+2, 0, (code&0xFFFF));
4167 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4168 &result_parent, 0, NULL, NULL, NULL, NULL);
4169 if (!NT_STATUS_IS_OK(status)) {
4172 *blob = data_blob_null;
4173 return NT_STATUS_OK;
4176 /*********************************************************
4177 Set an extended attribute utility fn.
4178 *********************************************************/
4180 static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsigned int param_len,
4181 const char *ea_name, const char *ea_val, size_t ea_len)
4183 unsigned int data_len = 0;
4185 char *rparam=NULL, *rdata=NULL;
4187 size_t ea_namelen = strlen(ea_name);
4189 if (ea_namelen == 0 && ea_len == 0) {
4191 data = (char *)SMB_MALLOC(data_len);
4196 SIVAL(p,0,data_len);
4198 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4199 data = (char *)SMB_MALLOC(data_len);
4204 SIVAL(p,0,data_len);
4206 SCVAL(p, 0, 0); /* EA flags. */
4207 SCVAL(p, 1, ea_namelen);
4208 SSVAL(p, 2, ea_len);
4209 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4210 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4213 if (!cli_send_trans(cli, SMBtrans2,
4215 -1, 0, /* fid, flags */
4216 &setup, 1, 0, /* setup, length, max */
4217 param, param_len, 2, /* param, length, max */
4218 data, data_len, cli->max_xmit /* data, length, max */
4224 if (!cli_receive_trans(cli, SMBtrans2,
4225 &rparam, ¶m_len,
4226 &rdata, &data_len)) {
4238 /*********************************************************
4239 Set an extended attribute on a pathname.
4240 *********************************************************/
4242 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
4244 uint16_t setup = TRANSACT2_SETPATHINFO;
4245 unsigned int param_len = 0;
4247 size_t srclen = 2*(strlen(path)+1);
4251 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
4255 memset(param, '\0', 6);
4256 SSVAL(param,0,SMB_INFO_SET_EA);
4259 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4260 param_len = PTR_DIFF(p, param);
4262 ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
4267 /*********************************************************
4268 Set an extended attribute on an fnum.
4269 *********************************************************/
4271 bool cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum, const char *ea_name, const char *ea_val, size_t ea_len)
4274 uint16_t setup = TRANSACT2_SETFILEINFO;
4276 memset(param, 0, 6);
4277 SSVAL(param,0,fnum);
4278 SSVAL(param,2,SMB_INFO_SET_EA);
4280 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
4283 /*********************************************************
4284 Get an extended attribute list utility fn.
4285 *********************************************************/
4287 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4289 size_t *pnum_eas, struct ea_struct **pea_list)
4291 struct ea_struct *ea_list = NULL;
4296 if (rdata_len < 4) {
4300 ea_size = (size_t)IVAL(rdata,0);
4301 if (ea_size > rdata_len) {
4306 /* No EA's present. */
4315 /* Validate the EA list and count it. */
4316 for (num_eas = 0; ea_size >= 4; num_eas++) {
4317 unsigned int ea_namelen = CVAL(p,1);
4318 unsigned int ea_valuelen = SVAL(p,2);
4319 if (ea_namelen == 0) {
4322 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4325 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4326 p += 4 + ea_namelen + 1 + ea_valuelen;
4335 *pnum_eas = num_eas;
4337 /* Caller only wants number of EA's. */
4341 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
4346 ea_size = (size_t)IVAL(rdata,0);
4349 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4350 struct ea_struct *ea = &ea_list[num_eas];
4351 fstring unix_ea_name;
4352 unsigned int ea_namelen = CVAL(p,1);
4353 unsigned int ea_valuelen = SVAL(p,2);
4355 ea->flags = CVAL(p,0);
4356 unix_ea_name[0] = '\0';
4357 pull_ascii_fstring(unix_ea_name, p + 4);
4358 ea->name = talloc_strdup(ea_list, unix_ea_name);
4362 /* Ensure the value is null terminated (in case it's a string). */
4363 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4364 if (!ea->value.data) {
4368 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4370 ea->value.data[ea_valuelen] = 0;
4372 p += 4 + ea_namelen + 1 + ea_valuelen;
4375 *pea_list = ea_list;
4379 TALLOC_FREE(ea_list);
4383 /*********************************************************
4384 Get an extended attribute list from a pathname.
4385 *********************************************************/
4387 struct cli_get_ea_list_path_state {
4392 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4394 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4395 struct tevent_context *ev,
4396 struct cli_state *cli,
4399 struct tevent_req *req, *subreq;
4400 struct cli_get_ea_list_path_state *state;
4402 req = tevent_req_create(mem_ctx, &state,
4403 struct cli_get_ea_list_path_state);
4407 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4408 SMB_INFO_QUERY_ALL_EAS, 4,
4410 if (tevent_req_nomem(subreq, req)) {
4411 return tevent_req_post(req, ev);
4413 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4417 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4419 struct tevent_req *req = tevent_req_callback_data(
4420 subreq, struct tevent_req);
4421 struct cli_get_ea_list_path_state *state = tevent_req_data(
4422 req, struct cli_get_ea_list_path_state);
4425 status = cli_qpathinfo_recv(subreq, state, &state->data,
4427 TALLOC_FREE(subreq);
4428 if (!NT_STATUS_IS_OK(status)) {
4429 tevent_req_nterror(req, status);
4432 tevent_req_done(req);
4435 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4436 size_t *pnum_eas, struct ea_struct **peas)
4438 struct cli_get_ea_list_path_state *state = tevent_req_data(
4439 req, struct cli_get_ea_list_path_state);
4442 if (tevent_req_is_nterror(req, &status)) {
4445 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4447 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4449 return NT_STATUS_OK;
4452 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4455 struct ea_struct **pea_list)
4457 TALLOC_CTX *frame = talloc_stackframe();
4458 struct event_context *ev = NULL;
4459 struct tevent_req *req = NULL;
4460 NTSTATUS status = NT_STATUS_NO_MEMORY;
4462 if (cli_has_async_calls(cli)) {
4464 * Can't use sync call while an async call is in flight
4466 status = NT_STATUS_INVALID_PARAMETER;
4469 ev = event_context_init(frame);
4473 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4477 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4480 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4483 if (!NT_STATUS_IS_OK(status)) {
4484 cli_set_error(cli, status);
4489 /****************************************************************************
4490 Convert open "flags" arg to uint32_t on wire.
4491 ****************************************************************************/
4493 static uint32_t open_flags_to_wire(int flags)
4495 int open_mode = flags & O_ACCMODE;
4498 switch (open_mode) {
4500 ret |= SMB_O_WRONLY;
4507 ret |= SMB_O_RDONLY;
4511 if (flags & O_CREAT) {
4514 if (flags & O_EXCL) {
4517 if (flags & O_TRUNC) {
4521 if (flags & O_SYNC) {
4525 if (flags & O_APPEND) {
4526 ret |= SMB_O_APPEND;
4528 #if defined(O_DIRECT)
4529 if (flags & O_DIRECT) {
4530 ret |= SMB_O_DIRECT;
4533 #if defined(O_DIRECTORY)
4534 if (flags & O_DIRECTORY) {
4535 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4536 ret |= SMB_O_DIRECTORY;
4542 /****************************************************************************
4543 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4544 ****************************************************************************/
4546 struct posix_open_state {
4550 uint16_t fnum; /* Out */
4553 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4555 struct tevent_req *req = tevent_req_callback_data(
4556 subreq, struct tevent_req);
4557 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4562 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4563 NULL, 0, NULL, &data, 12, &num_data);
4564 TALLOC_FREE(subreq);
4565 if (!NT_STATUS_IS_OK(status)) {
4566 tevent_req_nterror(req, status);
4569 state->fnum = SVAL(data,2);
4570 tevent_req_done(req);
4573 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4574 struct event_context *ev,
4575 struct cli_state *cli,
4581 struct tevent_req *req = NULL, *subreq = NULL;
4582 struct posix_open_state *state = NULL;
4583 uint32_t wire_flags = open_flags_to_wire(flags);
4585 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4590 /* Setup setup word. */
4591 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4593 /* Setup param array. */
4594 state->param = talloc_array(state, uint8_t, 6);
4595 if (tevent_req_nomem(state->param, req)) {
4596 return tevent_req_post(req, ev);
4598 memset(state->param, '\0', 6);
4599 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4601 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4602 strlen(fname)+1, NULL);
4604 if (tevent_req_nomem(state->param, req)) {
4605 return tevent_req_post(req, ev);
4608 /* Setup data words. */
4610 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4611 wire_flags |= SMB_O_DIRECTORY;
4614 SIVAL(state->data,0,0); /* No oplock. */
4615 SIVAL(state->data,4,wire_flags);
4616 SIVAL(state->data,8,unix_perms_to_wire(mode));
4617 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4618 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4620 subreq = cli_trans_send(state, /* mem ctx. */
4621 ev, /* event ctx. */
4622 cli, /* cli_state. */
4623 SMBtrans2, /* cmd. */
4624 NULL, /* pipe name. */
4628 &state->setup, /* setup. */
4629 1, /* num setup uint16_t words. */
4630 0, /* max returned setup. */
4631 state->param, /* param. */
4632 talloc_get_size(state->param),/* num param. */
4633 2, /* max returned param. */
4634 state->data, /* data. */
4636 12); /* max returned data. */
4638 if (tevent_req_nomem(subreq, req)) {
4639 return tevent_req_post(req, ev);
4641 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4645 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4646 struct event_context *ev,
4647 struct cli_state *cli,
4652 return cli_posix_open_internal_send(mem_ctx, ev,
4653 cli, fname, flags, mode, false);
4656 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4658 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4661 if (tevent_req_is_nterror(req, &status)) {
4664 *pfnum = state->fnum;
4665 return NT_STATUS_OK;
4668 /****************************************************************************
4669 Open - POSIX semantics. Doesn't request oplock.
4670 ****************************************************************************/
4672 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4673 int flags, mode_t mode, uint16_t *pfnum)
4676 TALLOC_CTX *frame = talloc_stackframe();
4677 struct event_context *ev = NULL;
4678 struct tevent_req *req = NULL;
4679 NTSTATUS status = NT_STATUS_OK;
4681 if (cli_has_async_calls(cli)) {
4683 * Can't use sync call while an async call is in flight
4685 status = NT_STATUS_INVALID_PARAMETER;
4689 ev = event_context_init(frame);
4691 status = NT_STATUS_NO_MEMORY;
4695 req = cli_posix_open_send(frame,
4702 status = NT_STATUS_NO_MEMORY;
4706 if (!tevent_req_poll(req, ev)) {
4707 status = map_nt_error_from_unix(errno);
4711 status = cli_posix_open_recv(req, pfnum);
4715 if (!NT_STATUS_IS_OK(status)) {
4716 cli_set_error(cli, status);
4721 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4722 struct event_context *ev,
4723 struct cli_state *cli,
4727 return cli_posix_open_internal_send(mem_ctx, ev,
4728 cli, fname, O_CREAT, mode, true);
4731 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4735 if (tevent_req_is_nterror(req, &status)) {
4738 return NT_STATUS_OK;
4741 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4743 TALLOC_CTX *frame = talloc_stackframe();
4744 struct event_context *ev = NULL;
4745 struct tevent_req *req = NULL;
4746 NTSTATUS status = NT_STATUS_OK;
4748 if (cli_has_async_calls(cli)) {
4750 * Can't use sync call while an async call is in flight
4752 status = NT_STATUS_INVALID_PARAMETER;
4756 ev = event_context_init(frame);
4758 status = NT_STATUS_NO_MEMORY;
4762 req = cli_posix_mkdir_send(frame,
4768 status = NT_STATUS_NO_MEMORY;
4772 if (!tevent_req_poll(req, ev)) {
4773 status = map_nt_error_from_unix(errno);
4777 status = cli_posix_mkdir_recv(req);
4781 if (!NT_STATUS_IS_OK(status)) {
4782 cli_set_error(cli, status);
4787 /****************************************************************************
4788 unlink or rmdir - POSIX semantics.
4789 ****************************************************************************/
4791 struct unlink_state {
4796 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4798 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
4799 NULL, 0, NULL, NULL, 0, NULL);
4800 tevent_req_simple_finish_ntstatus(subreq, status);
4803 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4804 struct event_context *ev,
4805 struct cli_state *cli,
4809 struct tevent_req *req = NULL, *subreq = NULL;
4810 struct unlink_state *state = NULL;
4811 uint8_t *param = NULL;
4813 req = tevent_req_create(mem_ctx, &state, struct unlink_state);
4818 /* Setup setup word. */
4819 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4821 /* Setup param array. */
4822 param = talloc_array(state, uint8_t, 6);
4823 if (tevent_req_nomem(param, req)) {
4824 return tevent_req_post(req, ev);
4826 memset(param, '\0', 6);
4827 SSVAL(param, 0, SMB_POSIX_PATH_UNLINK);
4829 param = trans2_bytes_push_str(param, cli_ucs2(cli), fname,
4830 strlen(fname)+1, NULL);
4832 if (tevent_req_nomem(param, req)) {
4833 return tevent_req_post(req, ev);
4836 /* Setup data word. */
4837 SSVAL(state->data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
4838 SMB_POSIX_UNLINK_FILE_TARGET);
4840 subreq = cli_trans_send(state, /* mem ctx. */
4841 ev, /* event ctx. */
4842 cli, /* cli_state. */
4843 SMBtrans2, /* cmd. */
4844 NULL, /* pipe name. */
4848 &state->setup, /* setup. */
4849 1, /* num setup uint16_t words. */
4850 0, /* max returned setup. */
4852 talloc_get_size(param), /* num param. */
4853 2, /* max returned param. */
4854 state->data, /* data. */
4856 0); /* max returned data. */
4858 if (tevent_req_nomem(subreq, req)) {
4859 return tevent_req_post(req, ev);
4861 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4865 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4866 struct event_context *ev,
4867 struct cli_state *cli,
4870 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, false);
4873 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4877 if (tevent_req_is_nterror(req, &status)) {
4880 return NT_STATUS_OK;
4883 /****************************************************************************
4884 unlink - POSIX semantics.
4885 ****************************************************************************/
4887 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4889 TALLOC_CTX *frame = talloc_stackframe();
4890 struct event_context *ev = NULL;
4891 struct tevent_req *req = NULL;
4892 NTSTATUS status = NT_STATUS_OK;
4894 if (cli_has_async_calls(cli)) {
4896 * Can't use sync call while an async call is in flight
4898 status = NT_STATUS_INVALID_PARAMETER;
4902 ev = event_context_init(frame);
4904 status = NT_STATUS_NO_MEMORY;
4908 req = cli_posix_unlink_send(frame,
4913 status = NT_STATUS_NO_MEMORY;
4917 if (!tevent_req_poll(req, ev)) {
4918 status = map_nt_error_from_unix(errno);
4922 status = cli_posix_unlink_recv(req);
4926 if (!NT_STATUS_IS_OK(status)) {
4927 cli_set_error(cli, status);
4932 /****************************************************************************
4933 rmdir - POSIX semantics.
4934 ****************************************************************************/
4936 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4937 struct event_context *ev,
4938 struct cli_state *cli,
4941 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, true);
4944 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4948 if (tevent_req_is_nterror(req, &status)) {
4951 return NT_STATUS_OK;
4954 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4956 TALLOC_CTX *frame = talloc_stackframe();
4957 struct event_context *ev = NULL;
4958 struct tevent_req *req = NULL;
4959 NTSTATUS status = NT_STATUS_OK;
4961 if (cli_has_async_calls(cli)) {
4963 * Can't use sync call while an async call is in flight
4965 status = NT_STATUS_INVALID_PARAMETER;
4969 ev = event_context_init(frame);
4971 status = NT_STATUS_NO_MEMORY;
4975 req = cli_posix_rmdir_send(frame,
4980 status = NT_STATUS_NO_MEMORY;
4984 if (!tevent_req_poll(req, ev)) {
4985 status = map_nt_error_from_unix(errno);
4989 status = cli_posix_rmdir_recv(req, frame);
4993 if (!NT_STATUS_IS_OK(status)) {
4994 cli_set_error(cli, status);
4999 /****************************************************************************
5001 ****************************************************************************/
5003 struct cli_notify_state {
5005 uint32_t num_changes;
5006 struct notify_change *changes;
5009 static void cli_notify_done(struct tevent_req *subreq);
5011 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
5012 struct tevent_context *ev,
5013 struct cli_state *cli, uint16_t fnum,
5014 uint32_t buffer_size,
5015 uint32_t completion_filter, bool recursive)
5017 struct tevent_req *req, *subreq;
5018 struct cli_notify_state *state;
5020 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
5025 SIVAL(state->setup, 0, completion_filter);
5026 SSVAL(state->setup, 4, fnum);
5027 SSVAL(state->setup, 6, recursive);
5029 subreq = cli_trans_send(
5030 state, /* mem ctx. */
5031 ev, /* event ctx. */
5032 cli, /* cli_state. */
5033 SMBnttrans, /* cmd. */
5034 NULL, /* pipe name. */
5036 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
5038 (uint16_t *)state->setup, /* setup. */
5039 4, /* num setup uint16_t words. */
5040 0, /* max returned setup. */
5043 buffer_size, /* max returned param. */
5046 0); /* max returned data. */
5048 if (tevent_req_nomem(subreq, req)) {
5049 return tevent_req_post(req, ev);
5051 tevent_req_set_callback(subreq, cli_notify_done, req);
5055 static void cli_notify_done(struct tevent_req *subreq)
5057 struct tevent_req *req = tevent_req_callback_data(
5058 subreq, struct tevent_req);
5059 struct cli_notify_state *state = tevent_req_data(
5060 req, struct cli_notify_state);
5063 uint32_t i, ofs, num_params;
5066 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
5067 ¶ms, 0, &num_params, NULL, 0, NULL);
5068 TALLOC_FREE(subreq);
5069 if (!NT_STATUS_IS_OK(status)) {
5070 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
5071 tevent_req_nterror(req, status);
5075 state->num_changes = 0;
5078 while (num_params - ofs > 12) {
5079 uint32_t len = IVAL(params, ofs);
5080 state->num_changes += 1;
5082 if ((len == 0) || (ofs+len >= num_params)) {
5088 state->changes = talloc_array(state, struct notify_change,
5089 state->num_changes);
5090 if (tevent_req_nomem(state->changes, req)) {
5091 TALLOC_FREE(params);
5097 for (i=0; i<state->num_changes; i++) {
5098 uint32_t next = IVAL(params, ofs);
5099 uint32_t len = IVAL(params, ofs+8);
5103 if ((next != 0) && (len+12 != next)) {
5104 TALLOC_FREE(params);
5106 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5110 state->changes[i].action = IVAL(params, ofs+4);
5111 ret = clistr_pull_talloc(params, (char *)params, flags2,
5112 &name, params+ofs+12, len,
5113 STR_TERMINATE|STR_UNICODE);
5115 TALLOC_FREE(params);
5116 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5119 state->changes[i].name = name;
5123 TALLOC_FREE(params);
5124 tevent_req_done(req);
5127 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5128 uint32_t *pnum_changes,
5129 struct notify_change **pchanges)
5131 struct cli_notify_state *state = tevent_req_data(
5132 req, struct cli_notify_state);
5135 if (tevent_req_is_nterror(req, &status)) {
5139 *pnum_changes = state->num_changes;
5140 *pchanges = talloc_move(mem_ctx, &state->changes);
5141 return NT_STATUS_OK;
5144 struct cli_qpathinfo_state {
5153 static void cli_qpathinfo_done(struct tevent_req *subreq);
5155 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5156 struct tevent_context *ev,
5157 struct cli_state *cli, const char *fname,
5158 uint16_t level, uint32_t min_rdata,
5161 struct tevent_req *req, *subreq;
5162 struct cli_qpathinfo_state *state;
5164 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5168 state->min_rdata = min_rdata;
5169 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5171 state->param = talloc_zero_array(state, uint8_t, 6);
5172 if (tevent_req_nomem(state->param, req)) {
5173 return tevent_req_post(req, ev);
5175 SSVAL(state->param, 0, level);
5176 state->param = trans2_bytes_push_str(
5177 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
5178 if (tevent_req_nomem(state->param, req)) {
5179 return tevent_req_post(req, ev);
5182 subreq = cli_trans_send(
5183 state, /* mem ctx. */
5184 ev, /* event ctx. */
5185 cli, /* cli_state. */
5186 SMBtrans2, /* cmd. */
5187 NULL, /* pipe name. */
5191 state->setup, /* setup. */
5192 1, /* num setup uint16_t words. */
5193 0, /* max returned setup. */
5194 state->param, /* param. */
5195 talloc_get_size(state->param), /* num param. */
5196 2, /* max returned param. */
5199 max_rdata); /* max returned data. */
5201 if (tevent_req_nomem(subreq, req)) {
5202 return tevent_req_post(req, ev);
5204 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5208 static void cli_qpathinfo_done(struct tevent_req *subreq)
5210 struct tevent_req *req = tevent_req_callback_data(
5211 subreq, struct tevent_req);
5212 struct cli_qpathinfo_state *state = tevent_req_data(
5213 req, struct cli_qpathinfo_state);
5216 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5218 &state->rdata, state->min_rdata,
5220 if (!NT_STATUS_IS_OK(status)) {
5221 tevent_req_nterror(req, status);
5224 tevent_req_done(req);
5227 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5228 uint8_t **rdata, uint32_t *num_rdata)
5230 struct cli_qpathinfo_state *state = tevent_req_data(
5231 req, struct cli_qpathinfo_state);
5234 if (tevent_req_is_nterror(req, &status)) {
5237 if (rdata != NULL) {
5238 *rdata = talloc_move(mem_ctx, &state->rdata);
5240 TALLOC_FREE(state->rdata);
5242 if (num_rdata != NULL) {
5243 *num_rdata = state->num_rdata;
5245 return NT_STATUS_OK;
5248 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5249 const char *fname, uint16_t level, uint32_t min_rdata,
5251 uint8_t **rdata, uint32_t *num_rdata)
5253 TALLOC_CTX *frame = talloc_stackframe();
5254 struct event_context *ev;
5255 struct tevent_req *req;
5256 NTSTATUS status = NT_STATUS_NO_MEMORY;
5258 if (cli_has_async_calls(cli)) {
5260 * Can't use sync call while an async call is in flight
5262 status = NT_STATUS_INVALID_PARAMETER;
5265 ev = event_context_init(frame);
5269 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5274 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5277 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5280 if (!NT_STATUS_IS_OK(status)) {
5281 cli_set_error(cli, status);
5286 struct cli_qfileinfo_state {
5295 static void cli_qfileinfo_done(struct tevent_req *subreq);
5297 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5298 struct tevent_context *ev,
5299 struct cli_state *cli, uint16_t fnum,
5300 uint16_t level, uint32_t min_rdata,
5303 struct tevent_req *req, *subreq;
5304 struct cli_qfileinfo_state *state;
5306 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5310 state->min_rdata = min_rdata;
5311 SSVAL(state->param, 0, fnum);
5312 SSVAL(state->param, 2, level);
5313 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5315 subreq = cli_trans_send(
5316 state, /* mem ctx. */
5317 ev, /* event ctx. */
5318 cli, /* cli_state. */
5319 SMBtrans2, /* cmd. */
5320 NULL, /* pipe name. */
5324 state->setup, /* setup. */
5325 1, /* num setup uint16_t words. */
5326 0, /* max returned setup. */
5327 state->param, /* param. */
5328 sizeof(state->param), /* num param. */
5329 2, /* max returned param. */
5332 max_rdata); /* max returned data. */
5334 if (tevent_req_nomem(subreq, req)) {
5335 return tevent_req_post(req, ev);
5337 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5341 static void cli_qfileinfo_done(struct tevent_req *subreq)
5343 struct tevent_req *req = tevent_req_callback_data(
5344 subreq, struct tevent_req);
5345 struct cli_qfileinfo_state *state = tevent_req_data(
5346 req, struct cli_qfileinfo_state);
5349 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5351 &state->rdata, state->min_rdata,
5353 if (!NT_STATUS_IS_OK(status)) {
5354 tevent_req_nterror(req, status);
5357 tevent_req_done(req);
5360 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5361 uint8_t **rdata, uint32_t *num_rdata)
5363 struct cli_qfileinfo_state *state = tevent_req_data(
5364 req, struct cli_qfileinfo_state);
5367 if (tevent_req_is_nterror(req, &status)) {
5370 if (rdata != NULL) {
5371 *rdata = talloc_move(mem_ctx, &state->rdata);
5373 TALLOC_FREE(state->rdata);
5375 if (num_rdata != NULL) {
5376 *num_rdata = state->num_rdata;
5378 return NT_STATUS_OK;
5381 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5382 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5384 uint8_t **rdata, uint32_t *num_rdata)
5386 TALLOC_CTX *frame = talloc_stackframe();
5387 struct event_context *ev;
5388 struct tevent_req *req;
5389 NTSTATUS status = NT_STATUS_NO_MEMORY;
5391 if (cli_has_async_calls(cli)) {
5393 * Can't use sync call while an async call is in flight
5395 status = NT_STATUS_INVALID_PARAMETER;
5398 ev = event_context_init(frame);
5402 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5407 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5410 status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
5413 if (!NT_STATUS_IS_OK(status)) {
5414 cli_set_error(cli, status);
5419 struct cli_flush_state {
5423 static void cli_flush_done(struct tevent_req *subreq);
5425 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5426 struct event_context *ev,
5427 struct cli_state *cli,
5430 struct tevent_req *req, *subreq;
5431 struct cli_flush_state *state;
5433 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5437 SSVAL(state->vwv + 0, 0, fnum);
5439 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5441 if (tevent_req_nomem(subreq, req)) {
5442 return tevent_req_post(req, ev);
5444 tevent_req_set_callback(subreq, cli_flush_done, req);
5448 static void cli_flush_done(struct tevent_req *subreq)
5450 struct tevent_req *req = tevent_req_callback_data(
5451 subreq, struct tevent_req);
5454 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5455 TALLOC_FREE(subreq);
5456 if (!NT_STATUS_IS_OK(status)) {
5457 tevent_req_nterror(req, status);
5460 tevent_req_done(req);
5463 NTSTATUS cli_flush_recv(struct tevent_req *req)
5465 return tevent_req_simple_recv_ntstatus(req);
5468 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5470 TALLOC_CTX *frame = talloc_stackframe();
5471 struct event_context *ev;
5472 struct tevent_req *req;
5473 NTSTATUS status = NT_STATUS_NO_MEMORY;
5475 if (cli_has_async_calls(cli)) {
5477 * Can't use sync call while an async call is in flight
5479 status = NT_STATUS_INVALID_PARAMETER;
5482 ev = event_context_init(frame);
5486 req = cli_flush_send(frame, ev, cli, fnum);
5490 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5493 status = cli_flush_recv(req);
5496 if (!NT_STATUS_IS_OK(status)) {
5497 cli_set_error(cli, status);