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 ****************************************************************************/
949 static bool cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname, uint32_t mode, uint32_t uid, uint32_t gid)
951 unsigned int data_len = 0;
952 unsigned int param_len = 0;
953 uint16_t setup = TRANSACT2_SETPATHINFO;
954 size_t nlen = 2*(strlen(fname)+1);
957 char *rparam=NULL, *rdata=NULL;
960 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
964 memset(param, '\0', 6);
965 memset(data, 0, sizeof(data));
967 SSVAL(param,0,SMB_SET_FILE_UNIX_BASIC);
970 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
971 param_len = PTR_DIFF(p, param);
973 memset(data, 0xff, 40); /* Set all sizes/times to no change. */
981 if (!cli_send_trans(cli, SMBtrans2,
983 -1, 0, /* fid, flags */
984 &setup, 1, 0, /* setup, length, max */
985 param, param_len, 2, /* param, length, max */
986 (char *)&data, data_len, cli->max_xmit /* data, length, max */
994 if (!cli_receive_trans(cli, SMBtrans2,
996 &rdata, &data_len)) {
1006 /****************************************************************************
1007 chmod a file (UNIX extensions).
1008 ****************************************************************************/
1010 bool cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
1012 return cli_unix_chmod_chown_internal(cli, fname,
1013 unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
1016 /****************************************************************************
1017 chown a file (UNIX extensions).
1018 ****************************************************************************/
1020 bool cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
1022 return cli_unix_chmod_chown_internal(cli, fname,
1023 SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
1026 /****************************************************************************
1028 ****************************************************************************/
1030 static void cli_rename_done(struct tevent_req *subreq);
1032 struct cli_rename_state {
1036 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
1037 struct event_context *ev,
1038 struct cli_state *cli,
1039 const char *fname_src,
1040 const char *fname_dst)
1042 struct tevent_req *req = NULL, *subreq = NULL;
1043 struct cli_rename_state *state = NULL;
1044 uint8_t additional_flags = 0;
1045 uint8_t *bytes = NULL;
1047 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
1052 SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
1054 bytes = talloc_array(state, uint8_t, 1);
1055 if (tevent_req_nomem(bytes, req)) {
1056 return tevent_req_post(req, ev);
1059 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1060 strlen(fname_src)+1, NULL);
1061 if (tevent_req_nomem(bytes, req)) {
1062 return tevent_req_post(req, ev);
1065 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1066 talloc_get_size(bytes)+1);
1067 if (tevent_req_nomem(bytes, req)) {
1068 return tevent_req_post(req, ev);
1071 bytes[talloc_get_size(bytes)-1] = 4;
1072 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1073 strlen(fname_dst)+1, NULL);
1074 if (tevent_req_nomem(bytes, req)) {
1075 return tevent_req_post(req, ev);
1078 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
1079 1, state->vwv, talloc_get_size(bytes), bytes);
1080 if (tevent_req_nomem(subreq, req)) {
1081 return tevent_req_post(req, ev);
1083 tevent_req_set_callback(subreq, cli_rename_done, req);
1087 static void cli_rename_done(struct tevent_req *subreq)
1089 struct tevent_req *req = tevent_req_callback_data(
1090 subreq, struct tevent_req);
1093 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1094 TALLOC_FREE(subreq);
1095 if (!NT_STATUS_IS_OK(status)) {
1096 tevent_req_nterror(req, status);
1099 tevent_req_done(req);
1102 NTSTATUS cli_rename_recv(struct tevent_req *req)
1104 return tevent_req_simple_recv_ntstatus(req);
1107 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1109 TALLOC_CTX *frame = talloc_stackframe();
1110 struct event_context *ev;
1111 struct tevent_req *req;
1112 NTSTATUS status = NT_STATUS_OK;
1114 if (cli_has_async_calls(cli)) {
1116 * Can't use sync call while an async call is in flight
1118 status = NT_STATUS_INVALID_PARAMETER;
1122 ev = event_context_init(frame);
1124 status = NT_STATUS_NO_MEMORY;
1128 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
1130 status = NT_STATUS_NO_MEMORY;
1134 if (!tevent_req_poll(req, ev)) {
1135 status = map_nt_error_from_unix(errno);
1139 status = cli_rename_recv(req);
1143 if (!NT_STATUS_IS_OK(status)) {
1144 cli_set_error(cli, status);
1149 /****************************************************************************
1151 ****************************************************************************/
1153 static void cli_ntrename_internal_done(struct tevent_req *subreq);
1155 struct cli_ntrename_internal_state {
1159 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
1160 struct event_context *ev,
1161 struct cli_state *cli,
1162 const char *fname_src,
1163 const char *fname_dst,
1164 uint16_t rename_flag)
1166 struct tevent_req *req = NULL, *subreq = NULL;
1167 struct cli_ntrename_internal_state *state = NULL;
1168 uint8_t additional_flags = 0;
1169 uint8_t *bytes = NULL;
1171 req = tevent_req_create(mem_ctx, &state,
1172 struct cli_ntrename_internal_state);
1177 SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
1178 SSVAL(state->vwv+1, 0, rename_flag);
1180 bytes = talloc_array(state, uint8_t, 1);
1181 if (tevent_req_nomem(bytes, req)) {
1182 return tevent_req_post(req, ev);
1185 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
1186 strlen(fname_src)+1, NULL);
1187 if (tevent_req_nomem(bytes, req)) {
1188 return tevent_req_post(req, ev);
1191 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
1192 talloc_get_size(bytes)+1);
1193 if (tevent_req_nomem(bytes, req)) {
1194 return tevent_req_post(req, ev);
1197 bytes[talloc_get_size(bytes)-1] = 4;
1198 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
1199 strlen(fname_dst)+1, NULL);
1200 if (tevent_req_nomem(bytes, req)) {
1201 return tevent_req_post(req, ev);
1204 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
1205 4, state->vwv, talloc_get_size(bytes), bytes);
1206 if (tevent_req_nomem(subreq, req)) {
1207 return tevent_req_post(req, ev);
1209 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
1213 static void cli_ntrename_internal_done(struct tevent_req *subreq)
1215 struct tevent_req *req = tevent_req_callback_data(
1216 subreq, struct tevent_req);
1219 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1220 TALLOC_FREE(subreq);
1221 if (!NT_STATUS_IS_OK(status)) {
1222 tevent_req_nterror(req, status);
1225 tevent_req_done(req);
1228 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
1230 return tevent_req_simple_recv_ntstatus(req);
1233 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
1234 struct event_context *ev,
1235 struct cli_state *cli,
1236 const char *fname_src,
1237 const char *fname_dst)
1239 return cli_ntrename_internal_send(mem_ctx,
1244 RENAME_FLAG_RENAME);
1247 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
1249 return cli_ntrename_internal_recv(req);
1252 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1254 TALLOC_CTX *frame = talloc_stackframe();
1255 struct event_context *ev;
1256 struct tevent_req *req;
1257 NTSTATUS status = NT_STATUS_OK;
1259 if (cli_has_async_calls(cli)) {
1261 * Can't use sync call while an async call is in flight
1263 status = NT_STATUS_INVALID_PARAMETER;
1267 ev = event_context_init(frame);
1269 status = NT_STATUS_NO_MEMORY;
1273 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
1275 status = NT_STATUS_NO_MEMORY;
1279 if (!tevent_req_poll(req, ev)) {
1280 status = map_nt_error_from_unix(errno);
1284 status = cli_ntrename_recv(req);
1288 if (!NT_STATUS_IS_OK(status)) {
1289 cli_set_error(cli, status);
1294 /****************************************************************************
1296 ****************************************************************************/
1298 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
1299 struct event_context *ev,
1300 struct cli_state *cli,
1301 const char *fname_src,
1302 const char *fname_dst)
1304 return cli_ntrename_internal_send(mem_ctx,
1309 RENAME_FLAG_HARD_LINK);
1312 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
1314 return cli_ntrename_internal_recv(req);
1317 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
1319 TALLOC_CTX *frame = talloc_stackframe();
1320 struct event_context *ev;
1321 struct tevent_req *req;
1322 NTSTATUS status = NT_STATUS_OK;
1324 if (cli_has_async_calls(cli)) {
1326 * Can't use sync call while an async call is in flight
1328 status = NT_STATUS_INVALID_PARAMETER;
1332 ev = event_context_init(frame);
1334 status = NT_STATUS_NO_MEMORY;
1338 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
1340 status = NT_STATUS_NO_MEMORY;
1344 if (!tevent_req_poll(req, ev)) {
1345 status = map_nt_error_from_unix(errno);
1349 status = cli_nt_hardlink_recv(req);
1353 if (!NT_STATUS_IS_OK(status)) {
1354 cli_set_error(cli, status);
1359 /****************************************************************************
1361 ****************************************************************************/
1363 static void cli_unlink_done(struct tevent_req *subreq);
1365 struct cli_unlink_state {
1369 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
1370 struct event_context *ev,
1371 struct cli_state *cli,
1373 uint16_t mayhave_attrs)
1375 struct tevent_req *req = NULL, *subreq = NULL;
1376 struct cli_unlink_state *state = NULL;
1377 uint8_t additional_flags = 0;
1378 uint8_t *bytes = NULL;
1380 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
1385 SSVAL(state->vwv+0, 0, mayhave_attrs);
1387 bytes = talloc_array(state, uint8_t, 1);
1388 if (tevent_req_nomem(bytes, req)) {
1389 return tevent_req_post(req, ev);
1392 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1393 strlen(fname)+1, NULL);
1395 if (tevent_req_nomem(bytes, req)) {
1396 return tevent_req_post(req, ev);
1399 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
1400 1, state->vwv, talloc_get_size(bytes), bytes);
1401 if (tevent_req_nomem(subreq, req)) {
1402 return tevent_req_post(req, ev);
1404 tevent_req_set_callback(subreq, cli_unlink_done, req);
1408 static void cli_unlink_done(struct tevent_req *subreq)
1410 struct tevent_req *req = tevent_req_callback_data(
1411 subreq, struct tevent_req);
1414 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1415 TALLOC_FREE(subreq);
1416 if (!NT_STATUS_IS_OK(status)) {
1417 tevent_req_nterror(req, status);
1420 tevent_req_done(req);
1423 NTSTATUS cli_unlink_recv(struct tevent_req *req)
1425 return tevent_req_simple_recv_ntstatus(req);
1428 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
1430 TALLOC_CTX *frame = talloc_stackframe();
1431 struct event_context *ev;
1432 struct tevent_req *req;
1433 NTSTATUS status = NT_STATUS_OK;
1435 if (cli_has_async_calls(cli)) {
1437 * Can't use sync call while an async call is in flight
1439 status = NT_STATUS_INVALID_PARAMETER;
1443 ev = event_context_init(frame);
1445 status = NT_STATUS_NO_MEMORY;
1449 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
1451 status = NT_STATUS_NO_MEMORY;
1455 if (!tevent_req_poll(req, ev)) {
1456 status = map_nt_error_from_unix(errno);
1460 status = cli_unlink_recv(req);
1464 if (!NT_STATUS_IS_OK(status)) {
1465 cli_set_error(cli, status);
1470 /****************************************************************************
1472 ****************************************************************************/
1474 static void cli_mkdir_done(struct tevent_req *subreq);
1476 struct cli_mkdir_state {
1480 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
1481 struct event_context *ev,
1482 struct cli_state *cli,
1485 struct tevent_req *req = NULL, *subreq = NULL;
1486 struct cli_mkdir_state *state = NULL;
1487 uint8_t additional_flags = 0;
1488 uint8_t *bytes = NULL;
1490 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
1495 bytes = talloc_array(state, uint8_t, 1);
1496 if (tevent_req_nomem(bytes, req)) {
1497 return tevent_req_post(req, ev);
1500 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1501 strlen(dname)+1, NULL);
1503 if (tevent_req_nomem(bytes, req)) {
1504 return tevent_req_post(req, ev);
1507 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
1508 0, NULL, talloc_get_size(bytes), bytes);
1509 if (tevent_req_nomem(subreq, req)) {
1510 return tevent_req_post(req, ev);
1512 tevent_req_set_callback(subreq, cli_mkdir_done, req);
1516 static void cli_mkdir_done(struct tevent_req *subreq)
1518 struct tevent_req *req = tevent_req_callback_data(
1519 subreq, struct tevent_req);
1522 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1523 TALLOC_FREE(subreq);
1524 if (!NT_STATUS_IS_OK(status)) {
1525 tevent_req_nterror(req, status);
1528 tevent_req_done(req);
1531 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
1533 return tevent_req_simple_recv_ntstatus(req);
1536 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
1538 TALLOC_CTX *frame = talloc_stackframe();
1539 struct event_context *ev;
1540 struct tevent_req *req;
1541 NTSTATUS status = NT_STATUS_OK;
1543 if (cli_has_async_calls(cli)) {
1545 * Can't use sync call while an async call is in flight
1547 status = NT_STATUS_INVALID_PARAMETER;
1551 ev = event_context_init(frame);
1553 status = NT_STATUS_NO_MEMORY;
1557 req = cli_mkdir_send(frame, ev, cli, dname);
1559 status = NT_STATUS_NO_MEMORY;
1563 if (!tevent_req_poll(req, ev)) {
1564 status = map_nt_error_from_unix(errno);
1568 status = cli_mkdir_recv(req);
1572 if (!NT_STATUS_IS_OK(status)) {
1573 cli_set_error(cli, status);
1578 /****************************************************************************
1580 ****************************************************************************/
1582 static void cli_rmdir_done(struct tevent_req *subreq);
1584 struct cli_rmdir_state {
1588 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
1589 struct event_context *ev,
1590 struct cli_state *cli,
1593 struct tevent_req *req = NULL, *subreq = NULL;
1594 struct cli_rmdir_state *state = NULL;
1595 uint8_t additional_flags = 0;
1596 uint8_t *bytes = NULL;
1598 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1603 bytes = talloc_array(state, uint8_t, 1);
1604 if (tevent_req_nomem(bytes, req)) {
1605 return tevent_req_post(req, ev);
1608 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1609 strlen(dname)+1, NULL);
1611 if (tevent_req_nomem(bytes, req)) {
1612 return tevent_req_post(req, ev);
1615 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1616 0, NULL, talloc_get_size(bytes), bytes);
1617 if (tevent_req_nomem(subreq, req)) {
1618 return tevent_req_post(req, ev);
1620 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1624 static void cli_rmdir_done(struct tevent_req *subreq)
1626 struct tevent_req *req = tevent_req_callback_data(
1627 subreq, struct tevent_req);
1630 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1631 TALLOC_FREE(subreq);
1632 if (!NT_STATUS_IS_OK(status)) {
1633 tevent_req_nterror(req, status);
1636 tevent_req_done(req);
1639 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1641 return tevent_req_simple_recv_ntstatus(req);
1644 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1646 TALLOC_CTX *frame = talloc_stackframe();
1647 struct event_context *ev;
1648 struct tevent_req *req;
1649 NTSTATUS status = NT_STATUS_OK;
1651 if (cli_has_async_calls(cli)) {
1653 * Can't use sync call while an async call is in flight
1655 status = NT_STATUS_INVALID_PARAMETER;
1659 ev = event_context_init(frame);
1661 status = NT_STATUS_NO_MEMORY;
1665 req = cli_rmdir_send(frame, ev, cli, dname);
1667 status = NT_STATUS_NO_MEMORY;
1671 if (!tevent_req_poll(req, ev)) {
1672 status = map_nt_error_from_unix(errno);
1676 status = cli_rmdir_recv(req);
1680 if (!NT_STATUS_IS_OK(status)) {
1681 cli_set_error(cli, status);
1686 /****************************************************************************
1687 Set or clear the delete on close flag.
1688 ****************************************************************************/
1690 int cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1692 unsigned int data_len = 1;
1693 unsigned int param_len = 6;
1694 uint16_t setup = TRANSACT2_SETFILEINFO;
1697 char *rparam=NULL, *rdata=NULL;
1699 memset(param, 0, param_len);
1700 SSVAL(param,0,fnum);
1701 SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
1703 data = flag ? 1 : 0;
1705 if (!cli_send_trans(cli, SMBtrans2,
1707 -1, 0, /* fid, flags */
1708 &setup, 1, 0, /* setup, length, max */
1709 param, param_len, 2, /* param, length, max */
1710 (char *)&data, data_len, cli->max_xmit /* data, length, max */
1715 if (!cli_receive_trans(cli, SMBtrans2,
1716 &rparam, ¶m_len,
1717 &rdata, &data_len)) {
1727 struct cli_ntcreate_state {
1732 static void cli_ntcreate_done(struct tevent_req *subreq);
1734 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1735 struct event_context *ev,
1736 struct cli_state *cli,
1738 uint32_t CreatFlags,
1739 uint32_t DesiredAccess,
1740 uint32_t FileAttributes,
1741 uint32_t ShareAccess,
1742 uint32_t CreateDisposition,
1743 uint32_t CreateOptions,
1744 uint8_t SecurityFlags)
1746 struct tevent_req *req, *subreq;
1747 struct cli_ntcreate_state *state;
1750 size_t converted_len;
1752 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1758 SCVAL(vwv+0, 0, 0xFF);
1763 if (cli->use_oplocks) {
1764 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1766 SIVAL(vwv+3, 1, CreatFlags);
1767 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1768 SIVAL(vwv+7, 1, DesiredAccess);
1769 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1770 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1771 SIVAL(vwv+13, 1, FileAttributes);
1772 SIVAL(vwv+15, 1, ShareAccess);
1773 SIVAL(vwv+17, 1, CreateDisposition);
1774 SIVAL(vwv+19, 1, CreateOptions);
1775 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1776 SCVAL(vwv+23, 1, SecurityFlags);
1778 bytes = talloc_array(state, uint8_t, 0);
1779 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1780 fname, strlen(fname)+1,
1783 /* sigh. this copes with broken netapp filer behaviour */
1784 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1786 if (tevent_req_nomem(bytes, req)) {
1787 return tevent_req_post(req, ev);
1790 SIVAL(vwv+2, 1, converted_len);
1792 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1793 talloc_get_size(bytes), bytes);
1794 if (tevent_req_nomem(subreq, req)) {
1795 return tevent_req_post(req, ev);
1797 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1801 static void cli_ntcreate_done(struct tevent_req *subreq)
1803 struct tevent_req *req = tevent_req_callback_data(
1804 subreq, struct tevent_req);
1805 struct cli_ntcreate_state *state = tevent_req_data(
1806 req, struct cli_ntcreate_state);
1813 status = cli_smb_recv(subreq, 3, &wct, &vwv, &num_bytes, &bytes);
1814 if (!NT_STATUS_IS_OK(status)) {
1815 TALLOC_FREE(subreq);
1816 tevent_req_nterror(req, status);
1819 state->fnum = SVAL(vwv+2, 1);
1820 tevent_req_done(req);
1823 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1825 struct cli_ntcreate_state *state = tevent_req_data(
1826 req, struct cli_ntcreate_state);
1829 if (tevent_req_is_nterror(req, &status)) {
1832 *pfnum = state->fnum;
1833 return NT_STATUS_OK;
1836 NTSTATUS cli_ntcreate(struct cli_state *cli,
1838 uint32_t CreatFlags,
1839 uint32_t DesiredAccess,
1840 uint32_t FileAttributes,
1841 uint32_t ShareAccess,
1842 uint32_t CreateDisposition,
1843 uint32_t CreateOptions,
1844 uint8_t SecurityFlags,
1847 TALLOC_CTX *frame = talloc_stackframe();
1848 struct event_context *ev;
1849 struct tevent_req *req;
1850 NTSTATUS status = NT_STATUS_OK;
1852 if (cli_has_async_calls(cli)) {
1854 * Can't use sync call while an async call is in flight
1856 status = NT_STATUS_INVALID_PARAMETER;
1860 ev = event_context_init(frame);
1862 status = NT_STATUS_NO_MEMORY;
1866 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1867 DesiredAccess, FileAttributes, ShareAccess,
1868 CreateDisposition, CreateOptions,
1871 status = NT_STATUS_NO_MEMORY;
1875 if (!tevent_req_poll(req, ev)) {
1876 status = map_nt_error_from_unix(errno);
1880 status = cli_ntcreate_recv(req, pfid);
1883 if (!NT_STATUS_IS_OK(status)) {
1884 cli_set_error(cli, status);
1889 /****************************************************************************
1891 WARNING: if you open with O_WRONLY then getattrE won't work!
1892 ****************************************************************************/
1894 struct cli_open_state {
1900 static void cli_open_done(struct tevent_req *subreq);
1902 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
1903 struct event_context *ev,
1904 struct cli_state *cli, const char *fname,
1905 int flags, int share_mode,
1906 struct tevent_req **psmbreq)
1908 struct tevent_req *req, *subreq;
1909 struct cli_open_state *state;
1911 unsigned accessmode;
1912 uint8_t additional_flags;
1915 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
1921 if (flags & O_CREAT) {
1924 if (!(flags & O_EXCL)) {
1925 if (flags & O_TRUNC)
1931 accessmode = (share_mode<<4);
1933 if ((flags & O_ACCMODE) == O_RDWR) {
1935 } else if ((flags & O_ACCMODE) == O_WRONLY) {
1940 if ((flags & O_SYNC) == O_SYNC) {
1941 accessmode |= (1<<14);
1945 if (share_mode == DENY_FCB) {
1949 SCVAL(state->vwv + 0, 0, 0xFF);
1950 SCVAL(state->vwv + 0, 1, 0);
1951 SSVAL(state->vwv + 1, 0, 0);
1952 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
1953 SSVAL(state->vwv + 3, 0, accessmode);
1954 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
1955 SSVAL(state->vwv + 5, 0, 0);
1956 SIVAL(state->vwv + 6, 0, 0);
1957 SSVAL(state->vwv + 8, 0, openfn);
1958 SIVAL(state->vwv + 9, 0, 0);
1959 SIVAL(state->vwv + 11, 0, 0);
1960 SIVAL(state->vwv + 13, 0, 0);
1962 additional_flags = 0;
1964 if (cli->use_oplocks) {
1965 /* if using oplocks then ask for a batch oplock via
1966 core and extended methods */
1968 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
1969 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
1972 bytes = talloc_array(state, uint8_t, 0);
1973 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1974 strlen(fname)+1, NULL);
1976 if (tevent_req_nomem(bytes, req)) {
1977 return tevent_req_post(req, ev);
1980 state->bytes.iov_base = (void *)bytes;
1981 state->bytes.iov_len = talloc_get_size(bytes);
1983 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
1984 15, state->vwv, 1, &state->bytes);
1985 if (subreq == NULL) {
1989 tevent_req_set_callback(subreq, cli_open_done, req);
1994 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1995 struct cli_state *cli, const char *fname,
1996 int flags, int share_mode)
1998 struct tevent_req *req, *subreq;
2001 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
2007 status = cli_smb_req_send(subreq);
2008 if (!NT_STATUS_IS_OK(status)) {
2009 tevent_req_nterror(req, status);
2010 return tevent_req_post(req, ev);
2015 static void cli_open_done(struct tevent_req *subreq)
2017 struct tevent_req *req = tevent_req_callback_data(
2018 subreq, struct tevent_req);
2019 struct cli_open_state *state = tevent_req_data(
2020 req, struct cli_open_state);
2025 status = cli_smb_recv(subreq, 3, &wct, &vwv, NULL, NULL);
2026 if (!NT_STATUS_IS_OK(status)) {
2027 TALLOC_FREE(subreq);
2028 tevent_req_nterror(req, status);
2031 state->fnum = SVAL(vwv+2, 0);
2032 tevent_req_done(req);
2035 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
2037 struct cli_open_state *state = tevent_req_data(
2038 req, struct cli_open_state);
2041 if (tevent_req_is_nterror(req, &status)) {
2044 *pfnum = state->fnum;
2045 return NT_STATUS_OK;
2048 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
2049 int share_mode, uint16_t *pfnum)
2051 TALLOC_CTX *frame = talloc_stackframe();
2052 struct event_context *ev;
2053 struct tevent_req *req;
2054 NTSTATUS status = NT_STATUS_OK;
2056 if (cli_has_async_calls(cli)) {
2058 * Can't use sync call while an async call is in flight
2060 status = NT_STATUS_INVALID_PARAMETER;
2064 ev = event_context_init(frame);
2066 status = NT_STATUS_NO_MEMORY;
2070 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
2072 status = NT_STATUS_NO_MEMORY;
2076 if (!tevent_req_poll(req, ev)) {
2077 status = map_nt_error_from_unix(errno);
2081 status = cli_open_recv(req, pfnum);
2084 if (!NT_STATUS_IS_OK(status)) {
2085 cli_set_error(cli, status);
2090 /****************************************************************************
2092 ****************************************************************************/
2094 struct cli_close_state {
2098 static void cli_close_done(struct tevent_req *subreq);
2100 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
2101 struct event_context *ev,
2102 struct cli_state *cli,
2104 struct tevent_req **psubreq)
2106 struct tevent_req *req, *subreq;
2107 struct cli_close_state *state;
2109 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
2113 SSVAL(state->vwv+0, 0, fnum);
2114 SIVALS(state->vwv+1, 0, -1);
2116 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
2118 if (subreq == NULL) {
2122 tevent_req_set_callback(subreq, cli_close_done, req);
2127 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
2128 struct event_context *ev,
2129 struct cli_state *cli,
2132 struct tevent_req *req, *subreq;
2135 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
2140 status = cli_smb_req_send(subreq);
2141 if (!NT_STATUS_IS_OK(status)) {
2142 tevent_req_nterror(req, status);
2143 return tevent_req_post(req, ev);
2148 static void cli_close_done(struct tevent_req *subreq)
2150 struct tevent_req *req = tevent_req_callback_data(
2151 subreq, struct tevent_req);
2154 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2155 TALLOC_FREE(subreq);
2156 if (!NT_STATUS_IS_OK(status)) {
2157 tevent_req_nterror(req, status);
2160 tevent_req_done(req);
2163 NTSTATUS cli_close_recv(struct tevent_req *req)
2165 return tevent_req_simple_recv_ntstatus(req);
2168 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
2170 TALLOC_CTX *frame = talloc_stackframe();
2171 struct event_context *ev;
2172 struct tevent_req *req;
2173 NTSTATUS status = NT_STATUS_OK;
2175 if (cli_has_async_calls(cli)) {
2177 * Can't use sync call while an async call is in flight
2179 status = NT_STATUS_INVALID_PARAMETER;
2183 ev = event_context_init(frame);
2185 status = NT_STATUS_NO_MEMORY;
2189 req = cli_close_send(frame, ev, cli, fnum);
2191 status = NT_STATUS_NO_MEMORY;
2195 if (!tevent_req_poll(req, ev)) {
2196 status = map_nt_error_from_unix(errno);
2200 status = cli_close_recv(req);
2203 if (!NT_STATUS_IS_OK(status)) {
2204 cli_set_error(cli, status);
2209 /****************************************************************************
2210 Truncate a file to a specified size
2211 ****************************************************************************/
2213 bool cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
2215 unsigned int param_len = 6;
2216 unsigned int data_len = 8;
2217 uint16_t setup = TRANSACT2_SETFILEINFO;
2219 unsigned char data[8];
2220 char *rparam=NULL, *rdata=NULL;
2221 int saved_timeout = cli->timeout;
2223 SSVAL(param,0,fnum);
2224 SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
2227 SBVAL(data, 0, size);
2229 if (!cli_send_trans(cli, SMBtrans2,
2231 -1, 0, /* fid, flags */
2232 &setup, 1, 0, /* setup, length, max */
2233 param, param_len, 2, /* param, length, max */
2234 (char *)&data, data_len,/* data, length, ... */
2235 cli->max_xmit)) { /* ... max */
2236 cli->timeout = saved_timeout;
2240 if (!cli_receive_trans(cli, SMBtrans2,
2241 &rparam, ¶m_len,
2242 &rdata, &data_len)) {
2243 cli->timeout = saved_timeout;
2249 cli->timeout = saved_timeout;
2258 /****************************************************************************
2259 send a lock with a specified locktype
2260 this is used for testing LOCKING_ANDX_CANCEL_LOCK
2261 ****************************************************************************/
2263 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
2264 uint32_t offset, uint32_t len,
2265 int timeout, unsigned char locktype)
2268 int saved_timeout = cli->timeout;
2270 memset(cli->outbuf,'\0',smb_size);
2271 memset(cli->inbuf,'\0', smb_size);
2273 cli_set_message(cli->outbuf,8,0,True);
2275 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2276 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2277 cli_setup_packet(cli);
2279 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2280 SSVAL(cli->outbuf,smb_vwv2,fnum);
2281 SCVAL(cli->outbuf,smb_vwv3,locktype);
2282 SIVALS(cli->outbuf, smb_vwv4, timeout);
2283 SSVAL(cli->outbuf,smb_vwv6,0);
2284 SSVAL(cli->outbuf,smb_vwv7,1);
2286 p = smb_buf(cli->outbuf);
2287 SSVAL(p, 0, cli->pid);
2288 SIVAL(p, 2, offset);
2293 cli_setup_bcc(cli, p);
2298 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
2301 if (!cli_receive_smb(cli)) {
2302 cli->timeout = saved_timeout;
2303 return NT_STATUS_UNSUCCESSFUL;
2306 cli->timeout = saved_timeout;
2308 return cli_nt_error(cli);
2311 /****************************************************************************
2313 note that timeout is in units of 2 milliseconds
2314 ****************************************************************************/
2316 bool cli_lock(struct cli_state *cli, uint16_t fnum,
2317 uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
2320 int saved_timeout = cli->timeout;
2322 memset(cli->outbuf,'\0',smb_size);
2323 memset(cli->inbuf,'\0', smb_size);
2325 cli_set_message(cli->outbuf,8,0,True);
2327 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2328 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2329 cli_setup_packet(cli);
2331 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2332 SSVAL(cli->outbuf,smb_vwv2,fnum);
2333 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
2334 SIVALS(cli->outbuf, smb_vwv4, timeout);
2335 SSVAL(cli->outbuf,smb_vwv6,0);
2336 SSVAL(cli->outbuf,smb_vwv7,1);
2338 p = smb_buf(cli->outbuf);
2339 SSVAL(p, 0, cli->pid);
2340 SIVAL(p, 2, offset);
2345 cli_setup_bcc(cli, p);
2350 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
2353 if (!cli_receive_smb(cli)) {
2354 cli->timeout = saved_timeout;
2358 cli->timeout = saved_timeout;
2360 if (cli_is_error(cli)) {
2367 /****************************************************************************
2369 ****************************************************************************/
2371 bool cli_unlock(struct cli_state *cli, uint16_t fnum, uint32_t offset, uint32_t len)
2375 memset(cli->outbuf,'\0',smb_size);
2376 memset(cli->inbuf,'\0',smb_size);
2378 cli_set_message(cli->outbuf,8,0,True);
2380 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2381 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2382 cli_setup_packet(cli);
2384 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2385 SSVAL(cli->outbuf,smb_vwv2,fnum);
2386 SCVAL(cli->outbuf,smb_vwv3,0);
2387 SIVALS(cli->outbuf, smb_vwv4, 0);
2388 SSVAL(cli->outbuf,smb_vwv6,1);
2389 SSVAL(cli->outbuf,smb_vwv7,0);
2391 p = smb_buf(cli->outbuf);
2392 SSVAL(p, 0, cli->pid);
2393 SIVAL(p, 2, offset);
2396 cli_setup_bcc(cli, p);
2398 if (!cli_receive_smb(cli)) {
2402 if (cli_is_error(cli)) {
2409 /****************************************************************************
2410 Lock a file with 64 bit offsets.
2411 ****************************************************************************/
2413 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
2414 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
2417 int saved_timeout = cli->timeout;
2420 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2421 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
2424 ltype = (lock_type == READ_LOCK? 1 : 0);
2425 ltype |= LOCKING_ANDX_LARGE_FILES;
2427 memset(cli->outbuf,'\0',smb_size);
2428 memset(cli->inbuf,'\0', smb_size);
2430 cli_set_message(cli->outbuf,8,0,True);
2432 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2433 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2434 cli_setup_packet(cli);
2436 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2437 SSVAL(cli->outbuf,smb_vwv2,fnum);
2438 SCVAL(cli->outbuf,smb_vwv3,ltype);
2439 SIVALS(cli->outbuf, smb_vwv4, timeout);
2440 SSVAL(cli->outbuf,smb_vwv6,0);
2441 SSVAL(cli->outbuf,smb_vwv7,1);
2443 p = smb_buf(cli->outbuf);
2444 SIVAL(p, 0, cli->pid);
2445 SOFF_T_R(p, 4, offset);
2446 SOFF_T_R(p, 12, len);
2449 cli_setup_bcc(cli, p);
2453 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
2456 if (!cli_receive_smb(cli)) {
2457 cli->timeout = saved_timeout;
2461 cli->timeout = saved_timeout;
2463 if (cli_is_error(cli)) {
2470 /****************************************************************************
2471 Unlock a file with 64 bit offsets.
2472 ****************************************************************************/
2474 bool cli_unlock64(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
2478 if (! (cli->capabilities & CAP_LARGE_FILES)) {
2479 return cli_unlock(cli, fnum, offset, len);
2482 memset(cli->outbuf,'\0',smb_size);
2483 memset(cli->inbuf,'\0',smb_size);
2485 cli_set_message(cli->outbuf,8,0,True);
2487 SCVAL(cli->outbuf,smb_com,SMBlockingX);
2488 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2489 cli_setup_packet(cli);
2491 SCVAL(cli->outbuf,smb_vwv0,0xFF);
2492 SSVAL(cli->outbuf,smb_vwv2,fnum);
2493 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
2494 SIVALS(cli->outbuf, smb_vwv4, 0);
2495 SSVAL(cli->outbuf,smb_vwv6,1);
2496 SSVAL(cli->outbuf,smb_vwv7,0);
2498 p = smb_buf(cli->outbuf);
2499 SIVAL(p, 0, cli->pid);
2500 SOFF_T_R(p, 4, offset);
2501 SOFF_T_R(p, 12, len);
2503 cli_setup_bcc(cli, p);
2505 if (!cli_receive_smb(cli)) {
2509 if (cli_is_error(cli)) {
2516 /****************************************************************************
2517 Get/unlock a POSIX lock on a file - internal function.
2518 ****************************************************************************/
2520 static bool cli_posix_lock_internal(struct cli_state *cli, uint16_t fnum,
2521 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
2523 unsigned int param_len = 4;
2524 unsigned int data_len = POSIX_LOCK_DATA_SIZE;
2525 uint16_t setup = TRANSACT2_SETFILEINFO;
2527 unsigned char data[POSIX_LOCK_DATA_SIZE];
2528 char *rparam=NULL, *rdata=NULL;
2529 int saved_timeout = cli->timeout;
2531 SSVAL(param,0,fnum);
2532 SSVAL(param,2,SMB_SET_POSIX_LOCK);
2534 switch (lock_type) {
2536 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
2539 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
2542 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
2549 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
2550 cli->timeout = 0x7FFFFFFF;
2552 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
2555 SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
2556 SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
2557 SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
2559 if (!cli_send_trans(cli, SMBtrans2,
2561 -1, 0, /* fid, flags */
2562 &setup, 1, 0, /* setup, length, max */
2563 param, param_len, 2, /* param, length, max */
2564 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2566 cli->timeout = saved_timeout;
2570 if (!cli_receive_trans(cli, SMBtrans2,
2571 &rparam, ¶m_len,
2572 &rdata, &data_len)) {
2573 cli->timeout = saved_timeout;
2579 cli->timeout = saved_timeout;
2587 /****************************************************************************
2589 ****************************************************************************/
2591 bool cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2592 uint64_t offset, uint64_t len,
2593 bool wait_lock, enum brl_type lock_type)
2595 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2598 return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
2601 /****************************************************************************
2602 POSIX Unlock a file.
2603 ****************************************************************************/
2605 bool cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
2607 return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
2610 /****************************************************************************
2611 POSIX Get any lock covering a file.
2612 ****************************************************************************/
2614 bool cli_posix_getlock(struct cli_state *cli, uint16_t fnum, uint64_t *poffset, uint64_t *plen)
2619 /****************************************************************************
2620 Do a SMBgetattrE call.
2621 ****************************************************************************/
2623 static void cli_getattrE_done(struct tevent_req *subreq);
2625 struct cli_getattrE_state {
2635 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
2636 struct event_context *ev,
2637 struct cli_state *cli,
2640 struct tevent_req *req = NULL, *subreq = NULL;
2641 struct cli_getattrE_state *state = NULL;
2642 uint8_t additional_flags = 0;
2644 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
2649 state->zone_offset = cli->serverzone;
2650 SSVAL(state->vwv+0,0,fnum);
2652 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
2653 1, state->vwv, 0, NULL);
2654 if (tevent_req_nomem(subreq, req)) {
2655 return tevent_req_post(req, ev);
2657 tevent_req_set_callback(subreq, cli_getattrE_done, req);
2661 static void cli_getattrE_done(struct tevent_req *subreq)
2663 struct tevent_req *req = tevent_req_callback_data(
2664 subreq, struct tevent_req);
2665 struct cli_getattrE_state *state = tevent_req_data(
2666 req, struct cli_getattrE_state);
2668 uint16_t *vwv = NULL;
2671 status = cli_smb_recv(subreq, 11, &wct, &vwv, NULL, NULL);
2672 if (!NT_STATUS_IS_OK(status)) {
2673 tevent_req_nterror(req, status);
2677 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
2678 state->attr = SVAL(vwv+10,0);
2679 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
2680 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
2681 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
2683 TALLOC_FREE(subreq);
2684 tevent_req_done(req);
2687 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
2690 time_t *change_time,
2691 time_t *access_time,
2694 struct cli_getattrE_state *state = tevent_req_data(
2695 req, struct cli_getattrE_state);
2698 if (tevent_req_is_nterror(req, &status)) {
2702 *attr = state->attr;
2705 *size = state->size;
2708 *change_time = state->change_time;
2711 *access_time = state->access_time;
2714 *write_time = state->write_time;
2716 return NT_STATUS_OK;
2719 NTSTATUS cli_getattrE(struct cli_state *cli,
2723 time_t *change_time,
2724 time_t *access_time,
2727 TALLOC_CTX *frame = talloc_stackframe();
2728 struct event_context *ev = NULL;
2729 struct tevent_req *req = NULL;
2730 NTSTATUS status = NT_STATUS_OK;
2732 if (cli_has_async_calls(cli)) {
2734 * Can't use sync call while an async call is in flight
2736 status = NT_STATUS_INVALID_PARAMETER;
2740 ev = event_context_init(frame);
2742 status = NT_STATUS_NO_MEMORY;
2746 req = cli_getattrE_send(frame, ev, cli, fnum);
2748 status = NT_STATUS_NO_MEMORY;
2752 if (!tevent_req_poll(req, ev)) {
2753 status = map_nt_error_from_unix(errno);
2757 status = cli_getattrE_recv(req,
2766 if (!NT_STATUS_IS_OK(status)) {
2767 cli_set_error(cli, status);
2772 /****************************************************************************
2774 ****************************************************************************/
2776 static void cli_getatr_done(struct tevent_req *subreq);
2778 struct cli_getatr_state {
2785 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
2786 struct event_context *ev,
2787 struct cli_state *cli,
2790 struct tevent_req *req = NULL, *subreq = NULL;
2791 struct cli_getatr_state *state = NULL;
2792 uint8_t additional_flags = 0;
2793 uint8_t *bytes = NULL;
2795 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
2800 state->zone_offset = cli->serverzone;
2802 bytes = talloc_array(state, uint8_t, 1);
2803 if (tevent_req_nomem(bytes, req)) {
2804 return tevent_req_post(req, ev);
2807 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2808 strlen(fname)+1, NULL);
2810 if (tevent_req_nomem(bytes, req)) {
2811 return tevent_req_post(req, ev);
2814 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
2815 0, NULL, talloc_get_size(bytes), bytes);
2816 if (tevent_req_nomem(subreq, req)) {
2817 return tevent_req_post(req, ev);
2819 tevent_req_set_callback(subreq, cli_getatr_done, req);
2823 static void cli_getatr_done(struct tevent_req *subreq)
2825 struct tevent_req *req = tevent_req_callback_data(
2826 subreq, struct tevent_req);
2827 struct cli_getatr_state *state = tevent_req_data(
2828 req, struct cli_getatr_state);
2830 uint16_t *vwv = NULL;
2833 status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
2834 if (!NT_STATUS_IS_OK(status)) {
2835 tevent_req_nterror(req, status);
2839 state->attr = SVAL(vwv+0,0);
2840 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
2841 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
2843 TALLOC_FREE(subreq);
2844 tevent_req_done(req);
2847 NTSTATUS cli_getatr_recv(struct tevent_req *req,
2852 struct cli_getatr_state *state = tevent_req_data(
2853 req, struct cli_getatr_state);
2856 if (tevent_req_is_nterror(req, &status)) {
2860 *attr = state->attr;
2863 *size = state->size;
2866 *write_time = state->write_time;
2868 return NT_STATUS_OK;
2871 NTSTATUS cli_getatr(struct cli_state *cli,
2877 TALLOC_CTX *frame = talloc_stackframe();
2878 struct event_context *ev = NULL;
2879 struct tevent_req *req = NULL;
2880 NTSTATUS status = NT_STATUS_OK;
2882 if (cli_has_async_calls(cli)) {
2884 * Can't use sync call while an async call is in flight
2886 status = NT_STATUS_INVALID_PARAMETER;
2890 ev = event_context_init(frame);
2892 status = NT_STATUS_NO_MEMORY;
2896 req = cli_getatr_send(frame, ev, cli, fname);
2898 status = NT_STATUS_NO_MEMORY;
2902 if (!tevent_req_poll(req, ev)) {
2903 status = map_nt_error_from_unix(errno);
2907 status = cli_getatr_recv(req,
2914 if (!NT_STATUS_IS_OK(status)) {
2915 cli_set_error(cli, status);
2920 /****************************************************************************
2921 Do a SMBsetattrE call.
2922 ****************************************************************************/
2924 static void cli_setattrE_done(struct tevent_req *subreq);
2926 struct cli_setattrE_state {
2930 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
2931 struct event_context *ev,
2932 struct cli_state *cli,
2938 struct tevent_req *req = NULL, *subreq = NULL;
2939 struct cli_setattrE_state *state = NULL;
2940 uint8_t additional_flags = 0;
2943 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
2948 memset(vwv, '\0', sizeof(vwv));
2949 SSVAL(vwv+0, 0, fnum);
2950 cli_put_dos_date2(cli, (char *)&vwv[1], 0, change_time);
2951 cli_put_dos_date2(cli, (char *)&vwv[3], 0, access_time);
2952 cli_put_dos_date2(cli, (char *)&vwv[5], 0, write_time);
2954 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
2956 if (tevent_req_nomem(subreq, req)) {
2957 return tevent_req_post(req, ev);
2959 tevent_req_set_callback(subreq, cli_setattrE_done, req);
2963 static void cli_setattrE_done(struct tevent_req *subreq)
2965 struct tevent_req *req = tevent_req_callback_data(
2966 subreq, struct tevent_req);
2969 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2970 TALLOC_FREE(subreq);
2971 if (!NT_STATUS_IS_OK(status)) {
2972 tevent_req_nterror(req, status);
2975 tevent_req_done(req);
2978 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
2980 return tevent_req_simple_recv_ntstatus(req);
2983 NTSTATUS cli_setattrE(struct cli_state *cli,
2989 TALLOC_CTX *frame = talloc_stackframe();
2990 struct event_context *ev = NULL;
2991 struct tevent_req *req = NULL;
2992 NTSTATUS status = NT_STATUS_OK;
2994 if (cli_has_async_calls(cli)) {
2996 * Can't use sync call while an async call is in flight
2998 status = NT_STATUS_INVALID_PARAMETER;
3002 ev = event_context_init(frame);
3004 status = NT_STATUS_NO_MEMORY;
3008 req = cli_setattrE_send(frame, ev,
3016 status = NT_STATUS_NO_MEMORY;
3020 if (!tevent_req_poll(req, ev)) {
3021 status = map_nt_error_from_unix(errno);
3025 status = cli_setattrE_recv(req);
3029 if (!NT_STATUS_IS_OK(status)) {
3030 cli_set_error(cli, status);
3035 /****************************************************************************
3036 Do a SMBsetatr call.
3037 ****************************************************************************/
3039 static void cli_setatr_done(struct tevent_req *subreq);
3041 struct cli_setatr_state {
3045 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
3046 struct event_context *ev,
3047 struct cli_state *cli,
3052 struct tevent_req *req = NULL, *subreq = NULL;
3053 struct cli_setatr_state *state = NULL;
3054 uint8_t additional_flags = 0;
3055 uint8_t *bytes = NULL;
3057 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
3062 memset(state->vwv, '\0', sizeof(state->vwv));
3063 SSVAL(state->vwv+0, 0, attr);
3064 cli_put_dos_date3(cli, (char *)&state->vwv[1], 0, mtime);
3066 bytes = talloc_array(state, uint8_t, 1);
3067 if (tevent_req_nomem(bytes, req)) {
3068 return tevent_req_post(req, ev);
3071 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3072 strlen(fname)+1, NULL);
3073 if (tevent_req_nomem(bytes, req)) {
3074 return tevent_req_post(req, ev);
3076 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
3077 talloc_get_size(bytes)+1);
3078 if (tevent_req_nomem(bytes, req)) {
3079 return tevent_req_post(req, ev);
3082 bytes[talloc_get_size(bytes)-1] = 4;
3083 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
3085 if (tevent_req_nomem(bytes, req)) {
3086 return tevent_req_post(req, ev);
3089 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
3090 8, state->vwv, talloc_get_size(bytes), bytes);
3091 if (tevent_req_nomem(subreq, req)) {
3092 return tevent_req_post(req, ev);
3094 tevent_req_set_callback(subreq, cli_setatr_done, req);
3098 static void cli_setatr_done(struct tevent_req *subreq)
3100 struct tevent_req *req = tevent_req_callback_data(
3101 subreq, struct tevent_req);
3104 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
3105 TALLOC_FREE(subreq);
3106 if (!NT_STATUS_IS_OK(status)) {
3107 tevent_req_nterror(req, status);
3110 tevent_req_done(req);
3113 NTSTATUS cli_setatr_recv(struct tevent_req *req)
3115 return tevent_req_simple_recv_ntstatus(req);
3118 NTSTATUS cli_setatr(struct cli_state *cli,
3123 TALLOC_CTX *frame = talloc_stackframe();
3124 struct event_context *ev = NULL;
3125 struct tevent_req *req = NULL;
3126 NTSTATUS status = NT_STATUS_OK;
3128 if (cli_has_async_calls(cli)) {
3130 * Can't use sync call while an async call is in flight
3132 status = NT_STATUS_INVALID_PARAMETER;
3136 ev = event_context_init(frame);
3138 status = NT_STATUS_NO_MEMORY;
3142 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
3144 status = NT_STATUS_NO_MEMORY;
3148 if (!tevent_req_poll(req, ev)) {
3149 status = map_nt_error_from_unix(errno);
3153 status = cli_setatr_recv(req);
3157 if (!NT_STATUS_IS_OK(status)) {
3158 cli_set_error(cli, status);
3163 /****************************************************************************
3164 Check for existance of a dir.
3165 ****************************************************************************/
3167 static void cli_chkpath_done(struct tevent_req *subreq);
3169 struct cli_chkpath_state {
3173 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
3174 struct event_context *ev,
3175 struct cli_state *cli,
3178 struct tevent_req *req = NULL, *subreq = NULL;
3179 struct cli_chkpath_state *state = NULL;
3180 uint8_t additional_flags = 0;
3181 uint8_t *bytes = NULL;
3183 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
3188 bytes = talloc_array(state, uint8_t, 1);
3189 if (tevent_req_nomem(bytes, req)) {
3190 return tevent_req_post(req, ev);
3193 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
3194 strlen(fname)+1, NULL);
3196 if (tevent_req_nomem(bytes, req)) {
3197 return tevent_req_post(req, ev);
3200 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
3201 0, NULL, talloc_get_size(bytes), bytes);
3202 if (tevent_req_nomem(subreq, req)) {
3203 return tevent_req_post(req, ev);
3205 tevent_req_set_callback(subreq, cli_chkpath_done, req);
3209 static void cli_chkpath_done(struct tevent_req *subreq)
3211 struct tevent_req *req = tevent_req_callback_data(
3212 subreq, struct tevent_req);
3215 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
3216 TALLOC_FREE(subreq);
3217 if (!NT_STATUS_IS_OK(status)) {
3218 tevent_req_nterror(req, status);
3221 tevent_req_done(req);
3224 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
3226 return tevent_req_simple_recv_ntstatus(req);
3229 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
3231 TALLOC_CTX *frame = talloc_stackframe();
3232 struct event_context *ev = NULL;
3233 struct tevent_req *req = NULL;
3235 NTSTATUS status = NT_STATUS_OK;
3237 if (cli_has_async_calls(cli)) {
3239 * Can't use sync call while an async call is in flight
3241 status = NT_STATUS_INVALID_PARAMETER;
3245 path2 = talloc_strdup(frame, path);
3247 status = NT_STATUS_NO_MEMORY;
3250 trim_char(path2,'\0','\\');
3252 path2 = talloc_strdup(frame, "\\");
3254 status = NT_STATUS_NO_MEMORY;
3259 ev = event_context_init(frame);
3261 status = NT_STATUS_NO_MEMORY;
3265 req = cli_chkpath_send(frame, ev, cli, path2);
3267 status = NT_STATUS_NO_MEMORY;
3271 if (!tevent_req_poll(req, ev)) {
3272 status = map_nt_error_from_unix(errno);
3276 status = cli_chkpath_recv(req);
3280 if (!NT_STATUS_IS_OK(status)) {
3281 cli_set_error(cli, status);
3286 /****************************************************************************
3288 ****************************************************************************/
3290 static void cli_dskattr_done(struct tevent_req *subreq);
3292 struct cli_dskattr_state {
3298 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
3299 struct event_context *ev,
3300 struct cli_state *cli)
3302 struct tevent_req *req = NULL, *subreq = NULL;
3303 struct cli_dskattr_state *state = NULL;
3304 uint8_t additional_flags = 0;
3306 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
3311 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
3313 if (tevent_req_nomem(subreq, req)) {
3314 return tevent_req_post(req, ev);
3316 tevent_req_set_callback(subreq, cli_dskattr_done, req);
3320 static void cli_dskattr_done(struct tevent_req *subreq)
3322 struct tevent_req *req = tevent_req_callback_data(
3323 subreq, struct tevent_req);
3324 struct cli_dskattr_state *state = tevent_req_data(
3325 req, struct cli_dskattr_state);
3327 uint16_t *vwv = NULL;
3330 status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
3331 if (!NT_STATUS_IS_OK(status)) {
3332 tevent_req_nterror(req, status);
3335 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
3336 state->total = SVAL(vwv+0, 0);
3337 state->avail = SVAL(vwv+3, 0);
3338 TALLOC_FREE(subreq);
3339 tevent_req_done(req);
3342 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
3344 struct cli_dskattr_state *state = tevent_req_data(
3345 req, struct cli_dskattr_state);
3348 if (tevent_req_is_nterror(req, &status)) {
3351 *bsize = state->bsize;
3352 *total = state->total;
3353 *avail = state->avail;
3354 return NT_STATUS_OK;
3357 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
3359 TALLOC_CTX *frame = talloc_stackframe();
3360 struct event_context *ev = NULL;
3361 struct tevent_req *req = NULL;
3362 NTSTATUS status = NT_STATUS_OK;
3364 if (cli_has_async_calls(cli)) {
3366 * Can't use sync call while an async call is in flight
3368 status = NT_STATUS_INVALID_PARAMETER;
3372 ev = event_context_init(frame);
3374 status = NT_STATUS_NO_MEMORY;
3378 req = cli_dskattr_send(frame, ev, cli);
3380 status = NT_STATUS_NO_MEMORY;
3384 if (!tevent_req_poll(req, ev)) {
3385 status = map_nt_error_from_unix(errno);
3389 status = cli_dskattr_recv(req, bsize, total, avail);
3393 if (!NT_STATUS_IS_OK(status)) {
3394 cli_set_error(cli, status);
3399 /****************************************************************************
3400 Create and open a temporary file.
3401 ****************************************************************************/
3403 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
3408 memset(cli->outbuf,'\0',smb_size);
3409 memset(cli->inbuf,'\0',smb_size);
3411 cli_set_message(cli->outbuf,3,0,True);
3413 SCVAL(cli->outbuf,smb_com,SMBctemp);
3414 SSVAL(cli->outbuf,smb_tid,cli->cnum);
3415 cli_setup_packet(cli);
3417 SSVAL(cli->outbuf,smb_vwv0,0);
3418 SIVALS(cli->outbuf,smb_vwv1,-1);
3420 p = smb_buf(cli->outbuf);
3422 p += clistr_push(cli, p, path,
3423 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
3425 cli_setup_bcc(cli, p);
3428 if (!cli_receive_smb(cli)) {
3432 if (cli_is_error(cli)) {
3436 /* despite the spec, the result has a -1, followed by
3437 length, followed by name */
3438 p = smb_buf(cli->inbuf);
3440 len = smb_buflen(cli->inbuf) - 4;
3441 if (len <= 0 || len > PATH_MAX) return -1;
3444 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
3448 clistr_pull(cli->inbuf, path2, p,
3449 len+1, len, STR_ASCII);
3453 return SVAL(cli->inbuf,smb_vwv0);
3457 send a raw ioctl - used by the torture code
3459 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
3461 memset(cli->outbuf,'\0',smb_size);
3462 memset(cli->inbuf,'\0',smb_size);
3464 cli_set_message(cli->outbuf, 3, 0, True);
3465 SCVAL(cli->outbuf,smb_com,SMBioctl);
3466 cli_setup_packet(cli);
3468 SSVAL(cli->outbuf, smb_vwv0, fnum);
3469 SSVAL(cli->outbuf, smb_vwv1, code>>16);
3470 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
3473 if (!cli_receive_smb(cli)) {
3474 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
3477 if (cli_is_error(cli)) {
3478 return cli_nt_error(cli);
3481 *blob = data_blob_null;
3483 return NT_STATUS_OK;
3486 /*********************************************************
3487 Set an extended attribute utility fn.
3488 *********************************************************/
3490 static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsigned int param_len,
3491 const char *ea_name, const char *ea_val, size_t ea_len)
3493 unsigned int data_len = 0;
3495 char *rparam=NULL, *rdata=NULL;
3497 size_t ea_namelen = strlen(ea_name);
3499 if (ea_namelen == 0 && ea_len == 0) {
3501 data = (char *)SMB_MALLOC(data_len);
3506 SIVAL(p,0,data_len);
3508 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
3509 data = (char *)SMB_MALLOC(data_len);
3514 SIVAL(p,0,data_len);
3516 SCVAL(p, 0, 0); /* EA flags. */
3517 SCVAL(p, 1, ea_namelen);
3518 SSVAL(p, 2, ea_len);
3519 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
3520 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
3523 if (!cli_send_trans(cli, SMBtrans2,
3525 -1, 0, /* fid, flags */
3526 &setup, 1, 0, /* setup, length, max */
3527 param, param_len, 2, /* param, length, max */
3528 data, data_len, cli->max_xmit /* data, length, max */
3534 if (!cli_receive_trans(cli, SMBtrans2,
3535 &rparam, ¶m_len,
3536 &rdata, &data_len)) {
3548 /*********************************************************
3549 Set an extended attribute on a pathname.
3550 *********************************************************/
3552 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
3554 uint16_t setup = TRANSACT2_SETPATHINFO;
3555 unsigned int param_len = 0;
3557 size_t srclen = 2*(strlen(path)+1);
3561 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
3565 memset(param, '\0', 6);
3566 SSVAL(param,0,SMB_INFO_SET_EA);
3569 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
3570 param_len = PTR_DIFF(p, param);
3572 ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
3577 /*********************************************************
3578 Set an extended attribute on an fnum.
3579 *********************************************************/
3581 bool cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum, const char *ea_name, const char *ea_val, size_t ea_len)
3584 uint16_t setup = TRANSACT2_SETFILEINFO;
3586 memset(param, 0, 6);
3587 SSVAL(param,0,fnum);
3588 SSVAL(param,2,SMB_INFO_SET_EA);
3590 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
3593 /*********************************************************
3594 Get an extended attribute list utility fn.
3595 *********************************************************/
3597 static bool cli_get_ea_list(struct cli_state *cli,
3598 uint16_t setup, char *param, unsigned int param_len,
3601 struct ea_struct **pea_list)
3603 unsigned int data_len = 0;
3604 unsigned int rparam_len, rdata_len;
3605 char *rparam=NULL, *rdata=NULL;
3610 struct ea_struct *ea_list;
3617 if (!cli_send_trans(cli, SMBtrans2,
3619 -1, 0, /* fid, flags */
3620 &setup, 1, 0, /* setup, length, max */
3621 param, param_len, 10, /* param, length, max */
3622 NULL, data_len, cli->max_xmit /* data, length, max */
3627 if (!cli_receive_trans(cli, SMBtrans2,
3628 &rparam, &rparam_len,
3629 &rdata, &rdata_len)) {
3633 if (!rdata || rdata_len < 4) {
3637 ea_size = (size_t)IVAL(rdata,0);
3638 if (ea_size > rdata_len) {
3643 /* No EA's present. */
3651 /* Validate the EA list and count it. */
3652 for (num_eas = 0; ea_size >= 4; num_eas++) {
3653 unsigned int ea_namelen = CVAL(p,1);
3654 unsigned int ea_valuelen = SVAL(p,2);
3655 if (ea_namelen == 0) {
3658 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
3661 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
3662 p += 4 + ea_namelen + 1 + ea_valuelen;
3670 *pnum_eas = num_eas;
3672 /* Caller only wants number of EA's. */
3677 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
3682 ea_size = (size_t)IVAL(rdata,0);
3685 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
3686 struct ea_struct *ea = &ea_list[num_eas];
3687 fstring unix_ea_name;
3688 unsigned int ea_namelen = CVAL(p,1);
3689 unsigned int ea_valuelen = SVAL(p,2);
3691 ea->flags = CVAL(p,0);
3692 unix_ea_name[0] = '\0';
3693 pull_ascii_fstring(unix_ea_name, p + 4);
3694 ea->name = talloc_strdup(ctx, unix_ea_name);
3695 /* Ensure the value is null terminated (in case it's a string). */
3696 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
3697 if (!ea->value.data) {
3701 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
3703 ea->value.data[ea_valuelen] = 0;
3705 p += 4 + ea_namelen + 1 + ea_valuelen;
3708 *pea_list = ea_list;
3718 /*********************************************************
3719 Get an extended attribute list from a pathname.
3720 *********************************************************/
3722 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
3725 struct ea_struct **pea_list)
3727 uint16_t setup = TRANSACT2_QPATHINFO;
3728 unsigned int param_len = 0;
3731 size_t srclen = 2*(strlen(path)+1);
3734 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
3740 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
3742 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
3743 param_len = PTR_DIFF(p, param);
3745 ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
3750 /*********************************************************
3751 Get an extended attribute list from an fnum.
3752 *********************************************************/
3754 bool cli_get_ea_list_fnum(struct cli_state *cli, uint16_t fnum,
3757 struct ea_struct **pea_list)
3759 uint16_t setup = TRANSACT2_QFILEINFO;
3762 memset(param, 0, 6);
3763 SSVAL(param,0,fnum);
3764 SSVAL(param,2,SMB_INFO_SET_EA);
3766 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
3769 /****************************************************************************
3770 Convert open "flags" arg to uint32_t on wire.
3771 ****************************************************************************/
3773 static uint32_t open_flags_to_wire(int flags)
3775 int open_mode = flags & O_ACCMODE;
3778 switch (open_mode) {
3780 ret |= SMB_O_WRONLY;
3787 ret |= SMB_O_RDONLY;
3791 if (flags & O_CREAT) {
3794 if (flags & O_EXCL) {
3797 if (flags & O_TRUNC) {
3801 if (flags & O_SYNC) {
3805 if (flags & O_APPEND) {
3806 ret |= SMB_O_APPEND;
3808 #if defined(O_DIRECT)
3809 if (flags & O_DIRECT) {
3810 ret |= SMB_O_DIRECT;
3813 #if defined(O_DIRECTORY)
3814 if (flags & O_DIRECTORY) {
3815 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
3816 ret |= SMB_O_DIRECTORY;
3822 /****************************************************************************
3823 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
3824 ****************************************************************************/
3826 struct posix_open_state {
3830 uint16_t fnum; /* Out */
3833 static void cli_posix_open_internal_done(struct tevent_req *subreq)
3835 struct tevent_req *req = tevent_req_callback_data(
3836 subreq, struct tevent_req);
3837 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
3842 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, &data, &num_data);
3843 TALLOC_FREE(subreq);
3844 if (!NT_STATUS_IS_OK(status)) {
3845 tevent_req_nterror(req, status);
3848 if (num_data < 12) {
3849 tevent_req_nterror(req, status);
3852 state->fnum = SVAL(data,2);
3853 tevent_req_done(req);
3856 static struct tevent_req *cli_posix_open_internal_send(TALLOC_CTX *mem_ctx,
3857 struct event_context *ev,
3858 struct cli_state *cli,
3864 struct tevent_req *req = NULL, *subreq = NULL;
3865 struct posix_open_state *state = NULL;
3866 uint32_t wire_flags = open_flags_to_wire(flags);
3868 req = tevent_req_create(mem_ctx, &state, struct posix_open_state);
3873 /* Setup setup word. */
3874 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
3876 /* Setup param array. */
3877 state->param = talloc_array(state, uint8_t, 6);
3878 if (tevent_req_nomem(state->param, req)) {
3879 return tevent_req_post(req, ev);
3881 memset(state->param, '\0', 6);
3882 SSVAL(state->param, 0, SMB_POSIX_PATH_OPEN);
3884 state->param = trans2_bytes_push_str(state->param, cli_ucs2(cli), fname,
3885 strlen(fname)+1, NULL);
3887 if (tevent_req_nomem(state->param, req)) {
3888 return tevent_req_post(req, ev);
3891 /* Setup data words. */
3893 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
3894 wire_flags |= SMB_O_DIRECTORY;
3897 SIVAL(state->data,0,0); /* No oplock. */
3898 SIVAL(state->data,4,wire_flags);
3899 SIVAL(state->data,8,unix_perms_to_wire(mode));
3900 SIVAL(state->data,12,0); /* Top bits of perms currently undefined. */
3901 SSVAL(state->data,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
3903 subreq = cli_trans_send(state, /* mem ctx. */
3904 ev, /* event ctx. */
3905 cli, /* cli_state. */
3906 SMBtrans2, /* cmd. */
3907 NULL, /* pipe name. */
3911 &state->setup, /* setup. */
3912 1, /* num setup uint16_t words. */
3913 0, /* max returned setup. */
3914 state->param, /* param. */
3915 talloc_get_size(state->param),/* num param. */
3916 2, /* max returned param. */
3917 state->data, /* data. */
3919 12); /* max returned data. */
3921 if (tevent_req_nomem(subreq, req)) {
3922 return tevent_req_post(req, ev);
3924 tevent_req_set_callback(subreq, cli_posix_open_internal_done, req);
3928 struct tevent_req *cli_posix_open_send(TALLOC_CTX *mem_ctx,
3929 struct event_context *ev,
3930 struct cli_state *cli,
3935 return cli_posix_open_internal_send(mem_ctx, ev,
3936 cli, fname, flags, mode, false);
3939 NTSTATUS cli_posix_open_recv(struct tevent_req *req, uint16_t *pfnum)
3941 struct posix_open_state *state = tevent_req_data(req, struct posix_open_state);
3944 if (tevent_req_is_nterror(req, &status)) {
3947 *pfnum = state->fnum;
3948 return NT_STATUS_OK;
3951 /****************************************************************************
3952 Open - POSIX semantics. Doesn't request oplock.
3953 ****************************************************************************/
3955 NTSTATUS cli_posix_open(struct cli_state *cli, const char *fname,
3956 int flags, mode_t mode, uint16_t *pfnum)
3959 TALLOC_CTX *frame = talloc_stackframe();
3960 struct event_context *ev = NULL;
3961 struct tevent_req *req = NULL;
3962 NTSTATUS status = NT_STATUS_OK;
3964 if (cli_has_async_calls(cli)) {
3966 * Can't use sync call while an async call is in flight
3968 status = NT_STATUS_INVALID_PARAMETER;
3972 ev = event_context_init(frame);
3974 status = NT_STATUS_NO_MEMORY;
3978 req = cli_posix_open_send(frame,
3985 status = NT_STATUS_NO_MEMORY;
3989 if (!tevent_req_poll(req, ev)) {
3990 status = map_nt_error_from_unix(errno);
3994 status = cli_posix_open_recv(req, pfnum);
3998 if (!NT_STATUS_IS_OK(status)) {
3999 cli_set_error(cli, status);
4004 struct tevent_req *cli_posix_mkdir_send(TALLOC_CTX *mem_ctx,
4005 struct event_context *ev,
4006 struct cli_state *cli,
4010 return cli_posix_open_internal_send(mem_ctx, ev,
4011 cli, fname, O_CREAT, mode, true);
4014 NTSTATUS cli_posix_mkdir_recv(struct tevent_req *req)
4018 if (tevent_req_is_nterror(req, &status)) {
4021 return NT_STATUS_OK;
4024 NTSTATUS cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
4026 TALLOC_CTX *frame = talloc_stackframe();
4027 struct event_context *ev = NULL;
4028 struct tevent_req *req = NULL;
4029 NTSTATUS status = NT_STATUS_OK;
4031 if (cli_has_async_calls(cli)) {
4033 * Can't use sync call while an async call is in flight
4035 status = NT_STATUS_INVALID_PARAMETER;
4039 ev = event_context_init(frame);
4041 status = NT_STATUS_NO_MEMORY;
4045 req = cli_posix_mkdir_send(frame,
4051 status = NT_STATUS_NO_MEMORY;
4055 if (!tevent_req_poll(req, ev)) {
4056 status = map_nt_error_from_unix(errno);
4060 status = cli_posix_mkdir_recv(req);
4064 if (!NT_STATUS_IS_OK(status)) {
4065 cli_set_error(cli, status);
4070 /****************************************************************************
4071 unlink or rmdir - POSIX semantics.
4072 ****************************************************************************/
4074 struct unlink_state {
4079 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
4081 struct tevent_req *req = tevent_req_callback_data(
4082 subreq, struct tevent_req);
4083 struct unlink_state *state = tevent_req_data(req, struct unlink_state);
4086 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, NULL, NULL);
4087 TALLOC_FREE(subreq);
4088 if (!NT_STATUS_IS_OK(status)) {
4089 tevent_req_nterror(req, status);
4092 tevent_req_done(req);
4095 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
4096 struct event_context *ev,
4097 struct cli_state *cli,
4101 struct tevent_req *req = NULL, *subreq = NULL;
4102 struct unlink_state *state = NULL;
4103 uint8_t *param = NULL;
4105 req = tevent_req_create(mem_ctx, &state, struct unlink_state);
4110 /* Setup setup word. */
4111 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
4113 /* Setup param array. */
4114 param = talloc_array(state, uint8_t, 6);
4115 if (tevent_req_nomem(param, req)) {
4116 return tevent_req_post(req, ev);
4118 memset(param, '\0', 6);
4119 SSVAL(param, 0, SMB_POSIX_PATH_UNLINK);
4121 param = trans2_bytes_push_str(param, cli_ucs2(cli), fname,
4122 strlen(fname)+1, NULL);
4124 if (tevent_req_nomem(param, req)) {
4125 return tevent_req_post(req, ev);
4128 /* Setup data word. */
4129 SSVAL(state->data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
4130 SMB_POSIX_UNLINK_FILE_TARGET);
4132 subreq = cli_trans_send(state, /* mem ctx. */
4133 ev, /* event ctx. */
4134 cli, /* cli_state. */
4135 SMBtrans2, /* cmd. */
4136 NULL, /* pipe name. */
4140 &state->setup, /* setup. */
4141 1, /* num setup uint16_t words. */
4142 0, /* max returned setup. */
4144 talloc_get_size(param), /* num param. */
4145 2, /* max returned param. */
4146 state->data, /* data. */
4148 0); /* max returned data. */
4150 if (tevent_req_nomem(subreq, req)) {
4151 return tevent_req_post(req, ev);
4153 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
4157 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
4158 struct event_context *ev,
4159 struct cli_state *cli,
4162 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, false);
4165 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req)
4169 if (tevent_req_is_nterror(req, &status)) {
4172 return NT_STATUS_OK;
4175 /****************************************************************************
4176 unlink - POSIX semantics.
4177 ****************************************************************************/
4179 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
4181 TALLOC_CTX *frame = talloc_stackframe();
4182 struct event_context *ev = NULL;
4183 struct tevent_req *req = NULL;
4184 NTSTATUS status = NT_STATUS_OK;
4186 if (cli_has_async_calls(cli)) {
4188 * Can't use sync call while an async call is in flight
4190 status = NT_STATUS_INVALID_PARAMETER;
4194 ev = event_context_init(frame);
4196 status = NT_STATUS_NO_MEMORY;
4200 req = cli_posix_unlink_send(frame,
4205 status = NT_STATUS_NO_MEMORY;
4209 if (!tevent_req_poll(req, ev)) {
4210 status = map_nt_error_from_unix(errno);
4214 status = cli_posix_unlink_recv(req);
4218 if (!NT_STATUS_IS_OK(status)) {
4219 cli_set_error(cli, status);
4224 /****************************************************************************
4225 rmdir - POSIX semantics.
4226 ****************************************************************************/
4228 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
4229 struct event_context *ev,
4230 struct cli_state *cli,
4233 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, true);
4236 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
4240 if (tevent_req_is_nterror(req, &status)) {
4243 return NT_STATUS_OK;
4246 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
4248 TALLOC_CTX *frame = talloc_stackframe();
4249 struct event_context *ev = NULL;
4250 struct tevent_req *req = NULL;
4251 NTSTATUS status = NT_STATUS_OK;
4253 if (cli_has_async_calls(cli)) {
4255 * Can't use sync call while an async call is in flight
4257 status = NT_STATUS_INVALID_PARAMETER;
4261 ev = event_context_init(frame);
4263 status = NT_STATUS_NO_MEMORY;
4267 req = cli_posix_rmdir_send(frame,
4272 status = NT_STATUS_NO_MEMORY;
4276 if (!tevent_req_poll(req, ev)) {
4277 status = map_nt_error_from_unix(errno);
4281 status = cli_posix_rmdir_recv(req, frame);
4285 if (!NT_STATUS_IS_OK(status)) {
4286 cli_set_error(cli, status);