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 Map wire permissions to standard UNIX.
107 ****************************************************************************/
109 mode_t wire_perms_to_unix(uint32 perms)
111 mode_t ret = (mode_t)0;
113 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
114 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
115 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
116 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
117 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
118 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
119 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
120 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
121 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
123 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
126 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
129 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
134 /****************************************************************************
135 Return the file type from the wire filetype for UNIX extensions.
136 ****************************************************************************/
138 static mode_t unix_filetype_from_wire(uint32 wire_type)
146 case UNIX_TYPE_SYMLINK:
150 case UNIX_TYPE_CHARDEV:
154 case UNIX_TYPE_BLKDEV:
162 case UNIX_TYPE_SOCKET:
170 /****************************************************************************
171 Stat a file (UNIX extensions).
172 ****************************************************************************/
174 BOOL cli_unix_stat(struct cli_state *cli, const char *name, SMB_STRUCT_STAT *sbuf)
176 unsigned int param_len = 0;
177 unsigned int data_len = 0;
178 uint16 setup = TRANSACT2_QPATHINFO;
179 char param[sizeof(pstring)+6];
180 char *rparam=NULL, *rdata=NULL;
187 SSVAL(p, 0, SMB_QUERY_FILE_UNIX_BASIC);
189 p += clistr_push(cli, p, name, sizeof(pstring)-6, STR_TERMINATE);
190 param_len = PTR_DIFF(p, param);
192 if (!cli_send_trans(cli, SMBtrans2,
194 -1, 0, /* fid, flags */
195 &setup, 1, 0, /* setup, length, max */
196 param, param_len, 2, /* param, length, max */
197 NULL, 0, cli->max_xmit /* data, length, max */
202 if (!cli_receive_trans(cli, SMBtrans2,
204 &rdata, &data_len)) {
208 sbuf->st_size = IVAL2_TO_SMB_BIG_UINT(rdata,0); /* total size, in bytes */
209 sbuf->st_blocks = IVAL2_TO_SMB_BIG_UINT(rdata,8); /* number of blocks allocated */
210 sbuf->st_blocks /= STAT_ST_BLOCKSIZE;
211 sbuf->st_ctime = interpret_long_date(rdata + 16); /* time of last change */
212 sbuf->st_atime = interpret_long_date(rdata + 24); /* time of last access */
213 sbuf->st_mtime = interpret_long_date(rdata + 32); /* time of last modification */
214 sbuf->st_uid = IVAL(rdata,40); /* user ID of owner */
215 sbuf->st_gid = IVAL(rdata,48); /* group ID of owner */
216 sbuf->st_mode |= unix_filetype_from_wire(IVAL(rdata, 56));
217 #if defined(HAVE_MAKEDEV)
219 uint32 dev_major = IVAL(rdata,60);
220 uint32 dev_minor = IVAL(rdata,68);
221 sbuf->st_rdev = makedev(dev_major, dev_minor);
224 sbuf->st_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(rdata,76); /* inode */
225 sbuf->st_mode |= wire_perms_to_unix(IVAL(rdata,84)); /* protection */
226 sbuf->st_nlink = IVAL(rdata,92); /* number of hard links */
234 /****************************************************************************
235 Symlink a file (UNIX extensions).
236 ****************************************************************************/
238 BOOL cli_unix_symlink(struct cli_state *cli, const char *oldname, const char *newname)
240 return cli_link_internal(cli, oldname, newname, False);
243 /****************************************************************************
244 Hard a file (UNIX extensions).
245 ****************************************************************************/
247 BOOL cli_unix_hardlink(struct cli_state *cli, const char *oldname, const char *newname)
249 return cli_link_internal(cli, oldname, newname, True);
252 /****************************************************************************
253 Chmod or chown a file internal (UNIX extensions).
254 ****************************************************************************/
256 static BOOL cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname, uint32 mode, uint32 uid, uint32 gid)
258 unsigned int data_len = 0;
259 unsigned int param_len = 0;
260 uint16 setup = TRANSACT2_SETPATHINFO;
261 char param[sizeof(pstring)+6];
263 char *rparam=NULL, *rdata=NULL;
266 memset(param, 0, sizeof(param));
267 memset(data, 0, sizeof(data));
268 SSVAL(param,0,SMB_SET_FILE_UNIX_BASIC);
271 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
272 param_len = PTR_DIFF(p, param);
280 if (!cli_send_trans(cli, SMBtrans2,
282 -1, 0, /* fid, flags */
283 &setup, 1, 0, /* setup, length, max */
284 param, param_len, 2, /* param, length, max */
285 (char *)&data, data_len, cli->max_xmit /* data, length, max */
290 if (!cli_receive_trans(cli, SMBtrans2,
292 &rdata, &data_len)) {
302 /****************************************************************************
303 chmod a file (UNIX extensions).
304 ****************************************************************************/
306 BOOL cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
308 return cli_unix_chmod_chown_internal(cli, fname,
309 unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
312 /****************************************************************************
313 chown a file (UNIX extensions).
314 ****************************************************************************/
316 BOOL cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
318 return cli_unix_chmod_chown_internal(cli, fname, SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
321 /****************************************************************************
323 ****************************************************************************/
325 BOOL cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
329 memset(cli->outbuf,'\0',smb_size);
330 memset(cli->inbuf,'\0',smb_size);
332 set_message(cli->outbuf,1, 0, True);
334 SCVAL(cli->outbuf,smb_com,SMBmv);
335 SSVAL(cli->outbuf,smb_tid,cli->cnum);
336 cli_setup_packet(cli);
338 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
340 p = smb_buf(cli->outbuf);
342 p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
344 p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
346 cli_setup_bcc(cli, p);
349 if (!cli_receive_smb(cli))
352 if (cli_is_error(cli))
358 /****************************************************************************
360 ****************************************************************************/
362 BOOL cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
366 memset(cli->outbuf,'\0',smb_size);
367 memset(cli->inbuf,'\0',smb_size);
369 set_message(cli->outbuf, 4, 0, True);
371 SCVAL(cli->outbuf,smb_com,SMBntrename);
372 SSVAL(cli->outbuf,smb_tid,cli->cnum);
373 cli_setup_packet(cli);
375 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
376 SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_RENAME);
378 p = smb_buf(cli->outbuf);
380 p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
382 p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
384 cli_setup_bcc(cli, p);
387 if (!cli_receive_smb(cli))
390 if (cli_is_error(cli))
396 /****************************************************************************
398 ****************************************************************************/
400 BOOL cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
404 memset(cli->outbuf,'\0',smb_size);
405 memset(cli->inbuf,'\0',smb_size);
407 set_message(cli->outbuf, 4, 0, True);
409 SCVAL(cli->outbuf,smb_com,SMBntrename);
410 SSVAL(cli->outbuf,smb_tid,cli->cnum);
411 cli_setup_packet(cli);
413 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
414 SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_HARD_LINK);
416 p = smb_buf(cli->outbuf);
418 p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
420 p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
422 cli_setup_bcc(cli, p);
425 if (!cli_receive_smb(cli))
428 if (cli_is_error(cli))
434 /****************************************************************************
436 ****************************************************************************/
438 BOOL cli_unlink(struct cli_state *cli, const char *fname)
442 memset(cli->outbuf,'\0',smb_size);
443 memset(cli->inbuf,'\0',smb_size);
445 set_message(cli->outbuf,1, 0,True);
447 SCVAL(cli->outbuf,smb_com,SMBunlink);
448 SSVAL(cli->outbuf,smb_tid,cli->cnum);
449 cli_setup_packet(cli);
451 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN);
453 p = smb_buf(cli->outbuf);
455 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
457 cli_setup_bcc(cli, p);
459 if (!cli_receive_smb(cli)) {
463 if (cli_is_error(cli)) {
470 /****************************************************************************
472 ****************************************************************************/
474 BOOL cli_mkdir(struct cli_state *cli, const char *dname)
478 memset(cli->outbuf,'\0',smb_size);
479 memset(cli->inbuf,'\0',smb_size);
481 set_message(cli->outbuf,0, 0,True);
483 SCVAL(cli->outbuf,smb_com,SMBmkdir);
484 SSVAL(cli->outbuf,smb_tid,cli->cnum);
485 cli_setup_packet(cli);
487 p = smb_buf(cli->outbuf);
489 p += clistr_push(cli, p, dname, -1, STR_TERMINATE);
491 cli_setup_bcc(cli, p);
494 if (!cli_receive_smb(cli)) {
498 if (cli_is_error(cli)) {
505 /****************************************************************************
507 ****************************************************************************/
509 BOOL cli_rmdir(struct cli_state *cli, const char *dname)
513 memset(cli->outbuf,'\0',smb_size);
514 memset(cli->inbuf,'\0',smb_size);
516 set_message(cli->outbuf,0, 0, True);
518 SCVAL(cli->outbuf,smb_com,SMBrmdir);
519 SSVAL(cli->outbuf,smb_tid,cli->cnum);
520 cli_setup_packet(cli);
522 p = smb_buf(cli->outbuf);
524 p += clistr_push(cli, p, dname, -1, STR_TERMINATE);
526 cli_setup_bcc(cli, p);
529 if (!cli_receive_smb(cli)) {
533 if (cli_is_error(cli)) {
540 /****************************************************************************
541 Set or clear the delete on close flag.
542 ****************************************************************************/
544 int cli_nt_delete_on_close(struct cli_state *cli, int fnum, BOOL flag)
546 unsigned int data_len = 1;
547 unsigned int param_len = 6;
548 uint16 setup = TRANSACT2_SETFILEINFO;
551 char *rparam=NULL, *rdata=NULL;
553 memset(param, 0, param_len);
555 SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
559 if (!cli_send_trans(cli, SMBtrans2,
561 -1, 0, /* fid, flags */
562 &setup, 1, 0, /* setup, length, max */
563 param, param_len, 2, /* param, length, max */
564 (char *)&data, data_len, cli->max_xmit /* data, length, max */
569 if (!cli_receive_trans(cli, SMBtrans2,
571 &rdata, &data_len)) {
581 /****************************************************************************
582 Open a file - exposing the full horror of the NT API :-).
584 ****************************************************************************/
586 int cli_nt_create_full(struct cli_state *cli, const char *fname,
587 uint32 CreatFlags, uint32 DesiredAccess,
588 uint32 FileAttributes, uint32 ShareAccess,
589 uint32 CreateDisposition, uint32 CreateOptions,
595 memset(cli->outbuf,'\0',smb_size);
596 memset(cli->inbuf,'\0',smb_size);
598 set_message(cli->outbuf,24,0,True);
600 SCVAL(cli->outbuf,smb_com,SMBntcreateX);
601 SSVAL(cli->outbuf,smb_tid,cli->cnum);
602 cli_setup_packet(cli);
604 SSVAL(cli->outbuf,smb_vwv0,0xFF);
605 if (cli->use_oplocks)
606 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
608 SIVAL(cli->outbuf,smb_ntcreate_Flags, CreatFlags);
609 SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
610 SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, DesiredAccess);
611 SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, FileAttributes);
612 SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, ShareAccess);
613 SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, CreateDisposition);
614 SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, CreateOptions);
615 SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
616 SCVAL(cli->outbuf,smb_ntcreate_SecurityFlags, SecuityFlags);
618 p = smb_buf(cli->outbuf);
619 /* this alignment and termination is critical for netapp filers. Don't change */
620 p += clistr_align_out(cli, p, 0);
621 len = clistr_push(cli, p, fname, -1, 0);
623 SSVAL(cli->outbuf,smb_ntcreate_NameLength, len);
624 /* sigh. this copes with broken netapp filer behaviour */
625 p += clistr_push(cli, p, "", -1, STR_TERMINATE);
627 cli_setup_bcc(cli, p);
630 if (!cli_receive_smb(cli)) {
634 if (cli_is_error(cli)) {
638 return SVAL(cli->inbuf,smb_vwv2 + 1);
641 /****************************************************************************
643 ****************************************************************************/
645 int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess)
647 return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
648 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_EXISTS_OPEN, 0x0, 0x0);
651 /****************************************************************************
653 WARNING: if you open with O_WRONLY then getattrE won't work!
654 ****************************************************************************/
656 int cli_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
660 unsigned accessmode=0;
664 if (!(flags & O_EXCL)) {
671 accessmode = (share_mode<<4);
673 if ((flags & O_ACCMODE) == O_RDWR) {
675 } else if ((flags & O_ACCMODE) == O_WRONLY) {
680 if ((flags & O_SYNC) == O_SYNC) {
681 accessmode |= (1<<14);
685 if (share_mode == DENY_FCB) {
689 memset(cli->outbuf,'\0',smb_size);
690 memset(cli->inbuf,'\0',smb_size);
692 set_message(cli->outbuf,15,0,True);
694 SCVAL(cli->outbuf,smb_com,SMBopenX);
695 SSVAL(cli->outbuf,smb_tid,cli->cnum);
696 cli_setup_packet(cli);
698 SSVAL(cli->outbuf,smb_vwv0,0xFF);
699 SSVAL(cli->outbuf,smb_vwv2,0); /* no additional info */
700 SSVAL(cli->outbuf,smb_vwv3,accessmode);
701 SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
702 SSVAL(cli->outbuf,smb_vwv5,0);
703 SSVAL(cli->outbuf,smb_vwv8,openfn);
705 if (cli->use_oplocks) {
706 /* if using oplocks then ask for a batch oplock via
707 core and extended methods */
708 SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
709 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
710 SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
713 p = smb_buf(cli->outbuf);
714 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
716 cli_setup_bcc(cli, p);
719 if (!cli_receive_smb(cli)) {
723 if (cli_is_error(cli)) {
727 return SVAL(cli->inbuf,smb_vwv2);
730 /****************************************************************************
732 ****************************************************************************/
734 BOOL cli_close(struct cli_state *cli, int fnum)
736 memset(cli->outbuf,'\0',smb_size);
737 memset(cli->inbuf,'\0',smb_size);
739 set_message(cli->outbuf,3,0,True);
741 SCVAL(cli->outbuf,smb_com,SMBclose);
742 SSVAL(cli->outbuf,smb_tid,cli->cnum);
743 cli_setup_packet(cli);
745 SSVAL(cli->outbuf,smb_vwv0,fnum);
746 SIVALS(cli->outbuf,smb_vwv1,-1);
749 if (!cli_receive_smb(cli)) {
753 return !cli_is_error(cli);
757 /****************************************************************************
758 send a lock with a specified locktype
759 this is used for testing LOCKING_ANDX_CANCEL_LOCK
760 ****************************************************************************/
761 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
762 uint32 offset, uint32 len, int timeout, unsigned char locktype)
765 int saved_timeout = cli->timeout;
767 memset(cli->outbuf,'\0',smb_size);
768 memset(cli->inbuf,'\0', smb_size);
770 set_message(cli->outbuf,8,0,True);
772 SCVAL(cli->outbuf,smb_com,SMBlockingX);
773 SSVAL(cli->outbuf,smb_tid,cli->cnum);
774 cli_setup_packet(cli);
776 SCVAL(cli->outbuf,smb_vwv0,0xFF);
777 SSVAL(cli->outbuf,smb_vwv2,fnum);
778 SCVAL(cli->outbuf,smb_vwv3,locktype);
779 SIVALS(cli->outbuf, smb_vwv4, timeout);
780 SSVAL(cli->outbuf,smb_vwv6,0);
781 SSVAL(cli->outbuf,smb_vwv7,1);
783 p = smb_buf(cli->outbuf);
784 SSVAL(p, 0, cli->pid);
790 cli_setup_bcc(cli, p);
795 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
798 if (!cli_receive_smb(cli)) {
799 cli->timeout = saved_timeout;
800 return NT_STATUS_UNSUCCESSFUL;
803 cli->timeout = saved_timeout;
805 return cli_nt_error(cli);
809 /****************************************************************************
811 note that timeout is in units of 2 milliseconds
812 ****************************************************************************/
813 BOOL cli_lock(struct cli_state *cli, int fnum,
814 uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
817 int saved_timeout = cli->timeout;
819 memset(cli->outbuf,'\0',smb_size);
820 memset(cli->inbuf,'\0', smb_size);
822 set_message(cli->outbuf,8,0,True);
824 SCVAL(cli->outbuf,smb_com,SMBlockingX);
825 SSVAL(cli->outbuf,smb_tid,cli->cnum);
826 cli_setup_packet(cli);
828 SCVAL(cli->outbuf,smb_vwv0,0xFF);
829 SSVAL(cli->outbuf,smb_vwv2,fnum);
830 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
831 SIVALS(cli->outbuf, smb_vwv4, timeout);
832 SSVAL(cli->outbuf,smb_vwv6,0);
833 SSVAL(cli->outbuf,smb_vwv7,1);
835 p = smb_buf(cli->outbuf);
836 SSVAL(p, 0, cli->pid);
842 cli_setup_bcc(cli, p);
847 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
850 if (!cli_receive_smb(cli)) {
851 cli->timeout = saved_timeout;
855 cli->timeout = saved_timeout;
857 if (cli_is_error(cli)) {
864 /****************************************************************************
866 ****************************************************************************/
868 BOOL cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
872 memset(cli->outbuf,'\0',smb_size);
873 memset(cli->inbuf,'\0',smb_size);
875 set_message(cli->outbuf,8,0,True);
877 SCVAL(cli->outbuf,smb_com,SMBlockingX);
878 SSVAL(cli->outbuf,smb_tid,cli->cnum);
879 cli_setup_packet(cli);
881 SCVAL(cli->outbuf,smb_vwv0,0xFF);
882 SSVAL(cli->outbuf,smb_vwv2,fnum);
883 SCVAL(cli->outbuf,smb_vwv3,0);
884 SIVALS(cli->outbuf, smb_vwv4, 0);
885 SSVAL(cli->outbuf,smb_vwv6,1);
886 SSVAL(cli->outbuf,smb_vwv7,0);
888 p = smb_buf(cli->outbuf);
889 SSVAL(p, 0, cli->pid);
893 cli_setup_bcc(cli, p);
895 if (!cli_receive_smb(cli)) {
899 if (cli_is_error(cli)) {
906 /****************************************************************************
907 Lock a file with 64 bit offsets.
908 ****************************************************************************/
910 BOOL cli_lock64(struct cli_state *cli, int fnum,
911 SMB_BIG_UINT offset, SMB_BIG_UINT len, int timeout, enum brl_type lock_type)
914 int saved_timeout = cli->timeout;
917 if (! (cli->capabilities & CAP_LARGE_FILES)) {
918 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
921 ltype = (lock_type == READ_LOCK? 1 : 0);
922 ltype |= LOCKING_ANDX_LARGE_FILES;
924 memset(cli->outbuf,'\0',smb_size);
925 memset(cli->inbuf,'\0', smb_size);
927 set_message(cli->outbuf,8,0,True);
929 SCVAL(cli->outbuf,smb_com,SMBlockingX);
930 SSVAL(cli->outbuf,smb_tid,cli->cnum);
931 cli_setup_packet(cli);
933 SCVAL(cli->outbuf,smb_vwv0,0xFF);
934 SSVAL(cli->outbuf,smb_vwv2,fnum);
935 SCVAL(cli->outbuf,smb_vwv3,ltype);
936 SIVALS(cli->outbuf, smb_vwv4, timeout);
937 SSVAL(cli->outbuf,smb_vwv6,0);
938 SSVAL(cli->outbuf,smb_vwv7,1);
940 p = smb_buf(cli->outbuf);
941 SIVAL(p, 0, cli->pid);
942 SOFF_T_R(p, 4, offset);
943 SOFF_T_R(p, 12, len);
946 cli_setup_bcc(cli, p);
950 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
953 if (!cli_receive_smb(cli)) {
954 cli->timeout = saved_timeout;
958 cli->timeout = saved_timeout;
960 if (cli_is_error(cli)) {
967 /****************************************************************************
968 Unlock a file with 64 bit offsets.
969 ****************************************************************************/
971 BOOL cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
975 if (! (cli->capabilities & CAP_LARGE_FILES)) {
976 return cli_unlock(cli, fnum, offset, len);
979 memset(cli->outbuf,'\0',smb_size);
980 memset(cli->inbuf,'\0',smb_size);
982 set_message(cli->outbuf,8,0,True);
984 SCVAL(cli->outbuf,smb_com,SMBlockingX);
985 SSVAL(cli->outbuf,smb_tid,cli->cnum);
986 cli_setup_packet(cli);
988 SCVAL(cli->outbuf,smb_vwv0,0xFF);
989 SSVAL(cli->outbuf,smb_vwv2,fnum);
990 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
991 SIVALS(cli->outbuf, smb_vwv4, 0);
992 SSVAL(cli->outbuf,smb_vwv6,1);
993 SSVAL(cli->outbuf,smb_vwv7,0);
995 p = smb_buf(cli->outbuf);
996 SIVAL(p, 0, cli->pid);
997 SOFF_T_R(p, 4, offset);
998 SOFF_T_R(p, 12, len);
1000 cli_setup_bcc(cli, p);
1002 if (!cli_receive_smb(cli)) {
1006 if (cli_is_error(cli)) {
1014 /****************************************************************************
1015 Do a SMBgetattrE call.
1016 ****************************************************************************/
1018 BOOL cli_getattrE(struct cli_state *cli, int fd,
1019 uint16 *attr, SMB_BIG_UINT *size,
1020 time_t *c_time, time_t *a_time, time_t *m_time)
1022 memset(cli->outbuf,'\0',smb_size);
1023 memset(cli->inbuf,'\0',smb_size);
1025 set_message(cli->outbuf,1,0,True);
1027 SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1028 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1029 cli_setup_packet(cli);
1031 SSVAL(cli->outbuf,smb_vwv0,fd);
1034 if (!cli_receive_smb(cli)) {
1038 if (cli_is_error(cli)) {
1043 *size = IVAL(cli->inbuf, smb_vwv6);
1047 *attr = SVAL(cli->inbuf,smb_vwv10);
1051 *c_time = make_unix_date3(cli->inbuf+smb_vwv0);
1055 *a_time = make_unix_date3(cli->inbuf+smb_vwv2);
1059 *m_time = make_unix_date3(cli->inbuf+smb_vwv4);
1065 /****************************************************************************
1067 ****************************************************************************/
1069 BOOL cli_getatr(struct cli_state *cli, const char *fname,
1070 uint16 *attr, size_t *size, time_t *t)
1074 memset(cli->outbuf,'\0',smb_size);
1075 memset(cli->inbuf,'\0',smb_size);
1077 set_message(cli->outbuf,0,0,True);
1079 SCVAL(cli->outbuf,smb_com,SMBgetatr);
1080 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1081 cli_setup_packet(cli);
1083 p = smb_buf(cli->outbuf);
1085 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
1087 cli_setup_bcc(cli, p);
1090 if (!cli_receive_smb(cli)) {
1094 if (cli_is_error(cli)) {
1099 *size = IVAL(cli->inbuf, smb_vwv3);
1103 *t = make_unix_date3(cli->inbuf+smb_vwv1);
1107 *attr = SVAL(cli->inbuf,smb_vwv0);
1114 /****************************************************************************
1115 Do a SMBsetattrE call.
1116 ****************************************************************************/
1118 BOOL cli_setattrE(struct cli_state *cli, int fd,
1119 time_t c_time, time_t a_time, time_t m_time)
1124 memset(cli->outbuf,'\0',smb_size);
1125 memset(cli->inbuf,'\0',smb_size);
1127 set_message(cli->outbuf,7,0,True);
1129 SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1130 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1131 cli_setup_packet(cli);
1133 SSVAL(cli->outbuf,smb_vwv0, fd);
1134 put_dos_date3(cli->outbuf,smb_vwv1, c_time);
1135 put_dos_date3(cli->outbuf,smb_vwv3, a_time);
1136 put_dos_date3(cli->outbuf,smb_vwv5, m_time);
1138 p = smb_buf(cli->outbuf);
1141 cli_setup_bcc(cli, p);
1144 if (!cli_receive_smb(cli)) {
1148 if (cli_is_error(cli)) {
1155 /****************************************************************************
1156 Do a SMBsetatr call.
1157 ****************************************************************************/
1159 BOOL cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1163 memset(cli->outbuf,'\0',smb_size);
1164 memset(cli->inbuf,'\0',smb_size);
1166 set_message(cli->outbuf,8,0,True);
1168 SCVAL(cli->outbuf,smb_com,SMBsetatr);
1169 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1170 cli_setup_packet(cli);
1172 SSVAL(cli->outbuf,smb_vwv0, attr);
1173 put_dos_date3(cli->outbuf,smb_vwv1, t);
1175 p = smb_buf(cli->outbuf);
1177 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
1180 cli_setup_bcc(cli, p);
1183 if (!cli_receive_smb(cli)) {
1187 if (cli_is_error(cli)) {
1194 /****************************************************************************
1195 Check for existance of a dir.
1196 ****************************************************************************/
1197 BOOL cli_chkpath(struct cli_state *cli, const char *path)
1202 pstrcpy(path2,path);
1203 trim_char(path2,'\0','\\');
1207 memset(cli->outbuf,'\0',smb_size);
1208 set_message(cli->outbuf,0,0,True);
1209 SCVAL(cli->outbuf,smb_com,SMBchkpth);
1210 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1211 cli_setup_packet(cli);
1212 p = smb_buf(cli->outbuf);
1214 p += clistr_push(cli, p, path2, -1, STR_TERMINATE);
1216 cli_setup_bcc(cli, p);
1219 if (!cli_receive_smb(cli)) {
1223 if (cli_is_error(cli)) return False;
1228 /****************************************************************************
1230 ****************************************************************************/
1232 BOOL cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1234 memset(cli->outbuf,'\0',smb_size);
1235 set_message(cli->outbuf,0,0,True);
1236 SCVAL(cli->outbuf,smb_com,SMBdskattr);
1237 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1238 cli_setup_packet(cli);
1241 if (!cli_receive_smb(cli)) {
1245 *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1246 *total = SVAL(cli->inbuf,smb_vwv0);
1247 *avail = SVAL(cli->inbuf,smb_vwv3);
1252 /****************************************************************************
1253 Create and open a temporary file.
1254 ****************************************************************************/
1256 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1261 memset(cli->outbuf,'\0',smb_size);
1262 memset(cli->inbuf,'\0',smb_size);
1264 set_message(cli->outbuf,3,0,True);
1266 SCVAL(cli->outbuf,smb_com,SMBctemp);
1267 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1268 cli_setup_packet(cli);
1270 SSVAL(cli->outbuf,smb_vwv0,0);
1271 SIVALS(cli->outbuf,smb_vwv1,-1);
1273 p = smb_buf(cli->outbuf);
1275 p += clistr_push(cli, p, path, -1, STR_TERMINATE);
1277 cli_setup_bcc(cli, p);
1280 if (!cli_receive_smb(cli)) {
1284 if (cli_is_error(cli)) {
1288 /* despite the spec, the result has a -1, followed by
1289 length, followed by name */
1290 p = smb_buf(cli->inbuf);
1292 len = smb_buflen(cli->inbuf) - 4;
1293 if (len <= 0) return -1;
1297 clistr_pull(cli, path2, p,
1298 sizeof(path2), len, STR_ASCII);
1299 *tmp_path = strdup(path2);
1302 return SVAL(cli->inbuf,smb_vwv0);
1307 send a raw ioctl - used by the torture code
1309 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1311 memset(cli->outbuf,'\0',smb_size);
1312 memset(cli->inbuf,'\0',smb_size);
1314 set_message(cli->outbuf, 3, 0, True);
1315 SCVAL(cli->outbuf,smb_com,SMBioctl);
1316 cli_setup_packet(cli);
1318 SSVAL(cli->outbuf, smb_vwv0, fnum);
1319 SSVAL(cli->outbuf, smb_vwv1, code>>16);
1320 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1323 if (!cli_receive_smb(cli)) {
1324 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1327 if (cli_is_error(cli)) {
1328 return cli_nt_error(cli);
1331 *blob = data_blob(NULL, 0);
1333 return NT_STATUS_OK;
1336 /*********************************************************
1337 Set an extended attribute utility fn.
1338 *********************************************************/
1340 static BOOL cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
1341 const char *ea_name, const char *ea_val, size_t ea_len)
1343 unsigned int data_len = 0;
1345 char *rparam=NULL, *rdata=NULL;
1347 size_t ea_namelen = strlen(ea_name);
1349 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
1350 data = malloc(data_len);
1355 SIVAL(p,0,data_len);
1357 SCVAL(p, 0, 0); /* EA flags. */
1358 SCVAL(p, 1, ea_namelen);
1359 SSVAL(p, 2, ea_len);
1360 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
1361 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
1363 if (!cli_send_trans(cli, SMBtrans2,
1365 -1, 0, /* fid, flags */
1366 &setup, 1, 0, /* setup, length, max */
1367 param, param_len, 2, /* param, length, max */
1368 data, data_len, cli->max_xmit /* data, length, max */
1373 if (!cli_receive_trans(cli, SMBtrans2,
1374 &rparam, ¶m_len,
1375 &rdata, &data_len)) {
1386 /*********************************************************
1387 Set an extended attribute on a pathname.
1388 *********************************************************/
1390 BOOL cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
1392 uint16 setup = TRANSACT2_SETPATHINFO;
1393 unsigned int param_len = 0;
1394 char param[sizeof(pstring)+6];
1395 size_t srclen = 2*(strlen(path)+1);
1398 memset(param, 0, sizeof(param));
1399 SSVAL(param,0,SMB_INFO_SET_EA);
1402 p += clistr_push(cli, p, path, MIN(srclen, sizeof(param)-6), STR_TERMINATE);
1403 param_len = PTR_DIFF(p, param);
1405 return cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
1408 /*********************************************************
1409 Set an extended attribute on an fnum.
1410 *********************************************************/
1412 BOOL cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
1415 uint16 setup = TRANSACT2_SETFILEINFO;
1417 memset(param, 0, 6);
1418 SSVAL(param,0,fnum);
1419 SSVAL(param,2,SMB_INFO_SET_EA);
1421 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
1424 /*********************************************************
1425 Get an extended attribute list tility fn.
1426 *********************************************************/
1428 static BOOL cli_get_ea_list(struct cli_state *cli,
1429 uint16 setup, char *param, unsigned int param_len,
1432 struct ea_struct **pea_list)
1434 unsigned int data_len = 0;
1435 unsigned int rparam_len, rdata_len;
1436 char *rparam=NULL, *rdata=NULL;
1441 struct ea_struct *ea_list;
1446 if (!cli_send_trans(cli, SMBtrans2,
1448 -1, 0, /* fid, flags */
1449 &setup, 1, 0, /* setup, length, max */
1450 param, param_len, 10, /* param, length, max */
1451 NULL, data_len, cli->max_xmit /* data, length, max */
1456 if (!cli_receive_trans(cli, SMBtrans2,
1457 &rparam, &rparam_len,
1458 &rdata, &rdata_len)) {
1462 if (!rdata || rdata_len < 4) {
1466 ea_size = (size_t)IVAL(rdata,0);
1467 if (ea_size > rdata_len) {
1472 /* No EA's present. */
1480 /* Validate the EA list and count it. */
1481 for (num_eas = 0; ea_size >= 4; num_eas++) {
1482 unsigned int ea_namelen = CVAL(p,1);
1483 unsigned int ea_valuelen = SVAL(p,2);
1484 if (ea_namelen == 0) {
1487 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
1490 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
1491 p += 4 + ea_namelen + 1 + ea_valuelen;
1499 *pnum_eas = num_eas;
1501 /* Caller only wants number of EA's. */
1506 ea_list = (struct ea_struct *)talloc(ctx, num_eas*sizeof(struct ea_struct));
1511 ea_size = (size_t)IVAL(rdata,0);
1514 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
1515 struct ea_struct *ea = &ea_list[num_eas];
1516 fstring unix_ea_name;
1517 unsigned int ea_namelen = CVAL(p,1);
1518 unsigned int ea_valuelen = SVAL(p,2);
1520 ea->flags = CVAL(p,0);
1521 unix_ea_name[0] = '\0';
1522 pull_ascii_fstring(unix_ea_name, p + 4);
1523 ea->name = talloc_strdup(ctx, unix_ea_name);
1524 /* Ensure the value is null terminated (in case it's a string). */
1525 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
1526 if (!ea->value.data) {
1530 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
1532 ea->value.data[ea_valuelen] = 0;
1534 p += 4 + ea_namelen + 1 + ea_valuelen;
1537 *pea_list = ea_list;
1547 /*********************************************************
1548 Get an extended attribute list from a pathname.
1549 *********************************************************/
1551 BOOL cli_get_ea_list_path(struct cli_state *cli, const char *path,
1554 struct ea_struct **pea_list)
1556 uint16 setup = TRANSACT2_QPATHINFO;
1557 unsigned int param_len = 0;
1558 char param[sizeof(pstring)+6];
1563 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
1565 p += clistr_push(cli, p, path, sizeof(pstring)-6, STR_TERMINATE);
1566 param_len = PTR_DIFF(p, param);
1568 return cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
1571 /*********************************************************
1572 Get an extended attribute list from an fnum.
1573 *********************************************************/
1575 BOOL cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
1578 struct ea_struct **pea_list)
1580 uint16 setup = TRANSACT2_QFILEINFO;
1583 memset(param, 0, 6);
1584 SSVAL(param,0,fnum);
1585 SSVAL(param,2,SMB_INFO_SET_EA);
1587 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);