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, size_t *prb_size, 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)) {
214 *prb_size = (size_t)data_len;
219 /****************************************************************************
220 Stat a file (UNIX extensions).
221 ****************************************************************************/
223 BOOL cli_unix_stat(struct cli_state *cli, const char *name, SMB_STRUCT_STAT *sbuf)
225 unsigned int param_len = 0;
226 unsigned int data_len = 0;
227 uint16 setup = TRANSACT2_QPATHINFO;
228 char param[sizeof(pstring)+6];
229 char *rparam=NULL, *rdata=NULL;
236 SSVAL(p, 0, SMB_QUERY_FILE_UNIX_BASIC);
238 p += clistr_push(cli, p, name, sizeof(pstring)-6, STR_TERMINATE);
239 param_len = PTR_DIFF(p, param);
241 if (!cli_send_trans(cli, SMBtrans2,
243 -1, 0, /* fid, flags */
244 &setup, 1, 0, /* setup, length, max */
245 param, param_len, 2, /* param, length, max */
246 NULL, 0, cli->max_xmit /* data, length, max */
251 if (!cli_receive_trans(cli, SMBtrans2,
253 &rdata, &data_len)) {
263 sbuf->st_size = IVAL2_TO_SMB_BIG_UINT(rdata,0); /* total size, in bytes */
264 sbuf->st_blocks = IVAL2_TO_SMB_BIG_UINT(rdata,8); /* number of blocks allocated */
265 sbuf->st_blocks /= STAT_ST_BLOCKSIZE;
266 sbuf->st_ctime = interpret_long_date(rdata + 16); /* time of last change */
267 sbuf->st_atime = interpret_long_date(rdata + 24); /* time of last access */
268 sbuf->st_mtime = interpret_long_date(rdata + 32); /* time of last modification */
269 sbuf->st_uid = (uid_t) IVAL(rdata,40); /* user ID of owner */
270 sbuf->st_gid = (gid_t) IVAL(rdata,48); /* group ID of owner */
271 sbuf->st_mode |= unix_filetype_from_wire(IVAL(rdata, 56));
272 #if defined(HAVE_MAKEDEV)
274 uint32 dev_major = IVAL(rdata,60);
275 uint32 dev_minor = IVAL(rdata,68);
276 sbuf->st_rdev = makedev(dev_major, dev_minor);
279 sbuf->st_ino = (SMB_INO_T)IVAL2_TO_SMB_BIG_UINT(rdata,76); /* inode */
280 sbuf->st_mode |= wire_perms_to_unix(IVAL(rdata,84)); /* protection */
281 sbuf->st_nlink = IVAL(rdata,92); /* number of hard links */
289 /****************************************************************************
290 Symlink a file (UNIX extensions).
291 ****************************************************************************/
293 BOOL cli_unix_symlink(struct cli_state *cli, const char *oldname, const char *newname)
295 return cli_link_internal(cli, oldname, newname, False);
298 /****************************************************************************
299 Hard a file (UNIX extensions).
300 ****************************************************************************/
302 BOOL cli_unix_hardlink(struct cli_state *cli, const char *oldname, const char *newname)
304 return cli_link_internal(cli, oldname, newname, True);
307 /****************************************************************************
308 Chmod or chown a file internal (UNIX extensions).
309 ****************************************************************************/
311 static BOOL cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname, uint32 mode, uint32 uid, uint32 gid)
313 unsigned int data_len = 0;
314 unsigned int param_len = 0;
315 uint16 setup = TRANSACT2_SETPATHINFO;
316 char param[sizeof(pstring)+6];
318 char *rparam=NULL, *rdata=NULL;
321 memset(param, 0, sizeof(param));
322 memset(data, 0, sizeof(data));
323 SSVAL(param,0,SMB_SET_FILE_UNIX_BASIC);
326 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
327 param_len = PTR_DIFF(p, param);
335 if (!cli_send_trans(cli, SMBtrans2,
337 -1, 0, /* fid, flags */
338 &setup, 1, 0, /* setup, length, max */
339 param, param_len, 2, /* param, length, max */
340 (char *)&data, data_len, cli->max_xmit /* data, length, max */
345 if (!cli_receive_trans(cli, SMBtrans2,
347 &rdata, &data_len)) {
357 /****************************************************************************
358 chmod a file (UNIX extensions).
359 ****************************************************************************/
361 BOOL cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
363 return cli_unix_chmod_chown_internal(cli, fname,
364 unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
367 /****************************************************************************
368 chown a file (UNIX extensions).
369 ****************************************************************************/
371 BOOL cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
373 return cli_unix_chmod_chown_internal(cli, fname, SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
376 /****************************************************************************
378 ****************************************************************************/
380 BOOL cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
384 memset(cli->outbuf,'\0',smb_size);
385 memset(cli->inbuf,'\0',smb_size);
387 set_message(cli->outbuf,1, 0, True);
389 SCVAL(cli->outbuf,smb_com,SMBmv);
390 SSVAL(cli->outbuf,smb_tid,cli->cnum);
391 cli_setup_packet(cli);
393 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
395 p = smb_buf(cli->outbuf);
397 p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
399 p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
401 cli_setup_bcc(cli, p);
404 if (!cli_receive_smb(cli))
407 if (cli_is_error(cli))
413 /****************************************************************************
415 ****************************************************************************/
417 BOOL cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
421 memset(cli->outbuf,'\0',smb_size);
422 memset(cli->inbuf,'\0',smb_size);
424 set_message(cli->outbuf, 4, 0, True);
426 SCVAL(cli->outbuf,smb_com,SMBntrename);
427 SSVAL(cli->outbuf,smb_tid,cli->cnum);
428 cli_setup_packet(cli);
430 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
431 SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_RENAME);
433 p = smb_buf(cli->outbuf);
435 p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
437 p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
439 cli_setup_bcc(cli, p);
442 if (!cli_receive_smb(cli))
445 if (cli_is_error(cli))
451 /****************************************************************************
453 ****************************************************************************/
455 BOOL cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
459 memset(cli->outbuf,'\0',smb_size);
460 memset(cli->inbuf,'\0',smb_size);
462 set_message(cli->outbuf, 4, 0, True);
464 SCVAL(cli->outbuf,smb_com,SMBntrename);
465 SSVAL(cli->outbuf,smb_tid,cli->cnum);
466 cli_setup_packet(cli);
468 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
469 SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_HARD_LINK);
471 p = smb_buf(cli->outbuf);
473 p += clistr_push(cli, p, fname_src, -1, STR_TERMINATE);
475 p += clistr_push(cli, p, fname_dst, -1, STR_TERMINATE);
477 cli_setup_bcc(cli, p);
480 if (!cli_receive_smb(cli))
483 if (cli_is_error(cli))
489 /****************************************************************************
491 ****************************************************************************/
493 BOOL cli_unlink(struct cli_state *cli, const char *fname)
497 memset(cli->outbuf,'\0',smb_size);
498 memset(cli->inbuf,'\0',smb_size);
500 set_message(cli->outbuf,1, 0,True);
502 SCVAL(cli->outbuf,smb_com,SMBunlink);
503 SSVAL(cli->outbuf,smb_tid,cli->cnum);
504 cli_setup_packet(cli);
506 SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN);
508 p = smb_buf(cli->outbuf);
510 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
512 cli_setup_bcc(cli, p);
514 if (!cli_receive_smb(cli)) {
518 if (cli_is_error(cli)) {
525 /****************************************************************************
527 ****************************************************************************/
529 BOOL cli_mkdir(struct cli_state *cli, const char *dname)
533 memset(cli->outbuf,'\0',smb_size);
534 memset(cli->inbuf,'\0',smb_size);
536 set_message(cli->outbuf,0, 0,True);
538 SCVAL(cli->outbuf,smb_com,SMBmkdir);
539 SSVAL(cli->outbuf,smb_tid,cli->cnum);
540 cli_setup_packet(cli);
542 p = smb_buf(cli->outbuf);
544 p += clistr_push(cli, p, dname, -1, STR_TERMINATE);
546 cli_setup_bcc(cli, p);
549 if (!cli_receive_smb(cli)) {
553 if (cli_is_error(cli)) {
560 /****************************************************************************
562 ****************************************************************************/
564 BOOL cli_rmdir(struct cli_state *cli, const char *dname)
568 memset(cli->outbuf,'\0',smb_size);
569 memset(cli->inbuf,'\0',smb_size);
571 set_message(cli->outbuf,0, 0, True);
573 SCVAL(cli->outbuf,smb_com,SMBrmdir);
574 SSVAL(cli->outbuf,smb_tid,cli->cnum);
575 cli_setup_packet(cli);
577 p = smb_buf(cli->outbuf);
579 p += clistr_push(cli, p, dname, -1, STR_TERMINATE);
581 cli_setup_bcc(cli, p);
584 if (!cli_receive_smb(cli)) {
588 if (cli_is_error(cli)) {
595 /****************************************************************************
596 Set or clear the delete on close flag.
597 ****************************************************************************/
599 int cli_nt_delete_on_close(struct cli_state *cli, int fnum, BOOL flag)
601 unsigned int data_len = 1;
602 unsigned int param_len = 6;
603 uint16 setup = TRANSACT2_SETFILEINFO;
606 char *rparam=NULL, *rdata=NULL;
608 memset(param, 0, param_len);
610 SSVAL(param,2,SMB_SET_FILE_DISPOSITION_INFO);
614 if (!cli_send_trans(cli, SMBtrans2,
616 -1, 0, /* fid, flags */
617 &setup, 1, 0, /* setup, length, max */
618 param, param_len, 2, /* param, length, max */
619 (char *)&data, data_len, cli->max_xmit /* data, length, max */
624 if (!cli_receive_trans(cli, SMBtrans2,
626 &rdata, &data_len)) {
636 /****************************************************************************
637 Open a file - exposing the full horror of the NT API :-).
639 ****************************************************************************/
641 int cli_nt_create_full(struct cli_state *cli, const char *fname,
642 uint32 CreatFlags, uint32 DesiredAccess,
643 uint32 FileAttributes, uint32 ShareAccess,
644 uint32 CreateDisposition, uint32 CreateOptions,
650 memset(cli->outbuf,'\0',smb_size);
651 memset(cli->inbuf,'\0',smb_size);
653 set_message(cli->outbuf,24,0,True);
655 SCVAL(cli->outbuf,smb_com,SMBntcreateX);
656 SSVAL(cli->outbuf,smb_tid,cli->cnum);
657 cli_setup_packet(cli);
659 SSVAL(cli->outbuf,smb_vwv0,0xFF);
660 if (cli->use_oplocks)
661 CreatFlags |= (REQUEST_OPLOCK|REQUEST_BATCH_OPLOCK);
663 SIVAL(cli->outbuf,smb_ntcreate_Flags, CreatFlags);
664 SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
665 SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, DesiredAccess);
666 SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, FileAttributes);
667 SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, ShareAccess);
668 SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, CreateDisposition);
669 SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, CreateOptions);
670 SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
671 SCVAL(cli->outbuf,smb_ntcreate_SecurityFlags, SecuityFlags);
673 p = smb_buf(cli->outbuf);
674 /* this alignment and termination is critical for netapp filers. Don't change */
675 p += clistr_align_out(cli, p, 0);
676 len = clistr_push(cli, p, fname, -1, 0);
678 SSVAL(cli->outbuf,smb_ntcreate_NameLength, len);
679 /* sigh. this copes with broken netapp filer behaviour */
680 p += clistr_push(cli, p, "", -1, STR_TERMINATE);
682 cli_setup_bcc(cli, p);
685 if (!cli_receive_smb(cli)) {
689 if (cli_is_error(cli)) {
693 return SVAL(cli->inbuf,smb_vwv2 + 1);
696 /****************************************************************************
698 ****************************************************************************/
700 int cli_nt_create(struct cli_state *cli, const char *fname, uint32 DesiredAccess)
702 return cli_nt_create_full(cli, fname, 0, DesiredAccess, 0,
703 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_EXISTS_OPEN, 0x0, 0x0);
706 /****************************************************************************
708 WARNING: if you open with O_WRONLY then getattrE won't work!
709 ****************************************************************************/
711 int cli_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
715 unsigned accessmode=0;
719 if (!(flags & O_EXCL)) {
726 accessmode = (share_mode<<4);
728 if ((flags & O_ACCMODE) == O_RDWR) {
730 } else if ((flags & O_ACCMODE) == O_WRONLY) {
735 if ((flags & O_SYNC) == O_SYNC) {
736 accessmode |= (1<<14);
740 if (share_mode == DENY_FCB) {
744 memset(cli->outbuf,'\0',smb_size);
745 memset(cli->inbuf,'\0',smb_size);
747 set_message(cli->outbuf,15,0,True);
749 SCVAL(cli->outbuf,smb_com,SMBopenX);
750 SSVAL(cli->outbuf,smb_tid,cli->cnum);
751 cli_setup_packet(cli);
753 SSVAL(cli->outbuf,smb_vwv0,0xFF);
754 SSVAL(cli->outbuf,smb_vwv2,0); /* no additional info */
755 SSVAL(cli->outbuf,smb_vwv3,accessmode);
756 SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
757 SSVAL(cli->outbuf,smb_vwv5,0);
758 SSVAL(cli->outbuf,smb_vwv8,openfn);
760 if (cli->use_oplocks) {
761 /* if using oplocks then ask for a batch oplock via
762 core and extended methods */
763 SCVAL(cli->outbuf,smb_flg, CVAL(cli->outbuf,smb_flg)|
764 FLAG_REQUEST_OPLOCK|FLAG_REQUEST_BATCH_OPLOCK);
765 SSVAL(cli->outbuf,smb_vwv2,SVAL(cli->outbuf,smb_vwv2) | 6);
768 p = smb_buf(cli->outbuf);
769 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
771 cli_setup_bcc(cli, p);
774 if (!cli_receive_smb(cli)) {
778 if (cli_is_error(cli)) {
782 return SVAL(cli->inbuf,smb_vwv2);
785 /****************************************************************************
787 ****************************************************************************/
789 BOOL cli_close(struct cli_state *cli, int fnum)
791 memset(cli->outbuf,'\0',smb_size);
792 memset(cli->inbuf,'\0',smb_size);
794 set_message(cli->outbuf,3,0,True);
796 SCVAL(cli->outbuf,smb_com,SMBclose);
797 SSVAL(cli->outbuf,smb_tid,cli->cnum);
798 cli_setup_packet(cli);
800 SSVAL(cli->outbuf,smb_vwv0,fnum);
801 SIVALS(cli->outbuf,smb_vwv1,-1);
804 if (!cli_receive_smb(cli)) {
808 return !cli_is_error(cli);
812 /****************************************************************************
813 send a lock with a specified locktype
814 this is used for testing LOCKING_ANDX_CANCEL_LOCK
815 ****************************************************************************/
816 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
817 uint32 offset, uint32 len, int timeout, unsigned char locktype)
820 int saved_timeout = cli->timeout;
822 memset(cli->outbuf,'\0',smb_size);
823 memset(cli->inbuf,'\0', smb_size);
825 set_message(cli->outbuf,8,0,True);
827 SCVAL(cli->outbuf,smb_com,SMBlockingX);
828 SSVAL(cli->outbuf,smb_tid,cli->cnum);
829 cli_setup_packet(cli);
831 SCVAL(cli->outbuf,smb_vwv0,0xFF);
832 SSVAL(cli->outbuf,smb_vwv2,fnum);
833 SCVAL(cli->outbuf,smb_vwv3,locktype);
834 SIVALS(cli->outbuf, smb_vwv4, timeout);
835 SSVAL(cli->outbuf,smb_vwv6,0);
836 SSVAL(cli->outbuf,smb_vwv7,1);
838 p = smb_buf(cli->outbuf);
839 SSVAL(p, 0, cli->pid);
845 cli_setup_bcc(cli, p);
850 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 2*1000);
853 if (!cli_receive_smb(cli)) {
854 cli->timeout = saved_timeout;
855 return NT_STATUS_UNSUCCESSFUL;
858 cli->timeout = saved_timeout;
860 return cli_nt_error(cli);
864 /****************************************************************************
866 note that timeout is in units of 2 milliseconds
867 ****************************************************************************/
868 BOOL cli_lock(struct cli_state *cli, int fnum,
869 uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
872 int saved_timeout = cli->timeout;
874 memset(cli->outbuf,'\0',smb_size);
875 memset(cli->inbuf,'\0', smb_size);
877 set_message(cli->outbuf,8,0,True);
879 SCVAL(cli->outbuf,smb_com,SMBlockingX);
880 SSVAL(cli->outbuf,smb_tid,cli->cnum);
881 cli_setup_packet(cli);
883 SCVAL(cli->outbuf,smb_vwv0,0xFF);
884 SSVAL(cli->outbuf,smb_vwv2,fnum);
885 SCVAL(cli->outbuf,smb_vwv3,(lock_type == READ_LOCK? 1 : 0));
886 SIVALS(cli->outbuf, smb_vwv4, timeout);
887 SSVAL(cli->outbuf,smb_vwv6,0);
888 SSVAL(cli->outbuf,smb_vwv7,1);
890 p = smb_buf(cli->outbuf);
891 SSVAL(p, 0, cli->pid);
897 cli_setup_bcc(cli, p);
902 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout*2 + 5*1000);
905 if (!cli_receive_smb(cli)) {
906 cli->timeout = saved_timeout;
910 cli->timeout = saved_timeout;
912 if (cli_is_error(cli)) {
919 /****************************************************************************
921 ****************************************************************************/
923 BOOL cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
927 memset(cli->outbuf,'\0',smb_size);
928 memset(cli->inbuf,'\0',smb_size);
930 set_message(cli->outbuf,8,0,True);
932 SCVAL(cli->outbuf,smb_com,SMBlockingX);
933 SSVAL(cli->outbuf,smb_tid,cli->cnum);
934 cli_setup_packet(cli);
936 SCVAL(cli->outbuf,smb_vwv0,0xFF);
937 SSVAL(cli->outbuf,smb_vwv2,fnum);
938 SCVAL(cli->outbuf,smb_vwv3,0);
939 SIVALS(cli->outbuf, smb_vwv4, 0);
940 SSVAL(cli->outbuf,smb_vwv6,1);
941 SSVAL(cli->outbuf,smb_vwv7,0);
943 p = smb_buf(cli->outbuf);
944 SSVAL(p, 0, cli->pid);
948 cli_setup_bcc(cli, p);
950 if (!cli_receive_smb(cli)) {
954 if (cli_is_error(cli)) {
961 /****************************************************************************
962 Lock a file with 64 bit offsets.
963 ****************************************************************************/
965 BOOL cli_lock64(struct cli_state *cli, int fnum,
966 SMB_BIG_UINT offset, SMB_BIG_UINT len, int timeout, enum brl_type lock_type)
969 int saved_timeout = cli->timeout;
972 if (! (cli->capabilities & CAP_LARGE_FILES)) {
973 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
976 ltype = (lock_type == READ_LOCK? 1 : 0);
977 ltype |= LOCKING_ANDX_LARGE_FILES;
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,ltype);
991 SIVALS(cli->outbuf, smb_vwv4, timeout);
992 SSVAL(cli->outbuf,smb_vwv6,0);
993 SSVAL(cli->outbuf,smb_vwv7,1);
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);
1001 cli_setup_bcc(cli, p);
1005 cli->timeout = (timeout == -1) ? 0x7FFFFFFF : (timeout + 5*1000);
1008 if (!cli_receive_smb(cli)) {
1009 cli->timeout = saved_timeout;
1013 cli->timeout = saved_timeout;
1015 if (cli_is_error(cli)) {
1022 /****************************************************************************
1023 Unlock a file with 64 bit offsets.
1024 ****************************************************************************/
1026 BOOL cli_unlock64(struct cli_state *cli, int fnum, SMB_BIG_UINT offset, SMB_BIG_UINT len)
1030 if (! (cli->capabilities & CAP_LARGE_FILES)) {
1031 return cli_unlock(cli, fnum, offset, len);
1034 memset(cli->outbuf,'\0',smb_size);
1035 memset(cli->inbuf,'\0',smb_size);
1037 set_message(cli->outbuf,8,0,True);
1039 SCVAL(cli->outbuf,smb_com,SMBlockingX);
1040 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1041 cli_setup_packet(cli);
1043 SCVAL(cli->outbuf,smb_vwv0,0xFF);
1044 SSVAL(cli->outbuf,smb_vwv2,fnum);
1045 SCVAL(cli->outbuf,smb_vwv3,LOCKING_ANDX_LARGE_FILES);
1046 SIVALS(cli->outbuf, smb_vwv4, 0);
1047 SSVAL(cli->outbuf,smb_vwv6,1);
1048 SSVAL(cli->outbuf,smb_vwv7,0);
1050 p = smb_buf(cli->outbuf);
1051 SIVAL(p, 0, cli->pid);
1052 SOFF_T_R(p, 4, offset);
1053 SOFF_T_R(p, 12, len);
1055 cli_setup_bcc(cli, p);
1057 if (!cli_receive_smb(cli)) {
1061 if (cli_is_error(cli)) {
1069 /****************************************************************************
1070 Do a SMBgetattrE call.
1071 ****************************************************************************/
1073 BOOL cli_getattrE(struct cli_state *cli, int fd,
1074 uint16 *attr, SMB_OFF_T *size,
1075 time_t *c_time, time_t *a_time, time_t *m_time)
1077 memset(cli->outbuf,'\0',smb_size);
1078 memset(cli->inbuf,'\0',smb_size);
1080 set_message(cli->outbuf,1,0,True);
1082 SCVAL(cli->outbuf,smb_com,SMBgetattrE);
1083 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1084 cli_setup_packet(cli);
1086 SSVAL(cli->outbuf,smb_vwv0,fd);
1089 if (!cli_receive_smb(cli)) {
1093 if (cli_is_error(cli)) {
1098 *size = IVAL(cli->inbuf, smb_vwv6);
1102 *attr = SVAL(cli->inbuf,smb_vwv10);
1106 *c_time = make_unix_date3(cli->inbuf+smb_vwv0);
1110 *a_time = make_unix_date3(cli->inbuf+smb_vwv2);
1114 *m_time = make_unix_date3(cli->inbuf+smb_vwv4);
1120 /****************************************************************************
1122 ****************************************************************************/
1124 BOOL cli_getatr(struct cli_state *cli, const char *fname,
1125 uint16 *attr, SMB_OFF_T *size, time_t *t)
1129 memset(cli->outbuf,'\0',smb_size);
1130 memset(cli->inbuf,'\0',smb_size);
1132 set_message(cli->outbuf,0,0,True);
1134 SCVAL(cli->outbuf,smb_com,SMBgetatr);
1135 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1136 cli_setup_packet(cli);
1138 p = smb_buf(cli->outbuf);
1140 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
1142 cli_setup_bcc(cli, p);
1145 if (!cli_receive_smb(cli)) {
1149 if (cli_is_error(cli)) {
1154 *size = IVAL(cli->inbuf, smb_vwv3);
1158 *t = make_unix_date3(cli->inbuf+smb_vwv1);
1162 *attr = SVAL(cli->inbuf,smb_vwv0);
1169 /****************************************************************************
1170 Do a SMBsetattrE call.
1171 ****************************************************************************/
1173 BOOL cli_setattrE(struct cli_state *cli, int fd,
1174 time_t c_time, time_t a_time, time_t m_time)
1179 memset(cli->outbuf,'\0',smb_size);
1180 memset(cli->inbuf,'\0',smb_size);
1182 set_message(cli->outbuf,7,0,True);
1184 SCVAL(cli->outbuf,smb_com,SMBsetattrE);
1185 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1186 cli_setup_packet(cli);
1188 SSVAL(cli->outbuf,smb_vwv0, fd);
1189 put_dos_date3(cli->outbuf,smb_vwv1, c_time);
1190 put_dos_date3(cli->outbuf,smb_vwv3, a_time);
1191 put_dos_date3(cli->outbuf,smb_vwv5, m_time);
1193 p = smb_buf(cli->outbuf);
1196 cli_setup_bcc(cli, p);
1199 if (!cli_receive_smb(cli)) {
1203 if (cli_is_error(cli)) {
1210 /****************************************************************************
1211 Do a SMBsetatr call.
1212 ****************************************************************************/
1214 BOOL cli_setatr(struct cli_state *cli, const char *fname, uint16 attr, time_t t)
1218 memset(cli->outbuf,'\0',smb_size);
1219 memset(cli->inbuf,'\0',smb_size);
1221 set_message(cli->outbuf,8,0,True);
1223 SCVAL(cli->outbuf,smb_com,SMBsetatr);
1224 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1225 cli_setup_packet(cli);
1227 SSVAL(cli->outbuf,smb_vwv0, attr);
1228 put_dos_date3(cli->outbuf,smb_vwv1, t);
1230 p = smb_buf(cli->outbuf);
1232 p += clistr_push(cli, p, fname, -1, STR_TERMINATE);
1235 cli_setup_bcc(cli, p);
1238 if (!cli_receive_smb(cli)) {
1242 if (cli_is_error(cli)) {
1249 /****************************************************************************
1250 Check for existance of a dir.
1251 ****************************************************************************/
1252 BOOL cli_chkpath(struct cli_state *cli, const char *path)
1257 pstrcpy(path2,path);
1258 trim_char(path2,'\0','\\');
1262 memset(cli->outbuf,'\0',smb_size);
1263 set_message(cli->outbuf,0,0,True);
1264 SCVAL(cli->outbuf,smb_com,SMBchkpth);
1265 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1266 cli_setup_packet(cli);
1267 p = smb_buf(cli->outbuf);
1269 p += clistr_push(cli, p, path2, -1, STR_TERMINATE);
1271 cli_setup_bcc(cli, p);
1274 if (!cli_receive_smb(cli)) {
1278 if (cli_is_error(cli)) return False;
1283 /****************************************************************************
1285 ****************************************************************************/
1287 BOOL cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
1289 memset(cli->outbuf,'\0',smb_size);
1290 set_message(cli->outbuf,0,0,True);
1291 SCVAL(cli->outbuf,smb_com,SMBdskattr);
1292 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1293 cli_setup_packet(cli);
1296 if (!cli_receive_smb(cli)) {
1300 *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
1301 *total = SVAL(cli->inbuf,smb_vwv0);
1302 *avail = SVAL(cli->inbuf,smb_vwv3);
1307 /****************************************************************************
1308 Create and open a temporary file.
1309 ****************************************************************************/
1311 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
1316 memset(cli->outbuf,'\0',smb_size);
1317 memset(cli->inbuf,'\0',smb_size);
1319 set_message(cli->outbuf,3,0,True);
1321 SCVAL(cli->outbuf,smb_com,SMBctemp);
1322 SSVAL(cli->outbuf,smb_tid,cli->cnum);
1323 cli_setup_packet(cli);
1325 SSVAL(cli->outbuf,smb_vwv0,0);
1326 SIVALS(cli->outbuf,smb_vwv1,-1);
1328 p = smb_buf(cli->outbuf);
1330 p += clistr_push(cli, p, path, -1, STR_TERMINATE);
1332 cli_setup_bcc(cli, p);
1335 if (!cli_receive_smb(cli)) {
1339 if (cli_is_error(cli)) {
1343 /* despite the spec, the result has a -1, followed by
1344 length, followed by name */
1345 p = smb_buf(cli->inbuf);
1347 len = smb_buflen(cli->inbuf) - 4;
1348 if (len <= 0) return -1;
1352 clistr_pull(cli, path2, p,
1353 sizeof(path2), len, STR_ASCII);
1354 *tmp_path = SMB_STRDUP(path2);
1357 return SVAL(cli->inbuf,smb_vwv0);
1362 send a raw ioctl - used by the torture code
1364 NTSTATUS cli_raw_ioctl(struct cli_state *cli, int fnum, uint32 code, DATA_BLOB *blob)
1366 memset(cli->outbuf,'\0',smb_size);
1367 memset(cli->inbuf,'\0',smb_size);
1369 set_message(cli->outbuf, 3, 0, True);
1370 SCVAL(cli->outbuf,smb_com,SMBioctl);
1371 cli_setup_packet(cli);
1373 SSVAL(cli->outbuf, smb_vwv0, fnum);
1374 SSVAL(cli->outbuf, smb_vwv1, code>>16);
1375 SSVAL(cli->outbuf, smb_vwv2, (code&0xFFFF));
1378 if (!cli_receive_smb(cli)) {
1379 return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
1382 if (cli_is_error(cli)) {
1383 return cli_nt_error(cli);
1386 *blob = data_blob(NULL, 0);
1388 return NT_STATUS_OK;
1391 /*********************************************************
1392 Set an extended attribute utility fn.
1393 *********************************************************/
1395 static BOOL cli_set_ea(struct cli_state *cli, uint16 setup, char *param, unsigned int param_len,
1396 const char *ea_name, const char *ea_val, size_t ea_len)
1398 unsigned int data_len = 0;
1400 char *rparam=NULL, *rdata=NULL;
1402 size_t ea_namelen = strlen(ea_name);
1404 data_len = 4 + 4 + ea_namelen + 1 + ea_len;
1405 data = SMB_MALLOC(data_len);
1410 SIVAL(p,0,data_len);
1412 SCVAL(p, 0, 0); /* EA flags. */
1413 SCVAL(p, 1, ea_namelen);
1414 SSVAL(p, 2, ea_len);
1415 memcpy(p+4, ea_name, ea_namelen+1); /* Copy in the name. */
1416 memcpy(p+4+ea_namelen+1, ea_val, ea_len);
1418 if (!cli_send_trans(cli, SMBtrans2,
1420 -1, 0, /* fid, flags */
1421 &setup, 1, 0, /* setup, length, max */
1422 param, param_len, 2, /* param, length, max */
1423 data, data_len, cli->max_xmit /* data, length, max */
1428 if (!cli_receive_trans(cli, SMBtrans2,
1429 &rparam, ¶m_len,
1430 &rdata, &data_len)) {
1441 /*********************************************************
1442 Set an extended attribute on a pathname.
1443 *********************************************************/
1445 BOOL cli_set_ea_path(struct cli_state *cli, const char *path, const char *ea_name, const char *ea_val, size_t ea_len)
1447 uint16 setup = TRANSACT2_SETPATHINFO;
1448 unsigned int param_len = 0;
1449 char param[sizeof(pstring)+6];
1450 size_t srclen = 2*(strlen(path)+1);
1453 memset(param, 0, sizeof(param));
1454 SSVAL(param,0,SMB_INFO_SET_EA);
1457 p += clistr_push(cli, p, path, MIN(srclen, sizeof(param)-6), STR_TERMINATE);
1458 param_len = PTR_DIFF(p, param);
1460 return cli_set_ea(cli, setup, param, param_len, ea_name, ea_val, ea_len);
1463 /*********************************************************
1464 Set an extended attribute on an fnum.
1465 *********************************************************/
1467 BOOL cli_set_ea_fnum(struct cli_state *cli, int fnum, const char *ea_name, const char *ea_val, size_t ea_len)
1470 uint16 setup = TRANSACT2_SETFILEINFO;
1472 memset(param, 0, 6);
1473 SSVAL(param,0,fnum);
1474 SSVAL(param,2,SMB_INFO_SET_EA);
1476 return cli_set_ea(cli, setup, param, 6, ea_name, ea_val, ea_len);
1479 /*********************************************************
1480 Get an extended attribute list tility fn.
1481 *********************************************************/
1483 static BOOL cli_get_ea_list(struct cli_state *cli,
1484 uint16 setup, char *param, unsigned int param_len,
1487 struct ea_struct **pea_list)
1489 unsigned int data_len = 0;
1490 unsigned int rparam_len, rdata_len;
1491 char *rparam=NULL, *rdata=NULL;
1496 struct ea_struct *ea_list;
1501 if (!cli_send_trans(cli, SMBtrans2,
1503 -1, 0, /* fid, flags */
1504 &setup, 1, 0, /* setup, length, max */
1505 param, param_len, 10, /* param, length, max */
1506 NULL, data_len, cli->max_xmit /* data, length, max */
1511 if (!cli_receive_trans(cli, SMBtrans2,
1512 &rparam, &rparam_len,
1513 &rdata, &rdata_len)) {
1517 if (!rdata || rdata_len < 4) {
1521 ea_size = (size_t)IVAL(rdata,0);
1522 if (ea_size > rdata_len) {
1527 /* No EA's present. */
1535 /* Validate the EA list and count it. */
1536 for (num_eas = 0; ea_size >= 4; num_eas++) {
1537 unsigned int ea_namelen = CVAL(p,1);
1538 unsigned int ea_valuelen = SVAL(p,2);
1539 if (ea_namelen == 0) {
1542 if (4 + ea_namelen + 1 + ea_valuelen > ea_size) {
1545 ea_size -= 4 + ea_namelen + 1 + ea_valuelen;
1546 p += 4 + ea_namelen + 1 + ea_valuelen;
1554 *pnum_eas = num_eas;
1556 /* Caller only wants number of EA's. */
1561 ea_list = TALLOC_ARRAY(ctx, struct ea_struct, num_eas);
1566 ea_size = (size_t)IVAL(rdata,0);
1569 for (num_eas = 0; num_eas < *pnum_eas; num_eas++ ) {
1570 struct ea_struct *ea = &ea_list[num_eas];
1571 fstring unix_ea_name;
1572 unsigned int ea_namelen = CVAL(p,1);
1573 unsigned int ea_valuelen = SVAL(p,2);
1575 ea->flags = CVAL(p,0);
1576 unix_ea_name[0] = '\0';
1577 pull_ascii_fstring(unix_ea_name, p + 4);
1578 ea->name = talloc_strdup(ctx, unix_ea_name);
1579 /* Ensure the value is null terminated (in case it's a string). */
1580 ea->value = data_blob_talloc(ctx, NULL, ea_valuelen + 1);
1581 if (!ea->value.data) {
1585 memcpy(ea->value.data, p+4+ea_namelen+1, ea_valuelen);
1587 ea->value.data[ea_valuelen] = 0;
1589 p += 4 + ea_namelen + 1 + ea_valuelen;
1592 *pea_list = ea_list;
1602 /*********************************************************
1603 Get an extended attribute list from a pathname.
1604 *********************************************************/
1606 BOOL cli_get_ea_list_path(struct cli_state *cli, const char *path,
1609 struct ea_struct **pea_list)
1611 uint16 setup = TRANSACT2_QPATHINFO;
1612 unsigned int param_len = 0;
1613 char param[sizeof(pstring)+6];
1618 SSVAL(p, 0, SMB_INFO_QUERY_ALL_EAS);
1620 p += clistr_push(cli, p, path, sizeof(pstring)-6, STR_TERMINATE);
1621 param_len = PTR_DIFF(p, param);
1623 return cli_get_ea_list(cli, setup, param, param_len, ctx, pnum_eas, pea_list);
1626 /*********************************************************
1627 Get an extended attribute list from an fnum.
1628 *********************************************************/
1630 BOOL cli_get_ea_list_fnum(struct cli_state *cli, int fnum,
1633 struct ea_struct **pea_list)
1635 uint16 setup = TRANSACT2_QFILEINFO;
1638 memset(param, 0, 6);
1639 SSVAL(param,0,fnum);
1640 SSVAL(param,2,SMB_INFO_SET_EA);
1642 return cli_get_ea_list(cli, setup, param, 6, ctx, pnum_eas, pea_list);