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.
24 /****************************************************************************
25 Hard/Symlink a file (UNIX extensions).
26 Creates new name (sym)linked to oldname.
27 ****************************************************************************/
29 static BOOL
cli_link_internal(struct cli_state
*cli
, const char *oldname
, const char *newname
, BOOL hard_link
)
31 unsigned int data_len
= 0;
32 unsigned int param_len
= 0;
33 uint16 setup
= TRANSACT2_SETPATHINFO
;
34 char param
[sizeof(pstring
)+6];
36 char *rparam
=NULL
, *rdata
=NULL
;
38 size_t oldlen
= 2*(strlen(oldname
)+1);
39 size_t newlen
= 2*(strlen(newname
)+1);
41 memset(param
, 0, sizeof(param
));
42 SSVAL(param
,0,hard_link
? SMB_SET_FILE_UNIX_HLINK
: SMB_SET_FILE_UNIX_LINK
);
45 p
+= clistr_push(cli
, p
, newname
, MIN(newlen
, sizeof(param
)-6), STR_TERMINATE
);
46 param_len
= PTR_DIFF(p
, param
);
49 p
+= clistr_push(cli
, p
, oldname
, MIN(oldlen
,sizeof(data
)), STR_TERMINATE
);
50 data_len
= PTR_DIFF(p
, data
);
52 if (!cli_send_trans(cli
, SMBtrans2
,
54 -1, 0, /* fid, flags */
55 &setup
, 1, 0, /* setup, length, max */
56 param
, param_len
, 2, /* param, length, max */
57 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
62 if (!cli_receive_trans(cli
, SMBtrans2
,
74 /****************************************************************************
75 Map standard UNIX permissions onto wire representations.
76 ****************************************************************************/
78 uint32
unix_perms_to_wire(mode_t perms
)
82 ret
|= ((perms
& S_IXOTH
) ? UNIX_X_OTH
: 0);
83 ret
|= ((perms
& S_IWOTH
) ? UNIX_W_OTH
: 0);
84 ret
|= ((perms
& S_IROTH
) ? UNIX_R_OTH
: 0);
85 ret
|= ((perms
& S_IXGRP
) ? UNIX_X_GRP
: 0);
86 ret
|= ((perms
& S_IWGRP
) ? UNIX_W_GRP
: 0);
87 ret
|= ((perms
& S_IRGRP
) ? UNIX_R_GRP
: 0);
88 ret
|= ((perms
& S_IXUSR
) ? UNIX_X_USR
: 0);
89 ret
|= ((perms
& S_IWUSR
) ? UNIX_W_USR
: 0);
90 ret
|= ((perms
& S_IRUSR
) ? UNIX_R_USR
: 0);
92 ret
|= ((perms
& S_ISVTX
) ? UNIX_STICKY
: 0);
95 ret
|= ((perms
& S_ISGID
) ? UNIX_SET_GID
: 0);
98 ret
|= ((perms
& S_ISUID
) ? UNIX_SET_UID
: 0);
103 /****************************************************************************
104 Map wire permissions to standard UNIX.
105 ****************************************************************************/
107 mode_t
wire_perms_to_unix(uint32 perms
)
109 mode_t ret
= (mode_t
)0;
111 ret
|= ((perms
& UNIX_X_OTH
) ? S_IXOTH
: 0);
112 ret
|= ((perms
& UNIX_W_OTH
) ? S_IWOTH
: 0);
113 ret
|= ((perms
& UNIX_R_OTH
) ? S_IROTH
: 0);
114 ret
|= ((perms
& UNIX_X_GRP
) ? S_IXGRP
: 0);
115 ret
|= ((perms
& UNIX_W_GRP
) ? S_IWGRP
: 0);
116 ret
|= ((perms
& UNIX_R_GRP
) ? S_IRGRP
: 0);
117 ret
|= ((perms
& UNIX_X_USR
) ? S_IXUSR
: 0);
118 ret
|= ((perms
& UNIX_W_USR
) ? S_IWUSR
: 0);
119 ret
|= ((perms
& UNIX_R_USR
) ? S_IRUSR
: 0);
121 ret
|= ((perms
& UNIX_STICKY
) ? S_ISVTX
: 0);
124 ret
|= ((perms
& UNIX_SET_GID
) ? S_ISGID
: 0);
127 ret
|= ((perms
& UNIX_SET_UID
) ? S_ISUID
: 0);
132 /****************************************************************************
133 Return the file type from the wire filetype for UNIX extensions.
134 ****************************************************************************/
136 static mode_t
unix_filetype_from_wire(uint32 wire_type
)
144 case UNIX_TYPE_SYMLINK
:
148 case UNIX_TYPE_CHARDEV
:
152 case UNIX_TYPE_BLKDEV
:
160 case UNIX_TYPE_SOCKET
:
168 /****************************************************************************
169 Do a POSIX getfacl (UNIX extensions).
170 ****************************************************************************/
172 BOOL
cli_unix_getfacl(struct cli_state
*cli
, const char *name
, size_t *prb_size
, char **retbuf
)
174 unsigned int param_len
= 0;
175 unsigned int data_len
= 0;
176 uint16 setup
= TRANSACT2_QPATHINFO
;
177 char param
[sizeof(pstring
)+6];
178 char *rparam
=NULL
, *rdata
=NULL
;
183 SSVAL(p
, 0, SMB_QUERY_POSIX_ACL
);
185 p
+= clistr_push(cli
, p
, name
, sizeof(pstring
)-6, STR_TERMINATE
);
186 param_len
= PTR_DIFF(p
, param
);
188 if (!cli_send_trans(cli
, SMBtrans2
,
190 -1, 0, /* fid, flags */
191 &setup
, 1, 0, /* setup, length, max */
192 param
, param_len
, 2, /* param, length, max */
193 NULL
, 0, cli
->max_xmit
/* data, length, max */
198 if (!cli_receive_trans(cli
, SMBtrans2
,
200 &rdata
, &data_len
)) {
212 *prb_size
= (size_t)data_len
;
217 /****************************************************************************
218 Stat a file (UNIX extensions).
219 ****************************************************************************/
221 BOOL
cli_unix_stat(struct cli_state
*cli
, const char *name
, SMB_STRUCT_STAT
*sbuf
)
223 unsigned int param_len
= 0;
224 unsigned int data_len
= 0;
225 uint16 setup
= TRANSACT2_QPATHINFO
;
226 char param
[sizeof(pstring
)+6];
227 char *rparam
=NULL
, *rdata
=NULL
;
234 SSVAL(p
, 0, SMB_QUERY_FILE_UNIX_BASIC
);
236 p
+= clistr_push(cli
, p
, name
, sizeof(pstring
)-6, STR_TERMINATE
);
237 param_len
= PTR_DIFF(p
, param
);
239 if (!cli_send_trans(cli
, SMBtrans2
,
241 -1, 0, /* fid, flags */
242 &setup
, 1, 0, /* setup, length, max */
243 param
, param_len
, 2, /* param, length, max */
244 NULL
, 0, cli
->max_xmit
/* data, length, max */
249 if (!cli_receive_trans(cli
, SMBtrans2
,
251 &rdata
, &data_len
)) {
261 sbuf
->st_size
= IVAL2_TO_SMB_BIG_UINT(rdata
,0); /* total size, in bytes */
262 sbuf
->st_blocks
= IVAL2_TO_SMB_BIG_UINT(rdata
,8); /* number of blocks allocated */
263 #if defined (HAVE_STAT_ST_BLOCKS) && defined(STAT_ST_BLOCKSIZE)
264 sbuf
->st_blocks
/= STAT_ST_BLOCKSIZE
;
266 /* assume 512 byte blocks */
267 sbuf
->st_blocks
/= 512;
269 sbuf
->st_ctime
= interpret_long_date(rdata
+ 16); /* time of last change */
270 sbuf
->st_atime
= interpret_long_date(rdata
+ 24); /* time of last access */
271 sbuf
->st_mtime
= interpret_long_date(rdata
+ 32); /* time of last modification */
272 sbuf
->st_uid
= (uid_t
) IVAL(rdata
,40); /* user ID of owner */
273 sbuf
->st_gid
= (gid_t
) IVAL(rdata
,48); /* group ID of owner */
274 sbuf
->st_mode
|= unix_filetype_from_wire(IVAL(rdata
, 56));
275 #if defined(HAVE_MAKEDEV)
277 uint32 dev_major
= IVAL(rdata
,60);
278 uint32 dev_minor
= IVAL(rdata
,68);
279 sbuf
->st_rdev
= makedev(dev_major
, dev_minor
);
282 sbuf
->st_ino
= (SMB_INO_T
)IVAL2_TO_SMB_BIG_UINT(rdata
,76); /* inode */
283 sbuf
->st_mode
|= wire_perms_to_unix(IVAL(rdata
,84)); /* protection */
284 sbuf
->st_nlink
= IVAL(rdata
,92); /* number of hard links */
292 /****************************************************************************
293 Symlink a file (UNIX extensions).
294 ****************************************************************************/
296 BOOL
cli_unix_symlink(struct cli_state
*cli
, const char *oldname
, const char *newname
)
298 return cli_link_internal(cli
, oldname
, newname
, False
);
301 /****************************************************************************
302 Hard a file (UNIX extensions).
303 ****************************************************************************/
305 BOOL
cli_unix_hardlink(struct cli_state
*cli
, const char *oldname
, const char *newname
)
307 return cli_link_internal(cli
, oldname
, newname
, True
);
310 /****************************************************************************
311 Chmod or chown a file internal (UNIX extensions).
312 ****************************************************************************/
314 static BOOL
cli_unix_chmod_chown_internal(struct cli_state
*cli
, const char *fname
, uint32 mode
, uint32 uid
, uint32 gid
)
316 unsigned int data_len
= 0;
317 unsigned int param_len
= 0;
318 uint16 setup
= TRANSACT2_SETPATHINFO
;
319 char param
[sizeof(pstring
)+6];
321 char *rparam
=NULL
, *rdata
=NULL
;
324 memset(param
, 0, sizeof(param
));
325 memset(data
, 0, sizeof(data
));
326 SSVAL(param
,0,SMB_SET_FILE_UNIX_BASIC
);
329 p
+= clistr_push(cli
, p
, fname
, -1, STR_TERMINATE
);
330 param_len
= PTR_DIFF(p
, param
);
338 if (!cli_send_trans(cli
, SMBtrans2
,
340 -1, 0, /* fid, flags */
341 &setup
, 1, 0, /* setup, length, max */
342 param
, param_len
, 2, /* param, length, max */
343 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
348 if (!cli_receive_trans(cli
, SMBtrans2
,
350 &rdata
, &data_len
)) {
360 /****************************************************************************
361 chmod a file (UNIX extensions).
362 ****************************************************************************/
364 BOOL
cli_unix_chmod(struct cli_state
*cli
, const char *fname
, mode_t mode
)
366 return cli_unix_chmod_chown_internal(cli
, fname
,
367 unix_perms_to_wire(mode
), SMB_UID_NO_CHANGE
, SMB_GID_NO_CHANGE
);
370 /****************************************************************************
371 chown a file (UNIX extensions).
372 ****************************************************************************/
374 BOOL
cli_unix_chown(struct cli_state
*cli
, const char *fname
, uid_t uid
, gid_t gid
)
376 return cli_unix_chmod_chown_internal(cli
, fname
, SMB_MODE_NO_CHANGE
, (uint32
)uid
, (uint32
)gid
);
379 /****************************************************************************
381 ****************************************************************************/
383 BOOL
cli_rename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
387 memset(cli
->outbuf
,'\0',smb_size
);
388 memset(cli
->inbuf
,'\0',smb_size
);
390 set_message(cli
->outbuf
,1, 0, True
);
392 SCVAL(cli
->outbuf
,smb_com
,SMBmv
);
393 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
394 cli_setup_packet(cli
);
396 SSVAL(cli
->outbuf
,smb_vwv0
,aSYSTEM
| aHIDDEN
| aDIR
);
398 p
= smb_buf(cli
->outbuf
);
400 p
+= clistr_push(cli
, p
, fname_src
, -1, STR_TERMINATE
);
402 p
+= clistr_push(cli
, p
, fname_dst
, -1, STR_TERMINATE
);
404 cli_setup_bcc(cli
, p
);
407 if (!cli_receive_smb(cli
))
410 if (cli_is_error(cli
))
416 /****************************************************************************
418 ****************************************************************************/
420 BOOL
cli_ntrename(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
424 memset(cli
->outbuf
,'\0',smb_size
);
425 memset(cli
->inbuf
,'\0',smb_size
);
427 set_message(cli
->outbuf
, 4, 0, True
);
429 SCVAL(cli
->outbuf
,smb_com
,SMBntrename
);
430 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
431 cli_setup_packet(cli
);
433 SSVAL(cli
->outbuf
,smb_vwv0
,aSYSTEM
| aHIDDEN
| aDIR
);
434 SSVAL(cli
->outbuf
,smb_vwv1
, RENAME_FLAG_RENAME
);
436 p
= smb_buf(cli
->outbuf
);
438 p
+= clistr_push(cli
, p
, fname_src
, -1, STR_TERMINATE
);
440 p
+= clistr_push(cli
, p
, fname_dst
, -1, STR_TERMINATE
);
442 cli_setup_bcc(cli
, p
);
445 if (!cli_receive_smb(cli
))
448 if (cli_is_error(cli
))
454 /****************************************************************************
456 ****************************************************************************/
458 BOOL
cli_nt_hardlink(struct cli_state
*cli
, const char *fname_src
, const char *fname_dst
)
462 memset(cli
->outbuf
,'\0',smb_size
);
463 memset(cli
->inbuf
,'\0',smb_size
);
465 set_message(cli
->outbuf
, 4, 0, True
);
467 SCVAL(cli
->outbuf
,smb_com
,SMBntrename
);
468 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
469 cli_setup_packet(cli
);
471 SSVAL(cli
->outbuf
,smb_vwv0
,aSYSTEM
| aHIDDEN
| aDIR
);
472 SSVAL(cli
->outbuf
,smb_vwv1
, RENAME_FLAG_HARD_LINK
);
474 p
= smb_buf(cli
->outbuf
);
476 p
+= clistr_push(cli
, p
, fname_src
, -1, STR_TERMINATE
);
478 p
+= clistr_push(cli
, p
, fname_dst
, -1, STR_TERMINATE
);
480 cli_setup_bcc(cli
, p
);
483 if (!cli_receive_smb(cli
))
486 if (cli_is_error(cli
))
492 /****************************************************************************
494 ****************************************************************************/
496 BOOL
cli_unlink(struct cli_state
*cli
, const char *fname
)
500 memset(cli
->outbuf
,'\0',smb_size
);
501 memset(cli
->inbuf
,'\0',smb_size
);
503 set_message(cli
->outbuf
,1, 0,True
);
505 SCVAL(cli
->outbuf
,smb_com
,SMBunlink
);
506 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
507 cli_setup_packet(cli
);
509 SSVAL(cli
->outbuf
,smb_vwv0
,aSYSTEM
| aHIDDEN
);
511 p
= smb_buf(cli
->outbuf
);
513 p
+= clistr_push(cli
, p
, fname
, -1, STR_TERMINATE
);
515 cli_setup_bcc(cli
, p
);
517 if (!cli_receive_smb(cli
)) {
521 if (cli_is_error(cli
)) {
528 /****************************************************************************
530 ****************************************************************************/
532 BOOL
cli_mkdir(struct cli_state
*cli
, const char *dname
)
536 memset(cli
->outbuf
,'\0',smb_size
);
537 memset(cli
->inbuf
,'\0',smb_size
);
539 set_message(cli
->outbuf
,0, 0,True
);
541 SCVAL(cli
->outbuf
,smb_com
,SMBmkdir
);
542 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
543 cli_setup_packet(cli
);
545 p
= smb_buf(cli
->outbuf
);
547 p
+= clistr_push(cli
, p
, dname
, -1, STR_TERMINATE
);
549 cli_setup_bcc(cli
, p
);
552 if (!cli_receive_smb(cli
)) {
556 if (cli_is_error(cli
)) {
563 /****************************************************************************
565 ****************************************************************************/
567 BOOL
cli_rmdir(struct cli_state
*cli
, const char *dname
)
571 memset(cli
->outbuf
,'\0',smb_size
);
572 memset(cli
->inbuf
,'\0',smb_size
);
574 set_message(cli
->outbuf
,0, 0, True
);
576 SCVAL(cli
->outbuf
,smb_com
,SMBrmdir
);
577 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
578 cli_setup_packet(cli
);
580 p
= smb_buf(cli
->outbuf
);
582 p
+= clistr_push(cli
, p
, dname
, -1, STR_TERMINATE
);
584 cli_setup_bcc(cli
, p
);
587 if (!cli_receive_smb(cli
)) {
591 if (cli_is_error(cli
)) {
598 /****************************************************************************
599 Set or clear the delete on close flag.
600 ****************************************************************************/
602 int cli_nt_delete_on_close(struct cli_state
*cli
, int fnum
, BOOL flag
)
604 unsigned int data_len
= 1;
605 unsigned int param_len
= 6;
606 uint16 setup
= TRANSACT2_SETFILEINFO
;
609 char *rparam
=NULL
, *rdata
=NULL
;
611 memset(param
, 0, param_len
);
613 SSVAL(param
,2,SMB_SET_FILE_DISPOSITION_INFO
);
617 if (!cli_send_trans(cli
, SMBtrans2
,
619 -1, 0, /* fid, flags */
620 &setup
, 1, 0, /* setup, length, max */
621 param
, param_len
, 2, /* param, length, max */
622 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
627 if (!cli_receive_trans(cli
, SMBtrans2
,
629 &rdata
, &data_len
)) {
639 /****************************************************************************
640 Open a file - exposing the full horror of the NT API :-).
642 ****************************************************************************/
644 int cli_nt_create_full(struct cli_state
*cli
, const char *fname
,
645 uint32 CreatFlags
, uint32 DesiredAccess
,
646 uint32 FileAttributes
, uint32 ShareAccess
,
647 uint32 CreateDisposition
, uint32 CreateOptions
,
653 memset(cli
->outbuf
,'\0',smb_size
);
654 memset(cli
->inbuf
,'\0',smb_size
);
656 set_message(cli
->outbuf
,24,0,True
);
658 SCVAL(cli
->outbuf
,smb_com
,SMBntcreateX
);
659 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
660 cli_setup_packet(cli
);
662 SSVAL(cli
->outbuf
,smb_vwv0
,0xFF);
663 if (cli
->use_oplocks
)
664 CreatFlags
|= (REQUEST_OPLOCK
|REQUEST_BATCH_OPLOCK
);
666 SIVAL(cli
->outbuf
,smb_ntcreate_Flags
, CreatFlags
);
667 SIVAL(cli
->outbuf
,smb_ntcreate_RootDirectoryFid
, 0x0);
668 SIVAL(cli
->outbuf
,smb_ntcreate_DesiredAccess
, DesiredAccess
);
669 SIVAL(cli
->outbuf
,smb_ntcreate_FileAttributes
, FileAttributes
);
670 SIVAL(cli
->outbuf
,smb_ntcreate_ShareAccess
, ShareAccess
);
671 SIVAL(cli
->outbuf
,smb_ntcreate_CreateDisposition
, CreateDisposition
);
672 SIVAL(cli
->outbuf
,smb_ntcreate_CreateOptions
, CreateOptions
);
673 SIVAL(cli
->outbuf
,smb_ntcreate_ImpersonationLevel
, 0x02);
674 SCVAL(cli
->outbuf
,smb_ntcreate_SecurityFlags
, SecuityFlags
);
676 p
= smb_buf(cli
->outbuf
);
677 /* this alignment and termination is critical for netapp filers. Don't change */
678 p
+= clistr_align_out(cli
, p
, 0);
679 len
= clistr_push(cli
, p
, fname
, -1, 0);
681 SSVAL(cli
->outbuf
,smb_ntcreate_NameLength
, len
);
682 /* sigh. this copes with broken netapp filer behaviour */
683 p
+= clistr_push(cli
, p
, "", -1, STR_TERMINATE
);
685 cli_setup_bcc(cli
, p
);
688 if (!cli_receive_smb(cli
)) {
692 if (cli_is_error(cli
)) {
696 return SVAL(cli
->inbuf
,smb_vwv2
+ 1);
699 /****************************************************************************
701 ****************************************************************************/
703 int cli_nt_create(struct cli_state
*cli
, const char *fname
, uint32 DesiredAccess
)
705 return cli_nt_create_full(cli
, fname
, 0, DesiredAccess
, 0,
706 FILE_SHARE_READ
|FILE_SHARE_WRITE
, FILE_OPEN
, 0x0, 0x0);
709 /****************************************************************************
711 WARNING: if you open with O_WRONLY then getattrE won't work!
712 ****************************************************************************/
714 int cli_open(struct cli_state
*cli
, const char *fname
, int flags
, int share_mode
)
718 unsigned accessmode
=0;
722 if (!(flags
& O_EXCL
)) {
729 accessmode
= (share_mode
<<4);
731 if ((flags
& O_ACCMODE
) == O_RDWR
) {
733 } else if ((flags
& O_ACCMODE
) == O_WRONLY
) {
738 if ((flags
& O_SYNC
) == O_SYNC
) {
739 accessmode
|= (1<<14);
743 if (share_mode
== DENY_FCB
) {
747 memset(cli
->outbuf
,'\0',smb_size
);
748 memset(cli
->inbuf
,'\0',smb_size
);
750 set_message(cli
->outbuf
,15,0,True
);
752 SCVAL(cli
->outbuf
,smb_com
,SMBopenX
);
753 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
754 cli_setup_packet(cli
);
756 SSVAL(cli
->outbuf
,smb_vwv0
,0xFF);
757 SSVAL(cli
->outbuf
,smb_vwv2
,0); /* no additional info */
758 SSVAL(cli
->outbuf
,smb_vwv3
,accessmode
);
759 SSVAL(cli
->outbuf
,smb_vwv4
,aSYSTEM
| aHIDDEN
);
760 SSVAL(cli
->outbuf
,smb_vwv5
,0);
761 SSVAL(cli
->outbuf
,smb_vwv8
,openfn
);
763 if (cli
->use_oplocks
) {
764 /* if using oplocks then ask for a batch oplock via
765 core and extended methods */
766 SCVAL(cli
->outbuf
,smb_flg
, CVAL(cli
->outbuf
,smb_flg
)|
767 FLAG_REQUEST_OPLOCK
|FLAG_REQUEST_BATCH_OPLOCK
);
768 SSVAL(cli
->outbuf
,smb_vwv2
,SVAL(cli
->outbuf
,smb_vwv2
) | 6);
771 p
= smb_buf(cli
->outbuf
);
772 p
+= clistr_push(cli
, p
, fname
, -1, STR_TERMINATE
);
774 cli_setup_bcc(cli
, p
);
777 if (!cli_receive_smb(cli
)) {
781 if (cli_is_error(cli
)) {
785 return SVAL(cli
->inbuf
,smb_vwv2
);
788 /****************************************************************************
790 ****************************************************************************/
792 BOOL
cli_close(struct cli_state
*cli
, int fnum
)
794 memset(cli
->outbuf
,'\0',smb_size
);
795 memset(cli
->inbuf
,'\0',smb_size
);
797 set_message(cli
->outbuf
,3,0,True
);
799 SCVAL(cli
->outbuf
,smb_com
,SMBclose
);
800 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
801 cli_setup_packet(cli
);
803 SSVAL(cli
->outbuf
,smb_vwv0
,fnum
);
804 SIVALS(cli
->outbuf
,smb_vwv1
,-1);
807 if (!cli_receive_smb(cli
)) {
811 return !cli_is_error(cli
);
815 /****************************************************************************
816 send a lock with a specified locktype
817 this is used for testing LOCKING_ANDX_CANCEL_LOCK
818 ****************************************************************************/
820 NTSTATUS
cli_locktype(struct cli_state
*cli
, int fnum
,
821 uint32 offset
, uint32 len
, int timeout
, unsigned char locktype
)
824 int saved_timeout
= cli
->timeout
;
826 memset(cli
->outbuf
,'\0',smb_size
);
827 memset(cli
->inbuf
,'\0', smb_size
);
829 set_message(cli
->outbuf
,8,0,True
);
831 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
832 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
833 cli_setup_packet(cli
);
835 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
836 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
837 SCVAL(cli
->outbuf
,smb_vwv3
,locktype
);
838 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
839 SSVAL(cli
->outbuf
,smb_vwv6
,0);
840 SSVAL(cli
->outbuf
,smb_vwv7
,1);
842 p
= smb_buf(cli
->outbuf
);
843 SSVAL(p
, 0, cli
->pid
);
849 cli_setup_bcc(cli
, p
);
854 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
+ 2*1000);
857 if (!cli_receive_smb(cli
)) {
858 cli
->timeout
= saved_timeout
;
859 return NT_STATUS_UNSUCCESSFUL
;
862 cli
->timeout
= saved_timeout
;
864 return cli_nt_error(cli
);
867 /****************************************************************************
869 note that timeout is in units of 2 milliseconds
870 ****************************************************************************/
872 BOOL
cli_lock(struct cli_state
*cli
, int fnum
,
873 uint32 offset
, uint32 len
, int timeout
, enum brl_type lock_type
)
876 int saved_timeout
= cli
->timeout
;
878 memset(cli
->outbuf
,'\0',smb_size
);
879 memset(cli
->inbuf
,'\0', smb_size
);
881 set_message(cli
->outbuf
,8,0,True
);
883 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
884 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
885 cli_setup_packet(cli
);
887 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
888 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
889 SCVAL(cli
->outbuf
,smb_vwv3
,(lock_type
== READ_LOCK
? 1 : 0));
890 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
891 SSVAL(cli
->outbuf
,smb_vwv6
,0);
892 SSVAL(cli
->outbuf
,smb_vwv7
,1);
894 p
= smb_buf(cli
->outbuf
);
895 SSVAL(p
, 0, cli
->pid
);
901 cli_setup_bcc(cli
, p
);
906 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
*2 + 5*1000);
909 if (!cli_receive_smb(cli
)) {
910 cli
->timeout
= saved_timeout
;
914 cli
->timeout
= saved_timeout
;
916 if (cli_is_error(cli
)) {
923 /****************************************************************************
925 ****************************************************************************/
927 BOOL
cli_unlock(struct cli_state
*cli
, int fnum
, uint32 offset
, uint32 len
)
931 memset(cli
->outbuf
,'\0',smb_size
);
932 memset(cli
->inbuf
,'\0',smb_size
);
934 set_message(cli
->outbuf
,8,0,True
);
936 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
937 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
938 cli_setup_packet(cli
);
940 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
941 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
942 SCVAL(cli
->outbuf
,smb_vwv3
,0);
943 SIVALS(cli
->outbuf
, smb_vwv4
, 0);
944 SSVAL(cli
->outbuf
,smb_vwv6
,1);
945 SSVAL(cli
->outbuf
,smb_vwv7
,0);
947 p
= smb_buf(cli
->outbuf
);
948 SSVAL(p
, 0, cli
->pid
);
952 cli_setup_bcc(cli
, p
);
954 if (!cli_receive_smb(cli
)) {
958 if (cli_is_error(cli
)) {
965 /****************************************************************************
966 Lock a file with 64 bit offsets.
967 ****************************************************************************/
969 BOOL
cli_lock64(struct cli_state
*cli
, int fnum
,
970 SMB_BIG_UINT offset
, SMB_BIG_UINT len
, int timeout
, enum brl_type lock_type
)
973 int saved_timeout
= cli
->timeout
;
976 if (! (cli
->capabilities
& CAP_LARGE_FILES
)) {
977 return cli_lock(cli
, fnum
, offset
, len
, timeout
, lock_type
);
980 ltype
= (lock_type
== READ_LOCK
? 1 : 0);
981 ltype
|= LOCKING_ANDX_LARGE_FILES
;
983 memset(cli
->outbuf
,'\0',smb_size
);
984 memset(cli
->inbuf
,'\0', smb_size
);
986 set_message(cli
->outbuf
,8,0,True
);
988 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
989 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
990 cli_setup_packet(cli
);
992 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
993 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
994 SCVAL(cli
->outbuf
,smb_vwv3
,ltype
);
995 SIVALS(cli
->outbuf
, smb_vwv4
, timeout
);
996 SSVAL(cli
->outbuf
,smb_vwv6
,0);
997 SSVAL(cli
->outbuf
,smb_vwv7
,1);
999 p
= smb_buf(cli
->outbuf
);
1000 SIVAL(p
, 0, cli
->pid
);
1001 SOFF_T_R(p
, 4, offset
);
1002 SOFF_T_R(p
, 12, len
);
1005 cli_setup_bcc(cli
, p
);
1009 cli
->timeout
= (timeout
== -1) ? 0x7FFFFFFF : (timeout
+ 5*1000);
1012 if (!cli_receive_smb(cli
)) {
1013 cli
->timeout
= saved_timeout
;
1017 cli
->timeout
= saved_timeout
;
1019 if (cli_is_error(cli
)) {
1026 /****************************************************************************
1027 Unlock a file with 64 bit offsets.
1028 ****************************************************************************/
1030 BOOL
cli_unlock64(struct cli_state
*cli
, int fnum
, SMB_BIG_UINT offset
, SMB_BIG_UINT len
)
1034 if (! (cli
->capabilities
& CAP_LARGE_FILES
)) {
1035 return cli_unlock(cli
, fnum
, offset
, len
);
1038 memset(cli
->outbuf
,'\0',smb_size
);
1039 memset(cli
->inbuf
,'\0',smb_size
);
1041 set_message(cli
->outbuf
,8,0,True
);
1043 SCVAL(cli
->outbuf
,smb_com
,SMBlockingX
);
1044 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1045 cli_setup_packet(cli
);
1047 SCVAL(cli
->outbuf
,smb_vwv0
,0xFF);
1048 SSVAL(cli
->outbuf
,smb_vwv2
,fnum
);
1049 SCVAL(cli
->outbuf
,smb_vwv3
,LOCKING_ANDX_LARGE_FILES
);
1050 SIVALS(cli
->outbuf
, smb_vwv4
, 0);
1051 SSVAL(cli
->outbuf
,smb_vwv6
,1);
1052 SSVAL(cli
->outbuf
,smb_vwv7
,0);
1054 p
= smb_buf(cli
->outbuf
);
1055 SIVAL(p
, 0, cli
->pid
);
1056 SOFF_T_R(p
, 4, offset
);
1057 SOFF_T_R(p
, 12, len
);
1059 cli_setup_bcc(cli
, p
);
1061 if (!cli_receive_smb(cli
)) {
1065 if (cli_is_error(cli
)) {
1072 /****************************************************************************
1073 Get/unlock a POSIX lock on a file - internal function.
1074 ****************************************************************************/
1076 static BOOL
cli_posix_lock_internal(struct cli_state
*cli
, int fnum
,
1077 SMB_BIG_UINT offset
, SMB_BIG_UINT len
, BOOL wait_lock
, enum brl_type lock_type
)
1079 unsigned int param_len
= 4;
1080 unsigned int data_len
= POSIX_LOCK_DATA_SIZE
;
1081 uint16 setup
= TRANSACT2_SETFILEINFO
;
1083 unsigned char data
[POSIX_LOCK_DATA_SIZE
];
1084 char *rparam
=NULL
, *rdata
=NULL
;
1085 int saved_timeout
= cli
->timeout
;
1087 SSVAL(param
,0,fnum
);
1088 SSVAL(param
,2,SMB_SET_POSIX_LOCK
);
1090 switch (lock_type
) {
1092 SSVAL(data
, POSIX_LOCK_TYPE_OFFSET
, POSIX_LOCK_TYPE_READ
);
1095 SSVAL(data
, POSIX_LOCK_TYPE_OFFSET
, POSIX_LOCK_TYPE_WRITE
);
1098 SSVAL(data
, POSIX_LOCK_TYPE_OFFSET
, POSIX_LOCK_TYPE_UNLOCK
);
1105 SSVAL(data
, POSIX_LOCK_FLAGS_OFFSET
, POSIX_LOCK_FLAG_WAIT
);
1106 cli
->timeout
= 0x7FFFFFFF;
1108 SSVAL(data
, POSIX_LOCK_FLAGS_OFFSET
, POSIX_LOCK_FLAG_NOWAIT
);
1111 SIVAL(data
, POSIX_LOCK_PID_OFFSET
, cli
->pid
);
1112 SOFF_T(data
, POSIX_LOCK_START_OFFSET
, offset
);
1113 SOFF_T(data
, POSIX_LOCK_LEN_OFFSET
, len
);
1115 if (!cli_send_trans(cli
, SMBtrans2
,
1117 -1, 0, /* fid, flags */
1118 &setup
, 1, 0, /* setup, length, max */
1119 param
, param_len
, 2, /* param, length, max */
1120 (char *)&data
, data_len
, cli
->max_xmit
/* data, length, max */
1122 cli
->timeout
= saved_timeout
;
1126 if (!cli_receive_trans(cli
, SMBtrans2
,
1127 &rparam
, ¶m_len
,
1128 &rdata
, &data_len
)) {
1129 cli
->timeout
= saved_timeout
;
1135 cli
->timeout
= saved_timeout
;
1143 /****************************************************************************
1145 ****************************************************************************/
1147 BOOL
cli_posix_lock(struct cli_state
*cli
, int fnum
,
1148 SMB_BIG_UINT offset
, SMB_BIG_UINT len
,
1149 BOOL wait_lock
, enum brl_type lock_type
)
1151 if (lock_type
!= READ_LOCK
&& lock_type
!= WRITE_LOCK
) {
1154 return cli_posix_lock_internal(cli
, fnum
, offset
, len
, wait_lock
, lock_type
);
1157 /****************************************************************************
1158 POSIX Unlock a file.
1159 ****************************************************************************/
1161 BOOL
cli_posix_unlock(struct cli_state
*cli
, int fnum
, SMB_BIG_UINT offset
, SMB_BIG_UINT len
)
1163 return cli_posix_lock_internal(cli
, fnum
, offset
, len
, False
, UNLOCK_LOCK
);
1166 /****************************************************************************
1167 POSIX Get any lock covering a file.
1168 ****************************************************************************/
1170 BOOL
cli_posix_getlock(struct cli_state
*cli
, int fnum
, SMB_BIG_UINT
*poffset
, SMB_BIG_UINT
*plen
)
1175 /****************************************************************************
1176 Do a SMBgetattrE call.
1177 ****************************************************************************/
1179 BOOL
cli_getattrE(struct cli_state
*cli
, int fd
,
1180 uint16
*attr
, SMB_OFF_T
*size
,
1181 time_t *c_time
, time_t *a_time
, time_t *m_time
)
1183 memset(cli
->outbuf
,'\0',smb_size
);
1184 memset(cli
->inbuf
,'\0',smb_size
);
1186 set_message(cli
->outbuf
,1,0,True
);
1188 SCVAL(cli
->outbuf
,smb_com
,SMBgetattrE
);
1189 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1190 cli_setup_packet(cli
);
1192 SSVAL(cli
->outbuf
,smb_vwv0
,fd
);
1195 if (!cli_receive_smb(cli
)) {
1199 if (cli_is_error(cli
)) {
1204 *size
= IVAL(cli
->inbuf
, smb_vwv6
);
1208 *attr
= SVAL(cli
->inbuf
,smb_vwv10
);
1212 *c_time
= cli_make_unix_date2(cli
, cli
->inbuf
+smb_vwv0
);
1216 *a_time
= cli_make_unix_date2(cli
, cli
->inbuf
+smb_vwv2
);
1220 *m_time
= cli_make_unix_date2(cli
, cli
->inbuf
+smb_vwv4
);
1226 /****************************************************************************
1228 ****************************************************************************/
1230 BOOL
cli_getatr(struct cli_state
*cli
, const char *fname
,
1231 uint16
*attr
, SMB_OFF_T
*size
, time_t *t
)
1235 memset(cli
->outbuf
,'\0',smb_size
);
1236 memset(cli
->inbuf
,'\0',smb_size
);
1238 set_message(cli
->outbuf
,0,0,True
);
1240 SCVAL(cli
->outbuf
,smb_com
,SMBgetatr
);
1241 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1242 cli_setup_packet(cli
);
1244 p
= smb_buf(cli
->outbuf
);
1246 p
+= clistr_push(cli
, p
, fname
, -1, STR_TERMINATE
);
1248 cli_setup_bcc(cli
, p
);
1251 if (!cli_receive_smb(cli
)) {
1255 if (cli_is_error(cli
)) {
1260 *size
= IVAL(cli
->inbuf
, smb_vwv3
);
1264 *t
= cli_make_unix_date3(cli
, cli
->inbuf
+smb_vwv1
);
1268 *attr
= SVAL(cli
->inbuf
,smb_vwv0
);
1275 /****************************************************************************
1276 Do a SMBsetattrE call.
1277 ****************************************************************************/
1279 BOOL
cli_setattrE(struct cli_state
*cli
, int fd
,
1280 time_t c_time
, time_t a_time
, time_t m_time
)
1285 memset(cli
->outbuf
,'\0',smb_size
);
1286 memset(cli
->inbuf
,'\0',smb_size
);
1288 set_message(cli
->outbuf
,7,0,True
);
1290 SCVAL(cli
->outbuf
,smb_com
,SMBsetattrE
);
1291 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1292 cli_setup_packet(cli
);
1294 SSVAL(cli
->outbuf
,smb_vwv0
, fd
);
1295 cli_put_dos_date2(cli
, cli
->outbuf
,smb_vwv1
, c_time
);
1296 cli_put_dos_date2(cli
, cli
->outbuf
,smb_vwv3
, a_time
);
1297 cli_put_dos_date2(cli
, cli
->outbuf
,smb_vwv5
, m_time
);
1299 p
= smb_buf(cli
->outbuf
);
1302 cli_setup_bcc(cli
, p
);
1305 if (!cli_receive_smb(cli
)) {
1309 if (cli_is_error(cli
)) {
1316 /****************************************************************************
1317 Do a SMBsetatr call.
1318 ****************************************************************************/
1320 BOOL
cli_setatr(struct cli_state
*cli
, const char *fname
, uint16 attr
, time_t t
)
1324 memset(cli
->outbuf
,'\0',smb_size
);
1325 memset(cli
->inbuf
,'\0',smb_size
);
1327 set_message(cli
->outbuf
,8,0,True
);
1329 SCVAL(cli
->outbuf
,smb_com
,SMBsetatr
);
1330 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1331 cli_setup_packet(cli
);
1333 SSVAL(cli
->outbuf
,smb_vwv0
, attr
);
1334 cli_put_dos_date3(cli
, cli
->outbuf
,smb_vwv1
, t
);
1336 p
= smb_buf(cli
->outbuf
);
1338 p
+= clistr_push(cli
, p
, fname
, -1, STR_TERMINATE
);
1341 cli_setup_bcc(cli
, p
);
1344 if (!cli_receive_smb(cli
)) {
1348 if (cli_is_error(cli
)) {
1355 /****************************************************************************
1356 Check for existance of a dir.
1357 ****************************************************************************/
1358 BOOL
cli_chkpath(struct cli_state
*cli
, const char *path
)
1363 pstrcpy(path2
,path
);
1364 trim_char(path2
,'\0','\\');
1368 memset(cli
->outbuf
,'\0',smb_size
);
1369 set_message(cli
->outbuf
,0,0,True
);
1370 SCVAL(cli
->outbuf
,smb_com
,SMBchkpth
);
1371 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1372 cli_setup_packet(cli
);
1373 p
= smb_buf(cli
->outbuf
);
1375 p
+= clistr_push(cli
, p
, path2
, -1, STR_TERMINATE
);
1377 cli_setup_bcc(cli
, p
);
1380 if (!cli_receive_smb(cli
)) {
1384 if (cli_is_error(cli
)) return False
;
1389 /****************************************************************************
1391 ****************************************************************************/
1393 BOOL
cli_dskattr(struct cli_state
*cli
, int *bsize
, int *total
, int *avail
)
1395 memset(cli
->outbuf
,'\0',smb_size
);
1396 set_message(cli
->outbuf
,0,0,True
);
1397 SCVAL(cli
->outbuf
,smb_com
,SMBdskattr
);
1398 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1399 cli_setup_packet(cli
);
1402 if (!cli_receive_smb(cli
)) {
1406 *bsize
= SVAL(cli
->inbuf
,smb_vwv1
)*SVAL(cli
->inbuf
,smb_vwv2
);
1407 *total
= SVAL(cli
->inbuf
,smb_vwv0
);
1408 *avail
= SVAL(cli
->inbuf
,smb_vwv3
);
1413 /****************************************************************************
1414 Create and open a temporary file.
1415 ****************************************************************************/
1417 int cli_ctemp(struct cli_state
*cli
, const char *path
, char **tmp_path
)
1422 memset(cli
->outbuf
,'\0',smb_size
);
1423 memset(cli
->inbuf
,'\0',smb_size
);
1425 set_message(cli
->outbuf
,3,0,True
);
1427 SCVAL(cli
->outbuf
,smb_com
,SMBctemp
);
1428 SSVAL(cli
->outbuf
,smb_tid
,cli
->cnum
);
1429 cli_setup_packet(cli
);
1431 SSVAL(cli
->outbuf
,smb_vwv0
,0);
1432 SIVALS(cli
->outbuf
,smb_vwv1
,-1);
1434 p
= smb_buf(cli
->outbuf
);
1436 p
+= clistr_push(cli
, p
, path
, -1, STR_TERMINATE
);
1438 cli_setup_bcc(cli
, p
);
1441 if (!cli_receive_smb(cli
)) {
1445 if (cli_is_error(cli
)) {
1449 /* despite the spec, the result has a -1, followed by
1450 length, followed by name */
1451 p
= smb_buf(cli
->inbuf
);
1453 len
= smb_buflen(cli
->inbuf
) - 4;
1454 if (len
<= 0) return -1;
1458 clistr_pull(cli
, path2
, p
,
1459 sizeof(path2
), len
, STR_ASCII
);
1460 *tmp_path
= SMB_STRDUP(path2
);
1463 return SVAL(cli
->inbuf
,smb_vwv0
);
1468 send a raw ioctl - used by the torture code
1470 NTSTATUS
cli_raw_ioctl(struct cli_state
*cli
, int fnum
, uint32 code
, DATA_BLOB
*blob
)
1472 memset(cli
->outbuf
,'\0',smb_size
);
1473 memset(cli
->inbuf
,'\0',smb_size
);
1475 set_message(cli
->outbuf
, 3, 0, True
);
1476 SCVAL(cli
->outbuf
,smb_com
,SMBioctl
);
1477 cli_setup_packet(cli
);
1479 SSVAL(cli
->outbuf
, smb_vwv0
, fnum
);
1480 SSVAL(cli
->outbuf
, smb_vwv1
, code
>>16);
1481 SSVAL(cli
->outbuf
, smb_vwv2
, (code
&0xFFFF));
1484 if (!cli_receive_smb(cli
)) {
1485 return NT_STATUS_UNEXPECTED_NETWORK_ERROR
;
1488 if (cli_is_error(cli
)) {
1489 return cli_nt_error(cli
);
1492 *blob
= data_blob(NULL
, 0);
1494 return NT_STATUS_OK
;
1497 /*********************************************************
1498 Set an extended attribute utility fn.
1499 *********************************************************/
1501 static BOOL
cli_set_ea(struct cli_state
*cli
, uint16 setup
, char *param
, unsigned int param_len
,
1502 const char *ea_name
, const char *ea_val
, size_t ea_len
)
1504 unsigned int data_len
= 0;
1506 char *rparam
=NULL
, *rdata
=NULL
;
1508 size_t ea_namelen
= strlen(ea_name
);
1510 if (ea_namelen
== 0 && ea_len
== 0) {
1512 data
= SMB_MALLOC(data_len
);
1517 SIVAL(p
,0,data_len
);
1519 data_len
= 4 + 4 + ea_namelen
+ 1 + ea_len
;
1520 data
= SMB_MALLOC(data_len
);
1525 SIVAL(p
,0,data_len
);
1527 SCVAL(p
, 0, 0); /* EA flags. */
1528 SCVAL(p
, 1, ea_namelen
);
1529 SSVAL(p
, 2, ea_len
);
1530 memcpy(p
+4, ea_name
, ea_namelen
+1); /* Copy in the name. */
1531 memcpy(p
+4+ea_namelen
+1, ea_val
, ea_len
);
1534 if (!cli_send_trans(cli
, SMBtrans2
,
1536 -1, 0, /* fid, flags */
1537 &setup
, 1, 0, /* setup, length, max */
1538 param
, param_len
, 2, /* param, length, max */
1539 data
, data_len
, cli
->max_xmit
/* data, length, max */
1544 if (!cli_receive_trans(cli
, SMBtrans2
,
1545 &rparam
, ¶m_len
,
1546 &rdata
, &data_len
)) {
1557 /*********************************************************
1558 Set an extended attribute on a pathname.
1559 *********************************************************/
1561 BOOL
cli_set_ea_path(struct cli_state
*cli
, const char *path
, const char *ea_name
, const char *ea_val
, size_t ea_len
)
1563 uint16 setup
= TRANSACT2_SETPATHINFO
;
1564 unsigned int param_len
= 0;
1565 char param
[sizeof(pstring
)+6];
1566 size_t srclen
= 2*(strlen(path
)+1);
1569 memset(param
, 0, sizeof(param
));
1570 SSVAL(param
,0,SMB_INFO_SET_EA
);
1573 p
+= clistr_push(cli
, p
, path
, MIN(srclen
, sizeof(param
)-6), STR_TERMINATE
);
1574 param_len
= PTR_DIFF(p
, param
);
1576 return cli_set_ea(cli
, setup
, param
, param_len
, ea_name
, ea_val
, ea_len
);
1579 /*********************************************************
1580 Set an extended attribute on an fnum.
1581 *********************************************************/
1583 BOOL
cli_set_ea_fnum(struct cli_state
*cli
, int fnum
, const char *ea_name
, const char *ea_val
, size_t ea_len
)
1586 uint16 setup
= TRANSACT2_SETFILEINFO
;
1588 memset(param
, 0, 6);
1589 SSVAL(param
,0,fnum
);
1590 SSVAL(param
,2,SMB_INFO_SET_EA
);
1592 return cli_set_ea(cli
, setup
, param
, 6, ea_name
, ea_val
, ea_len
);
1595 /*********************************************************
1596 Get an extended attribute list tility fn.
1597 *********************************************************/
1599 static BOOL
cli_get_ea_list(struct cli_state
*cli
,
1600 uint16 setup
, char *param
, unsigned int param_len
,
1603 struct ea_struct
**pea_list
)
1605 unsigned int data_len
= 0;
1606 unsigned int rparam_len
, rdata_len
;
1607 char *rparam
=NULL
, *rdata
=NULL
;
1612 struct ea_struct
*ea_list
;
1619 if (!cli_send_trans(cli
, SMBtrans2
,
1621 -1, 0, /* fid, flags */
1622 &setup
, 1, 0, /* setup, length, max */
1623 param
, param_len
, 10, /* param, length, max */
1624 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
1629 if (!cli_receive_trans(cli
, SMBtrans2
,
1630 &rparam
, &rparam_len
,
1631 &rdata
, &rdata_len
)) {
1635 if (!rdata
|| rdata_len
< 4) {
1639 ea_size
= (size_t)IVAL(rdata
,0);
1640 if (ea_size
> rdata_len
) {
1645 /* No EA's present. */
1653 /* Validate the EA list and count it. */
1654 for (num_eas
= 0; ea_size
>= 4; num_eas
++) {
1655 unsigned int ea_namelen
= CVAL(p
,1);
1656 unsigned int ea_valuelen
= SVAL(p
,2);
1657 if (ea_namelen
== 0) {
1660 if (4 + ea_namelen
+ 1 + ea_valuelen
> ea_size
) {
1663 ea_size
-= 4 + ea_namelen
+ 1 + ea_valuelen
;
1664 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
1672 *pnum_eas
= num_eas
;
1674 /* Caller only wants number of EA's. */
1679 ea_list
= TALLOC_ARRAY(ctx
, struct ea_struct
, num_eas
);
1684 ea_size
= (size_t)IVAL(rdata
,0);
1687 for (num_eas
= 0; num_eas
< *pnum_eas
; num_eas
++ ) {
1688 struct ea_struct
*ea
= &ea_list
[num_eas
];
1689 fstring unix_ea_name
;
1690 unsigned int ea_namelen
= CVAL(p
,1);
1691 unsigned int ea_valuelen
= SVAL(p
,2);
1693 ea
->flags
= CVAL(p
,0);
1694 unix_ea_name
[0] = '\0';
1695 pull_ascii_fstring(unix_ea_name
, p
+ 4);
1696 ea
->name
= talloc_strdup(ctx
, unix_ea_name
);
1697 /* Ensure the value is null terminated (in case it's a string). */
1698 ea
->value
= data_blob_talloc(ctx
, NULL
, ea_valuelen
+ 1);
1699 if (!ea
->value
.data
) {
1703 memcpy(ea
->value
.data
, p
+4+ea_namelen
+1, ea_valuelen
);
1705 ea
->value
.data
[ea_valuelen
] = 0;
1707 p
+= 4 + ea_namelen
+ 1 + ea_valuelen
;
1710 *pea_list
= ea_list
;
1720 /*********************************************************
1721 Get an extended attribute list from a pathname.
1722 *********************************************************/
1724 BOOL
cli_get_ea_list_path(struct cli_state
*cli
, const char *path
,
1727 struct ea_struct
**pea_list
)
1729 uint16 setup
= TRANSACT2_QPATHINFO
;
1730 unsigned int param_len
= 0;
1731 char param
[sizeof(pstring
)+6];
1736 SSVAL(p
, 0, SMB_INFO_QUERY_ALL_EAS
);
1738 p
+= clistr_push(cli
, p
, path
, sizeof(pstring
)-6, STR_TERMINATE
);
1739 param_len
= PTR_DIFF(p
, param
);
1741 return cli_get_ea_list(cli
, setup
, param
, param_len
, ctx
, pnum_eas
, pea_list
);
1744 /*********************************************************
1745 Get an extended attribute list from an fnum.
1746 *********************************************************/
1748 BOOL
cli_get_ea_list_fnum(struct cli_state
*cli
, int fnum
,
1751 struct ea_struct
**pea_list
)
1753 uint16 setup
= TRANSACT2_QFILEINFO
;
1756 memset(param
, 0, 6);
1757 SSVAL(param
,0,fnum
);
1758 SSVAL(param
,2,SMB_INFO_SET_EA
);
1760 return cli_get_ea_list(cli
, setup
, param
, 6, ctx
, pnum_eas
, pea_list
);