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 *req = tevent_req_callback_data(
710 subreq, struct tevent_req);
711 struct stat_state *state = tevent_req_data(req, struct stat_state);
714 status = cli_trans_recv(subreq, state, NULL, 0, NULL, NULL, 0, NULL,
715 &state->data, 100, &state->num_data);
717 if (!NT_STATUS_IS_OK(status)) {
718 tevent_req_nterror(req, status);
721 tevent_req_done(req);
724 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
725 struct event_context *ev,
726 struct cli_state *cli,
729 struct tevent_req *req = NULL, *subreq = NULL;
730 struct stat_state *state = NULL;
732 req = tevent_req_create(mem_ctx, &state, struct stat_state);
737 /* Setup setup word. */
738 SSVAL(&state->setup, 0, TRANSACT2_QPATHINFO);
740 /* Setup param array. */
741 state->param = talloc_array(state, uint8_t, 6);
742 if (tevent_req_nomem(state->param, req)) {
743 return tevent_req_post(req, ev);
745 memset(state->param, '\0', 6);
746 SSVAL(state->param, 0, SMB_QUERY_FILE_UNIX_BASIC);
748 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
749 strlen(fname)+1, NULL);
751 if (tevent_req_nomem(state->param, req)) {
752 return tevent_req_post(req, ev);
755 subreq = cli_trans_send(state, /* mem ctx. */
757 cli, /* cli_state. */
758 SMBtrans2, /* cmd. */
759 NULL, /* pipe name. */
763 &state->setup, /* setup. */
764 1, /* num setup uint16_t words. */
765 0, /* max returned setup. */
766 state->param, /* param. */
767 talloc_get_size(state->param), /* num param. */
768 2, /* max returned param. */
771 100); /* max returned data. */
773 if (tevent_req_nomem(subreq, req)) {
774 return tevent_req_post(req, ev);
776 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
780 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
781 SMB_STRUCT_STAT *sbuf)
783 struct stat_state *state = tevent_req_data(req, struct stat_state);
786 if (tevent_req_is_nterror(req, &status)) {
790 if (state->num_data != 100) {
791 return NT_STATUS_DATA_ERROR;
794 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
795 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
796 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
797 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
799 /* assume 512 byte blocks */
800 sbuf->st_ex_blocks /= 512;
802 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
803 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
804 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
806 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
807 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
808 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
809 #if defined(HAVE_MAKEDEV)
811 uint32_t dev_major = IVAL(state->data,60);
812 uint32_t dev_minor = IVAL(state->data,68);
813 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
816 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
817 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
818 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
823 NTSTATUS cli_posix_stat(struct cli_state *cli,
825 SMB_STRUCT_STAT *sbuf)
827 TALLOC_CTX *frame = talloc_stackframe();
828 struct event_context *ev = NULL;
829 struct tevent_req *req = NULL;
830 NTSTATUS status = NT_STATUS_OK;
832 if (cli_has_async_calls(cli)) {
834 * Can't use sync call while an async call is in flight
836 status = NT_STATUS_INVALID_PARAMETER;
840 ev = event_context_init(frame);
842 status = NT_STATUS_NO_MEMORY;
846 req = cli_posix_stat_send(frame,
851 status = NT_STATUS_NO_MEMORY;
855 if (!tevent_req_poll(req, ev)) {
856 status = map_nt_error_from_unix(errno);
860 status = cli_posix_stat_recv(req, sbuf);
864 if (!NT_STATUS_IS_OK(status)) {
865 cli_set_error(cli, status);
870 /****************************************************************************
871 Chmod or chown a file internal (UNIX extensions).
872 ****************************************************************************/
880 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
882 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, 0, NULL,
883 NULL, 0, NULL, NULL, 0, NULL);
884 tevent_req_simple_finish_ntstatus(subreq, status);
887 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
888 struct event_context *ev,
889 struct cli_state *cli,
895 struct tevent_req *req = NULL, *subreq = NULL;
896 struct ch_state *state = NULL;
898 req = tevent_req_create(mem_ctx, &state, struct ch_state);
903 /* Setup setup word. */
904 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
906 /* Setup param array. */
907 state->param = talloc_array(state, uint8_t, 6);
908 if (tevent_req_nomem(state->param, req)) {
909 return tevent_req_post(req, ev);
911 memset(state->param, '\0', 6);
912 SSVAL(state->param,0,SMB_SET_FILE_UNIX_BASIC);
914 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
915 strlen(fname)+1, NULL);
917 if (tevent_req_nomem(state->param, req)) {
918 return tevent_req_post(req, ev);
921 /* Setup data array. */
922 state->data = talloc_array(state, uint8_t, 100);
923 if (tevent_req_nomem(state->data, req)) {
924 return tevent_req_post(req, ev);
926 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
927 memset(&state->data[40], '\0', 60);
928 SIVAL(state->data,40,uid);
929 SIVAL(state->data,48,gid);
930 SIVAL(state->data,84,mode);
932 subreq = cli_trans_send(state, /* mem ctx. */
934 cli, /* cli_state. */
935 SMBtrans2, /* cmd. */
936 NULL, /* pipe name. */
940 &state->setup, /* setup. */
941 1, /* num setup uint16_t words. */
942 0, /* max returned setup. */
943 state->param, /* param. */
944 talloc_get_size(state->param), /* num param. */
945 2, /* max returned param. */
946 state->data, /* data. */
947 talloc_get_size(state->data), /* num data. */
948 0); /* max returned data. */
950 if (tevent_req_nomem(subreq, req)) {
951 return tevent_req_post(req, ev);
953 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done, req);
957 /****************************************************************************
958 chmod a file (UNIX extensions).
959 ****************************************************************************/
961 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
962 struct event_context *ev,
963 struct cli_state *cli,
967 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
969 unix_perms_to_wire(mode),
974 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
978 if (tevent_req_is_nterror(req, &status)) {
984 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
986 TALLOC_CTX *frame = talloc_stackframe();
987 struct event_context *ev = NULL;
988 struct tevent_req *req = NULL;
989 NTSTATUS status = NT_STATUS_OK;
991 if (cli_has_async_calls(cli)) {
993 * Can't use sync call while an async call is in flight
995 status = NT_STATUS_INVALID_PARAMETER;
999 ev = event_context_init(frame);
1001 status = NT_STATUS_NO_MEMORY;
1005 req = cli_posix_chmod_send(frame,
1011 status = NT_STATUS_NO_MEMORY;
1015 if (!tevent_req_poll(req, ev)) {
1016 status = map_nt_error_from_unix(errno);
1020 status = cli_posix_chmod_recv(req);
1024 if (!NT_STATUS_IS_OK(status)) {
1025 cli_set_error(cli, status);
1030 /****************************************************************************
1031 chown a file (UNIX extensions).
1032 ****************************************************************************/
1034 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1035 struct event_context *ev,
1036 struct cli_state *cli,
1041 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1048 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1052 if (tevent_req_is_nterror(req, &status)) {
1055 return NT_STATUS_OK;
1058 NTSTATUS cli_posix_chown(struct cli_state *cli,
1063 TALLOC_CTX *frame = talloc_stackframe();
1064 struct event_context *ev = NULL;
1065 struct tevent_req *req = NULL;
1066 NTSTATUS status = NT_STATUS_OK;
1068 if (cli_has_async_calls(cli)) {
1070 * Can't use sync call while an async call is in flight
1072 status = NT_STATUS_INVALID_PARAMETER;
1076 ev = event_context_init(frame);
1078 status = NT_STATUS_NO_MEMORY;
1082 req = cli_posix_chown_send(frame,
1089 status = NT_STATUS_NO_MEMORY;
1093 if (!tevent_req_poll(req, ev)) {
1094 status = map_nt_error_from_unix(errno);
1098 status = cli_posix_chown_recv(req);
1102 if (!NT_STATUS_IS_OK(status)) {
1103 cli_set_error(cli, status);
1108 /****************************************************************************
1110 ****************************************************************************/
1112 static void cli_rename_done(struct tevent_req *subreq);
1114 struct cli_rename_state {
1118 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1119 struct event_context *ev,
1120 struct cli_state *cli,
1121 const char *fname_src,
1122 const char *fname_dst)
1124 struct tevent_req *req = NULL, *subreq = NULL;
1125 struct cli_rename_state *state = NULL;
1126 uint8_t additional_flags = 0;
1127 uint8_t *bytes = NULL;
1129 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1134 SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
1136 bytes = talloc_array(state, uint8_t, 1);
1137 if (tevent_req_nomem(bytes, req)) {
1138 return tevent_req_post(req, ev);
1141 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1142 strlen(fname_src)+1, NULL);
1143 if (tevent_req_nomem(bytes, req)) {
1144 return tevent_req_post(req, ev);
1147 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1148 talloc_get_size(bytes)+1);
1149 if (tevent_req_nomem(bytes, req)) {
1150 return tevent_req_post(req, ev);
1153 bytes[talloc_get_size(bytes)-1] = 4;
1154 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1155 strlen(fname_dst)+1, NULL);
1156 if (tevent_req_nomem(bytes, req)) {
1157 return tevent_req_post(req, ev);
1160 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1161 1, state->vwv, talloc_get_size(bytes), bytes);
1162 if (tevent_req_nomem(subreq, req)) {
1163 return tevent_req_post(req, ev);
1165 tevent_req_set_callback(subreq, cli_rename_done, req);
1169 static void cli_rename_done(struct tevent_req *subreq)
1171 struct tevent_req *req = tevent_req_callback_data(
1172 subreq, struct tevent_req);
1175 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1176 TALLOC_FREE(subreq);
1177 if (!NT_STATUS_IS_OK(status)) {
1178 tevent_req_nterror(req, status);
1181 tevent_req_done(req);
1184 NTSTATUS cli_rename_recv(struct tevent_req *req)
1186 return tevent_req_simple_recv_ntstatus(req);
1189 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1191 TALLOC_CTX *frame = talloc_stackframe();
1192 struct event_context *ev;
1193 struct tevent_req *req;
1194 NTSTATUS status = NT_STATUS_OK;
1196 if (cli_has_async_calls(cli)) {
1198 * Can't use sync call while an async call is in flight
1200 status = NT_STATUS_INVALID_PARAMETER;
1204 ev = event_context_init(frame);
1206 status = NT_STATUS_NO_MEMORY;
1210 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1212 status = NT_STATUS_NO_MEMORY;
1216 if (!tevent_req_poll(req, ev)) {
1217 status = map_nt_error_from_unix(errno);
1221 status = cli_rename_recv(req);
1225 if (!NT_STATUS_IS_OK(status)) {
1226 cli_set_error(cli, status);
1231 /****************************************************************************
1233 ****************************************************************************/
1235 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1237 struct cli_ntrename_internal_state {
1241 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1242 struct event_context *ev,
1243 struct cli_state *cli,
1244 const char *fname_src,
1245 const char *fname_dst,
1246 uint16_t rename_flag)
1248 struct tevent_req *req = NULL, *subreq = NULL;
1249 struct cli_ntrename_internal_state *state = NULL;
1250 uint8_t additional_flags = 0;
1251 uint8_t *bytes = NULL;
1253 req = tevent_req_create(mem_ctx, &state,
1254 struct cli_ntrename_internal_state);
1259 SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
1260 SSVAL(state->vwv+1, 0, rename_flag);
1262 bytes = talloc_array(state, uint8_t, 1);
1263 if (tevent_req_nomem(bytes, req)) {
1264 return tevent_req_post(req, ev);
1267 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1268 strlen(fname_src)+1, NULL);
1269 if (tevent_req_nomem(bytes, req)) {
1270 return tevent_req_post(req, ev);
1273 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1274 talloc_get_size(bytes)+1);
1275 if (tevent_req_nomem(bytes, req)) {
1276 return tevent_req_post(req, ev);
1279 bytes[talloc_get_size(bytes)-1] = 4;
1280 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1281 strlen(fname_dst)+1, NULL);
1282 if (tevent_req_nomem(bytes, req)) {
1283 return tevent_req_post(req, ev);
1286 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1287 4, state->vwv, talloc_get_size(bytes), bytes);
1288 if (tevent_req_nomem(subreq, req)) {
1289 return tevent_req_post(req, ev);
1291 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1295 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1297 struct tevent_req *req = tevent_req_callback_data(
1298 subreq, struct tevent_req);
1301 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1302 TALLOC_FREE(subreq);
1303 if (!NT_STATUS_IS_OK(status)) {
1304 tevent_req_nterror(req, status);
1307 tevent_req_done(req);
1310 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1312 return tevent_req_simple_recv_ntstatus(req);
1315 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1316 struct event_context *ev,
1317 struct cli_state *cli,
1318 const char *fname_src,
1319 const char *fname_dst)
1321 return cli_ntrename_internal_send(mem_ctx,
1326 RENAME_FLAG_RENAME);
1329 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1331 return cli_ntrename_internal_recv(req);
1334 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1336 TALLOC_CTX *frame = talloc_stackframe();
1337 struct event_context *ev;
1338 struct tevent_req *req;
1339 NTSTATUS status = NT_STATUS_OK;
1341 if (cli_has_async_calls(cli)) {
1343 * Can't use sync call while an async call is in flight
1345 status = NT_STATUS_INVALID_PARAMETER;
1349 ev = event_context_init(frame);
1351 status = NT_STATUS_NO_MEMORY;
1355 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1357 status = NT_STATUS_NO_MEMORY;
1361 if (!tevent_req_poll(req, ev)) {
1362 status = map_nt_error_from_unix(errno);
1366 status = cli_ntrename_recv(req);
1370 if (!NT_STATUS_IS_OK(status)) {
1371 cli_set_error(cli, status);
1376 /****************************************************************************
1378 ****************************************************************************/
1380 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1381 struct event_context *ev,
1382 struct cli_state *cli,
1383 const char *fname_src,
1384 const char *fname_dst)
1386 return cli_ntrename_internal_send(mem_ctx,
1391 RENAME_FLAG_HARD_LINK);
1394 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1396 return cli_ntrename_internal_recv(req);
1399 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1401 TALLOC_CTX *frame = talloc_stackframe();
1402 struct event_context *ev;
1403 struct tevent_req *req;
1404 NTSTATUS status = NT_STATUS_OK;
1406 if (cli_has_async_calls(cli)) {
1408 * Can't use sync call while an async call is in flight
1410 status = NT_STATUS_INVALID_PARAMETER;
1414 ev = event_context_init(frame);
1416 status = NT_STATUS_NO_MEMORY;
1420 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1422 status = NT_STATUS_NO_MEMORY;
1426 if (!tevent_req_poll(req, ev)) {
1427 status = map_nt_error_from_unix(errno);
1431 status = cli_nt_hardlink_recv(req);
1435 if (!NT_STATUS_IS_OK(status)) {
1436 cli_set_error(cli, status);
1441 /****************************************************************************
1443 ****************************************************************************/
1445 static void cli_unlink_done(struct tevent_req *subreq);
1447 struct cli_unlink_state {
1451 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1452 struct event_context *ev,
1453 struct cli_state *cli,
1455 uint16_t mayhave_attrs)
1457 struct tevent_req *req = NULL, *subreq = NULL;
1458 struct cli_unlink_state *state = NULL;
1459 uint8_t additional_flags = 0;
1460 uint8_t *bytes = NULL;
1462 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1467 SSVAL(state->vwv+0, 0, mayhave_attrs);
1469 bytes = talloc_array(state, uint8_t, 1);
1470 if (tevent_req_nomem(bytes, req)) {
1471 return tevent_req_post(req, ev);
1474 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1475 strlen(fname)+1, NULL);
1477 if (tevent_req_nomem(bytes, req)) {
1478 return tevent_req_post(req, ev);
1481 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1482 1, state->vwv, talloc_get_size(bytes), bytes);
1483 if (tevent_req_nomem(subreq, req)) {
1484 return tevent_req_post(req, ev);
1486 tevent_req_set_callback(subreq, cli_unlink_done, req);
1490 static void cli_unlink_done(struct tevent_req *subreq)
1492 struct tevent_req *req = tevent_req_callback_data(
1493 subreq, struct tevent_req);
1496 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1497 TALLOC_FREE(subreq);
1498 if (!NT_STATUS_IS_OK(status)) {
1499 tevent_req_nterror(req, status);
1502 tevent_req_done(req);
1505 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1507 return tevent_req_simple_recv_ntstatus(req);
1510 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1512 TALLOC_CTX *frame = talloc_stackframe();
1513 struct event_context *ev;
1514 struct tevent_req *req;
1515 NTSTATUS status = NT_STATUS_OK;
1517 if (cli_has_async_calls(cli)) {
1519 * Can't use sync call while an async call is in flight
1521 status = NT_STATUS_INVALID_PARAMETER;
1525 ev = event_context_init(frame);
1527 status = NT_STATUS_NO_MEMORY;
1531 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1533 status = NT_STATUS_NO_MEMORY;
1537 if (!tevent_req_poll(req, ev)) {
1538 status = map_nt_error_from_unix(errno);
1542 status = cli_unlink_recv(req);
1546 if (!NT_STATUS_IS_OK(status)) {
1547 cli_set_error(cli, status);
1552 /****************************************************************************
1554 ****************************************************************************/
1556 static void cli_mkdir_done(struct tevent_req *subreq);
1558 struct cli_mkdir_state {
1562 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1563 struct event_context *ev,
1564 struct cli_state *cli,
1567 struct tevent_req *req = NULL, *subreq = NULL;
1568 struct cli_mkdir_state *state = NULL;
1569 uint8_t additional_flags = 0;
1570 uint8_t *bytes = NULL;
1572 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1577 bytes = talloc_array(state, uint8_t, 1);
1578 if (tevent_req_nomem(bytes, req)) {
1579 return tevent_req_post(req, ev);
1582 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1583 strlen(dname)+1, NULL);
1585 if (tevent_req_nomem(bytes, req)) {
1586 return tevent_req_post(req, ev);
1589 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1590 0, NULL, talloc_get_size(bytes), bytes);
1591 if (tevent_req_nomem(subreq, req)) {
1592 return tevent_req_post(req, ev);
1594 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1598 static void cli_mkdir_done(struct tevent_req *subreq)
1600 struct tevent_req *req = tevent_req_callback_data(
1601 subreq, struct tevent_req);
1604 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1605 TALLOC_FREE(subreq);
1606 if (!NT_STATUS_IS_OK(status)) {
1607 tevent_req_nterror(req, status);
1610 tevent_req_done(req);
1613 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1615 return tevent_req_simple_recv_ntstatus(req);
1618 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1620 TALLOC_CTX *frame = talloc_stackframe();
1621 struct event_context *ev;
1622 struct tevent_req *req;
1623 NTSTATUS status = NT_STATUS_OK;
1625 if (cli_has_async_calls(cli)) {
1627 * Can't use sync call while an async call is in flight
1629 status = NT_STATUS_INVALID_PARAMETER;
1633 ev = event_context_init(frame);
1635 status = NT_STATUS_NO_MEMORY;
1639 req = cli_mkdir_send(frame, ev, cli, dname);
1641 status = NT_STATUS_NO_MEMORY;
1645 if (!tevent_req_poll(req, ev)) {
1646 status = map_nt_error_from_unix(errno);
1650 status = cli_mkdir_recv(req);
1654 if (!NT_STATUS_IS_OK(status)) {
1655 cli_set_error(cli, status);
1660 /****************************************************************************
1662 ****************************************************************************/
1664 static void cli_rmdir_done(struct tevent_req *subreq);
1666 struct cli_rmdir_state {
1670 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1671 struct event_context *ev,
1672 struct cli_state *cli,
1675 struct tevent_req *req = NULL, *subreq = NULL;
1676 struct cli_rmdir_state *state = NULL;
1677 uint8_t additional_flags = 0;
1678 uint8_t *bytes = NULL;
1680 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1685 bytes = talloc_array(state, uint8_t, 1);
1686 if (tevent_req_nomem(bytes, req)) {
1687 return tevent_req_post(req, ev);
1690 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1691 strlen(dname)+1, NULL);
1693 if (tevent_req_nomem(bytes, req)) {
1694 return tevent_req_post(req, ev);
1697 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1698 0, NULL, talloc_get_size(bytes), bytes);
1699 if (tevent_req_nomem(subreq, req)) {
1700 return tevent_req_post(req, ev);
1702 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1706 static void cli_rmdir_done(struct tevent_req *subreq)
1708 struct tevent_req *req = tevent_req_callback_data(
1709 subreq, struct tevent_req);
1712 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1713 TALLOC_FREE(subreq);
1714 if (!NT_STATUS_IS_OK(status)) {
1715 tevent_req_nterror(req, status);
1718 tevent_req_done(req);
1721 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1723 return tevent_req_simple_recv_ntstatus(req);
1726 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1728 TALLOC_CTX *frame = talloc_stackframe();
1729 struct event_context *ev;
1730 struct tevent_req *req;
1731 NTSTATUS status = NT_STATUS_OK;
1733 if (cli_has_async_calls(cli)) {
1735 * Can't use sync call while an async call is in flight
1737 status = NT_STATUS_INVALID_PARAMETER;
1741 ev = event_context_init(frame);
1743 status = NT_STATUS_NO_MEMORY;
1747 req = cli_rmdir_send(frame, ev, cli, dname);
1749 status = NT_STATUS_NO_MEMORY;
1753 if (!tevent_req_poll(req, ev)) {
1754 status = map_nt_error_from_unix(errno);
1758 status = cli_rmdir_recv(req);
1762 if (!NT_STATUS_IS_OK(status)) {
1763 cli_set_error(cli, status);
1768 /****************************************************************************
1769 Set or clear the delete on close flag.
1770 ****************************************************************************/
1778 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1780 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, 0, NULL,
1781 NULL, 0, NULL, NULL, 0, NULL);
1782 tevent_req_simple_finish_ntstatus(subreq, status);
1785 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1786 struct event_context *ev,
1787 struct cli_state *cli,
1791 struct tevent_req *req = NULL, *subreq = NULL;
1792 struct doc_state *state = NULL;
1794 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1799 /* Setup setup word. */
1800 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1802 /* Setup param array. */
1803 SSVAL(state->param,0,fnum);
1804 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1806 /* Setup data array. */
1807 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1809 subreq = cli_trans_send(state, /* mem ctx. */
1810 ev, /* event ctx. */
1811 cli, /* cli_state. */
1812 SMBtrans2, /* cmd. */
1813 NULL, /* pipe name. */
1817 &state->setup, /* setup. */
1818 1, /* num setup uint16_t words. */
1819 0, /* max returned setup. */
1820 state->param, /* param. */
1822 2, /* max returned param. */
1823 state->data, /* data. */
1825 0); /* max returned data. */
1827 if (tevent_req_nomem(subreq, req)) {
1828 return tevent_req_post(req, ev);
1830 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1834 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1838 if (tevent_req_is_nterror(req, &status)) {
1841 return NT_STATUS_OK;
1844 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1846 TALLOC_CTX *frame = talloc_stackframe();
1847 struct event_context *ev = NULL;
1848 struct tevent_req *req = NULL;
1849 NTSTATUS status = NT_STATUS_OK;
1851 if (cli_has_async_calls(cli)) {
1853 * Can't use sync call while an async call is in flight
1855 status = NT_STATUS_INVALID_PARAMETER;
1859 ev = event_context_init(frame);
1861 status = NT_STATUS_NO_MEMORY;
1865 req = cli_nt_delete_on_close_send(frame,
1871 status = NT_STATUS_NO_MEMORY;
1875 if (!tevent_req_poll(req, ev)) {
1876 status = map_nt_error_from_unix(errno);
1880 status = cli_nt_delete_on_close_recv(req);
1884 if (!NT_STATUS_IS_OK(status)) {
1885 cli_set_error(cli, status);
1890 struct cli_ntcreate_state {
1895 static void cli_ntcreate_done(struct tevent_req *subreq);
1897 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1898 struct event_context *ev,
1899 struct cli_state *cli,
1901 uint32_t CreatFlags,
1902 uint32_t DesiredAccess,
1903 uint32_t FileAttributes,
1904 uint32_t ShareAccess,
1905 uint32_t CreateDisposition,
1906 uint32_t CreateOptions,
1907 uint8_t SecurityFlags)
1909 struct tevent_req *req, *subreq;
1910 struct cli_ntcreate_state *state;
1913 size_t converted_len;
1915 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1922 SCVAL(vwv+0, 0, 0xFF);
1927 if (cli->use_oplocks) {
1928 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1930 SIVAL(vwv+3, 1, CreatFlags);
1931 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1932 SIVAL(vwv+7, 1, DesiredAccess);
1933 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1934 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1935 SIVAL(vwv+13, 1, FileAttributes);
1936 SIVAL(vwv+15, 1, ShareAccess);
1937 SIVAL(vwv+17, 1, CreateDisposition);
1938 SIVAL(vwv+19, 1, CreateOptions);
1939 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1940 SCVAL(vwv+23, 1, SecurityFlags);
1942 bytes = talloc_array(state, uint8_t, 0);
1943 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1944 fname, strlen(fname)+1,
1947 /* sigh. this copes with broken netapp filer behaviour */
1948 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1950 if (tevent_req_nomem(bytes, req)) {
1951 return tevent_req_post(req, ev);
1954 SSVAL(vwv+2, 1, converted_len);
1956 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1957 talloc_get_size(bytes), bytes);
1958 if (tevent_req_nomem(subreq, req)) {
1959 return tevent_req_post(req, ev);
1961 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1965 static void cli_ntcreate_done(struct tevent_req *subreq)
1967 struct tevent_req *req = tevent_req_callback_data(
1968 subreq, struct tevent_req);
1969 struct cli_ntcreate_state *state = tevent_req_data(
1970 req, struct cli_ntcreate_state);
1978 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1979 &num_bytes, &bytes);
1980 TALLOC_FREE(subreq);
1981 if (!NT_STATUS_IS_OK(status)) {
1982 tevent_req_nterror(req, status);
1985 state->fnum = SVAL(vwv+2, 1);
1986 tevent_req_done(req);
1989 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1991 struct cli_ntcreate_state *state = tevent_req_data(
1992 req, struct cli_ntcreate_state);
1995 if (tevent_req_is_nterror(req, &status)) {
1998 *pfnum = state->fnum;
1999 return NT_STATUS_OK;
2002 NTSTATUS cli_ntcreate(struct cli_state *cli,
2004 uint32_t CreatFlags,
2005 uint32_t DesiredAccess,
2006 uint32_t FileAttributes,
2007 uint32_t ShareAccess,
2008 uint32_t CreateDisposition,
2009 uint32_t CreateOptions,
2010 uint8_t SecurityFlags,
2013 TALLOC_CTX *frame = talloc_stackframe();
2014 struct event_context *ev;
2015 struct tevent_req *req;
2016 NTSTATUS status = NT_STATUS_OK;
2018 if (cli_has_async_calls(cli)) {
2020 * Can't use sync call while an async call is in flight
2022 status = NT_STATUS_INVALID_PARAMETER;
2026 ev = event_context_init(frame);
2028 status = NT_STATUS_NO_MEMORY;
2032 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2033 DesiredAccess, FileAttributes, ShareAccess,
2034 CreateDisposition, CreateOptions,
2037 status = NT_STATUS_NO_MEMORY;
2041 if (!tevent_req_poll(req, ev)) {
2042 status = map_nt_error_from_unix(errno);
2046 status = cli_ntcreate_recv(req, pfid);
2049 if (!NT_STATUS_IS_OK(status)) {
2050 cli_set_error(cli, status);
2055 /****************************************************************************
2057 WARNING: if you open with O_WRONLY then getattrE won't work!
2058 ****************************************************************************/
2060 struct cli_open_state {
2066 static void cli_open_done(struct tevent_req *subreq);
2068 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2069 struct event_context *ev,
2070 struct cli_state *cli, const char *fname,
2071 int flags, int share_mode,
2072 struct tevent_req **psmbreq)
2074 struct tevent_req *req, *subreq;
2075 struct cli_open_state *state;
2077 unsigned accessmode;
2078 uint8_t additional_flags;
2081 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2087 if (flags & O_CREAT) {
2090 if (!(flags & O_EXCL)) {
2091 if (flags & O_TRUNC)
2097 accessmode = (share_mode<<4);
2099 if ((flags & O_ACCMODE) == O_RDWR) {
2101 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2106 if ((flags & O_SYNC) == O_SYNC) {
2107 accessmode |= (1<<14);
2111 if (share_mode == DENY_FCB) {
2115 SCVAL(state->vwv + 0, 0, 0xFF);
2116 SCVAL(state->vwv + 0, 1, 0);
2117 SSVAL(state->vwv + 1, 0, 0);
2118 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2119 SSVAL(state->vwv + 3, 0, accessmode);
2120 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
2121 SSVAL(state->vwv + 5, 0, 0);
2122 SIVAL(state->vwv + 6, 0, 0);
2123 SSVAL(state->vwv + 8, 0, openfn);
2124 SIVAL(state->vwv + 9, 0, 0);
2125 SIVAL(state->vwv + 11, 0, 0);
2126 SIVAL(state->vwv + 13, 0, 0);
2128 additional_flags = 0;
2130 if (cli->use_oplocks) {
2131 /* if using oplocks then ask for a batch oplock via
2132 core and extended methods */
2134 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2135 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2138 bytes = talloc_array(state, uint8_t, 0);
2139 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2140 strlen(fname)+1, NULL);
2142 if (tevent_req_nomem(bytes, req)) {
2143 return tevent_req_post(req, ev);
2146 state->bytes.iov_base = (void *)bytes;
2147 state->bytes.iov_len = talloc_get_size(bytes);
2149 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2150 15, state->vwv, 1, &state->bytes);
2151 if (subreq == NULL) {
2155 tevent_req_set_callback(subreq, cli_open_done, req);
2160 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2161 struct cli_state *cli, const char *fname,
2162 int flags, int share_mode)
2164 struct tevent_req *req, *subreq;
2167 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2173 status = cli_smb_req_send(subreq);
2174 if (!NT_STATUS_IS_OK(status)) {
2175 tevent_req_nterror(req, status);
2176 return tevent_req_post(req, ev);
2181 static void cli_open_done(struct tevent_req *subreq)
2183 struct tevent_req *req = tevent_req_callback_data(
2184 subreq, struct tevent_req);
2185 struct cli_open_state *state = tevent_req_data(
2186 req, struct cli_open_state);
2192 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2194 TALLOC_FREE(subreq);
2195 if (!NT_STATUS_IS_OK(status)) {
2196 tevent_req_nterror(req, status);
2199 state->fnum = SVAL(vwv+2, 0);
2200 tevent_req_done(req);
2203 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2205 struct cli_open_state *state = tevent_req_data(
2206 req, struct cli_open_state);
2209 if (tevent_req_is_nterror(req, &status)) {
2212 *pfnum = state->fnum;
2213 return NT_STATUS_OK;
2216 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2217 int share_mode, uint16_t *pfnum)
2219 TALLOC_CTX *frame = talloc_stackframe();
2220 struct event_context *ev;
2221 struct tevent_req *req;
2222 NTSTATUS status = NT_STATUS_OK;
2224 if (cli_has_async_calls(cli)) {
2226 * Can't use sync call while an async call is in flight
2228 status = NT_STATUS_INVALID_PARAMETER;
2232 ev = event_context_init(frame);
2234 status = NT_STATUS_NO_MEMORY;
2238 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2240 status = NT_STATUS_NO_MEMORY;
2244 if (!tevent_req_poll(req, ev)) {
2245 status = map_nt_error_from_unix(errno);
2249 status = cli_open_recv(req, pfnum);
2252 if (!NT_STATUS_IS_OK(status)) {
2253 cli_set_error(cli, status);
2258 /****************************************************************************
2260 ****************************************************************************/
2262 struct cli_close_state {
2266 static void cli_close_done(struct tevent_req *subreq);
2268 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2269 struct event_context *ev,
2270 struct cli_state *cli,
2272 struct tevent_req **psubreq)
2274 struct tevent_req *req, *subreq;
2275 struct cli_close_state *state;
2277 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2282 SSVAL(state->vwv+0, 0, fnum);
2283 SIVALS(state->vwv+1, 0, -1);
2285 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2287 if (subreq == NULL) {
2291 tevent_req_set_callback(subreq, cli_close_done, req);
2296 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2297 struct event_context *ev,
2298 struct cli_state *cli,
2301 struct tevent_req *req, *subreq;
2304 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2309 status = cli_smb_req_send(subreq);
2310 if (!NT_STATUS_IS_OK(status)) {
2311 tevent_req_nterror(req, status);
2312 return tevent_req_post(req, ev);
2317 static void cli_close_done(struct tevent_req *subreq)
2319 struct tevent_req *req = tevent_req_callback_data(
2320 subreq, struct tevent_req);
2323 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2324 TALLOC_FREE(subreq);
2325 if (!NT_STATUS_IS_OK(status)) {
2326 tevent_req_nterror(req, status);
2329 tevent_req_done(req);
2332 NTSTATUS cli_close_recv(struct tevent_req *req)
2334 return tevent_req_simple_recv_ntstatus(req);
2337 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2339 TALLOC_CTX *frame = talloc_stackframe();
2340 struct event_context *ev;
2341 struct tevent_req *req;
2342 NTSTATUS status = NT_STATUS_OK;
2344 if (cli_has_async_calls(cli)) {
2346 * Can't use sync call while an async call is in flight
2348 status = NT_STATUS_INVALID_PARAMETER;
2352 ev = event_context_init(frame);
2354 status = NT_STATUS_NO_MEMORY;
2358 req = cli_close_send(frame, ev, cli, fnum);
2360 status = NT_STATUS_NO_MEMORY;
2364 if (!tevent_req_poll(req, ev)) {
2365 status = map_nt_error_from_unix(errno);
2369 status = cli_close_recv(req);
2372 if (!NT_STATUS_IS_OK(status)) {
2373 cli_set_error(cli, status);
2378 /****************************************************************************
2379 Truncate a file to a specified size
2380 ****************************************************************************/
2382 struct ftrunc_state {
2388 static void cli_ftruncate_done(struct tevent_req *subreq)
2390 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, 0, NULL,
2391 NULL, 0, NULL, NULL, 0, NULL);
2392 tevent_req_simple_finish_ntstatus(subreq, status);
2395 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2396 struct event_context *ev,
2397 struct cli_state *cli,
2401 struct tevent_req *req = NULL, *subreq = NULL;
2402 struct ftrunc_state *state = NULL;
2404 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2409 /* Setup setup word. */
2410 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2412 /* Setup param array. */
2413 SSVAL(state->param,0,fnum);
2414 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2415 SSVAL(state->param,4,0);
2417 /* Setup data array. */
2418 SBVAL(state->data, 0, size);
2420 subreq = cli_trans_send(state, /* mem ctx. */
2421 ev, /* event ctx. */
2422 cli, /* cli_state. */
2423 SMBtrans2, /* cmd. */
2424 NULL, /* pipe name. */
2428 &state->setup, /* setup. */
2429 1, /* num setup uint16_t words. */
2430 0, /* max returned setup. */
2431 state->param, /* param. */
2433 2, /* max returned param. */
2434 state->data, /* data. */
2436 0); /* max returned data. */
2438 if (tevent_req_nomem(subreq, req)) {
2439 return tevent_req_post(req, ev);
2441 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2445 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2449 if (tevent_req_is_nterror(req, &status)) {
2452 return NT_STATUS_OK;
2455 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2457 TALLOC_CTX *frame = talloc_stackframe();
2458 struct event_context *ev = NULL;
2459 struct tevent_req *req = NULL;
2460 NTSTATUS status = NT_STATUS_OK;
2462 if (cli_has_async_calls(cli)) {
2464 * Can't use sync call while an async call is in flight
2466 status = NT_STATUS_INVALID_PARAMETER;
2470 ev = event_context_init(frame);
2472 status = NT_STATUS_NO_MEMORY;
2476 req = cli_ftruncate_send(frame,
2482 status = NT_STATUS_NO_MEMORY;
2486 if (!tevent_req_poll(req, ev)) {
2487 status = map_nt_error_from_unix(errno);
2491 status = cli_ftruncate_recv(req);
2495 if (!NT_STATUS_IS_OK(status)) {
2496 cli_set_error(cli, status);
2501 /****************************************************************************
2502 send a lock with a specified locktype
2503 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2504 ****************************************************************************/
2506 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2507 uint32_t offset, uint32_t len,
2508 int timeout, unsigned char locktype)
2511 int saved_timeout = cli->timeout;
2513 memset(cli->outbuf,'\0',smb_size);
2514 memset(cli->inbuf,'\0', smb_size);
2516 cli_set_message(cli->outbuf,8,0,True);
2518 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2519 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2520 cli_setup_packet(cli);
2522 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2523 SSVAL(cli->outbuf,smb_vwv2,fnum);
2524 SCVAL(cli->outbuf,smb_vwv3,locktype);
2525 SIVALS(cli->outbuf, smb_vwv4, timeout);
2526 SSVAL(cli->outbuf,smb_vwv6,0);
2527 SSVAL(cli->outbuf,smb_vwv7,1);
2529 p = smb_buf(cli->outbuf);
2530 SSVAL(p, 0, cli->pid);
2531 SIVAL(p, 2, offset);
2536 cli_setup_bcc(cli, p);
2541 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
2544 if (!cli_receive_smb(cli)) {
2545 cli->timeout = saved_timeout;
2546 return NT_STATUS_UNSUCCESSFUL;
2549 cli->timeout = saved_timeout;
2551 return cli_nt_error(cli);
2554 /****************************************************************************
2556 note that timeout is in units of 2 milliseconds
2557 ****************************************************************************/
2559 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2560 uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
2563 int saved_timeout = cli->timeout;
2565 memset(cli->outbuf,'\0',smb_size);
2566 memset(cli->inbuf,'\0', smb_size);
2568 cli_set_message(cli->outbuf,8,0,True);
2570 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2571 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2572 cli_setup_packet(cli);
2574 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2575 SSVAL(cli->outbuf,smb_vwv2,fnum);
2576 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
2577 SIVALS(cli->outbuf, smb_vwv4, timeout);
2578 SSVAL(cli->outbuf,smb_vwv6,0);
2579 SSVAL(cli->outbuf,smb_vwv7,1);
2581 p = smb_buf(cli->outbuf);
2582 SSVAL(p, 0, cli->pid);
2583 SIVAL(p, 2, offset);
2588 cli_setup_bcc(cli, p);
2593 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
2596 if (!cli_receive_smb(cli)) {
2597 cli->timeout = saved_timeout;
2601 cli->timeout = saved_timeout;
2603 if (cli_is_error(cli)) {
2610 /****************************************************************************
2612 ****************************************************************************/
2614 struct cli_unlock_state {
2619 static void cli_unlock_done(struct tevent_req *subreq);
2621 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2622 struct event_context *ev,
2623 struct cli_state *cli,
2629 struct tevent_req *req = NULL, *subreq = NULL;
2630 struct cli_unlock_state *state = NULL;
2631 uint8_t additional_flags = 0;
2633 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2638 SCVAL(state->vwv+0, 0, 0xFF);
2639 SSVAL(state->vwv+2, 0, fnum);
2640 SCVAL(state->vwv+3, 0, 0);
2641 SIVALS(state->vwv+4, 0, 0);
2642 SSVAL(state->vwv+6, 0, 1);
2643 SSVAL(state->vwv+7, 0, 0);
2645 SSVAL(state->data, 0, cli->pid);
2646 SIVAL(state->data, 2, offset);
2647 SIVAL(state->data, 6, len);
2649 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2650 8, state->vwv, 10, state->data);
2651 if (tevent_req_nomem(subreq, req)) {
2652 return tevent_req_post(req, ev);
2654 tevent_req_set_callback(subreq, cli_unlock_done, req);
2658 static void cli_unlock_done(struct tevent_req *subreq)
2660 struct tevent_req *req = tevent_req_callback_data(
2661 subreq, struct tevent_req);
2664 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2665 TALLOC_FREE(subreq);
2666 if (!NT_STATUS_IS_OK(status)) {
2667 tevent_req_nterror(req, status);
2670 tevent_req_done(req);
2673 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2675 return tevent_req_simple_recv_ntstatus(req);
2678 NTSTATUS cli_unlock(struct cli_state *cli,
2683 TALLOC_CTX *frame = talloc_stackframe();
2684 struct event_context *ev;
2685 struct tevent_req *req;
2686 NTSTATUS status = NT_STATUS_OK;
2688 if (cli_has_async_calls(cli)) {
2690 * Can't use sync call while an async call is in flight
2692 status = NT_STATUS_INVALID_PARAMETER;
2696 ev = event_context_init(frame);
2698 status = NT_STATUS_NO_MEMORY;
2702 req = cli_unlock_send(frame, ev, cli,
2705 status = NT_STATUS_NO_MEMORY;
2709 if (!tevent_req_poll(req, ev)) {
2710 status = map_nt_error_from_unix(errno);
2714 status = cli_unlock_recv(req);
2718 if (!NT_STATUS_IS_OK(status)) {
2719 cli_set_error(cli, status);
2724 /****************************************************************************
2725 Lock a file with 64 bit offsets.
2726 ****************************************************************************/
2728 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2729 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
2732 int saved_timeout = cli->timeout;
2735 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2736 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2739 ltype = (lock_type == READ_LOCK? 1 : 0);
2740 ltype |= LOCKING_ANDX_LARGE_FILES;
2742 memset(cli->outbuf,'\0',smb_size);
2743 memset(cli->inbuf,'\0', smb_size);
2745 cli_set_message(cli->outbuf,8,0,True);
2747 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2748 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2749 cli_setup_packet(cli);
2751 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2752 SSVAL(cli->outbuf,smb_vwv2,fnum);
2753 SCVAL(cli->outbuf,smb_vwv3,ltype);
2754 SIVALS(cli->outbuf, smb_vwv4, timeout);
2755 SSVAL(cli->outbuf,smb_vwv6,0);
2756 SSVAL(cli->outbuf,smb_vwv7,1);
2758 p = smb_buf(cli->outbuf);
2759 SIVAL(p, 0, cli->pid);
2760 SOFF_T_R(p, 4, offset);
2761 SOFF_T_R(p, 12, len);
2764 cli_setup_bcc(cli, p);
2768 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
2771 if (!cli_receive_smb(cli)) {
2772 cli->timeout = saved_timeout;
2776 cli->timeout = saved_timeout;
2778 if (cli_is_error(cli)) {
2785 /****************************************************************************
2786 Unlock a file with 64 bit offsets.
2787 ****************************************************************************/
2789 struct cli_unlock64_state {
2794 static void cli_unlock64_done(struct tevent_req *subreq);
2796 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2797 struct event_context *ev,
2798 struct cli_state *cli,
2804 struct tevent_req *req = NULL, *subreq = NULL;
2805 struct cli_unlock64_state *state = NULL;
2806 uint8_t additional_flags = 0;
2808 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2813 SCVAL(state->vwv+0, 0, 0xff);
2814 SSVAL(state->vwv+2, 0, fnum);
2815 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2816 SIVALS(state->vwv+4, 0, 0);
2817 SSVAL(state->vwv+6, 0, 1);
2818 SSVAL(state->vwv+7, 0, 0);
2820 SIVAL(state->data, 0, cli->pid);
2821 SOFF_T_R(state->data, 4, offset);
2822 SOFF_T_R(state->data, 12, len);
2824 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2825 8, state->vwv, 20, state->data);
2826 if (tevent_req_nomem(subreq, req)) {
2827 return tevent_req_post(req, ev);
2829 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2833 static void cli_unlock64_done(struct tevent_req *subreq)
2835 struct tevent_req *req = tevent_req_callback_data(
2836 subreq, struct tevent_req);
2839 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2840 TALLOC_FREE(subreq);
2841 if (!NT_STATUS_IS_OK(status)) {
2842 tevent_req_nterror(req, status);
2845 tevent_req_done(req);
2848 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2850 return tevent_req_simple_recv_ntstatus(req);
2853 NTSTATUS cli_unlock64(struct cli_state *cli,
2858 TALLOC_CTX *frame = talloc_stackframe();
2859 struct event_context *ev;
2860 struct tevent_req *req;
2861 NTSTATUS status = NT_STATUS_OK;
2863 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2864 return cli_unlock(cli, fnum, offset, len);
2867 if (cli_has_async_calls(cli)) {
2869 * Can't use sync call while an async call is in flight
2871 status = NT_STATUS_INVALID_PARAMETER;
2875 ev = event_context_init(frame);
2877 status = NT_STATUS_NO_MEMORY;
2881 req = cli_unlock64_send(frame, ev, cli,
2884 status = NT_STATUS_NO_MEMORY;
2888 if (!tevent_req_poll(req, ev)) {
2889 status = map_nt_error_from_unix(errno);
2893 status = cli_unlock64_recv(req);
2897 if (!NT_STATUS_IS_OK(status)) {
2898 cli_set_error(cli, status);
2903 /****************************************************************************
2904 Get/unlock a POSIX lock on a file - internal function.
2905 ****************************************************************************/
2907 struct posix_lock_state {
2910 uint8_t data[POSIX_LOCK_DATA_SIZE];
2913 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2915 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, 0, NULL,
2916 NULL, 0, NULL, NULL, 0, NULL);
2917 tevent_req_simple_finish_ntstatus(subreq, status);
2920 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2921 struct event_context *ev,
2922 struct cli_state *cli,
2927 enum brl_type lock_type)
2929 struct tevent_req *req = NULL, *subreq = NULL;
2930 struct posix_lock_state *state = NULL;
2932 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2937 /* Setup setup word. */
2938 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2940 /* Setup param array. */
2941 SSVAL(&state->param, 0, fnum);
2942 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2944 /* Setup data array. */
2945 switch (lock_type) {
2947 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2948 POSIX_LOCK_TYPE_READ);
2951 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2952 POSIX_LOCK_TYPE_WRITE);
2955 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2956 POSIX_LOCK_TYPE_UNLOCK);
2963 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2964 POSIX_LOCK_FLAG_WAIT);
2966 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2967 POSIX_LOCK_FLAG_NOWAIT);
2970 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
2971 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2972 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2974 subreq = cli_trans_send(state, /* mem ctx. */
2975 ev, /* event ctx. */
2976 cli, /* cli_state. */
2977 SMBtrans2, /* cmd. */
2978 NULL, /* pipe name. */
2982 &state->setup, /* setup. */
2983 1, /* num setup uint16_t words. */
2984 0, /* max returned setup. */
2985 state->param, /* param. */
2987 2, /* max returned param. */
2988 state->data, /* data. */
2989 POSIX_LOCK_DATA_SIZE, /* num data. */
2990 0); /* max returned data. */
2992 if (tevent_req_nomem(subreq, req)) {
2993 return tevent_req_post(req, ev);
2995 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
2999 /****************************************************************************
3001 ****************************************************************************/
3003 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
3004 struct event_context *ev,
3005 struct cli_state *cli,
3010 enum brl_type lock_type)
3012 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3013 wait_lock, lock_type);
3016 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
3020 if (tevent_req_is_nterror(req, &status)) {
3023 return NT_STATUS_OK;
3026 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
3027 uint64_t offset, uint64_t len,
3028 bool wait_lock, enum brl_type lock_type)
3030 TALLOC_CTX *frame = talloc_stackframe();
3031 struct event_context *ev = NULL;
3032 struct tevent_req *req = NULL;
3033 NTSTATUS status = NT_STATUS_OK;
3035 if (cli_has_async_calls(cli)) {
3037 * Can't use sync call while an async call is in flight
3039 status = NT_STATUS_INVALID_PARAMETER;
3043 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
3044 status = NT_STATUS_INVALID_PARAMETER;
3048 ev = event_context_init(frame);
3050 status = NT_STATUS_NO_MEMORY;
3054 req = cli_posix_lock_send(frame,
3063 status = NT_STATUS_NO_MEMORY;
3067 if (!tevent_req_poll(req, ev)) {
3068 status = map_nt_error_from_unix(errno);
3072 status = cli_posix_lock_recv(req);
3076 if (!NT_STATUS_IS_OK(status)) {
3077 cli_set_error(cli, status);
3082 /****************************************************************************
3083 POSIX Unlock a file.
3084 ****************************************************************************/
3086 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3087 struct event_context *ev,
3088 struct cli_state *cli,
3093 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3094 false, UNLOCK_LOCK);
3097 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3101 if (tevent_req_is_nterror(req, &status)) {
3104 return NT_STATUS_OK;
3107 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3109 TALLOC_CTX *frame = talloc_stackframe();
3110 struct event_context *ev = NULL;
3111 struct tevent_req *req = NULL;
3112 NTSTATUS status = NT_STATUS_OK;
3114 if (cli_has_async_calls(cli)) {
3116 * Can't use sync call while an async call is in flight
3118 status = NT_STATUS_INVALID_PARAMETER;
3122 ev = event_context_init(frame);
3124 status = NT_STATUS_NO_MEMORY;
3128 req = cli_posix_unlock_send(frame,
3135 status = NT_STATUS_NO_MEMORY;
3139 if (!tevent_req_poll(req, ev)) {
3140 status = map_nt_error_from_unix(errno);
3144 status = cli_posix_unlock_recv(req);
3148 if (!NT_STATUS_IS_OK(status)) {
3149 cli_set_error(cli, status);
3154 /****************************************************************************
3155 Do a SMBgetattrE call.
3156 ****************************************************************************/
3158 static void cli_getattrE_done(struct tevent_req *subreq);
3160 struct cli_getattrE_state {
3170 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3171 struct event_context *ev,
3172 struct cli_state *cli,
3175 struct tevent_req *req = NULL, *subreq = NULL;
3176 struct cli_getattrE_state *state = NULL;
3177 uint8_t additional_flags = 0;
3179 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3184 state->zone_offset = cli->serverzone;
3185 SSVAL(state->vwv+0,0,fnum);
3187 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3188 1, state->vwv, 0, NULL);
3189 if (tevent_req_nomem(subreq, req)) {
3190 return tevent_req_post(req, ev);
3192 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3196 static void cli_getattrE_done(struct tevent_req *subreq)
3198 struct tevent_req *req = tevent_req_callback_data(
3199 subreq, struct tevent_req);
3200 struct cli_getattrE_state *state = tevent_req_data(
3201 req, struct cli_getattrE_state);
3203 uint16_t *vwv = NULL;
3207 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3209 TALLOC_FREE(subreq);
3210 if (!NT_STATUS_IS_OK(status)) {
3211 tevent_req_nterror(req, status);
3215 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3216 state->attr = SVAL(vwv+10,0);
3217 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3218 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3219 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3221 tevent_req_done(req);
3224 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3227 time_t *change_time,
3228 time_t *access_time,
3231 struct cli_getattrE_state *state = tevent_req_data(
3232 req, struct cli_getattrE_state);
3235 if (tevent_req_is_nterror(req, &status)) {
3239 *attr = state->attr;
3242 *size = state->size;
3245 *change_time = state->change_time;
3248 *access_time = state->access_time;
3251 *write_time = state->write_time;
3253 return NT_STATUS_OK;
3256 NTSTATUS cli_getattrE(struct cli_state *cli,
3260 time_t *change_time,
3261 time_t *access_time,
3264 TALLOC_CTX *frame = talloc_stackframe();
3265 struct event_context *ev = NULL;
3266 struct tevent_req *req = NULL;
3267 NTSTATUS status = NT_STATUS_OK;
3269 if (cli_has_async_calls(cli)) {
3271 * Can't use sync call while an async call is in flight
3273 status = NT_STATUS_INVALID_PARAMETER;
3277 ev = event_context_init(frame);
3279 status = NT_STATUS_NO_MEMORY;
3283 req = cli_getattrE_send(frame, ev, cli, fnum);
3285 status = NT_STATUS_NO_MEMORY;
3289 if (!tevent_req_poll(req, ev)) {
3290 status = map_nt_error_from_unix(errno);
3294 status = cli_getattrE_recv(req,
3303 if (!NT_STATUS_IS_OK(status)) {
3304 cli_set_error(cli, status);
3309 /****************************************************************************
3311 ****************************************************************************/
3313 static void cli_getatr_done(struct tevent_req *subreq);
3315 struct cli_getatr_state {
3322 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3323 struct event_context *ev,
3324 struct cli_state *cli,
3327 struct tevent_req *req = NULL, *subreq = NULL;
3328 struct cli_getatr_state *state = NULL;
3329 uint8_t additional_flags = 0;
3330 uint8_t *bytes = NULL;
3332 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3337 state->zone_offset = cli->serverzone;
3339 bytes = talloc_array(state, uint8_t, 1);
3340 if (tevent_req_nomem(bytes, req)) {
3341 return tevent_req_post(req, ev);
3344 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3345 strlen(fname)+1, NULL);
3347 if (tevent_req_nomem(bytes, req)) {
3348 return tevent_req_post(req, ev);
3351 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3352 0, NULL, talloc_get_size(bytes), bytes);
3353 if (tevent_req_nomem(subreq, req)) {
3354 return tevent_req_post(req, ev);
3356 tevent_req_set_callback(subreq, cli_getatr_done, req);
3360 static void cli_getatr_done(struct tevent_req *subreq)
3362 struct tevent_req *req = tevent_req_callback_data(
3363 subreq, struct tevent_req);
3364 struct cli_getatr_state *state = tevent_req_data(
3365 req, struct cli_getatr_state);
3367 uint16_t *vwv = NULL;
3371 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3373 TALLOC_FREE(subreq);
3374 if (!NT_STATUS_IS_OK(status)) {
3375 tevent_req_nterror(req, status);
3379 state->attr = SVAL(vwv+0,0);
3380 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3381 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3383 tevent_req_done(req);
3386 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3391 struct cli_getatr_state *state = tevent_req_data(
3392 req, struct cli_getatr_state);
3395 if (tevent_req_is_nterror(req, &status)) {
3399 *attr = state->attr;
3402 *size = state->size;
3405 *write_time = state->write_time;
3407 return NT_STATUS_OK;
3410 NTSTATUS cli_getatr(struct cli_state *cli,
3416 TALLOC_CTX *frame = talloc_stackframe();
3417 struct event_context *ev = NULL;
3418 struct tevent_req *req = NULL;
3419 NTSTATUS status = NT_STATUS_OK;
3421 if (cli_has_async_calls(cli)) {
3423 * Can't use sync call while an async call is in flight
3425 status = NT_STATUS_INVALID_PARAMETER;
3429 ev = event_context_init(frame);
3431 status = NT_STATUS_NO_MEMORY;
3435 req = cli_getatr_send(frame, ev, cli, fname);
3437 status = NT_STATUS_NO_MEMORY;
3441 if (!tevent_req_poll(req, ev)) {
3442 status = map_nt_error_from_unix(errno);
3446 status = cli_getatr_recv(req,
3453 if (!NT_STATUS_IS_OK(status)) {
3454 cli_set_error(cli, status);
3459 /****************************************************************************
3460 Do a SMBsetattrE call.
3461 ****************************************************************************/
3463 static void cli_setattrE_done(struct tevent_req *subreq);
3465 struct cli_setattrE_state {
3469 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3470 struct event_context *ev,
3471 struct cli_state *cli,
3477 struct tevent_req *req = NULL, *subreq = NULL;
3478 struct cli_setattrE_state *state = NULL;
3479 uint8_t additional_flags = 0;
3481 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3486 SSVAL(state->vwv+0, 0, fnum);
3487 cli_put_dos_date2(cli, (char *)&state->vwv[1], 0, change_time);
3488 cli_put_dos_date2(cli, (char *)&state->vwv[3], 0, access_time);
3489 cli_put_dos_date2(cli, (char *)&state->vwv[5], 0, write_time);
3491 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3492 7, state->vwv, 0, NULL);
3493 if (tevent_req_nomem(subreq, req)) {
3494 return tevent_req_post(req, ev);
3496 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3500 static void cli_setattrE_done(struct tevent_req *subreq)
3502 struct tevent_req *req = tevent_req_callback_data(
3503 subreq, struct tevent_req);
3506 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3507 TALLOC_FREE(subreq);
3508 if (!NT_STATUS_IS_OK(status)) {
3509 tevent_req_nterror(req, status);
3512 tevent_req_done(req);
3515 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3517 return tevent_req_simple_recv_ntstatus(req);
3520 NTSTATUS cli_setattrE(struct cli_state *cli,
3526 TALLOC_CTX *frame = talloc_stackframe();
3527 struct event_context *ev = NULL;
3528 struct tevent_req *req = NULL;
3529 NTSTATUS status = NT_STATUS_OK;
3531 if (cli_has_async_calls(cli)) {
3533 * Can't use sync call while an async call is in flight
3535 status = NT_STATUS_INVALID_PARAMETER;
3539 ev = event_context_init(frame);
3541 status = NT_STATUS_NO_MEMORY;
3545 req = cli_setattrE_send(frame, ev,
3553 status = NT_STATUS_NO_MEMORY;
3557 if (!tevent_req_poll(req, ev)) {
3558 status = map_nt_error_from_unix(errno);
3562 status = cli_setattrE_recv(req);
3566 if (!NT_STATUS_IS_OK(status)) {
3567 cli_set_error(cli, status);
3572 /****************************************************************************
3573 Do a SMBsetatr call.
3574 ****************************************************************************/
3576 static void cli_setatr_done(struct tevent_req *subreq);
3578 struct cli_setatr_state {
3582 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3583 struct event_context *ev,
3584 struct cli_state *cli,
3589 struct tevent_req *req = NULL, *subreq = NULL;
3590 struct cli_setatr_state *state = NULL;
3591 uint8_t additional_flags = 0;
3592 uint8_t *bytes = NULL;
3594 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3599 SSVAL(state->vwv+0, 0, attr);
3600 cli_put_dos_date3(cli, (char *)&state->vwv[1], 0, mtime);
3602 bytes = talloc_array(state, uint8_t, 1);
3603 if (tevent_req_nomem(bytes, req)) {
3604 return tevent_req_post(req, ev);
3607 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3608 strlen(fname)+1, NULL);
3609 if (tevent_req_nomem(bytes, req)) {
3610 return tevent_req_post(req, ev);
3612 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3613 talloc_get_size(bytes)+1);
3614 if (tevent_req_nomem(bytes, req)) {
3615 return tevent_req_post(req, ev);
3618 bytes[talloc_get_size(bytes)-1] = 4;
3619 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3621 if (tevent_req_nomem(bytes, req)) {
3622 return tevent_req_post(req, ev);
3625 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3626 8, state->vwv, talloc_get_size(bytes), bytes);
3627 if (tevent_req_nomem(subreq, req)) {
3628 return tevent_req_post(req, ev);
3630 tevent_req_set_callback(subreq, cli_setatr_done, req);
3634 static void cli_setatr_done(struct tevent_req *subreq)
3636 struct tevent_req *req = tevent_req_callback_data(
3637 subreq, struct tevent_req);
3640 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3641 TALLOC_FREE(subreq);
3642 if (!NT_STATUS_IS_OK(status)) {
3643 tevent_req_nterror(req, status);
3646 tevent_req_done(req);
3649 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3651 return tevent_req_simple_recv_ntstatus(req);
3654 NTSTATUS cli_setatr(struct cli_state *cli,
3659 TALLOC_CTX *frame = talloc_stackframe();
3660 struct event_context *ev = NULL;
3661 struct tevent_req *req = NULL;
3662 NTSTATUS status = NT_STATUS_OK;
3664 if (cli_has_async_calls(cli)) {
3666 * Can't use sync call while an async call is in flight
3668 status = NT_STATUS_INVALID_PARAMETER;
3672 ev = event_context_init(frame);
3674 status = NT_STATUS_NO_MEMORY;
3678 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3680 status = NT_STATUS_NO_MEMORY;
3684 if (!tevent_req_poll(req, ev)) {
3685 status = map_nt_error_from_unix(errno);
3689 status = cli_setatr_recv(req);
3693 if (!NT_STATUS_IS_OK(status)) {
3694 cli_set_error(cli, status);
3699 /****************************************************************************
3700 Check for existance of a dir.
3701 ****************************************************************************/
3703 static void cli_chkpath_done(struct tevent_req *subreq);
3705 struct cli_chkpath_state {
3709 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3710 struct event_context *ev,
3711 struct cli_state *cli,
3714 struct tevent_req *req = NULL, *subreq = NULL;
3715 struct cli_chkpath_state *state = NULL;
3716 uint8_t additional_flags = 0;
3717 uint8_t *bytes = NULL;
3719 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3724 bytes = talloc_array(state, uint8_t, 1);
3725 if (tevent_req_nomem(bytes, req)) {
3726 return tevent_req_post(req, ev);
3729 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3730 strlen(fname)+1, NULL);
3732 if (tevent_req_nomem(bytes, req)) {
3733 return tevent_req_post(req, ev);
3736 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3737 0, NULL, talloc_get_size(bytes), bytes);
3738 if (tevent_req_nomem(subreq, req)) {
3739 return tevent_req_post(req, ev);
3741 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3745 static void cli_chkpath_done(struct tevent_req *subreq)
3747 struct tevent_req *req = tevent_req_callback_data(
3748 subreq, struct tevent_req);
3751 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3752 TALLOC_FREE(subreq);
3753 if (!NT_STATUS_IS_OK(status)) {
3754 tevent_req_nterror(req, status);
3757 tevent_req_done(req);
3760 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3762 return tevent_req_simple_recv_ntstatus(req);
3765 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3767 TALLOC_CTX *frame = talloc_stackframe();
3768 struct event_context *ev = NULL;
3769 struct tevent_req *req = NULL;
3771 NTSTATUS status = NT_STATUS_OK;
3773 if (cli_has_async_calls(cli)) {
3775 * Can't use sync call while an async call is in flight
3777 status = NT_STATUS_INVALID_PARAMETER;
3781 path2 = talloc_strdup(frame, path);
3783 status = NT_STATUS_NO_MEMORY;
3786 trim_char(path2,'\0','\\');
3788 path2 = talloc_strdup(frame, "\\");
3790 status = NT_STATUS_NO_MEMORY;
3795 ev = event_context_init(frame);
3797 status = NT_STATUS_NO_MEMORY;
3801 req = cli_chkpath_send(frame, ev, cli, path2);
3803 status = NT_STATUS_NO_MEMORY;
3807 if (!tevent_req_poll(req, ev)) {
3808 status = map_nt_error_from_unix(errno);
3812 status = cli_chkpath_recv(req);
3816 if (!NT_STATUS_IS_OK(status)) {
3817 cli_set_error(cli, status);
3822 /****************************************************************************
3824 ****************************************************************************/
3826 static void cli_dskattr_done(struct tevent_req *subreq);
3828 struct cli_dskattr_state {
3834 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3835 struct event_context *ev,
3836 struct cli_state *cli)
3838 struct tevent_req *req = NULL, *subreq = NULL;
3839 struct cli_dskattr_state *state = NULL;
3840 uint8_t additional_flags = 0;
3842 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3847 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3849 if (tevent_req_nomem(subreq, req)) {
3850 return tevent_req_post(req, ev);
3852 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3856 static void cli_dskattr_done(struct tevent_req *subreq)
3858 struct tevent_req *req = tevent_req_callback_data(
3859 subreq, struct tevent_req);
3860 struct cli_dskattr_state *state = tevent_req_data(
3861 req, struct cli_dskattr_state);
3863 uint16_t *vwv = NULL;
3867 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3869 TALLOC_FREE(subreq);
3870 if (!NT_STATUS_IS_OK(status)) {
3871 tevent_req_nterror(req, status);
3874 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3875 state->total = SVAL(vwv+0, 0);
3876 state->avail = SVAL(vwv+3, 0);
3877 tevent_req_done(req);
3880 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3882 struct cli_dskattr_state *state = tevent_req_data(
3883 req, struct cli_dskattr_state);
3886 if (tevent_req_is_nterror(req, &status)) {
3889 *bsize = state->bsize;
3890 *total = state->total;
3891 *avail = state->avail;
3892 return NT_STATUS_OK;
3895 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3897 TALLOC_CTX *frame = talloc_stackframe();
3898 struct event_context *ev = NULL;
3899 struct tevent_req *req = NULL;
3900 NTSTATUS status = NT_STATUS_OK;
3902 if (cli_has_async_calls(cli)) {
3904 * Can't use sync call while an async call is in flight
3906 status = NT_STATUS_INVALID_PARAMETER;
3910 ev = event_context_init(frame);
3912 status = NT_STATUS_NO_MEMORY;
3916 req = cli_dskattr_send(frame, ev, cli);
3918 status = NT_STATUS_NO_MEMORY;
3922 if (!tevent_req_poll(req, ev)) {
3923 status = map_nt_error_from_unix(errno);
3927 status = cli_dskattr_recv(req, bsize, total, avail);
3931 if (!NT_STATUS_IS_OK(status)) {
3932 cli_set_error(cli, status);
3937 /****************************************************************************
3938 Create and open a temporary file.
3939 ****************************************************************************/
3941 static void cli_ctemp_done(struct tevent_req *subreq);
3943 struct ctemp_state {
3949 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3950 struct event_context *ev,
3951 struct cli_state *cli,
3954 struct tevent_req *req = NULL, *subreq = NULL;
3955 struct ctemp_state *state = NULL;
3956 uint8_t additional_flags = 0;
3957 uint8_t *bytes = NULL;
3959 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3964 SSVAL(state->vwv,0,0);
3965 SIVALS(state->vwv+1,0,-1);
3967 bytes = talloc_array(state, uint8_t, 1);
3968 if (tevent_req_nomem(bytes, req)) {
3969 return tevent_req_post(req, ev);
3972 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3973 strlen(path)+1, NULL);
3974 if (tevent_req_nomem(bytes, req)) {
3975 return tevent_req_post(req, ev);
3978 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3979 3, state->vwv, talloc_get_size(bytes), bytes);
3980 if (tevent_req_nomem(subreq, req)) {
3981 return tevent_req_post(req, ev);
3983 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3987 static void cli_ctemp_done(struct tevent_req *subreq)
3989 struct tevent_req *req = tevent_req_callback_data(
3990 subreq, struct tevent_req);
3991 struct ctemp_state *state = tevent_req_data(
3992 req, struct ctemp_state);
3996 uint32_t num_bytes = 0;
3997 uint8_t *bytes = NULL;
4000 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
4001 &num_bytes, &bytes);
4002 TALLOC_FREE(subreq);
4003 if (!NT_STATUS_IS_OK(status)) {
4004 tevent_req_nterror(req, status);
4008 state->fnum = SVAL(vwv+0, 0);
4010 /* From W2K3, the result is just the ASCII name */
4011 if (num_bytes < 2) {
4012 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
4016 if (pull_string_talloc(state,
4023 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
4026 tevent_req_done(req);
4029 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
4034 struct ctemp_state *state = tevent_req_data(req,
4035 struct ctemp_state);
4038 if (tevent_req_is_nterror(req, &status)) {
4041 *pfnum = state->fnum;
4042 *outfile = talloc_strdup(ctx, state->ret_path);
4044 return NT_STATUS_NO_MEMORY;
4046 return NT_STATUS_OK;
4049 NTSTATUS cli_ctemp(struct cli_state *cli,
4055 TALLOC_CTX *frame = talloc_stackframe();
4056 struct event_context *ev;
4057 struct tevent_req *req;
4058 NTSTATUS status = NT_STATUS_OK;
4060 if (cli_has_async_calls(cli)) {
4062 * Can't use sync call while an async call is in flight
4064 status = NT_STATUS_INVALID_PARAMETER;
4068 ev = event_context_init(frame);
4070 status = NT_STATUS_NO_MEMORY;
4074 req = cli_ctemp_send(frame, ev, cli, path);
4076 status = NT_STATUS_NO_MEMORY;
4080 if (!tevent_req_poll(req, ev)) {
4081 status = map_nt_error_from_unix(errno);
4085 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4089 if (!NT_STATUS_IS_OK(status)) {
4090 cli_set_error(cli, status);
4096 send a raw ioctl - used by the torture code
4098 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4102 struct tevent_req *result_parent;
4104 SSVAL(vwv+0, 0, fnum);
4105 SSVAL(vwv+1, 0, code>>16);
4106 SSVAL(vwv+2, 0, (code&0xFFFF));
4108 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4109 &result_parent, 0, NULL, NULL, NULL, NULL);
4110 if (!NT_STATUS_IS_OK(status)) {
4113 *blob = data_blob_null;
4114 return NT_STATUS_OK;
4117 /*********************************************************
4118 Set an extended attribute utility fn.
4119 *********************************************************/
4121 static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsigned int param_len,
4122 const char *ea_name, const char *ea_val, size_t ea_len)
4124 unsigned int data_len = 0;
4126 char *rparam=NULL, *rdata=NULL;
4128 size_t ea_namelen = strlen(ea_name);
4130 if (ea_namelen == 0 && ea_len == 0) {
4132 data = (char *)SMB_MALLOC(data_len);
4137 SIVAL(p,0,data_len);
4139 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4140 data = (char *)SMB_MALLOC(data_len);
4145 SIVAL(p,0,data_len);
4147 SCVAL(p, 0, 0); /* EA flags. */
4148 SCVAL(p, 1, ea_namelen);
4149 SSVAL(p, 2, ea_len);
4150 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4151 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4154 if (!cli_send_trans(cli, SMBtrans2,
4156 -1, 0, /* fid, flags */
4157 &setup, 1, 0, /* setup, length, max */
4158 param, param_len, 2, /* param, length, max */
4159 data, data_len, cli->max_xmit /* data, length, max */
4165 if (!cli_receive_trans(cli, SMBtrans2,
4166 &rparam, ¶m_len,
4167 &rdata, &data_len)) {
4179 /*********************************************************
4180 Set an extended attribute on a pathname.
4181 *********************************************************/
4183 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
4185 uint16_t setup = TRANSACT2_SETPATHINFO;
4186 unsigned int param_len = 0;
4188 size_t srclen = 2*(strlen(path)+1);
4192 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
4196 memset(param, '\0', 6);
4197 SSVAL(param,0,SMB_INFO_SET_EA);
4200 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4201 param_len = PTR_DIFF(p, param);
4203 ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
4208 /*********************************************************
4209 Set an extended attribute on an fnum.
4210 *********************************************************/
4212 bool cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum, const char *ea_name, const char *ea_val, size_t ea_len)
4215 uint16_t setup = TRANSACT2_SETFILEINFO;
4217 memset(param, 0, 6);
4218 SSVAL(param,0,fnum);
4219 SSVAL(param,2,SMB_INFO_SET_EA);
4221 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
4224 /*********************************************************
4225 Get an extended attribute list utility fn.
4226 *********************************************************/
4228 static bool cli_get_ea_list(struct cli_state *cli,
4229 uint16_t setup, char *param, unsigned int param_len,
4232 struct ea_struct **pea_list)
4234 unsigned int data_len = 0;
4235 unsigned int rparam_len, rdata_len;
4236 char *rparam=NULL, *rdata=NULL;
4241 struct ea_struct *ea_list;
4248 if (!cli_send_trans(cli, SMBtrans2,
4250 -1, 0, /* fid, flags */
4251 &setup, 1, 0, /* setup, length, max */
4252 param, param_len, 10, /* param, length, max */
4253 NULL, data_len, cli->max_xmit /* data, length, max */
4258 if (!cli_receive_trans(cli, SMBtrans2,
4259 &rparam, &rparam_len,
4260 &rdata, &rdata_len)) {
4264 if (!rdata || rdata_len < 4) {
4268 ea_size = (size_t)IVAL(rdata,0);
4269 if (ea_size > rdata_len) {
4274 /* No EA's present. */
4282 /* Validate the EA list and count it. */
4283 for (num_eas = 0; ea_size >= 4; num_eas++) {
4284 unsigned int ea_namelen = CVAL(p,1);
4285 unsigned int ea_valuelen = SVAL(p,2);
4286 if (ea_namelen == 0) {
4289 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4292 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4293 p += 4 + ea_namelen + 1 + ea_valuelen;
4301 *pnum_eas = num_eas;
4303 /* Caller only wants number of EA's. */
4308 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
4313 ea_size = (size_t)IVAL(rdata,0);
4316 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4317 struct ea_struct *ea = &ea_list[num_eas];
4318 fstring unix_ea_name;
4319 unsigned int ea_namelen = CVAL(p,1);
4320 unsigned int ea_valuelen = SVAL(p,2);
4322 ea->flags = CVAL(p,0);
4323 unix_ea_name[0] = '\0';
4324 pull_ascii_fstring(unix_ea_name, p + 4);
4325 ea->name = talloc_strdup(ctx, unix_ea_name);
4326 /* Ensure the value is null terminated (in case it's a string). */
4327 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
4328 if (!ea->value.data) {
4332 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4334 ea->value.data[ea_valuelen] = 0;
4336 p += 4 + ea_namelen + 1 + ea_valuelen;
4339 *pea_list = ea_list;
4349 /*********************************************************
4350 Get an extended attribute list from a pathname.
4351 *********************************************************/
4353 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
4356 struct ea_struct **pea_list)
4358 uint16_t setup = TRANSACT2_QPATHINFO;
4359 unsigned int param_len = 0;
4362 size_t srclen = 2*(strlen(path)+1);
4365 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
4371 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
4373 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4374 param_len = PTR_DIFF(p, param);
4376 ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
4381 /*********************************************************
4382 Get an extended attribute list from an fnum.
4383 *********************************************************/
4385 bool cli_get_ea_list_fnum(struct cli_state *cli, uint16_t fnum,
4388 struct ea_struct **pea_list)
4390 uint16_t setup = TRANSACT2_QFILEINFO;
4393 memset(param, 0, 6);
4394 SSVAL(param,0,fnum);
4395 SSVAL(param,2,SMB_INFO_SET_EA);
4397 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
4400 /****************************************************************************
4401 Convert open "flags" arg to uint32_t on wire.
4402 ****************************************************************************/
4404 static uint32_t open_flags_to_wire(int flags)
4406 int open_mode = flags & O_ACCMODE;
4409 switch (open_mode) {
4411 ret |= SMB_O_WRONLY;
4418 ret |= SMB_O_RDONLY;
4422 if (flags & O_CREAT) {
4425 if (flags & O_EXCL) {
4428 if (flags & O_TRUNC) {
4432 if (flags & O_SYNC) {
4436 if (flags & O_APPEND) {
4437 ret |= SMB_O_APPEND;
4439 #if defined(O_DIRECT)
4440 if (flags & O_DIRECT) {
4441 ret |= SMB_O_DIRECT;
4444 #if defined(O_DIRECTORY)
4445 if (flags & O_DIRECTORY) {
4446 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4447 ret |= SMB_O_DIRECTORY;
4453 /****************************************************************************
4454 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4455 ****************************************************************************/
4457 struct posix_open_state {
4461 uint16_t fnum; /* Out */
4464 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4466 struct tevent_req *req = tevent_req_callback_data(
4467 subreq, struct tevent_req);
4468 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4473 status = cli_trans_recv(subreq, state, NULL, 0, NULL, NULL, 0, NULL,
4474 &data, 12, &num_data);
4475 TALLOC_FREE(subreq);
4476 if (!NT_STATUS_IS_OK(status)) {
4477 tevent_req_nterror(req, status);
4480 state->fnum = SVAL(data,2);
4481 tevent_req_done(req);
4484 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4485 struct event_context *ev,
4486 struct cli_state *cli,
4492 struct tevent_req *req = NULL, *subreq = NULL;
4493 struct posix_open_state *state = NULL;
4494 uint32_t wire_flags = open_flags_to_wire(flags);
4496 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4501 /* Setup setup word. */
4502 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4504 /* Setup param array. */
4505 state->param = talloc_array(state, uint8_t, 6);
4506 if (tevent_req_nomem(state->param, req)) {
4507 return tevent_req_post(req, ev);
4509 memset(state->param, '\0', 6);
4510 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4512 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4513 strlen(fname)+1, NULL);
4515 if (tevent_req_nomem(state->param, req)) {
4516 return tevent_req_post(req, ev);
4519 /* Setup data words. */
4521 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4522 wire_flags |= SMB_O_DIRECTORY;
4525 SIVAL(state->data,0,0); /* No oplock. */
4526 SIVAL(state->data,4,wire_flags);
4527 SIVAL(state->data,8,unix_perms_to_wire(mode));
4528 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4529 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4531 subreq = cli_trans_send(state, /* mem ctx. */
4532 ev, /* event ctx. */
4533 cli, /* cli_state. */
4534 SMBtrans2, /* cmd. */
4535 NULL, /* pipe name. */
4539 &state->setup, /* setup. */
4540 1, /* num setup uint16_t words. */
4541 0, /* max returned setup. */
4542 state->param, /* param. */
4543 talloc_get_size(state->param),/* num param. */
4544 2, /* max returned param. */
4545 state->data, /* data. */
4547 12); /* max returned data. */
4549 if (tevent_req_nomem(subreq, req)) {
4550 return tevent_req_post(req, ev);
4552 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4556 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4557 struct event_context *ev,
4558 struct cli_state *cli,
4563 return cli_posix_open_internal_send(mem_ctx, ev,
4564 cli, fname, flags, mode, false);
4567 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4569 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4572 if (tevent_req_is_nterror(req, &status)) {
4575 *pfnum = state->fnum;
4576 return NT_STATUS_OK;
4579 /****************************************************************************
4580 Open - POSIX semantics. Doesn't request oplock.
4581 ****************************************************************************/
4583 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4584 int flags, mode_t mode, uint16_t *pfnum)
4587 TALLOC_CTX *frame = talloc_stackframe();
4588 struct event_context *ev = NULL;
4589 struct tevent_req *req = NULL;
4590 NTSTATUS status = NT_STATUS_OK;
4592 if (cli_has_async_calls(cli)) {
4594 * Can't use sync call while an async call is in flight
4596 status = NT_STATUS_INVALID_PARAMETER;
4600 ev = event_context_init(frame);
4602 status = NT_STATUS_NO_MEMORY;
4606 req = cli_posix_open_send(frame,
4613 status = NT_STATUS_NO_MEMORY;
4617 if (!tevent_req_poll(req, ev)) {
4618 status = map_nt_error_from_unix(errno);
4622 status = cli_posix_open_recv(req, pfnum);
4626 if (!NT_STATUS_IS_OK(status)) {
4627 cli_set_error(cli, status);
4632 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4633 struct event_context *ev,
4634 struct cli_state *cli,
4638 return cli_posix_open_internal_send(mem_ctx, ev,
4639 cli, fname, O_CREAT, mode, true);
4642 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4646 if (tevent_req_is_nterror(req, &status)) {
4649 return NT_STATUS_OK;
4652 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4654 TALLOC_CTX *frame = talloc_stackframe();
4655 struct event_context *ev = NULL;
4656 struct tevent_req *req = NULL;
4657 NTSTATUS status = NT_STATUS_OK;
4659 if (cli_has_async_calls(cli)) {
4661 * Can't use sync call while an async call is in flight
4663 status = NT_STATUS_INVALID_PARAMETER;
4667 ev = event_context_init(frame);
4669 status = NT_STATUS_NO_MEMORY;
4673 req = cli_posix_mkdir_send(frame,
4679 status = NT_STATUS_NO_MEMORY;
4683 if (!tevent_req_poll(req, ev)) {
4684 status = map_nt_error_from_unix(errno);
4688 status = cli_posix_mkdir_recv(req);
4692 if (!NT_STATUS_IS_OK(status)) {
4693 cli_set_error(cli, status);
4698 /****************************************************************************
4699 unlink or rmdir - POSIX semantics.
4700 ****************************************************************************/
4702 struct unlink_state {
4707 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4709 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, 0, NULL,
4710 NULL, 0, NULL, NULL, 0, NULL);
4711 tevent_req_simple_finish_ntstatus(subreq, status);
4714 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4715 struct event_context *ev,
4716 struct cli_state *cli,
4720 struct tevent_req *req = NULL, *subreq = NULL;
4721 struct unlink_state *state = NULL;
4722 uint8_t *param = NULL;
4724 req = tevent_req_create(mem_ctx, &state, struct unlink_state);
4729 /* Setup setup word. */
4730 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4732 /* Setup param array. */
4733 param = talloc_array(state, uint8_t, 6);
4734 if (tevent_req_nomem(param, req)) {
4735 return tevent_req_post(req, ev);
4737 memset(param, '\0', 6);
4738 SSVAL(param, 0, SMB_POSIX_PATH_UNLINK);
4740 param = trans2_bytes_push_str(param, cli_ucs2(cli), fname,
4741 strlen(fname)+1, NULL);
4743 if (tevent_req_nomem(param, req)) {
4744 return tevent_req_post(req, ev);
4747 /* Setup data word. */
4748 SSVAL(state->data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
4749 SMB_POSIX_UNLINK_FILE_TARGET);
4751 subreq = cli_trans_send(state, /* mem ctx. */
4752 ev, /* event ctx. */
4753 cli, /* cli_state. */
4754 SMBtrans2, /* cmd. */
4755 NULL, /* pipe name. */
4759 &state->setup, /* setup. */
4760 1, /* num setup uint16_t words. */
4761 0, /* max returned setup. */
4763 talloc_get_size(param), /* num param. */
4764 2, /* max returned param. */
4765 state->data, /* data. */
4767 0); /* max returned data. */
4769 if (tevent_req_nomem(subreq, req)) {
4770 return tevent_req_post(req, ev);
4772 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4776 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4777 struct event_context *ev,
4778 struct cli_state *cli,
4781 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, false);
4784 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4788 if (tevent_req_is_nterror(req, &status)) {
4791 return NT_STATUS_OK;
4794 /****************************************************************************
4795 unlink - POSIX semantics.
4796 ****************************************************************************/
4798 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4800 TALLOC_CTX *frame = talloc_stackframe();
4801 struct event_context *ev = NULL;
4802 struct tevent_req *req = NULL;
4803 NTSTATUS status = NT_STATUS_OK;
4805 if (cli_has_async_calls(cli)) {
4807 * Can't use sync call while an async call is in flight
4809 status = NT_STATUS_INVALID_PARAMETER;
4813 ev = event_context_init(frame);
4815 status = NT_STATUS_NO_MEMORY;
4819 req = cli_posix_unlink_send(frame,
4824 status = NT_STATUS_NO_MEMORY;
4828 if (!tevent_req_poll(req, ev)) {
4829 status = map_nt_error_from_unix(errno);
4833 status = cli_posix_unlink_recv(req);
4837 if (!NT_STATUS_IS_OK(status)) {
4838 cli_set_error(cli, status);
4843 /****************************************************************************
4844 rmdir - POSIX semantics.
4845 ****************************************************************************/
4847 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4848 struct event_context *ev,
4849 struct cli_state *cli,
4852 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, true);
4855 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4859 if (tevent_req_is_nterror(req, &status)) {
4862 return NT_STATUS_OK;
4865 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4867 TALLOC_CTX *frame = talloc_stackframe();
4868 struct event_context *ev = NULL;
4869 struct tevent_req *req = NULL;
4870 NTSTATUS status = NT_STATUS_OK;
4872 if (cli_has_async_calls(cli)) {
4874 * Can't use sync call while an async call is in flight
4876 status = NT_STATUS_INVALID_PARAMETER;
4880 ev = event_context_init(frame);
4882 status = NT_STATUS_NO_MEMORY;
4886 req = cli_posix_rmdir_send(frame,
4891 status = NT_STATUS_NO_MEMORY;
4895 if (!tevent_req_poll(req, ev)) {
4896 status = map_nt_error_from_unix(errno);
4900 status = cli_posix_rmdir_recv(req, frame);
4904 if (!NT_STATUS_IS_OK(status)) {
4905 cli_set_error(cli, status);
4910 /****************************************************************************
4912 ****************************************************************************/
4914 struct cli_notify_state {
4916 uint32_t num_changes;
4917 struct notify_change *changes;
4920 static void cli_notify_done(struct tevent_req *subreq);
4922 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4923 struct tevent_context *ev,
4924 struct cli_state *cli, uint16_t fnum,
4925 uint32_t buffer_size,
4926 uint32_t completion_filter, bool recursive)
4928 struct tevent_req *req, *subreq;
4929 struct cli_notify_state *state;
4931 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4936 SIVAL(state->setup, 0, completion_filter);
4937 SSVAL(state->setup, 4, fnum);
4938 SSVAL(state->setup, 6, recursive);
4940 subreq = cli_trans_send(
4941 state, /* mem ctx. */
4942 ev, /* event ctx. */
4943 cli, /* cli_state. */
4944 SMBnttrans, /* cmd. */
4945 NULL, /* pipe name. */
4947 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4949 (uint16_t *)state->setup, /* setup. */
4950 4, /* num setup uint16_t words. */
4951 0, /* max returned setup. */
4954 buffer_size, /* max returned param. */
4957 0); /* max returned data. */
4959 if (tevent_req_nomem(subreq, req)) {
4960 return tevent_req_post(req, ev);
4962 tevent_req_set_callback(subreq, cli_notify_done, req);
4966 static void cli_notify_done(struct tevent_req *subreq)
4968 struct tevent_req *req = tevent_req_callback_data(
4969 subreq, struct tevent_req);
4970 struct cli_notify_state *state = tevent_req_data(
4971 req, struct cli_notify_state);
4974 uint32_t i, ofs, num_params;
4976 status = cli_trans_recv(subreq, talloc_tos(), NULL, 0, NULL,
4977 ¶ms, 0, &num_params, NULL, 0, NULL);
4978 TALLOC_FREE(subreq);
4979 if (!NT_STATUS_IS_OK(status)) {
4980 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4981 tevent_req_nterror(req, status);
4985 state->num_changes = 0;
4988 while (num_params - ofs > 12) {
4989 uint32_t len = IVAL(params, ofs);
4990 state->num_changes += 1;
4992 if ((len == 0) || (ofs+len >= num_params)) {
4998 state->changes = talloc_array(state, struct notify_change,
4999 state->num_changes);
5000 if (tevent_req_nomem(state->changes, req)) {
5001 TALLOC_FREE(params);
5007 for (i=0; i<state->num_changes; i++) {
5008 uint32_t next = IVAL(params, ofs);
5009 uint32_t len = IVAL(params, ofs+8);
5013 if ((next != 0) && (len+12 != next)) {
5014 TALLOC_FREE(params);
5016 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
5020 state->changes[i].action = IVAL(params, ofs+4);
5021 ret = clistr_pull_talloc(params, (char *)params, &name,
5023 STR_TERMINATE|STR_UNICODE);
5025 TALLOC_FREE(params);
5026 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
5029 state->changes[i].name = name;
5033 TALLOC_FREE(params);
5034 tevent_req_done(req);
5037 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5038 uint32_t *pnum_changes,
5039 struct notify_change **pchanges)
5041 struct cli_notify_state *state = tevent_req_data(
5042 req, struct cli_notify_state);
5045 if (tevent_req_is_nterror(req, &status)) {
5049 *pnum_changes = state->num_changes;
5050 *pchanges = talloc_move(mem_ctx, &state->changes);
5051 return NT_STATUS_OK;
5054 struct cli_qpathinfo_state {
5063 static void cli_qpathinfo_done(struct tevent_req *subreq);
5065 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
5066 struct tevent_context *ev,
5067 struct cli_state *cli, const char *fname,
5068 uint16_t level, uint32_t min_rdata,
5071 struct tevent_req *req, *subreq;
5072 struct cli_qpathinfo_state *state;
5074 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
5078 state->min_rdata = min_rdata;
5079 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
5081 state->param = talloc_zero_array(state, uint8_t, 6);
5082 if (tevent_req_nomem(state->param, req)) {
5083 return tevent_req_post(req, ev);
5085 SSVAL(state->param, 0, level);
5086 state->param = trans2_bytes_push_str(
5087 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
5088 if (tevent_req_nomem(state->param, req)) {
5089 return tevent_req_post(req, ev);
5092 subreq = cli_trans_send(
5093 state, /* mem ctx. */
5094 ev, /* event ctx. */
5095 cli, /* cli_state. */
5096 SMBtrans2, /* cmd. */
5097 NULL, /* pipe name. */
5101 state->setup, /* setup. */
5102 1, /* num setup uint16_t words. */
5103 0, /* max returned setup. */
5104 state->param, /* param. */
5105 talloc_get_size(state->param), /* num param. */
5106 2, /* max returned param. */
5109 max_rdata); /* max returned data. */
5111 if (tevent_req_nomem(subreq, req)) {
5112 return tevent_req_post(req, ev);
5114 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5118 static void cli_qpathinfo_done(struct tevent_req *subreq)
5120 struct tevent_req *req = tevent_req_callback_data(
5121 subreq, struct tevent_req);
5122 struct cli_qpathinfo_state *state = tevent_req_data(
5123 req, struct cli_qpathinfo_state);
5126 status = cli_trans_recv(subreq, state, NULL, 0, NULL, NULL, 0, NULL,
5127 &state->rdata, state->min_rdata,
5129 if (!NT_STATUS_IS_OK(status)) {
5130 tevent_req_nterror(req, status);
5133 tevent_req_done(req);
5136 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5137 uint8_t **rdata, uint32_t *num_rdata)
5139 struct cli_qpathinfo_state *state = tevent_req_data(
5140 req, struct cli_qpathinfo_state);
5143 if (tevent_req_is_nterror(req, &status)) {
5146 *rdata = talloc_move(mem_ctx, &state->rdata);
5147 *num_rdata = state->num_rdata;
5148 return NT_STATUS_OK;
5151 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5152 const char *fname, uint16_t level, uint32_t min_rdata,
5154 uint8_t **rdata, uint32_t *num_rdata)
5156 TALLOC_CTX *frame = talloc_stackframe();
5157 struct event_context *ev;
5158 struct tevent_req *req;
5159 NTSTATUS status = NT_STATUS_NO_MEMORY;
5161 if (cli_has_async_calls(cli)) {
5163 * Can't use sync call while an async call is in flight
5165 status = NT_STATUS_INVALID_PARAMETER;
5168 ev = event_context_init(frame);
5172 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5177 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5180 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5183 if (!NT_STATUS_IS_OK(status)) {
5184 cli_set_error(cli, status);