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"
23 #include "libsmb/clirap.h"
25 /***********************************************************
26 Common function for pushing stings, used by smb_bytes_push_str()
27 and trans_bytes_push_str(). Only difference is the align_odd
29 ***********************************************************/
31 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
32 const char *str, size_t str_len,
34 size_t *pconverted_size)
38 size_t converted_size;
44 buflen = talloc_get_size(buf);
46 if (align_odd && ucs2 && (buflen % 2 == 0)) {
48 * We're pushing into an SMB buffer, align odd
50 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
58 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
59 ucs2 ? CH_UTF16LE : CH_DOS,
60 str, str_len, &converted,
65 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
66 buflen + converted_size);
68 TALLOC_FREE(converted);
72 memcpy(buf + buflen, converted, converted_size);
74 TALLOC_FREE(converted);
76 if (pconverted_size) {
77 *pconverted_size = converted_size;
83 /***********************************************************
84 Push a string into an SMB buffer, with odd byte alignment
85 if it's a UCS2 string.
86 ***********************************************************/
88 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
89 const char *str, size_t str_len,
90 size_t *pconverted_size)
92 return internal_bytes_push_str(buf, ucs2, str, str_len,
93 true, pconverted_size);
96 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
97 const uint8_t *bytes, size_t num_bytes)
104 buflen = talloc_get_size(buf);
106 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
107 buflen + 1 + num_bytes);
111 buf[buflen] = prefix;
112 memcpy(&buf[buflen+1], bytes, num_bytes);
116 /***********************************************************
117 Same as smb_bytes_push_str(), but without the odd byte
118 align for ucs2 (we're pushing into a param or data block).
119 static for now, although this will probably change when
120 other modules use async trans calls.
121 ***********************************************************/
123 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
124 const char *str, size_t str_len,
125 size_t *pconverted_size)
127 return internal_bytes_push_str(buf, ucs2, str, str_len,
128 false, pconverted_size);
131 struct cli_setpathinfo_state {
136 static void cli_setpathinfo_done(struct tevent_req *subreq);
138 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
139 struct tevent_context *ev,
140 struct cli_state *cli,
146 struct tevent_req *req, *subreq;
147 struct cli_setpathinfo_state *state;
149 req = tevent_req_create(mem_ctx, &state,
150 struct cli_setpathinfo_state);
155 /* Setup setup word. */
156 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
158 /* Setup param array. */
159 state->param = TALLOC_ZERO_ARRAY(state, uint8_t, 6);
160 if (tevent_req_nomem(state->param, req)) {
161 return tevent_req_post(req, ev);
163 SSVAL(state->param, 0, level);
165 state->param = trans2_bytes_push_str(
166 state->param, cli_ucs2(cli), path, strlen(path)+1, NULL);
167 if (tevent_req_nomem(state->param, req)) {
168 return tevent_req_post(req, ev);
171 subreq = cli_trans_send(
172 state, /* mem ctx. */
174 cli, /* cli_state. */
175 SMBtrans2, /* cmd. */
176 NULL, /* pipe name. */
180 &state->setup, /* setup. */
181 1, /* num setup uint16_t words. */
182 0, /* max returned setup. */
183 state->param, /* param. */
184 talloc_get_size(state->param), /* num param. */
185 2, /* max returned param. */
187 data_len, /* num data. */
188 0); /* max returned data. */
190 if (tevent_req_nomem(subreq, req)) {
191 return tevent_req_post(req, ev);
193 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
197 static void cli_setpathinfo_done(struct tevent_req *subreq)
199 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
200 NULL, 0, NULL, NULL, 0, NULL);
201 tevent_req_simple_finish_ntstatus(subreq, status);
204 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
206 return tevent_req_simple_recv_ntstatus(req);
209 NTSTATUS cli_setpathinfo(struct cli_state *cli,
215 TALLOC_CTX *frame = talloc_stackframe();
216 struct tevent_context *ev;
217 struct tevent_req *req;
218 NTSTATUS status = NT_STATUS_NO_MEMORY;
220 if (cli_has_async_calls(cli)) {
222 * Can't use sync call while an async call is in flight
224 status = NT_STATUS_INVALID_PARAMETER;
227 ev = tevent_context_init(frame);
231 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
235 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
238 status = cli_setpathinfo_recv(req);
244 /****************************************************************************
245 Hard/Symlink a file (UNIX extensions).
246 Creates new name (sym)linked to oldname.
247 ****************************************************************************/
249 struct cli_posix_link_internal_state {
253 static void cli_posix_link_internal_done(struct tevent_req *subreq);
255 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
256 struct event_context *ev,
257 struct cli_state *cli,
262 struct tevent_req *req = NULL, *subreq = NULL;
263 struct cli_posix_link_internal_state *state = NULL;
265 req = tevent_req_create(mem_ctx, &state,
266 struct cli_posix_link_internal_state);
271 /* Setup data array. */
272 state->data = talloc_array(state, uint8_t, 0);
273 if (tevent_req_nomem(state->data, req)) {
274 return tevent_req_post(req, ev);
276 state->data = trans2_bytes_push_str(
277 state->data, cli_ucs2(cli), oldname, strlen(oldname)+1, NULL);
279 subreq = cli_setpathinfo_send(
280 state, ev, cli, level, newname,
281 state->data, talloc_get_size(state->data));
282 if (tevent_req_nomem(subreq, req)) {
283 return tevent_req_post(req, ev);
285 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
289 static void cli_posix_link_internal_done(struct tevent_req *subreq)
291 NTSTATUS status = cli_setpathinfo_recv(subreq);
292 tevent_req_simple_finish_ntstatus(subreq, status);
295 /****************************************************************************
296 Symlink a file (UNIX extensions).
297 ****************************************************************************/
299 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
300 struct event_context *ev,
301 struct cli_state *cli,
305 return cli_posix_link_internal_send(
306 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
309 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
311 return tevent_req_simple_recv_ntstatus(req);
314 NTSTATUS cli_posix_symlink(struct cli_state *cli,
318 TALLOC_CTX *frame = talloc_stackframe();
319 struct event_context *ev = NULL;
320 struct tevent_req *req = NULL;
321 NTSTATUS status = NT_STATUS_OK;
323 if (cli_has_async_calls(cli)) {
325 * Can't use sync call while an async call is in flight
327 status = NT_STATUS_INVALID_PARAMETER;
331 ev = event_context_init(frame);
333 status = NT_STATUS_NO_MEMORY;
337 req = cli_posix_symlink_send(frame,
343 status = NT_STATUS_NO_MEMORY;
347 if (!tevent_req_poll(req, ev)) {
348 status = map_nt_error_from_unix(errno);
352 status = cli_posix_symlink_recv(req);
356 if (!NT_STATUS_IS_OK(status)) {
357 cli_set_error(cli, status);
362 /****************************************************************************
363 Read a POSIX symlink.
364 ****************************************************************************/
366 struct readlink_state {
371 static void cli_posix_readlink_done(struct tevent_req *subreq);
373 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
374 struct event_context *ev,
375 struct cli_state *cli,
379 struct tevent_req *req = NULL, *subreq = NULL;
380 struct readlink_state *state = NULL;
381 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
383 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
389 * Len is in bytes, we need it in UCS2 units.
391 if ((2*len < len) || (maxbytelen < len)) {
392 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
393 return tevent_req_post(req, ev);
396 subreq = cli_qpathinfo_send(state, ev, cli, fname,
397 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
398 if (tevent_req_nomem(subreq, req)) {
399 return tevent_req_post(req, ev);
401 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
405 static void cli_posix_readlink_done(struct tevent_req *subreq)
407 struct tevent_req *req = tevent_req_callback_data(
408 subreq, struct tevent_req);
409 struct readlink_state *state = tevent_req_data(
410 req, struct readlink_state);
413 status = cli_qpathinfo_recv(subreq, state, &state->data,
416 if (!NT_STATUS_IS_OK(status)) {
417 tevent_req_nterror(req, status);
421 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
423 if (state->data[state->num_data-1] != '\0') {
424 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
427 tevent_req_done(req);
430 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
431 char *retpath, size_t len)
434 char *converted = NULL;
435 size_t converted_size = 0;
436 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
438 if (tevent_req_is_nterror(req, &status)) {
441 /* The returned data is a pushed string, not raw data. */
442 if (!convert_string_talloc(state,
443 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
449 return NT_STATUS_NO_MEMORY;
452 len = MIN(len,converted_size);
454 return NT_STATUS_DATA_ERROR;
456 memcpy(retpath, converted, len);
460 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
461 char *linkpath, size_t len)
463 TALLOC_CTX *frame = talloc_stackframe();
464 struct event_context *ev = NULL;
465 struct tevent_req *req = NULL;
466 NTSTATUS status = NT_STATUS_OK;
468 if (cli_has_async_calls(cli)) {
470 * Can't use sync call while an async call is in flight
472 status = NT_STATUS_INVALID_PARAMETER;
476 ev = event_context_init(frame);
478 status = NT_STATUS_NO_MEMORY;
482 req = cli_posix_readlink_send(frame,
488 status = NT_STATUS_NO_MEMORY;
492 if (!tevent_req_poll(req, ev)) {
493 status = map_nt_error_from_unix(errno);
497 status = cli_posix_readlink_recv(req, cli, linkpath, len);
501 if (!NT_STATUS_IS_OK(status)) {
502 cli_set_error(cli, status);
507 /****************************************************************************
508 Hard link a file (UNIX extensions).
509 ****************************************************************************/
511 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
512 struct event_context *ev,
513 struct cli_state *cli,
517 return cli_posix_link_internal_send(
518 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
521 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
523 return tevent_req_simple_recv_ntstatus(req);
526 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
530 TALLOC_CTX *frame = talloc_stackframe();
531 struct event_context *ev = NULL;
532 struct tevent_req *req = NULL;
533 NTSTATUS status = NT_STATUS_OK;
535 if (cli_has_async_calls(cli)) {
537 * Can't use sync call while an async call is in flight
539 status = NT_STATUS_INVALID_PARAMETER;
543 ev = event_context_init(frame);
545 status = NT_STATUS_NO_MEMORY;
549 req = cli_posix_hardlink_send(frame,
555 status = NT_STATUS_NO_MEMORY;
559 if (!tevent_req_poll(req, ev)) {
560 status = map_nt_error_from_unix(errno);
564 status = cli_posix_hardlink_recv(req);
568 if (!NT_STATUS_IS_OK(status)) {
569 cli_set_error(cli, status);
574 /****************************************************************************
575 Map standard UNIX permissions onto wire representations.
576 ****************************************************************************/
578 uint32_t unix_perms_to_wire(mode_t perms)
580 unsigned int ret = 0;
582 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
583 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
584 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
585 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
586 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
587 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
588 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
589 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
590 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
592 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
595 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
598 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
603 /****************************************************************************
604 Map wire permissions to standard UNIX.
605 ****************************************************************************/
607 mode_t wire_perms_to_unix(uint32_t perms)
609 mode_t ret = (mode_t)0;
611 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
612 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
613 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
614 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
615 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
616 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
617 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
618 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
619 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
621 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
624 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
627 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
632 /****************************************************************************
633 Return the file type from the wire filetype for UNIX extensions.
634 ****************************************************************************/
636 static mode_t unix_filetype_from_wire(uint32_t wire_type)
644 case UNIX_TYPE_SYMLINK:
648 case UNIX_TYPE_CHARDEV:
652 case UNIX_TYPE_BLKDEV:
660 case UNIX_TYPE_SOCKET:
668 /****************************************************************************
669 Do a POSIX getfacl (UNIX extensions).
670 ****************************************************************************/
672 struct getfacl_state {
677 static void cli_posix_getfacl_done(struct tevent_req *subreq);
679 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
680 struct event_context *ev,
681 struct cli_state *cli,
684 struct tevent_req *req = NULL, *subreq = NULL;
685 struct getfacl_state *state = NULL;
687 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
691 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
693 if (tevent_req_nomem(subreq, req)) {
694 return tevent_req_post(req, ev);
696 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
700 static void cli_posix_getfacl_done(struct tevent_req *subreq)
702 struct tevent_req *req = tevent_req_callback_data(
703 subreq, struct tevent_req);
704 struct getfacl_state *state = tevent_req_data(
705 req, struct getfacl_state);
708 status = cli_qpathinfo_recv(subreq, state, &state->data,
711 if (!NT_STATUS_IS_OK(status)) {
712 tevent_req_nterror(req, status);
715 tevent_req_done(req);
718 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
723 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
726 if (tevent_req_is_nterror(req, &status)) {
729 *prb_size = (size_t)state->num_data;
730 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
734 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
740 TALLOC_CTX *frame = talloc_stackframe();
741 struct event_context *ev = NULL;
742 struct tevent_req *req = NULL;
743 NTSTATUS status = NT_STATUS_OK;
745 if (cli_has_async_calls(cli)) {
747 * Can't use sync call while an async call is in flight
749 status = NT_STATUS_INVALID_PARAMETER;
753 ev = event_context_init(frame);
755 status = NT_STATUS_NO_MEMORY;
759 req = cli_posix_getfacl_send(frame,
764 status = NT_STATUS_NO_MEMORY;
768 if (!tevent_req_poll(req, ev)) {
769 status = map_nt_error_from_unix(errno);
773 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
777 if (!NT_STATUS_IS_OK(status)) {
778 cli_set_error(cli, status);
783 /****************************************************************************
784 Stat a file (UNIX extensions).
785 ****************************************************************************/
792 static void cli_posix_stat_done(struct tevent_req *subreq);
794 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
795 struct event_context *ev,
796 struct cli_state *cli,
799 struct tevent_req *req = NULL, *subreq = NULL;
800 struct stat_state *state = NULL;
802 req = tevent_req_create(mem_ctx, &state, struct stat_state);
806 subreq = cli_qpathinfo_send(state, ev, cli, fname,
807 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
808 if (tevent_req_nomem(subreq, req)) {
809 return tevent_req_post(req, ev);
811 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
815 static void cli_posix_stat_done(struct tevent_req *subreq)
817 struct tevent_req *req = tevent_req_callback_data(
818 subreq, struct tevent_req);
819 struct stat_state *state = tevent_req_data(req, struct stat_state);
822 status = cli_qpathinfo_recv(subreq, state, &state->data,
825 if (!NT_STATUS_IS_OK(status)) {
826 tevent_req_nterror(req, status);
829 tevent_req_done(req);
832 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
833 SMB_STRUCT_STAT *sbuf)
835 struct stat_state *state = tevent_req_data(req, struct stat_state);
838 if (tevent_req_is_nterror(req, &status)) {
842 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
843 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
844 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
845 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
847 /* assume 512 byte blocks */
848 sbuf->st_ex_blocks /= 512;
850 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
851 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
852 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
854 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
855 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
856 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
857 #if defined(HAVE_MAKEDEV)
859 uint32_t dev_major = IVAL(state->data,60);
860 uint32_t dev_minor = IVAL(state->data,68);
861 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
864 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
865 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
866 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
871 NTSTATUS cli_posix_stat(struct cli_state *cli,
873 SMB_STRUCT_STAT *sbuf)
875 TALLOC_CTX *frame = talloc_stackframe();
876 struct event_context *ev = NULL;
877 struct tevent_req *req = NULL;
878 NTSTATUS status = NT_STATUS_OK;
880 if (cli_has_async_calls(cli)) {
882 * Can't use sync call while an async call is in flight
884 status = NT_STATUS_INVALID_PARAMETER;
888 ev = event_context_init(frame);
890 status = NT_STATUS_NO_MEMORY;
894 req = cli_posix_stat_send(frame,
899 status = NT_STATUS_NO_MEMORY;
903 if (!tevent_req_poll(req, ev)) {
904 status = map_nt_error_from_unix(errno);
908 status = cli_posix_stat_recv(req, sbuf);
912 if (!NT_STATUS_IS_OK(status)) {
913 cli_set_error(cli, status);
918 /****************************************************************************
919 Chmod or chown a file internal (UNIX extensions).
920 ****************************************************************************/
922 struct cli_posix_chown_chmod_internal_state {
926 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
928 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
929 struct event_context *ev,
930 struct cli_state *cli,
936 struct tevent_req *req = NULL, *subreq = NULL;
937 struct cli_posix_chown_chmod_internal_state *state = NULL;
939 req = tevent_req_create(mem_ctx, &state,
940 struct cli_posix_chown_chmod_internal_state);
945 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
946 memset(&state->data[40], '\0', 60);
947 SIVAL(state->data,40,uid);
948 SIVAL(state->data,48,gid);
949 SIVAL(state->data,84,mode);
951 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
952 fname, state->data, sizeof(state->data));
953 if (tevent_req_nomem(subreq, req)) {
954 return tevent_req_post(req, ev);
956 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
961 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
963 NTSTATUS status = cli_setpathinfo_recv(subreq);
964 tevent_req_simple_finish_ntstatus(subreq, status);
967 /****************************************************************************
968 chmod a file (UNIX extensions).
969 ****************************************************************************/
971 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
972 struct event_context *ev,
973 struct cli_state *cli,
977 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
979 unix_perms_to_wire(mode),
984 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
986 return tevent_req_simple_recv_ntstatus(req);
989 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
991 TALLOC_CTX *frame = talloc_stackframe();
992 struct event_context *ev = NULL;
993 struct tevent_req *req = NULL;
994 NTSTATUS status = NT_STATUS_OK;
996 if (cli_has_async_calls(cli)) {
998 * Can't use sync call while an async call is in flight
1000 status = NT_STATUS_INVALID_PARAMETER;
1004 ev = event_context_init(frame);
1006 status = NT_STATUS_NO_MEMORY;
1010 req = cli_posix_chmod_send(frame,
1016 status = NT_STATUS_NO_MEMORY;
1020 if (!tevent_req_poll(req, ev)) {
1021 status = map_nt_error_from_unix(errno);
1025 status = cli_posix_chmod_recv(req);
1029 if (!NT_STATUS_IS_OK(status)) {
1030 cli_set_error(cli, status);
1035 /****************************************************************************
1036 chown a file (UNIX extensions).
1037 ****************************************************************************/
1039 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1040 struct event_context *ev,
1041 struct cli_state *cli,
1046 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1053 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1055 return tevent_req_simple_recv_ntstatus(req);
1058 NTSTATUS cli_posix_chown(struct cli_state *cli,
1063 TALLOC_CTX *frame = talloc_stackframe();
1064 struct event_context *ev = NULL;
1065 struct tevent_req *req = NULL;
1066 NTSTATUS status = NT_STATUS_OK;
1068 if (cli_has_async_calls(cli)) {
1070 * Can't use sync call while an async call is in flight
1072 status = NT_STATUS_INVALID_PARAMETER;
1076 ev = event_context_init(frame);
1078 status = NT_STATUS_NO_MEMORY;
1082 req = cli_posix_chown_send(frame,
1089 status = NT_STATUS_NO_MEMORY;
1093 if (!tevent_req_poll(req, ev)) {
1094 status = map_nt_error_from_unix(errno);
1098 status = cli_posix_chown_recv(req);
1102 if (!NT_STATUS_IS_OK(status)) {
1103 cli_set_error(cli, status);
1108 /****************************************************************************
1110 ****************************************************************************/
1112 static void cli_rename_done(struct tevent_req *subreq);
1114 struct cli_rename_state {
1118 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1119 struct event_context *ev,
1120 struct cli_state *cli,
1121 const char *fname_src,
1122 const char *fname_dst)
1124 struct tevent_req *req = NULL, *subreq = NULL;
1125 struct cli_rename_state *state = NULL;
1126 uint8_t additional_flags = 0;
1127 uint8_t *bytes = NULL;
1129 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1134 SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
1136 bytes = talloc_array(state, uint8_t, 1);
1137 if (tevent_req_nomem(bytes, req)) {
1138 return tevent_req_post(req, ev);
1141 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1142 strlen(fname_src)+1, NULL);
1143 if (tevent_req_nomem(bytes, req)) {
1144 return tevent_req_post(req, ev);
1147 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1148 talloc_get_size(bytes)+1);
1149 if (tevent_req_nomem(bytes, req)) {
1150 return tevent_req_post(req, ev);
1153 bytes[talloc_get_size(bytes)-1] = 4;
1154 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1155 strlen(fname_dst)+1, NULL);
1156 if (tevent_req_nomem(bytes, req)) {
1157 return tevent_req_post(req, ev);
1160 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1161 1, state->vwv, talloc_get_size(bytes), bytes);
1162 if (tevent_req_nomem(subreq, req)) {
1163 return tevent_req_post(req, ev);
1165 tevent_req_set_callback(subreq, cli_rename_done, req);
1169 static void cli_rename_done(struct tevent_req *subreq)
1171 struct tevent_req *req = tevent_req_callback_data(
1172 subreq, struct tevent_req);
1175 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1176 TALLOC_FREE(subreq);
1177 if (!NT_STATUS_IS_OK(status)) {
1178 tevent_req_nterror(req, status);
1181 tevent_req_done(req);
1184 NTSTATUS cli_rename_recv(struct tevent_req *req)
1186 return tevent_req_simple_recv_ntstatus(req);
1189 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1191 TALLOC_CTX *frame = talloc_stackframe();
1192 struct event_context *ev;
1193 struct tevent_req *req;
1194 NTSTATUS status = NT_STATUS_OK;
1196 if (cli_has_async_calls(cli)) {
1198 * Can't use sync call while an async call is in flight
1200 status = NT_STATUS_INVALID_PARAMETER;
1204 ev = event_context_init(frame);
1206 status = NT_STATUS_NO_MEMORY;
1210 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1212 status = NT_STATUS_NO_MEMORY;
1216 if (!tevent_req_poll(req, ev)) {
1217 status = map_nt_error_from_unix(errno);
1221 status = cli_rename_recv(req);
1225 if (!NT_STATUS_IS_OK(status)) {
1226 cli_set_error(cli, status);
1231 /****************************************************************************
1233 ****************************************************************************/
1235 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1237 struct cli_ntrename_internal_state {
1241 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1242 struct event_context *ev,
1243 struct cli_state *cli,
1244 const char *fname_src,
1245 const char *fname_dst,
1246 uint16_t rename_flag)
1248 struct tevent_req *req = NULL, *subreq = NULL;
1249 struct cli_ntrename_internal_state *state = NULL;
1250 uint8_t additional_flags = 0;
1251 uint8_t *bytes = NULL;
1253 req = tevent_req_create(mem_ctx, &state,
1254 struct cli_ntrename_internal_state);
1259 SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
1260 SSVAL(state->vwv+1, 0, rename_flag);
1262 bytes = talloc_array(state, uint8_t, 1);
1263 if (tevent_req_nomem(bytes, req)) {
1264 return tevent_req_post(req, ev);
1267 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1268 strlen(fname_src)+1, NULL);
1269 if (tevent_req_nomem(bytes, req)) {
1270 return tevent_req_post(req, ev);
1273 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1274 talloc_get_size(bytes)+1);
1275 if (tevent_req_nomem(bytes, req)) {
1276 return tevent_req_post(req, ev);
1279 bytes[talloc_get_size(bytes)-1] = 4;
1280 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1281 strlen(fname_dst)+1, NULL);
1282 if (tevent_req_nomem(bytes, req)) {
1283 return tevent_req_post(req, ev);
1286 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1287 4, state->vwv, talloc_get_size(bytes), bytes);
1288 if (tevent_req_nomem(subreq, req)) {
1289 return tevent_req_post(req, ev);
1291 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1295 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1297 struct tevent_req *req = tevent_req_callback_data(
1298 subreq, struct tevent_req);
1301 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1302 TALLOC_FREE(subreq);
1303 if (!NT_STATUS_IS_OK(status)) {
1304 tevent_req_nterror(req, status);
1307 tevent_req_done(req);
1310 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1312 return tevent_req_simple_recv_ntstatus(req);
1315 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1316 struct event_context *ev,
1317 struct cli_state *cli,
1318 const char *fname_src,
1319 const char *fname_dst)
1321 return cli_ntrename_internal_send(mem_ctx,
1326 RENAME_FLAG_RENAME);
1329 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1331 return cli_ntrename_internal_recv(req);
1334 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1336 TALLOC_CTX *frame = talloc_stackframe();
1337 struct event_context *ev;
1338 struct tevent_req *req;
1339 NTSTATUS status = NT_STATUS_OK;
1341 if (cli_has_async_calls(cli)) {
1343 * Can't use sync call while an async call is in flight
1345 status = NT_STATUS_INVALID_PARAMETER;
1349 ev = event_context_init(frame);
1351 status = NT_STATUS_NO_MEMORY;
1355 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1357 status = NT_STATUS_NO_MEMORY;
1361 if (!tevent_req_poll(req, ev)) {
1362 status = map_nt_error_from_unix(errno);
1366 status = cli_ntrename_recv(req);
1370 if (!NT_STATUS_IS_OK(status)) {
1371 cli_set_error(cli, status);
1376 /****************************************************************************
1378 ****************************************************************************/
1380 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1381 struct event_context *ev,
1382 struct cli_state *cli,
1383 const char *fname_src,
1384 const char *fname_dst)
1386 return cli_ntrename_internal_send(mem_ctx,
1391 RENAME_FLAG_HARD_LINK);
1394 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1396 return cli_ntrename_internal_recv(req);
1399 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1401 TALLOC_CTX *frame = talloc_stackframe();
1402 struct event_context *ev;
1403 struct tevent_req *req;
1404 NTSTATUS status = NT_STATUS_OK;
1406 if (cli_has_async_calls(cli)) {
1408 * Can't use sync call while an async call is in flight
1410 status = NT_STATUS_INVALID_PARAMETER;
1414 ev = event_context_init(frame);
1416 status = NT_STATUS_NO_MEMORY;
1420 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1422 status = NT_STATUS_NO_MEMORY;
1426 if (!tevent_req_poll(req, ev)) {
1427 status = map_nt_error_from_unix(errno);
1431 status = cli_nt_hardlink_recv(req);
1435 if (!NT_STATUS_IS_OK(status)) {
1436 cli_set_error(cli, status);
1441 /****************************************************************************
1443 ****************************************************************************/
1445 static void cli_unlink_done(struct tevent_req *subreq);
1447 struct cli_unlink_state {
1451 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1452 struct event_context *ev,
1453 struct cli_state *cli,
1455 uint16_t mayhave_attrs)
1457 struct tevent_req *req = NULL, *subreq = NULL;
1458 struct cli_unlink_state *state = NULL;
1459 uint8_t additional_flags = 0;
1460 uint8_t *bytes = NULL;
1462 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1467 SSVAL(state->vwv+0, 0, mayhave_attrs);
1469 bytes = talloc_array(state, uint8_t, 1);
1470 if (tevent_req_nomem(bytes, req)) {
1471 return tevent_req_post(req, ev);
1474 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1475 strlen(fname)+1, NULL);
1477 if (tevent_req_nomem(bytes, req)) {
1478 return tevent_req_post(req, ev);
1481 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1482 1, state->vwv, talloc_get_size(bytes), bytes);
1483 if (tevent_req_nomem(subreq, req)) {
1484 return tevent_req_post(req, ev);
1486 tevent_req_set_callback(subreq, cli_unlink_done, req);
1490 static void cli_unlink_done(struct tevent_req *subreq)
1492 struct tevent_req *req = tevent_req_callback_data(
1493 subreq, struct tevent_req);
1496 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1497 TALLOC_FREE(subreq);
1498 if (!NT_STATUS_IS_OK(status)) {
1499 tevent_req_nterror(req, status);
1502 tevent_req_done(req);
1505 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1507 return tevent_req_simple_recv_ntstatus(req);
1510 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1512 TALLOC_CTX *frame = talloc_stackframe();
1513 struct event_context *ev;
1514 struct tevent_req *req;
1515 NTSTATUS status = NT_STATUS_OK;
1517 if (cli_has_async_calls(cli)) {
1519 * Can't use sync call while an async call is in flight
1521 status = NT_STATUS_INVALID_PARAMETER;
1525 ev = event_context_init(frame);
1527 status = NT_STATUS_NO_MEMORY;
1531 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1533 status = NT_STATUS_NO_MEMORY;
1537 if (!tevent_req_poll(req, ev)) {
1538 status = map_nt_error_from_unix(errno);
1542 status = cli_unlink_recv(req);
1546 if (!NT_STATUS_IS_OK(status)) {
1547 cli_set_error(cli, status);
1552 /****************************************************************************
1554 ****************************************************************************/
1556 static void cli_mkdir_done(struct tevent_req *subreq);
1558 struct cli_mkdir_state {
1562 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1563 struct event_context *ev,
1564 struct cli_state *cli,
1567 struct tevent_req *req = NULL, *subreq = NULL;
1568 struct cli_mkdir_state *state = NULL;
1569 uint8_t additional_flags = 0;
1570 uint8_t *bytes = NULL;
1572 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1577 bytes = talloc_array(state, uint8_t, 1);
1578 if (tevent_req_nomem(bytes, req)) {
1579 return tevent_req_post(req, ev);
1582 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1583 strlen(dname)+1, NULL);
1585 if (tevent_req_nomem(bytes, req)) {
1586 return tevent_req_post(req, ev);
1589 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1590 0, NULL, talloc_get_size(bytes), bytes);
1591 if (tevent_req_nomem(subreq, req)) {
1592 return tevent_req_post(req, ev);
1594 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1598 static void cli_mkdir_done(struct tevent_req *subreq)
1600 struct tevent_req *req = tevent_req_callback_data(
1601 subreq, struct tevent_req);
1604 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1605 TALLOC_FREE(subreq);
1606 if (!NT_STATUS_IS_OK(status)) {
1607 tevent_req_nterror(req, status);
1610 tevent_req_done(req);
1613 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1615 return tevent_req_simple_recv_ntstatus(req);
1618 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1620 TALLOC_CTX *frame = talloc_stackframe();
1621 struct event_context *ev;
1622 struct tevent_req *req;
1623 NTSTATUS status = NT_STATUS_OK;
1625 if (cli_has_async_calls(cli)) {
1627 * Can't use sync call while an async call is in flight
1629 status = NT_STATUS_INVALID_PARAMETER;
1633 ev = event_context_init(frame);
1635 status = NT_STATUS_NO_MEMORY;
1639 req = cli_mkdir_send(frame, ev, cli, dname);
1641 status = NT_STATUS_NO_MEMORY;
1645 if (!tevent_req_poll(req, ev)) {
1646 status = map_nt_error_from_unix(errno);
1650 status = cli_mkdir_recv(req);
1654 if (!NT_STATUS_IS_OK(status)) {
1655 cli_set_error(cli, status);
1660 /****************************************************************************
1662 ****************************************************************************/
1664 static void cli_rmdir_done(struct tevent_req *subreq);
1666 struct cli_rmdir_state {
1670 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1671 struct event_context *ev,
1672 struct cli_state *cli,
1675 struct tevent_req *req = NULL, *subreq = NULL;
1676 struct cli_rmdir_state *state = NULL;
1677 uint8_t additional_flags = 0;
1678 uint8_t *bytes = NULL;
1680 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1685 bytes = talloc_array(state, uint8_t, 1);
1686 if (tevent_req_nomem(bytes, req)) {
1687 return tevent_req_post(req, ev);
1690 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1691 strlen(dname)+1, NULL);
1693 if (tevent_req_nomem(bytes, req)) {
1694 return tevent_req_post(req, ev);
1697 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1698 0, NULL, talloc_get_size(bytes), bytes);
1699 if (tevent_req_nomem(subreq, req)) {
1700 return tevent_req_post(req, ev);
1702 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1706 static void cli_rmdir_done(struct tevent_req *subreq)
1708 struct tevent_req *req = tevent_req_callback_data(
1709 subreq, struct tevent_req);
1712 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1713 TALLOC_FREE(subreq);
1714 if (!NT_STATUS_IS_OK(status)) {
1715 tevent_req_nterror(req, status);
1718 tevent_req_done(req);
1721 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1723 return tevent_req_simple_recv_ntstatus(req);
1726 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1728 TALLOC_CTX *frame = talloc_stackframe();
1729 struct event_context *ev;
1730 struct tevent_req *req;
1731 NTSTATUS status = NT_STATUS_OK;
1733 if (cli_has_async_calls(cli)) {
1735 * Can't use sync call while an async call is in flight
1737 status = NT_STATUS_INVALID_PARAMETER;
1741 ev = event_context_init(frame);
1743 status = NT_STATUS_NO_MEMORY;
1747 req = cli_rmdir_send(frame, ev, cli, dname);
1749 status = NT_STATUS_NO_MEMORY;
1753 if (!tevent_req_poll(req, ev)) {
1754 status = map_nt_error_from_unix(errno);
1758 status = cli_rmdir_recv(req);
1762 if (!NT_STATUS_IS_OK(status)) {
1763 cli_set_error(cli, status);
1768 /****************************************************************************
1769 Set or clear the delete on close flag.
1770 ****************************************************************************/
1778 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1780 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1781 NULL, 0, NULL, NULL, 0, NULL);
1782 tevent_req_simple_finish_ntstatus(subreq, status);
1785 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1786 struct event_context *ev,
1787 struct cli_state *cli,
1791 struct tevent_req *req = NULL, *subreq = NULL;
1792 struct doc_state *state = NULL;
1794 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1799 /* Setup setup word. */
1800 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1802 /* Setup param array. */
1803 SSVAL(state->param,0,fnum);
1804 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1806 /* Setup data array. */
1807 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1809 subreq = cli_trans_send(state, /* mem ctx. */
1810 ev, /* event ctx. */
1811 cli, /* cli_state. */
1812 SMBtrans2, /* cmd. */
1813 NULL, /* pipe name. */
1817 &state->setup, /* setup. */
1818 1, /* num setup uint16_t words. */
1819 0, /* max returned setup. */
1820 state->param, /* param. */
1822 2, /* max returned param. */
1823 state->data, /* data. */
1825 0); /* max returned data. */
1827 if (tevent_req_nomem(subreq, req)) {
1828 return tevent_req_post(req, ev);
1830 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1834 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1836 return tevent_req_simple_recv_ntstatus(req);
1839 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1841 TALLOC_CTX *frame = talloc_stackframe();
1842 struct event_context *ev = NULL;
1843 struct tevent_req *req = NULL;
1844 NTSTATUS status = NT_STATUS_OK;
1846 if (cli_has_async_calls(cli)) {
1848 * Can't use sync call while an async call is in flight
1850 status = NT_STATUS_INVALID_PARAMETER;
1854 ev = event_context_init(frame);
1856 status = NT_STATUS_NO_MEMORY;
1860 req = cli_nt_delete_on_close_send(frame,
1866 status = NT_STATUS_NO_MEMORY;
1870 if (!tevent_req_poll(req, ev)) {
1871 status = map_nt_error_from_unix(errno);
1875 status = cli_nt_delete_on_close_recv(req);
1879 if (!NT_STATUS_IS_OK(status)) {
1880 cli_set_error(cli, status);
1885 struct cli_ntcreate_state {
1890 static void cli_ntcreate_done(struct tevent_req *subreq);
1892 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1893 struct event_context *ev,
1894 struct cli_state *cli,
1896 uint32_t CreatFlags,
1897 uint32_t DesiredAccess,
1898 uint32_t FileAttributes,
1899 uint32_t ShareAccess,
1900 uint32_t CreateDisposition,
1901 uint32_t CreateOptions,
1902 uint8_t SecurityFlags)
1904 struct tevent_req *req, *subreq;
1905 struct cli_ntcreate_state *state;
1908 size_t converted_len;
1910 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1917 SCVAL(vwv+0, 0, 0xFF);
1922 if (cli->use_oplocks) {
1923 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1925 SIVAL(vwv+3, 1, CreatFlags);
1926 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1927 SIVAL(vwv+7, 1, DesiredAccess);
1928 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1929 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1930 SIVAL(vwv+13, 1, FileAttributes);
1931 SIVAL(vwv+15, 1, ShareAccess);
1932 SIVAL(vwv+17, 1, CreateDisposition);
1933 SIVAL(vwv+19, 1, CreateOptions);
1934 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1935 SCVAL(vwv+23, 1, SecurityFlags);
1937 bytes = talloc_array(state, uint8_t, 0);
1938 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1939 fname, strlen(fname)+1,
1942 /* sigh. this copes with broken netapp filer behaviour */
1943 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1945 if (tevent_req_nomem(bytes, req)) {
1946 return tevent_req_post(req, ev);
1949 SSVAL(vwv+2, 1, converted_len);
1951 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1952 talloc_get_size(bytes), bytes);
1953 if (tevent_req_nomem(subreq, req)) {
1954 return tevent_req_post(req, ev);
1956 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1960 static void cli_ntcreate_done(struct tevent_req *subreq)
1962 struct tevent_req *req = tevent_req_callback_data(
1963 subreq, struct tevent_req);
1964 struct cli_ntcreate_state *state = tevent_req_data(
1965 req, struct cli_ntcreate_state);
1973 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1974 &num_bytes, &bytes);
1975 TALLOC_FREE(subreq);
1976 if (!NT_STATUS_IS_OK(status)) {
1977 tevent_req_nterror(req, status);
1980 state->fnum = SVAL(vwv+2, 1);
1981 tevent_req_done(req);
1984 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1986 struct cli_ntcreate_state *state = tevent_req_data(
1987 req, struct cli_ntcreate_state);
1990 if (tevent_req_is_nterror(req, &status)) {
1993 *pfnum = state->fnum;
1994 return NT_STATUS_OK;
1997 NTSTATUS cli_ntcreate(struct cli_state *cli,
1999 uint32_t CreatFlags,
2000 uint32_t DesiredAccess,
2001 uint32_t FileAttributes,
2002 uint32_t ShareAccess,
2003 uint32_t CreateDisposition,
2004 uint32_t CreateOptions,
2005 uint8_t SecurityFlags,
2008 TALLOC_CTX *frame = talloc_stackframe();
2009 struct event_context *ev;
2010 struct tevent_req *req;
2011 NTSTATUS status = NT_STATUS_OK;
2013 if (cli_has_async_calls(cli)) {
2015 * Can't use sync call while an async call is in flight
2017 status = NT_STATUS_INVALID_PARAMETER;
2021 ev = event_context_init(frame);
2023 status = NT_STATUS_NO_MEMORY;
2027 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2028 DesiredAccess, FileAttributes, ShareAccess,
2029 CreateDisposition, CreateOptions,
2032 status = NT_STATUS_NO_MEMORY;
2036 if (!tevent_req_poll(req, ev)) {
2037 status = map_nt_error_from_unix(errno);
2041 status = cli_ntcreate_recv(req, pfid);
2044 if (!NT_STATUS_IS_OK(status)) {
2045 cli_set_error(cli, status);
2050 /****************************************************************************
2052 WARNING: if you open with O_WRONLY then getattrE won't work!
2053 ****************************************************************************/
2055 struct cli_open_state {
2061 static void cli_open_done(struct tevent_req *subreq);
2063 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2064 struct event_context *ev,
2065 struct cli_state *cli, const char *fname,
2066 int flags, int share_mode,
2067 struct tevent_req **psmbreq)
2069 struct tevent_req *req, *subreq;
2070 struct cli_open_state *state;
2072 unsigned accessmode;
2073 uint8_t additional_flags;
2076 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2082 if (flags & O_CREAT) {
2085 if (!(flags & O_EXCL)) {
2086 if (flags & O_TRUNC)
2092 accessmode = (share_mode<<4);
2094 if ((flags & O_ACCMODE) == O_RDWR) {
2096 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2101 if ((flags & O_SYNC) == O_SYNC) {
2102 accessmode |= (1<<14);
2106 if (share_mode == DENY_FCB) {
2110 SCVAL(state->vwv + 0, 0, 0xFF);
2111 SCVAL(state->vwv + 0, 1, 0);
2112 SSVAL(state->vwv + 1, 0, 0);
2113 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2114 SSVAL(state->vwv + 3, 0, accessmode);
2115 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
2116 SSVAL(state->vwv + 5, 0, 0);
2117 SIVAL(state->vwv + 6, 0, 0);
2118 SSVAL(state->vwv + 8, 0, openfn);
2119 SIVAL(state->vwv + 9, 0, 0);
2120 SIVAL(state->vwv + 11, 0, 0);
2121 SIVAL(state->vwv + 13, 0, 0);
2123 additional_flags = 0;
2125 if (cli->use_oplocks) {
2126 /* if using oplocks then ask for a batch oplock via
2127 core and extended methods */
2129 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2130 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2133 bytes = talloc_array(state, uint8_t, 0);
2134 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2135 strlen(fname)+1, NULL);
2137 if (tevent_req_nomem(bytes, req)) {
2138 return tevent_req_post(req, ev);
2141 state->bytes.iov_base = (void *)bytes;
2142 state->bytes.iov_len = talloc_get_size(bytes);
2144 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2145 15, state->vwv, 1, &state->bytes);
2146 if (subreq == NULL) {
2150 tevent_req_set_callback(subreq, cli_open_done, req);
2155 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2156 struct cli_state *cli, const char *fname,
2157 int flags, int share_mode)
2159 struct tevent_req *req, *subreq;
2162 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2168 status = cli_smb_req_send(subreq);
2169 if (!NT_STATUS_IS_OK(status)) {
2170 tevent_req_nterror(req, status);
2171 return tevent_req_post(req, ev);
2176 static void cli_open_done(struct tevent_req *subreq)
2178 struct tevent_req *req = tevent_req_callback_data(
2179 subreq, struct tevent_req);
2180 struct cli_open_state *state = tevent_req_data(
2181 req, struct cli_open_state);
2187 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2189 TALLOC_FREE(subreq);
2190 if (!NT_STATUS_IS_OK(status)) {
2191 tevent_req_nterror(req, status);
2194 state->fnum = SVAL(vwv+2, 0);
2195 tevent_req_done(req);
2198 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2200 struct cli_open_state *state = tevent_req_data(
2201 req, struct cli_open_state);
2204 if (tevent_req_is_nterror(req, &status)) {
2207 *pfnum = state->fnum;
2208 return NT_STATUS_OK;
2211 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2212 int share_mode, uint16_t *pfnum)
2214 TALLOC_CTX *frame = talloc_stackframe();
2215 struct event_context *ev;
2216 struct tevent_req *req;
2217 NTSTATUS status = NT_STATUS_OK;
2219 if (cli_has_async_calls(cli)) {
2221 * Can't use sync call while an async call is in flight
2223 status = NT_STATUS_INVALID_PARAMETER;
2227 ev = event_context_init(frame);
2229 status = NT_STATUS_NO_MEMORY;
2233 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2235 status = NT_STATUS_NO_MEMORY;
2239 if (!tevent_req_poll(req, ev)) {
2240 status = map_nt_error_from_unix(errno);
2244 status = cli_open_recv(req, pfnum);
2247 if (!NT_STATUS_IS_OK(status)) {
2248 cli_set_error(cli, status);
2253 /****************************************************************************
2255 ****************************************************************************/
2257 struct cli_close_state {
2261 static void cli_close_done(struct tevent_req *subreq);
2263 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2264 struct event_context *ev,
2265 struct cli_state *cli,
2267 struct tevent_req **psubreq)
2269 struct tevent_req *req, *subreq;
2270 struct cli_close_state *state;
2272 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2277 SSVAL(state->vwv+0, 0, fnum);
2278 SIVALS(state->vwv+1, 0, -1);
2280 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2282 if (subreq == NULL) {
2286 tevent_req_set_callback(subreq, cli_close_done, req);
2291 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2292 struct event_context *ev,
2293 struct cli_state *cli,
2296 struct tevent_req *req, *subreq;
2299 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2304 status = cli_smb_req_send(subreq);
2305 if (!NT_STATUS_IS_OK(status)) {
2306 tevent_req_nterror(req, status);
2307 return tevent_req_post(req, ev);
2312 static void cli_close_done(struct tevent_req *subreq)
2314 struct tevent_req *req = tevent_req_callback_data(
2315 subreq, struct tevent_req);
2318 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2319 TALLOC_FREE(subreq);
2320 if (!NT_STATUS_IS_OK(status)) {
2321 tevent_req_nterror(req, status);
2324 tevent_req_done(req);
2327 NTSTATUS cli_close_recv(struct tevent_req *req)
2329 return tevent_req_simple_recv_ntstatus(req);
2332 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2334 TALLOC_CTX *frame = talloc_stackframe();
2335 struct event_context *ev;
2336 struct tevent_req *req;
2337 NTSTATUS status = NT_STATUS_OK;
2339 if (cli_has_async_calls(cli)) {
2341 * Can't use sync call while an async call is in flight
2343 status = NT_STATUS_INVALID_PARAMETER;
2347 ev = event_context_init(frame);
2349 status = NT_STATUS_NO_MEMORY;
2353 req = cli_close_send(frame, ev, cli, fnum);
2355 status = NT_STATUS_NO_MEMORY;
2359 if (!tevent_req_poll(req, ev)) {
2360 status = map_nt_error_from_unix(errno);
2364 status = cli_close_recv(req);
2367 if (!NT_STATUS_IS_OK(status)) {
2368 cli_set_error(cli, status);
2373 /****************************************************************************
2374 Truncate a file to a specified size
2375 ****************************************************************************/
2377 struct ftrunc_state {
2383 static void cli_ftruncate_done(struct tevent_req *subreq)
2385 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2386 NULL, 0, NULL, NULL, 0, NULL);
2387 tevent_req_simple_finish_ntstatus(subreq, status);
2390 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2391 struct event_context *ev,
2392 struct cli_state *cli,
2396 struct tevent_req *req = NULL, *subreq = NULL;
2397 struct ftrunc_state *state = NULL;
2399 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2404 /* Setup setup word. */
2405 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2407 /* Setup param array. */
2408 SSVAL(state->param,0,fnum);
2409 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2410 SSVAL(state->param,4,0);
2412 /* Setup data array. */
2413 SBVAL(state->data, 0, size);
2415 subreq = cli_trans_send(state, /* mem ctx. */
2416 ev, /* event ctx. */
2417 cli, /* cli_state. */
2418 SMBtrans2, /* cmd. */
2419 NULL, /* pipe name. */
2423 &state->setup, /* setup. */
2424 1, /* num setup uint16_t words. */
2425 0, /* max returned setup. */
2426 state->param, /* param. */
2428 2, /* max returned param. */
2429 state->data, /* data. */
2431 0); /* max returned data. */
2433 if (tevent_req_nomem(subreq, req)) {
2434 return tevent_req_post(req, ev);
2436 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2440 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2442 return tevent_req_simple_recv_ntstatus(req);
2445 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2447 TALLOC_CTX *frame = talloc_stackframe();
2448 struct event_context *ev = NULL;
2449 struct tevent_req *req = NULL;
2450 NTSTATUS status = NT_STATUS_OK;
2452 if (cli_has_async_calls(cli)) {
2454 * Can't use sync call while an async call is in flight
2456 status = NT_STATUS_INVALID_PARAMETER;
2460 ev = event_context_init(frame);
2462 status = NT_STATUS_NO_MEMORY;
2466 req = cli_ftruncate_send(frame,
2472 status = NT_STATUS_NO_MEMORY;
2476 if (!tevent_req_poll(req, ev)) {
2477 status = map_nt_error_from_unix(errno);
2481 status = cli_ftruncate_recv(req);
2485 if (!NT_STATUS_IS_OK(status)) {
2486 cli_set_error(cli, status);
2491 /****************************************************************************
2492 send a lock with a specified locktype
2493 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2494 ****************************************************************************/
2496 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2497 uint32_t offset, uint32_t len,
2498 int timeout, unsigned char locktype)
2505 SCVAL(vwv + 0, 0, 0xff);
2506 SCVAL(vwv + 0, 1, 0);
2507 SSVAL(vwv + 1, 0, 0);
2508 SSVAL(vwv + 2, 0, fnum);
2509 SCVAL(vwv + 3, 0, locktype);
2510 SCVAL(vwv + 3, 1, 0);
2511 SIVALS(vwv + 4, 0, timeout);
2512 SSVAL(vwv + 6, 0, 0);
2513 SSVAL(vwv + 7, 0, 1);
2515 SSVAL(bytes, 0, cli->pid);
2516 SIVAL(bytes, 2, offset);
2517 SIVAL(bytes, 6, len);
2519 saved_timeout = cli->timeout;
2522 cli->timeout = (timeout == -1)
2523 ? 0x7FFFFFFF : (timeout + 2*1000);
2526 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2527 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2529 cli->timeout = saved_timeout;
2534 /****************************************************************************
2536 note that timeout is in units of 2 milliseconds
2537 ****************************************************************************/
2539 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2540 uint32_t offset, uint32_t len, int timeout,
2541 enum brl_type lock_type)
2545 status = cli_locktype(cli, fnum, offset, len, timeout,
2546 (lock_type == READ_LOCK? 1 : 0));
2547 cli_set_error(cli, status);
2548 return NT_STATUS_IS_OK(status);
2551 /****************************************************************************
2553 ****************************************************************************/
2555 struct cli_unlock_state {
2560 static void cli_unlock_done(struct tevent_req *subreq);
2562 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2563 struct event_context *ev,
2564 struct cli_state *cli,
2570 struct tevent_req *req = NULL, *subreq = NULL;
2571 struct cli_unlock_state *state = NULL;
2572 uint8_t additional_flags = 0;
2574 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2579 SCVAL(state->vwv+0, 0, 0xFF);
2580 SSVAL(state->vwv+2, 0, fnum);
2581 SCVAL(state->vwv+3, 0, 0);
2582 SIVALS(state->vwv+4, 0, 0);
2583 SSVAL(state->vwv+6, 0, 1);
2584 SSVAL(state->vwv+7, 0, 0);
2586 SSVAL(state->data, 0, cli->pid);
2587 SIVAL(state->data, 2, offset);
2588 SIVAL(state->data, 6, len);
2590 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2591 8, state->vwv, 10, state->data);
2592 if (tevent_req_nomem(subreq, req)) {
2593 return tevent_req_post(req, ev);
2595 tevent_req_set_callback(subreq, cli_unlock_done, req);
2599 static void cli_unlock_done(struct tevent_req *subreq)
2601 struct tevent_req *req = tevent_req_callback_data(
2602 subreq, struct tevent_req);
2605 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2606 TALLOC_FREE(subreq);
2607 if (!NT_STATUS_IS_OK(status)) {
2608 tevent_req_nterror(req, status);
2611 tevent_req_done(req);
2614 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2616 return tevent_req_simple_recv_ntstatus(req);
2619 NTSTATUS cli_unlock(struct cli_state *cli,
2624 TALLOC_CTX *frame = talloc_stackframe();
2625 struct event_context *ev;
2626 struct tevent_req *req;
2627 NTSTATUS status = NT_STATUS_OK;
2629 if (cli_has_async_calls(cli)) {
2631 * Can't use sync call while an async call is in flight
2633 status = NT_STATUS_INVALID_PARAMETER;
2637 ev = event_context_init(frame);
2639 status = NT_STATUS_NO_MEMORY;
2643 req = cli_unlock_send(frame, ev, cli,
2646 status = NT_STATUS_NO_MEMORY;
2650 if (!tevent_req_poll(req, ev)) {
2651 status = map_nt_error_from_unix(errno);
2655 status = cli_unlock_recv(req);
2659 if (!NT_STATUS_IS_OK(status)) {
2660 cli_set_error(cli, status);
2665 /****************************************************************************
2666 Lock a file with 64 bit offsets.
2667 ****************************************************************************/
2669 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2670 uint64_t offset, uint64_t len, int timeout,
2671 enum brl_type lock_type)
2675 int saved_timeout = cli->timeout;
2679 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2680 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2683 ltype = (lock_type == READ_LOCK? 1 : 0);
2684 ltype |= LOCKING_ANDX_LARGE_FILES;
2686 SCVAL(vwv + 0, 0, 0xff);
2687 SCVAL(vwv + 0, 1, 0);
2688 SSVAL(vwv + 1, 0, 0);
2689 SSVAL(vwv + 2, 0, fnum);
2690 SCVAL(vwv + 3, 0, ltype);
2691 SCVAL(vwv + 3, 1, 0);
2692 SIVALS(vwv + 4, 0, timeout);
2693 SSVAL(vwv + 6, 0, 0);
2694 SSVAL(vwv + 7, 0, 1);
2696 SIVAL(bytes, 0, cli->pid);
2697 SOFF_T_R(bytes, 4, offset);
2698 SOFF_T_R(bytes, 12, len);
2700 saved_timeout = cli->timeout;
2703 cli->timeout = (timeout == -1)
2704 ? 0x7FFFFFFF : (timeout + 2*1000);
2707 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2708 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2710 cli->timeout = saved_timeout;
2712 cli_set_error(cli, status);
2713 return NT_STATUS_IS_OK(status);
2716 /****************************************************************************
2717 Unlock a file with 64 bit offsets.
2718 ****************************************************************************/
2720 struct cli_unlock64_state {
2725 static void cli_unlock64_done(struct tevent_req *subreq);
2727 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2728 struct event_context *ev,
2729 struct cli_state *cli,
2735 struct tevent_req *req = NULL, *subreq = NULL;
2736 struct cli_unlock64_state *state = NULL;
2737 uint8_t additional_flags = 0;
2739 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2744 SCVAL(state->vwv+0, 0, 0xff);
2745 SSVAL(state->vwv+2, 0, fnum);
2746 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2747 SIVALS(state->vwv+4, 0, 0);
2748 SSVAL(state->vwv+6, 0, 1);
2749 SSVAL(state->vwv+7, 0, 0);
2751 SIVAL(state->data, 0, cli->pid);
2752 SOFF_T_R(state->data, 4, offset);
2753 SOFF_T_R(state->data, 12, len);
2755 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2756 8, state->vwv, 20, state->data);
2757 if (tevent_req_nomem(subreq, req)) {
2758 return tevent_req_post(req, ev);
2760 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2764 static void cli_unlock64_done(struct tevent_req *subreq)
2766 struct tevent_req *req = tevent_req_callback_data(
2767 subreq, struct tevent_req);
2770 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2771 TALLOC_FREE(subreq);
2772 if (!NT_STATUS_IS_OK(status)) {
2773 tevent_req_nterror(req, status);
2776 tevent_req_done(req);
2779 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2781 return tevent_req_simple_recv_ntstatus(req);
2784 NTSTATUS cli_unlock64(struct cli_state *cli,
2789 TALLOC_CTX *frame = talloc_stackframe();
2790 struct event_context *ev;
2791 struct tevent_req *req;
2792 NTSTATUS status = NT_STATUS_OK;
2794 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2795 return cli_unlock(cli, fnum, offset, len);
2798 if (cli_has_async_calls(cli)) {
2800 * Can't use sync call while an async call is in flight
2802 status = NT_STATUS_INVALID_PARAMETER;
2806 ev = event_context_init(frame);
2808 status = NT_STATUS_NO_MEMORY;
2812 req = cli_unlock64_send(frame, ev, cli,
2815 status = NT_STATUS_NO_MEMORY;
2819 if (!tevent_req_poll(req, ev)) {
2820 status = map_nt_error_from_unix(errno);
2824 status = cli_unlock64_recv(req);
2828 if (!NT_STATUS_IS_OK(status)) {
2829 cli_set_error(cli, status);
2834 /****************************************************************************
2835 Get/unlock a POSIX lock on a file - internal function.
2836 ****************************************************************************/
2838 struct posix_lock_state {
2841 uint8_t data[POSIX_LOCK_DATA_SIZE];
2844 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2846 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2847 NULL, 0, NULL, NULL, 0, NULL);
2848 tevent_req_simple_finish_ntstatus(subreq, status);
2851 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2852 struct event_context *ev,
2853 struct cli_state *cli,
2858 enum brl_type lock_type)
2860 struct tevent_req *req = NULL, *subreq = NULL;
2861 struct posix_lock_state *state = NULL;
2863 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2868 /* Setup setup word. */
2869 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2871 /* Setup param array. */
2872 SSVAL(&state->param, 0, fnum);
2873 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2875 /* Setup data array. */
2876 switch (lock_type) {
2878 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2879 POSIX_LOCK_TYPE_READ);
2882 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2883 POSIX_LOCK_TYPE_WRITE);
2886 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2887 POSIX_LOCK_TYPE_UNLOCK);
2894 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2895 POSIX_LOCK_FLAG_WAIT);
2897 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2898 POSIX_LOCK_FLAG_NOWAIT);
2901 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
2902 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2903 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2905 subreq = cli_trans_send(state, /* mem ctx. */
2906 ev, /* event ctx. */
2907 cli, /* cli_state. */
2908 SMBtrans2, /* cmd. */
2909 NULL, /* pipe name. */
2913 &state->setup, /* setup. */
2914 1, /* num setup uint16_t words. */
2915 0, /* max returned setup. */
2916 state->param, /* param. */
2918 2, /* max returned param. */
2919 state->data, /* data. */
2920 POSIX_LOCK_DATA_SIZE, /* num data. */
2921 0); /* max returned data. */
2923 if (tevent_req_nomem(subreq, req)) {
2924 return tevent_req_post(req, ev);
2926 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
2930 /****************************************************************************
2932 ****************************************************************************/
2934 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
2935 struct event_context *ev,
2936 struct cli_state *cli,
2941 enum brl_type lock_type)
2943 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2944 wait_lock, lock_type);
2947 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
2949 return tevent_req_simple_recv_ntstatus(req);
2952 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2953 uint64_t offset, uint64_t len,
2954 bool wait_lock, enum brl_type lock_type)
2956 TALLOC_CTX *frame = talloc_stackframe();
2957 struct event_context *ev = NULL;
2958 struct tevent_req *req = NULL;
2959 NTSTATUS status = NT_STATUS_OK;
2961 if (cli_has_async_calls(cli)) {
2963 * Can't use sync call while an async call is in flight
2965 status = NT_STATUS_INVALID_PARAMETER;
2969 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2970 status = NT_STATUS_INVALID_PARAMETER;
2974 ev = event_context_init(frame);
2976 status = NT_STATUS_NO_MEMORY;
2980 req = cli_posix_lock_send(frame,
2989 status = NT_STATUS_NO_MEMORY;
2993 if (!tevent_req_poll(req, ev)) {
2994 status = map_nt_error_from_unix(errno);
2998 status = cli_posix_lock_recv(req);
3002 if (!NT_STATUS_IS_OK(status)) {
3003 cli_set_error(cli, status);
3008 /****************************************************************************
3009 POSIX Unlock a file.
3010 ****************************************************************************/
3012 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3013 struct event_context *ev,
3014 struct cli_state *cli,
3019 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3020 false, UNLOCK_LOCK);
3023 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3025 return tevent_req_simple_recv_ntstatus(req);
3028 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3030 TALLOC_CTX *frame = talloc_stackframe();
3031 struct event_context *ev = NULL;
3032 struct tevent_req *req = NULL;
3033 NTSTATUS status = NT_STATUS_OK;
3035 if (cli_has_async_calls(cli)) {
3037 * Can't use sync call while an async call is in flight
3039 status = NT_STATUS_INVALID_PARAMETER;
3043 ev = event_context_init(frame);
3045 status = NT_STATUS_NO_MEMORY;
3049 req = cli_posix_unlock_send(frame,
3056 status = NT_STATUS_NO_MEMORY;
3060 if (!tevent_req_poll(req, ev)) {
3061 status = map_nt_error_from_unix(errno);
3065 status = cli_posix_unlock_recv(req);
3069 if (!NT_STATUS_IS_OK(status)) {
3070 cli_set_error(cli, status);
3075 /****************************************************************************
3076 Do a SMBgetattrE call.
3077 ****************************************************************************/
3079 static void cli_getattrE_done(struct tevent_req *subreq);
3081 struct cli_getattrE_state {
3091 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3092 struct event_context *ev,
3093 struct cli_state *cli,
3096 struct tevent_req *req = NULL, *subreq = NULL;
3097 struct cli_getattrE_state *state = NULL;
3098 uint8_t additional_flags = 0;
3100 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3105 state->zone_offset = cli->serverzone;
3106 SSVAL(state->vwv+0,0,fnum);
3108 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3109 1, state->vwv, 0, NULL);
3110 if (tevent_req_nomem(subreq, req)) {
3111 return tevent_req_post(req, ev);
3113 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3117 static void cli_getattrE_done(struct tevent_req *subreq)
3119 struct tevent_req *req = tevent_req_callback_data(
3120 subreq, struct tevent_req);
3121 struct cli_getattrE_state *state = tevent_req_data(
3122 req, struct cli_getattrE_state);
3124 uint16_t *vwv = NULL;
3128 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3130 TALLOC_FREE(subreq);
3131 if (!NT_STATUS_IS_OK(status)) {
3132 tevent_req_nterror(req, status);
3136 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3137 state->attr = SVAL(vwv+10,0);
3138 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3139 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3140 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3142 tevent_req_done(req);
3145 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3148 time_t *change_time,
3149 time_t *access_time,
3152 struct cli_getattrE_state *state = tevent_req_data(
3153 req, struct cli_getattrE_state);
3156 if (tevent_req_is_nterror(req, &status)) {
3160 *attr = state->attr;
3163 *size = state->size;
3166 *change_time = state->change_time;
3169 *access_time = state->access_time;
3172 *write_time = state->write_time;
3174 return NT_STATUS_OK;
3177 NTSTATUS cli_getattrE(struct cli_state *cli,
3181 time_t *change_time,
3182 time_t *access_time,
3185 TALLOC_CTX *frame = talloc_stackframe();
3186 struct event_context *ev = NULL;
3187 struct tevent_req *req = NULL;
3188 NTSTATUS status = NT_STATUS_OK;
3190 if (cli_has_async_calls(cli)) {
3192 * Can't use sync call while an async call is in flight
3194 status = NT_STATUS_INVALID_PARAMETER;
3198 ev = event_context_init(frame);
3200 status = NT_STATUS_NO_MEMORY;
3204 req = cli_getattrE_send(frame, ev, cli, fnum);
3206 status = NT_STATUS_NO_MEMORY;
3210 if (!tevent_req_poll(req, ev)) {
3211 status = map_nt_error_from_unix(errno);
3215 status = cli_getattrE_recv(req,
3224 if (!NT_STATUS_IS_OK(status)) {
3225 cli_set_error(cli, status);
3230 /****************************************************************************
3232 ****************************************************************************/
3234 static void cli_getatr_done(struct tevent_req *subreq);
3236 struct cli_getatr_state {
3243 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3244 struct event_context *ev,
3245 struct cli_state *cli,
3248 struct tevent_req *req = NULL, *subreq = NULL;
3249 struct cli_getatr_state *state = NULL;
3250 uint8_t additional_flags = 0;
3251 uint8_t *bytes = NULL;
3253 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3258 state->zone_offset = cli->serverzone;
3260 bytes = talloc_array(state, uint8_t, 1);
3261 if (tevent_req_nomem(bytes, req)) {
3262 return tevent_req_post(req, ev);
3265 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3266 strlen(fname)+1, NULL);
3268 if (tevent_req_nomem(bytes, req)) {
3269 return tevent_req_post(req, ev);
3272 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3273 0, NULL, talloc_get_size(bytes), bytes);
3274 if (tevent_req_nomem(subreq, req)) {
3275 return tevent_req_post(req, ev);
3277 tevent_req_set_callback(subreq, cli_getatr_done, req);
3281 static void cli_getatr_done(struct tevent_req *subreq)
3283 struct tevent_req *req = tevent_req_callback_data(
3284 subreq, struct tevent_req);
3285 struct cli_getatr_state *state = tevent_req_data(
3286 req, struct cli_getatr_state);
3288 uint16_t *vwv = NULL;
3292 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3294 TALLOC_FREE(subreq);
3295 if (!NT_STATUS_IS_OK(status)) {
3296 tevent_req_nterror(req, status);
3300 state->attr = SVAL(vwv+0,0);
3301 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3302 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3304 tevent_req_done(req);
3307 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3312 struct cli_getatr_state *state = tevent_req_data(
3313 req, struct cli_getatr_state);
3316 if (tevent_req_is_nterror(req, &status)) {
3320 *attr = state->attr;
3323 *size = state->size;
3326 *write_time = state->write_time;
3328 return NT_STATUS_OK;
3331 NTSTATUS cli_getatr(struct cli_state *cli,
3337 TALLOC_CTX *frame = talloc_stackframe();
3338 struct event_context *ev = NULL;
3339 struct tevent_req *req = NULL;
3340 NTSTATUS status = NT_STATUS_OK;
3342 if (cli_has_async_calls(cli)) {
3344 * Can't use sync call while an async call is in flight
3346 status = NT_STATUS_INVALID_PARAMETER;
3350 ev = event_context_init(frame);
3352 status = NT_STATUS_NO_MEMORY;
3356 req = cli_getatr_send(frame, ev, cli, fname);
3358 status = NT_STATUS_NO_MEMORY;
3362 if (!tevent_req_poll(req, ev)) {
3363 status = map_nt_error_from_unix(errno);
3367 status = cli_getatr_recv(req,
3374 if (!NT_STATUS_IS_OK(status)) {
3375 cli_set_error(cli, status);
3380 /****************************************************************************
3381 Do a SMBsetattrE call.
3382 ****************************************************************************/
3384 static void cli_setattrE_done(struct tevent_req *subreq);
3386 struct cli_setattrE_state {
3390 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3391 struct event_context *ev,
3392 struct cli_state *cli,
3398 struct tevent_req *req = NULL, *subreq = NULL;
3399 struct cli_setattrE_state *state = NULL;
3400 uint8_t additional_flags = 0;
3402 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3407 SSVAL(state->vwv+0, 0, fnum);
3408 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3410 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3412 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3415 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3416 7, state->vwv, 0, NULL);
3417 if (tevent_req_nomem(subreq, req)) {
3418 return tevent_req_post(req, ev);
3420 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3424 static void cli_setattrE_done(struct tevent_req *subreq)
3426 struct tevent_req *req = tevent_req_callback_data(
3427 subreq, struct tevent_req);
3430 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3431 TALLOC_FREE(subreq);
3432 if (!NT_STATUS_IS_OK(status)) {
3433 tevent_req_nterror(req, status);
3436 tevent_req_done(req);
3439 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3441 return tevent_req_simple_recv_ntstatus(req);
3444 NTSTATUS cli_setattrE(struct cli_state *cli,
3450 TALLOC_CTX *frame = talloc_stackframe();
3451 struct event_context *ev = NULL;
3452 struct tevent_req *req = NULL;
3453 NTSTATUS status = NT_STATUS_OK;
3455 if (cli_has_async_calls(cli)) {
3457 * Can't use sync call while an async call is in flight
3459 status = NT_STATUS_INVALID_PARAMETER;
3463 ev = event_context_init(frame);
3465 status = NT_STATUS_NO_MEMORY;
3469 req = cli_setattrE_send(frame, ev,
3477 status = NT_STATUS_NO_MEMORY;
3481 if (!tevent_req_poll(req, ev)) {
3482 status = map_nt_error_from_unix(errno);
3486 status = cli_setattrE_recv(req);
3490 if (!NT_STATUS_IS_OK(status)) {
3491 cli_set_error(cli, status);
3496 /****************************************************************************
3497 Do a SMBsetatr call.
3498 ****************************************************************************/
3500 static void cli_setatr_done(struct tevent_req *subreq);
3502 struct cli_setatr_state {
3506 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3507 struct event_context *ev,
3508 struct cli_state *cli,
3513 struct tevent_req *req = NULL, *subreq = NULL;
3514 struct cli_setatr_state *state = NULL;
3515 uint8_t additional_flags = 0;
3516 uint8_t *bytes = NULL;
3518 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3523 SSVAL(state->vwv+0, 0, attr);
3524 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
3526 bytes = talloc_array(state, uint8_t, 1);
3527 if (tevent_req_nomem(bytes, req)) {
3528 return tevent_req_post(req, ev);
3531 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3532 strlen(fname)+1, NULL);
3533 if (tevent_req_nomem(bytes, req)) {
3534 return tevent_req_post(req, ev);
3536 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3537 talloc_get_size(bytes)+1);
3538 if (tevent_req_nomem(bytes, req)) {
3539 return tevent_req_post(req, ev);
3542 bytes[talloc_get_size(bytes)-1] = 4;
3543 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3545 if (tevent_req_nomem(bytes, req)) {
3546 return tevent_req_post(req, ev);
3549 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3550 8, state->vwv, talloc_get_size(bytes), bytes);
3551 if (tevent_req_nomem(subreq, req)) {
3552 return tevent_req_post(req, ev);
3554 tevent_req_set_callback(subreq, cli_setatr_done, req);
3558 static void cli_setatr_done(struct tevent_req *subreq)
3560 struct tevent_req *req = tevent_req_callback_data(
3561 subreq, struct tevent_req);
3564 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3565 TALLOC_FREE(subreq);
3566 if (!NT_STATUS_IS_OK(status)) {
3567 tevent_req_nterror(req, status);
3570 tevent_req_done(req);
3573 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3575 return tevent_req_simple_recv_ntstatus(req);
3578 NTSTATUS cli_setatr(struct cli_state *cli,
3583 TALLOC_CTX *frame = talloc_stackframe();
3584 struct event_context *ev = NULL;
3585 struct tevent_req *req = NULL;
3586 NTSTATUS status = NT_STATUS_OK;
3588 if (cli_has_async_calls(cli)) {
3590 * Can't use sync call while an async call is in flight
3592 status = NT_STATUS_INVALID_PARAMETER;
3596 ev = event_context_init(frame);
3598 status = NT_STATUS_NO_MEMORY;
3602 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3604 status = NT_STATUS_NO_MEMORY;
3608 if (!tevent_req_poll(req, ev)) {
3609 status = map_nt_error_from_unix(errno);
3613 status = cli_setatr_recv(req);
3617 if (!NT_STATUS_IS_OK(status)) {
3618 cli_set_error(cli, status);
3623 /****************************************************************************
3624 Check for existance of a dir.
3625 ****************************************************************************/
3627 static void cli_chkpath_done(struct tevent_req *subreq);
3629 struct cli_chkpath_state {
3633 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3634 struct event_context *ev,
3635 struct cli_state *cli,
3638 struct tevent_req *req = NULL, *subreq = NULL;
3639 struct cli_chkpath_state *state = NULL;
3640 uint8_t additional_flags = 0;
3641 uint8_t *bytes = NULL;
3643 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3648 bytes = talloc_array(state, uint8_t, 1);
3649 if (tevent_req_nomem(bytes, req)) {
3650 return tevent_req_post(req, ev);
3653 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3654 strlen(fname)+1, NULL);
3656 if (tevent_req_nomem(bytes, req)) {
3657 return tevent_req_post(req, ev);
3660 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3661 0, NULL, talloc_get_size(bytes), bytes);
3662 if (tevent_req_nomem(subreq, req)) {
3663 return tevent_req_post(req, ev);
3665 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3669 static void cli_chkpath_done(struct tevent_req *subreq)
3671 struct tevent_req *req = tevent_req_callback_data(
3672 subreq, struct tevent_req);
3675 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3676 TALLOC_FREE(subreq);
3677 if (!NT_STATUS_IS_OK(status)) {
3678 tevent_req_nterror(req, status);
3681 tevent_req_done(req);
3684 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3686 return tevent_req_simple_recv_ntstatus(req);
3689 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3691 TALLOC_CTX *frame = talloc_stackframe();
3692 struct event_context *ev = NULL;
3693 struct tevent_req *req = NULL;
3695 NTSTATUS status = NT_STATUS_OK;
3697 if (cli_has_async_calls(cli)) {
3699 * Can't use sync call while an async call is in flight
3701 status = NT_STATUS_INVALID_PARAMETER;
3705 path2 = talloc_strdup(frame, path);
3707 status = NT_STATUS_NO_MEMORY;
3710 trim_char(path2,'\0','\\');
3712 path2 = talloc_strdup(frame, "\\");
3714 status = NT_STATUS_NO_MEMORY;
3719 ev = event_context_init(frame);
3721 status = NT_STATUS_NO_MEMORY;
3725 req = cli_chkpath_send(frame, ev, cli, path2);
3727 status = NT_STATUS_NO_MEMORY;
3731 if (!tevent_req_poll(req, ev)) {
3732 status = map_nt_error_from_unix(errno);
3736 status = cli_chkpath_recv(req);
3740 if (!NT_STATUS_IS_OK(status)) {
3741 cli_set_error(cli, status);
3746 /****************************************************************************
3748 ****************************************************************************/
3750 static void cli_dskattr_done(struct tevent_req *subreq);
3752 struct cli_dskattr_state {
3758 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3759 struct event_context *ev,
3760 struct cli_state *cli)
3762 struct tevent_req *req = NULL, *subreq = NULL;
3763 struct cli_dskattr_state *state = NULL;
3764 uint8_t additional_flags = 0;
3766 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3771 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3773 if (tevent_req_nomem(subreq, req)) {
3774 return tevent_req_post(req, ev);
3776 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3780 static void cli_dskattr_done(struct tevent_req *subreq)
3782 struct tevent_req *req = tevent_req_callback_data(
3783 subreq, struct tevent_req);
3784 struct cli_dskattr_state *state = tevent_req_data(
3785 req, struct cli_dskattr_state);
3787 uint16_t *vwv = NULL;
3791 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3793 TALLOC_FREE(subreq);
3794 if (!NT_STATUS_IS_OK(status)) {
3795 tevent_req_nterror(req, status);
3798 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3799 state->total = SVAL(vwv+0, 0);
3800 state->avail = SVAL(vwv+3, 0);
3801 tevent_req_done(req);
3804 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3806 struct cli_dskattr_state *state = tevent_req_data(
3807 req, struct cli_dskattr_state);
3810 if (tevent_req_is_nterror(req, &status)) {
3813 *bsize = state->bsize;
3814 *total = state->total;
3815 *avail = state->avail;
3816 return NT_STATUS_OK;
3819 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3821 TALLOC_CTX *frame = talloc_stackframe();
3822 struct event_context *ev = NULL;
3823 struct tevent_req *req = NULL;
3824 NTSTATUS status = NT_STATUS_OK;
3826 if (cli_has_async_calls(cli)) {
3828 * Can't use sync call while an async call is in flight
3830 status = NT_STATUS_INVALID_PARAMETER;
3834 ev = event_context_init(frame);
3836 status = NT_STATUS_NO_MEMORY;
3840 req = cli_dskattr_send(frame, ev, cli);
3842 status = NT_STATUS_NO_MEMORY;
3846 if (!tevent_req_poll(req, ev)) {
3847 status = map_nt_error_from_unix(errno);
3851 status = cli_dskattr_recv(req, bsize, total, avail);
3855 if (!NT_STATUS_IS_OK(status)) {
3856 cli_set_error(cli, status);
3861 /****************************************************************************
3862 Create and open a temporary file.
3863 ****************************************************************************/
3865 static void cli_ctemp_done(struct tevent_req *subreq);
3867 struct ctemp_state {
3873 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3874 struct event_context *ev,
3875 struct cli_state *cli,
3878 struct tevent_req *req = NULL, *subreq = NULL;
3879 struct ctemp_state *state = NULL;
3880 uint8_t additional_flags = 0;
3881 uint8_t *bytes = NULL;
3883 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3888 SSVAL(state->vwv,0,0);
3889 SIVALS(state->vwv+1,0,-1);
3891 bytes = talloc_array(state, uint8_t, 1);
3892 if (tevent_req_nomem(bytes, req)) {
3893 return tevent_req_post(req, ev);
3896 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3897 strlen(path)+1, NULL);
3898 if (tevent_req_nomem(bytes, req)) {
3899 return tevent_req_post(req, ev);
3902 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3903 3, state->vwv, talloc_get_size(bytes), bytes);
3904 if (tevent_req_nomem(subreq, req)) {
3905 return tevent_req_post(req, ev);
3907 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3911 static void cli_ctemp_done(struct tevent_req *subreq)
3913 struct tevent_req *req = tevent_req_callback_data(
3914 subreq, struct tevent_req);
3915 struct ctemp_state *state = tevent_req_data(
3916 req, struct ctemp_state);
3920 uint32_t num_bytes = 0;
3921 uint8_t *bytes = NULL;
3924 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3925 &num_bytes, &bytes);
3926 TALLOC_FREE(subreq);
3927 if (!NT_STATUS_IS_OK(status)) {
3928 tevent_req_nterror(req, status);
3932 state->fnum = SVAL(vwv+0, 0);
3934 /* From W2K3, the result is just the ASCII name */
3935 if (num_bytes < 2) {
3936 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
3940 if (pull_string_talloc(state,
3947 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
3950 tevent_req_done(req);
3953 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
3958 struct ctemp_state *state = tevent_req_data(req,
3959 struct ctemp_state);
3962 if (tevent_req_is_nterror(req, &status)) {
3965 *pfnum = state->fnum;
3966 *outfile = talloc_strdup(ctx, state->ret_path);
3968 return NT_STATUS_NO_MEMORY;
3970 return NT_STATUS_OK;
3973 NTSTATUS cli_ctemp(struct cli_state *cli,
3979 TALLOC_CTX *frame = talloc_stackframe();
3980 struct event_context *ev;
3981 struct tevent_req *req;
3982 NTSTATUS status = NT_STATUS_OK;
3984 if (cli_has_async_calls(cli)) {
3986 * Can't use sync call while an async call is in flight
3988 status = NT_STATUS_INVALID_PARAMETER;
3992 ev = event_context_init(frame);
3994 status = NT_STATUS_NO_MEMORY;
3998 req = cli_ctemp_send(frame, ev, cli, path);
4000 status = NT_STATUS_NO_MEMORY;
4004 if (!tevent_req_poll(req, ev)) {
4005 status = map_nt_error_from_unix(errno);
4009 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4013 if (!NT_STATUS_IS_OK(status)) {
4014 cli_set_error(cli, status);
4020 send a raw ioctl - used by the torture code
4022 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4027 SSVAL(vwv+0, 0, fnum);
4028 SSVAL(vwv+1, 0, code>>16);
4029 SSVAL(vwv+2, 0, (code&0xFFFF));
4031 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4032 NULL, 0, NULL, NULL, NULL, NULL);
4033 if (!NT_STATUS_IS_OK(status)) {
4036 *blob = data_blob_null;
4037 return NT_STATUS_OK;
4040 /*********************************************************
4041 Set an extended attribute utility fn.
4042 *********************************************************/
4044 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4045 uint8_t *param, unsigned int param_len,
4046 const char *ea_name,
4047 const char *ea_val, size_t ea_len)
4050 unsigned int data_len = 0;
4051 uint8_t *data = NULL;
4053 size_t ea_namelen = strlen(ea_name);
4056 SSVAL(setup, 0, setup_val);
4058 if (ea_namelen == 0 && ea_len == 0) {
4060 data = (uint8_t *)SMB_MALLOC(data_len);
4062 return NT_STATUS_NO_MEMORY;
4065 SIVAL(p,0,data_len);
4067 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4068 data = (uint8_t *)SMB_MALLOC(data_len);
4070 return NT_STATUS_NO_MEMORY;
4073 SIVAL(p,0,data_len);
4075 SCVAL(p, 0, 0); /* EA flags. */
4076 SCVAL(p, 1, ea_namelen);
4077 SSVAL(p, 2, ea_len);
4078 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4079 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4082 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4084 param, param_len, 2,
4085 data, data_len, cli->max_xmit,
4087 NULL, 0, NULL, /* rsetup */
4088 NULL, 0, NULL, /* rparam */
4089 NULL, 0, NULL); /* rdata */
4094 /*********************************************************
4095 Set an extended attribute on a pathname.
4096 *********************************************************/
4098 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4099 const char *ea_name, const char *ea_val,
4102 unsigned int param_len = 0;
4104 size_t srclen = 2*(strlen(path)+1);
4108 param = SMB_MALLOC_ARRAY(uint8_t, 6+srclen+2);
4110 return NT_STATUS_NO_MEMORY;
4112 memset(param, '\0', 6);
4113 SSVAL(param,0,SMB_INFO_SET_EA);
4114 p = (char *)(¶m[6]);
4116 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4117 param_len = PTR_DIFF(p, param);
4119 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4120 ea_name, ea_val, ea_len);
4125 /*********************************************************
4126 Set an extended attribute on an fnum.
4127 *********************************************************/
4129 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4130 const char *ea_name, const char *ea_val,
4135 memset(param, 0, 6);
4136 SSVAL(param,0,fnum);
4137 SSVAL(param,2,SMB_INFO_SET_EA);
4139 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4140 ea_name, ea_val, ea_len);
4143 /*********************************************************
4144 Get an extended attribute list utility fn.
4145 *********************************************************/
4147 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4149 size_t *pnum_eas, struct ea_struct **pea_list)
4151 struct ea_struct *ea_list = NULL;
4156 if (rdata_len < 4) {
4160 ea_size = (size_t)IVAL(rdata,0);
4161 if (ea_size > rdata_len) {
4166 /* No EA's present. */
4175 /* Validate the EA list and count it. */
4176 for (num_eas = 0; ea_size >= 4; num_eas++) {
4177 unsigned int ea_namelen = CVAL(p,1);
4178 unsigned int ea_valuelen = SVAL(p,2);
4179 if (ea_namelen == 0) {
4182 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4185 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4186 p += 4 + ea_namelen + 1 + ea_valuelen;
4195 *pnum_eas = num_eas;
4197 /* Caller only wants number of EA's. */
4201 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
4206 ea_size = (size_t)IVAL(rdata,0);
4209 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4210 struct ea_struct *ea = &ea_list[num_eas];
4211 fstring unix_ea_name;
4212 unsigned int ea_namelen = CVAL(p,1);
4213 unsigned int ea_valuelen = SVAL(p,2);
4215 ea->flags = CVAL(p,0);
4216 unix_ea_name[0] = '\0';
4217 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4218 ea->name = talloc_strdup(ea_list, unix_ea_name);
4222 /* Ensure the value is null terminated (in case it's a string). */
4223 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4224 if (!ea->value.data) {
4228 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4230 ea->value.data[ea_valuelen] = 0;
4232 p += 4 + ea_namelen + 1 + ea_valuelen;
4235 *pea_list = ea_list;
4239 TALLOC_FREE(ea_list);
4243 /*********************************************************
4244 Get an extended attribute list from a pathname.
4245 *********************************************************/
4247 struct cli_get_ea_list_path_state {
4252 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4254 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4255 struct tevent_context *ev,
4256 struct cli_state *cli,
4259 struct tevent_req *req, *subreq;
4260 struct cli_get_ea_list_path_state *state;
4262 req = tevent_req_create(mem_ctx, &state,
4263 struct cli_get_ea_list_path_state);
4267 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4268 SMB_INFO_QUERY_ALL_EAS, 4,
4270 if (tevent_req_nomem(subreq, req)) {
4271 return tevent_req_post(req, ev);
4273 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4277 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4279 struct tevent_req *req = tevent_req_callback_data(
4280 subreq, struct tevent_req);
4281 struct cli_get_ea_list_path_state *state = tevent_req_data(
4282 req, struct cli_get_ea_list_path_state);
4285 status = cli_qpathinfo_recv(subreq, state, &state->data,
4287 TALLOC_FREE(subreq);
4288 if (!NT_STATUS_IS_OK(status)) {
4289 tevent_req_nterror(req, status);
4292 tevent_req_done(req);
4295 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4296 size_t *pnum_eas, struct ea_struct **peas)
4298 struct cli_get_ea_list_path_state *state = tevent_req_data(
4299 req, struct cli_get_ea_list_path_state);
4302 if (tevent_req_is_nterror(req, &status)) {
4305 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4307 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4309 return NT_STATUS_OK;
4312 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4315 struct ea_struct **pea_list)
4317 TALLOC_CTX *frame = talloc_stackframe();
4318 struct event_context *ev = NULL;
4319 struct tevent_req *req = NULL;
4320 NTSTATUS status = NT_STATUS_NO_MEMORY;
4322 if (cli_has_async_calls(cli)) {
4324 * Can't use sync call while an async call is in flight
4326 status = NT_STATUS_INVALID_PARAMETER;
4329 ev = event_context_init(frame);
4333 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4337 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4340 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4343 if (!NT_STATUS_IS_OK(status)) {
4344 cli_set_error(cli, status);
4349 /****************************************************************************
4350 Convert open "flags" arg to uint32_t on wire.
4351 ****************************************************************************/
4353 static uint32_t open_flags_to_wire(int flags)
4355 int open_mode = flags & O_ACCMODE;
4358 switch (open_mode) {
4360 ret |= SMB_O_WRONLY;
4367 ret |= SMB_O_RDONLY;
4371 if (flags & O_CREAT) {
4374 if (flags & O_EXCL) {
4377 if (flags & O_TRUNC) {
4381 if (flags & O_SYNC) {
4385 if (flags & O_APPEND) {
4386 ret |= SMB_O_APPEND;
4388 #if defined(O_DIRECT)
4389 if (flags & O_DIRECT) {
4390 ret |= SMB_O_DIRECT;
4393 #if defined(O_DIRECTORY)
4394 if (flags & O_DIRECTORY) {
4395 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4396 ret |= SMB_O_DIRECTORY;
4402 /****************************************************************************
4403 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4404 ****************************************************************************/
4406 struct posix_open_state {
4410 uint16_t fnum; /* Out */
4413 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4415 struct tevent_req *req = tevent_req_callback_data(
4416 subreq, struct tevent_req);
4417 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4422 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4423 NULL, 0, NULL, &data, 12, &num_data);
4424 TALLOC_FREE(subreq);
4425 if (!NT_STATUS_IS_OK(status)) {
4426 tevent_req_nterror(req, status);
4429 state->fnum = SVAL(data,2);
4430 tevent_req_done(req);
4433 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4434 struct event_context *ev,
4435 struct cli_state *cli,
4441 struct tevent_req *req = NULL, *subreq = NULL;
4442 struct posix_open_state *state = NULL;
4443 uint32_t wire_flags = open_flags_to_wire(flags);
4445 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4450 /* Setup setup word. */
4451 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4453 /* Setup param array. */
4454 state->param = talloc_array(state, uint8_t, 6);
4455 if (tevent_req_nomem(state->param, req)) {
4456 return tevent_req_post(req, ev);
4458 memset(state->param, '\0', 6);
4459 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4461 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4462 strlen(fname)+1, NULL);
4464 if (tevent_req_nomem(state->param, req)) {
4465 return tevent_req_post(req, ev);
4468 /* Setup data words. */
4470 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4471 wire_flags |= SMB_O_DIRECTORY;
4474 SIVAL(state->data,0,0); /* No oplock. */
4475 SIVAL(state->data,4,wire_flags);
4476 SIVAL(state->data,8,unix_perms_to_wire(mode));
4477 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4478 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4480 subreq = cli_trans_send(state, /* mem ctx. */
4481 ev, /* event ctx. */
4482 cli, /* cli_state. */
4483 SMBtrans2, /* cmd. */
4484 NULL, /* pipe name. */
4488 &state->setup, /* setup. */
4489 1, /* num setup uint16_t words. */
4490 0, /* max returned setup. */
4491 state->param, /* param. */
4492 talloc_get_size(state->param),/* num param. */
4493 2, /* max returned param. */
4494 state->data, /* data. */
4496 12); /* max returned data. */
4498 if (tevent_req_nomem(subreq, req)) {
4499 return tevent_req_post(req, ev);
4501 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4505 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4506 struct event_context *ev,
4507 struct cli_state *cli,
4512 return cli_posix_open_internal_send(mem_ctx, ev,
4513 cli, fname, flags, mode, false);
4516 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4518 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4521 if (tevent_req_is_nterror(req, &status)) {
4524 *pfnum = state->fnum;
4525 return NT_STATUS_OK;
4528 /****************************************************************************
4529 Open - POSIX semantics. Doesn't request oplock.
4530 ****************************************************************************/
4532 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4533 int flags, mode_t mode, uint16_t *pfnum)
4536 TALLOC_CTX *frame = talloc_stackframe();
4537 struct event_context *ev = NULL;
4538 struct tevent_req *req = NULL;
4539 NTSTATUS status = NT_STATUS_OK;
4541 if (cli_has_async_calls(cli)) {
4543 * Can't use sync call while an async call is in flight
4545 status = NT_STATUS_INVALID_PARAMETER;
4549 ev = event_context_init(frame);
4551 status = NT_STATUS_NO_MEMORY;
4555 req = cli_posix_open_send(frame,
4562 status = NT_STATUS_NO_MEMORY;
4566 if (!tevent_req_poll(req, ev)) {
4567 status = map_nt_error_from_unix(errno);
4571 status = cli_posix_open_recv(req, pfnum);
4575 if (!NT_STATUS_IS_OK(status)) {
4576 cli_set_error(cli, status);
4581 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4582 struct event_context *ev,
4583 struct cli_state *cli,
4587 return cli_posix_open_internal_send(mem_ctx, ev,
4588 cli, fname, O_CREAT, mode, true);
4591 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4593 return tevent_req_simple_recv_ntstatus(req);
4596 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4598 TALLOC_CTX *frame = talloc_stackframe();
4599 struct event_context *ev = NULL;
4600 struct tevent_req *req = NULL;
4601 NTSTATUS status = NT_STATUS_OK;
4603 if (cli_has_async_calls(cli)) {
4605 * Can't use sync call while an async call is in flight
4607 status = NT_STATUS_INVALID_PARAMETER;
4611 ev = event_context_init(frame);
4613 status = NT_STATUS_NO_MEMORY;
4617 req = cli_posix_mkdir_send(frame,
4623 status = NT_STATUS_NO_MEMORY;
4627 if (!tevent_req_poll(req, ev)) {
4628 status = map_nt_error_from_unix(errno);
4632 status = cli_posix_mkdir_recv(req);
4636 if (!NT_STATUS_IS_OK(status)) {
4637 cli_set_error(cli, status);
4642 /****************************************************************************
4643 unlink or rmdir - POSIX semantics.
4644 ****************************************************************************/
4646 struct cli_posix_unlink_internal_state {
4650 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4652 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4653 struct event_context *ev,
4654 struct cli_state *cli,
4658 struct tevent_req *req = NULL, *subreq = NULL;
4659 struct cli_posix_unlink_internal_state *state = NULL;
4661 req = tevent_req_create(mem_ctx, &state,
4662 struct cli_posix_unlink_internal_state);
4667 /* Setup data word. */
4668 SSVAL(state->data, 0, level);
4670 subreq = cli_setpathinfo_send(state, ev, cli,
4671 SMB_POSIX_PATH_UNLINK,
4673 state->data, sizeof(state->data));
4674 if (tevent_req_nomem(subreq, req)) {
4675 return tevent_req_post(req, ev);
4677 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4681 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4683 NTSTATUS status = cli_setpathinfo_recv(subreq);
4684 tevent_req_simple_finish_ntstatus(subreq, status);
4687 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4688 struct event_context *ev,
4689 struct cli_state *cli,
4692 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4693 SMB_POSIX_UNLINK_FILE_TARGET);
4696 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4698 return tevent_req_simple_recv_ntstatus(req);
4701 /****************************************************************************
4702 unlink - POSIX semantics.
4703 ****************************************************************************/
4705 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4707 TALLOC_CTX *frame = talloc_stackframe();
4708 struct event_context *ev = NULL;
4709 struct tevent_req *req = NULL;
4710 NTSTATUS status = NT_STATUS_OK;
4712 if (cli_has_async_calls(cli)) {
4714 * Can't use sync call while an async call is in flight
4716 status = NT_STATUS_INVALID_PARAMETER;
4720 ev = event_context_init(frame);
4722 status = NT_STATUS_NO_MEMORY;
4726 req = cli_posix_unlink_send(frame,
4731 status = NT_STATUS_NO_MEMORY;
4735 if (!tevent_req_poll(req, ev)) {
4736 status = map_nt_error_from_unix(errno);
4740 status = cli_posix_unlink_recv(req);
4744 if (!NT_STATUS_IS_OK(status)) {
4745 cli_set_error(cli, status);
4750 /****************************************************************************
4751 rmdir - POSIX semantics.
4752 ****************************************************************************/
4754 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4755 struct event_context *ev,
4756 struct cli_state *cli,
4759 return cli_posix_unlink_internal_send(
4760 mem_ctx, ev, cli, fname,
4761 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4764 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4766 return tevent_req_simple_recv_ntstatus(req);
4769 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4771 TALLOC_CTX *frame = talloc_stackframe();
4772 struct event_context *ev = NULL;
4773 struct tevent_req *req = NULL;
4774 NTSTATUS status = NT_STATUS_OK;
4776 if (cli_has_async_calls(cli)) {
4778 * Can't use sync call while an async call is in flight
4780 status = NT_STATUS_INVALID_PARAMETER;
4784 ev = event_context_init(frame);
4786 status = NT_STATUS_NO_MEMORY;
4790 req = cli_posix_rmdir_send(frame,
4795 status = NT_STATUS_NO_MEMORY;
4799 if (!tevent_req_poll(req, ev)) {
4800 status = map_nt_error_from_unix(errno);
4804 status = cli_posix_rmdir_recv(req, frame);
4808 if (!NT_STATUS_IS_OK(status)) {
4809 cli_set_error(cli, status);
4814 /****************************************************************************
4816 ****************************************************************************/
4818 struct cli_notify_state {
4820 uint32_t num_changes;
4821 struct notify_change *changes;
4824 static void cli_notify_done(struct tevent_req *subreq);
4826 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4827 struct tevent_context *ev,
4828 struct cli_state *cli, uint16_t fnum,
4829 uint32_t buffer_size,
4830 uint32_t completion_filter, bool recursive)
4832 struct tevent_req *req, *subreq;
4833 struct cli_notify_state *state;
4835 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4840 SIVAL(state->setup, 0, completion_filter);
4841 SSVAL(state->setup, 4, fnum);
4842 SSVAL(state->setup, 6, recursive);
4844 subreq = cli_trans_send(
4845 state, /* mem ctx. */
4846 ev, /* event ctx. */
4847 cli, /* cli_state. */
4848 SMBnttrans, /* cmd. */
4849 NULL, /* pipe name. */
4851 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4853 (uint16_t *)state->setup, /* setup. */
4854 4, /* num setup uint16_t words. */
4855 0, /* max returned setup. */
4858 buffer_size, /* max returned param. */
4861 0); /* max returned data. */
4863 if (tevent_req_nomem(subreq, req)) {
4864 return tevent_req_post(req, ev);
4866 tevent_req_set_callback(subreq, cli_notify_done, req);
4870 static void cli_notify_done(struct tevent_req *subreq)
4872 struct tevent_req *req = tevent_req_callback_data(
4873 subreq, struct tevent_req);
4874 struct cli_notify_state *state = tevent_req_data(
4875 req, struct cli_notify_state);
4878 uint32_t i, ofs, num_params;
4881 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4882 ¶ms, 0, &num_params, NULL, 0, NULL);
4883 TALLOC_FREE(subreq);
4884 if (!NT_STATUS_IS_OK(status)) {
4885 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4886 tevent_req_nterror(req, status);
4890 state->num_changes = 0;
4893 while (num_params - ofs > 12) {
4894 uint32_t len = IVAL(params, ofs);
4895 state->num_changes += 1;
4897 if ((len == 0) || (ofs+len >= num_params)) {
4903 state->changes = talloc_array(state, struct notify_change,
4904 state->num_changes);
4905 if (tevent_req_nomem(state->changes, req)) {
4906 TALLOC_FREE(params);
4912 for (i=0; i<state->num_changes; i++) {
4913 uint32_t next = IVAL(params, ofs);
4914 uint32_t len = IVAL(params, ofs+8);
4918 if ((next != 0) && (len+12 != next)) {
4919 TALLOC_FREE(params);
4921 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4925 state->changes[i].action = IVAL(params, ofs+4);
4926 ret = clistr_pull_talloc(params, (char *)params, flags2,
4927 &name, params+ofs+12, len,
4928 STR_TERMINATE|STR_UNICODE);
4930 TALLOC_FREE(params);
4931 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4934 state->changes[i].name = name;
4938 TALLOC_FREE(params);
4939 tevent_req_done(req);
4942 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4943 uint32_t *pnum_changes,
4944 struct notify_change **pchanges)
4946 struct cli_notify_state *state = tevent_req_data(
4947 req, struct cli_notify_state);
4950 if (tevent_req_is_nterror(req, &status)) {
4954 *pnum_changes = state->num_changes;
4955 *pchanges = talloc_move(mem_ctx, &state->changes);
4956 return NT_STATUS_OK;
4959 struct cli_qpathinfo_state {
4968 static void cli_qpathinfo_done(struct tevent_req *subreq);
4970 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
4971 struct tevent_context *ev,
4972 struct cli_state *cli, const char *fname,
4973 uint16_t level, uint32_t min_rdata,
4976 struct tevent_req *req, *subreq;
4977 struct cli_qpathinfo_state *state;
4979 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
4983 state->min_rdata = min_rdata;
4984 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
4986 state->param = talloc_zero_array(state, uint8_t, 6);
4987 if (tevent_req_nomem(state->param, req)) {
4988 return tevent_req_post(req, ev);
4990 SSVAL(state->param, 0, level);
4991 state->param = trans2_bytes_push_str(
4992 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
4993 if (tevent_req_nomem(state->param, req)) {
4994 return tevent_req_post(req, ev);
4997 subreq = cli_trans_send(
4998 state, /* mem ctx. */
4999 ev, /* event ctx. */
5000 cli, /* cli_state. */
5001 SMBtrans2, /* cmd. */
5002 NULL, /* pipe name. */
5006 state->setup, /* setup. */
5007 1, /* num setup uint16_t words. */
5008 0, /* max returned setup. */
5009 state->param, /* param. */
5010 talloc_get_size(state->param), /* num param. */
5011 2, /* max returned param. */
5014 max_rdata); /* max returned data. */
5016 if (tevent_req_nomem(subreq, req)) {
5017 return tevent_req_post(req, ev);
5019 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5023 static void cli_qpathinfo_done(struct tevent_req *subreq)
5025 struct tevent_req *req = tevent_req_callback_data(
5026 subreq, struct tevent_req);
5027 struct cli_qpathinfo_state *state = tevent_req_data(
5028 req, struct cli_qpathinfo_state);
5031 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5033 &state->rdata, state->min_rdata,
5035 if (!NT_STATUS_IS_OK(status)) {
5036 tevent_req_nterror(req, status);
5039 tevent_req_done(req);
5042 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5043 uint8_t **rdata, uint32_t *num_rdata)
5045 struct cli_qpathinfo_state *state = tevent_req_data(
5046 req, struct cli_qpathinfo_state);
5049 if (tevent_req_is_nterror(req, &status)) {
5052 if (rdata != NULL) {
5053 *rdata = talloc_move(mem_ctx, &state->rdata);
5055 TALLOC_FREE(state->rdata);
5057 if (num_rdata != NULL) {
5058 *num_rdata = state->num_rdata;
5060 return NT_STATUS_OK;
5063 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5064 const char *fname, uint16_t level, uint32_t min_rdata,
5066 uint8_t **rdata, uint32_t *num_rdata)
5068 TALLOC_CTX *frame = talloc_stackframe();
5069 struct event_context *ev;
5070 struct tevent_req *req;
5071 NTSTATUS status = NT_STATUS_NO_MEMORY;
5073 if (cli_has_async_calls(cli)) {
5075 * Can't use sync call while an async call is in flight
5077 status = NT_STATUS_INVALID_PARAMETER;
5080 ev = event_context_init(frame);
5084 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5089 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5092 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5095 if (!NT_STATUS_IS_OK(status)) {
5096 cli_set_error(cli, status);
5101 struct cli_qfileinfo_state {
5110 static void cli_qfileinfo_done(struct tevent_req *subreq);
5112 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5113 struct tevent_context *ev,
5114 struct cli_state *cli, uint16_t fnum,
5115 uint16_t level, uint32_t min_rdata,
5118 struct tevent_req *req, *subreq;
5119 struct cli_qfileinfo_state *state;
5121 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5125 state->min_rdata = min_rdata;
5126 SSVAL(state->param, 0, fnum);
5127 SSVAL(state->param, 2, level);
5128 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5130 subreq = cli_trans_send(
5131 state, /* mem ctx. */
5132 ev, /* event ctx. */
5133 cli, /* cli_state. */
5134 SMBtrans2, /* cmd. */
5135 NULL, /* pipe name. */
5139 state->setup, /* setup. */
5140 1, /* num setup uint16_t words. */
5141 0, /* max returned setup. */
5142 state->param, /* param. */
5143 sizeof(state->param), /* num param. */
5144 2, /* max returned param. */
5147 max_rdata); /* max returned data. */
5149 if (tevent_req_nomem(subreq, req)) {
5150 return tevent_req_post(req, ev);
5152 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5156 static void cli_qfileinfo_done(struct tevent_req *subreq)
5158 struct tevent_req *req = tevent_req_callback_data(
5159 subreq, struct tevent_req);
5160 struct cli_qfileinfo_state *state = tevent_req_data(
5161 req, struct cli_qfileinfo_state);
5164 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5166 &state->rdata, state->min_rdata,
5168 if (!NT_STATUS_IS_OK(status)) {
5169 tevent_req_nterror(req, status);
5172 tevent_req_done(req);
5175 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5176 uint8_t **rdata, uint32_t *num_rdata)
5178 struct cli_qfileinfo_state *state = tevent_req_data(
5179 req, struct cli_qfileinfo_state);
5182 if (tevent_req_is_nterror(req, &status)) {
5185 if (rdata != NULL) {
5186 *rdata = talloc_move(mem_ctx, &state->rdata);
5188 TALLOC_FREE(state->rdata);
5190 if (num_rdata != NULL) {
5191 *num_rdata = state->num_rdata;
5193 return NT_STATUS_OK;
5196 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5197 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5199 uint8_t **rdata, uint32_t *num_rdata)
5201 TALLOC_CTX *frame = talloc_stackframe();
5202 struct event_context *ev;
5203 struct tevent_req *req;
5204 NTSTATUS status = NT_STATUS_NO_MEMORY;
5206 if (cli_has_async_calls(cli)) {
5208 * Can't use sync call while an async call is in flight
5210 status = NT_STATUS_INVALID_PARAMETER;
5213 ev = event_context_init(frame);
5217 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5222 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5225 status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
5228 if (!NT_STATUS_IS_OK(status)) {
5229 cli_set_error(cli, status);
5234 struct cli_flush_state {
5238 static void cli_flush_done(struct tevent_req *subreq);
5240 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5241 struct event_context *ev,
5242 struct cli_state *cli,
5245 struct tevent_req *req, *subreq;
5246 struct cli_flush_state *state;
5248 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5252 SSVAL(state->vwv + 0, 0, fnum);
5254 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5256 if (tevent_req_nomem(subreq, req)) {
5257 return tevent_req_post(req, ev);
5259 tevent_req_set_callback(subreq, cli_flush_done, req);
5263 static void cli_flush_done(struct tevent_req *subreq)
5265 struct tevent_req *req = tevent_req_callback_data(
5266 subreq, struct tevent_req);
5269 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5270 TALLOC_FREE(subreq);
5271 if (!NT_STATUS_IS_OK(status)) {
5272 tevent_req_nterror(req, status);
5275 tevent_req_done(req);
5278 NTSTATUS cli_flush_recv(struct tevent_req *req)
5280 return tevent_req_simple_recv_ntstatus(req);
5283 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5285 TALLOC_CTX *frame = talloc_stackframe();
5286 struct event_context *ev;
5287 struct tevent_req *req;
5288 NTSTATUS status = NT_STATUS_NO_MEMORY;
5290 if (cli_has_async_calls(cli)) {
5292 * Can't use sync call while an async call is in flight
5294 status = NT_STATUS_INVALID_PARAMETER;
5297 ev = event_context_init(frame);
5301 req = cli_flush_send(frame, ev, cli, fnum);
5305 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5308 status = cli_flush_recv(req);
5311 if (!NT_STATUS_IS_OK(status)) {
5312 cli_set_error(cli, status);
5317 struct cli_shadow_copy_data_state {
5324 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5326 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5327 struct tevent_context *ev,
5328 struct cli_state *cli,
5332 struct tevent_req *req, *subreq;
5333 struct cli_shadow_copy_data_state *state;
5336 req = tevent_req_create(mem_ctx, &state,
5337 struct cli_shadow_copy_data_state);
5341 state->get_names = get_names;
5342 ret_size = get_names ? cli->max_xmit : 16;
5344 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5345 SSVAL(state->setup + 2, 0, fnum);
5346 SCVAL(state->setup + 3, 0, 0); /* isFsctl */
5347 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5349 subreq = cli_trans_send(
5350 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5351 state->setup, ARRAY_SIZE(state->setup), 0,
5354 if (tevent_req_nomem(subreq, req)) {
5355 return tevent_req_post(req, ev);
5357 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5361 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5363 struct tevent_req *req = tevent_req_callback_data(
5364 subreq, struct tevent_req);
5365 struct cli_shadow_copy_data_state *state = tevent_req_data(
5366 req, struct cli_shadow_copy_data_state);
5369 status = cli_trans_recv(subreq, state, NULL,
5370 NULL, 0, NULL, /* setup */
5371 NULL, 0, NULL, /* param */
5372 &state->data, 12, &state->num_data);
5373 TALLOC_FREE(subreq);
5374 if (!NT_STATUS_IS_OK(status)) {
5375 tevent_req_nterror(req, status);
5378 tevent_req_done(req);
5381 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5382 char ***pnames, int *pnum_names)
5384 struct cli_shadow_copy_data_state *state = tevent_req_data(
5385 req, struct cli_shadow_copy_data_state);
5391 if (tevent_req_is_nterror(req, &status)) {
5394 num_names = IVAL(state->data, 4);
5395 dlength = IVAL(state->data, 8);
5397 if (!state->get_names) {
5398 *pnum_names = num_names;
5399 return NT_STATUS_OK;
5402 if (dlength+12 > state->num_data) {
5403 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5405 names = talloc_array(mem_ctx, char *, num_names);
5406 if (names == NULL) {
5407 return NT_STATUS_NO_MEMORY;
5410 for (i=0; i<num_names; i++) {
5413 size_t converted_size;
5415 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5416 ret = convert_string_talloc(
5417 names, CH_UTF16LE, CH_UNIX,
5418 src, 2 * sizeof(SHADOW_COPY_LABEL),
5419 &names[i], &converted_size);
5422 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5425 *pnum_names = num_names;
5427 return NT_STATUS_OK;
5430 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5431 uint16_t fnum, bool get_names,
5432 char ***pnames, int *pnum_names)
5434 TALLOC_CTX *frame = talloc_stackframe();
5435 struct event_context *ev;
5436 struct tevent_req *req;
5437 NTSTATUS status = NT_STATUS_NO_MEMORY;
5439 if (cli_has_async_calls(cli)) {
5441 * Can't use sync call while an async call is in flight
5443 status = NT_STATUS_INVALID_PARAMETER;
5446 ev = event_context_init(frame);
5450 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5454 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5457 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5460 if (!NT_STATUS_IS_OK(status)) {
5461 cli_set_error(cli, status);