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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /****************************************************************************
27 Hard/Symlink a file (UNIX extensions).
28 Creates new name (sym)linked to oldname.
29 ****************************************************************************/
31 static BOOL
cli_link_internal(struct cli_state
*cli
, const char *oldname
, const char *newname
, BOOL hard_link
)
33 unsigned int data_len
= 0;
34 unsigned int param_len
= 0;
35 uint16 setup
= TRANSACT2_SETPATHINFO
;
36 char param
[sizeof(pstring
)+6];
38 char *rparam
=NULL
, *rdata
=NULL
;
40 size_t oldlen
= 2*(strlen(oldname
)+1);
41 size_t newlen
= 2*(strlen(newname
)+1);
43 memset(param
, 0, sizeof(param
));
44 SSVAL(param
,0,hard_link
? SMB_SET_FILE_UNIX_HLINK
: SMB_SET_FILE_UNIX_LINK
);
47 p
+= clistr_push(cli
, p
, newname
, MIN(newlen
, sizeof(param
)-6), STR_TERMINATE
);
48 param_len
= PTR_DIFF(p
, param
);
51 p
+= clistr_push(cli
, p
, oldname
, MIN(oldlen
,sizeof(data
)), STR_TERMINATE
);
52 data_len
= PTR_DIFF(p
, data
);
54 if (!cli_send_trans(cli
, SMBtrans2
,
56 -1, 0, /* fid, flags */
57 &setup
, 1, 0, /* setup, length, max */
58 param
, param_len
, 2, /* param, length, max */
59 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
64 if (!cli_receive_trans(cli
, SMBtrans2
,
76 /****************************************************************************
77 Map standard UNIX permissions onto wire representations.
78 ****************************************************************************/
80 uint32
unix_perms_to_wire(mode_t perms
)
84 ret
|= ((perms
& S_IXOTH
) ? UNIX_X_OTH
: 0);
85 ret
|= ((perms
& S_IWOTH
) ? UNIX_W_OTH
: 0);
86 ret
|= ((perms
& S_IROTH
) ? UNIX_R_OTH
: 0);
87 ret
|= ((perms
& S_IXGRP
) ? UNIX_X_GRP
: 0);
88 ret
|= ((perms
& S_IWGRP
) ? UNIX_W_GRP
: 0);
89 ret
|= ((perms
& S_IRGRP
) ? UNIX_R_GRP
: 0);
90 ret
|= ((perms
& S_IXUSR
) ? UNIX_X_USR
: 0);
91 ret
|= ((perms
& S_IWUSR
) ? UNIX_W_USR
: 0);
92 ret
|= ((perms
& S_IRUSR
) ? UNIX_R_USR
: 0);
94 ret
|= ((perms
& S_ISVTX
) ? UNIX_STICKY
: 0);
97 ret
|= ((perms
& S_ISGID
) ? UNIX_SET_GID
: 0);
100 ret
|= ((perms
& S_ISUID
) ? UNIX_SET_UID
: 0);
105 /****************************************************************************
106 Symlink a file (UNIX extensions).
107 ****************************************************************************/
109 BOOL
cli_unix_symlink(struct cli_state
*cli
, const char *oldname
, const char *newname
)
111 return cli_link_internal(cli
, oldname
, newname
, False
);
114 /****************************************************************************
115 Hard a file (UNIX extensions).
116 ****************************************************************************/
118 BOOL
cli_unix_hardlink(struct cli_state
*cli
, const char *oldname
, const char *newname
)
120 return cli_link_internal(cli
, oldname
, newname
, True
);
123 /****************************************************************************
124 Chmod or chown a file internal (UNIX extensions).
125 ****************************************************************************/
127 static BOOL
cli_unix_chmod_chown_internal(struct cli_state
*cli
, const char *fname
, uint32 mode
, uint32 uid
, uint32 gid
)
129 unsigned int data_len
= 0;
130 unsigned int param_len
= 0;
131 uint16 setup
= TRANSACT2_SETPATHINFO
;
132 char param
[sizeof(pstring
)+6];
134 char *rparam
=NULL
, *rdata
=NULL
;
137 memset(param
, 0, sizeof(param
));
138 memset(data
, 0, sizeof(data
));
139 SSVAL(param
,0,SMB_SET_FILE_UNIX_BASIC
);
142 p
+= clistr_push(cli
, p
, fname
, -1, STR_TERMINATE
);
143 param_len
= PTR_DIFF(p
, param
);
151 if (!cli_send_trans(cli
, SMBtrans2
,
153 -1, 0, /* fid, flags */
154 &setup
, 1, 0, /* setup, length, max */
155 param
, param_len
, 2, /* param, length, max */
156 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
161 if (!cli_receive_trans(cli
, SMBtrans2
,
163 &rdata
, &data_len
)) {
173 /****************************************************************************
174 chmod a file (UNIX extensions).
175 ****************************************************************************/
177 BOOL
cli_unix_chmod(struct cli_state
*cli
, const char *fname
, mode_t mode
)
179 return cli_unix_chmod_chown_internal(cli
, fname
,
180 unix_perms_to_wire(mode
), SMB_UID_NO_CHANGE
, SMB_GID_NO_CHANGE
);
183 /****************************************************************************
184 chown a file (UNIX extensions).
185 ****************************************************************************/
187 BOOL
cli_unix_chown(struct cli_state
*cli
, const char *fname
, uid_t uid
, gid_t gid
)
189 return cli_unix_chmod_chown_internal(cli
, fname
, SMB_MODE_NO_CHANGE
, (uint32
)uid
, (uint32
)gid
);
192 /****************************************************************************
194 ****************************************************************************/
196 BOOL
cli_rename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
200 memset(cli
->outbuf
,'\0',smb_size
);
201 memset(cli
->inbuf
,'\0',smb_size
);
203 set_message(cli
->outbuf
,1, 0, True
);
205 SCVAL(cli
->outbuf
,smb_com
,SMBmv
);
206 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
207 cli_setup_packet(cli
);
209 SSVAL(cli
->outbuf
,smb_vwv0
,aSYSTEM
| aHIDDEN
| aDIR
);
211 p
= smb_buf(cli
->outbuf
);
213 p
+= clistr_push(cli
, p
, fname_src
, -1, STR_TERMINATE
);
215 p
+= clistr_push(cli
, p
, fname_dst
, -1, STR_TERMINATE
);
217 cli_setup_bcc(cli
, p
);
220 if (!cli_receive_smb(cli
))
223 if (cli_is_error(cli
))
229 /****************************************************************************
231 ****************************************************************************/
233 BOOL
cli_ntrename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
237 memset(cli
->outbuf
,'\0',smb_size
);
238 memset(cli
->inbuf
,'\0',smb_size
);
240 set_message(cli
->outbuf
, 4, 0, True
);
242 SCVAL(cli
->outbuf
,smb_com
,SMBntrename
);
243 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
244 cli_setup_packet(cli
);
246 SSVAL(cli
->outbuf
,smb_vwv0
,aSYSTEM
| aHIDDEN
| aDIR
);
247 SSVAL(cli
->outbuf
,smb_vwv1
, RENAME_FLAG_RENAME
);
249 p
= smb_buf(cli
->outbuf
);
251 p
+= clistr_push(cli
, p
, fname_src
, -1, STR_TERMINATE
);
253 p
+= clistr_push(cli
, p
, fname_dst
, -1, STR_TERMINATE
);
255 cli_setup_bcc(cli
, p
);
258 if (!cli_receive_smb(cli
))
261 if (cli_is_error(cli
))
267 /****************************************************************************
269 ****************************************************************************/
271 BOOL
cli_nt_hardlink(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
275 memset(cli
->outbuf
,'\0',smb_size
);
276 memset(cli
->inbuf
,'\0',smb_size
);
278 set_message(cli
->outbuf
, 4, 0, True
);
280 SCVAL(cli
->outbuf
,smb_com
,SMBntrename
);
281 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
282 cli_setup_packet(cli
);
284 SSVAL(cli
->outbuf
,smb_vwv0
,aSYSTEM
| aHIDDEN
| aDIR
);
285 SSVAL(cli
->outbuf
,smb_vwv1
, RENAME_FLAG_HARD_LINK
);
287 p
= smb_buf(cli
->outbuf
);
289 p
+= clistr_push(cli
, p
, fname_src
, -1, STR_TERMINATE
);
291 p
+= clistr_push(cli
, p
, fname_dst
, -1, STR_TERMINATE
);
293 cli_setup_bcc(cli
, p
);
296 if (!cli_receive_smb(cli
))
299 if (cli_is_error(cli
))
305 /****************************************************************************
307 ****************************************************************************/
309 BOOL
cli_unlink(struct cli_state
*cli
, const char *fname
)
313 memset(cli
->outbuf
,'\0',smb_size
);
314 memset(cli
->inbuf
,'\0',smb_size
);
316 set_message(cli
->outbuf
,1, 0,True
);
318 SCVAL(cli
->outbuf
,smb_com
,SMBunlink
);
319 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
320 cli_setup_packet(cli
);
322 SSVAL(cli
->outbuf
,smb_vwv0
,aSYSTEM
| aHIDDEN
);
324 p
= smb_buf(cli
->outbuf
);
326 p
+= clistr_push(cli
, p
, fname
, -1, STR_TERMINATE
);
328 cli_setup_bcc(cli
, p
);
330 if (!cli_receive_smb(cli
)) {
334 if (cli_is_error(cli
)) {
341 /****************************************************************************
343 ****************************************************************************/
345 BOOL
cli_mkdir(struct cli_state
*cli
, const char *dname
)
349 memset(cli
->outbuf
,'\0',smb_size
);
350 memset(cli
->inbuf
,'\0',smb_size
);
352 set_message(cli
->outbuf
,0, 0,True
);
354 SCVAL(cli
->outbuf
,smb_com
,SMBmkdir
);
355 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
356 cli_setup_packet(cli
);
358 p
= smb_buf(cli
->outbuf
);
360 p
+= clistr_push(cli
, p
, dname
, -1, STR_TERMINATE
);
362 cli_setup_bcc(cli
, p
);
365 if (!cli_receive_smb(cli
)) {
369 if (cli_is_error(cli
)) {
376 /****************************************************************************
378 ****************************************************************************/
380 BOOL
cli_rmdir(struct cli_state
*cli
, const char *dname
)
384 memset(cli
->outbuf
,'\0',smb_size
);
385 memset(cli
->inbuf
,'\0',smb_size
);
387 set_message(cli
->outbuf
,0, 0, True
);
389 SCVAL(cli
->outbuf
,smb_com
,SMBrmdir
);
390 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
391 cli_setup_packet(cli
);
393 p
= smb_buf(cli
->outbuf
);
395 p
+= clistr_push(cli
, p
, dname
, -1, STR_TERMINATE
);
397 cli_setup_bcc(cli
, p
);
400 if (!cli_receive_smb(cli
)) {
404 if (cli_is_error(cli
)) {
411 /****************************************************************************
412 Set or clear the delete on close flag.
413 ****************************************************************************/
415 int cli_nt_delete_on_close(struct cli_state
*cli
, int fnum
, BOOL flag
)
417 unsigned int data_len
= 1;
418 unsigned int param_len
= 6;
419 uint16 setup
= TRANSACT2_SETFILEINFO
;
422 char *rparam
=NULL
, *rdata
=NULL
;
424 memset(param
, 0, param_len
);
426 SSVAL(param
,2,SMB_SET_FILE_DISPOSITION_INFO
);
430 if (!cli_send_trans(cli
, SMBtrans2
,
432 -1, 0, /* fid, flags */
433 &setup
, 1, 0, /* setup, length, max */
434 param
, param_len
, 2, /* param, length, max */
435 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
440 if (!cli_receive_trans(cli
, SMBtrans2
,
442 &rdata
, &data_len
)) {
452 /****************************************************************************
453 Open a file - exposing the full horror of the NT API :-).
455 ****************************************************************************/
457 int cli_nt_create_full(struct cli_state
*cli
, const char *fname
,
458 uint32 CreatFlags
, uint32 DesiredAccess
,
459 uint32 FileAttributes
, uint32 ShareAccess
,
460 uint32 CreateDisposition
, uint32 CreateOptions
,
466 memset(cli
->outbuf
,'\0',smb_size
);
467 memset(cli
->inbuf
,'\0',smb_size
);
469 set_message(cli
->outbuf
,24,0,True
);
471 SCVAL(cli
->outbuf
,smb_com
,SMBntcreateX
);
472 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
473 cli_setup_packet(cli
);
475 SSVAL(cli
->outbuf
,smb_vwv0
,0xFF);
476 if (cli
->use_oplocks
)
477 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
479 SIVAL(cli
->outbuf
,smb_ntcreate_Flags
, CreatFlags
);
480 SIVAL(cli
->outbuf
,smb_ntcreate_RootDirectoryFid
, 0x0);
481 SIVAL(cli
->outbuf
,smb_ntcreate_DesiredAccess
, DesiredAccess
);
482 SIVAL(cli
->outbuf
,smb_ntcreate_FileAttributes
, FileAttributes
);
483 SIVAL(cli
->outbuf
,smb_ntcreate_ShareAccess
, ShareAccess
);
484 SIVAL(cli
->outbuf
,smb_ntcreate_CreateDisposition
, CreateDisposition
);
485 SIVAL(cli
->outbuf
,smb_ntcreate_CreateOptions
, CreateOptions
);
486 SIVAL(cli
->outbuf
,smb_ntcreate_ImpersonationLevel
, 0x02);
487 SCVAL(cli
->outbuf
,smb_ntcreate_SecurityFlags
, SecuityFlags
);
489 p
= smb_buf(cli
->outbuf
);
490 /* this alignment and termination is critical for netapp filers. Don't change */
491 p
+= clistr_align_out(cli
, p
, 0);
492 len
= clistr_push(cli
, p
, fname
, -1, 0);
494 SSVAL(cli
->outbuf
,smb_ntcreate_NameLength
, len
);
495 /* sigh. this copes with broken netapp filer behaviour */
496 p
+= clistr_push(cli
, p
, "", -1, STR_TERMINATE
);
498 cli_setup_bcc(cli
, p
);
501 if (!cli_receive_smb(cli
)) {
505 if (cli_is_error(cli
)) {
509 return SVAL(cli
->inbuf
,smb_vwv2
+ 1);
512 /****************************************************************************
514 ****************************************************************************/
516 int cli_nt_create(struct cli_state
*cli
, const char *fname
, uint32 DesiredAccess
)
518 return cli_nt_create_full(cli
, fname
, 0, DesiredAccess
, 0,
519 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_EXISTS_OPEN
, 0x0, 0x0);
522 /****************************************************************************
524 WARNING: if you open with O_WRONLY then getattrE won't work!
525 ****************************************************************************/
527 int cli_open(struct cli_state
*cli
, const char *fname
, int flags
, int share_mode
)
531 unsigned accessmode
=0;
535 if (!(flags
& O_EXCL
)) {
542 accessmode
= (share_mode
<<4);
544 if ((flags
& O_ACCMODE
) == O_RDWR
) {
546 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
551 if ((flags
& O_SYNC
) == O_SYNC
) {
552 accessmode
|= (1<<14);
556 if (share_mode
== DENY_FCB
) {
560 memset(cli
->outbuf
,'\0',smb_size
);
561 memset(cli
->inbuf
,'\0',smb_size
);
563 set_message(cli
->outbuf
,15,0,True
);
565 SCVAL(cli
->outbuf
,smb_com
,SMBopenX
);
566 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
567 cli_setup_packet(cli
);
569 SSVAL(cli
->outbuf
,smb_vwv0
,0xFF);
570 SSVAL(cli
->outbuf
,smb_vwv2
,0); /* no additional info */
571 SSVAL(cli
->outbuf
,smb_vwv3
,accessmode
);
572 SSVAL(cli
->outbuf
,smb_vwv4
,aSYSTEM
| aHIDDEN
);
573 SSVAL(cli
->outbuf
,smb_vwv5
,0);
574 SSVAL(cli
->outbuf
,smb_vwv8
,openfn
);
576 if (cli
->use_oplocks
) {
577 /* if using oplocks then ask for a batch oplock via
578 core and extended methods */
579 SCVAL(cli
->outbuf
,smb_flg
, CVAL(cli
->outbuf
,smb_flg
)|
580 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
);
581 SSVAL(cli
->outbuf
,smb_vwv2
,SVAL(cli
->outbuf
,smb_vwv2
) | 6);
584 p
= smb_buf(cli
->outbuf
);
585 p
+= clistr_push(cli
, p
, fname
, -1, STR_TERMINATE
);
587 cli_setup_bcc(cli
, p
);
590 if (!cli_receive_smb(cli
)) {
594 if (cli_is_error(cli
)) {
598 return SVAL(cli
->inbuf
,smb_vwv2
);
601 /****************************************************************************
603 ****************************************************************************/
605 BOOL
cli_close(struct cli_state
*cli
, int fnum
)
607 memset(cli
->outbuf
,'\0',smb_size
);
608 memset(cli
->inbuf
,'\0',smb_size
);
610 set_message(cli
->outbuf
,3,0,True
);
612 SCVAL(cli
->outbuf
,smb_com
,SMBclose
);
613 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
614 cli_setup_packet(cli
);
616 SSVAL(cli
->outbuf
,smb_vwv0
,fnum
);
617 SIVALS(cli
->outbuf
,smb_vwv1
,-1);
620 if (!cli_receive_smb(cli
)) {
624 return !cli_is_error(cli
);
628 /****************************************************************************
629 send a lock with a specified locktype
630 this is used for testing LOCKING_ANDX_CANCEL_LOCK
631 ****************************************************************************/
632 NTSTATUS
cli_locktype(struct cli_state
*cli
, int fnum
,
633 uint32 offset
, uint32 len
, int timeout
, unsigned char locktype
)
636 int saved_timeout
= cli
->timeout
;
638 memset(cli
->outbuf
,'\0',smb_size
);
639 memset(cli
->inbuf
,'\0', smb_size
);
641 set_message(cli
->outbuf
,8,0,True
);
643 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
644 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
645 cli_setup_packet(cli
);
647 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
648 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
649 SCVAL(cli
->outbuf
,smb_vwv3
,locktype
);
650 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
651 SSVAL(cli
->outbuf
,smb_vwv6
,0);
652 SSVAL(cli
->outbuf
,smb_vwv7
,1);
654 p
= smb_buf(cli
->outbuf
);
655 SSVAL(p
, 0, cli
->pid
);
661 cli_setup_bcc(cli
, p
);
666 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
+ 2*1000);
669 if (!cli_receive_smb(cli
)) {
670 cli
->timeout
= saved_timeout
;
671 return NT_STATUS_UNSUCCESSFUL
;
674 cli
->timeout
= saved_timeout
;
676 return cli_nt_error(cli
);
680 /****************************************************************************
682 note that timeout is in units of 2 milliseconds
683 ****************************************************************************/
684 BOOL
cli_lock(struct cli_state
*cli
, int fnum
,
685 uint32 offset
, uint32 len
, int timeout
, enum brl_type lock_type
)
688 int saved_timeout
= cli
->timeout
;
690 memset(cli
->outbuf
,'\0',smb_size
);
691 memset(cli
->inbuf
,'\0', smb_size
);
693 set_message(cli
->outbuf
,8,0,True
);
695 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
696 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
697 cli_setup_packet(cli
);
699 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
700 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
701 SCVAL(cli
->outbuf
,smb_vwv3
,(lock_type
== READ_LOCK
? 1 : 0));
702 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
703 SSVAL(cli
->outbuf
,smb_vwv6
,0);
704 SSVAL(cli
->outbuf
,smb_vwv7
,1);
706 p
= smb_buf(cli
->outbuf
);
707 SSVAL(p
, 0, cli
->pid
);
713 cli_setup_bcc(cli
, p
);
718 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
*2 + 5*1000);
721 if (!cli_receive_smb(cli
)) {
722 cli
->timeout
= saved_timeout
;
726 cli
->timeout
= saved_timeout
;
728 if (cli_is_error(cli
)) {
735 /****************************************************************************
737 ****************************************************************************/
739 BOOL
cli_unlock(struct cli_state
*cli
, int fnum
, uint32 offset
, uint32 len
)
743 memset(cli
->outbuf
,'\0',smb_size
);
744 memset(cli
->inbuf
,'\0',smb_size
);
746 set_message(cli
->outbuf
,8,0,True
);
748 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
749 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
750 cli_setup_packet(cli
);
752 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
753 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
754 SCVAL(cli
->outbuf
,smb_vwv3
,0);
755 SIVALS(cli
->outbuf
, smb_vwv4
, 0);
756 SSVAL(cli
->outbuf
,smb_vwv6
,1);
757 SSVAL(cli
->outbuf
,smb_vwv7
,0);
759 p
= smb_buf(cli
->outbuf
);
760 SSVAL(p
, 0, cli
->pid
);
764 cli_setup_bcc(cli
, p
);
766 if (!cli_receive_smb(cli
)) {
770 if (cli_is_error(cli
)) {
777 /****************************************************************************
778 Lock a file with 64 bit offsets.
779 ****************************************************************************/
781 BOOL
cli_lock64(struct cli_state
*cli
, int fnum
,
782 SMB_BIG_UINT offset
, SMB_BIG_UINT len
, int timeout
, enum brl_type lock_type
)
785 int saved_timeout
= cli
->timeout
;
788 if (! (cli
->capabilities
& CAP_LARGE_FILES
)) {
789 return cli_lock(cli
, fnum
, offset
, len
, timeout
, lock_type
);
792 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
793 ltype
|= LOCKING_ANDX_LARGE_FILES
;
795 memset(cli
->outbuf
,'\0',smb_size
);
796 memset(cli
->inbuf
,'\0', smb_size
);
798 set_message(cli
->outbuf
,8,0,True
);
800 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
801 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
802 cli_setup_packet(cli
);
804 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
805 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
806 SCVAL(cli
->outbuf
,smb_vwv3
,ltype
);
807 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
808 SSVAL(cli
->outbuf
,smb_vwv6
,0);
809 SSVAL(cli
->outbuf
,smb_vwv7
,1);
811 p
= smb_buf(cli
->outbuf
);
812 SIVAL(p
, 0, cli
->pid
);
813 SOFF_T_R(p
, 4, offset
);
814 SOFF_T_R(p
, 12, len
);
817 cli_setup_bcc(cli
, p
);
821 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
+ 5*1000);
824 if (!cli_receive_smb(cli
)) {
825 cli
->timeout
= saved_timeout
;
829 cli
->timeout
= saved_timeout
;
831 if (cli_is_error(cli
)) {
838 /****************************************************************************
839 Unlock a file with 64 bit offsets.
840 ****************************************************************************/
842 BOOL
cli_unlock64(struct cli_state
*cli
, int fnum
, SMB_BIG_UINT offset
, SMB_BIG_UINT len
)
846 if (! (cli
->capabilities
& CAP_LARGE_FILES
)) {
847 return cli_unlock(cli
, fnum
, offset
, len
);
850 memset(cli
->outbuf
,'\0',smb_size
);
851 memset(cli
->inbuf
,'\0',smb_size
);
853 set_message(cli
->outbuf
,8,0,True
);
855 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
856 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
857 cli_setup_packet(cli
);
859 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
860 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
861 SCVAL(cli
->outbuf
,smb_vwv3
,LOCKING_ANDX_LARGE_FILES
);
862 SIVALS(cli
->outbuf
, smb_vwv4
, 0);
863 SSVAL(cli
->outbuf
,smb_vwv6
,1);
864 SSVAL(cli
->outbuf
,smb_vwv7
,0);
866 p
= smb_buf(cli
->outbuf
);
867 SIVAL(p
, 0, cli
->pid
);
868 SOFF_T_R(p
, 4, offset
);
869 SOFF_T_R(p
, 12, len
);
871 cli_setup_bcc(cli
, p
);
873 if (!cli_receive_smb(cli
)) {
877 if (cli_is_error(cli
)) {
885 /****************************************************************************
886 Do a SMBgetattrE call.
887 ****************************************************************************/
889 BOOL
cli_getattrE(struct cli_state
*cli
, int fd
,
890 uint16
*attr
, SMB_BIG_UINT
*size
,
891 time_t *c_time
, time_t *a_time
, time_t *m_time
)
893 memset(cli
->outbuf
,'\0',smb_size
);
894 memset(cli
->inbuf
,'\0',smb_size
);
896 set_message(cli
->outbuf
,1,0,True
);
898 SCVAL(cli
->outbuf
,smb_com
,SMBgetattrE
);
899 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
900 cli_setup_packet(cli
);
902 SSVAL(cli
->outbuf
,smb_vwv0
,fd
);
905 if (!cli_receive_smb(cli
)) {
909 if (cli_is_error(cli
)) {
914 *size
= IVAL(cli
->inbuf
, smb_vwv6
);
918 *attr
= SVAL(cli
->inbuf
,smb_vwv10
);
922 *c_time
= make_unix_date3(cli
->inbuf
+smb_vwv0
);
926 *a_time
= make_unix_date3(cli
->inbuf
+smb_vwv2
);
930 *m_time
= make_unix_date3(cli
->inbuf
+smb_vwv4
);
936 /****************************************************************************
938 ****************************************************************************/
940 BOOL
cli_getatr(struct cli_state
*cli
, const char *fname
,
941 uint16
*attr
, size_t *size
, time_t *t
)
945 memset(cli
->outbuf
,'\0',smb_size
);
946 memset(cli
->inbuf
,'\0',smb_size
);
948 set_message(cli
->outbuf
,0,0,True
);
950 SCVAL(cli
->outbuf
,smb_com
,SMBgetatr
);
951 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
952 cli_setup_packet(cli
);
954 p
= smb_buf(cli
->outbuf
);
956 p
+= clistr_push(cli
, p
, fname
, -1, STR_TERMINATE
);
958 cli_setup_bcc(cli
, p
);
961 if (!cli_receive_smb(cli
)) {
965 if (cli_is_error(cli
)) {
970 *size
= IVAL(cli
->inbuf
, smb_vwv3
);
974 *t
= make_unix_date3(cli
->inbuf
+smb_vwv1
);
978 *attr
= SVAL(cli
->inbuf
,smb_vwv0
);
985 /****************************************************************************
987 ****************************************************************************/
989 BOOL
cli_setatr(struct cli_state
*cli
, const char *fname
, uint16 attr
, time_t t
)
993 memset(cli
->outbuf
,'\0',smb_size
);
994 memset(cli
->inbuf
,'\0',smb_size
);
996 set_message(cli
->outbuf
,8,0,True
);
998 SCVAL(cli
->outbuf
,smb_com
,SMBsetatr
);
999 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1000 cli_setup_packet(cli
);
1002 SSVAL(cli
->outbuf
,smb_vwv0
, attr
);
1003 put_dos_date3(cli
->outbuf
,smb_vwv1
, t
);
1005 p
= smb_buf(cli
->outbuf
);
1007 p
+= clistr_push(cli
, p
, fname
, -1, STR_TERMINATE
);
1010 cli_setup_bcc(cli
, p
);
1013 if (!cli_receive_smb(cli
)) {
1017 if (cli_is_error(cli
)) {
1024 /****************************************************************************
1025 Check for existance of a dir.
1026 ****************************************************************************/
1027 BOOL
cli_chkpath(struct cli_state
*cli
, const char *path
)
1032 pstrcpy(path2
,path
);
1033 trim_char(path2
,'\0','\\');
1037 memset(cli
->outbuf
,'\0',smb_size
);
1038 set_message(cli
->outbuf
,0,0,True
);
1039 SCVAL(cli
->outbuf
,smb_com
,SMBchkpth
);
1040 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1041 cli_setup_packet(cli
);
1042 p
= smb_buf(cli
->outbuf
);
1044 p
+= clistr_push(cli
, p
, path2
, -1, STR_TERMINATE
);
1046 cli_setup_bcc(cli
, p
);
1049 if (!cli_receive_smb(cli
)) {
1053 if (cli_is_error(cli
)) return False
;
1058 /****************************************************************************
1060 ****************************************************************************/
1062 BOOL
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
1064 memset(cli
->outbuf
,'\0',smb_size
);
1065 set_message(cli
->outbuf
,0,0,True
);
1066 SCVAL(cli
->outbuf
,smb_com
,SMBdskattr
);
1067 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1068 cli_setup_packet(cli
);
1071 if (!cli_receive_smb(cli
)) {
1075 *bsize
= SVAL(cli
->inbuf
,smb_vwv1
)*SVAL(cli
->inbuf
,smb_vwv2
);
1076 *total
= SVAL(cli
->inbuf
,smb_vwv0
);
1077 *avail
= SVAL(cli
->inbuf
,smb_vwv3
);
1082 /****************************************************************************
1083 Create and open a temporary file.
1084 ****************************************************************************/
1086 int cli_ctemp(struct cli_state
*cli
, const char *path
, char **tmp_path
)
1091 memset(cli
->outbuf
,'\0',smb_size
);
1092 memset(cli
->inbuf
,'\0',smb_size
);
1094 set_message(cli
->outbuf
,3,0,True
);
1096 SCVAL(cli
->outbuf
,smb_com
,SMBctemp
);
1097 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1098 cli_setup_packet(cli
);
1100 SSVAL(cli
->outbuf
,smb_vwv0
,0);
1101 SIVALS(cli
->outbuf
,smb_vwv1
,-1);
1103 p
= smb_buf(cli
->outbuf
);
1105 p
+= clistr_push(cli
, p
, path
, -1, STR_TERMINATE
);
1107 cli_setup_bcc(cli
, p
);
1110 if (!cli_receive_smb(cli
)) {
1114 if (cli_is_error(cli
)) {
1118 /* despite the spec, the result has a -1, followed by
1119 length, followed by name */
1120 p
= smb_buf(cli
->inbuf
);
1122 len
= smb_buflen(cli
->inbuf
) - 4;
1123 if (len
<= 0) return -1;
1127 clistr_pull(cli
, path2
, p
,
1128 sizeof(path2
), len
, STR_ASCII
);
1129 *tmp_path
= strdup(path2
);
1132 return SVAL(cli
->inbuf
,smb_vwv0
);
1137 send a raw ioctl - used by the torture code
1139 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, int fnum
, uint32 code
, DATA_BLOB
*blob
)
1141 memset(cli
->outbuf
,'\0',smb_size
);
1142 memset(cli
->inbuf
,'\0',smb_size
);
1144 set_message(cli
->outbuf
, 3, 0, True
);
1145 SCVAL(cli
->outbuf
,smb_com
,SMBioctl
);
1146 cli_setup_packet(cli
);
1148 SSVAL(cli
->outbuf
, smb_vwv0
, fnum
);
1149 SSVAL(cli
->outbuf
, smb_vwv1
, code
>>16);
1150 SSVAL(cli
->outbuf
, smb_vwv2
, (code
&0xFFFF));
1153 if (!cli_receive_smb(cli
)) {
1154 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
1157 if (cli_is_error(cli
)) {
1158 return cli_nt_error(cli
);
1161 *blob
= data_blob(NULL
, 0);
1163 return NT_STATUS_OK
;
1166 /*********************************************************
1167 Set an extended attribute utility fn.
1168 *********************************************************/
1170 static BOOL
cli_set_ea(struct cli_state
*cli
, uint16 setup
, char *param
, unsigned int param_len
,
1171 const char *ea_name
, const char *ea_val
, size_t ea_len
)
1173 unsigned int data_len
= 0;
1175 char *rparam
=NULL
, *rdata
=NULL
;
1177 size_t ea_namelen
= strlen(ea_name
);
1179 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
1180 data
= malloc(data_len
);
1185 SIVAL(p
,0,data_len
);
1187 SCVAL(p
, 0, 0); /* EA flags. */
1188 SCVAL(p
, 1, ea_namelen
);
1189 SSVAL(p
, 2, ea_len
);
1190 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
1191 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
1193 if (!cli_send_trans(cli
, SMBtrans2
,
1195 -1, 0, /* fid, flags */
1196 &setup
, 1, 0, /* setup, length, max */
1197 param
, param_len
, 2, /* param, length, max */
1198 data
, data_len
, cli
->max_xmit
/* data, length, max */
1203 if (!cli_receive_trans(cli
, SMBtrans2
,
1204 &rparam
, ¶m_len
,
1205 &rdata
, &data_len
)) {
1216 /*********************************************************
1217 Set an extended attribute on a pathname.
1218 *********************************************************/
1220 BOOL
cli_set_ea_path(struct cli_state
*cli
, const char *path
, const char *ea_name
, const char *ea_val
, size_t ea_len
)
1222 uint16 setup
= TRANSACT2_SETPATHINFO
;
1223 unsigned int param_len
= 0;
1224 char param
[sizeof(pstring
)+6];
1225 size_t srclen
= 2*(strlen(path
)+1);
1228 memset(param
, 0, sizeof(param
));
1229 SSVAL(param
,0,SMB_INFO_SET_EA
);
1232 p
+= clistr_push(cli
, p
, path
, MIN(srclen
, sizeof(param
)-6), STR_TERMINATE
);
1233 param_len
= PTR_DIFF(p
, param
);
1235 return cli_set_ea(cli
, setup
, param
, param_len
, ea_name
, ea_val
, ea_len
);
1238 /*********************************************************
1239 Set an extended attribute on an fnum.
1240 *********************************************************/
1242 BOOL
cli_set_ea_fnum(struct cli_state
*cli
, int fnum
, const char *ea_name
, const char *ea_val
, size_t ea_len
)
1245 uint16 setup
= TRANSACT2_SETFILEINFO
;
1247 memset(param
, 0, 6);
1248 SSVAL(param
,0,fnum
);
1249 SSVAL(param
,2,SMB_INFO_SET_EA
);
1251 return cli_set_ea(cli
, setup
, param
, 6, ea_name
, ea_val
, ea_len
);
1254 /*********************************************************
1255 Get an extended attribute list tility fn.
1256 *********************************************************/
1258 static BOOL
cli_get_ea_list(struct cli_state
*cli
,
1259 uint16 setup
, char *param
, unsigned int param_len
,
1262 struct ea_struct
**pea_list
)
1264 unsigned int data_len
= 0;
1265 unsigned int rparam_len
, rdata_len
;
1266 char *rparam
=NULL
, *rdata
=NULL
;
1271 struct ea_struct
*ea_list
;
1276 if (!cli_send_trans(cli
, SMBtrans2
,
1278 -1, 0, /* fid, flags */
1279 &setup
, 1, 0, /* setup, length, max */
1280 param
, param_len
, 10, /* param, length, max */
1281 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
1286 if (!cli_receive_trans(cli
, SMBtrans2
,
1287 &rparam
, &rparam_len
,
1288 &rdata
, &rdata_len
)) {
1292 if (!rdata
|| rdata_len
< 4) {
1296 ea_size
= (size_t)IVAL(rdata
,0);
1297 if (ea_size
> rdata_len
) {
1302 /* No EA's present. */
1310 /* Validate the EA list and count it. */
1311 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
1312 unsigned int ea_namelen
= CVAL(p
,1);
1313 unsigned int ea_valuelen
= SVAL(p
,2);
1314 if (ea_namelen
== 0) {
1317 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
1320 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
1321 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
1329 *pnum_eas
= num_eas
;
1331 /* Caller only wants number of EA's. */
1336 ea_list
= (struct ea_struct
*)talloc(ctx
, num_eas
*sizeof(struct ea_struct
));
1341 ea_size
= (size_t)IVAL(rdata
,0);
1344 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
1345 struct ea_struct
*ea
= &ea_list
[num_eas
];
1346 fstring unix_ea_name
;
1347 unsigned int ea_namelen
= CVAL(p
,1);
1348 unsigned int ea_valuelen
= SVAL(p
,2);
1350 ea
->flags
= CVAL(p
,0);
1351 unix_ea_name
[0] = '\0';
1352 pull_ascii_fstring(unix_ea_name
, p
+ 4);
1353 ea
->name
= talloc_strdup(ctx
, unix_ea_name
);
1354 /* Ensure the value is null terminated (in case it's a string). */
1355 ea
->value
= data_blob_talloc(ctx
, NULL
, ea_valuelen
+ 1);
1356 if (!ea
->value
.data
) {
1360 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
1362 ea
->value
.data
[ea_valuelen
] = 0;
1364 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
1367 *pea_list
= ea_list
;
1377 /*********************************************************
1378 Get an extended attribute list from a pathname.
1379 *********************************************************/
1381 BOOL
cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
1384 struct ea_struct
**pea_list
)
1386 uint16 setup
= TRANSACT2_QPATHINFO
;
1387 unsigned int param_len
= 0;
1388 char param
[sizeof(pstring
)+6];
1393 SSVAL(p
, 0, SMB_INFO_QUERY_ALL_EAS
);
1395 p
+= clistr_push(cli
, p
, path
, sizeof(pstring
)-6, STR_TERMINATE
);
1396 param_len
= PTR_DIFF(p
, param
);
1398 return cli_get_ea_list(cli
, setup
, param
, param_len
, ctx
, pnum_eas
, pea_list
);
1401 /*********************************************************
1402 Get an extended attribute list from an fnum.
1403 *********************************************************/
1405 BOOL
cli_get_ea_list_fnum(struct cli_state
*cli
, int fnum
,
1408 struct ea_struct
**pea_list
)
1410 uint16 setup
= TRANSACT2_QFILEINFO
;
1413 memset(param
, 0, 6);
1414 SSVAL(param
,0,fnum
);
1415 SSVAL(param
,2,SMB_INFO_SET_EA
);
1417 return cli_get_ea_list(cli
, setup
, param
, 6, ctx
, pnum_eas
, pea_list
);