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 async_req *cli_open_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1002 struct cli_state *cli,
1003 const char *fname, int flags, int share_mode)
1005 unsigned openfn = 0;
1006 unsigned accessmode = 0;
1007 uint8_t additional_flags = 0;
1010 struct async_req *result;
1012 if (flags & O_CREAT) {
1015 if (!(flags & O_EXCL)) {
1016 if (flags & O_TRUNC)
1022 accessmode = (share_mode<<4);
1024 if ((flags & O_ACCMODE) == O_RDWR) {
1026 } else if ((flags & O_ACCMODE) == O_WRONLY) {
1031 if ((flags & O_SYNC) == O_SYNC) {
1032 accessmode |= (1<<14);
1036 if (share_mode == DENY_FCB) {
1040 SCVAL(vwv + 0, 0, 0xFF);
1041 SCVAL(vwv + 0, 1, 0);
1042 SSVAL(vwv + 1, 0, 0);
1043 SSVAL(vwv + 2, 0, 0); /* no additional info */
1044 SSVAL(vwv + 3, 0, accessmode);
1045 SSVAL(vwv + 4, 0, aSYSTEM | aHIDDEN);
1046 SSVAL(vwv + 5, 0, 0);
1047 SIVAL(vwv + 6, 0, 0);
1048 SSVAL(vwv + 8, 0, openfn);
1049 SIVAL(vwv + 9, 0, 0);
1050 SIVAL(vwv + 11, 0, 0);
1051 SIVAL(vwv + 13, 0, 0);
1053 if (cli->use_oplocks) {
1054 /* if using oplocks then ask for a batch oplock via
1055 core and extended methods */
1057 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK;
1058 SSVAL(vwv+2, 0, SVAL(vwv+2, 0) | 6);
1061 bytes = talloc_array(talloc_tos(), uint8_t, 0);
1062 bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname,
1063 strlen(fname)+1, NULL);
1064 if (bytes == NULL) {
1068 result = cli_request_send(mem_ctx, ev, cli, SMBopenX, additional_flags,
1069 15, vwv, 0, talloc_get_size(bytes), bytes);
1074 NTSTATUS cli_open_recv(struct async_req *req, int *fnum)
1082 if (async_req_is_nterror(req, &status)) {
1086 status = cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
1087 if (!NT_STATUS_IS_OK(status)) {
1092 return NT_STATUS_INVALID_NETWORK_RESPONSE;
1095 *fnum = SVAL(vwv+2, 0);
1097 return NT_STATUS_OK;
1100 int cli_open(struct cli_state *cli, const char *fname, int flags,
1103 TALLOC_CTX *frame = talloc_stackframe();
1104 struct event_context *ev;
1105 struct async_req *req;
1108 if (cli->fd_event != NULL) {
1110 * Can't use sync call while an async call is in flight
1112 cli_set_error(cli, NT_STATUS_INVALID_PARAMETER);
1116 ev = event_context_init(frame);
1121 req = cli_open_send(frame, ev, cli, fname, flags, share_mode);
1126 while (req->state < ASYNC_REQ_DONE) {
1127 event_loop_once(ev);
1130 cli_open_recv(req, &result);
1136 /****************************************************************************
1138 ****************************************************************************/
1140 struct async_req *cli_close_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1141 struct cli_state *cli, int fnum)
1145 SSVAL(vwv+0, 0, fnum);
1146 SIVALS(vwv+1, 0, -1);
1148 return cli_request_send(mem_ctx, ev, cli, SMBclose, 0, 3, vwv, 0,
1152 NTSTATUS cli_close_recv(struct async_req *req)
1160 if (async_req_is_nterror(req, &status)) {
1164 return cli_pull_reply(req, &wct, &vwv, &num_bytes, &bytes);
1167 bool cli_close(struct cli_state *cli, int fnum)
1169 TALLOC_CTX *frame = talloc_stackframe();
1170 struct event_context *ev;
1171 struct async_req *req;
1172 bool result = false;
1174 if (cli->fd_event != NULL) {
1176 * Can't use sync call while an async call is in flight
1178 cli_set_error(cli, NT_STATUS_INVALID_PARAMETER);
1182 ev = event_context_init(frame);
1187 req = cli_close_send(frame, ev, cli, fnum);
1192 while (req->state < ASYNC_REQ_DONE) {
1193 event_loop_once(ev);
1196 result = NT_STATUS_IS_OK(cli_close_recv(req));
1202 /****************************************************************************
1203 Truncate a file to a specified size
1204 ****************************************************************************/
1206 bool cli_ftruncate(struct cli_state *cli, int fnum, uint64_t size)
1208 unsigned int param_len = 6;
1209 unsigned int data_len = 8;
1210 uint16 setup = TRANSACT2_SETFILEINFO;
1212 unsigned char data[8];
1213 char *rparam=NULL, *rdata=NULL;
1214 int saved_timeout = cli->timeout;
1216 SSVAL(param,0,fnum);
1217 SSVAL(param,2,SMB_SET_FILE_END_OF_FILE_INFO);
1220 SBVAL(data, 0, size);
1222 if (!cli_send_trans(cli, SMBtrans2,
1224 -1, 0, /* fid, flags */
1225 &setup, 1, 0, /* setup, length, max */
1226 param, param_len, 2, /* param, length, max */
1227 (char *)&data, data_len,/* data, length, ... */
1228 cli->max_xmit)) { /* ... max */
1229 cli->timeout = saved_timeout;
1233 if (!cli_receive_trans(cli, SMBtrans2,
1234 &rparam, ¶m_len,
1235 &rdata, &data_len)) {
1236 cli->timeout = saved_timeout;
1242 cli->timeout = saved_timeout;
1251 /****************************************************************************
1252 send a lock with a specified locktype
1253 this is used for testing LOCKING_ANDX_CANCEL_LOCK
1254 ****************************************************************************/
1256 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
1257 uint32 offset, uint32 len,
1258 int timeout, unsigned char locktype)
1261 int saved_timeout = cli->timeout;
1263 memset(cli->outbuf,'\0',smb_size);
1264 memset(cli->inbuf,'\0', smb_size);
1266 cli_set_message(cli->outbuf,8,0,True);
1268 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1269 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1270 cli_setup_packet(cli);
1272 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1273 SSVAL(cli->outbuf,smb_vwv2,fnum);
1274 SCVAL(cli->outbuf,smb_vwv3,locktype);
1275 SIVALS(cli->outbuf, smb_vwv4, timeout);
1276 SSVAL(cli->outbuf,smb_vwv6,0);
1277 SSVAL(cli->outbuf,smb_vwv7,1);
1279 p = smb_buf(cli->outbuf);
1280 SSVAL(p, 0, cli->pid);
1281 SIVAL(p, 2, offset);
1286 cli_setup_bcc(cli, p);
1291 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
1294 if (!cli_receive_smb(cli)) {
1295 cli->timeout = saved_timeout;
1296 return NT_STATUS_UNSUCCESSFUL;
1299 cli->timeout = saved_timeout;
1301 return cli_nt_error(cli);
1304 /****************************************************************************
1306 note that timeout is in units of 2 milliseconds
1307 ****************************************************************************/
1309 bool cli_lock(struct cli_state *cli, int fnum,
1310 uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
1313 int saved_timeout = cli->timeout;
1315 memset(cli->outbuf,'\0',smb_size);
1316 memset(cli->inbuf,'\0', smb_size);
1318 cli_set_message(cli->outbuf,8,0,True);
1320 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1321 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1322 cli_setup_packet(cli);
1324 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1325 SSVAL(cli->outbuf,smb_vwv2,fnum);
1326 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
1327 SIVALS(cli->outbuf, smb_vwv4, timeout);
1328 SSVAL(cli->outbuf,smb_vwv6,0);
1329 SSVAL(cli->outbuf,smb_vwv7,1);
1331 p = smb_buf(cli->outbuf);
1332 SSVAL(p, 0, cli->pid);
1333 SIVAL(p, 2, offset);
1338 cli_setup_bcc(cli, p);
1343 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
1346 if (!cli_receive_smb(cli)) {
1347 cli->timeout = saved_timeout;
1351 cli->timeout = saved_timeout;
1353 if (cli_is_error(cli)) {
1360 /****************************************************************************
1362 ****************************************************************************/
1364 bool cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
1368 memset(cli->outbuf,'\0',smb_size);
1369 memset(cli->inbuf,'\0',smb_size);
1371 cli_set_message(cli->outbuf,8,0,True);
1373 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1374 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1375 cli_setup_packet(cli);
1377 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1378 SSVAL(cli->outbuf,smb_vwv2,fnum);
1379 SCVAL(cli->outbuf,smb_vwv3,0);
1380 SIVALS(cli->outbuf, smb_vwv4, 0);
1381 SSVAL(cli->outbuf,smb_vwv6,1);
1382 SSVAL(cli->outbuf,smb_vwv7,0);
1384 p = smb_buf(cli->outbuf);
1385 SSVAL(p, 0, cli->pid);
1386 SIVAL(p, 2, offset);
1389 cli_setup_bcc(cli, p);
1391 if (!cli_receive_smb(cli)) {
1395 if (cli_is_error(cli)) {
1402 /****************************************************************************
1403 Lock a file with 64 bit offsets.
1404 ****************************************************************************/
1406 bool cli_lock64(struct cli_state *cli, int fnum,
1407 uint64_t offset, uint64_t len, int timeout, enum brl_type lock_type)
1410 int saved_timeout = cli->timeout;
1413 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1414 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1417 ltype = (lock_type == READ_LOCK? 1 : 0);
1418 ltype |= LOCKING_ANDX_LARGE_FILES;
1420 memset(cli->outbuf,'\0',smb_size);
1421 memset(cli->inbuf,'\0', smb_size);
1423 cli_set_message(cli->outbuf,8,0,True);
1425 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1426 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1427 cli_setup_packet(cli);
1429 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1430 SSVAL(cli->outbuf,smb_vwv2,fnum);
1431 SCVAL(cli->outbuf,smb_vwv3,ltype);
1432 SIVALS(cli->outbuf, smb_vwv4, timeout);
1433 SSVAL(cli->outbuf,smb_vwv6,0);
1434 SSVAL(cli->outbuf,smb_vwv7,1);
1436 p = smb_buf(cli->outbuf);
1437 SIVAL(p, 0, cli->pid);
1438 SOFF_T_R(p, 4, offset);
1439 SOFF_T_R(p, 12, len);
1442 cli_setup_bcc(cli, p);
1446 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1449 if (!cli_receive_smb(cli)) {
1450 cli->timeout = saved_timeout;
1454 cli->timeout = saved_timeout;
1456 if (cli_is_error(cli)) {
1463 /****************************************************************************
1464 Unlock a file with 64 bit offsets.
1465 ****************************************************************************/
1467 bool cli_unlock64(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
1471 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1472 return cli_unlock(cli, fnum, offset, len);
1475 memset(cli->outbuf,'\0',smb_size);
1476 memset(cli->inbuf,'\0',smb_size);
1478 cli_set_message(cli->outbuf,8,0,True);
1480 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1481 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1482 cli_setup_packet(cli);
1484 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1485 SSVAL(cli->outbuf,smb_vwv2,fnum);
1486 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1487 SIVALS(cli->outbuf, smb_vwv4, 0);
1488 SSVAL(cli->outbuf,smb_vwv6,1);
1489 SSVAL(cli->outbuf,smb_vwv7,0);
1491 p = smb_buf(cli->outbuf);
1492 SIVAL(p, 0, cli->pid);
1493 SOFF_T_R(p, 4, offset);
1494 SOFF_T_R(p, 12, len);
1496 cli_setup_bcc(cli, p);
1498 if (!cli_receive_smb(cli)) {
1502 if (cli_is_error(cli)) {
1509 /****************************************************************************
1510 Get/unlock a POSIX lock on a file - internal function.
1511 ****************************************************************************/
1513 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
1514 uint64_t offset, uint64_t len, bool wait_lock, enum brl_type lock_type)
1516 unsigned int param_len = 4;
1517 unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1518 uint16 setup = TRANSACT2_SETFILEINFO;
1520 unsigned char data[POSIX_LOCK_DATA_SIZE];
1521 char *rparam=NULL, *rdata=NULL;
1522 int saved_timeout = cli->timeout;
1524 SSVAL(param,0,fnum);
1525 SSVAL(param,2,SMB_SET_POSIX_LOCK);
1527 switch (lock_type) {
1529 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
1532 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
1535 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
1542 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
1543 cli->timeout = 0x7FFFFFFF;
1545 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
1548 SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
1549 SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
1550 SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
1552 if (!cli_send_trans(cli, SMBtrans2,
1554 -1, 0, /* fid, flags */
1555 &setup, 1, 0, /* setup, length, max */
1556 param, param_len, 2, /* param, length, max */
1557 (char *)&data, data_len, cli->max_xmit /* data, length, max */
1559 cli->timeout = saved_timeout;
1563 if (!cli_receive_trans(cli, SMBtrans2,
1564 &rparam, ¶m_len,
1565 &rdata, &data_len)) {
1566 cli->timeout = saved_timeout;
1572 cli->timeout = saved_timeout;
1580 /****************************************************************************
1582 ****************************************************************************/
1584 bool cli_posix_lock(struct cli_state *cli, int fnum,
1585 uint64_t offset, uint64_t len,
1586 bool wait_lock, enum brl_type lock_type)
1588 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
1591 return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
1594 /****************************************************************************
1595 POSIX Unlock a file.
1596 ****************************************************************************/
1598 bool cli_posix_unlock(struct cli_state *cli, int fnum, uint64_t offset, uint64_t len)
1600 return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
1603 /****************************************************************************
1604 POSIX Get any lock covering a file.
1605 ****************************************************************************/
1607 bool cli_posix_getlock(struct cli_state *cli, int fnum, uint64_t *poffset, uint64_t *plen)
1612 /****************************************************************************
1613 Do a SMBgetattrE call.
1614 ****************************************************************************/
1616 bool cli_getattrE(struct cli_state *cli, int fd,
1617 uint16 *attr, SMB_OFF_T *size,
1618 time_t *change_time,
1619 time_t *access_time,
1622 memset(cli->outbuf,'\0',smb_size);
1623 memset(cli->inbuf,'\0',smb_size);
1625 cli_set_message(cli->outbuf,1,0,True);
1627 SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1628 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1629 cli_setup_packet(cli);
1631 SSVAL(cli->outbuf,smb_vwv0,fd);
1634 if (!cli_receive_smb(cli)) {
1638 if (cli_is_error(cli)) {
1643 *size = IVAL(cli->inbuf, smb_vwv6);
1647 *attr = SVAL(cli->inbuf,smb_vwv10);
1651 *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
1655 *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
1659 *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
1665 /****************************************************************************
1667 ****************************************************************************/
1669 bool cli_getatr(struct cli_state *cli, const char *fname,
1670 uint16 *attr, SMB_OFF_T *size, time_t *write_time)
1674 memset(cli->outbuf,'\0',smb_size);
1675 memset(cli->inbuf,'\0',smb_size);
1677 cli_set_message(cli->outbuf,0,0,True);
1679 SCVAL(cli->outbuf,smb_com,SMBgetatr);
1680 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1681 cli_setup_packet(cli);
1683 p = smb_buf(cli->outbuf);
1685 p += clistr_push(cli, p, fname,
1686 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1688 cli_setup_bcc(cli, p);
1691 if (!cli_receive_smb(cli)) {
1695 if (cli_is_error(cli)) {
1700 *size = IVAL(cli->inbuf, smb_vwv3);
1704 *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
1708 *attr = SVAL(cli->inbuf,smb_vwv0);
1714 /****************************************************************************
1715 Do a SMBsetattrE call.
1716 ****************************************************************************/
1718 bool cli_setattrE(struct cli_state *cli, int fd,
1726 memset(cli->outbuf,'\0',smb_size);
1727 memset(cli->inbuf,'\0',smb_size);
1729 cli_set_message(cli->outbuf,7,0,True);
1731 SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1732 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1733 cli_setup_packet(cli);
1735 SSVAL(cli->outbuf,smb_vwv0, fd);
1736 cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
1737 cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
1738 cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
1740 p = smb_buf(cli->outbuf);
1743 cli_setup_bcc(cli, p);
1746 if (!cli_receive_smb(cli)) {
1750 if (cli_is_error(cli)) {
1757 /****************************************************************************
1758 Do a SMBsetatr call.
1759 ****************************************************************************/
1761 bool cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1765 memset(cli->outbuf,'\0',smb_size);
1766 memset(cli->inbuf,'\0',smb_size);
1768 cli_set_message(cli->outbuf,8,0,True);
1770 SCVAL(cli->outbuf,smb_com,SMBsetatr);
1771 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1772 cli_setup_packet(cli);
1774 SSVAL(cli->outbuf,smb_vwv0, attr);
1775 cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
1777 p = smb_buf(cli->outbuf);
1779 p += clistr_push(cli, p, fname,
1780 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1783 cli_setup_bcc(cli, p);
1786 if (!cli_receive_smb(cli)) {
1790 if (cli_is_error(cli)) {
1797 /****************************************************************************
1798 Check for existance of a dir.
1799 ****************************************************************************/
1801 bool cli_chkpath(struct cli_state *cli, const char *path)
1805 TALLOC_CTX *frame = talloc_stackframe();
1807 path2 = talloc_strdup(frame, path);
1812 trim_char(path2,'\0','\\');
1814 path2 = talloc_strdup(frame, "\\");
1821 memset(cli->outbuf,'\0',smb_size);
1822 cli_set_message(cli->outbuf,0,0,True);
1823 SCVAL(cli->outbuf,smb_com,SMBcheckpath);
1824 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1825 cli_setup_packet(cli);
1826 p = smb_buf(cli->outbuf);
1828 p += clistr_push(cli, p, path2,
1829 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1831 cli_setup_bcc(cli, p);
1834 if (!cli_receive_smb(cli)) {
1841 if (cli_is_error(cli)) return False;
1846 /****************************************************************************
1848 ****************************************************************************/
1850 bool cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1852 memset(cli->outbuf,'\0',smb_size);
1853 cli_set_message(cli->outbuf,0,0,True);
1854 SCVAL(cli->outbuf,smb_com,SMBdskattr);
1855 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1856 cli_setup_packet(cli);
1859 if (!cli_receive_smb(cli)) {
1863 *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1864 *total = SVAL(cli->inbuf,smb_vwv0);
1865 *avail = SVAL(cli->inbuf,smb_vwv3);
1870 /****************************************************************************
1871 Create and open a temporary file.
1872 ****************************************************************************/
1874 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1879 memset(cli->outbuf,'\0',smb_size);
1880 memset(cli->inbuf,'\0',smb_size);
1882 cli_set_message(cli->outbuf,3,0,True);
1884 SCVAL(cli->outbuf,smb_com,SMBctemp);
1885 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1886 cli_setup_packet(cli);
1888 SSVAL(cli->outbuf,smb_vwv0,0);
1889 SIVALS(cli->outbuf,smb_vwv1,-1);
1891 p = smb_buf(cli->outbuf);
1893 p += clistr_push(cli, p, path,
1894 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1896 cli_setup_bcc(cli, p);
1899 if (!cli_receive_smb(cli)) {
1903 if (cli_is_error(cli)) {
1907 /* despite the spec, the result has a -1, followed by
1908 length, followed by name */
1909 p = smb_buf(cli->inbuf);
1911 len = smb_buflen(cli->inbuf) - 4;
1912 if (len <= 0 || len > PATH_MAX) return -1;
1915 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
1919 clistr_pull(cli->inbuf, path2, p,
1920 len+1, len, STR_ASCII);
1924 return SVAL(cli->inbuf,smb_vwv0);
1928 send a raw ioctl - used by the torture code
1930 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1932 memset(cli->outbuf,'\0',smb_size);
1933 memset(cli->inbuf,'\0',smb_size);
1935 cli_set_message(cli->outbuf, 3, 0, True);
1936 SCVAL(cli->outbuf,smb_com,SMBioctl);
1937 cli_setup_packet(cli);
1939 SSVAL(cli->outbuf, smb_vwv0, fnum);
1940 SSVAL(cli->outbuf, smb_vwv1, code>>16);
1941 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1944 if (!cli_receive_smb(cli)) {
1945 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1948 if (cli_is_error(cli)) {
1949 return cli_nt_error(cli);
1952 *blob = data_blob_null;
1954 return NT_STATUS_OK;
1957 /*********************************************************
1958 Set an extended attribute utility fn.
1959 *********************************************************/
1961 static bool cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
1962 const char *ea_name, const char *ea_val, size_t ea_len)
1964 unsigned int data_len = 0;
1966 char *rparam=NULL, *rdata=NULL;
1968 size_t ea_namelen = strlen(ea_name);
1970 if (ea_namelen == 0 && ea_len == 0) {
1972 data = (char *)SMB_MALLOC(data_len);
1977 SIVAL(p,0,data_len);
1979 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
1980 data = (char *)SMB_MALLOC(data_len);
1985 SIVAL(p,0,data_len);
1987 SCVAL(p, 0, 0); /* EA flags. */
1988 SCVAL(p, 1, ea_namelen);
1989 SSVAL(p, 2, ea_len);
1990 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
1991 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
1994 if (!cli_send_trans(cli, SMBtrans2,
1996 -1, 0, /* fid, flags */
1997 &setup, 1, 0, /* setup, length, max */
1998 param, param_len, 2, /* param, length, max */
1999 data, data_len, cli->max_xmit /* data, length, max */
2005 if (!cli_receive_trans(cli, SMBtrans2,
2006 &rparam, ¶m_len,
2007 &rdata, &data_len)) {
2019 /*********************************************************
2020 Set an extended attribute on a pathname.
2021 *********************************************************/
2023 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
2025 uint16 setup = TRANSACT2_SETPATHINFO;
2026 unsigned int param_len = 0;
2028 size_t srclen = 2*(strlen(path)+1);
2032 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2036 memset(param, '\0', 6);
2037 SSVAL(param,0,SMB_INFO_SET_EA);
2040 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2041 param_len = PTR_DIFF(p, param);
2043 ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
2048 /*********************************************************
2049 Set an extended attribute on an fnum.
2050 *********************************************************/
2052 bool cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
2055 uint16 setup = TRANSACT2_SETFILEINFO;
2057 memset(param, 0, 6);
2058 SSVAL(param,0,fnum);
2059 SSVAL(param,2,SMB_INFO_SET_EA);
2061 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
2064 /*********************************************************
2065 Get an extended attribute list utility fn.
2066 *********************************************************/
2068 static bool cli_get_ea_list(struct cli_state *cli,
2069 uint16 setup, char *param, unsigned int param_len,
2072 struct ea_struct **pea_list)
2074 unsigned int data_len = 0;
2075 unsigned int rparam_len, rdata_len;
2076 char *rparam=NULL, *rdata=NULL;
2081 struct ea_struct *ea_list;
2088 if (!cli_send_trans(cli, SMBtrans2,
2090 -1, 0, /* fid, flags */
2091 &setup, 1, 0, /* setup, length, max */
2092 param, param_len, 10, /* param, length, max */
2093 NULL, data_len, cli->max_xmit /* data, length, max */
2098 if (!cli_receive_trans(cli, SMBtrans2,
2099 &rparam, &rparam_len,
2100 &rdata, &rdata_len)) {
2104 if (!rdata || rdata_len < 4) {
2108 ea_size = (size_t)IVAL(rdata,0);
2109 if (ea_size > rdata_len) {
2114 /* No EA's present. */
2122 /* Validate the EA list and count it. */
2123 for (num_eas = 0; ea_size >= 4; num_eas++) {
2124 unsigned int ea_namelen = CVAL(p,1);
2125 unsigned int ea_valuelen = SVAL(p,2);
2126 if (ea_namelen == 0) {
2129 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
2132 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
2133 p += 4 + ea_namelen + 1 + ea_valuelen;
2141 *pnum_eas = num_eas;
2143 /* Caller only wants number of EA's. */
2148 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
2153 ea_size = (size_t)IVAL(rdata,0);
2156 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
2157 struct ea_struct *ea = &ea_list[num_eas];
2158 fstring unix_ea_name;
2159 unsigned int ea_namelen = CVAL(p,1);
2160 unsigned int ea_valuelen = SVAL(p,2);
2162 ea->flags = CVAL(p,0);
2163 unix_ea_name[0] = '\0';
2164 pull_ascii_fstring(unix_ea_name, p + 4);
2165 ea->name = talloc_strdup(ctx, unix_ea_name);
2166 /* Ensure the value is null terminated (in case it's a string). */
2167 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
2168 if (!ea->value.data) {
2172 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
2174 ea->value.data[ea_valuelen] = 0;
2176 p += 4 + ea_namelen + 1 + ea_valuelen;
2179 *pea_list = ea_list;
2189 /*********************************************************
2190 Get an extended attribute list from a pathname.
2191 *********************************************************/
2193 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
2196 struct ea_struct **pea_list)
2198 uint16 setup = TRANSACT2_QPATHINFO;
2199 unsigned int param_len = 0;
2202 size_t srclen = 2*(strlen(path)+1);
2205 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2211 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
2213 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
2214 param_len = PTR_DIFF(p, param);
2216 ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
2221 /*********************************************************
2222 Get an extended attribute list from an fnum.
2223 *********************************************************/
2225 bool cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
2228 struct ea_struct **pea_list)
2230 uint16 setup = TRANSACT2_QFILEINFO;
2233 memset(param, 0, 6);
2234 SSVAL(param,0,fnum);
2235 SSVAL(param,2,SMB_INFO_SET_EA);
2237 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
2240 /****************************************************************************
2241 Convert open "flags" arg to uint32 on wire.
2242 ****************************************************************************/
2244 static uint32 open_flags_to_wire(int flags)
2246 int open_mode = flags & O_ACCMODE;
2249 switch (open_mode) {
2251 ret |= SMB_O_WRONLY;
2258 ret |= SMB_O_RDONLY;
2262 if (flags & O_CREAT) {
2265 if (flags & O_EXCL) {
2268 if (flags & O_TRUNC) {
2272 if (flags & O_SYNC) {
2276 if (flags & O_APPEND) {
2277 ret |= SMB_O_APPEND;
2279 #if defined(O_DIRECT)
2280 if (flags & O_DIRECT) {
2281 ret |= SMB_O_DIRECT;
2284 #if defined(O_DIRECTORY)
2285 if (flags & O_DIRECTORY) {
2286 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2287 ret |= SMB_O_DIRECTORY;
2293 /****************************************************************************
2294 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
2295 ****************************************************************************/
2297 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
2299 unsigned int data_len = 0;
2300 unsigned int param_len = 0;
2301 uint16 setup = TRANSACT2_SETPATHINFO;
2304 char *rparam=NULL, *rdata=NULL;
2307 uint32 wire_flags = open_flags_to_wire(flags);
2308 size_t srclen = 2*(strlen(fname)+1);
2310 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2314 memset(param, '\0', 6);
2315 SSVAL(param,0, SMB_POSIX_PATH_OPEN);
2318 p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2319 param_len = PTR_DIFF(p, param);
2322 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
2323 wire_flags |= SMB_O_DIRECTORY;
2327 SIVAL(p,0,0); /* No oplock. */
2328 SIVAL(p,4,wire_flags);
2329 SIVAL(p,8,unix_perms_to_wire(mode));
2330 SIVAL(p,12,0); /* Top bits of perms currently undefined. */
2331 SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
2335 if (!cli_send_trans(cli, SMBtrans2,
2337 -1, 0, /* fid, flags */
2338 &setup, 1, 0, /* setup, length, max */
2339 param, param_len, 2, /* param, length, max */
2340 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2348 if (!cli_receive_trans(cli, SMBtrans2,
2349 &rparam, ¶m_len,
2350 &rdata, &data_len)) {
2354 fnum = SVAL(rdata,2);
2362 /****************************************************************************
2363 open - POSIX semantics.
2364 ****************************************************************************/
2366 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
2368 return cli_posix_open_internal(cli, fname, flags, mode, False);
2371 /****************************************************************************
2372 mkdir - POSIX semantics.
2373 ****************************************************************************/
2375 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
2377 return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
2380 /****************************************************************************
2381 unlink or rmdir - POSIX semantics.
2382 ****************************************************************************/
2384 static bool cli_posix_unlink_internal(struct cli_state *cli, const char *fname, bool is_dir)
2386 unsigned int data_len = 0;
2387 unsigned int param_len = 0;
2388 uint16 setup = TRANSACT2_SETPATHINFO;
2391 char *rparam=NULL, *rdata=NULL;
2393 size_t srclen = 2*(strlen(fname)+1);
2395 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2399 memset(param, '\0', 6);
2400 SSVAL(param,0, SMB_POSIX_PATH_UNLINK);
2403 p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2404 param_len = PTR_DIFF(p, param);
2406 SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
2407 SMB_POSIX_UNLINK_FILE_TARGET);
2410 if (!cli_send_trans(cli, SMBtrans2,
2412 -1, 0, /* fid, flags */
2413 &setup, 1, 0, /* setup, length, max */
2414 param, param_len, 2, /* param, length, max */
2415 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2423 if (!cli_receive_trans(cli, SMBtrans2,
2424 &rparam, ¶m_len,
2425 &rdata, &data_len)) {
2435 /****************************************************************************
2436 unlink - POSIX semantics.
2437 ****************************************************************************/
2439 bool cli_posix_unlink(struct cli_state *cli, const char *fname)
2441 return cli_posix_unlink_internal(cli, fname, False);
2444 /****************************************************************************
2445 rmdir - POSIX semantics.
2446 ****************************************************************************/
2448 int cli_posix_rmdir(struct cli_state *cli, const char *fname)
2450 return cli_posix_unlink_internal(cli, fname, True);