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 Hard/Symlink a file (UNIX extensions).
25 Creates new name (sym)linked to oldname.
26 ****************************************************************************/
28 static bool cli_link_internal(struct cli_state *cli, const char *oldname, const char *newname, bool hard_link)
30 unsigned int data_len = 0;
31 unsigned int param_len = 0;
32 uint16_t setup = TRANSACT2_SETPATHINFO;
35 char *rparam=NULL, *rdata=NULL;
37 size_t oldlen = 2*(strlen(oldname)+1);
38 size_t newlen = 2*(strlen(newname)+1);
40 param = SMB_MALLOC_ARRAY(char, 6+newlen+2);
46 data = SMB_MALLOC_ARRAY(char, oldlen+2);
53 SSVAL(param,0,hard_link ? SMB_SET_FILE_UNIX_HLINK : SMB_SET_FILE_UNIX_LINK);
57 p += clistr_push(cli, p, newname, newlen, STR_TERMINATE);
58 param_len = PTR_DIFF(p, param);
61 p += clistr_push(cli, p, oldname, oldlen, STR_TERMINATE);
62 data_len = PTR_DIFF(p, data);
64 if (!cli_send_trans(cli, SMBtrans2,
66 -1, 0, /* fid, flags */
67 &setup, 1, 0, /* setup, length, max */
68 param, param_len, 2, /* param, length, max */
69 data, data_len, cli->max_xmit /* data, length, max */
79 if (!cli_receive_trans(cli, SMBtrans2,
93 /****************************************************************************
94 Map standard UNIX permissions onto wire representations.
95 ****************************************************************************/
97 uint32_t unix_perms_to_wire(mode_t perms)
101 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
102 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
103 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
104 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
105 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
106 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
107 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
108 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
109 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
111 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
114 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
117 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
122 /****************************************************************************
123 Map wire permissions to standard UNIX.
124 ****************************************************************************/
126 mode_t wire_perms_to_unix(uint32_t perms)
128 mode_t ret = (mode_t)0;
130 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
131 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
132 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
133 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
134 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
135 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
136 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
137 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
138 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
140 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
143 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
146 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
151 /****************************************************************************
152 Return the file type from the wire filetype for UNIX extensions.
153 ****************************************************************************/
155 static mode_t unix_filetype_from_wire(uint32_t wire_type)
163 case UNIX_TYPE_SYMLINK:
167 case UNIX_TYPE_CHARDEV:
171 case UNIX_TYPE_BLKDEV:
179 case UNIX_TYPE_SOCKET:
187 /****************************************************************************
188 Do a POSIX getfacl (UNIX extensions).
189 ****************************************************************************/
191 bool cli_unix_getfacl(struct cli_state *cli, const char *name, size_t *prb_size, char **retbuf)
193 unsigned int param_len = 0;
194 unsigned int data_len = 0;
195 uint16_t setup = TRANSACT2_QPATHINFO;
197 size_t nlen = 2*(strlen(name)+1);
198 char *rparam=NULL, *rdata=NULL;
201 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
208 SSVAL(p, 0, SMB_QUERY_POSIX_ACL);
210 p += clistr_push(cli, p, name, nlen, STR_TERMINATE);
211 param_len = PTR_DIFF(p, param);
213 if (!cli_send_trans(cli, SMBtrans2,
215 -1, 0, /* fid, flags */
216 &setup, 1, 0, /* setup, length, max */
217 param, param_len, 2, /* param, length, max */
218 NULL, 0, cli->max_xmit /* data, length, max */
226 if (!cli_receive_trans(cli, SMBtrans2,
228 &rdata, &data_len)) {
240 *prb_size = (size_t)data_len;
245 /****************************************************************************
246 Stat a file (UNIX extensions).
247 ****************************************************************************/
249 bool cli_unix_stat(struct cli_state *cli, const char *name, SMB_STRUCT_STAT *sbuf)
251 unsigned int param_len = 0;
252 unsigned int data_len = 0;
253 uint16_t setup = TRANSACT2_QPATHINFO;
255 size_t nlen = 2*(strlen(name)+1);
256 char *rparam=NULL, *rdata=NULL;
261 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
267 SSVAL(p, 0, SMB_QUERY_FILE_UNIX_BASIC);
269 p += clistr_push(cli, p, name, nlen, STR_TERMINATE);
270 param_len = PTR_DIFF(p, param);
272 if (!cli_send_trans(cli, SMBtrans2,
274 -1, 0, /* fid, flags */
275 &setup, 1, 0, /* setup, length, max */
276 param, param_len, 2, /* param, length, max */
277 NULL, 0, cli->max_xmit /* data, length, max */
285 if (!cli_receive_trans(cli, SMBtrans2,
287 &rdata, &data_len)) {
297 sbuf->st_size = IVAL2_TO_SMB_BIG_UINT(rdata,0); /* total size, in bytes */
298 sbuf->st_blocks = IVAL2_TO_SMB_BIG_UINT(rdata,8); /* number of blocks allocated */
299 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
300 sbuf->st_blocks /= STAT_ST_BLOCKSIZE;
302 /* assume 512 byte blocks */
303 sbuf->st_blocks /= 512;
305 set_ctimespec(sbuf, interpret_long_date(rdata + 16)); /* time of last change */
306 set_atimespec(sbuf, interpret_long_date(rdata + 24)); /* time of last access */
307 set_mtimespec(sbuf, interpret_long_date(rdata + 32)); /* time of last modification */
309 sbuf->st_uid = (uid_t) IVAL(rdata,40); /* user ID of owner */
310 sbuf->st_gid = (gid_t) IVAL(rdata,48); /* group ID of owner */
311 sbuf->st_mode |= unix_filetype_from_wire(IVAL(rdata, 56));
312 #if defined(HAVE_MAKEDEV)
314 uint32_t dev_major = IVAL(rdata,60);
315 uint32_t dev_minor = IVAL(rdata,68);
316 sbuf->st_rdev = makedev(dev_major, dev_minor);
319 sbuf->st_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(rdata,76); /* inode */
320 sbuf->st_mode |= wire_perms_to_unix(IVAL(rdata,84)); /* protection */
321 sbuf->st_nlink = IVAL(rdata,92); /* number of hard links */
329 /****************************************************************************
330 Symlink a file (UNIX extensions).
331 ****************************************************************************/
333 bool cli_unix_symlink(struct cli_state *cli, const char *oldname, const char *newname)
335 return cli_link_internal(cli, oldname, newname, False);
338 /****************************************************************************
339 Hard a file (UNIX extensions).
340 ****************************************************************************/
342 bool cli_unix_hardlink(struct cli_state *cli, const char *oldname, const char *newname)
344 return cli_link_internal(cli, oldname, newname, True);
347 /****************************************************************************
348 Chmod or chown a file internal (UNIX extensions).
349 ****************************************************************************/
351 static bool cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname, uint32_t mode, uint32_t uid, uint32_t gid)
353 unsigned int data_len = 0;
354 unsigned int param_len = 0;
355 uint16_t setup = TRANSACT2_SETPATHINFO;
356 size_t nlen = 2*(strlen(fname)+1);
359 char *rparam=NULL, *rdata=NULL;
362 param = SMB_MALLOC_ARRAY(char, 6+nlen+2);
366 memset(param, '\0', 6);
367 memset(data, 0, sizeof(data));
369 SSVAL(param,0,SMB_SET_FILE_UNIX_BASIC);
372 p += clistr_push(cli, p, fname, nlen, STR_TERMINATE);
373 param_len = PTR_DIFF(p, param);
375 memset(data, 0xff, 40); /* Set all sizes/times to no change. */
383 if (!cli_send_trans(cli, SMBtrans2,
385 -1, 0, /* fid, flags */
386 &setup, 1, 0, /* setup, length, max */
387 param, param_len, 2, /* param, length, max */
388 (char *)&data, data_len, cli->max_xmit /* data, length, max */
396 if (!cli_receive_trans(cli, SMBtrans2,
398 &rdata, &data_len)) {
408 /****************************************************************************
409 chmod a file (UNIX extensions).
410 ****************************************************************************/
412 bool cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
414 return cli_unix_chmod_chown_internal(cli, fname,
415 unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
418 /****************************************************************************
419 chown a file (UNIX extensions).
420 ****************************************************************************/
422 bool cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
424 return cli_unix_chmod_chown_internal(cli, fname,
425 SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
428 /****************************************************************************
430 ****************************************************************************/
432 static void cli_rename_done(struct tevent_req *subreq);
434 struct cli_rename_state {
438 struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
439 struct event_context *ev,
440 struct cli_state *cli,
441 const char *fname_src,
442 const char *fname_dst)
444 struct tevent_req *req = NULL, *subreq = NULL;
445 struct cli_rename_state *state = NULL;
446 uint8_t additional_flags = 0;
447 uint8_t *bytes = NULL;
449 req = tevent_req_create(mem_ctx, &state, struct cli_rename_state);
454 SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
456 bytes = talloc_array(state, uint8_t, 1);
457 if (tevent_req_nomem(bytes, req)) {
458 return tevent_req_post(req, ev);
461 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
462 strlen(fname_src)+1, NULL);
463 if (tevent_req_nomem(bytes, req)) {
464 return tevent_req_post(req, ev);
467 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
468 talloc_get_size(bytes)+1);
469 if (tevent_req_nomem(bytes, req)) {
470 return tevent_req_post(req, ev);
473 bytes[talloc_get_size(bytes)-1] = 4;
474 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
475 strlen(fname_dst)+1, NULL);
476 if (tevent_req_nomem(bytes, req)) {
477 return tevent_req_post(req, ev);
480 subreq = cli_smb_send(state, ev, cli, SMBmv, additional_flags,
481 1, state->vwv, talloc_get_size(bytes), bytes);
482 if (tevent_req_nomem(subreq, req)) {
483 return tevent_req_post(req, ev);
485 tevent_req_set_callback(subreq, cli_rename_done, req);
489 static void cli_rename_done(struct tevent_req *subreq)
491 struct tevent_req *req = tevent_req_callback_data(
492 subreq, struct tevent_req);
495 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
497 if (!NT_STATUS_IS_OK(status)) {
498 tevent_req_nterror(req, status);
501 tevent_req_done(req);
504 NTSTATUS cli_rename_recv(struct tevent_req *req)
506 return tevent_req_simple_recv_ntstatus(req);
509 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
511 TALLOC_CTX *frame = talloc_stackframe();
512 struct event_context *ev;
513 struct tevent_req *req;
514 NTSTATUS status = NT_STATUS_OK;
516 if (cli_has_async_calls(cli)) {
518 * Can't use sync call while an async call is in flight
520 status = NT_STATUS_INVALID_PARAMETER;
524 ev = event_context_init(frame);
526 status = NT_STATUS_NO_MEMORY;
530 req = cli_rename_send(frame, ev, cli, fname_src, fname_dst);
532 status = NT_STATUS_NO_MEMORY;
536 if (!tevent_req_poll(req, ev)) {
537 status = map_nt_error_from_unix(errno);
541 status = cli_rename_recv(req);
545 if (!NT_STATUS_IS_OK(status)) {
546 cli_set_error(cli, status);
551 /****************************************************************************
553 ****************************************************************************/
555 static void cli_ntrename_internal_done(struct tevent_req *subreq);
557 struct cli_ntrename_internal_state {
561 static struct tevent_req *cli_ntrename_internal_send(TALLOC_CTX *mem_ctx,
562 struct event_context *ev,
563 struct cli_state *cli,
564 const char *fname_src,
565 const char *fname_dst,
566 uint16_t rename_flag)
568 struct tevent_req *req = NULL, *subreq = NULL;
569 struct cli_ntrename_internal_state *state = NULL;
570 uint8_t additional_flags = 0;
571 uint8_t *bytes = NULL;
573 req = tevent_req_create(mem_ctx, &state,
574 struct cli_ntrename_internal_state);
579 SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
580 SSVAL(state->vwv+1, 0, rename_flag);
582 bytes = talloc_array(state, uint8_t, 1);
583 if (tevent_req_nomem(bytes, req)) {
584 return tevent_req_post(req, ev);
587 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
588 strlen(fname_src)+1, NULL);
589 if (tevent_req_nomem(bytes, req)) {
590 return tevent_req_post(req, ev);
593 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
594 talloc_get_size(bytes)+1);
595 if (tevent_req_nomem(bytes, req)) {
596 return tevent_req_post(req, ev);
599 bytes[talloc_get_size(bytes)-1] = 4;
600 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
601 strlen(fname_dst)+1, NULL);
602 if (tevent_req_nomem(bytes, req)) {
603 return tevent_req_post(req, ev);
606 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
607 4, state->vwv, talloc_get_size(bytes), bytes);
608 if (tevent_req_nomem(subreq, req)) {
609 return tevent_req_post(req, ev);
611 tevent_req_set_callback(subreq, cli_ntrename_internal_done, req);
615 static void cli_ntrename_internal_done(struct tevent_req *subreq)
617 struct tevent_req *req = tevent_req_callback_data(
618 subreq, struct tevent_req);
621 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
623 if (!NT_STATUS_IS_OK(status)) {
624 tevent_req_nterror(req, status);
627 tevent_req_done(req);
630 static NTSTATUS cli_ntrename_internal_recv(struct tevent_req *req)
632 return tevent_req_simple_recv_ntstatus(req);
635 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
636 struct event_context *ev,
637 struct cli_state *cli,
638 const char *fname_src,
639 const char *fname_dst)
641 return cli_ntrename_internal_send(mem_ctx,
649 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
651 return cli_ntrename_internal_recv(req);
654 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
656 TALLOC_CTX *frame = talloc_stackframe();
657 struct event_context *ev;
658 struct tevent_req *req;
659 NTSTATUS status = NT_STATUS_OK;
661 if (cli_has_async_calls(cli)) {
663 * Can't use sync call while an async call is in flight
665 status = NT_STATUS_INVALID_PARAMETER;
669 ev = event_context_init(frame);
671 status = NT_STATUS_NO_MEMORY;
675 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
677 status = NT_STATUS_NO_MEMORY;
681 if (!tevent_req_poll(req, ev)) {
682 status = map_nt_error_from_unix(errno);
686 status = cli_ntrename_recv(req);
690 if (!NT_STATUS_IS_OK(status)) {
691 cli_set_error(cli, status);
696 /****************************************************************************
698 ****************************************************************************/
700 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
701 struct event_context *ev,
702 struct cli_state *cli,
703 const char *fname_src,
704 const char *fname_dst)
706 return cli_ntrename_internal_send(mem_ctx,
711 RENAME_FLAG_HARD_LINK);
714 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
716 return cli_ntrename_internal_recv(req);
719 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
721 TALLOC_CTX *frame = talloc_stackframe();
722 struct event_context *ev;
723 struct tevent_req *req;
724 NTSTATUS status = NT_STATUS_OK;
726 if (cli_has_async_calls(cli)) {
728 * Can't use sync call while an async call is in flight
730 status = NT_STATUS_INVALID_PARAMETER;
734 ev = event_context_init(frame);
736 status = NT_STATUS_NO_MEMORY;
740 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
742 status = NT_STATUS_NO_MEMORY;
746 if (!tevent_req_poll(req, ev)) {
747 status = map_nt_error_from_unix(errno);
751 status = cli_nt_hardlink_recv(req);
755 if (!NT_STATUS_IS_OK(status)) {
756 cli_set_error(cli, status);
761 /****************************************************************************
763 ****************************************************************************/
765 static void cli_unlink_done(struct tevent_req *subreq);
767 struct cli_unlink_state {
771 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
772 struct event_context *ev,
773 struct cli_state *cli,
775 uint16_t mayhave_attrs)
777 struct tevent_req *req = NULL, *subreq = NULL;
778 struct cli_unlink_state *state = NULL;
779 uint8_t additional_flags = 0;
780 uint8_t *bytes = NULL;
782 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
787 SSVAL(state->vwv+0, 0, mayhave_attrs);
789 bytes = talloc_array(state, uint8_t, 1);
790 if (tevent_req_nomem(bytes, req)) {
791 return tevent_req_post(req, ev);
794 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
795 strlen(fname)+1, NULL);
797 if (tevent_req_nomem(bytes, req)) {
798 return tevent_req_post(req, ev);
801 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
802 1, state->vwv, talloc_get_size(bytes), bytes);
803 if (tevent_req_nomem(subreq, req)) {
804 return tevent_req_post(req, ev);
806 tevent_req_set_callback(subreq, cli_unlink_done, req);
810 static void cli_unlink_done(struct tevent_req *subreq)
812 struct tevent_req *req = tevent_req_callback_data(
813 subreq, struct tevent_req);
816 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
818 if (!NT_STATUS_IS_OK(status)) {
819 tevent_req_nterror(req, status);
822 tevent_req_done(req);
825 NTSTATUS cli_unlink_recv(struct tevent_req *req)
827 return tevent_req_simple_recv_ntstatus(req);
830 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
832 TALLOC_CTX *frame = talloc_stackframe();
833 struct event_context *ev;
834 struct tevent_req *req;
835 NTSTATUS status = NT_STATUS_OK;
837 if (cli_has_async_calls(cli)) {
839 * Can't use sync call while an async call is in flight
841 status = NT_STATUS_INVALID_PARAMETER;
845 ev = event_context_init(frame);
847 status = NT_STATUS_NO_MEMORY;
851 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
853 status = NT_STATUS_NO_MEMORY;
857 if (!tevent_req_poll(req, ev)) {
858 status = map_nt_error_from_unix(errno);
862 status = cli_unlink_recv(req);
866 if (!NT_STATUS_IS_OK(status)) {
867 cli_set_error(cli, status);
872 /****************************************************************************
874 ****************************************************************************/
876 static void cli_mkdir_done(struct tevent_req *subreq);
878 struct cli_mkdir_state {
882 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
883 struct event_context *ev,
884 struct cli_state *cli,
887 struct tevent_req *req = NULL, *subreq = NULL;
888 struct cli_mkdir_state *state = NULL;
889 uint8_t additional_flags = 0;
890 uint8_t *bytes = NULL;
892 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
897 bytes = talloc_array(state, uint8_t, 1);
898 if (tevent_req_nomem(bytes, req)) {
899 return tevent_req_post(req, ev);
902 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
903 strlen(dname)+1, NULL);
905 if (tevent_req_nomem(bytes, req)) {
906 return tevent_req_post(req, ev);
909 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
910 0, NULL, talloc_get_size(bytes), bytes);
911 if (tevent_req_nomem(subreq, req)) {
912 return tevent_req_post(req, ev);
914 tevent_req_set_callback(subreq, cli_mkdir_done, req);
918 static void cli_mkdir_done(struct tevent_req *subreq)
920 struct tevent_req *req = tevent_req_callback_data(
921 subreq, struct tevent_req);
924 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
926 if (!NT_STATUS_IS_OK(status)) {
927 tevent_req_nterror(req, status);
930 tevent_req_done(req);
933 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
935 return tevent_req_simple_recv_ntstatus(req);
938 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
940 TALLOC_CTX *frame = talloc_stackframe();
941 struct event_context *ev;
942 struct tevent_req *req;
943 NTSTATUS status = NT_STATUS_OK;
945 if (cli_has_async_calls(cli)) {
947 * Can't use sync call while an async call is in flight
949 status = NT_STATUS_INVALID_PARAMETER;
953 ev = event_context_init(frame);
955 status = NT_STATUS_NO_MEMORY;
959 req = cli_mkdir_send(frame, ev, cli, dname);
961 status = NT_STATUS_NO_MEMORY;
965 if (!tevent_req_poll(req, ev)) {
966 status = map_nt_error_from_unix(errno);
970 status = cli_mkdir_recv(req);
974 if (!NT_STATUS_IS_OK(status)) {
975 cli_set_error(cli, status);
980 /****************************************************************************
982 ****************************************************************************/
984 static void cli_rmdir_done(struct tevent_req *subreq);
986 struct cli_rmdir_state {
990 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
991 struct event_context *ev,
992 struct cli_state *cli,
995 struct tevent_req *req = NULL, *subreq = NULL;
996 struct cli_rmdir_state *state = NULL;
997 uint8_t additional_flags = 0;
998 uint8_t *bytes = NULL;
1000 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
1005 bytes = talloc_array(state, uint8_t, 1);
1006 if (tevent_req_nomem(bytes, req)) {
1007 return tevent_req_post(req, ev);
1010 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1011 strlen(dname)+1, NULL);
1013 if (tevent_req_nomem(bytes, req)) {
1014 return tevent_req_post(req, ev);
1017 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1018 0, NULL, talloc_get_size(bytes), bytes);
1019 if (tevent_req_nomem(subreq, req)) {
1020 return tevent_req_post(req, ev);
1022 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1026 static void cli_rmdir_done(struct tevent_req *subreq)
1028 struct tevent_req *req = tevent_req_callback_data(
1029 subreq, struct tevent_req);
1032 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1033 TALLOC_FREE(subreq);
1034 if (!NT_STATUS_IS_OK(status)) {
1035 tevent_req_nterror(req, status);
1038 tevent_req_done(req);
1041 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1043 return tevent_req_simple_recv_ntstatus(req);
1046 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1048 TALLOC_CTX *frame = talloc_stackframe();
1049 struct event_context *ev;
1050 struct tevent_req *req;
1051 NTSTATUS status = NT_STATUS_OK;
1053 if (cli_has_async_calls(cli)) {
1055 * Can't use sync call while an async call is in flight
1057 status = NT_STATUS_INVALID_PARAMETER;
1061 ev = event_context_init(frame);
1063 status = NT_STATUS_NO_MEMORY;
1067 req = cli_rmdir_send(frame, ev, cli, dname);
1069 status = NT_STATUS_NO_MEMORY;
1073 if (!tevent_req_poll(req, ev)) {
1074 status = map_nt_error_from_unix(errno);
1078 status = cli_rmdir_recv(req);
1082 if (!NT_STATUS_IS_OK(status)) {
1083 cli_set_error(cli, status);
1088 /****************************************************************************
1089 Set or clear the delete on close flag.
1090 ****************************************************************************/
1092 int cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1094 unsigned int data_len = 1;
1095 unsigned int param_len = 6;
1096 uint16_t setup = TRANSACT2_SETFILEINFO;
1099 char *rparam=NULL, *rdata=NULL;
1101 memset(param, 0, param_len);
1102 SSVAL(param,0,fnum);
1103 SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
1105 data = flag ? 1 : 0;
1107 if (!cli_send_trans(cli, SMBtrans2,
1109 -1, 0, /* fid, flags */
1110 &setup, 1, 0, /* setup, length, max */
1111 param, param_len, 2, /* param, length, max */
1112 (char *)&data, data_len, cli->max_xmit /* data, length, max */
1117 if (!cli_receive_trans(cli, SMBtrans2,
1118 &rparam, ¶m_len,
1119 &rdata, &data_len)) {
1129 struct cli_ntcreate_state {
1134 static void cli_ntcreate_done(struct tevent_req *subreq);
1136 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1137 struct event_context *ev,
1138 struct cli_state *cli,
1140 uint32_t CreatFlags,
1141 uint32_t DesiredAccess,
1142 uint32_t FileAttributes,
1143 uint32_t ShareAccess,
1144 uint32_t CreateDisposition,
1145 uint32_t CreateOptions,
1146 uint8_t SecurityFlags)
1148 struct tevent_req *req, *subreq;
1149 struct cli_ntcreate_state *state;
1152 size_t converted_len;
1154 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1160 SCVAL(vwv+0, 0, 0xFF);
1165 if (cli->use_oplocks) {
1166 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1168 SIVAL(vwv+3, 1, CreatFlags);
1169 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1170 SIVAL(vwv+7, 1, DesiredAccess);
1171 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1172 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1173 SIVAL(vwv+13, 1, FileAttributes);
1174 SIVAL(vwv+15, 1, ShareAccess);
1175 SIVAL(vwv+17, 1, CreateDisposition);
1176 SIVAL(vwv+19, 1, CreateOptions);
1177 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1178 SCVAL(vwv+23, 1, SecurityFlags);
1180 bytes = talloc_array(state, uint8_t, 0);
1181 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1182 fname, strlen(fname)+1,
1185 /* sigh. this copes with broken netapp filer behaviour */
1186 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1188 if (tevent_req_nomem(bytes, req)) {
1189 return tevent_req_post(req, ev);
1192 SIVAL(vwv+2, 1, converted_len);
1194 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1195 talloc_get_size(bytes), bytes);
1196 if (tevent_req_nomem(subreq, req)) {
1197 return tevent_req_post(req, ev);
1199 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1203 static void cli_ntcreate_done(struct tevent_req *subreq)
1205 struct tevent_req *req = tevent_req_callback_data(
1206 subreq, struct tevent_req);
1207 struct cli_ntcreate_state *state = tevent_req_data(
1208 req, struct cli_ntcreate_state);
1215 status = cli_smb_recv(subreq, 3, &wct, &vwv, &num_bytes, &bytes);
1216 if (!NT_STATUS_IS_OK(status)) {
1217 TALLOC_FREE(subreq);
1218 tevent_req_nterror(req, status);
1221 state->fnum = SVAL(vwv+2, 1);
1222 tevent_req_done(req);
1225 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1227 struct cli_ntcreate_state *state = tevent_req_data(
1228 req, struct cli_ntcreate_state);
1231 if (tevent_req_is_nterror(req, &status)) {
1234 *pfnum = state->fnum;
1235 return NT_STATUS_OK;
1238 NTSTATUS cli_ntcreate(struct cli_state *cli,
1240 uint32_t CreatFlags,
1241 uint32_t DesiredAccess,
1242 uint32_t FileAttributes,
1243 uint32_t ShareAccess,
1244 uint32_t CreateDisposition,
1245 uint32_t CreateOptions,
1246 uint8_t SecurityFlags,
1249 TALLOC_CTX *frame = talloc_stackframe();
1250 struct event_context *ev;
1251 struct tevent_req *req;
1252 NTSTATUS status = NT_STATUS_OK;
1254 if (cli_has_async_calls(cli)) {
1256 * Can't use sync call while an async call is in flight
1258 status = NT_STATUS_INVALID_PARAMETER;
1262 ev = event_context_init(frame);
1264 status = NT_STATUS_NO_MEMORY;
1268 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1269 DesiredAccess, FileAttributes, ShareAccess,
1270 CreateDisposition, CreateOptions,
1273 status = NT_STATUS_NO_MEMORY;
1277 if (!tevent_req_poll(req, ev)) {
1278 status = map_nt_error_from_unix(errno);
1282 status = cli_ntcreate_recv(req, pfid);
1285 if (!NT_STATUS_IS_OK(status)) {
1286 cli_set_error(cli, status);
1291 /***********************************************************
1292 Common function for pushing stings, used by smb_bytes_push_str()
1293 and trans_bytes_push_str(). Only difference is the align_odd
1295 ***********************************************************/
1297 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
1298 const char *str, size_t str_len,
1300 size_t *pconverted_size)
1304 size_t converted_size;
1310 buflen = talloc_get_size(buf);
1312 if (align_odd && ucs2 && (buflen % 2 == 0)) {
1314 * We're pushing into an SMB buffer, align odd
1316 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
1324 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
1325 ucs2 ? CH_UTF16LE : CH_DOS,
1326 str, str_len, &converted,
1327 &converted_size, true)) {
1331 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
1332 buflen + converted_size);
1334 TALLOC_FREE(converted);
1338 memcpy(buf + buflen, converted, converted_size);
1340 TALLOC_FREE(converted);
1342 if (pconverted_size) {
1343 *pconverted_size = converted_size;
1349 /***********************************************************
1350 Push a string into an SMB buffer, with odd byte alignment
1351 if it's a UCS2 string.
1352 ***********************************************************/
1354 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
1355 const char *str, size_t str_len,
1356 size_t *pconverted_size)
1358 return internal_bytes_push_str(buf, ucs2, str, str_len,
1359 true, pconverted_size);
1362 /***********************************************************
1363 Same as smb_bytes_push_str(), but without the odd byte
1364 align for ucs2 (we're pushing into a param or data block).
1365 static for now, although this will probably change when
1366 other modules use async trans calls.
1367 ***********************************************************/
1369 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
1370 const char *str, size_t str_len,
1371 size_t *pconverted_size)
1373 return internal_bytes_push_str(buf, ucs2, str, str_len,
1374 false, pconverted_size);
1377 /****************************************************************************
1379 WARNING: if you open with O_WRONLY then getattrE won't work!
1380 ****************************************************************************/
1382 struct cli_open_state {
1388 static void cli_open_done(struct tevent_req *subreq);
1390 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
1391 struct event_context *ev,
1392 struct cli_state *cli, const char *fname,
1393 int flags, int share_mode,
1394 struct tevent_req **psmbreq)
1396 struct tevent_req *req, *subreq;
1397 struct cli_open_state *state;
1399 unsigned accessmode;
1400 uint8_t additional_flags;
1403 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
1409 if (flags & O_CREAT) {
1412 if (!(flags & O_EXCL)) {
1413 if (flags & O_TRUNC)
1419 accessmode = (share_mode<<4);
1421 if ((flags & O_ACCMODE) == O_RDWR) {
1423 } else if ((flags & O_ACCMODE) == O_WRONLY) {
1428 if ((flags & O_SYNC) == O_SYNC) {
1429 accessmode |= (1<<14);
1433 if (share_mode == DENY_FCB) {
1437 SCVAL(state->vwv + 0, 0, 0xFF);
1438 SCVAL(state->vwv + 0, 1, 0);
1439 SSVAL(state->vwv + 1, 0, 0);
1440 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
1441 SSVAL(state->vwv + 3, 0, accessmode);
1442 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
1443 SSVAL(state->vwv + 5, 0, 0);
1444 SIVAL(state->vwv + 6, 0, 0);
1445 SSVAL(state->vwv + 8, 0, openfn);
1446 SIVAL(state->vwv + 9, 0, 0);
1447 SIVAL(state->vwv + 11, 0, 0);
1448 SIVAL(state->vwv + 13, 0, 0);
1450 additional_flags = 0;
1452 if (cli->use_oplocks) {
1453 /* if using oplocks then ask for a batch oplock via
1454 core and extended methods */
1456 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
1457 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
1460 bytes = talloc_array(state, uint8_t, 0);
1461 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1462 strlen(fname)+1, NULL);
1464 if (tevent_req_nomem(bytes, req)) {
1465 return tevent_req_post(req, ev);
1468 state->bytes.iov_base = bytes;
1469 state->bytes.iov_len = talloc_get_size(bytes);
1471 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
1472 15, state->vwv, 1, &state->bytes);
1473 if (subreq == NULL) {
1477 tevent_req_set_callback(subreq, cli_open_done, req);
1482 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1483 struct cli_state *cli, const char *fname,
1484 int flags, int share_mode)
1486 struct tevent_req *req, *subreq;
1488 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
1490 if ((req == NULL) || !cli_smb_req_send(subreq)) {
1497 static void cli_open_done(struct tevent_req *subreq)
1499 struct tevent_req *req = tevent_req_callback_data(
1500 subreq, struct tevent_req);
1501 struct cli_open_state *state = tevent_req_data(
1502 req, struct cli_open_state);
1507 status = cli_smb_recv(subreq, 3, &wct, &vwv, NULL, NULL);
1508 if (!NT_STATUS_IS_OK(status)) {
1509 TALLOC_FREE(subreq);
1510 tevent_req_nterror(req, status);
1513 state->fnum = SVAL(vwv+2, 0);
1514 tevent_req_done(req);
1517 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
1519 struct cli_open_state *state = tevent_req_data(
1520 req, struct cli_open_state);
1523 if (tevent_req_is_nterror(req, &status)) {
1526 *pfnum = state->fnum;
1527 return NT_STATUS_OK;
1530 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
1531 int share_mode, uint16_t *pfnum)
1533 TALLOC_CTX *frame = talloc_stackframe();
1534 struct event_context *ev;
1535 struct tevent_req *req;
1536 NTSTATUS status = NT_STATUS_OK;
1538 if (cli_has_async_calls(cli)) {
1540 * Can't use sync call while an async call is in flight
1542 status = NT_STATUS_INVALID_PARAMETER;
1546 ev = event_context_init(frame);
1548 status = NT_STATUS_NO_MEMORY;
1552 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
1554 status = NT_STATUS_NO_MEMORY;
1558 if (!tevent_req_poll(req, ev)) {
1559 status = map_nt_error_from_unix(errno);
1563 status = cli_open_recv(req, pfnum);
1566 if (!NT_STATUS_IS_OK(status)) {
1567 cli_set_error(cli, status);
1572 /****************************************************************************
1574 ****************************************************************************/
1576 struct cli_close_state {
1580 static void cli_close_done(struct tevent_req *subreq);
1582 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
1583 struct event_context *ev,
1584 struct cli_state *cli,
1586 struct tevent_req **psubreq)
1588 struct tevent_req *req, *subreq;
1589 struct cli_close_state *state;
1591 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
1595 SSVAL(state->vwv+0, 0, fnum);
1596 SIVALS(state->vwv+1, 0, -1);
1598 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
1600 if (subreq == NULL) {
1604 tevent_req_set_callback(subreq, cli_close_done, req);
1609 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
1610 struct event_context *ev,
1611 struct cli_state *cli,
1614 struct tevent_req *req, *subreq;
1616 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
1617 if ((req == NULL) || !cli_smb_req_send(subreq)) {
1624 static void cli_close_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_close_recv(struct tevent_req *req)
1641 return tevent_req_simple_recv_ntstatus(req);
1644 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
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_close_send(frame, ev, cli, fnum);
1667 status = NT_STATUS_NO_MEMORY;
1671 if (!tevent_req_poll(req, ev)) {
1672 status = map_nt_error_from_unix(errno);
1676 status = cli_close_recv(req);
1679 if (!NT_STATUS_IS_OK(status)) {
1680 cli_set_error(cli, status);
1685 /****************************************************************************
1686 Truncate a file to a specified size
1687 ****************************************************************************/
1689 bool cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
1691 unsigned int param_len = 6;
1692 unsigned int data_len = 8;
1693 uint16_t setup = TRANSACT2_SETFILEINFO;
1695 unsigned char data[8];
1696 char *rparam=NULL, *rdata=NULL;
1697 int saved_timeout = cli->timeout;
1699 SSVAL(param,0,fnum);
1700 SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
1703 SBVAL(data, 0, size);
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,/* data, length, ... */
1711 cli->max_xmit)) { /* ... max */
1712 cli->timeout = saved_timeout;
1716 if (!cli_receive_trans(cli, SMBtrans2,
1717 &rparam, ¶m_len,
1718 &rdata, &data_len)) {
1719 cli->timeout = saved_timeout;
1725 cli->timeout = saved_timeout;
1734 /****************************************************************************
1735 send a lock with a specified locktype
1736 this is used for testing LOCKING_ANDX_CANCEL_LOCK
1737 ****************************************************************************/
1739 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
1740 uint32_t offset, uint32_t len,
1741 int timeout, unsigned char locktype)
1744 int saved_timeout = cli->timeout;
1746 memset(cli->outbuf,'\0',smb_size);
1747 memset(cli->inbuf,'\0', smb_size);
1749 cli_set_message(cli->outbuf,8,0,True);
1751 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1752 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1753 cli_setup_packet(cli);
1755 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1756 SSVAL(cli->outbuf,smb_vwv2,fnum);
1757 SCVAL(cli->outbuf,smb_vwv3,locktype);
1758 SIVALS(cli->outbuf, smb_vwv4, timeout);
1759 SSVAL(cli->outbuf,smb_vwv6,0);
1760 SSVAL(cli->outbuf,smb_vwv7,1);
1762 p = smb_buf(cli->outbuf);
1763 SSVAL(p, 0, cli->pid);
1764 SIVAL(p, 2, offset);
1769 cli_setup_bcc(cli, p);
1774 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
1777 if (!cli_receive_smb(cli)) {
1778 cli->timeout = saved_timeout;
1779 return NT_STATUS_UNSUCCESSFUL;
1782 cli->timeout = saved_timeout;
1784 return cli_nt_error(cli);
1787 /****************************************************************************
1789 note that timeout is in units of 2 milliseconds
1790 ****************************************************************************/
1792 bool cli_lock(struct cli_state *cli, uint16_t fnum,
1793 uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
1796 int saved_timeout = cli->timeout;
1798 memset(cli->outbuf,'\0',smb_size);
1799 memset(cli->inbuf,'\0', smb_size);
1801 cli_set_message(cli->outbuf,8,0,True);
1803 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1804 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1805 cli_setup_packet(cli);
1807 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1808 SSVAL(cli->outbuf,smb_vwv2,fnum);
1809 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
1810 SIVALS(cli->outbuf, smb_vwv4, timeout);
1811 SSVAL(cli->outbuf,smb_vwv6,0);
1812 SSVAL(cli->outbuf,smb_vwv7,1);
1814 p = smb_buf(cli->outbuf);
1815 SSVAL(p, 0, cli->pid);
1816 SIVAL(p, 2, offset);
1821 cli_setup_bcc(cli, p);
1826 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
1829 if (!cli_receive_smb(cli)) {
1830 cli->timeout = saved_timeout;
1834 cli->timeout = saved_timeout;
1836 if (cli_is_error(cli)) {
1843 /****************************************************************************
1845 ****************************************************************************/
1847 bool cli_unlock(struct cli_state *cli, uint16_t fnum, uint32_t offset, uint32_t len)
1851 memset(cli->outbuf,'\0',smb_size);
1852 memset(cli->inbuf,'\0',smb_size);
1854 cli_set_message(cli->outbuf,8,0,True);
1856 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1857 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1858 cli_setup_packet(cli);
1860 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1861 SSVAL(cli->outbuf,smb_vwv2,fnum);
1862 SCVAL(cli->outbuf,smb_vwv3,0);
1863 SIVALS(cli->outbuf, smb_vwv4, 0);
1864 SSVAL(cli->outbuf,smb_vwv6,1);
1865 SSVAL(cli->outbuf,smb_vwv7,0);
1867 p = smb_buf(cli->outbuf);
1868 SSVAL(p, 0, cli->pid);
1869 SIVAL(p, 2, offset);
1872 cli_setup_bcc(cli, p);
1874 if (!cli_receive_smb(cli)) {
1878 if (cli_is_error(cli)) {
1885 /****************************************************************************
1886 Lock a file with 64 bit offsets.
1887 ****************************************************************************/
1889 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
1890 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
1893 int saved_timeout = cli->timeout;
1896 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1897 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1900 ltype = (lock_type == READ_LOCK? 1 : 0);
1901 ltype |= LOCKING_ANDX_LARGE_FILES;
1903 memset(cli->outbuf,'\0',smb_size);
1904 memset(cli->inbuf,'\0', smb_size);
1906 cli_set_message(cli->outbuf,8,0,True);
1908 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1909 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1910 cli_setup_packet(cli);
1912 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1913 SSVAL(cli->outbuf,smb_vwv2,fnum);
1914 SCVAL(cli->outbuf,smb_vwv3,ltype);
1915 SIVALS(cli->outbuf, smb_vwv4, timeout);
1916 SSVAL(cli->outbuf,smb_vwv6,0);
1917 SSVAL(cli->outbuf,smb_vwv7,1);
1919 p = smb_buf(cli->outbuf);
1920 SIVAL(p, 0, cli->pid);
1921 SOFF_T_R(p, 4, offset);
1922 SOFF_T_R(p, 12, len);
1925 cli_setup_bcc(cli, p);
1929 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1932 if (!cli_receive_smb(cli)) {
1933 cli->timeout = saved_timeout;
1937 cli->timeout = saved_timeout;
1939 if (cli_is_error(cli)) {
1946 /****************************************************************************
1947 Unlock a file with 64 bit offsets.
1948 ****************************************************************************/
1950 bool cli_unlock64(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
1954 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1955 return cli_unlock(cli, fnum, offset, len);
1958 memset(cli->outbuf,'\0',smb_size);
1959 memset(cli->inbuf,'\0',smb_size);
1961 cli_set_message(cli->outbuf,8,0,True);
1963 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1964 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1965 cli_setup_packet(cli);
1967 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1968 SSVAL(cli->outbuf,smb_vwv2,fnum);
1969 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1970 SIVALS(cli->outbuf, smb_vwv4, 0);
1971 SSVAL(cli->outbuf,smb_vwv6,1);
1972 SSVAL(cli->outbuf,smb_vwv7,0);
1974 p = smb_buf(cli->outbuf);
1975 SIVAL(p, 0, cli->pid);
1976 SOFF_T_R(p, 4, offset);
1977 SOFF_T_R(p, 12, len);
1979 cli_setup_bcc(cli, p);
1981 if (!cli_receive_smb(cli)) {
1985 if (cli_is_error(cli)) {
1992 /****************************************************************************
1993 Get/unlock a POSIX lock on a file - internal function.
1994 ****************************************************************************/
1996 static bool cli_posix_lock_internal(struct cli_state *cli, uint16_t fnum,
1997 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
1999 unsigned int param_len = 4;
2000 unsigned int data_len = POSIX_LOCK_DATA_SIZE;
2001 uint16_t setup = TRANSACT2_SETFILEINFO;
2003 unsigned char data[POSIX_LOCK_DATA_SIZE];
2004 char *rparam=NULL, *rdata=NULL;
2005 int saved_timeout = cli->timeout;
2007 SSVAL(param,0,fnum);
2008 SSVAL(param,2,SMB_SET_POSIX_LOCK);
2010 switch (lock_type) {
2012 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
2015 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
2018 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
2025 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
2026 cli->timeout = 0x7FFFFFFF;
2028 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
2031 SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
2032 SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
2033 SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
2035 if (!cli_send_trans(cli, SMBtrans2,
2037 -1, 0, /* fid, flags */
2038 &setup, 1, 0, /* setup, length, max */
2039 param, param_len, 2, /* param, length, max */
2040 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2042 cli->timeout = saved_timeout;
2046 if (!cli_receive_trans(cli, SMBtrans2,
2047 &rparam, ¶m_len,
2048 &rdata, &data_len)) {
2049 cli->timeout = saved_timeout;
2055 cli->timeout = saved_timeout;
2063 /****************************************************************************
2065 ****************************************************************************/
2067 bool cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2068 uint64_t offset, uint64_t len,
2069 bool wait_lock, enum brl_type lock_type)
2071 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2074 return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
2077 /****************************************************************************
2078 POSIX Unlock a file.
2079 ****************************************************************************/
2081 bool cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
2083 return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
2086 /****************************************************************************
2087 POSIX Get any lock covering a file.
2088 ****************************************************************************/
2090 bool cli_posix_getlock(struct cli_state *cli, uint16_t fnum, uint64_t *poffset, uint64_t *plen)
2095 /****************************************************************************
2096 Do a SMBgetattrE call.
2097 ****************************************************************************/
2099 static void cli_getattrE_done(struct tevent_req *subreq);
2101 struct cli_getattrE_state {
2111 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
2112 struct event_context *ev,
2113 struct cli_state *cli,
2116 struct tevent_req *req = NULL, *subreq = NULL;
2117 struct cli_getattrE_state *state = NULL;
2118 uint8_t additional_flags = 0;
2120 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
2125 state->zone_offset = cli->serverzone;
2126 SSVAL(state->vwv+0,0,fnum);
2128 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
2129 1, state->vwv, 0, NULL);
2130 if (tevent_req_nomem(subreq, req)) {
2131 return tevent_req_post(req, ev);
2133 tevent_req_set_callback(subreq, cli_getattrE_done, req);
2137 static void cli_getattrE_done(struct tevent_req *subreq)
2139 struct tevent_req *req = tevent_req_callback_data(
2140 subreq, struct tevent_req);
2141 struct cli_getattrE_state *state = tevent_req_data(
2142 req, struct cli_getattrE_state);
2144 uint16_t *vwv = NULL;
2147 status = cli_smb_recv(subreq, 11, &wct, &vwv, NULL, NULL);
2148 if (!NT_STATUS_IS_OK(status)) {
2149 tevent_req_nterror(req, status);
2153 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
2154 state->attr = SVAL(vwv+10,0);
2155 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
2156 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
2157 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
2159 TALLOC_FREE(subreq);
2160 tevent_req_done(req);
2163 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
2166 time_t *change_time,
2167 time_t *access_time,
2170 struct cli_getattrE_state *state = tevent_req_data(
2171 req, struct cli_getattrE_state);
2174 if (tevent_req_is_nterror(req, &status)) {
2178 *attr = state->attr;
2181 *size = state->size;
2184 *change_time = state->change_time;
2187 *access_time = state->access_time;
2190 *write_time = state->write_time;
2192 return NT_STATUS_OK;
2195 NTSTATUS cli_getattrE(struct cli_state *cli,
2199 time_t *change_time,
2200 time_t *access_time,
2203 TALLOC_CTX *frame = talloc_stackframe();
2204 struct event_context *ev = NULL;
2205 struct tevent_req *req = NULL;
2206 NTSTATUS status = NT_STATUS_OK;
2208 if (cli_has_async_calls(cli)) {
2210 * Can't use sync call while an async call is in flight
2212 status = NT_STATUS_INVALID_PARAMETER;
2216 ev = event_context_init(frame);
2218 status = NT_STATUS_NO_MEMORY;
2222 req = cli_getattrE_send(frame, ev, cli, fnum);
2224 status = NT_STATUS_NO_MEMORY;
2228 if (!tevent_req_poll(req, ev)) {
2229 status = map_nt_error_from_unix(errno);
2233 status = cli_getattrE_recv(req,
2242 if (!NT_STATUS_IS_OK(status)) {
2243 cli_set_error(cli, status);
2248 /****************************************************************************
2250 ****************************************************************************/
2252 static void cli_getatr_done(struct tevent_req *subreq);
2254 struct cli_getatr_state {
2261 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
2262 struct event_context *ev,
2263 struct cli_state *cli,
2266 struct tevent_req *req = NULL, *subreq = NULL;
2267 struct cli_getatr_state *state = NULL;
2268 uint8_t additional_flags = 0;
2269 uint8_t *bytes = NULL;
2271 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
2276 state->zone_offset = cli->serverzone;
2278 bytes = talloc_array(state, uint8_t, 1);
2279 if (tevent_req_nomem(bytes, req)) {
2280 return tevent_req_post(req, ev);
2283 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2284 strlen(fname)+1, NULL);
2286 if (tevent_req_nomem(bytes, req)) {
2287 return tevent_req_post(req, ev);
2290 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
2291 0, NULL, talloc_get_size(bytes), bytes);
2292 if (tevent_req_nomem(subreq, req)) {
2293 return tevent_req_post(req, ev);
2295 tevent_req_set_callback(subreq, cli_getatr_done, req);
2299 static void cli_getatr_done(struct tevent_req *subreq)
2301 struct tevent_req *req = tevent_req_callback_data(
2302 subreq, struct tevent_req);
2303 struct cli_getatr_state *state = tevent_req_data(
2304 req, struct cli_getatr_state);
2306 uint16_t *vwv = NULL;
2309 status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
2310 if (!NT_STATUS_IS_OK(status)) {
2311 tevent_req_nterror(req, status);
2315 state->attr = SVAL(vwv+0,0);
2316 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
2317 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
2319 TALLOC_FREE(subreq);
2320 tevent_req_done(req);
2323 NTSTATUS cli_getatr_recv(struct tevent_req *req,
2328 struct cli_getatr_state *state = tevent_req_data(
2329 req, struct cli_getatr_state);
2332 if (tevent_req_is_nterror(req, &status)) {
2336 *attr = state->attr;
2339 *size = state->size;
2342 *write_time = state->write_time;
2344 return NT_STATUS_OK;
2347 NTSTATUS cli_getatr(struct cli_state *cli,
2353 TALLOC_CTX *frame = talloc_stackframe();
2354 struct event_context *ev = NULL;
2355 struct tevent_req *req = NULL;
2356 NTSTATUS status = NT_STATUS_OK;
2358 if (cli_has_async_calls(cli)) {
2360 * Can't use sync call while an async call is in flight
2362 status = NT_STATUS_INVALID_PARAMETER;
2366 ev = event_context_init(frame);
2368 status = NT_STATUS_NO_MEMORY;
2372 req = cli_getatr_send(frame, ev, cli, fname);
2374 status = NT_STATUS_NO_MEMORY;
2378 if (!tevent_req_poll(req, ev)) {
2379 status = map_nt_error_from_unix(errno);
2383 status = cli_getatr_recv(req,
2390 if (!NT_STATUS_IS_OK(status)) {
2391 cli_set_error(cli, status);
2396 /****************************************************************************
2397 Do a SMBsetattrE call.
2398 ****************************************************************************/
2400 static void cli_setattrE_done(struct tevent_req *subreq);
2402 struct cli_setattrE_state {
2406 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
2407 struct event_context *ev,
2408 struct cli_state *cli,
2414 struct tevent_req *req = NULL, *subreq = NULL;
2415 struct cli_setattrE_state *state = NULL;
2416 uint8_t additional_flags = 0;
2419 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
2424 memset(vwv, '\0', sizeof(vwv));
2425 SSVAL(vwv+0, 0, fnum);
2426 cli_put_dos_date2(cli, (char *)&vwv[1], 0, change_time);
2427 cli_put_dos_date2(cli, (char *)&vwv[3], 0, access_time);
2428 cli_put_dos_date2(cli, (char *)&vwv[5], 0, write_time);
2430 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
2432 if (tevent_req_nomem(subreq, req)) {
2433 return tevent_req_post(req, ev);
2435 tevent_req_set_callback(subreq, cli_setattrE_done, req);
2439 static void cli_setattrE_done(struct tevent_req *subreq)
2441 struct tevent_req *req = tevent_req_callback_data(
2442 subreq, struct tevent_req);
2445 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2446 TALLOC_FREE(subreq);
2447 if (!NT_STATUS_IS_OK(status)) {
2448 tevent_req_nterror(req, status);
2451 tevent_req_done(req);
2454 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
2456 return tevent_req_simple_recv_ntstatus(req);
2459 NTSTATUS cli_setattrE(struct cli_state *cli,
2465 TALLOC_CTX *frame = talloc_stackframe();
2466 struct event_context *ev = NULL;
2467 struct tevent_req *req = NULL;
2468 NTSTATUS status = NT_STATUS_OK;
2470 if (cli_has_async_calls(cli)) {
2472 * Can't use sync call while an async call is in flight
2474 status = NT_STATUS_INVALID_PARAMETER;
2478 ev = event_context_init(frame);
2480 status = NT_STATUS_NO_MEMORY;
2484 req = cli_setattrE_send(frame, ev,
2492 status = NT_STATUS_NO_MEMORY;
2496 if (!tevent_req_poll(req, ev)) {
2497 status = map_nt_error_from_unix(errno);
2501 status = cli_setattrE_recv(req);
2505 if (!NT_STATUS_IS_OK(status)) {
2506 cli_set_error(cli, status);
2511 /****************************************************************************
2512 Do a SMBsetatr call.
2513 ****************************************************************************/
2515 static void cli_setatr_done(struct tevent_req *subreq);
2517 struct cli_setatr_state {
2521 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
2522 struct event_context *ev,
2523 struct cli_state *cli,
2528 struct tevent_req *req = NULL, *subreq = NULL;
2529 struct cli_setatr_state *state = NULL;
2530 uint8_t additional_flags = 0;
2531 uint8_t *bytes = NULL;
2533 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
2538 memset(state->vwv, '\0', sizeof(state->vwv));
2539 SSVAL(state->vwv+0, 0, attr);
2540 cli_put_dos_date3(cli, (char *)&state->vwv[1], 0, mtime);
2542 bytes = talloc_array(state, uint8_t, 1);
2543 if (tevent_req_nomem(bytes, req)) {
2544 return tevent_req_post(req, ev);
2547 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2548 strlen(fname)+1, NULL);
2549 if (tevent_req_nomem(bytes, req)) {
2550 return tevent_req_post(req, ev);
2552 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
2553 talloc_get_size(bytes)+1);
2554 if (tevent_req_nomem(bytes, req)) {
2555 return tevent_req_post(req, ev);
2558 bytes[talloc_get_size(bytes)-1] = 4;
2559 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
2561 if (tevent_req_nomem(bytes, req)) {
2562 return tevent_req_post(req, ev);
2565 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
2566 8, state->vwv, talloc_get_size(bytes), bytes);
2567 if (tevent_req_nomem(subreq, req)) {
2568 return tevent_req_post(req, ev);
2570 tevent_req_set_callback(subreq, cli_setatr_done, req);
2574 static void cli_setatr_done(struct tevent_req *subreq)
2576 struct tevent_req *req = tevent_req_callback_data(
2577 subreq, struct tevent_req);
2580 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2581 TALLOC_FREE(subreq);
2582 if (!NT_STATUS_IS_OK(status)) {
2583 tevent_req_nterror(req, status);
2586 tevent_req_done(req);
2589 NTSTATUS cli_setatr_recv(struct tevent_req *req)
2591 return tevent_req_simple_recv_ntstatus(req);
2594 NTSTATUS cli_setatr(struct cli_state *cli,
2599 TALLOC_CTX *frame = talloc_stackframe();
2600 struct event_context *ev = NULL;
2601 struct tevent_req *req = NULL;
2602 NTSTATUS status = NT_STATUS_OK;
2604 if (cli_has_async_calls(cli)) {
2606 * Can't use sync call while an async call is in flight
2608 status = NT_STATUS_INVALID_PARAMETER;
2612 ev = event_context_init(frame);
2614 status = NT_STATUS_NO_MEMORY;
2618 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
2620 status = NT_STATUS_NO_MEMORY;
2624 if (!tevent_req_poll(req, ev)) {
2625 status = map_nt_error_from_unix(errno);
2629 status = cli_setatr_recv(req);
2633 if (!NT_STATUS_IS_OK(status)) {
2634 cli_set_error(cli, status);
2639 /****************************************************************************
2640 Check for existance of a dir.
2641 ****************************************************************************/
2643 static void cli_chkpath_done(struct tevent_req *subreq);
2645 struct cli_chkpath_state {
2649 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
2650 struct event_context *ev,
2651 struct cli_state *cli,
2654 struct tevent_req *req = NULL, *subreq = NULL;
2655 struct cli_chkpath_state *state = NULL;
2656 uint8_t additional_flags = 0;
2657 uint8_t *bytes = NULL;
2659 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
2664 bytes = talloc_array(state, uint8_t, 1);
2665 if (tevent_req_nomem(bytes, req)) {
2666 return tevent_req_post(req, ev);
2669 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2670 strlen(fname)+1, NULL);
2672 if (tevent_req_nomem(bytes, req)) {
2673 return tevent_req_post(req, ev);
2676 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
2677 0, NULL, talloc_get_size(bytes), bytes);
2678 if (tevent_req_nomem(subreq, req)) {
2679 return tevent_req_post(req, ev);
2681 tevent_req_set_callback(subreq, cli_chkpath_done, req);
2685 static void cli_chkpath_done(struct tevent_req *subreq)
2687 struct tevent_req *req = tevent_req_callback_data(
2688 subreq, struct tevent_req);
2691 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2692 TALLOC_FREE(subreq);
2693 if (!NT_STATUS_IS_OK(status)) {
2694 tevent_req_nterror(req, status);
2697 tevent_req_done(req);
2700 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
2702 return tevent_req_simple_recv_ntstatus(req);
2705 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
2707 TALLOC_CTX *frame = talloc_stackframe();
2708 struct event_context *ev = NULL;
2709 struct tevent_req *req = NULL;
2711 NTSTATUS status = NT_STATUS_OK;
2713 if (cli_has_async_calls(cli)) {
2715 * Can't use sync call while an async call is in flight
2717 status = NT_STATUS_INVALID_PARAMETER;
2721 path2 = talloc_strdup(frame, path);
2723 status = NT_STATUS_NO_MEMORY;
2726 trim_char(path2,'\0','\\');
2728 path2 = talloc_strdup(frame, "\\");
2730 status = NT_STATUS_NO_MEMORY;
2735 ev = event_context_init(frame);
2737 status = NT_STATUS_NO_MEMORY;
2741 req = cli_chkpath_send(frame, ev, cli, path2);
2743 status = NT_STATUS_NO_MEMORY;
2747 if (!tevent_req_poll(req, ev)) {
2748 status = map_nt_error_from_unix(errno);
2752 status = cli_chkpath_recv(req);
2756 if (!NT_STATUS_IS_OK(status)) {
2757 cli_set_error(cli, status);
2762 /****************************************************************************
2764 ****************************************************************************/
2766 static void cli_dskattr_done(struct tevent_req *subreq);
2768 struct cli_dskattr_state {
2774 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
2775 struct event_context *ev,
2776 struct cli_state *cli)
2778 struct tevent_req *req = NULL, *subreq = NULL;
2779 struct cli_dskattr_state *state = NULL;
2780 uint8_t additional_flags = 0;
2782 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
2787 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
2789 if (tevent_req_nomem(subreq, req)) {
2790 return tevent_req_post(req, ev);
2792 tevent_req_set_callback(subreq, cli_dskattr_done, req);
2796 static void cli_dskattr_done(struct tevent_req *subreq)
2798 struct tevent_req *req = tevent_req_callback_data(
2799 subreq, struct tevent_req);
2800 struct cli_dskattr_state *state = tevent_req_data(
2801 req, struct cli_dskattr_state);
2803 uint16_t *vwv = NULL;
2806 status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
2807 if (!NT_STATUS_IS_OK(status)) {
2808 tevent_req_nterror(req, status);
2811 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
2812 state->total = SVAL(vwv+0, 0);
2813 state->avail = SVAL(vwv+3, 0);
2814 TALLOC_FREE(subreq);
2815 tevent_req_done(req);
2818 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
2820 struct cli_dskattr_state *state = tevent_req_data(
2821 req, struct cli_dskattr_state);
2824 if (tevent_req_is_nterror(req, &status)) {
2827 *bsize = state->bsize;
2828 *total = state->total;
2829 *avail = state->avail;
2830 return NT_STATUS_OK;
2833 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
2835 TALLOC_CTX *frame = talloc_stackframe();
2836 struct event_context *ev = NULL;
2837 struct tevent_req *req = NULL;
2838 NTSTATUS status = NT_STATUS_OK;
2840 if (cli_has_async_calls(cli)) {
2842 * Can't use sync call while an async call is in flight
2844 status = NT_STATUS_INVALID_PARAMETER;
2848 ev = event_context_init(frame);
2850 status = NT_STATUS_NO_MEMORY;
2854 req = cli_dskattr_send(frame, ev, cli);
2856 status = NT_STATUS_NO_MEMORY;
2860 if (!tevent_req_poll(req, ev)) {
2861 status = map_nt_error_from_unix(errno);
2865 status = cli_dskattr_recv(req, bsize, total, avail);
2869 if (!NT_STATUS_IS_OK(status)) {
2870 cli_set_error(cli, status);
2875 /****************************************************************************
2876 Create and open a temporary file.
2877 ****************************************************************************/
2879 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
2884 memset(cli->outbuf,'\0',smb_size);
2885 memset(cli->inbuf,'\0',smb_size);
2887 cli_set_message(cli->outbuf,3,0,True);
2889 SCVAL(cli->outbuf,smb_com,SMBctemp);
2890 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2891 cli_setup_packet(cli);
2893 SSVAL(cli->outbuf,smb_vwv0,0);
2894 SIVALS(cli->outbuf,smb_vwv1,-1);
2896 p = smb_buf(cli->outbuf);
2898 p += clistr_push(cli, p, path,
2899 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2901 cli_setup_bcc(cli, p);
2904 if (!cli_receive_smb(cli)) {
2908 if (cli_is_error(cli)) {
2912 /* despite the spec, the result has a -1, followed by
2913 length, followed by name */
2914 p = smb_buf(cli->inbuf);
2916 len = smb_buflen(cli->inbuf) - 4;
2917 if (len <= 0 || len > PATH_MAX) return -1;
2920 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
2924 clistr_pull(cli->inbuf, path2, p,
2925 len+1, len, STR_ASCII);
2929 return SVAL(cli->inbuf,smb_vwv0);
2933 send a raw ioctl - used by the torture code
2935 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
2937 memset(cli->outbuf,'\0',smb_size);
2938 memset(cli->inbuf,'\0',smb_size);
2940 cli_set_message(cli->outbuf, 3, 0, True);
2941 SCVAL(cli->outbuf,smb_com,SMBioctl);
2942 cli_setup_packet(cli);
2944 SSVAL(cli->outbuf, smb_vwv0, fnum);
2945 SSVAL(cli->outbuf, smb_vwv1, code>>16);
2946 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
2949 if (!cli_receive_smb(cli)) {
2950 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2953 if (cli_is_error(cli)) {
2954 return cli_nt_error(cli);
2957 *blob = data_blob_null;
2959 return NT_STATUS_OK;
2962 /*********************************************************
2963 Set an extended attribute utility fn.
2964 *********************************************************/
2966 static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsigned int param_len,
2967 const char *ea_name, const char *ea_val, size_t ea_len)
2969 unsigned int data_len = 0;
2971 char *rparam=NULL, *rdata=NULL;
2973 size_t ea_namelen = strlen(ea_name);
2975 if (ea_namelen == 0 && ea_len == 0) {
2977 data = (char *)SMB_MALLOC(data_len);
2982 SIVAL(p,0,data_len);
2984 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
2985 data = (char *)SMB_MALLOC(data_len);
2990 SIVAL(p,0,data_len);
2992 SCVAL(p, 0, 0); /* EA flags. */
2993 SCVAL(p, 1, ea_namelen);
2994 SSVAL(p, 2, ea_len);
2995 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
2996 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
2999 if (!cli_send_trans(cli, SMBtrans2,
3001 -1, 0, /* fid, flags */
3002 &setup, 1, 0, /* setup, length, max */
3003 param, param_len, 2, /* param, length, max */
3004 data, data_len, cli->max_xmit /* data, length, max */
3010 if (!cli_receive_trans(cli, SMBtrans2,
3011 &rparam, ¶m_len,
3012 &rdata, &data_len)) {
3024 /*********************************************************
3025 Set an extended attribute on a pathname.
3026 *********************************************************/
3028 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
3030 uint16_t setup = TRANSACT2_SETPATHINFO;
3031 unsigned int param_len = 0;
3033 size_t srclen = 2*(strlen(path)+1);
3037 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
3041 memset(param, '\0', 6);
3042 SSVAL(param,0,SMB_INFO_SET_EA);
3045 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
3046 param_len = PTR_DIFF(p, param);
3048 ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
3053 /*********************************************************
3054 Set an extended attribute on an fnum.
3055 *********************************************************/
3057 bool cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum, const char *ea_name, const char *ea_val, size_t ea_len)
3060 uint16_t setup = TRANSACT2_SETFILEINFO;
3062 memset(param, 0, 6);
3063 SSVAL(param,0,fnum);
3064 SSVAL(param,2,SMB_INFO_SET_EA);
3066 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
3069 /*********************************************************
3070 Get an extended attribute list utility fn.
3071 *********************************************************/
3073 static bool cli_get_ea_list(struct cli_state *cli,
3074 uint16_t setup, char *param, unsigned int param_len,
3077 struct ea_struct **pea_list)
3079 unsigned int data_len = 0;
3080 unsigned int rparam_len, rdata_len;
3081 char *rparam=NULL, *rdata=NULL;
3086 struct ea_struct *ea_list;
3093 if (!cli_send_trans(cli, SMBtrans2,
3095 -1, 0, /* fid, flags */
3096 &setup, 1, 0, /* setup, length, max */
3097 param, param_len, 10, /* param, length, max */
3098 NULL, data_len, cli->max_xmit /* data, length, max */
3103 if (!cli_receive_trans(cli, SMBtrans2,
3104 &rparam, &rparam_len,
3105 &rdata, &rdata_len)) {
3109 if (!rdata || rdata_len < 4) {
3113 ea_size = (size_t)IVAL(rdata,0);
3114 if (ea_size > rdata_len) {
3119 /* No EA's present. */
3127 /* Validate the EA list and count it. */
3128 for (num_eas = 0; ea_size >= 4; num_eas++) {
3129 unsigned int ea_namelen = CVAL(p,1);
3130 unsigned int ea_valuelen = SVAL(p,2);
3131 if (ea_namelen == 0) {
3134 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
3137 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
3138 p += 4 + ea_namelen + 1 + ea_valuelen;
3146 *pnum_eas = num_eas;
3148 /* Caller only wants number of EA's. */
3153 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
3158 ea_size = (size_t)IVAL(rdata,0);
3161 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
3162 struct ea_struct *ea = &ea_list[num_eas];
3163 fstring unix_ea_name;
3164 unsigned int ea_namelen = CVAL(p,1);
3165 unsigned int ea_valuelen = SVAL(p,2);
3167 ea->flags = CVAL(p,0);
3168 unix_ea_name[0] = '\0';
3169 pull_ascii_fstring(unix_ea_name, p + 4);
3170 ea->name = talloc_strdup(ctx, unix_ea_name);
3171 /* Ensure the value is null terminated (in case it's a string). */
3172 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
3173 if (!ea->value.data) {
3177 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
3179 ea->value.data[ea_valuelen] = 0;
3181 p += 4 + ea_namelen + 1 + ea_valuelen;
3184 *pea_list = ea_list;
3194 /*********************************************************
3195 Get an extended attribute list from a pathname.
3196 *********************************************************/
3198 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
3201 struct ea_struct **pea_list)
3203 uint16_t setup = TRANSACT2_QPATHINFO;
3204 unsigned int param_len = 0;
3207 size_t srclen = 2*(strlen(path)+1);
3210 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
3216 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
3218 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
3219 param_len = PTR_DIFF(p, param);
3221 ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
3226 /*********************************************************
3227 Get an extended attribute list from an fnum.
3228 *********************************************************/
3230 bool cli_get_ea_list_fnum(struct cli_state *cli, uint16_t fnum,
3233 struct ea_struct **pea_list)
3235 uint16_t setup = TRANSACT2_QFILEINFO;
3238 memset(param, 0, 6);
3239 SSVAL(param,0,fnum);
3240 SSVAL(param,2,SMB_INFO_SET_EA);
3242 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
3245 /****************************************************************************
3246 Convert open "flags" arg to uint32_t on wire.
3247 ****************************************************************************/
3249 static uint32_t open_flags_to_wire(int flags)
3251 int open_mode = flags & O_ACCMODE;
3254 switch (open_mode) {
3256 ret |= SMB_O_WRONLY;
3263 ret |= SMB_O_RDONLY;
3267 if (flags & O_CREAT) {
3270 if (flags & O_EXCL) {
3273 if (flags & O_TRUNC) {
3277 if (flags & O_SYNC) {
3281 if (flags & O_APPEND) {
3282 ret |= SMB_O_APPEND;
3284 #if defined(O_DIRECT)
3285 if (flags & O_DIRECT) {
3286 ret |= SMB_O_DIRECT;
3289 #if defined(O_DIRECTORY)
3290 if (flags & O_DIRECTORY) {
3291 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
3292 ret |= SMB_O_DIRECTORY;
3298 /****************************************************************************
3299 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
3300 ****************************************************************************/
3302 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
3304 unsigned int data_len = 0;
3305 unsigned int param_len = 0;
3306 uint16_t setup = TRANSACT2_SETPATHINFO;
3309 char *rparam=NULL, *rdata=NULL;
3311 uint16_t fnum = (uint16_t)-1;
3312 uint32_t wire_flags = open_flags_to_wire(flags);
3313 size_t srclen = 2*(strlen(fname)+1);
3315 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
3319 memset(param, '\0', 6);
3320 SSVAL(param,0, SMB_POSIX_PATH_OPEN);
3323 p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
3324 param_len = PTR_DIFF(p, param);
3327 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
3328 wire_flags |= SMB_O_DIRECTORY;
3332 SIVAL(p,0,0); /* No oplock. */
3333 SIVAL(p,4,wire_flags);
3334 SIVAL(p,8,unix_perms_to_wire(mode));
3335 SIVAL(p,12,0); /* Top bits of perms currently undefined. */
3336 SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
3340 if (!cli_send_trans(cli, SMBtrans2,
3342 -1, 0, /* fid, flags */
3343 &setup, 1, 0, /* setup, length, max */
3344 param, param_len, 0, /* param, length, max */
3345 (char *)&data, data_len, cli->max_xmit /* data, length, max */
3353 if (!cli_receive_trans(cli, SMBtrans2,
3354 &rparam, ¶m_len,
3355 &rdata, &data_len)) {
3359 fnum = SVAL(rdata,2);
3367 /****************************************************************************
3368 open - POSIX semantics.
3369 ****************************************************************************/
3371 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
3373 return cli_posix_open_internal(cli, fname, flags, mode, False);
3376 /****************************************************************************
3377 mkdir - POSIX semantics.
3378 ****************************************************************************/
3380 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
3382 return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
3385 /****************************************************************************
3386 unlink or rmdir - POSIX semantics.
3387 ****************************************************************************/
3389 struct unlink_state {
3394 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
3396 struct tevent_req *req = tevent_req_callback_data(
3397 subreq, struct tevent_req);
3398 struct unlink_state *state = tevent_req_data(req, struct unlink_state);
3401 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, NULL, NULL);
3402 TALLOC_FREE(subreq);
3403 if (!NT_STATUS_IS_OK(status)) {
3404 tevent_req_nterror(req, status);
3407 tevent_req_done(req);
3410 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
3411 struct event_context *ev,
3412 struct cli_state *cli,
3416 struct tevent_req *req = NULL, *subreq = NULL;
3417 struct unlink_state *state = NULL;
3418 uint8_t *param = NULL;
3420 req = tevent_req_create(mem_ctx, &state, struct unlink_state);
3425 /* Setup setup word. */
3426 SSVAL(&state->setup, 0, TRANSACT2_SETPATHINFO);
3428 /* Setup param array. */
3429 param = talloc_array(state, uint8_t, 6);
3430 if (tevent_req_nomem(param, req)) {
3431 return tevent_req_post(req, ev);
3433 memset(param, '\0', 6);
3434 SSVAL(param, 0, SMB_POSIX_PATH_UNLINK);
3436 param = trans2_bytes_push_str(param, cli_ucs2(cli), fname,
3437 strlen(fname)+1, NULL);
3439 if (tevent_req_nomem(param, req)) {
3440 return tevent_req_post(req, ev);
3443 /* Setup data word. */
3444 SSVAL(state->data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
3445 SMB_POSIX_UNLINK_FILE_TARGET);
3447 subreq = cli_trans_send(state, /* mem ctx. */
3448 ev, /* event ctx. */
3449 cli, /* cli_state. */
3450 SMBtrans2, /* cmd. */
3451 NULL, /* pipe name. */
3455 &state->setup, /* setup. */
3456 1, /* num setup uint16_t words. */
3457 0, /* max returned setup. */
3459 talloc_get_size(param), /* num param. */
3460 2, /* max returned param. */
3461 state->data, /* data. */
3463 0); /* max returned data. */
3465 if (tevent_req_nomem(subreq, req)) {
3466 return tevent_req_post(req, ev);
3468 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
3472 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
3473 struct event_context *ev,
3474 struct cli_state *cli,
3477 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, false);
3480 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
3484 if (tevent_req_is_nterror(req, &status)) {
3487 return NT_STATUS_OK;
3490 /****************************************************************************
3491 unlink - POSIX semantics.
3492 ****************************************************************************/
3494 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
3496 TALLOC_CTX *frame = talloc_stackframe();
3497 struct event_context *ev = NULL;
3498 struct tevent_req *req = NULL;
3499 NTSTATUS status = NT_STATUS_OK;
3501 if (cli_has_async_calls(cli)) {
3503 * Can't use sync call while an async call is in flight
3505 status = NT_STATUS_INVALID_PARAMETER;
3509 ev = event_context_init(frame);
3511 status = NT_STATUS_NO_MEMORY;
3515 req = cli_posix_unlink_send(frame,
3520 status = NT_STATUS_NO_MEMORY;
3524 if (!tevent_req_poll(req, ev)) {
3525 status = map_nt_error_from_unix(errno);
3529 status = cli_posix_unlink_recv(req, frame);
3533 if (!NT_STATUS_IS_OK(status)) {
3534 cli_set_error(cli, status);
3539 /****************************************************************************
3540 rmdir - POSIX semantics.
3541 ****************************************************************************/
3543 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
3544 struct event_context *ev,
3545 struct cli_state *cli,
3548 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, true);
3551 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
3555 if (tevent_req_is_nterror(req, &status)) {
3558 return NT_STATUS_OK;
3561 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
3563 TALLOC_CTX *frame = talloc_stackframe();
3564 struct event_context *ev = NULL;
3565 struct tevent_req *req = NULL;
3566 NTSTATUS status = NT_STATUS_OK;
3568 if (cli_has_async_calls(cli)) {
3570 * Can't use sync call while an async call is in flight
3572 status = NT_STATUS_INVALID_PARAMETER;
3576 ev = event_context_init(frame);
3578 status = NT_STATUS_NO_MEMORY;
3582 req = cli_posix_rmdir_send(frame,
3587 status = NT_STATUS_NO_MEMORY;
3591 if (!tevent_req_poll(req, ev)) {
3592 status = map_nt_error_from_unix(errno);
3596 status = cli_posix_rmdir_recv(req, frame);
3600 if (!NT_STATUS_IS_OK(status)) {
3601 cli_set_error(cli, status);