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 (char *)&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, SecuityFlags);
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 /****************************************************************************
776 ****************************************************************************/
778 int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess)
780 return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
781 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN, 0x0, 0x0);
784 /****************************************************************************
786 WARNING: if you open with O_WRONLY then getattrE won't work!
787 ****************************************************************************/
789 int cli_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
793 unsigned accessmode=0;
797 if (!(flags & O_EXCL)) {
804 accessmode = (share_mode<<4);
806 if ((flags & O_ACCMODE) == O_RDWR) {
808 } else if ((flags & O_ACCMODE) == O_WRONLY) {
813 if ((flags & O_SYNC) == O_SYNC) {
814 accessmode |= (1<<14);
818 if (share_mode == DENY_FCB) {
822 memset(cli->outbuf,'\0',smb_size);
823 memset(cli->inbuf,'\0',smb_size);
825 cli_set_message(cli->outbuf,15,0, true);
827 SCVAL(cli->outbuf,smb_com,SMBopenX);
828 SSVAL(cli->outbuf,smb_tid,cli->cnum);
829 cli_setup_packet(cli);
831 SSVAL(cli->outbuf,smb_vwv0,0xFF);
832 SSVAL(cli->outbuf,smb_vwv2,0); /* no additional info */
833 SSVAL(cli->outbuf,smb_vwv3,accessmode);
834 SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
835 SSVAL(cli->outbuf,smb_vwv5,0);
836 SSVAL(cli->outbuf,smb_vwv8,openfn);
838 if (cli->use_oplocks) {
839 /* if using oplocks then ask for a batch oplock via
840 core and extended methods */
841 SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
842 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
843 SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
846 p = smb_buf(cli->outbuf);
847 p += clistr_push(cli, p, fname,
848 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
850 cli_setup_bcc(cli, p);
853 if (!cli_receive_smb(cli)) {
857 if (cli_is_error(cli)) {
861 return SVAL(cli->inbuf,smb_vwv2);
864 /****************************************************************************
866 ****************************************************************************/
868 bool cli_close(struct cli_state *cli, int fnum)
870 memset(cli->outbuf,'\0',smb_size);
871 memset(cli->inbuf,'\0',smb_size);
873 cli_set_message(cli->outbuf,3,0,True);
875 SCVAL(cli->outbuf,smb_com,SMBclose);
876 SSVAL(cli->outbuf,smb_tid,cli->cnum);
877 cli_setup_packet(cli);
879 SSVAL(cli->outbuf,smb_vwv0,fnum);
880 SIVALS(cli->outbuf,smb_vwv1,-1);
883 if (!cli_receive_smb(cli)) {
887 return !cli_is_error(cli);
891 /****************************************************************************
892 send a lock with a specified locktype
893 this is used for testing LOCKING_ANDX_CANCEL_LOCK
894 ****************************************************************************/
896 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
897 uint32 offset, uint32 len,
898 int timeout, unsigned char locktype)
901 int saved_timeout = cli->timeout;
903 memset(cli->outbuf,'\0',smb_size);
904 memset(cli->inbuf,'\0', smb_size);
906 cli_set_message(cli->outbuf,8,0,True);
908 SCVAL(cli->outbuf,smb_com,SMBlockingX);
909 SSVAL(cli->outbuf,smb_tid,cli->cnum);
910 cli_setup_packet(cli);
912 SCVAL(cli->outbuf,smb_vwv0,0xFF);
913 SSVAL(cli->outbuf,smb_vwv2,fnum);
914 SCVAL(cli->outbuf,smb_vwv3,locktype);
915 SIVALS(cli->outbuf, smb_vwv4, timeout);
916 SSVAL(cli->outbuf,smb_vwv6,0);
917 SSVAL(cli->outbuf,smb_vwv7,1);
919 p = smb_buf(cli->outbuf);
920 SSVAL(p, 0, cli->pid);
926 cli_setup_bcc(cli, p);
931 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
934 if (!cli_receive_smb(cli)) {
935 cli->timeout = saved_timeout;
936 return NT_STATUS_UNSUCCESSFUL;
939 cli->timeout = saved_timeout;
941 return cli_nt_error(cli);
944 /****************************************************************************
946 note that timeout is in units of 2 milliseconds
947 ****************************************************************************/
949 bool cli_lock(struct cli_state *cli, int fnum,
950 uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
953 int saved_timeout = cli->timeout;
955 memset(cli->outbuf,'\0',smb_size);
956 memset(cli->inbuf,'\0', smb_size);
958 cli_set_message(cli->outbuf,8,0,True);
960 SCVAL(cli->outbuf,smb_com,SMBlockingX);
961 SSVAL(cli->outbuf,smb_tid,cli->cnum);
962 cli_setup_packet(cli);
964 SCVAL(cli->outbuf,smb_vwv0,0xFF);
965 SSVAL(cli->outbuf,smb_vwv2,fnum);
966 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
967 SIVALS(cli->outbuf, smb_vwv4, timeout);
968 SSVAL(cli->outbuf,smb_vwv6,0);
969 SSVAL(cli->outbuf,smb_vwv7,1);
971 p = smb_buf(cli->outbuf);
972 SSVAL(p, 0, cli->pid);
978 cli_setup_bcc(cli, p);
983 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
986 if (!cli_receive_smb(cli)) {
987 cli->timeout = saved_timeout;
991 cli->timeout = saved_timeout;
993 if (cli_is_error(cli)) {
1000 /****************************************************************************
1002 ****************************************************************************/
1004 bool cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
1008 memset(cli->outbuf,'\0',smb_size);
1009 memset(cli->inbuf,'\0',smb_size);
1011 cli_set_message(cli->outbuf,8,0,True);
1013 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1014 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1015 cli_setup_packet(cli);
1017 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1018 SSVAL(cli->outbuf,smb_vwv2,fnum);
1019 SCVAL(cli->outbuf,smb_vwv3,0);
1020 SIVALS(cli->outbuf, smb_vwv4, 0);
1021 SSVAL(cli->outbuf,smb_vwv6,1);
1022 SSVAL(cli->outbuf,smb_vwv7,0);
1024 p = smb_buf(cli->outbuf);
1025 SSVAL(p, 0, cli->pid);
1026 SIVAL(p, 2, offset);
1029 cli_setup_bcc(cli, p);
1031 if (!cli_receive_smb(cli)) {
1035 if (cli_is_error(cli)) {
1042 /****************************************************************************
1043 Lock a file with 64 bit offsets.
1044 ****************************************************************************/
1046 bool cli_lock64(struct cli_state *cli, int fnum,
1047 SMB_BIG_UINT offset, SMB_BIG_UINT len, int timeout, enum brl_type lock_type)
1050 int saved_timeout = cli->timeout;
1053 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1054 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
1057 ltype = (lock_type == READ_LOCK? 1 : 0);
1058 ltype |= LOCKING_ANDX_LARGE_FILES;
1060 memset(cli->outbuf,'\0',smb_size);
1061 memset(cli->inbuf,'\0', smb_size);
1063 cli_set_message(cli->outbuf,8,0,True);
1065 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1066 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1067 cli_setup_packet(cli);
1069 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1070 SSVAL(cli->outbuf,smb_vwv2,fnum);
1071 SCVAL(cli->outbuf,smb_vwv3,ltype);
1072 SIVALS(cli->outbuf, smb_vwv4, timeout);
1073 SSVAL(cli->outbuf,smb_vwv6,0);
1074 SSVAL(cli->outbuf,smb_vwv7,1);
1076 p = smb_buf(cli->outbuf);
1077 SIVAL(p, 0, cli->pid);
1078 SOFF_T_R(p, 4, offset);
1079 SOFF_T_R(p, 12, len);
1082 cli_setup_bcc(cli, p);
1086 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1089 if (!cli_receive_smb(cli)) {
1090 cli->timeout = saved_timeout;
1094 cli->timeout = saved_timeout;
1096 if (cli_is_error(cli)) {
1103 /****************************************************************************
1104 Unlock a file with 64 bit offsets.
1105 ****************************************************************************/
1107 bool cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1111 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1112 return cli_unlock(cli, fnum, offset, len);
1115 memset(cli->outbuf,'\0',smb_size);
1116 memset(cli->inbuf,'\0',smb_size);
1118 cli_set_message(cli->outbuf,8,0,True);
1120 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1121 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1122 cli_setup_packet(cli);
1124 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1125 SSVAL(cli->outbuf,smb_vwv2,fnum);
1126 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1127 SIVALS(cli->outbuf, smb_vwv4, 0);
1128 SSVAL(cli->outbuf,smb_vwv6,1);
1129 SSVAL(cli->outbuf,smb_vwv7,0);
1131 p = smb_buf(cli->outbuf);
1132 SIVAL(p, 0, cli->pid);
1133 SOFF_T_R(p, 4, offset);
1134 SOFF_T_R(p, 12, len);
1136 cli_setup_bcc(cli, p);
1138 if (!cli_receive_smb(cli)) {
1142 if (cli_is_error(cli)) {
1149 /****************************************************************************
1150 Get/unlock a POSIX lock on a file - internal function.
1151 ****************************************************************************/
1153 static bool cli_posix_lock_internal(struct cli_state *cli, int fnum,
1154 SMB_BIG_UINT offset, SMB_BIG_UINT len, bool wait_lock, enum brl_type lock_type)
1156 unsigned int param_len = 4;
1157 unsigned int data_len = POSIX_LOCK_DATA_SIZE;
1158 uint16 setup = TRANSACT2_SETFILEINFO;
1160 unsigned char data[POSIX_LOCK_DATA_SIZE];
1161 char *rparam=NULL, *rdata=NULL;
1162 int saved_timeout = cli->timeout;
1164 SSVAL(param,0,fnum);
1165 SSVAL(param,2,SMB_SET_POSIX_LOCK);
1167 switch (lock_type) {
1169 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_READ);
1172 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_WRITE);
1175 SSVAL(data, POSIX_LOCK_TYPE_OFFSET, POSIX_LOCK_TYPE_UNLOCK);
1182 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_WAIT);
1183 cli->timeout = 0x7FFFFFFF;
1185 SSVAL(data, POSIX_LOCK_FLAGS_OFFSET, POSIX_LOCK_FLAG_NOWAIT);
1188 SIVAL(data, POSIX_LOCK_PID_OFFSET, cli->pid);
1189 SOFF_T(data, POSIX_LOCK_START_OFFSET, offset);
1190 SOFF_T(data, POSIX_LOCK_LEN_OFFSET, len);
1192 if (!cli_send_trans(cli, SMBtrans2,
1194 -1, 0, /* fid, flags */
1195 &setup, 1, 0, /* setup, length, max */
1196 param, param_len, 2, /* param, length, max */
1197 (char *)&data, data_len, cli->max_xmit /* data, length, max */
1199 cli->timeout = saved_timeout;
1203 if (!cli_receive_trans(cli, SMBtrans2,
1204 &rparam, ¶m_len,
1205 &rdata, &data_len)) {
1206 cli->timeout = saved_timeout;
1212 cli->timeout = saved_timeout;
1220 /****************************************************************************
1222 ****************************************************************************/
1224 bool cli_posix_lock(struct cli_state *cli, int fnum,
1225 SMB_BIG_UINT offset, SMB_BIG_UINT len,
1226 bool wait_lock, enum brl_type lock_type)
1228 if (lock_type != READ_LOCK && lock_type != WRITE_LOCK) {
1231 return cli_posix_lock_internal(cli, fnum, offset, len, wait_lock, lock_type);
1234 /****************************************************************************
1235 POSIX Unlock a file.
1236 ****************************************************************************/
1238 bool cli_posix_unlock(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1240 return cli_posix_lock_internal(cli, fnum, offset, len, False, UNLOCK_LOCK);
1243 /****************************************************************************
1244 POSIX Get any lock covering a file.
1245 ****************************************************************************/
1247 bool cli_posix_getlock(struct cli_state *cli, int fnum, SMB_BIG_UINT *poffset, SMB_BIG_UINT *plen)
1252 /****************************************************************************
1253 Do a SMBgetattrE call.
1254 ****************************************************************************/
1256 bool cli_getattrE(struct cli_state *cli, int fd,
1257 uint16 *attr, SMB_OFF_T *size,
1258 time_t *change_time,
1259 time_t *access_time,
1262 memset(cli->outbuf,'\0',smb_size);
1263 memset(cli->inbuf,'\0',smb_size);
1265 cli_set_message(cli->outbuf,1,0,True);
1267 SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1268 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1269 cli_setup_packet(cli);
1271 SSVAL(cli->outbuf,smb_vwv0,fd);
1274 if (!cli_receive_smb(cli)) {
1278 if (cli_is_error(cli)) {
1283 *size = IVAL(cli->inbuf, smb_vwv6);
1287 *attr = SVAL(cli->inbuf,smb_vwv10);
1291 *change_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv0);
1295 *access_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv2);
1299 *write_time = cli_make_unix_date2(cli, cli->inbuf+smb_vwv4);
1305 /****************************************************************************
1307 ****************************************************************************/
1309 bool cli_getatr(struct cli_state *cli, const char *fname,
1310 uint16 *attr, SMB_OFF_T *size, time_t *write_time)
1314 memset(cli->outbuf,'\0',smb_size);
1315 memset(cli->inbuf,'\0',smb_size);
1317 cli_set_message(cli->outbuf,0,0,True);
1319 SCVAL(cli->outbuf,smb_com,SMBgetatr);
1320 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1321 cli_setup_packet(cli);
1323 p = smb_buf(cli->outbuf);
1325 p += clistr_push(cli, p, fname,
1326 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1328 cli_setup_bcc(cli, p);
1331 if (!cli_receive_smb(cli)) {
1335 if (cli_is_error(cli)) {
1340 *size = IVAL(cli->inbuf, smb_vwv3);
1344 *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
1348 *attr = SVAL(cli->inbuf,smb_vwv0);
1354 /****************************************************************************
1355 Do a SMBsetattrE call.
1356 ****************************************************************************/
1358 bool cli_setattrE(struct cli_state *cli, int fd,
1366 memset(cli->outbuf,'\0',smb_size);
1367 memset(cli->inbuf,'\0',smb_size);
1369 cli_set_message(cli->outbuf,7,0,True);
1371 SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1372 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1373 cli_setup_packet(cli);
1375 SSVAL(cli->outbuf,smb_vwv0, fd);
1376 cli_put_dos_date2(cli, cli->outbuf,smb_vwv1, change_time);
1377 cli_put_dos_date2(cli, cli->outbuf,smb_vwv3, access_time);
1378 cli_put_dos_date2(cli, cli->outbuf,smb_vwv5, write_time);
1380 p = smb_buf(cli->outbuf);
1383 cli_setup_bcc(cli, p);
1386 if (!cli_receive_smb(cli)) {
1390 if (cli_is_error(cli)) {
1397 /****************************************************************************
1398 Do a SMBsetatr call.
1399 ****************************************************************************/
1401 bool cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1405 memset(cli->outbuf,'\0',smb_size);
1406 memset(cli->inbuf,'\0',smb_size);
1408 cli_set_message(cli->outbuf,8,0,True);
1410 SCVAL(cli->outbuf,smb_com,SMBsetatr);
1411 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1412 cli_setup_packet(cli);
1414 SSVAL(cli->outbuf,smb_vwv0, attr);
1415 cli_put_dos_date3(cli, cli->outbuf,smb_vwv1, t);
1417 p = smb_buf(cli->outbuf);
1419 p += clistr_push(cli, p, fname,
1420 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1423 cli_setup_bcc(cli, p);
1426 if (!cli_receive_smb(cli)) {
1430 if (cli_is_error(cli)) {
1437 /****************************************************************************
1438 Check for existance of a dir.
1439 ****************************************************************************/
1441 bool cli_chkpath(struct cli_state *cli, const char *path)
1445 TALLOC_CTX *frame = talloc_stackframe();
1447 path2 = talloc_strdup(frame, path);
1452 trim_char(path2,'\0','\\');
1454 path2 = talloc_strdup(frame, "\\");
1461 memset(cli->outbuf,'\0',smb_size);
1462 cli_set_message(cli->outbuf,0,0,True);
1463 SCVAL(cli->outbuf,smb_com,SMBcheckpath);
1464 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1465 cli_setup_packet(cli);
1466 p = smb_buf(cli->outbuf);
1468 p += clistr_push(cli, p, path2,
1469 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1471 cli_setup_bcc(cli, p);
1474 if (!cli_receive_smb(cli)) {
1481 if (cli_is_error(cli)) return False;
1486 /****************************************************************************
1488 ****************************************************************************/
1490 bool cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1492 memset(cli->outbuf,'\0',smb_size);
1493 cli_set_message(cli->outbuf,0,0,True);
1494 SCVAL(cli->outbuf,smb_com,SMBdskattr);
1495 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1496 cli_setup_packet(cli);
1499 if (!cli_receive_smb(cli)) {
1503 *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1504 *total = SVAL(cli->inbuf,smb_vwv0);
1505 *avail = SVAL(cli->inbuf,smb_vwv3);
1510 /****************************************************************************
1511 Create and open a temporary file.
1512 ****************************************************************************/
1514 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1519 memset(cli->outbuf,'\0',smb_size);
1520 memset(cli->inbuf,'\0',smb_size);
1522 cli_set_message(cli->outbuf,3,0,True);
1524 SCVAL(cli->outbuf,smb_com,SMBctemp);
1525 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1526 cli_setup_packet(cli);
1528 SSVAL(cli->outbuf,smb_vwv0,0);
1529 SIVALS(cli->outbuf,smb_vwv1,-1);
1531 p = smb_buf(cli->outbuf);
1533 p += clistr_push(cli, p, path,
1534 cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
1536 cli_setup_bcc(cli, p);
1539 if (!cli_receive_smb(cli)) {
1543 if (cli_is_error(cli)) {
1547 /* despite the spec, the result has a -1, followed by
1548 length, followed by name */
1549 p = smb_buf(cli->inbuf);
1551 len = smb_buflen(cli->inbuf) - 4;
1552 if (len <= 0 || len > PATH_MAX) return -1;
1555 char *path2 = SMB_MALLOC_ARRAY(char, len+1);
1559 clistr_pull(cli, path2, p,
1560 len+1, len, STR_ASCII);
1564 return SVAL(cli->inbuf,smb_vwv0);
1568 send a raw ioctl - used by the torture code
1570 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1572 memset(cli->outbuf,'\0',smb_size);
1573 memset(cli->inbuf,'\0',smb_size);
1575 cli_set_message(cli->outbuf, 3, 0, True);
1576 SCVAL(cli->outbuf,smb_com,SMBioctl);
1577 cli_setup_packet(cli);
1579 SSVAL(cli->outbuf, smb_vwv0, fnum);
1580 SSVAL(cli->outbuf, smb_vwv1, code>>16);
1581 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1584 if (!cli_receive_smb(cli)) {
1585 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1588 if (cli_is_error(cli)) {
1589 return cli_nt_error(cli);
1592 *blob = data_blob_null;
1594 return NT_STATUS_OK;
1597 /*********************************************************
1598 Set an extended attribute utility fn.
1599 *********************************************************/
1601 static bool cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
1602 const char *ea_name, const char *ea_val, size_t ea_len)
1604 unsigned int data_len = 0;
1606 char *rparam=NULL, *rdata=NULL;
1608 size_t ea_namelen = strlen(ea_name);
1610 if (ea_namelen == 0 && ea_len == 0) {
1612 data = (char *)SMB_MALLOC(data_len);
1617 SIVAL(p,0,data_len);
1619 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
1620 data = (char *)SMB_MALLOC(data_len);
1625 SIVAL(p,0,data_len);
1627 SCVAL(p, 0, 0); /* EA flags. */
1628 SCVAL(p, 1, ea_namelen);
1629 SSVAL(p, 2, ea_len);
1630 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
1631 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
1634 if (!cli_send_trans(cli, SMBtrans2,
1636 -1, 0, /* fid, flags */
1637 &setup, 1, 0, /* setup, length, max */
1638 param, param_len, 2, /* param, length, max */
1639 data, data_len, cli->max_xmit /* data, length, max */
1645 if (!cli_receive_trans(cli, SMBtrans2,
1646 &rparam, ¶m_len,
1647 &rdata, &data_len)) {
1659 /*********************************************************
1660 Set an extended attribute on a pathname.
1661 *********************************************************/
1663 bool cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
1665 uint16 setup = TRANSACT2_SETPATHINFO;
1666 unsigned int param_len = 0;
1668 size_t srclen = 2*(strlen(path)+1);
1672 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
1676 memset(param, '\0', 6);
1677 SSVAL(param,0,SMB_INFO_SET_EA);
1680 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
1681 param_len = PTR_DIFF(p, param);
1683 ret = cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
1688 /*********************************************************
1689 Set an extended attribute on an fnum.
1690 *********************************************************/
1692 bool cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
1695 uint16 setup = TRANSACT2_SETFILEINFO;
1697 memset(param, 0, 6);
1698 SSVAL(param,0,fnum);
1699 SSVAL(param,2,SMB_INFO_SET_EA);
1701 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
1704 /*********************************************************
1705 Get an extended attribute list tility fn.
1706 *********************************************************/
1708 static bool cli_get_ea_list(struct cli_state *cli,
1709 uint16 setup, char *param, unsigned int param_len,
1712 struct ea_struct **pea_list)
1714 unsigned int data_len = 0;
1715 unsigned int rparam_len, rdata_len;
1716 char *rparam=NULL, *rdata=NULL;
1721 struct ea_struct *ea_list;
1728 if (!cli_send_trans(cli, SMBtrans2,
1730 -1, 0, /* fid, flags */
1731 &setup, 1, 0, /* setup, length, max */
1732 param, param_len, 10, /* param, length, max */
1733 NULL, data_len, cli->max_xmit /* data, length, max */
1738 if (!cli_receive_trans(cli, SMBtrans2,
1739 &rparam, &rparam_len,
1740 &rdata, &rdata_len)) {
1744 if (!rdata || rdata_len < 4) {
1748 ea_size = (size_t)IVAL(rdata,0);
1749 if (ea_size > rdata_len) {
1754 /* No EA's present. */
1762 /* Validate the EA list and count it. */
1763 for (num_eas = 0; ea_size >= 4; num_eas++) {
1764 unsigned int ea_namelen = CVAL(p,1);
1765 unsigned int ea_valuelen = SVAL(p,2);
1766 if (ea_namelen == 0) {
1769 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
1772 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
1773 p += 4 + ea_namelen + 1 + ea_valuelen;
1781 *pnum_eas = num_eas;
1783 /* Caller only wants number of EA's. */
1788 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
1793 ea_size = (size_t)IVAL(rdata,0);
1796 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
1797 struct ea_struct *ea = &ea_list[num_eas];
1798 fstring unix_ea_name;
1799 unsigned int ea_namelen = CVAL(p,1);
1800 unsigned int ea_valuelen = SVAL(p,2);
1802 ea->flags = CVAL(p,0);
1803 unix_ea_name[0] = '\0';
1804 pull_ascii_fstring(unix_ea_name, p + 4);
1805 ea->name = talloc_strdup(ctx, unix_ea_name);
1806 /* Ensure the value is null terminated (in case it's a string). */
1807 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
1808 if (!ea->value.data) {
1812 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
1814 ea->value.data[ea_valuelen] = 0;
1816 p += 4 + ea_namelen + 1 + ea_valuelen;
1819 *pea_list = ea_list;
1829 /*********************************************************
1830 Get an extended attribute list from a pathname.
1831 *********************************************************/
1833 bool cli_get_ea_list_path(struct cli_state *cli, const char *path,
1836 struct ea_struct **pea_list)
1838 uint16 setup = TRANSACT2_QPATHINFO;
1839 unsigned int param_len = 0;
1842 size_t srclen = 2*(strlen(path)+1);
1845 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
1851 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
1853 p += clistr_push(cli, p, path, srclen, STR_TERMINATE);
1854 param_len = PTR_DIFF(p, param);
1856 ret = cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
1861 /*********************************************************
1862 Get an extended attribute list from an fnum.
1863 *********************************************************/
1865 bool cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
1868 struct ea_struct **pea_list)
1870 uint16 setup = TRANSACT2_QFILEINFO;
1873 memset(param, 0, 6);
1874 SSVAL(param,0,fnum);
1875 SSVAL(param,2,SMB_INFO_SET_EA);
1877 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);
1880 /****************************************************************************
1881 Convert open "flags" arg to uint32 on wire.
1882 ****************************************************************************/
1884 static uint32 open_flags_to_wire(int flags)
1886 int open_mode = flags & O_ACCMODE;
1889 switch (open_mode) {
1891 ret |= SMB_O_WRONLY;
1898 ret |= SMB_O_RDONLY;
1902 if (flags & O_CREAT) {
1905 if (flags & O_EXCL) {
1908 if (flags & O_TRUNC) {
1912 if (flags & O_SYNC) {
1916 if (flags & O_APPEND) {
1917 ret |= SMB_O_APPEND;
1919 #if defined(O_DIRECT)
1920 if (flags & O_DIRECT) {
1921 ret |= SMB_O_DIRECT;
1924 #if defined(O_DIRECTORY)
1925 if (flags & O_DIRECTORY) {
1926 ret &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
1927 ret |= SMB_O_DIRECTORY;
1933 /****************************************************************************
1934 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
1935 ****************************************************************************/
1937 static int cli_posix_open_internal(struct cli_state *cli, const char *fname, int flags, mode_t mode, bool is_dir)
1939 unsigned int data_len = 0;
1940 unsigned int param_len = 0;
1941 uint16 setup = TRANSACT2_SETPATHINFO;
1944 char *rparam=NULL, *rdata=NULL;
1947 uint32 wire_flags = open_flags_to_wire(flags);
1948 size_t srclen = 2*(strlen(fname)+1);
1950 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
1954 memset(param, '\0', 6);
1955 SSVAL(param,0, SMB_POSIX_PATH_OPEN);
1958 p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
1959 param_len = PTR_DIFF(p, param);
1962 wire_flags &= ~(SMB_O_RDONLY|SMB_O_RDWR|SMB_O_WRONLY);
1963 wire_flags |= SMB_O_DIRECTORY;
1967 SIVAL(p,0,0); /* No oplock. */
1968 SIVAL(p,4,wire_flags);
1969 SIVAL(p,8,unix_perms_to_wire(mode));
1970 SIVAL(p,12,0); /* Top bits of perms currently undefined. */
1971 SSVAL(p,16,SMB_NO_INFO_LEVEL_RETURNED); /* No info level returned. */
1975 if (!cli_send_trans(cli, SMBtrans2,
1977 -1, 0, /* fid, flags */
1978 &setup, 1, 0, /* setup, length, max */
1979 param, param_len, 2, /* param, length, max */
1980 (char *)&data, data_len, cli->max_xmit /* data, length, max */
1988 if (!cli_receive_trans(cli, SMBtrans2,
1989 &rparam, ¶m_len,
1990 &rdata, &data_len)) {
1994 fnum = SVAL(rdata,2);
2002 /****************************************************************************
2003 open - POSIX semantics.
2004 ****************************************************************************/
2006 int cli_posix_open(struct cli_state *cli, const char *fname, int flags, mode_t mode)
2008 return cli_posix_open_internal(cli, fname, flags, mode, False);
2011 /****************************************************************************
2012 mkdir - POSIX semantics.
2013 ****************************************************************************/
2015 int cli_posix_mkdir(struct cli_state *cli, const char *fname, mode_t mode)
2017 return (cli_posix_open_internal(cli, fname, O_CREAT, mode, True) == -1) ? -1 : 0;
2020 /****************************************************************************
2021 unlink or rmdir - POSIX semantics.
2022 ****************************************************************************/
2024 static bool cli_posix_unlink_internal(struct cli_state *cli, const char *fname, bool is_dir)
2026 unsigned int data_len = 0;
2027 unsigned int param_len = 0;
2028 uint16 setup = TRANSACT2_SETPATHINFO;
2031 char *rparam=NULL, *rdata=NULL;
2033 size_t srclen = 2*(strlen(fname)+1);
2035 param = SMB_MALLOC_ARRAY(char, 6+srclen+2);
2039 memset(param, '\0', 6);
2040 SSVAL(param,0, SMB_POSIX_PATH_UNLINK);
2043 p += clistr_push(cli, p, fname, srclen, STR_TERMINATE);
2044 param_len = PTR_DIFF(p, param);
2046 SSVAL(data, 0, is_dir ? SMB_POSIX_UNLINK_DIRECTORY_TARGET :
2047 SMB_POSIX_UNLINK_FILE_TARGET);
2050 if (!cli_send_trans(cli, SMBtrans2,
2052 -1, 0, /* fid, flags */
2053 &setup, 1, 0, /* setup, length, max */
2054 param, param_len, 2, /* param, length, max */
2055 (char *)&data, data_len, cli->max_xmit /* data, length, max */
2063 if (!cli_receive_trans(cli, SMBtrans2,
2064 &rparam, ¶m_len,
2065 &rdata, &data_len)) {
2075 /****************************************************************************
2076 unlink - POSIX semantics.
2077 ****************************************************************************/
2079 bool cli_posix_unlink(struct cli_state *cli, const char *fname)
2081 return cli_posix_unlink_internal(cli, fname, False);
2084 /****************************************************************************
2085 rmdir - POSIX semantics.
2086 ****************************************************************************/
2088 int cli_posix_rmdir(struct cli_state *cli, const char *fname)
2090 return cli_posix_unlink_internal(cli, fname, True);