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_done(struct tevent_req *subreq);
557 struct cli_ntrename_state {
561 static struct tevent_req *cli_ntrename_send_internal(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_state *state = NULL;
570 uint8_t additional_flags = 0;
571 uint8_t *bytes = NULL;
573 req = tevent_req_create(mem_ctx, &state, struct cli_ntrename_state);
578 SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
579 SSVAL(state->vwv+1, 0, rename_flag);
581 bytes = talloc_array(state, uint8_t, 1);
582 if (tevent_req_nomem(bytes, req)) {
583 return tevent_req_post(req, ev);
586 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
587 strlen(fname_src)+1, NULL);
588 if (tevent_req_nomem(bytes, req)) {
589 return tevent_req_post(req, ev);
592 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
593 talloc_get_size(bytes)+1);
594 if (tevent_req_nomem(bytes, req)) {
595 return tevent_req_post(req, ev);
598 bytes[talloc_get_size(bytes)-1] = 4;
599 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
600 strlen(fname_dst)+1, NULL);
601 if (tevent_req_nomem(bytes, req)) {
602 return tevent_req_post(req, ev);
605 subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
606 4, state->vwv, talloc_get_size(bytes), bytes);
607 if (tevent_req_nomem(subreq, req)) {
608 return tevent_req_post(req, ev);
610 tevent_req_set_callback(subreq, cli_ntrename_done, req);
614 struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
615 struct event_context *ev,
616 struct cli_state *cli,
617 const char *fname_src,
618 const char *fname_dst)
620 return cli_ntrename_send_internal(mem_ctx,
628 static void cli_ntrename_done(struct tevent_req *subreq)
630 struct tevent_req *req = tevent_req_callback_data(
631 subreq, struct tevent_req);
634 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
636 if (!NT_STATUS_IS_OK(status)) {
637 tevent_req_nterror(req, status);
640 tevent_req_done(req);
643 NTSTATUS cli_ntrename_recv(struct tevent_req *req)
645 return tevent_req_simple_recv_ntstatus(req);
648 NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
650 TALLOC_CTX *frame = talloc_stackframe();
651 struct event_context *ev;
652 struct tevent_req *req;
653 NTSTATUS status = NT_STATUS_OK;
655 if (cli_has_async_calls(cli)) {
657 * Can't use sync call while an async call is in flight
659 status = NT_STATUS_INVALID_PARAMETER;
663 ev = event_context_init(frame);
665 status = NT_STATUS_NO_MEMORY;
669 req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
671 status = NT_STATUS_NO_MEMORY;
675 if (!tevent_req_poll(req, ev)) {
676 status = map_nt_error_from_unix(errno);
680 status = cli_ntrename_recv(req);
684 if (!NT_STATUS_IS_OK(status)) {
685 cli_set_error(cli, status);
690 /****************************************************************************
692 ****************************************************************************/
694 struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
695 struct event_context *ev,
696 struct cli_state *cli,
697 const char *fname_src,
698 const char *fname_dst)
700 return cli_ntrename_send_internal(mem_ctx,
705 RENAME_FLAG_HARD_LINK);
708 NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
710 return tevent_req_simple_recv_ntstatus(req);
713 NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
715 TALLOC_CTX *frame = talloc_stackframe();
716 struct event_context *ev;
717 struct tevent_req *req;
718 NTSTATUS status = NT_STATUS_OK;
720 if (cli_has_async_calls(cli)) {
722 * Can't use sync call while an async call is in flight
724 status = NT_STATUS_INVALID_PARAMETER;
728 ev = event_context_init(frame);
730 status = NT_STATUS_NO_MEMORY;
734 req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
736 status = NT_STATUS_NO_MEMORY;
740 if (!tevent_req_poll(req, ev)) {
741 status = map_nt_error_from_unix(errno);
745 status = cli_nt_hardlink_recv(req);
749 if (!NT_STATUS_IS_OK(status)) {
750 cli_set_error(cli, status);
755 /****************************************************************************
757 ****************************************************************************/
759 static void cli_unlink_done(struct tevent_req *subreq);
761 struct cli_unlink_state {
765 struct tevent_req *cli_unlink_send(TALLOC_CTX *mem_ctx,
766 struct event_context *ev,
767 struct cli_state *cli,
769 uint16_t mayhave_attrs)
771 struct tevent_req *req = NULL, *subreq = NULL;
772 struct cli_unlink_state *state = NULL;
773 uint8_t additional_flags = 0;
774 uint8_t *bytes = NULL;
776 req = tevent_req_create(mem_ctx, &state, struct cli_unlink_state);
781 SSVAL(state->vwv+0, 0, mayhave_attrs);
783 bytes = talloc_array(state, uint8_t, 1);
784 if (tevent_req_nomem(bytes, req)) {
785 return tevent_req_post(req, ev);
788 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
789 strlen(fname)+1, NULL);
791 if (tevent_req_nomem(bytes, req)) {
792 return tevent_req_post(req, ev);
795 subreq = cli_smb_send(state, ev, cli, SMBunlink, additional_flags,
796 1, state->vwv, talloc_get_size(bytes), bytes);
797 if (tevent_req_nomem(subreq, req)) {
798 return tevent_req_post(req, ev);
800 tevent_req_set_callback(subreq, cli_unlink_done, req);
804 static void cli_unlink_done(struct tevent_req *subreq)
806 struct tevent_req *req = tevent_req_callback_data(
807 subreq, struct tevent_req);
810 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
812 if (!NT_STATUS_IS_OK(status)) {
813 tevent_req_nterror(req, status);
816 tevent_req_done(req);
819 NTSTATUS cli_unlink_recv(struct tevent_req *req)
821 return tevent_req_simple_recv_ntstatus(req);
824 NTSTATUS cli_unlink(struct cli_state *cli, const char *fname, uint16_t mayhave_attrs)
826 TALLOC_CTX *frame = talloc_stackframe();
827 struct event_context *ev;
828 struct tevent_req *req;
829 NTSTATUS status = NT_STATUS_OK;
831 if (cli_has_async_calls(cli)) {
833 * Can't use sync call while an async call is in flight
835 status = NT_STATUS_INVALID_PARAMETER;
839 ev = event_context_init(frame);
841 status = NT_STATUS_NO_MEMORY;
845 req = cli_unlink_send(frame, ev, cli, fname, mayhave_attrs);
847 status = NT_STATUS_NO_MEMORY;
851 if (!tevent_req_poll(req, ev)) {
852 status = map_nt_error_from_unix(errno);
856 status = cli_unlink_recv(req);
860 if (!NT_STATUS_IS_OK(status)) {
861 cli_set_error(cli, status);
866 /****************************************************************************
868 ****************************************************************************/
870 static void cli_mkdir_done(struct tevent_req *subreq);
872 struct cli_mkdir_state {
876 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
877 struct event_context *ev,
878 struct cli_state *cli,
881 struct tevent_req *req = NULL, *subreq = NULL;
882 struct cli_mkdir_state *state = NULL;
883 uint8_t additional_flags = 0;
884 uint8_t *bytes = NULL;
886 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
891 bytes = talloc_array(state, uint8_t, 1);
892 if (tevent_req_nomem(bytes, req)) {
893 return tevent_req_post(req, ev);
896 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
897 strlen(dname)+1, NULL);
899 if (tevent_req_nomem(bytes, req)) {
900 return tevent_req_post(req, ev);
903 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
904 0, NULL, talloc_get_size(bytes), bytes);
905 if (tevent_req_nomem(subreq, req)) {
906 return tevent_req_post(req, ev);
908 tevent_req_set_callback(subreq, cli_mkdir_done, req);
912 static void cli_mkdir_done(struct tevent_req *subreq)
914 struct tevent_req *req = tevent_req_callback_data(
915 subreq, struct tevent_req);
918 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
920 if (!NT_STATUS_IS_OK(status)) {
921 tevent_req_nterror(req, status);
924 tevent_req_done(req);
927 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
929 return tevent_req_simple_recv_ntstatus(req);
932 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
934 TALLOC_CTX *frame = talloc_stackframe();
935 struct event_context *ev;
936 struct tevent_req *req;
937 NTSTATUS status = NT_STATUS_OK;
939 if (cli_has_async_calls(cli)) {
941 * Can't use sync call while an async call is in flight
943 status = NT_STATUS_INVALID_PARAMETER;
947 ev = event_context_init(frame);
949 status = NT_STATUS_NO_MEMORY;
953 req = cli_mkdir_send(frame, ev, cli, dname);
955 status = NT_STATUS_NO_MEMORY;
959 if (!tevent_req_poll(req, ev)) {
960 status = map_nt_error_from_unix(errno);
964 status = cli_mkdir_recv(req);
968 if (!NT_STATUS_IS_OK(status)) {
969 cli_set_error(cli, status);
974 /****************************************************************************
976 ****************************************************************************/
978 static void cli_rmdir_done(struct tevent_req *subreq);
980 struct cli_rmdir_state {
984 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
985 struct event_context *ev,
986 struct cli_state *cli,
989 struct tevent_req *req = NULL, *subreq = NULL;
990 struct cli_rmdir_state *state = NULL;
991 uint8_t additional_flags = 0;
992 uint8_t *bytes = NULL;
994 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
999 bytes = talloc_array(state, uint8_t, 1);
1000 if (tevent_req_nomem(bytes, req)) {
1001 return tevent_req_post(req, ev);
1004 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
1005 strlen(dname)+1, NULL);
1007 if (tevent_req_nomem(bytes, req)) {
1008 return tevent_req_post(req, ev);
1011 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
1012 0, NULL, talloc_get_size(bytes), bytes);
1013 if (tevent_req_nomem(subreq, req)) {
1014 return tevent_req_post(req, ev);
1016 tevent_req_set_callback(subreq, cli_rmdir_done, req);
1020 static void cli_rmdir_done(struct tevent_req *subreq)
1022 struct tevent_req *req = tevent_req_callback_data(
1023 subreq, struct tevent_req);
1026 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1027 TALLOC_FREE(subreq);
1028 if (!NT_STATUS_IS_OK(status)) {
1029 tevent_req_nterror(req, status);
1032 tevent_req_done(req);
1035 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
1037 return tevent_req_simple_recv_ntstatus(req);
1040 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
1042 TALLOC_CTX *frame = talloc_stackframe();
1043 struct event_context *ev;
1044 struct tevent_req *req;
1045 NTSTATUS status = NT_STATUS_OK;
1047 if (cli_has_async_calls(cli)) {
1049 * Can't use sync call while an async call is in flight
1051 status = NT_STATUS_INVALID_PARAMETER;
1055 ev = event_context_init(frame);
1057 status = NT_STATUS_NO_MEMORY;
1061 req = cli_rmdir_send(frame, ev, cli, dname);
1063 status = NT_STATUS_NO_MEMORY;
1067 if (!tevent_req_poll(req, ev)) {
1068 status = map_nt_error_from_unix(errno);
1072 status = cli_rmdir_recv(req);
1076 if (!NT_STATUS_IS_OK(status)) {
1077 cli_set_error(cli, status);
1082 /****************************************************************************
1083 Set or clear the delete on close flag.
1084 ****************************************************************************/
1086 int cli_nt_delete_on_close(struct cli_state *cli, uint16_t fnum, bool flag)
1088 unsigned int data_len = 1;
1089 unsigned int param_len = 6;
1090 uint16_t setup = TRANSACT2_SETFILEINFO;
1093 char *rparam=NULL, *rdata=NULL;
1095 memset(param, 0, param_len);
1096 SSVAL(param,0,fnum);
1097 SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
1099 data = flag ? 1 : 0;
1101 if (!cli_send_trans(cli, SMBtrans2,
1103 -1, 0, /* fid, flags */
1104 &setup, 1, 0, /* setup, length, max */
1105 param, param_len, 2, /* param, length, max */
1106 (char *)&data, data_len, cli->max_xmit /* data, length, max */
1111 if (!cli_receive_trans(cli, SMBtrans2,
1112 &rparam, ¶m_len,
1113 &rdata, &data_len)) {
1123 struct cli_ntcreate_state {
1128 static void cli_ntcreate_done(struct tevent_req *subreq);
1130 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1131 struct event_context *ev,
1132 struct cli_state *cli,
1134 uint32_t CreatFlags,
1135 uint32_t DesiredAccess,
1136 uint32_t FileAttributes,
1137 uint32_t ShareAccess,
1138 uint32_t CreateDisposition,
1139 uint32_t CreateOptions,
1140 uint8_t SecurityFlags)
1142 struct tevent_req *req, *subreq;
1143 struct cli_ntcreate_state *state;
1146 size_t converted_len;
1148 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1154 SCVAL(vwv+0, 0, 0xFF);
1159 if (cli->use_oplocks) {
1160 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1162 SIVAL(vwv+3, 1, CreatFlags);
1163 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1164 SIVAL(vwv+7, 1, DesiredAccess);
1165 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1166 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1167 SIVAL(vwv+13, 1, FileAttributes);
1168 SIVAL(vwv+15, 1, ShareAccess);
1169 SIVAL(vwv+17, 1, CreateDisposition);
1170 SIVAL(vwv+19, 1, CreateOptions);
1171 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1172 SCVAL(vwv+23, 1, SecurityFlags);
1174 bytes = talloc_array(state, uint8_t, 0);
1175 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1176 fname, strlen(fname)+1,
1179 /* sigh. this copes with broken netapp filer behaviour */
1180 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1182 if (tevent_req_nomem(bytes, req)) {
1183 return tevent_req_post(req, ev);
1186 SIVAL(vwv+2, 1, converted_len);
1188 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1189 talloc_get_size(bytes), bytes);
1190 if (tevent_req_nomem(subreq, req)) {
1191 return tevent_req_post(req, ev);
1193 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1197 static void cli_ntcreate_done(struct tevent_req *subreq)
1199 struct tevent_req *req = tevent_req_callback_data(
1200 subreq, struct tevent_req);
1201 struct cli_ntcreate_state *state = tevent_req_data(
1202 req, struct cli_ntcreate_state);
1209 status = cli_smb_recv(subreq, 3, &wct, &vwv, &num_bytes, &bytes);
1210 if (!NT_STATUS_IS_OK(status)) {
1211 TALLOC_FREE(subreq);
1212 tevent_req_nterror(req, status);
1215 state->fnum = SVAL(vwv+2, 1);
1216 tevent_req_done(req);
1219 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1221 struct cli_ntcreate_state *state = tevent_req_data(
1222 req, struct cli_ntcreate_state);
1225 if (tevent_req_is_nterror(req, &status)) {
1228 *pfnum = state->fnum;
1229 return NT_STATUS_OK;
1232 NTSTATUS cli_ntcreate(struct cli_state *cli,
1234 uint32_t CreatFlags,
1235 uint32_t DesiredAccess,
1236 uint32_t FileAttributes,
1237 uint32_t ShareAccess,
1238 uint32_t CreateDisposition,
1239 uint32_t CreateOptions,
1240 uint8_t SecurityFlags,
1243 TALLOC_CTX *frame = talloc_stackframe();
1244 struct event_context *ev;
1245 struct tevent_req *req;
1246 NTSTATUS status = NT_STATUS_OK;
1248 if (cli_has_async_calls(cli)) {
1250 * Can't use sync call while an async call is in flight
1252 status = NT_STATUS_INVALID_PARAMETER;
1256 ev = event_context_init(frame);
1258 status = NT_STATUS_NO_MEMORY;
1262 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1263 DesiredAccess, FileAttributes, ShareAccess,
1264 CreateDisposition, CreateOptions,
1267 status = NT_STATUS_NO_MEMORY;
1271 if (!tevent_req_poll(req, ev)) {
1272 status = map_nt_error_from_unix(errno);
1276 status = cli_ntcreate_recv(req, pfid);
1279 if (!NT_STATUS_IS_OK(status)) {
1280 cli_set_error(cli, status);
1285 /***********************************************************
1286 Common function for pushing stings, used by smb_bytes_push_str()
1287 and trans_bytes_push_str(). Only difference is the align_odd
1289 ***********************************************************/
1291 static uint8_t *internal_bytes_push_str(uint8_t *buf, bool ucs2,
1292 const char *str, size_t str_len,
1294 size_t *pconverted_size)
1298 size_t converted_size;
1304 buflen = talloc_get_size(buf);
1306 if (align_odd && ucs2 && (buflen % 2 == 0)) {
1308 * We're pushing into an SMB buffer, align odd
1310 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
1318 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
1319 ucs2 ? CH_UTF16LE : CH_DOS,
1320 str, str_len, &converted,
1321 &converted_size, true)) {
1325 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
1326 buflen + converted_size);
1328 TALLOC_FREE(converted);
1332 memcpy(buf + buflen, converted, converted_size);
1334 TALLOC_FREE(converted);
1336 if (pconverted_size) {
1337 *pconverted_size = converted_size;
1343 /***********************************************************
1344 Push a string into an SMB buffer, with odd byte alignment
1345 if it's a UCS2 string.
1346 ***********************************************************/
1348 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
1349 const char *str, size_t str_len,
1350 size_t *pconverted_size)
1352 return internal_bytes_push_str(buf, ucs2, str, str_len,
1353 true, pconverted_size);
1356 /***********************************************************
1357 Same as smb_bytes_push_str(), but without the odd byte
1358 align for ucs2 (we're pushing into a param or data block).
1359 static for now, although this will probably change when
1360 other modules use async trans calls.
1361 ***********************************************************/
1363 static uint8_t *trans2_bytes_push_str(uint8_t *buf, bool ucs2,
1364 const char *str, size_t str_len,
1365 size_t *pconverted_size)
1367 return internal_bytes_push_str(buf, ucs2, str, str_len,
1368 false, pconverted_size);
1371 /****************************************************************************
1373 WARNING: if you open with O_WRONLY then getattrE won't work!
1374 ****************************************************************************/
1376 struct cli_open_state {
1382 static void cli_open_done(struct tevent_req *subreq);
1384 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
1385 struct event_context *ev,
1386 struct cli_state *cli, const char *fname,
1387 int flags, int share_mode,
1388 struct tevent_req **psmbreq)
1390 struct tevent_req *req, *subreq;
1391 struct cli_open_state *state;
1393 unsigned accessmode;
1394 uint8_t additional_flags;
1397 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
1403 if (flags & O_CREAT) {
1406 if (!(flags & O_EXCL)) {
1407 if (flags & O_TRUNC)
1413 accessmode = (share_mode<<4);
1415 if ((flags & O_ACCMODE) == O_RDWR) {
1417 } else if ((flags & O_ACCMODE) == O_WRONLY) {
1422 if ((flags & O_SYNC) == O_SYNC) {
1423 accessmode |= (1<<14);
1427 if (share_mode == DENY_FCB) {
1431 SCVAL(state->vwv + 0, 0, 0xFF);
1432 SCVAL(state->vwv + 0, 1, 0);
1433 SSVAL(state->vwv + 1, 0, 0);
1434 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
1435 SSVAL(state->vwv + 3, 0, accessmode);
1436 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
1437 SSVAL(state->vwv + 5, 0, 0);
1438 SIVAL(state->vwv + 6, 0, 0);
1439 SSVAL(state->vwv + 8, 0, openfn);
1440 SIVAL(state->vwv + 9, 0, 0);
1441 SIVAL(state->vwv + 11, 0, 0);
1442 SIVAL(state->vwv + 13, 0, 0);
1444 additional_flags = 0;
1446 if (cli->use_oplocks) {
1447 /* if using oplocks then ask for a batch oplock via
1448 core and extended methods */
1450 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
1451 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
1454 bytes = talloc_array(state, uint8_t, 0);
1455 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1456 strlen(fname)+1, NULL);
1458 if (tevent_req_nomem(bytes, req)) {
1459 return tevent_req_post(req, ev);
1462 state->bytes.iov_base = bytes;
1463 state->bytes.iov_len = talloc_get_size(bytes);
1465 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
1466 15, state->vwv, 1, &state->bytes);
1467 if (subreq == NULL) {
1471 tevent_req_set_callback(subreq, cli_open_done, req);
1476 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1477 struct cli_state *cli, const char *fname,
1478 int flags, int share_mode)
1480 struct tevent_req *req, *subreq;
1482 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
1484 if ((req == NULL) || !cli_smb_req_send(subreq)) {
1491 static void cli_open_done(struct tevent_req *subreq)
1493 struct tevent_req *req = tevent_req_callback_data(
1494 subreq, struct tevent_req);
1495 struct cli_open_state *state = tevent_req_data(
1496 req, struct cli_open_state);
1501 status = cli_smb_recv(subreq, 3, &wct, &vwv, NULL, NULL);
1502 if (!NT_STATUS_IS_OK(status)) {
1503 TALLOC_FREE(subreq);
1504 tevent_req_nterror(req, status);
1507 state->fnum = SVAL(vwv+2, 0);
1508 tevent_req_done(req);
1511 NTSTATUS cli_open_recv(struct tevent_req *req, uint16_t *pfnum)
1513 struct cli_open_state *state = tevent_req_data(
1514 req, struct cli_open_state);
1517 if (tevent_req_is_nterror(req, &status)) {
1520 *pfnum = state->fnum;
1521 return NT_STATUS_OK;
1524 NTSTATUS cli_open(struct cli_state *cli, const char *fname, int flags,
1525 int share_mode, uint16_t *pfnum)
1527 TALLOC_CTX *frame = talloc_stackframe();
1528 struct event_context *ev;
1529 struct tevent_req *req;
1530 NTSTATUS status = NT_STATUS_OK;
1532 if (cli_has_async_calls(cli)) {
1534 * Can't use sync call while an async call is in flight
1536 status = NT_STATUS_INVALID_PARAMETER;
1540 ev = event_context_init(frame);
1542 status = NT_STATUS_NO_MEMORY;
1546 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
1548 status = NT_STATUS_NO_MEMORY;
1552 if (!tevent_req_poll(req, ev)) {
1553 status = map_nt_error_from_unix(errno);
1557 status = cli_open_recv(req, pfnum);
1560 if (!NT_STATUS_IS_OK(status)) {
1561 cli_set_error(cli, status);
1566 /****************************************************************************
1568 ****************************************************************************/
1570 struct cli_close_state {
1574 static void cli_close_done(struct tevent_req *subreq);
1576 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
1577 struct event_context *ev,
1578 struct cli_state *cli,
1580 struct tevent_req **psubreq)
1582 struct tevent_req *req, *subreq;
1583 struct cli_close_state *state;
1585 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
1589 SSVAL(state->vwv+0, 0, fnum);
1590 SIVALS(state->vwv+1, 0, -1);
1592 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
1594 if (subreq == NULL) {
1598 tevent_req_set_callback(subreq, cli_close_done, req);
1603 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
1604 struct event_context *ev,
1605 struct cli_state *cli,
1608 struct tevent_req *req, *subreq;
1610 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
1611 if ((req == NULL) || !cli_smb_req_send(subreq)) {
1618 static void cli_close_done(struct tevent_req *subreq)
1620 struct tevent_req *req = tevent_req_callback_data(
1621 subreq, struct tevent_req);
1624 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1625 TALLOC_FREE(subreq);
1626 if (!NT_STATUS_IS_OK(status)) {
1627 tevent_req_nterror(req, status);
1630 tevent_req_done(req);
1633 NTSTATUS cli_close_recv(struct tevent_req *req)
1635 return tevent_req_simple_recv_ntstatus(req);
1638 NTSTATUS cli_close(struct cli_state *cli, uint16_t fnum)
1640 TALLOC_CTX *frame = talloc_stackframe();
1641 struct event_context *ev;
1642 struct tevent_req *req;
1643 NTSTATUS status = NT_STATUS_OK;
1645 if (cli_has_async_calls(cli)) {
1647 * Can't use sync call while an async call is in flight
1649 status = NT_STATUS_INVALID_PARAMETER;
1653 ev = event_context_init(frame);
1655 status = NT_STATUS_NO_MEMORY;
1659 req = cli_close_send(frame, ev, cli, fnum);
1661 status = NT_STATUS_NO_MEMORY;
1665 if (!tevent_req_poll(req, ev)) {
1666 status = map_nt_error_from_unix(errno);
1670 status = cli_close_recv(req);
1673 if (!NT_STATUS_IS_OK(status)) {
1674 cli_set_error(cli, status);
1679 /****************************************************************************
1680 Truncate a file to a specified size
1681 ****************************************************************************/
1683 bool cli_ftruncate(struct cli_state *cli, uint16_t fnum, uint64_t size)
1685 unsigned int param_len = 6;
1686 unsigned int data_len = 8;
1687 uint16_t setup = TRANSACT2_SETFILEINFO;
1689 unsigned char data[8];
1690 char *rparam=NULL, *rdata=NULL;
1691 int saved_timeout = cli->timeout;
1693 SSVAL(param,0,fnum);
1694 SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
1697 SBVAL(data, 0, size);
1699 if (!cli_send_trans(cli, SMBtrans2,
1701 -1, 0, /* fid, flags */
1702 &setup, 1, 0, /* setup, length, max */
1703 param, param_len, 2, /* param, length, max */
1704 (char *)&data, data_len,/* data, length, ... */
1705 cli->max_xmit)) { /* ... max */
1706 cli->timeout = saved_timeout;
1710 if (!cli_receive_trans(cli, SMBtrans2,
1711 &rparam, ¶m_len,
1712 &rdata, &data_len)) {
1713 cli->timeout = saved_timeout;
1719 cli->timeout = saved_timeout;
1728 /****************************************************************************
1729 send a lock with a specified locktype
1730 this is used for testing LOCKING_ANDX_CANCEL_LOCK
1731 ****************************************************************************/
1733 NTSTATUS cli_locktype(struct cli_state *cli, uint16_t fnum,
1734 uint32_t offset, uint32_t len,
1735 int timeout, unsigned char locktype)
1738 int saved_timeout = cli->timeout;
1740 memset(cli->outbuf,'\0',smb_size);
1741 memset(cli->inbuf,'\0', smb_size);
1743 cli_set_message(cli->outbuf,8,0,True);
1745 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1746 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1747 cli_setup_packet(cli);
1749 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1750 SSVAL(cli->outbuf,smb_vwv2,fnum);
1751 SCVAL(cli->outbuf,smb_vwv3,locktype);
1752 SIVALS(cli->outbuf, smb_vwv4, timeout);
1753 SSVAL(cli->outbuf,smb_vwv6,0);
1754 SSVAL(cli->outbuf,smb_vwv7,1);
1756 p = smb_buf(cli->outbuf);
1757 SSVAL(p, 0, cli->pid);
1758 SIVAL(p, 2, offset);
1763 cli_setup_bcc(cli, p);
1768 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
1771 if (!cli_receive_smb(cli)) {
1772 cli->timeout = saved_timeout;
1773 return NT_STATUS_UNSUCCESSFUL;
1776 cli->timeout = saved_timeout;
1778 return cli_nt_error(cli);
1781 /****************************************************************************
1783 note that timeout is in units of 2 milliseconds
1784 ****************************************************************************/
1786 bool cli_lock(struct cli_state *cli, uint16_t fnum,
1787 uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
1790 int saved_timeout = cli->timeout;
1792 memset(cli->outbuf,'\0',smb_size);
1793 memset(cli->inbuf,'\0', smb_size);
1795 cli_set_message(cli->outbuf,8,0,True);
1797 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1798 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1799 cli_setup_packet(cli);
1801 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1802 SSVAL(cli->outbuf,smb_vwv2,fnum);
1803 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
1804 SIVALS(cli->outbuf, smb_vwv4, timeout);
1805 SSVAL(cli->outbuf,smb_vwv6,0);
1806 SSVAL(cli->outbuf,smb_vwv7,1);
1808 p = smb_buf(cli->outbuf);
1809 SSVAL(p, 0, cli->pid);
1810 SIVAL(p, 2, offset);
1815 cli_setup_bcc(cli, p);
1820 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
1823 if (!cli_receive_smb(cli)) {
1824 cli->timeout = saved_timeout;
1828 cli->timeout = saved_timeout;
1830 if (cli_is_error(cli)) {
1837 /****************************************************************************
1839 ****************************************************************************/
1841 bool cli_unlock(struct cli_state *cli, uint16_t fnum, uint32_t offset, uint32_t len)
1845 memset(cli->outbuf,'\0',smb_size);
1846 memset(cli->inbuf,'\0',smb_size);
1848 cli_set_message(cli->outbuf,8,0,True);
1850 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1851 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1852 cli_setup_packet(cli);
1854 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1855 SSVAL(cli->outbuf,smb_vwv2,fnum);
1856 SCVAL(cli->outbuf,smb_vwv3,0);
1857 SIVALS(cli->outbuf, smb_vwv4, 0);
1858 SSVAL(cli->outbuf,smb_vwv6,1);
1859 SSVAL(cli->outbuf,smb_vwv7,0);
1861 p = smb_buf(cli->outbuf);
1862 SSVAL(p, 0, cli->pid);
1863 SIVAL(p, 2, offset);
1866 cli_setup_bcc(cli, p);
1868 if (!cli_receive_smb(cli)) {
1872 if (cli_is_error(cli)) {
1879 /****************************************************************************
1880 Lock a file with 64 bit offsets.
1881 ****************************************************************************/
1883 bool cli_lock64(struct cli_state *cli, uint16_t fnum,
1884 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
1887 int saved_timeout = cli->timeout;
1890 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1891 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1894 ltype = (lock_type == READ_LOCK? 1 : 0);
1895 ltype |= LOCKING_ANDX_LARGE_FILES;
1897 memset(cli->outbuf,'\0',smb_size);
1898 memset(cli->inbuf,'\0', smb_size);
1900 cli_set_message(cli->outbuf,8,0,True);
1902 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1903 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1904 cli_setup_packet(cli);
1906 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1907 SSVAL(cli->outbuf,smb_vwv2,fnum);
1908 SCVAL(cli->outbuf,smb_vwv3,ltype);
1909 SIVALS(cli->outbuf, smb_vwv4, timeout);
1910 SSVAL(cli->outbuf,smb_vwv6,0);
1911 SSVAL(cli->outbuf,smb_vwv7,1);
1913 p = smb_buf(cli->outbuf);
1914 SIVAL(p, 0, cli->pid);
1915 SOFF_T_R(p, 4, offset);
1916 SOFF_T_R(p, 12, len);
1919 cli_setup_bcc(cli, p);
1923 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1926 if (!cli_receive_smb(cli)) {
1927 cli->timeout = saved_timeout;
1931 cli->timeout = saved_timeout;
1933 if (cli_is_error(cli)) {
1940 /****************************************************************************
1941 Unlock a file with 64 bit offsets.
1942 ****************************************************************************/
1944 bool cli_unlock64(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
1948 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1949 return cli_unlock(cli, fnum, offset, len);
1952 memset(cli->outbuf,'\0',smb_size);
1953 memset(cli->inbuf,'\0',smb_size);
1955 cli_set_message(cli->outbuf,8,0,True);
1957 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1958 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1959 cli_setup_packet(cli);
1961 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1962 SSVAL(cli->outbuf,smb_vwv2,fnum);
1963 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1964 SIVALS(cli->outbuf, smb_vwv4, 0);
1965 SSVAL(cli->outbuf,smb_vwv6,1);
1966 SSVAL(cli->outbuf,smb_vwv7,0);
1968 p = smb_buf(cli->outbuf);
1969 SIVAL(p, 0, cli->pid);
1970 SOFF_T_R(p, 4, offset);
1971 SOFF_T_R(p, 12, len);
1973 cli_setup_bcc(cli, p);
1975 if (!cli_receive_smb(cli)) {
1979 if (cli_is_error(cli)) {
1986 /****************************************************************************
1987 Get/unlock a POSIX lock on a file - internal function.
1988 ****************************************************************************/
1990 static bool cli_posix_lock_internal(struct cli_state *cli, uint16_t fnum,
1991 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
1993 unsigned int param_len = 4;
1994 unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1995 uint16_t setup = TRANSACT2_SETFILEINFO;
1997 unsigned char data[POSIX_LOCK_DATA_SIZE];
1998 char *rparam=NULL, *rdata=NULL;
1999 int saved_timeout = cli->timeout;
2001 SSVAL(param,0,fnum);
2002 SSVAL(param,2,SMB_SET_POSIX_LOCK);
2004 switch (lock_type) {
2006 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
2009 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
2012 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
2019 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
2020 cli->timeout = 0x7FFFFFFF;
2022 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
2025 SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
2026 SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
2027 SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
2029 if (!cli_send_trans(cli, SMBtrans2,
2031 -1, 0, /* fid, flags */
2032 &setup, 1, 0, /* setup, length, max */
2033 param, param_len, 2, /* param, length, max */
2034 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2036 cli->timeout = saved_timeout;
2040 if (!cli_receive_trans(cli, SMBtrans2,
2041 &rparam, ¶m_len,
2042 &rdata, &data_len)) {
2043 cli->timeout = saved_timeout;
2049 cli->timeout = saved_timeout;
2057 /****************************************************************************
2059 ****************************************************************************/
2061 bool cli_posix_lock(struct cli_state *cli, uint16_t fnum,
2062 uint64_t offset, uint64_t len,
2063 bool wait_lock, enum brl_type lock_type)
2065 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
2068 return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
2071 /****************************************************************************
2072 POSIX Unlock a file.
2073 ****************************************************************************/
2075 bool cli_posix_unlock(struct cli_state *cli, uint16_t fnum, uint64_t offset, uint64_t len)
2077 return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
2080 /****************************************************************************
2081 POSIX Get any lock covering a file.
2082 ****************************************************************************/
2084 bool cli_posix_getlock(struct cli_state *cli, uint16_t fnum, uint64_t *poffset, uint64_t *plen)
2089 /****************************************************************************
2090 Do a SMBgetattrE call.
2091 ****************************************************************************/
2093 static void cli_getattrE_done(struct tevent_req *subreq);
2095 struct cli_getattrE_state {
2104 struct tevent_req *cli_getattrE_send(TALLOC_CTX *mem_ctx,
2105 struct event_context *ev,
2106 struct cli_state *cli,
2109 struct tevent_req *req = NULL, *subreq = NULL;
2110 struct cli_getattrE_state *state = NULL;
2111 uint8_t additional_flags = 0;
2114 req = tevent_req_create(mem_ctx, &state, struct cli_getattrE_state);
2119 state->zone_offset = cli->serverzone;
2120 SSVAL(vwv+0,0,fnum);
2122 subreq = cli_smb_send(state, ev, cli, SMBgetattrE, additional_flags,
2124 if (tevent_req_nomem(subreq, req)) {
2125 return tevent_req_post(req, ev);
2127 tevent_req_set_callback(subreq, cli_getattrE_done, req);
2131 static void cli_getattrE_done(struct tevent_req *subreq)
2133 struct tevent_req *req = tevent_req_callback_data(
2134 subreq, struct tevent_req);
2135 struct cli_getattrE_state *state = tevent_req_data(
2136 req, struct cli_getattrE_state);
2138 uint16_t *vwv = NULL;
2141 status = cli_smb_recv(subreq, 11, &wct, &vwv, NULL, NULL);
2142 if (!NT_STATUS_IS_OK(status)) {
2143 tevent_req_nterror(req, status);
2147 state->size = (SMB_OFF_T)IVAL(vwv+6,0);
2148 state->attr = SVAL(vwv+10,0);
2149 state->change_time = make_unix_date2(vwv+0, state->zone_offset);
2150 state->access_time = make_unix_date2(vwv+2, state->zone_offset);
2151 state->write_time = make_unix_date2(vwv+4, state->zone_offset);
2153 TALLOC_FREE(subreq);
2154 tevent_req_done(req);
2157 NTSTATUS cli_getattrE_recv(struct tevent_req *req,
2160 time_t *change_time,
2161 time_t *access_time,
2164 struct cli_getattrE_state *state = tevent_req_data(
2165 req, struct cli_getattrE_state);
2168 if (tevent_req_is_nterror(req, &status)) {
2172 *attr = state->attr;
2175 *size = state->size;
2178 *change_time = state->change_time;
2181 *access_time = state->access_time;
2184 *write_time = state->write_time;
2186 return NT_STATUS_OK;
2189 NTSTATUS cli_getattrE(struct cli_state *cli,
2193 time_t *change_time,
2194 time_t *access_time,
2197 TALLOC_CTX *frame = talloc_stackframe();
2198 struct event_context *ev = NULL;
2199 struct tevent_req *req = NULL;
2200 NTSTATUS status = NT_STATUS_OK;
2202 if (cli_has_async_calls(cli)) {
2204 * Can't use sync call while an async call is in flight
2206 status = NT_STATUS_INVALID_PARAMETER;
2210 ev = event_context_init(frame);
2212 status = NT_STATUS_NO_MEMORY;
2216 req = cli_getattrE_send(frame, ev, cli, fnum);
2218 status = NT_STATUS_NO_MEMORY;
2222 if (!tevent_req_poll(req, ev)) {
2223 status = map_nt_error_from_unix(errno);
2227 status = cli_getattrE_recv(req,
2236 if (!NT_STATUS_IS_OK(status)) {
2237 cli_set_error(cli, status);
2242 /****************************************************************************
2244 ****************************************************************************/
2246 static void cli_getatr_done(struct tevent_req *subreq);
2248 struct cli_getatr_state {
2255 struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
2256 struct event_context *ev,
2257 struct cli_state *cli,
2260 struct tevent_req *req = NULL, *subreq = NULL;
2261 struct cli_getatr_state *state = NULL;
2262 uint8_t additional_flags = 0;
2263 uint8_t *bytes = NULL;
2265 req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
2270 state->zone_offset = cli->serverzone;
2272 bytes = talloc_array(state, uint8_t, 1);
2273 if (tevent_req_nomem(bytes, req)) {
2274 return tevent_req_post(req, ev);
2277 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2278 strlen(fname)+1, NULL);
2280 if (tevent_req_nomem(bytes, req)) {
2281 return tevent_req_post(req, ev);
2284 subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
2285 0, NULL, talloc_get_size(bytes), bytes);
2286 if (tevent_req_nomem(subreq, req)) {
2287 return tevent_req_post(req, ev);
2289 tevent_req_set_callback(subreq, cli_getatr_done, req);
2293 static void cli_getatr_done(struct tevent_req *subreq)
2295 struct tevent_req *req = tevent_req_callback_data(
2296 subreq, struct tevent_req);
2297 struct cli_getatr_state *state = tevent_req_data(
2298 req, struct cli_getatr_state);
2300 uint16_t *vwv = NULL;
2303 status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
2304 if (!NT_STATUS_IS_OK(status)) {
2305 tevent_req_nterror(req, status);
2309 state->attr = SVAL(vwv+0,0);
2310 state->size = (SMB_OFF_T)IVAL(vwv+3,0);
2311 state->write_time = make_unix_date3(vwv+1, state->zone_offset);
2313 TALLOC_FREE(subreq);
2314 tevent_req_done(req);
2317 NTSTATUS cli_getatr_recv(struct tevent_req *req,
2322 struct cli_getatr_state *state = tevent_req_data(
2323 req, struct cli_getatr_state);
2326 if (tevent_req_is_nterror(req, &status)) {
2330 *attr = state->attr;
2333 *size = state->size;
2336 *write_time = state->write_time;
2338 return NT_STATUS_OK;
2341 NTSTATUS cli_getatr(struct cli_state *cli,
2347 TALLOC_CTX *frame = talloc_stackframe();
2348 struct event_context *ev = NULL;
2349 struct tevent_req *req = NULL;
2350 NTSTATUS status = NT_STATUS_OK;
2352 if (cli_has_async_calls(cli)) {
2354 * Can't use sync call while an async call is in flight
2356 status = NT_STATUS_INVALID_PARAMETER;
2360 ev = event_context_init(frame);
2362 status = NT_STATUS_NO_MEMORY;
2366 req = cli_getatr_send(frame, ev, cli, fname);
2368 status = NT_STATUS_NO_MEMORY;
2372 if (!tevent_req_poll(req, ev)) {
2373 status = map_nt_error_from_unix(errno);
2377 status = cli_getatr_recv(req,
2384 if (!NT_STATUS_IS_OK(status)) {
2385 cli_set_error(cli, status);
2390 /****************************************************************************
2391 Do a SMBsetattrE call.
2392 ****************************************************************************/
2394 static void cli_setattrE_done(struct tevent_req *subreq);
2396 struct cli_setattrE_state {
2400 struct tevent_req *cli_setattrE_send(TALLOC_CTX *mem_ctx,
2401 struct event_context *ev,
2402 struct cli_state *cli,
2408 struct tevent_req *req = NULL, *subreq = NULL;
2409 struct cli_setattrE_state *state = NULL;
2410 uint8_t additional_flags = 0;
2413 req = tevent_req_create(mem_ctx, &state, struct cli_setattrE_state);
2418 memset(vwv, '\0', sizeof(vwv));
2419 SSVAL(vwv+0, 0, fnum);
2420 cli_put_dos_date2(cli, (char *)&vwv[1], 0, change_time);
2421 cli_put_dos_date2(cli, (char *)&vwv[3], 0, access_time);
2422 cli_put_dos_date2(cli, (char *)&vwv[5], 0, write_time);
2424 subreq = cli_smb_send(state, ev, cli, SMBsetattrE, additional_flags,
2426 if (tevent_req_nomem(subreq, req)) {
2427 return tevent_req_post(req, ev);
2429 tevent_req_set_callback(subreq, cli_setattrE_done, req);
2433 static void cli_setattrE_done(struct tevent_req *subreq)
2435 struct tevent_req *req = tevent_req_callback_data(
2436 subreq, struct tevent_req);
2439 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2440 TALLOC_FREE(subreq);
2441 if (!NT_STATUS_IS_OK(status)) {
2442 tevent_req_nterror(req, status);
2445 tevent_req_done(req);
2448 NTSTATUS cli_setattrE_recv(struct tevent_req *req)
2450 return tevent_req_simple_recv_ntstatus(req);
2453 NTSTATUS cli_setattrE(struct cli_state *cli,
2459 TALLOC_CTX *frame = talloc_stackframe();
2460 struct event_context *ev = NULL;
2461 struct tevent_req *req = NULL;
2462 NTSTATUS status = NT_STATUS_OK;
2464 if (cli_has_async_calls(cli)) {
2466 * Can't use sync call while an async call is in flight
2468 status = NT_STATUS_INVALID_PARAMETER;
2472 ev = event_context_init(frame);
2474 status = NT_STATUS_NO_MEMORY;
2478 req = cli_setattrE_send(frame, ev,
2486 status = NT_STATUS_NO_MEMORY;
2490 if (!tevent_req_poll(req, ev)) {
2491 status = map_nt_error_from_unix(errno);
2495 status = cli_setattrE_recv(req);
2499 if (!NT_STATUS_IS_OK(status)) {
2500 cli_set_error(cli, status);
2505 /****************************************************************************
2506 Do a SMBsetatr call.
2507 ****************************************************************************/
2509 static void cli_setatr_done(struct tevent_req *subreq);
2511 struct cli_setatr_state {
2515 struct tevent_req *cli_setatr_send(TALLOC_CTX *mem_ctx,
2516 struct event_context *ev,
2517 struct cli_state *cli,
2522 struct tevent_req *req = NULL, *subreq = NULL;
2523 struct cli_setatr_state *state = NULL;
2524 uint8_t additional_flags = 0;
2526 uint8_t *bytes = NULL;
2528 req = tevent_req_create(mem_ctx, &state, struct cli_setatr_state);
2533 memset(vwv, '\0', sizeof(vwv));
2534 SSVAL(vwv+0, 0, attr);
2535 cli_put_dos_date3(cli, (char *)&vwv[1], 0, mtime);
2537 bytes = talloc_array(state, uint8_t, 1);
2538 if (tevent_req_nomem(bytes, req)) {
2539 return tevent_req_post(req, ev);
2542 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2543 strlen(fname)+1, NULL);
2544 if (tevent_req_nomem(bytes, req)) {
2545 return tevent_req_post(req, ev);
2547 bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
2548 talloc_get_size(bytes)+1);
2549 if (tevent_req_nomem(bytes, req)) {
2550 return tevent_req_post(req, ev);
2553 bytes[talloc_get_size(bytes)-1] = 4;
2554 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "",
2556 if (tevent_req_nomem(bytes, req)) {
2557 return tevent_req_post(req, ev);
2560 subreq = cli_smb_send(state, ev, cli, SMBsetatr, additional_flags,
2561 8, vwv, talloc_get_size(bytes), bytes);
2562 if (tevent_req_nomem(subreq, req)) {
2563 return tevent_req_post(req, ev);
2565 tevent_req_set_callback(subreq, cli_setatr_done, req);
2569 static void cli_setatr_done(struct tevent_req *subreq)
2571 struct tevent_req *req = tevent_req_callback_data(
2572 subreq, struct tevent_req);
2575 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2576 TALLOC_FREE(subreq);
2577 if (!NT_STATUS_IS_OK(status)) {
2578 tevent_req_nterror(req, status);
2581 tevent_req_done(req);
2584 NTSTATUS cli_setatr_recv(struct tevent_req *req)
2586 return tevent_req_simple_recv_ntstatus(req);
2589 NTSTATUS cli_setatr(struct cli_state *cli,
2594 TALLOC_CTX *frame = talloc_stackframe();
2595 struct event_context *ev = NULL;
2596 struct tevent_req *req = NULL;
2597 NTSTATUS status = NT_STATUS_OK;
2599 if (cli_has_async_calls(cli)) {
2601 * Can't use sync call while an async call is in flight
2603 status = NT_STATUS_INVALID_PARAMETER;
2607 ev = event_context_init(frame);
2609 status = NT_STATUS_NO_MEMORY;
2613 req = cli_setatr_send(frame, ev, cli, fname, attr, mtime);
2615 status = NT_STATUS_NO_MEMORY;
2619 if (!tevent_req_poll(req, ev)) {
2620 status = map_nt_error_from_unix(errno);
2624 status = cli_setatr_recv(req);
2628 if (!NT_STATUS_IS_OK(status)) {
2629 cli_set_error(cli, status);
2635 bool cli_setatr(struct cli_state *cli, const char *fname, uint16_t attr, time_t t)
2639 memset(cli->outbuf,'\0',smb_size);
2640 memset(cli->inbuf,'\0',smb_size);
2642 cli_set_message(cli->outbuf,8,0,True);
2644 SCVAL(cli->outbuf,smb_com,SMBsetatr);
2645 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2646 cli_setup_packet(cli);
2648 SSVAL(cli->outbuf,smb_vwv0, attr);
2649 cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
2651 p = smb_buf(cli->outbuf);
2653 p += clistr_push(cli, p, fname,
2654 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2657 cli_setup_bcc(cli, p);
2660 if (!cli_receive_smb(cli)) {
2664 if (cli_is_error(cli)) {
2672 /****************************************************************************
2673 Check for existance of a dir.
2674 ****************************************************************************/
2676 static void cli_chkpath_done(struct tevent_req *subreq);
2678 struct cli_chkpath_state {
2682 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
2683 struct event_context *ev,
2684 struct cli_state *cli,
2687 struct tevent_req *req = NULL, *subreq = NULL;
2688 struct cli_chkpath_state *state = NULL;
2689 uint8_t additional_flags = 0;
2690 uint8_t *bytes = NULL;
2692 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
2697 bytes = talloc_array(state, uint8_t, 1);
2698 if (tevent_req_nomem(bytes, req)) {
2699 return tevent_req_post(req, ev);
2702 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2703 strlen(fname)+1, NULL);
2705 if (tevent_req_nomem(bytes, req)) {
2706 return tevent_req_post(req, ev);
2709 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
2710 0, NULL, talloc_get_size(bytes), bytes);
2711 if (tevent_req_nomem(subreq, req)) {
2712 return tevent_req_post(req, ev);
2714 tevent_req_set_callback(subreq, cli_chkpath_done, req);
2718 static void cli_chkpath_done(struct tevent_req *subreq)
2720 struct tevent_req *req = tevent_req_callback_data(
2721 subreq, struct tevent_req);
2724 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2725 TALLOC_FREE(subreq);
2726 if (!NT_STATUS_IS_OK(status)) {
2727 tevent_req_nterror(req, status);
2730 tevent_req_done(req);
2733 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
2735 return tevent_req_simple_recv_ntstatus(req);
2738 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
2740 TALLOC_CTX *frame = talloc_stackframe();
2741 struct event_context *ev = NULL;
2742 struct tevent_req *req = NULL;
2744 NTSTATUS status = NT_STATUS_OK;
2746 if (cli_has_async_calls(cli)) {
2748 * Can't use sync call while an async call is in flight
2750 status = NT_STATUS_INVALID_PARAMETER;
2754 path2 = talloc_strdup(frame, path);
2756 status = NT_STATUS_NO_MEMORY;
2759 trim_char(path2,'\0','\\');
2761 path2 = talloc_strdup(frame, "\\");
2763 status = NT_STATUS_NO_MEMORY;
2768 ev = event_context_init(frame);
2770 status = NT_STATUS_NO_MEMORY;
2774 req = cli_chkpath_send(frame, ev, cli, path2);
2776 status = NT_STATUS_NO_MEMORY;
2780 if (!tevent_req_poll(req, ev)) {
2781 status = map_nt_error_from_unix(errno);
2785 status = cli_chkpath_recv(req);
2789 if (!NT_STATUS_IS_OK(status)) {
2790 cli_set_error(cli, status);
2795 /****************************************************************************
2797 ****************************************************************************/
2799 static void cli_dskattr_done(struct tevent_req *subreq);
2801 struct cli_dskattr_state {
2807 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
2808 struct event_context *ev,
2809 struct cli_state *cli)
2811 struct tevent_req *req = NULL, *subreq = NULL;
2812 struct cli_dskattr_state *state = NULL;
2813 uint8_t additional_flags = 0;
2815 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
2820 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
2822 if (tevent_req_nomem(subreq, req)) {
2823 return tevent_req_post(req, ev);
2825 tevent_req_set_callback(subreq, cli_dskattr_done, req);
2829 static void cli_dskattr_done(struct tevent_req *subreq)
2831 struct tevent_req *req = tevent_req_callback_data(
2832 subreq, struct tevent_req);
2833 struct cli_dskattr_state *state = tevent_req_data(
2834 req, struct cli_dskattr_state);
2836 uint16_t *vwv = NULL;
2839 status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
2840 if (!NT_STATUS_IS_OK(status)) {
2841 tevent_req_nterror(req, status);
2844 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
2845 state->total = SVAL(vwv+0, 0);
2846 state->avail = SVAL(vwv+3, 0);
2847 TALLOC_FREE(subreq);
2848 tevent_req_done(req);
2851 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
2853 struct cli_dskattr_state *state = tevent_req_data(
2854 req, struct cli_dskattr_state);
2857 if (tevent_req_is_nterror(req, &status)) {
2860 *bsize = state->bsize;
2861 *total = state->total;
2862 *avail = state->avail;
2863 return NT_STATUS_OK;
2866 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
2868 TALLOC_CTX *frame = talloc_stackframe();
2869 struct event_context *ev = NULL;
2870 struct tevent_req *req = NULL;
2871 NTSTATUS status = NT_STATUS_OK;
2873 if (cli_has_async_calls(cli)) {
2875 * Can't use sync call while an async call is in flight
2877 status = NT_STATUS_INVALID_PARAMETER;
2881 ev = event_context_init(frame);
2883 status = NT_STATUS_NO_MEMORY;
2887 req = cli_dskattr_send(frame, ev, cli);
2889 status = NT_STATUS_NO_MEMORY;
2893 if (!tevent_req_poll(req, ev)) {
2894 status = map_nt_error_from_unix(errno);
2898 status = cli_dskattr_recv(req, bsize, total, avail);
2902 if (!NT_STATUS_IS_OK(status)) {
2903 cli_set_error(cli, status);
2908 /****************************************************************************
2909 Create and open a temporary file.
2910 ****************************************************************************/
2912 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
2917 memset(cli->outbuf,'\0',smb_size);
2918 memset(cli->inbuf,'\0',smb_size);
2920 cli_set_message(cli->outbuf,3,0,True);
2922 SCVAL(cli->outbuf,smb_com,SMBctemp);
2923 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2924 cli_setup_packet(cli);
2926 SSVAL(cli->outbuf,smb_vwv0,0);
2927 SIVALS(cli->outbuf,smb_vwv1,-1);
2929 p = smb_buf(cli->outbuf);
2931 p += clistr_push(cli, p, path,
2932 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2934 cli_setup_bcc(cli, p);
2937 if (!cli_receive_smb(cli)) {
2941 if (cli_is_error(cli)) {
2945 /* despite the spec, the result has a -1, followed by
2946 length, followed by name */
2947 p = smb_buf(cli->inbuf);
2949 len = smb_buflen(cli->inbuf) - 4;
2950 if (len <= 0 || len > PATH_MAX) return -1;
2953 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
2957 clistr_pull(cli->inbuf, path2, p,
2958 len+1, len, STR_ASCII);
2962 return SVAL(cli->inbuf,smb_vwv0);
2966 send a raw ioctl - used by the torture code
2968 NTSTATUS cli_raw_ioctl(struct cli_state *cli, uint16_t fnum, uint32_t code, DATA_BLOB *blob)
2970 memset(cli->outbuf,'\0',smb_size);
2971 memset(cli->inbuf,'\0',smb_size);
2973 cli_set_message(cli->outbuf, 3, 0, True);
2974 SCVAL(cli->outbuf,smb_com,SMBioctl);
2975 cli_setup_packet(cli);
2977 SSVAL(cli->outbuf, smb_vwv0, fnum);
2978 SSVAL(cli->outbuf, smb_vwv1, code>>16);
2979 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
2982 if (!cli_receive_smb(cli)) {
2983 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2986 if (cli_is_error(cli)) {
2987 return cli_nt_error(cli);
2990 *blob = data_blob_null;
2992 return NT_STATUS_OK;
2995 /*********************************************************
2996 Set an extended attribute utility fn.
2997 *********************************************************/
2999 static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsigned int param_len,
3000 const char *ea_name, const char *ea_val, size_t ea_len)
3002 unsigned int data_len = 0;
3004 char *rparam=NULL, *rdata=NULL;
3006 size_t ea_namelen = strlen(ea_name);
3008 if (ea_namelen == 0 && ea_len == 0) {
3010 data = (char *)SMB_MALLOC(data_len);
3015 SIVAL(p,0,data_len);
3017 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
3018 data = (char *)SMB_MALLOC(data_len);
3023 SIVAL(p,0,data_len);
3025 SCVAL(p, 0, 0); /* EA flags. */
3026 SCVAL(p, 1, ea_namelen);
3027 SSVAL(p, 2, ea_len);
3028 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
3029 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
3032 if (!cli_send_trans(cli, SMBtrans2,
3034 -1, 0, /* fid, flags */
3035 &setup, 1, 0, /* setup, length, max */
3036 param, param_len, 2, /* param, length, max */
3037 data, data_len, cli->max_xmit /* data, length, max */
3043 if (!cli_receive_trans(cli, SMBtrans2,
3044 &rparam, ¶m_len,
3045 &rdata, &data_len)) {
3057 /*********************************************************
3058 Set an extended attribute on a pathname.
3059 *********************************************************/
3061 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
3063 uint16_t setup = TRANSACT2_SETPATHINFO;
3064 unsigned int param_len = 0;
3066 size_t srclen = 2*(strlen(path)+1);
3070 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
3074 memset(param, '\0', 6);
3075 SSVAL(param,0,SMB_INFO_SET_EA);
3078 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
3079 param_len = PTR_DIFF(p, param);
3081 ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
3086 /*********************************************************
3087 Set an extended attribute on an fnum.
3088 *********************************************************/
3090 bool cli_set_ea_fnum(struct cli_state *cli, uint16_t fnum, const char *ea_name, const char *ea_val, size_t ea_len)
3093 uint16_t setup = TRANSACT2_SETFILEINFO;
3095 memset(param, 0, 6);
3096 SSVAL(param,0,fnum);
3097 SSVAL(param,2,SMB_INFO_SET_EA);
3099 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
3102 /*********************************************************
3103 Get an extended attribute list utility fn.
3104 *********************************************************/
3106 static bool cli_get_ea_list(struct cli_state *cli,
3107 uint16_t setup, char *param, unsigned int param_len,
3110 struct ea_struct **pea_list)
3112 unsigned int data_len = 0;
3113 unsigned int rparam_len, rdata_len;
3114 char *rparam=NULL, *rdata=NULL;
3119 struct ea_struct *ea_list;
3126 if (!cli_send_trans(cli, SMBtrans2,
3128 -1, 0, /* fid, flags */
3129 &setup, 1, 0, /* setup, length, max */
3130 param, param_len, 10, /* param, length, max */
3131 NULL, data_len, cli->max_xmit /* data, length, max */
3136 if (!cli_receive_trans(cli, SMBtrans2,
3137 &rparam, &rparam_len,
3138 &rdata, &rdata_len)) {
3142 if (!rdata || rdata_len < 4) {
3146 ea_size = (size_t)IVAL(rdata,0);
3147 if (ea_size > rdata_len) {
3152 /* No EA's present. */
3160 /* Validate the EA list and count it. */
3161 for (num_eas = 0; ea_size >= 4; num_eas++) {
3162 unsigned int ea_namelen = CVAL(p,1);
3163 unsigned int ea_valuelen = SVAL(p,2);
3164 if (ea_namelen == 0) {
3167 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
3170 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
3171 p += 4 + ea_namelen + 1 + ea_valuelen;
3179 *pnum_eas = num_eas;
3181 /* Caller only wants number of EA's. */
3186 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
3191 ea_size = (size_t)IVAL(rdata,0);
3194 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
3195 struct ea_struct *ea = &ea_list[num_eas];
3196 fstring unix_ea_name;
3197 unsigned int ea_namelen = CVAL(p,1);
3198 unsigned int ea_valuelen = SVAL(p,2);
3200 ea->flags = CVAL(p,0);
3201 unix_ea_name[0] = '\0';
3202 pull_ascii_fstring(unix_ea_name, p + 4);
3203 ea->name = talloc_strdup(ctx, unix_ea_name);
3204 /* Ensure the value is null terminated (in case it's a string). */
3205 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
3206 if (!ea->value.data) {
3210 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
3212 ea->value.data[ea_valuelen] = 0;
3214 p += 4 + ea_namelen + 1 + ea_valuelen;
3217 *pea_list = ea_list;
3227 /*********************************************************
3228 Get an extended attribute list from a pathname.
3229 *********************************************************/
3231 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
3234 struct ea_struct **pea_list)
3236 uint16_t setup = TRANSACT2_QPATHINFO;
3237 unsigned int param_len = 0;
3240 size_t srclen = 2*(strlen(path)+1);
3243 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
3249 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
3251 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
3252 param_len = PTR_DIFF(p, param);
3254 ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
3259 /*********************************************************
3260 Get an extended attribute list from an fnum.
3261 *********************************************************/
3263 bool cli_get_ea_list_fnum(struct cli_state *cli, uint16_t fnum,
3266 struct ea_struct **pea_list)
3268 uint16_t setup = TRANSACT2_QFILEINFO;
3271 memset(param, 0, 6);
3272 SSVAL(param,0,fnum);
3273 SSVAL(param,2,SMB_INFO_SET_EA);
3275 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
3278 /****************************************************************************
3279 Convert open "flags" arg to uint32_t on wire.
3280 ****************************************************************************/
3282 static uint32_t open_flags_to_wire(int flags)
3284 int open_mode = flags & O_ACCMODE;
3287 switch (open_mode) {
3289 ret |= SMB_O_WRONLY;
3296 ret |= SMB_O_RDONLY;
3300 if (flags & O_CREAT) {
3303 if (flags & O_EXCL) {
3306 if (flags & O_TRUNC) {
3310 if (flags & O_SYNC) {
3314 if (flags & O_APPEND) {
3315 ret |= SMB_O_APPEND;
3317 #if defined(O_DIRECT)
3318 if (flags & O_DIRECT) {
3319 ret |= SMB_O_DIRECT;
3322 #if defined(O_DIRECTORY)
3323 if (flags & O_DIRECTORY) {
3324 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
3325 ret |= SMB_O_DIRECTORY;
3331 /****************************************************************************
3332 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
3333 ****************************************************************************/
3335 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
3337 unsigned int data_len = 0;
3338 unsigned int param_len = 0;
3339 uint16_t setup = TRANSACT2_SETPATHINFO;
3342 char *rparam=NULL, *rdata=NULL;
3344 uint16_t fnum = (uint16_t)-1;
3345 uint32_t wire_flags = open_flags_to_wire(flags);
3346 size_t srclen = 2*(strlen(fname)+1);
3348 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
3352 memset(param, '\0', 6);
3353 SSVAL(param,0, SMB_POSIX_PATH_OPEN);
3356 p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
3357 param_len = PTR_DIFF(p, param);
3360 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
3361 wire_flags |= SMB_O_DIRECTORY;
3365 SIVAL(p,0,0); /* No oplock. */
3366 SIVAL(p,4,wire_flags);
3367 SIVAL(p,8,unix_perms_to_wire(mode));
3368 SIVAL(p,12,0); /* Top bits of perms currently undefined. */
3369 SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
3373 if (!cli_send_trans(cli, SMBtrans2,
3375 -1, 0, /* fid, flags */
3376 &setup, 1, 0, /* setup, length, max */
3377 param, param_len, 0, /* param, length, max */
3378 (char *)&data, data_len, cli->max_xmit /* data, length, max */
3386 if (!cli_receive_trans(cli, SMBtrans2,
3387 &rparam, ¶m_len,
3388 &rdata, &data_len)) {
3392 fnum = SVAL(rdata,2);
3400 /****************************************************************************
3401 open - POSIX semantics.
3402 ****************************************************************************/
3404 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
3406 return cli_posix_open_internal(cli, fname, flags, mode, False);
3409 /****************************************************************************
3410 mkdir - POSIX semantics.
3411 ****************************************************************************/
3413 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
3415 return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
3418 /****************************************************************************
3419 unlink or rmdir - POSIX semantics.
3420 ****************************************************************************/
3422 struct unlink_state {
3426 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
3428 struct tevent_req *req = tevent_req_callback_data(
3429 subreq, struct tevent_req);
3430 struct unlink_state *state = tevent_req_data(req, struct unlink_state);
3433 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, NULL, NULL);
3434 TALLOC_FREE(subreq);
3435 if (!NT_STATUS_IS_OK(status)) {
3436 tevent_req_nterror(req, status);
3439 tevent_req_done(req);
3442 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
3443 struct event_context *ev,
3444 struct cli_state *cli,
3448 struct tevent_req *req = NULL, *subreq = NULL;
3449 struct unlink_state *state = NULL;
3451 uint8_t *param = NULL;
3454 req = tevent_req_create(mem_ctx, &state, struct unlink_state);
3459 /* Setup setup word. */
3460 SSVAL(&setup+0, 0, TRANSACT2_SETPATHINFO);
3462 /* Setup param array. */
3463 param = talloc_array(state, uint8_t, 6);
3464 if (tevent_req_nomem(data, req)) {
3465 return tevent_req_post(req, ev);
3467 memset(param, '\0', 6);
3468 SSVAL(param, 0, SMB_POSIX_PATH_UNLINK);
3470 param = trans2_bytes_push_str(param, cli_ucs2(cli), fname,
3471 strlen(fname)+1, NULL);
3473 if (tevent_req_nomem(param, req)) {
3474 return tevent_req_post(req, ev);
3477 /* Setup data word. */
3478 SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
3479 SMB_POSIX_UNLINK_FILE_TARGET);
3481 subreq = cli_trans_send(state, /* mem ctx. */
3482 ev, /* event ctx. */
3483 cli, /* cli_state. */
3484 SMBtrans2, /* cmd. */
3485 NULL, /* pipe name. */
3489 &setup, /* setup. */
3490 1, /* num setup uint16_t words. */
3491 0, /* max returned setup. */
3493 talloc_get_size(param), /* num param. */
3494 2, /* max returned param. */
3497 0); /* max returned data. */
3499 if (tevent_req_nomem(subreq, req)) {
3500 return tevent_req_post(req, ev);
3502 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
3506 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
3507 struct event_context *ev,
3508 struct cli_state *cli,
3511 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, false);
3514 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
3518 if (tevent_req_is_nterror(req, &status)) {
3521 return NT_STATUS_OK;
3524 /****************************************************************************
3525 unlink - POSIX semantics.
3526 ****************************************************************************/
3528 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
3530 TALLOC_CTX *frame = talloc_stackframe();
3531 struct event_context *ev = NULL;
3532 struct tevent_req *req = NULL;
3533 NTSTATUS status = NT_STATUS_OK;
3535 if (cli_has_async_calls(cli)) {
3537 * Can't use sync call while an async call is in flight
3539 status = NT_STATUS_INVALID_PARAMETER;
3543 ev = event_context_init(frame);
3545 status = NT_STATUS_NO_MEMORY;
3549 req = cli_posix_unlink_send(frame,
3554 status = NT_STATUS_NO_MEMORY;
3558 if (!tevent_req_poll(req, ev)) {
3559 status = map_nt_error_from_unix(errno);
3563 status = cli_posix_unlink_recv(req, frame);
3567 if (!NT_STATUS_IS_OK(status)) {
3568 cli_set_error(cli, status);
3573 /****************************************************************************
3574 rmdir - POSIX semantics.
3575 ****************************************************************************/
3577 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
3578 struct event_context *ev,
3579 struct cli_state *cli,
3582 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, true);
3585 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
3589 if (tevent_req_is_nterror(req, &status)) {
3592 return NT_STATUS_OK;
3595 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
3597 TALLOC_CTX *frame = talloc_stackframe();
3598 struct event_context *ev = NULL;
3599 struct tevent_req *req = NULL;
3600 NTSTATUS status = NT_STATUS_OK;
3602 if (cli_has_async_calls(cli)) {
3604 * Can't use sync call while an async call is in flight
3606 status = NT_STATUS_INVALID_PARAMETER;
3610 ev = event_context_init(frame);
3612 status = NT_STATUS_NO_MEMORY;
3616 req = cli_posix_rmdir_send(frame,
3621 status = NT_STATUS_NO_MEMORY;
3625 if (!tevent_req_poll(req, ev)) {
3626 status = map_nt_error_from_unix(errno);
3630 status = cli_posix_rmdir_recv(req, frame);
3634 if (!NT_STATUS_IS_OK(status)) {
3635 cli_set_error(cli, status);