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 Do a POSIX getfacl (UNIX extensions).
172 ****************************************************************************/
174 BOOL cli_unix_getfacl(struct cli_state *cli, const char *name, char **retbuf)
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;
185 SSVAL(p, 0, SMB_QUERY_POSIX_ACL);
187 p += clistr_push(cli, p, name, sizeof(pstring)-6, STR_TERMINATE);
188 param_len = PTR_DIFF(p, param);
190 if (!cli_send_trans(cli, SMBtrans2,
192 -1, 0, /* fid, flags */
193 &setup, 1, 0, /* setup, length, max */
194 param, param_len, 2, /* param, length, max */
195 NULL, 0, cli->max_xmit /* data, length, max */
200 if (!cli_receive_trans(cli, SMBtrans2,
202 &rdata, &data_len)) {
218 /****************************************************************************
219 Stat a file (UNIX extensions).
220 ****************************************************************************/
222 BOOL cli_unix_stat(struct cli_state *cli, const char *name, SMB_STRUCT_STAT *sbuf)
224 unsigned int param_len = 0;
225 unsigned int data_len = 0;
226 uint16 setup = TRANSACT2_QPATHINFO;
227 char param[sizeof(pstring)+6];
228 char *rparam=NULL, *rdata=NULL;
235 SSVAL(p, 0, SMB_QUERY_FILE_UNIX_BASIC);
237 p += clistr_push(cli, p, name, sizeof(pstring)-6, STR_TERMINATE);
238 param_len = PTR_DIFF(p, param);
240 if (!cli_send_trans(cli, SMBtrans2,
242 -1, 0, /* fid, flags */
243 &setup, 1, 0, /* setup, length, max */
244 param, param_len, 2, /* param, length, max */
245 NULL, 0, cli->max_xmit /* data, length, max */
250 if (!cli_receive_trans(cli, SMBtrans2,
252 &rdata, &data_len)) {
262 sbuf->st_size = IVAL2_TO_SMB_BIG_UINT(rdata,0); /* total size, in bytes */
263 sbuf->st_blocks = IVAL2_TO_SMB_BIG_UINT(rdata,8); /* number of blocks allocated */
264 sbuf->st_blocks /= STAT_ST_BLOCKSIZE;
265 sbuf->st_ctime = interpret_long_date(rdata + 16); /* time of last change */
266 sbuf->st_atime = interpret_long_date(rdata + 24); /* time of last access */
267 sbuf->st_mtime = interpret_long_date(rdata + 32); /* time of last modification */
268 sbuf->st_uid = IVAL(rdata,40); /* user ID of owner */
269 sbuf->st_gid = IVAL(rdata,48); /* group ID of owner */
270 sbuf->st_mode |= unix_filetype_from_wire(IVAL(rdata, 56));
271 #if defined(HAVE_MAKEDEV)
273 uint32 dev_major = IVAL(rdata,60);
274 uint32 dev_minor = IVAL(rdata,68);
275 sbuf->st_rdev = makedev(dev_major, dev_minor);
278 sbuf->st_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(rdata,76); /* inode */
279 sbuf->st_mode |= wire_perms_to_unix(IVAL(rdata,84)); /* protection */
280 sbuf->st_nlink = IVAL(rdata,92); /* number of hard links */
288 /****************************************************************************
289 Symlink a file (UNIX extensions).
290 ****************************************************************************/
292 BOOL cli_unix_symlink(struct cli_state *cli, const char *oldname, const char *newname)
294 return cli_link_internal(cli, oldname, newname, False);
297 /****************************************************************************
298 Hard a file (UNIX extensions).
299 ****************************************************************************/
301 BOOL cli_unix_hardlink(struct cli_state *cli, const char *oldname, const char *newname)
303 return cli_link_internal(cli, oldname, newname, True);
306 /****************************************************************************
307 Chmod or chown a file internal (UNIX extensions).
308 ****************************************************************************/
310 static BOOL cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname, uint32 mode, uint32 uid, uint32 gid)
312 unsigned int data_len = 0;
313 unsigned int param_len = 0;
314 uint16 setup = TRANSACT2_SETPATHINFO;
315 char param[sizeof(pstring)+6];
317 char *rparam=NULL, *rdata=NULL;
320 memset(param, 0, sizeof(param));
321 memset(data, 0, sizeof(data));
322 SSVAL(param,0,SMB_SET_FILE_UNIX_BASIC);
325 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
326 param_len = PTR_DIFF(p, param);
334 if (!cli_send_trans(cli, SMBtrans2,
336 -1, 0, /* fid, flags */
337 &setup, 1, 0, /* setup, length, max */
338 param, param_len, 2, /* param, length, max */
339 (char *)&data, data_len, cli->max_xmit /* data, length, max */
344 if (!cli_receive_trans(cli, SMBtrans2,
346 &rdata, &data_len)) {
356 /****************************************************************************
357 chmod a file (UNIX extensions).
358 ****************************************************************************/
360 BOOL cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
362 return cli_unix_chmod_chown_internal(cli, fname,
363 unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
366 /****************************************************************************
367 chown a file (UNIX extensions).
368 ****************************************************************************/
370 BOOL cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
372 return cli_unix_chmod_chown_internal(cli, fname, SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
375 /****************************************************************************
377 ****************************************************************************/
379 BOOL cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
383 memset(cli->outbuf,'\0',smb_size);
384 memset(cli->inbuf,'\0',smb_size);
386 set_message(cli->outbuf,1, 0, True);
388 SCVAL(cli->outbuf,smb_com,SMBmv);
389 SSVAL(cli->outbuf,smb_tid,cli->cnum);
390 cli_setup_packet(cli);
392 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
394 p = smb_buf(cli->outbuf);
396 p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
398 p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
400 cli_setup_bcc(cli, p);
403 if (!cli_receive_smb(cli))
406 if (cli_is_error(cli))
412 /****************************************************************************
414 ****************************************************************************/
416 BOOL cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
420 memset(cli->outbuf,'\0',smb_size);
421 memset(cli->inbuf,'\0',smb_size);
423 set_message(cli->outbuf, 4, 0, True);
425 SCVAL(cli->outbuf,smb_com,SMBntrename);
426 SSVAL(cli->outbuf,smb_tid,cli->cnum);
427 cli_setup_packet(cli);
429 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
430 SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_RENAME);
432 p = smb_buf(cli->outbuf);
434 p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
436 p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
438 cli_setup_bcc(cli, p);
441 if (!cli_receive_smb(cli))
444 if (cli_is_error(cli))
450 /****************************************************************************
452 ****************************************************************************/
454 BOOL cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
458 memset(cli->outbuf,'\0',smb_size);
459 memset(cli->inbuf,'\0',smb_size);
461 set_message(cli->outbuf, 4, 0, True);
463 SCVAL(cli->outbuf,smb_com,SMBntrename);
464 SSVAL(cli->outbuf,smb_tid,cli->cnum);
465 cli_setup_packet(cli);
467 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
468 SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_HARD_LINK);
470 p = smb_buf(cli->outbuf);
472 p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
474 p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
476 cli_setup_bcc(cli, p);
479 if (!cli_receive_smb(cli))
482 if (cli_is_error(cli))
488 /****************************************************************************
490 ****************************************************************************/
492 BOOL cli_unlink(struct cli_state *cli, const char *fname)
496 memset(cli->outbuf,'\0',smb_size);
497 memset(cli->inbuf,'\0',smb_size);
499 set_message(cli->outbuf,1, 0,True);
501 SCVAL(cli->outbuf,smb_com,SMBunlink);
502 SSVAL(cli->outbuf,smb_tid,cli->cnum);
503 cli_setup_packet(cli);
505 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN);
507 p = smb_buf(cli->outbuf);
509 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
511 cli_setup_bcc(cli, p);
513 if (!cli_receive_smb(cli)) {
517 if (cli_is_error(cli)) {
524 /****************************************************************************
526 ****************************************************************************/
528 BOOL cli_mkdir(struct cli_state *cli, const char *dname)
532 memset(cli->outbuf,'\0',smb_size);
533 memset(cli->inbuf,'\0',smb_size);
535 set_message(cli->outbuf,0, 0,True);
537 SCVAL(cli->outbuf,smb_com,SMBmkdir);
538 SSVAL(cli->outbuf,smb_tid,cli->cnum);
539 cli_setup_packet(cli);
541 p = smb_buf(cli->outbuf);
543 p += clistr_push(cli, p, dname, -1, STR_TERMINATE);
545 cli_setup_bcc(cli, p);
548 if (!cli_receive_smb(cli)) {
552 if (cli_is_error(cli)) {
559 /****************************************************************************
561 ****************************************************************************/
563 BOOL cli_rmdir(struct cli_state *cli, const char *dname)
567 memset(cli->outbuf,'\0',smb_size);
568 memset(cli->inbuf,'\0',smb_size);
570 set_message(cli->outbuf,0, 0, True);
572 SCVAL(cli->outbuf,smb_com,SMBrmdir);
573 SSVAL(cli->outbuf,smb_tid,cli->cnum);
574 cli_setup_packet(cli);
576 p = smb_buf(cli->outbuf);
578 p += clistr_push(cli, p, dname, -1, STR_TERMINATE);
580 cli_setup_bcc(cli, p);
583 if (!cli_receive_smb(cli)) {
587 if (cli_is_error(cli)) {
594 /****************************************************************************
595 Set or clear the delete on close flag.
596 ****************************************************************************/
598 int cli_nt_delete_on_close(struct cli_state *cli, int fnum, BOOL flag)
600 unsigned int data_len = 1;
601 unsigned int param_len = 6;
602 uint16 setup = TRANSACT2_SETFILEINFO;
605 char *rparam=NULL, *rdata=NULL;
607 memset(param, 0, param_len);
609 SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
613 if (!cli_send_trans(cli, SMBtrans2,
615 -1, 0, /* fid, flags */
616 &setup, 1, 0, /* setup, length, max */
617 param, param_len, 2, /* param, length, max */
618 (char *)&data, data_len, cli->max_xmit /* data, length, max */
623 if (!cli_receive_trans(cli, SMBtrans2,
625 &rdata, &data_len)) {
635 /****************************************************************************
636 Open a file - exposing the full horror of the NT API :-).
638 ****************************************************************************/
640 int cli_nt_create_full(struct cli_state *cli, const char *fname,
641 uint32 CreatFlags, uint32 DesiredAccess,
642 uint32 FileAttributes, uint32 ShareAccess,
643 uint32 CreateDisposition, uint32 CreateOptions,
649 memset(cli->outbuf,'\0',smb_size);
650 memset(cli->inbuf,'\0',smb_size);
652 set_message(cli->outbuf,24,0,True);
654 SCVAL(cli->outbuf,smb_com,SMBntcreateX);
655 SSVAL(cli->outbuf,smb_tid,cli->cnum);
656 cli_setup_packet(cli);
658 SSVAL(cli->outbuf,smb_vwv0,0xFF);
659 if (cli->use_oplocks)
660 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
662 SIVAL(cli->outbuf,smb_ntcreate_Flags, CreatFlags);
663 SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
664 SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, DesiredAccess);
665 SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, FileAttributes);
666 SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, ShareAccess);
667 SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, CreateDisposition);
668 SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, CreateOptions);
669 SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
670 SCVAL(cli->outbuf,smb_ntcreate_SecurityFlags, SecuityFlags);
672 p = smb_buf(cli->outbuf);
673 /* this alignment and termination is critical for netapp filers. Don't change */
674 p += clistr_align_out(cli, p, 0);
675 len = clistr_push(cli, p, fname, -1, 0);
677 SSVAL(cli->outbuf,smb_ntcreate_NameLength, len);
678 /* sigh. this copes with broken netapp filer behaviour */
679 p += clistr_push(cli, p, "", -1, STR_TERMINATE);
681 cli_setup_bcc(cli, p);
684 if (!cli_receive_smb(cli)) {
688 if (cli_is_error(cli)) {
692 return SVAL(cli->inbuf,smb_vwv2 + 1);
695 /****************************************************************************
697 ****************************************************************************/
699 int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess)
701 return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
702 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_EXISTS_OPEN, 0x0, 0x0);
705 /****************************************************************************
707 WARNING: if you open with O_WRONLY then getattrE won't work!
708 ****************************************************************************/
710 int cli_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
714 unsigned accessmode=0;
718 if (!(flags & O_EXCL)) {
725 accessmode = (share_mode<<4);
727 if ((flags & O_ACCMODE) == O_RDWR) {
729 } else if ((flags & O_ACCMODE) == O_WRONLY) {
734 if ((flags & O_SYNC) == O_SYNC) {
735 accessmode |= (1<<14);
739 if (share_mode == DENY_FCB) {
743 memset(cli->outbuf,'\0',smb_size);
744 memset(cli->inbuf,'\0',smb_size);
746 set_message(cli->outbuf,15,0,True);
748 SCVAL(cli->outbuf,smb_com,SMBopenX);
749 SSVAL(cli->outbuf,smb_tid,cli->cnum);
750 cli_setup_packet(cli);
752 SSVAL(cli->outbuf,smb_vwv0,0xFF);
753 SSVAL(cli->outbuf,smb_vwv2,0); /* no additional info */
754 SSVAL(cli->outbuf,smb_vwv3,accessmode);
755 SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
756 SSVAL(cli->outbuf,smb_vwv5,0);
757 SSVAL(cli->outbuf,smb_vwv8,openfn);
759 if (cli->use_oplocks) {
760 /* if using oplocks then ask for a batch oplock via
761 core and extended methods */
762 SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
763 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
764 SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
767 p = smb_buf(cli->outbuf);
768 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
770 cli_setup_bcc(cli, p);
773 if (!cli_receive_smb(cli)) {
777 if (cli_is_error(cli)) {
781 return SVAL(cli->inbuf,smb_vwv2);
784 /****************************************************************************
786 ****************************************************************************/
788 BOOL cli_close(struct cli_state *cli, int fnum)
790 memset(cli->outbuf,'\0',smb_size);
791 memset(cli->inbuf,'\0',smb_size);
793 set_message(cli->outbuf,3,0,True);
795 SCVAL(cli->outbuf,smb_com,SMBclose);
796 SSVAL(cli->outbuf,smb_tid,cli->cnum);
797 cli_setup_packet(cli);
799 SSVAL(cli->outbuf,smb_vwv0,fnum);
800 SIVALS(cli->outbuf,smb_vwv1,-1);
803 if (!cli_receive_smb(cli)) {
807 return !cli_is_error(cli);
811 /****************************************************************************
812 send a lock with a specified locktype
813 this is used for testing LOCKING_ANDX_CANCEL_LOCK
814 ****************************************************************************/
815 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
816 uint32 offset, uint32 len, int timeout, unsigned char locktype)
819 int saved_timeout = cli->timeout;
821 memset(cli->outbuf,'\0',smb_size);
822 memset(cli->inbuf,'\0', smb_size);
824 set_message(cli->outbuf,8,0,True);
826 SCVAL(cli->outbuf,smb_com,SMBlockingX);
827 SSVAL(cli->outbuf,smb_tid,cli->cnum);
828 cli_setup_packet(cli);
830 SCVAL(cli->outbuf,smb_vwv0,0xFF);
831 SSVAL(cli->outbuf,smb_vwv2,fnum);
832 SCVAL(cli->outbuf,smb_vwv3,locktype);
833 SIVALS(cli->outbuf, smb_vwv4, timeout);
834 SSVAL(cli->outbuf,smb_vwv6,0);
835 SSVAL(cli->outbuf,smb_vwv7,1);
837 p = smb_buf(cli->outbuf);
838 SSVAL(p, 0, cli->pid);
844 cli_setup_bcc(cli, p);
849 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
852 if (!cli_receive_smb(cli)) {
853 cli->timeout = saved_timeout;
854 return NT_STATUS_UNSUCCESSFUL;
857 cli->timeout = saved_timeout;
859 return cli_nt_error(cli);
863 /****************************************************************************
865 note that timeout is in units of 2 milliseconds
866 ****************************************************************************/
867 BOOL cli_lock(struct cli_state *cli, int fnum,
868 uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
871 int saved_timeout = cli->timeout;
873 memset(cli->outbuf,'\0',smb_size);
874 memset(cli->inbuf,'\0', smb_size);
876 set_message(cli->outbuf,8,0,True);
878 SCVAL(cli->outbuf,smb_com,SMBlockingX);
879 SSVAL(cli->outbuf,smb_tid,cli->cnum);
880 cli_setup_packet(cli);
882 SCVAL(cli->outbuf,smb_vwv0,0xFF);
883 SSVAL(cli->outbuf,smb_vwv2,fnum);
884 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
885 SIVALS(cli->outbuf, smb_vwv4, timeout);
886 SSVAL(cli->outbuf,smb_vwv6,0);
887 SSVAL(cli->outbuf,smb_vwv7,1);
889 p = smb_buf(cli->outbuf);
890 SSVAL(p, 0, cli->pid);
896 cli_setup_bcc(cli, p);
901 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
904 if (!cli_receive_smb(cli)) {
905 cli->timeout = saved_timeout;
909 cli->timeout = saved_timeout;
911 if (cli_is_error(cli)) {
918 /****************************************************************************
920 ****************************************************************************/
922 BOOL cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
926 memset(cli->outbuf,'\0',smb_size);
927 memset(cli->inbuf,'\0',smb_size);
929 set_message(cli->outbuf,8,0,True);
931 SCVAL(cli->outbuf,smb_com,SMBlockingX);
932 SSVAL(cli->outbuf,smb_tid,cli->cnum);
933 cli_setup_packet(cli);
935 SCVAL(cli->outbuf,smb_vwv0,0xFF);
936 SSVAL(cli->outbuf,smb_vwv2,fnum);
937 SCVAL(cli->outbuf,smb_vwv3,0);
938 SIVALS(cli->outbuf, smb_vwv4, 0);
939 SSVAL(cli->outbuf,smb_vwv6,1);
940 SSVAL(cli->outbuf,smb_vwv7,0);
942 p = smb_buf(cli->outbuf);
943 SSVAL(p, 0, cli->pid);
947 cli_setup_bcc(cli, p);
949 if (!cli_receive_smb(cli)) {
953 if (cli_is_error(cli)) {
960 /****************************************************************************
961 Lock a file with 64 bit offsets.
962 ****************************************************************************/
964 BOOL cli_lock64(struct cli_state *cli, int fnum,
965 SMB_BIG_UINT offset, SMB_BIG_UINT len, int timeout, enum brl_type lock_type)
968 int saved_timeout = cli->timeout;
971 if (! (cli->capabilities & CAP_LARGE_FILES)) {
972 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
975 ltype = (lock_type == READ_LOCK? 1 : 0);
976 ltype |= LOCKING_ANDX_LARGE_FILES;
978 memset(cli->outbuf,'\0',smb_size);
979 memset(cli->inbuf,'\0', smb_size);
981 set_message(cli->outbuf,8,0,True);
983 SCVAL(cli->outbuf,smb_com,SMBlockingX);
984 SSVAL(cli->outbuf,smb_tid,cli->cnum);
985 cli_setup_packet(cli);
987 SCVAL(cli->outbuf,smb_vwv0,0xFF);
988 SSVAL(cli->outbuf,smb_vwv2,fnum);
989 SCVAL(cli->outbuf,smb_vwv3,ltype);
990 SIVALS(cli->outbuf, smb_vwv4, timeout);
991 SSVAL(cli->outbuf,smb_vwv6,0);
992 SSVAL(cli->outbuf,smb_vwv7,1);
994 p = smb_buf(cli->outbuf);
995 SIVAL(p, 0, cli->pid);
996 SOFF_T_R(p, 4, offset);
997 SOFF_T_R(p, 12, len);
1000 cli_setup_bcc(cli, p);
1004 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1007 if (!cli_receive_smb(cli)) {
1008 cli->timeout = saved_timeout;
1012 cli->timeout = saved_timeout;
1014 if (cli_is_error(cli)) {
1021 /****************************************************************************
1022 Unlock a file with 64 bit offsets.
1023 ****************************************************************************/
1025 BOOL cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1029 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1030 return cli_unlock(cli, fnum, offset, len);
1033 memset(cli->outbuf,'\0',smb_size);
1034 memset(cli->inbuf,'\0',smb_size);
1036 set_message(cli->outbuf,8,0,True);
1038 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1039 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1040 cli_setup_packet(cli);
1042 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1043 SSVAL(cli->outbuf,smb_vwv2,fnum);
1044 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1045 SIVALS(cli->outbuf, smb_vwv4, 0);
1046 SSVAL(cli->outbuf,smb_vwv6,1);
1047 SSVAL(cli->outbuf,smb_vwv7,0);
1049 p = smb_buf(cli->outbuf);
1050 SIVAL(p, 0, cli->pid);
1051 SOFF_T_R(p, 4, offset);
1052 SOFF_T_R(p, 12, len);
1054 cli_setup_bcc(cli, p);
1056 if (!cli_receive_smb(cli)) {
1060 if (cli_is_error(cli)) {
1068 /****************************************************************************
1069 Do a SMBgetattrE call.
1070 ****************************************************************************/
1072 BOOL cli_getattrE(struct cli_state *cli, int fd,
1073 uint16 *attr, SMB_BIG_UINT *size,
1074 time_t *c_time, time_t *a_time, time_t *m_time)
1076 memset(cli->outbuf,'\0',smb_size);
1077 memset(cli->inbuf,'\0',smb_size);
1079 set_message(cli->outbuf,1,0,True);
1081 SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1082 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1083 cli_setup_packet(cli);
1085 SSVAL(cli->outbuf,smb_vwv0,fd);
1088 if (!cli_receive_smb(cli)) {
1092 if (cli_is_error(cli)) {
1097 *size = IVAL(cli->inbuf, smb_vwv6);
1101 *attr = SVAL(cli->inbuf,smb_vwv10);
1105 *c_time = make_unix_date3(cli->inbuf+smb_vwv0);
1109 *a_time = make_unix_date3(cli->inbuf+smb_vwv2);
1113 *m_time = make_unix_date3(cli->inbuf+smb_vwv4);
1119 /****************************************************************************
1121 ****************************************************************************/
1123 BOOL cli_getatr(struct cli_state *cli, const char *fname,
1124 uint16 *attr, size_t *size, time_t *t)
1128 memset(cli->outbuf,'\0',smb_size);
1129 memset(cli->inbuf,'\0',smb_size);
1131 set_message(cli->outbuf,0,0,True);
1133 SCVAL(cli->outbuf,smb_com,SMBgetatr);
1134 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1135 cli_setup_packet(cli);
1137 p = smb_buf(cli->outbuf);
1139 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
1141 cli_setup_bcc(cli, p);
1144 if (!cli_receive_smb(cli)) {
1148 if (cli_is_error(cli)) {
1153 *size = IVAL(cli->inbuf, smb_vwv3);
1157 *t = make_unix_date3(cli->inbuf+smb_vwv1);
1161 *attr = SVAL(cli->inbuf,smb_vwv0);
1168 /****************************************************************************
1169 Do a SMBsetattrE call.
1170 ****************************************************************************/
1172 BOOL cli_setattrE(struct cli_state *cli, int fd,
1173 time_t c_time, time_t a_time, time_t m_time)
1178 memset(cli->outbuf,'\0',smb_size);
1179 memset(cli->inbuf,'\0',smb_size);
1181 set_message(cli->outbuf,7,0,True);
1183 SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1184 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1185 cli_setup_packet(cli);
1187 SSVAL(cli->outbuf,smb_vwv0, fd);
1188 put_dos_date3(cli->outbuf,smb_vwv1, c_time);
1189 put_dos_date3(cli->outbuf,smb_vwv3, a_time);
1190 put_dos_date3(cli->outbuf,smb_vwv5, m_time);
1192 p = smb_buf(cli->outbuf);
1195 cli_setup_bcc(cli, p);
1198 if (!cli_receive_smb(cli)) {
1202 if (cli_is_error(cli)) {
1209 /****************************************************************************
1210 Do a SMBsetatr call.
1211 ****************************************************************************/
1213 BOOL cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1217 memset(cli->outbuf,'\0',smb_size);
1218 memset(cli->inbuf,'\0',smb_size);
1220 set_message(cli->outbuf,8,0,True);
1222 SCVAL(cli->outbuf,smb_com,SMBsetatr);
1223 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1224 cli_setup_packet(cli);
1226 SSVAL(cli->outbuf,smb_vwv0, attr);
1227 put_dos_date3(cli->outbuf,smb_vwv1, t);
1229 p = smb_buf(cli->outbuf);
1231 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
1234 cli_setup_bcc(cli, p);
1237 if (!cli_receive_smb(cli)) {
1241 if (cli_is_error(cli)) {
1248 /****************************************************************************
1249 Check for existance of a dir.
1250 ****************************************************************************/
1251 BOOL cli_chkpath(struct cli_state *cli, const char *path)
1256 pstrcpy(path2,path);
1257 trim_char(path2,'\0','\\');
1261 memset(cli->outbuf,'\0',smb_size);
1262 set_message(cli->outbuf,0,0,True);
1263 SCVAL(cli->outbuf,smb_com,SMBchkpth);
1264 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1265 cli_setup_packet(cli);
1266 p = smb_buf(cli->outbuf);
1268 p += clistr_push(cli, p, path2, -1, STR_TERMINATE);
1270 cli_setup_bcc(cli, p);
1273 if (!cli_receive_smb(cli)) {
1277 if (cli_is_error(cli)) return False;
1282 /****************************************************************************
1284 ****************************************************************************/
1286 BOOL cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1288 memset(cli->outbuf,'\0',smb_size);
1289 set_message(cli->outbuf,0,0,True);
1290 SCVAL(cli->outbuf,smb_com,SMBdskattr);
1291 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1292 cli_setup_packet(cli);
1295 if (!cli_receive_smb(cli)) {
1299 *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1300 *total = SVAL(cli->inbuf,smb_vwv0);
1301 *avail = SVAL(cli->inbuf,smb_vwv3);
1306 /****************************************************************************
1307 Create and open a temporary file.
1308 ****************************************************************************/
1310 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1315 memset(cli->outbuf,'\0',smb_size);
1316 memset(cli->inbuf,'\0',smb_size);
1318 set_message(cli->outbuf,3,0,True);
1320 SCVAL(cli->outbuf,smb_com,SMBctemp);
1321 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1322 cli_setup_packet(cli);
1324 SSVAL(cli->outbuf,smb_vwv0,0);
1325 SIVALS(cli->outbuf,smb_vwv1,-1);
1327 p = smb_buf(cli->outbuf);
1329 p += clistr_push(cli, p, path, -1, STR_TERMINATE);
1331 cli_setup_bcc(cli, p);
1334 if (!cli_receive_smb(cli)) {
1338 if (cli_is_error(cli)) {
1342 /* despite the spec, the result has a -1, followed by
1343 length, followed by name */
1344 p = smb_buf(cli->inbuf);
1346 len = smb_buflen(cli->inbuf) - 4;
1347 if (len <= 0) return -1;
1351 clistr_pull(cli, path2, p,
1352 sizeof(path2), len, STR_ASCII);
1353 *tmp_path = strdup(path2);
1356 return SVAL(cli->inbuf,smb_vwv0);
1361 send a raw ioctl - used by the torture code
1363 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1365 memset(cli->outbuf,'\0',smb_size);
1366 memset(cli->inbuf,'\0',smb_size);
1368 set_message(cli->outbuf, 3, 0, True);
1369 SCVAL(cli->outbuf,smb_com,SMBioctl);
1370 cli_setup_packet(cli);
1372 SSVAL(cli->outbuf, smb_vwv0, fnum);
1373 SSVAL(cli->outbuf, smb_vwv1, code>>16);
1374 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1377 if (!cli_receive_smb(cli)) {
1378 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1381 if (cli_is_error(cli)) {
1382 return cli_nt_error(cli);
1385 *blob = data_blob(NULL, 0);
1387 return NT_STATUS_OK;
1390 /*********************************************************
1391 Set an extended attribute utility fn.
1392 *********************************************************/
1394 static BOOL cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
1395 const char *ea_name, const char *ea_val, size_t ea_len)
1397 unsigned int data_len = 0;
1399 char *rparam=NULL, *rdata=NULL;
1401 size_t ea_namelen = strlen(ea_name);
1403 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
1404 data = malloc(data_len);
1409 SIVAL(p,0,data_len);
1411 SCVAL(p, 0, 0); /* EA flags. */
1412 SCVAL(p, 1, ea_namelen);
1413 SSVAL(p, 2, ea_len);
1414 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
1415 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
1417 if (!cli_send_trans(cli, SMBtrans2,
1419 -1, 0, /* fid, flags */
1420 &setup, 1, 0, /* setup, length, max */
1421 param, param_len, 2, /* param, length, max */
1422 data, data_len, cli->max_xmit /* data, length, max */
1427 if (!cli_receive_trans(cli, SMBtrans2,
1428 &rparam, ¶m_len,
1429 &rdata, &data_len)) {
1440 /*********************************************************
1441 Set an extended attribute on a pathname.
1442 *********************************************************/
1444 BOOL cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
1446 uint16 setup = TRANSACT2_SETPATHINFO;
1447 unsigned int param_len = 0;
1448 char param[sizeof(pstring)+6];
1449 size_t srclen = 2*(strlen(path)+1);
1452 memset(param, 0, sizeof(param));
1453 SSVAL(param,0,SMB_INFO_SET_EA);
1456 p += clistr_push(cli, p, path, MIN(srclen, sizeof(param)-6), STR_TERMINATE);
1457 param_len = PTR_DIFF(p, param);
1459 return cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
1462 /*********************************************************
1463 Set an extended attribute on an fnum.
1464 *********************************************************/
1466 BOOL cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
1469 uint16 setup = TRANSACT2_SETFILEINFO;
1471 memset(param, 0, 6);
1472 SSVAL(param,0,fnum);
1473 SSVAL(param,2,SMB_INFO_SET_EA);
1475 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
1478 /*********************************************************
1479 Get an extended attribute list tility fn.
1480 *********************************************************/
1482 static BOOL cli_get_ea_list(struct cli_state *cli,
1483 uint16 setup, char *param, unsigned int param_len,
1486 struct ea_struct **pea_list)
1488 unsigned int data_len = 0;
1489 unsigned int rparam_len, rdata_len;
1490 char *rparam=NULL, *rdata=NULL;
1495 struct ea_struct *ea_list;
1500 if (!cli_send_trans(cli, SMBtrans2,
1502 -1, 0, /* fid, flags */
1503 &setup, 1, 0, /* setup, length, max */
1504 param, param_len, 10, /* param, length, max */
1505 NULL, data_len, cli->max_xmit /* data, length, max */
1510 if (!cli_receive_trans(cli, SMBtrans2,
1511 &rparam, &rparam_len,
1512 &rdata, &rdata_len)) {
1516 if (!rdata || rdata_len < 4) {
1520 ea_size = (size_t)IVAL(rdata,0);
1521 if (ea_size > rdata_len) {
1526 /* No EA's present. */
1534 /* Validate the EA list and count it. */
1535 for (num_eas = 0; ea_size >= 4; num_eas++) {
1536 unsigned int ea_namelen = CVAL(p,1);
1537 unsigned int ea_valuelen = SVAL(p,2);
1538 if (ea_namelen == 0) {
1541 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
1544 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
1545 p += 4 + ea_namelen + 1 + ea_valuelen;
1553 *pnum_eas = num_eas;
1555 /* Caller only wants number of EA's. */
1560 ea_list = (struct ea_struct *)talloc(ctx, num_eas*sizeof(struct ea_struct));
1565 ea_size = (size_t)IVAL(rdata,0);
1568 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
1569 struct ea_struct *ea = &ea_list[num_eas];
1570 fstring unix_ea_name;
1571 unsigned int ea_namelen = CVAL(p,1);
1572 unsigned int ea_valuelen = SVAL(p,2);
1574 ea->flags = CVAL(p,0);
1575 unix_ea_name[0] = '\0';
1576 pull_ascii_fstring(unix_ea_name, p + 4);
1577 ea->name = talloc_strdup(ctx, unix_ea_name);
1578 /* Ensure the value is null terminated (in case it's a string). */
1579 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
1580 if (!ea->value.data) {
1584 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
1586 ea->value.data[ea_valuelen] = 0;
1588 p += 4 + ea_namelen + 1 + ea_valuelen;
1591 *pea_list = ea_list;
1601 /*********************************************************
1602 Get an extended attribute list from a pathname.
1603 *********************************************************/
1605 BOOL cli_get_ea_list_path(struct cli_state *cli, const char *path,
1608 struct ea_struct **pea_list)
1610 uint16 setup = TRANSACT2_QPATHINFO;
1611 unsigned int param_len = 0;
1612 char param[sizeof(pstring)+6];
1617 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
1619 p += clistr_push(cli, p, path, sizeof(pstring)-6, STR_TERMINATE);
1620 param_len = PTR_DIFF(p, param);
1622 return cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
1625 /*********************************************************
1626 Get an extended attribute list from an fnum.
1627 *********************************************************/
1629 BOOL cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
1632 struct ea_struct **pea_list)
1634 uint16 setup = TRANSACT2_QFILEINFO;
1637 memset(param, 0, 6);
1638 SSVAL(param,0,fnum);
1639 SSVAL(param,2,SMB_INFO_SET_EA);
1641 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);