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);
1985 int cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1987 unsigned int data_len = 1;
1988 unsigned int param_len = 6;
1989 uint16_t setup = TRANSACT2_SETFILEINFO;
1992 char *rparam=NULL, *rdata=NULL;
1994 memset(param, 0, param_len);
1995 SSVAL(param,0,fnum);
1996 SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
1998 data = flag ? 1 : 0;
2000 if (!cli_send_trans(cli, SMBtrans2,
2002 -1, 0, /* fid, flags */
2003 &setup, 1, 0, /* setup, length, max */
2004 param, param_len, 2, /* param, length, max */
2005 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2010 if (!cli_receive_trans(cli, SMBtrans2,
2011 &rparam, ¶m_len,
2012 &rdata, &data_len)) {
2023 struct cli_ntcreate_state {
2028 static void cli_ntcreate_done(struct tevent_req *subreq);
2030 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
2031 struct event_context *ev,
2032 struct cli_state *cli,
2034 uint32_t CreatFlags,
2035 uint32_t DesiredAccess,
2036 uint32_t FileAttributes,
2037 uint32_t ShareAccess,
2038 uint32_t CreateDisposition,
2039 uint32_t CreateOptions,
2040 uint8_t SecurityFlags)
2042 struct tevent_req *req, *subreq;
2043 struct cli_ntcreate_state *state;
2046 size_t converted_len;
2048 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
2054 SCVAL(vwv+0, 0, 0xFF);
2059 if (cli->use_oplocks) {
2060 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
2062 SIVAL(vwv+3, 1, CreatFlags);
2063 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
2064 SIVAL(vwv+7, 1, DesiredAccess);
2065 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
2066 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
2067 SIVAL(vwv+13, 1, FileAttributes);
2068 SIVAL(vwv+15, 1, ShareAccess);
2069 SIVAL(vwv+17, 1, CreateDisposition);
2070 SIVAL(vwv+19, 1, CreateOptions);
2071 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
2072 SCVAL(vwv+23, 1, SecurityFlags);
2074 bytes = talloc_array(state, uint8_t, 0);
2075 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
2076 fname, strlen(fname)+1,
2079 /* sigh. this copes with broken netapp filer behaviour */
2080 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
2082 if (tevent_req_nomem(bytes, req)) {
2083 return tevent_req_post(req, ev);
2086 SIVAL(vwv+2, 1, converted_len);
2088 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
2089 talloc_get_size(bytes), bytes);
2090 if (tevent_req_nomem(subreq, req)) {
2091 return tevent_req_post(req, ev);
2093 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
2097 static void cli_ntcreate_done(struct tevent_req *subreq)
2099 struct tevent_req *req = tevent_req_callback_data(
2100 subreq, struct tevent_req);
2101 struct cli_ntcreate_state *state = tevent_req_data(
2102 req, struct cli_ntcreate_state);
2109 status = cli_smb_recv(subreq, 3, &wct, &vwv, &num_bytes, &bytes);
2110 if (!NT_STATUS_IS_OK(status)) {
2111 TALLOC_FREE(subreq);
2112 tevent_req_nterror(req, status);
2115 state->fnum = SVAL(vwv+2, 1);
2116 tevent_req_done(req);
2119 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
2121 struct cli_ntcreate_state *state = tevent_req_data(
2122 req, struct cli_ntcreate_state);
2125 if (tevent_req_is_nterror(req, &status)) {
2128 *pfnum = state->fnum;
2129 return NT_STATUS_OK;
2132 NTSTATUS cli_ntcreate(struct cli_state *cli,
2134 uint32_t CreatFlags,
2135 uint32_t DesiredAccess,
2136 uint32_t FileAttributes,
2137 uint32_t ShareAccess,
2138 uint32_t CreateDisposition,
2139 uint32_t CreateOptions,
2140 uint8_t SecurityFlags,
2143 TALLOC_CTX *frame = talloc_stackframe();
2144 struct event_context *ev;
2145 struct tevent_req *req;
2146 NTSTATUS status = NT_STATUS_OK;
2148 if (cli_has_async_calls(cli)) {
2150 * Can't use sync call while an async call is in flight
2152 status = NT_STATUS_INVALID_PARAMETER;
2156 ev = event_context_init(frame);
2158 status = NT_STATUS_NO_MEMORY;
2162 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
2163 DesiredAccess, FileAttributes, ShareAccess,
2164 CreateDisposition, CreateOptions,
2167 status = NT_STATUS_NO_MEMORY;
2171 if (!tevent_req_poll(req, ev)) {
2172 status = map_nt_error_from_unix(errno);
2176 status = cli_ntcreate_recv(req, pfid);
2179 if (!NT_STATUS_IS_OK(status)) {
2180 cli_set_error(cli, status);
2185 /****************************************************************************
2187 WARNING: if you open with O_WRONLY then getattrE won't work!
2188 ****************************************************************************/
2190 struct cli_open_state {
2196 static void cli_open_done(struct tevent_req *subreq);
2198 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
2199 struct event_context *ev,
2200 struct cli_state *cli, const char *fname,
2201 int flags, int share_mode,
2202 struct tevent_req **psmbreq)
2204 struct tevent_req *req, *subreq;
2205 struct cli_open_state *state;
2207 unsigned accessmode;
2208 uint8_t additional_flags;
2211 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
2217 if (flags & O_CREAT) {
2220 if (!(flags & O_EXCL)) {
2221 if (flags & O_TRUNC)
2227 accessmode = (share_mode<<4);
2229 if ((flags & O_ACCMODE) == O_RDWR) {
2231 } else if ((flags & O_ACCMODE) == O_WRONLY) {
2236 if ((flags & O_SYNC) == O_SYNC) {
2237 accessmode |= (1<<14);
2241 if (share_mode == DENY_FCB) {
2245 SCVAL(state->vwv + 0, 0, 0xFF);
2246 SCVAL(state->vwv + 0, 1, 0);
2247 SSVAL(state->vwv + 1, 0, 0);
2248 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
2249 SSVAL(state->vwv + 3, 0, accessmode);
2250 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
2251 SSVAL(state->vwv + 5, 0, 0);
2252 SIVAL(state->vwv + 6, 0, 0);
2253 SSVAL(state->vwv + 8, 0, openfn);
2254 SIVAL(state->vwv + 9, 0, 0);
2255 SIVAL(state->vwv + 11, 0, 0);
2256 SIVAL(state->vwv + 13, 0, 0);
2258 additional_flags = 0;
2260 if (cli->use_oplocks) {
2261 /* if using oplocks then ask for a batch oplock via
2262 core and extended methods */
2264 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
2265 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
2268 bytes = talloc_array(state, uint8_t, 0);
2269 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2270 strlen(fname)+1, NULL);
2272 if (tevent_req_nomem(bytes, req)) {
2273 return tevent_req_post(req, ev);
2276 state->bytes.iov_base = (void *)bytes;
2277 state->bytes.iov_len = talloc_get_size(bytes);
2279 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
2280 15, state->vwv, 1, &state->bytes);
2281 if (subreq == NULL) {
2285 tevent_req_set_callback(subreq, cli_open_done, req);
2290 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
2291 struct cli_state *cli, const char *fname,
2292 int flags, int share_mode)
2294 struct tevent_req *req, *subreq;
2297 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2303 status = cli_smb_req_send(subreq);
2304 if (!NT_STATUS_IS_OK(status)) {
2305 tevent_req_nterror(req, status);
2306 return tevent_req_post(req, ev);
2311 static void cli_open_done(struct tevent_req *subreq)
2313 struct tevent_req *req = tevent_req_callback_data(
2314 subreq, struct tevent_req);
2315 struct cli_open_state *state = tevent_req_data(
2316 req, struct cli_open_state);
2321 status = cli_smb_recv(subreq, 3, &wct, &vwv, NULL, NULL);
2322 if (!NT_STATUS_IS_OK(status)) {
2323 TALLOC_FREE(subreq);
2324 tevent_req_nterror(req, status);
2327 state->fnum = SVAL(vwv+2, 0);
2328 tevent_req_done(req);
2331 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2333 struct cli_open_state *state = tevent_req_data(
2334 req, struct cli_open_state);
2337 if (tevent_req_is_nterror(req, &status)) {
2340 *pfnum = state->fnum;
2341 return NT_STATUS_OK;
2344 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2345 int share_mode, uint16_t *pfnum)
2347 TALLOC_CTX *frame = talloc_stackframe();
2348 struct event_context *ev;
2349 struct tevent_req *req;
2350 NTSTATUS status = NT_STATUS_OK;
2352 if (cli_has_async_calls(cli)) {
2354 * Can't use sync call while an async call is in flight
2356 status = NT_STATUS_INVALID_PARAMETER;
2360 ev = event_context_init(frame);
2362 status = NT_STATUS_NO_MEMORY;
2366 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2368 status = NT_STATUS_NO_MEMORY;
2372 if (!tevent_req_poll(req, ev)) {
2373 status = map_nt_error_from_unix(errno);
2377 status = cli_open_recv(req, pfnum);
2380 if (!NT_STATUS_IS_OK(status)) {
2381 cli_set_error(cli, status);
2386 /****************************************************************************
2388 ****************************************************************************/
2390 struct cli_close_state {
2394 static void cli_close_done(struct tevent_req *subreq);
2396 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2397 struct event_context *ev,
2398 struct cli_state *cli,
2400 struct tevent_req **psubreq)
2402 struct tevent_req *req, *subreq;
2403 struct cli_close_state *state;
2405 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2409 SSVAL(state->vwv+0, 0, fnum);
2410 SIVALS(state->vwv+1, 0, -1);
2412 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2414 if (subreq == NULL) {
2418 tevent_req_set_callback(subreq, cli_close_done, req);
2423 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2424 struct event_context *ev,
2425 struct cli_state *cli,
2428 struct tevent_req *req, *subreq;
2431 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2436 status = cli_smb_req_send(subreq);
2437 if (!NT_STATUS_IS_OK(status)) {
2438 tevent_req_nterror(req, status);
2439 return tevent_req_post(req, ev);
2444 static void cli_close_done(struct tevent_req *subreq)
2446 struct tevent_req *req = tevent_req_callback_data(
2447 subreq, struct tevent_req);
2450 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2451 TALLOC_FREE(subreq);
2452 if (!NT_STATUS_IS_OK(status)) {
2453 tevent_req_nterror(req, status);
2456 tevent_req_done(req);
2459 NTSTATUS cli_close_recv(struct tevent_req *req)
2461 return tevent_req_simple_recv_ntstatus(req);
2464 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2466 TALLOC_CTX *frame = talloc_stackframe();
2467 struct event_context *ev;
2468 struct tevent_req *req;
2469 NTSTATUS status = NT_STATUS_OK;
2471 if (cli_has_async_calls(cli)) {
2473 * Can't use sync call while an async call is in flight
2475 status = NT_STATUS_INVALID_PARAMETER;
2479 ev = event_context_init(frame);
2481 status = NT_STATUS_NO_MEMORY;
2485 req = cli_close_send(frame, ev, cli, fnum);
2487 status = NT_STATUS_NO_MEMORY;
2491 if (!tevent_req_poll(req, ev)) {
2492 status = map_nt_error_from_unix(errno);
2496 status = cli_close_recv(req);
2499 if (!NT_STATUS_IS_OK(status)) {
2500 cli_set_error(cli, status);
2505 /****************************************************************************
2506 Truncate a file to a specified size
2507 ****************************************************************************/
2509 bool cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2511 unsigned int param_len = 6;
2512 unsigned int data_len = 8;
2513 uint16_t setup = TRANSACT2_SETFILEINFO;
2515 unsigned char data[8];
2516 char *rparam=NULL, *rdata=NULL;
2517 int saved_timeout = cli->timeout;
2519 SSVAL(param,0,fnum);
2520 SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2523 SBVAL(data, 0, size);
2525 if (!cli_send_trans(cli, SMBtrans2,
2527 -1, 0, /* fid, flags */
2528 &setup, 1, 0, /* setup, length, max */
2529 param, param_len, 2, /* param, length, max */
2530 (char *)&data, data_len,/* data, length, ... */
2531 cli->max_xmit)) { /* ... max */
2532 cli->timeout = saved_timeout;
2536 if (!cli_receive_trans(cli, SMBtrans2,
2537 &rparam, ¶m_len,
2538 &rdata, &data_len)) {
2539 cli->timeout = saved_timeout;
2545 cli->timeout = saved_timeout;
2554 /****************************************************************************
2555 send a lock with a specified locktype
2556 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2557 ****************************************************************************/
2559 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2560 uint32_t offset, uint32_t len,
2561 int timeout, unsigned char locktype)
2564 int saved_timeout = cli->timeout;
2566 memset(cli->outbuf,'\0',smb_size);
2567 memset(cli->inbuf,'\0', smb_size);
2569 cli_set_message(cli->outbuf,8,0,True);
2571 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2572 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2573 cli_setup_packet(cli);
2575 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2576 SSVAL(cli->outbuf,smb_vwv2,fnum);
2577 SCVAL(cli->outbuf,smb_vwv3,locktype);
2578 SIVALS(cli->outbuf, smb_vwv4, timeout);
2579 SSVAL(cli->outbuf,smb_vwv6,0);
2580 SSVAL(cli->outbuf,smb_vwv7,1);
2582 p = smb_buf(cli->outbuf);
2583 SSVAL(p, 0, cli->pid);
2584 SIVAL(p, 2, offset);
2589 cli_setup_bcc(cli, p);
2594 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
2597 if (!cli_receive_smb(cli)) {
2598 cli->timeout = saved_timeout;
2599 return NT_STATUS_UNSUCCESSFUL;
2602 cli->timeout = saved_timeout;
2604 return cli_nt_error(cli);
2607 /****************************************************************************
2609 note that timeout is in units of 2 milliseconds
2610 ****************************************************************************/
2612 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2613 uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
2616 int saved_timeout = cli->timeout;
2618 memset(cli->outbuf,'\0',smb_size);
2619 memset(cli->inbuf,'\0', smb_size);
2621 cli_set_message(cli->outbuf,8,0,True);
2623 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2624 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2625 cli_setup_packet(cli);
2627 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2628 SSVAL(cli->outbuf,smb_vwv2,fnum);
2629 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
2630 SIVALS(cli->outbuf, smb_vwv4, timeout);
2631 SSVAL(cli->outbuf,smb_vwv6,0);
2632 SSVAL(cli->outbuf,smb_vwv7,1);
2634 p = smb_buf(cli->outbuf);
2635 SSVAL(p, 0, cli->pid);
2636 SIVAL(p, 2, offset);
2641 cli_setup_bcc(cli, p);
2646 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
2649 if (!cli_receive_smb(cli)) {
2650 cli->timeout = saved_timeout;
2654 cli->timeout = saved_timeout;
2656 if (cli_is_error(cli)) {
2663 /****************************************************************************
2665 ****************************************************************************/
2667 bool cli_unlock(struct cli_state *cli, uint16_t fnum, uint32_t offset, uint32_t len)
2671 memset(cli->outbuf,'\0',smb_size);
2672 memset(cli->inbuf,'\0',smb_size);
2674 cli_set_message(cli->outbuf,8,0,True);
2676 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2677 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2678 cli_setup_packet(cli);
2680 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2681 SSVAL(cli->outbuf,smb_vwv2,fnum);
2682 SCVAL(cli->outbuf,smb_vwv3,0);
2683 SIVALS(cli->outbuf, smb_vwv4, 0);
2684 SSVAL(cli->outbuf,smb_vwv6,1);
2685 SSVAL(cli->outbuf,smb_vwv7,0);
2687 p = smb_buf(cli->outbuf);
2688 SSVAL(p, 0, cli->pid);
2689 SIVAL(p, 2, offset);
2692 cli_setup_bcc(cli, p);
2694 if (!cli_receive_smb(cli)) {
2698 if (cli_is_error(cli)) {
2705 /****************************************************************************
2706 Lock a file with 64 bit offsets.
2707 ****************************************************************************/
2709 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2710 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
2713 int saved_timeout = cli->timeout;
2716 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2717 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2720 ltype = (lock_type == READ_LOCK? 1 : 0);
2721 ltype |= LOCKING_ANDX_LARGE_FILES;
2723 memset(cli->outbuf,'\0',smb_size);
2724 memset(cli->inbuf,'\0', smb_size);
2726 cli_set_message(cli->outbuf,8,0,True);
2728 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2729 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2730 cli_setup_packet(cli);
2732 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2733 SSVAL(cli->outbuf,smb_vwv2,fnum);
2734 SCVAL(cli->outbuf,smb_vwv3,ltype);
2735 SIVALS(cli->outbuf, smb_vwv4, timeout);
2736 SSVAL(cli->outbuf,smb_vwv6,0);
2737 SSVAL(cli->outbuf,smb_vwv7,1);
2739 p = smb_buf(cli->outbuf);
2740 SIVAL(p, 0, cli->pid);
2741 SOFF_T_R(p, 4, offset);
2742 SOFF_T_R(p, 12, len);
2745 cli_setup_bcc(cli, p);
2749 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
2752 if (!cli_receive_smb(cli)) {
2753 cli->timeout = saved_timeout;
2757 cli->timeout = saved_timeout;
2759 if (cli_is_error(cli)) {
2766 /****************************************************************************
2767 Unlock a file with 64 bit offsets.
2768 ****************************************************************************/
2770 bool cli_unlock64(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
2774 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2775 return cli_unlock(cli, fnum, offset, len);
2778 memset(cli->outbuf,'\0',smb_size);
2779 memset(cli->inbuf,'\0',smb_size);
2781 cli_set_message(cli->outbuf,8,0,True);
2783 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2784 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2785 cli_setup_packet(cli);
2787 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2788 SSVAL(cli->outbuf,smb_vwv2,fnum);
2789 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
2790 SIVALS(cli->outbuf, smb_vwv4, 0);
2791 SSVAL(cli->outbuf,smb_vwv6,1);
2792 SSVAL(cli->outbuf,smb_vwv7,0);
2794 p = smb_buf(cli->outbuf);
2795 SIVAL(p, 0, cli->pid);
2796 SOFF_T_R(p, 4, offset);
2797 SOFF_T_R(p, 12, len);
2799 cli_setup_bcc(cli, p);
2801 if (!cli_receive_smb(cli)) {
2805 if (cli_is_error(cli)) {
2812 /****************************************************************************
2813 Get/unlock a POSIX lock on a file - internal function.
2814 ****************************************************************************/
2816 static bool cli_posix_lock_internal(struct cli_state *cli, uint16_t fnum,
2817 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
2819 unsigned int param_len = 4;
2820 unsigned int data_len = POSIX_LOCK_DATA_SIZE;
2821 uint16_t setup = TRANSACT2_SETFILEINFO;
2823 unsigned char data[POSIX_LOCK_DATA_SIZE];
2824 char *rparam=NULL, *rdata=NULL;
2825 int saved_timeout = cli->timeout;
2827 SSVAL(param,0,fnum);
2828 SSVAL(param,2,SMB_SET_POSIX_LOCK);
2830 switch (lock_type) {
2832 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
2835 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
2838 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
2845 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
2846 cli->timeout = 0x7FFFFFFF;
2848 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
2851 SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
2852 SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
2853 SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
2855 if (!cli_send_trans(cli, SMBtrans2,
2857 -1, 0, /* fid, flags */
2858 &setup, 1, 0, /* setup, length, max */
2859 param, param_len, 2, /* param, length, max */
2860 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2862 cli->timeout = saved_timeout;
2866 if (!cli_receive_trans(cli, SMBtrans2,
2867 &rparam, ¶m_len,
2868 &rdata, &data_len)) {
2869 cli->timeout = saved_timeout;
2875 cli->timeout = saved_timeout;
2883 /****************************************************************************
2885 ****************************************************************************/
2887 bool cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2888 uint64_t offset, uint64_t len,
2889 bool wait_lock, enum brl_type lock_type)
2891 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2894 return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
2897 /****************************************************************************
2898 POSIX Unlock a file.
2899 ****************************************************************************/
2901 bool cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
2903 return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
2906 /****************************************************************************
2907 POSIX Get any lock covering a file.
2908 ****************************************************************************/
2910 bool cli_posix_getlock(struct cli_state *cli, uint16_t fnum, uint64_t *poffset, uint64_t *plen)
2915 /****************************************************************************
2916 Do a SMBgetattrE call.
2917 ****************************************************************************/
2919 static void cli_getattrE_done(struct tevent_req *subreq);
2921 struct cli_getattrE_state {
2931 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
2932 struct event_context *ev,
2933 struct cli_state *cli,
2936 struct tevent_req *req = NULL, *subreq = NULL;
2937 struct cli_getattrE_state *state = NULL;
2938 uint8_t additional_flags = 0;
2940 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
2945 state->zone_offset = cli->serverzone;
2946 SSVAL(state->vwv+0,0,fnum);
2948 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
2949 1, state->vwv, 0, NULL);
2950 if (tevent_req_nomem(subreq, req)) {
2951 return tevent_req_post(req, ev);
2953 tevent_req_set_callback(subreq, cli_getattrE_done, req);
2957 static void cli_getattrE_done(struct tevent_req *subreq)
2959 struct tevent_req *req = tevent_req_callback_data(
2960 subreq, struct tevent_req);
2961 struct cli_getattrE_state *state = tevent_req_data(
2962 req, struct cli_getattrE_state);
2964 uint16_t *vwv = NULL;
2967 status = cli_smb_recv(subreq, 11, &wct, &vwv, NULL, NULL);
2968 if (!NT_STATUS_IS_OK(status)) {
2969 tevent_req_nterror(req, status);
2973 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
2974 state->attr = SVAL(vwv+10,0);
2975 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
2976 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
2977 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
2979 TALLOC_FREE(subreq);
2980 tevent_req_done(req);
2983 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
2986 time_t *change_time,
2987 time_t *access_time,
2990 struct cli_getattrE_state *state = tevent_req_data(
2991 req, struct cli_getattrE_state);
2994 if (tevent_req_is_nterror(req, &status)) {
2998 *attr = state->attr;
3001 *size = state->size;
3004 *change_time = state->change_time;
3007 *access_time = state->access_time;
3010 *write_time = state->write_time;
3012 return NT_STATUS_OK;
3015 NTSTATUS cli_getattrE(struct cli_state *cli,
3019 time_t *change_time,
3020 time_t *access_time,
3023 TALLOC_CTX *frame = talloc_stackframe();
3024 struct event_context *ev = NULL;
3025 struct tevent_req *req = NULL;
3026 NTSTATUS status = NT_STATUS_OK;
3028 if (cli_has_async_calls(cli)) {
3030 * Can't use sync call while an async call is in flight
3032 status = NT_STATUS_INVALID_PARAMETER;
3036 ev = event_context_init(frame);
3038 status = NT_STATUS_NO_MEMORY;
3042 req = cli_getattrE_send(frame, ev, cli, fnum);
3044 status = NT_STATUS_NO_MEMORY;
3048 if (!tevent_req_poll(req, ev)) {
3049 status = map_nt_error_from_unix(errno);
3053 status = cli_getattrE_recv(req,
3062 if (!NT_STATUS_IS_OK(status)) {
3063 cli_set_error(cli, status);
3068 /****************************************************************************
3070 ****************************************************************************/
3072 static void cli_getatr_done(struct tevent_req *subreq);
3074 struct cli_getatr_state {
3081 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
3082 struct event_context *ev,
3083 struct cli_state *cli,
3086 struct tevent_req *req = NULL, *subreq = NULL;
3087 struct cli_getatr_state *state = NULL;
3088 uint8_t additional_flags = 0;
3089 uint8_t *bytes = NULL;
3091 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
3096 state->zone_offset = cli->serverzone;
3098 bytes = talloc_array(state, uint8_t, 1);
3099 if (tevent_req_nomem(bytes, req)) {
3100 return tevent_req_post(req, ev);
3103 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3104 strlen(fname)+1, NULL);
3106 if (tevent_req_nomem(bytes, req)) {
3107 return tevent_req_post(req, ev);
3110 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
3111 0, NULL, talloc_get_size(bytes), bytes);
3112 if (tevent_req_nomem(subreq, req)) {
3113 return tevent_req_post(req, ev);
3115 tevent_req_set_callback(subreq, cli_getatr_done, req);
3119 static void cli_getatr_done(struct tevent_req *subreq)
3121 struct tevent_req *req = tevent_req_callback_data(
3122 subreq, struct tevent_req);
3123 struct cli_getatr_state *state = tevent_req_data(
3124 req, struct cli_getatr_state);
3126 uint16_t *vwv = NULL;
3129 status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
3130 if (!NT_STATUS_IS_OK(status)) {
3131 tevent_req_nterror(req, status);
3135 state->attr = SVAL(vwv+0,0);
3136 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
3137 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
3139 TALLOC_FREE(subreq);
3140 tevent_req_done(req);
3143 NTSTATUS cli_getatr_recv(struct tevent_req *req,
3148 struct cli_getatr_state *state = tevent_req_data(
3149 req, struct cli_getatr_state);
3152 if (tevent_req_is_nterror(req, &status)) {
3156 *attr = state->attr;
3159 *size = state->size;
3162 *write_time = state->write_time;
3164 return NT_STATUS_OK;
3167 NTSTATUS cli_getatr(struct cli_state *cli,
3173 TALLOC_CTX *frame = talloc_stackframe();
3174 struct event_context *ev = NULL;
3175 struct tevent_req *req = NULL;
3176 NTSTATUS status = NT_STATUS_OK;
3178 if (cli_has_async_calls(cli)) {
3180 * Can't use sync call while an async call is in flight
3182 status = NT_STATUS_INVALID_PARAMETER;
3186 ev = event_context_init(frame);
3188 status = NT_STATUS_NO_MEMORY;
3192 req = cli_getatr_send(frame, ev, cli, fname);
3194 status = NT_STATUS_NO_MEMORY;
3198 if (!tevent_req_poll(req, ev)) {
3199 status = map_nt_error_from_unix(errno);
3203 status = cli_getatr_recv(req,
3210 if (!NT_STATUS_IS_OK(status)) {
3211 cli_set_error(cli, status);
3216 /****************************************************************************
3217 Do a SMBsetattrE call.
3218 ****************************************************************************/
3220 static void cli_setattrE_done(struct tevent_req *subreq);
3222 struct cli_setattrE_state {
3226 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
3227 struct event_context *ev,
3228 struct cli_state *cli,
3234 struct tevent_req *req = NULL, *subreq = NULL;
3235 struct cli_setattrE_state *state = NULL;
3236 uint8_t additional_flags = 0;
3239 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
3244 memset(vwv, '\0', sizeof(vwv));
3245 SSVAL(vwv+0, 0, fnum);
3246 cli_put_dos_date2(cli, (char *)&vwv[1], 0, change_time);
3247 cli_put_dos_date2(cli, (char *)&vwv[3], 0, access_time);
3248 cli_put_dos_date2(cli, (char *)&vwv[5], 0, write_time);
3250 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
3252 if (tevent_req_nomem(subreq, req)) {
3253 return tevent_req_post(req, ev);
3255 tevent_req_set_callback(subreq, cli_setattrE_done, req);
3259 static void cli_setattrE_done(struct tevent_req *subreq)
3261 struct tevent_req *req = tevent_req_callback_data(
3262 subreq, struct tevent_req);
3265 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
3266 TALLOC_FREE(subreq);
3267 if (!NT_STATUS_IS_OK(status)) {
3268 tevent_req_nterror(req, status);
3271 tevent_req_done(req);
3274 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
3276 return tevent_req_simple_recv_ntstatus(req);
3279 NTSTATUS cli_setattrE(struct cli_state *cli,
3285 TALLOC_CTX *frame = talloc_stackframe();
3286 struct event_context *ev = NULL;
3287 struct tevent_req *req = NULL;
3288 NTSTATUS status = NT_STATUS_OK;
3290 if (cli_has_async_calls(cli)) {
3292 * Can't use sync call while an async call is in flight
3294 status = NT_STATUS_INVALID_PARAMETER;
3298 ev = event_context_init(frame);
3300 status = NT_STATUS_NO_MEMORY;
3304 req = cli_setattrE_send(frame, ev,
3312 status = NT_STATUS_NO_MEMORY;
3316 if (!tevent_req_poll(req, ev)) {
3317 status = map_nt_error_from_unix(errno);
3321 status = cli_setattrE_recv(req);
3325 if (!NT_STATUS_IS_OK(status)) {
3326 cli_set_error(cli, status);
3331 /****************************************************************************
3332 Do a SMBsetatr call.
3333 ****************************************************************************/
3335 static void cli_setatr_done(struct tevent_req *subreq);
3337 struct cli_setatr_state {
3341 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3342 struct event_context *ev,
3343 struct cli_state *cli,
3348 struct tevent_req *req = NULL, *subreq = NULL;
3349 struct cli_setatr_state *state = NULL;
3350 uint8_t additional_flags = 0;
3351 uint8_t *bytes = NULL;
3353 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3358 memset(state->vwv, '\0', sizeof(state->vwv));
3359 SSVAL(state->vwv+0, 0, attr);
3360 cli_put_dos_date3(cli, (char *)&state->vwv[1], 0, mtime);
3362 bytes = talloc_array(state, uint8_t, 1);
3363 if (tevent_req_nomem(bytes, req)) {
3364 return tevent_req_post(req, ev);
3367 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3368 strlen(fname)+1, NULL);
3369 if (tevent_req_nomem(bytes, req)) {
3370 return tevent_req_post(req, ev);
3372 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3373 talloc_get_size(bytes)+1);
3374 if (tevent_req_nomem(bytes, req)) {
3375 return tevent_req_post(req, ev);
3378 bytes[talloc_get_size(bytes)-1] = 4;
3379 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3381 if (tevent_req_nomem(bytes, req)) {
3382 return tevent_req_post(req, ev);
3385 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3386 8, state->vwv, talloc_get_size(bytes), bytes);
3387 if (tevent_req_nomem(subreq, req)) {
3388 return tevent_req_post(req, ev);
3390 tevent_req_set_callback(subreq, cli_setatr_done, req);
3394 static void cli_setatr_done(struct tevent_req *subreq)
3396 struct tevent_req *req = tevent_req_callback_data(
3397 subreq, struct tevent_req);
3400 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
3401 TALLOC_FREE(subreq);
3402 if (!NT_STATUS_IS_OK(status)) {
3403 tevent_req_nterror(req, status);
3406 tevent_req_done(req);
3409 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3411 return tevent_req_simple_recv_ntstatus(req);
3414 NTSTATUS cli_setatr(struct cli_state *cli,
3419 TALLOC_CTX *frame = talloc_stackframe();
3420 struct event_context *ev = NULL;
3421 struct tevent_req *req = NULL;
3422 NTSTATUS status = NT_STATUS_OK;
3424 if (cli_has_async_calls(cli)) {
3426 * Can't use sync call while an async call is in flight
3428 status = NT_STATUS_INVALID_PARAMETER;
3432 ev = event_context_init(frame);
3434 status = NT_STATUS_NO_MEMORY;
3438 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3440 status = NT_STATUS_NO_MEMORY;
3444 if (!tevent_req_poll(req, ev)) {
3445 status = map_nt_error_from_unix(errno);
3449 status = cli_setatr_recv(req);
3453 if (!NT_STATUS_IS_OK(status)) {
3454 cli_set_error(cli, status);
3459 /****************************************************************************
3460 Check for existance of a dir.
3461 ****************************************************************************/
3463 static void cli_chkpath_done(struct tevent_req *subreq);
3465 struct cli_chkpath_state {
3469 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3470 struct event_context *ev,
3471 struct cli_state *cli,
3474 struct tevent_req *req = NULL, *subreq = NULL;
3475 struct cli_chkpath_state *state = NULL;
3476 uint8_t additional_flags = 0;
3477 uint8_t *bytes = NULL;
3479 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3484 bytes = talloc_array(state, uint8_t, 1);
3485 if (tevent_req_nomem(bytes, req)) {
3486 return tevent_req_post(req, ev);
3489 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3490 strlen(fname)+1, NULL);
3492 if (tevent_req_nomem(bytes, req)) {
3493 return tevent_req_post(req, ev);
3496 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3497 0, NULL, talloc_get_size(bytes), bytes);
3498 if (tevent_req_nomem(subreq, req)) {
3499 return tevent_req_post(req, ev);
3501 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3505 static void cli_chkpath_done(struct tevent_req *subreq)
3507 struct tevent_req *req = tevent_req_callback_data(
3508 subreq, struct tevent_req);
3511 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
3512 TALLOC_FREE(subreq);
3513 if (!NT_STATUS_IS_OK(status)) {
3514 tevent_req_nterror(req, status);
3517 tevent_req_done(req);
3520 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3522 return tevent_req_simple_recv_ntstatus(req);
3525 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3527 TALLOC_CTX *frame = talloc_stackframe();
3528 struct event_context *ev = NULL;
3529 struct tevent_req *req = NULL;
3531 NTSTATUS status = NT_STATUS_OK;
3533 if (cli_has_async_calls(cli)) {
3535 * Can't use sync call while an async call is in flight
3537 status = NT_STATUS_INVALID_PARAMETER;
3541 path2 = talloc_strdup(frame, path);
3543 status = NT_STATUS_NO_MEMORY;
3546 trim_char(path2,'\0','\\');
3548 path2 = talloc_strdup(frame, "\\");
3550 status = NT_STATUS_NO_MEMORY;
3555 ev = event_context_init(frame);
3557 status = NT_STATUS_NO_MEMORY;
3561 req = cli_chkpath_send(frame, ev, cli, path2);
3563 status = NT_STATUS_NO_MEMORY;
3567 if (!tevent_req_poll(req, ev)) {
3568 status = map_nt_error_from_unix(errno);
3572 status = cli_chkpath_recv(req);
3576 if (!NT_STATUS_IS_OK(status)) {
3577 cli_set_error(cli, status);
3582 /****************************************************************************
3584 ****************************************************************************/
3586 static void cli_dskattr_done(struct tevent_req *subreq);
3588 struct cli_dskattr_state {
3594 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3595 struct event_context *ev,
3596 struct cli_state *cli)
3598 struct tevent_req *req = NULL, *subreq = NULL;
3599 struct cli_dskattr_state *state = NULL;
3600 uint8_t additional_flags = 0;
3602 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3607 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3609 if (tevent_req_nomem(subreq, req)) {
3610 return tevent_req_post(req, ev);
3612 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3616 static void cli_dskattr_done(struct tevent_req *subreq)
3618 struct tevent_req *req = tevent_req_callback_data(
3619 subreq, struct tevent_req);
3620 struct cli_dskattr_state *state = tevent_req_data(
3621 req, struct cli_dskattr_state);
3623 uint16_t *vwv = NULL;
3626 status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
3627 if (!NT_STATUS_IS_OK(status)) {
3628 tevent_req_nterror(req, status);
3631 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3632 state->total = SVAL(vwv+0, 0);
3633 state->avail = SVAL(vwv+3, 0);
3634 TALLOC_FREE(subreq);
3635 tevent_req_done(req);
3638 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3640 struct cli_dskattr_state *state = tevent_req_data(
3641 req, struct cli_dskattr_state);
3644 if (tevent_req_is_nterror(req, &status)) {
3647 *bsize = state->bsize;
3648 *total = state->total;
3649 *avail = state->avail;
3650 return NT_STATUS_OK;
3653 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3655 TALLOC_CTX *frame = talloc_stackframe();
3656 struct event_context *ev = NULL;
3657 struct tevent_req *req = NULL;
3658 NTSTATUS status = NT_STATUS_OK;
3660 if (cli_has_async_calls(cli)) {
3662 * Can't use sync call while an async call is in flight
3664 status = NT_STATUS_INVALID_PARAMETER;
3668 ev = event_context_init(frame);
3670 status = NT_STATUS_NO_MEMORY;
3674 req = cli_dskattr_send(frame, ev, cli);
3676 status = NT_STATUS_NO_MEMORY;
3680 if (!tevent_req_poll(req, ev)) {
3681 status = map_nt_error_from_unix(errno);
3685 status = cli_dskattr_recv(req, bsize, total, avail);
3689 if (!NT_STATUS_IS_OK(status)) {
3690 cli_set_error(cli, status);
3695 /****************************************************************************
3696 Create and open a temporary file.
3697 ****************************************************************************/
3699 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
3704 memset(cli->outbuf,'\0',smb_size);
3705 memset(cli->inbuf,'\0',smb_size);
3707 cli_set_message(cli->outbuf,3,0,True);
3709 SCVAL(cli->outbuf,smb_com,SMBctemp);
3710 SSVAL(cli->outbuf,smb_tid,cli->cnum);
3711 cli_setup_packet(cli);
3713 SSVAL(cli->outbuf,smb_vwv0,0);
3714 SIVALS(cli->outbuf,smb_vwv1,-1);
3716 p = smb_buf(cli->outbuf);
3718 p += clistr_push(cli, p, path,
3719 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
3721 cli_setup_bcc(cli, p);
3724 if (!cli_receive_smb(cli)) {
3728 if (cli_is_error(cli)) {
3732 /* despite the spec, the result has a -1, followed by
3733 length, followed by name */
3734 p = smb_buf(cli->inbuf);
3736 len = smb_buflen(cli->inbuf) - 4;
3737 if (len <= 0 || len > PATH_MAX) return -1;
3740 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
3744 clistr_pull(cli->inbuf, path2, p,
3745 len+1, len, STR_ASCII);
3749 return SVAL(cli->inbuf,smb_vwv0);
3753 send a raw ioctl - used by the torture code
3755 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
3757 memset(cli->outbuf,'\0',smb_size);
3758 memset(cli->inbuf,'\0',smb_size);
3760 cli_set_message(cli->outbuf, 3, 0, True);
3761 SCVAL(cli->outbuf,smb_com,SMBioctl);
3762 cli_setup_packet(cli);
3764 SSVAL(cli->outbuf, smb_vwv0, fnum);
3765 SSVAL(cli->outbuf, smb_vwv1, code>>16);
3766 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
3769 if (!cli_receive_smb(cli)) {
3770 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3773 if (cli_is_error(cli)) {
3774 return cli_nt_error(cli);
3777 *blob = data_blob_null;
3779 return NT_STATUS_OK;
3782 /*********************************************************
3783 Set an extended attribute utility fn.
3784 *********************************************************/
3786 static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsigned int param_len,
3787 const char *ea_name, const char *ea_val, size_t ea_len)
3789 unsigned int data_len = 0;
3791 char *rparam=NULL, *rdata=NULL;
3793 size_t ea_namelen = strlen(ea_name);
3795 if (ea_namelen == 0 && ea_len == 0) {
3797 data = (char *)SMB_MALLOC(data_len);
3802 SIVAL(p,0,data_len);
3804 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
3805 data = (char *)SMB_MALLOC(data_len);
3810 SIVAL(p,0,data_len);
3812 SCVAL(p, 0, 0); /* EA flags. */
3813 SCVAL(p, 1, ea_namelen);
3814 SSVAL(p, 2, ea_len);
3815 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
3816 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
3819 if (!cli_send_trans(cli, SMBtrans2,
3821 -1, 0, /* fid, flags */
3822 &setup, 1, 0, /* setup, length, max */
3823 param, param_len, 2, /* param, length, max */
3824 data, data_len, cli->max_xmit /* data, length, max */
3830 if (!cli_receive_trans(cli, SMBtrans2,
3831 &rparam, ¶m_len,
3832 &rdata, &data_len)) {
3844 /*********************************************************
3845 Set an extended attribute on a pathname.
3846 *********************************************************/
3848 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
3850 uint16_t setup = TRANSACT2_SETPATHINFO;
3851 unsigned int param_len = 0;
3853 size_t srclen = 2*(strlen(path)+1);
3857 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
3861 memset(param, '\0', 6);
3862 SSVAL(param,0,SMB_INFO_SET_EA);
3865 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
3866 param_len = PTR_DIFF(p, param);
3868 ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
3873 /*********************************************************
3874 Set an extended attribute on an fnum.
3875 *********************************************************/
3877 bool cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum, const char *ea_name, const char *ea_val, size_t ea_len)
3880 uint16_t setup = TRANSACT2_SETFILEINFO;
3882 memset(param, 0, 6);
3883 SSVAL(param,0,fnum);
3884 SSVAL(param,2,SMB_INFO_SET_EA);
3886 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
3889 /*********************************************************
3890 Get an extended attribute list utility fn.
3891 *********************************************************/
3893 static bool cli_get_ea_list(struct cli_state *cli,
3894 uint16_t setup, char *param, unsigned int param_len,
3897 struct ea_struct **pea_list)
3899 unsigned int data_len = 0;
3900 unsigned int rparam_len, rdata_len;
3901 char *rparam=NULL, *rdata=NULL;
3906 struct ea_struct *ea_list;
3913 if (!cli_send_trans(cli, SMBtrans2,
3915 -1, 0, /* fid, flags */
3916 &setup, 1, 0, /* setup, length, max */
3917 param, param_len, 10, /* param, length, max */
3918 NULL, data_len, cli->max_xmit /* data, length, max */
3923 if (!cli_receive_trans(cli, SMBtrans2,
3924 &rparam, &rparam_len,
3925 &rdata, &rdata_len)) {
3929 if (!rdata || rdata_len < 4) {
3933 ea_size = (size_t)IVAL(rdata,0);
3934 if (ea_size > rdata_len) {
3939 /* No EA's present. */
3947 /* Validate the EA list and count it. */
3948 for (num_eas = 0; ea_size >= 4; num_eas++) {
3949 unsigned int ea_namelen = CVAL(p,1);
3950 unsigned int ea_valuelen = SVAL(p,2);
3951 if (ea_namelen == 0) {
3954 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
3957 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
3958 p += 4 + ea_namelen + 1 + ea_valuelen;
3966 *pnum_eas = num_eas;
3968 /* Caller only wants number of EA's. */
3973 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
3978 ea_size = (size_t)IVAL(rdata,0);
3981 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
3982 struct ea_struct *ea = &ea_list[num_eas];
3983 fstring unix_ea_name;
3984 unsigned int ea_namelen = CVAL(p,1);
3985 unsigned int ea_valuelen = SVAL(p,2);
3987 ea->flags = CVAL(p,0);
3988 unix_ea_name[0] = '\0';
3989 pull_ascii_fstring(unix_ea_name, p + 4);
3990 ea->name = talloc_strdup(ctx, unix_ea_name);
3991 /* Ensure the value is null terminated (in case it's a string). */
3992 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
3993 if (!ea->value.data) {
3997 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
3999 ea->value.data[ea_valuelen] = 0;
4001 p += 4 + ea_namelen + 1 + ea_valuelen;
4004 *pea_list = ea_list;
4014 /*********************************************************
4015 Get an extended attribute list from a pathname.
4016 *********************************************************/
4018 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
4021 struct ea_struct **pea_list)
4023 uint16_t setup = TRANSACT2_QPATHINFO;
4024 unsigned int param_len = 0;
4027 size_t srclen = 2*(strlen(path)+1);
4030 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
4036 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
4038 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
4039 param_len = PTR_DIFF(p, param);
4041 ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
4046 /*********************************************************
4047 Get an extended attribute list from an fnum.
4048 *********************************************************/
4050 bool cli_get_ea_list_fnum(struct cli_state *cli, uint16_t fnum,
4053 struct ea_struct **pea_list)
4055 uint16_t setup = TRANSACT2_QFILEINFO;
4058 memset(param, 0, 6);
4059 SSVAL(param,0,fnum);
4060 SSVAL(param,2,SMB_INFO_SET_EA);
4062 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
4065 /****************************************************************************
4066 Convert open "flags" arg to uint32_t on wire.
4067 ****************************************************************************/
4069 static uint32_t open_flags_to_wire(int flags)
4071 int open_mode = flags & O_ACCMODE;
4074 switch (open_mode) {
4076 ret |= SMB_O_WRONLY;
4083 ret |= SMB_O_RDONLY;
4087 if (flags & O_CREAT) {
4090 if (flags & O_EXCL) {
4093 if (flags & O_TRUNC) {
4097 if (flags & O_SYNC) {
4101 if (flags & O_APPEND) {
4102 ret |= SMB_O_APPEND;
4104 #if defined(O_DIRECT)
4105 if (flags & O_DIRECT) {
4106 ret |= SMB_O_DIRECT;
4109 #if defined(O_DIRECTORY)
4110 if (flags & O_DIRECTORY) {
4111 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4112 ret |= SMB_O_DIRECTORY;
4118 /****************************************************************************
4119 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
4120 ****************************************************************************/
4122 struct posix_open_state {
4126 uint16_t fnum; /* Out */
4129 static void cli_posix_open_internal_done(struct tevent_req *subreq)
4131 struct tevent_req *req = tevent_req_callback_data(
4132 subreq, struct tevent_req);
4133 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4138 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, &data, &num_data);
4139 TALLOC_FREE(subreq);
4140 if (!NT_STATUS_IS_OK(status)) {
4141 tevent_req_nterror(req, status);
4144 if (num_data < 12) {
4145 tevent_req_nterror(req, status);
4148 state->fnum = SVAL(data,2);
4149 tevent_req_done(req);
4152 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
4153 struct event_context *ev,
4154 struct cli_state *cli,
4160 struct tevent_req *req = NULL, *subreq = NULL;
4161 struct posix_open_state *state = NULL;
4162 uint32_t wire_flags = open_flags_to_wire(flags);
4164 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
4169 /* Setup setup word. */
4170 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4172 /* Setup param array. */
4173 state->param = talloc_array(state, uint8_t, 6);
4174 if (tevent_req_nomem(state->param, req)) {
4175 return tevent_req_post(req, ev);
4177 memset(state->param, '\0', 6);
4178 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
4180 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
4181 strlen(fname)+1, NULL);
4183 if (tevent_req_nomem(state->param, req)) {
4184 return tevent_req_post(req, ev);
4187 /* Setup data words. */
4189 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
4190 wire_flags |= SMB_O_DIRECTORY;
4193 SIVAL(state->data,0,0); /* No oplock. */
4194 SIVAL(state->data,4,wire_flags);
4195 SIVAL(state->data,8,unix_perms_to_wire(mode));
4196 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
4197 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
4199 subreq = cli_trans_send(state, /* mem ctx. */
4200 ev, /* event ctx. */
4201 cli, /* cli_state. */
4202 SMBtrans2, /* cmd. */
4203 NULL, /* pipe name. */
4207 &state->setup, /* setup. */
4208 1, /* num setup uint16_t words. */
4209 0, /* max returned setup. */
4210 state->param, /* param. */
4211 talloc_get_size(state->param),/* num param. */
4212 2, /* max returned param. */
4213 state->data, /* data. */
4215 12); /* max returned data. */
4217 if (tevent_req_nomem(subreq, req)) {
4218 return tevent_req_post(req, ev);
4220 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
4224 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
4225 struct event_context *ev,
4226 struct cli_state *cli,
4231 return cli_posix_open_internal_send(mem_ctx, ev,
4232 cli, fname, flags, mode, false);
4235 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
4237 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
4240 if (tevent_req_is_nterror(req, &status)) {
4243 *pfnum = state->fnum;
4244 return NT_STATUS_OK;
4247 /****************************************************************************
4248 Open - POSIX semantics. Doesn't request oplock.
4249 ****************************************************************************/
4251 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
4252 int flags, mode_t mode, uint16_t *pfnum)
4255 TALLOC_CTX *frame = talloc_stackframe();
4256 struct event_context *ev = NULL;
4257 struct tevent_req *req = NULL;
4258 NTSTATUS status = NT_STATUS_OK;
4260 if (cli_has_async_calls(cli)) {
4262 * Can't use sync call while an async call is in flight
4264 status = NT_STATUS_INVALID_PARAMETER;
4268 ev = event_context_init(frame);
4270 status = NT_STATUS_NO_MEMORY;
4274 req = cli_posix_open_send(frame,
4281 status = NT_STATUS_NO_MEMORY;
4285 if (!tevent_req_poll(req, ev)) {
4286 status = map_nt_error_from_unix(errno);
4290 status = cli_posix_open_recv(req, pfnum);
4294 if (!NT_STATUS_IS_OK(status)) {
4295 cli_set_error(cli, status);
4300 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4301 struct event_context *ev,
4302 struct cli_state *cli,
4306 return cli_posix_open_internal_send(mem_ctx, ev,
4307 cli, fname, O_CREAT, mode, true);
4310 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4314 if (tevent_req_is_nterror(req, &status)) {
4317 return NT_STATUS_OK;
4320 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4322 TALLOC_CTX *frame = talloc_stackframe();
4323 struct event_context *ev = NULL;
4324 struct tevent_req *req = NULL;
4325 NTSTATUS status = NT_STATUS_OK;
4327 if (cli_has_async_calls(cli)) {
4329 * Can't use sync call while an async call is in flight
4331 status = NT_STATUS_INVALID_PARAMETER;
4335 ev = event_context_init(frame);
4337 status = NT_STATUS_NO_MEMORY;
4341 req = cli_posix_mkdir_send(frame,
4347 status = NT_STATUS_NO_MEMORY;
4351 if (!tevent_req_poll(req, ev)) {
4352 status = map_nt_error_from_unix(errno);
4356 status = cli_posix_mkdir_recv(req);
4360 if (!NT_STATUS_IS_OK(status)) {
4361 cli_set_error(cli, status);
4366 /****************************************************************************
4367 unlink or rmdir - POSIX semantics.
4368 ****************************************************************************/
4370 struct unlink_state {
4375 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4377 struct tevent_req *req = tevent_req_callback_data(
4378 subreq, struct tevent_req);
4379 struct unlink_state *state = tevent_req_data(req, struct unlink_state);
4382 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, NULL, NULL);
4383 TALLOC_FREE(subreq);
4384 if (!NT_STATUS_IS_OK(status)) {
4385 tevent_req_nterror(req, status);
4388 tevent_req_done(req);
4391 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4392 struct event_context *ev,
4393 struct cli_state *cli,
4397 struct tevent_req *req = NULL, *subreq = NULL;
4398 struct unlink_state *state = NULL;
4399 uint8_t *param = NULL;
4401 req = tevent_req_create(mem_ctx, &state, struct unlink_state);
4406 /* Setup setup word. */
4407 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4409 /* Setup param array. */
4410 param = talloc_array(state, uint8_t, 6);
4411 if (tevent_req_nomem(param, req)) {
4412 return tevent_req_post(req, ev);
4414 memset(param, '\0', 6);
4415 SSVAL(param, 0, SMB_POSIX_PATH_UNLINK);
4417 param = trans2_bytes_push_str(param, cli_ucs2(cli), fname,
4418 strlen(fname)+1, NULL);
4420 if (tevent_req_nomem(param, req)) {
4421 return tevent_req_post(req, ev);
4424 /* Setup data word. */
4425 SSVAL(state->data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
4426 SMB_POSIX_UNLINK_FILE_TARGET);
4428 subreq = cli_trans_send(state, /* mem ctx. */
4429 ev, /* event ctx. */
4430 cli, /* cli_state. */
4431 SMBtrans2, /* cmd. */
4432 NULL, /* pipe name. */
4436 &state->setup, /* setup. */
4437 1, /* num setup uint16_t words. */
4438 0, /* max returned setup. */
4440 talloc_get_size(param), /* num param. */
4441 2, /* max returned param. */
4442 state->data, /* data. */
4444 0); /* max returned data. */
4446 if (tevent_req_nomem(subreq, req)) {
4447 return tevent_req_post(req, ev);
4449 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4453 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4454 struct event_context *ev,
4455 struct cli_state *cli,
4458 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, false);
4461 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4465 if (tevent_req_is_nterror(req, &status)) {
4468 return NT_STATUS_OK;
4471 /****************************************************************************
4472 unlink - POSIX semantics.
4473 ****************************************************************************/
4475 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4477 TALLOC_CTX *frame = talloc_stackframe();
4478 struct event_context *ev = NULL;
4479 struct tevent_req *req = NULL;
4480 NTSTATUS status = NT_STATUS_OK;
4482 if (cli_has_async_calls(cli)) {
4484 * Can't use sync call while an async call is in flight
4486 status = NT_STATUS_INVALID_PARAMETER;
4490 ev = event_context_init(frame);
4492 status = NT_STATUS_NO_MEMORY;
4496 req = cli_posix_unlink_send(frame,
4501 status = NT_STATUS_NO_MEMORY;
4505 if (!tevent_req_poll(req, ev)) {
4506 status = map_nt_error_from_unix(errno);
4510 status = cli_posix_unlink_recv(req);
4514 if (!NT_STATUS_IS_OK(status)) {
4515 cli_set_error(cli, status);
4520 /****************************************************************************
4521 rmdir - POSIX semantics.
4522 ****************************************************************************/
4524 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4525 struct event_context *ev,
4526 struct cli_state *cli,
4529 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, true);
4532 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4536 if (tevent_req_is_nterror(req, &status)) {
4539 return NT_STATUS_OK;
4542 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4544 TALLOC_CTX *frame = talloc_stackframe();
4545 struct event_context *ev = NULL;
4546 struct tevent_req *req = NULL;
4547 NTSTATUS status = NT_STATUS_OK;
4549 if (cli_has_async_calls(cli)) {
4551 * Can't use sync call while an async call is in flight
4553 status = NT_STATUS_INVALID_PARAMETER;
4557 ev = event_context_init(frame);
4559 status = NT_STATUS_NO_MEMORY;
4563 req = cli_posix_rmdir_send(frame,
4568 status = NT_STATUS_NO_MEMORY;
4572 if (!tevent_req_poll(req, ev)) {
4573 status = map_nt_error_from_unix(errno);
4577 status = cli_posix_rmdir_recv(req, frame);
4581 if (!NT_STATUS_IS_OK(status)) {
4582 cli_set_error(cli, status);