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/>.
23 /***********************************************************
24 Common function for pushing stings, used by smb_bytes_push_str()
25 and trans_bytes_push_str(). Only difference is the align_odd
27 ***********************************************************/
29 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
30 const char *str, size_t str_len,
32 size_t *pconverted_size)
36 size_t converted_size;
42 buflen = talloc_get_size(buf);
44 if (align_odd && ucs2 && (buflen % 2 == 0)) {
46 * We're pushing into an SMB buffer, align odd
48 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
56 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
57 ucs2 ? CH_UTF16LE : CH_DOS,
58 str, str_len, &converted,
59 &converted_size, true)) {
63 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
64 buflen + converted_size);
66 TALLOC_FREE(converted);
70 memcpy(buf + buflen, converted, converted_size);
72 TALLOC_FREE(converted);
74 if (pconverted_size) {
75 *pconverted_size = converted_size;
81 /***********************************************************
82 Push a string into an SMB buffer, with odd byte alignment
83 if it's a UCS2 string.
84 ***********************************************************/
86 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
87 const char *str, size_t str_len,
88 size_t *pconverted_size)
90 return internal_bytes_push_str(buf, ucs2, str, str_len,
91 true, pconverted_size);
94 /***********************************************************
95 Same as smb_bytes_push_str(), but without the odd byte
96 align for ucs2 (we're pushing into a param or data block).
97 static for now, although this will probably change when
98 other modules use async trans calls.
99 ***********************************************************/
101 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
102 const char *str, size_t str_len,
103 size_t *pconverted_size)
105 return internal_bytes_push_str(buf, ucs2, str, str_len,
106 false, pconverted_size);
109 /****************************************************************************
110 Hard/Symlink a file (UNIX extensions).
111 Creates new name (sym)linked to oldname.
112 ****************************************************************************/
120 static void cli_posix_link_internal_done(struct tevent_req *subreq)
122 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, 0, NULL,
123 NULL, 0, NULL, NULL, 0, NULL);
124 tevent_req_simple_finish_ntstatus(subreq, status);
127 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
128 struct event_context *ev,
129 struct cli_state *cli,
134 struct tevent_req *req = NULL, *subreq = NULL;
135 struct link_state *state = NULL;
137 req = tevent_req_create(mem_ctx, &state, struct link_state);
142 /* Setup setup word. */
143 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
145 /* Setup param array. */
146 state->param = talloc_array(state, uint8_t, 6);
147 if (tevent_req_nomem(state->param, req)) {
148 return tevent_req_post(req, ev);
150 memset(state->param, '\0', 6);
151 SSVAL(state->param,0,hardlink ? SMB_SET_FILE_UNIX_HLINK : SMB_SET_FILE_UNIX_LINK);
153 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), newname,
154 strlen(newname)+1, NULL);
156 if (tevent_req_nomem(state->param, req)) {
157 return tevent_req_post(req, ev);
160 /* Setup data array. */
161 state->data = talloc_array(state, uint8_t, 0);
162 if (tevent_req_nomem(state->data, req)) {
163 return tevent_req_post(req, ev);
165 state->data = trans2_bytes_push_str(state->data, cli_ucs2(cli), oldname,
166 strlen(oldname)+1, NULL);
168 subreq = cli_trans_send(state, /* mem ctx. */
170 cli, /* cli_state. */
171 SMBtrans2, /* cmd. */
172 NULL, /* pipe name. */
176 &state->setup, /* setup. */
177 1, /* num setup uint16_t words. */
178 0, /* max returned setup. */
179 state->param, /* param. */
180 talloc_get_size(state->param), /* num param. */
181 2, /* max returned param. */
182 state->data, /* data. */
183 talloc_get_size(state->data), /* num data. */
184 0); /* max returned data. */
186 if (tevent_req_nomem(subreq, req)) {
187 return tevent_req_post(req, ev);
189 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
193 /****************************************************************************
194 Symlink a file (UNIX extensions).
195 ****************************************************************************/
197 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
198 struct event_context *ev,
199 struct cli_state *cli,
203 return cli_posix_link_internal_send(mem_ctx, ev, cli,
204 oldname, newname, false);
207 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
211 if (tevent_req_is_nterror(req, &status)) {
217 NTSTATUS cli_posix_symlink(struct cli_state *cli,
221 TALLOC_CTX *frame = talloc_stackframe();
222 struct event_context *ev = NULL;
223 struct tevent_req *req = NULL;
224 NTSTATUS status = NT_STATUS_OK;
226 if (cli_has_async_calls(cli)) {
228 * Can't use sync call while an async call is in flight
230 status = NT_STATUS_INVALID_PARAMETER;
234 ev = event_context_init(frame);
236 status = NT_STATUS_NO_MEMORY;
240 req = cli_posix_symlink_send(frame,
246 status = NT_STATUS_NO_MEMORY;
250 if (!tevent_req_poll(req, ev)) {
251 status = map_nt_error_from_unix(errno);
255 status = cli_posix_symlink_recv(req);
259 if (!NT_STATUS_IS_OK(status)) {
260 cli_set_error(cli, status);
265 /****************************************************************************
266 Read a POSIX symlink.
267 ****************************************************************************/
269 struct readlink_state {
276 static void cli_posix_readlink_done(struct tevent_req *subreq);
278 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
279 struct event_context *ev,
280 struct cli_state *cli,
284 struct tevent_req *req = NULL, *subreq = NULL;
285 struct readlink_state *state = NULL;
286 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
288 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
294 * Len is in bytes, we need it in UCS2 units.
296 if ((2*len < len) || (maxbytelen < len)) {
297 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
298 return tevent_req_post(req, ev);
301 subreq = cli_qpathinfo_send(state, ev, cli, fname,
302 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
303 if (tevent_req_nomem(subreq, req)) {
304 return tevent_req_post(req, ev);
306 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
310 static void cli_posix_readlink_done(struct tevent_req *subreq)
312 struct tevent_req *req = tevent_req_callback_data(
313 subreq, struct tevent_req);
314 struct readlink_state *state = tevent_req_data(
315 req, struct readlink_state);
318 status = cli_qpathinfo_recv(subreq, state, &state->data,
321 if (!NT_STATUS_IS_OK(status)) {
322 tevent_req_nterror(req, status);
326 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
328 if (state->data[state->num_data-1] != '\0') {
329 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
332 tevent_req_done(req);
335 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
336 char *retpath, size_t len)
339 char *converted = NULL;
340 size_t converted_size = 0;
341 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
343 if (tevent_req_is_nterror(req, &status)) {
346 /* The returned data is a pushed string, not raw data. */
347 if (!convert_string_talloc(state,
348 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
355 return NT_STATUS_NO_MEMORY;
358 len = MIN(len,converted_size);
360 return NT_STATUS_DATA_ERROR;
362 memcpy(retpath, converted, len);
366 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
367 char *linkpath, size_t len)
369 TALLOC_CTX *frame = talloc_stackframe();
370 struct event_context *ev = NULL;
371 struct tevent_req *req = NULL;
372 NTSTATUS status = NT_STATUS_OK;
374 if (cli_has_async_calls(cli)) {
376 * Can't use sync call while an async call is in flight
378 status = NT_STATUS_INVALID_PARAMETER;
382 ev = event_context_init(frame);
384 status = NT_STATUS_NO_MEMORY;
388 req = cli_posix_readlink_send(frame,
394 status = NT_STATUS_NO_MEMORY;
398 if (!tevent_req_poll(req, ev)) {
399 status = map_nt_error_from_unix(errno);
403 status = cli_posix_readlink_recv(req, cli, linkpath, len);
407 if (!NT_STATUS_IS_OK(status)) {
408 cli_set_error(cli, status);
413 /****************************************************************************
414 Hard link a file (UNIX extensions).
415 ****************************************************************************/
417 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
418 struct event_context *ev,
419 struct cli_state *cli,
423 return cli_posix_link_internal_send(mem_ctx, ev, cli,
424 oldname, newname, true);
427 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
431 if (tevent_req_is_nterror(req, &status)) {
437 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
441 TALLOC_CTX *frame = talloc_stackframe();
442 struct event_context *ev = NULL;
443 struct tevent_req *req = NULL;
444 NTSTATUS status = NT_STATUS_OK;
446 if (cli_has_async_calls(cli)) {
448 * Can't use sync call while an async call is in flight
450 status = NT_STATUS_INVALID_PARAMETER;
454 ev = event_context_init(frame);
456 status = NT_STATUS_NO_MEMORY;
460 req = cli_posix_hardlink_send(frame,
466 status = NT_STATUS_NO_MEMORY;
470 if (!tevent_req_poll(req, ev)) {
471 status = map_nt_error_from_unix(errno);
475 status = cli_posix_hardlink_recv(req);
479 if (!NT_STATUS_IS_OK(status)) {
480 cli_set_error(cli, status);
485 /****************************************************************************
486 Map standard UNIX permissions onto wire representations.
487 ****************************************************************************/
489 uint32_t unix_perms_to_wire(mode_t perms)
491 unsigned int ret = 0;
493 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
494 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
495 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
496 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
497 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
498 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
499 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
500 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
501 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
503 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
506 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
509 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
514 /****************************************************************************
515 Map wire permissions to standard UNIX.
516 ****************************************************************************/
518 mode_t wire_perms_to_unix(uint32_t perms)
520 mode_t ret = (mode_t)0;
522 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
523 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
524 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
525 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
526 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
527 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
528 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
529 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
530 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
532 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
535 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
538 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
543 /****************************************************************************
544 Return the file type from the wire filetype for UNIX extensions.
545 ****************************************************************************/
547 static mode_t unix_filetype_from_wire(uint32_t wire_type)
555 case UNIX_TYPE_SYMLINK:
559 case UNIX_TYPE_CHARDEV:
563 case UNIX_TYPE_BLKDEV:
571 case UNIX_TYPE_SOCKET:
579 /****************************************************************************
580 Do a POSIX getfacl (UNIX extensions).
581 ****************************************************************************/
583 struct getfacl_state {
590 static void cli_posix_getfacl_done(struct tevent_req *subreq);
592 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
593 struct event_context *ev,
594 struct cli_state *cli,
597 struct tevent_req *req = NULL, *subreq = NULL;
598 struct link_state *state = NULL;
600 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
604 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
606 if (tevent_req_nomem(subreq, req)) {
607 return tevent_req_post(req, ev);
609 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
613 static void cli_posix_getfacl_done(struct tevent_req *subreq)
615 struct tevent_req *req = tevent_req_callback_data(
616 subreq, struct tevent_req);
617 struct getfacl_state *state = tevent_req_data(
618 req, struct getfacl_state);
621 status = cli_qpathinfo_recv(subreq, state, &state->data,
624 if (!NT_STATUS_IS_OK(status)) {
625 tevent_req_nterror(req, status);
628 tevent_req_done(req);
631 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
636 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
639 if (tevent_req_is_nterror(req, &status)) {
642 *prb_size = (size_t)state->num_data;
643 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
647 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
653 TALLOC_CTX *frame = talloc_stackframe();
654 struct event_context *ev = NULL;
655 struct tevent_req *req = NULL;
656 NTSTATUS status = NT_STATUS_OK;
658 if (cli_has_async_calls(cli)) {
660 * Can't use sync call while an async call is in flight
662 status = NT_STATUS_INVALID_PARAMETER;
666 ev = event_context_init(frame);
668 status = NT_STATUS_NO_MEMORY;
672 req = cli_posix_getfacl_send(frame,
677 status = NT_STATUS_NO_MEMORY;
681 if (!tevent_req_poll(req, ev)) {
682 status = map_nt_error_from_unix(errno);
686 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
690 if (!NT_STATUS_IS_OK(status)) {
691 cli_set_error(cli, status);
696 /****************************************************************************
697 Stat a file (UNIX extensions).
698 ****************************************************************************/
707 static void cli_posix_stat_done(struct tevent_req *subreq);
709 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
710 struct event_context *ev,
711 struct cli_state *cli,
714 struct tevent_req *req = NULL, *subreq = NULL;
715 struct stat_state *state = NULL;
717 req = tevent_req_create(mem_ctx, &state, struct stat_state);
721 subreq = cli_qpathinfo_send(state, ev, cli, fname,
722 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
723 if (tevent_req_nomem(subreq, req)) {
724 return tevent_req_post(req, ev);
726 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
730 static void cli_posix_stat_done(struct tevent_req *subreq)
732 struct tevent_req *req = tevent_req_callback_data(
733 subreq, struct tevent_req);
734 struct stat_state *state = tevent_req_data(req, struct stat_state);
737 status = cli_qpathinfo_recv(subreq, state, &state->data,
740 if (!NT_STATUS_IS_OK(status)) {
741 tevent_req_nterror(req, status);
744 tevent_req_done(req);
747 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
748 SMB_STRUCT_STAT *sbuf)
750 struct stat_state *state = tevent_req_data(req, struct stat_state);
753 if (tevent_req_is_nterror(req, &status)) {
757 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
758 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
759 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
760 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
762 /* assume 512 byte blocks */
763 sbuf->st_ex_blocks /= 512;
765 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
766 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
767 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
769 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
770 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
771 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
772 #if defined(HAVE_MAKEDEV)
774 uint32_t dev_major = IVAL(state->data,60);
775 uint32_t dev_minor = IVAL(state->data,68);
776 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
779 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
780 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
781 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
786 NTSTATUS cli_posix_stat(struct cli_state *cli,
788 SMB_STRUCT_STAT *sbuf)
790 TALLOC_CTX *frame = talloc_stackframe();
791 struct event_context *ev = NULL;
792 struct tevent_req *req = NULL;
793 NTSTATUS status = NT_STATUS_OK;
795 if (cli_has_async_calls(cli)) {
797 * Can't use sync call while an async call is in flight
799 status = NT_STATUS_INVALID_PARAMETER;
803 ev = event_context_init(frame);
805 status = NT_STATUS_NO_MEMORY;
809 req = cli_posix_stat_send(frame,
814 status = NT_STATUS_NO_MEMORY;
818 if (!tevent_req_poll(req, ev)) {
819 status = map_nt_error_from_unix(errno);
823 status = cli_posix_stat_recv(req, sbuf);
827 if (!NT_STATUS_IS_OK(status)) {
828 cli_set_error(cli, status);
833 /****************************************************************************
834 Chmod or chown a file internal (UNIX extensions).
835 ****************************************************************************/
843 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
845 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, 0, NULL,
846 NULL, 0, NULL, NULL, 0, NULL);
847 tevent_req_simple_finish_ntstatus(subreq, status);
850 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
851 struct event_context *ev,
852 struct cli_state *cli,
858 struct tevent_req *req = NULL, *subreq = NULL;
859 struct ch_state *state = NULL;
861 req = tevent_req_create(mem_ctx, &state, struct ch_state);
866 /* Setup setup word. */
867 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
869 /* Setup param array. */
870 state->param = talloc_array(state, uint8_t, 6);
871 if (tevent_req_nomem(state->param, req)) {
872 return tevent_req_post(req, ev);
874 memset(state->param, '\0', 6);
875 SSVAL(state->param,0,SMB_SET_FILE_UNIX_BASIC);
877 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
878 strlen(fname)+1, NULL);
880 if (tevent_req_nomem(state->param, req)) {
881 return tevent_req_post(req, ev);
884 /* Setup data array. */
885 state->data = talloc_array(state, uint8_t, 100);
886 if (tevent_req_nomem(state->data, req)) {
887 return tevent_req_post(req, ev);
889 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
890 memset(&state->data[40], '\0', 60);
891 SIVAL(state->data,40,uid);
892 SIVAL(state->data,48,gid);
893 SIVAL(state->data,84,mode);
895 subreq = cli_trans_send(state, /* mem ctx. */
897 cli, /* cli_state. */
898 SMBtrans2, /* cmd. */
899 NULL, /* pipe name. */
903 &state->setup, /* setup. */
904 1, /* num setup uint16_t words. */
905 0, /* max returned setup. */
906 state->param, /* param. */
907 talloc_get_size(state->param), /* num param. */
908 2, /* max returned param. */
909 state->data, /* data. */
910 talloc_get_size(state->data), /* num data. */
911 0); /* max returned data. */
913 if (tevent_req_nomem(subreq, req)) {
914 return tevent_req_post(req, ev);
916 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done, req);
920 /****************************************************************************
921 chmod a file (UNIX extensions).
922 ****************************************************************************/
924 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
925 struct event_context *ev,
926 struct cli_state *cli,
930 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
932 unix_perms_to_wire(mode),
937 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
941 if (tevent_req_is_nterror(req, &status)) {
947 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
949 TALLOC_CTX *frame = talloc_stackframe();
950 struct event_context *ev = NULL;
951 struct tevent_req *req = NULL;
952 NTSTATUS status = NT_STATUS_OK;
954 if (cli_has_async_calls(cli)) {
956 * Can't use sync call while an async call is in flight
958 status = NT_STATUS_INVALID_PARAMETER;
962 ev = event_context_init(frame);
964 status = NT_STATUS_NO_MEMORY;
968 req = cli_posix_chmod_send(frame,
974 status = NT_STATUS_NO_MEMORY;
978 if (!tevent_req_poll(req, ev)) {
979 status = map_nt_error_from_unix(errno);
983 status = cli_posix_chmod_recv(req);
987 if (!NT_STATUS_IS_OK(status)) {
988 cli_set_error(cli, status);
993 /****************************************************************************
994 chown a file (UNIX extensions).
995 ****************************************************************************/
997 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
998 struct event_context *ev,
999 struct cli_state *cli,
1004 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1011 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1015 if (tevent_req_is_nterror(req, &status)) {
1018 return NT_STATUS_OK;
1021 NTSTATUS cli_posix_chown(struct cli_state *cli,
1026 TALLOC_CTX *frame = talloc_stackframe();
1027 struct event_context *ev = NULL;
1028 struct tevent_req *req = NULL;
1029 NTSTATUS status = NT_STATUS_OK;
1031 if (cli_has_async_calls(cli)) {
1033 * Can't use sync call while an async call is in flight
1035 status = NT_STATUS_INVALID_PARAMETER;
1039 ev = event_context_init(frame);
1041 status = NT_STATUS_NO_MEMORY;
1045 req = cli_posix_chown_send(frame,
1052 status = NT_STATUS_NO_MEMORY;
1056 if (!tevent_req_poll(req, ev)) {
1057 status = map_nt_error_from_unix(errno);
1061 status = cli_posix_chown_recv(req);
1065 if (!NT_STATUS_IS_OK(status)) {
1066 cli_set_error(cli, status);
1071 /****************************************************************************
1073 ****************************************************************************/
1075 static void cli_rename_done(struct tevent_req *subreq);
1077 struct cli_rename_state {
1081 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1082 struct event_context *ev,
1083 struct cli_state *cli,
1084 const char *fname_src,
1085 const char *fname_dst)
1087 struct tevent_req *req = NULL, *subreq = NULL;
1088 struct cli_rename_state *state = NULL;
1089 uint8_t additional_flags = 0;
1090 uint8_t *bytes = NULL;
1092 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1097 SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
1099 bytes = talloc_array(state, uint8_t, 1);
1100 if (tevent_req_nomem(bytes, req)) {
1101 return tevent_req_post(req, ev);
1104 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1105 strlen(fname_src)+1, NULL);
1106 if (tevent_req_nomem(bytes, req)) {
1107 return tevent_req_post(req, ev);
1110 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1111 talloc_get_size(bytes)+1);
1112 if (tevent_req_nomem(bytes, req)) {
1113 return tevent_req_post(req, ev);
1116 bytes[talloc_get_size(bytes)-1] = 4;
1117 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1118 strlen(fname_dst)+1, NULL);
1119 if (tevent_req_nomem(bytes, req)) {
1120 return tevent_req_post(req, ev);
1123 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1124 1, state->vwv, talloc_get_size(bytes), bytes);
1125 if (tevent_req_nomem(subreq, req)) {
1126 return tevent_req_post(req, ev);
1128 tevent_req_set_callback(subreq, cli_rename_done, req);
1132 static void cli_rename_done(struct tevent_req *subreq)
1134 struct tevent_req *req = tevent_req_callback_data(
1135 subreq, struct tevent_req);
1138 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1139 TALLOC_FREE(subreq);
1140 if (!NT_STATUS_IS_OK(status)) {
1141 tevent_req_nterror(req, status);
1144 tevent_req_done(req);
1147 NTSTATUS cli_rename_recv(struct tevent_req *req)
1149 return tevent_req_simple_recv_ntstatus(req);
1152 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1154 TALLOC_CTX *frame = talloc_stackframe();
1155 struct event_context *ev;
1156 struct tevent_req *req;
1157 NTSTATUS status = NT_STATUS_OK;
1159 if (cli_has_async_calls(cli)) {
1161 * Can't use sync call while an async call is in flight
1163 status = NT_STATUS_INVALID_PARAMETER;
1167 ev = event_context_init(frame);
1169 status = NT_STATUS_NO_MEMORY;
1173 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1175 status = NT_STATUS_NO_MEMORY;
1179 if (!tevent_req_poll(req, ev)) {
1180 status = map_nt_error_from_unix(errno);
1184 status = cli_rename_recv(req);
1188 if (!NT_STATUS_IS_OK(status)) {
1189 cli_set_error(cli, status);
1194 /****************************************************************************
1196 ****************************************************************************/
1198 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1200 struct cli_ntrename_internal_state {
1204 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1205 struct event_context *ev,
1206 struct cli_state *cli,
1207 const char *fname_src,
1208 const char *fname_dst,
1209 uint16_t rename_flag)
1211 struct tevent_req *req = NULL, *subreq = NULL;
1212 struct cli_ntrename_internal_state *state = NULL;
1213 uint8_t additional_flags = 0;
1214 uint8_t *bytes = NULL;
1216 req = tevent_req_create(mem_ctx, &state,
1217 struct cli_ntrename_internal_state);
1222 SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
1223 SSVAL(state->vwv+1, 0, rename_flag);
1225 bytes = talloc_array(state, uint8_t, 1);
1226 if (tevent_req_nomem(bytes, req)) {
1227 return tevent_req_post(req, ev);
1230 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1231 strlen(fname_src)+1, NULL);
1232 if (tevent_req_nomem(bytes, req)) {
1233 return tevent_req_post(req, ev);
1236 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1237 talloc_get_size(bytes)+1);
1238 if (tevent_req_nomem(bytes, req)) {
1239 return tevent_req_post(req, ev);
1242 bytes[talloc_get_size(bytes)-1] = 4;
1243 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1244 strlen(fname_dst)+1, NULL);
1245 if (tevent_req_nomem(bytes, req)) {
1246 return tevent_req_post(req, ev);
1249 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1250 4, state->vwv, talloc_get_size(bytes), bytes);
1251 if (tevent_req_nomem(subreq, req)) {
1252 return tevent_req_post(req, ev);
1254 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1258 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1260 struct tevent_req *req = tevent_req_callback_data(
1261 subreq, struct tevent_req);
1264 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1265 TALLOC_FREE(subreq);
1266 if (!NT_STATUS_IS_OK(status)) {
1267 tevent_req_nterror(req, status);
1270 tevent_req_done(req);
1273 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1275 return tevent_req_simple_recv_ntstatus(req);
1278 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1279 struct event_context *ev,
1280 struct cli_state *cli,
1281 const char *fname_src,
1282 const char *fname_dst)
1284 return cli_ntrename_internal_send(mem_ctx,
1289 RENAME_FLAG_RENAME);
1292 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1294 return cli_ntrename_internal_recv(req);
1297 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1299 TALLOC_CTX *frame = talloc_stackframe();
1300 struct event_context *ev;
1301 struct tevent_req *req;
1302 NTSTATUS status = NT_STATUS_OK;
1304 if (cli_has_async_calls(cli)) {
1306 * Can't use sync call while an async call is in flight
1308 status = NT_STATUS_INVALID_PARAMETER;
1312 ev = event_context_init(frame);
1314 status = NT_STATUS_NO_MEMORY;
1318 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1320 status = NT_STATUS_NO_MEMORY;
1324 if (!tevent_req_poll(req, ev)) {
1325 status = map_nt_error_from_unix(errno);
1329 status = cli_ntrename_recv(req);
1333 if (!NT_STATUS_IS_OK(status)) {
1334 cli_set_error(cli, status);
1339 /****************************************************************************
1341 ****************************************************************************/
1343 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1344 struct event_context *ev,
1345 struct cli_state *cli,
1346 const char *fname_src,
1347 const char *fname_dst)
1349 return cli_ntrename_internal_send(mem_ctx,
1354 RENAME_FLAG_HARD_LINK);
1357 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1359 return cli_ntrename_internal_recv(req);
1362 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1364 TALLOC_CTX *frame = talloc_stackframe();
1365 struct event_context *ev;
1366 struct tevent_req *req;
1367 NTSTATUS status = NT_STATUS_OK;
1369 if (cli_has_async_calls(cli)) {
1371 * Can't use sync call while an async call is in flight
1373 status = NT_STATUS_INVALID_PARAMETER;
1377 ev = event_context_init(frame);
1379 status = NT_STATUS_NO_MEMORY;
1383 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1385 status = NT_STATUS_NO_MEMORY;
1389 if (!tevent_req_poll(req, ev)) {
1390 status = map_nt_error_from_unix(errno);
1394 status = cli_nt_hardlink_recv(req);
1398 if (!NT_STATUS_IS_OK(status)) {
1399 cli_set_error(cli, status);
1404 /****************************************************************************
1406 ****************************************************************************/
1408 static void cli_unlink_done(struct tevent_req *subreq);
1410 struct cli_unlink_state {
1414 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1415 struct event_context *ev,
1416 struct cli_state *cli,
1418 uint16_t mayhave_attrs)
1420 struct tevent_req *req = NULL, *subreq = NULL;
1421 struct cli_unlink_state *state = NULL;
1422 uint8_t additional_flags = 0;
1423 uint8_t *bytes = NULL;
1425 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1430 SSVAL(state->vwv+0, 0, mayhave_attrs);
1432 bytes = talloc_array(state, uint8_t, 1);
1433 if (tevent_req_nomem(bytes, req)) {
1434 return tevent_req_post(req, ev);
1437 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1438 strlen(fname)+1, NULL);
1440 if (tevent_req_nomem(bytes, req)) {
1441 return tevent_req_post(req, ev);
1444 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1445 1, state->vwv, talloc_get_size(bytes), bytes);
1446 if (tevent_req_nomem(subreq, req)) {
1447 return tevent_req_post(req, ev);
1449 tevent_req_set_callback(subreq, cli_unlink_done, req);
1453 static void cli_unlink_done(struct tevent_req *subreq)
1455 struct tevent_req *req = tevent_req_callback_data(
1456 subreq, struct tevent_req);
1459 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1460 TALLOC_FREE(subreq);
1461 if (!NT_STATUS_IS_OK(status)) {
1462 tevent_req_nterror(req, status);
1465 tevent_req_done(req);
1468 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1470 return tevent_req_simple_recv_ntstatus(req);
1473 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1475 TALLOC_CTX *frame = talloc_stackframe();
1476 struct event_context *ev;
1477 struct tevent_req *req;
1478 NTSTATUS status = NT_STATUS_OK;
1480 if (cli_has_async_calls(cli)) {
1482 * Can't use sync call while an async call is in flight
1484 status = NT_STATUS_INVALID_PARAMETER;
1488 ev = event_context_init(frame);
1490 status = NT_STATUS_NO_MEMORY;
1494 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1496 status = NT_STATUS_NO_MEMORY;
1500 if (!tevent_req_poll(req, ev)) {
1501 status = map_nt_error_from_unix(errno);
1505 status = cli_unlink_recv(req);
1509 if (!NT_STATUS_IS_OK(status)) {
1510 cli_set_error(cli, status);
1515 /****************************************************************************
1517 ****************************************************************************/
1519 static void cli_mkdir_done(struct tevent_req *subreq);
1521 struct cli_mkdir_state {
1525 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1526 struct event_context *ev,
1527 struct cli_state *cli,
1530 struct tevent_req *req = NULL, *subreq = NULL;
1531 struct cli_mkdir_state *state = NULL;
1532 uint8_t additional_flags = 0;
1533 uint8_t *bytes = NULL;
1535 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1540 bytes = talloc_array(state, uint8_t, 1);
1541 if (tevent_req_nomem(bytes, req)) {
1542 return tevent_req_post(req, ev);
1545 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1546 strlen(dname)+1, NULL);
1548 if (tevent_req_nomem(bytes, req)) {
1549 return tevent_req_post(req, ev);
1552 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1553 0, NULL, talloc_get_size(bytes), bytes);
1554 if (tevent_req_nomem(subreq, req)) {
1555 return tevent_req_post(req, ev);
1557 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1561 static void cli_mkdir_done(struct tevent_req *subreq)
1563 struct tevent_req *req = tevent_req_callback_data(
1564 subreq, struct tevent_req);
1567 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1568 TALLOC_FREE(subreq);
1569 if (!NT_STATUS_IS_OK(status)) {
1570 tevent_req_nterror(req, status);
1573 tevent_req_done(req);
1576 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1578 return tevent_req_simple_recv_ntstatus(req);
1581 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1583 TALLOC_CTX *frame = talloc_stackframe();
1584 struct event_context *ev;
1585 struct tevent_req *req;
1586 NTSTATUS status = NT_STATUS_OK;
1588 if (cli_has_async_calls(cli)) {
1590 * Can't use sync call while an async call is in flight
1592 status = NT_STATUS_INVALID_PARAMETER;
1596 ev = event_context_init(frame);
1598 status = NT_STATUS_NO_MEMORY;
1602 req = cli_mkdir_send(frame, ev, cli, dname);
1604 status = NT_STATUS_NO_MEMORY;
1608 if (!tevent_req_poll(req, ev)) {
1609 status = map_nt_error_from_unix(errno);
1613 status = cli_mkdir_recv(req);
1617 if (!NT_STATUS_IS_OK(status)) {
1618 cli_set_error(cli, status);
1623 /****************************************************************************
1625 ****************************************************************************/
1627 static void cli_rmdir_done(struct tevent_req *subreq);
1629 struct cli_rmdir_state {
1633 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1634 struct event_context *ev,
1635 struct cli_state *cli,
1638 struct tevent_req *req = NULL, *subreq = NULL;
1639 struct cli_rmdir_state *state = NULL;
1640 uint8_t additional_flags = 0;
1641 uint8_t *bytes = NULL;
1643 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1648 bytes = talloc_array(state, uint8_t, 1);
1649 if (tevent_req_nomem(bytes, req)) {
1650 return tevent_req_post(req, ev);
1653 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1654 strlen(dname)+1, NULL);
1656 if (tevent_req_nomem(bytes, req)) {
1657 return tevent_req_post(req, ev);
1660 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1661 0, NULL, talloc_get_size(bytes), bytes);
1662 if (tevent_req_nomem(subreq, req)) {
1663 return tevent_req_post(req, ev);
1665 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1669 static void cli_rmdir_done(struct tevent_req *subreq)
1671 struct tevent_req *req = tevent_req_callback_data(
1672 subreq, struct tevent_req);
1675 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1676 TALLOC_FREE(subreq);
1677 if (!NT_STATUS_IS_OK(status)) {
1678 tevent_req_nterror(req, status);
1681 tevent_req_done(req);
1684 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1686 return tevent_req_simple_recv_ntstatus(req);
1689 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1691 TALLOC_CTX *frame = talloc_stackframe();
1692 struct event_context *ev;
1693 struct tevent_req *req;
1694 NTSTATUS status = NT_STATUS_OK;
1696 if (cli_has_async_calls(cli)) {
1698 * Can't use sync call while an async call is in flight
1700 status = NT_STATUS_INVALID_PARAMETER;
1704 ev = event_context_init(frame);
1706 status = NT_STATUS_NO_MEMORY;
1710 req = cli_rmdir_send(frame, ev, cli, dname);
1712 status = NT_STATUS_NO_MEMORY;
1716 if (!tevent_req_poll(req, ev)) {
1717 status = map_nt_error_from_unix(errno);
1721 status = cli_rmdir_recv(req);
1725 if (!NT_STATUS_IS_OK(status)) {
1726 cli_set_error(cli, status);
1731 /****************************************************************************
1732 Set or clear the delete on close flag.
1733 ****************************************************************************/
1741 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1743 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, 0, NULL,
1744 NULL, 0, NULL, NULL, 0, NULL);
1745 tevent_req_simple_finish_ntstatus(subreq, status);
1748 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1749 struct event_context *ev,
1750 struct cli_state *cli,
1754 struct tevent_req *req = NULL, *subreq = NULL;
1755 struct doc_state *state = NULL;
1757 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1762 /* Setup setup word. */
1763 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1765 /* Setup param array. */
1766 SSVAL(state->param,0,fnum);
1767 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1769 /* Setup data array. */
1770 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1772 subreq = cli_trans_send(state, /* mem ctx. */
1773 ev, /* event ctx. */
1774 cli, /* cli_state. */
1775 SMBtrans2, /* cmd. */
1776 NULL, /* pipe name. */
1780 &state->setup, /* setup. */
1781 1, /* num setup uint16_t words. */
1782 0, /* max returned setup. */
1783 state->param, /* param. */
1785 2, /* max returned param. */
1786 state->data, /* data. */
1788 0); /* max returned data. */
1790 if (tevent_req_nomem(subreq, req)) {
1791 return tevent_req_post(req, ev);
1793 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1797 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1801 if (tevent_req_is_nterror(req, &status)) {
1804 return NT_STATUS_OK;
1807 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1809 TALLOC_CTX *frame = talloc_stackframe();
1810 struct event_context *ev = NULL;
1811 struct tevent_req *req = NULL;
1812 NTSTATUS status = NT_STATUS_OK;
1814 if (cli_has_async_calls(cli)) {
1816 * Can't use sync call while an async call is in flight
1818 status = NT_STATUS_INVALID_PARAMETER;
1822 ev = event_context_init(frame);
1824 status = NT_STATUS_NO_MEMORY;
1828 req = cli_nt_delete_on_close_send(frame,
1834 status = NT_STATUS_NO_MEMORY;
1838 if (!tevent_req_poll(req, ev)) {
1839 status = map_nt_error_from_unix(errno);
1843 status = cli_nt_delete_on_close_recv(req);
1847 if (!NT_STATUS_IS_OK(status)) {
1848 cli_set_error(cli, status);
1853 struct cli_ntcreate_state {
1858 static void cli_ntcreate_done(struct tevent_req *subreq);
1860 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1861 struct event_context *ev,
1862 struct cli_state *cli,
1864 uint32_t CreatFlags,
1865 uint32_t DesiredAccess,
1866 uint32_t FileAttributes,
1867 uint32_t ShareAccess,
1868 uint32_t CreateDisposition,
1869 uint32_t CreateOptions,
1870 uint8_t SecurityFlags)
1872 struct tevent_req *req, *subreq;
1873 struct cli_ntcreate_state *state;
1876 size_t converted_len;
1878 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1885 SCVAL(vwv+0, 0, 0xFF);
1890 if (cli->use_oplocks) {
1891 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1893 SIVAL(vwv+3, 1, CreatFlags);
1894 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1895 SIVAL(vwv+7, 1, DesiredAccess);
1896 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1897 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1898 SIVAL(vwv+13, 1, FileAttributes);
1899 SIVAL(vwv+15, 1, ShareAccess);
1900 SIVAL(vwv+17, 1, CreateDisposition);
1901 SIVAL(vwv+19, 1, CreateOptions);
1902 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1903 SCVAL(vwv+23, 1, SecurityFlags);
1905 bytes = talloc_array(state, uint8_t, 0);
1906 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1907 fname, strlen(fname)+1,
1910 /* sigh. this copes with broken netapp filer behaviour */
1911 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1913 if (tevent_req_nomem(bytes, req)) {
1914 return tevent_req_post(req, ev);
1917 SSVAL(vwv+2, 1, converted_len);
1919 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1920 talloc_get_size(bytes), bytes);
1921 if (tevent_req_nomem(subreq, req)) {
1922 return tevent_req_post(req, ev);
1924 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1928 static void cli_ntcreate_done(struct tevent_req *subreq)
1930 struct tevent_req *req = tevent_req_callback_data(
1931 subreq, struct tevent_req);
1932 struct cli_ntcreate_state *state = tevent_req_data(
1933 req, struct cli_ntcreate_state);
1941 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1942 &num_bytes, &bytes);
1943 TALLOC_FREE(subreq);
1944 if (!NT_STATUS_IS_OK(status)) {
1945 tevent_req_nterror(req, status);
1948 state->fnum = SVAL(vwv+2, 1);
1949 tevent_req_done(req);
1952 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1954 struct cli_ntcreate_state *state = tevent_req_data(
1955 req, struct cli_ntcreate_state);
1958 if (tevent_req_is_nterror(req, &status)) {
1961 *pfnum = state->fnum;
1962 return NT_STATUS_OK;
1965 NTSTATUS cli_ntcreate(struct cli_state *cli,
1967 uint32_t CreatFlags,
1968 uint32_t DesiredAccess,
1969 uint32_t FileAttributes,
1970 uint32_t ShareAccess,
1971 uint32_t CreateDisposition,
1972 uint32_t CreateOptions,
1973 uint8_t SecurityFlags,
1976 TALLOC_CTX *frame = talloc_stackframe();
1977 struct event_context *ev;
1978 struct tevent_req *req;
1979 NTSTATUS status = NT_STATUS_OK;
1981 if (cli_has_async_calls(cli)) {
1983 * Can't use sync call while an async call is in flight
1985 status = NT_STATUS_INVALID_PARAMETER;
1989 ev = event_context_init(frame);
1991 status = NT_STATUS_NO_MEMORY;
1995 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1996 DesiredAccess, FileAttributes, ShareAccess,
1997 CreateDisposition, CreateOptions,
2000 status = NT_STATUS_NO_MEMORY;
2004 if (!tevent_req_poll(req, ev)) {
2005 status = map_nt_error_from_unix(errno);
2009 status = cli_ntcreate_recv(req, pfid);
2012 if (!NT_STATUS_IS_OK(status)) {
2013 cli_set_error(cli, status);
2018 /****************************************************************************
2020 WARNING: if you open with O_WRONLY then getattrE won't work!
2021 ****************************************************************************/
2023 struct cli_open_state {
2029 static void cli_open_done(struct tevent_req *subreq);
2031 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2032 struct event_context *ev,
2033 struct cli_state *cli, const char *fname,
2034 int flags, int share_mode,
2035 struct tevent_req **psmbreq)
2037 struct tevent_req *req, *subreq;
2038 struct cli_open_state *state;
2040 unsigned accessmode;
2041 uint8_t additional_flags;
2044 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2050 if (flags & O_CREAT) {
2053 if (!(flags & O_EXCL)) {
2054 if (flags & O_TRUNC)
2060 accessmode = (share_mode<<4);
2062 if ((flags & O_ACCMODE) == O_RDWR) {
2064 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2069 if ((flags & O_SYNC) == O_SYNC) {
2070 accessmode |= (1<<14);
2074 if (share_mode == DENY_FCB) {
2078 SCVAL(state->vwv + 0, 0, 0xFF);
2079 SCVAL(state->vwv + 0, 1, 0);
2080 SSVAL(state->vwv + 1, 0, 0);
2081 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2082 SSVAL(state->vwv + 3, 0, accessmode);
2083 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
2084 SSVAL(state->vwv + 5, 0, 0);
2085 SIVAL(state->vwv + 6, 0, 0);
2086 SSVAL(state->vwv + 8, 0, openfn);
2087 SIVAL(state->vwv + 9, 0, 0);
2088 SIVAL(state->vwv + 11, 0, 0);
2089 SIVAL(state->vwv + 13, 0, 0);
2091 additional_flags = 0;
2093 if (cli->use_oplocks) {
2094 /* if using oplocks then ask for a batch oplock via
2095 core and extended methods */
2097 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2098 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2101 bytes = talloc_array(state, uint8_t, 0);
2102 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2103 strlen(fname)+1, NULL);
2105 if (tevent_req_nomem(bytes, req)) {
2106 return tevent_req_post(req, ev);
2109 state->bytes.iov_base = (void *)bytes;
2110 state->bytes.iov_len = talloc_get_size(bytes);
2112 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2113 15, state->vwv, 1, &state->bytes);
2114 if (subreq == NULL) {
2118 tevent_req_set_callback(subreq, cli_open_done, req);
2123 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2124 struct cli_state *cli, const char *fname,
2125 int flags, int share_mode)
2127 struct tevent_req *req, *subreq;
2130 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2136 status = cli_smb_req_send(subreq);
2137 if (!NT_STATUS_IS_OK(status)) {
2138 tevent_req_nterror(req, status);
2139 return tevent_req_post(req, ev);
2144 static void cli_open_done(struct tevent_req *subreq)
2146 struct tevent_req *req = tevent_req_callback_data(
2147 subreq, struct tevent_req);
2148 struct cli_open_state *state = tevent_req_data(
2149 req, struct cli_open_state);
2155 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2157 TALLOC_FREE(subreq);
2158 if (!NT_STATUS_IS_OK(status)) {
2159 tevent_req_nterror(req, status);
2162 state->fnum = SVAL(vwv+2, 0);
2163 tevent_req_done(req);
2166 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2168 struct cli_open_state *state = tevent_req_data(
2169 req, struct cli_open_state);
2172 if (tevent_req_is_nterror(req, &status)) {
2175 *pfnum = state->fnum;
2176 return NT_STATUS_OK;
2179 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2180 int share_mode, uint16_t *pfnum)
2182 TALLOC_CTX *frame = talloc_stackframe();
2183 struct event_context *ev;
2184 struct tevent_req *req;
2185 NTSTATUS status = NT_STATUS_OK;
2187 if (cli_has_async_calls(cli)) {
2189 * Can't use sync call while an async call is in flight
2191 status = NT_STATUS_INVALID_PARAMETER;
2195 ev = event_context_init(frame);
2197 status = NT_STATUS_NO_MEMORY;
2201 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2203 status = NT_STATUS_NO_MEMORY;
2207 if (!tevent_req_poll(req, ev)) {
2208 status = map_nt_error_from_unix(errno);
2212 status = cli_open_recv(req, pfnum);
2215 if (!NT_STATUS_IS_OK(status)) {
2216 cli_set_error(cli, status);
2221 /****************************************************************************
2223 ****************************************************************************/
2225 struct cli_close_state {
2229 static void cli_close_done(struct tevent_req *subreq);
2231 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2232 struct event_context *ev,
2233 struct cli_state *cli,
2235 struct tevent_req **psubreq)
2237 struct tevent_req *req, *subreq;
2238 struct cli_close_state *state;
2240 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2245 SSVAL(state->vwv+0, 0, fnum);
2246 SIVALS(state->vwv+1, 0, -1);
2248 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2250 if (subreq == NULL) {
2254 tevent_req_set_callback(subreq, cli_close_done, req);
2259 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2260 struct event_context *ev,
2261 struct cli_state *cli,
2264 struct tevent_req *req, *subreq;
2267 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2272 status = cli_smb_req_send(subreq);
2273 if (!NT_STATUS_IS_OK(status)) {
2274 tevent_req_nterror(req, status);
2275 return tevent_req_post(req, ev);
2280 static void cli_close_done(struct tevent_req *subreq)
2282 struct tevent_req *req = tevent_req_callback_data(
2283 subreq, struct tevent_req);
2286 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2287 TALLOC_FREE(subreq);
2288 if (!NT_STATUS_IS_OK(status)) {
2289 tevent_req_nterror(req, status);
2292 tevent_req_done(req);
2295 NTSTATUS cli_close_recv(struct tevent_req *req)
2297 return tevent_req_simple_recv_ntstatus(req);
2300 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2302 TALLOC_CTX *frame = talloc_stackframe();
2303 struct event_context *ev;
2304 struct tevent_req *req;
2305 NTSTATUS status = NT_STATUS_OK;
2307 if (cli_has_async_calls(cli)) {
2309 * Can't use sync call while an async call is in flight
2311 status = NT_STATUS_INVALID_PARAMETER;
2315 ev = event_context_init(frame);
2317 status = NT_STATUS_NO_MEMORY;
2321 req = cli_close_send(frame, ev, cli, fnum);
2323 status = NT_STATUS_NO_MEMORY;
2327 if (!tevent_req_poll(req, ev)) {
2328 status = map_nt_error_from_unix(errno);
2332 status = cli_close_recv(req);
2335 if (!NT_STATUS_IS_OK(status)) {
2336 cli_set_error(cli, status);
2341 /****************************************************************************
2342 Truncate a file to a specified size
2343 ****************************************************************************/
2345 struct ftrunc_state {
2351 static void cli_ftruncate_done(struct tevent_req *subreq)
2353 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, 0, NULL,
2354 NULL, 0, NULL, NULL, 0, NULL);
2355 tevent_req_simple_finish_ntstatus(subreq, status);
2358 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2359 struct event_context *ev,
2360 struct cli_state *cli,
2364 struct tevent_req *req = NULL, *subreq = NULL;
2365 struct ftrunc_state *state = NULL;
2367 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2372 /* Setup setup word. */
2373 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2375 /* Setup param array. */
2376 SSVAL(state->param,0,fnum);
2377 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2378 SSVAL(state->param,4,0);
2380 /* Setup data array. */
2381 SBVAL(state->data, 0, size);
2383 subreq = cli_trans_send(state, /* mem ctx. */
2384 ev, /* event ctx. */
2385 cli, /* cli_state. */
2386 SMBtrans2, /* cmd. */
2387 NULL, /* pipe name. */
2391 &state->setup, /* setup. */
2392 1, /* num setup uint16_t words. */
2393 0, /* max returned setup. */
2394 state->param, /* param. */
2396 2, /* max returned param. */
2397 state->data, /* data. */
2399 0); /* max returned data. */
2401 if (tevent_req_nomem(subreq, req)) {
2402 return tevent_req_post(req, ev);
2404 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2408 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2412 if (tevent_req_is_nterror(req, &status)) {
2415 return NT_STATUS_OK;
2418 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2420 TALLOC_CTX *frame = talloc_stackframe();
2421 struct event_context *ev = NULL;
2422 struct tevent_req *req = NULL;
2423 NTSTATUS status = NT_STATUS_OK;
2425 if (cli_has_async_calls(cli)) {
2427 * Can't use sync call while an async call is in flight
2429 status = NT_STATUS_INVALID_PARAMETER;
2433 ev = event_context_init(frame);
2435 status = NT_STATUS_NO_MEMORY;
2439 req = cli_ftruncate_send(frame,
2445 status = NT_STATUS_NO_MEMORY;
2449 if (!tevent_req_poll(req, ev)) {
2450 status = map_nt_error_from_unix(errno);
2454 status = cli_ftruncate_recv(req);
2458 if (!NT_STATUS_IS_OK(status)) {
2459 cli_set_error(cli, status);
2464 /****************************************************************************
2465 send a lock with a specified locktype
2466 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2467 ****************************************************************************/
2469 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2470 uint32_t offset, uint32_t len,
2471 int timeout, unsigned char locktype)
2474 int saved_timeout = cli->timeout;
2476 memset(cli->outbuf,'\0',smb_size);
2477 memset(cli->inbuf,'\0', smb_size);
2479 cli_set_message(cli->outbuf,8,0,True);
2481 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2482 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2483 cli_setup_packet(cli);
2485 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2486 SSVAL(cli->outbuf,smb_vwv2,fnum);
2487 SCVAL(cli->outbuf,smb_vwv3,locktype);
2488 SIVALS(cli->outbuf, smb_vwv4, timeout);
2489 SSVAL(cli->outbuf,smb_vwv6,0);
2490 SSVAL(cli->outbuf,smb_vwv7,1);
2492 p = smb_buf(cli->outbuf);
2493 SSVAL(p, 0, cli->pid);
2494 SIVAL(p, 2, offset);
2499 cli_setup_bcc(cli, p);
2504 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
2507 if (!cli_receive_smb(cli)) {
2508 cli->timeout = saved_timeout;
2509 return NT_STATUS_UNSUCCESSFUL;
2512 cli->timeout = saved_timeout;
2514 return cli_nt_error(cli);
2517 /****************************************************************************
2519 note that timeout is in units of 2 milliseconds
2520 ****************************************************************************/
2522 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2523 uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
2526 int saved_timeout = cli->timeout;
2528 memset(cli->outbuf,'\0',smb_size);
2529 memset(cli->inbuf,'\0', smb_size);
2531 cli_set_message(cli->outbuf,8,0,True);
2533 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2534 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2535 cli_setup_packet(cli);
2537 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2538 SSVAL(cli->outbuf,smb_vwv2,fnum);
2539 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
2540 SIVALS(cli->outbuf, smb_vwv4, timeout);
2541 SSVAL(cli->outbuf,smb_vwv6,0);
2542 SSVAL(cli->outbuf,smb_vwv7,1);
2544 p = smb_buf(cli->outbuf);
2545 SSVAL(p, 0, cli->pid);
2546 SIVAL(p, 2, offset);
2551 cli_setup_bcc(cli, p);
2556 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
2559 if (!cli_receive_smb(cli)) {
2560 cli->timeout = saved_timeout;
2564 cli->timeout = saved_timeout;
2566 if (cli_is_error(cli)) {
2573 /****************************************************************************
2575 ****************************************************************************/
2577 struct cli_unlock_state {
2582 static void cli_unlock_done(struct tevent_req *subreq);
2584 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2585 struct event_context *ev,
2586 struct cli_state *cli,
2592 struct tevent_req *req = NULL, *subreq = NULL;
2593 struct cli_unlock_state *state = NULL;
2594 uint8_t additional_flags = 0;
2596 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2601 SCVAL(state->vwv+0, 0, 0xFF);
2602 SSVAL(state->vwv+2, 0, fnum);
2603 SCVAL(state->vwv+3, 0, 0);
2604 SIVALS(state->vwv+4, 0, 0);
2605 SSVAL(state->vwv+6, 0, 1);
2606 SSVAL(state->vwv+7, 0, 0);
2608 SSVAL(state->data, 0, cli->pid);
2609 SIVAL(state->data, 2, offset);
2610 SIVAL(state->data, 6, len);
2612 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2613 8, state->vwv, 10, state->data);
2614 if (tevent_req_nomem(subreq, req)) {
2615 return tevent_req_post(req, ev);
2617 tevent_req_set_callback(subreq, cli_unlock_done, req);
2621 static void cli_unlock_done(struct tevent_req *subreq)
2623 struct tevent_req *req = tevent_req_callback_data(
2624 subreq, struct tevent_req);
2627 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2628 TALLOC_FREE(subreq);
2629 if (!NT_STATUS_IS_OK(status)) {
2630 tevent_req_nterror(req, status);
2633 tevent_req_done(req);
2636 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2638 return tevent_req_simple_recv_ntstatus(req);
2641 NTSTATUS cli_unlock(struct cli_state *cli,
2646 TALLOC_CTX *frame = talloc_stackframe();
2647 struct event_context *ev;
2648 struct tevent_req *req;
2649 NTSTATUS status = NT_STATUS_OK;
2651 if (cli_has_async_calls(cli)) {
2653 * Can't use sync call while an async call is in flight
2655 status = NT_STATUS_INVALID_PARAMETER;
2659 ev = event_context_init(frame);
2661 status = NT_STATUS_NO_MEMORY;
2665 req = cli_unlock_send(frame, ev, cli,
2668 status = NT_STATUS_NO_MEMORY;
2672 if (!tevent_req_poll(req, ev)) {
2673 status = map_nt_error_from_unix(errno);
2677 status = cli_unlock_recv(req);
2681 if (!NT_STATUS_IS_OK(status)) {
2682 cli_set_error(cli, status);
2687 /****************************************************************************
2688 Lock a file with 64 bit offsets.
2689 ****************************************************************************/
2691 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2692 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
2695 int saved_timeout = cli->timeout;
2698 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2699 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2702 ltype = (lock_type == READ_LOCK? 1 : 0);
2703 ltype |= LOCKING_ANDX_LARGE_FILES;
2705 memset(cli->outbuf,'\0',smb_size);
2706 memset(cli->inbuf,'\0', smb_size);
2708 cli_set_message(cli->outbuf,8,0,True);
2710 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2711 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2712 cli_setup_packet(cli);
2714 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2715 SSVAL(cli->outbuf,smb_vwv2,fnum);
2716 SCVAL(cli->outbuf,smb_vwv3,ltype);
2717 SIVALS(cli->outbuf, smb_vwv4, timeout);
2718 SSVAL(cli->outbuf,smb_vwv6,0);
2719 SSVAL(cli->outbuf,smb_vwv7,1);
2721 p = smb_buf(cli->outbuf);
2722 SIVAL(p, 0, cli->pid);
2723 SOFF_T_R(p, 4, offset);
2724 SOFF_T_R(p, 12, len);
2727 cli_setup_bcc(cli, p);
2731 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
2734 if (!cli_receive_smb(cli)) {
2735 cli->timeout = saved_timeout;
2739 cli->timeout = saved_timeout;
2741 if (cli_is_error(cli)) {
2748 /****************************************************************************
2749 Unlock a file with 64 bit offsets.
2750 ****************************************************************************/
2752 struct cli_unlock64_state {
2757 static void cli_unlock64_done(struct tevent_req *subreq);
2759 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2760 struct event_context *ev,
2761 struct cli_state *cli,
2767 struct tevent_req *req = NULL, *subreq = NULL;
2768 struct cli_unlock64_state *state = NULL;
2769 uint8_t additional_flags = 0;
2771 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2776 SCVAL(state->vwv+0, 0, 0xff);
2777 SSVAL(state->vwv+2, 0, fnum);
2778 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2779 SIVALS(state->vwv+4, 0, 0);
2780 SSVAL(state->vwv+6, 0, 1);
2781 SSVAL(state->vwv+7, 0, 0);
2783 SIVAL(state->data, 0, cli->pid);
2784 SOFF_T_R(state->data, 4, offset);
2785 SOFF_T_R(state->data, 12, len);
2787 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2788 8, state->vwv, 20, state->data);
2789 if (tevent_req_nomem(subreq, req)) {
2790 return tevent_req_post(req, ev);
2792 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2796 static void cli_unlock64_done(struct tevent_req *subreq)
2798 struct tevent_req *req = tevent_req_callback_data(
2799 subreq, struct tevent_req);
2802 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2803 TALLOC_FREE(subreq);
2804 if (!NT_STATUS_IS_OK(status)) {
2805 tevent_req_nterror(req, status);
2808 tevent_req_done(req);
2811 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2813 return tevent_req_simple_recv_ntstatus(req);
2816 NTSTATUS cli_unlock64(struct cli_state *cli,
2821 TALLOC_CTX *frame = talloc_stackframe();
2822 struct event_context *ev;
2823 struct tevent_req *req;
2824 NTSTATUS status = NT_STATUS_OK;
2826 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2827 return cli_unlock(cli, fnum, offset, len);
2830 if (cli_has_async_calls(cli)) {
2832 * Can't use sync call while an async call is in flight
2834 status = NT_STATUS_INVALID_PARAMETER;
2838 ev = event_context_init(frame);
2840 status = NT_STATUS_NO_MEMORY;
2844 req = cli_unlock64_send(frame, ev, cli,
2847 status = NT_STATUS_NO_MEMORY;
2851 if (!tevent_req_poll(req, ev)) {
2852 status = map_nt_error_from_unix(errno);
2856 status = cli_unlock64_recv(req);
2860 if (!NT_STATUS_IS_OK(status)) {
2861 cli_set_error(cli, status);
2866 /****************************************************************************
2867 Get/unlock a POSIX lock on a file - internal function.
2868 ****************************************************************************/
2870 struct posix_lock_state {
2873 uint8_t data[POSIX_LOCK_DATA_SIZE];
2876 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2878 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, 0, NULL,
2879 NULL, 0, NULL, NULL, 0, NULL);
2880 tevent_req_simple_finish_ntstatus(subreq, status);
2883 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2884 struct event_context *ev,
2885 struct cli_state *cli,
2890 enum brl_type lock_type)
2892 struct tevent_req *req = NULL, *subreq = NULL;
2893 struct posix_lock_state *state = NULL;
2895 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2900 /* Setup setup word. */
2901 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2903 /* Setup param array. */
2904 SSVAL(&state->param, 0, fnum);
2905 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2907 /* Setup data array. */
2908 switch (lock_type) {
2910 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2911 POSIX_LOCK_TYPE_READ);
2914 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2915 POSIX_LOCK_TYPE_WRITE);
2918 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2919 POSIX_LOCK_TYPE_UNLOCK);
2926 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2927 POSIX_LOCK_FLAG_WAIT);
2929 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2930 POSIX_LOCK_FLAG_NOWAIT);
2933 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
2934 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2935 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2937 subreq = cli_trans_send(state, /* mem ctx. */
2938 ev, /* event ctx. */
2939 cli, /* cli_state. */
2940 SMBtrans2, /* cmd. */
2941 NULL, /* pipe name. */
2945 &state->setup, /* setup. */
2946 1, /* num setup uint16_t words. */
2947 0, /* max returned setup. */
2948 state->param, /* param. */
2950 2, /* max returned param. */
2951 state->data, /* data. */
2952 POSIX_LOCK_DATA_SIZE, /* num data. */
2953 0); /* max returned data. */
2955 if (tevent_req_nomem(subreq, req)) {
2956 return tevent_req_post(req, ev);
2958 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
2962 /****************************************************************************
2964 ****************************************************************************/
2966 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
2967 struct event_context *ev,
2968 struct cli_state *cli,
2973 enum brl_type lock_type)
2975 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2976 wait_lock, lock_type);
2979 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
2983 if (tevent_req_is_nterror(req, &status)) {
2986 return NT_STATUS_OK;
2989 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2990 uint64_t offset, uint64_t len,
2991 bool wait_lock, enum brl_type lock_type)
2993 TALLOC_CTX *frame = talloc_stackframe();
2994 struct event_context *ev = NULL;
2995 struct tevent_req *req = NULL;
2996 NTSTATUS status = NT_STATUS_OK;
2998 if (cli_has_async_calls(cli)) {
3000 * Can't use sync call while an async call is in flight
3002 status = NT_STATUS_INVALID_PARAMETER;
3006 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3007 status = NT_STATUS_INVALID_PARAMETER;
3011 ev = event_context_init(frame);
3013 status = NT_STATUS_NO_MEMORY;
3017 req = cli_posix_lock_send(frame,
3026 status = NT_STATUS_NO_MEMORY;
3030 if (!tevent_req_poll(req, ev)) {
3031 status = map_nt_error_from_unix(errno);
3035 status = cli_posix_lock_recv(req);
3039 if (!NT_STATUS_IS_OK(status)) {
3040 cli_set_error(cli, status);
3045 /****************************************************************************
3046 POSIX Unlock a file.
3047 ****************************************************************************/
3049 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3050 struct event_context *ev,
3051 struct cli_state *cli,
3056 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3057 false, UNLOCK_LOCK);
3060 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3064 if (tevent_req_is_nterror(req, &status)) {
3067 return NT_STATUS_OK;
3070 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3072 TALLOC_CTX *frame = talloc_stackframe();
3073 struct event_context *ev = NULL;
3074 struct tevent_req *req = NULL;
3075 NTSTATUS status = NT_STATUS_OK;
3077 if (cli_has_async_calls(cli)) {
3079 * Can't use sync call while an async call is in flight
3081 status = NT_STATUS_INVALID_PARAMETER;
3085 ev = event_context_init(frame);
3087 status = NT_STATUS_NO_MEMORY;
3091 req = cli_posix_unlock_send(frame,
3098 status = NT_STATUS_NO_MEMORY;
3102 if (!tevent_req_poll(req, ev)) {
3103 status = map_nt_error_from_unix(errno);
3107 status = cli_posix_unlock_recv(req);
3111 if (!NT_STATUS_IS_OK(status)) {
3112 cli_set_error(cli, status);
3117 /****************************************************************************
3118 Do a SMBgetattrE call.
3119 ****************************************************************************/
3121 static void cli_getattrE_done(struct tevent_req *subreq);
3123 struct cli_getattrE_state {
3133 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3134 struct event_context *ev,
3135 struct cli_state *cli,
3138 struct tevent_req *req = NULL, *subreq = NULL;
3139 struct cli_getattrE_state *state = NULL;
3140 uint8_t additional_flags = 0;
3142 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3147 state->zone_offset = cli->serverzone;
3148 SSVAL(state->vwv+0,0,fnum);
3150 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3151 1, state->vwv, 0, NULL);
3152 if (tevent_req_nomem(subreq, req)) {
3153 return tevent_req_post(req, ev);
3155 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3159 static void cli_getattrE_done(struct tevent_req *subreq)
3161 struct tevent_req *req = tevent_req_callback_data(
3162 subreq, struct tevent_req);
3163 struct cli_getattrE_state *state = tevent_req_data(
3164 req, struct cli_getattrE_state);
3166 uint16_t *vwv = NULL;
3170 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3172 TALLOC_FREE(subreq);
3173 if (!NT_STATUS_IS_OK(status)) {
3174 tevent_req_nterror(req, status);
3178 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3179 state->attr = SVAL(vwv+10,0);
3180 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3181 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3182 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3184 tevent_req_done(req);
3187 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3190 time_t *change_time,
3191 time_t *access_time,
3194 struct cli_getattrE_state *state = tevent_req_data(
3195 req, struct cli_getattrE_state);
3198 if (tevent_req_is_nterror(req, &status)) {
3202 *attr = state->attr;
3205 *size = state->size;
3208 *change_time = state->change_time;
3211 *access_time = state->access_time;
3214 *write_time = state->write_time;
3216 return NT_STATUS_OK;
3219 NTSTATUS cli_getattrE(struct cli_state *cli,
3223 time_t *change_time,
3224 time_t *access_time,
3227 TALLOC_CTX *frame = talloc_stackframe();
3228 struct event_context *ev = NULL;
3229 struct tevent_req *req = NULL;
3230 NTSTATUS status = NT_STATUS_OK;
3232 if (cli_has_async_calls(cli)) {
3234 * Can't use sync call while an async call is in flight
3236 status = NT_STATUS_INVALID_PARAMETER;
3240 ev = event_context_init(frame);
3242 status = NT_STATUS_NO_MEMORY;
3246 req = cli_getattrE_send(frame, ev, cli, fnum);
3248 status = NT_STATUS_NO_MEMORY;
3252 if (!tevent_req_poll(req, ev)) {
3253 status = map_nt_error_from_unix(errno);
3257 status = cli_getattrE_recv(req,
3266 if (!NT_STATUS_IS_OK(status)) {
3267 cli_set_error(cli, status);
3272 /****************************************************************************
3274 ****************************************************************************/
3276 static void cli_getatr_done(struct tevent_req *subreq);
3278 struct cli_getatr_state {
3285 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3286 struct event_context *ev,
3287 struct cli_state *cli,
3290 struct tevent_req *req = NULL, *subreq = NULL;
3291 struct cli_getatr_state *state = NULL;
3292 uint8_t additional_flags = 0;
3293 uint8_t *bytes = NULL;
3295 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3300 state->zone_offset = cli->serverzone;
3302 bytes = talloc_array(state, uint8_t, 1);
3303 if (tevent_req_nomem(bytes, req)) {
3304 return tevent_req_post(req, ev);
3307 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3308 strlen(fname)+1, NULL);
3310 if (tevent_req_nomem(bytes, req)) {
3311 return tevent_req_post(req, ev);
3314 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3315 0, NULL, talloc_get_size(bytes), bytes);
3316 if (tevent_req_nomem(subreq, req)) {
3317 return tevent_req_post(req, ev);
3319 tevent_req_set_callback(subreq, cli_getatr_done, req);
3323 static void cli_getatr_done(struct tevent_req *subreq)
3325 struct tevent_req *req = tevent_req_callback_data(
3326 subreq, struct tevent_req);
3327 struct cli_getatr_state *state = tevent_req_data(
3328 req, struct cli_getatr_state);
3330 uint16_t *vwv = NULL;
3334 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3336 TALLOC_FREE(subreq);
3337 if (!NT_STATUS_IS_OK(status)) {
3338 tevent_req_nterror(req, status);
3342 state->attr = SVAL(vwv+0,0);
3343 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3344 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3346 tevent_req_done(req);
3349 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3354 struct cli_getatr_state *state = tevent_req_data(
3355 req, struct cli_getatr_state);
3358 if (tevent_req_is_nterror(req, &status)) {
3362 *attr = state->attr;
3365 *size = state->size;
3368 *write_time = state->write_time;
3370 return NT_STATUS_OK;
3373 NTSTATUS cli_getatr(struct cli_state *cli,
3379 TALLOC_CTX *frame = talloc_stackframe();
3380 struct event_context *ev = NULL;
3381 struct tevent_req *req = NULL;
3382 NTSTATUS status = NT_STATUS_OK;
3384 if (cli_has_async_calls(cli)) {
3386 * Can't use sync call while an async call is in flight
3388 status = NT_STATUS_INVALID_PARAMETER;
3392 ev = event_context_init(frame);
3394 status = NT_STATUS_NO_MEMORY;
3398 req = cli_getatr_send(frame, ev, cli, fname);
3400 status = NT_STATUS_NO_MEMORY;
3404 if (!tevent_req_poll(req, ev)) {
3405 status = map_nt_error_from_unix(errno);
3409 status = cli_getatr_recv(req,
3416 if (!NT_STATUS_IS_OK(status)) {
3417 cli_set_error(cli, status);
3422 /****************************************************************************
3423 Do a SMBsetattrE call.
3424 ****************************************************************************/
3426 static void cli_setattrE_done(struct tevent_req *subreq);
3428 struct cli_setattrE_state {
3432 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3433 struct event_context *ev,
3434 struct cli_state *cli,
3440 struct tevent_req *req = NULL, *subreq = NULL;
3441 struct cli_setattrE_state *state = NULL;
3442 uint8_t additional_flags = 0;
3444 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3449 SSVAL(state->vwv+0, 0, fnum);
3450 cli_put_dos_date2(cli, (char *)&state->vwv[1], 0, change_time);
3451 cli_put_dos_date2(cli, (char *)&state->vwv[3], 0, access_time);
3452 cli_put_dos_date2(cli, (char *)&state->vwv[5], 0, write_time);
3454 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3455 7, state->vwv, 0, NULL);
3456 if (tevent_req_nomem(subreq, req)) {
3457 return tevent_req_post(req, ev);
3459 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3463 static void cli_setattrE_done(struct tevent_req *subreq)
3465 struct tevent_req *req = tevent_req_callback_data(
3466 subreq, struct tevent_req);
3469 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3470 TALLOC_FREE(subreq);
3471 if (!NT_STATUS_IS_OK(status)) {
3472 tevent_req_nterror(req, status);
3475 tevent_req_done(req);
3478 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3480 return tevent_req_simple_recv_ntstatus(req);
3483 NTSTATUS cli_setattrE(struct cli_state *cli,
3489 TALLOC_CTX *frame = talloc_stackframe();
3490 struct event_context *ev = NULL;
3491 struct tevent_req *req = NULL;
3492 NTSTATUS status = NT_STATUS_OK;
3494 if (cli_has_async_calls(cli)) {
3496 * Can't use sync call while an async call is in flight
3498 status = NT_STATUS_INVALID_PARAMETER;
3502 ev = event_context_init(frame);
3504 status = NT_STATUS_NO_MEMORY;
3508 req = cli_setattrE_send(frame, ev,
3516 status = NT_STATUS_NO_MEMORY;
3520 if (!tevent_req_poll(req, ev)) {
3521 status = map_nt_error_from_unix(errno);
3525 status = cli_setattrE_recv(req);
3529 if (!NT_STATUS_IS_OK(status)) {
3530 cli_set_error(cli, status);
3535 /****************************************************************************
3536 Do a SMBsetatr call.
3537 ****************************************************************************/
3539 static void cli_setatr_done(struct tevent_req *subreq);
3541 struct cli_setatr_state {
3545 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3546 struct event_context *ev,
3547 struct cli_state *cli,
3552 struct tevent_req *req = NULL, *subreq = NULL;
3553 struct cli_setatr_state *state = NULL;
3554 uint8_t additional_flags = 0;
3555 uint8_t *bytes = NULL;
3557 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3562 SSVAL(state->vwv+0, 0, attr);
3563 cli_put_dos_date3(cli, (char *)&state->vwv[1], 0, mtime);
3565 bytes = talloc_array(state, uint8_t, 1);
3566 if (tevent_req_nomem(bytes, req)) {
3567 return tevent_req_post(req, ev);
3570 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3571 strlen(fname)+1, NULL);
3572 if (tevent_req_nomem(bytes, req)) {
3573 return tevent_req_post(req, ev);
3575 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3576 talloc_get_size(bytes)+1);
3577 if (tevent_req_nomem(bytes, req)) {
3578 return tevent_req_post(req, ev);
3581 bytes[talloc_get_size(bytes)-1] = 4;
3582 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3584 if (tevent_req_nomem(bytes, req)) {
3585 return tevent_req_post(req, ev);
3588 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3589 8, state->vwv, talloc_get_size(bytes), bytes);
3590 if (tevent_req_nomem(subreq, req)) {
3591 return tevent_req_post(req, ev);
3593 tevent_req_set_callback(subreq, cli_setatr_done, req);
3597 static void cli_setatr_done(struct tevent_req *subreq)
3599 struct tevent_req *req = tevent_req_callback_data(
3600 subreq, struct tevent_req);
3603 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3604 TALLOC_FREE(subreq);
3605 if (!NT_STATUS_IS_OK(status)) {
3606 tevent_req_nterror(req, status);
3609 tevent_req_done(req);
3612 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3614 return tevent_req_simple_recv_ntstatus(req);
3617 NTSTATUS cli_setatr(struct cli_state *cli,
3622 TALLOC_CTX *frame = talloc_stackframe();
3623 struct event_context *ev = NULL;
3624 struct tevent_req *req = NULL;
3625 NTSTATUS status = NT_STATUS_OK;
3627 if (cli_has_async_calls(cli)) {
3629 * Can't use sync call while an async call is in flight
3631 status = NT_STATUS_INVALID_PARAMETER;
3635 ev = event_context_init(frame);
3637 status = NT_STATUS_NO_MEMORY;
3641 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3643 status = NT_STATUS_NO_MEMORY;
3647 if (!tevent_req_poll(req, ev)) {
3648 status = map_nt_error_from_unix(errno);
3652 status = cli_setatr_recv(req);
3656 if (!NT_STATUS_IS_OK(status)) {
3657 cli_set_error(cli, status);
3662 /****************************************************************************
3663 Check for existance of a dir.
3664 ****************************************************************************/
3666 static void cli_chkpath_done(struct tevent_req *subreq);
3668 struct cli_chkpath_state {
3672 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3673 struct event_context *ev,
3674 struct cli_state *cli,
3677 struct tevent_req *req = NULL, *subreq = NULL;
3678 struct cli_chkpath_state *state = NULL;
3679 uint8_t additional_flags = 0;
3680 uint8_t *bytes = NULL;
3682 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3687 bytes = talloc_array(state, uint8_t, 1);
3688 if (tevent_req_nomem(bytes, req)) {
3689 return tevent_req_post(req, ev);
3692 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3693 strlen(fname)+1, NULL);
3695 if (tevent_req_nomem(bytes, req)) {
3696 return tevent_req_post(req, ev);
3699 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3700 0, NULL, talloc_get_size(bytes), bytes);
3701 if (tevent_req_nomem(subreq, req)) {
3702 return tevent_req_post(req, ev);
3704 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3708 static void cli_chkpath_done(struct tevent_req *subreq)
3710 struct tevent_req *req = tevent_req_callback_data(
3711 subreq, struct tevent_req);
3714 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3715 TALLOC_FREE(subreq);
3716 if (!NT_STATUS_IS_OK(status)) {
3717 tevent_req_nterror(req, status);
3720 tevent_req_done(req);
3723 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3725 return tevent_req_simple_recv_ntstatus(req);
3728 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3730 TALLOC_CTX *frame = talloc_stackframe();
3731 struct event_context *ev = NULL;
3732 struct tevent_req *req = NULL;
3734 NTSTATUS status = NT_STATUS_OK;
3736 if (cli_has_async_calls(cli)) {
3738 * Can't use sync call while an async call is in flight
3740 status = NT_STATUS_INVALID_PARAMETER;
3744 path2 = talloc_strdup(frame, path);
3746 status = NT_STATUS_NO_MEMORY;
3749 trim_char(path2,'\0','\\');
3751 path2 = talloc_strdup(frame, "\\");
3753 status = NT_STATUS_NO_MEMORY;
3758 ev = event_context_init(frame);
3760 status = NT_STATUS_NO_MEMORY;
3764 req = cli_chkpath_send(frame, ev, cli, path2);
3766 status = NT_STATUS_NO_MEMORY;
3770 if (!tevent_req_poll(req, ev)) {
3771 status = map_nt_error_from_unix(errno);
3775 status = cli_chkpath_recv(req);
3779 if (!NT_STATUS_IS_OK(status)) {
3780 cli_set_error(cli, status);
3785 /****************************************************************************
3787 ****************************************************************************/
3789 static void cli_dskattr_done(struct tevent_req *subreq);
3791 struct cli_dskattr_state {
3797 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3798 struct event_context *ev,
3799 struct cli_state *cli)
3801 struct tevent_req *req = NULL, *subreq = NULL;
3802 struct cli_dskattr_state *state = NULL;
3803 uint8_t additional_flags = 0;
3805 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3810 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3812 if (tevent_req_nomem(subreq, req)) {
3813 return tevent_req_post(req, ev);
3815 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3819 static void cli_dskattr_done(struct tevent_req *subreq)
3821 struct tevent_req *req = tevent_req_callback_data(
3822 subreq, struct tevent_req);
3823 struct cli_dskattr_state *state = tevent_req_data(
3824 req, struct cli_dskattr_state);
3826 uint16_t *vwv = NULL;
3830 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3832 TALLOC_FREE(subreq);
3833 if (!NT_STATUS_IS_OK(status)) {
3834 tevent_req_nterror(req, status);
3837 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3838 state->total = SVAL(vwv+0, 0);
3839 state->avail = SVAL(vwv+3, 0);
3840 tevent_req_done(req);
3843 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3845 struct cli_dskattr_state *state = tevent_req_data(
3846 req, struct cli_dskattr_state);
3849 if (tevent_req_is_nterror(req, &status)) {
3852 *bsize = state->bsize;
3853 *total = state->total;
3854 *avail = state->avail;
3855 return NT_STATUS_OK;
3858 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3860 TALLOC_CTX *frame = talloc_stackframe();
3861 struct event_context *ev = NULL;
3862 struct tevent_req *req = NULL;
3863 NTSTATUS status = NT_STATUS_OK;
3865 if (cli_has_async_calls(cli)) {
3867 * Can't use sync call while an async call is in flight
3869 status = NT_STATUS_INVALID_PARAMETER;
3873 ev = event_context_init(frame);
3875 status = NT_STATUS_NO_MEMORY;
3879 req = cli_dskattr_send(frame, ev, cli);
3881 status = NT_STATUS_NO_MEMORY;
3885 if (!tevent_req_poll(req, ev)) {
3886 status = map_nt_error_from_unix(errno);
3890 status = cli_dskattr_recv(req, bsize, total, avail);
3894 if (!NT_STATUS_IS_OK(status)) {
3895 cli_set_error(cli, status);
3900 /****************************************************************************
3901 Create and open a temporary file.
3902 ****************************************************************************/
3904 static void cli_ctemp_done(struct tevent_req *subreq);
3906 struct ctemp_state {
3912 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3913 struct event_context *ev,
3914 struct cli_state *cli,
3917 struct tevent_req *req = NULL, *subreq = NULL;
3918 struct ctemp_state *state = NULL;
3919 uint8_t additional_flags = 0;
3920 uint8_t *bytes = NULL;
3922 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3927 SSVAL(state->vwv,0,0);
3928 SIVALS(state->vwv+1,0,-1);
3930 bytes = talloc_array(state, uint8_t, 1);
3931 if (tevent_req_nomem(bytes, req)) {
3932 return tevent_req_post(req, ev);
3935 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3936 strlen(path)+1, NULL);
3937 if (tevent_req_nomem(bytes, req)) {
3938 return tevent_req_post(req, ev);
3941 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3942 3, state->vwv, talloc_get_size(bytes), bytes);
3943 if (tevent_req_nomem(subreq, req)) {
3944 return tevent_req_post(req, ev);
3946 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3950 static void cli_ctemp_done(struct tevent_req *subreq)
3952 struct tevent_req *req = tevent_req_callback_data(
3953 subreq, struct tevent_req);
3954 struct ctemp_state *state = tevent_req_data(
3955 req, struct ctemp_state);
3959 uint32_t num_bytes = 0;
3960 uint8_t *bytes = NULL;
3963 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3964 &num_bytes, &bytes);
3965 TALLOC_FREE(subreq);
3966 if (!NT_STATUS_IS_OK(status)) {
3967 tevent_req_nterror(req, status);
3971 state->fnum = SVAL(vwv+0, 0);
3973 /* From W2K3, the result is just the ASCII name */
3974 if (num_bytes < 2) {
3975 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
3979 if (pull_string_talloc(state,
3986 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
3989 tevent_req_done(req);
3992 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
3997 struct ctemp_state *state = tevent_req_data(req,
3998 struct ctemp_state);
4001 if (tevent_req_is_nterror(req, &status)) {
4004 *pfnum = state->fnum;
4005 *outfile = talloc_strdup(ctx, state->ret_path);
4007 return NT_STATUS_NO_MEMORY;
4009 return NT_STATUS_OK;
4012 NTSTATUS cli_ctemp(struct cli_state *cli,
4018 TALLOC_CTX *frame = talloc_stackframe();
4019 struct event_context *ev;
4020 struct tevent_req *req;
4021 NTSTATUS status = NT_STATUS_OK;
4023 if (cli_has_async_calls(cli)) {
4025 * Can't use sync call while an async call is in flight
4027 status = NT_STATUS_INVALID_PARAMETER;
4031 ev = event_context_init(frame);
4033 status = NT_STATUS_NO_MEMORY;
4037 req = cli_ctemp_send(frame, ev, cli, path);
4039 status = NT_STATUS_NO_MEMORY;
4043 if (!tevent_req_poll(req, ev)) {
4044 status = map_nt_error_from_unix(errno);
4048 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4052 if (!NT_STATUS_IS_OK(status)) {
4053 cli_set_error(cli, status);
4059 send a raw ioctl - used by the torture code
4061 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4065 struct tevent_req *result_parent;
4067 SSVAL(vwv+0, 0, fnum);
4068 SSVAL(vwv+1, 0, code>>16);
4069 SSVAL(vwv+2, 0, (code&0xFFFF));
4071 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4072 &result_parent, 0, NULL, NULL, NULL, NULL);
4073 if (!NT_STATUS_IS_OK(status)) {
4076 *blob = data_blob_null;
4077 return NT_STATUS_OK;
4080 /*********************************************************
4081 Set an extended attribute utility fn.
4082 *********************************************************/
4084 static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsigned int param_len,
4085 const char *ea_name, const char *ea_val, size_t ea_len)
4087 unsigned int data_len = 0;
4089 char *rparam=NULL, *rdata=NULL;
4091 size_t ea_namelen = strlen(ea_name);
4093 if (ea_namelen == 0 && ea_len == 0) {
4095 data = (char *)SMB_MALLOC(data_len);
4100 SIVAL(p,0,data_len);
4102 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4103 data = (char *)SMB_MALLOC(data_len);
4108 SIVAL(p,0,data_len);
4110 SCVAL(p, 0, 0); /* EA flags. */
4111 SCVAL(p, 1, ea_namelen);
4112 SSVAL(p, 2, ea_len);
4113 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4114 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4117 if (!cli_send_trans(cli, SMBtrans2,
4119 -1, 0, /* fid, flags */
4120 &setup, 1, 0, /* setup, length, max */
4121 param, param_len, 2, /* param, length, max */
4122 data, data_len, cli->max_xmit /* data, length, max */
4128 if (!cli_receive_trans(cli, SMBtrans2,
4129 &rparam, ¶m_len,
4130 &rdata, &data_len)) {
4142 /*********************************************************
4143 Set an extended attribute on a pathname.
4144 *********************************************************/
4146 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
4148 uint16_t setup = TRANSACT2_SETPATHINFO;
4149 unsigned int param_len = 0;
4151 size_t srclen = 2*(strlen(path)+1);
4155 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
4159 memset(param, '\0', 6);
4160 SSVAL(param,0,SMB_INFO_SET_EA);
4163 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4164 param_len = PTR_DIFF(p, param);
4166 ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
4171 /*********************************************************
4172 Set an extended attribute on an fnum.
4173 *********************************************************/
4175 bool cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum, const char *ea_name, const char *ea_val, size_t ea_len)
4178 uint16_t setup = TRANSACT2_SETFILEINFO;
4180 memset(param, 0, 6);
4181 SSVAL(param,0,fnum);
4182 SSVAL(param,2,SMB_INFO_SET_EA);
4184 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
4187 /*********************************************************
4188 Get an extended attribute list utility fn.
4189 *********************************************************/
4191 static bool cli_get_ea_list(struct cli_state *cli,
4192 uint16_t setup, char *param, unsigned int param_len,
4195 struct ea_struct **pea_list)
4197 unsigned int data_len = 0;
4198 unsigned int rparam_len, rdata_len;
4199 char *rparam=NULL, *rdata=NULL;
4204 struct ea_struct *ea_list;
4211 if (!cli_send_trans(cli, SMBtrans2,
4213 -1, 0, /* fid, flags */
4214 &setup, 1, 0, /* setup, length, max */
4215 param, param_len, 10, /* param, length, max */
4216 NULL, data_len, cli->max_xmit /* data, length, max */
4221 if (!cli_receive_trans(cli, SMBtrans2,
4222 &rparam, &rparam_len,
4223 &rdata, &rdata_len)) {
4227 if (!rdata || rdata_len < 4) {
4231 ea_size = (size_t)IVAL(rdata,0);
4232 if (ea_size > rdata_len) {
4237 /* No EA's present. */
4245 /* Validate the EA list and count it. */
4246 for (num_eas = 0; ea_size >= 4; num_eas++) {
4247 unsigned int ea_namelen = CVAL(p,1);
4248 unsigned int ea_valuelen = SVAL(p,2);
4249 if (ea_namelen == 0) {
4252 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4255 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4256 p += 4 + ea_namelen + 1 + ea_valuelen;
4264 *pnum_eas = num_eas;
4266 /* Caller only wants number of EA's. */
4271 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
4276 ea_size = (size_t)IVAL(rdata,0);
4279 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4280 struct ea_struct *ea = &ea_list[num_eas];
4281 fstring unix_ea_name;
4282 unsigned int ea_namelen = CVAL(p,1);
4283 unsigned int ea_valuelen = SVAL(p,2);
4285 ea->flags = CVAL(p,0);
4286 unix_ea_name[0] = '\0';
4287 pull_ascii_fstring(unix_ea_name, p + 4);
4288 ea->name = talloc_strdup(ctx, unix_ea_name);
4289 /* Ensure the value is null terminated (in case it's a string). */
4290 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
4291 if (!ea->value.data) {
4295 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4297 ea->value.data[ea_valuelen] = 0;
4299 p += 4 + ea_namelen + 1 + ea_valuelen;
4302 *pea_list = ea_list;
4312 /*********************************************************
4313 Get an extended attribute list from a pathname.
4314 *********************************************************/
4316 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
4319 struct ea_struct **pea_list)
4321 uint16_t setup = TRANSACT2_QPATHINFO;
4322 unsigned int param_len = 0;
4325 size_t srclen = 2*(strlen(path)+1);
4328 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
4334 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
4336 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4337 param_len = PTR_DIFF(p, param);
4339 ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
4344 /*********************************************************
4345 Get an extended attribute list from an fnum.
4346 *********************************************************/
4348 bool cli_get_ea_list_fnum(struct cli_state *cli, uint16_t fnum,
4351 struct ea_struct **pea_list)
4353 uint16_t setup = TRANSACT2_QFILEINFO;
4356 memset(param, 0, 6);
4357 SSVAL(param,0,fnum);
4358 SSVAL(param,2,SMB_INFO_SET_EA);
4360 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
4363 /****************************************************************************
4364 Convert open "flags" arg to uint32_t on wire.
4365 ****************************************************************************/
4367 static uint32_t open_flags_to_wire(int flags)
4369 int open_mode = flags & O_ACCMODE;
4372 switch (open_mode) {
4374 ret |= SMB_O_WRONLY;
4381 ret |= SMB_O_RDONLY;
4385 if (flags & O_CREAT) {
4388 if (flags & O_EXCL) {
4391 if (flags & O_TRUNC) {
4395 if (flags & O_SYNC) {
4399 if (flags & O_APPEND) {
4400 ret |= SMB_O_APPEND;
4402 #if defined(O_DIRECT)
4403 if (flags & O_DIRECT) {
4404 ret |= SMB_O_DIRECT;
4407 #if defined(O_DIRECTORY)
4408 if (flags & O_DIRECTORY) {
4409 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4410 ret |= SMB_O_DIRECTORY;
4416 /****************************************************************************
4417 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4418 ****************************************************************************/
4420 struct posix_open_state {
4424 uint16_t fnum; /* Out */
4427 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4429 struct tevent_req *req = tevent_req_callback_data(
4430 subreq, struct tevent_req);
4431 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4436 status = cli_trans_recv(subreq, state, NULL, 0, NULL, NULL, 0, NULL,
4437 &data, 12, &num_data);
4438 TALLOC_FREE(subreq);
4439 if (!NT_STATUS_IS_OK(status)) {
4440 tevent_req_nterror(req, status);
4443 state->fnum = SVAL(data,2);
4444 tevent_req_done(req);
4447 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4448 struct event_context *ev,
4449 struct cli_state *cli,
4455 struct tevent_req *req = NULL, *subreq = NULL;
4456 struct posix_open_state *state = NULL;
4457 uint32_t wire_flags = open_flags_to_wire(flags);
4459 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4464 /* Setup setup word. */
4465 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4467 /* Setup param array. */
4468 state->param = talloc_array(state, uint8_t, 6);
4469 if (tevent_req_nomem(state->param, req)) {
4470 return tevent_req_post(req, ev);
4472 memset(state->param, '\0', 6);
4473 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4475 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4476 strlen(fname)+1, NULL);
4478 if (tevent_req_nomem(state->param, req)) {
4479 return tevent_req_post(req, ev);
4482 /* Setup data words. */
4484 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4485 wire_flags |= SMB_O_DIRECTORY;
4488 SIVAL(state->data,0,0); /* No oplock. */
4489 SIVAL(state->data,4,wire_flags);
4490 SIVAL(state->data,8,unix_perms_to_wire(mode));
4491 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4492 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4494 subreq = cli_trans_send(state, /* mem ctx. */
4495 ev, /* event ctx. */
4496 cli, /* cli_state. */
4497 SMBtrans2, /* cmd. */
4498 NULL, /* pipe name. */
4502 &state->setup, /* setup. */
4503 1, /* num setup uint16_t words. */
4504 0, /* max returned setup. */
4505 state->param, /* param. */
4506 talloc_get_size(state->param),/* num param. */
4507 2, /* max returned param. */
4508 state->data, /* data. */
4510 12); /* max returned data. */
4512 if (tevent_req_nomem(subreq, req)) {
4513 return tevent_req_post(req, ev);
4515 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4519 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4520 struct event_context *ev,
4521 struct cli_state *cli,
4526 return cli_posix_open_internal_send(mem_ctx, ev,
4527 cli, fname, flags, mode, false);
4530 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4532 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4535 if (tevent_req_is_nterror(req, &status)) {
4538 *pfnum = state->fnum;
4539 return NT_STATUS_OK;
4542 /****************************************************************************
4543 Open - POSIX semantics. Doesn't request oplock.
4544 ****************************************************************************/
4546 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4547 int flags, mode_t mode, uint16_t *pfnum)
4550 TALLOC_CTX *frame = talloc_stackframe();
4551 struct event_context *ev = NULL;
4552 struct tevent_req *req = NULL;
4553 NTSTATUS status = NT_STATUS_OK;
4555 if (cli_has_async_calls(cli)) {
4557 * Can't use sync call while an async call is in flight
4559 status = NT_STATUS_INVALID_PARAMETER;
4563 ev = event_context_init(frame);
4565 status = NT_STATUS_NO_MEMORY;
4569 req = cli_posix_open_send(frame,
4576 status = NT_STATUS_NO_MEMORY;
4580 if (!tevent_req_poll(req, ev)) {
4581 status = map_nt_error_from_unix(errno);
4585 status = cli_posix_open_recv(req, pfnum);
4589 if (!NT_STATUS_IS_OK(status)) {
4590 cli_set_error(cli, status);
4595 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4596 struct event_context *ev,
4597 struct cli_state *cli,
4601 return cli_posix_open_internal_send(mem_ctx, ev,
4602 cli, fname, O_CREAT, mode, true);
4605 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4609 if (tevent_req_is_nterror(req, &status)) {
4612 return NT_STATUS_OK;
4615 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4617 TALLOC_CTX *frame = talloc_stackframe();
4618 struct event_context *ev = NULL;
4619 struct tevent_req *req = NULL;
4620 NTSTATUS status = NT_STATUS_OK;
4622 if (cli_has_async_calls(cli)) {
4624 * Can't use sync call while an async call is in flight
4626 status = NT_STATUS_INVALID_PARAMETER;
4630 ev = event_context_init(frame);
4632 status = NT_STATUS_NO_MEMORY;
4636 req = cli_posix_mkdir_send(frame,
4642 status = NT_STATUS_NO_MEMORY;
4646 if (!tevent_req_poll(req, ev)) {
4647 status = map_nt_error_from_unix(errno);
4651 status = cli_posix_mkdir_recv(req);
4655 if (!NT_STATUS_IS_OK(status)) {
4656 cli_set_error(cli, status);
4661 /****************************************************************************
4662 unlink or rmdir - POSIX semantics.
4663 ****************************************************************************/
4665 struct unlink_state {
4670 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4672 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, 0, NULL,
4673 NULL, 0, NULL, NULL, 0, NULL);
4674 tevent_req_simple_finish_ntstatus(subreq, status);
4677 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4678 struct event_context *ev,
4679 struct cli_state *cli,
4683 struct tevent_req *req = NULL, *subreq = NULL;
4684 struct unlink_state *state = NULL;
4685 uint8_t *param = NULL;
4687 req = tevent_req_create(mem_ctx, &state, struct unlink_state);
4692 /* Setup setup word. */
4693 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4695 /* Setup param array. */
4696 param = talloc_array(state, uint8_t, 6);
4697 if (tevent_req_nomem(param, req)) {
4698 return tevent_req_post(req, ev);
4700 memset(param, '\0', 6);
4701 SSVAL(param, 0, SMB_POSIX_PATH_UNLINK);
4703 param = trans2_bytes_push_str(param, cli_ucs2(cli), fname,
4704 strlen(fname)+1, NULL);
4706 if (tevent_req_nomem(param, req)) {
4707 return tevent_req_post(req, ev);
4710 /* Setup data word. */
4711 SSVAL(state->data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
4712 SMB_POSIX_UNLINK_FILE_TARGET);
4714 subreq = cli_trans_send(state, /* mem ctx. */
4715 ev, /* event ctx. */
4716 cli, /* cli_state. */
4717 SMBtrans2, /* cmd. */
4718 NULL, /* pipe name. */
4722 &state->setup, /* setup. */
4723 1, /* num setup uint16_t words. */
4724 0, /* max returned setup. */
4726 talloc_get_size(param), /* num param. */
4727 2, /* max returned param. */
4728 state->data, /* data. */
4730 0); /* max returned data. */
4732 if (tevent_req_nomem(subreq, req)) {
4733 return tevent_req_post(req, ev);
4735 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4739 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4740 struct event_context *ev,
4741 struct cli_state *cli,
4744 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, false);
4747 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4751 if (tevent_req_is_nterror(req, &status)) {
4754 return NT_STATUS_OK;
4757 /****************************************************************************
4758 unlink - POSIX semantics.
4759 ****************************************************************************/
4761 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4763 TALLOC_CTX *frame = talloc_stackframe();
4764 struct event_context *ev = NULL;
4765 struct tevent_req *req = NULL;
4766 NTSTATUS status = NT_STATUS_OK;
4768 if (cli_has_async_calls(cli)) {
4770 * Can't use sync call while an async call is in flight
4772 status = NT_STATUS_INVALID_PARAMETER;
4776 ev = event_context_init(frame);
4778 status = NT_STATUS_NO_MEMORY;
4782 req = cli_posix_unlink_send(frame,
4787 status = NT_STATUS_NO_MEMORY;
4791 if (!tevent_req_poll(req, ev)) {
4792 status = map_nt_error_from_unix(errno);
4796 status = cli_posix_unlink_recv(req);
4800 if (!NT_STATUS_IS_OK(status)) {
4801 cli_set_error(cli, status);
4806 /****************************************************************************
4807 rmdir - POSIX semantics.
4808 ****************************************************************************/
4810 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4811 struct event_context *ev,
4812 struct cli_state *cli,
4815 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, true);
4818 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4822 if (tevent_req_is_nterror(req, &status)) {
4825 return NT_STATUS_OK;
4828 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4830 TALLOC_CTX *frame = talloc_stackframe();
4831 struct event_context *ev = NULL;
4832 struct tevent_req *req = NULL;
4833 NTSTATUS status = NT_STATUS_OK;
4835 if (cli_has_async_calls(cli)) {
4837 * Can't use sync call while an async call is in flight
4839 status = NT_STATUS_INVALID_PARAMETER;
4843 ev = event_context_init(frame);
4845 status = NT_STATUS_NO_MEMORY;
4849 req = cli_posix_rmdir_send(frame,
4854 status = NT_STATUS_NO_MEMORY;
4858 if (!tevent_req_poll(req, ev)) {
4859 status = map_nt_error_from_unix(errno);
4863 status = cli_posix_rmdir_recv(req, frame);
4867 if (!NT_STATUS_IS_OK(status)) {
4868 cli_set_error(cli, status);
4873 /****************************************************************************
4875 ****************************************************************************/
4877 struct cli_notify_state {
4879 uint32_t num_changes;
4880 struct notify_change *changes;
4883 static void cli_notify_done(struct tevent_req *subreq);
4885 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4886 struct tevent_context *ev,
4887 struct cli_state *cli, uint16_t fnum,
4888 uint32_t buffer_size,
4889 uint32_t completion_filter, bool recursive)
4891 struct tevent_req *req, *subreq;
4892 struct cli_notify_state *state;
4894 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4899 SIVAL(state->setup, 0, completion_filter);
4900 SSVAL(state->setup, 4, fnum);
4901 SSVAL(state->setup, 6, recursive);
4903 subreq = cli_trans_send(
4904 state, /* mem ctx. */
4905 ev, /* event ctx. */
4906 cli, /* cli_state. */
4907 SMBnttrans, /* cmd. */
4908 NULL, /* pipe name. */
4910 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4912 (uint16_t *)state->setup, /* setup. */
4913 4, /* num setup uint16_t words. */
4914 0, /* max returned setup. */
4917 buffer_size, /* max returned param. */
4920 0); /* max returned data. */
4922 if (tevent_req_nomem(subreq, req)) {
4923 return tevent_req_post(req, ev);
4925 tevent_req_set_callback(subreq, cli_notify_done, req);
4929 static void cli_notify_done(struct tevent_req *subreq)
4931 struct tevent_req *req = tevent_req_callback_data(
4932 subreq, struct tevent_req);
4933 struct cli_notify_state *state = tevent_req_data(
4934 req, struct cli_notify_state);
4937 uint32_t i, ofs, num_params;
4939 status = cli_trans_recv(subreq, talloc_tos(), NULL, 0, NULL,
4940 ¶ms, 0, &num_params, NULL, 0, NULL);
4941 TALLOC_FREE(subreq);
4942 if (!NT_STATUS_IS_OK(status)) {
4943 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4944 tevent_req_nterror(req, status);
4948 state->num_changes = 0;
4951 while (num_params - ofs > 12) {
4952 uint32_t len = IVAL(params, ofs);
4953 state->num_changes += 1;
4955 if ((len == 0) || (ofs+len >= num_params)) {
4961 state->changes = talloc_array(state, struct notify_change,
4962 state->num_changes);
4963 if (tevent_req_nomem(state->changes, req)) {
4964 TALLOC_FREE(params);
4970 for (i=0; i<state->num_changes; i++) {
4971 uint32_t next = IVAL(params, ofs);
4972 uint32_t len = IVAL(params, ofs+8);
4976 if ((next != 0) && (len+12 != next)) {
4977 TALLOC_FREE(params);
4979 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4983 state->changes[i].action = IVAL(params, ofs+4);
4984 ret = clistr_pull_talloc(params, (char *)params, &name,
4986 STR_TERMINATE|STR_UNICODE);
4988 TALLOC_FREE(params);
4989 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4992 state->changes[i].name = name;
4996 TALLOC_FREE(params);
4997 tevent_req_done(req);
5000 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5001 uint32_t *pnum_changes,
5002 struct notify_change **pchanges)
5004 struct cli_notify_state *state = tevent_req_data(
5005 req, struct cli_notify_state);
5008 if (tevent_req_is_nterror(req, &status)) {
5012 *pnum_changes = state->num_changes;
5013 *pchanges = talloc_move(mem_ctx, &state->changes);
5014 return NT_STATUS_OK;
5017 struct cli_qpathinfo_state {
5026 static void cli_qpathinfo_done(struct tevent_req *subreq);
5028 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5029 struct tevent_context *ev,
5030 struct cli_state *cli, const char *fname,
5031 uint16_t level, uint32_t min_rdata,
5034 struct tevent_req *req, *subreq;
5035 struct cli_qpathinfo_state *state;
5037 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5041 state->min_rdata = min_rdata;
5042 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5044 state->param = talloc_zero_array(state, uint8_t, 6);
5045 if (tevent_req_nomem(state->param, req)) {
5046 return tevent_req_post(req, ev);
5048 SSVAL(state->param, 0, level);
5049 state->param = trans2_bytes_push_str(
5050 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
5051 if (tevent_req_nomem(state->param, req)) {
5052 return tevent_req_post(req, ev);
5055 subreq = cli_trans_send(
5056 state, /* mem ctx. */
5057 ev, /* event ctx. */
5058 cli, /* cli_state. */
5059 SMBtrans2, /* cmd. */
5060 NULL, /* pipe name. */
5064 state->setup, /* setup. */
5065 1, /* num setup uint16_t words. */
5066 0, /* max returned setup. */
5067 state->param, /* param. */
5068 talloc_get_size(state->param), /* num param. */
5069 2, /* max returned param. */
5072 max_rdata); /* max returned data. */
5074 if (tevent_req_nomem(subreq, req)) {
5075 return tevent_req_post(req, ev);
5077 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5081 static void cli_qpathinfo_done(struct tevent_req *subreq)
5083 struct tevent_req *req = tevent_req_callback_data(
5084 subreq, struct tevent_req);
5085 struct cli_qpathinfo_state *state = tevent_req_data(
5086 req, struct cli_qpathinfo_state);
5089 status = cli_trans_recv(subreq, state, NULL, 0, NULL, NULL, 0, NULL,
5090 &state->rdata, state->min_rdata,
5092 if (!NT_STATUS_IS_OK(status)) {
5093 tevent_req_nterror(req, status);
5096 tevent_req_done(req);
5099 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5100 uint8_t **rdata, uint32_t *num_rdata)
5102 struct cli_qpathinfo_state *state = tevent_req_data(
5103 req, struct cli_qpathinfo_state);
5106 if (tevent_req_is_nterror(req, &status)) {
5109 *rdata = talloc_move(mem_ctx, &state->rdata);
5110 *num_rdata = state->num_rdata;
5111 return NT_STATUS_OK;
5114 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5115 const char *fname, uint16_t level, uint32_t min_rdata,
5117 uint8_t **rdata, uint32_t *num_rdata)
5119 TALLOC_CTX *frame = talloc_stackframe();
5120 struct event_context *ev;
5121 struct tevent_req *req;
5122 NTSTATUS status = NT_STATUS_NO_MEMORY;
5124 if (cli_has_async_calls(cli)) {
5126 * Can't use sync call while an async call is in flight
5128 status = NT_STATUS_INVALID_PARAMETER;
5131 ev = event_context_init(frame);
5135 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5140 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5143 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5146 if (!NT_STATUS_IS_OK(status)) {
5147 cli_set_error(cli, status);