2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2002
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /****************************************************************************
24 Hard/Symlink a file (UNIX extensions).
25 Creates new name (sym)linked to oldname.
26 ****************************************************************************/
28 static bool cli_link_internal(struct cli_state
*cli
, const char *oldname
, const char *newname
, bool hard_link
)
30 unsigned int data_len
= 0;
31 unsigned int param_len
= 0;
32 uint16 setup
= TRANSACT2_SETPATHINFO
;
35 char *rparam
=NULL
, *rdata
=NULL
;
37 size_t oldlen
= 2*(strlen(oldname
)+1);
38 size_t newlen
= 2*(strlen(newname
)+1);
40 param
= SMB_MALLOC_ARRAY(char, 6+newlen
+2);
46 data
= SMB_MALLOC_ARRAY(char, oldlen
+2);
53 SSVAL(param
,0,hard_link
? SMB_SET_FILE_UNIX_HLINK
: SMB_SET_FILE_UNIX_LINK
);
57 p
+= clistr_push(cli
, p
, newname
, newlen
, STR_TERMINATE
);
58 param_len
= PTR_DIFF(p
, param
);
61 p
+= clistr_push(cli
, p
, oldname
, oldlen
, STR_TERMINATE
);
62 data_len
= PTR_DIFF(p
, data
);
64 if (!cli_send_trans(cli
, SMBtrans2
,
66 -1, 0, /* fid, flags */
67 &setup
, 1, 0, /* setup, length, max */
68 param
, param_len
, 2, /* param, length, max */
69 data
, data_len
, cli
->max_xmit
/* data, length, max */
79 if (!cli_receive_trans(cli
, SMBtrans2
,
93 /****************************************************************************
94 Map standard UNIX permissions onto wire representations.
95 ****************************************************************************/
97 uint32
unix_perms_to_wire(mode_t perms
)
101 ret
|= ((perms
& S_IXOTH
) ? UNIX_X_OTH
: 0);
102 ret
|= ((perms
& S_IWOTH
) ? UNIX_W_OTH
: 0);
103 ret
|= ((perms
& S_IROTH
) ? UNIX_R_OTH
: 0);
104 ret
|= ((perms
& S_IXGRP
) ? UNIX_X_GRP
: 0);
105 ret
|= ((perms
& S_IWGRP
) ? UNIX_W_GRP
: 0);
106 ret
|= ((perms
& S_IRGRP
) ? UNIX_R_GRP
: 0);
107 ret
|= ((perms
& S_IXUSR
) ? UNIX_X_USR
: 0);
108 ret
|= ((perms
& S_IWUSR
) ? UNIX_W_USR
: 0);
109 ret
|= ((perms
& S_IRUSR
) ? UNIX_R_USR
: 0);
111 ret
|= ((perms
& S_ISVTX
) ? UNIX_STICKY
: 0);
114 ret
|= ((perms
& S_ISGID
) ? UNIX_SET_GID
: 0);
117 ret
|= ((perms
& S_ISUID
) ? UNIX_SET_UID
: 0);
122 /****************************************************************************
123 Map wire permissions to standard UNIX.
124 ****************************************************************************/
126 mode_t
wire_perms_to_unix(uint32 perms
)
128 mode_t ret
= (mode_t
)0;
130 ret
|= ((perms
& UNIX_X_OTH
) ? S_IXOTH
: 0);
131 ret
|= ((perms
& UNIX_W_OTH
) ? S_IWOTH
: 0);
132 ret
|= ((perms
& UNIX_R_OTH
) ? S_IROTH
: 0);
133 ret
|= ((perms
& UNIX_X_GRP
) ? S_IXGRP
: 0);
134 ret
|= ((perms
& UNIX_W_GRP
) ? S_IWGRP
: 0);
135 ret
|= ((perms
& UNIX_R_GRP
) ? S_IRGRP
: 0);
136 ret
|= ((perms
& UNIX_X_USR
) ? S_IXUSR
: 0);
137 ret
|= ((perms
& UNIX_W_USR
) ? S_IWUSR
: 0);
138 ret
|= ((perms
& UNIX_R_USR
) ? S_IRUSR
: 0);
140 ret
|= ((perms
& UNIX_STICKY
) ? S_ISVTX
: 0);
143 ret
|= ((perms
& UNIX_SET_GID
) ? S_ISGID
: 0);
146 ret
|= ((perms
& UNIX_SET_UID
) ? S_ISUID
: 0);
151 /****************************************************************************
152 Return the file type from the wire filetype for UNIX extensions.
153 ****************************************************************************/
155 static mode_t
unix_filetype_from_wire(uint32 wire_type
)
163 case UNIX_TYPE_SYMLINK
:
167 case UNIX_TYPE_CHARDEV
:
171 case UNIX_TYPE_BLKDEV
:
179 case UNIX_TYPE_SOCKET
:
187 /****************************************************************************
188 Do a POSIX getfacl (UNIX extensions).
189 ****************************************************************************/
191 bool cli_unix_getfacl(struct cli_state
*cli
, const char *name
, size_t *prb_size
, char **retbuf
)
193 unsigned int param_len
= 0;
194 unsigned int data_len
= 0;
195 uint16 setup
= TRANSACT2_QPATHINFO
;
197 size_t nlen
= 2*(strlen(name
)+1);
198 char *rparam
=NULL
, *rdata
=NULL
;
201 param
= SMB_MALLOC_ARRAY(char, 6+nlen
+2);
208 SSVAL(p
, 0, SMB_QUERY_POSIX_ACL
);
210 p
+= clistr_push(cli
, p
, name
, nlen
, STR_TERMINATE
);
211 param_len
= PTR_DIFF(p
, param
);
213 if (!cli_send_trans(cli
, SMBtrans2
,
215 -1, 0, /* fid, flags */
216 &setup
, 1, 0, /* setup, length, max */
217 param
, param_len
, 2, /* param, length, max */
218 NULL
, 0, cli
->max_xmit
/* data, length, max */
226 if (!cli_receive_trans(cli
, SMBtrans2
,
228 &rdata
, &data_len
)) {
240 *prb_size
= (size_t)data_len
;
245 /****************************************************************************
246 Stat a file (UNIX extensions).
247 ****************************************************************************/
249 bool cli_unix_stat(struct cli_state
*cli
, const char *name
, SMB_STRUCT_STAT
*sbuf
)
251 unsigned int param_len
= 0;
252 unsigned int data_len
= 0;
253 uint16 setup
= TRANSACT2_QPATHINFO
;
255 size_t nlen
= 2*(strlen(name
)+1);
256 char *rparam
=NULL
, *rdata
=NULL
;
261 param
= SMB_MALLOC_ARRAY(char, 6+nlen
+2);
267 SSVAL(p
, 0, SMB_QUERY_FILE_UNIX_BASIC
);
269 p
+= clistr_push(cli
, p
, name
, nlen
, STR_TERMINATE
);
270 param_len
= PTR_DIFF(p
, param
);
272 if (!cli_send_trans(cli
, SMBtrans2
,
274 -1, 0, /* fid, flags */
275 &setup
, 1, 0, /* setup, length, max */
276 param
, param_len
, 2, /* param, length, max */
277 NULL
, 0, cli
->max_xmit
/* data, length, max */
285 if (!cli_receive_trans(cli
, SMBtrans2
,
287 &rdata
, &data_len
)) {
297 sbuf
->st_size
= IVAL2_TO_SMB_BIG_UINT(rdata
,0); /* total size, in bytes */
298 sbuf
->st_blocks
= IVAL2_TO_SMB_BIG_UINT(rdata
,8); /* number of blocks allocated */
299 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
300 sbuf
->st_blocks
/= STAT_ST_BLOCKSIZE
;
302 /* assume 512 byte blocks */
303 sbuf
->st_blocks
/= 512;
305 set_ctimespec(sbuf
, interpret_long_date(rdata
+ 16)); /* time of last change */
306 set_atimespec(sbuf
, interpret_long_date(rdata
+ 24)); /* time of last access */
307 set_mtimespec(sbuf
, interpret_long_date(rdata
+ 32)); /* time of last modification */
309 sbuf
->st_uid
= (uid_t
) IVAL(rdata
,40); /* user ID of owner */
310 sbuf
->st_gid
= (gid_t
) IVAL(rdata
,48); /* group ID of owner */
311 sbuf
->st_mode
= unix_filetype_from_wire(IVAL(rdata
, 56));
312 #if defined(HAVE_MAKEDEV)
314 uint32 dev_major
= IVAL(rdata
,60);
315 uint32 dev_minor
= IVAL(rdata
,68);
316 sbuf
->st_rdev
= makedev(dev_major
, dev_minor
);
319 sbuf
->st_ino
= (SMB_INO_T
)IVAL2_TO_SMB_BIG_UINT(rdata
,76); /* inode */
320 sbuf
->st_mode
|= wire_perms_to_unix(IVAL(rdata
,84)); /* protection */
321 sbuf
->st_nlink
= IVAL(rdata
,92); /* number of hard links */
329 /****************************************************************************
330 Symlink a file (UNIX extensions).
331 ****************************************************************************/
333 bool cli_unix_symlink(struct cli_state
*cli
, const char *oldname
, const char *newname
)
335 return cli_link_internal(cli
, oldname
, newname
, False
);
338 /****************************************************************************
339 Hard a file (UNIX extensions).
340 ****************************************************************************/
342 bool cli_unix_hardlink(struct cli_state
*cli
, const char *oldname
, const char *newname
)
344 return cli_link_internal(cli
, oldname
, newname
, True
);
347 /****************************************************************************
348 Chmod or chown a file internal (UNIX extensions).
349 ****************************************************************************/
351 static bool cli_unix_chmod_chown_internal(struct cli_state
*cli
, const char *fname
, uint32 mode
, uint32 uid
, uint32 gid
)
353 unsigned int data_len
= 0;
354 unsigned int param_len
= 0;
355 uint16 setup
= TRANSACT2_SETPATHINFO
;
356 size_t nlen
= 2*(strlen(fname
)+1);
359 char *rparam
=NULL
, *rdata
=NULL
;
362 param
= SMB_MALLOC_ARRAY(char, 6+nlen
+2);
366 memset(param
, '\0', 6);
367 memset(data
, 0, sizeof(data
));
369 SSVAL(param
,0,SMB_SET_FILE_UNIX_BASIC
);
372 p
+= clistr_push(cli
, p
, fname
, nlen
, STR_TERMINATE
);
373 param_len
= PTR_DIFF(p
, param
);
375 memset(data
, 0xff, 40); /* Set all sizes/times to no change. */
383 if (!cli_send_trans(cli
, SMBtrans2
,
385 -1, 0, /* fid, flags */
386 &setup
, 1, 0, /* setup, length, max */
387 param
, param_len
, 2, /* param, length, max */
388 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
396 if (!cli_receive_trans(cli
, SMBtrans2
,
398 &rdata
, &data_len
)) {
408 /****************************************************************************
409 chmod a file (UNIX extensions).
410 ****************************************************************************/
412 bool cli_unix_chmod(struct cli_state
*cli
, const char *fname
, mode_t mode
)
414 return cli_unix_chmod_chown_internal(cli
, fname
,
415 unix_perms_to_wire(mode
), SMB_UID_NO_CHANGE
, SMB_GID_NO_CHANGE
);
418 /****************************************************************************
419 chown a file (UNIX extensions).
420 ****************************************************************************/
422 bool cli_unix_chown(struct cli_state
*cli
, const char *fname
, uid_t uid
, gid_t gid
)
424 return cli_unix_chmod_chown_internal(cli
, fname
,
425 SMB_MODE_NO_CHANGE
, (uint32
)uid
, (uint32
)gid
);
428 /****************************************************************************
430 ****************************************************************************/
432 bool cli_rename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
436 memset(cli
->outbuf
,'\0',smb_size
);
437 memset(cli
->inbuf
,'\0',smb_size
);
439 cli_set_message(cli
->outbuf
,1, 0, true);
441 SCVAL(cli
->outbuf
,smb_com
,SMBmv
);
442 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
443 cli_setup_packet(cli
);
445 SSVAL(cli
->outbuf
,smb_vwv0
,aSYSTEM
| aHIDDEN
| aDIR
);
447 p
= smb_buf(cli
->outbuf
);
449 p
+= clistr_push(cli
, p
, fname_src
,
450 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
452 p
+= clistr_push(cli
, p
, fname_dst
,
453 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
455 cli_setup_bcc(cli
, p
);
458 if (!cli_receive_smb(cli
)) {
462 if (cli_is_error(cli
)) {
469 /****************************************************************************
471 ****************************************************************************/
473 bool cli_ntrename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
477 memset(cli
->outbuf
,'\0',smb_size
);
478 memset(cli
->inbuf
,'\0',smb_size
);
480 cli_set_message(cli
->outbuf
, 4, 0, true);
482 SCVAL(cli
->outbuf
,smb_com
,SMBntrename
);
483 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
484 cli_setup_packet(cli
);
486 SSVAL(cli
->outbuf
,smb_vwv0
,aSYSTEM
| aHIDDEN
| aDIR
);
487 SSVAL(cli
->outbuf
,smb_vwv1
, RENAME_FLAG_RENAME
);
489 p
= smb_buf(cli
->outbuf
);
491 p
+= clistr_push(cli
, p
, fname_src
,
492 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
494 p
+= clistr_push(cli
, p
, fname_dst
,
495 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
497 cli_setup_bcc(cli
, p
);
500 if (!cli_receive_smb(cli
)) {
504 if (cli_is_error(cli
)) {
511 /****************************************************************************
513 ****************************************************************************/
515 bool cli_nt_hardlink(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
519 memset(cli
->outbuf
,'\0',smb_size
);
520 memset(cli
->inbuf
,'\0',smb_size
);
522 cli_set_message(cli
->outbuf
, 4, 0, true);
524 SCVAL(cli
->outbuf
,smb_com
,SMBntrename
);
525 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
526 cli_setup_packet(cli
);
528 SSVAL(cli
->outbuf
,smb_vwv0
,aSYSTEM
| aHIDDEN
| aDIR
);
529 SSVAL(cli
->outbuf
,smb_vwv1
, RENAME_FLAG_HARD_LINK
);
531 p
= smb_buf(cli
->outbuf
);
533 p
+= clistr_push(cli
, p
, fname_src
,
534 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
536 p
+= clistr_push(cli
, p
, fname_dst
,
537 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
539 cli_setup_bcc(cli
, p
);
542 if (!cli_receive_smb(cli
)) {
546 if (cli_is_error(cli
)) {
553 /****************************************************************************
555 ****************************************************************************/
557 bool cli_unlink_full(struct cli_state
*cli
, const char *fname
, uint16 attrs
)
561 memset(cli
->outbuf
,'\0',smb_size
);
562 memset(cli
->inbuf
,'\0',smb_size
);
564 cli_set_message(cli
->outbuf
,1, 0, true);
566 SCVAL(cli
->outbuf
,smb_com
,SMBunlink
);
567 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
568 cli_setup_packet(cli
);
570 SSVAL(cli
->outbuf
,smb_vwv0
, attrs
);
572 p
= smb_buf(cli
->outbuf
);
574 p
+= clistr_push(cli
, p
, fname
,
575 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
577 cli_setup_bcc(cli
, p
);
579 if (!cli_receive_smb(cli
)) {
583 if (cli_is_error(cli
)) {
590 /****************************************************************************
592 ****************************************************************************/
594 bool cli_unlink(struct cli_state
*cli
, const char *fname
)
596 return cli_unlink_full(cli
, fname
, aSYSTEM
| aHIDDEN
);
599 /****************************************************************************
601 ****************************************************************************/
603 bool cli_mkdir(struct cli_state
*cli
, const char *dname
)
607 memset(cli
->outbuf
,'\0',smb_size
);
608 memset(cli
->inbuf
,'\0',smb_size
);
610 cli_set_message(cli
->outbuf
,0, 0, true);
612 SCVAL(cli
->outbuf
,smb_com
,SMBmkdir
);
613 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
614 cli_setup_packet(cli
);
616 p
= smb_buf(cli
->outbuf
);
618 p
+= clistr_push(cli
, p
, dname
,
619 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
621 cli_setup_bcc(cli
, p
);
624 if (!cli_receive_smb(cli
)) {
628 if (cli_is_error(cli
)) {
635 /****************************************************************************
637 ****************************************************************************/
639 bool cli_rmdir(struct cli_state
*cli
, const char *dname
)
643 memset(cli
->outbuf
,'\0',smb_size
);
644 memset(cli
->inbuf
,'\0',smb_size
);
646 cli_set_message(cli
->outbuf
,0, 0, true);
648 SCVAL(cli
->outbuf
,smb_com
,SMBrmdir
);
649 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
650 cli_setup_packet(cli
);
652 p
= smb_buf(cli
->outbuf
);
654 p
+= clistr_push(cli
, p
, dname
,
655 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
657 cli_setup_bcc(cli
, p
);
660 if (!cli_receive_smb(cli
)) {
664 if (cli_is_error(cli
)) {
671 /****************************************************************************
672 Set or clear the delete on close flag.
673 ****************************************************************************/
675 int cli_nt_delete_on_close(struct cli_state
*cli
, int fnum
, bool flag
)
677 unsigned int data_len
= 1;
678 unsigned int param_len
= 6;
679 uint16 setup
= TRANSACT2_SETFILEINFO
;
682 char *rparam
=NULL
, *rdata
=NULL
;
684 memset(param
, 0, param_len
);
686 SSVAL(param
,2,SMB_SET_FILE_DISPOSITION_INFO
);
690 if (!cli_send_trans(cli
, SMBtrans2
,
692 -1, 0, /* fid, flags */
693 &setup
, 1, 0, /* setup, length, max */
694 param
, param_len
, 2, /* param, length, max */
695 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
700 if (!cli_receive_trans(cli
, SMBtrans2
,
702 &rdata
, &data_len
)) {
712 /****************************************************************************
713 Open a file - exposing the full horror of the NT API :-).
715 ****************************************************************************/
717 int cli_nt_create_full(struct cli_state
*cli
, const char *fname
,
718 uint32 CreatFlags
, uint32 DesiredAccess
,
719 uint32 FileAttributes
, uint32 ShareAccess
,
720 uint32 CreateDisposition
, uint32 CreateOptions
,
726 memset(cli
->outbuf
,'\0',smb_size
);
727 memset(cli
->inbuf
,'\0',smb_size
);
729 cli_set_message(cli
->outbuf
,24,0, true);
731 SCVAL(cli
->outbuf
,smb_com
,SMBntcreateX
);
732 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
733 cli_setup_packet(cli
);
735 SSVAL(cli
->outbuf
,smb_vwv0
,0xFF);
736 if (cli
->use_oplocks
)
737 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
739 SIVAL(cli
->outbuf
,smb_ntcreate_Flags
, CreatFlags
);
740 SIVAL(cli
->outbuf
,smb_ntcreate_RootDirectoryFid
, 0x0);
741 SIVAL(cli
->outbuf
,smb_ntcreate_DesiredAccess
, DesiredAccess
);
742 SIVAL(cli
->outbuf
,smb_ntcreate_FileAttributes
, FileAttributes
);
743 SIVAL(cli
->outbuf
,smb_ntcreate_ShareAccess
, ShareAccess
);
744 SIVAL(cli
->outbuf
,smb_ntcreate_CreateDisposition
, CreateDisposition
);
745 SIVAL(cli
->outbuf
,smb_ntcreate_CreateOptions
, CreateOptions
);
746 SIVAL(cli
->outbuf
,smb_ntcreate_ImpersonationLevel
, 0x02);
747 SCVAL(cli
->outbuf
,smb_ntcreate_SecurityFlags
, SecurityFlags
);
749 p
= smb_buf(cli
->outbuf
);
750 /* this alignment and termination is critical for netapp filers. Don't change */
751 p
+= clistr_align_out(cli
, p
, 0);
752 len
= clistr_push(cli
, p
, fname
,
753 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), 0);
755 SSVAL(cli
->outbuf
,smb_ntcreate_NameLength
, len
);
756 /* sigh. this copes with broken netapp filer behaviour */
757 p
+= clistr_push(cli
, p
, "",
758 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
760 cli_setup_bcc(cli
, p
);
763 if (!cli_receive_smb(cli
)) {
767 if (cli_is_error(cli
)) {
771 return SVAL(cli
->inbuf
,smb_vwv2
+ 1);
774 struct async_req
*cli_ntcreate_send(TALLOC_CTX
*mem_ctx
,
775 struct event_context
*ev
,
776 struct cli_state
*cli
,
779 uint32_t DesiredAccess
,
780 uint32_t FileAttributes
,
781 uint32_t ShareAccess
,
782 uint32_t CreateDisposition
,
783 uint32_t CreateOptions
,
784 uint8_t SecurityFlags
)
786 struct async_req
*result
;
788 size_t converted_len
;
791 SCVAL(vwv
+0, 0, 0xFF);
796 if (cli
->use_oplocks
) {
797 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
799 SIVAL(vwv
+3, 1, CreatFlags
);
800 SIVAL(vwv
+5, 1, 0x0); /* RootDirectoryFid */
801 SIVAL(vwv
+7, 1, DesiredAccess
);
802 SIVAL(vwv
+9, 1, 0x0); /* AllocationSize */
803 SIVAL(vwv
+11, 1, 0x0); /* AllocationSize */
804 SIVAL(vwv
+13, 1, FileAttributes
);
805 SIVAL(vwv
+15, 1, ShareAccess
);
806 SIVAL(vwv
+17, 1, CreateDisposition
);
807 SIVAL(vwv
+19, 1, CreateOptions
);
808 SIVAL(vwv
+21, 1, 0x02); /* ImpersonationLevel */
809 SCVAL(vwv
+23, 1, SecurityFlags
);
811 bytes
= talloc_array(talloc_tos(), uint8_t, 0);
812 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
),
813 fname
, strlen(fname
)+1,
816 /* sigh. this copes with broken netapp filer behaviour */
817 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), "", 1, NULL
);
823 SSVAL(vwv
+2, 1, converted_len
);
825 result
= cli_request_send(mem_ctx
, ev
, cli
, SMBntcreateX
, 0,
826 24, vwv
, 0, talloc_get_size(bytes
), bytes
);
831 NTSTATUS
cli_ntcreate_recv(struct async_req
*req
, uint16_t *pfnum
)
839 if (async_req_is_nterror(req
, &status
)) {
843 status
= cli_pull_reply(req
, &wct
, &vwv
, &num_bytes
, &bytes
);
844 if (!NT_STATUS_IS_OK(status
)) {
849 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
852 *pfnum
= SVAL(vwv
+2, 1);
857 NTSTATUS
cli_ntcreate(struct cli_state
*cli
,
860 uint32_t DesiredAccess
,
861 uint32_t FileAttributes
,
862 uint32_t ShareAccess
,
863 uint32_t CreateDisposition
,
864 uint32_t CreateOptions
,
865 uint8_t SecurityFlags
,
868 TALLOC_CTX
*frame
= talloc_stackframe();
869 struct event_context
*ev
;
870 struct async_req
*req
;
873 if (cli
->fd_event
!= NULL
) {
875 * Can't use sync call while an async call is in flight
877 status
= NT_STATUS_INVALID_PARAMETER
;
881 ev
= event_context_init(frame
);
883 status
= NT_STATUS_NO_MEMORY
;
887 req
= cli_ntcreate_send(frame
, ev
, cli
, fname
, CreatFlags
,
888 DesiredAccess
, FileAttributes
, ShareAccess
,
889 CreateDisposition
, CreateOptions
,
892 status
= NT_STATUS_NO_MEMORY
;
896 while (req
->state
< ASYNC_REQ_DONE
) {
900 status
= cli_ntcreate_recv(req
, pfid
);
906 /****************************************************************************
908 ****************************************************************************/
910 int cli_nt_create(struct cli_state
*cli
, const char *fname
, uint32 DesiredAccess
)
912 return cli_nt_create_full(cli
, fname
, 0, DesiredAccess
, 0,
913 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0);
916 uint8_t *smb_bytes_push_str(uint8_t *buf
, bool ucs2
,
917 const char *str
, size_t str_len
,
918 size_t *pconverted_size
)
922 size_t converted_size
;
928 buflen
= talloc_get_size(buf
);
930 * We're pushing into an SMB buffer, align odd
932 if (ucs2
&& (buflen
% 2 == 0)) {
933 buf
= TALLOC_REALLOC_ARRAY(NULL
, buf
, uint8_t, buflen
+ 1);
941 if (!convert_string_allocate(talloc_tos(), CH_UNIX
,
942 ucs2
? CH_UTF16LE
: CH_DOS
,
943 str
, str_len
, &converted
,
944 &converted_size
, true)) {
948 buf
= TALLOC_REALLOC_ARRAY(NULL
, buf
, uint8_t,
949 buflen
+ converted_size
);
951 TALLOC_FREE(converted
);
955 memcpy(buf
+ buflen
, converted
, converted_size
);
957 TALLOC_FREE(converted
);
959 if (pconverted_size
) {
960 *pconverted_size
= converted_size
;
966 /****************************************************************************
968 WARNING: if you open with O_WRONLY then getattrE won't work!
969 ****************************************************************************/
971 struct async_req
*cli_open_send(TALLOC_CTX
*mem_ctx
, struct event_context
*ev
,
972 struct cli_state
*cli
,
973 const char *fname
, int flags
, int share_mode
)
976 unsigned accessmode
= 0;
977 uint8_t additional_flags
= 0;
980 struct async_req
*result
;
982 if (flags
& O_CREAT
) {
985 if (!(flags
& O_EXCL
)) {
992 accessmode
= (share_mode
<<4);
994 if ((flags
& O_ACCMODE
) == O_RDWR
) {
996 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
1001 if ((flags
& O_SYNC
) == O_SYNC
) {
1002 accessmode
|= (1<<14);
1006 if (share_mode
== DENY_FCB
) {
1010 SCVAL(vwv
+ 0, 0, 0xFF);
1011 SCVAL(vwv
+ 0, 1, 0);
1012 SSVAL(vwv
+ 1, 0, 0);
1013 SSVAL(vwv
+ 2, 0, 0); /* no additional info */
1014 SSVAL(vwv
+ 3, 0, accessmode
);
1015 SSVAL(vwv
+ 4, 0, aSYSTEM
| aHIDDEN
);
1016 SSVAL(vwv
+ 5, 0, 0);
1017 SIVAL(vwv
+ 6, 0, 0);
1018 SSVAL(vwv
+ 8, 0, openfn
);
1019 SIVAL(vwv
+ 9, 0, 0);
1020 SIVAL(vwv
+ 11, 0, 0);
1021 SIVAL(vwv
+ 13, 0, 0);
1023 if (cli
->use_oplocks
) {
1024 /* if using oplocks then ask for a batch oplock via
1025 core and extended methods */
1027 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
;
1028 SSVAL(vwv
+2, 0, SVAL(vwv
+2, 0) | 6);
1031 bytes
= talloc_array(talloc_tos(), uint8_t, 0);
1032 bytes
= smb_bytes_push_str(bytes
, cli_ucs2(cli
), fname
,
1033 strlen(fname
)+1, NULL
);
1034 if (bytes
== NULL
) {
1038 result
= cli_request_send(mem_ctx
, ev
, cli
, SMBopenX
, additional_flags
,
1039 15, vwv
, 0, talloc_get_size(bytes
), bytes
);
1044 NTSTATUS
cli_open_recv(struct async_req
*req
, int *fnum
)
1052 if (async_req_is_nterror(req
, &status
)) {
1056 status
= cli_pull_reply(req
, &wct
, &vwv
, &num_bytes
, &bytes
);
1057 if (!NT_STATUS_IS_OK(status
)) {
1062 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1065 *fnum
= SVAL(vwv
+2, 0);
1067 return NT_STATUS_OK
;
1070 int cli_open(struct cli_state
*cli
, const char *fname
, int flags
,
1073 TALLOC_CTX
*frame
= talloc_stackframe();
1074 struct event_context
*ev
;
1075 struct async_req
*req
;
1078 if (cli
->fd_event
!= NULL
) {
1080 * Can't use sync call while an async call is in flight
1082 cli_set_error(cli
, NT_STATUS_INVALID_PARAMETER
);
1086 ev
= event_context_init(frame
);
1091 req
= cli_open_send(frame
, ev
, cli
, fname
, flags
, share_mode
);
1096 while (req
->state
< ASYNC_REQ_DONE
) {
1097 event_loop_once(ev
);
1100 cli_open_recv(req
, &result
);
1106 /****************************************************************************
1108 ****************************************************************************/
1110 struct async_req
*cli_close_send(TALLOC_CTX
*mem_ctx
, struct event_context
*ev
,
1111 struct cli_state
*cli
, int fnum
)
1115 SSVAL(vwv
+0, 0, fnum
);
1116 SIVALS(vwv
+1, 0, -1);
1118 return cli_request_send(mem_ctx
, ev
, cli
, SMBclose
, 0, 3, vwv
, 0,
1122 NTSTATUS
cli_close_recv(struct async_req
*req
)
1130 if (async_req_is_nterror(req
, &status
)) {
1134 return cli_pull_reply(req
, &wct
, &vwv
, &num_bytes
, &bytes
);
1137 bool cli_close(struct cli_state
*cli
, int fnum
)
1139 TALLOC_CTX
*frame
= talloc_stackframe();
1140 struct event_context
*ev
;
1141 struct async_req
*req
;
1142 bool result
= false;
1144 if (cli
->fd_event
!= NULL
) {
1146 * Can't use sync call while an async call is in flight
1148 cli_set_error(cli
, NT_STATUS_INVALID_PARAMETER
);
1152 ev
= event_context_init(frame
);
1157 req
= cli_close_send(frame
, ev
, cli
, fnum
);
1162 while (req
->state
< ASYNC_REQ_DONE
) {
1163 event_loop_once(ev
);
1166 result
= NT_STATUS_IS_OK(cli_close_recv(req
));
1172 /****************************************************************************
1173 Truncate a file to a specified size
1174 ****************************************************************************/
1176 bool cli_ftruncate(struct cli_state
*cli
, int fnum
, uint64_t size
)
1178 unsigned int param_len
= 6;
1179 unsigned int data_len
= 8;
1180 uint16 setup
= TRANSACT2_SETFILEINFO
;
1182 unsigned char data
[8];
1183 char *rparam
=NULL
, *rdata
=NULL
;
1184 int saved_timeout
= cli
->timeout
;
1186 SSVAL(param
,0,fnum
);
1187 SSVAL(param
,2,SMB_SET_FILE_END_OF_FILE_INFO
);
1190 SBVAL(data
, 0, size
);
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
,/* data, length, ... */
1198 cli
->max_xmit
)) { /* ... 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
;
1221 /****************************************************************************
1222 send a lock with a specified locktype
1223 this is used for testing LOCKING_ANDX_CANCEL_LOCK
1224 ****************************************************************************/
1226 NTSTATUS
cli_locktype(struct cli_state
*cli
, int fnum
,
1227 uint32 offset
, uint32 len
,
1228 int timeout
, unsigned char locktype
)
1231 int saved_timeout
= cli
->timeout
;
1233 memset(cli
->outbuf
,'\0',smb_size
);
1234 memset(cli
->inbuf
,'\0', smb_size
);
1236 cli_set_message(cli
->outbuf
,8,0,True
);
1238 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
1239 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1240 cli_setup_packet(cli
);
1242 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1243 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
1244 SCVAL(cli
->outbuf
,smb_vwv3
,locktype
);
1245 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
1246 SSVAL(cli
->outbuf
,smb_vwv6
,0);
1247 SSVAL(cli
->outbuf
,smb_vwv7
,1);
1249 p
= smb_buf(cli
->outbuf
);
1250 SSVAL(p
, 0, cli
->pid
);
1251 SIVAL(p
, 2, offset
);
1256 cli_setup_bcc(cli
, p
);
1261 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
+ 2*1000);
1264 if (!cli_receive_smb(cli
)) {
1265 cli
->timeout
= saved_timeout
;
1266 return NT_STATUS_UNSUCCESSFUL
;
1269 cli
->timeout
= saved_timeout
;
1271 return cli_nt_error(cli
);
1274 /****************************************************************************
1276 note that timeout is in units of 2 milliseconds
1277 ****************************************************************************/
1279 bool cli_lock(struct cli_state
*cli
, int fnum
,
1280 uint32 offset
, uint32 len
, int timeout
, enum brl_type lock_type
)
1283 int saved_timeout
= cli
->timeout
;
1285 memset(cli
->outbuf
,'\0',smb_size
);
1286 memset(cli
->inbuf
,'\0', smb_size
);
1288 cli_set_message(cli
->outbuf
,8,0,True
);
1290 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
1291 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1292 cli_setup_packet(cli
);
1294 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1295 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
1296 SCVAL(cli
->outbuf
,smb_vwv3
,(lock_type
== READ_LOCK
? 1 : 0));
1297 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
1298 SSVAL(cli
->outbuf
,smb_vwv6
,0);
1299 SSVAL(cli
->outbuf
,smb_vwv7
,1);
1301 p
= smb_buf(cli
->outbuf
);
1302 SSVAL(p
, 0, cli
->pid
);
1303 SIVAL(p
, 2, offset
);
1308 cli_setup_bcc(cli
, p
);
1313 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
*2 + 5*1000);
1316 if (!cli_receive_smb(cli
)) {
1317 cli
->timeout
= saved_timeout
;
1321 cli
->timeout
= saved_timeout
;
1323 if (cli_is_error(cli
)) {
1330 /****************************************************************************
1332 ****************************************************************************/
1334 bool cli_unlock(struct cli_state
*cli
, int fnum
, uint32 offset
, uint32 len
)
1338 memset(cli
->outbuf
,'\0',smb_size
);
1339 memset(cli
->inbuf
,'\0',smb_size
);
1341 cli_set_message(cli
->outbuf
,8,0,True
);
1343 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
1344 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1345 cli_setup_packet(cli
);
1347 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1348 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
1349 SCVAL(cli
->outbuf
,smb_vwv3
,0);
1350 SIVALS(cli
->outbuf
, smb_vwv4
, 0);
1351 SSVAL(cli
->outbuf
,smb_vwv6
,1);
1352 SSVAL(cli
->outbuf
,smb_vwv7
,0);
1354 p
= smb_buf(cli
->outbuf
);
1355 SSVAL(p
, 0, cli
->pid
);
1356 SIVAL(p
, 2, offset
);
1359 cli_setup_bcc(cli
, p
);
1361 if (!cli_receive_smb(cli
)) {
1365 if (cli_is_error(cli
)) {
1372 /****************************************************************************
1373 Lock a file with 64 bit offsets.
1374 ****************************************************************************/
1376 bool cli_lock64(struct cli_state
*cli
, int fnum
,
1377 uint64_t offset
, uint64_t len
, int timeout
, enum brl_type lock_type
)
1380 int saved_timeout
= cli
->timeout
;
1383 if (! (cli
->capabilities
& CAP_LARGE_FILES
)) {
1384 return cli_lock(cli
, fnum
, offset
, len
, timeout
, lock_type
);
1387 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
1388 ltype
|= LOCKING_ANDX_LARGE_FILES
;
1390 memset(cli
->outbuf
,'\0',smb_size
);
1391 memset(cli
->inbuf
,'\0', smb_size
);
1393 cli_set_message(cli
->outbuf
,8,0,True
);
1395 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
1396 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1397 cli_setup_packet(cli
);
1399 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1400 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
1401 SCVAL(cli
->outbuf
,smb_vwv3
,ltype
);
1402 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
1403 SSVAL(cli
->outbuf
,smb_vwv6
,0);
1404 SSVAL(cli
->outbuf
,smb_vwv7
,1);
1406 p
= smb_buf(cli
->outbuf
);
1407 SIVAL(p
, 0, cli
->pid
);
1408 SOFF_T_R(p
, 4, offset
);
1409 SOFF_T_R(p
, 12, len
);
1412 cli_setup_bcc(cli
, p
);
1416 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
+ 5*1000);
1419 if (!cli_receive_smb(cli
)) {
1420 cli
->timeout
= saved_timeout
;
1424 cli
->timeout
= saved_timeout
;
1426 if (cli_is_error(cli
)) {
1433 /****************************************************************************
1434 Unlock a file with 64 bit offsets.
1435 ****************************************************************************/
1437 bool cli_unlock64(struct cli_state
*cli
, int fnum
, uint64_t offset
, uint64_t len
)
1441 if (! (cli
->capabilities
& CAP_LARGE_FILES
)) {
1442 return cli_unlock(cli
, fnum
, offset
, len
);
1445 memset(cli
->outbuf
,'\0',smb_size
);
1446 memset(cli
->inbuf
,'\0',smb_size
);
1448 cli_set_message(cli
->outbuf
,8,0,True
);
1450 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
1451 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1452 cli_setup_packet(cli
);
1454 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1455 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
1456 SCVAL(cli
->outbuf
,smb_vwv3
,LOCKING_ANDX_LARGE_FILES
);
1457 SIVALS(cli
->outbuf
, smb_vwv4
, 0);
1458 SSVAL(cli
->outbuf
,smb_vwv6
,1);
1459 SSVAL(cli
->outbuf
,smb_vwv7
,0);
1461 p
= smb_buf(cli
->outbuf
);
1462 SIVAL(p
, 0, cli
->pid
);
1463 SOFF_T_R(p
, 4, offset
);
1464 SOFF_T_R(p
, 12, len
);
1466 cli_setup_bcc(cli
, p
);
1468 if (!cli_receive_smb(cli
)) {
1472 if (cli_is_error(cli
)) {
1479 /****************************************************************************
1480 Get/unlock a POSIX lock on a file - internal function.
1481 ****************************************************************************/
1483 static bool cli_posix_lock_internal(struct cli_state
*cli
, int fnum
,
1484 uint64_t offset
, uint64_t len
, bool wait_lock
, enum brl_type lock_type
)
1486 unsigned int param_len
= 4;
1487 unsigned int data_len
= POSIX_LOCK_DATA_SIZE
;
1488 uint16 setup
= TRANSACT2_SETFILEINFO
;
1490 unsigned char data
[POSIX_LOCK_DATA_SIZE
];
1491 char *rparam
=NULL
, *rdata
=NULL
;
1492 int saved_timeout
= cli
->timeout
;
1494 SSVAL(param
,0,fnum
);
1495 SSVAL(param
,2,SMB_SET_POSIX_LOCK
);
1497 switch (lock_type
) {
1499 SSVAL(data
, POSIX_LOCK_TYPE_OFFSET
, POSIX_LOCK_TYPE_READ
);
1502 SSVAL(data
, POSIX_LOCK_TYPE_OFFSET
, POSIX_LOCK_TYPE_WRITE
);
1505 SSVAL(data
, POSIX_LOCK_TYPE_OFFSET
, POSIX_LOCK_TYPE_UNLOCK
);
1512 SSVAL(data
, POSIX_LOCK_FLAGS_OFFSET
, POSIX_LOCK_FLAG_WAIT
);
1513 cli
->timeout
= 0x7FFFFFFF;
1515 SSVAL(data
, POSIX_LOCK_FLAGS_OFFSET
, POSIX_LOCK_FLAG_NOWAIT
);
1518 SIVAL(data
, POSIX_LOCK_PID_OFFSET
, cli
->pid
);
1519 SOFF_T(data
, POSIX_LOCK_START_OFFSET
, offset
);
1520 SOFF_T(data
, POSIX_LOCK_LEN_OFFSET
, len
);
1522 if (!cli_send_trans(cli
, SMBtrans2
,
1524 -1, 0, /* fid, flags */
1525 &setup
, 1, 0, /* setup, length, max */
1526 param
, param_len
, 2, /* param, length, max */
1527 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
1529 cli
->timeout
= saved_timeout
;
1533 if (!cli_receive_trans(cli
, SMBtrans2
,
1534 &rparam
, ¶m_len
,
1535 &rdata
, &data_len
)) {
1536 cli
->timeout
= saved_timeout
;
1542 cli
->timeout
= saved_timeout
;
1550 /****************************************************************************
1552 ****************************************************************************/
1554 bool cli_posix_lock(struct cli_state
*cli
, int fnum
,
1555 uint64_t offset
, uint64_t len
,
1556 bool wait_lock
, enum brl_type lock_type
)
1558 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
1561 return cli_posix_lock_internal(cli
, fnum
, offset
, len
, wait_lock
, lock_type
);
1564 /****************************************************************************
1565 POSIX Unlock a file.
1566 ****************************************************************************/
1568 bool cli_posix_unlock(struct cli_state
*cli
, int fnum
, uint64_t offset
, uint64_t len
)
1570 return cli_posix_lock_internal(cli
, fnum
, offset
, len
, False
, UNLOCK_LOCK
);
1573 /****************************************************************************
1574 POSIX Get any lock covering a file.
1575 ****************************************************************************/
1577 bool cli_posix_getlock(struct cli_state
*cli
, int fnum
, uint64_t *poffset
, uint64_t *plen
)
1582 /****************************************************************************
1583 Do a SMBgetattrE call.
1584 ****************************************************************************/
1586 bool cli_getattrE(struct cli_state
*cli
, int fd
,
1587 uint16
*attr
, SMB_OFF_T
*size
,
1588 time_t *change_time
,
1589 time_t *access_time
,
1592 memset(cli
->outbuf
,'\0',smb_size
);
1593 memset(cli
->inbuf
,'\0',smb_size
);
1595 cli_set_message(cli
->outbuf
,1,0,True
);
1597 SCVAL(cli
->outbuf
,smb_com
,SMBgetattrE
);
1598 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1599 cli_setup_packet(cli
);
1601 SSVAL(cli
->outbuf
,smb_vwv0
,fd
);
1604 if (!cli_receive_smb(cli
)) {
1608 if (cli_is_error(cli
)) {
1613 *size
= IVAL(cli
->inbuf
, smb_vwv6
);
1617 *attr
= SVAL(cli
->inbuf
,smb_vwv10
);
1621 *change_time
= cli_make_unix_date2(cli
, cli
->inbuf
+smb_vwv0
);
1625 *access_time
= cli_make_unix_date2(cli
, cli
->inbuf
+smb_vwv2
);
1629 *write_time
= cli_make_unix_date2(cli
, cli
->inbuf
+smb_vwv4
);
1635 /****************************************************************************
1637 ****************************************************************************/
1639 bool cli_getatr(struct cli_state
*cli
, const char *fname
,
1640 uint16
*attr
, SMB_OFF_T
*size
, time_t *write_time
)
1644 memset(cli
->outbuf
,'\0',smb_size
);
1645 memset(cli
->inbuf
,'\0',smb_size
);
1647 cli_set_message(cli
->outbuf
,0,0,True
);
1649 SCVAL(cli
->outbuf
,smb_com
,SMBgetatr
);
1650 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1651 cli_setup_packet(cli
);
1653 p
= smb_buf(cli
->outbuf
);
1655 p
+= clistr_push(cli
, p
, fname
,
1656 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
1658 cli_setup_bcc(cli
, p
);
1661 if (!cli_receive_smb(cli
)) {
1665 if (cli_is_error(cli
)) {
1670 *size
= IVAL(cli
->inbuf
, smb_vwv3
);
1674 *write_time
= cli_make_unix_date3(cli
, cli
->inbuf
+smb_vwv1
);
1678 *attr
= SVAL(cli
->inbuf
,smb_vwv0
);
1684 /****************************************************************************
1685 Do a SMBsetattrE call.
1686 ****************************************************************************/
1688 bool cli_setattrE(struct cli_state
*cli
, int fd
,
1696 memset(cli
->outbuf
,'\0',smb_size
);
1697 memset(cli
->inbuf
,'\0',smb_size
);
1699 cli_set_message(cli
->outbuf
,7,0,True
);
1701 SCVAL(cli
->outbuf
,smb_com
,SMBsetattrE
);
1702 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1703 cli_setup_packet(cli
);
1705 SSVAL(cli
->outbuf
,smb_vwv0
, fd
);
1706 cli_put_dos_date2(cli
, cli
->outbuf
,smb_vwv1
, change_time
);
1707 cli_put_dos_date2(cli
, cli
->outbuf
,smb_vwv3
, access_time
);
1708 cli_put_dos_date2(cli
, cli
->outbuf
,smb_vwv5
, write_time
);
1710 p
= smb_buf(cli
->outbuf
);
1713 cli_setup_bcc(cli
, p
);
1716 if (!cli_receive_smb(cli
)) {
1720 if (cli_is_error(cli
)) {
1727 /****************************************************************************
1728 Do a SMBsetatr call.
1729 ****************************************************************************/
1731 bool cli_setatr(struct cli_state
*cli
, const char *fname
, uint16 attr
, time_t t
)
1735 memset(cli
->outbuf
,'\0',smb_size
);
1736 memset(cli
->inbuf
,'\0',smb_size
);
1738 cli_set_message(cli
->outbuf
,8,0,True
);
1740 SCVAL(cli
->outbuf
,smb_com
,SMBsetatr
);
1741 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1742 cli_setup_packet(cli
);
1744 SSVAL(cli
->outbuf
,smb_vwv0
, attr
);
1745 cli_put_dos_date3(cli
, cli
->outbuf
,smb_vwv1
, t
);
1747 p
= smb_buf(cli
->outbuf
);
1749 p
+= clistr_push(cli
, p
, fname
,
1750 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
1753 cli_setup_bcc(cli
, p
);
1756 if (!cli_receive_smb(cli
)) {
1760 if (cli_is_error(cli
)) {
1767 /****************************************************************************
1768 Check for existance of a dir.
1769 ****************************************************************************/
1771 bool cli_chkpath(struct cli_state
*cli
, const char *path
)
1775 TALLOC_CTX
*frame
= talloc_stackframe();
1777 path2
= talloc_strdup(frame
, path
);
1782 trim_char(path2
,'\0','\\');
1784 path2
= talloc_strdup(frame
, "\\");
1791 memset(cli
->outbuf
,'\0',smb_size
);
1792 cli_set_message(cli
->outbuf
,0,0,True
);
1793 SCVAL(cli
->outbuf
,smb_com
,SMBcheckpath
);
1794 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1795 cli_setup_packet(cli
);
1796 p
= smb_buf(cli
->outbuf
);
1798 p
+= clistr_push(cli
, p
, path2
,
1799 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
1801 cli_setup_bcc(cli
, p
);
1804 if (!cli_receive_smb(cli
)) {
1811 if (cli_is_error(cli
)) return False
;
1816 /****************************************************************************
1818 ****************************************************************************/
1820 bool cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
1822 memset(cli
->outbuf
,'\0',smb_size
);
1823 cli_set_message(cli
->outbuf
,0,0,True
);
1824 SCVAL(cli
->outbuf
,smb_com
,SMBdskattr
);
1825 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1826 cli_setup_packet(cli
);
1829 if (!cli_receive_smb(cli
)) {
1833 *bsize
= SVAL(cli
->inbuf
,smb_vwv1
)*SVAL(cli
->inbuf
,smb_vwv2
);
1834 *total
= SVAL(cli
->inbuf
,smb_vwv0
);
1835 *avail
= SVAL(cli
->inbuf
,smb_vwv3
);
1840 /****************************************************************************
1841 Create and open a temporary file.
1842 ****************************************************************************/
1844 int cli_ctemp(struct cli_state
*cli
, const char *path
, char **tmp_path
)
1849 memset(cli
->outbuf
,'\0',smb_size
);
1850 memset(cli
->inbuf
,'\0',smb_size
);
1852 cli_set_message(cli
->outbuf
,3,0,True
);
1854 SCVAL(cli
->outbuf
,smb_com
,SMBctemp
);
1855 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1856 cli_setup_packet(cli
);
1858 SSVAL(cli
->outbuf
,smb_vwv0
,0);
1859 SIVALS(cli
->outbuf
,smb_vwv1
,-1);
1861 p
= smb_buf(cli
->outbuf
);
1863 p
+= clistr_push(cli
, p
, path
,
1864 cli
->bufsize
- PTR_DIFF(p
,cli
->outbuf
), STR_TERMINATE
);
1866 cli_setup_bcc(cli
, p
);
1869 if (!cli_receive_smb(cli
)) {
1873 if (cli_is_error(cli
)) {
1877 /* despite the spec, the result has a -1, followed by
1878 length, followed by name */
1879 p
= smb_buf(cli
->inbuf
);
1881 len
= smb_buflen(cli
->inbuf
) - 4;
1882 if (len
<= 0 || len
> PATH_MAX
) return -1;
1885 char *path2
= SMB_MALLOC_ARRAY(char, len
+1);
1889 clistr_pull(cli
->inbuf
, path2
, p
,
1890 len
+1, len
, STR_ASCII
);
1894 return SVAL(cli
->inbuf
,smb_vwv0
);
1898 send a raw ioctl - used by the torture code
1900 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, int fnum
, uint32 code
, DATA_BLOB
*blob
)
1902 memset(cli
->outbuf
,'\0',smb_size
);
1903 memset(cli
->inbuf
,'\0',smb_size
);
1905 cli_set_message(cli
->outbuf
, 3, 0, True
);
1906 SCVAL(cli
->outbuf
,smb_com
,SMBioctl
);
1907 cli_setup_packet(cli
);
1909 SSVAL(cli
->outbuf
, smb_vwv0
, fnum
);
1910 SSVAL(cli
->outbuf
, smb_vwv1
, code
>>16);
1911 SSVAL(cli
->outbuf
, smb_vwv2
, (code
&0xFFFF));
1914 if (!cli_receive_smb(cli
)) {
1915 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
1918 if (cli_is_error(cli
)) {
1919 return cli_nt_error(cli
);
1922 *blob
= data_blob_null
;
1924 return NT_STATUS_OK
;
1927 /*********************************************************
1928 Set an extended attribute utility fn.
1929 *********************************************************/
1931 static bool cli_set_ea(struct cli_state
*cli
, uint16 setup
, char *param
, unsigned int param_len
,
1932 const char *ea_name
, const char *ea_val
, size_t ea_len
)
1934 unsigned int data_len
= 0;
1936 char *rparam
=NULL
, *rdata
=NULL
;
1938 size_t ea_namelen
= strlen(ea_name
);
1940 if (ea_namelen
== 0 && ea_len
== 0) {
1942 data
= (char *)SMB_MALLOC(data_len
);
1947 SIVAL(p
,0,data_len
);
1949 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
1950 data
= (char *)SMB_MALLOC(data_len
);
1955 SIVAL(p
,0,data_len
);
1957 SCVAL(p
, 0, 0); /* EA flags. */
1958 SCVAL(p
, 1, ea_namelen
);
1959 SSVAL(p
, 2, ea_len
);
1960 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
1961 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
1964 if (!cli_send_trans(cli
, SMBtrans2
,
1966 -1, 0, /* fid, flags */
1967 &setup
, 1, 0, /* setup, length, max */
1968 param
, param_len
, 2, /* param, length, max */
1969 data
, data_len
, cli
->max_xmit
/* data, length, max */
1975 if (!cli_receive_trans(cli
, SMBtrans2
,
1976 &rparam
, ¶m_len
,
1977 &rdata
, &data_len
)) {
1989 /*********************************************************
1990 Set an extended attribute on a pathname.
1991 *********************************************************/
1993 bool cli_set_ea_path(struct cli_state
*cli
, const char *path
, const char *ea_name
, const char *ea_val
, size_t ea_len
)
1995 uint16 setup
= TRANSACT2_SETPATHINFO
;
1996 unsigned int param_len
= 0;
1998 size_t srclen
= 2*(strlen(path
)+1);
2002 param
= SMB_MALLOC_ARRAY(char, 6+srclen
+2);
2006 memset(param
, '\0', 6);
2007 SSVAL(param
,0,SMB_INFO_SET_EA
);
2010 p
+= clistr_push(cli
, p
, path
, srclen
, STR_TERMINATE
);
2011 param_len
= PTR_DIFF(p
, param
);
2013 ret
= cli_set_ea(cli
, setup
, param
, param_len
, ea_name
, ea_val
, ea_len
);
2018 /*********************************************************
2019 Set an extended attribute on an fnum.
2020 *********************************************************/
2022 bool cli_set_ea_fnum(struct cli_state
*cli
, int fnum
, const char *ea_name
, const char *ea_val
, size_t ea_len
)
2025 uint16 setup
= TRANSACT2_SETFILEINFO
;
2027 memset(param
, 0, 6);
2028 SSVAL(param
,0,fnum
);
2029 SSVAL(param
,2,SMB_INFO_SET_EA
);
2031 return cli_set_ea(cli
, setup
, param
, 6, ea_name
, ea_val
, ea_len
);
2034 /*********************************************************
2035 Get an extended attribute list utility fn.
2036 *********************************************************/
2038 static bool cli_get_ea_list(struct cli_state
*cli
,
2039 uint16 setup
, char *param
, unsigned int param_len
,
2042 struct ea_struct
**pea_list
)
2044 unsigned int data_len
= 0;
2045 unsigned int rparam_len
, rdata_len
;
2046 char *rparam
=NULL
, *rdata
=NULL
;
2051 struct ea_struct
*ea_list
;
2058 if (!cli_send_trans(cli
, SMBtrans2
,
2060 -1, 0, /* fid, flags */
2061 &setup
, 1, 0, /* setup, length, max */
2062 param
, param_len
, 10, /* param, length, max */
2063 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
2068 if (!cli_receive_trans(cli
, SMBtrans2
,
2069 &rparam
, &rparam_len
,
2070 &rdata
, &rdata_len
)) {
2074 if (!rdata
|| rdata_len
< 4) {
2078 ea_size
= (size_t)IVAL(rdata
,0);
2079 if (ea_size
> rdata_len
) {
2084 /* No EA's present. */
2092 /* Validate the EA list and count it. */
2093 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
2094 unsigned int ea_namelen
= CVAL(p
,1);
2095 unsigned int ea_valuelen
= SVAL(p
,2);
2096 if (ea_namelen
== 0) {
2099 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
2102 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
2103 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
2111 *pnum_eas
= num_eas
;
2113 /* Caller only wants number of EA's. */
2118 ea_list
= TALLOC_ARRAY(ctx
, struct ea_struct
, num_eas
);
2123 ea_size
= (size_t)IVAL(rdata
,0);
2126 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
2127 struct ea_struct
*ea
= &ea_list
[num_eas
];
2128 fstring unix_ea_name
;
2129 unsigned int ea_namelen
= CVAL(p
,1);
2130 unsigned int ea_valuelen
= SVAL(p
,2);
2132 ea
->flags
= CVAL(p
,0);
2133 unix_ea_name
[0] = '\0';
2134 pull_ascii_fstring(unix_ea_name
, p
+ 4);
2135 ea
->name
= talloc_strdup(ctx
, unix_ea_name
);
2136 /* Ensure the value is null terminated (in case it's a string). */
2137 ea
->value
= data_blob_talloc(ctx
, NULL
, ea_valuelen
+ 1);
2138 if (!ea
->value
.data
) {
2142 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
2144 ea
->value
.data
[ea_valuelen
] = 0;
2146 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
2149 *pea_list
= ea_list
;
2159 /*********************************************************
2160 Get an extended attribute list from a pathname.
2161 *********************************************************/
2163 bool cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
2166 struct ea_struct
**pea_list
)
2168 uint16 setup
= TRANSACT2_QPATHINFO
;
2169 unsigned int param_len
= 0;
2172 size_t srclen
= 2*(strlen(path
)+1);
2175 param
= SMB_MALLOC_ARRAY(char, 6+srclen
+2);
2181 SSVAL(p
, 0, SMB_INFO_QUERY_ALL_EAS
);
2183 p
+= clistr_push(cli
, p
, path
, srclen
, STR_TERMINATE
);
2184 param_len
= PTR_DIFF(p
, param
);
2186 ret
= cli_get_ea_list(cli
, setup
, param
, param_len
, ctx
, pnum_eas
, pea_list
);
2191 /*********************************************************
2192 Get an extended attribute list from an fnum.
2193 *********************************************************/
2195 bool cli_get_ea_list_fnum(struct cli_state
*cli
, int fnum
,
2198 struct ea_struct
**pea_list
)
2200 uint16 setup
= TRANSACT2_QFILEINFO
;
2203 memset(param
, 0, 6);
2204 SSVAL(param
,0,fnum
);
2205 SSVAL(param
,2,SMB_INFO_SET_EA
);
2207 return cli_get_ea_list(cli
, setup
, param
, 6, ctx
, pnum_eas
, pea_list
);
2210 /****************************************************************************
2211 Convert open "flags" arg to uint32 on wire.
2212 ****************************************************************************/
2214 static uint32
open_flags_to_wire(int flags
)
2216 int open_mode
= flags
& O_ACCMODE
;
2219 switch (open_mode
) {
2221 ret
|= SMB_O_WRONLY
;
2228 ret
|= SMB_O_RDONLY
;
2232 if (flags
& O_CREAT
) {
2235 if (flags
& O_EXCL
) {
2238 if (flags
& O_TRUNC
) {
2242 if (flags
& O_SYNC
) {
2246 if (flags
& O_APPEND
) {
2247 ret
|= SMB_O_APPEND
;
2249 #if defined(O_DIRECT)
2250 if (flags
& O_DIRECT
) {
2251 ret
|= SMB_O_DIRECT
;
2254 #if defined(O_DIRECTORY)
2255 if (flags
& O_DIRECTORY
) {
2256 ret
&= ~(SMB_O_RDONLY
|SMB_O_RDWR
|SMB_O_WRONLY
);
2257 ret
|= SMB_O_DIRECTORY
;
2263 /****************************************************************************
2264 Open a file - POSIX semantics. Returns fnum. Doesn't request oplock.
2265 ****************************************************************************/
2267 static int cli_posix_open_internal(struct cli_state
*cli
, const char *fname
, int flags
, mode_t mode
, bool is_dir
)
2269 unsigned int data_len
= 0;
2270 unsigned int param_len
= 0;
2271 uint16 setup
= TRANSACT2_SETPATHINFO
;
2274 char *rparam
=NULL
, *rdata
=NULL
;
2277 uint32 wire_flags
= open_flags_to_wire(flags
);
2278 size_t srclen
= 2*(strlen(fname
)+1);
2280 param
= SMB_MALLOC_ARRAY(char, 6+srclen
+2);
2284 memset(param
, '\0', 6);
2285 SSVAL(param
,0, SMB_POSIX_PATH_OPEN
);
2288 p
+= clistr_push(cli
, p
, fname
, srclen
, STR_TERMINATE
);
2289 param_len
= PTR_DIFF(p
, param
);
2292 wire_flags
&= ~(SMB_O_RDONLY
|SMB_O_RDWR
|SMB_O_WRONLY
);
2293 wire_flags
|= SMB_O_DIRECTORY
;
2297 SIVAL(p
,0,0); /* No oplock. */
2298 SIVAL(p
,4,wire_flags
);
2299 SIVAL(p
,8,unix_perms_to_wire(mode
));
2300 SIVAL(p
,12,0); /* Top bits of perms currently undefined. */
2301 SSVAL(p
,16,SMB_NO_INFO_LEVEL_RETURNED
); /* No info level returned. */
2305 if (!cli_send_trans(cli
, SMBtrans2
,
2307 -1, 0, /* fid, flags */
2308 &setup
, 1, 0, /* setup, length, max */
2309 param
, param_len
, 2, /* param, length, max */
2310 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
2318 if (!cli_receive_trans(cli
, SMBtrans2
,
2319 &rparam
, ¶m_len
,
2320 &rdata
, &data_len
)) {
2324 fnum
= SVAL(rdata
,2);
2332 /****************************************************************************
2333 open - POSIX semantics.
2334 ****************************************************************************/
2336 int cli_posix_open(struct cli_state
*cli
, const char *fname
, int flags
, mode_t mode
)
2338 return cli_posix_open_internal(cli
, fname
, flags
, mode
, False
);
2341 /****************************************************************************
2342 mkdir - POSIX semantics.
2343 ****************************************************************************/
2345 int cli_posix_mkdir(struct cli_state
*cli
, const char *fname
, mode_t mode
)
2347 return (cli_posix_open_internal(cli
, fname
, O_CREAT
, mode
, True
) == -1) ? -1 : 0;
2350 /****************************************************************************
2351 unlink or rmdir - POSIX semantics.
2352 ****************************************************************************/
2354 static bool cli_posix_unlink_internal(struct cli_state
*cli
, const char *fname
, bool is_dir
)
2356 unsigned int data_len
= 0;
2357 unsigned int param_len
= 0;
2358 uint16 setup
= TRANSACT2_SETPATHINFO
;
2361 char *rparam
=NULL
, *rdata
=NULL
;
2363 size_t srclen
= 2*(strlen(fname
)+1);
2365 param
= SMB_MALLOC_ARRAY(char, 6+srclen
+2);
2369 memset(param
, '\0', 6);
2370 SSVAL(param
,0, SMB_POSIX_PATH_UNLINK
);
2373 p
+= clistr_push(cli
, p
, fname
, srclen
, STR_TERMINATE
);
2374 param_len
= PTR_DIFF(p
, param
);
2376 SSVAL(data
, 0, is_dir
? SMB_POSIX_UNLINK_DIRECTORY_TARGET
:
2377 SMB_POSIX_UNLINK_FILE_TARGET
);
2380 if (!cli_send_trans(cli
, SMBtrans2
,
2382 -1, 0, /* fid, flags */
2383 &setup
, 1, 0, /* setup, length, max */
2384 param
, param_len
, 2, /* param, length, max */
2385 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
2393 if (!cli_receive_trans(cli
, SMBtrans2
,
2394 &rparam
, ¶m_len
,
2395 &rdata
, &data_len
)) {
2405 /****************************************************************************
2406 unlink - POSIX semantics.
2407 ****************************************************************************/
2409 bool cli_posix_unlink(struct cli_state
*cli
, const char *fname
)
2411 return cli_posix_unlink_internal(cli
, fname
, False
);
2414 /****************************************************************************
2415 rmdir - POSIX semantics.
2416 ****************************************************************************/
2418 int cli_posix_rmdir(struct cli_state
*cli
, const char *fname
)
2420 return cli_posix_unlink_internal(cli
, fname
, True
);