2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /***********************************************************
24 Common function for pushing stings, used by smb_bytes_push_str()
25 and trans_bytes_push_str(). Only difference is the align_odd
27 ***********************************************************/
29 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
30 const char *str, size_t str_len,
32 size_t *pconverted_size)
36 size_t converted_size;
42 buflen = talloc_get_size(buf);
44 if (align_odd && ucs2 && (buflen % 2 == 0)) {
46 * We're pushing into an SMB buffer, align odd
48 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
56 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
57 ucs2 ? CH_UTF16LE : CH_DOS,
58 str, str_len, &converted,
59 &converted_size, true)) {
63 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
64 buflen + converted_size);
66 TALLOC_FREE(converted);
70 memcpy(buf + buflen, converted, converted_size);
72 TALLOC_FREE(converted);
74 if (pconverted_size) {
75 *pconverted_size = converted_size;
81 /***********************************************************
82 Push a string into an SMB buffer, with odd byte alignment
83 if it's a UCS2 string.
84 ***********************************************************/
86 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
87 const char *str, size_t str_len,
88 size_t *pconverted_size)
90 return internal_bytes_push_str(buf, ucs2, str, str_len,
91 true, pconverted_size);
94 /***********************************************************
95 Same as smb_bytes_push_str(), but without the odd byte
96 align for ucs2 (we're pushing into a param or data block).
97 static for now, although this will probably change when
98 other modules use async trans calls.
99 ***********************************************************/
101 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
102 const char *str, size_t str_len,
103 size_t *pconverted_size)
105 return internal_bytes_push_str(buf, ucs2, str, str_len,
106 false, pconverted_size);
109 /****************************************************************************
110 Hard/Symlink a file (UNIX extensions).
111 Creates new name (sym)linked to oldname.
112 ****************************************************************************/
120 static void cli_posix_link_internal_done(struct tevent_req *subreq)
122 struct tevent_req *req = tevent_req_callback_data(
123 subreq, struct tevent_req);
124 struct link_state *state = tevent_req_data(req, struct link_state);
127 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, NULL, NULL);
129 if (!NT_STATUS_IS_OK(status)) {
130 tevent_req_nterror(req, status);
133 tevent_req_done(req);
136 static struct tevent_req *cli_posix_link_internal_send(TALLOC_CTX *mem_ctx,
137 struct event_context *ev,
138 struct cli_state *cli,
143 struct tevent_req *req = NULL, *subreq = NULL;
144 struct link_state *state = NULL;
146 req = tevent_req_create(mem_ctx, &state, struct link_state);
151 /* Setup setup word. */
152 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
154 /* Setup param array. */
155 state->param = talloc_array(state, uint8_t, 6);
156 if (tevent_req_nomem(state->param, req)) {
157 return tevent_req_post(req, ev);
159 memset(state->param, '\0', 6);
160 SSVAL(state->param,0,hardlink ? SMB_SET_FILE_UNIX_HLINK : SMB_SET_FILE_UNIX_LINK);
162 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), newname,
163 strlen(newname)+1, NULL);
165 if (tevent_req_nomem(state->param, req)) {
166 return tevent_req_post(req, ev);
169 /* Setup data array. */
170 state->data = talloc_array(state, uint8_t, 0);
171 if (tevent_req_nomem(state->data, req)) {
172 return tevent_req_post(req, ev);
174 state->data = trans2_bytes_push_str(state->data, cli_ucs2(cli), oldname,
175 strlen(oldname)+1, NULL);
177 subreq = cli_trans_send(state, /* mem ctx. */
179 cli, /* cli_state. */
180 SMBtrans2, /* cmd. */
181 NULL, /* pipe name. */
185 &state->setup, /* setup. */
186 1, /* num setup uint16_t words. */
187 0, /* max returned setup. */
188 state->param, /* param. */
189 talloc_get_size(state->param), /* num param. */
190 2, /* max returned param. */
191 state->data, /* data. */
192 talloc_get_size(state->data), /* num data. */
193 0); /* max returned data. */
195 if (tevent_req_nomem(subreq, req)) {
196 return tevent_req_post(req, ev);
198 tevent_req_set_callback(subreq, cli_posix_link_internal_done, req);
202 /****************************************************************************
203 Symlink a file (UNIX extensions).
204 ****************************************************************************/
206 struct tevent_req *cli_posix_symlink_send(TALLOC_CTX *mem_ctx,
207 struct event_context *ev,
208 struct cli_state *cli,
212 return cli_posix_link_internal_send(mem_ctx, ev, cli,
213 oldname, newname, false);
216 NTSTATUS cli_posix_symlink_recv(struct tevent_req *req)
220 if (tevent_req_is_nterror(req, &status)) {
226 NTSTATUS cli_posix_symlink(struct cli_state *cli,
230 TALLOC_CTX *frame = talloc_stackframe();
231 struct event_context *ev = NULL;
232 struct tevent_req *req = NULL;
233 NTSTATUS status = NT_STATUS_OK;
235 if (cli_has_async_calls(cli)) {
237 * Can't use sync call while an async call is in flight
239 status = NT_STATUS_INVALID_PARAMETER;
243 ev = event_context_init(frame);
245 status = NT_STATUS_NO_MEMORY;
249 req = cli_posix_symlink_send(frame,
255 status = NT_STATUS_NO_MEMORY;
259 if (!tevent_req_poll(req, ev)) {
260 status = map_nt_error_from_unix(errno);
264 status = cli_posix_symlink_recv(req);
268 if (!NT_STATUS_IS_OK(status)) {
269 cli_set_error(cli, status);
274 /****************************************************************************
275 Read a POSIX symlink.
276 ****************************************************************************/
278 struct readlink_state {
285 static void cli_posix_readlink_done(struct tevent_req *subreq)
287 struct tevent_req *req = tevent_req_callback_data(
288 subreq, struct tevent_req);
289 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
292 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL,
293 &state->data, &state->num_data);
295 if (!NT_STATUS_IS_OK(status)) {
296 tevent_req_nterror(req, status);
299 if (state->num_data == 0) {
300 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
303 if (state->data[state->num_data-1] != '\0') {
304 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
307 tevent_req_done(req);
310 struct tevent_req *cli_posix_readlink_send(TALLOC_CTX *mem_ctx,
311 struct event_context *ev,
312 struct cli_state *cli,
316 struct tevent_req *req = NULL, *subreq = NULL;
317 struct readlink_state *state = NULL;
318 uint32_t maxbytelen = (uint32_t)(cli_ucs2(cli) ? len*3 : len);
320 if (maxbytelen < len) {
324 req = tevent_req_create(mem_ctx, &state, struct readlink_state);
329 /* Setup setup word. */
330 SSVAL(&state->setup, 0, TRANSACT2_QPATHINFO);
332 /* Setup param array. */
333 state->param = talloc_array(state, uint8_t, 6);
334 if (tevent_req_nomem(state->param, req)) {
335 return tevent_req_post(req, ev);
337 memset(state->param, '\0', 6);
338 SSVAL(state->param,0,SMB_QUERY_FILE_UNIX_LINK);
340 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
341 strlen(fname)+1, NULL);
343 if (tevent_req_nomem(state->param, req)) {
344 return tevent_req_post(req, ev);
347 subreq = cli_trans_send(state, /* mem ctx. */
349 cli, /* cli_state. */
350 SMBtrans2, /* cmd. */
351 NULL, /* pipe name. */
355 &state->setup, /* setup. */
356 1, /* num setup uint16_t words. */
357 0, /* max returned setup. */
358 state->param, /* param. */
359 talloc_get_size(state->param), /* num param. */
360 2, /* max returned param. */
363 maxbytelen); /* max returned data. */
365 if (tevent_req_nomem(subreq, req)) {
366 return tevent_req_post(req, ev);
368 tevent_req_set_callback(subreq, cli_posix_readlink_done, req);
372 NTSTATUS cli_posix_readlink_recv(struct tevent_req *req, struct cli_state *cli,
373 char *retpath, size_t len)
376 char *converted = NULL;
377 size_t converted_size = 0;
378 struct readlink_state *state = tevent_req_data(req, struct readlink_state);
380 if (tevent_req_is_nterror(req, &status)) {
383 /* The returned data is a pushed string, not raw data. */
384 if (!convert_string_talloc(state,
385 cli_ucs2(cli) ? CH_UTF16LE : CH_DOS,
392 return NT_STATUS_NO_MEMORY;
395 len = MIN(len,converted_size);
397 return NT_STATUS_DATA_ERROR;
399 memcpy(retpath, converted, len);
403 NTSTATUS cli_posix_readlink(struct cli_state *cli, const char *fname,
404 char *linkpath, size_t len)
406 TALLOC_CTX *frame = talloc_stackframe();
407 struct event_context *ev = NULL;
408 struct tevent_req *req = NULL;
409 NTSTATUS status = NT_STATUS_OK;
411 if (cli_has_async_calls(cli)) {
413 * Can't use sync call while an async call is in flight
415 status = NT_STATUS_INVALID_PARAMETER;
419 ev = event_context_init(frame);
421 status = NT_STATUS_NO_MEMORY;
425 /* Len is in bytes, we need it in UCS2 units. */
427 status = NT_STATUS_INVALID_PARAMETER;
431 req = cli_posix_readlink_send(frame,
437 status = NT_STATUS_NO_MEMORY;
441 if (!tevent_req_poll(req, ev)) {
442 status = map_nt_error_from_unix(errno);
446 status = cli_posix_readlink_recv(req, cli, linkpath, len);
450 if (!NT_STATUS_IS_OK(status)) {
451 cli_set_error(cli, status);
456 /****************************************************************************
457 Hard link a file (UNIX extensions).
458 ****************************************************************************/
460 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
461 struct event_context *ev,
462 struct cli_state *cli,
466 return cli_posix_link_internal_send(mem_ctx, ev, cli,
467 oldname, newname, true);
470 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
474 if (tevent_req_is_nterror(req, &status)) {
480 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
484 TALLOC_CTX *frame = talloc_stackframe();
485 struct event_context *ev = NULL;
486 struct tevent_req *req = NULL;
487 NTSTATUS status = NT_STATUS_OK;
489 if (cli_has_async_calls(cli)) {
491 * Can't use sync call while an async call is in flight
493 status = NT_STATUS_INVALID_PARAMETER;
497 ev = event_context_init(frame);
499 status = NT_STATUS_NO_MEMORY;
503 req = cli_posix_hardlink_send(frame,
509 status = NT_STATUS_NO_MEMORY;
513 if (!tevent_req_poll(req, ev)) {
514 status = map_nt_error_from_unix(errno);
518 status = cli_posix_hardlink_recv(req);
522 if (!NT_STATUS_IS_OK(status)) {
523 cli_set_error(cli, status);
528 /****************************************************************************
529 Map standard UNIX permissions onto wire representations.
530 ****************************************************************************/
532 uint32_t unix_perms_to_wire(mode_t perms)
534 unsigned int ret = 0;
536 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
537 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
538 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
539 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
540 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
541 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
542 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
543 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
544 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
546 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
549 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
552 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
557 /****************************************************************************
558 Map wire permissions to standard UNIX.
559 ****************************************************************************/
561 mode_t wire_perms_to_unix(uint32_t perms)
563 mode_t ret = (mode_t)0;
565 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
566 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
567 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
568 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
569 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
570 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
571 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
572 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
573 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
575 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
578 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
581 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
586 /****************************************************************************
587 Return the file type from the wire filetype for UNIX extensions.
588 ****************************************************************************/
590 static mode_t unix_filetype_from_wire(uint32_t wire_type)
598 case UNIX_TYPE_SYMLINK:
602 case UNIX_TYPE_CHARDEV:
606 case UNIX_TYPE_BLKDEV:
614 case UNIX_TYPE_SOCKET:
622 /****************************************************************************
623 Do a POSIX getfacl (UNIX extensions).
624 ****************************************************************************/
626 struct getfacl_state {
633 static void cli_posix_getfacl_done(struct tevent_req *subreq)
635 struct tevent_req *req = tevent_req_callback_data(
636 subreq, struct tevent_req);
637 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
640 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL,
641 &state->data, &state->num_data);
643 if (!NT_STATUS_IS_OK(status)) {
644 tevent_req_nterror(req, status);
647 tevent_req_done(req);
650 struct tevent_req *cli_posix_getfacl_send(TALLOC_CTX *mem_ctx,
651 struct event_context *ev,
652 struct cli_state *cli,
655 struct tevent_req *req = NULL, *subreq = NULL;
656 struct link_state *state = NULL;
658 req = tevent_req_create(mem_ctx, &state, struct getfacl_state);
663 /* Setup setup word. */
664 SSVAL(&state->setup, 0, TRANSACT2_QPATHINFO);
666 /* Setup param array. */
667 state->param = talloc_array(state, uint8_t, 6);
668 if (tevent_req_nomem(state->param, req)) {
669 return tevent_req_post(req, ev);
671 memset(state->param, '\0', 6);
672 SSVAL(state->param, 0, SMB_QUERY_POSIX_ACL);
674 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
675 strlen(fname)+1, NULL);
677 if (tevent_req_nomem(state->param, req)) {
678 return tevent_req_post(req, ev);
681 subreq = cli_trans_send(state, /* mem ctx. */
683 cli, /* cli_state. */
684 SMBtrans2, /* cmd. */
685 NULL, /* pipe name. */
689 &state->setup, /* setup. */
690 1, /* num setup uint16_t words. */
691 0, /* max returned setup. */
692 state->param, /* param. */
693 talloc_get_size(state->param), /* num param. */
694 2, /* max returned param. */
697 cli->max_xmit); /* max returned data. */
699 if (tevent_req_nomem(subreq, req)) {
700 return tevent_req_post(req, ev);
702 tevent_req_set_callback(subreq, cli_posix_getfacl_done, req);
706 NTSTATUS cli_posix_getfacl_recv(struct tevent_req *req,
711 struct getfacl_state *state = tevent_req_data(req, struct getfacl_state);
714 if (tevent_req_is_nterror(req, &status)) {
717 *prb_size = (size_t)state->num_data;
718 *retbuf = (char *)talloc_move(mem_ctx, &state->data);
722 NTSTATUS cli_posix_getfacl(struct cli_state *cli,
728 TALLOC_CTX *frame = talloc_stackframe();
729 struct event_context *ev = NULL;
730 struct tevent_req *req = NULL;
731 NTSTATUS status = NT_STATUS_OK;
733 if (cli_has_async_calls(cli)) {
735 * Can't use sync call while an async call is in flight
737 status = NT_STATUS_INVALID_PARAMETER;
741 ev = event_context_init(frame);
743 status = NT_STATUS_NO_MEMORY;
747 req = cli_posix_getfacl_send(frame,
752 status = NT_STATUS_NO_MEMORY;
756 if (!tevent_req_poll(req, ev)) {
757 status = map_nt_error_from_unix(errno);
761 status = cli_posix_getfacl_recv(req, mem_ctx, prb_size, retbuf);
765 if (!NT_STATUS_IS_OK(status)) {
766 cli_set_error(cli, status);
771 /****************************************************************************
772 Stat a file (UNIX extensions).
773 ****************************************************************************/
782 static void cli_posix_stat_done(struct tevent_req *subreq)
784 struct tevent_req *req = tevent_req_callback_data(
785 subreq, struct tevent_req);
786 struct stat_state *state = tevent_req_data(req, struct stat_state);
789 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL,
790 &state->data, &state->num_data);
792 if (!NT_STATUS_IS_OK(status)) {
793 tevent_req_nterror(req, status);
796 tevent_req_done(req);
799 struct tevent_req *cli_posix_stat_send(TALLOC_CTX *mem_ctx,
800 struct event_context *ev,
801 struct cli_state *cli,
804 struct tevent_req *req = NULL, *subreq = NULL;
805 struct stat_state *state = NULL;
807 req = tevent_req_create(mem_ctx, &state, struct stat_state);
812 /* Setup setup word. */
813 SSVAL(&state->setup, 0, TRANSACT2_QPATHINFO);
815 /* Setup param array. */
816 state->param = talloc_array(state, uint8_t, 6);
817 if (tevent_req_nomem(state->param, req)) {
818 return tevent_req_post(req, ev);
820 memset(state->param, '\0', 6);
821 SSVAL(state->param, 0, SMB_QUERY_FILE_UNIX_BASIC);
823 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
824 strlen(fname)+1, NULL);
826 if (tevent_req_nomem(state->param, req)) {
827 return tevent_req_post(req, ev);
830 subreq = cli_trans_send(state, /* mem ctx. */
832 cli, /* cli_state. */
833 SMBtrans2, /* cmd. */
834 NULL, /* pipe name. */
838 &state->setup, /* setup. */
839 1, /* num setup uint16_t words. */
840 0, /* max returned setup. */
841 state->param, /* param. */
842 talloc_get_size(state->param), /* num param. */
843 2, /* max returned param. */
846 96); /* max returned data. */
848 if (tevent_req_nomem(subreq, req)) {
849 return tevent_req_post(req, ev);
851 tevent_req_set_callback(subreq, cli_posix_stat_done, req);
855 NTSTATUS cli_posix_stat_recv(struct tevent_req *req,
856 SMB_STRUCT_STAT *sbuf)
858 struct stat_state *state = tevent_req_data(req, struct stat_state);
861 if (tevent_req_is_nterror(req, &status)) {
865 if (state->num_data != 96) {
866 return NT_STATUS_DATA_ERROR;
869 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(state->data,0); /* total size, in bytes */
870 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(state->data,8); /* number of blocks allocated */
871 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
872 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
874 /* assume 512 byte blocks */
875 sbuf->st_ex_blocks /= 512;
877 sbuf->st_ex_ctime = interpret_long_date((char *)(state->data + 16)); /* time of last change */
878 sbuf->st_ex_atime = interpret_long_date((char *)(state->data + 24)); /* time of last access */
879 sbuf->st_ex_mtime = interpret_long_date((char *)(state->data + 32)); /* time of last modification */
881 sbuf->st_ex_uid = (uid_t) IVAL(state->data,40); /* user ID of owner */
882 sbuf->st_ex_gid = (gid_t) IVAL(state->data,48); /* group ID of owner */
883 sbuf->st_ex_mode = unix_filetype_from_wire(IVAL(state->data, 56));
884 #if defined(HAVE_MAKEDEV)
886 uint32_t dev_major = IVAL(state->data,60);
887 uint32_t dev_minor = IVAL(state->data,68);
888 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
891 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(state->data,76); /* inode */
892 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(state->data,84)); /* protection */
893 sbuf->st_ex_nlink = IVAL(state->data,92); /* number of hard links */
898 NTSTATUS cli_posix_stat(struct cli_state *cli,
900 SMB_STRUCT_STAT *sbuf)
902 TALLOC_CTX *frame = talloc_stackframe();
903 struct event_context *ev = NULL;
904 struct tevent_req *req = NULL;
905 NTSTATUS status = NT_STATUS_OK;
907 if (cli_has_async_calls(cli)) {
909 * Can't use sync call while an async call is in flight
911 status = NT_STATUS_INVALID_PARAMETER;
915 ev = event_context_init(frame);
917 status = NT_STATUS_NO_MEMORY;
921 req = cli_posix_stat_send(frame,
926 status = NT_STATUS_NO_MEMORY;
930 if (!tevent_req_poll(req, ev)) {
931 status = map_nt_error_from_unix(errno);
935 status = cli_posix_stat_recv(req, sbuf);
939 if (!NT_STATUS_IS_OK(status)) {
940 cli_set_error(cli, status);
945 /****************************************************************************
946 Chmod or chown a file internal (UNIX extensions).
947 ****************************************************************************/
955 static void cli_posix_chown_chmod_internal_done(struct tevent_req *subreq)
957 struct tevent_req *req = tevent_req_callback_data(
958 subreq, struct tevent_req);
959 struct ch_state *state = tevent_req_data(req, struct ch_state);
962 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, NULL, NULL);
964 if (!NT_STATUS_IS_OK(status)) {
965 tevent_req_nterror(req, status);
968 tevent_req_done(req);
971 static struct tevent_req *cli_posix_chown_chmod_internal_send(TALLOC_CTX *mem_ctx,
972 struct event_context *ev,
973 struct cli_state *cli,
979 struct tevent_req *req = NULL, *subreq = NULL;
980 struct ch_state *state = NULL;
982 req = tevent_req_create(mem_ctx, &state, struct ch_state);
987 /* Setup setup word. */
988 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
990 /* Setup param array. */
991 state->param = talloc_array(state, uint8_t, 6);
992 if (tevent_req_nomem(state->param, req)) {
993 return tevent_req_post(req, ev);
995 memset(state->param, '\0', 6);
996 SSVAL(state->param,0,SMB_SET_FILE_UNIX_BASIC);
998 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
999 strlen(fname)+1, NULL);
1001 if (tevent_req_nomem(state->param, req)) {
1002 return tevent_req_post(req, ev);
1005 /* Setup data array. */
1006 state->data = talloc_array(state, uint8_t, 100);
1007 if (tevent_req_nomem(state->data, req)) {
1008 return tevent_req_post(req, ev);
1010 memset(state->data, 0xff, 40); /* Set all sizes/times to no change. */
1011 memset(&state->data[40], '\0', 60);
1012 SIVAL(state->data,40,uid);
1013 SIVAL(state->data,48,gid);
1014 SIVAL(state->data,84,mode);
1016 subreq = cli_trans_send(state, /* mem ctx. */
1017 ev, /* event ctx. */
1018 cli, /* cli_state. */
1019 SMBtrans2, /* cmd. */
1020 NULL, /* pipe name. */
1024 &state->setup, /* setup. */
1025 1, /* num setup uint16_t words. */
1026 0, /* max returned setup. */
1027 state->param, /* param. */
1028 talloc_get_size(state->param), /* num param. */
1029 2, /* max returned param. */
1030 state->data, /* data. */
1031 talloc_get_size(state->data), /* num data. */
1032 0); /* max returned data. */
1034 if (tevent_req_nomem(subreq, req)) {
1035 return tevent_req_post(req, ev);
1037 tevent_req_set_callback(subreq, cli_posix_chown_chmod_internal_done, req);
1041 /****************************************************************************
1042 chmod a file (UNIX extensions).
1043 ****************************************************************************/
1045 struct tevent_req *cli_posix_chmod_send(TALLOC_CTX *mem_ctx,
1046 struct event_context *ev,
1047 struct cli_state *cli,
1051 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1053 unix_perms_to_wire(mode),
1058 NTSTATUS cli_posix_chmod_recv(struct tevent_req *req)
1062 if (tevent_req_is_nterror(req, &status)) {
1065 return NT_STATUS_OK;
1068 NTSTATUS cli_posix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
1070 TALLOC_CTX *frame = talloc_stackframe();
1071 struct event_context *ev = NULL;
1072 struct tevent_req *req = NULL;
1073 NTSTATUS status = NT_STATUS_OK;
1075 if (cli_has_async_calls(cli)) {
1077 * Can't use sync call while an async call is in flight
1079 status = NT_STATUS_INVALID_PARAMETER;
1083 ev = event_context_init(frame);
1085 status = NT_STATUS_NO_MEMORY;
1089 req = cli_posix_chmod_send(frame,
1095 status = NT_STATUS_NO_MEMORY;
1099 if (!tevent_req_poll(req, ev)) {
1100 status = map_nt_error_from_unix(errno);
1104 status = cli_posix_chmod_recv(req);
1108 if (!NT_STATUS_IS_OK(status)) {
1109 cli_set_error(cli, status);
1114 /****************************************************************************
1115 chown a file (UNIX extensions).
1116 ****************************************************************************/
1118 struct tevent_req *cli_posix_chown_send(TALLOC_CTX *mem_ctx,
1119 struct event_context *ev,
1120 struct cli_state *cli,
1125 return cli_posix_chown_chmod_internal_send(mem_ctx, ev, cli,
1132 NTSTATUS cli_posix_chown_recv(struct tevent_req *req)
1136 if (tevent_req_is_nterror(req, &status)) {
1139 return NT_STATUS_OK;
1142 NTSTATUS cli_posix_chown(struct cli_state *cli,
1147 TALLOC_CTX *frame = talloc_stackframe();
1148 struct event_context *ev = NULL;
1149 struct tevent_req *req = NULL;
1150 NTSTATUS status = NT_STATUS_OK;
1152 if (cli_has_async_calls(cli)) {
1154 * Can't use sync call while an async call is in flight
1156 status = NT_STATUS_INVALID_PARAMETER;
1160 ev = event_context_init(frame);
1162 status = NT_STATUS_NO_MEMORY;
1166 req = cli_posix_chown_send(frame,
1173 status = NT_STATUS_NO_MEMORY;
1177 if (!tevent_req_poll(req, ev)) {
1178 status = map_nt_error_from_unix(errno);
1182 status = cli_posix_chown_recv(req);
1186 if (!NT_STATUS_IS_OK(status)) {
1187 cli_set_error(cli, status);
1192 /****************************************************************************
1194 ****************************************************************************/
1196 static void cli_rename_done(struct tevent_req *subreq);
1198 struct cli_rename_state {
1202 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1203 struct event_context *ev,
1204 struct cli_state *cli,
1205 const char *fname_src,
1206 const char *fname_dst)
1208 struct tevent_req *req = NULL, *subreq = NULL;
1209 struct cli_rename_state *state = NULL;
1210 uint8_t additional_flags = 0;
1211 uint8_t *bytes = NULL;
1213 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1218 SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
1220 bytes = talloc_array(state, uint8_t, 1);
1221 if (tevent_req_nomem(bytes, req)) {
1222 return tevent_req_post(req, ev);
1225 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1226 strlen(fname_src)+1, NULL);
1227 if (tevent_req_nomem(bytes, req)) {
1228 return tevent_req_post(req, ev);
1231 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1232 talloc_get_size(bytes)+1);
1233 if (tevent_req_nomem(bytes, req)) {
1234 return tevent_req_post(req, ev);
1237 bytes[talloc_get_size(bytes)-1] = 4;
1238 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1239 strlen(fname_dst)+1, NULL);
1240 if (tevent_req_nomem(bytes, req)) {
1241 return tevent_req_post(req, ev);
1244 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1245 1, state->vwv, talloc_get_size(bytes), bytes);
1246 if (tevent_req_nomem(subreq, req)) {
1247 return tevent_req_post(req, ev);
1249 tevent_req_set_callback(subreq, cli_rename_done, req);
1253 static void cli_rename_done(struct tevent_req *subreq)
1255 struct tevent_req *req = tevent_req_callback_data(
1256 subreq, struct tevent_req);
1259 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1260 TALLOC_FREE(subreq);
1261 if (!NT_STATUS_IS_OK(status)) {
1262 tevent_req_nterror(req, status);
1265 tevent_req_done(req);
1268 NTSTATUS cli_rename_recv(struct tevent_req *req)
1270 return tevent_req_simple_recv_ntstatus(req);
1273 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1275 TALLOC_CTX *frame = talloc_stackframe();
1276 struct event_context *ev;
1277 struct tevent_req *req;
1278 NTSTATUS status = NT_STATUS_OK;
1280 if (cli_has_async_calls(cli)) {
1282 * Can't use sync call while an async call is in flight
1284 status = NT_STATUS_INVALID_PARAMETER;
1288 ev = event_context_init(frame);
1290 status = NT_STATUS_NO_MEMORY;
1294 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1296 status = NT_STATUS_NO_MEMORY;
1300 if (!tevent_req_poll(req, ev)) {
1301 status = map_nt_error_from_unix(errno);
1305 status = cli_rename_recv(req);
1309 if (!NT_STATUS_IS_OK(status)) {
1310 cli_set_error(cli, status);
1315 /****************************************************************************
1317 ****************************************************************************/
1319 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1321 struct cli_ntrename_internal_state {
1325 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1326 struct event_context *ev,
1327 struct cli_state *cli,
1328 const char *fname_src,
1329 const char *fname_dst,
1330 uint16_t rename_flag)
1332 struct tevent_req *req = NULL, *subreq = NULL;
1333 struct cli_ntrename_internal_state *state = NULL;
1334 uint8_t additional_flags = 0;
1335 uint8_t *bytes = NULL;
1337 req = tevent_req_create(mem_ctx, &state,
1338 struct cli_ntrename_internal_state);
1343 SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
1344 SSVAL(state->vwv+1, 0, rename_flag);
1346 bytes = talloc_array(state, uint8_t, 1);
1347 if (tevent_req_nomem(bytes, req)) {
1348 return tevent_req_post(req, ev);
1351 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1352 strlen(fname_src)+1, NULL);
1353 if (tevent_req_nomem(bytes, req)) {
1354 return tevent_req_post(req, ev);
1357 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1358 talloc_get_size(bytes)+1);
1359 if (tevent_req_nomem(bytes, req)) {
1360 return tevent_req_post(req, ev);
1363 bytes[talloc_get_size(bytes)-1] = 4;
1364 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1365 strlen(fname_dst)+1, NULL);
1366 if (tevent_req_nomem(bytes, req)) {
1367 return tevent_req_post(req, ev);
1370 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1371 4, state->vwv, talloc_get_size(bytes), bytes);
1372 if (tevent_req_nomem(subreq, req)) {
1373 return tevent_req_post(req, ev);
1375 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1379 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1381 struct tevent_req *req = tevent_req_callback_data(
1382 subreq, struct tevent_req);
1385 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1386 TALLOC_FREE(subreq);
1387 if (!NT_STATUS_IS_OK(status)) {
1388 tevent_req_nterror(req, status);
1391 tevent_req_done(req);
1394 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1396 return tevent_req_simple_recv_ntstatus(req);
1399 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1400 struct event_context *ev,
1401 struct cli_state *cli,
1402 const char *fname_src,
1403 const char *fname_dst)
1405 return cli_ntrename_internal_send(mem_ctx,
1410 RENAME_FLAG_RENAME);
1413 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1415 return cli_ntrename_internal_recv(req);
1418 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1420 TALLOC_CTX *frame = talloc_stackframe();
1421 struct event_context *ev;
1422 struct tevent_req *req;
1423 NTSTATUS status = NT_STATUS_OK;
1425 if (cli_has_async_calls(cli)) {
1427 * Can't use sync call while an async call is in flight
1429 status = NT_STATUS_INVALID_PARAMETER;
1433 ev = event_context_init(frame);
1435 status = NT_STATUS_NO_MEMORY;
1439 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1441 status = NT_STATUS_NO_MEMORY;
1445 if (!tevent_req_poll(req, ev)) {
1446 status = map_nt_error_from_unix(errno);
1450 status = cli_ntrename_recv(req);
1454 if (!NT_STATUS_IS_OK(status)) {
1455 cli_set_error(cli, status);
1460 /****************************************************************************
1462 ****************************************************************************/
1464 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1465 struct event_context *ev,
1466 struct cli_state *cli,
1467 const char *fname_src,
1468 const char *fname_dst)
1470 return cli_ntrename_internal_send(mem_ctx,
1475 RENAME_FLAG_HARD_LINK);
1478 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1480 return cli_ntrename_internal_recv(req);
1483 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1485 TALLOC_CTX *frame = talloc_stackframe();
1486 struct event_context *ev;
1487 struct tevent_req *req;
1488 NTSTATUS status = NT_STATUS_OK;
1490 if (cli_has_async_calls(cli)) {
1492 * Can't use sync call while an async call is in flight
1494 status = NT_STATUS_INVALID_PARAMETER;
1498 ev = event_context_init(frame);
1500 status = NT_STATUS_NO_MEMORY;
1504 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1506 status = NT_STATUS_NO_MEMORY;
1510 if (!tevent_req_poll(req, ev)) {
1511 status = map_nt_error_from_unix(errno);
1515 status = cli_nt_hardlink_recv(req);
1519 if (!NT_STATUS_IS_OK(status)) {
1520 cli_set_error(cli, status);
1525 /****************************************************************************
1527 ****************************************************************************/
1529 static void cli_unlink_done(struct tevent_req *subreq);
1531 struct cli_unlink_state {
1535 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1536 struct event_context *ev,
1537 struct cli_state *cli,
1539 uint16_t mayhave_attrs)
1541 struct tevent_req *req = NULL, *subreq = NULL;
1542 struct cli_unlink_state *state = NULL;
1543 uint8_t additional_flags = 0;
1544 uint8_t *bytes = NULL;
1546 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1551 SSVAL(state->vwv+0, 0, mayhave_attrs);
1553 bytes = talloc_array(state, uint8_t, 1);
1554 if (tevent_req_nomem(bytes, req)) {
1555 return tevent_req_post(req, ev);
1558 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1559 strlen(fname)+1, NULL);
1561 if (tevent_req_nomem(bytes, req)) {
1562 return tevent_req_post(req, ev);
1565 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1566 1, state->vwv, talloc_get_size(bytes), bytes);
1567 if (tevent_req_nomem(subreq, req)) {
1568 return tevent_req_post(req, ev);
1570 tevent_req_set_callback(subreq, cli_unlink_done, req);
1574 static void cli_unlink_done(struct tevent_req *subreq)
1576 struct tevent_req *req = tevent_req_callback_data(
1577 subreq, struct tevent_req);
1580 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1581 TALLOC_FREE(subreq);
1582 if (!NT_STATUS_IS_OK(status)) {
1583 tevent_req_nterror(req, status);
1586 tevent_req_done(req);
1589 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1591 return tevent_req_simple_recv_ntstatus(req);
1594 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1596 TALLOC_CTX *frame = talloc_stackframe();
1597 struct event_context *ev;
1598 struct tevent_req *req;
1599 NTSTATUS status = NT_STATUS_OK;
1601 if (cli_has_async_calls(cli)) {
1603 * Can't use sync call while an async call is in flight
1605 status = NT_STATUS_INVALID_PARAMETER;
1609 ev = event_context_init(frame);
1611 status = NT_STATUS_NO_MEMORY;
1615 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1617 status = NT_STATUS_NO_MEMORY;
1621 if (!tevent_req_poll(req, ev)) {
1622 status = map_nt_error_from_unix(errno);
1626 status = cli_unlink_recv(req);
1630 if (!NT_STATUS_IS_OK(status)) {
1631 cli_set_error(cli, status);
1636 /****************************************************************************
1638 ****************************************************************************/
1640 static void cli_mkdir_done(struct tevent_req *subreq);
1642 struct cli_mkdir_state {
1646 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1647 struct event_context *ev,
1648 struct cli_state *cli,
1651 struct tevent_req *req = NULL, *subreq = NULL;
1652 struct cli_mkdir_state *state = NULL;
1653 uint8_t additional_flags = 0;
1654 uint8_t *bytes = NULL;
1656 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1661 bytes = talloc_array(state, uint8_t, 1);
1662 if (tevent_req_nomem(bytes, req)) {
1663 return tevent_req_post(req, ev);
1666 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1667 strlen(dname)+1, NULL);
1669 if (tevent_req_nomem(bytes, req)) {
1670 return tevent_req_post(req, ev);
1673 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1674 0, NULL, talloc_get_size(bytes), bytes);
1675 if (tevent_req_nomem(subreq, req)) {
1676 return tevent_req_post(req, ev);
1678 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1682 static void cli_mkdir_done(struct tevent_req *subreq)
1684 struct tevent_req *req = tevent_req_callback_data(
1685 subreq, struct tevent_req);
1688 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1689 TALLOC_FREE(subreq);
1690 if (!NT_STATUS_IS_OK(status)) {
1691 tevent_req_nterror(req, status);
1694 tevent_req_done(req);
1697 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1699 return tevent_req_simple_recv_ntstatus(req);
1702 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1704 TALLOC_CTX *frame = talloc_stackframe();
1705 struct event_context *ev;
1706 struct tevent_req *req;
1707 NTSTATUS status = NT_STATUS_OK;
1709 if (cli_has_async_calls(cli)) {
1711 * Can't use sync call while an async call is in flight
1713 status = NT_STATUS_INVALID_PARAMETER;
1717 ev = event_context_init(frame);
1719 status = NT_STATUS_NO_MEMORY;
1723 req = cli_mkdir_send(frame, ev, cli, dname);
1725 status = NT_STATUS_NO_MEMORY;
1729 if (!tevent_req_poll(req, ev)) {
1730 status = map_nt_error_from_unix(errno);
1734 status = cli_mkdir_recv(req);
1738 if (!NT_STATUS_IS_OK(status)) {
1739 cli_set_error(cli, status);
1744 /****************************************************************************
1746 ****************************************************************************/
1748 static void cli_rmdir_done(struct tevent_req *subreq);
1750 struct cli_rmdir_state {
1754 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1755 struct event_context *ev,
1756 struct cli_state *cli,
1759 struct tevent_req *req = NULL, *subreq = NULL;
1760 struct cli_rmdir_state *state = NULL;
1761 uint8_t additional_flags = 0;
1762 uint8_t *bytes = NULL;
1764 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1769 bytes = talloc_array(state, uint8_t, 1);
1770 if (tevent_req_nomem(bytes, req)) {
1771 return tevent_req_post(req, ev);
1774 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1775 strlen(dname)+1, NULL);
1777 if (tevent_req_nomem(bytes, req)) {
1778 return tevent_req_post(req, ev);
1781 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1782 0, NULL, talloc_get_size(bytes), bytes);
1783 if (tevent_req_nomem(subreq, req)) {
1784 return tevent_req_post(req, ev);
1786 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1790 static void cli_rmdir_done(struct tevent_req *subreq)
1792 struct tevent_req *req = tevent_req_callback_data(
1793 subreq, struct tevent_req);
1796 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1797 TALLOC_FREE(subreq);
1798 if (!NT_STATUS_IS_OK(status)) {
1799 tevent_req_nterror(req, status);
1802 tevent_req_done(req);
1805 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1807 return tevent_req_simple_recv_ntstatus(req);
1810 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1812 TALLOC_CTX *frame = talloc_stackframe();
1813 struct event_context *ev;
1814 struct tevent_req *req;
1815 NTSTATUS status = NT_STATUS_OK;
1817 if (cli_has_async_calls(cli)) {
1819 * Can't use sync call while an async call is in flight
1821 status = NT_STATUS_INVALID_PARAMETER;
1825 ev = event_context_init(frame);
1827 status = NT_STATUS_NO_MEMORY;
1831 req = cli_rmdir_send(frame, ev, cli, dname);
1833 status = NT_STATUS_NO_MEMORY;
1837 if (!tevent_req_poll(req, ev)) {
1838 status = map_nt_error_from_unix(errno);
1842 status = cli_rmdir_recv(req);
1846 if (!NT_STATUS_IS_OK(status)) {
1847 cli_set_error(cli, status);
1852 /****************************************************************************
1853 Set or clear the delete on close flag.
1854 ****************************************************************************/
1862 static void cli_nt_delete_on_close_done(struct tevent_req *subreq)
1864 struct tevent_req *req = tevent_req_callback_data(
1865 subreq, struct tevent_req);
1866 struct doc_state *state = tevent_req_data(req, struct doc_state);
1869 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, NULL, NULL);
1870 TALLOC_FREE(subreq);
1871 if (!NT_STATUS_IS_OK(status)) {
1872 tevent_req_nterror(req, status);
1875 tevent_req_done(req);
1878 struct tevent_req *cli_nt_delete_on_close_send(TALLOC_CTX *mem_ctx,
1879 struct event_context *ev,
1880 struct cli_state *cli,
1884 struct tevent_req *req = NULL, *subreq = NULL;
1885 struct doc_state *state = NULL;
1887 req = tevent_req_create(mem_ctx, &state, struct doc_state);
1892 /* Setup setup word. */
1893 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
1895 /* Setup param array. */
1896 memset(state->param, '\0', 6);
1897 SSVAL(state->param,0,fnum);
1898 SSVAL(state->param,2,SMB_SET_FILE_DISPOSITION_INFO);
1900 /* Setup data array. */
1901 SCVAL(&state->data[0], 0, flag ? 1 : 0);
1903 subreq = cli_trans_send(state, /* mem ctx. */
1904 ev, /* event ctx. */
1905 cli, /* cli_state. */
1906 SMBtrans2, /* cmd. */
1907 NULL, /* pipe name. */
1911 &state->setup, /* setup. */
1912 1, /* num setup uint16_t words. */
1913 0, /* max returned setup. */
1914 state->param, /* param. */
1916 2, /* max returned param. */
1917 state->data, /* data. */
1919 0); /* max returned data. */
1921 if (tevent_req_nomem(subreq, req)) {
1922 return tevent_req_post(req, ev);
1924 tevent_req_set_callback(subreq, cli_nt_delete_on_close_done, req);
1928 NTSTATUS cli_nt_delete_on_close_recv(struct tevent_req *req)
1932 if (tevent_req_is_nterror(req, &status)) {
1935 return NT_STATUS_OK;
1938 NTSTATUS cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1940 TALLOC_CTX *frame = talloc_stackframe();
1941 struct event_context *ev = NULL;
1942 struct tevent_req *req = NULL;
1943 NTSTATUS status = NT_STATUS_OK;
1945 if (cli_has_async_calls(cli)) {
1947 * Can't use sync call while an async call is in flight
1949 status = NT_STATUS_INVALID_PARAMETER;
1953 ev = event_context_init(frame);
1955 status = NT_STATUS_NO_MEMORY;
1959 req = cli_nt_delete_on_close_send(frame,
1965 status = NT_STATUS_NO_MEMORY;
1969 if (!tevent_req_poll(req, ev)) {
1970 status = map_nt_error_from_unix(errno);
1974 status = cli_nt_delete_on_close_recv(req);
1978 if (!NT_STATUS_IS_OK(status)) {
1979 cli_set_error(cli, status);
1984 struct cli_ntcreate_state {
1989 static void cli_ntcreate_done(struct tevent_req *subreq);
1991 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1992 struct event_context *ev,
1993 struct cli_state *cli,
1995 uint32_t CreatFlags,
1996 uint32_t DesiredAccess,
1997 uint32_t FileAttributes,
1998 uint32_t ShareAccess,
1999 uint32_t CreateDisposition,
2000 uint32_t CreateOptions,
2001 uint8_t SecurityFlags)
2003 struct tevent_req *req, *subreq;
2004 struct cli_ntcreate_state *state;
2007 size_t converted_len;
2009 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
2015 SCVAL(vwv+0, 0, 0xFF);
2020 if (cli->use_oplocks) {
2021 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
2023 SIVAL(vwv+3, 1, CreatFlags);
2024 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
2025 SIVAL(vwv+7, 1, DesiredAccess);
2026 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
2027 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
2028 SIVAL(vwv+13, 1, FileAttributes);
2029 SIVAL(vwv+15, 1, ShareAccess);
2030 SIVAL(vwv+17, 1, CreateDisposition);
2031 SIVAL(vwv+19, 1, CreateOptions);
2032 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
2033 SCVAL(vwv+23, 1, SecurityFlags);
2035 bytes = talloc_array(state, uint8_t, 0);
2036 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
2037 fname, strlen(fname)+1,
2040 /* sigh. this copes with broken netapp filer behaviour */
2041 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
2043 if (tevent_req_nomem(bytes, req)) {
2044 return tevent_req_post(req, ev);
2047 SIVAL(vwv+2, 1, converted_len);
2049 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
2050 talloc_get_size(bytes), bytes);
2051 if (tevent_req_nomem(subreq, req)) {
2052 return tevent_req_post(req, ev);
2054 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
2058 static void cli_ntcreate_done(struct tevent_req *subreq)
2060 struct tevent_req *req = tevent_req_callback_data(
2061 subreq, struct tevent_req);
2062 struct cli_ntcreate_state *state = tevent_req_data(
2063 req, struct cli_ntcreate_state);
2070 status = cli_smb_recv(subreq, 3, &wct, &vwv, &num_bytes, &bytes);
2071 if (!NT_STATUS_IS_OK(status)) {
2072 TALLOC_FREE(subreq);
2073 tevent_req_nterror(req, status);
2076 state->fnum = SVAL(vwv+2, 1);
2077 tevent_req_done(req);
2080 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
2082 struct cli_ntcreate_state *state = tevent_req_data(
2083 req, struct cli_ntcreate_state);
2086 if (tevent_req_is_nterror(req, &status)) {
2089 *pfnum = state->fnum;
2090 return NT_STATUS_OK;
2093 NTSTATUS cli_ntcreate(struct cli_state *cli,
2095 uint32_t CreatFlags,
2096 uint32_t DesiredAccess,
2097 uint32_t FileAttributes,
2098 uint32_t ShareAccess,
2099 uint32_t CreateDisposition,
2100 uint32_t CreateOptions,
2101 uint8_t SecurityFlags,
2104 TALLOC_CTX *frame = talloc_stackframe();
2105 struct event_context *ev;
2106 struct tevent_req *req;
2107 NTSTATUS status = NT_STATUS_OK;
2109 if (cli_has_async_calls(cli)) {
2111 * Can't use sync call while an async call is in flight
2113 status = NT_STATUS_INVALID_PARAMETER;
2117 ev = event_context_init(frame);
2119 status = NT_STATUS_NO_MEMORY;
2123 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2124 DesiredAccess, FileAttributes, ShareAccess,
2125 CreateDisposition, CreateOptions,
2128 status = NT_STATUS_NO_MEMORY;
2132 if (!tevent_req_poll(req, ev)) {
2133 status = map_nt_error_from_unix(errno);
2137 status = cli_ntcreate_recv(req, pfid);
2140 if (!NT_STATUS_IS_OK(status)) {
2141 cli_set_error(cli, status);
2146 /****************************************************************************
2148 WARNING: if you open with O_WRONLY then getattrE won't work!
2149 ****************************************************************************/
2151 struct cli_open_state {
2157 static void cli_open_done(struct tevent_req *subreq);
2159 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2160 struct event_context *ev,
2161 struct cli_state *cli, const char *fname,
2162 int flags, int share_mode,
2163 struct tevent_req **psmbreq)
2165 struct tevent_req *req, *subreq;
2166 struct cli_open_state *state;
2168 unsigned accessmode;
2169 uint8_t additional_flags;
2172 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2178 if (flags & O_CREAT) {
2181 if (!(flags & O_EXCL)) {
2182 if (flags & O_TRUNC)
2188 accessmode = (share_mode<<4);
2190 if ((flags & O_ACCMODE) == O_RDWR) {
2192 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2197 if ((flags & O_SYNC) == O_SYNC) {
2198 accessmode |= (1<<14);
2202 if (share_mode == DENY_FCB) {
2206 SCVAL(state->vwv + 0, 0, 0xFF);
2207 SCVAL(state->vwv + 0, 1, 0);
2208 SSVAL(state->vwv + 1, 0, 0);
2209 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2210 SSVAL(state->vwv + 3, 0, accessmode);
2211 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
2212 SSVAL(state->vwv + 5, 0, 0);
2213 SIVAL(state->vwv + 6, 0, 0);
2214 SSVAL(state->vwv + 8, 0, openfn);
2215 SIVAL(state->vwv + 9, 0, 0);
2216 SIVAL(state->vwv + 11, 0, 0);
2217 SIVAL(state->vwv + 13, 0, 0);
2219 additional_flags = 0;
2221 if (cli->use_oplocks) {
2222 /* if using oplocks then ask for a batch oplock via
2223 core and extended methods */
2225 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2226 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2229 bytes = talloc_array(state, uint8_t, 0);
2230 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2231 strlen(fname)+1, NULL);
2233 if (tevent_req_nomem(bytes, req)) {
2234 return tevent_req_post(req, ev);
2237 state->bytes.iov_base = (void *)bytes;
2238 state->bytes.iov_len = talloc_get_size(bytes);
2240 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2241 15, state->vwv, 1, &state->bytes);
2242 if (subreq == NULL) {
2246 tevent_req_set_callback(subreq, cli_open_done, req);
2251 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2252 struct cli_state *cli, const char *fname,
2253 int flags, int share_mode)
2255 struct tevent_req *req, *subreq;
2258 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2264 status = cli_smb_req_send(subreq);
2265 if (!NT_STATUS_IS_OK(status)) {
2266 tevent_req_nterror(req, status);
2267 return tevent_req_post(req, ev);
2272 static void cli_open_done(struct tevent_req *subreq)
2274 struct tevent_req *req = tevent_req_callback_data(
2275 subreq, struct tevent_req);
2276 struct cli_open_state *state = tevent_req_data(
2277 req, struct cli_open_state);
2282 status = cli_smb_recv(subreq, 3, &wct, &vwv, NULL, NULL);
2283 if (!NT_STATUS_IS_OK(status)) {
2284 TALLOC_FREE(subreq);
2285 tevent_req_nterror(req, status);
2288 state->fnum = SVAL(vwv+2, 0);
2289 tevent_req_done(req);
2292 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2294 struct cli_open_state *state = tevent_req_data(
2295 req, struct cli_open_state);
2298 if (tevent_req_is_nterror(req, &status)) {
2301 *pfnum = state->fnum;
2302 return NT_STATUS_OK;
2305 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2306 int share_mode, uint16_t *pfnum)
2308 TALLOC_CTX *frame = talloc_stackframe();
2309 struct event_context *ev;
2310 struct tevent_req *req;
2311 NTSTATUS status = NT_STATUS_OK;
2313 if (cli_has_async_calls(cli)) {
2315 * Can't use sync call while an async call is in flight
2317 status = NT_STATUS_INVALID_PARAMETER;
2321 ev = event_context_init(frame);
2323 status = NT_STATUS_NO_MEMORY;
2327 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2329 status = NT_STATUS_NO_MEMORY;
2333 if (!tevent_req_poll(req, ev)) {
2334 status = map_nt_error_from_unix(errno);
2338 status = cli_open_recv(req, pfnum);
2341 if (!NT_STATUS_IS_OK(status)) {
2342 cli_set_error(cli, status);
2347 /****************************************************************************
2349 ****************************************************************************/
2351 struct cli_close_state {
2355 static void cli_close_done(struct tevent_req *subreq);
2357 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2358 struct event_context *ev,
2359 struct cli_state *cli,
2361 struct tevent_req **psubreq)
2363 struct tevent_req *req, *subreq;
2364 struct cli_close_state *state;
2366 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2370 SSVAL(state->vwv+0, 0, fnum);
2371 SIVALS(state->vwv+1, 0, -1);
2373 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2375 if (subreq == NULL) {
2379 tevent_req_set_callback(subreq, cli_close_done, req);
2384 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2385 struct event_context *ev,
2386 struct cli_state *cli,
2389 struct tevent_req *req, *subreq;
2392 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2397 status = cli_smb_req_send(subreq);
2398 if (!NT_STATUS_IS_OK(status)) {
2399 tevent_req_nterror(req, status);
2400 return tevent_req_post(req, ev);
2405 static void cli_close_done(struct tevent_req *subreq)
2407 struct tevent_req *req = tevent_req_callback_data(
2408 subreq, struct tevent_req);
2411 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2412 TALLOC_FREE(subreq);
2413 if (!NT_STATUS_IS_OK(status)) {
2414 tevent_req_nterror(req, status);
2417 tevent_req_done(req);
2420 NTSTATUS cli_close_recv(struct tevent_req *req)
2422 return tevent_req_simple_recv_ntstatus(req);
2425 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2427 TALLOC_CTX *frame = talloc_stackframe();
2428 struct event_context *ev;
2429 struct tevent_req *req;
2430 NTSTATUS status = NT_STATUS_OK;
2432 if (cli_has_async_calls(cli)) {
2434 * Can't use sync call while an async call is in flight
2436 status = NT_STATUS_INVALID_PARAMETER;
2440 ev = event_context_init(frame);
2442 status = NT_STATUS_NO_MEMORY;
2446 req = cli_close_send(frame, ev, cli, fnum);
2448 status = NT_STATUS_NO_MEMORY;
2452 if (!tevent_req_poll(req, ev)) {
2453 status = map_nt_error_from_unix(errno);
2457 status = cli_close_recv(req);
2460 if (!NT_STATUS_IS_OK(status)) {
2461 cli_set_error(cli, status);
2466 /****************************************************************************
2467 Truncate a file to a specified size
2468 ****************************************************************************/
2470 struct ftrunc_state {
2476 static void cli_ftruncate_done(struct tevent_req *subreq)
2478 struct tevent_req *req = tevent_req_callback_data(
2479 subreq, struct tevent_req);
2480 struct ftrunc_state *state = tevent_req_data(req, struct ftrunc_state);
2483 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, NULL, NULL);
2484 TALLOC_FREE(subreq);
2485 if (!NT_STATUS_IS_OK(status)) {
2486 tevent_req_nterror(req, status);
2489 tevent_req_done(req);
2492 struct tevent_req *cli_ftruncate_send(TALLOC_CTX *mem_ctx,
2493 struct event_context *ev,
2494 struct cli_state *cli,
2498 struct tevent_req *req = NULL, *subreq = NULL;
2499 struct ftrunc_state *state = NULL;
2501 req = tevent_req_create(mem_ctx, &state, struct ftrunc_state);
2506 /* Setup setup word. */
2507 SSVAL(&state->setup, 0, TRANSACT2_SETFILEINFO);
2509 /* Setup param array. */
2510 SSVAL(state->param,0,fnum);
2511 SSVAL(state->param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2512 SSVAL(state->param,4,0);
2514 /* Setup data array. */
2515 SBVAL(state->data, 0, size);
2517 subreq = cli_trans_send(state, /* mem ctx. */
2518 ev, /* event ctx. */
2519 cli, /* cli_state. */
2520 SMBtrans2, /* cmd. */
2521 NULL, /* pipe name. */
2525 &state->setup, /* setup. */
2526 1, /* num setup uint16_t words. */
2527 0, /* max returned setup. */
2528 state->param, /* param. */
2530 2, /* max returned param. */
2531 state->data, /* data. */
2533 0); /* max returned data. */
2535 if (tevent_req_nomem(subreq, req)) {
2536 return tevent_req_post(req, ev);
2538 tevent_req_set_callback(subreq, cli_ftruncate_done, req);
2542 NTSTATUS cli_ftruncate_recv(struct tevent_req *req)
2546 if (tevent_req_is_nterror(req, &status)) {
2549 return NT_STATUS_OK;
2552 NTSTATUS cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2554 TALLOC_CTX *frame = talloc_stackframe();
2555 struct event_context *ev = NULL;
2556 struct tevent_req *req = NULL;
2557 NTSTATUS status = NT_STATUS_OK;
2559 if (cli_has_async_calls(cli)) {
2561 * Can't use sync call while an async call is in flight
2563 status = NT_STATUS_INVALID_PARAMETER;
2567 ev = event_context_init(frame);
2569 status = NT_STATUS_NO_MEMORY;
2573 req = cli_ftruncate_send(frame,
2579 status = NT_STATUS_NO_MEMORY;
2583 if (!tevent_req_poll(req, ev)) {
2584 status = map_nt_error_from_unix(errno);
2588 status = cli_ftruncate_recv(req);
2592 if (!NT_STATUS_IS_OK(status)) {
2593 cli_set_error(cli, status);
2598 /****************************************************************************
2599 send a lock with a specified locktype
2600 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2601 ****************************************************************************/
2603 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2604 uint32_t offset, uint32_t len,
2605 int timeout, unsigned char locktype)
2608 int saved_timeout = cli->timeout;
2610 memset(cli->outbuf,'\0',smb_size);
2611 memset(cli->inbuf,'\0', smb_size);
2613 cli_set_message(cli->outbuf,8,0,True);
2615 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2616 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2617 cli_setup_packet(cli);
2619 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2620 SSVAL(cli->outbuf,smb_vwv2,fnum);
2621 SCVAL(cli->outbuf,smb_vwv3,locktype);
2622 SIVALS(cli->outbuf, smb_vwv4, timeout);
2623 SSVAL(cli->outbuf,smb_vwv6,0);
2624 SSVAL(cli->outbuf,smb_vwv7,1);
2626 p = smb_buf(cli->outbuf);
2627 SSVAL(p, 0, cli->pid);
2628 SIVAL(p, 2, offset);
2633 cli_setup_bcc(cli, p);
2638 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
2641 if (!cli_receive_smb(cli)) {
2642 cli->timeout = saved_timeout;
2643 return NT_STATUS_UNSUCCESSFUL;
2646 cli->timeout = saved_timeout;
2648 return cli_nt_error(cli);
2651 /****************************************************************************
2653 note that timeout is in units of 2 milliseconds
2654 ****************************************************************************/
2656 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2657 uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
2660 int saved_timeout = cli->timeout;
2662 memset(cli->outbuf,'\0',smb_size);
2663 memset(cli->inbuf,'\0', smb_size);
2665 cli_set_message(cli->outbuf,8,0,True);
2667 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2668 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2669 cli_setup_packet(cli);
2671 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2672 SSVAL(cli->outbuf,smb_vwv2,fnum);
2673 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
2674 SIVALS(cli->outbuf, smb_vwv4, timeout);
2675 SSVAL(cli->outbuf,smb_vwv6,0);
2676 SSVAL(cli->outbuf,smb_vwv7,1);
2678 p = smb_buf(cli->outbuf);
2679 SSVAL(p, 0, cli->pid);
2680 SIVAL(p, 2, offset);
2685 cli_setup_bcc(cli, p);
2690 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
2693 if (!cli_receive_smb(cli)) {
2694 cli->timeout = saved_timeout;
2698 cli->timeout = saved_timeout;
2700 if (cli_is_error(cli)) {
2707 /****************************************************************************
2709 ****************************************************************************/
2711 bool cli_unlock(struct cli_state *cli, uint16_t fnum, uint32_t offset, uint32_t len)
2715 memset(cli->outbuf,'\0',smb_size);
2716 memset(cli->inbuf,'\0',smb_size);
2718 cli_set_message(cli->outbuf,8,0,True);
2720 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2721 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2722 cli_setup_packet(cli);
2724 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2725 SSVAL(cli->outbuf,smb_vwv2,fnum);
2726 SCVAL(cli->outbuf,smb_vwv3,0);
2727 SIVALS(cli->outbuf, smb_vwv4, 0);
2728 SSVAL(cli->outbuf,smb_vwv6,1);
2729 SSVAL(cli->outbuf,smb_vwv7,0);
2731 p = smb_buf(cli->outbuf);
2732 SSVAL(p, 0, cli->pid);
2733 SIVAL(p, 2, offset);
2736 cli_setup_bcc(cli, p);
2738 if (!cli_receive_smb(cli)) {
2742 if (cli_is_error(cli)) {
2749 /****************************************************************************
2750 Lock a file with 64 bit offsets.
2751 ****************************************************************************/
2753 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2754 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
2757 int saved_timeout = cli->timeout;
2760 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2761 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2764 ltype = (lock_type == READ_LOCK? 1 : 0);
2765 ltype |= LOCKING_ANDX_LARGE_FILES;
2767 memset(cli->outbuf,'\0',smb_size);
2768 memset(cli->inbuf,'\0', smb_size);
2770 cli_set_message(cli->outbuf,8,0,True);
2772 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2773 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2774 cli_setup_packet(cli);
2776 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2777 SSVAL(cli->outbuf,smb_vwv2,fnum);
2778 SCVAL(cli->outbuf,smb_vwv3,ltype);
2779 SIVALS(cli->outbuf, smb_vwv4, timeout);
2780 SSVAL(cli->outbuf,smb_vwv6,0);
2781 SSVAL(cli->outbuf,smb_vwv7,1);
2783 p = smb_buf(cli->outbuf);
2784 SIVAL(p, 0, cli->pid);
2785 SOFF_T_R(p, 4, offset);
2786 SOFF_T_R(p, 12, len);
2789 cli_setup_bcc(cli, p);
2793 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
2796 if (!cli_receive_smb(cli)) {
2797 cli->timeout = saved_timeout;
2801 cli->timeout = saved_timeout;
2803 if (cli_is_error(cli)) {
2810 /****************************************************************************
2811 Unlock a file with 64 bit offsets.
2812 ****************************************************************************/
2814 bool cli_unlock64(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
2818 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2819 return cli_unlock(cli, fnum, offset, len);
2822 memset(cli->outbuf,'\0',smb_size);
2823 memset(cli->inbuf,'\0',smb_size);
2825 cli_set_message(cli->outbuf,8,0,True);
2827 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2828 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2829 cli_setup_packet(cli);
2831 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2832 SSVAL(cli->outbuf,smb_vwv2,fnum);
2833 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
2834 SIVALS(cli->outbuf, smb_vwv4, 0);
2835 SSVAL(cli->outbuf,smb_vwv6,1);
2836 SSVAL(cli->outbuf,smb_vwv7,0);
2838 p = smb_buf(cli->outbuf);
2839 SIVAL(p, 0, cli->pid);
2840 SOFF_T_R(p, 4, offset);
2841 SOFF_T_R(p, 12, len);
2843 cli_setup_bcc(cli, p);
2845 if (!cli_receive_smb(cli)) {
2849 if (cli_is_error(cli)) {
2856 /****************************************************************************
2857 Get/unlock a POSIX lock on a file - internal function.
2858 ****************************************************************************/
2860 static bool cli_posix_lock_internal(struct cli_state *cli, uint16_t fnum,
2861 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
2863 unsigned int param_len = 4;
2864 unsigned int data_len = POSIX_LOCK_DATA_SIZE;
2865 uint16_t setup = TRANSACT2_SETFILEINFO;
2867 unsigned char data[POSIX_LOCK_DATA_SIZE];
2868 char *rparam=NULL, *rdata=NULL;
2869 int saved_timeout = cli->timeout;
2871 SSVAL(param,0,fnum);
2872 SSVAL(param,2,SMB_SET_POSIX_LOCK);
2874 switch (lock_type) {
2876 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
2879 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
2882 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
2889 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
2890 cli->timeout = 0x7FFFFFFF;
2892 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
2895 SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
2896 SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
2897 SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
2899 if (!cli_send_trans(cli, SMBtrans2,
2901 -1, 0, /* fid, flags */
2902 &setup, 1, 0, /* setup, length, max */
2903 param, param_len, 2, /* param, length, max */
2904 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2906 cli->timeout = saved_timeout;
2910 if (!cli_receive_trans(cli, SMBtrans2,
2911 &rparam, ¶m_len,
2912 &rdata, &data_len)) {
2913 cli->timeout = saved_timeout;
2919 cli->timeout = saved_timeout;
2927 /****************************************************************************
2929 ****************************************************************************/
2931 bool cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2932 uint64_t offset, uint64_t len,
2933 bool wait_lock, enum brl_type lock_type)
2935 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2938 return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
2941 /****************************************************************************
2942 POSIX Unlock a file.
2943 ****************************************************************************/
2945 bool cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
2947 return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
2950 /****************************************************************************
2951 POSIX Get any lock covering a file.
2952 ****************************************************************************/
2954 bool cli_posix_getlock(struct cli_state *cli, uint16_t fnum, uint64_t *poffset, uint64_t *plen)
2959 /****************************************************************************
2960 Do a SMBgetattrE call.
2961 ****************************************************************************/
2963 static void cli_getattrE_done(struct tevent_req *subreq);
2965 struct cli_getattrE_state {
2975 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
2976 struct event_context *ev,
2977 struct cli_state *cli,
2980 struct tevent_req *req = NULL, *subreq = NULL;
2981 struct cli_getattrE_state *state = NULL;
2982 uint8_t additional_flags = 0;
2984 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
2989 state->zone_offset = cli->serverzone;
2990 SSVAL(state->vwv+0,0,fnum);
2992 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
2993 1, state->vwv, 0, NULL);
2994 if (tevent_req_nomem(subreq, req)) {
2995 return tevent_req_post(req, ev);
2997 tevent_req_set_callback(subreq, cli_getattrE_done, req);
3001 static void cli_getattrE_done(struct tevent_req *subreq)
3003 struct tevent_req *req = tevent_req_callback_data(
3004 subreq, struct tevent_req);
3005 struct cli_getattrE_state *state = tevent_req_data(
3006 req, struct cli_getattrE_state);
3008 uint16_t *vwv = NULL;
3011 status = cli_smb_recv(subreq, 11, &wct, &vwv, NULL, NULL);
3012 if (!NT_STATUS_IS_OK(status)) {
3013 tevent_req_nterror(req, status);
3017 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
3018 state->attr = SVAL(vwv+10,0);
3019 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
3020 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
3021 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
3023 TALLOC_FREE(subreq);
3024 tevent_req_done(req);
3027 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
3030 time_t *change_time,
3031 time_t *access_time,
3034 struct cli_getattrE_state *state = tevent_req_data(
3035 req, struct cli_getattrE_state);
3038 if (tevent_req_is_nterror(req, &status)) {
3042 *attr = state->attr;
3045 *size = state->size;
3048 *change_time = state->change_time;
3051 *access_time = state->access_time;
3054 *write_time = state->write_time;
3056 return NT_STATUS_OK;
3059 NTSTATUS cli_getattrE(struct cli_state *cli,
3063 time_t *change_time,
3064 time_t *access_time,
3067 TALLOC_CTX *frame = talloc_stackframe();
3068 struct event_context *ev = NULL;
3069 struct tevent_req *req = NULL;
3070 NTSTATUS status = NT_STATUS_OK;
3072 if (cli_has_async_calls(cli)) {
3074 * Can't use sync call while an async call is in flight
3076 status = NT_STATUS_INVALID_PARAMETER;
3080 ev = event_context_init(frame);
3082 status = NT_STATUS_NO_MEMORY;
3086 req = cli_getattrE_send(frame, ev, cli, fnum);
3088 status = NT_STATUS_NO_MEMORY;
3092 if (!tevent_req_poll(req, ev)) {
3093 status = map_nt_error_from_unix(errno);
3097 status = cli_getattrE_recv(req,
3106 if (!NT_STATUS_IS_OK(status)) {
3107 cli_set_error(cli, status);
3112 /****************************************************************************
3114 ****************************************************************************/
3116 static void cli_getatr_done(struct tevent_req *subreq);
3118 struct cli_getatr_state {
3125 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3126 struct event_context *ev,
3127 struct cli_state *cli,
3130 struct tevent_req *req = NULL, *subreq = NULL;
3131 struct cli_getatr_state *state = NULL;
3132 uint8_t additional_flags = 0;
3133 uint8_t *bytes = NULL;
3135 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3140 state->zone_offset = cli->serverzone;
3142 bytes = talloc_array(state, uint8_t, 1);
3143 if (tevent_req_nomem(bytes, req)) {
3144 return tevent_req_post(req, ev);
3147 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3148 strlen(fname)+1, NULL);
3150 if (tevent_req_nomem(bytes, req)) {
3151 return tevent_req_post(req, ev);
3154 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3155 0, NULL, talloc_get_size(bytes), bytes);
3156 if (tevent_req_nomem(subreq, req)) {
3157 return tevent_req_post(req, ev);
3159 tevent_req_set_callback(subreq, cli_getatr_done, req);
3163 static void cli_getatr_done(struct tevent_req *subreq)
3165 struct tevent_req *req = tevent_req_callback_data(
3166 subreq, struct tevent_req);
3167 struct cli_getatr_state *state = tevent_req_data(
3168 req, struct cli_getatr_state);
3170 uint16_t *vwv = NULL;
3173 status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
3174 if (!NT_STATUS_IS_OK(status)) {
3175 tevent_req_nterror(req, status);
3179 state->attr = SVAL(vwv+0,0);
3180 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3181 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3183 TALLOC_FREE(subreq);
3184 tevent_req_done(req);
3187 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3192 struct cli_getatr_state *state = tevent_req_data(
3193 req, struct cli_getatr_state);
3196 if (tevent_req_is_nterror(req, &status)) {
3200 *attr = state->attr;
3203 *size = state->size;
3206 *write_time = state->write_time;
3208 return NT_STATUS_OK;
3211 NTSTATUS cli_getatr(struct cli_state *cli,
3217 TALLOC_CTX *frame = talloc_stackframe();
3218 struct event_context *ev = NULL;
3219 struct tevent_req *req = NULL;
3220 NTSTATUS status = NT_STATUS_OK;
3222 if (cli_has_async_calls(cli)) {
3224 * Can't use sync call while an async call is in flight
3226 status = NT_STATUS_INVALID_PARAMETER;
3230 ev = event_context_init(frame);
3232 status = NT_STATUS_NO_MEMORY;
3236 req = cli_getatr_send(frame, ev, cli, fname);
3238 status = NT_STATUS_NO_MEMORY;
3242 if (!tevent_req_poll(req, ev)) {
3243 status = map_nt_error_from_unix(errno);
3247 status = cli_getatr_recv(req,
3254 if (!NT_STATUS_IS_OK(status)) {
3255 cli_set_error(cli, status);
3260 /****************************************************************************
3261 Do a SMBsetattrE call.
3262 ****************************************************************************/
3264 static void cli_setattrE_done(struct tevent_req *subreq);
3266 struct cli_setattrE_state {
3270 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3271 struct event_context *ev,
3272 struct cli_state *cli,
3278 struct tevent_req *req = NULL, *subreq = NULL;
3279 struct cli_setattrE_state *state = NULL;
3280 uint8_t additional_flags = 0;
3283 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3288 memset(vwv, '\0', sizeof(vwv));
3289 SSVAL(vwv+0, 0, fnum);
3290 cli_put_dos_date2(cli, (char *)&vwv[1], 0, change_time);
3291 cli_put_dos_date2(cli, (char *)&vwv[3], 0, access_time);
3292 cli_put_dos_date2(cli, (char *)&vwv[5], 0, write_time);
3294 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3296 if (tevent_req_nomem(subreq, req)) {
3297 return tevent_req_post(req, ev);
3299 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3303 static void cli_setattrE_done(struct tevent_req *subreq)
3305 struct tevent_req *req = tevent_req_callback_data(
3306 subreq, struct tevent_req);
3309 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
3310 TALLOC_FREE(subreq);
3311 if (!NT_STATUS_IS_OK(status)) {
3312 tevent_req_nterror(req, status);
3315 tevent_req_done(req);
3318 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3320 return tevent_req_simple_recv_ntstatus(req);
3323 NTSTATUS cli_setattrE(struct cli_state *cli,
3329 TALLOC_CTX *frame = talloc_stackframe();
3330 struct event_context *ev = NULL;
3331 struct tevent_req *req = NULL;
3332 NTSTATUS status = NT_STATUS_OK;
3334 if (cli_has_async_calls(cli)) {
3336 * Can't use sync call while an async call is in flight
3338 status = NT_STATUS_INVALID_PARAMETER;
3342 ev = event_context_init(frame);
3344 status = NT_STATUS_NO_MEMORY;
3348 req = cli_setattrE_send(frame, ev,
3356 status = NT_STATUS_NO_MEMORY;
3360 if (!tevent_req_poll(req, ev)) {
3361 status = map_nt_error_from_unix(errno);
3365 status = cli_setattrE_recv(req);
3369 if (!NT_STATUS_IS_OK(status)) {
3370 cli_set_error(cli, status);
3375 /****************************************************************************
3376 Do a SMBsetatr call.
3377 ****************************************************************************/
3379 static void cli_setatr_done(struct tevent_req *subreq);
3381 struct cli_setatr_state {
3385 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3386 struct event_context *ev,
3387 struct cli_state *cli,
3392 struct tevent_req *req = NULL, *subreq = NULL;
3393 struct cli_setatr_state *state = NULL;
3394 uint8_t additional_flags = 0;
3395 uint8_t *bytes = NULL;
3397 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3402 memset(state->vwv, '\0', sizeof(state->vwv));
3403 SSVAL(state->vwv+0, 0, attr);
3404 cli_put_dos_date3(cli, (char *)&state->vwv[1], 0, mtime);
3406 bytes = talloc_array(state, uint8_t, 1);
3407 if (tevent_req_nomem(bytes, req)) {
3408 return tevent_req_post(req, ev);
3411 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3412 strlen(fname)+1, NULL);
3413 if (tevent_req_nomem(bytes, req)) {
3414 return tevent_req_post(req, ev);
3416 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3417 talloc_get_size(bytes)+1);
3418 if (tevent_req_nomem(bytes, req)) {
3419 return tevent_req_post(req, ev);
3422 bytes[talloc_get_size(bytes)-1] = 4;
3423 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3425 if (tevent_req_nomem(bytes, req)) {
3426 return tevent_req_post(req, ev);
3429 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3430 8, state->vwv, talloc_get_size(bytes), bytes);
3431 if (tevent_req_nomem(subreq, req)) {
3432 return tevent_req_post(req, ev);
3434 tevent_req_set_callback(subreq, cli_setatr_done, req);
3438 static void cli_setatr_done(struct tevent_req *subreq)
3440 struct tevent_req *req = tevent_req_callback_data(
3441 subreq, struct tevent_req);
3444 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
3445 TALLOC_FREE(subreq);
3446 if (!NT_STATUS_IS_OK(status)) {
3447 tevent_req_nterror(req, status);
3450 tevent_req_done(req);
3453 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3455 return tevent_req_simple_recv_ntstatus(req);
3458 NTSTATUS cli_setatr(struct cli_state *cli,
3463 TALLOC_CTX *frame = talloc_stackframe();
3464 struct event_context *ev = NULL;
3465 struct tevent_req *req = NULL;
3466 NTSTATUS status = NT_STATUS_OK;
3468 if (cli_has_async_calls(cli)) {
3470 * Can't use sync call while an async call is in flight
3472 status = NT_STATUS_INVALID_PARAMETER;
3476 ev = event_context_init(frame);
3478 status = NT_STATUS_NO_MEMORY;
3482 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3484 status = NT_STATUS_NO_MEMORY;
3488 if (!tevent_req_poll(req, ev)) {
3489 status = map_nt_error_from_unix(errno);
3493 status = cli_setatr_recv(req);
3497 if (!NT_STATUS_IS_OK(status)) {
3498 cli_set_error(cli, status);
3503 /****************************************************************************
3504 Check for existance of a dir.
3505 ****************************************************************************/
3507 static void cli_chkpath_done(struct tevent_req *subreq);
3509 struct cli_chkpath_state {
3513 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3514 struct event_context *ev,
3515 struct cli_state *cli,
3518 struct tevent_req *req = NULL, *subreq = NULL;
3519 struct cli_chkpath_state *state = NULL;
3520 uint8_t additional_flags = 0;
3521 uint8_t *bytes = NULL;
3523 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
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);
3536 if (tevent_req_nomem(bytes, req)) {
3537 return tevent_req_post(req, ev);
3540 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3541 0, NULL, talloc_get_size(bytes), bytes);
3542 if (tevent_req_nomem(subreq, req)) {
3543 return tevent_req_post(req, ev);
3545 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3549 static void cli_chkpath_done(struct tevent_req *subreq)
3551 struct tevent_req *req = tevent_req_callback_data(
3552 subreq, struct tevent_req);
3555 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
3556 TALLOC_FREE(subreq);
3557 if (!NT_STATUS_IS_OK(status)) {
3558 tevent_req_nterror(req, status);
3561 tevent_req_done(req);
3564 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3566 return tevent_req_simple_recv_ntstatus(req);
3569 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3571 TALLOC_CTX *frame = talloc_stackframe();
3572 struct event_context *ev = NULL;
3573 struct tevent_req *req = NULL;
3575 NTSTATUS status = NT_STATUS_OK;
3577 if (cli_has_async_calls(cli)) {
3579 * Can't use sync call while an async call is in flight
3581 status = NT_STATUS_INVALID_PARAMETER;
3585 path2 = talloc_strdup(frame, path);
3587 status = NT_STATUS_NO_MEMORY;
3590 trim_char(path2,'\0','\\');
3592 path2 = talloc_strdup(frame, "\\");
3594 status = NT_STATUS_NO_MEMORY;
3599 ev = event_context_init(frame);
3601 status = NT_STATUS_NO_MEMORY;
3605 req = cli_chkpath_send(frame, ev, cli, path2);
3607 status = NT_STATUS_NO_MEMORY;
3611 if (!tevent_req_poll(req, ev)) {
3612 status = map_nt_error_from_unix(errno);
3616 status = cli_chkpath_recv(req);
3620 if (!NT_STATUS_IS_OK(status)) {
3621 cli_set_error(cli, status);
3626 /****************************************************************************
3628 ****************************************************************************/
3630 static void cli_dskattr_done(struct tevent_req *subreq);
3632 struct cli_dskattr_state {
3638 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3639 struct event_context *ev,
3640 struct cli_state *cli)
3642 struct tevent_req *req = NULL, *subreq = NULL;
3643 struct cli_dskattr_state *state = NULL;
3644 uint8_t additional_flags = 0;
3646 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3651 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3653 if (tevent_req_nomem(subreq, req)) {
3654 return tevent_req_post(req, ev);
3656 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3660 static void cli_dskattr_done(struct tevent_req *subreq)
3662 struct tevent_req *req = tevent_req_callback_data(
3663 subreq, struct tevent_req);
3664 struct cli_dskattr_state *state = tevent_req_data(
3665 req, struct cli_dskattr_state);
3667 uint16_t *vwv = NULL;
3670 status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
3671 if (!NT_STATUS_IS_OK(status)) {
3672 tevent_req_nterror(req, status);
3675 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3676 state->total = SVAL(vwv+0, 0);
3677 state->avail = SVAL(vwv+3, 0);
3678 TALLOC_FREE(subreq);
3679 tevent_req_done(req);
3682 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3684 struct cli_dskattr_state *state = tevent_req_data(
3685 req, struct cli_dskattr_state);
3688 if (tevent_req_is_nterror(req, &status)) {
3691 *bsize = state->bsize;
3692 *total = state->total;
3693 *avail = state->avail;
3694 return NT_STATUS_OK;
3697 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3699 TALLOC_CTX *frame = talloc_stackframe();
3700 struct event_context *ev = NULL;
3701 struct tevent_req *req = NULL;
3702 NTSTATUS status = NT_STATUS_OK;
3704 if (cli_has_async_calls(cli)) {
3706 * Can't use sync call while an async call is in flight
3708 status = NT_STATUS_INVALID_PARAMETER;
3712 ev = event_context_init(frame);
3714 status = NT_STATUS_NO_MEMORY;
3718 req = cli_dskattr_send(frame, ev, cli);
3720 status = NT_STATUS_NO_MEMORY;
3724 if (!tevent_req_poll(req, ev)) {
3725 status = map_nt_error_from_unix(errno);
3729 status = cli_dskattr_recv(req, bsize, total, avail);
3733 if (!NT_STATUS_IS_OK(status)) {
3734 cli_set_error(cli, status);
3739 /****************************************************************************
3740 Create and open a temporary file.
3741 ****************************************************************************/
3743 static void cli_ctemp_done(struct tevent_req *subreq);
3745 struct ctemp_state {
3751 struct tevent_req *cli_ctemp_send(TALLOC_CTX *mem_ctx,
3752 struct event_context *ev,
3753 struct cli_state *cli,
3756 struct tevent_req *req = NULL, *subreq = NULL;
3757 struct ctemp_state *state = NULL;
3758 uint8_t additional_flags = 0;
3759 uint8_t *bytes = NULL;
3761 req = tevent_req_create(mem_ctx, &state, struct ctemp_state);
3766 SSVAL(state->vwv,0,0);
3767 SIVALS(state->vwv+1,0,-1);
3769 bytes = talloc_array(state, uint8_t, 1);
3770 if (tevent_req_nomem(bytes, req)) {
3771 return tevent_req_post(req, ev);
3774 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), path,
3775 strlen(path)+1, NULL);
3776 if (tevent_req_nomem(bytes, req)) {
3777 return tevent_req_post(req, ev);
3780 subreq = cli_smb_send(state, ev, cli, SMBctemp, additional_flags,
3781 3, state->vwv, talloc_get_size(bytes), bytes);
3782 if (tevent_req_nomem(subreq, req)) {
3783 return tevent_req_post(req, ev);
3785 tevent_req_set_callback(subreq, cli_ctemp_done, req);
3789 static void cli_ctemp_done(struct tevent_req *subreq)
3791 struct tevent_req *req = tevent_req_callback_data(
3792 subreq, struct tevent_req);
3793 struct ctemp_state *state = tevent_req_data(
3794 req, struct ctemp_state);
3798 uint32_t num_bytes = 0;
3799 uint8_t *bytes = NULL;
3801 status = cli_smb_recv(subreq, 1, &wcnt, &vwv, &num_bytes, &bytes);
3802 TALLOC_FREE(subreq);
3803 if (!NT_STATUS_IS_OK(status)) {
3804 tevent_req_nterror(req, status);
3808 state->fnum = SVAL(vwv+0, 0);
3810 /* From W2K3, the result is just the ASCII name */
3811 if (num_bytes < 2) {
3812 tevent_req_nterror(req, NT_STATUS_DATA_ERROR);
3816 if (pull_string_talloc(state,
3823 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
3826 tevent_req_done(req);
3829 NTSTATUS cli_ctemp_recv(struct tevent_req *req,
3834 struct ctemp_state *state = tevent_req_data(req,
3835 struct ctemp_state);
3838 if (tevent_req_is_nterror(req, &status)) {
3841 *pfnum = state->fnum;
3842 *outfile = talloc_strdup(ctx, state->ret_path);
3844 return NT_STATUS_NO_MEMORY;
3846 return NT_STATUS_OK;
3849 NTSTATUS cli_ctemp(struct cli_state *cli,
3855 TALLOC_CTX *frame = talloc_stackframe();
3856 struct event_context *ev;
3857 struct tevent_req *req;
3858 NTSTATUS status = NT_STATUS_OK;
3860 if (cli_has_async_calls(cli)) {
3862 * Can't use sync call while an async call is in flight
3864 status = NT_STATUS_INVALID_PARAMETER;
3868 ev = event_context_init(frame);
3870 status = NT_STATUS_NO_MEMORY;
3874 req = cli_ctemp_send(frame, ev, cli, path);
3876 status = NT_STATUS_NO_MEMORY;
3880 if (!tevent_req_poll(req, ev)) {
3881 status = map_nt_error_from_unix(errno);
3885 status = cli_ctemp_recv(req, ctx, pfnum, out_path);
3889 if (!NT_STATUS_IS_OK(status)) {
3890 cli_set_error(cli, status);
3896 send a raw ioctl - used by the torture code
3898 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
3900 memset(cli->outbuf,'\0',smb_size);
3901 memset(cli->inbuf,'\0',smb_size);
3903 cli_set_message(cli->outbuf, 3, 0, True);
3904 SCVAL(cli->outbuf,smb_com,SMBioctl);
3905 cli_setup_packet(cli);
3907 SSVAL(cli->outbuf, smb_vwv0, fnum);
3908 SSVAL(cli->outbuf, smb_vwv1, code>>16);
3909 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
3912 if (!cli_receive_smb(cli)) {
3913 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3916 if (cli_is_error(cli)) {
3917 return cli_nt_error(cli);
3920 *blob = data_blob_null;
3922 return NT_STATUS_OK;
3925 /*********************************************************
3926 Set an extended attribute utility fn.
3927 *********************************************************/
3929 static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsigned int param_len,
3930 const char *ea_name, const char *ea_val, size_t ea_len)
3932 unsigned int data_len = 0;
3934 char *rparam=NULL, *rdata=NULL;
3936 size_t ea_namelen = strlen(ea_name);
3938 if (ea_namelen == 0 && ea_len == 0) {
3940 data = (char *)SMB_MALLOC(data_len);
3945 SIVAL(p,0,data_len);
3947 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
3948 data = (char *)SMB_MALLOC(data_len);
3953 SIVAL(p,0,data_len);
3955 SCVAL(p, 0, 0); /* EA flags. */
3956 SCVAL(p, 1, ea_namelen);
3957 SSVAL(p, 2, ea_len);
3958 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
3959 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
3962 if (!cli_send_trans(cli, SMBtrans2,
3964 -1, 0, /* fid, flags */
3965 &setup, 1, 0, /* setup, length, max */
3966 param, param_len, 2, /* param, length, max */
3967 data, data_len, cli->max_xmit /* data, length, max */
3973 if (!cli_receive_trans(cli, SMBtrans2,
3974 &rparam, ¶m_len,
3975 &rdata, &data_len)) {
3987 /*********************************************************
3988 Set an extended attribute on a pathname.
3989 *********************************************************/
3991 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
3993 uint16_t setup = TRANSACT2_SETPATHINFO;
3994 unsigned int param_len = 0;
3996 size_t srclen = 2*(strlen(path)+1);
4000 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
4004 memset(param, '\0', 6);
4005 SSVAL(param,0,SMB_INFO_SET_EA);
4008 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4009 param_len = PTR_DIFF(p, param);
4011 ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
4016 /*********************************************************
4017 Set an extended attribute on an fnum.
4018 *********************************************************/
4020 bool cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum, const char *ea_name, const char *ea_val, size_t ea_len)
4023 uint16_t setup = TRANSACT2_SETFILEINFO;
4025 memset(param, 0, 6);
4026 SSVAL(param,0,fnum);
4027 SSVAL(param,2,SMB_INFO_SET_EA);
4029 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
4032 /*********************************************************
4033 Get an extended attribute list utility fn.
4034 *********************************************************/
4036 static bool cli_get_ea_list(struct cli_state *cli,
4037 uint16_t setup, char *param, unsigned int param_len,
4040 struct ea_struct **pea_list)
4042 unsigned int data_len = 0;
4043 unsigned int rparam_len, rdata_len;
4044 char *rparam=NULL, *rdata=NULL;
4049 struct ea_struct *ea_list;
4056 if (!cli_send_trans(cli, SMBtrans2,
4058 -1, 0, /* fid, flags */
4059 &setup, 1, 0, /* setup, length, max */
4060 param, param_len, 10, /* param, length, max */
4061 NULL, data_len, cli->max_xmit /* data, length, max */
4066 if (!cli_receive_trans(cli, SMBtrans2,
4067 &rparam, &rparam_len,
4068 &rdata, &rdata_len)) {
4072 if (!rdata || rdata_len < 4) {
4076 ea_size = (size_t)IVAL(rdata,0);
4077 if (ea_size > rdata_len) {
4082 /* No EA's present. */
4090 /* Validate the EA list and count it. */
4091 for (num_eas = 0; ea_size >= 4; num_eas++) {
4092 unsigned int ea_namelen = CVAL(p,1);
4093 unsigned int ea_valuelen = SVAL(p,2);
4094 if (ea_namelen == 0) {
4097 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
4100 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
4101 p += 4 + ea_namelen + 1 + ea_valuelen;
4109 *pnum_eas = num_eas;
4111 /* Caller only wants number of EA's. */
4116 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
4121 ea_size = (size_t)IVAL(rdata,0);
4124 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
4125 struct ea_struct *ea = &ea_list[num_eas];
4126 fstring unix_ea_name;
4127 unsigned int ea_namelen = CVAL(p,1);
4128 unsigned int ea_valuelen = SVAL(p,2);
4130 ea->flags = CVAL(p,0);
4131 unix_ea_name[0] = '\0';
4132 pull_ascii_fstring(unix_ea_name, p + 4);
4133 ea->name = talloc_strdup(ctx, unix_ea_name);
4134 /* Ensure the value is null terminated (in case it's a string). */
4135 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
4136 if (!ea->value.data) {
4140 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
4142 ea->value.data[ea_valuelen] = 0;
4144 p += 4 + ea_namelen + 1 + ea_valuelen;
4147 *pea_list = ea_list;
4157 /*********************************************************
4158 Get an extended attribute list from a pathname.
4159 *********************************************************/
4161 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
4164 struct ea_struct **pea_list)
4166 uint16_t setup = TRANSACT2_QPATHINFO;
4167 unsigned int param_len = 0;
4170 size_t srclen = 2*(strlen(path)+1);
4173 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
4179 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
4181 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4182 param_len = PTR_DIFF(p, param);
4184 ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
4189 /*********************************************************
4190 Get an extended attribute list from an fnum.
4191 *********************************************************/
4193 bool cli_get_ea_list_fnum(struct cli_state *cli, uint16_t fnum,
4196 struct ea_struct **pea_list)
4198 uint16_t setup = TRANSACT2_QFILEINFO;
4201 memset(param, 0, 6);
4202 SSVAL(param,0,fnum);
4203 SSVAL(param,2,SMB_INFO_SET_EA);
4205 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
4208 /****************************************************************************
4209 Convert open "flags" arg to uint32_t on wire.
4210 ****************************************************************************/
4212 static uint32_t open_flags_to_wire(int flags)
4214 int open_mode = flags & O_ACCMODE;
4217 switch (open_mode) {
4219 ret |= SMB_O_WRONLY;
4226 ret |= SMB_O_RDONLY;
4230 if (flags & O_CREAT) {
4233 if (flags & O_EXCL) {
4236 if (flags & O_TRUNC) {
4240 if (flags & O_SYNC) {
4244 if (flags & O_APPEND) {
4245 ret |= SMB_O_APPEND;
4247 #if defined(O_DIRECT)
4248 if (flags & O_DIRECT) {
4249 ret |= SMB_O_DIRECT;
4252 #if defined(O_DIRECTORY)
4253 if (flags & O_DIRECTORY) {
4254 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4255 ret |= SMB_O_DIRECTORY;
4261 /****************************************************************************
4262 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4263 ****************************************************************************/
4265 struct posix_open_state {
4269 uint16_t fnum; /* Out */
4272 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4274 struct tevent_req *req = tevent_req_callback_data(
4275 subreq, struct tevent_req);
4276 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4281 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, &data, &num_data);
4282 TALLOC_FREE(subreq);
4283 if (!NT_STATUS_IS_OK(status)) {
4284 tevent_req_nterror(req, status);
4287 if (num_data < 12) {
4288 tevent_req_nterror(req, status);
4291 state->fnum = SVAL(data,2);
4292 tevent_req_done(req);
4295 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4296 struct event_context *ev,
4297 struct cli_state *cli,
4303 struct tevent_req *req = NULL, *subreq = NULL;
4304 struct posix_open_state *state = NULL;
4305 uint32_t wire_flags = open_flags_to_wire(flags);
4307 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4312 /* Setup setup word. */
4313 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4315 /* Setup param array. */
4316 state->param = talloc_array(state, uint8_t, 6);
4317 if (tevent_req_nomem(state->param, req)) {
4318 return tevent_req_post(req, ev);
4320 memset(state->param, '\0', 6);
4321 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4323 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4324 strlen(fname)+1, NULL);
4326 if (tevent_req_nomem(state->param, req)) {
4327 return tevent_req_post(req, ev);
4330 /* Setup data words. */
4332 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4333 wire_flags |= SMB_O_DIRECTORY;
4336 SIVAL(state->data,0,0); /* No oplock. */
4337 SIVAL(state->data,4,wire_flags);
4338 SIVAL(state->data,8,unix_perms_to_wire(mode));
4339 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4340 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4342 subreq = cli_trans_send(state, /* mem ctx. */
4343 ev, /* event ctx. */
4344 cli, /* cli_state. */
4345 SMBtrans2, /* cmd. */
4346 NULL, /* pipe name. */
4350 &state->setup, /* setup. */
4351 1, /* num setup uint16_t words. */
4352 0, /* max returned setup. */
4353 state->param, /* param. */
4354 talloc_get_size(state->param),/* num param. */
4355 2, /* max returned param. */
4356 state->data, /* data. */
4358 12); /* max returned data. */
4360 if (tevent_req_nomem(subreq, req)) {
4361 return tevent_req_post(req, ev);
4363 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4367 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4368 struct event_context *ev,
4369 struct cli_state *cli,
4374 return cli_posix_open_internal_send(mem_ctx, ev,
4375 cli, fname, flags, mode, false);
4378 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4380 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4383 if (tevent_req_is_nterror(req, &status)) {
4386 *pfnum = state->fnum;
4387 return NT_STATUS_OK;
4390 /****************************************************************************
4391 Open - POSIX semantics. Doesn't request oplock.
4392 ****************************************************************************/
4394 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4395 int flags, mode_t mode, uint16_t *pfnum)
4398 TALLOC_CTX *frame = talloc_stackframe();
4399 struct event_context *ev = NULL;
4400 struct tevent_req *req = NULL;
4401 NTSTATUS status = NT_STATUS_OK;
4403 if (cli_has_async_calls(cli)) {
4405 * Can't use sync call while an async call is in flight
4407 status = NT_STATUS_INVALID_PARAMETER;
4411 ev = event_context_init(frame);
4413 status = NT_STATUS_NO_MEMORY;
4417 req = cli_posix_open_send(frame,
4424 status = NT_STATUS_NO_MEMORY;
4428 if (!tevent_req_poll(req, ev)) {
4429 status = map_nt_error_from_unix(errno);
4433 status = cli_posix_open_recv(req, pfnum);
4437 if (!NT_STATUS_IS_OK(status)) {
4438 cli_set_error(cli, status);
4443 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4444 struct event_context *ev,
4445 struct cli_state *cli,
4449 return cli_posix_open_internal_send(mem_ctx, ev,
4450 cli, fname, O_CREAT, mode, true);
4453 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4457 if (tevent_req_is_nterror(req, &status)) {
4460 return NT_STATUS_OK;
4463 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4465 TALLOC_CTX *frame = talloc_stackframe();
4466 struct event_context *ev = NULL;
4467 struct tevent_req *req = NULL;
4468 NTSTATUS status = NT_STATUS_OK;
4470 if (cli_has_async_calls(cli)) {
4472 * Can't use sync call while an async call is in flight
4474 status = NT_STATUS_INVALID_PARAMETER;
4478 ev = event_context_init(frame);
4480 status = NT_STATUS_NO_MEMORY;
4484 req = cli_posix_mkdir_send(frame,
4490 status = NT_STATUS_NO_MEMORY;
4494 if (!tevent_req_poll(req, ev)) {
4495 status = map_nt_error_from_unix(errno);
4499 status = cli_posix_mkdir_recv(req);
4503 if (!NT_STATUS_IS_OK(status)) {
4504 cli_set_error(cli, status);
4509 /****************************************************************************
4510 unlink or rmdir - POSIX semantics.
4511 ****************************************************************************/
4513 struct unlink_state {
4518 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4520 struct tevent_req *req = tevent_req_callback_data(
4521 subreq, struct tevent_req);
4522 struct unlink_state *state = tevent_req_data(req, struct unlink_state);
4525 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, NULL, NULL);
4526 TALLOC_FREE(subreq);
4527 if (!NT_STATUS_IS_OK(status)) {
4528 tevent_req_nterror(req, status);
4531 tevent_req_done(req);
4534 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4535 struct event_context *ev,
4536 struct cli_state *cli,
4540 struct tevent_req *req = NULL, *subreq = NULL;
4541 struct unlink_state *state = NULL;
4542 uint8_t *param = NULL;
4544 req = tevent_req_create(mem_ctx, &state, struct unlink_state);
4549 /* Setup setup word. */
4550 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4552 /* Setup param array. */
4553 param = talloc_array(state, uint8_t, 6);
4554 if (tevent_req_nomem(param, req)) {
4555 return tevent_req_post(req, ev);
4557 memset(param, '\0', 6);
4558 SSVAL(param, 0, SMB_POSIX_PATH_UNLINK);
4560 param = trans2_bytes_push_str(param, cli_ucs2(cli), fname,
4561 strlen(fname)+1, NULL);
4563 if (tevent_req_nomem(param, req)) {
4564 return tevent_req_post(req, ev);
4567 /* Setup data word. */
4568 SSVAL(state->data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
4569 SMB_POSIX_UNLINK_FILE_TARGET);
4571 subreq = cli_trans_send(state, /* mem ctx. */
4572 ev, /* event ctx. */
4573 cli, /* cli_state. */
4574 SMBtrans2, /* cmd. */
4575 NULL, /* pipe name. */
4579 &state->setup, /* setup. */
4580 1, /* num setup uint16_t words. */
4581 0, /* max returned setup. */
4583 talloc_get_size(param), /* num param. */
4584 2, /* max returned param. */
4585 state->data, /* data. */
4587 0); /* max returned data. */
4589 if (tevent_req_nomem(subreq, req)) {
4590 return tevent_req_post(req, ev);
4592 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4596 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4597 struct event_context *ev,
4598 struct cli_state *cli,
4601 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, false);
4604 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4608 if (tevent_req_is_nterror(req, &status)) {
4611 return NT_STATUS_OK;
4614 /****************************************************************************
4615 unlink - POSIX semantics.
4616 ****************************************************************************/
4618 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4620 TALLOC_CTX *frame = talloc_stackframe();
4621 struct event_context *ev = NULL;
4622 struct tevent_req *req = NULL;
4623 NTSTATUS status = NT_STATUS_OK;
4625 if (cli_has_async_calls(cli)) {
4627 * Can't use sync call while an async call is in flight
4629 status = NT_STATUS_INVALID_PARAMETER;
4633 ev = event_context_init(frame);
4635 status = NT_STATUS_NO_MEMORY;
4639 req = cli_posix_unlink_send(frame,
4644 status = NT_STATUS_NO_MEMORY;
4648 if (!tevent_req_poll(req, ev)) {
4649 status = map_nt_error_from_unix(errno);
4653 status = cli_posix_unlink_recv(req);
4657 if (!NT_STATUS_IS_OK(status)) {
4658 cli_set_error(cli, status);
4663 /****************************************************************************
4664 rmdir - POSIX semantics.
4665 ****************************************************************************/
4667 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4668 struct event_context *ev,
4669 struct cli_state *cli,
4672 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, true);
4675 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4679 if (tevent_req_is_nterror(req, &status)) {
4682 return NT_STATUS_OK;
4685 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4687 TALLOC_CTX *frame = talloc_stackframe();
4688 struct event_context *ev = NULL;
4689 struct tevent_req *req = NULL;
4690 NTSTATUS status = NT_STATUS_OK;
4692 if (cli_has_async_calls(cli)) {
4694 * Can't use sync call while an async call is in flight
4696 status = NT_STATUS_INVALID_PARAMETER;
4700 ev = event_context_init(frame);
4702 status = NT_STATUS_NO_MEMORY;
4706 req = cli_posix_rmdir_send(frame,
4711 status = NT_STATUS_NO_MEMORY;
4715 if (!tevent_req_poll(req, ev)) {
4716 status = map_nt_error_from_unix(errno);
4720 status = cli_posix_rmdir_recv(req, frame);
4724 if (!NT_STATUS_IS_OK(status)) {
4725 cli_set_error(cli, status);