2 Unix SMB/CIFS implementation.
3 Main SMB reply routines
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Andrew Bartlett 2001
6 Copyright (C) Jeremy Allison 1992-2004.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 This file handles most of the reply_ calls that the server
24 makes to handle specific protocols
29 /* look in server.c for some explanation of these variables */
30 extern enum protocol_types Protocol
;
33 unsigned int smb_echo_count
= 0;
34 extern uint32 global_client_caps
;
36 extern struct current_user current_user
;
37 extern BOOL global_encrypted_passwords_negotiated
;
39 /****************************************************************************
40 Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
41 We're assuming here that '/' is not the second byte in any multibyte char
42 set (a safe assumption). '\\' *may* be the second byte in a multibyte char
44 ****************************************************************************/
46 NTSTATUS
check_path_syntax(pstring destname
, const pstring srcname
)
49 const char *s
= srcname
;
50 NTSTATUS ret
= NT_STATUS_OK
;
51 BOOL start_of_name_component
= True
;
52 unsigned int num_bad_components
= 0;
55 if (IS_DIRECTORY_SEP(*s
)) {
57 * Safe to assume is not the second part of a mb char as this is handled below.
59 /* Eat multiple '/' or '\\' */
60 while (IS_DIRECTORY_SEP(*s
)) {
63 if ((d
!= destname
) && (*s
!= '\0')) {
64 /* We only care about non-leading or trailing '/' or '\\' */
68 start_of_name_component
= True
;
72 if (start_of_name_component
) {
73 if ((s
[0] == '.') && (s
[1] == '.') && (IS_DIRECTORY_SEP(s
[2]) || s
[2] == '\0')) {
74 /* Uh oh - "/../" or "\\..\\" or "/..\0" or "\\..\0" ! */
77 * No mb char starts with '.' so we're safe checking the directory separator here.
80 /* If we just added a '/' - delete it */
81 if ((d
> destname
) && (*(d
-1) == '/')) {
86 /* Are we at the start ? Can't go back further if so. */
88 ret
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
91 /* Go back one level... */
92 /* We know this is safe as '/' cannot be part of a mb sequence. */
93 /* NOTE - if this assumption is invalid we are not in good shape... */
94 /* Decrement d first as d points to the *next* char to write into. */
95 for (d
--; d
> destname
; d
--) {
99 s
+= 2; /* Else go past the .. */
100 /* We're still at the start of a name component, just the previous one. */
102 if (num_bad_components
) {
103 /* Hmmm. Should we only decrement the bad_components if
104 we're removing a bad component ? Need to check this. JRA. */
105 num_bad_components
--;
110 } else if ((s
[0] == '.') && ((s
[1] == '\0') || IS_DIRECTORY_SEP(s
[1]))) {
111 /* Component of pathname can't be "." only. */
112 ret
= NT_STATUS_OBJECT_NAME_INVALID
;
113 num_bad_components
++;
121 return NT_STATUS_OBJECT_NAME_INVALID
;
129 return NT_STATUS_OBJECT_NAME_INVALID
;
135 switch(next_mb_char_size(s
)) {
146 DEBUG(0,("check_path_syntax: character length assumptions invalid !\n"));
148 return NT_STATUS_INVALID_PARAMETER
;
151 if (start_of_name_component
&& num_bad_components
) {
152 num_bad_components
++;
154 start_of_name_component
= False
;
157 if (NT_STATUS_EQUAL(ret
, NT_STATUS_OBJECT_NAME_INVALID
)) {
158 if (num_bad_components
> 1) {
159 ret
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
167 /****************************************************************************
168 Ensure we check the path in *exactly* the same way as W2K for a findfirst/findnext
169 path or anything including wildcards.
170 We're assuming here that '/' is not the second byte in any multibyte char
171 set (a safe assumption). '\\' *may* be the second byte in a multibyte char
173 ****************************************************************************/
175 NTSTATUS
check_path_syntax_wcard(pstring destname
, const pstring srcname
, BOOL
*p_contains_wcard
)
178 const char *s
= srcname
;
179 NTSTATUS ret
= NT_STATUS_OK
;
180 BOOL start_of_name_component
= True
;
181 unsigned int num_bad_components
= 0;
183 *p_contains_wcard
= False
;
186 if (IS_DIRECTORY_SEP(*s
)) {
188 * Safe to assume is not the second part of a mb char as this is handled below.
190 /* Eat multiple '/' or '\\' */
191 while (IS_DIRECTORY_SEP(*s
)) {
194 if ((d
!= destname
) && (*s
!= '\0')) {
195 /* We only care about non-leading or trailing '/' or '\\' */
199 start_of_name_component
= True
;
203 if (start_of_name_component
) {
204 if ((s
[0] == '.') && (s
[1] == '.') && (IS_DIRECTORY_SEP(s
[2]) || s
[2] == '\0')) {
205 /* Uh oh - "/../" or "\\..\\" or "/..\0" or "\\..\0" ! */
208 * No mb char starts with '.' so we're safe checking the directory separator here.
211 /* If we just added a '/' - delete it */
212 if ((d
> destname
) && (*(d
-1) == '/')) {
217 /* Are we at the start ? Can't go back further if so. */
219 ret
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
222 /* Go back one level... */
223 /* We know this is safe as '/' cannot be part of a mb sequence. */
224 /* NOTE - if this assumption is invalid we are not in good shape... */
225 /* Decrement d first as d points to the *next* char to write into. */
226 for (d
--; d
> destname
; d
--) {
230 s
+= 2; /* Else go past the .. */
231 /* We're still at the start of a name component, just the previous one. */
233 if (num_bad_components
) {
234 /* Hmmm. Should we only decrement the bad_components if
235 we're removing a bad component ? Need to check this. JRA. */
236 num_bad_components
--;
241 } else if ((s
[0] == '.') && ((s
[1] == '\0') || IS_DIRECTORY_SEP(s
[1]))) {
242 /* Component of pathname can't be "." only. */
243 ret
= NT_STATUS_OBJECT_NAME_INVALID
;
244 num_bad_components
++;
252 return NT_STATUS_OBJECT_NAME_INVALID
;
254 if (!*p_contains_wcard
) {
261 *p_contains_wcard
= True
;
269 switch(next_mb_char_size(s
)) {
283 DEBUG(0,("check_path_syntax_wcard: character length assumptions invalid !\n"));
285 return NT_STATUS_INVALID_PARAMETER
;
288 if (start_of_name_component
&& num_bad_components
) {
289 num_bad_components
++;
291 start_of_name_component
= False
;
294 if (NT_STATUS_EQUAL(ret
, NT_STATUS_OBJECT_NAME_INVALID
)) {
295 /* For some strange reason being called from findfirst changes
296 the num_components number to cause the error return to change. JRA. */
297 if (num_bad_components
> 2) {
298 ret
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
306 /****************************************************************************
307 Check the path for a POSIX client.
308 We're assuming here that '/' is not the second byte in any multibyte char
309 set (a safe assumption).
310 ****************************************************************************/
312 NTSTATUS
check_path_syntax_posix(pstring destname
, const pstring srcname
)
315 const char *s
= srcname
;
316 NTSTATUS ret
= NT_STATUS_OK
;
317 BOOL start_of_name_component
= True
;
322 * Safe to assume is not the second part of a mb char as this is handled below.
324 /* Eat multiple '/' or '\\' */
328 if ((d
!= destname
) && (*s
!= '\0')) {
329 /* We only care about non-leading or trailing '/' */
333 start_of_name_component
= True
;
337 if (start_of_name_component
) {
338 if ((s
[0] == '.') && (s
[1] == '.') && (s
[2] == '/' || s
[2] == '\0')) {
339 /* Uh oh - "/../" or "/..\0" ! */
342 * No mb char starts with '.' so we're safe checking the directory separator here.
345 /* If we just added a '/' - delete it */
346 if ((d
> destname
) && (*(d
-1) == '/')) {
351 /* Are we at the start ? Can't go back further if so. */
353 ret
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
356 /* Go back one level... */
357 /* We know this is safe as '/' cannot be part of a mb sequence. */
358 /* NOTE - if this assumption is invalid we are not in good shape... */
359 /* Decrement d first as d points to the *next* char to write into. */
360 for (d
--; d
> destname
; d
--) {
364 s
+= 2; /* Else go past the .. */
367 } else if ((s
[0] == '.') && ((s
[1] == '\0') || (s
[1] == '/'))) {
377 switch(next_mb_char_size(s
)) {
391 DEBUG(0,("check_path_syntax_posix: character length assumptions invalid !\n"));
393 return NT_STATUS_INVALID_PARAMETER
;
396 start_of_name_component
= False
;
403 /****************************************************************************
404 Pull a string and check the path allowing a wilcard - provide for error return.
405 ****************************************************************************/
407 size_t srvstr_get_path_wcard(char *inbuf
, char *dest
, const char *src
, size_t dest_len
, size_t src_len
, int flags
,
408 NTSTATUS
*err
, BOOL
*contains_wcard
)
411 char *tmppath_ptr
= tmppath
;
414 SMB_ASSERT(dest_len
== sizeof(pstring
));
418 ret
= srvstr_pull_buf( inbuf
, tmppath_ptr
, src
, dest_len
, flags
);
420 ret
= srvstr_pull( inbuf
, tmppath_ptr
, src
, dest_len
, src_len
, flags
);
423 *contains_wcard
= False
;
425 if (lp_posix_pathnames()) {
426 *err
= check_path_syntax_posix(dest
, tmppath
);
428 *err
= check_path_syntax_wcard(dest
, tmppath
, contains_wcard
);
434 /****************************************************************************
435 Pull a string and check the path - provide for error return.
436 ****************************************************************************/
438 size_t srvstr_get_path(char *inbuf
, char *dest
, const char *src
, size_t dest_len
, size_t src_len
, int flags
, NTSTATUS
*err
)
441 char *tmppath_ptr
= tmppath
;
444 SMB_ASSERT(dest_len
== sizeof(pstring
));
448 ret
= srvstr_pull_buf( inbuf
, tmppath_ptr
, src
, dest_len
, flags
);
450 ret
= srvstr_pull( inbuf
, tmppath_ptr
, src
, dest_len
, src_len
, flags
);
452 if (lp_posix_pathnames()) {
453 *err
= check_path_syntax_posix(dest
, tmppath
);
455 *err
= check_path_syntax(dest
, tmppath
);
461 /****************************************************************************
462 Reply to a special message.
463 ****************************************************************************/
465 int reply_special(char *inbuf
,char *outbuf
)
468 int msg_type
= CVAL(inbuf
,0);
469 int msg_flags
= CVAL(inbuf
,1);
473 static BOOL already_got_session
= False
;
477 memset(outbuf
,'\0',smb_size
);
479 smb_setlen(outbuf
,0);
482 case 0x81: /* session request */
484 if (already_got_session
) {
485 exit_server("multiple session request not permitted");
488 SCVAL(outbuf
,0,0x82);
490 if (name_len(inbuf
+4) > 50 ||
491 name_len(inbuf
+4 + name_len(inbuf
+ 4)) > 50) {
492 DEBUG(0,("Invalid name length in session request\n"));
495 name_extract(inbuf
,4,name1
);
496 name_type
= name_extract(inbuf
,4 + name_len(inbuf
+ 4),name2
);
497 DEBUG(2,("netbios connect: name1=%s name2=%s\n",
500 set_local_machine_name(name1
, True
);
501 set_remote_machine_name(name2
, True
);
503 DEBUG(2,("netbios connect: local=%s remote=%s, name type = %x\n",
504 get_local_machine_name(), get_remote_machine_name(),
507 if (name_type
== 'R') {
508 /* We are being asked for a pathworks session ---
510 SCVAL(outbuf
, 0,0x83);
514 /* only add the client's machine name to the list
515 of possibly valid usernames if we are operating
516 in share mode security */
517 if (lp_security() == SEC_SHARE
) {
518 add_session_user(get_remote_machine_name());
521 reload_services(True
);
524 already_got_session
= True
;
527 case 0x89: /* session keepalive request
528 (some old clients produce this?) */
529 SCVAL(outbuf
,0,SMBkeepalive
);
533 case 0x82: /* positive session response */
534 case 0x83: /* negative session response */
535 case 0x84: /* retarget session response */
536 DEBUG(0,("Unexpected session response\n"));
539 case SMBkeepalive
: /* session keepalive */
544 DEBUG(5,("init msg_type=0x%x msg_flags=0x%x\n",
545 msg_type
, msg_flags
));
550 /****************************************************************************
552 conn POINTER CAN BE NULL HERE !
553 ****************************************************************************/
555 int reply_tcon(connection_struct
*conn
,
556 char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
563 uint16 vuid
= SVAL(inbuf
,smb_uid
);
567 DATA_BLOB password_blob
;
569 START_PROFILE(SMBtcon
);
571 *service_buf
= *password
= *dev
= 0;
573 p
= smb_buf(inbuf
)+1;
574 p
+= srvstr_pull_buf(inbuf
, service_buf
, p
, sizeof(service_buf
), STR_TERMINATE
) + 1;
575 pwlen
= srvstr_pull_buf(inbuf
, password
, p
, sizeof(password
), STR_TERMINATE
) + 1;
577 p
+= srvstr_pull_buf(inbuf
, dev
, p
, sizeof(dev
), STR_TERMINATE
) + 1;
579 p
= strrchr_m(service_buf
,'\\');
583 service
= service_buf
;
586 password_blob
= data_blob(password
, pwlen
+1);
588 conn
= make_connection(service
,password_blob
,dev
,vuid
,&nt_status
);
590 data_blob_clear_free(&password_blob
);
593 END_PROFILE(SMBtcon
);
594 return ERROR_NT(nt_status
);
597 outsize
= set_message(outbuf
,2,0,True
);
598 SSVAL(outbuf
,smb_vwv0
,max_recv
);
599 SSVAL(outbuf
,smb_vwv1
,conn
->cnum
);
600 SSVAL(outbuf
,smb_tid
,conn
->cnum
);
602 DEBUG(3,("tcon service=%s cnum=%d\n",
603 service
, conn
->cnum
));
605 END_PROFILE(SMBtcon
);
609 /****************************************************************************
610 Reply to a tcon and X.
611 conn POINTER CAN BE NULL HERE !
612 ****************************************************************************/
614 int reply_tcon_and_X(connection_struct
*conn
, char *inbuf
,char *outbuf
,int length
,int bufsize
)
619 /* what the cleint thinks the device is */
620 fstring client_devicetype
;
621 /* what the server tells the client the share represents */
622 const char *server_devicetype
;
624 uint16 vuid
= SVAL(inbuf
,smb_uid
);
625 int passlen
= SVAL(inbuf
,smb_vwv3
);
629 START_PROFILE(SMBtconX
);
631 *service
= *client_devicetype
= 0;
633 /* we might have to close an old one */
634 if ((SVAL(inbuf
,smb_vwv2
) & 0x1) && conn
) {
635 close_cnum(conn
,vuid
);
638 if (passlen
> MAX_PASS_LEN
) {
639 return ERROR_DOS(ERRDOS
,ERRbuftoosmall
);
642 if (global_encrypted_passwords_negotiated
) {
643 password
= data_blob(smb_buf(inbuf
),passlen
);
645 password
= data_blob(smb_buf(inbuf
),passlen
+1);
646 /* Ensure correct termination */
647 password
.data
[passlen
]=0;
650 p
= smb_buf(inbuf
) + passlen
;
651 p
+= srvstr_pull_buf(inbuf
, path
, p
, sizeof(path
), STR_TERMINATE
);
654 * the service name can be either: \\server\share
655 * or share directly like on the DELL PowerVault 705
658 q
= strchr_m(path
+2,'\\');
660 END_PROFILE(SMBtconX
);
661 return(ERROR_DOS(ERRDOS
,ERRnosuchshare
));
663 fstrcpy(service
,q
+1);
666 fstrcpy(service
,path
);
668 p
+= srvstr_pull(inbuf
, client_devicetype
, p
, sizeof(client_devicetype
), 6, STR_ASCII
);
670 DEBUG(4,("Client requested device type [%s] for share [%s]\n", client_devicetype
, service
));
672 conn
= make_connection(service
,password
,client_devicetype
,vuid
,&nt_status
);
674 data_blob_clear_free(&password
);
677 END_PROFILE(SMBtconX
);
678 return ERROR_NT(nt_status
);
682 server_devicetype
= "IPC";
683 else if ( IS_PRINT(conn
) )
684 server_devicetype
= "LPT1:";
686 server_devicetype
= "A:";
688 if (Protocol
< PROTOCOL_NT1
) {
689 set_message(outbuf
,2,0,True
);
691 p
+= srvstr_push(outbuf
, p
, server_devicetype
, -1,
692 STR_TERMINATE
|STR_ASCII
);
693 set_message_end(outbuf
,p
);
695 /* NT sets the fstype of IPC$ to the null string */
696 const char *fstype
= IS_IPC(conn
) ? "" : lp_fstype(SNUM(conn
));
698 set_message(outbuf
,3,0,True
);
701 p
+= srvstr_push(outbuf
, p
, server_devicetype
, -1,
702 STR_TERMINATE
|STR_ASCII
);
703 p
+= srvstr_push(outbuf
, p
, fstype
, -1,
706 set_message_end(outbuf
,p
);
708 /* what does setting this bit do? It is set by NT4 and
709 may affect the ability to autorun mounted cdroms */
710 SSVAL(outbuf
, smb_vwv2
, SMB_SUPPORT_SEARCH_BITS
|
711 (lp_csc_policy(SNUM(conn
)) << 2));
713 init_dfsroot(conn
, inbuf
, outbuf
);
717 DEBUG(3,("tconX service=%s \n",
720 /* set the incoming and outgoing tid to the just created one */
721 SSVAL(inbuf
,smb_tid
,conn
->cnum
);
722 SSVAL(outbuf
,smb_tid
,conn
->cnum
);
724 END_PROFILE(SMBtconX
);
725 return chain_reply(inbuf
,outbuf
,length
,bufsize
);
728 /****************************************************************************
729 Reply to an unknown type.
730 ****************************************************************************/
732 int reply_unknown(char *inbuf
,char *outbuf
)
735 type
= CVAL(inbuf
,smb_com
);
737 DEBUG(0,("unknown command type (%s): type=%d (0x%X)\n",
738 smb_fn_name(type
), type
, type
));
740 return(ERROR_DOS(ERRSRV
,ERRunknownsmb
));
743 /****************************************************************************
745 conn POINTER CAN BE NULL HERE !
746 ****************************************************************************/
748 int reply_ioctl(connection_struct
*conn
,
749 char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
751 uint16 device
= SVAL(inbuf
,smb_vwv1
);
752 uint16 function
= SVAL(inbuf
,smb_vwv2
);
753 uint32 ioctl_code
= (device
<< 16) + function
;
754 int replysize
, outsize
;
756 START_PROFILE(SMBioctl
);
758 DEBUG(4, ("Received IOCTL (code 0x%x)\n", ioctl_code
));
760 switch (ioctl_code
) {
761 case IOCTL_QUERY_JOB_INFO
:
765 END_PROFILE(SMBioctl
);
766 return(ERROR_DOS(ERRSRV
,ERRnosupport
));
769 outsize
= set_message(outbuf
,8,replysize
+1,True
);
770 SSVAL(outbuf
,smb_vwv1
,replysize
); /* Total data bytes returned */
771 SSVAL(outbuf
,smb_vwv5
,replysize
); /* Data bytes this buffer */
772 SSVAL(outbuf
,smb_vwv6
,52); /* Offset to data */
773 p
= smb_buf(outbuf
) + 1; /* Allow for alignment */
775 switch (ioctl_code
) {
776 case IOCTL_QUERY_JOB_INFO
:
778 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
780 END_PROFILE(SMBioctl
);
781 return(UNIXERROR(ERRDOS
,ERRbadfid
));
783 SSVAL(p
,0,fsp
->rap_print_jobid
); /* Job number */
784 srvstr_push(outbuf
, p
+2, global_myname(), 15, STR_TERMINATE
|STR_ASCII
);
786 srvstr_push(outbuf
, p
+18, lp_servicename(SNUM(conn
)), 13, STR_TERMINATE
|STR_ASCII
);
792 END_PROFILE(SMBioctl
);
796 /****************************************************************************
798 ****************************************************************************/
800 int reply_chkpth(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
805 BOOL bad_path
= False
;
806 SMB_STRUCT_STAT sbuf
;
809 START_PROFILE(SMBchkpth
);
811 srvstr_get_path(inbuf
, name
, smb_buf(inbuf
) + 1, sizeof(name
), 0, STR_TERMINATE
, &status
);
812 if (!NT_STATUS_IS_OK(status
)) {
813 END_PROFILE(SMBchkpth
);
815 /* Strange DOS error code semantics only for chkpth... */
816 if (!(SVAL(inbuf
,smb_flg2
) & FLAGS2_32_BIT_ERROR_CODES
)) {
817 if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_INVALID
,status
)) {
818 /* We need to map to ERRbadpath */
819 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
822 return ERROR_NT(status
);
825 RESOLVE_DFSPATH(name
, conn
, inbuf
, outbuf
);
827 unix_convert(name
,conn
,0,&bad_path
,&sbuf
);
829 END_PROFILE(SMBchkpth
);
830 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND
);
833 if (check_name(name
,conn
)) {
834 if (VALID_STAT(sbuf
) || SMB_VFS_STAT(conn
,name
,&sbuf
) == 0)
835 if (!(ok
= S_ISDIR(sbuf
.st_mode
))) {
836 END_PROFILE(SMBchkpth
);
837 return ERROR_BOTH(NT_STATUS_NOT_A_DIRECTORY
,ERRDOS
,ERRbadpath
);
842 /* We special case this - as when a Windows machine
843 is parsing a path is steps through the components
844 one at a time - if a component fails it expects
845 ERRbadpath, not ERRbadfile.
847 if(errno
== ENOENT
) {
849 * Windows returns different error codes if
850 * the parent directory is valid but not the
851 * last component - it returns NT_STATUS_OBJECT_NAME_NOT_FOUND
852 * for that case and NT_STATUS_OBJECT_PATH_NOT_FOUND
853 * if the path is invalid. This is different from set_bad_path_error()
854 * in the non-NT error case.
856 END_PROFILE(SMBchkpth
);
857 return ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND
,ERRDOS
,ERRbadpath
);
860 END_PROFILE(SMBchkpth
);
861 return(UNIXERROR(ERRDOS
,ERRbadpath
));
864 outsize
= set_message(outbuf
,0,0,False
);
865 DEBUG(3,("chkpth %s mode=%d\n", name
, (int)SVAL(inbuf
,smb_vwv0
)));
867 END_PROFILE(SMBchkpth
);
871 /****************************************************************************
873 ****************************************************************************/
875 int reply_getatr(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
879 SMB_STRUCT_STAT sbuf
;
884 BOOL bad_path
= False
;
888 START_PROFILE(SMBgetatr
);
890 p
= smb_buf(inbuf
) + 1;
891 p
+= srvstr_get_path(inbuf
, fname
, p
, sizeof(fname
), 0, STR_TERMINATE
, &status
);
892 if (!NT_STATUS_IS_OK(status
)) {
893 END_PROFILE(SMBgetatr
);
894 return ERROR_NT(status
);
897 RESOLVE_DFSPATH(fname
, conn
, inbuf
, outbuf
);
899 /* dos smetimes asks for a stat of "" - it returns a "hidden directory"
900 under WfWg - weird! */
902 mode
= aHIDDEN
| aDIR
;
903 if (!CAN_WRITE(conn
))
909 unix_convert(fname
,conn
,0,&bad_path
,&sbuf
);
911 END_PROFILE(SMBgetatr
);
912 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND
);
914 if (check_name(fname
,conn
)) {
915 if (VALID_STAT(sbuf
) || SMB_VFS_STAT(conn
,fname
,&sbuf
) == 0) {
916 mode
= dos_mode(conn
,fname
,&sbuf
);
918 mtime
= sbuf
.st_mtime
;
923 DEBUG(3,("stat of %s failed (%s)\n",fname
,strerror(errno
)));
929 END_PROFILE(SMBgetatr
);
930 return set_bad_path_error(errno
, bad_path
, outbuf
, ERRDOS
,ERRbadfile
);
933 outsize
= set_message(outbuf
,10,0,True
);
935 SSVAL(outbuf
,smb_vwv0
,mode
);
936 if(lp_dos_filetime_resolution(SNUM(conn
)) ) {
937 srv_put_dos_date3(outbuf
,smb_vwv1
,mtime
& ~1);
939 srv_put_dos_date3(outbuf
,smb_vwv1
,mtime
);
941 SIVAL(outbuf
,smb_vwv3
,(uint32
)size
);
943 if (Protocol
>= PROTOCOL_NT1
) {
944 SSVAL(outbuf
,smb_flg2
,SVAL(outbuf
, smb_flg2
) | FLAGS2_IS_LONG_NAME
);
947 DEBUG( 3, ( "getatr name=%s mode=%d size=%d\n", fname
, mode
, (uint32
)size
) );
949 END_PROFILE(SMBgetatr
);
953 /****************************************************************************
955 ****************************************************************************/
957 int reply_setatr(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
964 SMB_STRUCT_STAT sbuf
;
965 BOOL bad_path
= False
;
969 START_PROFILE(SMBsetatr
);
971 p
= smb_buf(inbuf
) + 1;
972 p
+= srvstr_get_path(inbuf
, fname
, p
, sizeof(fname
), 0, STR_TERMINATE
, &status
);
973 if (!NT_STATUS_IS_OK(status
)) {
974 END_PROFILE(SMBsetatr
);
975 return ERROR_NT(status
);
978 RESOLVE_DFSPATH(fname
, conn
, inbuf
, outbuf
);
980 unix_convert(fname
,conn
,0,&bad_path
,&sbuf
);
982 END_PROFILE(SMBsetatr
);
983 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND
);
986 mode
= SVAL(inbuf
,smb_vwv0
);
987 mtime
= srv_make_unix_date3(inbuf
+smb_vwv1
);
989 if (mode
!= FILE_ATTRIBUTE_NORMAL
) {
990 if (VALID_STAT_OF_DIR(sbuf
))
995 if (check_name(fname
,conn
)) {
996 ok
= (file_set_dosmode(conn
,fname
,mode
,&sbuf
,False
) == 0);
1003 ok
= set_filetime(conn
,fname
,mtime
);
1006 END_PROFILE(SMBsetatr
);
1007 return set_bad_path_error(errno
, bad_path
, outbuf
, ERRDOS
, ERRnoaccess
);
1010 outsize
= set_message(outbuf
,0,0,False
);
1012 DEBUG( 3, ( "setatr name=%s mode=%d\n", fname
, mode
) );
1014 END_PROFILE(SMBsetatr
);
1018 /****************************************************************************
1020 ****************************************************************************/
1022 int reply_dskattr(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
1025 SMB_BIG_UINT dfree
,dsize
,bsize
;
1026 START_PROFILE(SMBdskattr
);
1028 if (get_dfree_info(conn
,".",True
,&bsize
,&dfree
,&dsize
) == (SMB_BIG_UINT
)-1) {
1029 END_PROFILE(SMBdskattr
);
1030 return(UNIXERROR(ERRHRD
,ERRgeneral
));
1033 outsize
= set_message(outbuf
,5,0,True
);
1035 if (Protocol
<= PROTOCOL_LANMAN2
) {
1036 double total_space
, free_space
;
1037 /* we need to scale this to a number that DOS6 can handle. We
1038 use floating point so we can handle large drives on systems
1039 that don't have 64 bit integers
1041 we end up displaying a maximum of 2G to DOS systems
1043 total_space
= dsize
* (double)bsize
;
1044 free_space
= dfree
* (double)bsize
;
1046 dsize
= (total_space
+63*512) / (64*512);
1047 dfree
= (free_space
+63*512) / (64*512);
1049 if (dsize
> 0xFFFF) dsize
= 0xFFFF;
1050 if (dfree
> 0xFFFF) dfree
= 0xFFFF;
1052 SSVAL(outbuf
,smb_vwv0
,dsize
);
1053 SSVAL(outbuf
,smb_vwv1
,64); /* this must be 64 for dos systems */
1054 SSVAL(outbuf
,smb_vwv2
,512); /* and this must be 512 */
1055 SSVAL(outbuf
,smb_vwv3
,dfree
);
1057 SSVAL(outbuf
,smb_vwv0
,dsize
);
1058 SSVAL(outbuf
,smb_vwv1
,bsize
/512);
1059 SSVAL(outbuf
,smb_vwv2
,512);
1060 SSVAL(outbuf
,smb_vwv3
,dfree
);
1063 DEBUG(3,("dskattr dfree=%d\n", (unsigned int)dfree
));
1065 END_PROFILE(SMBdskattr
);
1069 /****************************************************************************
1071 Can be called from SMBsearch, SMBffirst or SMBfunique.
1072 ****************************************************************************/
1074 int reply_search(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
1084 unsigned int numentries
= 0;
1085 unsigned int maxentries
= 0;
1086 BOOL finished
= False
;
1093 BOOL check_descend
= False
;
1094 BOOL expect_close
= False
;
1095 BOOL can_open
= True
;
1096 BOOL bad_path
= False
;
1098 BOOL mask_contains_wcard
= False
;
1099 BOOL allow_long_path_components
= (SVAL(inbuf
,smb_flg2
) & FLAGS2_LONG_PATH_COMPONENTS
) ? True
: False
;
1101 START_PROFILE(SMBsearch
);
1103 if (lp_posix_pathnames()) {
1104 END_PROFILE(SMBsearch
);
1105 return reply_unknown(inbuf
, outbuf
);
1108 *mask
= *directory
= *fname
= 0;
1110 /* If we were called as SMBffirst then we must expect close. */
1111 if(CVAL(inbuf
,smb_com
) == SMBffirst
)
1112 expect_close
= True
;
1114 outsize
= set_message(outbuf
,1,3,True
);
1115 maxentries
= SVAL(inbuf
,smb_vwv0
);
1116 dirtype
= SVAL(inbuf
,smb_vwv1
);
1117 p
= smb_buf(inbuf
) + 1;
1118 p
+= srvstr_get_path_wcard(inbuf
, path
, p
, sizeof(path
), 0, STR_TERMINATE
, &nt_status
, &mask_contains_wcard
);
1119 if (!NT_STATUS_IS_OK(nt_status
)) {
1120 END_PROFILE(SMBsearch
);
1121 return ERROR_NT(nt_status
);
1124 RESOLVE_DFSPATH_WCARD(path
, conn
, inbuf
, outbuf
);
1127 status_len
= SVAL(p
, 0);
1130 /* dirtype &= ~aDIR; */
1132 if (status_len
== 0) {
1133 SMB_STRUCT_STAT sbuf
;
1136 pstrcpy(directory
,path
);
1138 unix_convert(directory
,conn
,0,&bad_path
,&sbuf
);
1141 if (!check_name(directory
,conn
))
1144 p
= strrchr_m(dir2
,'/');
1153 p
= strrchr_m(directory
,'/');
1159 if (strlen(directory
) == 0)
1160 pstrcpy(directory
,".");
1161 memset((char *)status
,'\0',21);
1162 SCVAL(status
,0,(dirtype
& 0x1F));
1166 memcpy(status
,p
,21);
1167 status_dirtype
= CVAL(status
,0) & 0x1F;
1168 if (status_dirtype
!= (dirtype
& 0x1F))
1169 dirtype
= status_dirtype
;
1171 conn
->dirptr
= dptr_fetch(status
+12,&dptr_num
);
1174 string_set(&conn
->dirpath
,dptr_path(dptr_num
));
1175 pstrcpy(mask
, dptr_wcard(dptr_num
));
1179 p
= smb_buf(outbuf
) + 3;
1182 if (status_len
== 0) {
1183 dptr_num
= dptr_create(conn
,directory
,True
,expect_close
,SVAL(inbuf
,smb_pid
), mask
, mask_contains_wcard
, dirtype
);
1185 if(dptr_num
== -2) {
1186 END_PROFILE(SMBsearch
);
1187 return set_bad_path_error(errno
, bad_path
, outbuf
, ERRDOS
, ERRnofids
);
1189 END_PROFILE(SMBsearch
);
1190 return ERROR_DOS(ERRDOS
,ERRnofids
);
1193 dirtype
= dptr_attr(dptr_num
);
1196 DEBUG(4,("dptr_num is %d\n",dptr_num
));
1199 if ((dirtype
&0x1F) == aVOLID
) {
1200 memcpy(p
,status
,21);
1201 make_dir_struct(p
,"???????????",volume_label(SNUM(conn
)),
1202 0,aVOLID
,0,!allow_long_path_components
);
1203 dptr_fill(p
+12,dptr_num
);
1204 if (dptr_zero(p
+12) && (status_len
==0))
1208 p
+= DIR_STRUCT_SIZE
;
1211 maxentries
= MIN(maxentries
, ((BUFFER_SIZE
- (p
- outbuf
))/DIR_STRUCT_SIZE
));
1213 DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
1214 conn
->dirpath
,lp_dontdescend(SNUM(conn
))));
1215 if (in_list(conn
->dirpath
, lp_dontdescend(SNUM(conn
)),True
))
1216 check_descend
= True
;
1218 for (i
=numentries
;(i
<maxentries
) && !finished
;i
++) {
1219 finished
= !get_dir_entry(conn
,mask
,dirtype
,fname
,&size
,&mode
,&date
,check_descend
);
1221 memcpy(p
,status
,21);
1222 make_dir_struct(p
,mask
,fname
,size
, mode
,date
,
1223 !allow_long_path_components
);
1224 if (!dptr_fill(p
+12,dptr_num
)) {
1228 p
+= DIR_STRUCT_SIZE
;
1238 /* If we were called as SMBffirst with smb_search_id == NULL
1239 and no entries were found then return error and close dirptr
1242 if (numentries
== 0 || !ok
) {
1243 dptr_close(&dptr_num
);
1244 } else if(ok
&& expect_close
&& status_len
== 0) {
1245 /* Close the dptr - we know it's gone */
1246 dptr_close(&dptr_num
);
1249 /* If we were called as SMBfunique, then we can close the dirptr now ! */
1250 if(dptr_num
>= 0 && CVAL(inbuf
,smb_com
) == SMBfunique
) {
1251 dptr_close(&dptr_num
);
1254 if ((numentries
== 0) && !mask_contains_wcard
) {
1255 return ERROR_BOTH(STATUS_NO_MORE_FILES
,ERRDOS
,ERRnofiles
);
1258 SSVAL(outbuf
,smb_vwv0
,numentries
);
1259 SSVAL(outbuf
,smb_vwv1
,3 + numentries
* DIR_STRUCT_SIZE
);
1260 SCVAL(smb_buf(outbuf
),0,5);
1261 SSVAL(smb_buf(outbuf
),1,numentries
*DIR_STRUCT_SIZE
);
1263 /* The replies here are never long name. */
1264 SSVAL(outbuf
,smb_flg2
,SVAL(outbuf
, smb_flg2
) & (~FLAGS2_IS_LONG_NAME
));
1265 if (!allow_long_path_components
) {
1266 SSVAL(outbuf
,smb_flg2
,SVAL(outbuf
, smb_flg2
) & (~FLAGS2_LONG_PATH_COMPONENTS
));
1269 /* This SMB *always* returns ASCII names. Remove the unicode bit in flags2. */
1270 SSVAL(outbuf
,smb_flg2
, (SVAL(outbuf
, smb_flg2
) & (~FLAGS2_UNICODE_STRINGS
)));
1272 outsize
+= DIR_STRUCT_SIZE
*numentries
;
1273 smb_setlen(outbuf
,outsize
- 4);
1275 if ((! *directory
) && dptr_path(dptr_num
))
1276 slprintf(directory
, sizeof(directory
)-1, "(%s)",dptr_path(dptr_num
));
1278 DEBUG( 4, ( "%s mask=%s path=%s dtype=%d nument=%u of %u\n",
1279 smb_fn_name(CVAL(inbuf
,smb_com
)),
1280 mask
, directory
, dirtype
, numentries
, maxentries
) );
1282 END_PROFILE(SMBsearch
);
1286 /****************************************************************************
1287 Reply to a fclose (stop directory search).
1288 ****************************************************************************/
1290 int reply_fclose(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
1299 BOOL path_contains_wcard
= False
;
1301 START_PROFILE(SMBfclose
);
1303 if (lp_posix_pathnames()) {
1304 END_PROFILE(SMBfclose
);
1305 return reply_unknown(inbuf
, outbuf
);
1308 outsize
= set_message(outbuf
,1,0,True
);
1309 p
= smb_buf(inbuf
) + 1;
1310 p
+= srvstr_get_path_wcard(inbuf
, path
, p
, sizeof(path
), 0, STR_TERMINATE
, &err
, &path_contains_wcard
);
1311 if (!NT_STATUS_IS_OK(err
)) {
1312 END_PROFILE(SMBfclose
);
1313 return ERROR_NT(err
);
1316 status_len
= SVAL(p
,0);
1319 if (status_len
== 0) {
1320 END_PROFILE(SMBfclose
);
1321 return ERROR_DOS(ERRSRV
,ERRsrverror
);
1324 memcpy(status
,p
,21);
1326 if(dptr_fetch(status
+12,&dptr_num
)) {
1327 /* Close the dptr - we know it's gone */
1328 dptr_close(&dptr_num
);
1331 SSVAL(outbuf
,smb_vwv0
,0);
1333 DEBUG(3,("search close\n"));
1335 END_PROFILE(SMBfclose
);
1339 /****************************************************************************
1341 ****************************************************************************/
1343 int reply_open(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
1351 SMB_STRUCT_STAT sbuf
;
1352 BOOL bad_path
= False
;
1354 int oplock_request
= CORE_OPLOCK_REQUEST(inbuf
);
1356 uint32 dos_attr
= SVAL(inbuf
,smb_vwv1
);
1359 uint32 create_disposition
;
1360 uint32 create_options
= 0;
1362 START_PROFILE(SMBopen
);
1364 deny_mode
= SVAL(inbuf
,smb_vwv0
);
1366 srvstr_get_path(inbuf
, fname
, smb_buf(inbuf
)+1, sizeof(fname
), 0, STR_TERMINATE
, &status
);
1367 if (!NT_STATUS_IS_OK(status
)) {
1368 END_PROFILE(SMBopen
);
1369 return ERROR_NT(status
);
1372 RESOLVE_DFSPATH(fname
, conn
, inbuf
, outbuf
);
1374 unix_convert(fname
,conn
,0,&bad_path
,&sbuf
);
1376 END_PROFILE(SMBopen
);
1377 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND
);
1380 if (!map_open_params_to_ntcreate(fname
, deny_mode
, OPENX_FILE_EXISTS_OPEN
,
1381 &access_mask
, &share_mode
, &create_disposition
, &create_options
)) {
1382 END_PROFILE(SMBopen
);
1383 return ERROR_NT(NT_STATUS_DOS(ERRDOS
, ERRbadaccess
));
1386 status
= open_file_ntcreate(conn
,fname
,&sbuf
,
1395 if (!NT_STATUS_IS_OK(status
)) {
1396 END_PROFILE(SMBopen
);
1397 if (open_was_deferred(SVAL(inbuf
,smb_mid
))) {
1398 /* We have re-scheduled this call. */
1401 return ERROR_NT(status
);
1404 size
= sbuf
.st_size
;
1405 fattr
= dos_mode(conn
,fname
,&sbuf
);
1406 mtime
= sbuf
.st_mtime
;
1409 DEBUG(3,("attempt to open a directory %s\n",fname
));
1410 close_file(fsp
,ERROR_CLOSE
);
1411 END_PROFILE(SMBopen
);
1412 return ERROR_DOS(ERRDOS
,ERRnoaccess
);
1415 outsize
= set_message(outbuf
,7,0,True
);
1416 SSVAL(outbuf
,smb_vwv0
,fsp
->fnum
);
1417 SSVAL(outbuf
,smb_vwv1
,fattr
);
1418 if(lp_dos_filetime_resolution(SNUM(conn
)) ) {
1419 srv_put_dos_date3(outbuf
,smb_vwv2
,mtime
& ~1);
1421 srv_put_dos_date3(outbuf
,smb_vwv2
,mtime
);
1423 SIVAL(outbuf
,smb_vwv4
,(uint32
)size
);
1424 SSVAL(outbuf
,smb_vwv6
,deny_mode
);
1426 if (oplock_request
&& lp_fake_oplocks(SNUM(conn
))) {
1427 SCVAL(outbuf
,smb_flg
,CVAL(outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
1430 if(EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1431 SCVAL(outbuf
,smb_flg
,CVAL(outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
1433 END_PROFILE(SMBopen
);
1437 /****************************************************************************
1438 Reply to an open and X.
1439 ****************************************************************************/
1441 int reply_open_and_X(connection_struct
*conn
, char *inbuf
,char *outbuf
,int length
,int bufsize
)
1444 uint16 open_flags
= SVAL(inbuf
,smb_vwv2
);
1445 int deny_mode
= SVAL(inbuf
,smb_vwv3
);
1446 uint32 smb_attr
= SVAL(inbuf
,smb_vwv5
);
1447 /* Breakout the oplock request bits so we can set the
1448 reply bits separately. */
1449 int ex_oplock_request
= EXTENDED_OPLOCK_REQUEST(inbuf
);
1450 int core_oplock_request
= CORE_OPLOCK_REQUEST(inbuf
);
1451 int oplock_request
= ex_oplock_request
| core_oplock_request
;
1453 int smb_sattr
= SVAL(inbuf
,smb_vwv4
);
1454 uint32 smb_time
= make_unix_date3(inbuf
+smb_vwv6
);
1456 int smb_ofun
= SVAL(inbuf
,smb_vwv8
);
1460 SMB_STRUCT_STAT sbuf
;
1462 BOOL bad_path
= False
;
1465 SMB_BIG_UINT allocation_size
= (SMB_BIG_UINT
)IVAL(inbuf
,smb_vwv9
);
1466 ssize_t retval
= -1;
1469 uint32 create_disposition
;
1470 uint32 create_options
= 0;
1472 START_PROFILE(SMBopenX
);
1474 /* If it's an IPC, pass off the pipe handler. */
1476 if (lp_nt_pipe_support()) {
1477 END_PROFILE(SMBopenX
);
1478 return reply_open_pipe_and_X(conn
, inbuf
,outbuf
,length
,bufsize
);
1480 END_PROFILE(SMBopenX
);
1481 return ERROR_DOS(ERRSRV
,ERRaccess
);
1485 /* XXXX we need to handle passed times, sattr and flags */
1486 srvstr_get_path(inbuf
, fname
, smb_buf(inbuf
), sizeof(fname
), 0, STR_TERMINATE
, &status
);
1487 if (!NT_STATUS_IS_OK(status
)) {
1488 END_PROFILE(SMBopenX
);
1489 return ERROR_NT(status
);
1492 RESOLVE_DFSPATH(fname
, conn
, inbuf
, outbuf
);
1494 unix_convert(fname
,conn
,0,&bad_path
,&sbuf
);
1496 END_PROFILE(SMBopenX
);
1497 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND
);
1500 if (!map_open_params_to_ntcreate(fname
, deny_mode
, smb_ofun
,
1503 &create_disposition
,
1505 END_PROFILE(SMBopenX
);
1506 return ERROR_NT(NT_STATUS_DOS(ERRDOS
, ERRbadaccess
));
1509 status
= open_file_ntcreate(conn
,fname
,&sbuf
,
1518 if (!NT_STATUS_IS_OK(status
)) {
1519 END_PROFILE(SMBopenX
);
1520 if (open_was_deferred(SVAL(inbuf
,smb_mid
))) {
1521 /* We have re-scheduled this call. */
1524 return ERROR_NT(status
);
1527 size
= sbuf
.st_size
;
1529 /* Setting the "size" field in vwv9 and vwv10 causes the file to be set to this size,
1530 if the file is truncated or created. */
1531 if (((smb_action
== FILE_WAS_CREATED
) || (smb_action
== FILE_WAS_OVERWRITTEN
)) && allocation_size
) {
1532 fsp
->initial_allocation_size
= smb_roundup(fsp
->conn
, allocation_size
);
1533 if (vfs_allocate_file_space(fsp
, fsp
->initial_allocation_size
) == -1) {
1534 close_file(fsp
,ERROR_CLOSE
);
1535 END_PROFILE(SMBopenX
);
1536 return ERROR_NT(NT_STATUS_DISK_FULL
);
1538 retval
= vfs_set_filelen(fsp
, (SMB_OFF_T
)allocation_size
);
1540 close_file(fsp
,ERROR_CLOSE
);
1541 END_PROFILE(SMBopenX
);
1542 return ERROR_NT(NT_STATUS_DISK_FULL
);
1544 size
= get_allocation_size(conn
,fsp
,&sbuf
);
1547 fattr
= dos_mode(conn
,fname
,&sbuf
);
1548 mtime
= sbuf
.st_mtime
;
1550 close_file(fsp
,ERROR_CLOSE
);
1551 END_PROFILE(SMBopenX
);
1552 return ERROR_DOS(ERRDOS
,ERRnoaccess
);
1555 /* If the caller set the extended oplock request bit
1556 and we granted one (by whatever means) - set the
1557 correct bit for extended oplock reply.
1560 if (ex_oplock_request
&& lp_fake_oplocks(SNUM(conn
))) {
1561 smb_action
|= EXTENDED_OPLOCK_GRANTED
;
1564 if(ex_oplock_request
&& EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1565 smb_action
|= EXTENDED_OPLOCK_GRANTED
;
1568 /* If the caller set the core oplock request bit
1569 and we granted one (by whatever means) - set the
1570 correct bit for core oplock reply.
1573 if (core_oplock_request
&& lp_fake_oplocks(SNUM(conn
))) {
1574 SCVAL(outbuf
,smb_flg
,CVAL(outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
1577 if(core_oplock_request
&& EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1578 SCVAL(outbuf
,smb_flg
,CVAL(outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
1581 if (open_flags
& EXTENDED_RESPONSE_REQUIRED
) {
1582 set_message(outbuf
,19,0,True
);
1584 set_message(outbuf
,15,0,True
);
1586 SSVAL(outbuf
,smb_vwv2
,fsp
->fnum
);
1587 SSVAL(outbuf
,smb_vwv3
,fattr
);
1588 if(lp_dos_filetime_resolution(SNUM(conn
)) ) {
1589 srv_put_dos_date3(outbuf
,smb_vwv4
,mtime
& ~1);
1591 srv_put_dos_date3(outbuf
,smb_vwv4
,mtime
);
1593 SIVAL(outbuf
,smb_vwv6
,(uint32
)size
);
1594 SSVAL(outbuf
,smb_vwv8
,GET_OPENX_MODE(deny_mode
));
1595 SSVAL(outbuf
,smb_vwv11
,smb_action
);
1597 if (open_flags
& EXTENDED_RESPONSE_REQUIRED
) {
1598 SIVAL(outbuf
, smb_vwv15
, STD_RIGHT_ALL_ACCESS
);
1601 END_PROFILE(SMBopenX
);
1602 return chain_reply(inbuf
,outbuf
,length
,bufsize
);
1605 /****************************************************************************
1606 Reply to a SMBulogoffX.
1607 conn POINTER CAN BE NULL HERE !
1608 ****************************************************************************/
1610 int reply_ulogoffX(connection_struct
*conn
, char *inbuf
,char *outbuf
,int length
,int bufsize
)
1612 uint16 vuid
= SVAL(inbuf
,smb_uid
);
1613 user_struct
*vuser
= get_valid_user_struct(vuid
);
1614 START_PROFILE(SMBulogoffX
);
1617 DEBUG(3,("ulogoff, vuser id %d does not map to user.\n", vuid
));
1619 /* in user level security we are supposed to close any files
1620 open by this user */
1621 if ((vuser
!= 0) && (lp_security() != SEC_SHARE
))
1622 file_close_user(vuid
);
1624 invalidate_vuid(vuid
);
1626 set_message(outbuf
,2,0,True
);
1628 DEBUG( 3, ( "ulogoffX vuid=%d\n", vuid
) );
1630 END_PROFILE(SMBulogoffX
);
1631 return chain_reply(inbuf
,outbuf
,length
,bufsize
);
1634 /****************************************************************************
1635 Reply to a mknew or a create.
1636 ****************************************************************************/
1638 int reply_mknew(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
1643 uint32 fattr
= SVAL(inbuf
,smb_vwv0
);
1644 BOOL bad_path
= False
;
1646 int oplock_request
= CORE_OPLOCK_REQUEST(inbuf
);
1647 SMB_STRUCT_STAT sbuf
;
1649 uint32 access_mask
= FILE_GENERIC_READ
| FILE_GENERIC_WRITE
;
1650 uint32 share_mode
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
1651 uint32 create_disposition
;
1652 uint32 create_options
= 0;
1654 START_PROFILE(SMBcreate
);
1656 com
= SVAL(inbuf
,smb_com
);
1658 srvstr_get_path(inbuf
, fname
, smb_buf(inbuf
) + 1, sizeof(fname
), 0, STR_TERMINATE
, &status
);
1659 if (!NT_STATUS_IS_OK(status
)) {
1660 END_PROFILE(SMBcreate
);
1661 return ERROR_NT(status
);
1664 RESOLVE_DFSPATH(fname
, conn
, inbuf
, outbuf
);
1666 unix_convert(fname
,conn
,0,&bad_path
,&sbuf
);
1668 END_PROFILE(SMBcreate
);
1669 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND
);
1672 if (fattr
& aVOLID
) {
1673 DEBUG(0,("Attempt to create file (%s) with volid set - please report this\n",fname
));
1676 if(com
== SMBmknew
) {
1677 /* We should fail if file exists. */
1678 create_disposition
= FILE_CREATE
;
1680 /* Create if file doesn't exist, truncate if it does. */
1681 create_disposition
= FILE_OVERWRITE_IF
;
1684 /* Open file using ntcreate. */
1685 status
= open_file_ntcreate(conn
,fname
,&sbuf
,
1694 if (!NT_STATUS_IS_OK(status
)) {
1695 END_PROFILE(SMBcreate
);
1696 if (open_was_deferred(SVAL(inbuf
,smb_mid
))) {
1697 /* We have re-scheduled this call. */
1700 return ERROR_NT(status
);
1703 outsize
= set_message(outbuf
,1,0,True
);
1704 SSVAL(outbuf
,smb_vwv0
,fsp
->fnum
);
1706 if (oplock_request
&& lp_fake_oplocks(SNUM(conn
))) {
1707 SCVAL(outbuf
,smb_flg
,CVAL(outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
1710 if(EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1711 SCVAL(outbuf
,smb_flg
,CVAL(outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
1714 DEBUG( 2, ( "reply_mknew: file %s\n", fname
) );
1715 DEBUG( 3, ( "reply_mknew %s fd=%d dmode=0x%x\n", fname
, fsp
->fh
->fd
, (unsigned int)fattr
) );
1717 END_PROFILE(SMBcreate
);
1721 /****************************************************************************
1722 Reply to a create temporary file.
1723 ****************************************************************************/
1725 int reply_ctemp(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
1729 uint32 fattr
= SVAL(inbuf
,smb_vwv0
);
1730 BOOL bad_path
= False
;
1732 int oplock_request
= CORE_OPLOCK_REQUEST(inbuf
);
1734 SMB_STRUCT_STAT sbuf
;
1737 unsigned int namelen
;
1739 START_PROFILE(SMBctemp
);
1741 srvstr_get_path(inbuf
, fname
, smb_buf(inbuf
)+1, sizeof(fname
), 0, STR_TERMINATE
, &status
);
1742 if (!NT_STATUS_IS_OK(status
)) {
1743 END_PROFILE(SMBctemp
);
1744 return ERROR_NT(status
);
1747 pstrcat(fname
,"/TMXXXXXX");
1749 pstrcat(fname
,"TMXXXXXX");
1752 RESOLVE_DFSPATH(fname
, conn
, inbuf
, outbuf
);
1754 unix_convert(fname
,conn
,0,&bad_path
,&sbuf
);
1756 END_PROFILE(SMBctemp
);
1757 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND
);
1760 tmpfd
= smb_mkstemp(fname
);
1762 END_PROFILE(SMBctemp
);
1763 return(UNIXERROR(ERRDOS
,ERRnoaccess
));
1766 SMB_VFS_STAT(conn
,fname
,&sbuf
);
1768 /* We should fail if file does not exist. */
1769 status
= open_file_ntcreate(conn
,fname
,&sbuf
,
1770 FILE_GENERIC_READ
| FILE_GENERIC_WRITE
,
1771 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1778 /* close fd from smb_mkstemp() */
1781 if (!NT_STATUS_IS_OK(status
)) {
1782 END_PROFILE(SMBctemp
);
1783 if (open_was_deferred(SVAL(inbuf
,smb_mid
))) {
1784 /* We have re-scheduled this call. */
1787 return ERROR_NT(status
);
1790 outsize
= set_message(outbuf
,1,0,True
);
1791 SSVAL(outbuf
,smb_vwv0
,fsp
->fnum
);
1793 /* the returned filename is relative to the directory */
1794 s
= strrchr_m(fname
, '/');
1801 p
= smb_buf(outbuf
);
1803 /* Tested vs W2K3 - this doesn't seem to be here - null terminated filename is the only
1804 thing in the byte section. JRA */
1805 SSVALS(p
, 0, -1); /* what is this? not in spec */
1807 namelen
= srvstr_push(outbuf
, p
, s
, -1, STR_ASCII
|STR_TERMINATE
);
1809 outsize
= set_message_end(outbuf
, p
);
1811 if (oplock_request
&& lp_fake_oplocks(SNUM(conn
))) {
1812 SCVAL(outbuf
,smb_flg
,CVAL(outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
1815 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1816 SCVAL(outbuf
,smb_flg
,CVAL(outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
1819 DEBUG( 2, ( "reply_ctemp: created temp file %s\n", fname
) );
1820 DEBUG( 3, ( "reply_ctemp %s fd=%d umode=0%o\n", fname
, fsp
->fh
->fd
,
1821 (unsigned int)sbuf
.st_mode
) );
1823 END_PROFILE(SMBctemp
);
1827 /*******************************************************************
1828 Check if a user is allowed to rename a file.
1829 ********************************************************************/
1831 static NTSTATUS
can_rename(connection_struct
*conn
, char *fname
, uint16 dirtype
, SMB_STRUCT_STAT
*pst
)
1837 if (!CAN_WRITE(conn
)) {
1838 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1841 fmode
= dos_mode(conn
,fname
,pst
);
1842 if ((fmode
& ~dirtype
) & (aHIDDEN
| aSYSTEM
)) {
1843 return NT_STATUS_NO_SUCH_FILE
;
1846 if (S_ISDIR(pst
->st_mode
)) {
1847 return NT_STATUS_OK
;
1850 status
= open_file_ntcreate(conn
, fname
, pst
,
1852 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1855 FILE_ATTRIBUTE_NORMAL
,
1859 if (!NT_STATUS_IS_OK(status
)) {
1860 return NT_STATUS_ACCESS_DENIED
;
1862 close_file(fsp
,NORMAL_CLOSE
);
1863 return NT_STATUS_OK
;
1866 /*******************************************************************
1867 Check if a user is allowed to delete a file.
1868 ********************************************************************/
1870 NTSTATUS
can_delete(connection_struct
*conn
, char *fname
, uint32 dirtype
, BOOL bad_path
, BOOL check_is_at_open
)
1872 SMB_STRUCT_STAT sbuf
;
1877 DEBUG(10,("can_delete: %s, dirtype = %d\n", fname
, dirtype
));
1879 if (!CAN_WRITE(conn
)) {
1880 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
1883 if (SMB_VFS_LSTAT(conn
,fname
,&sbuf
) != 0) {
1884 if(errno
== ENOENT
) {
1886 return NT_STATUS_OBJECT_PATH_NOT_FOUND
;
1888 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1891 return map_nt_error_from_unix(errno
);
1894 fattr
= dos_mode(conn
,fname
,&sbuf
);
1896 /* Can't delete a directory. */
1898 return NT_STATUS_FILE_IS_A_DIRECTORY
;
1902 else if (dirtype
& aDIR
) /* Asked for a directory and it isn't. */
1903 return NT_STATUS_OBJECT_NAME_INVALID
;
1904 #endif /* JRATEST */
1906 /* Fix for bug #3035 from SATOH Fumiyasu <fumiyas@miraclelinux.com>
1908 On a Windows share, a file with read-only dosmode can be opened with
1909 DELETE_ACCESS. But on a Samba share (delete readonly = no), it
1910 fails with NT_STATUS_CANNOT_DELETE error.
1912 This semantic causes a problem that a user can not
1913 rename a file with read-only dosmode on a Samba share
1914 from a Windows command prompt (i.e. cmd.exe, but can rename
1915 from Windows Explorer).
1918 if (!check_is_at_open
&& !lp_delete_readonly(SNUM(conn
))) {
1919 if (fattr
& aRONLY
) {
1920 return NT_STATUS_CANNOT_DELETE
;
1923 if ((fattr
& ~dirtype
) & (aHIDDEN
| aSYSTEM
)) {
1924 return NT_STATUS_NO_SUCH_FILE
;
1927 if (check_is_at_open
) {
1928 if (!can_delete_file_in_directory(conn
, fname
)) {
1929 return NT_STATUS_ACCESS_DENIED
;
1932 /* On open checks the open itself will check the share mode, so
1933 don't do it here as we'll get it wrong. */
1935 status
= open_file_ntcreate(conn
, fname
, &sbuf
,
1940 FILE_ATTRIBUTE_NORMAL
,
1944 if (!NT_STATUS_IS_OK(status
)) {
1947 close_file(fsp
,NORMAL_CLOSE
);
1949 return NT_STATUS_OK
;
1952 /****************************************************************************
1953 The guts of the unlink command, split out so it may be called by the NT SMB
1955 ****************************************************************************/
1957 NTSTATUS
unlink_internals(connection_struct
*conn
, uint32 dirtype
, char *name
, BOOL has_wild
)
1963 NTSTATUS error
= NT_STATUS_OK
;
1964 BOOL bad_path
= False
;
1966 SMB_STRUCT_STAT sbuf
;
1968 *directory
= *mask
= 0;
1970 rc
= unix_convert(name
,conn
,0,&bad_path
,&sbuf
);
1972 p
= strrchr_m(name
,'/');
1974 pstrcpy(directory
,".");
1978 pstrcpy(directory
,name
);
1983 * We should only check the mangled cache
1984 * here if unix_convert failed. This means
1985 * that the path in 'mask' doesn't exist
1986 * on the file system and so we need to look
1987 * for a possible mangle. This patch from
1988 * Tine Smukavec <valentin.smukavec@hermes.si>.
1991 if (!rc
&& mangle_is_mangled(mask
,conn
->params
))
1992 mangle_check_cache( mask
, sizeof(pstring
)-1, conn
->params
);
1995 pstrcat(directory
,"/");
1996 pstrcat(directory
,mask
);
1997 error
= can_delete(conn
,directory
,dirtype
,bad_path
,False
);
1998 if (!NT_STATUS_IS_OK(error
))
2001 if (SMB_VFS_UNLINK(conn
,directory
) == 0) {
2005 struct smb_Dir
*dir_hnd
= NULL
;
2008 if (strequal(mask
,"????????.???"))
2011 if (check_name(directory
,conn
))
2012 dir_hnd
= OpenDir(conn
, directory
, mask
, dirtype
);
2014 /* XXXX the CIFS spec says that if bit0 of the flags2 field is set then
2015 the pattern matches against the long name, otherwise the short name
2016 We don't implement this yet XXXX
2021 error
= NT_STATUS_NO_SUCH_FILE
;
2023 while ((dname
= ReadDirName(dir_hnd
, &offset
))) {
2026 BOOL sys_direntry
= False
;
2027 pstrcpy(fname
,dname
);
2029 if (!is_visible_file(conn
, directory
, dname
, &st
, True
)) {
2033 /* Quick check for "." and ".." */
2034 if (fname
[0] == '.') {
2035 if (!fname
[1] || (fname
[1] == '.' && !fname
[2])) {
2036 if ((dirtype
& FILE_ATTRIBUTE_DIRECTORY
) && (dirtype
& FILE_ATTRIBUTE_SYSTEM
)) {
2037 sys_direntry
= True
;
2044 if(!mask_match(fname
, mask
, conn
->case_sensitive
))
2048 error
= NT_STATUS_OBJECT_NAME_INVALID
;
2049 DEBUG(3,("unlink_internals: system directory delete denied [%s] mask [%s]\n",
2054 slprintf(fname
,sizeof(fname
)-1, "%s/%s",directory
,dname
);
2055 error
= can_delete(conn
,fname
,dirtype
,bad_path
,False
);
2056 if (!NT_STATUS_IS_OK(error
)) {
2059 if (SMB_VFS_UNLINK(conn
,fname
) == 0)
2061 DEBUG(3,("unlink_internals: succesful unlink [%s]\n",fname
));
2067 if (count
== 0 && NT_STATUS_IS_OK(error
)) {
2068 error
= map_nt_error_from_unix(errno
);
2074 /****************************************************************************
2076 ****************************************************************************/
2078 int reply_unlink(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
,
2085 BOOL path_contains_wcard
= False
;
2087 START_PROFILE(SMBunlink
);
2089 dirtype
= SVAL(inbuf
,smb_vwv0
);
2091 srvstr_get_path_wcard(inbuf
, name
, smb_buf(inbuf
) + 1, sizeof(name
), 0, STR_TERMINATE
, &status
, &path_contains_wcard
);
2092 if (!NT_STATUS_IS_OK(status
)) {
2093 END_PROFILE(SMBunlink
);
2094 return ERROR_NT(status
);
2097 RESOLVE_DFSPATH_WCARD(name
, conn
, inbuf
, outbuf
);
2099 DEBUG(3,("reply_unlink : %s\n",name
));
2101 status
= unlink_internals(conn
, dirtype
, name
, path_contains_wcard
);
2102 if (!NT_STATUS_IS_OK(status
)) {
2103 if (open_was_deferred(SVAL(inbuf
,smb_mid
))) {
2104 /* We have re-scheduled this call. */
2107 return ERROR_NT(status
);
2111 * Win2k needs a changenotify request response before it will
2112 * update after a rename..
2114 process_pending_change_notify_queue((time_t)0);
2116 outsize
= set_message(outbuf
,0,0,False
);
2118 END_PROFILE(SMBunlink
);
2122 /****************************************************************************
2124 ****************************************************************************/
2126 static void fail_readraw(void)
2129 slprintf(errstr
, sizeof(errstr
)-1, "FAIL ! reply_readbraw: socket write fail (%s)",
2131 exit_server(errstr
);
2134 #if defined(WITH_SENDFILE)
2135 /****************************************************************************
2136 Fake (read/write) sendfile. Returns -1 on read or write fail.
2137 ****************************************************************************/
2139 static ssize_t
fake_sendfile(files_struct
*fsp
, SMB_OFF_T startpos
, size_t nread
, char *buf
, int bufsize
)
2143 /* Paranioa check... */
2144 if (nread
> bufsize
) {
2149 ret
= read_file(fsp
,buf
,startpos
,nread
);
2155 /* If we had a short read, fill with zeros. */
2157 memset(buf
, '\0', nread
- ret
);
2160 if (write_data(smbd_server_fd(),buf
,nread
) != nread
) {
2164 return (ssize_t
)nread
;
2168 /****************************************************************************
2169 Use sendfile in readbraw.
2170 ****************************************************************************/
2172 void send_file_readbraw(connection_struct
*conn
, files_struct
*fsp
, SMB_OFF_T startpos
, size_t nread
,
2173 ssize_t mincount
, char *outbuf
, int out_buffsize
)
2177 #if defined(WITH_SENDFILE)
2179 * We can only use sendfile on a non-chained packet
2180 * but we can use on a non-oplocked file. tridge proved this
2181 * on a train in Germany :-). JRA.
2182 * reply_readbraw has already checked the length.
2185 if ( (chain_size
== 0) && (nread
> 0) &&
2186 (fsp
->wcp
== NULL
) && lp_use_sendfile(SNUM(conn
)) ) {
2189 _smb_setlen(outbuf
,nread
);
2190 header
.data
= (uint8
*)outbuf
;
2194 if ( SMB_VFS_SENDFILE( smbd_server_fd(), fsp
, fsp
->fh
->fd
, &header
, startpos
, nread
) == -1) {
2195 /* Returning ENOSYS means no data at all was sent. Do this as a normal read. */
2196 if (errno
== ENOSYS
) {
2197 goto normal_readbraw
;
2201 * Special hack for broken Linux with no working sendfile. If we
2202 * return EINTR we sent the header but not the rest of the data.
2203 * Fake this up by doing read/write calls.
2205 if (errno
== EINTR
) {
2206 /* Ensure we don't do this again. */
2207 set_use_sendfile(SNUM(conn
), False
);
2208 DEBUG(0,("send_file_readbraw: sendfile not available. Faking..\n"));
2210 if (fake_sendfile(fsp
, startpos
, nread
, outbuf
+ 4, out_buffsize
- 4) == -1) {
2211 DEBUG(0,("send_file_readbraw: fake_sendfile failed for file %s (%s).\n",
2212 fsp
->fsp_name
, strerror(errno
) ));
2213 exit_server("send_file_readbraw fake_sendfile failed");
2218 DEBUG(0,("send_file_readbraw: sendfile failed for file %s (%s). Terminating\n",
2219 fsp
->fsp_name
, strerror(errno
) ));
2220 exit_server("send_file_readbraw sendfile failed");
2230 ret
= read_file(fsp
,outbuf
+4,startpos
,nread
);
2231 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2240 _smb_setlen(outbuf
,ret
);
2241 if (write_data(smbd_server_fd(),outbuf
,4+ret
) != 4+ret
)
2245 /****************************************************************************
2246 Reply to a readbraw (core+ protocol).
2247 ****************************************************************************/
2249 int reply_readbraw(connection_struct
*conn
, char *inbuf
, char *outbuf
, int dum_size
, int out_buffsize
)
2251 ssize_t maxcount
,mincount
;
2254 char *header
= outbuf
;
2256 START_PROFILE(SMBreadbraw
);
2258 if (srv_is_signing_active()) {
2259 exit_server("reply_readbraw: SMB signing is active - raw reads/writes are disallowed.");
2263 * Special check if an oplock break has been issued
2264 * and the readraw request croses on the wire, we must
2265 * return a zero length response here.
2268 fsp
= file_fsp(inbuf
,smb_vwv0
);
2270 if (!FNUM_OK(fsp
,conn
) || !fsp
->can_read
) {
2272 * fsp could be NULL here so use the value from the packet. JRA.
2274 DEBUG(3,("fnum %d not open in readbraw - cache prime?\n",(int)SVAL(inbuf
,smb_vwv0
)));
2275 _smb_setlen(header
,0);
2276 if (write_data(smbd_server_fd(),header
,4) != 4)
2278 END_PROFILE(SMBreadbraw
);
2282 CHECK_FSP(fsp
,conn
);
2284 flush_write_cache(fsp
, READRAW_FLUSH
);
2286 startpos
= IVAL_TO_SMB_OFF_T(inbuf
,smb_vwv1
);
2287 if(CVAL(inbuf
,smb_wct
) == 10) {
2289 * This is a large offset (64 bit) read.
2291 #ifdef LARGE_SMB_OFF_T
2293 startpos
|= (((SMB_OFF_T
)IVAL(inbuf
,smb_vwv8
)) << 32);
2295 #else /* !LARGE_SMB_OFF_T */
2298 * Ensure we haven't been sent a >32 bit offset.
2301 if(IVAL(inbuf
,smb_vwv8
) != 0) {
2302 DEBUG(0,("readbraw - large offset (%x << 32) used and we don't support \
2303 64 bit offsets.\n", (unsigned int)IVAL(inbuf
,smb_vwv8
) ));
2304 _smb_setlen(header
,0);
2305 if (write_data(smbd_server_fd(),header
,4) != 4)
2307 END_PROFILE(SMBreadbraw
);
2311 #endif /* LARGE_SMB_OFF_T */
2314 DEBUG(0,("readbraw - negative 64 bit readraw offset (%.0f) !\n", (double)startpos
));
2315 _smb_setlen(header
,0);
2316 if (write_data(smbd_server_fd(),header
,4) != 4)
2318 END_PROFILE(SMBreadbraw
);
2322 maxcount
= (SVAL(inbuf
,smb_vwv3
) & 0xFFFF);
2323 mincount
= (SVAL(inbuf
,smb_vwv4
) & 0xFFFF);
2325 /* ensure we don't overrun the packet size */
2326 maxcount
= MIN(65535,maxcount
);
2328 if (!is_locked(fsp
,(uint32
)SVAL(inbuf
,smb_pid
),(SMB_BIG_UINT
)maxcount
,(SMB_BIG_UINT
)startpos
, READ_LOCK
)) {
2332 if (SMB_VFS_FSTAT(fsp
,fsp
->fh
->fd
,&st
) == 0) {
2336 if (startpos
>= size
) {
2339 nread
= MIN(maxcount
,(size
- startpos
));
2343 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2344 if (nread
< mincount
)
2348 DEBUG( 3, ( "readbraw fnum=%d start=%.0f max=%lu min=%lu nread=%lu\n", fsp
->fnum
, (double)startpos
,
2349 (unsigned long)maxcount
, (unsigned long)mincount
, (unsigned long)nread
) );
2351 send_file_readbraw(conn
, fsp
, startpos
, nread
, mincount
, outbuf
, out_buffsize
);
2353 DEBUG(5,("readbraw finished\n"));
2354 END_PROFILE(SMBreadbraw
);
2359 #define DBGC_CLASS DBGC_LOCKING
2361 /****************************************************************************
2362 Reply to a lockread (core+ protocol).
2363 ****************************************************************************/
2365 int reply_lockread(connection_struct
*conn
, char *inbuf
,char *outbuf
, int length
, int dum_buffsiz
)
2373 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
2374 START_PROFILE(SMBlockread
);
2376 CHECK_FSP(fsp
,conn
);
2377 if (!CHECK_READ(fsp
,inbuf
)) {
2378 return(ERROR_DOS(ERRDOS
,ERRbadaccess
));
2381 release_level_2_oplocks_on_change(fsp
);
2383 numtoread
= SVAL(inbuf
,smb_vwv1
);
2384 startpos
= IVAL_TO_SMB_OFF_T(inbuf
,smb_vwv2
);
2386 outsize
= set_message(outbuf
,5,3,True
);
2387 numtoread
= MIN(BUFFER_SIZE
-outsize
,numtoread
);
2388 data
= smb_buf(outbuf
) + 3;
2391 * NB. Discovered by Menny Hamburger at Mainsoft. This is a core+
2392 * protocol request that predates the read/write lock concept.
2393 * Thus instead of asking for a read lock here we need to ask
2394 * for a write lock. JRA.
2395 * Note that the requested lock size is unaffected by max_recv.
2398 status
= do_lock(fsp
,
2399 (uint32
)SVAL(inbuf
,smb_pid
),
2400 (SMB_BIG_UINT
)numtoread
,
2401 (SMB_BIG_UINT
)startpos
,
2404 0 /* zero timeout. */);
2406 if (NT_STATUS_V(status
)) {
2409 * We used to make lockread a blocking lock. It turns out
2410 * that this isn't on W2k. Found by the Samba 4 RAW-READ torture
2414 if (lp_blocking_locks(SNUM(conn
)) && ERROR_WAS_LOCK_DENIED(status
)) {
2416 * A blocking lock was requested. Package up
2417 * this smb into a queued request and push it
2418 * onto the blocking lock queue.
2420 if(push_blocking_lock_request(inbuf
, length
,
2424 SVAL(inbuf
,smb_pid
),
2427 (SMB_BIG_UINT
)startpos
,
2428 (SMB_BIG_UINT
)numtoread
)) {
2429 END_PROFILE(SMBlockread
);
2434 END_PROFILE(SMBlockread
);
2435 return ERROR_NT(status
);
2439 * However the requested READ size IS affected by max_recv. Insanity.... JRA.
2442 if (numtoread
> max_recv
) {
2443 DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u). \
2444 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
2445 (unsigned int)numtoread
, (unsigned int)max_recv
));
2446 numtoread
= MIN(numtoread
,max_recv
);
2448 nread
= read_file(fsp
,data
,startpos
,numtoread
);
2451 END_PROFILE(SMBlockread
);
2452 return(UNIXERROR(ERRDOS
,ERRnoaccess
));
2456 SSVAL(outbuf
,smb_vwv0
,nread
);
2457 SSVAL(outbuf
,smb_vwv5
,nread
+3);
2458 SSVAL(smb_buf(outbuf
),1,nread
);
2460 DEBUG(3,("lockread fnum=%d num=%d nread=%d\n",
2461 fsp
->fnum
, (int)numtoread
, (int)nread
));
2463 END_PROFILE(SMBlockread
);
2468 #define DBGC_CLASS DBGC_ALL
2470 /****************************************************************************
2472 ****************************************************************************/
2474 int reply_read(connection_struct
*conn
, char *inbuf
,char *outbuf
, int size
, int dum_buffsize
)
2481 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
2482 START_PROFILE(SMBread
);
2484 CHECK_FSP(fsp
,conn
);
2485 if (!CHECK_READ(fsp
,inbuf
)) {
2486 return(ERROR_DOS(ERRDOS
,ERRbadaccess
));
2489 numtoread
= SVAL(inbuf
,smb_vwv1
);
2490 startpos
= IVAL_TO_SMB_OFF_T(inbuf
,smb_vwv2
);
2492 outsize
= set_message(outbuf
,5,3,True
);
2493 numtoread
= MIN(BUFFER_SIZE
-outsize
,numtoread
);
2495 * The requested read size cannot be greater than max_recv. JRA.
2497 if (numtoread
> max_recv
) {
2498 DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u). \
2499 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
2500 (unsigned int)numtoread
, (unsigned int)max_recv
));
2501 numtoread
= MIN(numtoread
,max_recv
);
2504 data
= smb_buf(outbuf
) + 3;
2506 if (is_locked(fsp
,(uint32
)SVAL(inbuf
,smb_pid
),(SMB_BIG_UINT
)numtoread
,(SMB_BIG_UINT
)startpos
, READ_LOCK
)) {
2507 END_PROFILE(SMBread
);
2508 return ERROR_DOS(ERRDOS
,ERRlock
);
2512 nread
= read_file(fsp
,data
,startpos
,numtoread
);
2515 END_PROFILE(SMBread
);
2516 return(UNIXERROR(ERRDOS
,ERRnoaccess
));
2520 SSVAL(outbuf
,smb_vwv0
,nread
);
2521 SSVAL(outbuf
,smb_vwv5
,nread
+3);
2522 SCVAL(smb_buf(outbuf
),0,1);
2523 SSVAL(smb_buf(outbuf
),1,nread
);
2525 DEBUG( 3, ( "read fnum=%d num=%d nread=%d\n",
2526 fsp
->fnum
, (int)numtoread
, (int)nread
) );
2528 END_PROFILE(SMBread
);
2532 /****************************************************************************
2533 Reply to a read and X - possibly using sendfile.
2534 ****************************************************************************/
2536 int send_file_readX(connection_struct
*conn
, char *inbuf
,char *outbuf
,int length
, int len_outbuf
,
2537 files_struct
*fsp
, SMB_OFF_T startpos
, size_t smb_maxcnt
)
2541 char *data
= smb_buf(outbuf
);
2543 #if defined(WITH_SENDFILE)
2545 * We can only use sendfile on a non-chained packet
2546 * but we can use on a non-oplocked file. tridge proved this
2547 * on a train in Germany :-). JRA.
2550 if ((chain_size
== 0) && (CVAL(inbuf
,smb_vwv0
) == 0xFF) &&
2551 lp_use_sendfile(SNUM(conn
)) && (fsp
->wcp
== NULL
) ) {
2552 SMB_STRUCT_STAT sbuf
;
2555 if(SMB_VFS_FSTAT(fsp
,fsp
->fh
->fd
, &sbuf
) == -1)
2556 return(UNIXERROR(ERRDOS
,ERRnoaccess
));
2558 if (startpos
> sbuf
.st_size
)
2561 if (smb_maxcnt
> (sbuf
.st_size
- startpos
))
2562 smb_maxcnt
= (sbuf
.st_size
- startpos
);
2564 if (smb_maxcnt
== 0)
2568 * Set up the packet header before send. We
2569 * assume here the sendfile will work (get the
2570 * correct amount of data).
2573 SSVAL(outbuf
,smb_vwv2
,0xFFFF); /* Remaining - must be -1. */
2574 SSVAL(outbuf
,smb_vwv5
,smb_maxcnt
);
2575 SSVAL(outbuf
,smb_vwv6
,smb_offset(data
,outbuf
));
2576 SSVAL(outbuf
,smb_vwv7
,((smb_maxcnt
>> 16) & 1));
2577 SSVAL(smb_buf(outbuf
),-2,smb_maxcnt
);
2578 SCVAL(outbuf
,smb_vwv0
,0xFF);
2579 set_message(outbuf
,12,smb_maxcnt
,False
);
2580 header
.data
= (uint8
*)outbuf
;
2581 header
.length
= data
- outbuf
;
2584 if ((nread
= SMB_VFS_SENDFILE( smbd_server_fd(), fsp
, fsp
->fh
->fd
, &header
, startpos
, smb_maxcnt
)) == -1) {
2585 /* Returning ENOSYS means no data at all was sent. Do this as a normal read. */
2586 if (errno
== ENOSYS
) {
2591 * Special hack for broken Linux with no working sendfile. If we
2592 * return EINTR we sent the header but not the rest of the data.
2593 * Fake this up by doing read/write calls.
2596 if (errno
== EINTR
) {
2597 /* Ensure we don't do this again. */
2598 set_use_sendfile(SNUM(conn
), False
);
2599 DEBUG(0,("send_file_readX: sendfile not available. Faking..\n"));
2601 if ((nread
= fake_sendfile(fsp
, startpos
, smb_maxcnt
, data
,
2602 len_outbuf
- (data
-outbuf
))) == -1) {
2603 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
2604 fsp
->fsp_name
, strerror(errno
) ));
2605 exit_server("send_file_readX: fake_sendfile failed");
2607 DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
2608 fsp
->fnum
, (int)smb_maxcnt
, (int)nread
) );
2609 /* Returning -1 here means successful sendfile. */
2613 DEBUG(0,("send_file_readX: sendfile failed for file %s (%s). Terminating\n",
2614 fsp
->fsp_name
, strerror(errno
) ));
2615 exit_server("send_file_readX sendfile failed");
2618 DEBUG( 3, ( "send_file_readX: sendfile fnum=%d max=%d nread=%d\n",
2619 fsp
->fnum
, (int)smb_maxcnt
, (int)nread
) );
2620 /* Returning -1 here means successful sendfile. */
2628 nread
= read_file(fsp
,data
,startpos
,smb_maxcnt
);
2631 return(UNIXERROR(ERRDOS
,ERRnoaccess
));
2634 outsize
= set_message(outbuf
,12,nread
,False
);
2635 SSVAL(outbuf
,smb_vwv2
,0xFFFF); /* Remaining - must be -1. */
2636 SSVAL(outbuf
,smb_vwv5
,nread
);
2637 SSVAL(outbuf
,smb_vwv6
,smb_offset(data
,outbuf
));
2638 SSVAL(outbuf
,smb_vwv7
,((nread
>> 16) & 1));
2639 SSVAL(smb_buf(outbuf
),-2,nread
);
2641 DEBUG( 3, ( "send_file_readX fnum=%d max=%d nread=%d\n",
2642 fsp
->fnum
, (int)smb_maxcnt
, (int)nread
) );
2644 /* Returning the number of bytes we want to send back - including header. */
2648 /****************************************************************************
2649 Reply to a read and X.
2650 ****************************************************************************/
2652 int reply_read_and_X(connection_struct
*conn
, char *inbuf
,char *outbuf
,int length
,int bufsize
)
2654 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv2
);
2655 SMB_OFF_T startpos
= IVAL_TO_SMB_OFF_T(inbuf
,smb_vwv3
);
2657 size_t smb_maxcnt
= SVAL(inbuf
,smb_vwv5
);
2659 size_t smb_mincnt
= SVAL(inbuf
,smb_vwv6
);
2662 START_PROFILE(SMBreadX
);
2664 /* If it's an IPC, pass off the pipe handler. */
2666 END_PROFILE(SMBreadX
);
2667 return reply_pipe_read_and_X(inbuf
,outbuf
,length
,bufsize
);
2670 CHECK_FSP(fsp
,conn
);
2671 if (!CHECK_READ(fsp
,inbuf
)) {
2672 return(ERROR_DOS(ERRDOS
,ERRbadaccess
));
2675 set_message(outbuf
,12,0,True
);
2677 if (global_client_caps
& CAP_LARGE_READX
) {
2678 if (SVAL(inbuf
,smb_vwv7
) == 1) {
2679 smb_maxcnt
|= (1<<16);
2681 if (smb_maxcnt
> BUFFER_SIZE
) {
2682 DEBUG(0,("reply_read_and_X - read too large (%u) for reply buffer %u\n",
2683 (unsigned int)smb_maxcnt
, (unsigned int)BUFFER_SIZE
));
2684 END_PROFILE(SMBreadX
);
2685 return ERROR_NT(NT_STATUS_INVALID_PARAMETER
);
2689 if(CVAL(inbuf
,smb_wct
) == 12) {
2690 #ifdef LARGE_SMB_OFF_T
2692 * This is a large offset (64 bit) read.
2694 startpos
|= (((SMB_OFF_T
)IVAL(inbuf
,smb_vwv10
)) << 32);
2696 #else /* !LARGE_SMB_OFF_T */
2699 * Ensure we haven't been sent a >32 bit offset.
2702 if(IVAL(inbuf
,smb_vwv10
) != 0) {
2703 DEBUG(0,("reply_read_and_X - large offset (%x << 32) used and we don't support \
2704 64 bit offsets.\n", (unsigned int)IVAL(inbuf
,smb_vwv10
) ));
2705 END_PROFILE(SMBreadX
);
2706 return ERROR_DOS(ERRDOS
,ERRbadaccess
);
2709 #endif /* LARGE_SMB_OFF_T */
2713 if (is_locked(fsp
,(uint32
)SVAL(inbuf
,smb_pid
),(SMB_BIG_UINT
)smb_maxcnt
,(SMB_BIG_UINT
)startpos
, READ_LOCK
)) {
2714 END_PROFILE(SMBreadX
);
2715 return ERROR_DOS(ERRDOS
,ERRlock
);
2718 if (schedule_aio_read_and_X(conn
, inbuf
, outbuf
, length
, bufsize
, fsp
, startpos
, smb_maxcnt
)) {
2719 END_PROFILE(SMBreadX
);
2723 nread
= send_file_readX(conn
, inbuf
, outbuf
, length
, bufsize
, fsp
, startpos
, smb_maxcnt
);
2725 nread
= chain_reply(inbuf
,outbuf
,length
,bufsize
);
2727 END_PROFILE(SMBreadX
);
2731 /****************************************************************************
2732 Reply to a writebraw (core+ or LANMAN1.0 protocol).
2733 ****************************************************************************/
2735 int reply_writebraw(connection_struct
*conn
, char *inbuf
,char *outbuf
, int size
, int dum_buffsize
)
2738 ssize_t total_written
=0;
2739 size_t numtowrite
=0;
2744 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
2746 START_PROFILE(SMBwritebraw
);
2748 if (srv_is_signing_active()) {
2749 exit_server("reply_writebraw: SMB signing is active - raw reads/writes are disallowed.");
2752 CHECK_FSP(fsp
,conn
);
2753 if (!CHECK_WRITE(fsp
)) {
2754 return(ERROR_DOS(ERRDOS
,ERRbadaccess
));
2757 tcount
= IVAL(inbuf
,smb_vwv1
);
2758 startpos
= IVAL_TO_SMB_OFF_T(inbuf
,smb_vwv3
);
2759 write_through
= BITSETW(inbuf
+smb_vwv7
,0);
2761 /* We have to deal with slightly different formats depending
2762 on whether we are using the core+ or lanman1.0 protocol */
2764 if(Protocol
<= PROTOCOL_COREPLUS
) {
2765 numtowrite
= SVAL(smb_buf(inbuf
),-2);
2766 data
= smb_buf(inbuf
);
2768 numtowrite
= SVAL(inbuf
,smb_vwv10
);
2769 data
= smb_base(inbuf
) + SVAL(inbuf
, smb_vwv11
);
2772 /* force the error type */
2773 SCVAL(inbuf
,smb_com
,SMBwritec
);
2774 SCVAL(outbuf
,smb_com
,SMBwritec
);
2776 if (is_locked(fsp
,(uint32
)SVAL(inbuf
,smb_pid
),(SMB_BIG_UINT
)tcount
,(SMB_BIG_UINT
)startpos
, WRITE_LOCK
)) {
2777 END_PROFILE(SMBwritebraw
);
2778 return(ERROR_DOS(ERRDOS
,ERRlock
));
2782 nwritten
= write_file(fsp
,data
,startpos
,numtowrite
);
2784 DEBUG(3,("writebraw1 fnum=%d start=%.0f num=%d wrote=%d sync=%d\n",
2785 fsp
->fnum
, (double)startpos
, (int)numtowrite
, (int)nwritten
, (int)write_through
));
2787 if (nwritten
< (ssize_t
)numtowrite
) {
2788 END_PROFILE(SMBwritebraw
);
2789 return(UNIXERROR(ERRHRD
,ERRdiskfull
));
2792 total_written
= nwritten
;
2794 /* Return a message to the redirector to tell it to send more bytes */
2795 SCVAL(outbuf
,smb_com
,SMBwritebraw
);
2796 SSVALS(outbuf
,smb_vwv0
,-1);
2797 outsize
= set_message(outbuf
,Protocol
>PROTOCOL_COREPLUS
?1:0,0,True
);
2799 if (!send_smb(smbd_server_fd(),outbuf
))
2800 exit_server("reply_writebraw: send_smb failed.");
2802 /* Now read the raw data into the buffer and write it */
2803 if (read_smb_length(smbd_server_fd(),inbuf
,SMB_SECONDARY_WAIT
) == -1) {
2804 exit_server("secondary writebraw failed");
2807 /* Even though this is not an smb message, smb_len returns the generic length of an smb message */
2808 numtowrite
= smb_len(inbuf
);
2810 /* Set up outbuf to return the correct return */
2811 outsize
= set_message(outbuf
,1,0,True
);
2812 SCVAL(outbuf
,smb_com
,SMBwritec
);
2814 if (numtowrite
!= 0) {
2816 if (numtowrite
> BUFFER_SIZE
) {
2817 DEBUG(0,("reply_writebraw: Oversize secondary write raw requested (%u). Terminating\n",
2818 (unsigned int)numtowrite
));
2819 exit_server("secondary writebraw failed");
2822 if (tcount
> nwritten
+numtowrite
) {
2823 DEBUG(3,("Client overestimated the write %d %d %d\n",
2824 (int)tcount
,(int)nwritten
,(int)numtowrite
));
2827 if (read_data( smbd_server_fd(), inbuf
+4, numtowrite
) != numtowrite
) {
2828 DEBUG(0,("reply_writebraw: Oversize secondary write raw read failed (%s). Terminating\n",
2830 exit_server("secondary writebraw failed");
2833 nwritten
= write_file(fsp
,inbuf
+4,startpos
+nwritten
,numtowrite
);
2835 if (nwritten
< (ssize_t
)numtowrite
) {
2836 SCVAL(outbuf
,smb_rcls
,ERRHRD
);
2837 SSVAL(outbuf
,smb_err
,ERRdiskfull
);
2841 total_written
+= nwritten
;
2844 SSVAL(outbuf
,smb_vwv0
,total_written
);
2846 sync_file(conn
, fsp
, write_through
);
2848 DEBUG(3,("writebraw2 fnum=%d start=%.0f num=%d wrote=%d\n",
2849 fsp
->fnum
, (double)startpos
, (int)numtowrite
,(int)total_written
));
2851 /* we won't return a status if write through is not selected - this follows what WfWg does */
2852 END_PROFILE(SMBwritebraw
);
2853 if (!write_through
&& total_written
==tcount
) {
2855 #if RABBIT_PELLET_FIX
2857 * Fix for "rabbit pellet" mode, trigger an early TCP ack by
2858 * sending a SMBkeepalive. Thanks to DaveCB at Sun for this. JRA.
2860 if (!send_keepalive(smbd_server_fd()))
2861 exit_server("reply_writebraw: send of keepalive failed");
2870 #define DBGC_CLASS DBGC_LOCKING
2872 /****************************************************************************
2873 Reply to a writeunlock (core+).
2874 ****************************************************************************/
2876 int reply_writeunlock(connection_struct
*conn
, char *inbuf
,char *outbuf
,
2877 int size
, int dum_buffsize
)
2879 ssize_t nwritten
= -1;
2883 NTSTATUS status
= NT_STATUS_OK
;
2884 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
2886 START_PROFILE(SMBwriteunlock
);
2888 CHECK_FSP(fsp
,conn
);
2889 if (!CHECK_WRITE(fsp
)) {
2890 return(ERROR_DOS(ERRDOS
,ERRbadaccess
));
2893 numtowrite
= SVAL(inbuf
,smb_vwv1
);
2894 startpos
= IVAL_TO_SMB_OFF_T(inbuf
,smb_vwv2
);
2895 data
= smb_buf(inbuf
) + 3;
2897 if (numtowrite
&& is_locked(fsp
,(uint32
)SVAL(inbuf
,smb_pid
),(SMB_BIG_UINT
)numtowrite
,(SMB_BIG_UINT
)startpos
, WRITE_LOCK
)) {
2898 END_PROFILE(SMBwriteunlock
);
2899 return ERROR_DOS(ERRDOS
,ERRlock
);
2902 /* The special X/Open SMB protocol handling of
2903 zero length writes is *NOT* done for
2905 if(numtowrite
== 0) {
2908 nwritten
= write_file(fsp
,data
,startpos
,numtowrite
);
2911 sync_file(conn
, fsp
, False
/* write through */);
2913 if(((nwritten
== 0) && (numtowrite
!= 0))||(nwritten
< 0)) {
2914 END_PROFILE(SMBwriteunlock
);
2915 return(UNIXERROR(ERRHRD
,ERRdiskfull
));
2919 status
= do_unlock(fsp
,
2920 (uint32
)SVAL(inbuf
,smb_pid
),
2921 (SMB_BIG_UINT
)numtowrite
,
2922 (SMB_BIG_UINT
)startpos
,
2925 if (NT_STATUS_V(status
)) {
2926 END_PROFILE(SMBwriteunlock
);
2927 return ERROR_NT(status
);
2931 outsize
= set_message(outbuf
,1,0,True
);
2933 SSVAL(outbuf
,smb_vwv0
,nwritten
);
2935 DEBUG(3,("writeunlock fnum=%d num=%d wrote=%d\n",
2936 fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
2938 END_PROFILE(SMBwriteunlock
);
2943 #define DBGC_CLASS DBGC_ALL
2945 /****************************************************************************
2947 ****************************************************************************/
2949 int reply_write(connection_struct
*conn
, char *inbuf
,char *outbuf
,int size
,int dum_buffsize
)
2952 ssize_t nwritten
= -1;
2955 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
2957 START_PROFILE(SMBwrite
);
2959 /* If it's an IPC, pass off the pipe handler. */
2961 END_PROFILE(SMBwrite
);
2962 return reply_pipe_write(inbuf
,outbuf
,size
,dum_buffsize
);
2965 CHECK_FSP(fsp
,conn
);
2966 if (!CHECK_WRITE(fsp
)) {
2967 return(ERROR_DOS(ERRDOS
,ERRbadaccess
));
2970 numtowrite
= SVAL(inbuf
,smb_vwv1
);
2971 startpos
= IVAL_TO_SMB_OFF_T(inbuf
,smb_vwv2
);
2972 data
= smb_buf(inbuf
) + 3;
2974 if (is_locked(fsp
,(uint32
)SVAL(inbuf
,smb_pid
),(SMB_BIG_UINT
)numtowrite
,(SMB_BIG_UINT
)startpos
, WRITE_LOCK
)) {
2975 END_PROFILE(SMBwrite
);
2976 return ERROR_DOS(ERRDOS
,ERRlock
);
2980 * X/Open SMB protocol says that if smb_vwv1 is
2981 * zero then the file size should be extended or
2982 * truncated to the size given in smb_vwv[2-3].
2985 if(numtowrite
== 0) {
2987 * This is actually an allocate call, and set EOF. JRA.
2989 nwritten
= vfs_allocate_file_space(fsp
, (SMB_OFF_T
)startpos
);
2991 END_PROFILE(SMBwrite
);
2992 return ERROR_NT(NT_STATUS_DISK_FULL
);
2994 nwritten
= vfs_set_filelen(fsp
, (SMB_OFF_T
)startpos
);
2996 END_PROFILE(SMBwrite
);
2997 return ERROR_NT(NT_STATUS_DISK_FULL
);
3000 nwritten
= write_file(fsp
,data
,startpos
,numtowrite
);
3002 sync_file(conn
, fsp
, False
);
3004 if(((nwritten
== 0) && (numtowrite
!= 0))||(nwritten
< 0)) {
3005 END_PROFILE(SMBwrite
);
3006 return(UNIXERROR(ERRHRD
,ERRdiskfull
));
3009 outsize
= set_message(outbuf
,1,0,True
);
3011 SSVAL(outbuf
,smb_vwv0
,nwritten
);
3013 if (nwritten
< (ssize_t
)numtowrite
) {
3014 SCVAL(outbuf
,smb_rcls
,ERRHRD
);
3015 SSVAL(outbuf
,smb_err
,ERRdiskfull
);
3018 DEBUG(3,("write fnum=%d num=%d wrote=%d\n", fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
3020 END_PROFILE(SMBwrite
);
3024 /****************************************************************************
3025 Reply to a write and X.
3026 ****************************************************************************/
3028 int reply_write_and_X(connection_struct
*conn
, char *inbuf
,char *outbuf
,int length
,int bufsize
)
3030 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv2
);
3031 SMB_OFF_T startpos
= IVAL_TO_SMB_OFF_T(inbuf
,smb_vwv3
);
3032 size_t numtowrite
= SVAL(inbuf
,smb_vwv10
);
3033 BOOL write_through
= BITSETW(inbuf
+smb_vwv7
,0);
3034 ssize_t nwritten
= -1;
3035 unsigned int smb_doff
= SVAL(inbuf
,smb_vwv11
);
3036 unsigned int smblen
= smb_len(inbuf
);
3038 BOOL large_writeX
= ((CVAL(inbuf
,smb_wct
) == 14) && (smblen
> 0xFFFF));
3039 START_PROFILE(SMBwriteX
);
3041 /* If it's an IPC, pass off the pipe handler. */
3043 END_PROFILE(SMBwriteX
);
3044 return reply_pipe_write_and_X(inbuf
,outbuf
,length
,bufsize
);
3047 CHECK_FSP(fsp
,conn
);
3048 if (!CHECK_WRITE(fsp
)) {
3049 return(ERROR_DOS(ERRDOS
,ERRbadaccess
));
3052 set_message(outbuf
,6,0,True
);
3054 /* Deal with possible LARGE_WRITEX */
3056 numtowrite
|= ((((size_t)SVAL(inbuf
,smb_vwv9
)) & 1 )<<16);
3059 if(smb_doff
> smblen
|| (smb_doff
+ numtowrite
> smblen
)) {
3060 END_PROFILE(SMBwriteX
);
3061 return ERROR_DOS(ERRDOS
,ERRbadmem
);
3064 data
= smb_base(inbuf
) + smb_doff
;
3066 if(CVAL(inbuf
,smb_wct
) == 14) {
3067 #ifdef LARGE_SMB_OFF_T
3069 * This is a large offset (64 bit) write.
3071 startpos
|= (((SMB_OFF_T
)IVAL(inbuf
,smb_vwv12
)) << 32);
3073 #else /* !LARGE_SMB_OFF_T */
3076 * Ensure we haven't been sent a >32 bit offset.
3079 if(IVAL(inbuf
,smb_vwv12
) != 0) {
3080 DEBUG(0,("reply_write_and_X - large offset (%x << 32) used and we don't support \
3081 64 bit offsets.\n", (unsigned int)IVAL(inbuf
,smb_vwv12
) ));
3082 END_PROFILE(SMBwriteX
);
3083 return ERROR_DOS(ERRDOS
,ERRbadaccess
);
3086 #endif /* LARGE_SMB_OFF_T */
3089 if (is_locked(fsp
,(uint32
)SVAL(inbuf
,smb_pid
),(SMB_BIG_UINT
)numtowrite
,(SMB_BIG_UINT
)startpos
, WRITE_LOCK
)) {
3090 END_PROFILE(SMBwriteX
);
3091 return ERROR_DOS(ERRDOS
,ERRlock
);
3094 /* X/Open SMB protocol says that, unlike SMBwrite
3095 if the length is zero then NO truncation is
3096 done, just a write of zero. To truncate a file,
3099 if(numtowrite
== 0) {
3103 if (schedule_aio_write_and_X(conn
, inbuf
, outbuf
, length
, bufsize
,
3104 fsp
,data
,startpos
,numtowrite
)) {
3105 END_PROFILE(SMBwriteX
);
3109 nwritten
= write_file(fsp
,data
,startpos
,numtowrite
);
3112 if(((nwritten
== 0) && (numtowrite
!= 0))||(nwritten
< 0)) {
3113 END_PROFILE(SMBwriteX
);
3114 return(UNIXERROR(ERRHRD
,ERRdiskfull
));
3117 SSVAL(outbuf
,smb_vwv2
,nwritten
);
3119 SSVAL(outbuf
,smb_vwv4
,(nwritten
>>16)&1);
3121 if (nwritten
< (ssize_t
)numtowrite
) {
3122 SCVAL(outbuf
,smb_rcls
,ERRHRD
);
3123 SSVAL(outbuf
,smb_err
,ERRdiskfull
);
3126 DEBUG(3,("writeX fnum=%d num=%d wrote=%d\n",
3127 fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
3129 sync_file(conn
, fsp
, write_through
);
3131 END_PROFILE(SMBwriteX
);
3132 return chain_reply(inbuf
,outbuf
,length
,bufsize
);
3135 /****************************************************************************
3137 ****************************************************************************/
3139 int reply_lseek(connection_struct
*conn
, char *inbuf
,char *outbuf
, int size
, int dum_buffsize
)
3145 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
3146 START_PROFILE(SMBlseek
);
3148 CHECK_FSP(fsp
,conn
);
3150 flush_write_cache(fsp
, SEEK_FLUSH
);
3152 mode
= SVAL(inbuf
,smb_vwv1
) & 3;
3153 /* NB. This doesn't use IVAL_TO_SMB_OFF_T as startpos can be signed in this case. */
3154 startpos
= (SMB_OFF_T
)IVALS(inbuf
,smb_vwv2
);
3163 res
= fsp
->fh
->pos
+ startpos
;
3174 if (umode
== SEEK_END
) {
3175 if((res
= SMB_VFS_LSEEK(fsp
,fsp
->fh
->fd
,startpos
,umode
)) == -1) {
3176 if(errno
== EINVAL
) {
3177 SMB_OFF_T current_pos
= startpos
;
3178 SMB_STRUCT_STAT sbuf
;
3180 if(SMB_VFS_FSTAT(fsp
,fsp
->fh
->fd
, &sbuf
) == -1) {
3181 END_PROFILE(SMBlseek
);
3182 return(UNIXERROR(ERRDOS
,ERRnoaccess
));
3185 current_pos
+= sbuf
.st_size
;
3187 res
= SMB_VFS_LSEEK(fsp
,fsp
->fh
->fd
,0,SEEK_SET
);
3192 END_PROFILE(SMBlseek
);
3193 return(UNIXERROR(ERRDOS
,ERRnoaccess
));
3199 outsize
= set_message(outbuf
,2,0,True
);
3200 SIVAL(outbuf
,smb_vwv0
,res
);
3202 DEBUG(3,("lseek fnum=%d ofs=%.0f newpos = %.0f mode=%d\n",
3203 fsp
->fnum
, (double)startpos
, (double)res
, mode
));
3205 END_PROFILE(SMBlseek
);
3209 /****************************************************************************
3211 ****************************************************************************/
3213 int reply_flush(connection_struct
*conn
, char *inbuf
,char *outbuf
, int size
, int dum_buffsize
)
3215 int outsize
= set_message(outbuf
,0,0,False
);
3216 uint16 fnum
= SVAL(inbuf
,smb_vwv0
);
3217 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
3218 START_PROFILE(SMBflush
);
3221 CHECK_FSP(fsp
,conn
);
3224 file_sync_all(conn
);
3226 sync_file(conn
,fsp
, True
);
3229 DEBUG(3,("flush\n"));
3230 END_PROFILE(SMBflush
);
3234 /****************************************************************************
3236 conn POINTER CAN BE NULL HERE !
3237 ****************************************************************************/
3239 int reply_exit(connection_struct
*conn
,
3240 char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
3243 START_PROFILE(SMBexit
);
3245 file_close_pid(SVAL(inbuf
,smb_pid
),SVAL(inbuf
,smb_uid
));
3247 outsize
= set_message(outbuf
,0,0,False
);
3249 DEBUG(3,("exit\n"));
3251 END_PROFILE(SMBexit
);
3255 /****************************************************************************
3256 Reply to a close - has to deal with closing a directory opened by NT SMB's.
3257 ****************************************************************************/
3259 int reply_close(connection_struct
*conn
, char *inbuf
,char *outbuf
, int size
,
3264 int32 eclass
= 0, err
= 0;
3265 files_struct
*fsp
= NULL
;
3266 START_PROFILE(SMBclose
);
3268 outsize
= set_message(outbuf
,0,0,False
);
3270 /* If it's an IPC, pass off to the pipe handler. */
3272 END_PROFILE(SMBclose
);
3273 return reply_pipe_close(conn
, inbuf
,outbuf
);
3276 fsp
= file_fsp(inbuf
,smb_vwv0
);
3279 * We can only use CHECK_FSP if we know it's not a directory.
3282 if(!fsp
|| (fsp
->conn
!= conn
) || (fsp
->vuid
!= current_user
.vuid
)) {
3283 END_PROFILE(SMBclose
);
3284 return ERROR_DOS(ERRDOS
,ERRbadfid
);
3287 if(fsp
->is_directory
) {
3289 * Special case - close NT SMB directory handle.
3291 DEBUG(3,("close %s fnum=%d\n", fsp
->is_directory
? "directory" : "stat file open", fsp
->fnum
));
3292 close_file(fsp
,NORMAL_CLOSE
);
3295 * Close ordinary file.
3300 /* Save the name for time set in close. */
3301 pstrcpy( file_name
, fsp
->fsp_name
);
3303 DEBUG(3,("close fd=%d fnum=%d (numopen=%d)\n",
3304 fsp
->fh
->fd
, fsp
->fnum
,
3305 conn
->num_files_open
));
3308 * Take care of any time sent in the close.
3311 mtime
= srv_make_unix_date3(inbuf
+smb_vwv1
);
3312 fsp_set_pending_modtime(fsp
, mtime
);
3315 * close_file() returns the unix errno if an error
3316 * was detected on close - normally this is due to
3317 * a disk full error. If not then it was probably an I/O error.
3320 if((close_err
= close_file(fsp
,NORMAL_CLOSE
)) != 0) {
3322 END_PROFILE(SMBclose
);
3323 return (UNIXERROR(ERRHRD
,ERRgeneral
));
3327 /* We have a cached error */
3329 END_PROFILE(SMBclose
);
3330 return ERROR_DOS(eclass
,err
);
3333 END_PROFILE(SMBclose
);
3337 /****************************************************************************
3338 Reply to a writeclose (Core+ protocol).
3339 ****************************************************************************/
3341 int reply_writeclose(connection_struct
*conn
,
3342 char *inbuf
,char *outbuf
, int size
, int dum_buffsize
)
3345 ssize_t nwritten
= -1;
3351 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
3352 START_PROFILE(SMBwriteclose
);
3354 CHECK_FSP(fsp
,conn
);
3355 if (!CHECK_WRITE(fsp
)) {
3356 return(ERROR_DOS(ERRDOS
,ERRbadaccess
));
3359 numtowrite
= SVAL(inbuf
,smb_vwv1
);
3360 startpos
= IVAL_TO_SMB_OFF_T(inbuf
,smb_vwv2
);
3361 mtime
= srv_make_unix_date3(inbuf
+smb_vwv4
);
3362 data
= smb_buf(inbuf
) + 1;
3364 if (numtowrite
&& is_locked(fsp
,(uint32
)SVAL(inbuf
,smb_pid
),(SMB_BIG_UINT
)numtowrite
,(SMB_BIG_UINT
)startpos
, WRITE_LOCK
)) {
3365 END_PROFILE(SMBwriteclose
);
3366 return ERROR_DOS(ERRDOS
,ERRlock
);
3369 nwritten
= write_file(fsp
,data
,startpos
,numtowrite
);
3371 set_filetime(conn
, fsp
->fsp_name
,mtime
);
3374 * More insanity. W2K only closes the file if writelen > 0.
3379 DEBUG(3,("reply_writeclose: zero length write doesn't close file %s\n",
3381 close_err
= close_file(fsp
,NORMAL_CLOSE
);
3384 DEBUG(3,("writeclose fnum=%d num=%d wrote=%d (numopen=%d)\n",
3385 fsp
->fnum
, (int)numtowrite
, (int)nwritten
,
3386 conn
->num_files_open
));
3388 if(((nwritten
== 0) && (numtowrite
!= 0))||(nwritten
< 0)) {
3389 END_PROFILE(SMBwriteclose
);
3390 return(UNIXERROR(ERRHRD
,ERRdiskfull
));
3393 if(close_err
!= 0) {
3395 END_PROFILE(SMBwriteclose
);
3396 return(UNIXERROR(ERRHRD
,ERRgeneral
));
3399 outsize
= set_message(outbuf
,1,0,True
);
3401 SSVAL(outbuf
,smb_vwv0
,nwritten
);
3402 END_PROFILE(SMBwriteclose
);
3407 #define DBGC_CLASS DBGC_LOCKING
3409 /****************************************************************************
3411 ****************************************************************************/
3413 int reply_lock(connection_struct
*conn
,
3414 char *inbuf
,char *outbuf
, int length
, int dum_buffsize
)
3416 int outsize
= set_message(outbuf
,0,0,False
);
3417 SMB_BIG_UINT count
,offset
;
3419 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
3421 START_PROFILE(SMBlock
);
3423 CHECK_FSP(fsp
,conn
);
3425 release_level_2_oplocks_on_change(fsp
);
3427 count
= (SMB_BIG_UINT
)IVAL(inbuf
,smb_vwv1
);
3428 offset
= (SMB_BIG_UINT
)IVAL(inbuf
,smb_vwv3
);
3430 DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
3431 fsp
->fh
->fd
, fsp
->fnum
, (double)offset
, (double)count
));
3433 status
= do_lock(fsp
,
3434 (uint32
)SVAL(inbuf
,smb_pid
),
3439 0 /* zero timeout. */);
3441 if (NT_STATUS_V(status
)) {
3443 /* Tests using Samba4 against W2K show this call never creates a blocking lock. */
3444 if (lp_blocking_locks(SNUM(conn
)) && ERROR_WAS_LOCK_DENIED(status
)) {
3446 * A blocking lock was requested. Package up
3447 * this smb into a queued request and push it
3448 * onto the blocking lock queue.
3450 if(push_blocking_lock_request(inbuf
, length
,
3454 SVAL(inbuf
,smb_pid
),
3458 END_PROFILE(SMBlock
);
3463 END_PROFILE(SMBlock
);
3464 return ERROR_NT(status
);
3467 END_PROFILE(SMBlock
);
3471 /****************************************************************************
3473 ****************************************************************************/
3475 int reply_unlock(connection_struct
*conn
, char *inbuf
,char *outbuf
, int size
,
3478 int outsize
= set_message(outbuf
,0,0,False
);
3479 SMB_BIG_UINT count
,offset
;
3481 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
3482 START_PROFILE(SMBunlock
);
3484 CHECK_FSP(fsp
,conn
);
3486 count
= (SMB_BIG_UINT
)IVAL(inbuf
,smb_vwv1
);
3487 offset
= (SMB_BIG_UINT
)IVAL(inbuf
,smb_vwv3
);
3489 status
= do_unlock(fsp
,
3490 (uint32
)SVAL(inbuf
,smb_pid
),
3495 if (NT_STATUS_V(status
)) {
3496 END_PROFILE(SMBunlock
);
3497 return ERROR_NT(status
);
3500 DEBUG( 3, ( "unlock fd=%d fnum=%d offset=%.0f count=%.0f\n",
3501 fsp
->fh
->fd
, fsp
->fnum
, (double)offset
, (double)count
) );
3503 END_PROFILE(SMBunlock
);
3508 #define DBGC_CLASS DBGC_ALL
3510 /****************************************************************************
3512 conn POINTER CAN BE NULL HERE !
3513 ****************************************************************************/
3515 int reply_tdis(connection_struct
*conn
,
3516 char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
3518 int outsize
= set_message(outbuf
,0,0,False
);
3520 START_PROFILE(SMBtdis
);
3522 vuid
= SVAL(inbuf
,smb_uid
);
3525 DEBUG(4,("Invalid connection in tdis\n"));
3526 END_PROFILE(SMBtdis
);
3527 return ERROR_DOS(ERRSRV
,ERRinvnid
);
3532 close_cnum(conn
,vuid
);
3534 END_PROFILE(SMBtdis
);
3538 /****************************************************************************
3540 conn POINTER CAN BE NULL HERE !
3541 ****************************************************************************/
3543 int reply_echo(connection_struct
*conn
,
3544 char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
3546 int smb_reverb
= SVAL(inbuf
,smb_vwv0
);
3548 unsigned int data_len
= smb_buflen(inbuf
);
3549 int outsize
= set_message(outbuf
,1,data_len
,True
);
3550 START_PROFILE(SMBecho
);
3552 if (data_len
> BUFFER_SIZE
) {
3553 DEBUG(0,("reply_echo: data_len too large.\n"));
3554 END_PROFILE(SMBecho
);
3558 /* copy any incoming data back out */
3560 memcpy(smb_buf(outbuf
),smb_buf(inbuf
),data_len
);
3562 if (smb_reverb
> 100) {
3563 DEBUG(0,("large reverb (%d)?? Setting to 100\n",smb_reverb
));
3567 for (seq_num
=1 ; seq_num
<= smb_reverb
; seq_num
++) {
3568 SSVAL(outbuf
,smb_vwv0
,seq_num
);
3570 smb_setlen(outbuf
,outsize
- 4);
3573 if (!send_smb(smbd_server_fd(),outbuf
))
3574 exit_server("reply_echo: send_smb failed.");
3577 DEBUG(3,("echo %d times\n", smb_reverb
));
3581 END_PROFILE(SMBecho
);
3585 /****************************************************************************
3586 Reply to a printopen.
3587 ****************************************************************************/
3589 int reply_printopen(connection_struct
*conn
,
3590 char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
3596 START_PROFILE(SMBsplopen
);
3598 if (!CAN_PRINT(conn
)) {
3599 END_PROFILE(SMBsplopen
);
3600 return ERROR_DOS(ERRDOS
,ERRnoaccess
);
3603 /* Open for exclusive use, write only. */
3604 status
= print_fsp_open(conn
, NULL
, &fsp
);
3606 if (!NT_STATUS_IS_OK(status
)) {
3607 END_PROFILE(SMBsplopen
);
3608 return(ERROR_NT(status
));
3611 outsize
= set_message(outbuf
,1,0,True
);
3612 SSVAL(outbuf
,smb_vwv0
,fsp
->fnum
);
3614 DEBUG(3,("openprint fd=%d fnum=%d\n",
3615 fsp
->fh
->fd
, fsp
->fnum
));
3617 END_PROFILE(SMBsplopen
);
3621 /****************************************************************************
3622 Reply to a printclose.
3623 ****************************************************************************/
3625 int reply_printclose(connection_struct
*conn
,
3626 char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
3628 int outsize
= set_message(outbuf
,0,0,False
);
3629 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
3631 START_PROFILE(SMBsplclose
);
3633 CHECK_FSP(fsp
,conn
);
3635 if (!CAN_PRINT(conn
)) {
3636 END_PROFILE(SMBsplclose
);
3637 return ERROR_NT(NT_STATUS_UNSUCCESSFUL
);
3640 DEBUG(3,("printclose fd=%d fnum=%d\n",
3641 fsp
->fh
->fd
,fsp
->fnum
));
3643 close_err
= close_file(fsp
,NORMAL_CLOSE
);
3645 if(close_err
!= 0) {
3647 END_PROFILE(SMBsplclose
);
3648 return(UNIXERROR(ERRHRD
,ERRgeneral
));
3651 END_PROFILE(SMBsplclose
);
3655 /****************************************************************************
3656 Reply to a printqueue.
3657 ****************************************************************************/
3659 int reply_printqueue(connection_struct
*conn
,
3660 char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
3662 int outsize
= set_message(outbuf
,2,3,True
);
3663 int max_count
= SVAL(inbuf
,smb_vwv0
);
3664 int start_index
= SVAL(inbuf
,smb_vwv1
);
3665 START_PROFILE(SMBsplretq
);
3667 /* we used to allow the client to get the cnum wrong, but that
3668 is really quite gross and only worked when there was only
3669 one printer - I think we should now only accept it if they
3670 get it right (tridge) */
3671 if (!CAN_PRINT(conn
)) {
3672 END_PROFILE(SMBsplretq
);
3673 return ERROR_DOS(ERRDOS
,ERRnoaccess
);
3676 SSVAL(outbuf
,smb_vwv0
,0);
3677 SSVAL(outbuf
,smb_vwv1
,0);
3678 SCVAL(smb_buf(outbuf
),0,1);
3679 SSVAL(smb_buf(outbuf
),1,0);
3681 DEBUG(3,("printqueue start_index=%d max_count=%d\n",
3682 start_index
, max_count
));
3685 print_queue_struct
*queue
= NULL
;
3686 print_status_struct status
;
3687 char *p
= smb_buf(outbuf
) + 3;
3688 int count
= print_queue_status(SNUM(conn
), &queue
, &status
);
3689 int num_to_get
= ABS(max_count
);
3690 int first
= (max_count
>0?start_index
:start_index
+max_count
+1);
3696 num_to_get
= MIN(num_to_get
,count
-first
);
3699 for (i
=first
;i
<first
+num_to_get
;i
++) {
3700 srv_put_dos_date2(p
,0,queue
[i
].time
);
3701 SCVAL(p
,4,(queue
[i
].status
==LPQ_PRINTING
?2:3));
3702 SSVAL(p
,5, queue
[i
].job
);
3703 SIVAL(p
,7,queue
[i
].size
);
3705 srvstr_push(outbuf
, p
+12, queue
[i
].fs_user
, 16, STR_ASCII
);
3710 outsize
= set_message(outbuf
,2,28*count
+3,False
);
3711 SSVAL(outbuf
,smb_vwv0
,count
);
3712 SSVAL(outbuf
,smb_vwv1
,(max_count
>0?first
+count
:first
-1));
3713 SCVAL(smb_buf(outbuf
),0,1);
3714 SSVAL(smb_buf(outbuf
),1,28*count
);
3719 DEBUG(3,("%d entries returned in queue\n",count
));
3722 END_PROFILE(SMBsplretq
);
3726 /****************************************************************************
3727 Reply to a printwrite.
3728 ****************************************************************************/
3730 int reply_printwrite(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
3733 int outsize
= set_message(outbuf
,0,0,False
);
3735 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
3737 START_PROFILE(SMBsplwr
);
3739 if (!CAN_PRINT(conn
)) {
3740 END_PROFILE(SMBsplwr
);
3741 return ERROR_DOS(ERRDOS
,ERRnoaccess
);
3744 CHECK_FSP(fsp
,conn
);
3745 if (!CHECK_WRITE(fsp
)) {
3746 return(ERROR_DOS(ERRDOS
,ERRbadaccess
));
3749 numtowrite
= SVAL(smb_buf(inbuf
),1);
3750 data
= smb_buf(inbuf
) + 3;
3752 if (write_file(fsp
,data
,-1,numtowrite
) != numtowrite
) {
3753 END_PROFILE(SMBsplwr
);
3754 return(UNIXERROR(ERRHRD
,ERRdiskfull
));
3757 DEBUG( 3, ( "printwrite fnum=%d num=%d\n", fsp
->fnum
, numtowrite
) );
3759 END_PROFILE(SMBsplwr
);
3763 /****************************************************************************
3764 The guts of the mkdir command, split out so it may be called by the NT SMB
3766 ****************************************************************************/
3768 NTSTATUS
mkdir_internal(connection_struct
*conn
, const pstring directory
, BOOL bad_path
)
3772 if(!CAN_WRITE(conn
)) {
3773 DEBUG(5,("mkdir_internal: failing create on read-only share %s\n", lp_servicename(SNUM(conn
))));
3775 return map_nt_error_from_unix(errno
);
3779 return NT_STATUS_OBJECT_PATH_NOT_FOUND
;
3782 if (!check_name(directory
, conn
)) {
3783 if(errno
== ENOENT
) {
3785 return NT_STATUS_OBJECT_PATH_NOT_FOUND
;
3787 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3790 return map_nt_error_from_unix(errno
);
3793 ret
= vfs_MkDir(conn
,directory
,unix_mode(conn
,aDIR
,directory
,True
));
3795 if(errno
== ENOENT
) {
3796 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
3798 return map_nt_error_from_unix(errno
);
3801 return NT_STATUS_OK
;
3804 /****************************************************************************
3806 ****************************************************************************/
3808 int reply_mkdir(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
3813 BOOL bad_path
= False
;
3814 SMB_STRUCT_STAT sbuf
;
3816 START_PROFILE(SMBmkdir
);
3818 srvstr_get_path(inbuf
, directory
, smb_buf(inbuf
) + 1, sizeof(directory
), 0, STR_TERMINATE
, &status
);
3819 if (!NT_STATUS_IS_OK(status
)) {
3820 END_PROFILE(SMBmkdir
);
3821 return ERROR_NT(status
);
3824 RESOLVE_DFSPATH(directory
, conn
, inbuf
, outbuf
);
3826 unix_convert(directory
,conn
,0,&bad_path
,&sbuf
);
3828 if( is_ntfs_stream_name(directory
)) {
3829 DEBUG(5,("reply_mkdir: failing create on filename %s with colon in name\n", directory
));
3830 END_PROFILE(SMBmkdir
);
3831 return ERROR_NT(NT_STATUS_NOT_A_DIRECTORY
);
3834 status
= mkdir_internal(conn
, directory
,bad_path
);
3835 if (!NT_STATUS_IS_OK(status
)) {
3837 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_COLLISION
) &&
3840 * Yes, in the DOS error code case we get a
3841 * ERRDOS:ERRnoaccess here. See BASE-SAMBA3ERROR
3842 * samba4 torture test.
3844 status
= NT_STATUS_DOS(ERRDOS
, ERRnoaccess
);
3847 END_PROFILE(SMBmkdir
);
3848 return ERROR_NT(status
);
3851 if (lp_inherit_owner(SNUM(conn
))) {
3852 /* Ensure we're checking for a symlink here.... */
3853 /* We don't want to get caught by a symlink racer. */
3855 if(SMB_VFS_LSTAT(conn
,directory
, &sbuf
) != 0) {
3856 END_PROFILE(SMBmkdir
);
3857 return(UNIXERROR(ERRDOS
,ERRnoaccess
));
3860 if(!S_ISDIR(sbuf
.st_mode
)) {
3861 DEBUG(0,("reply_mkdir: %s is not a directory !\n", directory
));
3862 END_PROFILE(SMBmkdir
);
3863 return(UNIXERROR(ERRDOS
,ERRnoaccess
));
3866 change_owner_to_parent(conn
, NULL
, directory
, &sbuf
);
3869 outsize
= set_message(outbuf
,0,0,False
);
3871 DEBUG( 3, ( "mkdir %s ret=%d\n", directory
, outsize
) );
3873 END_PROFILE(SMBmkdir
);
3877 /****************************************************************************
3878 Static function used by reply_rmdir to delete an entire directory
3879 tree recursively. Return False on ok, True on fail.
3880 ****************************************************************************/
3882 static BOOL
recursive_rmdir(connection_struct
*conn
, char *directory
)
3884 const char *dname
= NULL
;
3887 struct smb_Dir
*dir_hnd
= OpenDir(conn
, directory
, NULL
, 0);
3892 while((dname
= ReadDirName(dir_hnd
, &offset
))) {
3896 if((strcmp(dname
, ".") == 0) || (strcmp(dname
, "..")==0))
3899 if (!is_visible_file(conn
, directory
, dname
, &st
, False
))
3902 /* Construct the full name. */
3903 if(strlen(directory
) + strlen(dname
) + 1 >= sizeof(fullname
)) {
3909 pstrcpy(fullname
, directory
);
3910 pstrcat(fullname
, "/");
3911 pstrcat(fullname
, dname
);
3913 if(SMB_VFS_LSTAT(conn
,fullname
, &st
) != 0) {
3918 if(st
.st_mode
& S_IFDIR
) {
3919 if(recursive_rmdir(conn
, fullname
)!=0) {
3923 if(SMB_VFS_RMDIR(conn
,fullname
) != 0) {
3927 } else if(SMB_VFS_UNLINK(conn
,fullname
) != 0) {
3936 /****************************************************************************
3937 The internals of the rmdir code - called elsewhere.
3938 ****************************************************************************/
3940 BOOL
rmdir_internals(connection_struct
*conn
, char *directory
)
3945 ok
= (SMB_VFS_RMDIR(conn
,directory
) == 0);
3946 if(!ok
&& ((errno
== ENOTEMPTY
)||(errno
== EEXIST
)) && lp_veto_files(SNUM(conn
))) {
3948 * Check to see if the only thing in this directory are
3949 * vetoed files/directories. If so then delete them and
3950 * retry. If we fail to delete any of them (and we *don't*
3951 * do a recursive delete) then fail the rmdir.
3953 BOOL all_veto_files
= True
;
3955 struct smb_Dir
*dir_hnd
= OpenDir(conn
, directory
, NULL
, 0);
3957 if(dir_hnd
!= NULL
) {
3959 while ((dname
= ReadDirName(dir_hnd
,&dirpos
))) {
3960 if((strcmp(dname
, ".") == 0) || (strcmp(dname
, "..")==0))
3962 if (!is_visible_file(conn
, directory
, dname
, &st
, False
))
3964 if(!IS_VETO_PATH(conn
, dname
)) {
3965 all_veto_files
= False
;
3970 if(all_veto_files
) {
3971 RewindDir(dir_hnd
,&dirpos
);
3972 while ((dname
= ReadDirName(dir_hnd
,&dirpos
))) {
3975 if((strcmp(dname
, ".") == 0) || (strcmp(dname
, "..")==0))
3977 if (!is_visible_file(conn
, directory
, dname
, &st
, False
))
3980 /* Construct the full name. */
3981 if(strlen(directory
) + strlen(dname
) + 1 >= sizeof(fullname
)) {
3986 pstrcpy(fullname
, directory
);
3987 pstrcat(fullname
, "/");
3988 pstrcat(fullname
, dname
);
3990 if(SMB_VFS_LSTAT(conn
,fullname
, &st
) != 0)
3992 if(st
.st_mode
& S_IFDIR
) {
3993 if(lp_recursive_veto_delete(SNUM(conn
))) {
3994 if(recursive_rmdir(conn
, fullname
) != 0)
3997 if(SMB_VFS_RMDIR(conn
,fullname
) != 0)
3999 } else if(SMB_VFS_UNLINK(conn
,fullname
) != 0)
4003 /* Retry the rmdir */
4004 ok
= (SMB_VFS_RMDIR(conn
,directory
) == 0);
4014 DEBUG(3,("rmdir_internals: couldn't remove directory %s : %s\n", directory
,strerror(errno
)));
4019 /****************************************************************************
4021 ****************************************************************************/
4023 int reply_rmdir(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
4028 BOOL bad_path
= False
;
4029 SMB_STRUCT_STAT sbuf
;
4031 START_PROFILE(SMBrmdir
);
4033 srvstr_get_path(inbuf
, directory
, smb_buf(inbuf
) + 1, sizeof(directory
), 0, STR_TERMINATE
, &status
);
4034 if (!NT_STATUS_IS_OK(status
)) {
4035 END_PROFILE(SMBrmdir
);
4036 return ERROR_NT(status
);
4039 RESOLVE_DFSPATH(directory
, conn
, inbuf
, outbuf
)
4041 unix_convert(directory
,conn
, NULL
,&bad_path
,&sbuf
);
4043 END_PROFILE(SMBrmdir
);
4044 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND
);
4047 if (check_name(directory
,conn
)) {
4048 dptr_closepath(directory
,SVAL(inbuf
,smb_pid
));
4049 ok
= rmdir_internals(conn
, directory
);
4053 END_PROFILE(SMBrmdir
);
4054 return set_bad_path_error(errno
, bad_path
, outbuf
, ERRDOS
, ERRbadpath
);
4057 outsize
= set_message(outbuf
,0,0,False
);
4059 DEBUG( 3, ( "rmdir %s\n", directory
) );
4061 END_PROFILE(SMBrmdir
);
4065 /*******************************************************************
4066 Resolve wildcards in a filename rename.
4067 Note that name is in UNIX charset and thus potentially can be more
4068 than fstring buffer (255 bytes) especially in default UTF-8 case.
4069 Therefore, we use pstring inside and all calls should ensure that
4070 name2 is at least pstring-long (they do already)
4071 ********************************************************************/
4073 static BOOL
resolve_wildcards(const char *name1
, char *name2
)
4075 pstring root1
,root2
;
4077 char *p
,*p2
, *pname1
, *pname2
;
4078 int available_space
, actual_space
;
4081 pname1
= strrchr_m(name1
,'/');
4082 pname2
= strrchr_m(name2
,'/');
4084 if (!pname1
|| !pname2
)
4087 pstrcpy(root1
,pname1
);
4088 pstrcpy(root2
,pname2
);
4089 p
= strrchr_m(root1
,'.');
4096 p
= strrchr_m(root2
,'.');
4110 } else if (*p2
== '*') {
4126 } else if (*p2
== '*') {
4136 available_space
= sizeof(pstring
) - PTR_DIFF(pname2
, name2
);
4139 actual_space
= snprintf(pname2
, available_space
- 1, "%s.%s", root2
, ext2
);
4140 if (actual_space
>= available_space
- 1) {
4141 DEBUG(1,("resolve_wildcards: can't fit resolved name into specified buffer (overrun by %d bytes)\n",
4142 actual_space
- available_space
));
4145 pstrcpy_base(pname2
, root2
, name2
);
4151 /****************************************************************************
4152 Ensure open files have their names updated. Updated to notify other smbd's
4154 ****************************************************************************/
4156 static void rename_open_files(connection_struct
*conn
, struct share_mode_lock
*lck
,
4157 SMB_DEV_T dev
, SMB_INO_T inode
, const char *newname
)
4160 BOOL did_rename
= False
;
4162 for(fsp
= file_find_di_first(dev
, inode
); fsp
; fsp
= file_find_di_next(fsp
)) {
4163 /* fsp_name is a relative path under the fsp. To change this for other
4164 sharepaths we need to manipulate relative paths. */
4165 /* TODO - create the absolute path and manipulate the newname
4166 relative to the sharepath. */
4167 if (fsp
->conn
!= conn
) {
4170 DEBUG(10,("rename_open_files: renaming file fnum %d (dev = %x, inode = %.0f) from %s -> %s\n",
4171 fsp
->fnum
, (unsigned int)fsp
->dev
, (double)fsp
->inode
,
4172 fsp
->fsp_name
, newname
));
4173 string_set(&fsp
->fsp_name
, newname
);
4178 DEBUG(10,("rename_open_files: no open files on dev %x, inode %.0f for %s\n",
4179 (unsigned int)dev
, (double)inode
, newname
));
4182 /* Send messages to all smbd's (not ourself) that the name has changed. */
4183 rename_share_filename(lck
, conn
->connectpath
, newname
);
4186 /****************************************************************************
4187 We need to check if the source path is a parent directory of the destination
4188 (ie. a rename of /foo/bar/baz -> /foo/bar/baz/bibble/bobble. If so we must
4189 refuse the rename with a sharing violation. Under UNIX the above call can
4190 *succeed* if /foo/bar/baz is a symlink to another area in the share. We
4191 probably need to check that the client is a Windows one before disallowing
4192 this as a UNIX client (one with UNIX extensions) can know the source is a
4193 symlink and make this decision intelligently. Found by an excellent bug
4194 report from <AndyLiebman@aol.com>.
4195 ****************************************************************************/
4197 static BOOL
rename_path_prefix_equal(const char *src
, const char *dest
)
4199 const char *psrc
= src
;
4200 const char *pdst
= dest
;
4203 if (psrc
[0] == '.' && psrc
[1] == '/') {
4206 if (pdst
[0] == '.' && pdst
[1] == '/') {
4209 if ((slen
= strlen(psrc
)) > strlen(pdst
)) {
4212 return ((memcmp(psrc
, pdst
, slen
) == 0) && pdst
[slen
] == '/');
4215 /****************************************************************************
4216 Rename an open file - given an fsp.
4217 ****************************************************************************/
4219 NTSTATUS
rename_internals_fsp(connection_struct
*conn
, files_struct
*fsp
, char *newname
, uint32 attrs
, BOOL replace_if_exists
)
4221 SMB_STRUCT_STAT sbuf
;
4222 BOOL bad_path
= False
;
4223 pstring newname_last_component
;
4224 NTSTATUS error
= NT_STATUS_OK
;
4227 struct share_mode_lock
*lck
= NULL
;
4230 rcdest
= unix_convert(newname
,conn
,newname_last_component
,&bad_path
,&sbuf
);
4232 /* Quick check for "." and ".." */
4233 if (!bad_path
&& newname_last_component
[0] == '.') {
4234 if (!newname_last_component
[1] || (newname_last_component
[1] == '.' && !newname_last_component
[2])) {
4235 return NT_STATUS_ACCESS_DENIED
;
4238 if (!rcdest
&& bad_path
) {
4239 return NT_STATUS_OBJECT_PATH_NOT_FOUND
;
4242 /* Ensure newname contains a '/' */
4243 if(strrchr_m(newname
,'/') == 0) {
4246 pstrcpy(tmpstr
, "./");
4247 pstrcat(tmpstr
, newname
);
4248 pstrcpy(newname
, tmpstr
);
4252 * Check for special case with case preserving and not
4253 * case sensitive. If the old last component differs from the original
4254 * last component only by case, then we should allow
4255 * the rename (user is trying to change the case of the
4259 if((conn
->case_sensitive
== False
) && (conn
->case_preserve
== True
) &&
4260 strequal(newname
, fsp
->fsp_name
)) {
4262 pstring newname_modified_last_component
;
4265 * Get the last component of the modified name.
4266 * Note that we guarantee that newname contains a '/'
4269 p
= strrchr_m(newname
,'/');
4270 pstrcpy(newname_modified_last_component
,p
+1);
4272 if(strcsequal(newname_modified_last_component
,
4273 newname_last_component
) == False
) {
4275 * Replace the modified last component with
4278 pstrcpy(p
+1, newname_last_component
);
4283 * If the src and dest names are identical - including case,
4284 * don't do the rename, just return success.
4287 if (strcsequal(fsp
->fsp_name
, newname
)) {
4288 DEBUG(3,("rename_internals_fsp: identical names in rename %s - returning success\n",
4290 return NT_STATUS_OK
;
4293 dest_exists
= vfs_object_exist(conn
,newname
,NULL
);
4295 if(!replace_if_exists
&& dest_exists
) {
4296 DEBUG(3,("rename_internals_fsp: dest exists doing rename %s -> %s\n",
4297 fsp
->fsp_name
,newname
));
4298 return NT_STATUS_OBJECT_NAME_COLLISION
;
4301 error
= can_rename(conn
,newname
,attrs
,&sbuf
);
4303 if (dest_exists
&& !NT_STATUS_IS_OK(error
)) {
4304 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4305 nt_errstr(error
), fsp
->fsp_name
,newname
));
4306 if (NT_STATUS_EQUAL(error
,NT_STATUS_SHARING_VIOLATION
))
4307 error
= NT_STATUS_ACCESS_DENIED
;
4311 if (rename_path_prefix_equal(fsp
->fsp_name
, newname
)) {
4312 return NT_STATUS_ACCESS_DENIED
;
4315 lck
= get_share_mode_lock(NULL
, fsp
->dev
, fsp
->inode
, NULL
, NULL
);
4317 if(SMB_VFS_RENAME(conn
,fsp
->fsp_name
, newname
) == 0) {
4318 DEBUG(3,("rename_internals_fsp: succeeded doing rename on %s -> %s\n",
4319 fsp
->fsp_name
,newname
));
4320 rename_open_files(conn
, lck
, fsp
->dev
, fsp
->inode
, newname
);
4322 return NT_STATUS_OK
;
4327 if (errno
== ENOTDIR
|| errno
== EISDIR
) {
4328 error
= NT_STATUS_OBJECT_NAME_COLLISION
;
4330 error
= map_nt_error_from_unix(errno
);
4333 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
4334 nt_errstr(error
), fsp
->fsp_name
,newname
));
4339 /****************************************************************************
4340 The guts of the rename command, split out so it may be called by the NT SMB
4342 ****************************************************************************/
4344 NTSTATUS
rename_internals(connection_struct
*conn
, char *name
, char *newname
, uint32 attrs
, BOOL replace_if_exists
, BOOL has_wild
)
4348 pstring last_component_src
;
4349 pstring last_component_dest
;
4351 BOOL bad_path_src
= False
;
4352 BOOL bad_path_dest
= False
;
4354 NTSTATUS error
= NT_STATUS_OK
;
4357 SMB_STRUCT_STAT sbuf1
, sbuf2
;
4358 struct share_mode_lock
*lck
= NULL
;
4360 *directory
= *mask
= 0;
4365 rc
= unix_convert(name
,conn
,last_component_src
,&bad_path_src
,&sbuf1
);
4366 if (!rc
&& bad_path_src
) {
4367 if (ms_has_wild(last_component_src
))
4368 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
4369 return NT_STATUS_OBJECT_PATH_NOT_FOUND
;
4372 /* Quick check for "." and ".." */
4373 if (last_component_src
[0] == '.') {
4374 if (!last_component_src
[1] || (last_component_src
[1] == '.' && !last_component_src
[2])) {
4375 return NT_STATUS_OBJECT_NAME_INVALID
;
4379 rcdest
= unix_convert(newname
,conn
,last_component_dest
,&bad_path_dest
,&sbuf2
);
4381 /* Quick check for "." and ".." */
4382 if (last_component_dest
[0] == '.') {
4383 if (!last_component_dest
[1] || (last_component_dest
[1] == '.' && !last_component_dest
[2])) {
4384 return NT_STATUS_OBJECT_NAME_INVALID
;
4389 * Split the old name into directory and last component
4390 * strings. Note that unix_convert may have stripped off a
4391 * leading ./ from both name and newname if the rename is
4392 * at the root of the share. We need to make sure either both
4393 * name and newname contain a / character or neither of them do
4394 * as this is checked in resolve_wildcards().
4397 p
= strrchr_m(name
,'/');
4399 pstrcpy(directory
,".");
4403 pstrcpy(directory
,name
);
4405 *p
= '/'; /* Replace needed for exceptional test below. */
4409 * We should only check the mangled cache
4410 * here if unix_convert failed. This means
4411 * that the path in 'mask' doesn't exist
4412 * on the file system and so we need to look
4413 * for a possible mangle. This patch from
4414 * Tine Smukavec <valentin.smukavec@hermes.si>.
4417 if (!rc
&& mangle_is_mangled(mask
, conn
->params
))
4418 mangle_check_cache( mask
, sizeof(pstring
)-1, conn
->params
);
4422 * No wildcards - just process the one file.
4424 BOOL is_short_name
= mangle_is_8_3(name
, True
, conn
->params
);
4426 /* Add a terminating '/' to the directory name. */
4427 pstrcat(directory
,"/");
4428 pstrcat(directory
,mask
);
4430 /* Ensure newname contains a '/' also */
4431 if(strrchr_m(newname
,'/') == 0) {
4434 pstrcpy(tmpstr
, "./");
4435 pstrcat(tmpstr
, newname
);
4436 pstrcpy(newname
, tmpstr
);
4439 DEBUG(3,("rename_internals: case_sensitive = %d, case_preserve = %d, short case preserve = %d, \
4440 directory = %s, newname = %s, last_component_dest = %s, is_8_3 = %d\n",
4441 conn
->case_sensitive
, conn
->case_preserve
, conn
->short_case_preserve
, directory
,
4442 newname
, last_component_dest
, is_short_name
));
4445 * Check for special case with case preserving and not
4446 * case sensitive, if directory and newname are identical,
4447 * and the old last component differs from the original
4448 * last component only by case, then we should allow
4449 * the rename (user is trying to change the case of the
4452 if((conn
->case_sensitive
== False
) &&
4453 (((conn
->case_preserve
== True
) &&
4454 (is_short_name
== False
)) ||
4455 ((conn
->short_case_preserve
== True
) &&
4456 (is_short_name
== True
))) &&
4457 strcsequal(directory
, newname
)) {
4458 pstring modified_last_component
;
4461 * Get the last component of the modified name.
4462 * Note that we guarantee that newname contains a '/'
4465 p
= strrchr_m(newname
,'/');
4466 pstrcpy(modified_last_component
,p
+1);
4468 if(strcsequal(modified_last_component
,
4469 last_component_dest
) == False
) {
4471 * Replace the modified last component with
4474 pstrcpy(p
+1, last_component_dest
);
4478 resolve_wildcards(directory
,newname
);
4481 * The source object must exist.
4484 if (!vfs_object_exist(conn
, directory
, &sbuf1
)) {
4485 DEBUG(3,("rename_internals: source doesn't exist doing rename %s -> %s\n",
4486 directory
,newname
));
4488 if (errno
== ENOTDIR
|| errno
== EISDIR
|| errno
== ENOENT
) {
4490 * Must return different errors depending on whether the parent
4491 * directory existed or not.
4494 p
= strrchr_m(directory
, '/');
4496 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
4498 if (vfs_object_exist(conn
, directory
, NULL
))
4499 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
4500 return NT_STATUS_OBJECT_PATH_NOT_FOUND
;
4502 error
= map_nt_error_from_unix(errno
);
4503 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4504 nt_errstr(error
), directory
,newname
));
4509 if (!rcdest
&& bad_path_dest
) {
4510 if (ms_has_wild(last_component_dest
))
4511 return NT_STATUS_OBJECT_NAME_INVALID
;
4512 return NT_STATUS_OBJECT_PATH_NOT_FOUND
;
4515 error
= can_rename(conn
,directory
,attrs
,&sbuf1
);
4517 if (!NT_STATUS_IS_OK(error
)) {
4518 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4519 nt_errstr(error
), directory
,newname
));
4524 * If the src and dest names are identical - including case,
4525 * don't do the rename, just return success.
4528 if (strcsequal(directory
, newname
)) {
4529 rename_open_files(conn
, NULL
, sbuf1
.st_dev
, sbuf1
.st_ino
, newname
);
4530 DEBUG(3,("rename_internals: identical names in rename %s - returning success\n", directory
));
4531 return NT_STATUS_OK
;
4534 if(!replace_if_exists
&& vfs_object_exist(conn
,newname
,NULL
)) {
4535 DEBUG(3,("rename_internals: dest exists doing rename %s -> %s\n",
4536 directory
,newname
));
4537 return NT_STATUS_OBJECT_NAME_COLLISION
;
4540 if (rename_path_prefix_equal(directory
, newname
)) {
4541 return NT_STATUS_SHARING_VIOLATION
;
4544 lck
= get_share_mode_lock(NULL
, sbuf1
.st_dev
, sbuf1
.st_ino
, NULL
, NULL
);
4546 if(SMB_VFS_RENAME(conn
,directory
, newname
) == 0) {
4547 DEBUG(3,("rename_internals: succeeded doing rename on %s -> %s\n",
4548 directory
,newname
));
4549 rename_open_files(conn
, lck
, sbuf1
.st_dev
, sbuf1
.st_ino
, newname
);
4551 return NT_STATUS_OK
;
4555 if (errno
== ENOTDIR
|| errno
== EISDIR
)
4556 error
= NT_STATUS_OBJECT_NAME_COLLISION
;
4558 error
= map_nt_error_from_unix(errno
);
4560 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4561 nt_errstr(error
), directory
,newname
));
4566 * Wildcards - process each file that matches.
4568 struct smb_Dir
*dir_hnd
= NULL
;
4572 if (strequal(mask
,"????????.???"))
4575 if (check_name(directory
,conn
))
4576 dir_hnd
= OpenDir(conn
, directory
, mask
, attrs
);
4580 error
= NT_STATUS_NO_SUCH_FILE
;
4581 /* Was error = NT_STATUS_OBJECT_NAME_NOT_FOUND; - gentest fix. JRA */
4583 while ((dname
= ReadDirName(dir_hnd
, &offset
))) {
4585 BOOL sysdir_entry
= False
;
4587 pstrcpy(fname
,dname
);
4589 /* Quick check for "." and ".." */
4590 if (fname
[0] == '.') {
4591 if (!fname
[1] || (fname
[1] == '.' && !fname
[2])) {
4593 sysdir_entry
= True
;
4600 if (!is_visible_file(conn
, directory
, dname
, &sbuf1
, False
))
4603 if(!mask_match(fname
, mask
, conn
->case_sensitive
))
4607 error
= NT_STATUS_OBJECT_NAME_INVALID
;
4611 error
= NT_STATUS_ACCESS_DENIED
;
4612 slprintf(fname
,sizeof(fname
)-1,"%s/%s",directory
,dname
);
4613 if (!vfs_object_exist(conn
, fname
, &sbuf1
)) {
4614 error
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
4615 DEBUG(6,("rename %s failed. Error %s\n", fname
, nt_errstr(error
)));
4618 error
= can_rename(conn
,fname
,attrs
,&sbuf1
);
4619 if (!NT_STATUS_IS_OK(error
)) {
4620 DEBUG(6,("rename %s refused\n", fname
));
4623 pstrcpy(destname
,newname
);
4625 if (!resolve_wildcards(fname
,destname
)) {
4626 DEBUG(6,("resolve_wildcards %s %s failed\n",
4631 if (strcsequal(fname
,destname
)) {
4632 rename_open_files(conn
, NULL
, sbuf1
.st_dev
, sbuf1
.st_ino
, newname
);
4633 DEBUG(3,("rename_internals: identical names in wildcard rename %s - success\n", fname
));
4635 error
= NT_STATUS_OK
;
4639 if (!replace_if_exists
&&
4640 vfs_file_exist(conn
,destname
, NULL
)) {
4641 DEBUG(6,("file_exist %s\n", destname
));
4642 error
= NT_STATUS_OBJECT_NAME_COLLISION
;
4646 if (rename_path_prefix_equal(fname
, destname
)) {
4647 return NT_STATUS_SHARING_VIOLATION
;
4650 lck
= get_share_mode_lock(NULL
, sbuf1
.st_dev
, sbuf1
.st_ino
, NULL
, NULL
);
4652 if (!SMB_VFS_RENAME(conn
,fname
,destname
)) {
4653 rename_open_files(conn
, lck
, sbuf1
.st_dev
, sbuf1
.st_ino
, newname
);
4655 error
= NT_STATUS_OK
;
4658 DEBUG(3,("rename_internals: doing rename on %s -> %s\n",fname
,destname
));
4663 if (!NT_STATUS_EQUAL(error
,NT_STATUS_NO_SUCH_FILE
)) {
4664 if (!rcdest
&& bad_path_dest
) {
4665 if (ms_has_wild(last_component_dest
))
4666 return NT_STATUS_OBJECT_NAME_INVALID
;
4667 return NT_STATUS_OBJECT_PATH_NOT_FOUND
;
4672 if (count
== 0 && NT_STATUS_IS_OK(error
)) {
4673 error
= map_nt_error_from_unix(errno
);
4679 /****************************************************************************
4681 ****************************************************************************/
4683 int reply_mv(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
,
4690 uint32 attrs
= SVAL(inbuf
,smb_vwv0
);
4692 BOOL path_contains_wcard
= False
;
4694 START_PROFILE(SMBmv
);
4696 p
= smb_buf(inbuf
) + 1;
4697 p
+= srvstr_get_path_wcard(inbuf
, name
, p
, sizeof(name
), 0, STR_TERMINATE
, &status
, &path_contains_wcard
);
4698 if (!NT_STATUS_IS_OK(status
)) {
4700 return ERROR_NT(status
);
4703 p
+= srvstr_get_path(inbuf
, newname
, p
, sizeof(newname
), 0, STR_TERMINATE
, &status
);
4704 if (!NT_STATUS_IS_OK(status
)) {
4706 return ERROR_NT(status
);
4709 RESOLVE_DFSPATH_WCARD(name
, conn
, inbuf
, outbuf
);
4710 RESOLVE_DFSPATH_WCARD(newname
, conn
, inbuf
, outbuf
);
4712 DEBUG(3,("reply_mv : %s -> %s\n",name
,newname
));
4714 status
= rename_internals(conn
, name
, newname
, attrs
, False
, path_contains_wcard
);
4715 if (!NT_STATUS_IS_OK(status
)) {
4717 if (open_was_deferred(SVAL(inbuf
,smb_mid
))) {
4718 /* We have re-scheduled this call. */
4721 return ERROR_NT(status
);
4725 * Win2k needs a changenotify request response before it will
4726 * update after a rename..
4728 process_pending_change_notify_queue((time_t)0);
4729 outsize
= set_message(outbuf
,0,0,False
);
4735 /*******************************************************************
4736 Copy a file as part of a reply_copy.
4737 ******************************************************************/
4739 BOOL
copy_file(char *src
,char *dest1
,connection_struct
*conn
, int ofun
,
4740 int count
,BOOL target_is_directory
, int *err_ret
)
4742 SMB_STRUCT_STAT src_sbuf
, sbuf2
;
4744 files_struct
*fsp1
,*fsp2
;
4747 uint32 new_create_disposition
;
4752 pstrcpy(dest
,dest1
);
4753 if (target_is_directory
) {
4754 char *p
= strrchr_m(src
,'/');
4764 if (!vfs_file_exist(conn
,src
,&src_sbuf
)) {
4768 if (!target_is_directory
&& count
) {
4769 new_create_disposition
= FILE_OPEN
;
4771 if (!map_open_params_to_ntcreate(dest1
,0,ofun
,
4772 NULL
, NULL
, &new_create_disposition
, NULL
)) {
4777 status
= open_file_ntcreate(conn
,src
,&src_sbuf
,
4779 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
4782 FILE_ATTRIBUTE_NORMAL
,
4786 if (!NT_STATUS_IS_OK(status
)) {
4790 dosattrs
= dos_mode(conn
, src
, &src_sbuf
);
4791 if (SMB_VFS_STAT(conn
,dest
,&sbuf2
) == -1) {
4792 ZERO_STRUCTP(&sbuf2
);
4795 status
= open_file_ntcreate(conn
,dest
,&sbuf2
,
4797 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
4798 new_create_disposition
,
4804 if (!NT_STATUS_IS_OK(status
)) {
4805 close_file(fsp1
,ERROR_CLOSE
);
4809 if ((ofun
&3) == 1) {
4810 if(SMB_VFS_LSEEK(fsp2
,fsp2
->fh
->fd
,0,SEEK_END
) == -1) {
4811 DEBUG(0,("copy_file: error - vfs lseek returned error %s\n", strerror(errno
) ));
4813 * Stop the copy from occurring.
4816 src_sbuf
.st_size
= 0;
4820 if (src_sbuf
.st_size
) {
4821 ret
= vfs_transfer_file(fsp1
, fsp2
, src_sbuf
.st_size
);
4824 close_file(fsp1
,NORMAL_CLOSE
);
4826 /* Ensure the modtime is set correctly on the destination file. */
4827 fsp_set_pending_modtime( fsp2
, src_sbuf
.st_mtime
);
4830 * As we are opening fsp1 read-only we only expect
4831 * an error on close on fsp2 if we are out of space.
4832 * Thus we don't look at the error return from the
4835 *err_ret
= close_file(fsp2
,NORMAL_CLOSE
);
4837 return(ret
== (SMB_OFF_T
)src_sbuf
.st_size
);
4840 /****************************************************************************
4841 Reply to a file copy.
4842 ****************************************************************************/
4844 int reply_copy(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
4849 pstring mask
,newname
;
4852 int error
= ERRnoaccess
;
4856 int tid2
= SVAL(inbuf
,smb_vwv0
);
4857 int ofun
= SVAL(inbuf
,smb_vwv1
);
4858 int flags
= SVAL(inbuf
,smb_vwv2
);
4859 BOOL target_is_directory
=False
;
4860 BOOL bad_path1
= False
;
4861 BOOL bad_path2
= False
;
4862 BOOL path_contains_wcard1
= False
;
4863 BOOL path_contains_wcard2
= False
;
4865 SMB_STRUCT_STAT sbuf1
, sbuf2
;
4867 START_PROFILE(SMBcopy
);
4869 *directory
= *mask
= 0;
4872 p
+= srvstr_get_path_wcard(inbuf
, name
, p
, sizeof(name
), 0, STR_TERMINATE
, &status
, &path_contains_wcard1
);
4873 if (!NT_STATUS_IS_OK(status
)) {
4874 END_PROFILE(SMBcopy
);
4875 return ERROR_NT(status
);
4877 p
+= srvstr_get_path_wcard(inbuf
, newname
, p
, sizeof(newname
), 0, STR_TERMINATE
, &status
, &path_contains_wcard2
);
4878 if (!NT_STATUS_IS_OK(status
)) {
4879 END_PROFILE(SMBcopy
);
4880 return ERROR_NT(status
);
4883 DEBUG(3,("reply_copy : %s -> %s\n",name
,newname
));
4885 if (tid2
!= conn
->cnum
) {
4886 /* can't currently handle inter share copies XXXX */
4887 DEBUG(3,("Rejecting inter-share copy\n"));
4888 END_PROFILE(SMBcopy
);
4889 return ERROR_DOS(ERRSRV
,ERRinvdevice
);
4892 RESOLVE_DFSPATH_WCARD(name
, conn
, inbuf
, outbuf
);
4893 RESOLVE_DFSPATH_WCARD(newname
, conn
, inbuf
, outbuf
);
4895 rc
= unix_convert(name
,conn
,0,&bad_path1
,&sbuf1
);
4896 unix_convert(newname
,conn
,0,&bad_path2
,&sbuf2
);
4898 target_is_directory
= VALID_STAT_OF_DIR(sbuf2
);
4900 if ((flags
&1) && target_is_directory
) {
4901 END_PROFILE(SMBcopy
);
4902 return ERROR_DOS(ERRDOS
,ERRbadfile
);
4905 if ((flags
&2) && !target_is_directory
) {
4906 END_PROFILE(SMBcopy
);
4907 return ERROR_DOS(ERRDOS
,ERRbadpath
);
4910 if ((flags
&(1<<5)) && VALID_STAT_OF_DIR(sbuf1
)) {
4911 /* wants a tree copy! XXXX */
4912 DEBUG(3,("Rejecting tree copy\n"));
4913 END_PROFILE(SMBcopy
);
4914 return ERROR_DOS(ERRSRV
,ERRerror
);
4917 p
= strrchr_m(name
,'/');
4919 pstrcpy(directory
,"./");
4923 pstrcpy(directory
,name
);
4928 * We should only check the mangled cache
4929 * here if unix_convert failed. This means
4930 * that the path in 'mask' doesn't exist
4931 * on the file system and so we need to look
4932 * for a possible mangle. This patch from
4933 * Tine Smukavec <valentin.smukavec@hermes.si>.
4936 if (!rc
&& mangle_is_mangled(mask
, conn
->params
))
4937 mangle_check_cache( mask
, sizeof(pstring
)-1, conn
->params
);
4939 has_wild
= path_contains_wcard1
;
4942 pstrcat(directory
,"/");
4943 pstrcat(directory
,mask
);
4944 if (resolve_wildcards(directory
,newname
) &&
4945 copy_file(directory
,newname
,conn
,ofun
, count
,target_is_directory
,&err
))
4949 END_PROFILE(SMBcopy
);
4950 return(UNIXERROR(ERRHRD
,ERRgeneral
));
4953 exists
= vfs_file_exist(conn
,directory
,NULL
);
4956 struct smb_Dir
*dir_hnd
= NULL
;
4960 if (strequal(mask
,"????????.???"))
4963 if (check_name(directory
,conn
))
4964 dir_hnd
= OpenDir(conn
, directory
, mask
, 0);
4970 while ((dname
= ReadDirName(dir_hnd
, &offset
))) {
4972 pstrcpy(fname
,dname
);
4974 if (!is_visible_file(conn
, directory
, dname
, &sbuf1
, False
))
4977 if(!mask_match(fname
, mask
, conn
->case_sensitive
))
4980 error
= ERRnoaccess
;
4981 slprintf(fname
,sizeof(fname
)-1, "%s/%s",directory
,dname
);
4982 pstrcpy(destname
,newname
);
4983 if (resolve_wildcards(fname
,destname
) &&
4984 copy_file(fname
,destname
,conn
,ofun
,
4985 count
,target_is_directory
,&err
))
4987 DEBUG(3,("reply_copy : doing copy on %s -> %s\n",fname
,destname
));
4995 /* Error on close... */
4997 END_PROFILE(SMBcopy
);
4998 return(UNIXERROR(ERRHRD
,ERRgeneral
));
5002 END_PROFILE(SMBcopy
);
5003 return ERROR_DOS(ERRDOS
,error
);
5005 if((errno
== ENOENT
) && (bad_path1
|| bad_path2
) &&
5007 /* Samba 3.0.22 has ERRDOS/ERRbadpath in the
5008 * DOS error code case
5010 return ERROR_DOS(ERRDOS
, ERRbadpath
);
5012 END_PROFILE(SMBcopy
);
5013 return(UNIXERROR(ERRDOS
,error
));
5017 outsize
= set_message(outbuf
,1,0,True
);
5018 SSVAL(outbuf
,smb_vwv0
,count
);
5020 END_PROFILE(SMBcopy
);
5024 /****************************************************************************
5026 ****************************************************************************/
5028 int reply_setdir(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
5036 START_PROFILE(pathworks_setdir
);
5039 if (!CAN_SETDIR(snum
)) {
5040 END_PROFILE(pathworks_setdir
);
5041 return ERROR_DOS(ERRDOS
,ERRnoaccess
);
5044 srvstr_get_path(inbuf
, newdir
, smb_buf(inbuf
) + 1, sizeof(newdir
), 0, STR_TERMINATE
, &status
);
5045 if (!NT_STATUS_IS_OK(status
)) {
5046 END_PROFILE(pathworks_setdir
);
5047 return ERROR_NT(status
);
5050 RESOLVE_DFSPATH(newdir
, conn
, inbuf
, outbuf
);
5052 if (strlen(newdir
) == 0) {
5055 ok
= vfs_directory_exist(conn
,newdir
,NULL
);
5057 set_conn_connectpath(conn
,newdir
);
5061 END_PROFILE(pathworks_setdir
);
5062 return ERROR_DOS(ERRDOS
,ERRbadpath
);
5065 outsize
= set_message(outbuf
,0,0,False
);
5066 SCVAL(outbuf
,smb_reh
,CVAL(inbuf
,smb_reh
));
5068 DEBUG(3,("setdir %s\n", newdir
));
5070 END_PROFILE(pathworks_setdir
);
5075 #define DBGC_CLASS DBGC_LOCKING
5077 /****************************************************************************
5078 Get a lock pid, dealing with large count requests.
5079 ****************************************************************************/
5081 uint32
get_lock_pid( char *data
, int data_offset
, BOOL large_file_format
)
5083 if(!large_file_format
)
5084 return (uint32
)SVAL(data
,SMB_LPID_OFFSET(data_offset
));
5086 return (uint32
)SVAL(data
,SMB_LARGE_LPID_OFFSET(data_offset
));
5089 /****************************************************************************
5090 Get a lock count, dealing with large count requests.
5091 ****************************************************************************/
5093 SMB_BIG_UINT
get_lock_count( char *data
, int data_offset
, BOOL large_file_format
)
5095 SMB_BIG_UINT count
= 0;
5097 if(!large_file_format
) {
5098 count
= (SMB_BIG_UINT
)IVAL(data
,SMB_LKLEN_OFFSET(data_offset
));
5101 #if defined(HAVE_LONGLONG)
5102 count
= (((SMB_BIG_UINT
) IVAL(data
,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset
))) << 32) |
5103 ((SMB_BIG_UINT
) IVAL(data
,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset
)));
5104 #else /* HAVE_LONGLONG */
5107 * NT4.x seems to be broken in that it sends large file (64 bit)
5108 * lockingX calls even if the CAP_LARGE_FILES was *not*
5109 * negotiated. For boxes without large unsigned ints truncate the
5110 * lock count by dropping the top 32 bits.
5113 if(IVAL(data
,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset
)) != 0) {
5114 DEBUG(3,("get_lock_count: truncating lock count (high)0x%x (low)0x%x to just low count.\n",
5115 (unsigned int)IVAL(data
,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset
)),
5116 (unsigned int)IVAL(data
,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset
)) ));
5117 SIVAL(data
,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset
),0);
5120 count
= (SMB_BIG_UINT
)IVAL(data
,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset
));
5121 #endif /* HAVE_LONGLONG */
5127 #if !defined(HAVE_LONGLONG)
5128 /****************************************************************************
5129 Pathetically try and map a 64 bit lock offset into 31 bits. I hate Windows :-).
5130 ****************************************************************************/
5132 static uint32
map_lock_offset(uint32 high
, uint32 low
)
5136 uint32 highcopy
= high
;
5139 * Try and find out how many significant bits there are in high.
5142 for(i
= 0; highcopy
; i
++)
5146 * We use 31 bits not 32 here as POSIX
5147 * lock offsets may not be negative.
5150 mask
= (~0) << (31 - i
);
5153 return 0; /* Fail. */
5159 #endif /* !defined(HAVE_LONGLONG) */
5161 /****************************************************************************
5162 Get a lock offset, dealing with large offset requests.
5163 ****************************************************************************/
5165 SMB_BIG_UINT
get_lock_offset( char *data
, int data_offset
, BOOL large_file_format
, BOOL
*err
)
5167 SMB_BIG_UINT offset
= 0;
5171 if(!large_file_format
) {
5172 offset
= (SMB_BIG_UINT
)IVAL(data
,SMB_LKOFF_OFFSET(data_offset
));
5175 #if defined(HAVE_LONGLONG)
5176 offset
= (((SMB_BIG_UINT
) IVAL(data
,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset
))) << 32) |
5177 ((SMB_BIG_UINT
) IVAL(data
,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset
)));
5178 #else /* HAVE_LONGLONG */
5181 * NT4.x seems to be broken in that it sends large file (64 bit)
5182 * lockingX calls even if the CAP_LARGE_FILES was *not*
5183 * negotiated. For boxes without large unsigned ints mangle the
5184 * lock offset by mapping the top 32 bits onto the lower 32.
5187 if(IVAL(data
,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset
)) != 0) {
5188 uint32 low
= IVAL(data
,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset
));
5189 uint32 high
= IVAL(data
,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset
));
5192 if((new_low
= map_lock_offset(high
, low
)) == 0) {
5194 return (SMB_BIG_UINT
)-1;
5197 DEBUG(3,("get_lock_offset: truncating lock offset (high)0x%x (low)0x%x to offset 0x%x.\n",
5198 (unsigned int)high
, (unsigned int)low
, (unsigned int)new_low
));
5199 SIVAL(data
,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset
),0);
5200 SIVAL(data
,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset
),new_low
);
5203 offset
= (SMB_BIG_UINT
)IVAL(data
,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset
));
5204 #endif /* HAVE_LONGLONG */
5210 /****************************************************************************
5211 Reply to a lockingX request.
5212 ****************************************************************************/
5214 int reply_lockingX(connection_struct
*conn
, char *inbuf
, char *outbuf
,
5215 int length
, int bufsize
)
5217 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv2
);
5218 unsigned char locktype
= CVAL(inbuf
,smb_vwv3
);
5219 unsigned char oplocklevel
= CVAL(inbuf
,smb_vwv3
+1);
5220 uint16 num_ulocks
= SVAL(inbuf
,smb_vwv6
);
5221 uint16 num_locks
= SVAL(inbuf
,smb_vwv7
);
5222 SMB_BIG_UINT count
= 0, offset
= 0;
5224 int32 lock_timeout
= IVAL(inbuf
,smb_vwv4
);
5227 BOOL large_file_format
=
5228 (locktype
& LOCKING_ANDX_LARGE_FILES
)?True
:False
;
5230 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
5232 START_PROFILE(SMBlockingX
);
5234 CHECK_FSP(fsp
,conn
);
5236 data
= smb_buf(inbuf
);
5238 if (locktype
& LOCKING_ANDX_CHANGE_LOCKTYPE
) {
5239 /* we don't support these - and CANCEL_LOCK makes w2k
5240 and XP reboot so I don't really want to be
5241 compatible! (tridge) */
5242 return ERROR_NT(NT_STATUS_DOS(ERRDOS
, ERRnoatomiclocks
));
5245 /* Check if this is an oplock break on a file
5246 we have granted an oplock on.
5248 if ((locktype
& LOCKING_ANDX_OPLOCK_RELEASE
)) {
5249 /* Client can insist on breaking to none. */
5250 BOOL break_to_none
= (oplocklevel
== 0);
5253 DEBUG(5,("reply_lockingX: oplock break reply (%u) from client "
5254 "for fnum = %d\n", (unsigned int)oplocklevel
,
5258 * Make sure we have granted an exclusive or batch oplock on
5262 if (fsp
->oplock_type
== 0) {
5264 /* The Samba4 nbench simulator doesn't understand
5265 the difference between break to level2 and break
5266 to none from level2 - it sends oplock break
5267 replies in both cases. Don't keep logging an error
5268 message here - just ignore it. JRA. */
5270 DEBUG(5,("reply_lockingX: Error : oplock break from "
5271 "client for fnum = %d (oplock=%d) and no "
5272 "oplock granted on this file (%s).\n",
5273 fsp
->fnum
, fsp
->oplock_type
, fsp
->fsp_name
));
5275 /* if this is a pure oplock break request then don't
5277 if (num_locks
== 0 && num_ulocks
== 0) {
5278 END_PROFILE(SMBlockingX
);
5281 END_PROFILE(SMBlockingX
);
5282 return ERROR_DOS(ERRDOS
,ERRlock
);
5286 if ((fsp
->sent_oplock_break
== BREAK_TO_NONE_SENT
) ||
5288 result
= remove_oplock(fsp
);
5290 result
= downgrade_oplock(fsp
);
5294 DEBUG(0, ("reply_lockingX: error in removing "
5295 "oplock on file %s\n", fsp
->fsp_name
));
5296 /* Hmmm. Is this panic justified? */
5297 smb_panic("internal tdb error");
5300 reply_to_oplock_break_requests(fsp
);
5302 /* if this is a pure oplock break request then don't send a
5304 if (num_locks
== 0 && num_ulocks
== 0) {
5305 /* Sanity check - ensure a pure oplock break is not a
5307 if(CVAL(inbuf
,smb_vwv0
) != 0xff)
5308 DEBUG(0,("reply_lockingX: Error : pure oplock "
5309 "break is a chained %d request !\n",
5310 (unsigned int)CVAL(inbuf
,smb_vwv0
) ));
5311 END_PROFILE(SMBlockingX
);
5317 * We do this check *after* we have checked this is not a oplock break
5318 * response message. JRA.
5321 release_level_2_oplocks_on_change(fsp
);
5323 /* Data now points at the beginning of the list
5324 of smb_unlkrng structs */
5325 for(i
= 0; i
< (int)num_ulocks
; i
++) {
5326 lock_pid
= get_lock_pid( data
, i
, large_file_format
);
5327 count
= get_lock_count( data
, i
, large_file_format
);
5328 offset
= get_lock_offset( data
, i
, large_file_format
, &err
);
5331 * There is no error code marked "stupid client bug".... :-).
5334 END_PROFILE(SMBlockingX
);
5335 return ERROR_DOS(ERRDOS
,ERRnoaccess
);
5338 DEBUG(10,("reply_lockingX: unlock start=%.0f, len=%.0f for "
5339 "pid %u, file %s\n", (double)offset
, (double)count
,
5340 (unsigned int)lock_pid
, fsp
->fsp_name
));
5342 status
= do_unlock(fsp
,
5348 if (NT_STATUS_V(status
)) {
5349 END_PROFILE(SMBlockingX
);
5350 return ERROR_NT(status
);
5354 /* Setup the timeout in seconds. */
5356 if (lp_blocking_locks(SNUM(conn
))) {
5357 lock_timeout
= ((lock_timeout
== -1) ? -1 : (lock_timeout
+999)/1000);
5362 /* Now do any requested locks */
5363 data
+= ((large_file_format
? 20 : 10)*num_ulocks
);
5365 /* Data now points at the beginning of the list
5366 of smb_lkrng structs */
5368 for(i
= 0; i
< (int)num_locks
; i
++) {
5369 enum brl_type lock_type
= ((locktype
& LOCKING_ANDX_SHARED_LOCK
) ?
5370 READ_LOCK
:WRITE_LOCK
);
5371 lock_pid
= get_lock_pid( data
, i
, large_file_format
);
5372 count
= get_lock_count( data
, i
, large_file_format
);
5373 offset
= get_lock_offset( data
, i
, large_file_format
, &err
);
5376 * There is no error code marked "stupid client bug".... :-).
5379 END_PROFILE(SMBlockingX
);
5380 return ERROR_DOS(ERRDOS
,ERRnoaccess
);
5383 DEBUG(10,("reply_lockingX: lock start=%.0f, len=%.0f for pid "
5384 "%u, file %s timeout = %d\n", (double)offset
,
5385 (double)count
, (unsigned int)lock_pid
,
5386 fsp
->fsp_name
, (int)lock_timeout
));
5388 if (locktype
& LOCKING_ANDX_CANCEL_LOCK
) {
5389 if (lp_blocking_locks(SNUM(conn
))) {
5391 /* Schedule a message to ourselves to
5392 remove the blocking lock record and
5393 return the right error. */
5395 if (!blocking_lock_cancel(fsp
,
5401 NT_STATUS_FILE_LOCK_CONFLICT
)) {
5402 END_PROFILE(SMBlockingX
);
5403 return ERROR_NT(NT_STATUS_DOS(ERRDOS
, ERRcancelviolation
));
5406 /* Remove a matching pending lock. */
5407 status
= do_lock_cancel(fsp
,
5413 status
= do_lock(fsp
,
5421 if (NT_STATUS_V(status
)) {
5423 * Interesting fact found by IFSTEST /t
5424 * LockOverlappedTest... Even if it's our own lock
5425 * context, we need to wait here as there may be an
5426 * unlock on the way. JRA.
5428 if ((lock_timeout
!= 0) &&
5429 ERROR_WAS_LOCK_DENIED(status
)) {
5431 * A blocking lock was requested. Package up
5432 * this smb into a queued request and push it
5433 * onto the blocking lock queue.
5435 if(push_blocking_lock_request(inbuf
, length
,
5444 END_PROFILE(SMBlockingX
);
5451 if (NT_STATUS_V(status
)) {
5452 END_PROFILE(SMBlockingX
);
5453 return ERROR_NT(status
);
5457 /* If any of the above locks failed, then we must unlock
5458 all of the previous locks (X/Open spec). */
5460 if (!(locktype
& LOCKING_ANDX_CANCEL_LOCK
) &&
5464 * Ensure we don't do a remove on the lock that just failed,
5465 * as under POSIX rules, if we have a lock already there, we
5466 * will delete it (and we shouldn't) .....
5468 for(i
--; i
>= 0; i
--) {
5469 lock_pid
= get_lock_pid( data
, i
, large_file_format
);
5470 count
= get_lock_count( data
, i
, large_file_format
);
5471 offset
= get_lock_offset( data
, i
, large_file_format
,
5475 * There is no error code marked "stupid client
5479 END_PROFILE(SMBlockingX
);
5480 return ERROR_DOS(ERRDOS
,ERRnoaccess
);
5489 END_PROFILE(SMBlockingX
);
5490 return ERROR_NT(status
);
5493 set_message(outbuf
,2,0,True
);
5495 DEBUG(3, ("lockingX fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
5496 fsp
->fnum
, (unsigned int)locktype
, num_locks
, num_ulocks
));
5498 END_PROFILE(SMBlockingX
);
5499 return chain_reply(inbuf
,outbuf
,length
,bufsize
);
5503 #define DBGC_CLASS DBGC_ALL
5505 /****************************************************************************
5506 Reply to a SMBreadbmpx (read block multiplex) request.
5507 ****************************************************************************/
5509 int reply_readbmpx(connection_struct
*conn
, char *inbuf
,char *outbuf
,int length
,int bufsize
)
5520 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
5521 START_PROFILE(SMBreadBmpx
);
5523 /* this function doesn't seem to work - disable by default */
5524 if (!lp_readbmpx()) {
5525 END_PROFILE(SMBreadBmpx
);
5526 return ERROR_DOS(ERRSRV
,ERRuseSTD
);
5529 outsize
= set_message(outbuf
,8,0,True
);
5531 CHECK_FSP(fsp
,conn
);
5532 if (!CHECK_READ(fsp
,inbuf
)) {
5533 return(ERROR_DOS(ERRDOS
,ERRbadaccess
));
5536 startpos
= IVAL_TO_SMB_OFF_T(inbuf
,smb_vwv1
);
5537 maxcount
= SVAL(inbuf
,smb_vwv3
);
5539 data
= smb_buf(outbuf
);
5540 pad
= ((long)data
)%4;
5545 max_per_packet
= bufsize
-(outsize
+pad
);
5549 if (is_locked(fsp
,(uint32
)SVAL(inbuf
,smb_pid
),(SMB_BIG_UINT
)maxcount
,(SMB_BIG_UINT
)startpos
, READ_LOCK
)) {
5550 END_PROFILE(SMBreadBmpx
);
5551 return ERROR_DOS(ERRDOS
,ERRlock
);
5555 size_t N
= MIN(max_per_packet
,tcount
-total_read
);
5557 nread
= read_file(fsp
,data
,startpos
,N
);
5562 if (nread
< (ssize_t
)N
)
5563 tcount
= total_read
+ nread
;
5565 set_message(outbuf
,8,nread
+pad
,False
);
5566 SIVAL(outbuf
,smb_vwv0
,startpos
);
5567 SSVAL(outbuf
,smb_vwv2
,tcount
);
5568 SSVAL(outbuf
,smb_vwv6
,nread
);
5569 SSVAL(outbuf
,smb_vwv7
,smb_offset(data
,outbuf
));
5572 if (!send_smb(smbd_server_fd(),outbuf
))
5573 exit_server("reply_readbmpx: send_smb failed.");
5575 total_read
+= nread
;
5577 } while (total_read
< (ssize_t
)tcount
);
5579 END_PROFILE(SMBreadBmpx
);
5583 /****************************************************************************
5584 Reply to a SMBsetattrE.
5585 ****************************************************************************/
5587 int reply_setattrE(connection_struct
*conn
, char *inbuf
,char *outbuf
, int size
, int dum_buffsize
)
5589 struct utimbuf unix_times
;
5591 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
5592 START_PROFILE(SMBsetattrE
);
5594 outsize
= set_message(outbuf
,0,0,False
);
5596 if(!fsp
|| (fsp
->conn
!= conn
)) {
5597 END_PROFILE(SMBsetattrE
);
5598 return ERROR_DOS(ERRDOS
,ERRbadfid
);
5602 * Convert the DOS times into unix times. Ignore create
5603 * time as UNIX can't set this.
5606 unix_times
.actime
= srv_make_unix_date2(inbuf
+smb_vwv3
);
5607 unix_times
.modtime
= srv_make_unix_date2(inbuf
+smb_vwv5
);
5610 * Patch from Ray Frush <frush@engr.colostate.edu>
5611 * Sometimes times are sent as zero - ignore them.
5614 if (null_mtime(unix_times
.actime
) && null_mtime(unix_times
.modtime
)) {
5615 /* Ignore request */
5616 if( DEBUGLVL( 3 ) ) {
5617 dbgtext( "reply_setattrE fnum=%d ", fsp
->fnum
);
5618 dbgtext( "ignoring zero request - not setting timestamps of 0\n" );
5620 END_PROFILE(SMBsetattrE
);
5622 } else if (!null_mtime(unix_times
.actime
) && null_mtime(unix_times
.modtime
)) {
5623 /* set modify time = to access time if modify time was unset */
5624 unix_times
.modtime
= unix_times
.actime
;
5627 /* Set the date on this file */
5628 /* Should we set pending modtime here ? JRA */
5629 if(file_utime(conn
, fsp
->fsp_name
, &unix_times
)) {
5630 END_PROFILE(SMBsetattrE
);
5631 return ERROR_DOS(ERRDOS
,ERRnoaccess
);
5634 DEBUG( 3, ( "reply_setattrE fnum=%d actime=%d modtime=%d\n",
5635 fsp
->fnum
, (int)unix_times
.actime
, (int)unix_times
.modtime
) );
5637 END_PROFILE(SMBsetattrE
);
5642 /* Back from the dead for OS/2..... JRA. */
5644 /****************************************************************************
5645 Reply to a SMBwritebmpx (write block multiplex primary) request.
5646 ****************************************************************************/
5648 int reply_writebmpx(connection_struct
*conn
, char *inbuf
,char *outbuf
, int size
, int dum_buffsize
)
5651 ssize_t nwritten
= -1;
5658 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
5659 START_PROFILE(SMBwriteBmpx
);
5661 CHECK_FSP(fsp
,conn
);
5662 if (!CHECK_WRITE(fsp
)) {
5663 return(ERROR_DOS(ERRDOS
,ERRbadaccess
));
5665 if (HAS_CACHED_ERROR(fsp
)) {
5666 return(CACHED_ERROR(fsp
));
5669 tcount
= SVAL(inbuf
,smb_vwv1
);
5670 startpos
= IVAL_TO_SMB_OFF_T(inbuf
,smb_vwv3
);
5671 write_through
= BITSETW(inbuf
+smb_vwv7
,0);
5672 numtowrite
= SVAL(inbuf
,smb_vwv10
);
5673 smb_doff
= SVAL(inbuf
,smb_vwv11
);
5675 data
= smb_base(inbuf
) + smb_doff
;
5677 /* If this fails we need to send an SMBwriteC response,
5678 not an SMBwritebmpx - set this up now so we don't forget */
5679 SCVAL(outbuf
,smb_com
,SMBwritec
);
5681 if (is_locked(fsp
,(uint32
)SVAL(inbuf
,smb_pid
),(SMB_BIG_UINT
)tcount
,(SMB_BIG_UINT
)startpos
,WRITE_LOCK
)) {
5682 END_PROFILE(SMBwriteBmpx
);
5683 return(ERROR_DOS(ERRDOS
,ERRlock
));
5686 nwritten
= write_file(fsp
,data
,startpos
,numtowrite
);
5688 sync_file(conn
, fsp
, write_through
);
5690 if(nwritten
< (ssize_t
)numtowrite
) {
5691 END_PROFILE(SMBwriteBmpx
);
5692 return(UNIXERROR(ERRHRD
,ERRdiskfull
));
5695 /* If the maximum to be written to this file
5696 is greater than what we just wrote then set
5697 up a secondary struct to be attached to this
5698 fd, we will use this to cache error messages etc. */
5700 if((ssize_t
)tcount
> nwritten
) {
5701 write_bmpx_struct
*wbms
;
5702 if(fsp
->wbmpx_ptr
!= NULL
)
5703 wbms
= fsp
->wbmpx_ptr
; /* Use an existing struct */
5705 wbms
= SMB_MALLOC_P(write_bmpx_struct
);
5707 DEBUG(0,("Out of memory in reply_readmpx\n"));
5708 END_PROFILE(SMBwriteBmpx
);
5709 return(ERROR_DOS(ERRSRV
,ERRnoresource
));
5711 wbms
->wr_mode
= write_through
;
5712 wbms
->wr_discard
= False
; /* No errors yet */
5713 wbms
->wr_total_written
= nwritten
;
5714 wbms
->wr_errclass
= 0;
5716 fsp
->wbmpx_ptr
= wbms
;
5719 /* We are returning successfully, set the message type back to
5721 SCVAL(outbuf
,smb_com
,SMBwriteBmpx
);
5723 outsize
= set_message(outbuf
,1,0,True
);
5725 SSVALS(outbuf
,smb_vwv0
,-1); /* We don't support smb_remaining */
5727 DEBUG( 3, ( "writebmpx fnum=%d num=%d wrote=%d\n",
5728 fsp
->fnum
, (int)numtowrite
, (int)nwritten
) );
5730 if (write_through
&& tcount
==nwritten
) {
5731 /* We need to send both a primary and a secondary response */
5732 smb_setlen(outbuf
,outsize
- 4);
5734 if (!send_smb(smbd_server_fd(),outbuf
))
5735 exit_server("reply_writebmpx: send_smb failed.");
5737 /* Now the secondary */
5738 outsize
= set_message(outbuf
,1,0,True
);
5739 SCVAL(outbuf
,smb_com
,SMBwritec
);
5740 SSVAL(outbuf
,smb_vwv0
,nwritten
);
5743 END_PROFILE(SMBwriteBmpx
);
5747 /****************************************************************************
5748 Reply to a SMBwritebs (write block multiplex secondary) request.
5749 ****************************************************************************/
5751 int reply_writebs(connection_struct
*conn
, char *inbuf
,char *outbuf
, int dum_size
, int dum_buffsize
)
5754 ssize_t nwritten
= -1;
5761 write_bmpx_struct
*wbms
;
5762 BOOL send_response
= False
;
5763 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
5764 START_PROFILE(SMBwriteBs
);
5766 CHECK_FSP(fsp
,conn
);
5767 if (!CHECK_WRITE(fsp
)) {
5768 return(ERROR_DOS(ERRDOS
,ERRbadaccess
));
5771 tcount
= SVAL(inbuf
,smb_vwv1
);
5772 startpos
= IVAL_TO_SMB_OFF_T(inbuf
,smb_vwv2
);
5773 numtowrite
= SVAL(inbuf
,smb_vwv6
);
5774 smb_doff
= SVAL(inbuf
,smb_vwv7
);
5776 data
= smb_base(inbuf
) + smb_doff
;
5778 /* We need to send an SMBwriteC response, not an SMBwritebs */
5779 SCVAL(outbuf
,smb_com
,SMBwritec
);
5781 /* This fd should have an auxiliary struct attached,
5782 check that it does */
5783 wbms
= fsp
->wbmpx_ptr
;
5785 END_PROFILE(SMBwriteBs
);
5789 /* If write through is set we can return errors, else we must cache them */
5790 write_through
= wbms
->wr_mode
;
5792 /* Check for an earlier error */
5793 if(wbms
->wr_discard
) {
5794 END_PROFILE(SMBwriteBs
);
5795 return -1; /* Just discard the packet */
5798 nwritten
= write_file(fsp
,data
,startpos
,numtowrite
);
5800 sync_file(conn
, fsp
, write_through
);
5802 if (nwritten
< (ssize_t
)numtowrite
) {
5804 /* We are returning an error - we can delete the aux struct */
5807 fsp
->wbmpx_ptr
= NULL
;
5808 END_PROFILE(SMBwriteBs
);
5809 return(ERROR_DOS(ERRHRD
,ERRdiskfull
));
5811 wbms
->wr_errclass
= ERRHRD
;
5812 wbms
->wr_error
= ERRdiskfull
;
5813 wbms
->wr_status
= NT_STATUS_DISK_FULL
;
5814 wbms
->wr_discard
= True
;
5815 END_PROFILE(SMBwriteBs
);
5819 /* Increment the total written, if this matches tcount
5820 we can discard the auxiliary struct (hurrah !) and return a writeC */
5821 wbms
->wr_total_written
+= nwritten
;
5822 if(wbms
->wr_total_written
>= tcount
) {
5823 if (write_through
) {
5824 outsize
= set_message(outbuf
,1,0,True
);
5825 SSVAL(outbuf
,smb_vwv0
,wbms
->wr_total_written
);
5826 send_response
= True
;
5830 fsp
->wbmpx_ptr
= NULL
;
5834 END_PROFILE(SMBwriteBs
);
5838 END_PROFILE(SMBwriteBs
);
5842 /****************************************************************************
5843 Reply to a SMBgetattrE.
5844 ****************************************************************************/
5846 int reply_getattrE(connection_struct
*conn
, char *inbuf
,char *outbuf
, int size
, int dum_buffsize
)
5848 SMB_STRUCT_STAT sbuf
;
5851 files_struct
*fsp
= file_fsp(inbuf
,smb_vwv0
);
5852 START_PROFILE(SMBgetattrE
);
5854 outsize
= set_message(outbuf
,11,0,True
);
5856 if(!fsp
|| (fsp
->conn
!= conn
)) {
5857 END_PROFILE(SMBgetattrE
);
5858 return ERROR_DOS(ERRDOS
,ERRbadfid
);
5861 /* Do an fstat on this file */
5862 if(fsp_stat(fsp
, &sbuf
)) {
5863 END_PROFILE(SMBgetattrE
);
5864 return(UNIXERROR(ERRDOS
,ERRnoaccess
));
5867 mode
= dos_mode(conn
,fsp
->fsp_name
,&sbuf
);
5870 * Convert the times into dos times. Set create
5871 * date to be last modify date as UNIX doesn't save
5875 srv_put_dos_date2(outbuf
,smb_vwv0
,get_create_time(&sbuf
,lp_fake_dir_create_times(SNUM(conn
))));
5876 srv_put_dos_date2(outbuf
,smb_vwv2
,sbuf
.st_atime
);
5877 /* Should we check pending modtime here ? JRA */
5878 srv_put_dos_date2(outbuf
,smb_vwv4
,sbuf
.st_mtime
);
5881 SIVAL(outbuf
,smb_vwv6
,0);
5882 SIVAL(outbuf
,smb_vwv8
,0);
5884 uint32 allocation_size
= get_allocation_size(conn
,fsp
, &sbuf
);
5885 SIVAL(outbuf
,smb_vwv6
,(uint32
)sbuf
.st_size
);
5886 SIVAL(outbuf
,smb_vwv8
,allocation_size
);
5888 SSVAL(outbuf
,smb_vwv10
, mode
);
5890 DEBUG( 3, ( "reply_getattrE fnum=%d\n", fsp
->fnum
));
5892 END_PROFILE(SMBgetattrE
);