2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "async_smb.h"
24 #include "libsmb/clirap.h"
27 /***********************************************************
28 Common function for pushing stings, used by smb_bytes_push_str()
29 and trans_bytes_push_str(). Only difference is the align_odd
31 ***********************************************************/
33 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
34 const char *str, size_t str_len,
36 size_t *pconverted_size)
40 size_t converted_size;
46 buflen = talloc_get_size(buf);
48 if (align_odd && ucs2 && (buflen % 2 == 0)) {
50 * We're pushing into an SMB buffer, align odd
52 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
60 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
61 ucs2 ? CH_UTF16LE : CH_DOS,
62 str, str_len, &converted,
67 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
68 buflen + converted_size);
70 TALLOC_FREE(converted);
74 memcpy(buf + buflen, converted, converted_size);
76 TALLOC_FREE(converted);
78 if (pconverted_size) {
79 *pconverted_size = converted_size;
85 /***********************************************************
86 Push a string into an SMB buffer, with odd byte alignment
87 if it's a UCS2 string.
88 ***********************************************************/
90 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
91 const char *str, size_t str_len,
92 size_t *pconverted_size)
94 return internal_bytes_push_str(buf, ucs2, str, str_len,
95 true, pconverted_size);
98 uint8_t *smb_bytes_push_bytes(uint8_t *buf, uint8_t prefix,
99 const uint8_t *bytes, size_t num_bytes)
106 buflen = talloc_get_size(buf);
108 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
109 buflen + 1 + num_bytes);
113 buf[buflen] = prefix;
114 memcpy(&buf[buflen+1], bytes, num_bytes);
118 /***********************************************************
119 Same as smb_bytes_push_str(), but without the odd byte
120 align for ucs2 (we're pushing into a param or data block).
121 static for now, although this will probably change when
122 other modules use async trans calls.
123 ***********************************************************/
125 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
126 const char *str, size_t str_len,
127 size_t *pconverted_size)
129 return internal_bytes_push_str(buf, ucs2, str, str_len,
130 false, pconverted_size);
133 struct cli_setpathinfo_state {
138 static void cli_setpathinfo_done(struct tevent_req *subreq);
140 struct tevent_req *cli_setpathinfo_send(TALLOC_CTX *mem_ctx,
141 struct tevent_context *ev,
142 struct cli_state *cli,
148 struct tevent_req *req, *subreq;
149 struct cli_setpathinfo_state *state;
151 req = tevent_req_create(mem_ctx, &state,
152 struct cli_setpathinfo_state);
157 /* Setup setup word. */
158 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
160 /* Setup param array. */
161 state->param = TALLOC_ZERO_ARRAY(state, uint8_t, 6);
162 if (tevent_req_nomem(state->param, req)) {
163 return tevent_req_post(req, ev);
165 SSVAL(state->param, 0, level);
167 state->param = trans2_bytes_push_str(
168 state->param, cli_ucs2(cli), path, strlen(path)+1, NULL);
169 if (tevent_req_nomem(state->param, req)) {
170 return tevent_req_post(req, ev);
173 subreq = cli_trans_send(
174 state, /* mem ctx. */
176 cli, /* cli_state. */
177 SMBtrans2, /* cmd. */
178 NULL, /* pipe name. */
182 &state->setup, /* setup. */
183 1, /* num setup uint16_t words. */
184 0, /* max returned setup. */
185 state->param, /* param. */
186 talloc_get_size(state->param), /* num param. */
187 2, /* max returned param. */
189 data_len, /* num data. */
190 0); /* max returned data. */
192 if (tevent_req_nomem(subreq, req)) {
193 return tevent_req_post(req, ev);
195 tevent_req_set_callback(subreq, cli_setpathinfo_done, req);
199 static void cli_setpathinfo_done(struct tevent_req *subreq)
201 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
202 NULL, 0, NULL, NULL, 0, NULL);
203 tevent_req_simple_finish_ntstatus(subreq, status);
206 NTSTATUS cli_setpathinfo_recv(struct tevent_req *req)
208 return tevent_req_simple_recv_ntstatus(req);
211 NTSTATUS cli_setpathinfo(struct cli_state *cli,
217 TALLOC_CTX *frame = talloc_stackframe();
218 struct tevent_context *ev;
219 struct tevent_req *req;
220 NTSTATUS status = NT_STATUS_NO_MEMORY;
222 if (cli_has_async_calls(cli)) {
224 * Can't use sync call while an async call is in flight
226 status = NT_STATUS_INVALID_PARAMETER;
229 ev = tevent_context_init(frame);
233 req = cli_setpathinfo_send(ev, ev, cli, level, path, data, data_len);
237 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
240 status = cli_setpathinfo_recv(req);
246 /****************************************************************************
247 Hard/Symlink a file (UNIX extensions).
248 Creates new name (sym)linked to oldname.
249 ****************************************************************************/
251 struct cli_posix_link_internal_state {
255 static void cli_posix_link_internal_done(struct tevent_req *subreq);
257 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
258 struct event_context *ev,
259 struct cli_state *cli,
264 struct tevent_req *req = NULL, *subreq = NULL;
265 struct cli_posix_link_internal_state *state = NULL;
267 req = tevent_req_create(mem_ctx, &state,
268 struct cli_posix_link_internal_state);
273 /* Setup data array. */
274 state->data = talloc_array(state, uint8_t, 0);
275 if (tevent_req_nomem(state->data, req)) {
276 return tevent_req_post(req, ev);
278 state->data = trans2_bytes_push_str(
279 state->data, cli_ucs2(cli), oldname, strlen(oldname)+1, NULL);
281 subreq = cli_setpathinfo_send(
282 state, ev, cli, level, newname,
283 state->data, talloc_get_size(state->data));
284 if (tevent_req_nomem(subreq, req)) {
285 return tevent_req_post(req, ev);
287 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
291 static void cli_posix_link_internal_done(struct tevent_req *subreq)
293 NTSTATUS status = cli_setpathinfo_recv(subreq);
294 tevent_req_simple_finish_ntstatus(subreq, status);
297 /****************************************************************************
298 Symlink a file (UNIX extensions).
299 ****************************************************************************/
301 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
302 struct event_context *ev,
303 struct cli_state *cli,
307 return cli_posix_link_internal_send(
308 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_LINK, oldname, newname);
311 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
313 return tevent_req_simple_recv_ntstatus(req);
316 NTSTATUS cli_posix_symlink(struct cli_state *cli,
320 TALLOC_CTX *frame = talloc_stackframe();
321 struct event_context *ev = NULL;
322 struct tevent_req *req = NULL;
323 NTSTATUS status = NT_STATUS_OK;
325 if (cli_has_async_calls(cli)) {
327 * Can't use sync call while an async call is in flight
329 status = NT_STATUS_INVALID_PARAMETER;
333 ev = event_context_init(frame);
335 status = NT_STATUS_NO_MEMORY;
339 req = cli_posix_symlink_send(frame,
345 status = NT_STATUS_NO_MEMORY;
349 if (!tevent_req_poll(req, ev)) {
350 status = map_nt_error_from_unix(errno);
354 status = cli_posix_symlink_recv(req);
358 if (!NT_STATUS_IS_OK(status)) {
359 cli_set_error(cli, status);
364 /****************************************************************************
365 Read a POSIX symlink.
366 ****************************************************************************/
368 struct readlink_state {
373 static void cli_posix_readlink_done(struct tevent_req *subreq);
375 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
376 struct event_context *ev,
377 struct cli_state *cli,
381 struct tevent_req *req = NULL, *subreq = NULL;
382 struct readlink_state *state = NULL;
383 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
385 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
391 * Len is in bytes, we need it in UCS2 units.
393 if ((2*len < len) || (maxbytelen < len)) {
394 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
395 return tevent_req_post(req, ev);
398 subreq = cli_qpathinfo_send(state, ev, cli, fname,
399 SMB_QUERY_FILE_UNIX_LINK, 1, maxbytelen);
400 if (tevent_req_nomem(subreq, req)) {
401 return tevent_req_post(req, ev);
403 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
407 static void cli_posix_readlink_done(struct tevent_req *subreq)
409 struct tevent_req *req = tevent_req_callback_data(
410 subreq, struct tevent_req);
411 struct readlink_state *state = tevent_req_data(
412 req, struct readlink_state);
415 status = cli_qpathinfo_recv(subreq, state, &state->data,
418 if (!NT_STATUS_IS_OK(status)) {
419 tevent_req_nterror(req, status);
423 * num_data is > 1, we've given 1 as minimum to cli_qpathinfo_send
425 if (state->data[state->num_data-1] != '\0') {
426 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
429 tevent_req_done(req);
432 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
433 char *retpath, size_t len)
436 char *converted = NULL;
437 size_t converted_size = 0;
438 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
440 if (tevent_req_is_nterror(req, &status)) {
443 /* The returned data is a pushed string, not raw data. */
444 if (!convert_string_talloc(state,
445 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
451 return NT_STATUS_NO_MEMORY;
454 len = MIN(len,converted_size);
456 return NT_STATUS_DATA_ERROR;
458 memcpy(retpath, converted, len);
462 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
463 char *linkpath, size_t len)
465 TALLOC_CTX *frame = talloc_stackframe();
466 struct event_context *ev = NULL;
467 struct tevent_req *req = NULL;
468 NTSTATUS status = NT_STATUS_OK;
470 if (cli_has_async_calls(cli)) {
472 * Can't use sync call while an async call is in flight
474 status = NT_STATUS_INVALID_PARAMETER;
478 ev = event_context_init(frame);
480 status = NT_STATUS_NO_MEMORY;
484 req = cli_posix_readlink_send(frame,
490 status = NT_STATUS_NO_MEMORY;
494 if (!tevent_req_poll(req, ev)) {
495 status = map_nt_error_from_unix(errno);
499 status = cli_posix_readlink_recv(req, cli, linkpath, len);
503 if (!NT_STATUS_IS_OK(status)) {
504 cli_set_error(cli, status);
509 /****************************************************************************
510 Hard link a file (UNIX extensions).
511 ****************************************************************************/
513 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
514 struct event_context *ev,
515 struct cli_state *cli,
519 return cli_posix_link_internal_send(
520 mem_ctx, ev, cli, SMB_SET_FILE_UNIX_HLINK, oldname, newname);
523 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
525 return tevent_req_simple_recv_ntstatus(req);
528 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
532 TALLOC_CTX *frame = talloc_stackframe();
533 struct event_context *ev = NULL;
534 struct tevent_req *req = NULL;
535 NTSTATUS status = NT_STATUS_OK;
537 if (cli_has_async_calls(cli)) {
539 * Can't use sync call while an async call is in flight
541 status = NT_STATUS_INVALID_PARAMETER;
545 ev = event_context_init(frame);
547 status = NT_STATUS_NO_MEMORY;
551 req = cli_posix_hardlink_send(frame,
557 status = NT_STATUS_NO_MEMORY;
561 if (!tevent_req_poll(req, ev)) {
562 status = map_nt_error_from_unix(errno);
566 status = cli_posix_hardlink_recv(req);
570 if (!NT_STATUS_IS_OK(status)) {
571 cli_set_error(cli, status);
576 /****************************************************************************
577 Map standard UNIX permissions onto wire representations.
578 ****************************************************************************/
580 uint32_t unix_perms_to_wire(mode_t perms)
582 unsigned int ret = 0;
584 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
585 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
586 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
587 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
588 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
589 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
590 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
591 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
592 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
594 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
597 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
600 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
605 /****************************************************************************
606 Map wire permissions to standard UNIX.
607 ****************************************************************************/
609 mode_t wire_perms_to_unix(uint32_t perms)
611 mode_t ret = (mode_t)0;
613 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
614 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
615 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
616 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
617 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
618 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
619 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
620 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
621 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
623 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
626 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
629 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
634 /****************************************************************************
635 Return the file type from the wire filetype for UNIX extensions.
636 ****************************************************************************/
638 static mode_t unix_filetype_from_wire(uint32_t wire_type)
646 case UNIX_TYPE_SYMLINK:
650 case UNIX_TYPE_CHARDEV:
654 case UNIX_TYPE_BLKDEV:
662 case UNIX_TYPE_SOCKET:
670 /****************************************************************************
671 Do a POSIX getfacl (UNIX extensions).
672 ****************************************************************************/
674 struct getfacl_state {
679 static void cli_posix_getfacl_done(struct tevent_req *subreq);
681 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
682 struct event_context *ev,
683 struct cli_state *cli,
686 struct tevent_req *req = NULL, *subreq = NULL;
687 struct getfacl_state *state = NULL;
689 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
693 subreq = cli_qpathinfo_send(state, ev, cli, fname, SMB_QUERY_POSIX_ACL,
695 if (tevent_req_nomem(subreq, req)) {
696 return tevent_req_post(req, ev);
698 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
702 static void cli_posix_getfacl_done(struct tevent_req *subreq)
704 struct tevent_req *req = tevent_req_callback_data(
705 subreq, struct tevent_req);
706 struct getfacl_state *state = tevent_req_data(
707 req, struct getfacl_state);
710 status = cli_qpathinfo_recv(subreq, state, &state->data,
713 if (!NT_STATUS_IS_OK(status)) {
714 tevent_req_nterror(req, status);
717 tevent_req_done(req);
720 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
725 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
728 if (tevent_req_is_nterror(req, &status)) {
731 *prb_size = (size_t)state->num_data;
732 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
736 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
742 TALLOC_CTX *frame = talloc_stackframe();
743 struct event_context *ev = NULL;
744 struct tevent_req *req = NULL;
745 NTSTATUS status = NT_STATUS_OK;
747 if (cli_has_async_calls(cli)) {
749 * Can't use sync call while an async call is in flight
751 status = NT_STATUS_INVALID_PARAMETER;
755 ev = event_context_init(frame);
757 status = NT_STATUS_NO_MEMORY;
761 req = cli_posix_getfacl_send(frame,
766 status = NT_STATUS_NO_MEMORY;
770 if (!tevent_req_poll(req, ev)) {
771 status = map_nt_error_from_unix(errno);
775 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
779 if (!NT_STATUS_IS_OK(status)) {
780 cli_set_error(cli, status);
785 /****************************************************************************
786 Stat a file (UNIX extensions).
787 ****************************************************************************/
794 static void cli_posix_stat_done(struct tevent_req *subreq);
796 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
797 struct event_context *ev,
798 struct cli_state *cli,
801 struct tevent_req *req = NULL, *subreq = NULL;
802 struct stat_state *state = NULL;
804 req = tevent_req_create(mem_ctx, &state, struct stat_state);
808 subreq = cli_qpathinfo_send(state, ev, cli, fname,
809 SMB_QUERY_FILE_UNIX_BASIC, 100, 100);
810 if (tevent_req_nomem(subreq, req)) {
811 return tevent_req_post(req, ev);
813 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
817 static void cli_posix_stat_done(struct tevent_req *subreq)
819 struct tevent_req *req = tevent_req_callback_data(
820 subreq, struct tevent_req);
821 struct stat_state *state = tevent_req_data(req, struct stat_state);
824 status = cli_qpathinfo_recv(subreq, state, &state->data,
827 if (!NT_STATUS_IS_OK(status)) {
828 tevent_req_nterror(req, status);
831 tevent_req_done(req);
834 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
835 SMB_STRUCT_STAT *sbuf)
837 struct stat_state *state = tevent_req_data(req, struct stat_state);
840 if (tevent_req_is_nterror(req, &status)) {
844 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
845 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
846 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
847 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
849 /* assume 512 byte blocks */
850 sbuf->st_ex_blocks /= 512;
852 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
853 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
854 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
856 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
857 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
858 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
859 #if defined(HAVE_MAKEDEV)
861 uint32_t dev_major = IVAL(state->data,60);
862 uint32_t dev_minor = IVAL(state->data,68);
863 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
866 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
867 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
868 sbuf->st_ex_nlink = BIG_UINT(state->data,92); /* number of hard links */
873 NTSTATUS cli_posix_stat(struct cli_state *cli,
875 SMB_STRUCT_STAT *sbuf)
877 TALLOC_CTX *frame = talloc_stackframe();
878 struct event_context *ev = NULL;
879 struct tevent_req *req = NULL;
880 NTSTATUS status = NT_STATUS_OK;
882 if (cli_has_async_calls(cli)) {
884 * Can't use sync call while an async call is in flight
886 status = NT_STATUS_INVALID_PARAMETER;
890 ev = event_context_init(frame);
892 status = NT_STATUS_NO_MEMORY;
896 req = cli_posix_stat_send(frame,
901 status = NT_STATUS_NO_MEMORY;
905 if (!tevent_req_poll(req, ev)) {
906 status = map_nt_error_from_unix(errno);
910 status = cli_posix_stat_recv(req, sbuf);
914 if (!NT_STATUS_IS_OK(status)) {
915 cli_set_error(cli, status);
920 /****************************************************************************
921 Chmod or chown a file internal (UNIX extensions).
922 ****************************************************************************/
924 struct cli_posix_chown_chmod_internal_state {
928 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq);
930 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
931 struct event_context *ev,
932 struct cli_state *cli,
938 struct tevent_req *req = NULL, *subreq = NULL;
939 struct cli_posix_chown_chmod_internal_state *state = NULL;
941 req = tevent_req_create(mem_ctx, &state,
942 struct cli_posix_chown_chmod_internal_state);
947 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
948 memset(&state->data[40], '\0', 60);
949 SIVAL(state->data,40,uid);
950 SIVAL(state->data,48,gid);
951 SIVAL(state->data,84,mode);
953 subreq = cli_setpathinfo_send(state, ev, cli, SMB_SET_FILE_UNIX_BASIC,
954 fname, state->data, sizeof(state->data));
955 if (tevent_req_nomem(subreq, req)) {
956 return tevent_req_post(req, ev);
958 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done,
963 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
965 NTSTATUS status = cli_setpathinfo_recv(subreq);
966 tevent_req_simple_finish_ntstatus(subreq, status);
969 /****************************************************************************
970 chmod a file (UNIX extensions).
971 ****************************************************************************/
973 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
974 struct event_context *ev,
975 struct cli_state *cli,
979 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
981 unix_perms_to_wire(mode),
986 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
988 return tevent_req_simple_recv_ntstatus(req);
991 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
993 TALLOC_CTX *frame = talloc_stackframe();
994 struct event_context *ev = NULL;
995 struct tevent_req *req = NULL;
996 NTSTATUS status = NT_STATUS_OK;
998 if (cli_has_async_calls(cli)) {
1000 * Can't use sync call while an async call is in flight
1002 status = NT_STATUS_INVALID_PARAMETER;
1006 ev = event_context_init(frame);
1008 status = NT_STATUS_NO_MEMORY;
1012 req = cli_posix_chmod_send(frame,
1018 status = NT_STATUS_NO_MEMORY;
1022 if (!tevent_req_poll(req, ev)) {
1023 status = map_nt_error_from_unix(errno);
1027 status = cli_posix_chmod_recv(req);
1031 if (!NT_STATUS_IS_OK(status)) {
1032 cli_set_error(cli, status);
1037 /****************************************************************************
1038 chown a file (UNIX extensions).
1039 ****************************************************************************/
1041 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1042 struct event_context *ev,
1043 struct cli_state *cli,
1048 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1055 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1057 return tevent_req_simple_recv_ntstatus(req);
1060 NTSTATUS cli_posix_chown(struct cli_state *cli,
1065 TALLOC_CTX *frame = talloc_stackframe();
1066 struct event_context *ev = NULL;
1067 struct tevent_req *req = NULL;
1068 NTSTATUS status = NT_STATUS_OK;
1070 if (cli_has_async_calls(cli)) {
1072 * Can't use sync call while an async call is in flight
1074 status = NT_STATUS_INVALID_PARAMETER;
1078 ev = event_context_init(frame);
1080 status = NT_STATUS_NO_MEMORY;
1084 req = cli_posix_chown_send(frame,
1091 status = NT_STATUS_NO_MEMORY;
1095 if (!tevent_req_poll(req, ev)) {
1096 status = map_nt_error_from_unix(errno);
1100 status = cli_posix_chown_recv(req);
1104 if (!NT_STATUS_IS_OK(status)) {
1105 cli_set_error(cli, status);
1110 /****************************************************************************
1112 ****************************************************************************/
1114 static void cli_rename_done(struct tevent_req *subreq);
1116 struct cli_rename_state {
1120 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1121 struct event_context *ev,
1122 struct cli_state *cli,
1123 const char *fname_src,
1124 const char *fname_dst)
1126 struct tevent_req *req = NULL, *subreq = NULL;
1127 struct cli_rename_state *state = NULL;
1128 uint8_t additional_flags = 0;
1129 uint8_t *bytes = NULL;
1131 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1136 SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
1138 bytes = talloc_array(state, uint8_t, 1);
1139 if (tevent_req_nomem(bytes, req)) {
1140 return tevent_req_post(req, ev);
1143 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1144 strlen(fname_src)+1, NULL);
1145 if (tevent_req_nomem(bytes, req)) {
1146 return tevent_req_post(req, ev);
1149 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1150 talloc_get_size(bytes)+1);
1151 if (tevent_req_nomem(bytes, req)) {
1152 return tevent_req_post(req, ev);
1155 bytes[talloc_get_size(bytes)-1] = 4;
1156 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1157 strlen(fname_dst)+1, NULL);
1158 if (tevent_req_nomem(bytes, req)) {
1159 return tevent_req_post(req, ev);
1162 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1163 1, state->vwv, talloc_get_size(bytes), bytes);
1164 if (tevent_req_nomem(subreq, req)) {
1165 return tevent_req_post(req, ev);
1167 tevent_req_set_callback(subreq, cli_rename_done, req);
1171 static void cli_rename_done(struct tevent_req *subreq)
1173 struct tevent_req *req = tevent_req_callback_data(
1174 subreq, struct tevent_req);
1177 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1178 TALLOC_FREE(subreq);
1179 if (!NT_STATUS_IS_OK(status)) {
1180 tevent_req_nterror(req, status);
1183 tevent_req_done(req);
1186 NTSTATUS cli_rename_recv(struct tevent_req *req)
1188 return tevent_req_simple_recv_ntstatus(req);
1191 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1193 TALLOC_CTX *frame = talloc_stackframe();
1194 struct event_context *ev;
1195 struct tevent_req *req;
1196 NTSTATUS status = NT_STATUS_OK;
1198 if (cli_has_async_calls(cli)) {
1200 * Can't use sync call while an async call is in flight
1202 status = NT_STATUS_INVALID_PARAMETER;
1206 ev = event_context_init(frame);
1208 status = NT_STATUS_NO_MEMORY;
1212 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1214 status = NT_STATUS_NO_MEMORY;
1218 if (!tevent_req_poll(req, ev)) {
1219 status = map_nt_error_from_unix(errno);
1223 status = cli_rename_recv(req);
1227 if (!NT_STATUS_IS_OK(status)) {
1228 cli_set_error(cli, status);
1233 /****************************************************************************
1235 ****************************************************************************/
1237 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1239 struct cli_ntrename_internal_state {
1243 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1244 struct event_context *ev,
1245 struct cli_state *cli,
1246 const char *fname_src,
1247 const char *fname_dst,
1248 uint16_t rename_flag)
1250 struct tevent_req *req = NULL, *subreq = NULL;
1251 struct cli_ntrename_internal_state *state = NULL;
1252 uint8_t additional_flags = 0;
1253 uint8_t *bytes = NULL;
1255 req = tevent_req_create(mem_ctx, &state,
1256 struct cli_ntrename_internal_state);
1261 SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
1262 SSVAL(state->vwv+1, 0, rename_flag);
1264 bytes = talloc_array(state, uint8_t, 1);
1265 if (tevent_req_nomem(bytes, req)) {
1266 return tevent_req_post(req, ev);
1269 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1270 strlen(fname_src)+1, NULL);
1271 if (tevent_req_nomem(bytes, req)) {
1272 return tevent_req_post(req, ev);
1275 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1276 talloc_get_size(bytes)+1);
1277 if (tevent_req_nomem(bytes, req)) {
1278 return tevent_req_post(req, ev);
1281 bytes[talloc_get_size(bytes)-1] = 4;
1282 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1283 strlen(fname_dst)+1, NULL);
1284 if (tevent_req_nomem(bytes, req)) {
1285 return tevent_req_post(req, ev);
1288 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1289 4, state->vwv, talloc_get_size(bytes), bytes);
1290 if (tevent_req_nomem(subreq, req)) {
1291 return tevent_req_post(req, ev);
1293 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1297 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1299 struct tevent_req *req = tevent_req_callback_data(
1300 subreq, struct tevent_req);
1303 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1304 TALLOC_FREE(subreq);
1305 if (!NT_STATUS_IS_OK(status)) {
1306 tevent_req_nterror(req, status);
1309 tevent_req_done(req);
1312 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1314 return tevent_req_simple_recv_ntstatus(req);
1317 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1318 struct event_context *ev,
1319 struct cli_state *cli,
1320 const char *fname_src,
1321 const char *fname_dst)
1323 return cli_ntrename_internal_send(mem_ctx,
1328 RENAME_FLAG_RENAME);
1331 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1333 return cli_ntrename_internal_recv(req);
1336 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1338 TALLOC_CTX *frame = talloc_stackframe();
1339 struct event_context *ev;
1340 struct tevent_req *req;
1341 NTSTATUS status = NT_STATUS_OK;
1343 if (cli_has_async_calls(cli)) {
1345 * Can't use sync call while an async call is in flight
1347 status = NT_STATUS_INVALID_PARAMETER;
1351 ev = event_context_init(frame);
1353 status = NT_STATUS_NO_MEMORY;
1357 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1359 status = NT_STATUS_NO_MEMORY;
1363 if (!tevent_req_poll(req, ev)) {
1364 status = map_nt_error_from_unix(errno);
1368 status = cli_ntrename_recv(req);
1372 if (!NT_STATUS_IS_OK(status)) {
1373 cli_set_error(cli, status);
1378 /****************************************************************************
1380 ****************************************************************************/
1382 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1383 struct event_context *ev,
1384 struct cli_state *cli,
1385 const char *fname_src,
1386 const char *fname_dst)
1388 return cli_ntrename_internal_send(mem_ctx,
1393 RENAME_FLAG_HARD_LINK);
1396 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1398 return cli_ntrename_internal_recv(req);
1401 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1403 TALLOC_CTX *frame = talloc_stackframe();
1404 struct event_context *ev;
1405 struct tevent_req *req;
1406 NTSTATUS status = NT_STATUS_OK;
1408 if (cli_has_async_calls(cli)) {
1410 * Can't use sync call while an async call is in flight
1412 status = NT_STATUS_INVALID_PARAMETER;
1416 ev = event_context_init(frame);
1418 status = NT_STATUS_NO_MEMORY;
1422 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1424 status = NT_STATUS_NO_MEMORY;
1428 if (!tevent_req_poll(req, ev)) {
1429 status = map_nt_error_from_unix(errno);
1433 status = cli_nt_hardlink_recv(req);
1437 if (!NT_STATUS_IS_OK(status)) {
1438 cli_set_error(cli, status);
1443 /****************************************************************************
1445 ****************************************************************************/
1447 static void cli_unlink_done(struct tevent_req *subreq);
1449 struct cli_unlink_state {
1453 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1454 struct event_context *ev,
1455 struct cli_state *cli,
1457 uint16_t mayhave_attrs)
1459 struct tevent_req *req = NULL, *subreq = NULL;
1460 struct cli_unlink_state *state = NULL;
1461 uint8_t additional_flags = 0;
1462 uint8_t *bytes = NULL;
1464 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1469 SSVAL(state->vwv+0, 0, mayhave_attrs);
1471 bytes = talloc_array(state, uint8_t, 1);
1472 if (tevent_req_nomem(bytes, req)) {
1473 return tevent_req_post(req, ev);
1476 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1477 strlen(fname)+1, NULL);
1479 if (tevent_req_nomem(bytes, req)) {
1480 return tevent_req_post(req, ev);
1483 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1484 1, state->vwv, talloc_get_size(bytes), bytes);
1485 if (tevent_req_nomem(subreq, req)) {
1486 return tevent_req_post(req, ev);
1488 tevent_req_set_callback(subreq, cli_unlink_done, req);
1492 static void cli_unlink_done(struct tevent_req *subreq)
1494 struct tevent_req *req = tevent_req_callback_data(
1495 subreq, struct tevent_req);
1498 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1499 TALLOC_FREE(subreq);
1500 if (!NT_STATUS_IS_OK(status)) {
1501 tevent_req_nterror(req, status);
1504 tevent_req_done(req);
1507 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1509 return tevent_req_simple_recv_ntstatus(req);
1512 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1514 TALLOC_CTX *frame = talloc_stackframe();
1515 struct event_context *ev;
1516 struct tevent_req *req;
1517 NTSTATUS status = NT_STATUS_OK;
1519 if (cli_has_async_calls(cli)) {
1521 * Can't use sync call while an async call is in flight
1523 status = NT_STATUS_INVALID_PARAMETER;
1527 ev = event_context_init(frame);
1529 status = NT_STATUS_NO_MEMORY;
1533 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1535 status = NT_STATUS_NO_MEMORY;
1539 if (!tevent_req_poll(req, ev)) {
1540 status = map_nt_error_from_unix(errno);
1544 status = cli_unlink_recv(req);
1548 if (!NT_STATUS_IS_OK(status)) {
1549 cli_set_error(cli, status);
1554 /****************************************************************************
1556 ****************************************************************************/
1558 static void cli_mkdir_done(struct tevent_req *subreq);
1560 struct cli_mkdir_state {
1564 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1565 struct event_context *ev,
1566 struct cli_state *cli,
1569 struct tevent_req *req = NULL, *subreq = NULL;
1570 struct cli_mkdir_state *state = NULL;
1571 uint8_t additional_flags = 0;
1572 uint8_t *bytes = NULL;
1574 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1579 bytes = talloc_array(state, uint8_t, 1);
1580 if (tevent_req_nomem(bytes, req)) {
1581 return tevent_req_post(req, ev);
1584 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1585 strlen(dname)+1, NULL);
1587 if (tevent_req_nomem(bytes, req)) {
1588 return tevent_req_post(req, ev);
1591 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1592 0, NULL, talloc_get_size(bytes), bytes);
1593 if (tevent_req_nomem(subreq, req)) {
1594 return tevent_req_post(req, ev);
1596 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1600 static void cli_mkdir_done(struct tevent_req *subreq)
1602 struct tevent_req *req = tevent_req_callback_data(
1603 subreq, struct tevent_req);
1606 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1607 TALLOC_FREE(subreq);
1608 if (!NT_STATUS_IS_OK(status)) {
1609 tevent_req_nterror(req, status);
1612 tevent_req_done(req);
1615 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1617 return tevent_req_simple_recv_ntstatus(req);
1620 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1622 TALLOC_CTX *frame = talloc_stackframe();
1623 struct event_context *ev;
1624 struct tevent_req *req;
1625 NTSTATUS status = NT_STATUS_OK;
1627 if (cli_has_async_calls(cli)) {
1629 * Can't use sync call while an async call is in flight
1631 status = NT_STATUS_INVALID_PARAMETER;
1635 ev = event_context_init(frame);
1637 status = NT_STATUS_NO_MEMORY;
1641 req = cli_mkdir_send(frame, ev, cli, dname);
1643 status = NT_STATUS_NO_MEMORY;
1647 if (!tevent_req_poll(req, ev)) {
1648 status = map_nt_error_from_unix(errno);
1652 status = cli_mkdir_recv(req);
1656 if (!NT_STATUS_IS_OK(status)) {
1657 cli_set_error(cli, status);
1662 /****************************************************************************
1664 ****************************************************************************/
1666 static void cli_rmdir_done(struct tevent_req *subreq);
1668 struct cli_rmdir_state {
1672 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1673 struct event_context *ev,
1674 struct cli_state *cli,
1677 struct tevent_req *req = NULL, *subreq = NULL;
1678 struct cli_rmdir_state *state = NULL;
1679 uint8_t additional_flags = 0;
1680 uint8_t *bytes = NULL;
1682 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1687 bytes = talloc_array(state, uint8_t, 1);
1688 if (tevent_req_nomem(bytes, req)) {
1689 return tevent_req_post(req, ev);
1692 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1693 strlen(dname)+1, NULL);
1695 if (tevent_req_nomem(bytes, req)) {
1696 return tevent_req_post(req, ev);
1699 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1700 0, NULL, talloc_get_size(bytes), bytes);
1701 if (tevent_req_nomem(subreq, req)) {
1702 return tevent_req_post(req, ev);
1704 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1708 static void cli_rmdir_done(struct tevent_req *subreq)
1710 struct tevent_req *req = tevent_req_callback_data(
1711 subreq, struct tevent_req);
1714 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
1715 TALLOC_FREE(subreq);
1716 if (!NT_STATUS_IS_OK(status)) {
1717 tevent_req_nterror(req, status);
1720 tevent_req_done(req);
1723 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1725 return tevent_req_simple_recv_ntstatus(req);
1728 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1730 TALLOC_CTX *frame = talloc_stackframe();
1731 struct event_context *ev;
1732 struct tevent_req *req;
1733 NTSTATUS status = NT_STATUS_OK;
1735 if (cli_has_async_calls(cli)) {
1737 * Can't use sync call while an async call is in flight
1739 status = NT_STATUS_INVALID_PARAMETER;
1743 ev = event_context_init(frame);
1745 status = NT_STATUS_NO_MEMORY;
1749 req = cli_rmdir_send(frame, ev, cli, dname);
1751 status = NT_STATUS_NO_MEMORY;
1755 if (!tevent_req_poll(req, ev)) {
1756 status = map_nt_error_from_unix(errno);
1760 status = cli_rmdir_recv(req);
1764 if (!NT_STATUS_IS_OK(status)) {
1765 cli_set_error(cli, status);
1770 /****************************************************************************
1771 Set or clear the delete on close flag.
1772 ****************************************************************************/
1780 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1782 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
1783 NULL, 0, NULL, NULL, 0, NULL);
1784 tevent_req_simple_finish_ntstatus(subreq, status);
1787 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1788 struct event_context *ev,
1789 struct cli_state *cli,
1793 struct tevent_req *req = NULL, *subreq = NULL;
1794 struct doc_state *state = NULL;
1796 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1801 /* Setup setup word. */
1802 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1804 /* Setup param array. */
1805 SSVAL(state->param,0,fnum);
1806 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1808 /* Setup data array. */
1809 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1811 subreq = cli_trans_send(state, /* mem ctx. */
1812 ev, /* event ctx. */
1813 cli, /* cli_state. */
1814 SMBtrans2, /* cmd. */
1815 NULL, /* pipe name. */
1819 &state->setup, /* setup. */
1820 1, /* num setup uint16_t words. */
1821 0, /* max returned setup. */
1822 state->param, /* param. */
1824 2, /* max returned param. */
1825 state->data, /* data. */
1827 0); /* max returned data. */
1829 if (tevent_req_nomem(subreq, req)) {
1830 return tevent_req_post(req, ev);
1832 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1836 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1838 return tevent_req_simple_recv_ntstatus(req);
1841 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1843 TALLOC_CTX *frame = talloc_stackframe();
1844 struct event_context *ev = NULL;
1845 struct tevent_req *req = NULL;
1846 NTSTATUS status = NT_STATUS_OK;
1848 if (cli_has_async_calls(cli)) {
1850 * Can't use sync call while an async call is in flight
1852 status = NT_STATUS_INVALID_PARAMETER;
1856 ev = event_context_init(frame);
1858 status = NT_STATUS_NO_MEMORY;
1862 req = cli_nt_delete_on_close_send(frame,
1868 status = NT_STATUS_NO_MEMORY;
1872 if (!tevent_req_poll(req, ev)) {
1873 status = map_nt_error_from_unix(errno);
1877 status = cli_nt_delete_on_close_recv(req);
1881 if (!NT_STATUS_IS_OK(status)) {
1882 cli_set_error(cli, status);
1887 struct cli_ntcreate_state {
1892 static void cli_ntcreate_done(struct tevent_req *subreq);
1894 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1895 struct event_context *ev,
1896 struct cli_state *cli,
1898 uint32_t CreatFlags,
1899 uint32_t DesiredAccess,
1900 uint32_t FileAttributes,
1901 uint32_t ShareAccess,
1902 uint32_t CreateDisposition,
1903 uint32_t CreateOptions,
1904 uint8_t SecurityFlags)
1906 struct tevent_req *req, *subreq;
1907 struct cli_ntcreate_state *state;
1910 size_t converted_len;
1912 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1919 SCVAL(vwv+0, 0, 0xFF);
1924 if (cli->use_oplocks) {
1925 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1927 SIVAL(vwv+3, 1, CreatFlags);
1928 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1929 SIVAL(vwv+7, 1, DesiredAccess);
1930 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1931 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1932 SIVAL(vwv+13, 1, FileAttributes);
1933 SIVAL(vwv+15, 1, ShareAccess);
1934 SIVAL(vwv+17, 1, CreateDisposition);
1935 SIVAL(vwv+19, 1, CreateOptions);
1936 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1937 SCVAL(vwv+23, 1, SecurityFlags);
1939 bytes = talloc_array(state, uint8_t, 0);
1940 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1941 fname, strlen(fname)+1,
1944 /* sigh. this copes with broken netapp filer behaviour */
1945 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1947 if (tevent_req_nomem(bytes, req)) {
1948 return tevent_req_post(req, ev);
1951 SSVAL(vwv+2, 1, converted_len);
1953 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1954 talloc_get_size(bytes), bytes);
1955 if (tevent_req_nomem(subreq, req)) {
1956 return tevent_req_post(req, ev);
1958 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1962 static void cli_ntcreate_done(struct tevent_req *subreq)
1964 struct tevent_req *req = tevent_req_callback_data(
1965 subreq, struct tevent_req);
1966 struct cli_ntcreate_state *state = tevent_req_data(
1967 req, struct cli_ntcreate_state);
1975 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv,
1976 &num_bytes, &bytes);
1977 TALLOC_FREE(subreq);
1978 if (!NT_STATUS_IS_OK(status)) {
1979 tevent_req_nterror(req, status);
1982 state->fnum = SVAL(vwv+2, 1);
1983 tevent_req_done(req);
1986 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1988 struct cli_ntcreate_state *state = tevent_req_data(
1989 req, struct cli_ntcreate_state);
1992 if (tevent_req_is_nterror(req, &status)) {
1995 *pfnum = state->fnum;
1996 return NT_STATUS_OK;
1999 NTSTATUS cli_ntcreate(struct cli_state *cli,
2001 uint32_t CreatFlags,
2002 uint32_t DesiredAccess,
2003 uint32_t FileAttributes,
2004 uint32_t ShareAccess,
2005 uint32_t CreateDisposition,
2006 uint32_t CreateOptions,
2007 uint8_t SecurityFlags,
2010 TALLOC_CTX *frame = talloc_stackframe();
2011 struct event_context *ev;
2012 struct tevent_req *req;
2013 NTSTATUS status = NT_STATUS_OK;
2015 if (cli_has_async_calls(cli)) {
2017 * Can't use sync call while an async call is in flight
2019 status = NT_STATUS_INVALID_PARAMETER;
2023 ev = event_context_init(frame);
2025 status = NT_STATUS_NO_MEMORY;
2029 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2030 DesiredAccess, FileAttributes, ShareAccess,
2031 CreateDisposition, CreateOptions,
2034 status = NT_STATUS_NO_MEMORY;
2038 if (!tevent_req_poll(req, ev)) {
2039 status = map_nt_error_from_unix(errno);
2043 status = cli_ntcreate_recv(req, pfid);
2046 if (!NT_STATUS_IS_OK(status)) {
2047 cli_set_error(cli, status);
2052 /****************************************************************************
2054 WARNING: if you open with O_WRONLY then getattrE won't work!
2055 ****************************************************************************/
2057 struct cli_open_state {
2063 static void cli_open_done(struct tevent_req *subreq);
2065 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2066 struct event_context *ev,
2067 struct cli_state *cli, const char *fname,
2068 int flags, int share_mode,
2069 struct tevent_req **psmbreq)
2071 struct tevent_req *req, *subreq;
2072 struct cli_open_state *state;
2074 unsigned accessmode;
2075 uint8_t additional_flags;
2078 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2084 if (flags & O_CREAT) {
2087 if (!(flags & O_EXCL)) {
2088 if (flags & O_TRUNC)
2094 accessmode = (share_mode<<4);
2096 if ((flags & O_ACCMODE) == O_RDWR) {
2098 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2103 if ((flags & O_SYNC) == O_SYNC) {
2104 accessmode |= (1<<14);
2108 if (share_mode == DENY_FCB) {
2112 SCVAL(state->vwv + 0, 0, 0xFF);
2113 SCVAL(state->vwv + 0, 1, 0);
2114 SSVAL(state->vwv + 1, 0, 0);
2115 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2116 SSVAL(state->vwv + 3, 0, accessmode);
2117 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
2118 SSVAL(state->vwv + 5, 0, 0);
2119 SIVAL(state->vwv + 6, 0, 0);
2120 SSVAL(state->vwv + 8, 0, openfn);
2121 SIVAL(state->vwv + 9, 0, 0);
2122 SIVAL(state->vwv + 11, 0, 0);
2123 SIVAL(state->vwv + 13, 0, 0);
2125 additional_flags = 0;
2127 if (cli->use_oplocks) {
2128 /* if using oplocks then ask for a batch oplock via
2129 core and extended methods */
2131 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2132 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2135 bytes = talloc_array(state, uint8_t, 0);
2136 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2137 strlen(fname)+1, NULL);
2139 if (tevent_req_nomem(bytes, req)) {
2140 return tevent_req_post(req, ev);
2143 state->bytes.iov_base = (void *)bytes;
2144 state->bytes.iov_len = talloc_get_size(bytes);
2146 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2147 15, state->vwv, 1, &state->bytes);
2148 if (subreq == NULL) {
2152 tevent_req_set_callback(subreq, cli_open_done, req);
2157 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2158 struct cli_state *cli, const char *fname,
2159 int flags, int share_mode)
2161 struct tevent_req *req, *subreq;
2164 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2170 status = cli_smb_req_send(subreq);
2171 if (!NT_STATUS_IS_OK(status)) {
2172 tevent_req_nterror(req, status);
2173 return tevent_req_post(req, ev);
2178 static void cli_open_done(struct tevent_req *subreq)
2180 struct tevent_req *req = tevent_req_callback_data(
2181 subreq, struct tevent_req);
2182 struct cli_open_state *state = tevent_req_data(
2183 req, struct cli_open_state);
2189 status = cli_smb_recv(subreq, state, &inbuf, 3, &wct, &vwv, NULL,
2191 TALLOC_FREE(subreq);
2192 if (!NT_STATUS_IS_OK(status)) {
2193 tevent_req_nterror(req, status);
2196 state->fnum = SVAL(vwv+2, 0);
2197 tevent_req_done(req);
2200 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2202 struct cli_open_state *state = tevent_req_data(
2203 req, struct cli_open_state);
2206 if (tevent_req_is_nterror(req, &status)) {
2209 *pfnum = state->fnum;
2210 return NT_STATUS_OK;
2213 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2214 int share_mode, uint16_t *pfnum)
2216 TALLOC_CTX *frame = talloc_stackframe();
2217 struct event_context *ev;
2218 struct tevent_req *req;
2219 NTSTATUS status = NT_STATUS_OK;
2221 if (cli_has_async_calls(cli)) {
2223 * Can't use sync call while an async call is in flight
2225 status = NT_STATUS_INVALID_PARAMETER;
2229 ev = event_context_init(frame);
2231 status = NT_STATUS_NO_MEMORY;
2235 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2237 status = NT_STATUS_NO_MEMORY;
2241 if (!tevent_req_poll(req, ev)) {
2242 status = map_nt_error_from_unix(errno);
2246 status = cli_open_recv(req, pfnum);
2249 if (!NT_STATUS_IS_OK(status)) {
2250 cli_set_error(cli, status);
2255 /****************************************************************************
2257 ****************************************************************************/
2259 struct cli_close_state {
2263 static void cli_close_done(struct tevent_req *subreq);
2265 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2266 struct event_context *ev,
2267 struct cli_state *cli,
2269 struct tevent_req **psubreq)
2271 struct tevent_req *req, *subreq;
2272 struct cli_close_state *state;
2274 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2279 SSVAL(state->vwv+0, 0, fnum);
2280 SIVALS(state->vwv+1, 0, -1);
2282 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2284 if (subreq == NULL) {
2288 tevent_req_set_callback(subreq, cli_close_done, req);
2293 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2294 struct event_context *ev,
2295 struct cli_state *cli,
2298 struct tevent_req *req, *subreq;
2301 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2306 status = cli_smb_req_send(subreq);
2307 if (!NT_STATUS_IS_OK(status)) {
2308 tevent_req_nterror(req, status);
2309 return tevent_req_post(req, ev);
2314 static void cli_close_done(struct tevent_req *subreq)
2316 struct tevent_req *req = tevent_req_callback_data(
2317 subreq, struct tevent_req);
2320 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2321 TALLOC_FREE(subreq);
2322 if (!NT_STATUS_IS_OK(status)) {
2323 tevent_req_nterror(req, status);
2326 tevent_req_done(req);
2329 NTSTATUS cli_close_recv(struct tevent_req *req)
2331 return tevent_req_simple_recv_ntstatus(req);
2334 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2336 TALLOC_CTX *frame = talloc_stackframe();
2337 struct event_context *ev;
2338 struct tevent_req *req;
2339 NTSTATUS status = NT_STATUS_OK;
2341 if (cli_has_async_calls(cli)) {
2343 * Can't use sync call while an async call is in flight
2345 status = NT_STATUS_INVALID_PARAMETER;
2349 ev = event_context_init(frame);
2351 status = NT_STATUS_NO_MEMORY;
2355 req = cli_close_send(frame, ev, cli, fnum);
2357 status = NT_STATUS_NO_MEMORY;
2361 if (!tevent_req_poll(req, ev)) {
2362 status = map_nt_error_from_unix(errno);
2366 status = cli_close_recv(req);
2369 if (!NT_STATUS_IS_OK(status)) {
2370 cli_set_error(cli, status);
2375 /****************************************************************************
2376 Truncate a file to a specified size
2377 ****************************************************************************/
2379 struct ftrunc_state {
2385 static void cli_ftruncate_done(struct tevent_req *subreq)
2387 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2388 NULL, 0, NULL, NULL, 0, NULL);
2389 tevent_req_simple_finish_ntstatus(subreq, status);
2392 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2393 struct event_context *ev,
2394 struct cli_state *cli,
2398 struct tevent_req *req = NULL, *subreq = NULL;
2399 struct ftrunc_state *state = NULL;
2401 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2406 /* Setup setup word. */
2407 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2409 /* Setup param array. */
2410 SSVAL(state->param,0,fnum);
2411 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2412 SSVAL(state->param,4,0);
2414 /* Setup data array. */
2415 SBVAL(state->data, 0, size);
2417 subreq = cli_trans_send(state, /* mem ctx. */
2418 ev, /* event ctx. */
2419 cli, /* cli_state. */
2420 SMBtrans2, /* cmd. */
2421 NULL, /* pipe name. */
2425 &state->setup, /* setup. */
2426 1, /* num setup uint16_t words. */
2427 0, /* max returned setup. */
2428 state->param, /* param. */
2430 2, /* max returned param. */
2431 state->data, /* data. */
2433 0); /* max returned data. */
2435 if (tevent_req_nomem(subreq, req)) {
2436 return tevent_req_post(req, ev);
2438 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2442 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2444 return tevent_req_simple_recv_ntstatus(req);
2447 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2449 TALLOC_CTX *frame = talloc_stackframe();
2450 struct event_context *ev = NULL;
2451 struct tevent_req *req = NULL;
2452 NTSTATUS status = NT_STATUS_OK;
2454 if (cli_has_async_calls(cli)) {
2456 * Can't use sync call while an async call is in flight
2458 status = NT_STATUS_INVALID_PARAMETER;
2462 ev = event_context_init(frame);
2464 status = NT_STATUS_NO_MEMORY;
2468 req = cli_ftruncate_send(frame,
2474 status = NT_STATUS_NO_MEMORY;
2478 if (!tevent_req_poll(req, ev)) {
2479 status = map_nt_error_from_unix(errno);
2483 status = cli_ftruncate_recv(req);
2487 if (!NT_STATUS_IS_OK(status)) {
2488 cli_set_error(cli, status);
2493 /****************************************************************************
2494 send a lock with a specified locktype
2495 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2496 ****************************************************************************/
2498 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2499 uint32_t offset, uint32_t len,
2500 int timeout, unsigned char locktype)
2507 SCVAL(vwv + 0, 0, 0xff);
2508 SCVAL(vwv + 0, 1, 0);
2509 SSVAL(vwv + 1, 0, 0);
2510 SSVAL(vwv + 2, 0, fnum);
2511 SCVAL(vwv + 3, 0, locktype);
2512 SCVAL(vwv + 3, 1, 0);
2513 SIVALS(vwv + 4, 0, timeout);
2514 SSVAL(vwv + 6, 0, 0);
2515 SSVAL(vwv + 7, 0, 1);
2517 SSVAL(bytes, 0, cli->pid);
2518 SIVAL(bytes, 2, offset);
2519 SIVAL(bytes, 6, len);
2521 saved_timeout = cli->timeout;
2524 cli->timeout = (timeout == -1)
2525 ? 0x7FFFFFFF : (timeout + 2*1000);
2528 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2529 10, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2531 cli->timeout = saved_timeout;
2536 /****************************************************************************
2538 note that timeout is in units of 2 milliseconds
2539 ****************************************************************************/
2541 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2542 uint32_t offset, uint32_t len, int timeout,
2543 enum brl_type lock_type)
2547 status = cli_locktype(cli, fnum, offset, len, timeout,
2548 (lock_type == READ_LOCK? 1 : 0));
2549 cli_set_error(cli, status);
2550 return NT_STATUS_IS_OK(status);
2553 /****************************************************************************
2555 ****************************************************************************/
2557 struct cli_unlock_state {
2562 static void cli_unlock_done(struct tevent_req *subreq);
2564 struct tevent_req *cli_unlock_send(TALLOC_CTX *mem_ctx,
2565 struct event_context *ev,
2566 struct cli_state *cli,
2572 struct tevent_req *req = NULL, *subreq = NULL;
2573 struct cli_unlock_state *state = NULL;
2574 uint8_t additional_flags = 0;
2576 req = tevent_req_create(mem_ctx, &state, struct cli_unlock_state);
2581 SCVAL(state->vwv+0, 0, 0xFF);
2582 SSVAL(state->vwv+2, 0, fnum);
2583 SCVAL(state->vwv+3, 0, 0);
2584 SIVALS(state->vwv+4, 0, 0);
2585 SSVAL(state->vwv+6, 0, 1);
2586 SSVAL(state->vwv+7, 0, 0);
2588 SSVAL(state->data, 0, cli->pid);
2589 SIVAL(state->data, 2, offset);
2590 SIVAL(state->data, 6, len);
2592 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2593 8, state->vwv, 10, state->data);
2594 if (tevent_req_nomem(subreq, req)) {
2595 return tevent_req_post(req, ev);
2597 tevent_req_set_callback(subreq, cli_unlock_done, req);
2601 static void cli_unlock_done(struct tevent_req *subreq)
2603 struct tevent_req *req = tevent_req_callback_data(
2604 subreq, struct tevent_req);
2607 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2608 TALLOC_FREE(subreq);
2609 if (!NT_STATUS_IS_OK(status)) {
2610 tevent_req_nterror(req, status);
2613 tevent_req_done(req);
2616 NTSTATUS cli_unlock_recv(struct tevent_req *req)
2618 return tevent_req_simple_recv_ntstatus(req);
2621 NTSTATUS cli_unlock(struct cli_state *cli,
2626 TALLOC_CTX *frame = talloc_stackframe();
2627 struct event_context *ev;
2628 struct tevent_req *req;
2629 NTSTATUS status = NT_STATUS_OK;
2631 if (cli_has_async_calls(cli)) {
2633 * Can't use sync call while an async call is in flight
2635 status = NT_STATUS_INVALID_PARAMETER;
2639 ev = event_context_init(frame);
2641 status = NT_STATUS_NO_MEMORY;
2645 req = cli_unlock_send(frame, ev, cli,
2648 status = NT_STATUS_NO_MEMORY;
2652 if (!tevent_req_poll(req, ev)) {
2653 status = map_nt_error_from_unix(errno);
2657 status = cli_unlock_recv(req);
2661 if (!NT_STATUS_IS_OK(status)) {
2662 cli_set_error(cli, status);
2667 /****************************************************************************
2668 Lock a file with 64 bit offsets.
2669 ****************************************************************************/
2671 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2672 uint64_t offset, uint64_t len, int timeout,
2673 enum brl_type lock_type)
2677 int saved_timeout = cli->timeout;
2681 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2682 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2685 ltype = (lock_type == READ_LOCK? 1 : 0);
2686 ltype |= LOCKING_ANDX_LARGE_FILES;
2688 SCVAL(vwv + 0, 0, 0xff);
2689 SCVAL(vwv + 0, 1, 0);
2690 SSVAL(vwv + 1, 0, 0);
2691 SSVAL(vwv + 2, 0, fnum);
2692 SCVAL(vwv + 3, 0, ltype);
2693 SCVAL(vwv + 3, 1, 0);
2694 SIVALS(vwv + 4, 0, timeout);
2695 SSVAL(vwv + 6, 0, 0);
2696 SSVAL(vwv + 7, 0, 1);
2698 SIVAL(bytes, 0, cli->pid);
2699 SOFF_T_R(bytes, 4, offset);
2700 SOFF_T_R(bytes, 12, len);
2702 saved_timeout = cli->timeout;
2705 cli->timeout = (timeout == -1)
2706 ? 0x7FFFFFFF : (timeout + 2*1000);
2709 status = cli_smb(talloc_tos(), cli, SMBlockingX, 0, 8, vwv,
2710 20, bytes, NULL, 0, NULL, NULL, NULL, NULL);
2712 cli->timeout = saved_timeout;
2714 cli_set_error(cli, status);
2715 return NT_STATUS_IS_OK(status);
2718 /****************************************************************************
2719 Unlock a file with 64 bit offsets.
2720 ****************************************************************************/
2722 struct cli_unlock64_state {
2727 static void cli_unlock64_done(struct tevent_req *subreq);
2729 struct tevent_req *cli_unlock64_send(TALLOC_CTX *mem_ctx,
2730 struct event_context *ev,
2731 struct cli_state *cli,
2737 struct tevent_req *req = NULL, *subreq = NULL;
2738 struct cli_unlock64_state *state = NULL;
2739 uint8_t additional_flags = 0;
2741 req = tevent_req_create(mem_ctx, &state, struct cli_unlock64_state);
2746 SCVAL(state->vwv+0, 0, 0xff);
2747 SSVAL(state->vwv+2, 0, fnum);
2748 SCVAL(state->vwv+3, 0,LOCKING_ANDX_LARGE_FILES);
2749 SIVALS(state->vwv+4, 0, 0);
2750 SSVAL(state->vwv+6, 0, 1);
2751 SSVAL(state->vwv+7, 0, 0);
2753 SIVAL(state->data, 0, cli->pid);
2754 SOFF_T_R(state->data, 4, offset);
2755 SOFF_T_R(state->data, 12, len);
2757 subreq = cli_smb_send(state, ev, cli, SMBlockingX, additional_flags,
2758 8, state->vwv, 20, state->data);
2759 if (tevent_req_nomem(subreq, req)) {
2760 return tevent_req_post(req, ev);
2762 tevent_req_set_callback(subreq, cli_unlock64_done, req);
2766 static void cli_unlock64_done(struct tevent_req *subreq)
2768 struct tevent_req *req = tevent_req_callback_data(
2769 subreq, struct tevent_req);
2772 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
2773 TALLOC_FREE(subreq);
2774 if (!NT_STATUS_IS_OK(status)) {
2775 tevent_req_nterror(req, status);
2778 tevent_req_done(req);
2781 NTSTATUS cli_unlock64_recv(struct tevent_req *req)
2783 return tevent_req_simple_recv_ntstatus(req);
2786 NTSTATUS cli_unlock64(struct cli_state *cli,
2791 TALLOC_CTX *frame = talloc_stackframe();
2792 struct event_context *ev;
2793 struct tevent_req *req;
2794 NTSTATUS status = NT_STATUS_OK;
2796 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2797 return cli_unlock(cli, fnum, offset, len);
2800 if (cli_has_async_calls(cli)) {
2802 * Can't use sync call while an async call is in flight
2804 status = NT_STATUS_INVALID_PARAMETER;
2808 ev = event_context_init(frame);
2810 status = NT_STATUS_NO_MEMORY;
2814 req = cli_unlock64_send(frame, ev, cli,
2817 status = NT_STATUS_NO_MEMORY;
2821 if (!tevent_req_poll(req, ev)) {
2822 status = map_nt_error_from_unix(errno);
2826 status = cli_unlock64_recv(req);
2830 if (!NT_STATUS_IS_OK(status)) {
2831 cli_set_error(cli, status);
2836 /****************************************************************************
2837 Get/unlock a POSIX lock on a file - internal function.
2838 ****************************************************************************/
2840 struct posix_lock_state {
2843 uint8_t data[POSIX_LOCK_DATA_SIZE];
2846 static void cli_posix_unlock_internal_done(struct tevent_req *subreq)
2848 NTSTATUS status = cli_trans_recv(subreq, NULL, NULL, NULL, 0, NULL,
2849 NULL, 0, NULL, NULL, 0, NULL);
2850 tevent_req_simple_finish_ntstatus(subreq, status);
2853 static struct tevent_req *cli_posix_lock_internal_send(TALLOC_CTX *mem_ctx,
2854 struct event_context *ev,
2855 struct cli_state *cli,
2860 enum brl_type lock_type)
2862 struct tevent_req *req = NULL, *subreq = NULL;
2863 struct posix_lock_state *state = NULL;
2865 req = tevent_req_create(mem_ctx, &state, struct posix_lock_state);
2870 /* Setup setup word. */
2871 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2873 /* Setup param array. */
2874 SSVAL(&state->param, 0, fnum);
2875 SSVAL(&state->param, 2, SMB_SET_POSIX_LOCK);
2877 /* Setup data array. */
2878 switch (lock_type) {
2880 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2881 POSIX_LOCK_TYPE_READ);
2884 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2885 POSIX_LOCK_TYPE_WRITE);
2888 SSVAL(&state->data, POSIX_LOCK_TYPE_OFFSET,
2889 POSIX_LOCK_TYPE_UNLOCK);
2896 SSVAL(&state->data, POSIX_LOCK_FLAGS_OFFSET,
2897 POSIX_LOCK_FLAG_WAIT);
2899 SSVAL(state->data, POSIX_LOCK_FLAGS_OFFSET,
2900 POSIX_LOCK_FLAG_NOWAIT);
2903 SIVAL(&state->data, POSIX_LOCK_PID_OFFSET, cli->pid);
2904 SOFF_T(&state->data, POSIX_LOCK_START_OFFSET, offset);
2905 SOFF_T(&state->data, POSIX_LOCK_LEN_OFFSET, len);
2907 subreq = cli_trans_send(state, /* mem ctx. */
2908 ev, /* event ctx. */
2909 cli, /* cli_state. */
2910 SMBtrans2, /* cmd. */
2911 NULL, /* pipe name. */
2915 &state->setup, /* setup. */
2916 1, /* num setup uint16_t words. */
2917 0, /* max returned setup. */
2918 state->param, /* param. */
2920 2, /* max returned param. */
2921 state->data, /* data. */
2922 POSIX_LOCK_DATA_SIZE, /* num data. */
2923 0); /* max returned data. */
2925 if (tevent_req_nomem(subreq, req)) {
2926 return tevent_req_post(req, ev);
2928 tevent_req_set_callback(subreq, cli_posix_unlock_internal_done, req);
2932 /****************************************************************************
2934 ****************************************************************************/
2936 struct tevent_req *cli_posix_lock_send(TALLOC_CTX *mem_ctx,
2937 struct event_context *ev,
2938 struct cli_state *cli,
2943 enum brl_type lock_type)
2945 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
2946 wait_lock, lock_type);
2949 NTSTATUS cli_posix_lock_recv(struct tevent_req *req)
2951 return tevent_req_simple_recv_ntstatus(req);
2954 NTSTATUS cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2955 uint64_t offset, uint64_t len,
2956 bool wait_lock, enum brl_type lock_type)
2958 TALLOC_CTX *frame = talloc_stackframe();
2959 struct event_context *ev = NULL;
2960 struct tevent_req *req = NULL;
2961 NTSTATUS status = NT_STATUS_OK;
2963 if (cli_has_async_calls(cli)) {
2965 * Can't use sync call while an async call is in flight
2967 status = NT_STATUS_INVALID_PARAMETER;
2971 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2972 status = NT_STATUS_INVALID_PARAMETER;
2976 ev = event_context_init(frame);
2978 status = NT_STATUS_NO_MEMORY;
2982 req = cli_posix_lock_send(frame,
2991 status = NT_STATUS_NO_MEMORY;
2995 if (!tevent_req_poll(req, ev)) {
2996 status = map_nt_error_from_unix(errno);
3000 status = cli_posix_lock_recv(req);
3004 if (!NT_STATUS_IS_OK(status)) {
3005 cli_set_error(cli, status);
3010 /****************************************************************************
3011 POSIX Unlock a file.
3012 ****************************************************************************/
3014 struct tevent_req *cli_posix_unlock_send(TALLOC_CTX *mem_ctx,
3015 struct event_context *ev,
3016 struct cli_state *cli,
3021 return cli_posix_lock_internal_send(mem_ctx, ev, cli, fnum, offset, len,
3022 false, UNLOCK_LOCK);
3025 NTSTATUS cli_posix_unlock_recv(struct tevent_req *req)
3027 return tevent_req_simple_recv_ntstatus(req);
3030 NTSTATUS cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
3032 TALLOC_CTX *frame = talloc_stackframe();
3033 struct event_context *ev = NULL;
3034 struct tevent_req *req = NULL;
3035 NTSTATUS status = NT_STATUS_OK;
3037 if (cli_has_async_calls(cli)) {
3039 * Can't use sync call while an async call is in flight
3041 status = NT_STATUS_INVALID_PARAMETER;
3045 ev = event_context_init(frame);
3047 status = NT_STATUS_NO_MEMORY;
3051 req = cli_posix_unlock_send(frame,
3058 status = NT_STATUS_NO_MEMORY;
3062 if (!tevent_req_poll(req, ev)) {
3063 status = map_nt_error_from_unix(errno);
3067 status = cli_posix_unlock_recv(req);
3071 if (!NT_STATUS_IS_OK(status)) {
3072 cli_set_error(cli, status);
3077 /****************************************************************************
3078 Do a SMBgetattrE call.
3079 ****************************************************************************/
3081 static void cli_getattrE_done(struct tevent_req *subreq);
3083 struct cli_getattrE_state {
3093 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
3094 struct event_context *ev,
3095 struct cli_state *cli,
3098 struct tevent_req *req = NULL, *subreq = NULL;
3099 struct cli_getattrE_state *state = NULL;
3100 uint8_t additional_flags = 0;
3102 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
3107 state->zone_offset = cli->serverzone;
3108 SSVAL(state->vwv+0,0,fnum);
3110 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
3111 1, state->vwv, 0, NULL);
3112 if (tevent_req_nomem(subreq, req)) {
3113 return tevent_req_post(req, ev);
3115 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3119 static void cli_getattrE_done(struct tevent_req *subreq)
3121 struct tevent_req *req = tevent_req_callback_data(
3122 subreq, struct tevent_req);
3123 struct cli_getattrE_state *state = tevent_req_data(
3124 req, struct cli_getattrE_state);
3126 uint16_t *vwv = NULL;
3130 status = cli_smb_recv(subreq, state, &inbuf, 11, &wct, &vwv,
3132 TALLOC_FREE(subreq);
3133 if (!NT_STATUS_IS_OK(status)) {
3134 tevent_req_nterror(req, status);
3138 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3139 state->attr = SVAL(vwv+10,0);
3140 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3141 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3142 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3144 tevent_req_done(req);
3147 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3150 time_t *change_time,
3151 time_t *access_time,
3154 struct cli_getattrE_state *state = tevent_req_data(
3155 req, struct cli_getattrE_state);
3158 if (tevent_req_is_nterror(req, &status)) {
3162 *attr = state->attr;
3165 *size = state->size;
3168 *change_time = state->change_time;
3171 *access_time = state->access_time;
3174 *write_time = state->write_time;
3176 return NT_STATUS_OK;
3179 NTSTATUS cli_getattrE(struct cli_state *cli,
3183 time_t *change_time,
3184 time_t *access_time,
3187 TALLOC_CTX *frame = talloc_stackframe();
3188 struct event_context *ev = NULL;
3189 struct tevent_req *req = NULL;
3190 NTSTATUS status = NT_STATUS_OK;
3192 if (cli_has_async_calls(cli)) {
3194 * Can't use sync call while an async call is in flight
3196 status = NT_STATUS_INVALID_PARAMETER;
3200 ev = event_context_init(frame);
3202 status = NT_STATUS_NO_MEMORY;
3206 req = cli_getattrE_send(frame, ev, cli, fnum);
3208 status = NT_STATUS_NO_MEMORY;
3212 if (!tevent_req_poll(req, ev)) {
3213 status = map_nt_error_from_unix(errno);
3217 status = cli_getattrE_recv(req,
3226 if (!NT_STATUS_IS_OK(status)) {
3227 cli_set_error(cli, status);
3232 /****************************************************************************
3234 ****************************************************************************/
3236 static void cli_getatr_done(struct tevent_req *subreq);
3238 struct cli_getatr_state {
3245 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3246 struct event_context *ev,
3247 struct cli_state *cli,
3250 struct tevent_req *req = NULL, *subreq = NULL;
3251 struct cli_getatr_state *state = NULL;
3252 uint8_t additional_flags = 0;
3253 uint8_t *bytes = NULL;
3255 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3260 state->zone_offset = cli->serverzone;
3262 bytes = talloc_array(state, uint8_t, 1);
3263 if (tevent_req_nomem(bytes, req)) {
3264 return tevent_req_post(req, ev);
3267 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3268 strlen(fname)+1, NULL);
3270 if (tevent_req_nomem(bytes, req)) {
3271 return tevent_req_post(req, ev);
3274 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3275 0, NULL, talloc_get_size(bytes), bytes);
3276 if (tevent_req_nomem(subreq, req)) {
3277 return tevent_req_post(req, ev);
3279 tevent_req_set_callback(subreq, cli_getatr_done, req);
3283 static void cli_getatr_done(struct tevent_req *subreq)
3285 struct tevent_req *req = tevent_req_callback_data(
3286 subreq, struct tevent_req);
3287 struct cli_getatr_state *state = tevent_req_data(
3288 req, struct cli_getatr_state);
3290 uint16_t *vwv = NULL;
3294 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3296 TALLOC_FREE(subreq);
3297 if (!NT_STATUS_IS_OK(status)) {
3298 tevent_req_nterror(req, status);
3302 state->attr = SVAL(vwv+0,0);
3303 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3304 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3306 tevent_req_done(req);
3309 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3314 struct cli_getatr_state *state = tevent_req_data(
3315 req, struct cli_getatr_state);
3318 if (tevent_req_is_nterror(req, &status)) {
3322 *attr = state->attr;
3325 *size = state->size;
3328 *write_time = state->write_time;
3330 return NT_STATUS_OK;
3333 NTSTATUS cli_getatr(struct cli_state *cli,
3339 TALLOC_CTX *frame = talloc_stackframe();
3340 struct event_context *ev = NULL;
3341 struct tevent_req *req = NULL;
3342 NTSTATUS status = NT_STATUS_OK;
3344 if (cli_has_async_calls(cli)) {
3346 * Can't use sync call while an async call is in flight
3348 status = NT_STATUS_INVALID_PARAMETER;
3352 ev = event_context_init(frame);
3354 status = NT_STATUS_NO_MEMORY;
3358 req = cli_getatr_send(frame, ev, cli, fname);
3360 status = NT_STATUS_NO_MEMORY;
3364 if (!tevent_req_poll(req, ev)) {
3365 status = map_nt_error_from_unix(errno);
3369 status = cli_getatr_recv(req,
3376 if (!NT_STATUS_IS_OK(status)) {
3377 cli_set_error(cli, status);
3382 /****************************************************************************
3383 Do a SMBsetattrE call.
3384 ****************************************************************************/
3386 static void cli_setattrE_done(struct tevent_req *subreq);
3388 struct cli_setattrE_state {
3392 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3393 struct event_context *ev,
3394 struct cli_state *cli,
3400 struct tevent_req *req = NULL, *subreq = NULL;
3401 struct cli_setattrE_state *state = NULL;
3402 uint8_t additional_flags = 0;
3404 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3409 SSVAL(state->vwv+0, 0, fnum);
3410 push_dos_date2((uint8_t *)&state->vwv[1], 0, change_time,
3412 push_dos_date2((uint8_t *)&state->vwv[3], 0, access_time,
3414 push_dos_date2((uint8_t *)&state->vwv[5], 0, write_time,
3417 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3418 7, state->vwv, 0, NULL);
3419 if (tevent_req_nomem(subreq, req)) {
3420 return tevent_req_post(req, ev);
3422 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3426 static void cli_setattrE_done(struct tevent_req *subreq)
3428 struct tevent_req *req = tevent_req_callback_data(
3429 subreq, struct tevent_req);
3432 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3433 TALLOC_FREE(subreq);
3434 if (!NT_STATUS_IS_OK(status)) {
3435 tevent_req_nterror(req, status);
3438 tevent_req_done(req);
3441 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3443 return tevent_req_simple_recv_ntstatus(req);
3446 NTSTATUS cli_setattrE(struct cli_state *cli,
3452 TALLOC_CTX *frame = talloc_stackframe();
3453 struct event_context *ev = NULL;
3454 struct tevent_req *req = NULL;
3455 NTSTATUS status = NT_STATUS_OK;
3457 if (cli_has_async_calls(cli)) {
3459 * Can't use sync call while an async call is in flight
3461 status = NT_STATUS_INVALID_PARAMETER;
3465 ev = event_context_init(frame);
3467 status = NT_STATUS_NO_MEMORY;
3471 req = cli_setattrE_send(frame, ev,
3479 status = NT_STATUS_NO_MEMORY;
3483 if (!tevent_req_poll(req, ev)) {
3484 status = map_nt_error_from_unix(errno);
3488 status = cli_setattrE_recv(req);
3492 if (!NT_STATUS_IS_OK(status)) {
3493 cli_set_error(cli, status);
3498 /****************************************************************************
3499 Do a SMBsetatr call.
3500 ****************************************************************************/
3502 static void cli_setatr_done(struct tevent_req *subreq);
3504 struct cli_setatr_state {
3508 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3509 struct event_context *ev,
3510 struct cli_state *cli,
3515 struct tevent_req *req = NULL, *subreq = NULL;
3516 struct cli_setatr_state *state = NULL;
3517 uint8_t additional_flags = 0;
3518 uint8_t *bytes = NULL;
3520 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3525 SSVAL(state->vwv+0, 0, attr);
3526 push_dos_date3((uint8_t *)&state->vwv[1], 0, mtime, cli->serverzone);
3528 bytes = talloc_array(state, uint8_t, 1);
3529 if (tevent_req_nomem(bytes, req)) {
3530 return tevent_req_post(req, ev);
3533 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3534 strlen(fname)+1, NULL);
3535 if (tevent_req_nomem(bytes, req)) {
3536 return tevent_req_post(req, ev);
3538 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3539 talloc_get_size(bytes)+1);
3540 if (tevent_req_nomem(bytes, req)) {
3541 return tevent_req_post(req, ev);
3544 bytes[talloc_get_size(bytes)-1] = 4;
3545 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3547 if (tevent_req_nomem(bytes, req)) {
3548 return tevent_req_post(req, ev);
3551 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3552 8, state->vwv, talloc_get_size(bytes), bytes);
3553 if (tevent_req_nomem(subreq, req)) {
3554 return tevent_req_post(req, ev);
3556 tevent_req_set_callback(subreq, cli_setatr_done, req);
3560 static void cli_setatr_done(struct tevent_req *subreq)
3562 struct tevent_req *req = tevent_req_callback_data(
3563 subreq, struct tevent_req);
3566 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3567 TALLOC_FREE(subreq);
3568 if (!NT_STATUS_IS_OK(status)) {
3569 tevent_req_nterror(req, status);
3572 tevent_req_done(req);
3575 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3577 return tevent_req_simple_recv_ntstatus(req);
3580 NTSTATUS cli_setatr(struct cli_state *cli,
3585 TALLOC_CTX *frame = talloc_stackframe();
3586 struct event_context *ev = NULL;
3587 struct tevent_req *req = NULL;
3588 NTSTATUS status = NT_STATUS_OK;
3590 if (cli_has_async_calls(cli)) {
3592 * Can't use sync call while an async call is in flight
3594 status = NT_STATUS_INVALID_PARAMETER;
3598 ev = event_context_init(frame);
3600 status = NT_STATUS_NO_MEMORY;
3604 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3606 status = NT_STATUS_NO_MEMORY;
3610 if (!tevent_req_poll(req, ev)) {
3611 status = map_nt_error_from_unix(errno);
3615 status = cli_setatr_recv(req);
3619 if (!NT_STATUS_IS_OK(status)) {
3620 cli_set_error(cli, status);
3625 /****************************************************************************
3626 Check for existance of a dir.
3627 ****************************************************************************/
3629 static void cli_chkpath_done(struct tevent_req *subreq);
3631 struct cli_chkpath_state {
3635 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3636 struct event_context *ev,
3637 struct cli_state *cli,
3640 struct tevent_req *req = NULL, *subreq = NULL;
3641 struct cli_chkpath_state *state = NULL;
3642 uint8_t additional_flags = 0;
3643 uint8_t *bytes = NULL;
3645 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3650 bytes = talloc_array(state, uint8_t, 1);
3651 if (tevent_req_nomem(bytes, req)) {
3652 return tevent_req_post(req, ev);
3655 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3656 strlen(fname)+1, NULL);
3658 if (tevent_req_nomem(bytes, req)) {
3659 return tevent_req_post(req, ev);
3662 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3663 0, NULL, talloc_get_size(bytes), bytes);
3664 if (tevent_req_nomem(subreq, req)) {
3665 return tevent_req_post(req, ev);
3667 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3671 static void cli_chkpath_done(struct tevent_req *subreq)
3673 struct tevent_req *req = tevent_req_callback_data(
3674 subreq, struct tevent_req);
3677 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
3678 TALLOC_FREE(subreq);
3679 if (!NT_STATUS_IS_OK(status)) {
3680 tevent_req_nterror(req, status);
3683 tevent_req_done(req);
3686 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3688 return tevent_req_simple_recv_ntstatus(req);
3691 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3693 TALLOC_CTX *frame = talloc_stackframe();
3694 struct event_context *ev = NULL;
3695 struct tevent_req *req = NULL;
3697 NTSTATUS status = NT_STATUS_OK;
3699 if (cli_has_async_calls(cli)) {
3701 * Can't use sync call while an async call is in flight
3703 status = NT_STATUS_INVALID_PARAMETER;
3707 path2 = talloc_strdup(frame, path);
3709 status = NT_STATUS_NO_MEMORY;
3712 trim_char(path2,'\0','\\');
3714 path2 = talloc_strdup(frame, "\\");
3716 status = NT_STATUS_NO_MEMORY;
3721 ev = event_context_init(frame);
3723 status = NT_STATUS_NO_MEMORY;
3727 req = cli_chkpath_send(frame, ev, cli, path2);
3729 status = NT_STATUS_NO_MEMORY;
3733 if (!tevent_req_poll(req, ev)) {
3734 status = map_nt_error_from_unix(errno);
3738 status = cli_chkpath_recv(req);
3742 if (!NT_STATUS_IS_OK(status)) {
3743 cli_set_error(cli, status);
3748 /****************************************************************************
3750 ****************************************************************************/
3752 static void cli_dskattr_done(struct tevent_req *subreq);
3754 struct cli_dskattr_state {
3760 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3761 struct event_context *ev,
3762 struct cli_state *cli)
3764 struct tevent_req *req = NULL, *subreq = NULL;
3765 struct cli_dskattr_state *state = NULL;
3766 uint8_t additional_flags = 0;
3768 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3773 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3775 if (tevent_req_nomem(subreq, req)) {
3776 return tevent_req_post(req, ev);
3778 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3782 static void cli_dskattr_done(struct tevent_req *subreq)
3784 struct tevent_req *req = tevent_req_callback_data(
3785 subreq, struct tevent_req);
3786 struct cli_dskattr_state *state = tevent_req_data(
3787 req, struct cli_dskattr_state);
3789 uint16_t *vwv = NULL;
3793 status = cli_smb_recv(subreq, state, &inbuf, 4, &wct, &vwv, NULL,
3795 TALLOC_FREE(subreq);
3796 if (!NT_STATUS_IS_OK(status)) {
3797 tevent_req_nterror(req, status);
3800 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3801 state->total = SVAL(vwv+0, 0);
3802 state->avail = SVAL(vwv+3, 0);
3803 tevent_req_done(req);
3806 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3808 struct cli_dskattr_state *state = tevent_req_data(
3809 req, struct cli_dskattr_state);
3812 if (tevent_req_is_nterror(req, &status)) {
3815 *bsize = state->bsize;
3816 *total = state->total;
3817 *avail = state->avail;
3818 return NT_STATUS_OK;
3821 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3823 TALLOC_CTX *frame = talloc_stackframe();
3824 struct event_context *ev = NULL;
3825 struct tevent_req *req = NULL;
3826 NTSTATUS status = NT_STATUS_OK;
3828 if (cli_has_async_calls(cli)) {
3830 * Can't use sync call while an async call is in flight
3832 status = NT_STATUS_INVALID_PARAMETER;
3836 ev = event_context_init(frame);
3838 status = NT_STATUS_NO_MEMORY;
3842 req = cli_dskattr_send(frame, ev, cli);
3844 status = NT_STATUS_NO_MEMORY;
3848 if (!tevent_req_poll(req, ev)) {
3849 status = map_nt_error_from_unix(errno);
3853 status = cli_dskattr_recv(req, bsize, total, avail);
3857 if (!NT_STATUS_IS_OK(status)) {
3858 cli_set_error(cli, status);
3863 /****************************************************************************
3864 Create and open a temporary file.
3865 ****************************************************************************/
3867 static void cli_ctemp_done(struct tevent_req *subreq);
3869 struct ctemp_state {
3875 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3876 struct event_context *ev,
3877 struct cli_state *cli,
3880 struct tevent_req *req = NULL, *subreq = NULL;
3881 struct ctemp_state *state = NULL;
3882 uint8_t additional_flags = 0;
3883 uint8_t *bytes = NULL;
3885 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3890 SSVAL(state->vwv,0,0);
3891 SIVALS(state->vwv+1,0,-1);
3893 bytes = talloc_array(state, uint8_t, 1);
3894 if (tevent_req_nomem(bytes, req)) {
3895 return tevent_req_post(req, ev);
3898 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3899 strlen(path)+1, NULL);
3900 if (tevent_req_nomem(bytes, req)) {
3901 return tevent_req_post(req, ev);
3904 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3905 3, state->vwv, talloc_get_size(bytes), bytes);
3906 if (tevent_req_nomem(subreq, req)) {
3907 return tevent_req_post(req, ev);
3909 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3913 static void cli_ctemp_done(struct tevent_req *subreq)
3915 struct tevent_req *req = tevent_req_callback_data(
3916 subreq, struct tevent_req);
3917 struct ctemp_state *state = tevent_req_data(
3918 req, struct ctemp_state);
3922 uint32_t num_bytes = 0;
3923 uint8_t *bytes = NULL;
3926 status = cli_smb_recv(subreq, state, &inbuf, 1, &wcnt, &vwv,
3927 &num_bytes, &bytes);
3928 TALLOC_FREE(subreq);
3929 if (!NT_STATUS_IS_OK(status)) {
3930 tevent_req_nterror(req, status);
3934 state->fnum = SVAL(vwv+0, 0);
3936 /* From W2K3, the result is just the ASCII name */
3937 if (num_bytes < 2) {
3938 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
3942 if (pull_string_talloc(state,
3949 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
3952 tevent_req_done(req);
3955 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
3960 struct ctemp_state *state = tevent_req_data(req,
3961 struct ctemp_state);
3964 if (tevent_req_is_nterror(req, &status)) {
3967 *pfnum = state->fnum;
3968 *outfile = talloc_strdup(ctx, state->ret_path);
3970 return NT_STATUS_NO_MEMORY;
3972 return NT_STATUS_OK;
3975 NTSTATUS cli_ctemp(struct cli_state *cli,
3981 TALLOC_CTX *frame = talloc_stackframe();
3982 struct event_context *ev;
3983 struct tevent_req *req;
3984 NTSTATUS status = NT_STATUS_OK;
3986 if (cli_has_async_calls(cli)) {
3988 * Can't use sync call while an async call is in flight
3990 status = NT_STATUS_INVALID_PARAMETER;
3994 ev = event_context_init(frame);
3996 status = NT_STATUS_NO_MEMORY;
4000 req = cli_ctemp_send(frame, ev, cli, path);
4002 status = NT_STATUS_NO_MEMORY;
4006 if (!tevent_req_poll(req, ev)) {
4007 status = map_nt_error_from_unix(errno);
4011 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
4015 if (!NT_STATUS_IS_OK(status)) {
4016 cli_set_error(cli, status);
4022 send a raw ioctl - used by the torture code
4024 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
4029 SSVAL(vwv+0, 0, fnum);
4030 SSVAL(vwv+1, 0, code>>16);
4031 SSVAL(vwv+2, 0, (code&0xFFFF));
4033 status = cli_smb(talloc_tos(), cli, SMBioctl, 0, 3, vwv, 0, NULL,
4034 NULL, 0, NULL, NULL, NULL, NULL);
4035 if (!NT_STATUS_IS_OK(status)) {
4038 *blob = data_blob_null;
4039 return NT_STATUS_OK;
4042 /*********************************************************
4043 Set an extended attribute utility fn.
4044 *********************************************************/
4046 static NTSTATUS cli_set_ea(struct cli_state *cli, uint16_t setup_val,
4047 uint8_t *param, unsigned int param_len,
4048 const char *ea_name,
4049 const char *ea_val, size_t ea_len)
4052 unsigned int data_len = 0;
4053 uint8_t *data = NULL;
4055 size_t ea_namelen = strlen(ea_name);
4058 SSVAL(setup, 0, setup_val);
4060 if (ea_namelen == 0 && ea_len == 0) {
4062 data = (uint8_t *)SMB_MALLOC(data_len);
4064 return NT_STATUS_NO_MEMORY;
4067 SIVAL(p,0,data_len);
4069 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
4070 data = (uint8_t *)SMB_MALLOC(data_len);
4072 return NT_STATUS_NO_MEMORY;
4075 SIVAL(p,0,data_len);
4077 SCVAL(p, 0, 0); /* EA flags. */
4078 SCVAL(p, 1, ea_namelen);
4079 SSVAL(p, 2, ea_len);
4080 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
4081 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
4084 status = cli_trans(talloc_tos(), cli, SMBtrans2, NULL, -1, 0, 0,
4086 param, param_len, 2,
4087 data, data_len, cli->max_xmit,
4089 NULL, 0, NULL, /* rsetup */
4090 NULL, 0, NULL, /* rparam */
4091 NULL, 0, NULL); /* rdata */
4096 /*********************************************************
4097 Set an extended attribute on a pathname.
4098 *********************************************************/
4100 NTSTATUS cli_set_ea_path(struct cli_state *cli, const char *path,
4101 const char *ea_name, const char *ea_val,
4104 unsigned int param_len = 0;
4106 size_t srclen = 2*(strlen(path)+1);
4110 param = SMB_MALLOC_ARRAY(uint8_t, 6+srclen+2);
4112 return NT_STATUS_NO_MEMORY;
4114 memset(param, '\0', 6);
4115 SSVAL(param,0,SMB_INFO_SET_EA);
4116 p = (char *)(¶m[6]);
4118 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4119 param_len = PTR_DIFF(p, param);
4121 status = cli_set_ea(cli, TRANSACT2_SETPATHINFO, param, param_len,
4122 ea_name, ea_val, ea_len);
4127 /*********************************************************
4128 Set an extended attribute on an fnum.
4129 *********************************************************/
4131 NTSTATUS cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum,
4132 const char *ea_name, const char *ea_val,
4137 memset(param, 0, 6);
4138 SSVAL(param,0,fnum);
4139 SSVAL(param,2,SMB_INFO_SET_EA);
4141 return cli_set_ea(cli, TRANSACT2_SETFILEINFO, param, 6,
4142 ea_name, ea_val, ea_len);
4145 /*********************************************************
4146 Get an extended attribute list utility fn.
4147 *********************************************************/
4149 static bool parse_ea_blob(TALLOC_CTX *ctx, const uint8_t *rdata,
4151 size_t *pnum_eas, struct ea_struct **pea_list)
4153 struct ea_struct *ea_list = NULL;
4158 if (rdata_len < 4) {
4162 ea_size = (size_t)IVAL(rdata,0);
4163 if (ea_size > rdata_len) {
4168 /* No EA's present. */
4177 /* Validate the EA list and count it. */
4178 for (num_eas = 0; ea_size >= 4; num_eas++) {
4179 unsigned int ea_namelen = CVAL(p,1);
4180 unsigned int ea_valuelen = SVAL(p,2);
4181 if (ea_namelen == 0) {
4184 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4187 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4188 p += 4 + ea_namelen + 1 + ea_valuelen;
4197 *pnum_eas = num_eas;
4199 /* Caller only wants number of EA's. */
4203 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
4208 ea_size = (size_t)IVAL(rdata,0);
4211 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4212 struct ea_struct *ea = &ea_list[num_eas];
4213 fstring unix_ea_name;
4214 unsigned int ea_namelen = CVAL(p,1);
4215 unsigned int ea_valuelen = SVAL(p,2);
4217 ea->flags = CVAL(p,0);
4218 unix_ea_name[0] = '\0';
4219 pull_ascii(unix_ea_name, p + 4, sizeof(unix_ea_name), rdata_len - PTR_DIFF(p+4, rdata), STR_TERMINATE);
4220 ea->name = talloc_strdup(ea_list, unix_ea_name);
4224 /* Ensure the value is null terminated (in case it's a string). */
4225 ea->value = data_blob_talloc(ea_list, NULL, ea_valuelen + 1);
4226 if (!ea->value.data) {
4230 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4232 ea->value.data[ea_valuelen] = 0;
4234 p += 4 + ea_namelen + 1 + ea_valuelen;
4237 *pea_list = ea_list;
4241 TALLOC_FREE(ea_list);
4245 /*********************************************************
4246 Get an extended attribute list from a pathname.
4247 *********************************************************/
4249 struct cli_get_ea_list_path_state {
4254 static void cli_get_ea_list_path_done(struct tevent_req *subreq);
4256 struct tevent_req *cli_get_ea_list_path_send(TALLOC_CTX *mem_ctx,
4257 struct tevent_context *ev,
4258 struct cli_state *cli,
4261 struct tevent_req *req, *subreq;
4262 struct cli_get_ea_list_path_state *state;
4264 req = tevent_req_create(mem_ctx, &state,
4265 struct cli_get_ea_list_path_state);
4269 subreq = cli_qpathinfo_send(state, ev, cli, fname,
4270 SMB_INFO_QUERY_ALL_EAS, 4,
4272 if (tevent_req_nomem(subreq, req)) {
4273 return tevent_req_post(req, ev);
4275 tevent_req_set_callback(subreq, cli_get_ea_list_path_done, req);
4279 static void cli_get_ea_list_path_done(struct tevent_req *subreq)
4281 struct tevent_req *req = tevent_req_callback_data(
4282 subreq, struct tevent_req);
4283 struct cli_get_ea_list_path_state *state = tevent_req_data(
4284 req, struct cli_get_ea_list_path_state);
4287 status = cli_qpathinfo_recv(subreq, state, &state->data,
4289 TALLOC_FREE(subreq);
4290 if (!NT_STATUS_IS_OK(status)) {
4291 tevent_req_nterror(req, status);
4294 tevent_req_done(req);
4297 NTSTATUS cli_get_ea_list_path_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4298 size_t *pnum_eas, struct ea_struct **peas)
4300 struct cli_get_ea_list_path_state *state = tevent_req_data(
4301 req, struct cli_get_ea_list_path_state);
4304 if (tevent_req_is_nterror(req, &status)) {
4307 if (!parse_ea_blob(mem_ctx, state->data, state->num_data,
4309 return NT_STATUS_INVALID_NETWORK_RESPONSE;
4311 return NT_STATUS_OK;
4314 NTSTATUS cli_get_ea_list_path(struct cli_state *cli, const char *path,
4317 struct ea_struct **pea_list)
4319 TALLOC_CTX *frame = talloc_stackframe();
4320 struct event_context *ev = NULL;
4321 struct tevent_req *req = NULL;
4322 NTSTATUS status = NT_STATUS_NO_MEMORY;
4324 if (cli_has_async_calls(cli)) {
4326 * Can't use sync call while an async call is in flight
4328 status = NT_STATUS_INVALID_PARAMETER;
4331 ev = event_context_init(frame);
4335 req = cli_get_ea_list_path_send(frame, ev, cli, path);
4339 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
4342 status = cli_get_ea_list_path_recv(req, ctx, pnum_eas, pea_list);
4345 if (!NT_STATUS_IS_OK(status)) {
4346 cli_set_error(cli, status);
4351 /****************************************************************************
4352 Convert open "flags" arg to uint32_t on wire.
4353 ****************************************************************************/
4355 static uint32_t open_flags_to_wire(int flags)
4357 int open_mode = flags & O_ACCMODE;
4360 switch (open_mode) {
4362 ret |= SMB_O_WRONLY;
4369 ret |= SMB_O_RDONLY;
4373 if (flags & O_CREAT) {
4376 if (flags & O_EXCL) {
4379 if (flags & O_TRUNC) {
4383 if (flags & O_SYNC) {
4387 if (flags & O_APPEND) {
4388 ret |= SMB_O_APPEND;
4390 #if defined(O_DIRECT)
4391 if (flags & O_DIRECT) {
4392 ret |= SMB_O_DIRECT;
4395 #if defined(O_DIRECTORY)
4396 if (flags & O_DIRECTORY) {
4397 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4398 ret |= SMB_O_DIRECTORY;
4404 /****************************************************************************
4405 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4406 ****************************************************************************/
4408 struct posix_open_state {
4412 uint16_t fnum; /* Out */
4415 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4417 struct tevent_req *req = tevent_req_callback_data(
4418 subreq, struct tevent_req);
4419 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4424 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
4425 NULL, 0, NULL, &data, 12, &num_data);
4426 TALLOC_FREE(subreq);
4427 if (!NT_STATUS_IS_OK(status)) {
4428 tevent_req_nterror(req, status);
4431 state->fnum = SVAL(data,2);
4432 tevent_req_done(req);
4435 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4436 struct event_context *ev,
4437 struct cli_state *cli,
4443 struct tevent_req *req = NULL, *subreq = NULL;
4444 struct posix_open_state *state = NULL;
4445 uint32_t wire_flags = open_flags_to_wire(flags);
4447 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4452 /* Setup setup word. */
4453 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4455 /* Setup param array. */
4456 state->param = talloc_array(state, uint8_t, 6);
4457 if (tevent_req_nomem(state->param, req)) {
4458 return tevent_req_post(req, ev);
4460 memset(state->param, '\0', 6);
4461 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4463 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4464 strlen(fname)+1, NULL);
4466 if (tevent_req_nomem(state->param, req)) {
4467 return tevent_req_post(req, ev);
4470 /* Setup data words. */
4472 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4473 wire_flags |= SMB_O_DIRECTORY;
4476 SIVAL(state->data,0,0); /* No oplock. */
4477 SIVAL(state->data,4,wire_flags);
4478 SIVAL(state->data,8,unix_perms_to_wire(mode));
4479 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4480 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4482 subreq = cli_trans_send(state, /* mem ctx. */
4483 ev, /* event ctx. */
4484 cli, /* cli_state. */
4485 SMBtrans2, /* cmd. */
4486 NULL, /* pipe name. */
4490 &state->setup, /* setup. */
4491 1, /* num setup uint16_t words. */
4492 0, /* max returned setup. */
4493 state->param, /* param. */
4494 talloc_get_size(state->param),/* num param. */
4495 2, /* max returned param. */
4496 state->data, /* data. */
4498 12); /* max returned data. */
4500 if (tevent_req_nomem(subreq, req)) {
4501 return tevent_req_post(req, ev);
4503 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4507 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4508 struct event_context *ev,
4509 struct cli_state *cli,
4514 return cli_posix_open_internal_send(mem_ctx, ev,
4515 cli, fname, flags, mode, false);
4518 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4520 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4523 if (tevent_req_is_nterror(req, &status)) {
4526 *pfnum = state->fnum;
4527 return NT_STATUS_OK;
4530 /****************************************************************************
4531 Open - POSIX semantics. Doesn't request oplock.
4532 ****************************************************************************/
4534 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4535 int flags, mode_t mode, uint16_t *pfnum)
4538 TALLOC_CTX *frame = talloc_stackframe();
4539 struct event_context *ev = NULL;
4540 struct tevent_req *req = NULL;
4541 NTSTATUS status = NT_STATUS_OK;
4543 if (cli_has_async_calls(cli)) {
4545 * Can't use sync call while an async call is in flight
4547 status = NT_STATUS_INVALID_PARAMETER;
4551 ev = event_context_init(frame);
4553 status = NT_STATUS_NO_MEMORY;
4557 req = cli_posix_open_send(frame,
4564 status = NT_STATUS_NO_MEMORY;
4568 if (!tevent_req_poll(req, ev)) {
4569 status = map_nt_error_from_unix(errno);
4573 status = cli_posix_open_recv(req, pfnum);
4577 if (!NT_STATUS_IS_OK(status)) {
4578 cli_set_error(cli, status);
4583 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4584 struct event_context *ev,
4585 struct cli_state *cli,
4589 return cli_posix_open_internal_send(mem_ctx, ev,
4590 cli, fname, O_CREAT, mode, true);
4593 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4595 return tevent_req_simple_recv_ntstatus(req);
4598 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4600 TALLOC_CTX *frame = talloc_stackframe();
4601 struct event_context *ev = NULL;
4602 struct tevent_req *req = NULL;
4603 NTSTATUS status = NT_STATUS_OK;
4605 if (cli_has_async_calls(cli)) {
4607 * Can't use sync call while an async call is in flight
4609 status = NT_STATUS_INVALID_PARAMETER;
4613 ev = event_context_init(frame);
4615 status = NT_STATUS_NO_MEMORY;
4619 req = cli_posix_mkdir_send(frame,
4625 status = NT_STATUS_NO_MEMORY;
4629 if (!tevent_req_poll(req, ev)) {
4630 status = map_nt_error_from_unix(errno);
4634 status = cli_posix_mkdir_recv(req);
4638 if (!NT_STATUS_IS_OK(status)) {
4639 cli_set_error(cli, status);
4644 /****************************************************************************
4645 unlink or rmdir - POSIX semantics.
4646 ****************************************************************************/
4648 struct cli_posix_unlink_internal_state {
4652 static void cli_posix_unlink_internal_done(struct tevent_req *subreq);
4654 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4655 struct event_context *ev,
4656 struct cli_state *cli,
4660 struct tevent_req *req = NULL, *subreq = NULL;
4661 struct cli_posix_unlink_internal_state *state = NULL;
4663 req = tevent_req_create(mem_ctx, &state,
4664 struct cli_posix_unlink_internal_state);
4669 /* Setup data word. */
4670 SSVAL(state->data, 0, level);
4672 subreq = cli_setpathinfo_send(state, ev, cli,
4673 SMB_POSIX_PATH_UNLINK,
4675 state->data, sizeof(state->data));
4676 if (tevent_req_nomem(subreq, req)) {
4677 return tevent_req_post(req, ev);
4679 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4683 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4685 NTSTATUS status = cli_setpathinfo_recv(subreq);
4686 tevent_req_simple_finish_ntstatus(subreq, status);
4689 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4690 struct event_context *ev,
4691 struct cli_state *cli,
4694 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname,
4695 SMB_POSIX_UNLINK_FILE_TARGET);
4698 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4700 return tevent_req_simple_recv_ntstatus(req);
4703 /****************************************************************************
4704 unlink - POSIX semantics.
4705 ****************************************************************************/
4707 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4709 TALLOC_CTX *frame = talloc_stackframe();
4710 struct event_context *ev = NULL;
4711 struct tevent_req *req = NULL;
4712 NTSTATUS status = NT_STATUS_OK;
4714 if (cli_has_async_calls(cli)) {
4716 * Can't use sync call while an async call is in flight
4718 status = NT_STATUS_INVALID_PARAMETER;
4722 ev = event_context_init(frame);
4724 status = NT_STATUS_NO_MEMORY;
4728 req = cli_posix_unlink_send(frame,
4733 status = NT_STATUS_NO_MEMORY;
4737 if (!tevent_req_poll(req, ev)) {
4738 status = map_nt_error_from_unix(errno);
4742 status = cli_posix_unlink_recv(req);
4746 if (!NT_STATUS_IS_OK(status)) {
4747 cli_set_error(cli, status);
4752 /****************************************************************************
4753 rmdir - POSIX semantics.
4754 ****************************************************************************/
4756 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4757 struct event_context *ev,
4758 struct cli_state *cli,
4761 return cli_posix_unlink_internal_send(
4762 mem_ctx, ev, cli, fname,
4763 SMB_POSIX_UNLINK_DIRECTORY_TARGET);
4766 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4768 return tevent_req_simple_recv_ntstatus(req);
4771 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4773 TALLOC_CTX *frame = talloc_stackframe();
4774 struct event_context *ev = NULL;
4775 struct tevent_req *req = NULL;
4776 NTSTATUS status = NT_STATUS_OK;
4778 if (cli_has_async_calls(cli)) {
4780 * Can't use sync call while an async call is in flight
4782 status = NT_STATUS_INVALID_PARAMETER;
4786 ev = event_context_init(frame);
4788 status = NT_STATUS_NO_MEMORY;
4792 req = cli_posix_rmdir_send(frame,
4797 status = NT_STATUS_NO_MEMORY;
4801 if (!tevent_req_poll(req, ev)) {
4802 status = map_nt_error_from_unix(errno);
4806 status = cli_posix_rmdir_recv(req, frame);
4810 if (!NT_STATUS_IS_OK(status)) {
4811 cli_set_error(cli, status);
4816 /****************************************************************************
4818 ****************************************************************************/
4820 struct cli_notify_state {
4822 uint32_t num_changes;
4823 struct notify_change *changes;
4826 static void cli_notify_done(struct tevent_req *subreq);
4828 struct tevent_req *cli_notify_send(TALLOC_CTX *mem_ctx,
4829 struct tevent_context *ev,
4830 struct cli_state *cli, uint16_t fnum,
4831 uint32_t buffer_size,
4832 uint32_t completion_filter, bool recursive)
4834 struct tevent_req *req, *subreq;
4835 struct cli_notify_state *state;
4837 req = tevent_req_create(mem_ctx, &state, struct cli_notify_state);
4842 SIVAL(state->setup, 0, completion_filter);
4843 SSVAL(state->setup, 4, fnum);
4844 SSVAL(state->setup, 6, recursive);
4846 subreq = cli_trans_send(
4847 state, /* mem ctx. */
4848 ev, /* event ctx. */
4849 cli, /* cli_state. */
4850 SMBnttrans, /* cmd. */
4851 NULL, /* pipe name. */
4853 NT_TRANSACT_NOTIFY_CHANGE, /* function. */
4855 (uint16_t *)state->setup, /* setup. */
4856 4, /* num setup uint16_t words. */
4857 0, /* max returned setup. */
4860 buffer_size, /* max returned param. */
4863 0); /* max returned data. */
4865 if (tevent_req_nomem(subreq, req)) {
4866 return tevent_req_post(req, ev);
4868 tevent_req_set_callback(subreq, cli_notify_done, req);
4872 static void cli_notify_done(struct tevent_req *subreq)
4874 struct tevent_req *req = tevent_req_callback_data(
4875 subreq, struct tevent_req);
4876 struct cli_notify_state *state = tevent_req_data(
4877 req, struct cli_notify_state);
4880 uint32_t i, ofs, num_params;
4883 status = cli_trans_recv(subreq, talloc_tos(), &flags2, NULL, 0, NULL,
4884 ¶ms, 0, &num_params, NULL, 0, NULL);
4885 TALLOC_FREE(subreq);
4886 if (!NT_STATUS_IS_OK(status)) {
4887 DEBUG(10, ("cli_trans_recv returned %s\n", nt_errstr(status)));
4888 tevent_req_nterror(req, status);
4892 state->num_changes = 0;
4895 while (num_params - ofs > 12) {
4896 uint32_t len = IVAL(params, ofs);
4897 state->num_changes += 1;
4899 if ((len == 0) || (ofs+len >= num_params)) {
4905 state->changes = talloc_array(state, struct notify_change,
4906 state->num_changes);
4907 if (tevent_req_nomem(state->changes, req)) {
4908 TALLOC_FREE(params);
4914 for (i=0; i<state->num_changes; i++) {
4915 uint32_t next = IVAL(params, ofs);
4916 uint32_t len = IVAL(params, ofs+8);
4920 if ((next != 0) && (len+12 != next)) {
4921 TALLOC_FREE(params);
4923 req, NT_STATUS_INVALID_NETWORK_RESPONSE);
4927 state->changes[i].action = IVAL(params, ofs+4);
4928 ret = clistr_pull_talloc(params, (char *)params, flags2,
4929 &name, params+ofs+12, len,
4930 STR_TERMINATE|STR_UNICODE);
4932 TALLOC_FREE(params);
4933 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
4936 state->changes[i].name = name;
4940 TALLOC_FREE(params);
4941 tevent_req_done(req);
4944 NTSTATUS cli_notify_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
4945 uint32_t *pnum_changes,
4946 struct notify_change **pchanges)
4948 struct cli_notify_state *state = tevent_req_data(
4949 req, struct cli_notify_state);
4952 if (tevent_req_is_nterror(req, &status)) {
4956 *pnum_changes = state->num_changes;
4957 *pchanges = talloc_move(mem_ctx, &state->changes);
4958 return NT_STATUS_OK;
4961 struct cli_qpathinfo_state {
4970 static void cli_qpathinfo_done(struct tevent_req *subreq);
4972 struct tevent_req *cli_qpathinfo_send(TALLOC_CTX *mem_ctx,
4973 struct tevent_context *ev,
4974 struct cli_state *cli, const char *fname,
4975 uint16_t level, uint32_t min_rdata,
4978 struct tevent_req *req, *subreq;
4979 struct cli_qpathinfo_state *state;
4981 req = tevent_req_create(mem_ctx, &state, struct cli_qpathinfo_state);
4985 state->min_rdata = min_rdata;
4986 SSVAL(state->setup, 0, TRANSACT2_QPATHINFO);
4988 state->param = talloc_zero_array(state, uint8_t, 6);
4989 if (tevent_req_nomem(state->param, req)) {
4990 return tevent_req_post(req, ev);
4992 SSVAL(state->param, 0, level);
4993 state->param = trans2_bytes_push_str(
4994 state->param, cli_ucs2(cli), fname, strlen(fname)+1, NULL);
4995 if (tevent_req_nomem(state->param, req)) {
4996 return tevent_req_post(req, ev);
4999 subreq = cli_trans_send(
5000 state, /* mem ctx. */
5001 ev, /* event ctx. */
5002 cli, /* cli_state. */
5003 SMBtrans2, /* cmd. */
5004 NULL, /* pipe name. */
5008 state->setup, /* setup. */
5009 1, /* num setup uint16_t words. */
5010 0, /* max returned setup. */
5011 state->param, /* param. */
5012 talloc_get_size(state->param), /* num param. */
5013 2, /* max returned param. */
5016 max_rdata); /* max returned data. */
5018 if (tevent_req_nomem(subreq, req)) {
5019 return tevent_req_post(req, ev);
5021 tevent_req_set_callback(subreq, cli_qpathinfo_done, req);
5025 static void cli_qpathinfo_done(struct tevent_req *subreq)
5027 struct tevent_req *req = tevent_req_callback_data(
5028 subreq, struct tevent_req);
5029 struct cli_qpathinfo_state *state = tevent_req_data(
5030 req, struct cli_qpathinfo_state);
5033 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5035 &state->rdata, state->min_rdata,
5037 if (!NT_STATUS_IS_OK(status)) {
5038 tevent_req_nterror(req, status);
5041 tevent_req_done(req);
5044 NTSTATUS cli_qpathinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5045 uint8_t **rdata, uint32_t *num_rdata)
5047 struct cli_qpathinfo_state *state = tevent_req_data(
5048 req, struct cli_qpathinfo_state);
5051 if (tevent_req_is_nterror(req, &status)) {
5054 if (rdata != NULL) {
5055 *rdata = talloc_move(mem_ctx, &state->rdata);
5057 TALLOC_FREE(state->rdata);
5059 if (num_rdata != NULL) {
5060 *num_rdata = state->num_rdata;
5062 return NT_STATUS_OK;
5065 NTSTATUS cli_qpathinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5066 const char *fname, uint16_t level, uint32_t min_rdata,
5068 uint8_t **rdata, uint32_t *num_rdata)
5070 TALLOC_CTX *frame = talloc_stackframe();
5071 struct event_context *ev;
5072 struct tevent_req *req;
5073 NTSTATUS status = NT_STATUS_NO_MEMORY;
5075 if (cli_has_async_calls(cli)) {
5077 * Can't use sync call while an async call is in flight
5079 status = NT_STATUS_INVALID_PARAMETER;
5082 ev = event_context_init(frame);
5086 req = cli_qpathinfo_send(frame, ev, cli, fname, level, min_rdata,
5091 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5094 status = cli_qpathinfo_recv(req, mem_ctx, rdata, num_rdata);
5097 if (!NT_STATUS_IS_OK(status)) {
5098 cli_set_error(cli, status);
5103 struct cli_qfileinfo_state {
5112 static void cli_qfileinfo_done(struct tevent_req *subreq);
5114 struct tevent_req *cli_qfileinfo_send(TALLOC_CTX *mem_ctx,
5115 struct tevent_context *ev,
5116 struct cli_state *cli, uint16_t fnum,
5117 uint16_t level, uint32_t min_rdata,
5120 struct tevent_req *req, *subreq;
5121 struct cli_qfileinfo_state *state;
5123 req = tevent_req_create(mem_ctx, &state, struct cli_qfileinfo_state);
5127 state->min_rdata = min_rdata;
5128 SSVAL(state->param, 0, fnum);
5129 SSVAL(state->param, 2, level);
5130 SSVAL(state->setup, 0, TRANSACT2_QFILEINFO);
5132 subreq = cli_trans_send(
5133 state, /* mem ctx. */
5134 ev, /* event ctx. */
5135 cli, /* cli_state. */
5136 SMBtrans2, /* cmd. */
5137 NULL, /* pipe name. */
5141 state->setup, /* setup. */
5142 1, /* num setup uint16_t words. */
5143 0, /* max returned setup. */
5144 state->param, /* param. */
5145 sizeof(state->param), /* num param. */
5146 2, /* max returned param. */
5149 max_rdata); /* max returned data. */
5151 if (tevent_req_nomem(subreq, req)) {
5152 return tevent_req_post(req, ev);
5154 tevent_req_set_callback(subreq, cli_qfileinfo_done, req);
5158 static void cli_qfileinfo_done(struct tevent_req *subreq)
5160 struct tevent_req *req = tevent_req_callback_data(
5161 subreq, struct tevent_req);
5162 struct cli_qfileinfo_state *state = tevent_req_data(
5163 req, struct cli_qfileinfo_state);
5166 status = cli_trans_recv(subreq, state, NULL, NULL, 0, NULL,
5168 &state->rdata, state->min_rdata,
5170 if (!NT_STATUS_IS_OK(status)) {
5171 tevent_req_nterror(req, status);
5174 tevent_req_done(req);
5177 NTSTATUS cli_qfileinfo_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5178 uint8_t **rdata, uint32_t *num_rdata)
5180 struct cli_qfileinfo_state *state = tevent_req_data(
5181 req, struct cli_qfileinfo_state);
5184 if (tevent_req_is_nterror(req, &status)) {
5187 if (rdata != NULL) {
5188 *rdata = talloc_move(mem_ctx, &state->rdata);
5190 TALLOC_FREE(state->rdata);
5192 if (num_rdata != NULL) {
5193 *num_rdata = state->num_rdata;
5195 return NT_STATUS_OK;
5198 NTSTATUS cli_qfileinfo(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5199 uint16_t fnum, uint16_t level, uint32_t min_rdata,
5201 uint8_t **rdata, uint32_t *num_rdata)
5203 TALLOC_CTX *frame = talloc_stackframe();
5204 struct event_context *ev;
5205 struct tevent_req *req;
5206 NTSTATUS status = NT_STATUS_NO_MEMORY;
5208 if (cli_has_async_calls(cli)) {
5210 * Can't use sync call while an async call is in flight
5212 status = NT_STATUS_INVALID_PARAMETER;
5215 ev = event_context_init(frame);
5219 req = cli_qfileinfo_send(frame, ev, cli, fnum, level, min_rdata,
5224 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5227 status = cli_qfileinfo_recv(req, mem_ctx, rdata, num_rdata);
5230 if (!NT_STATUS_IS_OK(status)) {
5231 cli_set_error(cli, status);
5236 struct cli_flush_state {
5240 static void cli_flush_done(struct tevent_req *subreq);
5242 struct tevent_req *cli_flush_send(TALLOC_CTX *mem_ctx,
5243 struct event_context *ev,
5244 struct cli_state *cli,
5247 struct tevent_req *req, *subreq;
5248 struct cli_flush_state *state;
5250 req = tevent_req_create(mem_ctx, &state, struct cli_flush_state);
5254 SSVAL(state->vwv + 0, 0, fnum);
5256 subreq = cli_smb_send(state, ev, cli, SMBflush, 0, 1, state->vwv,
5258 if (tevent_req_nomem(subreq, req)) {
5259 return tevent_req_post(req, ev);
5261 tevent_req_set_callback(subreq, cli_flush_done, req);
5265 static void cli_flush_done(struct tevent_req *subreq)
5267 struct tevent_req *req = tevent_req_callback_data(
5268 subreq, struct tevent_req);
5271 status = cli_smb_recv(subreq, NULL, NULL, 0, NULL, NULL, NULL, NULL);
5272 TALLOC_FREE(subreq);
5273 if (!NT_STATUS_IS_OK(status)) {
5274 tevent_req_nterror(req, status);
5277 tevent_req_done(req);
5280 NTSTATUS cli_flush_recv(struct tevent_req *req)
5282 return tevent_req_simple_recv_ntstatus(req);
5285 NTSTATUS cli_flush(TALLOC_CTX *mem_ctx, struct cli_state *cli, uint16_t fnum)
5287 TALLOC_CTX *frame = talloc_stackframe();
5288 struct event_context *ev;
5289 struct tevent_req *req;
5290 NTSTATUS status = NT_STATUS_NO_MEMORY;
5292 if (cli_has_async_calls(cli)) {
5294 * Can't use sync call while an async call is in flight
5296 status = NT_STATUS_INVALID_PARAMETER;
5299 ev = event_context_init(frame);
5303 req = cli_flush_send(frame, ev, cli, fnum);
5307 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5310 status = cli_flush_recv(req);
5313 if (!NT_STATUS_IS_OK(status)) {
5314 cli_set_error(cli, status);
5319 struct cli_shadow_copy_data_state {
5326 static void cli_shadow_copy_data_done(struct tevent_req *subreq);
5328 struct tevent_req *cli_shadow_copy_data_send(TALLOC_CTX *mem_ctx,
5329 struct tevent_context *ev,
5330 struct cli_state *cli,
5334 struct tevent_req *req, *subreq;
5335 struct cli_shadow_copy_data_state *state;
5338 req = tevent_req_create(mem_ctx, &state,
5339 struct cli_shadow_copy_data_state);
5343 state->get_names = get_names;
5344 ret_size = get_names ? cli->max_xmit : 16;
5346 SIVAL(state->setup + 0, 0, FSCTL_GET_SHADOW_COPY_DATA);
5347 SSVAL(state->setup + 2, 0, fnum);
5348 SCVAL(state->setup + 3, 0, 0); /* isFsctl */
5349 SCVAL(state->setup + 3, 1, 0); /* compfilter, isFlags (WSSP) */
5351 subreq = cli_trans_send(
5352 state, ev, cli, SMBnttrans, NULL, 0, NT_TRANSACT_IOCTL, 0,
5353 state->setup, ARRAY_SIZE(state->setup), 0,
5356 if (tevent_req_nomem(subreq, req)) {
5357 return tevent_req_post(req, ev);
5359 tevent_req_set_callback(subreq, cli_shadow_copy_data_done, req);
5363 static void cli_shadow_copy_data_done(struct tevent_req *subreq)
5365 struct tevent_req *req = tevent_req_callback_data(
5366 subreq, struct tevent_req);
5367 struct cli_shadow_copy_data_state *state = tevent_req_data(
5368 req, struct cli_shadow_copy_data_state);
5371 status = cli_trans_recv(subreq, state, NULL,
5372 NULL, 0, NULL, /* setup */
5373 NULL, 0, NULL, /* param */
5374 &state->data, 12, &state->num_data);
5375 TALLOC_FREE(subreq);
5376 if (!NT_STATUS_IS_OK(status)) {
5377 tevent_req_nterror(req, status);
5380 tevent_req_done(req);
5383 NTSTATUS cli_shadow_copy_data_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
5384 char ***pnames, int *pnum_names)
5386 struct cli_shadow_copy_data_state *state = tevent_req_data(
5387 req, struct cli_shadow_copy_data_state);
5393 if (tevent_req_is_nterror(req, &status)) {
5396 num_names = IVAL(state->data, 4);
5397 dlength = IVAL(state->data, 8);
5399 if (!state->get_names) {
5400 *pnum_names = num_names;
5401 return NT_STATUS_OK;
5404 if (dlength+12 > state->num_data) {
5405 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5407 names = talloc_array(mem_ctx, char *, num_names);
5408 if (names == NULL) {
5409 return NT_STATUS_NO_MEMORY;
5412 for (i=0; i<num_names; i++) {
5415 size_t converted_size;
5417 src = state->data + 12 + i * 2 * sizeof(SHADOW_COPY_LABEL);
5418 ret = convert_string_talloc(
5419 names, CH_UTF16LE, CH_UNIX,
5420 src, 2 * sizeof(SHADOW_COPY_LABEL),
5421 &names[i], &converted_size);
5424 return NT_STATUS_INVALID_NETWORK_RESPONSE;
5427 *pnum_names = num_names;
5429 return NT_STATUS_OK;
5432 NTSTATUS cli_shadow_copy_data(TALLOC_CTX *mem_ctx, struct cli_state *cli,
5433 uint16_t fnum, bool get_names,
5434 char ***pnames, int *pnum_names)
5436 TALLOC_CTX *frame = talloc_stackframe();
5437 struct event_context *ev;
5438 struct tevent_req *req;
5439 NTSTATUS status = NT_STATUS_NO_MEMORY;
5441 if (cli_has_async_calls(cli)) {
5443 * Can't use sync call while an async call is in flight
5445 status = NT_STATUS_INVALID_PARAMETER;
5448 ev = event_context_init(frame);
5452 req = cli_shadow_copy_data_send(frame, ev, cli, fnum, get_names);
5456 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
5459 status = cli_shadow_copy_data_recv(req, mem_ctx, pnames, pnum_names);
5462 if (!NT_STATUS_IS_OK(status)) {
5463 cli_set_error(cli, status);