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 bool cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
559 memset(cli->outbuf,'\0',smb_size);
560 memset(cli->inbuf,'\0',smb_size);
562 cli_set_message(cli->outbuf, 4, 0, true);
564 SCVAL(cli->outbuf,smb_com,SMBntrename);
565 SSVAL(cli->outbuf,smb_tid,cli->cnum);
566 cli_setup_packet(cli);
568 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
569 SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_RENAME);
571 p = smb_buf(cli->outbuf);
573 p += clistr_push(cli, p, fname_src,
574 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
576 p += clistr_push(cli, p, fname_dst,
577 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
579 cli_setup_bcc(cli, p);
582 if (!cli_receive_smb(cli)) {
586 if (cli_is_error(cli)) {
593 /****************************************************************************
595 ****************************************************************************/
597 bool cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
601 memset(cli->outbuf,'\0',smb_size);
602 memset(cli->inbuf,'\0',smb_size);
604 cli_set_message(cli->outbuf, 4, 0, true);
606 SCVAL(cli->outbuf,smb_com,SMBntrename);
607 SSVAL(cli->outbuf,smb_tid,cli->cnum);
608 cli_setup_packet(cli);
610 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
611 SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_HARD_LINK);
613 p = smb_buf(cli->outbuf);
615 p += clistr_push(cli, p, fname_src,
616 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
618 p += clistr_push(cli, p, fname_dst,
619 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
621 cli_setup_bcc(cli, p);
624 if (!cli_receive_smb(cli)) {
628 if (cli_is_error(cli)) {
635 /****************************************************************************
637 ****************************************************************************/
639 bool cli_unlink_full(struct cli_state *cli, const char *fname, uint16_t attrs)
643 memset(cli->outbuf,'\0',smb_size);
644 memset(cli->inbuf,'\0',smb_size);
646 cli_set_message(cli->outbuf,1, 0, true);
648 SCVAL(cli->outbuf,smb_com,SMBunlink);
649 SSVAL(cli->outbuf,smb_tid,cli->cnum);
650 cli_setup_packet(cli);
652 SSVAL(cli->outbuf,smb_vwv0, attrs);
654 p = smb_buf(cli->outbuf);
656 p += clistr_push(cli, p, fname,
657 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
659 cli_setup_bcc(cli, p);
661 if (!cli_receive_smb(cli)) {
665 if (cli_is_error(cli)) {
672 /****************************************************************************
674 ****************************************************************************/
676 bool cli_unlink(struct cli_state *cli, const char *fname)
678 return cli_unlink_full(cli, fname, aSYSTEM | aHIDDEN);
681 /****************************************************************************
683 ****************************************************************************/
685 static void cli_mkdir_done(struct tevent_req *subreq);
687 struct cli_mkdir_state {
691 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
692 struct event_context *ev,
693 struct cli_state *cli,
696 struct tevent_req *req = NULL, *subreq = NULL;
697 struct cli_mkdir_state *state = NULL;
698 uint8_t additional_flags = 0;
699 uint8_t *bytes = NULL;
701 req = tevent_req_create(mem_ctx, &state, struct cli_mkdir_state);
706 bytes = talloc_array(state, uint8_t, 1);
707 if (tevent_req_nomem(bytes, req)) {
708 return tevent_req_post(req, ev);
711 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
712 strlen(dname)+1, NULL);
714 if (tevent_req_nomem(bytes, req)) {
715 return tevent_req_post(req, ev);
718 subreq = cli_smb_send(state, ev, cli, SMBmkdir, additional_flags,
719 0, NULL, talloc_get_size(bytes), bytes);
720 if (tevent_req_nomem(subreq, req)) {
721 return tevent_req_post(req, ev);
723 tevent_req_set_callback(subreq, cli_mkdir_done, req);
727 static void cli_mkdir_done(struct tevent_req *subreq)
729 struct tevent_req *req = tevent_req_callback_data(
730 subreq, struct tevent_req);
733 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
735 if (!NT_STATUS_IS_OK(status)) {
736 tevent_req_nterror(req, status);
739 tevent_req_done(req);
742 NTSTATUS cli_mkdir_recv(struct tevent_req *req)
744 return tevent_req_simple_recv_ntstatus(req);
747 NTSTATUS cli_mkdir(struct cli_state *cli, const char *dname)
749 TALLOC_CTX *frame = talloc_stackframe();
750 struct event_context *ev;
751 struct tevent_req *req;
752 NTSTATUS status = NT_STATUS_OK;
754 if (cli_has_async_calls(cli)) {
756 * Can't use sync call while an async call is in flight
758 status = NT_STATUS_INVALID_PARAMETER;
762 ev = event_context_init(frame);
764 status = NT_STATUS_NO_MEMORY;
768 req = cli_mkdir_send(frame, ev, cli, dname);
770 status = NT_STATUS_NO_MEMORY;
774 if (!tevent_req_poll(req, ev)) {
775 status = map_nt_error_from_unix(errno);
779 status = cli_mkdir_recv(req);
783 if (!NT_STATUS_IS_OK(status)) {
784 cli_set_error(cli, status);
789 /****************************************************************************
791 ****************************************************************************/
793 static void cli_rmdir_done(struct tevent_req *subreq);
795 struct cli_rmdir_state {
799 struct tevent_req *cli_rmdir_send(TALLOC_CTX *mem_ctx,
800 struct event_context *ev,
801 struct cli_state *cli,
804 struct tevent_req *req = NULL, *subreq = NULL;
805 struct cli_rmdir_state *state = NULL;
806 uint8_t additional_flags = 0;
807 uint8_t *bytes = NULL;
809 req = tevent_req_create(mem_ctx, &state, struct cli_rmdir_state);
814 bytes = talloc_array(state, uint8_t, 1);
815 if (tevent_req_nomem(bytes, req)) {
816 return tevent_req_post(req, ev);
819 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), dname,
820 strlen(dname)+1, NULL);
822 if (tevent_req_nomem(bytes, req)) {
823 return tevent_req_post(req, ev);
826 subreq = cli_smb_send(state, ev, cli, SMBrmdir, additional_flags,
827 0, NULL, talloc_get_size(bytes), bytes);
828 if (tevent_req_nomem(subreq, req)) {
829 return tevent_req_post(req, ev);
831 tevent_req_set_callback(subreq, cli_rmdir_done, req);
835 static void cli_rmdir_done(struct tevent_req *subreq)
837 struct tevent_req *req = tevent_req_callback_data(
838 subreq, struct tevent_req);
841 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
843 if (!NT_STATUS_IS_OK(status)) {
844 tevent_req_nterror(req, status);
847 tevent_req_done(req);
850 NTSTATUS cli_rmdir_recv(struct tevent_req *req)
852 return tevent_req_simple_recv_ntstatus(req);
855 NTSTATUS cli_rmdir(struct cli_state *cli, const char *dname)
857 TALLOC_CTX *frame = talloc_stackframe();
858 struct event_context *ev;
859 struct tevent_req *req;
860 NTSTATUS status = NT_STATUS_OK;
862 if (cli_has_async_calls(cli)) {
864 * Can't use sync call while an async call is in flight
866 status = NT_STATUS_INVALID_PARAMETER;
870 ev = event_context_init(frame);
872 status = NT_STATUS_NO_MEMORY;
876 req = cli_rmdir_send(frame, ev, cli, dname);
878 status = NT_STATUS_NO_MEMORY;
882 if (!tevent_req_poll(req, ev)) {
883 status = map_nt_error_from_unix(errno);
887 status = cli_rmdir_recv(req);
891 if (!NT_STATUS_IS_OK(status)) {
892 cli_set_error(cli, status);
897 /****************************************************************************
898 Set or clear the delete on close flag.
899 ****************************************************************************/
901 int cli_nt_delete_on_close(struct cli_state *cli, int fnum, bool flag)
903 unsigned int data_len = 1;
904 unsigned int param_len = 6;
905 uint16_t setup = TRANSACT2_SETFILEINFO;
908 char *rparam=NULL, *rdata=NULL;
910 memset(param, 0, param_len);
912 SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
916 if (!cli_send_trans(cli, SMBtrans2,
918 -1, 0, /* fid, flags */
919 &setup, 1, 0, /* setup, length, max */
920 param, param_len, 2, /* param, length, max */
921 (char *)&data, data_len, cli->max_xmit /* data, length, max */
926 if (!cli_receive_trans(cli, SMBtrans2,
928 &rdata, &data_len)) {
938 /****************************************************************************
939 Open a file - exposing the full horror of the NT API :-).
941 ****************************************************************************/
943 int cli_nt_create_full(struct cli_state *cli, const char *fname,
944 uint32_t CreatFlags, uint32_t DesiredAccess,
945 uint32_t FileAttributes, uint32_t ShareAccess,
946 uint32_t CreateDisposition, uint32_t CreateOptions,
947 uint8_t SecurityFlags)
952 memset(cli->outbuf,'\0',smb_size);
953 memset(cli->inbuf,'\0',smb_size);
955 cli_set_message(cli->outbuf,24,0, true);
957 SCVAL(cli->outbuf,smb_com,SMBntcreateX);
958 SSVAL(cli->outbuf,smb_tid,cli->cnum);
959 cli_setup_packet(cli);
961 SSVAL(cli->outbuf,smb_vwv0,0xFF);
962 if (cli->use_oplocks)
963 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
965 SIVAL(cli->outbuf,smb_ntcreate_Flags, CreatFlags);
966 SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
967 SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, DesiredAccess);
968 SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, FileAttributes);
969 SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, ShareAccess);
970 SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, CreateDisposition);
971 SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, CreateOptions);
972 SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
973 SCVAL(cli->outbuf,smb_ntcreate_SecurityFlags, SecurityFlags);
975 p = smb_buf(cli->outbuf);
976 /* this alignment and termination is critical for netapp filers. Don't change */
977 p += clistr_align_out(cli, p, 0);
978 len = clistr_push(cli, p, fname,
979 cli->bufsize - PTR_DIFF(p,cli->outbuf), 0);
981 SSVAL(cli->outbuf,smb_ntcreate_NameLength, len);
982 /* sigh. this copes with broken netapp filer behaviour */
983 p += clistr_push(cli, p, "",
984 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
986 cli_setup_bcc(cli, p);
989 if (!cli_receive_smb(cli)) {
993 if (cli_is_error(cli)) {
997 return SVAL(cli->inbuf,smb_vwv2 + 1);
1000 struct cli_ntcreate_state {
1005 static void cli_ntcreate_done(struct tevent_req *subreq);
1007 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
1008 struct event_context *ev,
1009 struct cli_state *cli,
1011 uint32_t CreatFlags,
1012 uint32_t DesiredAccess,
1013 uint32_t FileAttributes,
1014 uint32_t ShareAccess,
1015 uint32_t CreateDisposition,
1016 uint32_t CreateOptions,
1017 uint8_t SecurityFlags)
1019 struct tevent_req *req, *subreq;
1020 struct cli_ntcreate_state *state;
1023 size_t converted_len;
1025 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
1031 SCVAL(vwv+0, 0, 0xFF);
1036 if (cli->use_oplocks) {
1037 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
1039 SIVAL(vwv+3, 1, CreatFlags);
1040 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
1041 SIVAL(vwv+7, 1, DesiredAccess);
1042 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
1043 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
1044 SIVAL(vwv+13, 1, FileAttributes);
1045 SIVAL(vwv+15, 1, ShareAccess);
1046 SIVAL(vwv+17, 1, CreateDisposition);
1047 SIVAL(vwv+19, 1, CreateOptions);
1048 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
1049 SCVAL(vwv+23, 1, SecurityFlags);
1051 bytes = talloc_array(state, uint8_t, 0);
1052 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
1053 fname, strlen(fname)+1,
1056 /* sigh. this copes with broken netapp filer behaviour */
1057 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
1059 if (tevent_req_nomem(bytes, req)) {
1060 return tevent_req_post(req, ev);
1063 SIVAL(vwv+2, 1, converted_len);
1065 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
1066 talloc_get_size(bytes), bytes);
1067 if (tevent_req_nomem(subreq, req)) {
1068 return tevent_req_post(req, ev);
1070 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
1074 static void cli_ntcreate_done(struct tevent_req *subreq)
1076 struct tevent_req *req = tevent_req_callback_data(
1077 subreq, struct tevent_req);
1078 struct cli_ntcreate_state *state = tevent_req_data(
1079 req, struct cli_ntcreate_state);
1086 status = cli_smb_recv(subreq, 3, &wct, &vwv, &num_bytes, &bytes);
1087 if (!NT_STATUS_IS_OK(status)) {
1088 TALLOC_FREE(subreq);
1089 tevent_req_nterror(req, status);
1092 state->fnum = SVAL(vwv+2, 1);
1093 tevent_req_done(req);
1096 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
1098 struct cli_ntcreate_state *state = tevent_req_data(
1099 req, struct cli_ntcreate_state);
1102 if (tevent_req_is_nterror(req, &status)) {
1105 *pfnum = state->fnum;
1106 return NT_STATUS_OK;
1109 NTSTATUS cli_ntcreate(struct cli_state *cli,
1111 uint32_t CreatFlags,
1112 uint32_t DesiredAccess,
1113 uint32_t FileAttributes,
1114 uint32_t ShareAccess,
1115 uint32_t CreateDisposition,
1116 uint32_t CreateOptions,
1117 uint8_t SecurityFlags,
1120 TALLOC_CTX *frame = talloc_stackframe();
1121 struct event_context *ev;
1122 struct tevent_req *req;
1123 NTSTATUS status = NT_STATUS_OK;
1125 if (cli_has_async_calls(cli)) {
1127 * Can't use sync call while an async call is in flight
1129 status = NT_STATUS_INVALID_PARAMETER;
1133 ev = event_context_init(frame);
1135 status = NT_STATUS_NO_MEMORY;
1139 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
1140 DesiredAccess, FileAttributes, ShareAccess,
1141 CreateDisposition, CreateOptions,
1144 status = NT_STATUS_NO_MEMORY;
1148 if (!tevent_req_poll(req, ev)) {
1149 status = map_nt_error_from_unix(errno);
1153 status = cli_ntcreate_recv(req, pfid);
1156 if (!NT_STATUS_IS_OK(status)) {
1157 cli_set_error(cli, status);
1162 /****************************************************************************
1164 ****************************************************************************/
1166 int cli_nt_create(struct cli_state *cli, const char *fname, uint32_t DesiredAccess)
1168 return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
1169 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0);
1172 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
1173 const char *str, size_t str_len,
1174 size_t *pconverted_size)
1178 size_t converted_size;
1184 buflen = talloc_get_size(buf);
1186 * We're pushing into an SMB buffer, align odd
1188 if (ucs2 && (buflen % 2 == 0)) {
1189 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
1197 if (!convert_string_talloc(talloc_tos(), CH_UNIX,
1198 ucs2 ? CH_UTF16LE : CH_DOS,
1199 str, str_len, &converted,
1200 &converted_size, true)) {
1204 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
1205 buflen + converted_size);
1207 TALLOC_FREE(converted);
1211 memcpy(buf + buflen, converted, converted_size);
1213 TALLOC_FREE(converted);
1215 if (pconverted_size) {
1216 *pconverted_size = converted_size;
1222 /****************************************************************************
1224 WARNING: if you open with O_WRONLY then getattrE won't work!
1225 ****************************************************************************/
1227 struct cli_open_state {
1233 static void cli_open_done(struct tevent_req *subreq);
1235 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
1236 struct event_context *ev,
1237 struct cli_state *cli, const char *fname,
1238 int flags, int share_mode,
1239 struct tevent_req **psmbreq)
1241 struct tevent_req *req, *subreq;
1242 struct cli_open_state *state;
1244 unsigned accessmode;
1245 uint8_t additional_flags;
1248 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
1254 if (flags & O_CREAT) {
1257 if (!(flags & O_EXCL)) {
1258 if (flags & O_TRUNC)
1264 accessmode = (share_mode<<4);
1266 if ((flags & O_ACCMODE) == O_RDWR) {
1268 } else if ((flags & O_ACCMODE) == O_WRONLY) {
1273 if ((flags & O_SYNC) == O_SYNC) {
1274 accessmode |= (1<<14);
1278 if (share_mode == DENY_FCB) {
1282 SCVAL(state->vwv + 0, 0, 0xFF);
1283 SCVAL(state->vwv + 0, 1, 0);
1284 SSVAL(state->vwv + 1, 0, 0);
1285 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
1286 SSVAL(state->vwv + 3, 0, accessmode);
1287 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
1288 SSVAL(state->vwv + 5, 0, 0);
1289 SIVAL(state->vwv + 6, 0, 0);
1290 SSVAL(state->vwv + 8, 0, openfn);
1291 SIVAL(state->vwv + 9, 0, 0);
1292 SIVAL(state->vwv + 11, 0, 0);
1293 SIVAL(state->vwv + 13, 0, 0);
1295 additional_flags = 0;
1297 if (cli->use_oplocks) {
1298 /* if using oplocks then ask for a batch oplock via
1299 core and extended methods */
1301 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
1302 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
1305 bytes = talloc_array(state, uint8_t, 0);
1306 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1307 strlen(fname)+1, NULL);
1309 if (tevent_req_nomem(bytes, req)) {
1310 return tevent_req_post(req, ev);
1313 state->bytes.iov_base = bytes;
1314 state->bytes.iov_len = talloc_get_size(bytes);
1316 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
1317 15, state->vwv, 1, &state->bytes);
1318 if (subreq == NULL) {
1322 tevent_req_set_callback(subreq, cli_open_done, req);
1327 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1328 struct cli_state *cli, const char *fname,
1329 int flags, int share_mode)
1331 struct tevent_req *req, *subreq;
1333 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
1335 if ((req == NULL) || !cli_smb_req_send(subreq)) {
1342 static void cli_open_done(struct tevent_req *subreq)
1344 struct tevent_req *req = tevent_req_callback_data(
1345 subreq, struct tevent_req);
1346 struct cli_open_state *state = tevent_req_data(
1347 req, struct cli_open_state);
1352 status = cli_smb_recv(subreq, 3, &wct, &vwv, NULL, NULL);
1353 if (!NT_STATUS_IS_OK(status)) {
1354 TALLOC_FREE(subreq);
1355 tevent_req_nterror(req, status);
1358 state->fnum = SVAL(vwv+2, 0);
1359 tevent_req_done(req);
1362 NTSTATUS cli_open_recv(struct tevent_req *req, int *fnum)
1364 struct cli_open_state *state = tevent_req_data(
1365 req, struct cli_open_state);
1368 if (tevent_req_is_nterror(req, &status)) {
1371 *fnum = state->fnum;
1372 return NT_STATUS_OK;
1375 int cli_open(struct cli_state *cli, const char *fname, int flags,
1378 TALLOC_CTX *frame = talloc_stackframe();
1379 struct event_context *ev;
1380 struct tevent_req *req;
1381 NTSTATUS status = NT_STATUS_OK;
1384 if (cli_has_async_calls(cli)) {
1386 * Can't use sync call while an async call is in flight
1388 status = NT_STATUS_INVALID_PARAMETER;
1392 ev = event_context_init(frame);
1394 status = NT_STATUS_NO_MEMORY;
1398 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
1400 status = NT_STATUS_NO_MEMORY;
1404 if (!tevent_req_poll(req, ev)) {
1405 status = map_nt_error_from_unix(errno);
1409 cli_open_recv(req, &result);
1412 if (!NT_STATUS_IS_OK(status)) {
1413 cli_set_error(cli, status);
1418 /****************************************************************************
1420 ****************************************************************************/
1422 struct cli_close_state {
1426 static void cli_close_done(struct tevent_req *subreq);
1428 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
1429 struct event_context *ev,
1430 struct cli_state *cli, int fnum,
1431 struct tevent_req **psubreq)
1433 struct tevent_req *req, *subreq;
1434 struct cli_close_state *state;
1436 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
1440 SSVAL(state->vwv+0, 0, fnum);
1441 SIVALS(state->vwv+1, 0, -1);
1443 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
1445 if (subreq == NULL) {
1449 tevent_req_set_callback(subreq, cli_close_done, req);
1454 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
1455 struct event_context *ev,
1456 struct cli_state *cli, int fnum)
1458 struct tevent_req *req, *subreq;
1460 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
1461 if ((req == NULL) || !cli_smb_req_send(subreq)) {
1468 static void cli_close_done(struct tevent_req *subreq)
1470 struct tevent_req *req = tevent_req_callback_data(
1471 subreq, struct tevent_req);
1474 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1475 TALLOC_FREE(subreq);
1476 if (!NT_STATUS_IS_OK(status)) {
1477 tevent_req_nterror(req, status);
1480 tevent_req_done(req);
1483 NTSTATUS cli_close_recv(struct tevent_req *req)
1485 return tevent_req_simple_recv_ntstatus(req);
1488 bool cli_close(struct cli_state *cli, int fnum)
1490 TALLOC_CTX *frame = talloc_stackframe();
1491 struct event_context *ev;
1492 struct tevent_req *req;
1493 NTSTATUS status = NT_STATUS_OK;
1494 bool result = false;
1496 if (cli_has_async_calls(cli)) {
1498 * Can't use sync call while an async call is in flight
1500 status = NT_STATUS_INVALID_PARAMETER;
1504 ev = event_context_init(frame);
1506 status = NT_STATUS_NO_MEMORY;
1510 req = cli_close_send(frame, ev, cli, fnum);
1512 status = NT_STATUS_NO_MEMORY;
1516 if (!tevent_req_poll(req, ev)) {
1517 status = map_nt_error_from_unix(errno);
1521 result = NT_STATUS_IS_OK(cli_close_recv(req));
1524 if (!NT_STATUS_IS_OK(status)) {
1525 cli_set_error(cli, status);
1530 /****************************************************************************
1531 Truncate a file to a specified size
1532 ****************************************************************************/
1534 bool cli_ftruncate(struct cli_state *cli, int fnum, uint64_t size)
1536 unsigned int param_len = 6;
1537 unsigned int data_len = 8;
1538 uint16_t setup = TRANSACT2_SETFILEINFO;
1540 unsigned char data[8];
1541 char *rparam=NULL, *rdata=NULL;
1542 int saved_timeout = cli->timeout;
1544 SSVAL(param,0,fnum);
1545 SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
1548 SBVAL(data, 0, size);
1550 if (!cli_send_trans(cli, SMBtrans2,
1552 -1, 0, /* fid, flags */
1553 &setup, 1, 0, /* setup, length, max */
1554 param, param_len, 2, /* param, length, max */
1555 (char *)&data, data_len,/* data, length, ... */
1556 cli->max_xmit)) { /* ... max */
1557 cli->timeout = saved_timeout;
1561 if (!cli_receive_trans(cli, SMBtrans2,
1562 &rparam, ¶m_len,
1563 &rdata, &data_len)) {
1564 cli->timeout = saved_timeout;
1570 cli->timeout = saved_timeout;
1579 /****************************************************************************
1580 send a lock with a specified locktype
1581 this is used for testing LOCKING_ANDX_CANCEL_LOCK
1582 ****************************************************************************/
1584 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
1585 uint32_t offset, uint32_t len,
1586 int timeout, unsigned char locktype)
1589 int saved_timeout = cli->timeout;
1591 memset(cli->outbuf,'\0',smb_size);
1592 memset(cli->inbuf,'\0', smb_size);
1594 cli_set_message(cli->outbuf,8,0,True);
1596 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1597 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1598 cli_setup_packet(cli);
1600 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1601 SSVAL(cli->outbuf,smb_vwv2,fnum);
1602 SCVAL(cli->outbuf,smb_vwv3,locktype);
1603 SIVALS(cli->outbuf, smb_vwv4, timeout);
1604 SSVAL(cli->outbuf,smb_vwv6,0);
1605 SSVAL(cli->outbuf,smb_vwv7,1);
1607 p = smb_buf(cli->outbuf);
1608 SSVAL(p, 0, cli->pid);
1609 SIVAL(p, 2, offset);
1614 cli_setup_bcc(cli, p);
1619 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
1622 if (!cli_receive_smb(cli)) {
1623 cli->timeout = saved_timeout;
1624 return NT_STATUS_UNSUCCESSFUL;
1627 cli->timeout = saved_timeout;
1629 return cli_nt_error(cli);
1632 /****************************************************************************
1634 note that timeout is in units of 2 milliseconds
1635 ****************************************************************************/
1637 bool cli_lock(struct cli_state *cli, int fnum,
1638 uint32_t offset, uint32_t len, int timeout, enum brl_type lock_type)
1641 int saved_timeout = cli->timeout;
1643 memset(cli->outbuf,'\0',smb_size);
1644 memset(cli->inbuf,'\0', smb_size);
1646 cli_set_message(cli->outbuf,8,0,True);
1648 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1649 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1650 cli_setup_packet(cli);
1652 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1653 SSVAL(cli->outbuf,smb_vwv2,fnum);
1654 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
1655 SIVALS(cli->outbuf, smb_vwv4, timeout);
1656 SSVAL(cli->outbuf,smb_vwv6,0);
1657 SSVAL(cli->outbuf,smb_vwv7,1);
1659 p = smb_buf(cli->outbuf);
1660 SSVAL(p, 0, cli->pid);
1661 SIVAL(p, 2, offset);
1666 cli_setup_bcc(cli, p);
1671 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
1674 if (!cli_receive_smb(cli)) {
1675 cli->timeout = saved_timeout;
1679 cli->timeout = saved_timeout;
1681 if (cli_is_error(cli)) {
1688 /****************************************************************************
1690 ****************************************************************************/
1692 bool cli_unlock(struct cli_state *cli, int fnum, uint32_t offset, uint32_t len)
1696 memset(cli->outbuf,'\0',smb_size);
1697 memset(cli->inbuf,'\0',smb_size);
1699 cli_set_message(cli->outbuf,8,0,True);
1701 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1702 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1703 cli_setup_packet(cli);
1705 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1706 SSVAL(cli->outbuf,smb_vwv2,fnum);
1707 SCVAL(cli->outbuf,smb_vwv3,0);
1708 SIVALS(cli->outbuf, smb_vwv4, 0);
1709 SSVAL(cli->outbuf,smb_vwv6,1);
1710 SSVAL(cli->outbuf,smb_vwv7,0);
1712 p = smb_buf(cli->outbuf);
1713 SSVAL(p, 0, cli->pid);
1714 SIVAL(p, 2, offset);
1717 cli_setup_bcc(cli, p);
1719 if (!cli_receive_smb(cli)) {
1723 if (cli_is_error(cli)) {
1730 /****************************************************************************
1731 Lock a file with 64 bit offsets.
1732 ****************************************************************************/
1734 bool cli_lock64(struct cli_state *cli, int fnum,
1735 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
1738 int saved_timeout = cli->timeout;
1741 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1742 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1745 ltype = (lock_type == READ_LOCK? 1 : 0);
1746 ltype |= LOCKING_ANDX_LARGE_FILES;
1748 memset(cli->outbuf,'\0',smb_size);
1749 memset(cli->inbuf,'\0', smb_size);
1751 cli_set_message(cli->outbuf,8,0,True);
1753 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1754 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1755 cli_setup_packet(cli);
1757 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1758 SSVAL(cli->outbuf,smb_vwv2,fnum);
1759 SCVAL(cli->outbuf,smb_vwv3,ltype);
1760 SIVALS(cli->outbuf, smb_vwv4, timeout);
1761 SSVAL(cli->outbuf,smb_vwv6,0);
1762 SSVAL(cli->outbuf,smb_vwv7,1);
1764 p = smb_buf(cli->outbuf);
1765 SIVAL(p, 0, cli->pid);
1766 SOFF_T_R(p, 4, offset);
1767 SOFF_T_R(p, 12, len);
1770 cli_setup_bcc(cli, p);
1774 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1777 if (!cli_receive_smb(cli)) {
1778 cli->timeout = saved_timeout;
1782 cli->timeout = saved_timeout;
1784 if (cli_is_error(cli)) {
1791 /****************************************************************************
1792 Unlock a file with 64 bit offsets.
1793 ****************************************************************************/
1795 bool cli_unlock64(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
1799 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1800 return cli_unlock(cli, fnum, offset, len);
1803 memset(cli->outbuf,'\0',smb_size);
1804 memset(cli->inbuf,'\0',smb_size);
1806 cli_set_message(cli->outbuf,8,0,True);
1808 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1809 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1810 cli_setup_packet(cli);
1812 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1813 SSVAL(cli->outbuf,smb_vwv2,fnum);
1814 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1815 SIVALS(cli->outbuf, smb_vwv4, 0);
1816 SSVAL(cli->outbuf,smb_vwv6,1);
1817 SSVAL(cli->outbuf,smb_vwv7,0);
1819 p = smb_buf(cli->outbuf);
1820 SIVAL(p, 0, cli->pid);
1821 SOFF_T_R(p, 4, offset);
1822 SOFF_T_R(p, 12, len);
1824 cli_setup_bcc(cli, p);
1826 if (!cli_receive_smb(cli)) {
1830 if (cli_is_error(cli)) {
1837 /****************************************************************************
1838 Get/unlock a POSIX lock on a file - internal function.
1839 ****************************************************************************/
1841 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
1842 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
1844 unsigned int param_len = 4;
1845 unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1846 uint16_t setup = TRANSACT2_SETFILEINFO;
1848 unsigned char data[POSIX_LOCK_DATA_SIZE];
1849 char *rparam=NULL, *rdata=NULL;
1850 int saved_timeout = cli->timeout;
1852 SSVAL(param,0,fnum);
1853 SSVAL(param,2,SMB_SET_POSIX_LOCK);
1855 switch (lock_type) {
1857 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
1860 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
1863 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
1870 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
1871 cli->timeout = 0x7FFFFFFF;
1873 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
1876 SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
1877 SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
1878 SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
1880 if (!cli_send_trans(cli, SMBtrans2,
1882 -1, 0, /* fid, flags */
1883 &setup, 1, 0, /* setup, length, max */
1884 param, param_len, 2, /* param, length, max */
1885 (char *)&data, data_len, cli->max_xmit /* data, length, max */
1887 cli->timeout = saved_timeout;
1891 if (!cli_receive_trans(cli, SMBtrans2,
1892 &rparam, ¶m_len,
1893 &rdata, &data_len)) {
1894 cli->timeout = saved_timeout;
1900 cli->timeout = saved_timeout;
1908 /****************************************************************************
1910 ****************************************************************************/
1912 bool cli_posix_lock(struct cli_state *cli, int fnum,
1913 uint64_t offset, uint64_t len,
1914 bool wait_lock, enum brl_type lock_type)
1916 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
1919 return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
1922 /****************************************************************************
1923 POSIX Unlock a file.
1924 ****************************************************************************/
1926 bool cli_posix_unlock(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
1928 return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
1931 /****************************************************************************
1932 POSIX Get any lock covering a file.
1933 ****************************************************************************/
1935 bool cli_posix_getlock(struct cli_state *cli, int fnum, uint64_t *poffset, uint64_t *plen)
1940 /****************************************************************************
1941 Do a SMBgetattrE call.
1942 ****************************************************************************/
1944 bool cli_getattrE(struct cli_state *cli, int fd,
1945 uint16_t *attr, SMB_OFF_T *size,
1946 time_t *change_time,
1947 time_t *access_time,
1950 memset(cli->outbuf,'\0',smb_size);
1951 memset(cli->inbuf,'\0',smb_size);
1953 cli_set_message(cli->outbuf,1,0,True);
1955 SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1956 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1957 cli_setup_packet(cli);
1959 SSVAL(cli->outbuf,smb_vwv0,fd);
1962 if (!cli_receive_smb(cli)) {
1966 if (cli_is_error(cli)) {
1971 *size = IVAL(cli->inbuf, smb_vwv6);
1975 *attr = SVAL(cli->inbuf,smb_vwv10);
1979 *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
1983 *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
1987 *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
1993 /****************************************************************************
1995 ****************************************************************************/
1997 bool cli_getatr(struct cli_state *cli, const char *fname,
1998 uint16_t *attr, SMB_OFF_T *size, time_t *write_time)
2002 memset(cli->outbuf,'\0',smb_size);
2003 memset(cli->inbuf,'\0',smb_size);
2005 cli_set_message(cli->outbuf,0,0,True);
2007 SCVAL(cli->outbuf,smb_com,SMBgetatr);
2008 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2009 cli_setup_packet(cli);
2011 p = smb_buf(cli->outbuf);
2013 p += clistr_push(cli, p, fname,
2014 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2016 cli_setup_bcc(cli, p);
2019 if (!cli_receive_smb(cli)) {
2023 if (cli_is_error(cli)) {
2028 *size = IVAL(cli->inbuf, smb_vwv3);
2032 *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
2036 *attr = SVAL(cli->inbuf,smb_vwv0);
2042 /****************************************************************************
2043 Do a SMBsetattrE call.
2044 ****************************************************************************/
2046 bool cli_setattrE(struct cli_state *cli, int fd,
2054 memset(cli->outbuf,'\0',smb_size);
2055 memset(cli->inbuf,'\0',smb_size);
2057 cli_set_message(cli->outbuf,7,0,True);
2059 SCVAL(cli->outbuf,smb_com,SMBsetattrE);
2060 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2061 cli_setup_packet(cli);
2063 SSVAL(cli->outbuf,smb_vwv0, fd);
2064 cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
2065 cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
2066 cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
2068 p = smb_buf(cli->outbuf);
2071 cli_setup_bcc(cli, p);
2074 if (!cli_receive_smb(cli)) {
2078 if (cli_is_error(cli)) {
2085 /****************************************************************************
2086 Do a SMBsetatr call.
2087 ****************************************************************************/
2089 bool cli_setatr(struct cli_state *cli, const char *fname, uint16_t attr, time_t t)
2093 memset(cli->outbuf,'\0',smb_size);
2094 memset(cli->inbuf,'\0',smb_size);
2096 cli_set_message(cli->outbuf,8,0,True);
2098 SCVAL(cli->outbuf,smb_com,SMBsetatr);
2099 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2100 cli_setup_packet(cli);
2102 SSVAL(cli->outbuf,smb_vwv0, attr);
2103 cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
2105 p = smb_buf(cli->outbuf);
2107 p += clistr_push(cli, p, fname,
2108 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2111 cli_setup_bcc(cli, p);
2114 if (!cli_receive_smb(cli)) {
2118 if (cli_is_error(cli)) {
2125 /****************************************************************************
2126 Check for existance of a dir.
2127 ****************************************************************************/
2129 static void cli_chkpath_done(struct tevent_req *subreq);
2131 struct cli_chkpath_state {
2135 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
2136 struct event_context *ev,
2137 struct cli_state *cli,
2140 struct tevent_req *req = NULL, *subreq = NULL;
2141 struct cli_chkpath_state *state = NULL;
2142 uint8_t additional_flags = 0;
2143 uint8_t *bytes = NULL;
2145 req = tevent_req_create(mem_ctx, &state, struct cli_chkpath_state);
2150 bytes = talloc_array(state, uint8_t, 1);
2151 if (tevent_req_nomem(bytes, req)) {
2152 return tevent_req_post(req, ev);
2155 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
2156 strlen(fname)+1, NULL);
2158 if (tevent_req_nomem(bytes, req)) {
2159 return tevent_req_post(req, ev);
2162 subreq = cli_smb_send(state, ev, cli, SMBcheckpath, additional_flags,
2163 0, NULL, talloc_get_size(bytes), bytes);
2164 if (tevent_req_nomem(subreq, req)) {
2165 return tevent_req_post(req, ev);
2167 tevent_req_set_callback(subreq, cli_chkpath_done, req);
2171 static void cli_chkpath_done(struct tevent_req *subreq)
2173 struct tevent_req *req = tevent_req_callback_data(
2174 subreq, struct tevent_req);
2177 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
2178 TALLOC_FREE(subreq);
2179 if (!NT_STATUS_IS_OK(status)) {
2180 tevent_req_nterror(req, status);
2183 tevent_req_done(req);
2186 NTSTATUS cli_chkpath_recv(struct tevent_req *req)
2188 return tevent_req_simple_recv_ntstatus(req);
2191 NTSTATUS cli_chkpath(struct cli_state *cli, const char *path)
2193 TALLOC_CTX *frame = talloc_stackframe();
2194 struct event_context *ev = NULL;
2195 struct tevent_req *req = NULL;
2197 NTSTATUS status = NT_STATUS_OK;
2199 if (cli_has_async_calls(cli)) {
2201 * Can't use sync call while an async call is in flight
2203 status = NT_STATUS_INVALID_PARAMETER;
2207 path2 = talloc_strdup(frame, path);
2209 status = NT_STATUS_NO_MEMORY;
2212 trim_char(path2,'\0','\\');
2214 path2 = talloc_strdup(frame, "\\");
2216 status = NT_STATUS_NO_MEMORY;
2221 ev = event_context_init(frame);
2223 status = NT_STATUS_NO_MEMORY;
2227 req = cli_chkpath_send(frame, ev, cli, path2);
2229 status = NT_STATUS_NO_MEMORY;
2233 if (!tevent_req_poll(req, ev)) {
2234 status = map_nt_error_from_unix(errno);
2238 status = cli_chkpath_recv(req);
2242 if (!NT_STATUS_IS_OK(status)) {
2243 cli_set_error(cli, status);
2248 /****************************************************************************
2250 ****************************************************************************/
2252 static void cli_dskattr_done(struct tevent_req *subreq);
2254 struct cli_dskattr_state {
2260 struct tevent_req *cli_dskattr_send(TALLOC_CTX *mem_ctx,
2261 struct event_context *ev,
2262 struct cli_state *cli)
2264 struct tevent_req *req = NULL, *subreq = NULL;
2265 struct cli_dskattr_state *state = NULL;
2266 uint8_t additional_flags = 0;
2268 req = tevent_req_create(mem_ctx, &state, struct cli_dskattr_state);
2273 subreq = cli_smb_send(state, ev, cli, SMBdskattr, additional_flags,
2275 if (tevent_req_nomem(subreq, req)) {
2276 return tevent_req_post(req, ev);
2278 tevent_req_set_callback(subreq, cli_dskattr_done, req);
2282 static void cli_dskattr_done(struct tevent_req *subreq)
2284 struct tevent_req *req = tevent_req_callback_data(
2285 subreq, struct tevent_req);
2286 struct cli_dskattr_state *state = tevent_req_data(
2287 req, struct cli_dskattr_state);
2289 uint16_t *vwv = NULL;
2292 status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
2293 if (!NT_STATUS_IS_OK(status)) {
2294 tevent_req_nterror(req, status);
2297 state->bsize = SVAL(vwv+1, 0)*SVAL(vwv+2,0);
2298 state->total = SVAL(vwv+0, 0);
2299 state->avail = SVAL(vwv+3, 0);
2300 TALLOC_FREE(subreq);
2301 tevent_req_done(req);
2304 NTSTATUS cli_dskattr_recv(struct tevent_req *req, int *bsize, int *total, int *avail)
2306 struct cli_dskattr_state *state = tevent_req_data(
2307 req, struct cli_dskattr_state);
2310 if (tevent_req_is_nterror(req, &status)) {
2313 *bsize = state->bsize;
2314 *total = state->total;
2315 *avail = state->avail;
2316 return NT_STATUS_OK;
2319 NTSTATUS cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
2321 TALLOC_CTX *frame = talloc_stackframe();
2322 struct event_context *ev = NULL;
2323 struct tevent_req *req = NULL;
2324 NTSTATUS status = NT_STATUS_OK;
2326 if (cli_has_async_calls(cli)) {
2328 * Can't use sync call while an async call is in flight
2330 status = NT_STATUS_INVALID_PARAMETER;
2334 ev = event_context_init(frame);
2336 status = NT_STATUS_NO_MEMORY;
2340 req = cli_dskattr_send(frame, ev, cli);
2342 status = NT_STATUS_NO_MEMORY;
2346 if (!tevent_req_poll(req, ev)) {
2347 status = map_nt_error_from_unix(errno);
2351 status = cli_dskattr_recv(req, bsize, total, avail);
2355 if (!NT_STATUS_IS_OK(status)) {
2356 cli_set_error(cli, status);
2361 /****************************************************************************
2362 Create and open a temporary file.
2363 ****************************************************************************/
2365 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
2370 memset(cli->outbuf,'\0',smb_size);
2371 memset(cli->inbuf,'\0',smb_size);
2373 cli_set_message(cli->outbuf,3,0,True);
2375 SCVAL(cli->outbuf,smb_com,SMBctemp);
2376 SSVAL(cli->outbuf,smb_tid,cli->cnum);
2377 cli_setup_packet(cli);
2379 SSVAL(cli->outbuf,smb_vwv0,0);
2380 SIVALS(cli->outbuf,smb_vwv1,-1);
2382 p = smb_buf(cli->outbuf);
2384 p += clistr_push(cli, p, path,
2385 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
2387 cli_setup_bcc(cli, p);
2390 if (!cli_receive_smb(cli)) {
2394 if (cli_is_error(cli)) {
2398 /* despite the spec, the result has a -1, followed by
2399 length, followed by name */
2400 p = smb_buf(cli->inbuf);
2402 len = smb_buflen(cli->inbuf) - 4;
2403 if (len <= 0 || len > PATH_MAX) return -1;
2406 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
2410 clistr_pull(cli->inbuf, path2, p,
2411 len+1, len, STR_ASCII);
2415 return SVAL(cli->inbuf,smb_vwv0);
2419 send a raw ioctl - used by the torture code
2421 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32_t code, DATA_BLOB *blob)
2423 memset(cli->outbuf,'\0',smb_size);
2424 memset(cli->inbuf,'\0',smb_size);
2426 cli_set_message(cli->outbuf, 3, 0, True);
2427 SCVAL(cli->outbuf,smb_com,SMBioctl);
2428 cli_setup_packet(cli);
2430 SSVAL(cli->outbuf, smb_vwv0, fnum);
2431 SSVAL(cli->outbuf, smb_vwv1, code>>16);
2432 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
2435 if (!cli_receive_smb(cli)) {
2436 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2439 if (cli_is_error(cli)) {
2440 return cli_nt_error(cli);
2443 *blob = data_blob_null;
2445 return NT_STATUS_OK;
2448 /*********************************************************
2449 Set an extended attribute utility fn.
2450 *********************************************************/
2452 static bool cli_set_ea(struct cli_state *cli, uint16_t setup, char *param, unsigned int param_len,
2453 const char *ea_name, const char *ea_val, size_t ea_len)
2455 unsigned int data_len = 0;
2457 char *rparam=NULL, *rdata=NULL;
2459 size_t ea_namelen = strlen(ea_name);
2461 if (ea_namelen == 0 && ea_len == 0) {
2463 data = (char *)SMB_MALLOC(data_len);
2468 SIVAL(p,0,data_len);
2470 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
2471 data = (char *)SMB_MALLOC(data_len);
2476 SIVAL(p,0,data_len);
2478 SCVAL(p, 0, 0); /* EA flags. */
2479 SCVAL(p, 1, ea_namelen);
2480 SSVAL(p, 2, ea_len);
2481 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
2482 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
2485 if (!cli_send_trans(cli, SMBtrans2,
2487 -1, 0, /* fid, flags */
2488 &setup, 1, 0, /* setup, length, max */
2489 param, param_len, 2, /* param, length, max */
2490 data, data_len, cli->max_xmit /* data, length, max */
2496 if (!cli_receive_trans(cli, SMBtrans2,
2497 &rparam, ¶m_len,
2498 &rdata, &data_len)) {
2510 /*********************************************************
2511 Set an extended attribute on a pathname.
2512 *********************************************************/
2514 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
2516 uint16_t setup = TRANSACT2_SETPATHINFO;
2517 unsigned int param_len = 0;
2519 size_t srclen = 2*(strlen(path)+1);
2523 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2527 memset(param, '\0', 6);
2528 SSVAL(param,0,SMB_INFO_SET_EA);
2531 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2532 param_len = PTR_DIFF(p, param);
2534 ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
2539 /*********************************************************
2540 Set an extended attribute on an fnum.
2541 *********************************************************/
2543 bool cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
2546 uint16_t setup = TRANSACT2_SETFILEINFO;
2548 memset(param, 0, 6);
2549 SSVAL(param,0,fnum);
2550 SSVAL(param,2,SMB_INFO_SET_EA);
2552 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
2555 /*********************************************************
2556 Get an extended attribute list utility fn.
2557 *********************************************************/
2559 static bool cli_get_ea_list(struct cli_state *cli,
2560 uint16_t setup, char *param, unsigned int param_len,
2563 struct ea_struct **pea_list)
2565 unsigned int data_len = 0;
2566 unsigned int rparam_len, rdata_len;
2567 char *rparam=NULL, *rdata=NULL;
2572 struct ea_struct *ea_list;
2579 if (!cli_send_trans(cli, SMBtrans2,
2581 -1, 0, /* fid, flags */
2582 &setup, 1, 0, /* setup, length, max */
2583 param, param_len, 10, /* param, length, max */
2584 NULL, data_len, cli->max_xmit /* data, length, max */
2589 if (!cli_receive_trans(cli, SMBtrans2,
2590 &rparam, &rparam_len,
2591 &rdata, &rdata_len)) {
2595 if (!rdata || rdata_len < 4) {
2599 ea_size = (size_t)IVAL(rdata,0);
2600 if (ea_size > rdata_len) {
2605 /* No EA's present. */
2613 /* Validate the EA list and count it. */
2614 for (num_eas = 0; ea_size >= 4; num_eas++) {
2615 unsigned int ea_namelen = CVAL(p,1);
2616 unsigned int ea_valuelen = SVAL(p,2);
2617 if (ea_namelen == 0) {
2620 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
2623 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
2624 p += 4 + ea_namelen + 1 + ea_valuelen;
2632 *pnum_eas = num_eas;
2634 /* Caller only wants number of EA's. */
2639 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
2644 ea_size = (size_t)IVAL(rdata,0);
2647 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
2648 struct ea_struct *ea = &ea_list[num_eas];
2649 fstring unix_ea_name;
2650 unsigned int ea_namelen = CVAL(p,1);
2651 unsigned int ea_valuelen = SVAL(p,2);
2653 ea->flags = CVAL(p,0);
2654 unix_ea_name[0] = '\0';
2655 pull_ascii_fstring(unix_ea_name, p + 4);
2656 ea->name = talloc_strdup(ctx, unix_ea_name);
2657 /* Ensure the value is null terminated (in case it's a string). */
2658 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
2659 if (!ea->value.data) {
2663 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
2665 ea->value.data[ea_valuelen] = 0;
2667 p += 4 + ea_namelen + 1 + ea_valuelen;
2670 *pea_list = ea_list;
2680 /*********************************************************
2681 Get an extended attribute list from a pathname.
2682 *********************************************************/
2684 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
2687 struct ea_struct **pea_list)
2689 uint16_t setup = TRANSACT2_QPATHINFO;
2690 unsigned int param_len = 0;
2693 size_t srclen = 2*(strlen(path)+1);
2696 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2702 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
2704 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2705 param_len = PTR_DIFF(p, param);
2707 ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
2712 /*********************************************************
2713 Get an extended attribute list from an fnum.
2714 *********************************************************/
2716 bool cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
2719 struct ea_struct **pea_list)
2721 uint16_t setup = TRANSACT2_QFILEINFO;
2724 memset(param, 0, 6);
2725 SSVAL(param,0,fnum);
2726 SSVAL(param,2,SMB_INFO_SET_EA);
2728 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
2731 /****************************************************************************
2732 Convert open "flags" arg to uint32_t on wire.
2733 ****************************************************************************/
2735 static uint32_t open_flags_to_wire(int flags)
2737 int open_mode = flags & O_ACCMODE;
2740 switch (open_mode) {
2742 ret |= SMB_O_WRONLY;
2749 ret |= SMB_O_RDONLY;
2753 if (flags & O_CREAT) {
2756 if (flags & O_EXCL) {
2759 if (flags & O_TRUNC) {
2763 if (flags & O_SYNC) {
2767 if (flags & O_APPEND) {
2768 ret |= SMB_O_APPEND;
2770 #if defined(O_DIRECT)
2771 if (flags & O_DIRECT) {
2772 ret |= SMB_O_DIRECT;
2775 #if defined(O_DIRECTORY)
2776 if (flags & O_DIRECTORY) {
2777 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2778 ret |= SMB_O_DIRECTORY;
2784 /****************************************************************************
2785 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
2786 ****************************************************************************/
2788 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
2790 unsigned int data_len = 0;
2791 unsigned int param_len = 0;
2792 uint16_t setup = TRANSACT2_SETPATHINFO;
2795 char *rparam=NULL, *rdata=NULL;
2798 uint32_t wire_flags = open_flags_to_wire(flags);
2799 size_t srclen = 2*(strlen(fname)+1);
2801 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2805 memset(param, '\0', 6);
2806 SSVAL(param,0, SMB_POSIX_PATH_OPEN);
2809 p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2810 param_len = PTR_DIFF(p, param);
2813 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2814 wire_flags |= SMB_O_DIRECTORY;
2818 SIVAL(p,0,0); /* No oplock. */
2819 SIVAL(p,4,wire_flags);
2820 SIVAL(p,8,unix_perms_to_wire(mode));
2821 SIVAL(p,12,0); /* Top bits of perms currently undefined. */
2822 SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
2826 if (!cli_send_trans(cli, SMBtrans2,
2828 -1, 0, /* fid, flags */
2829 &setup, 1, 0, /* setup, length, max */
2830 param, param_len, 2, /* param, length, max */
2831 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2839 if (!cli_receive_trans(cli, SMBtrans2,
2840 &rparam, ¶m_len,
2841 &rdata, &data_len)) {
2845 fnum = SVAL(rdata,2);
2853 /****************************************************************************
2854 open - POSIX semantics.
2855 ****************************************************************************/
2857 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
2859 return cli_posix_open_internal(cli, fname, flags, mode, False);
2862 /****************************************************************************
2863 mkdir - POSIX semantics.
2864 ****************************************************************************/
2866 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
2868 return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
2871 /****************************************************************************
2872 unlink or rmdir - POSIX semantics.
2873 ****************************************************************************/
2875 struct unlink_state {
2879 static void cli_posix_unlink_internal_done(struct tevent_req *subreq)
2881 struct tevent_req *req = tevent_req_callback_data(
2882 subreq, struct tevent_req);
2883 struct unlink_state *state = tevent_req_data(req, struct unlink_state);
2886 status = cli_trans_recv(subreq, state, NULL, NULL, NULL, NULL, NULL, NULL);
2887 TALLOC_FREE(subreq);
2888 if (!NT_STATUS_IS_OK(status)) {
2889 tevent_req_nterror(req, status);
2892 tevent_req_done(req);
2895 static struct tevent_req *cli_posix_unlink_internal_send(TALLOC_CTX *mem_ctx,
2896 struct event_context *ev,
2897 struct cli_state *cli,
2901 struct tevent_req *req = NULL, *subreq = NULL;
2902 struct unlink_state *state = NULL;
2904 uint8_t *param = NULL;
2907 req = tevent_req_create(mem_ctx, &state, struct unlink_state);
2912 /* Setup setup word. */
2913 SSVAL(&setup+0, 0, TRANSACT2_SETPATHINFO);
2915 /* Setup param array. */
2916 param = talloc_array(state, uint8_t, 6);
2917 if (tevent_req_nomem(data, req)) {
2918 return tevent_req_post(req, ev);
2920 memset(param, '\0', 6);
2921 SSVAL(param, 0, SMB_POSIX_PATH_UNLINK);
2923 param = smb_bytes_push_str(param, cli_ucs2(cli), fname,
2924 strlen(fname)+1, NULL);
2926 if (tevent_req_nomem(param, req)) {
2927 return tevent_req_post(req, ev);
2930 /* Setup data word. */
2931 SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
2932 SMB_POSIX_UNLINK_FILE_TARGET);
2934 subreq = cli_trans_send(state, /* mem ctx. */
2935 ev, /* event ctx. */
2936 cli, /* cli_state. */
2937 SMBtrans2, /* cmd. */
2938 NULL, /* pipe name. */
2942 &setup, /* setup. */
2946 talloc_get_size(param), /* num param. */
2952 if (tevent_req_nomem(subreq, req)) {
2953 return tevent_req_post(req, ev);
2955 tevent_req_set_callback(subreq, cli_posix_unlink_internal_done, req);
2959 struct tevent_req *cli_posix_unlink_send(TALLOC_CTX *mem_ctx,
2960 struct event_context *ev,
2961 struct cli_state *cli,
2964 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, false);
2967 NTSTATUS cli_posix_unlink_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
2971 if (tevent_req_is_nterror(req, &status)) {
2974 return NT_STATUS_OK;
2977 /****************************************************************************
2978 unlink - POSIX semantics.
2979 ****************************************************************************/
2981 NTSTATUS cli_posix_unlink(struct cli_state *cli, const char *fname)
2983 TALLOC_CTX *frame = talloc_stackframe();
2984 struct event_context *ev = NULL;
2985 struct tevent_req *req = NULL;
2986 NTSTATUS status = NT_STATUS_OK;
2988 if (cli_has_async_calls(cli)) {
2990 * Can't use sync call while an async call is in flight
2992 status = NT_STATUS_INVALID_PARAMETER;
2996 ev = event_context_init(frame);
2998 status = NT_STATUS_NO_MEMORY;
3002 req = cli_posix_unlink_send(frame,
3007 status = NT_STATUS_NO_MEMORY;
3011 if (!tevent_req_poll(req, ev)) {
3012 status = map_nt_error_from_unix(errno);
3016 status = cli_posix_unlink_recv(req, frame);
3020 if (!NT_STATUS_IS_OK(status)) {
3021 cli_set_error(cli, status);
3026 /****************************************************************************
3027 rmdir - POSIX semantics.
3028 ****************************************************************************/
3030 struct tevent_req *cli_posix_rmdir_send(TALLOC_CTX *mem_ctx,
3031 struct event_context *ev,
3032 struct cli_state *cli,
3035 return cli_posix_unlink_internal_send(mem_ctx, ev, cli, fname, true);
3038 NTSTATUS cli_posix_rmdir_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)
3042 if (tevent_req_is_nterror(req, &status)) {
3045 return NT_STATUS_OK;
3048 NTSTATUS cli_posix_rmdir(struct cli_state *cli, const char *fname)
3050 TALLOC_CTX *frame = talloc_stackframe();
3051 struct event_context *ev = NULL;
3052 struct tevent_req *req = NULL;
3053 NTSTATUS status = NT_STATUS_OK;
3055 if (cli_has_async_calls(cli)) {
3057 * Can't use sync call while an async call is in flight
3059 status = NT_STATUS_INVALID_PARAMETER;
3063 ev = event_context_init(frame);
3065 status = NT_STATUS_NO_MEMORY;
3069 req = cli_posix_rmdir_send(frame,
3074 status = NT_STATUS_NO_MEMORY;
3078 if (!tevent_req_poll(req, ev)) {
3079 status = map_nt_error_from_unix(errno);
3083 status = cli_posix_rmdir_recv(req, frame);
3087 if (!NT_STATUS_IS_OK(status)) {
3088 cli_set_error(cli, status);