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 Truncate a file to a specified size
893 ****************************************************************************/
895 bool cli_ftruncate(struct cli_state
*cli
, int fnum
, uint64_t size
)
897 unsigned int param_len
= 6;
898 unsigned int data_len
= 8;
899 uint16 setup
= TRANSACT2_SETFILEINFO
;
901 unsigned char data
[8];
902 char *rparam
=NULL
, *rdata
=NULL
;
903 int saved_timeout
= cli
->timeout
;
906 SSVAL(param
,2,SMB_SET_FILE_END_OF_FILE_INFO
);
909 SBVAL(data
, 0, size
);
911 if (!cli_send_trans(cli
, SMBtrans2
,
913 -1, 0, /* fid, flags */
914 &setup
, 1, 0, /* setup, length, max */
915 param
, param_len
, 2, /* param, length, max */
916 (char *)&data
, data_len
,/* data, length, ... */
917 cli
->max_xmit
)) { /* ... max */
918 cli
->timeout
= saved_timeout
;
922 if (!cli_receive_trans(cli
, SMBtrans2
,
924 &rdata
, &data_len
)) {
925 cli
->timeout
= saved_timeout
;
931 cli
->timeout
= saved_timeout
;
940 /****************************************************************************
941 send a lock with a specified locktype
942 this is used for testing LOCKING_ANDX_CANCEL_LOCK
943 ****************************************************************************/
945 NTSTATUS
cli_locktype(struct cli_state
*cli
, int fnum
,
946 uint32 offset
, uint32 len
,
947 int timeout
, unsigned char locktype
)
950 int saved_timeout
= cli
->timeout
;
952 memset(cli
->outbuf
,'\0',smb_size
);
953 memset(cli
->inbuf
,'\0', smb_size
);
955 cli_set_message(cli
->outbuf
,8,0,True
);
957 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
958 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
959 cli_setup_packet(cli
);
961 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
962 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
963 SCVAL(cli
->outbuf
,smb_vwv3
,locktype
);
964 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
965 SSVAL(cli
->outbuf
,smb_vwv6
,0);
966 SSVAL(cli
->outbuf
,smb_vwv7
,1);
968 p
= smb_buf(cli
->outbuf
);
969 SSVAL(p
, 0, cli
->pid
);
975 cli_setup_bcc(cli
, p
);
980 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
+ 2*1000);
983 if (!cli_receive_smb(cli
)) {
984 cli
->timeout
= saved_timeout
;
985 return NT_STATUS_UNSUCCESSFUL
;
988 cli
->timeout
= saved_timeout
;
990 return cli_nt_error(cli
);
993 /****************************************************************************
995 note that timeout is in units of 2 milliseconds
996 ****************************************************************************/
998 bool cli_lock(struct cli_state
*cli
, int fnum
,
999 uint32 offset
, uint32 len
, int timeout
, enum brl_type lock_type
)
1002 int saved_timeout
= cli
->timeout
;
1004 memset(cli
->outbuf
,'\0',smb_size
);
1005 memset(cli
->inbuf
,'\0', smb_size
);
1007 cli_set_message(cli
->outbuf
,8,0,True
);
1009 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
1010 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1011 cli_setup_packet(cli
);
1013 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1014 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
1015 SCVAL(cli
->outbuf
,smb_vwv3
,(lock_type
== READ_LOCK
? 1 : 0));
1016 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
1017 SSVAL(cli
->outbuf
,smb_vwv6
,0);
1018 SSVAL(cli
->outbuf
,smb_vwv7
,1);
1020 p
= smb_buf(cli
->outbuf
);
1021 SSVAL(p
, 0, cli
->pid
);
1022 SIVAL(p
, 2, offset
);
1027 cli_setup_bcc(cli
, p
);
1032 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
*2 + 5*1000);
1035 if (!cli_receive_smb(cli
)) {
1036 cli
->timeout
= saved_timeout
;
1040 cli
->timeout
= saved_timeout
;
1042 if (cli_is_error(cli
)) {
1049 /****************************************************************************
1051 ****************************************************************************/
1053 bool cli_unlock(struct cli_state
*cli
, int fnum
, uint32 offset
, uint32 len
)
1057 memset(cli
->outbuf
,'\0',smb_size
);
1058 memset(cli
->inbuf
,'\0',smb_size
);
1060 cli_set_message(cli
->outbuf
,8,0,True
);
1062 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
1063 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1064 cli_setup_packet(cli
);
1066 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1067 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
1068 SCVAL(cli
->outbuf
,smb_vwv3
,0);
1069 SIVALS(cli
->outbuf
, smb_vwv4
, 0);
1070 SSVAL(cli
->outbuf
,smb_vwv6
,1);
1071 SSVAL(cli
->outbuf
,smb_vwv7
,0);
1073 p
= smb_buf(cli
->outbuf
);
1074 SSVAL(p
, 0, cli
->pid
);
1075 SIVAL(p
, 2, offset
);
1078 cli_setup_bcc(cli
, p
);
1080 if (!cli_receive_smb(cli
)) {
1084 if (cli_is_error(cli
)) {
1091 /****************************************************************************
1092 Lock a file with 64 bit offsets.
1093 ****************************************************************************/
1095 bool cli_lock64(struct cli_state
*cli
, int fnum
,
1096 SMB_BIG_UINT offset
, SMB_BIG_UINT len
, int timeout
, enum brl_type lock_type
)
1099 int saved_timeout
= cli
->timeout
;
1102 if (! (cli
->capabilities
& CAP_LARGE_FILES
)) {
1103 return cli_lock(cli
, fnum
, offset
, len
, timeout
, lock_type
);
1106 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
1107 ltype
|= LOCKING_ANDX_LARGE_FILES
;
1109 memset(cli
->outbuf
,'\0',smb_size
);
1110 memset(cli
->inbuf
,'\0', smb_size
);
1112 cli_set_message(cli
->outbuf
,8,0,True
);
1114 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
1115 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1116 cli_setup_packet(cli
);
1118 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1119 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
1120 SCVAL(cli
->outbuf
,smb_vwv3
,ltype
);
1121 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
1122 SSVAL(cli
->outbuf
,smb_vwv6
,0);
1123 SSVAL(cli
->outbuf
,smb_vwv7
,1);
1125 p
= smb_buf(cli
->outbuf
);
1126 SIVAL(p
, 0, cli
->pid
);
1127 SOFF_T_R(p
, 4, offset
);
1128 SOFF_T_R(p
, 12, len
);
1131 cli_setup_bcc(cli
, p
);
1135 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
+ 5*1000);
1138 if (!cli_receive_smb(cli
)) {
1139 cli
->timeout
= saved_timeout
;
1143 cli
->timeout
= saved_timeout
;
1145 if (cli_is_error(cli
)) {
1152 /****************************************************************************
1153 Unlock a file with 64 bit offsets.
1154 ****************************************************************************/
1156 bool cli_unlock64(struct cli_state
*cli
, int fnum
, SMB_BIG_UINT offset
, SMB_BIG_UINT len
)
1160 if (! (cli
->capabilities
& CAP_LARGE_FILES
)) {
1161 return cli_unlock(cli
, fnum
, offset
, len
);
1164 memset(cli
->outbuf
,'\0',smb_size
);
1165 memset(cli
->inbuf
,'\0',smb_size
);
1167 cli_set_message(cli
->outbuf
,8,0,True
);
1169 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
1170 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1171 cli_setup_packet(cli
);
1173 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1174 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
1175 SCVAL(cli
->outbuf
,smb_vwv3
,LOCKING_ANDX_LARGE_FILES
);
1176 SIVALS(cli
->outbuf
, smb_vwv4
, 0);
1177 SSVAL(cli
->outbuf
,smb_vwv6
,1);
1178 SSVAL(cli
->outbuf
,smb_vwv7
,0);
1180 p
= smb_buf(cli
->outbuf
);
1181 SIVAL(p
, 0, cli
->pid
);
1182 SOFF_T_R(p
, 4, offset
);
1183 SOFF_T_R(p
, 12, len
);
1185 cli_setup_bcc(cli
, p
);
1187 if (!cli_receive_smb(cli
)) {
1191 if (cli_is_error(cli
)) {
1198 /****************************************************************************
1199 Get/unlock a POSIX lock on a file - internal function.
1200 ****************************************************************************/
1202 static bool cli_posix_lock_internal(struct cli_state
*cli
, int fnum
,
1203 SMB_BIG_UINT offset
, SMB_BIG_UINT len
, bool wait_lock
, enum brl_type lock_type
)
1205 unsigned int param_len
= 4;
1206 unsigned int data_len
= POSIX_LOCK_DATA_SIZE
;
1207 uint16 setup
= TRANSACT2_SETFILEINFO
;
1209 unsigned char data
[POSIX_LOCK_DATA_SIZE
];
1210 char *rparam
=NULL
, *rdata
=NULL
;
1211 int saved_timeout
= cli
->timeout
;
1213 SSVAL(param
,0,fnum
);
1214 SSVAL(param
,2,SMB_SET_POSIX_LOCK
);
1216 switch (lock_type
) {
1218 SSVAL(data
, POSIX_LOCK_TYPE_OFFSET
, POSIX_LOCK_TYPE_READ
);
1221 SSVAL(data
, POSIX_LOCK_TYPE_OFFSET
, POSIX_LOCK_TYPE_WRITE
);
1224 SSVAL(data
, POSIX_LOCK_TYPE_OFFSET
, POSIX_LOCK_TYPE_UNLOCK
);
1231 SSVAL(data
, POSIX_LOCK_FLAGS_OFFSET
, POSIX_LOCK_FLAG_WAIT
);
1232 cli
->timeout
= 0x7FFFFFFF;
1234 SSVAL(data
, POSIX_LOCK_FLAGS_OFFSET
, POSIX_LOCK_FLAG_NOWAIT
);
1237 SIVAL(data
, POSIX_LOCK_PID_OFFSET
, cli
->pid
);
1238 SOFF_T(data
, POSIX_LOCK_START_OFFSET
, offset
);
1239 SOFF_T(data
, POSIX_LOCK_LEN_OFFSET
, len
);
1241 if (!cli_send_trans(cli
, SMBtrans2
,
1243 -1, 0, /* fid, flags */
1244 &setup
, 1, 0, /* setup, length, max */
1245 param
, param_len
, 2, /* param, length, max */
1246 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
1248 cli
->timeout
= saved_timeout
;
1252 if (!cli_receive_trans(cli
, SMBtrans2
,
1253 &rparam
, ¶m_len
,
1254 &rdata
, &data_len
)) {
1255 cli
->timeout
= saved_timeout
;
1261 cli
->timeout
= saved_timeout
;
1269 /****************************************************************************
1271 ****************************************************************************/
1273 bool cli_posix_lock(struct cli_state
*cli
, int fnum
,
1274 SMB_BIG_UINT offset
, SMB_BIG_UINT len
,
1275 bool wait_lock
, enum brl_type lock_type
)
1277 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
1280 return cli_posix_lock_internal(cli
, fnum
, offset
, len
, wait_lock
, lock_type
);
1283 /****************************************************************************
1284 POSIX Unlock a file.
1285 ****************************************************************************/
1287 bool cli_posix_unlock(struct cli_state
*cli
, int fnum
, SMB_BIG_UINT offset
, SMB_BIG_UINT len
)
1289 return cli_posix_lock_internal(cli
, fnum
, offset
, len
, False
, UNLOCK_LOCK
);
1292 /****************************************************************************
1293 POSIX Get any lock covering a file.
1294 ****************************************************************************/
1296 bool cli_posix_getlock(struct cli_state
*cli
, int fnum
, SMB_BIG_UINT
*poffset
, SMB_BIG_UINT
*plen
)
1301 /****************************************************************************
1302 Do a SMBgetattrE call.
1303 ****************************************************************************/
1305 bool cli_getattrE(struct cli_state
*cli
, int fd
,
1306 uint16
*attr
, SMB_OFF_T
*size
,
1307 time_t *change_time
,
1308 time_t *access_time
,
1311 memset(cli
->outbuf
,'\0',smb_size
);
1312 memset(cli
->inbuf
,'\0',smb_size
);
1314 cli_set_message(cli
->outbuf
,1,0,True
);
1316 SCVAL(cli
->outbuf
,smb_com
,SMBgetattrE
);
1317 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1318 cli_setup_packet(cli
);
1320 SSVAL(cli
->outbuf
,smb_vwv0
,fd
);
1323 if (!cli_receive_smb(cli
)) {
1327 if (cli_is_error(cli
)) {
1332 *size
= IVAL(cli
->inbuf
, smb_vwv6
);
1336 *attr
= SVAL(cli
->inbuf
,smb_vwv10
);
1340 *change_time
= cli_make_unix_date2(cli
, cli
->inbuf
+smb_vwv0
);
1344 *access_time
= cli_make_unix_date2(cli
, cli
->inbuf
+smb_vwv2
);
1348 *write_time
= cli_make_unix_date2(cli
, cli
->inbuf
+smb_vwv4
);
1354 /****************************************************************************
1356 ****************************************************************************/
1358 bool cli_getatr(struct cli_state
*cli
, const char *fname
,
1359 uint16
*attr
, SMB_OFF_T
*size
, time_t *write_time
)
1363 memset(cli
->outbuf
,'\0',smb_size
);
1364 memset(cli
->inbuf
,'\0',smb_size
);
1366 cli_set_message(cli
->outbuf
,0,0,True
);
1368 SCVAL(cli
->outbuf
,smb_com
,SMBgetatr
);
1369 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1370 cli_setup_packet(cli
);
1372 p
= smb_buf(cli
->outbuf
);
1374 p
+= clistr_push(cli
, p
, fname
,
1375 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
1377 cli_setup_bcc(cli
, p
);
1380 if (!cli_receive_smb(cli
)) {
1384 if (cli_is_error(cli
)) {
1389 *size
= IVAL(cli
->inbuf
, smb_vwv3
);
1393 *write_time
= cli_make_unix_date3(cli
, cli
->inbuf
+smb_vwv1
);
1397 *attr
= SVAL(cli
->inbuf
,smb_vwv0
);
1403 /****************************************************************************
1404 Do a SMBsetattrE call.
1405 ****************************************************************************/
1407 bool cli_setattrE(struct cli_state
*cli
, int fd
,
1415 memset(cli
->outbuf
,'\0',smb_size
);
1416 memset(cli
->inbuf
,'\0',smb_size
);
1418 cli_set_message(cli
->outbuf
,7,0,True
);
1420 SCVAL(cli
->outbuf
,smb_com
,SMBsetattrE
);
1421 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1422 cli_setup_packet(cli
);
1424 SSVAL(cli
->outbuf
,smb_vwv0
, fd
);
1425 cli_put_dos_date2(cli
, cli
->outbuf
,smb_vwv1
, change_time
);
1426 cli_put_dos_date2(cli
, cli
->outbuf
,smb_vwv3
, access_time
);
1427 cli_put_dos_date2(cli
, cli
->outbuf
,smb_vwv5
, write_time
);
1429 p
= smb_buf(cli
->outbuf
);
1432 cli_setup_bcc(cli
, p
);
1435 if (!cli_receive_smb(cli
)) {
1439 if (cli_is_error(cli
)) {
1446 /****************************************************************************
1447 Do a SMBsetatr call.
1448 ****************************************************************************/
1450 bool cli_setatr(struct cli_state
*cli
, const char *fname
, uint16 attr
, time_t t
)
1454 memset(cli
->outbuf
,'\0',smb_size
);
1455 memset(cli
->inbuf
,'\0',smb_size
);
1457 cli_set_message(cli
->outbuf
,8,0,True
);
1459 SCVAL(cli
->outbuf
,smb_com
,SMBsetatr
);
1460 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1461 cli_setup_packet(cli
);
1463 SSVAL(cli
->outbuf
,smb_vwv0
, attr
);
1464 cli_put_dos_date3(cli
, cli
->outbuf
,smb_vwv1
, t
);
1466 p
= smb_buf(cli
->outbuf
);
1468 p
+= clistr_push(cli
, p
, fname
,
1469 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
1472 cli_setup_bcc(cli
, p
);
1475 if (!cli_receive_smb(cli
)) {
1479 if (cli_is_error(cli
)) {
1486 /****************************************************************************
1487 Check for existance of a dir.
1488 ****************************************************************************/
1490 bool cli_chkpath(struct cli_state
*cli
, const char *path
)
1494 TALLOC_CTX
*frame
= talloc_stackframe();
1496 path2
= talloc_strdup(frame
, path
);
1501 trim_char(path2
,'\0','\\');
1503 path2
= talloc_strdup(frame
, "\\");
1510 memset(cli
->outbuf
,'\0',smb_size
);
1511 cli_set_message(cli
->outbuf
,0,0,True
);
1512 SCVAL(cli
->outbuf
,smb_com
,SMBcheckpath
);
1513 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1514 cli_setup_packet(cli
);
1515 p
= smb_buf(cli
->outbuf
);
1517 p
+= clistr_push(cli
, p
, path2
,
1518 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
1520 cli_setup_bcc(cli
, p
);
1523 if (!cli_receive_smb(cli
)) {
1530 if (cli_is_error(cli
)) return False
;
1535 /****************************************************************************
1537 ****************************************************************************/
1539 bool cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
1541 memset(cli
->outbuf
,'\0',smb_size
);
1542 cli_set_message(cli
->outbuf
,0,0,True
);
1543 SCVAL(cli
->outbuf
,smb_com
,SMBdskattr
);
1544 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1545 cli_setup_packet(cli
);
1548 if (!cli_receive_smb(cli
)) {
1552 *bsize
= SVAL(cli
->inbuf
,smb_vwv1
)*SVAL(cli
->inbuf
,smb_vwv2
);
1553 *total
= SVAL(cli
->inbuf
,smb_vwv0
);
1554 *avail
= SVAL(cli
->inbuf
,smb_vwv3
);
1559 /****************************************************************************
1560 Create and open a temporary file.
1561 ****************************************************************************/
1563 int cli_ctemp(struct cli_state
*cli
, const char *path
, char **tmp_path
)
1568 memset(cli
->outbuf
,'\0',smb_size
);
1569 memset(cli
->inbuf
,'\0',smb_size
);
1571 cli_set_message(cli
->outbuf
,3,0,True
);
1573 SCVAL(cli
->outbuf
,smb_com
,SMBctemp
);
1574 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1575 cli_setup_packet(cli
);
1577 SSVAL(cli
->outbuf
,smb_vwv0
,0);
1578 SIVALS(cli
->outbuf
,smb_vwv1
,-1);
1580 p
= smb_buf(cli
->outbuf
);
1582 p
+= clistr_push(cli
, p
, path
,
1583 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
1585 cli_setup_bcc(cli
, p
);
1588 if (!cli_receive_smb(cli
)) {
1592 if (cli_is_error(cli
)) {
1596 /* despite the spec, the result has a -1, followed by
1597 length, followed by name */
1598 p
= smb_buf(cli
->inbuf
);
1600 len
= smb_buflen(cli
->inbuf
) - 4;
1601 if (len
<= 0 || len
> PATH_MAX
) return -1;
1604 char *path2
= SMB_MALLOC_ARRAY(char, len
+1);
1608 clistr_pull(cli
, path2
, p
,
1609 len
+1, len
, STR_ASCII
);
1613 return SVAL(cli
->inbuf
,smb_vwv0
);
1617 send a raw ioctl - used by the torture code
1619 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, int fnum
, uint32 code
, DATA_BLOB
*blob
)
1621 memset(cli
->outbuf
,'\0',smb_size
);
1622 memset(cli
->inbuf
,'\0',smb_size
);
1624 cli_set_message(cli
->outbuf
, 3, 0, True
);
1625 SCVAL(cli
->outbuf
,smb_com
,SMBioctl
);
1626 cli_setup_packet(cli
);
1628 SSVAL(cli
->outbuf
, smb_vwv0
, fnum
);
1629 SSVAL(cli
->outbuf
, smb_vwv1
, code
>>16);
1630 SSVAL(cli
->outbuf
, smb_vwv2
, (code
&0xFFFF));
1633 if (!cli_receive_smb(cli
)) {
1634 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
1637 if (cli_is_error(cli
)) {
1638 return cli_nt_error(cli
);
1641 *blob
= data_blob_null
;
1643 return NT_STATUS_OK
;
1646 /*********************************************************
1647 Set an extended attribute utility fn.
1648 *********************************************************/
1650 static bool cli_set_ea(struct cli_state
*cli
, uint16 setup
, char *param
, unsigned int param_len
,
1651 const char *ea_name
, const char *ea_val
, size_t ea_len
)
1653 unsigned int data_len
= 0;
1655 char *rparam
=NULL
, *rdata
=NULL
;
1657 size_t ea_namelen
= strlen(ea_name
);
1659 if (ea_namelen
== 0 && ea_len
== 0) {
1661 data
= (char *)SMB_MALLOC(data_len
);
1666 SIVAL(p
,0,data_len
);
1668 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
1669 data
= (char *)SMB_MALLOC(data_len
);
1674 SIVAL(p
,0,data_len
);
1676 SCVAL(p
, 0, 0); /* EA flags. */
1677 SCVAL(p
, 1, ea_namelen
);
1678 SSVAL(p
, 2, ea_len
);
1679 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
1680 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
1683 if (!cli_send_trans(cli
, SMBtrans2
,
1685 -1, 0, /* fid, flags */
1686 &setup
, 1, 0, /* setup, length, max */
1687 param
, param_len
, 2, /* param, length, max */
1688 data
, data_len
, cli
->max_xmit
/* data, length, max */
1694 if (!cli_receive_trans(cli
, SMBtrans2
,
1695 &rparam
, ¶m_len
,
1696 &rdata
, &data_len
)) {
1708 /*********************************************************
1709 Set an extended attribute on a pathname.
1710 *********************************************************/
1712 bool cli_set_ea_path(struct cli_state
*cli
, const char *path
, const char *ea_name
, const char *ea_val
, size_t ea_len
)
1714 uint16 setup
= TRANSACT2_SETPATHINFO
;
1715 unsigned int param_len
= 0;
1717 size_t srclen
= 2*(strlen(path
)+1);
1721 param
= SMB_MALLOC_ARRAY(char, 6+srclen
+2);
1725 memset(param
, '\0', 6);
1726 SSVAL(param
,0,SMB_INFO_SET_EA
);
1729 p
+= clistr_push(cli
, p
, path
, srclen
, STR_TERMINATE
);
1730 param_len
= PTR_DIFF(p
, param
);
1732 ret
= cli_set_ea(cli
, setup
, param
, param_len
, ea_name
, ea_val
, ea_len
);
1737 /*********************************************************
1738 Set an extended attribute on an fnum.
1739 *********************************************************/
1741 bool cli_set_ea_fnum(struct cli_state
*cli
, int fnum
, const char *ea_name
, const char *ea_val
, size_t ea_len
)
1744 uint16 setup
= TRANSACT2_SETFILEINFO
;
1746 memset(param
, 0, 6);
1747 SSVAL(param
,0,fnum
);
1748 SSVAL(param
,2,SMB_INFO_SET_EA
);
1750 return cli_set_ea(cli
, setup
, param
, 6, ea_name
, ea_val
, ea_len
);
1753 /*********************************************************
1754 Get an extended attribute list tility fn.
1755 *********************************************************/
1757 static bool cli_get_ea_list(struct cli_state
*cli
,
1758 uint16 setup
, char *param
, unsigned int param_len
,
1761 struct ea_struct
**pea_list
)
1763 unsigned int data_len
= 0;
1764 unsigned int rparam_len
, rdata_len
;
1765 char *rparam
=NULL
, *rdata
=NULL
;
1770 struct ea_struct
*ea_list
;
1777 if (!cli_send_trans(cli
, SMBtrans2
,
1779 -1, 0, /* fid, flags */
1780 &setup
, 1, 0, /* setup, length, max */
1781 param
, param_len
, 10, /* param, length, max */
1782 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
1787 if (!cli_receive_trans(cli
, SMBtrans2
,
1788 &rparam
, &rparam_len
,
1789 &rdata
, &rdata_len
)) {
1793 if (!rdata
|| rdata_len
< 4) {
1797 ea_size
= (size_t)IVAL(rdata
,0);
1798 if (ea_size
> rdata_len
) {
1803 /* No EA's present. */
1811 /* Validate the EA list and count it. */
1812 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
1813 unsigned int ea_namelen
= CVAL(p
,1);
1814 unsigned int ea_valuelen
= SVAL(p
,2);
1815 if (ea_namelen
== 0) {
1818 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
1821 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
1822 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
1830 *pnum_eas
= num_eas
;
1832 /* Caller only wants number of EA's. */
1837 ea_list
= TALLOC_ARRAY(ctx
, struct ea_struct
, num_eas
);
1842 ea_size
= (size_t)IVAL(rdata
,0);
1845 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
1846 struct ea_struct
*ea
= &ea_list
[num_eas
];
1847 fstring unix_ea_name
;
1848 unsigned int ea_namelen
= CVAL(p
,1);
1849 unsigned int ea_valuelen
= SVAL(p
,2);
1851 ea
->flags
= CVAL(p
,0);
1852 unix_ea_name
[0] = '\0';
1853 pull_ascii_fstring(unix_ea_name
, p
+ 4);
1854 ea
->name
= talloc_strdup(ctx
, unix_ea_name
);
1855 /* Ensure the value is null terminated (in case it's a string). */
1856 ea
->value
= data_blob_talloc(ctx
, NULL
, ea_valuelen
+ 1);
1857 if (!ea
->value
.data
) {
1861 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
1863 ea
->value
.data
[ea_valuelen
] = 0;
1865 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
1868 *pea_list
= ea_list
;
1878 /*********************************************************
1879 Get an extended attribute list from a pathname.
1880 *********************************************************/
1882 bool cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
1885 struct ea_struct
**pea_list
)
1887 uint16 setup
= TRANSACT2_QPATHINFO
;
1888 unsigned int param_len
= 0;
1891 size_t srclen
= 2*(strlen(path
)+1);
1894 param
= SMB_MALLOC_ARRAY(char, 6+srclen
+2);
1900 SSVAL(p
, 0, SMB_INFO_QUERY_ALL_EAS
);
1902 p
+= clistr_push(cli
, p
, path
, srclen
, STR_TERMINATE
);
1903 param_len
= PTR_DIFF(p
, param
);
1905 ret
= cli_get_ea_list(cli
, setup
, param
, param_len
, ctx
, pnum_eas
, pea_list
);
1910 /*********************************************************
1911 Get an extended attribute list from an fnum.
1912 *********************************************************/
1914 bool cli_get_ea_list_fnum(struct cli_state
*cli
, int fnum
,
1917 struct ea_struct
**pea_list
)
1919 uint16 setup
= TRANSACT2_QFILEINFO
;
1922 memset(param
, 0, 6);
1923 SSVAL(param
,0,fnum
);
1924 SSVAL(param
,2,SMB_INFO_SET_EA
);
1926 return cli_get_ea_list(cli
, setup
, param
, 6, ctx
, pnum_eas
, pea_list
);
1929 /****************************************************************************
1930 Convert open "flags" arg to uint32 on wire.
1931 ****************************************************************************/
1933 static uint32
open_flags_to_wire(int flags
)
1935 int open_mode
= flags
& O_ACCMODE
;
1938 switch (open_mode
) {
1940 ret
|= SMB_O_WRONLY
;
1947 ret
|= SMB_O_RDONLY
;
1951 if (flags
& O_CREAT
) {
1954 if (flags
& O_EXCL
) {
1957 if (flags
& O_TRUNC
) {
1961 if (flags
& O_SYNC
) {
1965 if (flags
& O_APPEND
) {
1966 ret
|= SMB_O_APPEND
;
1968 #if defined(O_DIRECT)
1969 if (flags
& O_DIRECT
) {
1970 ret
|= SMB_O_DIRECT
;
1973 #if defined(O_DIRECTORY)
1974 if (flags
& O_DIRECTORY
) {
1975 ret
&= ~(SMB_O_RDONLY
|SMB_O_RDWR
|SMB_O_WRONLY
);
1976 ret
|= SMB_O_DIRECTORY
;
1982 /****************************************************************************
1983 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
1984 ****************************************************************************/
1986 static int cli_posix_open_internal(struct cli_state
*cli
, const char *fname
, int flags
, mode_t mode
, bool is_dir
)
1988 unsigned int data_len
= 0;
1989 unsigned int param_len
= 0;
1990 uint16 setup
= TRANSACT2_SETPATHINFO
;
1993 char *rparam
=NULL
, *rdata
=NULL
;
1996 uint32 wire_flags
= open_flags_to_wire(flags
);
1997 size_t srclen
= 2*(strlen(fname
)+1);
1999 param
= SMB_MALLOC_ARRAY(char, 6+srclen
+2);
2003 memset(param
, '\0', 6);
2004 SSVAL(param
,0, SMB_POSIX_PATH_OPEN
);
2007 p
+= clistr_push(cli
, p
, fname
, srclen
, STR_TERMINATE
);
2008 param_len
= PTR_DIFF(p
, param
);
2011 wire_flags
&= ~(SMB_O_RDONLY
|SMB_O_RDWR
|SMB_O_WRONLY
);
2012 wire_flags
|= SMB_O_DIRECTORY
;
2016 SIVAL(p
,0,0); /* No oplock. */
2017 SIVAL(p
,4,wire_flags
);
2018 SIVAL(p
,8,unix_perms_to_wire(mode
));
2019 SIVAL(p
,12,0); /* Top bits of perms currently undefined. */
2020 SSVAL(p
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
2024 if (!cli_send_trans(cli
, SMBtrans2
,
2026 -1, 0, /* fid, flags */
2027 &setup
, 1, 0, /* setup, length, max */
2028 param
, param_len
, 2, /* param, length, max */
2029 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
2037 if (!cli_receive_trans(cli
, SMBtrans2
,
2038 &rparam
, ¶m_len
,
2039 &rdata
, &data_len
)) {
2043 fnum
= SVAL(rdata
,2);
2051 /****************************************************************************
2052 open - POSIX semantics.
2053 ****************************************************************************/
2055 int cli_posix_open(struct cli_state
*cli
, const char *fname
, int flags
, mode_t mode
)
2057 return cli_posix_open_internal(cli
, fname
, flags
, mode
, False
);
2060 /****************************************************************************
2061 mkdir - POSIX semantics.
2062 ****************************************************************************/
2064 int cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
2066 return (cli_posix_open_internal(cli
, fname
, O_CREAT
, mode
, True
) == -1) ? -1 : 0;
2069 /****************************************************************************
2070 unlink or rmdir - POSIX semantics.
2071 ****************************************************************************/
2073 static bool cli_posix_unlink_internal(struct cli_state
*cli
, const char *fname
, bool is_dir
)
2075 unsigned int data_len
= 0;
2076 unsigned int param_len
= 0;
2077 uint16 setup
= TRANSACT2_SETPATHINFO
;
2080 char *rparam
=NULL
, *rdata
=NULL
;
2082 size_t srclen
= 2*(strlen(fname
)+1);
2084 param
= SMB_MALLOC_ARRAY(char, 6+srclen
+2);
2088 memset(param
, '\0', 6);
2089 SSVAL(param
,0, SMB_POSIX_PATH_UNLINK
);
2092 p
+= clistr_push(cli
, p
, fname
, srclen
, STR_TERMINATE
);
2093 param_len
= PTR_DIFF(p
, param
);
2095 SSVAL(data
, 0, is_dir
? SMB_POSIX_UNLINK_DIRECTORY_TARGET
:
2096 SMB_POSIX_UNLINK_FILE_TARGET
);
2099 if (!cli_send_trans(cli
, SMBtrans2
,
2101 -1, 0, /* fid, flags */
2102 &setup
, 1, 0, /* setup, length, max */
2103 param
, param_len
, 2, /* param, length, max */
2104 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
2112 if (!cli_receive_trans(cli
, SMBtrans2
,
2113 &rparam
, ¶m_len
,
2114 &rdata
, &data_len
)) {
2124 /****************************************************************************
2125 unlink - POSIX semantics.
2126 ****************************************************************************/
2128 bool cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
2130 return cli_posix_unlink_internal(cli
, fname
, False
);
2133 /****************************************************************************
2134 rmdir - POSIX semantics.
2135 ****************************************************************************/
2137 int cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
2139 return cli_posix_unlink_internal(cli
, fname
, True
);