2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2002
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 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 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 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 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 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 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 dev_major = IVAL(rdata,60);
315 uint32 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 mode, uint32 uid, uint32 gid)
353 unsigned int data_len = 0;
354 unsigned int param_len = 0;
355 uint16 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 bool cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
436 memset(cli->outbuf,'\0',smb_size);
437 memset(cli->inbuf,'\0',smb_size);
439 cli_set_message(cli->outbuf,1, 0, true);
441 SCVAL(cli->outbuf,smb_com,SMBmv);
442 SSVAL(cli->outbuf,smb_tid,cli->cnum);
443 cli_setup_packet(cli);
445 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
447 p = smb_buf(cli->outbuf);
449 p += clistr_push(cli, p, fname_src,
450 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
452 p += clistr_push(cli, p, fname_dst,
453 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
455 cli_setup_bcc(cli, p);
458 if (!cli_receive_smb(cli)) {
462 if (cli_is_error(cli)) {
469 /****************************************************************************
471 ****************************************************************************/
473 bool cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
477 memset(cli->outbuf,'\0',smb_size);
478 memset(cli->inbuf,'\0',smb_size);
480 cli_set_message(cli->outbuf, 4, 0, true);
482 SCVAL(cli->outbuf,smb_com,SMBntrename);
483 SSVAL(cli->outbuf,smb_tid,cli->cnum);
484 cli_setup_packet(cli);
486 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
487 SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_RENAME);
489 p = smb_buf(cli->outbuf);
491 p += clistr_push(cli, p, fname_src,
492 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
494 p += clistr_push(cli, p, fname_dst,
495 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
497 cli_setup_bcc(cli, p);
500 if (!cli_receive_smb(cli)) {
504 if (cli_is_error(cli)) {
511 /****************************************************************************
513 ****************************************************************************/
515 bool cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
519 memset(cli->outbuf,'\0',smb_size);
520 memset(cli->inbuf,'\0',smb_size);
522 cli_set_message(cli->outbuf, 4, 0, true);
524 SCVAL(cli->outbuf,smb_com,SMBntrename);
525 SSVAL(cli->outbuf,smb_tid,cli->cnum);
526 cli_setup_packet(cli);
528 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
529 SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_HARD_LINK);
531 p = smb_buf(cli->outbuf);
533 p += clistr_push(cli, p, fname_src,
534 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
536 p += clistr_push(cli, p, fname_dst,
537 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
539 cli_setup_bcc(cli, p);
542 if (!cli_receive_smb(cli)) {
546 if (cli_is_error(cli)) {
553 /****************************************************************************
555 ****************************************************************************/
557 bool cli_unlink_full(struct cli_state *cli, const char *fname, uint16 attrs)
561 memset(cli->outbuf,'\0',smb_size);
562 memset(cli->inbuf,'\0',smb_size);
564 cli_set_message(cli->outbuf,1, 0, true);
566 SCVAL(cli->outbuf,smb_com,SMBunlink);
567 SSVAL(cli->outbuf,smb_tid,cli->cnum);
568 cli_setup_packet(cli);
570 SSVAL(cli->outbuf,smb_vwv0, attrs);
572 p = smb_buf(cli->outbuf);
574 p += clistr_push(cli, p, fname,
575 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
577 cli_setup_bcc(cli, p);
579 if (!cli_receive_smb(cli)) {
583 if (cli_is_error(cli)) {
590 /****************************************************************************
592 ****************************************************************************/
594 bool cli_unlink(struct cli_state *cli, const char *fname)
596 return cli_unlink_full(cli, fname, aSYSTEM | aHIDDEN);
599 /****************************************************************************
601 ****************************************************************************/
603 bool cli_mkdir(struct cli_state *cli, const char *dname)
607 memset(cli->outbuf,'\0',smb_size);
608 memset(cli->inbuf,'\0',smb_size);
610 cli_set_message(cli->outbuf,0, 0, true);
612 SCVAL(cli->outbuf,smb_com,SMBmkdir);
613 SSVAL(cli->outbuf,smb_tid,cli->cnum);
614 cli_setup_packet(cli);
616 p = smb_buf(cli->outbuf);
618 p += clistr_push(cli, p, dname,
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_rmdir(struct cli_state *cli, const char *dname)
643 memset(cli->outbuf,'\0',smb_size);
644 memset(cli->inbuf,'\0',smb_size);
646 cli_set_message(cli->outbuf,0, 0, true);
648 SCVAL(cli->outbuf,smb_com,SMBrmdir);
649 SSVAL(cli->outbuf,smb_tid,cli->cnum);
650 cli_setup_packet(cli);
652 p = smb_buf(cli->outbuf);
654 p += clistr_push(cli, p, dname,
655 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
657 cli_setup_bcc(cli, p);
660 if (!cli_receive_smb(cli)) {
664 if (cli_is_error(cli)) {
671 /****************************************************************************
672 Set or clear the delete on close flag.
673 ****************************************************************************/
675 int cli_nt_delete_on_close(struct cli_state *cli, int fnum, bool flag)
677 unsigned int data_len = 1;
678 unsigned int param_len = 6;
679 uint16 setup = TRANSACT2_SETFILEINFO;
682 char *rparam=NULL, *rdata=NULL;
684 memset(param, 0, param_len);
686 SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
690 if (!cli_send_trans(cli, SMBtrans2,
692 -1, 0, /* fid, flags */
693 &setup, 1, 0, /* setup, length, max */
694 param, param_len, 2, /* param, length, max */
695 (char *)&data, data_len, cli->max_xmit /* data, length, max */
700 if (!cli_receive_trans(cli, SMBtrans2,
702 &rdata, &data_len)) {
712 /****************************************************************************
713 Open a file - exposing the full horror of the NT API :-).
715 ****************************************************************************/
717 int cli_nt_create_full(struct cli_state *cli, const char *fname,
718 uint32 CreatFlags, uint32 DesiredAccess,
719 uint32 FileAttributes, uint32 ShareAccess,
720 uint32 CreateDisposition, uint32 CreateOptions,
726 memset(cli->outbuf,'\0',smb_size);
727 memset(cli->inbuf,'\0',smb_size);
729 cli_set_message(cli->outbuf,24,0, true);
731 SCVAL(cli->outbuf,smb_com,SMBntcreateX);
732 SSVAL(cli->outbuf,smb_tid,cli->cnum);
733 cli_setup_packet(cli);
735 SSVAL(cli->outbuf,smb_vwv0,0xFF);
736 if (cli->use_oplocks)
737 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
739 SIVAL(cli->outbuf,smb_ntcreate_Flags, CreatFlags);
740 SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
741 SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, DesiredAccess);
742 SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, FileAttributes);
743 SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, ShareAccess);
744 SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, CreateDisposition);
745 SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, CreateOptions);
746 SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
747 SCVAL(cli->outbuf,smb_ntcreate_SecurityFlags, SecurityFlags);
749 p = smb_buf(cli->outbuf);
750 /* this alignment and termination is critical for netapp filers. Don't change */
751 p += clistr_align_out(cli, p, 0);
752 len = clistr_push(cli, p, fname,
753 cli->bufsize - PTR_DIFF(p,cli->outbuf), 0);
755 SSVAL(cli->outbuf,smb_ntcreate_NameLength, len);
756 /* sigh. this copes with broken netapp filer behaviour */
757 p += clistr_push(cli, p, "",
758 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
760 cli_setup_bcc(cli, p);
763 if (!cli_receive_smb(cli)) {
767 if (cli_is_error(cli)) {
771 return SVAL(cli->inbuf,smb_vwv2 + 1);
774 struct cli_ntcreate_state {
779 static void cli_ntcreate_done(struct tevent_req *subreq);
781 struct tevent_req *cli_ntcreate_send(TALLOC_CTX *mem_ctx,
782 struct event_context *ev,
783 struct cli_state *cli,
786 uint32_t DesiredAccess,
787 uint32_t FileAttributes,
788 uint32_t ShareAccess,
789 uint32_t CreateDisposition,
790 uint32_t CreateOptions,
791 uint8_t SecurityFlags)
793 struct tevent_req *req, *subreq;
794 struct cli_ntcreate_state *state;
797 size_t converted_len;
799 req = tevent_req_create(mem_ctx, &state, struct cli_ntcreate_state);
805 SCVAL(vwv+0, 0, 0xFF);
810 if (cli->use_oplocks) {
811 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
813 SIVAL(vwv+3, 1, CreatFlags);
814 SIVAL(vwv+5, 1, 0x0); /* RootDirectoryFid */
815 SIVAL(vwv+7, 1, DesiredAccess);
816 SIVAL(vwv+9, 1, 0x0); /* AllocationSize */
817 SIVAL(vwv+11, 1, 0x0); /* AllocationSize */
818 SIVAL(vwv+13, 1, FileAttributes);
819 SIVAL(vwv+15, 1, ShareAccess);
820 SIVAL(vwv+17, 1, CreateDisposition);
821 SIVAL(vwv+19, 1, CreateOptions);
822 SIVAL(vwv+21, 1, 0x02); /* ImpersonationLevel */
823 SCVAL(vwv+23, 1, SecurityFlags);
825 bytes = talloc_array(state, uint8_t, 0);
826 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli),
827 fname, strlen(fname)+1,
830 /* sigh. this copes with broken netapp filer behaviour */
831 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), "", 1, NULL);
833 if (tevent_req_nomem(bytes, req)) {
834 return tevent_req_post(req, ev);
837 SIVAL(vwv+2, 1, converted_len);
839 subreq = cli_smb_send(state, ev, cli, SMBntcreateX, 0, 24, vwv,
840 talloc_get_size(bytes), bytes);
841 if (tevent_req_nomem(subreq, req)) {
842 return tevent_req_post(req, ev);
844 tevent_req_set_callback(subreq, cli_ntcreate_done, req);
848 static void cli_ntcreate_done(struct tevent_req *subreq)
850 struct tevent_req *req = tevent_req_callback_data(
851 subreq, struct tevent_req);
852 struct cli_ntcreate_state *state = tevent_req_data(
853 req, struct cli_ntcreate_state);
860 status = cli_smb_recv(subreq, 3, &wct, &vwv, &num_bytes, &bytes);
861 if (!NT_STATUS_IS_OK(status)) {
863 tevent_req_nterror(req, status);
866 state->fnum = SVAL(vwv+2, 1);
867 tevent_req_done(req);
870 NTSTATUS cli_ntcreate_recv(struct tevent_req *req, uint16_t *pfnum)
872 struct cli_ntcreate_state *state = tevent_req_data(
873 req, struct cli_ntcreate_state);
876 if (tevent_req_is_nterror(req, &status)) {
879 *pfnum = state->fnum;
883 NTSTATUS cli_ntcreate(struct cli_state *cli,
886 uint32_t DesiredAccess,
887 uint32_t FileAttributes,
888 uint32_t ShareAccess,
889 uint32_t CreateDisposition,
890 uint32_t CreateOptions,
891 uint8_t SecurityFlags,
894 TALLOC_CTX *frame = talloc_stackframe();
895 struct event_context *ev;
896 struct tevent_req *req;
897 NTSTATUS status = NT_STATUS_OK;
899 if (cli_has_async_calls(cli)) {
901 * Can't use sync call while an async call is in flight
903 status = NT_STATUS_INVALID_PARAMETER;
907 ev = event_context_init(frame);
909 status = NT_STATUS_NO_MEMORY;
913 req = cli_ntcreate_send(frame, ev, cli, fname, CreatFlags,
914 DesiredAccess, FileAttributes, ShareAccess,
915 CreateDisposition, CreateOptions,
918 status = NT_STATUS_NO_MEMORY;
922 if (!tevent_req_poll(req, ev)) {
923 status = map_nt_error_from_unix(errno);
927 status = cli_ntcreate_recv(req, pfid);
930 if (!NT_STATUS_IS_OK(status)) {
931 cli_set_error(cli, status);
936 /****************************************************************************
938 ****************************************************************************/
940 int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess)
942 return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
943 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0);
946 uint8_t *smb_bytes_push_str(uint8_t *buf, bool ucs2,
947 const char *str, size_t str_len,
948 size_t *pconverted_size)
952 size_t converted_size;
958 buflen = talloc_get_size(buf);
960 * We're pushing into an SMB buffer, align odd
962 if (ucs2 && (buflen % 2 == 0)) {
963 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t, buflen + 1);
971 if (!convert_string_allocate(talloc_tos(), CH_UNIX,
972 ucs2 ? CH_UTF16LE : CH_DOS,
973 str, str_len, &converted,
974 &converted_size, true)) {
978 buf = TALLOC_REALLOC_ARRAY(NULL, buf, uint8_t,
979 buflen + converted_size);
981 TALLOC_FREE(converted);
985 memcpy(buf + buflen, converted, converted_size);
987 TALLOC_FREE(converted);
989 if (pconverted_size) {
990 *pconverted_size = converted_size;
996 /****************************************************************************
998 WARNING: if you open with O_WRONLY then getattrE won't work!
999 ****************************************************************************/
1001 struct cli_open_state {
1007 static void cli_open_done(struct tevent_req *subreq);
1009 struct tevent_req *cli_open_create(TALLOC_CTX *mem_ctx,
1010 struct event_context *ev,
1011 struct cli_state *cli, const char *fname,
1012 int flags, int share_mode,
1013 struct tevent_req **psmbreq)
1015 struct tevent_req *req, *subreq;
1016 struct cli_open_state *state;
1018 unsigned accessmode;
1019 uint8_t additional_flags;
1022 req = tevent_req_create(mem_ctx, &state, struct cli_open_state);
1028 if (flags & O_CREAT) {
1031 if (!(flags & O_EXCL)) {
1032 if (flags & O_TRUNC)
1038 accessmode = (share_mode<<4);
1040 if ((flags & O_ACCMODE) == O_RDWR) {
1042 } else if ((flags & O_ACCMODE) == O_WRONLY) {
1047 if ((flags & O_SYNC) == O_SYNC) {
1048 accessmode |= (1<<14);
1052 if (share_mode == DENY_FCB) {
1056 SCVAL(state->vwv + 0, 0, 0xFF);
1057 SCVAL(state->vwv + 0, 1, 0);
1058 SSVAL(state->vwv + 1, 0, 0);
1059 SSVAL(state->vwv + 2, 0, 0); /* no additional info */
1060 SSVAL(state->vwv + 3, 0, accessmode);
1061 SSVAL(state->vwv + 4, 0, aSYSTEM | aHIDDEN);
1062 SSVAL(state->vwv + 5, 0, 0);
1063 SIVAL(state->vwv + 6, 0, 0);
1064 SSVAL(state->vwv + 8, 0, openfn);
1065 SIVAL(state->vwv + 9, 0, 0);
1066 SIVAL(state->vwv + 11, 0, 0);
1067 SIVAL(state->vwv + 13, 0, 0);
1069 additional_flags = 0;
1071 if (cli->use_oplocks) {
1072 /* if using oplocks then ask for a batch oplock via
1073 core and extended methods */
1075 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
1076 SSVAL(state->vwv+2, 0, SVAL(state->vwv+2, 0) | 6);
1079 bytes = talloc_array(state, uint8_t, 0);
1080 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1081 strlen(fname)+1, NULL);
1083 if (tevent_req_nomem(bytes, req)) {
1084 return tevent_req_post(req, ev);
1087 state->bytes.iov_base = bytes;
1088 state->bytes.iov_len = talloc_get_size(bytes);
1090 subreq = cli_smb_req_create(state, ev, cli, SMBopenX, additional_flags,
1091 15, state->vwv, 1, &state->bytes);
1092 if (subreq == NULL) {
1096 tevent_req_set_callback(subreq, cli_open_done, req);
1101 struct tevent_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1102 struct cli_state *cli, const char *fname,
1103 int flags, int share_mode)
1105 struct tevent_req *req, *subreq;
1107 req = cli_open_create(mem_ctx, ev, cli, fname, flags, share_mode,
1109 if ((req == NULL) || !cli_smb_req_send(subreq)) {
1116 static void cli_open_done(struct tevent_req *subreq)
1118 struct tevent_req *req = tevent_req_callback_data(
1119 subreq, struct tevent_req);
1120 struct cli_open_state *state = tevent_req_data(
1121 req, struct cli_open_state);
1126 status = cli_smb_recv(subreq, 3, &wct, &vwv, NULL, NULL);
1127 if (!NT_STATUS_IS_OK(status)) {
1128 TALLOC_FREE(subreq);
1129 tevent_req_nterror(req, status);
1132 state->fnum = SVAL(vwv+2, 0);
1133 tevent_req_done(req);
1136 NTSTATUS cli_open_recv(struct tevent_req *req, int *fnum)
1138 struct cli_open_state *state = tevent_req_data(
1139 req, struct cli_open_state);
1142 if (tevent_req_is_nterror(req, &status)) {
1145 *fnum = state->fnum;
1146 return NT_STATUS_OK;
1149 int cli_open(struct cli_state *cli, const char *fname, int flags,
1152 TALLOC_CTX *frame = talloc_stackframe();
1153 struct event_context *ev;
1154 struct tevent_req *req;
1155 NTSTATUS status = NT_STATUS_OK;
1158 if (cli_has_async_calls(cli)) {
1160 * Can't use sync call while an async call is in flight
1162 status = NT_STATUS_INVALID_PARAMETER;
1166 ev = event_context_init(frame);
1168 status = NT_STATUS_NO_MEMORY;
1172 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
1174 status = NT_STATUS_NO_MEMORY;
1178 if (!tevent_req_poll(req, ev)) {
1179 status = map_nt_error_from_unix(errno);
1183 cli_open_recv(req, &result);
1186 if (!NT_STATUS_IS_OK(status)) {
1187 cli_set_error(cli, status);
1192 /****************************************************************************
1194 ****************************************************************************/
1196 struct cli_close_state {
1200 static void cli_close_done(struct tevent_req *subreq);
1202 struct tevent_req *cli_close_create(TALLOC_CTX *mem_ctx,
1203 struct event_context *ev,
1204 struct cli_state *cli, int fnum,
1205 struct tevent_req **psubreq)
1207 struct tevent_req *req, *subreq;
1208 struct cli_close_state *state;
1210 req = tevent_req_create(mem_ctx, &state, struct cli_close_state);
1214 SSVAL(state->vwv+0, 0, fnum);
1215 SIVALS(state->vwv+1, 0, -1);
1217 subreq = cli_smb_req_create(state, ev, cli, SMBclose, 0, 3, state->vwv,
1219 if (subreq == NULL) {
1223 tevent_req_set_callback(subreq, cli_close_done, req);
1228 struct tevent_req *cli_close_send(TALLOC_CTX *mem_ctx,
1229 struct event_context *ev,
1230 struct cli_state *cli, int fnum)
1232 struct tevent_req *req, *subreq;
1234 req = cli_close_create(mem_ctx, ev, cli, fnum, &subreq);
1235 if ((req == NULL) || !cli_smb_req_send(subreq)) {
1242 static void cli_close_done(struct tevent_req *subreq)
1244 struct tevent_req *req = tevent_req_callback_data(
1245 subreq, struct tevent_req);
1248 status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
1249 TALLOC_FREE(subreq);
1250 if (!NT_STATUS_IS_OK(status)) {
1251 tevent_req_nterror(req, status);
1254 tevent_req_done(req);
1257 NTSTATUS cli_close_recv(struct tevent_req *req)
1259 return tevent_req_simple_recv_ntstatus(req);
1262 bool cli_close(struct cli_state *cli, int fnum)
1264 TALLOC_CTX *frame = talloc_stackframe();
1265 struct event_context *ev;
1266 struct tevent_req *req;
1267 NTSTATUS status = NT_STATUS_OK;
1268 bool result = false;
1270 if (cli_has_async_calls(cli)) {
1272 * Can't use sync call while an async call is in flight
1274 status = NT_STATUS_INVALID_PARAMETER;
1278 ev = event_context_init(frame);
1280 status = NT_STATUS_NO_MEMORY;
1284 req = cli_close_send(frame, ev, cli, fnum);
1286 status = NT_STATUS_NO_MEMORY;
1290 if (!tevent_req_poll(req, ev)) {
1291 status = map_nt_error_from_unix(errno);
1295 result = NT_STATUS_IS_OK(cli_close_recv(req));
1298 if (!NT_STATUS_IS_OK(status)) {
1299 cli_set_error(cli, status);
1304 /****************************************************************************
1305 Truncate a file to a specified size
1306 ****************************************************************************/
1308 bool cli_ftruncate(struct cli_state *cli, int fnum, uint64_t size)
1310 unsigned int param_len = 6;
1311 unsigned int data_len = 8;
1312 uint16 setup = TRANSACT2_SETFILEINFO;
1314 unsigned char data[8];
1315 char *rparam=NULL, *rdata=NULL;
1316 int saved_timeout = cli->timeout;
1318 SSVAL(param,0,fnum);
1319 SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
1322 SBVAL(data, 0, size);
1324 if (!cli_send_trans(cli, SMBtrans2,
1326 -1, 0, /* fid, flags */
1327 &setup, 1, 0, /* setup, length, max */
1328 param, param_len, 2, /* param, length, max */
1329 (char *)&data, data_len,/* data, length, ... */
1330 cli->max_xmit)) { /* ... max */
1331 cli->timeout = saved_timeout;
1335 if (!cli_receive_trans(cli, SMBtrans2,
1336 &rparam, ¶m_len,
1337 &rdata, &data_len)) {
1338 cli->timeout = saved_timeout;
1344 cli->timeout = saved_timeout;
1353 /****************************************************************************
1354 send a lock with a specified locktype
1355 this is used for testing LOCKING_ANDX_CANCEL_LOCK
1356 ****************************************************************************/
1358 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
1359 uint32 offset, uint32 len,
1360 int timeout, unsigned char locktype)
1363 int saved_timeout = cli->timeout;
1365 memset(cli->outbuf,'\0',smb_size);
1366 memset(cli->inbuf,'\0', smb_size);
1368 cli_set_message(cli->outbuf,8,0,True);
1370 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1371 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1372 cli_setup_packet(cli);
1374 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1375 SSVAL(cli->outbuf,smb_vwv2,fnum);
1376 SCVAL(cli->outbuf,smb_vwv3,locktype);
1377 SIVALS(cli->outbuf, smb_vwv4, timeout);
1378 SSVAL(cli->outbuf,smb_vwv6,0);
1379 SSVAL(cli->outbuf,smb_vwv7,1);
1381 p = smb_buf(cli->outbuf);
1382 SSVAL(p, 0, cli->pid);
1383 SIVAL(p, 2, offset);
1388 cli_setup_bcc(cli, p);
1393 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
1396 if (!cli_receive_smb(cli)) {
1397 cli->timeout = saved_timeout;
1398 return NT_STATUS_UNSUCCESSFUL;
1401 cli->timeout = saved_timeout;
1403 return cli_nt_error(cli);
1406 /****************************************************************************
1408 note that timeout is in units of 2 milliseconds
1409 ****************************************************************************/
1411 bool cli_lock(struct cli_state *cli, int fnum,
1412 uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
1415 int saved_timeout = cli->timeout;
1417 memset(cli->outbuf,'\0',smb_size);
1418 memset(cli->inbuf,'\0', smb_size);
1420 cli_set_message(cli->outbuf,8,0,True);
1422 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1423 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1424 cli_setup_packet(cli);
1426 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1427 SSVAL(cli->outbuf,smb_vwv2,fnum);
1428 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
1429 SIVALS(cli->outbuf, smb_vwv4, timeout);
1430 SSVAL(cli->outbuf,smb_vwv6,0);
1431 SSVAL(cli->outbuf,smb_vwv7,1);
1433 p = smb_buf(cli->outbuf);
1434 SSVAL(p, 0, cli->pid);
1435 SIVAL(p, 2, offset);
1440 cli_setup_bcc(cli, p);
1445 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
1448 if (!cli_receive_smb(cli)) {
1449 cli->timeout = saved_timeout;
1453 cli->timeout = saved_timeout;
1455 if (cli_is_error(cli)) {
1462 /****************************************************************************
1464 ****************************************************************************/
1466 bool cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
1470 memset(cli->outbuf,'\0',smb_size);
1471 memset(cli->inbuf,'\0',smb_size);
1473 cli_set_message(cli->outbuf,8,0,True);
1475 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1476 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1477 cli_setup_packet(cli);
1479 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1480 SSVAL(cli->outbuf,smb_vwv2,fnum);
1481 SCVAL(cli->outbuf,smb_vwv3,0);
1482 SIVALS(cli->outbuf, smb_vwv4, 0);
1483 SSVAL(cli->outbuf,smb_vwv6,1);
1484 SSVAL(cli->outbuf,smb_vwv7,0);
1486 p = smb_buf(cli->outbuf);
1487 SSVAL(p, 0, cli->pid);
1488 SIVAL(p, 2, offset);
1491 cli_setup_bcc(cli, p);
1493 if (!cli_receive_smb(cli)) {
1497 if (cli_is_error(cli)) {
1504 /****************************************************************************
1505 Lock a file with 64 bit offsets.
1506 ****************************************************************************/
1508 bool cli_lock64(struct cli_state *cli, int fnum,
1509 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
1512 int saved_timeout = cli->timeout;
1515 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1516 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1519 ltype = (lock_type == READ_LOCK? 1 : 0);
1520 ltype |= LOCKING_ANDX_LARGE_FILES;
1522 memset(cli->outbuf,'\0',smb_size);
1523 memset(cli->inbuf,'\0', smb_size);
1525 cli_set_message(cli->outbuf,8,0,True);
1527 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1528 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1529 cli_setup_packet(cli);
1531 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1532 SSVAL(cli->outbuf,smb_vwv2,fnum);
1533 SCVAL(cli->outbuf,smb_vwv3,ltype);
1534 SIVALS(cli->outbuf, smb_vwv4, timeout);
1535 SSVAL(cli->outbuf,smb_vwv6,0);
1536 SSVAL(cli->outbuf,smb_vwv7,1);
1538 p = smb_buf(cli->outbuf);
1539 SIVAL(p, 0, cli->pid);
1540 SOFF_T_R(p, 4, offset);
1541 SOFF_T_R(p, 12, len);
1544 cli_setup_bcc(cli, p);
1548 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1551 if (!cli_receive_smb(cli)) {
1552 cli->timeout = saved_timeout;
1556 cli->timeout = saved_timeout;
1558 if (cli_is_error(cli)) {
1565 /****************************************************************************
1566 Unlock a file with 64 bit offsets.
1567 ****************************************************************************/
1569 bool cli_unlock64(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
1573 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1574 return cli_unlock(cli, fnum, offset, len);
1577 memset(cli->outbuf,'\0',smb_size);
1578 memset(cli->inbuf,'\0',smb_size);
1580 cli_set_message(cli->outbuf,8,0,True);
1582 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1583 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1584 cli_setup_packet(cli);
1586 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1587 SSVAL(cli->outbuf,smb_vwv2,fnum);
1588 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1589 SIVALS(cli->outbuf, smb_vwv4, 0);
1590 SSVAL(cli->outbuf,smb_vwv6,1);
1591 SSVAL(cli->outbuf,smb_vwv7,0);
1593 p = smb_buf(cli->outbuf);
1594 SIVAL(p, 0, cli->pid);
1595 SOFF_T_R(p, 4, offset);
1596 SOFF_T_R(p, 12, len);
1598 cli_setup_bcc(cli, p);
1600 if (!cli_receive_smb(cli)) {
1604 if (cli_is_error(cli)) {
1611 /****************************************************************************
1612 Get/unlock a POSIX lock on a file - internal function.
1613 ****************************************************************************/
1615 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
1616 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
1618 unsigned int param_len = 4;
1619 unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1620 uint16 setup = TRANSACT2_SETFILEINFO;
1622 unsigned char data[POSIX_LOCK_DATA_SIZE];
1623 char *rparam=NULL, *rdata=NULL;
1624 int saved_timeout = cli->timeout;
1626 SSVAL(param,0,fnum);
1627 SSVAL(param,2,SMB_SET_POSIX_LOCK);
1629 switch (lock_type) {
1631 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
1634 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
1637 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
1644 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
1645 cli->timeout = 0x7FFFFFFF;
1647 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
1650 SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
1651 SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
1652 SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
1654 if (!cli_send_trans(cli, SMBtrans2,
1656 -1, 0, /* fid, flags */
1657 &setup, 1, 0, /* setup, length, max */
1658 param, param_len, 2, /* param, length, max */
1659 (char *)&data, data_len, cli->max_xmit /* data, length, max */
1661 cli->timeout = saved_timeout;
1665 if (!cli_receive_trans(cli, SMBtrans2,
1666 &rparam, ¶m_len,
1667 &rdata, &data_len)) {
1668 cli->timeout = saved_timeout;
1674 cli->timeout = saved_timeout;
1682 /****************************************************************************
1684 ****************************************************************************/
1686 bool cli_posix_lock(struct cli_state *cli, int fnum,
1687 uint64_t offset, uint64_t len,
1688 bool wait_lock, enum brl_type lock_type)
1690 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
1693 return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
1696 /****************************************************************************
1697 POSIX Unlock a file.
1698 ****************************************************************************/
1700 bool cli_posix_unlock(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
1702 return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
1705 /****************************************************************************
1706 POSIX Get any lock covering a file.
1707 ****************************************************************************/
1709 bool cli_posix_getlock(struct cli_state *cli, int fnum, uint64_t *poffset, uint64_t *plen)
1714 /****************************************************************************
1715 Do a SMBgetattrE call.
1716 ****************************************************************************/
1718 bool cli_getattrE(struct cli_state *cli, int fd,
1719 uint16 *attr, SMB_OFF_T *size,
1720 time_t *change_time,
1721 time_t *access_time,
1724 memset(cli->outbuf,'\0',smb_size);
1725 memset(cli->inbuf,'\0',smb_size);
1727 cli_set_message(cli->outbuf,1,0,True);
1729 SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1730 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1731 cli_setup_packet(cli);
1733 SSVAL(cli->outbuf,smb_vwv0,fd);
1736 if (!cli_receive_smb(cli)) {
1740 if (cli_is_error(cli)) {
1745 *size = IVAL(cli->inbuf, smb_vwv6);
1749 *attr = SVAL(cli->inbuf,smb_vwv10);
1753 *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
1757 *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
1761 *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
1767 /****************************************************************************
1769 ****************************************************************************/
1771 bool cli_getatr(struct cli_state *cli, const char *fname,
1772 uint16 *attr, SMB_OFF_T *size, time_t *write_time)
1776 memset(cli->outbuf,'\0',smb_size);
1777 memset(cli->inbuf,'\0',smb_size);
1779 cli_set_message(cli->outbuf,0,0,True);
1781 SCVAL(cli->outbuf,smb_com,SMBgetatr);
1782 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1783 cli_setup_packet(cli);
1785 p = smb_buf(cli->outbuf);
1787 p += clistr_push(cli, p, fname,
1788 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1790 cli_setup_bcc(cli, p);
1793 if (!cli_receive_smb(cli)) {
1797 if (cli_is_error(cli)) {
1802 *size = IVAL(cli->inbuf, smb_vwv3);
1806 *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
1810 *attr = SVAL(cli->inbuf,smb_vwv0);
1816 /****************************************************************************
1817 Do a SMBsetattrE call.
1818 ****************************************************************************/
1820 bool cli_setattrE(struct cli_state *cli, int fd,
1828 memset(cli->outbuf,'\0',smb_size);
1829 memset(cli->inbuf,'\0',smb_size);
1831 cli_set_message(cli->outbuf,7,0,True);
1833 SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1834 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1835 cli_setup_packet(cli);
1837 SSVAL(cli->outbuf,smb_vwv0, fd);
1838 cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
1839 cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
1840 cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
1842 p = smb_buf(cli->outbuf);
1845 cli_setup_bcc(cli, p);
1848 if (!cli_receive_smb(cli)) {
1852 if (cli_is_error(cli)) {
1859 /****************************************************************************
1860 Do a SMBsetatr call.
1861 ****************************************************************************/
1863 bool cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1867 memset(cli->outbuf,'\0',smb_size);
1868 memset(cli->inbuf,'\0',smb_size);
1870 cli_set_message(cli->outbuf,8,0,True);
1872 SCVAL(cli->outbuf,smb_com,SMBsetatr);
1873 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1874 cli_setup_packet(cli);
1876 SSVAL(cli->outbuf,smb_vwv0, attr);
1877 cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
1879 p = smb_buf(cli->outbuf);
1881 p += clistr_push(cli, p, fname,
1882 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1885 cli_setup_bcc(cli, p);
1888 if (!cli_receive_smb(cli)) {
1892 if (cli_is_error(cli)) {
1899 /****************************************************************************
1900 Check for existance of a dir.
1901 ****************************************************************************/
1903 bool cli_chkpath(struct cli_state *cli, const char *path)
1907 TALLOC_CTX *frame = talloc_stackframe();
1909 path2 = talloc_strdup(frame, path);
1914 trim_char(path2,'\0','\\');
1916 path2 = talloc_strdup(frame, "\\");
1923 memset(cli->outbuf,'\0',smb_size);
1924 cli_set_message(cli->outbuf,0,0,True);
1925 SCVAL(cli->outbuf,smb_com,SMBcheckpath);
1926 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1927 cli_setup_packet(cli);
1928 p = smb_buf(cli->outbuf);
1930 p += clistr_push(cli, p, path2,
1931 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1933 cli_setup_bcc(cli, p);
1936 if (!cli_receive_smb(cli)) {
1943 if (cli_is_error(cli)) return False;
1948 /****************************************************************************
1950 ****************************************************************************/
1952 bool cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1954 memset(cli->outbuf,'\0',smb_size);
1955 cli_set_message(cli->outbuf,0,0,True);
1956 SCVAL(cli->outbuf,smb_com,SMBdskattr);
1957 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1958 cli_setup_packet(cli);
1961 if (!cli_receive_smb(cli)) {
1965 *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1966 *total = SVAL(cli->inbuf,smb_vwv0);
1967 *avail = SVAL(cli->inbuf,smb_vwv3);
1972 /****************************************************************************
1973 Create and open a temporary file.
1974 ****************************************************************************/
1976 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1981 memset(cli->outbuf,'\0',smb_size);
1982 memset(cli->inbuf,'\0',smb_size);
1984 cli_set_message(cli->outbuf,3,0,True);
1986 SCVAL(cli->outbuf,smb_com,SMBctemp);
1987 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1988 cli_setup_packet(cli);
1990 SSVAL(cli->outbuf,smb_vwv0,0);
1991 SIVALS(cli->outbuf,smb_vwv1,-1);
1993 p = smb_buf(cli->outbuf);
1995 p += clistr_push(cli, p, path,
1996 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1998 cli_setup_bcc(cli, p);
2001 if (!cli_receive_smb(cli)) {
2005 if (cli_is_error(cli)) {
2009 /* despite the spec, the result has a -1, followed by
2010 length, followed by name */
2011 p = smb_buf(cli->inbuf);
2013 len = smb_buflen(cli->inbuf) - 4;
2014 if (len <= 0 || len > PATH_MAX) return -1;
2017 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
2021 clistr_pull(cli->inbuf, path2, p,
2022 len+1, len, STR_ASCII);
2026 return SVAL(cli->inbuf,smb_vwv0);
2030 send a raw ioctl - used by the torture code
2032 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
2034 memset(cli->outbuf,'\0',smb_size);
2035 memset(cli->inbuf,'\0',smb_size);
2037 cli_set_message(cli->outbuf, 3, 0, True);
2038 SCVAL(cli->outbuf,smb_com,SMBioctl);
2039 cli_setup_packet(cli);
2041 SSVAL(cli->outbuf, smb_vwv0, fnum);
2042 SSVAL(cli->outbuf, smb_vwv1, code>>16);
2043 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
2046 if (!cli_receive_smb(cli)) {
2047 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
2050 if (cli_is_error(cli)) {
2051 return cli_nt_error(cli);
2054 *blob = data_blob_null;
2056 return NT_STATUS_OK;
2059 /*********************************************************
2060 Set an extended attribute utility fn.
2061 *********************************************************/
2063 static bool cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
2064 const char *ea_name, const char *ea_val, size_t ea_len)
2066 unsigned int data_len = 0;
2068 char *rparam=NULL, *rdata=NULL;
2070 size_t ea_namelen = strlen(ea_name);
2072 if (ea_namelen == 0 && ea_len == 0) {
2074 data = (char *)SMB_MALLOC(data_len);
2079 SIVAL(p,0,data_len);
2081 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
2082 data = (char *)SMB_MALLOC(data_len);
2087 SIVAL(p,0,data_len);
2089 SCVAL(p, 0, 0); /* EA flags. */
2090 SCVAL(p, 1, ea_namelen);
2091 SSVAL(p, 2, ea_len);
2092 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
2093 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
2096 if (!cli_send_trans(cli, SMBtrans2,
2098 -1, 0, /* fid, flags */
2099 &setup, 1, 0, /* setup, length, max */
2100 param, param_len, 2, /* param, length, max */
2101 data, data_len, cli->max_xmit /* data, length, max */
2107 if (!cli_receive_trans(cli, SMBtrans2,
2108 &rparam, ¶m_len,
2109 &rdata, &data_len)) {
2121 /*********************************************************
2122 Set an extended attribute on a pathname.
2123 *********************************************************/
2125 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
2127 uint16 setup = TRANSACT2_SETPATHINFO;
2128 unsigned int param_len = 0;
2130 size_t srclen = 2*(strlen(path)+1);
2134 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2138 memset(param, '\0', 6);
2139 SSVAL(param,0,SMB_INFO_SET_EA);
2142 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2143 param_len = PTR_DIFF(p, param);
2145 ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
2150 /*********************************************************
2151 Set an extended attribute on an fnum.
2152 *********************************************************/
2154 bool cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
2157 uint16 setup = TRANSACT2_SETFILEINFO;
2159 memset(param, 0, 6);
2160 SSVAL(param,0,fnum);
2161 SSVAL(param,2,SMB_INFO_SET_EA);
2163 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
2166 /*********************************************************
2167 Get an extended attribute list utility fn.
2168 *********************************************************/
2170 static bool cli_get_ea_list(struct cli_state *cli,
2171 uint16 setup, char *param, unsigned int param_len,
2174 struct ea_struct **pea_list)
2176 unsigned int data_len = 0;
2177 unsigned int rparam_len, rdata_len;
2178 char *rparam=NULL, *rdata=NULL;
2183 struct ea_struct *ea_list;
2190 if (!cli_send_trans(cli, SMBtrans2,
2192 -1, 0, /* fid, flags */
2193 &setup, 1, 0, /* setup, length, max */
2194 param, param_len, 10, /* param, length, max */
2195 NULL, data_len, cli->max_xmit /* data, length, max */
2200 if (!cli_receive_trans(cli, SMBtrans2,
2201 &rparam, &rparam_len,
2202 &rdata, &rdata_len)) {
2206 if (!rdata || rdata_len < 4) {
2210 ea_size = (size_t)IVAL(rdata,0);
2211 if (ea_size > rdata_len) {
2216 /* No EA's present. */
2224 /* Validate the EA list and count it. */
2225 for (num_eas = 0; ea_size >= 4; num_eas++) {
2226 unsigned int ea_namelen = CVAL(p,1);
2227 unsigned int ea_valuelen = SVAL(p,2);
2228 if (ea_namelen == 0) {
2231 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
2234 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
2235 p += 4 + ea_namelen + 1 + ea_valuelen;
2243 *pnum_eas = num_eas;
2245 /* Caller only wants number of EA's. */
2250 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
2255 ea_size = (size_t)IVAL(rdata,0);
2258 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
2259 struct ea_struct *ea = &ea_list[num_eas];
2260 fstring unix_ea_name;
2261 unsigned int ea_namelen = CVAL(p,1);
2262 unsigned int ea_valuelen = SVAL(p,2);
2264 ea->flags = CVAL(p,0);
2265 unix_ea_name[0] = '\0';
2266 pull_ascii_fstring(unix_ea_name, p + 4);
2267 ea->name = talloc_strdup(ctx, unix_ea_name);
2268 /* Ensure the value is null terminated (in case it's a string). */
2269 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
2270 if (!ea->value.data) {
2274 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
2276 ea->value.data[ea_valuelen] = 0;
2278 p += 4 + ea_namelen + 1 + ea_valuelen;
2281 *pea_list = ea_list;
2291 /*********************************************************
2292 Get an extended attribute list from a pathname.
2293 *********************************************************/
2295 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
2298 struct ea_struct **pea_list)
2300 uint16 setup = TRANSACT2_QPATHINFO;
2301 unsigned int param_len = 0;
2304 size_t srclen = 2*(strlen(path)+1);
2307 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2313 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
2315 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2316 param_len = PTR_DIFF(p, param);
2318 ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
2323 /*********************************************************
2324 Get an extended attribute list from an fnum.
2325 *********************************************************/
2327 bool cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
2330 struct ea_struct **pea_list)
2332 uint16 setup = TRANSACT2_QFILEINFO;
2335 memset(param, 0, 6);
2336 SSVAL(param,0,fnum);
2337 SSVAL(param,2,SMB_INFO_SET_EA);
2339 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
2342 /****************************************************************************
2343 Convert open "flags" arg to uint32 on wire.
2344 ****************************************************************************/
2346 static uint32 open_flags_to_wire(int flags)
2348 int open_mode = flags & O_ACCMODE;
2351 switch (open_mode) {
2353 ret |= SMB_O_WRONLY;
2360 ret |= SMB_O_RDONLY;
2364 if (flags & O_CREAT) {
2367 if (flags & O_EXCL) {
2370 if (flags & O_TRUNC) {
2374 if (flags & O_SYNC) {
2378 if (flags & O_APPEND) {
2379 ret |= SMB_O_APPEND;
2381 #if defined(O_DIRECT)
2382 if (flags & O_DIRECT) {
2383 ret |= SMB_O_DIRECT;
2386 #if defined(O_DIRECTORY)
2387 if (flags & O_DIRECTORY) {
2388 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2389 ret |= SMB_O_DIRECTORY;
2395 /****************************************************************************
2396 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
2397 ****************************************************************************/
2399 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
2401 unsigned int data_len = 0;
2402 unsigned int param_len = 0;
2403 uint16 setup = TRANSACT2_SETPATHINFO;
2406 char *rparam=NULL, *rdata=NULL;
2409 uint32 wire_flags = open_flags_to_wire(flags);
2410 size_t srclen = 2*(strlen(fname)+1);
2412 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2416 memset(param, '\0', 6);
2417 SSVAL(param,0, SMB_POSIX_PATH_OPEN);
2420 p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2421 param_len = PTR_DIFF(p, param);
2424 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2425 wire_flags |= SMB_O_DIRECTORY;
2429 SIVAL(p,0,0); /* No oplock. */
2430 SIVAL(p,4,wire_flags);
2431 SIVAL(p,8,unix_perms_to_wire(mode));
2432 SIVAL(p,12,0); /* Top bits of perms currently undefined. */
2433 SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
2437 if (!cli_send_trans(cli, SMBtrans2,
2439 -1, 0, /* fid, flags */
2440 &setup, 1, 0, /* setup, length, max */
2441 param, param_len, 2, /* param, length, max */
2442 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2450 if (!cli_receive_trans(cli, SMBtrans2,
2451 &rparam, ¶m_len,
2452 &rdata, &data_len)) {
2456 fnum = SVAL(rdata,2);
2464 /****************************************************************************
2465 open - POSIX semantics.
2466 ****************************************************************************/
2468 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
2470 return cli_posix_open_internal(cli, fname, flags, mode, False);
2473 /****************************************************************************
2474 mkdir - POSIX semantics.
2475 ****************************************************************************/
2477 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
2479 return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
2482 /****************************************************************************
2483 unlink or rmdir - POSIX semantics.
2484 ****************************************************************************/
2486 static bool cli_posix_unlink_internal(struct cli_state *cli, const char *fname, bool is_dir)
2488 unsigned int data_len = 0;
2489 unsigned int param_len = 0;
2490 uint16 setup = TRANSACT2_SETPATHINFO;
2493 char *rparam=NULL, *rdata=NULL;
2495 size_t srclen = 2*(strlen(fname)+1);
2497 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2501 memset(param, '\0', 6);
2502 SSVAL(param,0, SMB_POSIX_PATH_UNLINK);
2505 p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2506 param_len = PTR_DIFF(p, param);
2508 SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
2509 SMB_POSIX_UNLINK_FILE_TARGET);
2512 if (!cli_send_trans(cli, SMBtrans2,
2514 -1, 0, /* fid, flags */
2515 &setup, 1, 0, /* setup, length, max */
2516 param, param_len, 2, /* param, length, max */
2517 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2525 if (!cli_receive_trans(cli, SMBtrans2,
2526 &rparam, ¶m_len,
2527 &rdata, &data_len)) {
2537 /****************************************************************************
2538 unlink - POSIX semantics.
2539 ****************************************************************************/
2541 bool cli_posix_unlink(struct cli_state *cli, const char *fname)
2543 return cli_posix_unlink_internal(cli, fname, False);
2546 /****************************************************************************
2547 rmdir - POSIX semantics.
2548 ****************************************************************************/
2550 int cli_posix_rmdir(struct cli_state *cli, const char *fname)
2552 return cli_posix_unlink_internal(cli, fname, True);