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);
457 /****************************************************************************
458 Hard link a file (UNIX extensions).
459 ****************************************************************************/
461 struct tevent_req *cli_posix_hardlink_send(TALLOC_CTX *mem_ctx,
462 struct event_context *ev,
463 struct cli_state *cli,
467 return cli_posix_link_internal_send(mem_ctx, ev, cli,
468 oldname, newname, true);
471 NTSTATUS cli_posix_hardlink_recv(struct tevent_req *req)
475 if (tevent_req_is_nterror(req, &status)) {
481 NTSTATUS cli_posix_hardlink(struct cli_state *cli,
485 TALLOC_CTX *frame = talloc_stackframe();
486 struct event_context *ev = NULL;
487 struct tevent_req *req = NULL;
488 NTSTATUS status = NT_STATUS_OK;
490 if (cli_has_async_calls(cli)) {
492 * Can't use sync call while an async call is in flight
494 status = NT_STATUS_INVALID_PARAMETER;
498 ev = event_context_init(frame);
500 status = NT_STATUS_NO_MEMORY;
504 req = cli_posix_hardlink_send(frame,
510 status = NT_STATUS_NO_MEMORY;
514 if (!tevent_req_poll(req, ev)) {
515 status = map_nt_error_from_unix(errno);
519 status = cli_posix_hardlink_recv(req);
523 if (!NT_STATUS_IS_OK(status)) {
524 cli_set_error(cli, status);
529 /****************************************************************************
530 Map standard UNIX permissions onto wire representations.
531 ****************************************************************************/
533 uint32_t unix_perms_to_wire(mode_t perms)
535 unsigned int ret = 0;
537 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
538 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
539 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
540 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
541 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
542 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
543 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
544 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
545 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
547 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
550 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
553 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
558 /****************************************************************************
559 Map wire permissions to standard UNIX.
560 ****************************************************************************/
562 mode_t wire_perms_to_unix(uint32_t perms)
564 mode_t ret = (mode_t)0;
566 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
567 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
568 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
569 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
570 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
571 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
572 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
573 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
574 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
576 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
579 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
582 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
587 /****************************************************************************
588 Return the file type from the wire filetype for UNIX extensions.
589 ****************************************************************************/
591 static mode_t unix_filetype_from_wire(uint32_t wire_type)
599 case UNIX_TYPE_SYMLINK:
603 case UNIX_TYPE_CHARDEV:
607 case UNIX_TYPE_BLKDEV:
615 case UNIX_TYPE_SOCKET:
623 /****************************************************************************
624 Do a POSIX getfacl (UNIX extensions).
625 ****************************************************************************/
627 bool cli_unix_getfacl(struct cli_state *cli, const char *name, size_t *prb_size, char **retbuf)
629 unsigned int param_len = 0;
630 unsigned int data_len = 0;
631 uint16_t setup = TRANSACT2_QPATHINFO;
633 size_t nlen = 2*(strlen(name)+1);
634 char *rparam=NULL, *rdata=NULL;
637 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
644 SSVAL(p, 0, SMB_QUERY_POSIX_ACL);
646 p += clistr_push(cli, p, name, nlen, STR_TERMINATE);
647 param_len = PTR_DIFF(p, param);
649 if (!cli_send_trans(cli, SMBtrans2,
651 -1, 0, /* fid, flags */
652 &setup, 1, 0, /* setup, length, max */
653 param, param_len, 2, /* param, length, max */
654 NULL, 0, cli->max_xmit /* data, length, max */
662 if (!cli_receive_trans(cli, SMBtrans2,
664 &rdata, &data_len)) {
676 *prb_size = (size_t)data_len;
681 /****************************************************************************
682 Stat a file (UNIX extensions).
683 ****************************************************************************/
685 bool cli_unix_stat(struct cli_state *cli, const char *name, SMB_STRUCT_STAT *sbuf)
687 unsigned int param_len = 0;
688 unsigned int data_len = 0;
689 uint16_t setup = TRANSACT2_QPATHINFO;
691 size_t nlen = 2*(strlen(name)+1);
692 char *rparam=NULL, *rdata=NULL;
697 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
703 SSVAL(p, 0, SMB_QUERY_FILE_UNIX_BASIC);
705 p += clistr_push(cli, p, name, nlen, STR_TERMINATE);
706 param_len = PTR_DIFF(p, param);
708 if (!cli_send_trans(cli, SMBtrans2,
710 -1, 0, /* fid, flags */
711 &setup, 1, 0, /* setup, length, max */
712 param, param_len, 2, /* param, length, max */
713 NULL, 0, cli->max_xmit /* data, length, max */
721 if (!cli_receive_trans(cli, SMBtrans2,
723 &rdata, &data_len)) {
733 sbuf->st_ex_size = IVAL2_TO_SMB_BIG_UINT(rdata,0); /* total size, in bytes */
734 sbuf->st_ex_blocks = IVAL2_TO_SMB_BIG_UINT(rdata,8); /* number of blocks allocated */
735 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
736 sbuf->st_ex_blocks /= STAT_ST_BLOCKSIZE;
738 /* assume 512 byte blocks */
739 sbuf->st_ex_blocks /= 512;
741 sbuf->st_ex_ctime = interpret_long_date(rdata + 16); /* time of last change */
742 sbuf->st_ex_atime = interpret_long_date(rdata + 24); /* time of last access */
743 sbuf->st_ex_mtime = interpret_long_date(rdata + 32); /* time of last modification */
745 sbuf->st_ex_uid = (uid_t) IVAL(rdata,40); /* user ID of owner */
746 sbuf->st_ex_gid = (gid_t) IVAL(rdata,48); /* group ID of owner */
747 sbuf->st_ex_mode |= unix_filetype_from_wire(IVAL(rdata, 56));
748 #if defined(HAVE_MAKEDEV)
750 uint32_t dev_major = IVAL(rdata,60);
751 uint32_t dev_minor = IVAL(rdata,68);
752 sbuf->st_ex_rdev = makedev(dev_major, dev_minor);
755 sbuf->st_ex_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(rdata,76); /* inode */
756 sbuf->st_ex_mode |= wire_perms_to_unix(IVAL(rdata,84)); /* protection */
757 sbuf->st_ex_nlink = IVAL(rdata,92); /* number of hard links */
764 /****************************************************************************
765 Chmod or chown a file internal (UNIX extensions).
766 ****************************************************************************/
768 static bool cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname, uint32_t mode, uint32_t uid, uint32_t gid)
770 unsigned int data_len = 0;
771 unsigned int param_len = 0;
772 uint16_t setup = TRANSACT2_SETPATHINFO;
773 size_t nlen = 2*(strlen(fname)+1);
776 char *rparam=NULL, *rdata=NULL;
779 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
783 memset(param, '\0', 6);
784 memset(data, 0, sizeof(data));
786 SSVAL(param,0,SMB_SET_FILE_UNIX_BASIC);
789 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
790 param_len = PTR_DIFF(p, param);
792 memset(data, 0xff, 40); /* Set all sizes/times to no change. */
800 if (!cli_send_trans(cli, SMBtrans2,
802 -1, 0, /* fid, flags */
803 &setup, 1, 0, /* setup, length, max */
804 param, param_len, 2, /* param, length, max */
805 (char *)&data, data_len, cli->max_xmit /* data, length, max */
813 if (!cli_receive_trans(cli, SMBtrans2,
815 &rdata, &data_len)) {
825 /****************************************************************************
826 chmod a file (UNIX extensions).
827 ****************************************************************************/
829 bool cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
831 return cli_unix_chmod_chown_internal(cli, fname,
832 unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
835 /****************************************************************************
836 chown a file (UNIX extensions).
837 ****************************************************************************/
839 bool cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
841 return cli_unix_chmod_chown_internal(cli, fname,
842 SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
845 /****************************************************************************
847 ****************************************************************************/
849 static void cli_rename_done(struct tevent_req *subreq);
851 struct cli_rename_state {
855 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
856 struct event_context *ev,
857 struct cli_state *cli,
858 const char *fname_src,
859 const char *fname_dst)
861 struct tevent_req *req = NULL, *subreq = NULL;
862 struct cli_rename_state *state = NULL;
863 uint8_t additional_flags = 0;
864 uint8_t *bytes = NULL;
866 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
871 SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
873 bytes = talloc_array(state, uint8_t, 1);
874 if (tevent_req_nomem(bytes, req)) {
875 return tevent_req_post(req, ev);
878 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
879 strlen(fname_src)+1, NULL);
880 if (tevent_req_nomem(bytes, req)) {
881 return tevent_req_post(req, ev);
884 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
885 talloc_get_size(bytes)+1);
886 if (tevent_req_nomem(bytes, req)) {
887 return tevent_req_post(req, ev);
890 bytes[talloc_get_size(bytes)-1] = 4;
891 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
892 strlen(fname_dst)+1, NULL);
893 if (tevent_req_nomem(bytes, req)) {
894 return tevent_req_post(req, ev);
897 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
898 1, state->vwv, talloc_get_size(bytes), bytes);
899 if (tevent_req_nomem(subreq, req)) {
900 return tevent_req_post(req, ev);
902 tevent_req_set_callback(subreq, cli_rename_done, req);
906 static void cli_rename_done(struct tevent_req *subreq)
908 struct tevent_req *req = tevent_req_callback_data(
909 subreq, struct tevent_req);
912 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
914 if (!NT_STATUS_IS_OK(status)) {
915 tevent_req_nterror(req, status);
918 tevent_req_done(req);
921 NTSTATUS cli_rename_recv(struct tevent_req *req)
923 return tevent_req_simple_recv_ntstatus(req);
926 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
928 TALLOC_CTX *frame = talloc_stackframe();
929 struct event_context *ev;
930 struct tevent_req *req;
931 NTSTATUS status = NT_STATUS_OK;
933 if (cli_has_async_calls(cli)) {
935 * Can't use sync call while an async call is in flight
937 status = NT_STATUS_INVALID_PARAMETER;
941 ev = event_context_init(frame);
943 status = NT_STATUS_NO_MEMORY;
947 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
949 status = NT_STATUS_NO_MEMORY;
953 if (!tevent_req_poll(req, ev)) {
954 status = map_nt_error_from_unix(errno);
958 status = cli_rename_recv(req);
962 if (!NT_STATUS_IS_OK(status)) {
963 cli_set_error(cli, status);
968 /****************************************************************************
970 ****************************************************************************/
972 static void cli_ntrename_internal_done(struct tevent_req *subreq);
974 struct cli_ntrename_internal_state {
978 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
979 struct event_context *ev,
980 struct cli_state *cli,
981 const char *fname_src,
982 const char *fname_dst,
983 uint16_t rename_flag)
985 struct tevent_req *req = NULL, *subreq = NULL;
986 struct cli_ntrename_internal_state *state = NULL;
987 uint8_t additional_flags = 0;
988 uint8_t *bytes = NULL;
990 req = tevent_req_create(mem_ctx, &state,
991 struct cli_ntrename_internal_state);
996 SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
997 SSVAL(state->vwv+1, 0, rename_flag);
999 bytes = talloc_array(state, uint8_t, 1);
1000 if (tevent_req_nomem(bytes, req)) {
1001 return tevent_req_post(req, ev);
1004 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1005 strlen(fname_src)+1, NULL);
1006 if (tevent_req_nomem(bytes, req)) {
1007 return tevent_req_post(req, ev);
1010 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1011 talloc_get_size(bytes)+1);
1012 if (tevent_req_nomem(bytes, req)) {
1013 return tevent_req_post(req, ev);
1016 bytes[talloc_get_size(bytes)-1] = 4;
1017 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1018 strlen(fname_dst)+1, NULL);
1019 if (tevent_req_nomem(bytes, req)) {
1020 return tevent_req_post(req, ev);
1023 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1024 4, state->vwv, talloc_get_size(bytes), bytes);
1025 if (tevent_req_nomem(subreq, req)) {
1026 return tevent_req_post(req, ev);
1028 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1032 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1034 struct tevent_req *req = tevent_req_callback_data(
1035 subreq, struct tevent_req);
1038 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1039 TALLOC_FREE(subreq);
1040 if (!NT_STATUS_IS_OK(status)) {
1041 tevent_req_nterror(req, status);
1044 tevent_req_done(req);
1047 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1049 return tevent_req_simple_recv_ntstatus(req);
1052 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1053 struct event_context *ev,
1054 struct cli_state *cli,
1055 const char *fname_src,
1056 const char *fname_dst)
1058 return cli_ntrename_internal_send(mem_ctx,
1063 RENAME_FLAG_RENAME);
1066 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1068 return cli_ntrename_internal_recv(req);
1071 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1073 TALLOC_CTX *frame = talloc_stackframe();
1074 struct event_context *ev;
1075 struct tevent_req *req;
1076 NTSTATUS status = NT_STATUS_OK;
1078 if (cli_has_async_calls(cli)) {
1080 * Can't use sync call while an async call is in flight
1082 status = NT_STATUS_INVALID_PARAMETER;
1086 ev = event_context_init(frame);
1088 status = NT_STATUS_NO_MEMORY;
1092 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1094 status = NT_STATUS_NO_MEMORY;
1098 if (!tevent_req_poll(req, ev)) {
1099 status = map_nt_error_from_unix(errno);
1103 status = cli_ntrename_recv(req);
1107 if (!NT_STATUS_IS_OK(status)) {
1108 cli_set_error(cli, status);
1113 /****************************************************************************
1115 ****************************************************************************/
1117 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1118 struct event_context *ev,
1119 struct cli_state *cli,
1120 const char *fname_src,
1121 const char *fname_dst)
1123 return cli_ntrename_internal_send(mem_ctx,
1128 RENAME_FLAG_HARD_LINK);
1131 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1133 return cli_ntrename_internal_recv(req);
1136 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1138 TALLOC_CTX *frame = talloc_stackframe();
1139 struct event_context *ev;
1140 struct tevent_req *req;
1141 NTSTATUS status = NT_STATUS_OK;
1143 if (cli_has_async_calls(cli)) {
1145 * Can't use sync call while an async call is in flight
1147 status = NT_STATUS_INVALID_PARAMETER;
1151 ev = event_context_init(frame);
1153 status = NT_STATUS_NO_MEMORY;
1157 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1159 status = NT_STATUS_NO_MEMORY;
1163 if (!tevent_req_poll(req, ev)) {
1164 status = map_nt_error_from_unix(errno);
1168 status = cli_nt_hardlink_recv(req);
1172 if (!NT_STATUS_IS_OK(status)) {
1173 cli_set_error(cli, status);
1178 /****************************************************************************
1180 ****************************************************************************/
1182 static void cli_unlink_done(struct tevent_req *subreq);
1184 struct cli_unlink_state {
1188 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1189 struct event_context *ev,
1190 struct cli_state *cli,
1192 uint16_t mayhave_attrs)
1194 struct tevent_req *req = NULL, *subreq = NULL;
1195 struct cli_unlink_state *state = NULL;
1196 uint8_t additional_flags = 0;
1197 uint8_t *bytes = NULL;
1199 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1204 SSVAL(state->vwv+0, 0, mayhave_attrs);
1206 bytes = talloc_array(state, uint8_t, 1);
1207 if (tevent_req_nomem(bytes, req)) {
1208 return tevent_req_post(req, ev);
1211 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1212 strlen(fname)+1, NULL);
1214 if (tevent_req_nomem(bytes, req)) {
1215 return tevent_req_post(req, ev);
1218 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1219 1, state->vwv, talloc_get_size(bytes), bytes);
1220 if (tevent_req_nomem(subreq, req)) {
1221 return tevent_req_post(req, ev);
1223 tevent_req_set_callback(subreq, cli_unlink_done, req);
1227 static void cli_unlink_done(struct tevent_req *subreq)
1229 struct tevent_req *req = tevent_req_callback_data(
1230 subreq, struct tevent_req);
1233 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1234 TALLOC_FREE(subreq);
1235 if (!NT_STATUS_IS_OK(status)) {
1236 tevent_req_nterror(req, status);
1239 tevent_req_done(req);
1242 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1244 return tevent_req_simple_recv_ntstatus(req);
1247 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1249 TALLOC_CTX *frame = talloc_stackframe();
1250 struct event_context *ev;
1251 struct tevent_req *req;
1252 NTSTATUS status = NT_STATUS_OK;
1254 if (cli_has_async_calls(cli)) {
1256 * Can't use sync call while an async call is in flight
1258 status = NT_STATUS_INVALID_PARAMETER;
1262 ev = event_context_init(frame);
1264 status = NT_STATUS_NO_MEMORY;
1268 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1270 status = NT_STATUS_NO_MEMORY;
1274 if (!tevent_req_poll(req, ev)) {
1275 status = map_nt_error_from_unix(errno);
1279 status = cli_unlink_recv(req);
1283 if (!NT_STATUS_IS_OK(status)) {
1284 cli_set_error(cli, status);
1289 /****************************************************************************
1291 ****************************************************************************/
1293 static void cli_mkdir_done(struct tevent_req *subreq);
1295 struct cli_mkdir_state {
1299 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1300 struct event_context *ev,
1301 struct cli_state *cli,
1304 struct tevent_req *req = NULL, *subreq = NULL;
1305 struct cli_mkdir_state *state = NULL;
1306 uint8_t additional_flags = 0;
1307 uint8_t *bytes = NULL;
1309 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1314 bytes = talloc_array(state, uint8_t, 1);
1315 if (tevent_req_nomem(bytes, req)) {
1316 return tevent_req_post(req, ev);
1319 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1320 strlen(dname)+1, NULL);
1322 if (tevent_req_nomem(bytes, req)) {
1323 return tevent_req_post(req, ev);
1326 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1327 0, NULL, talloc_get_size(bytes), bytes);
1328 if (tevent_req_nomem(subreq, req)) {
1329 return tevent_req_post(req, ev);
1331 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1335 static void cli_mkdir_done(struct tevent_req *subreq)
1337 struct tevent_req *req = tevent_req_callback_data(
1338 subreq, struct tevent_req);
1341 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1342 TALLOC_FREE(subreq);
1343 if (!NT_STATUS_IS_OK(status)) {
1344 tevent_req_nterror(req, status);
1347 tevent_req_done(req);
1350 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1352 return tevent_req_simple_recv_ntstatus(req);
1355 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1357 TALLOC_CTX *frame = talloc_stackframe();
1358 struct event_context *ev;
1359 struct tevent_req *req;
1360 NTSTATUS status = NT_STATUS_OK;
1362 if (cli_has_async_calls(cli)) {
1364 * Can't use sync call while an async call is in flight
1366 status = NT_STATUS_INVALID_PARAMETER;
1370 ev = event_context_init(frame);
1372 status = NT_STATUS_NO_MEMORY;
1376 req = cli_mkdir_send(frame, ev, cli, dname);
1378 status = NT_STATUS_NO_MEMORY;
1382 if (!tevent_req_poll(req, ev)) {
1383 status = map_nt_error_from_unix(errno);
1387 status = cli_mkdir_recv(req);
1391 if (!NT_STATUS_IS_OK(status)) {
1392 cli_set_error(cli, status);
1397 /****************************************************************************
1399 ****************************************************************************/
1401 static void cli_rmdir_done(struct tevent_req *subreq);
1403 struct cli_rmdir_state {
1407 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1408 struct event_context *ev,
1409 struct cli_state *cli,
1412 struct tevent_req *req = NULL, *subreq = NULL;
1413 struct cli_rmdir_state *state = NULL;
1414 uint8_t additional_flags = 0;
1415 uint8_t *bytes = NULL;
1417 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1422 bytes = talloc_array(state, uint8_t, 1);
1423 if (tevent_req_nomem(bytes, req)) {
1424 return tevent_req_post(req, ev);
1427 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1428 strlen(dname)+1, NULL);
1430 if (tevent_req_nomem(bytes, req)) {
1431 return tevent_req_post(req, ev);
1434 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1435 0, NULL, talloc_get_size(bytes), bytes);
1436 if (tevent_req_nomem(subreq, req)) {
1437 return tevent_req_post(req, ev);
1439 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1443 static void cli_rmdir_done(struct tevent_req *subreq)
1445 struct tevent_req *req = tevent_req_callback_data(
1446 subreq, struct tevent_req);
1449 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1450 TALLOC_FREE(subreq);
1451 if (!NT_STATUS_IS_OK(status)) {
1452 tevent_req_nterror(req, status);
1455 tevent_req_done(req);
1458 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1460 return tevent_req_simple_recv_ntstatus(req);
1463 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1465 TALLOC_CTX *frame = talloc_stackframe();
1466 struct event_context *ev;
1467 struct tevent_req *req;
1468 NTSTATUS status = NT_STATUS_OK;
1470 if (cli_has_async_calls(cli)) {
1472 * Can't use sync call while an async call is in flight
1474 status = NT_STATUS_INVALID_PARAMETER;
1478 ev = event_context_init(frame);
1480 status = NT_STATUS_NO_MEMORY;
1484 req = cli_rmdir_send(frame, ev, cli, dname);
1486 status = NT_STATUS_NO_MEMORY;
1490 if (!tevent_req_poll(req, ev)) {
1491 status = map_nt_error_from_unix(errno);
1495 status = cli_rmdir_recv(req);
1499 if (!NT_STATUS_IS_OK(status)) {
1500 cli_set_error(cli, status);
1505 /****************************************************************************
1506 Set or clear the delete on close flag.
1507 ****************************************************************************/
1509 int cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1511 unsigned int data_len = 1;
1512 unsigned int param_len = 6;
1513 uint16_t setup = TRANSACT2_SETFILEINFO;
1516 char *rparam=NULL, *rdata=NULL;
1518 memset(param, 0, param_len);
1519 SSVAL(param,0,fnum);
1520 SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
1522 data = flag ? 1 : 0;
1524 if (!cli_send_trans(cli, SMBtrans2,
1526 -1, 0, /* fid, flags */
1527 &setup, 1, 0, /* setup, length, max */
1528 param, param_len, 2, /* param, length, max */
1529 (char *)&data, data_len, cli->max_xmit /* data, length, max */
1534 if (!cli_receive_trans(cli, SMBtrans2,
1535 &rparam, ¶m_len,
1536 &rdata, &data_len)) {
1546 struct cli_ntcreate_state {
1551 static void cli_ntcreate_done(struct tevent_req *subreq);
1553 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1554 struct event_context *ev,
1555 struct cli_state *cli,
1557 uint32_t CreatFlags,
1558 uint32_t DesiredAccess,
1559 uint32_t FileAttributes,
1560 uint32_t ShareAccess,
1561 uint32_t CreateDisposition,
1562 uint32_t CreateOptions,
1563 uint8_t SecurityFlags)
1565 struct tevent_req *req, *subreq;
1566 struct cli_ntcreate_state *state;
1569 size_t converted_len;
1571 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1577 SCVAL(vwv+0, 0, 0xFF);
1582 if (cli->use_oplocks) {
1583 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1585 SIVAL(vwv+3, 1, CreatFlags);
1586 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1587 SIVAL(vwv+7, 1, DesiredAccess);
1588 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1589 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1590 SIVAL(vwv+13, 1, FileAttributes);
1591 SIVAL(vwv+15, 1, ShareAccess);
1592 SIVAL(vwv+17, 1, CreateDisposition);
1593 SIVAL(vwv+19, 1, CreateOptions);
1594 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1595 SCVAL(vwv+23, 1, SecurityFlags);
1597 bytes = talloc_array(state, uint8_t, 0);
1598 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1599 fname, strlen(fname)+1,
1602 /* sigh. this copes with broken netapp filer behaviour */
1603 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1605 if (tevent_req_nomem(bytes, req)) {
1606 return tevent_req_post(req, ev);
1609 SIVAL(vwv+2, 1, converted_len);
1611 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1612 talloc_get_size(bytes), bytes);
1613 if (tevent_req_nomem(subreq, req)) {
1614 return tevent_req_post(req, ev);
1616 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1620 static void cli_ntcreate_done(struct tevent_req *subreq)
1622 struct tevent_req *req = tevent_req_callback_data(
1623 subreq, struct tevent_req);
1624 struct cli_ntcreate_state *state = tevent_req_data(
1625 req, struct cli_ntcreate_state);
1632 status = cli_smb_recv(subreq, 3, &wct, &vwv, &num_bytes, &bytes);
1633 if (!NT_STATUS_IS_OK(status)) {
1634 TALLOC_FREE(subreq);
1635 tevent_req_nterror(req, status);
1638 state->fnum = SVAL(vwv+2, 1);
1639 tevent_req_done(req);
1642 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1644 struct cli_ntcreate_state *state = tevent_req_data(
1645 req, struct cli_ntcreate_state);
1648 if (tevent_req_is_nterror(req, &status)) {
1651 *pfnum = state->fnum;
1652 return NT_STATUS_OK;
1655 NTSTATUS cli_ntcreate(struct cli_state *cli,
1657 uint32_t CreatFlags,
1658 uint32_t DesiredAccess,
1659 uint32_t FileAttributes,
1660 uint32_t ShareAccess,
1661 uint32_t CreateDisposition,
1662 uint32_t CreateOptions,
1663 uint8_t SecurityFlags,
1666 TALLOC_CTX *frame = talloc_stackframe();
1667 struct event_context *ev;
1668 struct tevent_req *req;
1669 NTSTATUS status = NT_STATUS_OK;
1671 if (cli_has_async_calls(cli)) {
1673 * Can't use sync call while an async call is in flight
1675 status = NT_STATUS_INVALID_PARAMETER;
1679 ev = event_context_init(frame);
1681 status = NT_STATUS_NO_MEMORY;
1685 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1686 DesiredAccess, FileAttributes, ShareAccess,
1687 CreateDisposition, CreateOptions,
1690 status = NT_STATUS_NO_MEMORY;
1694 if (!tevent_req_poll(req, ev)) {
1695 status = map_nt_error_from_unix(errno);
1699 status = cli_ntcreate_recv(req, pfid);
1702 if (!NT_STATUS_IS_OK(status)) {
1703 cli_set_error(cli, status);
1708 /****************************************************************************
1710 WARNING: if you open with O_WRONLY then getattrE won't work!
1711 ****************************************************************************/
1713 struct cli_open_state {
1719 static void cli_open_done(struct tevent_req *subreq);
1721 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
1722 struct event_context *ev,
1723 struct cli_state *cli, const char *fname,
1724 int flags, int share_mode,
1725 struct tevent_req **psmbreq)
1727 struct tevent_req *req, *subreq;
1728 struct cli_open_state *state;
1730 unsigned accessmode;
1731 uint8_t additional_flags;
1734 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
1740 if (flags & O_CREAT) {
1743 if (!(flags & O_EXCL)) {
1744 if (flags & O_TRUNC)
1750 accessmode = (share_mode<<4);
1752 if ((flags & O_ACCMODE) == O_RDWR) {
1754 } else if ((flags & O_ACCMODE) == O_WRONLY) {
1759 if ((flags & O_SYNC) == O_SYNC) {
1760 accessmode |= (1<<14);
1764 if (share_mode == DENY_FCB) {
1768 SCVAL(state->vwv + 0, 0, 0xFF);
1769 SCVAL(state->vwv + 0, 1, 0);
1770 SSVAL(state->vwv + 1, 0, 0);
1771 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
1772 SSVAL(state->vwv + 3, 0, accessmode);
1773 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
1774 SSVAL(state->vwv + 5, 0, 0);
1775 SIVAL(state->vwv + 6, 0, 0);
1776 SSVAL(state->vwv + 8, 0, openfn);
1777 SIVAL(state->vwv + 9, 0, 0);
1778 SIVAL(state->vwv + 11, 0, 0);
1779 SIVAL(state->vwv + 13, 0, 0);
1781 additional_flags = 0;
1783 if (cli->use_oplocks) {
1784 /* if using oplocks then ask for a batch oplock via
1785 core and extended methods */
1787 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
1788 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
1791 bytes = talloc_array(state, uint8_t, 0);
1792 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1793 strlen(fname)+1, NULL);
1795 if (tevent_req_nomem(bytes, req)) {
1796 return tevent_req_post(req, ev);
1799 state->bytes.iov_base = (void *)bytes;
1800 state->bytes.iov_len = talloc_get_size(bytes);
1802 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
1803 15, state->vwv, 1, &state->bytes);
1804 if (subreq == NULL) {
1808 tevent_req_set_callback(subreq, cli_open_done, req);
1813 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1814 struct cli_state *cli, const char *fname,
1815 int flags, int share_mode)
1817 struct tevent_req *req, *subreq;
1820 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
1826 status = cli_smb_req_send(subreq);
1827 if (!NT_STATUS_IS_OK(status)) {
1828 tevent_req_nterror(req, status);
1829 return tevent_req_post(req, ev);
1834 static void cli_open_done(struct tevent_req *subreq)
1836 struct tevent_req *req = tevent_req_callback_data(
1837 subreq, struct tevent_req);
1838 struct cli_open_state *state = tevent_req_data(
1839 req, struct cli_open_state);
1844 status = cli_smb_recv(subreq, 3, &wct, &vwv, NULL, NULL);
1845 if (!NT_STATUS_IS_OK(status)) {
1846 TALLOC_FREE(subreq);
1847 tevent_req_nterror(req, status);
1850 state->fnum = SVAL(vwv+2, 0);
1851 tevent_req_done(req);
1854 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
1856 struct cli_open_state *state = tevent_req_data(
1857 req, struct cli_open_state);
1860 if (tevent_req_is_nterror(req, &status)) {
1863 *pfnum = state->fnum;
1864 return NT_STATUS_OK;
1867 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
1868 int share_mode, uint16_t *pfnum)
1870 TALLOC_CTX *frame = talloc_stackframe();
1871 struct event_context *ev;
1872 struct tevent_req *req;
1873 NTSTATUS status = NT_STATUS_OK;
1875 if (cli_has_async_calls(cli)) {
1877 * Can't use sync call while an async call is in flight
1879 status = NT_STATUS_INVALID_PARAMETER;
1883 ev = event_context_init(frame);
1885 status = NT_STATUS_NO_MEMORY;
1889 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
1891 status = NT_STATUS_NO_MEMORY;
1895 if (!tevent_req_poll(req, ev)) {
1896 status = map_nt_error_from_unix(errno);
1900 status = cli_open_recv(req, pfnum);
1903 if (!NT_STATUS_IS_OK(status)) {
1904 cli_set_error(cli, status);
1909 /****************************************************************************
1911 ****************************************************************************/
1913 struct cli_close_state {
1917 static void cli_close_done(struct tevent_req *subreq);
1919 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
1920 struct event_context *ev,
1921 struct cli_state *cli,
1923 struct tevent_req **psubreq)
1925 struct tevent_req *req, *subreq;
1926 struct cli_close_state *state;
1928 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
1932 SSVAL(state->vwv+0, 0, fnum);
1933 SIVALS(state->vwv+1, 0, -1);
1935 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
1937 if (subreq == NULL) {
1941 tevent_req_set_callback(subreq, cli_close_done, req);
1946 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
1947 struct event_context *ev,
1948 struct cli_state *cli,
1951 struct tevent_req *req, *subreq;
1954 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
1959 status = cli_smb_req_send(subreq);
1960 if (!NT_STATUS_IS_OK(status)) {
1961 tevent_req_nterror(req, status);
1962 return tevent_req_post(req, ev);
1967 static void cli_close_done(struct tevent_req *subreq)
1969 struct tevent_req *req = tevent_req_callback_data(
1970 subreq, struct tevent_req);
1973 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1974 TALLOC_FREE(subreq);
1975 if (!NT_STATUS_IS_OK(status)) {
1976 tevent_req_nterror(req, status);
1979 tevent_req_done(req);
1982 NTSTATUS cli_close_recv(struct tevent_req *req)
1984 return tevent_req_simple_recv_ntstatus(req);
1987 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
1989 TALLOC_CTX *frame = talloc_stackframe();
1990 struct event_context *ev;
1991 struct tevent_req *req;
1992 NTSTATUS status = NT_STATUS_OK;
1994 if (cli_has_async_calls(cli)) {
1996 * Can't use sync call while an async call is in flight
1998 status = NT_STATUS_INVALID_PARAMETER;
2002 ev = event_context_init(frame);
2004 status = NT_STATUS_NO_MEMORY;
2008 req = cli_close_send(frame, ev, cli, fnum);
2010 status = NT_STATUS_NO_MEMORY;
2014 if (!tevent_req_poll(req, ev)) {
2015 status = map_nt_error_from_unix(errno);
2019 status = cli_close_recv(req);
2022 if (!NT_STATUS_IS_OK(status)) {
2023 cli_set_error(cli, status);
2028 /****************************************************************************
2029 Truncate a file to a specified size
2030 ****************************************************************************/
2032 bool cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2034 unsigned int param_len = 6;
2035 unsigned int data_len = 8;
2036 uint16_t setup = TRANSACT2_SETFILEINFO;
2038 unsigned char data[8];
2039 char *rparam=NULL, *rdata=NULL;
2040 int saved_timeout = cli->timeout;
2042 SSVAL(param,0,fnum);
2043 SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2046 SBVAL(data, 0, size);
2048 if (!cli_send_trans(cli, SMBtrans2,
2050 -1, 0, /* fid, flags */
2051 &setup, 1, 0, /* setup, length, max */
2052 param, param_len, 2, /* param, length, max */
2053 (char *)&data, data_len,/* data, length, ... */
2054 cli->max_xmit)) { /* ... max */
2055 cli->timeout = saved_timeout;
2059 if (!cli_receive_trans(cli, SMBtrans2,
2060 &rparam, ¶m_len,
2061 &rdata, &data_len)) {
2062 cli->timeout = saved_timeout;
2068 cli->timeout = saved_timeout;
2077 /****************************************************************************
2078 send a lock with a specified locktype
2079 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2080 ****************************************************************************/
2082 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2083 uint32_t offset, uint32_t len,
2084 int timeout, unsigned char locktype)
2087 int saved_timeout = cli->timeout;
2089 memset(cli->outbuf,'\0',smb_size);
2090 memset(cli->inbuf,'\0', smb_size);
2092 cli_set_message(cli->outbuf,8,0,True);
2094 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2095 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2096 cli_setup_packet(cli);
2098 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2099 SSVAL(cli->outbuf,smb_vwv2,fnum);
2100 SCVAL(cli->outbuf,smb_vwv3,locktype);
2101 SIVALS(cli->outbuf, smb_vwv4, timeout);
2102 SSVAL(cli->outbuf,smb_vwv6,0);
2103 SSVAL(cli->outbuf,smb_vwv7,1);
2105 p = smb_buf(cli->outbuf);
2106 SSVAL(p, 0, cli->pid);
2107 SIVAL(p, 2, offset);
2112 cli_setup_bcc(cli, p);
2117 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
2120 if (!cli_receive_smb(cli)) {
2121 cli->timeout = saved_timeout;
2122 return NT_STATUS_UNSUCCESSFUL;
2125 cli->timeout = saved_timeout;
2127 return cli_nt_error(cli);
2130 /****************************************************************************
2132 note that timeout is in units of 2 milliseconds
2133 ****************************************************************************/
2135 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2136 uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
2139 int saved_timeout = cli->timeout;
2141 memset(cli->outbuf,'\0',smb_size);
2142 memset(cli->inbuf,'\0', smb_size);
2144 cli_set_message(cli->outbuf,8,0,True);
2146 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2147 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2148 cli_setup_packet(cli);
2150 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2151 SSVAL(cli->outbuf,smb_vwv2,fnum);
2152 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
2153 SIVALS(cli->outbuf, smb_vwv4, timeout);
2154 SSVAL(cli->outbuf,smb_vwv6,0);
2155 SSVAL(cli->outbuf,smb_vwv7,1);
2157 p = smb_buf(cli->outbuf);
2158 SSVAL(p, 0, cli->pid);
2159 SIVAL(p, 2, offset);
2164 cli_setup_bcc(cli, p);
2169 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
2172 if (!cli_receive_smb(cli)) {
2173 cli->timeout = saved_timeout;
2177 cli->timeout = saved_timeout;
2179 if (cli_is_error(cli)) {
2186 /****************************************************************************
2188 ****************************************************************************/
2190 bool cli_unlock(struct cli_state *cli, uint16_t fnum, uint32_t offset, uint32_t len)
2194 memset(cli->outbuf,'\0',smb_size);
2195 memset(cli->inbuf,'\0',smb_size);
2197 cli_set_message(cli->outbuf,8,0,True);
2199 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2200 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2201 cli_setup_packet(cli);
2203 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2204 SSVAL(cli->outbuf,smb_vwv2,fnum);
2205 SCVAL(cli->outbuf,smb_vwv3,0);
2206 SIVALS(cli->outbuf, smb_vwv4, 0);
2207 SSVAL(cli->outbuf,smb_vwv6,1);
2208 SSVAL(cli->outbuf,smb_vwv7,0);
2210 p = smb_buf(cli->outbuf);
2211 SSVAL(p, 0, cli->pid);
2212 SIVAL(p, 2, offset);
2215 cli_setup_bcc(cli, p);
2217 if (!cli_receive_smb(cli)) {
2221 if (cli_is_error(cli)) {
2228 /****************************************************************************
2229 Lock a file with 64 bit offsets.
2230 ****************************************************************************/
2232 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2233 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
2236 int saved_timeout = cli->timeout;
2239 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2240 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2243 ltype = (lock_type == READ_LOCK? 1 : 0);
2244 ltype |= LOCKING_ANDX_LARGE_FILES;
2246 memset(cli->outbuf,'\0',smb_size);
2247 memset(cli->inbuf,'\0', smb_size);
2249 cli_set_message(cli->outbuf,8,0,True);
2251 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2252 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2253 cli_setup_packet(cli);
2255 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2256 SSVAL(cli->outbuf,smb_vwv2,fnum);
2257 SCVAL(cli->outbuf,smb_vwv3,ltype);
2258 SIVALS(cli->outbuf, smb_vwv4, timeout);
2259 SSVAL(cli->outbuf,smb_vwv6,0);
2260 SSVAL(cli->outbuf,smb_vwv7,1);
2262 p = smb_buf(cli->outbuf);
2263 SIVAL(p, 0, cli->pid);
2264 SOFF_T_R(p, 4, offset);
2265 SOFF_T_R(p, 12, len);
2268 cli_setup_bcc(cli, p);
2272 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
2275 if (!cli_receive_smb(cli)) {
2276 cli->timeout = saved_timeout;
2280 cli->timeout = saved_timeout;
2282 if (cli_is_error(cli)) {
2289 /****************************************************************************
2290 Unlock a file with 64 bit offsets.
2291 ****************************************************************************/
2293 bool cli_unlock64(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
2297 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2298 return cli_unlock(cli, fnum, offset, len);
2301 memset(cli->outbuf,'\0',smb_size);
2302 memset(cli->inbuf,'\0',smb_size);
2304 cli_set_message(cli->outbuf,8,0,True);
2306 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2307 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2308 cli_setup_packet(cli);
2310 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2311 SSVAL(cli->outbuf,smb_vwv2,fnum);
2312 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
2313 SIVALS(cli->outbuf, smb_vwv4, 0);
2314 SSVAL(cli->outbuf,smb_vwv6,1);
2315 SSVAL(cli->outbuf,smb_vwv7,0);
2317 p = smb_buf(cli->outbuf);
2318 SIVAL(p, 0, cli->pid);
2319 SOFF_T_R(p, 4, offset);
2320 SOFF_T_R(p, 12, len);
2322 cli_setup_bcc(cli, p);
2324 if (!cli_receive_smb(cli)) {
2328 if (cli_is_error(cli)) {
2335 /****************************************************************************
2336 Get/unlock a POSIX lock on a file - internal function.
2337 ****************************************************************************/
2339 static bool cli_posix_lock_internal(struct cli_state *cli, uint16_t fnum,
2340 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
2342 unsigned int param_len = 4;
2343 unsigned int data_len = POSIX_LOCK_DATA_SIZE;
2344 uint16_t setup = TRANSACT2_SETFILEINFO;
2346 unsigned char data[POSIX_LOCK_DATA_SIZE];
2347 char *rparam=NULL, *rdata=NULL;
2348 int saved_timeout = cli->timeout;
2350 SSVAL(param,0,fnum);
2351 SSVAL(param,2,SMB_SET_POSIX_LOCK);
2353 switch (lock_type) {
2355 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
2358 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
2361 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
2368 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
2369 cli->timeout = 0x7FFFFFFF;
2371 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
2374 SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
2375 SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
2376 SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
2378 if (!cli_send_trans(cli, SMBtrans2,
2380 -1, 0, /* fid, flags */
2381 &setup, 1, 0, /* setup, length, max */
2382 param, param_len, 2, /* param, length, max */
2383 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2385 cli->timeout = saved_timeout;
2389 if (!cli_receive_trans(cli, SMBtrans2,
2390 &rparam, ¶m_len,
2391 &rdata, &data_len)) {
2392 cli->timeout = saved_timeout;
2398 cli->timeout = saved_timeout;
2406 /****************************************************************************
2408 ****************************************************************************/
2410 bool cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2411 uint64_t offset, uint64_t len,
2412 bool wait_lock, enum brl_type lock_type)
2414 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2417 return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
2420 /****************************************************************************
2421 POSIX Unlock a file.
2422 ****************************************************************************/
2424 bool cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
2426 return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
2429 /****************************************************************************
2430 POSIX Get any lock covering a file.
2431 ****************************************************************************/
2433 bool cli_posix_getlock(struct cli_state *cli, uint16_t fnum, uint64_t *poffset, uint64_t *plen)
2438 /****************************************************************************
2439 Do a SMBgetattrE call.
2440 ****************************************************************************/
2442 static void cli_getattrE_done(struct tevent_req *subreq);
2444 struct cli_getattrE_state {
2454 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
2455 struct event_context *ev,
2456 struct cli_state *cli,
2459 struct tevent_req *req = NULL, *subreq = NULL;
2460 struct cli_getattrE_state *state = NULL;
2461 uint8_t additional_flags = 0;
2463 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
2468 state->zone_offset = cli->serverzone;
2469 SSVAL(state->vwv+0,0,fnum);
2471 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
2472 1, state->vwv, 0, NULL);
2473 if (tevent_req_nomem(subreq, req)) {
2474 return tevent_req_post(req, ev);
2476 tevent_req_set_callback(subreq, cli_getattrE_done, req);
2480 static void cli_getattrE_done(struct tevent_req *subreq)
2482 struct tevent_req *req = tevent_req_callback_data(
2483 subreq, struct tevent_req);
2484 struct cli_getattrE_state *state = tevent_req_data(
2485 req, struct cli_getattrE_state);
2487 uint16_t *vwv = NULL;
2490 status = cli_smb_recv(subreq, 11, &wct, &vwv, NULL, NULL);
2491 if (!NT_STATUS_IS_OK(status)) {
2492 tevent_req_nterror(req, status);
2496 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
2497 state->attr = SVAL(vwv+10,0);
2498 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
2499 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
2500 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
2502 TALLOC_FREE(subreq);
2503 tevent_req_done(req);
2506 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
2509 time_t *change_time,
2510 time_t *access_time,
2513 struct cli_getattrE_state *state = tevent_req_data(
2514 req, struct cli_getattrE_state);
2517 if (tevent_req_is_nterror(req, &status)) {
2521 *attr = state->attr;
2524 *size = state->size;
2527 *change_time = state->change_time;
2530 *access_time = state->access_time;
2533 *write_time = state->write_time;
2535 return NT_STATUS_OK;
2538 NTSTATUS cli_getattrE(struct cli_state *cli,
2542 time_t *change_time,
2543 time_t *access_time,
2546 TALLOC_CTX *frame = talloc_stackframe();
2547 struct event_context *ev = NULL;
2548 struct tevent_req *req = NULL;
2549 NTSTATUS status = NT_STATUS_OK;
2551 if (cli_has_async_calls(cli)) {
2553 * Can't use sync call while an async call is in flight
2555 status = NT_STATUS_INVALID_PARAMETER;
2559 ev = event_context_init(frame);
2561 status = NT_STATUS_NO_MEMORY;
2565 req = cli_getattrE_send(frame, ev, cli, fnum);
2567 status = NT_STATUS_NO_MEMORY;
2571 if (!tevent_req_poll(req, ev)) {
2572 status = map_nt_error_from_unix(errno);
2576 status = cli_getattrE_recv(req,
2585 if (!NT_STATUS_IS_OK(status)) {
2586 cli_set_error(cli, status);
2591 /****************************************************************************
2593 ****************************************************************************/
2595 static void cli_getatr_done(struct tevent_req *subreq);
2597 struct cli_getatr_state {
2604 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
2605 struct event_context *ev,
2606 struct cli_state *cli,
2609 struct tevent_req *req = NULL, *subreq = NULL;
2610 struct cli_getatr_state *state = NULL;
2611 uint8_t additional_flags = 0;
2612 uint8_t *bytes = NULL;
2614 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
2619 state->zone_offset = cli->serverzone;
2621 bytes = talloc_array(state, uint8_t, 1);
2622 if (tevent_req_nomem(bytes, req)) {
2623 return tevent_req_post(req, ev);
2626 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2627 strlen(fname)+1, NULL);
2629 if (tevent_req_nomem(bytes, req)) {
2630 return tevent_req_post(req, ev);
2633 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
2634 0, NULL, talloc_get_size(bytes), bytes);
2635 if (tevent_req_nomem(subreq, req)) {
2636 return tevent_req_post(req, ev);
2638 tevent_req_set_callback(subreq, cli_getatr_done, req);
2642 static void cli_getatr_done(struct tevent_req *subreq)
2644 struct tevent_req *req = tevent_req_callback_data(
2645 subreq, struct tevent_req);
2646 struct cli_getatr_state *state = tevent_req_data(
2647 req, struct cli_getatr_state);
2649 uint16_t *vwv = NULL;
2652 status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
2653 if (!NT_STATUS_IS_OK(status)) {
2654 tevent_req_nterror(req, status);
2658 state->attr = SVAL(vwv+0,0);
2659 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
2660 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
2662 TALLOC_FREE(subreq);
2663 tevent_req_done(req);
2666 NTSTATUS cli_getatr_recv(struct tevent_req *req,
2671 struct cli_getatr_state *state = tevent_req_data(
2672 req, struct cli_getatr_state);
2675 if (tevent_req_is_nterror(req, &status)) {
2679 *attr = state->attr;
2682 *size = state->size;
2685 *write_time = state->write_time;
2687 return NT_STATUS_OK;
2690 NTSTATUS cli_getatr(struct cli_state *cli,
2696 TALLOC_CTX *frame = talloc_stackframe();
2697 struct event_context *ev = NULL;
2698 struct tevent_req *req = NULL;
2699 NTSTATUS status = NT_STATUS_OK;
2701 if (cli_has_async_calls(cli)) {
2703 * Can't use sync call while an async call is in flight
2705 status = NT_STATUS_INVALID_PARAMETER;
2709 ev = event_context_init(frame);
2711 status = NT_STATUS_NO_MEMORY;
2715 req = cli_getatr_send(frame, ev, cli, fname);
2717 status = NT_STATUS_NO_MEMORY;
2721 if (!tevent_req_poll(req, ev)) {
2722 status = map_nt_error_from_unix(errno);
2726 status = cli_getatr_recv(req,
2733 if (!NT_STATUS_IS_OK(status)) {
2734 cli_set_error(cli, status);
2739 /****************************************************************************
2740 Do a SMBsetattrE call.
2741 ****************************************************************************/
2743 static void cli_setattrE_done(struct tevent_req *subreq);
2745 struct cli_setattrE_state {
2749 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
2750 struct event_context *ev,
2751 struct cli_state *cli,
2757 struct tevent_req *req = NULL, *subreq = NULL;
2758 struct cli_setattrE_state *state = NULL;
2759 uint8_t additional_flags = 0;
2762 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
2767 memset(vwv, '\0', sizeof(vwv));
2768 SSVAL(vwv+0, 0, fnum);
2769 cli_put_dos_date2(cli, (char *)&vwv[1], 0, change_time);
2770 cli_put_dos_date2(cli, (char *)&vwv[3], 0, access_time);
2771 cli_put_dos_date2(cli, (char *)&vwv[5], 0, write_time);
2773 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
2775 if (tevent_req_nomem(subreq, req)) {
2776 return tevent_req_post(req, ev);
2778 tevent_req_set_callback(subreq, cli_setattrE_done, req);
2782 static void cli_setattrE_done(struct tevent_req *subreq)
2784 struct tevent_req *req = tevent_req_callback_data(
2785 subreq, struct tevent_req);
2788 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2789 TALLOC_FREE(subreq);
2790 if (!NT_STATUS_IS_OK(status)) {
2791 tevent_req_nterror(req, status);
2794 tevent_req_done(req);
2797 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
2799 return tevent_req_simple_recv_ntstatus(req);
2802 NTSTATUS cli_setattrE(struct cli_state *cli,
2808 TALLOC_CTX *frame = talloc_stackframe();
2809 struct event_context *ev = NULL;
2810 struct tevent_req *req = NULL;
2811 NTSTATUS status = NT_STATUS_OK;
2813 if (cli_has_async_calls(cli)) {
2815 * Can't use sync call while an async call is in flight
2817 status = NT_STATUS_INVALID_PARAMETER;
2821 ev = event_context_init(frame);
2823 status = NT_STATUS_NO_MEMORY;
2827 req = cli_setattrE_send(frame, ev,
2835 status = NT_STATUS_NO_MEMORY;
2839 if (!tevent_req_poll(req, ev)) {
2840 status = map_nt_error_from_unix(errno);
2844 status = cli_setattrE_recv(req);
2848 if (!NT_STATUS_IS_OK(status)) {
2849 cli_set_error(cli, status);
2854 /****************************************************************************
2855 Do a SMBsetatr call.
2856 ****************************************************************************/
2858 static void cli_setatr_done(struct tevent_req *subreq);
2860 struct cli_setatr_state {
2864 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
2865 struct event_context *ev,
2866 struct cli_state *cli,
2871 struct tevent_req *req = NULL, *subreq = NULL;
2872 struct cli_setatr_state *state = NULL;
2873 uint8_t additional_flags = 0;
2874 uint8_t *bytes = NULL;
2876 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
2881 memset(state->vwv, '\0', sizeof(state->vwv));
2882 SSVAL(state->vwv+0, 0, attr);
2883 cli_put_dos_date3(cli, (char *)&state->vwv[1], 0, mtime);
2885 bytes = talloc_array(state, uint8_t, 1);
2886 if (tevent_req_nomem(bytes, req)) {
2887 return tevent_req_post(req, ev);
2890 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2891 strlen(fname)+1, NULL);
2892 if (tevent_req_nomem(bytes, req)) {
2893 return tevent_req_post(req, ev);
2895 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
2896 talloc_get_size(bytes)+1);
2897 if (tevent_req_nomem(bytes, req)) {
2898 return tevent_req_post(req, ev);
2901 bytes[talloc_get_size(bytes)-1] = 4;
2902 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
2904 if (tevent_req_nomem(bytes, req)) {
2905 return tevent_req_post(req, ev);
2908 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
2909 8, state->vwv, talloc_get_size(bytes), bytes);
2910 if (tevent_req_nomem(subreq, req)) {
2911 return tevent_req_post(req, ev);
2913 tevent_req_set_callback(subreq, cli_setatr_done, req);
2917 static void cli_setatr_done(struct tevent_req *subreq)
2919 struct tevent_req *req = tevent_req_callback_data(
2920 subreq, struct tevent_req);
2923 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2924 TALLOC_FREE(subreq);
2925 if (!NT_STATUS_IS_OK(status)) {
2926 tevent_req_nterror(req, status);
2929 tevent_req_done(req);
2932 NTSTATUS cli_setatr_recv(struct tevent_req *req)
2934 return tevent_req_simple_recv_ntstatus(req);
2937 NTSTATUS cli_setatr(struct cli_state *cli,
2942 TALLOC_CTX *frame = talloc_stackframe();
2943 struct event_context *ev = NULL;
2944 struct tevent_req *req = NULL;
2945 NTSTATUS status = NT_STATUS_OK;
2947 if (cli_has_async_calls(cli)) {
2949 * Can't use sync call while an async call is in flight
2951 status = NT_STATUS_INVALID_PARAMETER;
2955 ev = event_context_init(frame);
2957 status = NT_STATUS_NO_MEMORY;
2961 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
2963 status = NT_STATUS_NO_MEMORY;
2967 if (!tevent_req_poll(req, ev)) {
2968 status = map_nt_error_from_unix(errno);
2972 status = cli_setatr_recv(req);
2976 if (!NT_STATUS_IS_OK(status)) {
2977 cli_set_error(cli, status);
2982 /****************************************************************************
2983 Check for existance of a dir.
2984 ****************************************************************************/
2986 static void cli_chkpath_done(struct tevent_req *subreq);
2988 struct cli_chkpath_state {
2992 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
2993 struct event_context *ev,
2994 struct cli_state *cli,
2997 struct tevent_req *req = NULL, *subreq = NULL;
2998 struct cli_chkpath_state *state = NULL;
2999 uint8_t additional_flags = 0;
3000 uint8_t *bytes = NULL;
3002 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3007 bytes = talloc_array(state, uint8_t, 1);
3008 if (tevent_req_nomem(bytes, req)) {
3009 return tevent_req_post(req, ev);
3012 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3013 strlen(fname)+1, NULL);
3015 if (tevent_req_nomem(bytes, req)) {
3016 return tevent_req_post(req, ev);
3019 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3020 0, NULL, talloc_get_size(bytes), bytes);
3021 if (tevent_req_nomem(subreq, req)) {
3022 return tevent_req_post(req, ev);
3024 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3028 static void cli_chkpath_done(struct tevent_req *subreq)
3030 struct tevent_req *req = tevent_req_callback_data(
3031 subreq, struct tevent_req);
3034 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
3035 TALLOC_FREE(subreq);
3036 if (!NT_STATUS_IS_OK(status)) {
3037 tevent_req_nterror(req, status);
3040 tevent_req_done(req);
3043 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3045 return tevent_req_simple_recv_ntstatus(req);
3048 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3050 TALLOC_CTX *frame = talloc_stackframe();
3051 struct event_context *ev = NULL;
3052 struct tevent_req *req = NULL;
3054 NTSTATUS status = NT_STATUS_OK;
3056 if (cli_has_async_calls(cli)) {
3058 * Can't use sync call while an async call is in flight
3060 status = NT_STATUS_INVALID_PARAMETER;
3064 path2 = talloc_strdup(frame, path);
3066 status = NT_STATUS_NO_MEMORY;
3069 trim_char(path2,'\0','\\');
3071 path2 = talloc_strdup(frame, "\\");
3073 status = NT_STATUS_NO_MEMORY;
3078 ev = event_context_init(frame);
3080 status = NT_STATUS_NO_MEMORY;
3084 req = cli_chkpath_send(frame, ev, cli, path2);
3086 status = NT_STATUS_NO_MEMORY;
3090 if (!tevent_req_poll(req, ev)) {
3091 status = map_nt_error_from_unix(errno);
3095 status = cli_chkpath_recv(req);
3099 if (!NT_STATUS_IS_OK(status)) {
3100 cli_set_error(cli, status);
3105 /****************************************************************************
3107 ****************************************************************************/
3109 static void cli_dskattr_done(struct tevent_req *subreq);
3111 struct cli_dskattr_state {
3117 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3118 struct event_context *ev,
3119 struct cli_state *cli)
3121 struct tevent_req *req = NULL, *subreq = NULL;
3122 struct cli_dskattr_state *state = NULL;
3123 uint8_t additional_flags = 0;
3125 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3130 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3132 if (tevent_req_nomem(subreq, req)) {
3133 return tevent_req_post(req, ev);
3135 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3139 static void cli_dskattr_done(struct tevent_req *subreq)
3141 struct tevent_req *req = tevent_req_callback_data(
3142 subreq, struct tevent_req);
3143 struct cli_dskattr_state *state = tevent_req_data(
3144 req, struct cli_dskattr_state);
3146 uint16_t *vwv = NULL;
3149 status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
3150 if (!NT_STATUS_IS_OK(status)) {
3151 tevent_req_nterror(req, status);
3154 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3155 state->total = SVAL(vwv+0, 0);
3156 state->avail = SVAL(vwv+3, 0);
3157 TALLOC_FREE(subreq);
3158 tevent_req_done(req);
3161 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3163 struct cli_dskattr_state *state = tevent_req_data(
3164 req, struct cli_dskattr_state);
3167 if (tevent_req_is_nterror(req, &status)) {
3170 *bsize = state->bsize;
3171 *total = state->total;
3172 *avail = state->avail;
3173 return NT_STATUS_OK;
3176 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3178 TALLOC_CTX *frame = talloc_stackframe();
3179 struct event_context *ev = NULL;
3180 struct tevent_req *req = NULL;
3181 NTSTATUS status = NT_STATUS_OK;
3183 if (cli_has_async_calls(cli)) {
3185 * Can't use sync call while an async call is in flight
3187 status = NT_STATUS_INVALID_PARAMETER;
3191 ev = event_context_init(frame);
3193 status = NT_STATUS_NO_MEMORY;
3197 req = cli_dskattr_send(frame, ev, cli);
3199 status = NT_STATUS_NO_MEMORY;
3203 if (!tevent_req_poll(req, ev)) {
3204 status = map_nt_error_from_unix(errno);
3208 status = cli_dskattr_recv(req, bsize, total, avail);
3212 if (!NT_STATUS_IS_OK(status)) {
3213 cli_set_error(cli, status);
3218 /****************************************************************************
3219 Create and open a temporary file.
3220 ****************************************************************************/
3222 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
3227 memset(cli->outbuf,'\0',smb_size);
3228 memset(cli->inbuf,'\0',smb_size);
3230 cli_set_message(cli->outbuf,3,0,True);
3232 SCVAL(cli->outbuf,smb_com,SMBctemp);
3233 SSVAL(cli->outbuf,smb_tid,cli->cnum);
3234 cli_setup_packet(cli);
3236 SSVAL(cli->outbuf,smb_vwv0,0);
3237 SIVALS(cli->outbuf,smb_vwv1,-1);
3239 p = smb_buf(cli->outbuf);
3241 p += clistr_push(cli, p, path,
3242 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
3244 cli_setup_bcc(cli, p);
3247 if (!cli_receive_smb(cli)) {
3251 if (cli_is_error(cli)) {
3255 /* despite the spec, the result has a -1, followed by
3256 length, followed by name */
3257 p = smb_buf(cli->inbuf);
3259 len = smb_buflen(cli->inbuf) - 4;
3260 if (len <= 0 || len > PATH_MAX) return -1;
3263 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
3267 clistr_pull(cli->inbuf, path2, p,
3268 len+1, len, STR_ASCII);
3272 return SVAL(cli->inbuf,smb_vwv0);
3276 send a raw ioctl - used by the torture code
3278 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
3280 memset(cli->outbuf,'\0',smb_size);
3281 memset(cli->inbuf,'\0',smb_size);
3283 cli_set_message(cli->outbuf, 3, 0, True);
3284 SCVAL(cli->outbuf,smb_com,SMBioctl);
3285 cli_setup_packet(cli);
3287 SSVAL(cli->outbuf, smb_vwv0, fnum);
3288 SSVAL(cli->outbuf, smb_vwv1, code>>16);
3289 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
3292 if (!cli_receive_smb(cli)) {
3293 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3296 if (cli_is_error(cli)) {
3297 return cli_nt_error(cli);
3300 *blob = data_blob_null;
3302 return NT_STATUS_OK;
3305 /*********************************************************
3306 Set an extended attribute utility fn.
3307 *********************************************************/
3309 static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsigned int param_len,
3310 const char *ea_name, const char *ea_val, size_t ea_len)
3312 unsigned int data_len = 0;
3314 char *rparam=NULL, *rdata=NULL;
3316 size_t ea_namelen = strlen(ea_name);
3318 if (ea_namelen == 0 && ea_len == 0) {
3320 data = (char *)SMB_MALLOC(data_len);
3325 SIVAL(p,0,data_len);
3327 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
3328 data = (char *)SMB_MALLOC(data_len);
3333 SIVAL(p,0,data_len);
3335 SCVAL(p, 0, 0); /* EA flags. */
3336 SCVAL(p, 1, ea_namelen);
3337 SSVAL(p, 2, ea_len);
3338 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
3339 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
3342 if (!cli_send_trans(cli, SMBtrans2,
3344 -1, 0, /* fid, flags */
3345 &setup, 1, 0, /* setup, length, max */
3346 param, param_len, 2, /* param, length, max */
3347 data, data_len, cli->max_xmit /* data, length, max */
3353 if (!cli_receive_trans(cli, SMBtrans2,
3354 &rparam, ¶m_len,
3355 &rdata, &data_len)) {
3367 /*********************************************************
3368 Set an extended attribute on a pathname.
3369 *********************************************************/
3371 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
3373 uint16_t setup = TRANSACT2_SETPATHINFO;
3374 unsigned int param_len = 0;
3376 size_t srclen = 2*(strlen(path)+1);
3380 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
3384 memset(param, '\0', 6);
3385 SSVAL(param,0,SMB_INFO_SET_EA);
3388 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
3389 param_len = PTR_DIFF(p, param);
3391 ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);