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-2007.
7 Copyright (C) Volker Lendecke 2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
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
;
32 extern uint32 global_client_caps
;
34 extern bool global_encrypted_passwords_negotiated
;
36 /****************************************************************************
37 Ensure we check the path in *exactly* the same way as W2K for a findfirst/findnext
38 path or anything including wildcards.
39 We're assuming here that '/' is not the second byte in any multibyte char
40 set (a safe assumption). '\\' *may* be the second byte in a multibyte char
42 ****************************************************************************/
44 /* Custom version for processing POSIX paths. */
45 #define IS_PATH_SEP(c,posix_only) ((c) == '/' || (!(posix_only) && (c) == '\\'))
47 static NTSTATUS
check_path_syntax_internal(char *path
,
49 bool *p_last_component_contains_wcard
)
53 NTSTATUS ret
= NT_STATUS_OK
;
54 bool start_of_name_component
= True
;
55 bool stream_started
= false;
57 *p_last_component_contains_wcard
= False
;
64 return NT_STATUS_OBJECT_NAME_INVALID
;
67 return NT_STATUS_OBJECT_NAME_INVALID
;
69 if (strchr_m(&s
[1], ':')) {
70 return NT_STATUS_OBJECT_NAME_INVALID
;
72 if (StrCaseCmp(s
, ":$DATA") != 0) {
73 return NT_STATUS_INVALID_PARAMETER
;
79 if (!stream_started
&& *s
== ':') {
80 if (*p_last_component_contains_wcard
) {
81 return NT_STATUS_OBJECT_NAME_INVALID
;
83 /* stream names allow more characters than file names */
84 stream_started
= true;
85 start_of_name_component
= false;
89 return NT_STATUS_OBJECT_NAME_INVALID
;
93 if (!stream_started
&& IS_PATH_SEP(*s
,posix_path
)) {
95 * Safe to assume is not the second part of a mb char
96 * as this is handled below.
98 /* Eat multiple '/' or '\\' */
99 while (IS_PATH_SEP(*s
,posix_path
)) {
102 if ((d
!= path
) && (*s
!= '\0')) {
103 /* We only care about non-leading or trailing '/' or '\\' */
107 start_of_name_component
= True
;
109 *p_last_component_contains_wcard
= False
;
113 if (start_of_name_component
) {
114 if ((s
[0] == '.') && (s
[1] == '.') && (IS_PATH_SEP(s
[2],posix_path
) || s
[2] == '\0')) {
115 /* Uh oh - "/../" or "\\..\\" or "/..\0" or "\\..\0" ! */
118 * No mb char starts with '.' so we're safe checking the directory separator here.
121 /* If we just added a '/' - delete it */
122 if ((d
> path
) && (*(d
-1) == '/')) {
127 /* Are we at the start ? Can't go back further if so. */
129 ret
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
132 /* Go back one level... */
133 /* We know this is safe as '/' cannot be part of a mb sequence. */
134 /* NOTE - if this assumption is invalid we are not in good shape... */
135 /* Decrement d first as d points to the *next* char to write into. */
136 for (d
--; d
> path
; d
--) {
140 s
+= 2; /* Else go past the .. */
141 /* We're still at the start of a name component, just the previous one. */
144 } else if ((s
[0] == '.') && ((s
[1] == '\0') || IS_PATH_SEP(s
[1],posix_path
))) {
156 if (*s
<= 0x1f || *s
== '|') {
157 return NT_STATUS_OBJECT_NAME_INVALID
;
165 *p_last_component_contains_wcard
= True
;
174 /* Get the size of the next MB character. */
175 next_codepoint(s
,&siz
);
193 DEBUG(0,("check_path_syntax_internal: character length assumptions invalid !\n"));
195 return NT_STATUS_INVALID_PARAMETER
;
198 start_of_name_component
= False
;
206 /****************************************************************************
207 Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
208 No wildcards allowed.
209 ****************************************************************************/
211 NTSTATUS
check_path_syntax(char *path
)
214 return check_path_syntax_internal(path
, False
, &ignore
);
217 /****************************************************************************
218 Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
219 Wildcards allowed - p_contains_wcard returns true if the last component contained
221 ****************************************************************************/
223 NTSTATUS
check_path_syntax_wcard(char *path
, bool *p_contains_wcard
)
225 return check_path_syntax_internal(path
, False
, p_contains_wcard
);
228 /****************************************************************************
229 Check the path for a POSIX client.
230 We're assuming here that '/' is not the second byte in any multibyte char
231 set (a safe assumption).
232 ****************************************************************************/
234 NTSTATUS
check_path_syntax_posix(char *path
)
237 return check_path_syntax_internal(path
, True
, &ignore
);
240 /****************************************************************************
241 Pull a string and check the path allowing a wilcard - provide for error return.
242 ****************************************************************************/
244 size_t srvstr_get_path_wcard(TALLOC_CTX
*ctx
,
252 bool *contains_wcard
)
259 ret
= srvstr_pull_buf_talloc(ctx
,
266 ret
= srvstr_pull_talloc(ctx
,
276 *err
= NT_STATUS_INVALID_PARAMETER
;
280 *contains_wcard
= False
;
282 if (smb_flags2
& FLAGS2_DFS_PATHNAMES
) {
284 * For a DFS path the function parse_dfs_path()
285 * will do the path processing, just make a copy.
291 if (lp_posix_pathnames()) {
292 *err
= check_path_syntax_posix(*pp_dest
);
294 *err
= check_path_syntax_wcard(*pp_dest
, contains_wcard
);
300 /****************************************************************************
301 Pull a string and check the path - provide for error return.
302 ****************************************************************************/
304 size_t srvstr_get_path(TALLOC_CTX
*ctx
,
318 ret
= srvstr_pull_buf_talloc(ctx
,
325 ret
= srvstr_pull_talloc(ctx
,
335 *err
= NT_STATUS_INVALID_PARAMETER
;
339 if (smb_flags2
& FLAGS2_DFS_PATHNAMES
) {
341 * For a DFS path the function parse_dfs_path()
342 * will do the path processing, just make a copy.
348 if (lp_posix_pathnames()) {
349 *err
= check_path_syntax_posix(*pp_dest
);
351 *err
= check_path_syntax(*pp_dest
);
357 /****************************************************************************
358 Check if we have a correct fsp pointing to a file. Basic check for open fsp.
359 ****************************************************************************/
361 bool check_fsp_open(connection_struct
*conn
, struct smb_request
*req
,
364 if (!(fsp
) || !(conn
)) {
365 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
368 if (((conn
) != (fsp
)->conn
) || req
->vuid
!= (fsp
)->vuid
) {
369 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
375 /****************************************************************************
376 Check if we have a correct fsp pointing to a file.
377 ****************************************************************************/
379 bool check_fsp(connection_struct
*conn
, struct smb_request
*req
,
382 if (!check_fsp_open(conn
, req
, fsp
)) {
385 if ((fsp
)->is_directory
) {
386 reply_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
389 if ((fsp
)->fh
->fd
== -1) {
390 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
393 (fsp
)->num_smb_operations
++;
397 /****************************************************************************
398 Check if we have a correct fsp pointing to a quota fake file. Replacement for
399 the CHECK_NTQUOTA_HANDLE_OK macro.
400 ****************************************************************************/
402 bool check_fsp_ntquota_handle(connection_struct
*conn
, struct smb_request
*req
,
405 if (!check_fsp_open(conn
, req
, fsp
)) {
409 if (fsp
->is_directory
) {
413 if (fsp
->fake_file_handle
== NULL
) {
417 if (fsp
->fake_file_handle
->type
!= FAKE_FILE_TYPE_QUOTA
) {
421 if (fsp
->fake_file_handle
->private_data
== NULL
) {
428 /****************************************************************************
429 Check if we have a correct fsp. Replacement for the FSP_BELONGS_CONN macro
430 ****************************************************************************/
432 bool fsp_belongs_conn(connection_struct
*conn
, struct smb_request
*req
,
435 if ((fsp
) && (conn
) && ((conn
)==(fsp
)->conn
)
436 && (req
->vuid
== (fsp
)->vuid
)) {
440 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
444 /****************************************************************************
445 Reply to a (netbios-level) special message.
446 ****************************************************************************/
448 void reply_special(char *inbuf
)
450 int msg_type
= CVAL(inbuf
,0);
451 int msg_flags
= CVAL(inbuf
,1);
456 * We only really use 4 bytes of the outbuf, but for the smb_setlen
457 * calculation & friends (srv_send_smb uses that) we need the full smb
460 char outbuf
[smb_size
];
462 static bool already_got_session
= False
;
466 memset(outbuf
, '\0', sizeof(outbuf
));
468 smb_setlen(outbuf
,0);
471 case 0x81: /* session request */
473 if (already_got_session
) {
474 exit_server_cleanly("multiple session request not permitted");
477 SCVAL(outbuf
,0,0x82);
479 if (name_len(inbuf
+4) > 50 ||
480 name_len(inbuf
+4 + name_len(inbuf
+ 4)) > 50) {
481 DEBUG(0,("Invalid name length in session request\n"));
484 name_extract(inbuf
,4,name1
);
485 name_type
= name_extract(inbuf
,4 + name_len(inbuf
+ 4),name2
);
486 DEBUG(2,("netbios connect: name1=%s name2=%s\n",
489 set_local_machine_name(name1
, True
);
490 set_remote_machine_name(name2
, True
);
492 DEBUG(2,("netbios connect: local=%s remote=%s, name type = %x\n",
493 get_local_machine_name(), get_remote_machine_name(),
496 if (name_type
== 'R') {
497 /* We are being asked for a pathworks session ---
499 SCVAL(outbuf
, 0,0x83);
503 /* only add the client's machine name to the list
504 of possibly valid usernames if we are operating
505 in share mode security */
506 if (lp_security() == SEC_SHARE
) {
507 add_session_user(get_remote_machine_name());
510 reload_services(True
);
513 already_got_session
= True
;
516 case 0x89: /* session keepalive request
517 (some old clients produce this?) */
518 SCVAL(outbuf
,0,SMBkeepalive
);
522 case 0x82: /* positive session response */
523 case 0x83: /* negative session response */
524 case 0x84: /* retarget session response */
525 DEBUG(0,("Unexpected session response\n"));
528 case SMBkeepalive
: /* session keepalive */
533 DEBUG(5,("init msg_type=0x%x msg_flags=0x%x\n",
534 msg_type
, msg_flags
));
536 srv_send_smb(smbd_server_fd(), outbuf
, false);
540 /****************************************************************************
542 conn POINTER CAN BE NULL HERE !
543 ****************************************************************************/
545 void reply_tcon(struct smb_request
*req
)
547 connection_struct
*conn
= req
->conn
;
549 char *service_buf
= NULL
;
550 char *password
= NULL
;
555 DATA_BLOB password_blob
;
556 TALLOC_CTX
*ctx
= talloc_tos();
558 START_PROFILE(SMBtcon
);
560 if (smb_buflen(req
->inbuf
) < 4) {
561 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
562 END_PROFILE(SMBtcon
);
566 p
= smb_buf(req
->inbuf
)+1;
567 p
+= srvstr_pull_buf_talloc(ctx
, req
->inbuf
, req
->flags2
,
568 &service_buf
, p
, STR_TERMINATE
) + 1;
569 pwlen
= srvstr_pull_buf_talloc(ctx
, req
->inbuf
, req
->flags2
,
570 &password
, p
, STR_TERMINATE
) + 1;
572 p
+= srvstr_pull_buf_talloc(ctx
, req
->inbuf
, req
->flags2
,
573 &dev
, p
, STR_TERMINATE
) + 1;
575 if (service_buf
== NULL
|| password
== NULL
|| dev
== NULL
) {
576 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
577 END_PROFILE(SMBtcon
);
580 p
= strrchr_m(service_buf
,'\\');
584 service
= service_buf
;
587 password_blob
= data_blob(password
, pwlen
+1);
589 conn
= make_connection(service
,password_blob
,dev
,req
->vuid
,&nt_status
);
592 data_blob_clear_free(&password_blob
);
595 reply_nterror(req
, nt_status
);
596 END_PROFILE(SMBtcon
);
600 reply_outbuf(req
, 2, 0);
601 SSVAL(req
->outbuf
,smb_vwv0
,max_recv
);
602 SSVAL(req
->outbuf
,smb_vwv1
,conn
->cnum
);
603 SSVAL(req
->outbuf
,smb_tid
,conn
->cnum
);
605 DEBUG(3,("tcon service=%s cnum=%d\n",
606 service
, conn
->cnum
));
608 END_PROFILE(SMBtcon
);
612 /****************************************************************************
613 Reply to a tcon and X.
614 conn POINTER CAN BE NULL HERE !
615 ****************************************************************************/
617 void reply_tcon_and_X(struct smb_request
*req
)
619 connection_struct
*conn
= req
->conn
;
620 char *service
= NULL
;
622 TALLOC_CTX
*ctx
= talloc_tos();
623 /* what the cleint thinks the device is */
624 char *client_devicetype
= NULL
;
625 /* what the server tells the client the share represents */
626 const char *server_devicetype
;
633 START_PROFILE(SMBtconX
);
636 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
637 END_PROFILE(SMBtconX
);
641 passlen
= SVAL(req
->inbuf
,smb_vwv3
);
642 tcon_flags
= SVAL(req
->inbuf
,smb_vwv2
);
644 /* we might have to close an old one */
645 if ((tcon_flags
& 0x1) && conn
) {
646 close_cnum(conn
,req
->vuid
);
651 if ((passlen
> MAX_PASS_LEN
) || (passlen
>= smb_buflen(req
->inbuf
))) {
652 reply_doserror(req
, ERRDOS
, ERRbuftoosmall
);
653 END_PROFILE(SMBtconX
);
657 if (global_encrypted_passwords_negotiated
) {
658 password
= data_blob_talloc(talloc_tos(), smb_buf(req
->inbuf
),
660 if (lp_security() == SEC_SHARE
) {
662 * Security = share always has a pad byte
663 * after the password.
665 p
= smb_buf(req
->inbuf
) + passlen
+ 1;
667 p
= smb_buf(req
->inbuf
) + passlen
;
670 password
= data_blob_talloc(talloc_tos(), smb_buf(req
->inbuf
),
672 /* Ensure correct termination */
673 password
.data
[passlen
]=0;
674 p
= smb_buf(req
->inbuf
) + passlen
+ 1;
677 p
+= srvstr_pull_buf_talloc(ctx
, req
->inbuf
, req
->flags2
, &path
, p
,
681 data_blob_clear_free(&password
);
682 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
683 END_PROFILE(SMBtconX
);
688 * the service name can be either: \\server\share
689 * or share directly like on the DELL PowerVault 705
692 q
= strchr_m(path
+2,'\\');
694 data_blob_clear_free(&password
);
695 reply_doserror(req
, ERRDOS
, ERRnosuchshare
);
696 END_PROFILE(SMBtconX
);
704 p
+= srvstr_pull_talloc(ctx
, req
->inbuf
, req
->flags2
,
705 &client_devicetype
, p
,
706 MIN(6,smb_bufrem(req
->inbuf
, p
)), STR_ASCII
);
708 if (client_devicetype
== NULL
) {
709 data_blob_clear_free(&password
);
710 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
711 END_PROFILE(SMBtconX
);
715 DEBUG(4,("Client requested device type [%s] for share [%s]\n", client_devicetype
, service
));
717 conn
= make_connection(service
, password
, client_devicetype
,
718 req
->vuid
, &nt_status
);
721 data_blob_clear_free(&password
);
724 reply_nterror(req
, nt_status
);
725 END_PROFILE(SMBtconX
);
730 server_devicetype
= "IPC";
731 else if ( IS_PRINT(conn
) )
732 server_devicetype
= "LPT1:";
734 server_devicetype
= "A:";
736 if (Protocol
< PROTOCOL_NT1
) {
737 reply_outbuf(req
, 2, 0);
738 if (message_push_string(&req
->outbuf
, server_devicetype
,
739 STR_TERMINATE
|STR_ASCII
) == -1) {
740 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
741 END_PROFILE(SMBtconX
);
745 /* NT sets the fstype of IPC$ to the null string */
746 const char *fstype
= IS_IPC(conn
) ? "" : lp_fstype(SNUM(conn
));
748 if (tcon_flags
& TCONX_FLAG_EXTENDED_RESPONSE
) {
749 /* Return permissions. */
753 reply_outbuf(req
, 7, 0);
756 perm1
= FILE_ALL_ACCESS
;
757 perm2
= FILE_ALL_ACCESS
;
759 perm1
= CAN_WRITE(conn
) ?
764 SIVAL(req
->outbuf
, smb_vwv3
, perm1
);
765 SIVAL(req
->outbuf
, smb_vwv5
, perm2
);
767 reply_outbuf(req
, 3, 0);
770 if ((message_push_string(&req
->outbuf
, server_devicetype
,
771 STR_TERMINATE
|STR_ASCII
) == -1)
772 || (message_push_string(&req
->outbuf
, fstype
,
773 STR_TERMINATE
) == -1)) {
774 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
775 END_PROFILE(SMBtconX
);
779 /* what does setting this bit do? It is set by NT4 and
780 may affect the ability to autorun mounted cdroms */
781 SSVAL(req
->outbuf
, smb_vwv2
, SMB_SUPPORT_SEARCH_BITS
|
782 (lp_csc_policy(SNUM(conn
)) << 2));
784 init_dfsroot(conn
, req
->inbuf
, req
->outbuf
);
788 DEBUG(3,("tconX service=%s \n",
791 /* set the incoming and outgoing tid to the just created one */
792 SSVAL(req
->inbuf
,smb_tid
,conn
->cnum
);
793 SSVAL(req
->outbuf
,smb_tid
,conn
->cnum
);
795 END_PROFILE(SMBtconX
);
801 /****************************************************************************
802 Reply to an unknown type.
803 ****************************************************************************/
805 void reply_unknown_new(struct smb_request
*req
, uint8 type
)
807 DEBUG(0, ("unknown command type (%s): type=%d (0x%X)\n",
808 smb_fn_name(type
), type
, type
));
809 reply_doserror(req
, ERRSRV
, ERRunknownsmb
);
813 /****************************************************************************
815 conn POINTER CAN BE NULL HERE !
816 ****************************************************************************/
818 void reply_ioctl(struct smb_request
*req
)
820 connection_struct
*conn
= req
->conn
;
827 START_PROFILE(SMBioctl
);
830 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
831 END_PROFILE(SMBioctl
);
835 device
= SVAL(req
->inbuf
,smb_vwv1
);
836 function
= SVAL(req
->inbuf
,smb_vwv2
);
837 ioctl_code
= (device
<< 16) + function
;
839 DEBUG(4, ("Received IOCTL (code 0x%x)\n", ioctl_code
));
841 switch (ioctl_code
) {
842 case IOCTL_QUERY_JOB_INFO
:
846 reply_doserror(req
, ERRSRV
, ERRnosupport
);
847 END_PROFILE(SMBioctl
);
851 reply_outbuf(req
, 8, replysize
+1);
852 SSVAL(req
->outbuf
,smb_vwv1
,replysize
); /* Total data bytes returned */
853 SSVAL(req
->outbuf
,smb_vwv5
,replysize
); /* Data bytes this buffer */
854 SSVAL(req
->outbuf
,smb_vwv6
,52); /* Offset to data */
855 p
= smb_buf(req
->outbuf
);
856 memset(p
, '\0', replysize
+1); /* valgrind-safe. */
857 p
+= 1; /* Allow for alignment */
859 switch (ioctl_code
) {
860 case IOCTL_QUERY_JOB_INFO
:
862 files_struct
*fsp
= file_fsp(SVAL(req
->inbuf
,
865 reply_doserror(req
, ERRDOS
, ERRbadfid
);
866 END_PROFILE(SMBioctl
);
869 SSVAL(p
,0,fsp
->rap_print_jobid
); /* Job number */
870 srvstr_push((char *)req
->outbuf
, req
->flags2
, p
+2,
872 STR_TERMINATE
|STR_ASCII
);
874 srvstr_push((char *)req
->outbuf
, req
->flags2
,
875 p
+18, lp_servicename(SNUM(conn
)),
876 13, STR_TERMINATE
|STR_ASCII
);
884 END_PROFILE(SMBioctl
);
888 /****************************************************************************
889 Strange checkpath NTSTATUS mapping.
890 ****************************************************************************/
892 static NTSTATUS
map_checkpath_error(const char *inbuf
, NTSTATUS status
)
894 /* Strange DOS error code semantics only for checkpath... */
895 if (!(SVAL(inbuf
,smb_flg2
) & FLAGS2_32_BIT_ERROR_CODES
)) {
896 if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_INVALID
,status
)) {
897 /* We need to map to ERRbadpath */
898 return NT_STATUS_OBJECT_PATH_NOT_FOUND
;
904 /****************************************************************************
905 Reply to a checkpath.
906 ****************************************************************************/
908 void reply_checkpath(struct smb_request
*req
)
910 connection_struct
*conn
= req
->conn
;
912 SMB_STRUCT_STAT sbuf
;
914 TALLOC_CTX
*ctx
= talloc_tos();
916 START_PROFILE(SMBcheckpath
);
918 srvstr_get_path(ctx
,(char *)req
->inbuf
, req
->flags2
, &name
,
919 smb_buf(req
->inbuf
) + 1, 0,
920 STR_TERMINATE
, &status
);
921 if (!NT_STATUS_IS_OK(status
)) {
922 status
= map_checkpath_error((char *)req
->inbuf
, status
);
923 reply_nterror(req
, status
);
924 END_PROFILE(SMBcheckpath
);
928 status
= resolve_dfspath(ctx
, conn
,
929 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
932 if (!NT_STATUS_IS_OK(status
)) {
933 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
934 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
936 END_PROFILE(SMBcheckpath
);
942 DEBUG(3,("reply_checkpath %s mode=%d\n", name
, (int)SVAL(req
->inbuf
,smb_vwv0
)));
944 status
= unix_convert(ctx
, conn
, name
, False
, &name
, NULL
, &sbuf
);
945 if (!NT_STATUS_IS_OK(status
)) {
949 status
= check_name(conn
, name
);
950 if (!NT_STATUS_IS_OK(status
)) {
951 DEBUG(3,("reply_checkpath: check_name of %s failed (%s)\n",name
,nt_errstr(status
)));
955 if (!VALID_STAT(sbuf
) && (SMB_VFS_STAT(conn
,name
,&sbuf
) != 0)) {
956 DEBUG(3,("reply_checkpath: stat of %s failed (%s)\n",name
,strerror(errno
)));
957 status
= map_nt_error_from_unix(errno
);
961 if (!S_ISDIR(sbuf
.st_mode
)) {
962 reply_botherror(req
, NT_STATUS_NOT_A_DIRECTORY
,
964 END_PROFILE(SMBcheckpath
);
968 reply_outbuf(req
, 0, 0);
970 END_PROFILE(SMBcheckpath
);
975 END_PROFILE(SMBcheckpath
);
977 /* We special case this - as when a Windows machine
978 is parsing a path is steps through the components
979 one at a time - if a component fails it expects
980 ERRbadpath, not ERRbadfile.
982 status
= map_checkpath_error((char *)req
->inbuf
, status
);
983 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
985 * Windows returns different error codes if
986 * the parent directory is valid but not the
987 * last component - it returns NT_STATUS_OBJECT_NAME_NOT_FOUND
988 * for that case and NT_STATUS_OBJECT_PATH_NOT_FOUND
989 * if the path is invalid.
991 reply_botherror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
996 reply_nterror(req
, status
);
999 /****************************************************************************
1001 ****************************************************************************/
1003 void reply_getatr(struct smb_request
*req
)
1005 connection_struct
*conn
= req
->conn
;
1007 SMB_STRUCT_STAT sbuf
;
1013 TALLOC_CTX
*ctx
= talloc_tos();
1015 START_PROFILE(SMBgetatr
);
1017 p
= smb_buf(req
->inbuf
) + 1;
1018 p
+= srvstr_get_path(ctx
, (char *)req
->inbuf
, req
->flags2
, &fname
, p
,
1019 0, STR_TERMINATE
, &status
);
1020 if (!NT_STATUS_IS_OK(status
)) {
1021 reply_nterror(req
, status
);
1022 END_PROFILE(SMBgetatr
);
1026 status
= resolve_dfspath(ctx
, conn
,
1027 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1030 if (!NT_STATUS_IS_OK(status
)) {
1031 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
1032 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
1033 ERRSRV
, ERRbadpath
);
1034 END_PROFILE(SMBgetatr
);
1037 reply_nterror(req
, status
);
1038 END_PROFILE(SMBgetatr
);
1042 /* dos smetimes asks for a stat of "" - it returns a "hidden directory"
1043 under WfWg - weird! */
1044 if (*fname
== '\0') {
1045 mode
= aHIDDEN
| aDIR
;
1046 if (!CAN_WRITE(conn
)) {
1052 status
= unix_convert(ctx
, conn
, fname
, False
, &fname
, NULL
,&sbuf
);
1053 if (!NT_STATUS_IS_OK(status
)) {
1054 reply_nterror(req
, status
);
1055 END_PROFILE(SMBgetatr
);
1058 status
= check_name(conn
, fname
);
1059 if (!NT_STATUS_IS_OK(status
)) {
1060 DEBUG(3,("reply_getatr: check_name of %s failed (%s)\n",fname
,nt_errstr(status
)));
1061 reply_nterror(req
, status
);
1062 END_PROFILE(SMBgetatr
);
1065 if (!VALID_STAT(sbuf
) && (SMB_VFS_STAT(conn
,fname
,&sbuf
) != 0)) {
1066 DEBUG(3,("reply_getatr: stat of %s failed (%s)\n",fname
,strerror(errno
)));
1067 reply_unixerror(req
, ERRDOS
,ERRbadfile
);
1068 END_PROFILE(SMBgetatr
);
1072 mode
= dos_mode(conn
,fname
,&sbuf
);
1073 size
= sbuf
.st_size
;
1074 mtime
= sbuf
.st_mtime
;
1080 reply_outbuf(req
, 10, 0);
1082 SSVAL(req
->outbuf
,smb_vwv0
,mode
);
1083 if(lp_dos_filetime_resolution(SNUM(conn
)) ) {
1084 srv_put_dos_date3((char *)req
->outbuf
,smb_vwv1
,mtime
& ~1);
1086 srv_put_dos_date3((char *)req
->outbuf
,smb_vwv1
,mtime
);
1088 SIVAL(req
->outbuf
,smb_vwv3
,(uint32
)size
);
1090 if (Protocol
>= PROTOCOL_NT1
) {
1091 SSVAL(req
->outbuf
, smb_flg2
,
1092 SVAL(req
->outbuf
, smb_flg2
) | FLAGS2_IS_LONG_NAME
);
1095 DEBUG(3,("reply_getatr: name=%s mode=%d size=%u\n", fname
, mode
, (unsigned int)size
) );
1097 END_PROFILE(SMBgetatr
);
1101 /****************************************************************************
1103 ****************************************************************************/
1105 void reply_setatr(struct smb_request
*req
)
1107 struct timespec ts
[2];
1108 connection_struct
*conn
= req
->conn
;
1112 SMB_STRUCT_STAT sbuf
;
1115 TALLOC_CTX
*ctx
= talloc_tos();
1117 START_PROFILE(SMBsetatr
);
1122 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1126 p
= smb_buf(req
->inbuf
) + 1;
1127 p
+= srvstr_get_path(ctx
, (char *)req
->inbuf
, req
->flags2
, &fname
, p
,
1128 0, STR_TERMINATE
, &status
);
1129 if (!NT_STATUS_IS_OK(status
)) {
1130 reply_nterror(req
, status
);
1131 END_PROFILE(SMBsetatr
);
1135 status
= resolve_dfspath(ctx
, conn
,
1136 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1139 if (!NT_STATUS_IS_OK(status
)) {
1140 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
1141 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
1142 ERRSRV
, ERRbadpath
);
1143 END_PROFILE(SMBsetatr
);
1146 reply_nterror(req
, status
);
1147 END_PROFILE(SMBsetatr
);
1151 status
= unix_convert(ctx
, conn
, fname
, False
, &fname
, NULL
, &sbuf
);
1152 if (!NT_STATUS_IS_OK(status
)) {
1153 reply_nterror(req
, status
);
1154 END_PROFILE(SMBsetatr
);
1158 status
= check_name(conn
, fname
);
1159 if (!NT_STATUS_IS_OK(status
)) {
1160 reply_nterror(req
, status
);
1161 END_PROFILE(SMBsetatr
);
1165 if (fname
[0] == '.' && fname
[1] == '\0') {
1167 * Not sure here is the right place to catch this
1168 * condition. Might be moved to somewhere else later -- vl
1170 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
1171 END_PROFILE(SMBsetatr
);
1175 mode
= SVAL(req
->inbuf
,smb_vwv0
);
1176 mtime
= srv_make_unix_date3(req
->inbuf
+smb_vwv1
);
1178 ts
[1] = convert_time_t_to_timespec(mtime
);
1179 status
= smb_set_file_time(conn
, NULL
, fname
,
1181 if (!NT_STATUS_IS_OK(status
)) {
1182 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
1183 END_PROFILE(SMBsetatr
);
1187 if (mode
!= FILE_ATTRIBUTE_NORMAL
) {
1188 if (VALID_STAT_OF_DIR(sbuf
))
1193 if (file_set_dosmode(conn
,fname
,mode
,&sbuf
,NULL
,false) != 0) {
1194 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
1195 END_PROFILE(SMBsetatr
);
1200 reply_outbuf(req
, 0, 0);
1202 DEBUG( 3, ( "setatr name=%s mode=%d\n", fname
, mode
) );
1204 END_PROFILE(SMBsetatr
);
1208 /****************************************************************************
1210 ****************************************************************************/
1212 void reply_dskattr(struct smb_request
*req
)
1214 connection_struct
*conn
= req
->conn
;
1215 SMB_BIG_UINT dfree
,dsize
,bsize
;
1216 START_PROFILE(SMBdskattr
);
1218 if (get_dfree_info(conn
,".",True
,&bsize
,&dfree
,&dsize
) == (SMB_BIG_UINT
)-1) {
1219 reply_unixerror(req
, ERRHRD
, ERRgeneral
);
1220 END_PROFILE(SMBdskattr
);
1224 reply_outbuf(req
, 5, 0);
1226 if (Protocol
<= PROTOCOL_LANMAN2
) {
1227 double total_space
, free_space
;
1228 /* we need to scale this to a number that DOS6 can handle. We
1229 use floating point so we can handle large drives on systems
1230 that don't have 64 bit integers
1232 we end up displaying a maximum of 2G to DOS systems
1234 total_space
= dsize
* (double)bsize
;
1235 free_space
= dfree
* (double)bsize
;
1237 dsize
= (SMB_BIG_UINT
)((total_space
+63*512) / (64*512));
1238 dfree
= (SMB_BIG_UINT
)((free_space
+63*512) / (64*512));
1240 if (dsize
> 0xFFFF) dsize
= 0xFFFF;
1241 if (dfree
> 0xFFFF) dfree
= 0xFFFF;
1243 SSVAL(req
->outbuf
,smb_vwv0
,dsize
);
1244 SSVAL(req
->outbuf
,smb_vwv1
,64); /* this must be 64 for dos systems */
1245 SSVAL(req
->outbuf
,smb_vwv2
,512); /* and this must be 512 */
1246 SSVAL(req
->outbuf
,smb_vwv3
,dfree
);
1248 SSVAL(req
->outbuf
,smb_vwv0
,dsize
);
1249 SSVAL(req
->outbuf
,smb_vwv1
,bsize
/512);
1250 SSVAL(req
->outbuf
,smb_vwv2
,512);
1251 SSVAL(req
->outbuf
,smb_vwv3
,dfree
);
1254 DEBUG(3,("dskattr dfree=%d\n", (unsigned int)dfree
));
1256 END_PROFILE(SMBdskattr
);
1260 /****************************************************************************
1262 Can be called from SMBsearch, SMBffirst or SMBfunique.
1263 ****************************************************************************/
1265 void reply_search(struct smb_request
*req
)
1267 connection_struct
*conn
= req
->conn
;
1269 char *directory
= NULL
;
1275 unsigned int numentries
= 0;
1276 unsigned int maxentries
= 0;
1277 bool finished
= False
;
1283 bool check_descend
= False
;
1284 bool expect_close
= False
;
1286 bool mask_contains_wcard
= False
;
1287 bool allow_long_path_components
= (req
->flags2
& FLAGS2_LONG_PATH_COMPONENTS
) ? True
: False
;
1288 TALLOC_CTX
*ctx
= talloc_tos();
1289 bool ask_sharemode
= lp_parm_bool(SNUM(conn
), "smbd", "search ask sharemode", true);
1291 START_PROFILE(SMBsearch
);
1294 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1295 END_PROFILE(SMBsearch
);
1299 if (lp_posix_pathnames()) {
1300 reply_unknown_new(req
, CVAL(req
->inbuf
, smb_com
));
1301 END_PROFILE(SMBsearch
);
1305 /* If we were called as SMBffirst then we must expect close. */
1306 if(CVAL(req
->inbuf
,smb_com
) == SMBffirst
) {
1307 expect_close
= True
;
1310 reply_outbuf(req
, 1, 3);
1311 maxentries
= SVAL(req
->inbuf
,smb_vwv0
);
1312 dirtype
= SVAL(req
->inbuf
,smb_vwv1
);
1313 p
= smb_buf(req
->inbuf
) + 1;
1314 p
+= srvstr_get_path_wcard(ctx
,
1322 &mask_contains_wcard
);
1323 if (!NT_STATUS_IS_OK(nt_status
)) {
1324 reply_nterror(req
, nt_status
);
1325 END_PROFILE(SMBsearch
);
1329 nt_status
= resolve_dfspath_wcard(ctx
, conn
,
1330 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1333 &mask_contains_wcard
);
1334 if (!NT_STATUS_IS_OK(nt_status
)) {
1335 if (NT_STATUS_EQUAL(nt_status
,NT_STATUS_PATH_NOT_COVERED
)) {
1336 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
1337 ERRSRV
, ERRbadpath
);
1338 END_PROFILE(SMBsearch
);
1341 reply_nterror(req
, nt_status
);
1342 END_PROFILE(SMBsearch
);
1347 status_len
= SVAL(p
, 0);
1350 /* dirtype &= ~aDIR; */
1352 if (status_len
== 0) {
1353 SMB_STRUCT_STAT sbuf
;
1355 nt_status
= unix_convert(ctx
, conn
, path
, True
,
1356 &directory
, NULL
, &sbuf
);
1357 if (!NT_STATUS_IS_OK(nt_status
)) {
1358 reply_nterror(req
, nt_status
);
1359 END_PROFILE(SMBsearch
);
1363 nt_status
= check_name(conn
, directory
);
1364 if (!NT_STATUS_IS_OK(nt_status
)) {
1365 reply_nterror(req
, nt_status
);
1366 END_PROFILE(SMBsearch
);
1370 p
= strrchr_m(directory
,'/');
1373 directory
= talloc_strdup(ctx
,".");
1375 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1376 END_PROFILE(SMBsearch
);
1384 if (*directory
== '\0') {
1385 directory
= talloc_strdup(ctx
,".");
1387 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1388 END_PROFILE(SMBsearch
);
1392 memset((char *)status
,'\0',21);
1393 SCVAL(status
,0,(dirtype
& 0x1F));
1395 nt_status
= dptr_create(conn
,
1401 mask_contains_wcard
,
1404 if (!NT_STATUS_IS_OK(nt_status
)) {
1405 reply_nterror(req
, nt_status
);
1406 END_PROFILE(SMBsearch
);
1409 dptr_num
= dptr_dnum(conn
->dirptr
);
1413 memcpy(status
,p
,21);
1414 status_dirtype
= CVAL(status
,0) & 0x1F;
1415 if (status_dirtype
!= (dirtype
& 0x1F)) {
1416 dirtype
= status_dirtype
;
1419 conn
->dirptr
= dptr_fetch(status
+12,&dptr_num
);
1420 if (!conn
->dirptr
) {
1423 string_set(&conn
->dirpath
,dptr_path(dptr_num
));
1424 mask
= dptr_wcard(dptr_num
);
1429 * For a 'continue' search we have no string. So
1430 * check from the initial saved string.
1432 mask_contains_wcard
= ms_has_wild(mask
);
1433 dirtype
= dptr_attr(dptr_num
);
1436 DEBUG(4,("dptr_num is %d\n",dptr_num
));
1438 if ((dirtype
&0x1F) == aVOLID
) {
1439 char buf
[DIR_STRUCT_SIZE
];
1440 memcpy(buf
,status
,21);
1441 if (!make_dir_struct(ctx
,buf
,"???????????",volume_label(SNUM(conn
)),
1442 0,aVOLID
,0,!allow_long_path_components
)) {
1443 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1444 END_PROFILE(SMBsearch
);
1447 dptr_fill(buf
+12,dptr_num
);
1448 if (dptr_zero(buf
+12) && (status_len
==0)) {
1453 if (message_push_blob(&req
->outbuf
,
1454 data_blob_const(buf
, sizeof(buf
)))
1456 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1457 END_PROFILE(SMBsearch
);
1465 ((uint8
*)smb_buf(req
->outbuf
) + 3 - req
->outbuf
))
1468 DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
1469 conn
->dirpath
,lp_dontdescend(SNUM(conn
))));
1470 if (in_list(conn
->dirpath
, lp_dontdescend(SNUM(conn
)),True
)) {
1471 check_descend
= True
;
1474 for (i
=numentries
;(i
<maxentries
) && !finished
;i
++) {
1475 finished
= !get_dir_entry(ctx
,
1486 char buf
[DIR_STRUCT_SIZE
];
1487 memcpy(buf
,status
,21);
1488 if (!make_dir_struct(ctx
,
1495 !allow_long_path_components
)) {
1496 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1497 END_PROFILE(SMBsearch
);
1500 if (!dptr_fill(buf
+12,dptr_num
)) {
1503 if (message_push_blob(&req
->outbuf
,
1504 data_blob_const(buf
, sizeof(buf
)))
1506 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1507 END_PROFILE(SMBsearch
);
1517 /* If we were called as SMBffirst with smb_search_id == NULL
1518 and no entries were found then return error and close dirptr
1521 if (numentries
== 0) {
1522 dptr_close(&dptr_num
);
1523 } else if(expect_close
&& status_len
== 0) {
1524 /* Close the dptr - we know it's gone */
1525 dptr_close(&dptr_num
);
1528 /* If we were called as SMBfunique, then we can close the dirptr now ! */
1529 if(dptr_num
>= 0 && CVAL(req
->inbuf
,smb_com
) == SMBfunique
) {
1530 dptr_close(&dptr_num
);
1533 if ((numentries
== 0) && !mask_contains_wcard
) {
1534 reply_botherror(req
, STATUS_NO_MORE_FILES
, ERRDOS
, ERRnofiles
);
1535 END_PROFILE(SMBsearch
);
1539 SSVAL(req
->outbuf
,smb_vwv0
,numentries
);
1540 SSVAL(req
->outbuf
,smb_vwv1
,3 + numentries
* DIR_STRUCT_SIZE
);
1541 SCVAL(smb_buf(req
->outbuf
),0,5);
1542 SSVAL(smb_buf(req
->outbuf
),1,numentries
*DIR_STRUCT_SIZE
);
1544 /* The replies here are never long name. */
1545 SSVAL(req
->outbuf
, smb_flg2
,
1546 SVAL(req
->outbuf
, smb_flg2
) & (~FLAGS2_IS_LONG_NAME
));
1547 if (!allow_long_path_components
) {
1548 SSVAL(req
->outbuf
, smb_flg2
,
1549 SVAL(req
->outbuf
, smb_flg2
)
1550 & (~FLAGS2_LONG_PATH_COMPONENTS
));
1553 /* This SMB *always* returns ASCII names. Remove the unicode bit in flags2. */
1554 SSVAL(req
->outbuf
, smb_flg2
,
1555 (SVAL(req
->outbuf
, smb_flg2
) & (~FLAGS2_UNICODE_STRINGS
)));
1558 directory
= dptr_path(dptr_num
);
1561 DEBUG(4,("%s mask=%s path=%s dtype=%d nument=%u of %u\n",
1562 smb_fn_name(CVAL(req
->inbuf
,smb_com
)),
1564 directory
? directory
: "./",
1569 END_PROFILE(SMBsearch
);
1573 /****************************************************************************
1574 Reply to a fclose (stop directory search).
1575 ****************************************************************************/
1577 void reply_fclose(struct smb_request
*req
)
1585 bool path_contains_wcard
= False
;
1586 TALLOC_CTX
*ctx
= talloc_tos();
1588 START_PROFILE(SMBfclose
);
1590 if (lp_posix_pathnames()) {
1591 reply_unknown_new(req
, CVAL(req
->inbuf
, smb_com
));
1592 END_PROFILE(SMBfclose
);
1596 p
= smb_buf(req
->inbuf
) + 1;
1597 p
+= srvstr_get_path_wcard(ctx
,
1605 &path_contains_wcard
);
1606 if (!NT_STATUS_IS_OK(err
)) {
1607 reply_nterror(req
, err
);
1608 END_PROFILE(SMBfclose
);
1612 status_len
= SVAL(p
,0);
1615 if (status_len
== 0) {
1616 reply_doserror(req
, ERRSRV
, ERRsrverror
);
1617 END_PROFILE(SMBfclose
);
1621 memcpy(status
,p
,21);
1623 if(dptr_fetch(status
+12,&dptr_num
)) {
1624 /* Close the dptr - we know it's gone */
1625 dptr_close(&dptr_num
);
1628 reply_outbuf(req
, 1, 0);
1629 SSVAL(req
->outbuf
,smb_vwv0
,0);
1631 DEBUG(3,("search close\n"));
1633 END_PROFILE(SMBfclose
);
1637 /****************************************************************************
1639 ****************************************************************************/
1641 void reply_open(struct smb_request
*req
)
1643 connection_struct
*conn
= req
->conn
;
1649 SMB_STRUCT_STAT sbuf
;
1656 uint32 create_disposition
;
1657 uint32 create_options
= 0;
1659 TALLOC_CTX
*ctx
= talloc_tos();
1661 START_PROFILE(SMBopen
);
1664 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1665 END_PROFILE(SMBopen
);
1669 oplock_request
= CORE_OPLOCK_REQUEST(req
->inbuf
);
1670 deny_mode
= SVAL(req
->inbuf
,smb_vwv0
);
1671 dos_attr
= SVAL(req
->inbuf
,smb_vwv1
);
1673 srvstr_get_path(ctx
, (char *)req
->inbuf
, req
->flags2
, &fname
,
1674 smb_buf(req
->inbuf
)+1, 0,
1675 STR_TERMINATE
, &status
);
1676 if (!NT_STATUS_IS_OK(status
)) {
1677 reply_nterror(req
, status
);
1678 END_PROFILE(SMBopen
);
1682 if (!map_open_params_to_ntcreate(
1683 fname
, deny_mode
, OPENX_FILE_EXISTS_OPEN
, &access_mask
,
1684 &share_mode
, &create_disposition
, &create_options
)) {
1685 reply_nterror(req
, NT_STATUS_DOS(ERRDOS
, ERRbadaccess
));
1686 END_PROFILE(SMBopen
);
1690 status
= create_file(conn
, /* conn */
1692 0, /* root_dir_fid */
1694 access_mask
, /* access_mask */
1695 share_mode
, /* share_access */
1696 create_disposition
, /* create_disposition*/
1697 create_options
, /* create_options */
1698 dos_attr
, /* file_attributes */
1699 oplock_request
, /* oplock_request */
1700 0, /* allocation_size */
1707 if (!NT_STATUS_IS_OK(status
)) {
1708 if (open_was_deferred(req
->mid
)) {
1709 /* We have re-scheduled this call. */
1710 END_PROFILE(SMBopen
);
1713 reply_openerror(req
, status
);
1714 END_PROFILE(SMBopen
);
1718 size
= sbuf
.st_size
;
1719 fattr
= dos_mode(conn
,fsp
->fsp_name
,&sbuf
);
1720 mtime
= sbuf
.st_mtime
;
1723 DEBUG(3,("attempt to open a directory %s\n",fsp
->fsp_name
));
1724 close_file(fsp
,ERROR_CLOSE
);
1725 reply_doserror(req
, ERRDOS
,ERRnoaccess
);
1726 END_PROFILE(SMBopen
);
1730 reply_outbuf(req
, 7, 0);
1731 SSVAL(req
->outbuf
,smb_vwv0
,fsp
->fnum
);
1732 SSVAL(req
->outbuf
,smb_vwv1
,fattr
);
1733 if(lp_dos_filetime_resolution(SNUM(conn
)) ) {
1734 srv_put_dos_date3((char *)req
->outbuf
,smb_vwv2
,mtime
& ~1);
1736 srv_put_dos_date3((char *)req
->outbuf
,smb_vwv2
,mtime
);
1738 SIVAL(req
->outbuf
,smb_vwv4
,(uint32
)size
);
1739 SSVAL(req
->outbuf
,smb_vwv6
,deny_mode
);
1741 if (oplock_request
&& lp_fake_oplocks(SNUM(conn
))) {
1742 SCVAL(req
->outbuf
,smb_flg
,
1743 CVAL(req
->outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
1746 if(EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1747 SCVAL(req
->outbuf
,smb_flg
,
1748 CVAL(req
->outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
1750 END_PROFILE(SMBopen
);
1754 /****************************************************************************
1755 Reply to an open and X.
1756 ****************************************************************************/
1758 void reply_open_and_X(struct smb_request
*req
)
1760 connection_struct
*conn
= req
->conn
;
1765 /* Breakout the oplock request bits so we can set the
1766 reply bits separately. */
1767 int ex_oplock_request
;
1768 int core_oplock_request
;
1771 int smb_sattr
= SVAL(req
->inbuf
,smb_vwv4
);
1772 uint32 smb_time
= make_unix_date3(req
->inbuf
+smb_vwv6
);
1777 SMB_STRUCT_STAT sbuf
;
1781 SMB_BIG_UINT allocation_size
;
1782 ssize_t retval
= -1;
1785 uint32 create_disposition
;
1786 uint32 create_options
= 0;
1787 TALLOC_CTX
*ctx
= talloc_tos();
1789 START_PROFILE(SMBopenX
);
1791 if (req
->wct
< 15) {
1792 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1793 END_PROFILE(SMBopenX
);
1797 open_flags
= SVAL(req
->inbuf
,smb_vwv2
);
1798 deny_mode
= SVAL(req
->inbuf
,smb_vwv3
);
1799 smb_attr
= SVAL(req
->inbuf
,smb_vwv5
);
1800 ex_oplock_request
= EXTENDED_OPLOCK_REQUEST(req
->inbuf
);
1801 core_oplock_request
= CORE_OPLOCK_REQUEST(req
->inbuf
);
1802 oplock_request
= ex_oplock_request
| core_oplock_request
;
1803 smb_ofun
= SVAL(req
->inbuf
,smb_vwv8
);
1804 allocation_size
= (SMB_BIG_UINT
)IVAL(req
->inbuf
,smb_vwv9
);
1806 /* If it's an IPC, pass off the pipe handler. */
1808 if (lp_nt_pipe_support()) {
1809 reply_open_pipe_and_X(conn
, req
);
1811 reply_doserror(req
, ERRSRV
, ERRaccess
);
1813 END_PROFILE(SMBopenX
);
1817 /* XXXX we need to handle passed times, sattr and flags */
1818 srvstr_get_path(ctx
, (char *)req
->inbuf
, req
->flags2
, &fname
,
1819 smb_buf(req
->inbuf
), 0, STR_TERMINATE
,
1821 if (!NT_STATUS_IS_OK(status
)) {
1822 reply_nterror(req
, status
);
1823 END_PROFILE(SMBopenX
);
1827 if (!map_open_params_to_ntcreate(
1828 fname
, deny_mode
, smb_ofun
, &access_mask
,
1829 &share_mode
, &create_disposition
, &create_options
)) {
1830 reply_nterror(req
, NT_STATUS_DOS(ERRDOS
, ERRbadaccess
));
1831 END_PROFILE(SMBopenX
);
1835 status
= create_file(conn
, /* conn */
1837 0, /* root_dir_fid */
1839 access_mask
, /* access_mask */
1840 share_mode
, /* share_access */
1841 create_disposition
, /* create_disposition*/
1842 create_options
, /* create_options */
1843 smb_attr
, /* file_attributes */
1844 oplock_request
, /* oplock_request */
1845 0, /* allocation_size */
1849 &smb_action
, /* pinfo */
1852 if (!NT_STATUS_IS_OK(status
)) {
1853 END_PROFILE(SMBopenX
);
1854 if (open_was_deferred(req
->mid
)) {
1855 /* We have re-scheduled this call. */
1858 reply_openerror(req
, status
);
1862 /* Setting the "size" field in vwv9 and vwv10 causes the file to be set to this size,
1863 if the file is truncated or created. */
1864 if (((smb_action
== FILE_WAS_CREATED
) || (smb_action
== FILE_WAS_OVERWRITTEN
)) && allocation_size
) {
1865 fsp
->initial_allocation_size
= smb_roundup(fsp
->conn
, allocation_size
);
1866 if (vfs_allocate_file_space(fsp
, fsp
->initial_allocation_size
) == -1) {
1867 close_file(fsp
,ERROR_CLOSE
);
1868 reply_nterror(req
, NT_STATUS_DISK_FULL
);
1869 END_PROFILE(SMBopenX
);
1872 retval
= vfs_set_filelen(fsp
, (SMB_OFF_T
)allocation_size
);
1874 close_file(fsp
,ERROR_CLOSE
);
1875 reply_nterror(req
, NT_STATUS_DISK_FULL
);
1876 END_PROFILE(SMBopenX
);
1879 sbuf
.st_size
= get_allocation_size(conn
,fsp
,&sbuf
);
1882 fattr
= dos_mode(conn
,fsp
->fsp_name
,&sbuf
);
1883 mtime
= sbuf
.st_mtime
;
1885 close_file(fsp
,ERROR_CLOSE
);
1886 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
1887 END_PROFILE(SMBopenX
);
1891 /* If the caller set the extended oplock request bit
1892 and we granted one (by whatever means) - set the
1893 correct bit for extended oplock reply.
1896 if (ex_oplock_request
&& lp_fake_oplocks(SNUM(conn
))) {
1897 smb_action
|= EXTENDED_OPLOCK_GRANTED
;
1900 if(ex_oplock_request
&& EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1901 smb_action
|= EXTENDED_OPLOCK_GRANTED
;
1904 /* If the caller set the core oplock request bit
1905 and we granted one (by whatever means) - set the
1906 correct bit for core oplock reply.
1909 if (open_flags
& EXTENDED_RESPONSE_REQUIRED
) {
1910 reply_outbuf(req
, 19, 0);
1912 reply_outbuf(req
, 15, 0);
1915 if (core_oplock_request
&& lp_fake_oplocks(SNUM(conn
))) {
1916 SCVAL(req
->outbuf
, smb_flg
,
1917 CVAL(req
->outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
1920 if(core_oplock_request
&& EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1921 SCVAL(req
->outbuf
, smb_flg
,
1922 CVAL(req
->outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
1925 SSVAL(req
->outbuf
,smb_vwv2
,fsp
->fnum
);
1926 SSVAL(req
->outbuf
,smb_vwv3
,fattr
);
1927 if(lp_dos_filetime_resolution(SNUM(conn
)) ) {
1928 srv_put_dos_date3((char *)req
->outbuf
,smb_vwv4
,mtime
& ~1);
1930 srv_put_dos_date3((char *)req
->outbuf
,smb_vwv4
,mtime
);
1932 SIVAL(req
->outbuf
,smb_vwv6
,(uint32
)sbuf
.st_size
);
1933 SSVAL(req
->outbuf
,smb_vwv8
,GET_OPENX_MODE(deny_mode
));
1934 SSVAL(req
->outbuf
,smb_vwv11
,smb_action
);
1936 if (open_flags
& EXTENDED_RESPONSE_REQUIRED
) {
1937 SIVAL(req
->outbuf
, smb_vwv15
, STD_RIGHT_ALL_ACCESS
);
1940 END_PROFILE(SMBopenX
);
1945 /****************************************************************************
1946 Reply to a SMBulogoffX.
1947 ****************************************************************************/
1949 void reply_ulogoffX(struct smb_request
*req
)
1953 START_PROFILE(SMBulogoffX
);
1955 vuser
= get_valid_user_struct(req
->vuid
);
1958 DEBUG(3,("ulogoff, vuser id %d does not map to user.\n",
1962 /* in user level security we are supposed to close any files
1963 open by this user */
1964 if ((vuser
!= NULL
) && (lp_security() != SEC_SHARE
)) {
1965 file_close_user(req
->vuid
);
1968 invalidate_vuid(req
->vuid
);
1970 reply_outbuf(req
, 2, 0);
1972 DEBUG( 3, ( "ulogoffX vuid=%d\n", req
->vuid
) );
1974 END_PROFILE(SMBulogoffX
);
1978 /****************************************************************************
1979 Reply to a mknew or a create.
1980 ****************************************************************************/
1982 void reply_mknew(struct smb_request
*req
)
1984 connection_struct
*conn
= req
->conn
;
1988 struct timespec ts
[2];
1990 int oplock_request
= 0;
1991 SMB_STRUCT_STAT sbuf
;
1993 uint32 access_mask
= FILE_GENERIC_READ
| FILE_GENERIC_WRITE
;
1994 uint32 share_mode
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
1995 uint32 create_disposition
;
1996 uint32 create_options
= 0;
1997 TALLOC_CTX
*ctx
= talloc_tos();
1999 START_PROFILE(SMBcreate
);
2002 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2003 END_PROFILE(SMBcreate
);
2007 fattr
= SVAL(req
->inbuf
,smb_vwv0
);
2008 oplock_request
= CORE_OPLOCK_REQUEST(req
->inbuf
);
2009 com
= SVAL(req
->inbuf
,smb_com
);
2011 ts
[1] =convert_time_t_to_timespec(
2012 srv_make_unix_date3(req
->inbuf
+ smb_vwv1
));
2015 srvstr_get_path(ctx
, (char *)req
->inbuf
, req
->flags2
, &fname
,
2016 smb_buf(req
->inbuf
) + 1, 0,
2017 STR_TERMINATE
, &status
);
2018 if (!NT_STATUS_IS_OK(status
)) {
2019 reply_nterror(req
, status
);
2020 END_PROFILE(SMBcreate
);
2024 if (fattr
& aVOLID
) {
2025 DEBUG(0,("Attempt to create file (%s) with volid set - "
2026 "please report this\n", fname
));
2029 if(com
== SMBmknew
) {
2030 /* We should fail if file exists. */
2031 create_disposition
= FILE_CREATE
;
2033 /* Create if file doesn't exist, truncate if it does. */
2034 create_disposition
= FILE_OVERWRITE_IF
;
2037 status
= create_file(conn
, /* conn */
2039 0, /* root_dir_fid */
2041 access_mask
, /* access_mask */
2042 share_mode
, /* share_access */
2043 create_disposition
, /* create_disposition*/
2044 create_options
, /* create_options */
2045 fattr
, /* file_attributes */
2046 oplock_request
, /* oplock_request */
2047 0, /* allocation_size */
2054 if (!NT_STATUS_IS_OK(status
)) {
2055 END_PROFILE(SMBcreate
);
2056 if (open_was_deferred(req
->mid
)) {
2057 /* We have re-scheduled this call. */
2060 reply_openerror(req
, status
);
2064 ts
[0] = get_atimespec(&sbuf
); /* atime. */
2065 status
= smb_set_file_time(conn
, fsp
, fsp
->fsp_name
, &sbuf
, ts
, true);
2066 if (!NT_STATUS_IS_OK(status
)) {
2067 END_PROFILE(SMBcreate
);
2068 reply_openerror(req
, status
);
2072 reply_outbuf(req
, 1, 0);
2073 SSVAL(req
->outbuf
,smb_vwv0
,fsp
->fnum
);
2075 if (oplock_request
&& lp_fake_oplocks(SNUM(conn
))) {
2076 SCVAL(req
->outbuf
,smb_flg
,
2077 CVAL(req
->outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
2080 if(EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
2081 SCVAL(req
->outbuf
,smb_flg
,
2082 CVAL(req
->outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
2085 DEBUG( 2, ( "reply_mknew: file %s\n", fsp
->fsp_name
) );
2086 DEBUG( 3, ( "reply_mknew %s fd=%d dmode=0x%x\n",
2087 fsp
->fsp_name
, fsp
->fh
->fd
, (unsigned int)fattr
) );
2089 END_PROFILE(SMBcreate
);
2093 /****************************************************************************
2094 Reply to a create temporary file.
2095 ****************************************************************************/
2097 void reply_ctemp(struct smb_request
*req
)
2099 connection_struct
*conn
= req
->conn
;
2105 SMB_STRUCT_STAT sbuf
;
2108 TALLOC_CTX
*ctx
= talloc_tos();
2110 START_PROFILE(SMBctemp
);
2113 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2114 END_PROFILE(SMBctemp
);
2118 fattr
= SVAL(req
->inbuf
,smb_vwv0
);
2119 oplock_request
= CORE_OPLOCK_REQUEST(req
->inbuf
);
2121 srvstr_get_path(ctx
, (char *)req
->inbuf
, req
->flags2
, &fname
,
2122 smb_buf(req
->inbuf
)+1, 0, STR_TERMINATE
,
2124 if (!NT_STATUS_IS_OK(status
)) {
2125 reply_nterror(req
, status
);
2126 END_PROFILE(SMBctemp
);
2130 fname
= talloc_asprintf(ctx
,
2134 fname
= talloc_strdup(ctx
, "TMXXXXXX");
2138 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2139 END_PROFILE(SMBctemp
);
2143 status
= resolve_dfspath(ctx
, conn
,
2144 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
2147 if (!NT_STATUS_IS_OK(status
)) {
2148 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
2149 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
2150 ERRSRV
, ERRbadpath
);
2151 END_PROFILE(SMBctemp
);
2154 reply_nterror(req
, status
);
2155 END_PROFILE(SMBctemp
);
2159 status
= unix_convert(ctx
, conn
, fname
, False
, &fname
, NULL
, &sbuf
);
2160 if (!NT_STATUS_IS_OK(status
)) {
2161 reply_nterror(req
, status
);
2162 END_PROFILE(SMBctemp
);
2166 status
= check_name(conn
, fname
);
2167 if (!NT_STATUS_IS_OK(status
)) {
2168 reply_nterror(req
, status
);
2169 END_PROFILE(SMBctemp
);
2173 tmpfd
= smb_mkstemp(fname
);
2175 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
2176 END_PROFILE(SMBctemp
);
2180 SMB_VFS_STAT(conn
,fname
,&sbuf
);
2182 /* We should fail if file does not exist. */
2183 status
= open_file_ntcreate(conn
, req
, fname
, &sbuf
,
2184 FILE_GENERIC_READ
| FILE_GENERIC_WRITE
,
2185 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2192 /* close fd from smb_mkstemp() */
2195 if (!NT_STATUS_IS_OK(status
)) {
2196 if (open_was_deferred(req
->mid
)) {
2197 /* We have re-scheduled this call. */
2198 END_PROFILE(SMBctemp
);
2201 reply_openerror(req
, status
);
2202 END_PROFILE(SMBctemp
);
2206 reply_outbuf(req
, 1, 0);
2207 SSVAL(req
->outbuf
,smb_vwv0
,fsp
->fnum
);
2209 /* the returned filename is relative to the directory */
2210 s
= strrchr_m(fsp
->fsp_name
, '/');
2218 /* Tested vs W2K3 - this doesn't seem to be here - null terminated filename is the only
2219 thing in the byte section. JRA */
2220 SSVALS(p
, 0, -1); /* what is this? not in spec */
2222 if (message_push_string(&req
->outbuf
, s
, STR_ASCII
|STR_TERMINATE
)
2224 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2225 END_PROFILE(SMBctemp
);
2229 if (oplock_request
&& lp_fake_oplocks(SNUM(conn
))) {
2230 SCVAL(req
->outbuf
, smb_flg
,
2231 CVAL(req
->outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
2234 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
2235 SCVAL(req
->outbuf
, smb_flg
,
2236 CVAL(req
->outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
2239 DEBUG( 2, ( "reply_ctemp: created temp file %s\n", fsp
->fsp_name
) );
2240 DEBUG( 3, ( "reply_ctemp %s fd=%d umode=0%o\n", fsp
->fsp_name
,
2241 fsp
->fh
->fd
, (unsigned int)sbuf
.st_mode
) );
2243 END_PROFILE(SMBctemp
);
2247 /*******************************************************************
2248 Check if a user is allowed to rename a file.
2249 ********************************************************************/
2251 static NTSTATUS
can_rename(connection_struct
*conn
, files_struct
*fsp
,
2252 uint16 dirtype
, SMB_STRUCT_STAT
*pst
)
2256 if (!CAN_WRITE(conn
)) {
2257 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
2260 fmode
= dos_mode(conn
, fsp
->fsp_name
, pst
);
2261 if ((fmode
& ~dirtype
) & (aHIDDEN
| aSYSTEM
)) {
2262 return NT_STATUS_NO_SUCH_FILE
;
2265 if (S_ISDIR(pst
->st_mode
)) {
2266 return NT_STATUS_OK
;
2269 if (fsp
->access_mask
& (DELETE_ACCESS
|FILE_WRITE_ATTRIBUTES
)) {
2270 return NT_STATUS_OK
;
2273 return NT_STATUS_ACCESS_DENIED
;
2276 /*******************************************************************
2277 * unlink a file with all relevant access checks
2278 *******************************************************************/
2280 static NTSTATUS
do_unlink(connection_struct
*conn
,
2281 struct smb_request
*req
,
2285 SMB_STRUCT_STAT sbuf
;
2288 uint32 dirtype_orig
= dirtype
;
2291 DEBUG(10,("do_unlink: %s, dirtype = %d\n", fname
, dirtype
));
2293 if (!CAN_WRITE(conn
)) {
2294 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
2297 if (SMB_VFS_LSTAT(conn
,fname
,&sbuf
) != 0) {
2298 return map_nt_error_from_unix(errno
);
2301 fattr
= dos_mode(conn
,fname
,&sbuf
);
2303 if (dirtype
& FILE_ATTRIBUTE_NORMAL
) {
2304 dirtype
= aDIR
|aARCH
|aRONLY
;
2307 dirtype
&= (aDIR
|aARCH
|aRONLY
|aHIDDEN
|aSYSTEM
);
2309 return NT_STATUS_NO_SUCH_FILE
;
2312 if (!dir_check_ftype(conn
, fattr
, dirtype
)) {
2314 return NT_STATUS_FILE_IS_A_DIRECTORY
;
2316 return NT_STATUS_NO_SUCH_FILE
;
2319 if (dirtype_orig
& 0x8000) {
2320 /* These will never be set for POSIX. */
2321 return NT_STATUS_NO_SUCH_FILE
;
2325 if ((fattr
& dirtype
) & FILE_ATTRIBUTE_DIRECTORY
) {
2326 return NT_STATUS_FILE_IS_A_DIRECTORY
;
2329 if ((fattr
& ~dirtype
) & (FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
)) {
2330 return NT_STATUS_NO_SUCH_FILE
;
2333 if (dirtype
& 0xFF00) {
2334 /* These will never be set for POSIX. */
2335 return NT_STATUS_NO_SUCH_FILE
;
2340 return NT_STATUS_NO_SUCH_FILE
;
2343 /* Can't delete a directory. */
2345 return NT_STATUS_FILE_IS_A_DIRECTORY
;
2350 else if (dirtype
& aDIR
) /* Asked for a directory and it isn't. */
2351 return NT_STATUS_OBJECT_NAME_INVALID
;
2352 #endif /* JRATEST */
2354 /* Fix for bug #3035 from SATOH Fumiyasu <fumiyas@miraclelinux.com>
2356 On a Windows share, a file with read-only dosmode can be opened with
2357 DELETE_ACCESS. But on a Samba share (delete readonly = no), it
2358 fails with NT_STATUS_CANNOT_DELETE error.
2360 This semantic causes a problem that a user can not
2361 rename a file with read-only dosmode on a Samba share
2362 from a Windows command prompt (i.e. cmd.exe, but can rename
2363 from Windows Explorer).
2366 if (!lp_delete_readonly(SNUM(conn
))) {
2367 if (fattr
& aRONLY
) {
2368 return NT_STATUS_CANNOT_DELETE
;
2372 /* On open checks the open itself will check the share mode, so
2373 don't do it here as we'll get it wrong. */
2375 status
= create_file_unixpath
2379 DELETE_ACCESS
, /* access_mask */
2380 FILE_SHARE_NONE
, /* share_access */
2381 FILE_OPEN
, /* create_disposition*/
2382 FILE_NON_DIRECTORY_FILE
, /* create_options */
2383 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
2384 0, /* oplock_request */
2385 0, /* allocation_size */
2392 if (!NT_STATUS_IS_OK(status
)) {
2393 DEBUG(10, ("create_file_unixpath failed: %s\n",
2394 nt_errstr(status
)));
2398 /* The set is across all open files on this dev/inode pair. */
2399 if (!set_delete_on_close(fsp
, True
, &conn
->server_info
->utok
)) {
2400 close_file(fsp
, NORMAL_CLOSE
);
2401 return NT_STATUS_ACCESS_DENIED
;
2404 return close_file(fsp
,NORMAL_CLOSE
);
2407 /****************************************************************************
2408 The guts of the unlink command, split out so it may be called by the NT SMB
2410 ****************************************************************************/
2412 NTSTATUS
unlink_internals(connection_struct
*conn
, struct smb_request
*req
,
2413 uint32 dirtype
, const char *name_in
, bool has_wild
)
2415 const char *directory
= NULL
;
2420 NTSTATUS status
= NT_STATUS_OK
;
2421 SMB_STRUCT_STAT sbuf
;
2422 TALLOC_CTX
*ctx
= talloc_tos();
2424 status
= unix_convert(ctx
, conn
, name_in
, has_wild
, &name
, NULL
, &sbuf
);
2425 if (!NT_STATUS_IS_OK(status
)) {
2429 p
= strrchr_m(name
,'/');
2431 directory
= talloc_strdup(ctx
, ".");
2433 return NT_STATUS_NO_MEMORY
;
2443 * We should only check the mangled cache
2444 * here if unix_convert failed. This means
2445 * that the path in 'mask' doesn't exist
2446 * on the file system and so we need to look
2447 * for a possible mangle. This patch from
2448 * Tine Smukavec <valentin.smukavec@hermes.si>.
2451 if (!VALID_STAT(sbuf
) && mangle_is_mangled(mask
,conn
->params
)) {
2452 char *new_mask
= NULL
;
2453 mangle_lookup_name_from_8_3(ctx
,
2463 directory
= talloc_asprintf(ctx
,
2468 return NT_STATUS_NO_MEMORY
;
2471 dirtype
= FILE_ATTRIBUTE_NORMAL
;
2474 status
= check_name(conn
, directory
);
2475 if (!NT_STATUS_IS_OK(status
)) {
2479 status
= do_unlink(conn
, req
, directory
, dirtype
);
2480 if (!NT_STATUS_IS_OK(status
)) {
2486 struct smb_Dir
*dir_hnd
= NULL
;
2490 if ((dirtype
& SAMBA_ATTRIBUTES_MASK
) == aDIR
) {
2491 return NT_STATUS_OBJECT_NAME_INVALID
;
2494 if (strequal(mask
,"????????.???")) {
2499 status
= check_name(conn
, directory
);
2500 if (!NT_STATUS_IS_OK(status
)) {
2504 dir_hnd
= OpenDir(talloc_tos(), conn
, directory
, mask
,
2506 if (dir_hnd
== NULL
) {
2507 return map_nt_error_from_unix(errno
);
2510 /* XXXX the CIFS spec says that if bit0 of the flags2 field is set then
2511 the pattern matches against the long name, otherwise the short name
2512 We don't implement this yet XXXX
2515 status
= NT_STATUS_NO_SUCH_FILE
;
2517 while ((dname
= ReadDirName(dir_hnd
, &offset
))) {
2521 if (!is_visible_file(conn
, directory
, dname
, &st
, True
)) {
2525 /* Quick check for "." and ".." */
2526 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
2530 if(!mask_match(dname
, mask
, conn
->case_sensitive
)) {
2534 fname
= talloc_asprintf(ctx
, "%s/%s",
2538 return NT_STATUS_NO_MEMORY
;
2541 status
= check_name(conn
, fname
);
2542 if (!NT_STATUS_IS_OK(status
)) {
2543 TALLOC_FREE(dir_hnd
);
2547 status
= do_unlink(conn
, req
, fname
, dirtype
);
2548 if (!NT_STATUS_IS_OK(status
)) {
2554 DEBUG(3,("unlink_internals: successful unlink [%s]\n",
2559 TALLOC_FREE(dir_hnd
);
2562 if (count
== 0 && NT_STATUS_IS_OK(status
) && errno
!= 0) {
2563 status
= map_nt_error_from_unix(errno
);
2569 /****************************************************************************
2571 ****************************************************************************/
2573 void reply_unlink(struct smb_request
*req
)
2575 connection_struct
*conn
= req
->conn
;
2579 bool path_contains_wcard
= False
;
2580 TALLOC_CTX
*ctx
= talloc_tos();
2582 START_PROFILE(SMBunlink
);
2585 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2586 END_PROFILE(SMBunlink
);
2590 dirtype
= SVAL(req
->inbuf
,smb_vwv0
);
2592 srvstr_get_path_wcard(ctx
, (char *)req
->inbuf
, req
->flags2
, &name
,
2593 smb_buf(req
->inbuf
) + 1, 0,
2594 STR_TERMINATE
, &status
, &path_contains_wcard
);
2595 if (!NT_STATUS_IS_OK(status
)) {
2596 reply_nterror(req
, status
);
2597 END_PROFILE(SMBunlink
);
2601 status
= resolve_dfspath_wcard(ctx
, conn
,
2602 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
2605 &path_contains_wcard
);
2606 if (!NT_STATUS_IS_OK(status
)) {
2607 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
2608 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
2609 ERRSRV
, ERRbadpath
);
2610 END_PROFILE(SMBunlink
);
2613 reply_nterror(req
, status
);
2614 END_PROFILE(SMBunlink
);
2618 DEBUG(3,("reply_unlink : %s\n",name
));
2620 status
= unlink_internals(conn
, req
, dirtype
, name
,
2621 path_contains_wcard
);
2622 if (!NT_STATUS_IS_OK(status
)) {
2623 if (open_was_deferred(req
->mid
)) {
2624 /* We have re-scheduled this call. */
2625 END_PROFILE(SMBunlink
);
2628 reply_nterror(req
, status
);
2629 END_PROFILE(SMBunlink
);
2633 reply_outbuf(req
, 0, 0);
2634 END_PROFILE(SMBunlink
);
2639 /****************************************************************************
2641 ****************************************************************************/
2643 static void fail_readraw(void)
2645 const char *errstr
= talloc_asprintf(talloc_tos(),
2646 "FAIL ! reply_readbraw: socket write fail (%s)",
2651 exit_server_cleanly(errstr
);
2654 /****************************************************************************
2655 Fake (read/write) sendfile. Returns -1 on read or write fail.
2656 ****************************************************************************/
2658 static ssize_t
fake_sendfile(files_struct
*fsp
, SMB_OFF_T startpos
,
2662 size_t tosend
= nread
;
2669 bufsize
= MIN(nread
, 65536);
2671 if (!(buf
= SMB_MALLOC_ARRAY(char, bufsize
))) {
2675 while (tosend
> 0) {
2679 if (tosend
> bufsize
) {
2684 ret
= read_file(fsp
,buf
,startpos
,cur_read
);
2690 /* If we had a short read, fill with zeros. */
2691 if (ret
< cur_read
) {
2692 memset(buf
, '\0', cur_read
- ret
);
2695 if (write_data(smbd_server_fd(),buf
,cur_read
) != cur_read
) {
2700 startpos
+= cur_read
;
2704 return (ssize_t
)nread
;
2707 /****************************************************************************
2708 Return a readbraw error (4 bytes of zero).
2709 ****************************************************************************/
2711 static void reply_readbraw_error(void)
2715 if (write_data(smbd_server_fd(),header
,4) != 4) {
2720 /****************************************************************************
2721 Use sendfile in readbraw.
2722 ****************************************************************************/
2724 void send_file_readbraw(connection_struct
*conn
,
2730 char *outbuf
= NULL
;
2733 #if defined(WITH_SENDFILE)
2735 * We can only use sendfile on a non-chained packet
2736 * but we can use on a non-oplocked file. tridge proved this
2737 * on a train in Germany :-). JRA.
2738 * reply_readbraw has already checked the length.
2741 if ( (chain_size
== 0) && (nread
> 0) && (fsp
->base_fsp
== NULL
) &&
2742 (fsp
->wcp
== NULL
) && lp_use_sendfile(SNUM(conn
)) ) {
2744 DATA_BLOB header_blob
;
2746 _smb_setlen(header
,nread
);
2747 header_blob
= data_blob_const(header
, 4);
2749 if (SMB_VFS_SENDFILE(smbd_server_fd(), fsp
,
2750 &header_blob
, startpos
, nread
) == -1) {
2751 /* Returning ENOSYS means no data at all was sent.
2752 * Do this as a normal read. */
2753 if (errno
== ENOSYS
) {
2754 goto normal_readbraw
;
2758 * Special hack for broken Linux with no working sendfile. If we
2759 * return EINTR we sent the header but not the rest of the data.
2760 * Fake this up by doing read/write calls.
2762 if (errno
== EINTR
) {
2763 /* Ensure we don't do this again. */
2764 set_use_sendfile(SNUM(conn
), False
);
2765 DEBUG(0,("send_file_readbraw: sendfile not available. Faking..\n"));
2767 if (fake_sendfile(fsp
, startpos
, nread
) == -1) {
2768 DEBUG(0,("send_file_readbraw: fake_sendfile failed for file %s (%s).\n",
2769 fsp
->fsp_name
, strerror(errno
) ));
2770 exit_server_cleanly("send_file_readbraw fake_sendfile failed");
2775 DEBUG(0,("send_file_readbraw: sendfile failed for file %s (%s). Terminating\n",
2776 fsp
->fsp_name
, strerror(errno
) ));
2777 exit_server_cleanly("send_file_readbraw sendfile failed");
2786 outbuf
= TALLOC_ARRAY(NULL
, char, nread
+4);
2788 DEBUG(0,("send_file_readbraw: TALLOC_ARRAY failed for size %u.\n",
2789 (unsigned)(nread
+4)));
2790 reply_readbraw_error();
2795 ret
= read_file(fsp
,outbuf
+4,startpos
,nread
);
2796 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2805 _smb_setlen(outbuf
,ret
);
2806 if (write_data(smbd_server_fd(),outbuf
,4+ret
) != 4+ret
)
2809 TALLOC_FREE(outbuf
);
2812 /****************************************************************************
2813 Reply to a readbraw (core+ protocol).
2814 ****************************************************************************/
2816 void reply_readbraw(struct smb_request
*req
)
2818 connection_struct
*conn
= req
->conn
;
2819 ssize_t maxcount
,mincount
;
2826 START_PROFILE(SMBreadbraw
);
2828 if (srv_is_signing_active() || is_encrypted_packet(req
->inbuf
)) {
2829 exit_server_cleanly("reply_readbraw: SMB signing/sealing is active - "
2830 "raw reads/writes are disallowed.");
2834 reply_readbraw_error();
2835 END_PROFILE(SMBreadbraw
);
2840 * Special check if an oplock break has been issued
2841 * and the readraw request croses on the wire, we must
2842 * return a zero length response here.
2845 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
2848 * We have to do a check_fsp by hand here, as
2849 * we must always return 4 zero bytes on error,
2853 if (!fsp
|| !conn
|| conn
!= fsp
->conn
||
2854 req
->vuid
!= fsp
->vuid
||
2855 fsp
->is_directory
|| fsp
->fh
->fd
== -1) {
2857 * fsp could be NULL here so use the value from the packet. JRA.
2859 DEBUG(3,("reply_readbraw: fnum %d not valid "
2861 (int)SVAL(req
->inbuf
,smb_vwv0
)));
2862 reply_readbraw_error();
2863 END_PROFILE(SMBreadbraw
);
2867 /* Do a "by hand" version of CHECK_READ. */
2868 if (!(fsp
->can_read
||
2869 ((req
->flags2
& FLAGS2_READ_PERMIT_EXECUTE
) &&
2870 (fsp
->access_mask
& FILE_EXECUTE
)))) {
2871 DEBUG(3,("reply_readbraw: fnum %d not readable.\n",
2872 (int)SVAL(req
->inbuf
,smb_vwv0
)));
2873 reply_readbraw_error();
2874 END_PROFILE(SMBreadbraw
);
2878 flush_write_cache(fsp
, READRAW_FLUSH
);
2880 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv1
);
2881 if(req
->wct
== 10) {
2883 * This is a large offset (64 bit) read.
2885 #ifdef LARGE_SMB_OFF_T
2887 startpos
|= (((SMB_OFF_T
)IVAL(req
->inbuf
,smb_vwv8
)) << 32);
2889 #else /* !LARGE_SMB_OFF_T */
2892 * Ensure we haven't been sent a >32 bit offset.
2895 if(IVAL(req
->inbuf
,smb_vwv8
) != 0) {
2896 DEBUG(0,("reply_readbraw: large offset "
2897 "(%x << 32) used and we don't support "
2898 "64 bit offsets.\n",
2899 (unsigned int)IVAL(req
->inbuf
,smb_vwv8
) ));
2900 reply_readbraw_error();
2901 END_PROFILE(SMBreadbraw
);
2905 #endif /* LARGE_SMB_OFF_T */
2908 DEBUG(0,("reply_readbraw: negative 64 bit "
2909 "readraw offset (%.0f) !\n",
2910 (double)startpos
));
2911 reply_readbraw_error();
2912 END_PROFILE(SMBreadbraw
);
2917 maxcount
= (SVAL(req
->inbuf
,smb_vwv3
) & 0xFFFF);
2918 mincount
= (SVAL(req
->inbuf
,smb_vwv4
) & 0xFFFF);
2920 /* ensure we don't overrun the packet size */
2921 maxcount
= MIN(65535,maxcount
);
2923 if (is_locked(fsp
,(uint32
)req
->smbpid
,
2924 (SMB_BIG_UINT
)maxcount
,
2925 (SMB_BIG_UINT
)startpos
,
2927 reply_readbraw_error();
2928 END_PROFILE(SMBreadbraw
);
2932 if (SMB_VFS_FSTAT(fsp
, &st
) == 0) {
2936 if (startpos
>= size
) {
2939 nread
= MIN(maxcount
,(size
- startpos
));
2942 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2943 if (nread
< mincount
)
2947 DEBUG( 3, ( "reply_readbraw: fnum=%d start=%.0f max=%lu "
2948 "min=%lu nread=%lu\n",
2949 fsp
->fnum
, (double)startpos
,
2950 (unsigned long)maxcount
,
2951 (unsigned long)mincount
,
2952 (unsigned long)nread
) );
2954 send_file_readbraw(conn
, fsp
, startpos
, nread
, mincount
);
2956 DEBUG(5,("reply_readbraw finished\n"));
2957 END_PROFILE(SMBreadbraw
);
2961 #define DBGC_CLASS DBGC_LOCKING
2963 /****************************************************************************
2964 Reply to a lockread (core+ protocol).
2965 ****************************************************************************/
2967 void reply_lockread(struct smb_request
*req
)
2969 connection_struct
*conn
= req
->conn
;
2976 struct byte_range_lock
*br_lck
= NULL
;
2979 START_PROFILE(SMBlockread
);
2982 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2983 END_PROFILE(SMBlockread
);
2987 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
2989 if (!check_fsp(conn
, req
, fsp
)) {
2990 END_PROFILE(SMBlockread
);
2994 if (!CHECK_READ(fsp
,req
->inbuf
)) {
2995 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
2996 END_PROFILE(SMBlockread
);
3000 release_level_2_oplocks_on_change(fsp
);
3002 numtoread
= SVAL(req
->inbuf
,smb_vwv1
);
3003 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv2
);
3005 numtoread
= MIN(BUFFER_SIZE
- (smb_size
+ 3*2 + 3), numtoread
);
3007 reply_outbuf(req
, 5, numtoread
+ 3);
3009 data
= smb_buf(req
->outbuf
) + 3;
3012 * NB. Discovered by Menny Hamburger at Mainsoft. This is a core+
3013 * protocol request that predates the read/write lock concept.
3014 * Thus instead of asking for a read lock here we need to ask
3015 * for a write lock. JRA.
3016 * Note that the requested lock size is unaffected by max_recv.
3019 br_lck
= do_lock(smbd_messaging_context(),
3022 (SMB_BIG_UINT
)numtoread
,
3023 (SMB_BIG_UINT
)startpos
,
3026 False
, /* Non-blocking lock. */
3029 TALLOC_FREE(br_lck
);
3031 if (NT_STATUS_V(status
)) {
3032 reply_nterror(req
, status
);
3033 END_PROFILE(SMBlockread
);
3038 * However the requested READ size IS affected by max_recv. Insanity.... JRA.
3041 if (numtoread
> max_recv
) {
3042 DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u). \
3043 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
3044 (unsigned int)numtoread
, (unsigned int)max_recv
));
3045 numtoread
= MIN(numtoread
,max_recv
);
3047 nread
= read_file(fsp
,data
,startpos
,numtoread
);
3050 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
3051 END_PROFILE(SMBlockread
);
3055 srv_set_message((char *)req
->outbuf
, 5, nread
+3, False
);
3057 SSVAL(req
->outbuf
,smb_vwv0
,nread
);
3058 SSVAL(req
->outbuf
,smb_vwv5
,nread
+3);
3059 p
= smb_buf(req
->outbuf
);
3060 SCVAL(p
,0,0); /* pad byte. */
3063 DEBUG(3,("lockread fnum=%d num=%d nread=%d\n",
3064 fsp
->fnum
, (int)numtoread
, (int)nread
));
3066 END_PROFILE(SMBlockread
);
3071 #define DBGC_CLASS DBGC_ALL
3073 /****************************************************************************
3075 ****************************************************************************/
3077 void reply_read(struct smb_request
*req
)
3079 connection_struct
*conn
= req
->conn
;
3087 START_PROFILE(SMBread
);
3090 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3091 END_PROFILE(SMBread
);
3095 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
3097 if (!check_fsp(conn
, req
, fsp
)) {
3098 END_PROFILE(SMBread
);
3102 if (!CHECK_READ(fsp
,req
->inbuf
)) {
3103 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
3104 END_PROFILE(SMBread
);
3108 numtoread
= SVAL(req
->inbuf
,smb_vwv1
);
3109 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv2
);
3111 numtoread
= MIN(BUFFER_SIZE
-outsize
,numtoread
);
3114 * The requested read size cannot be greater than max_recv. JRA.
3116 if (numtoread
> max_recv
) {
3117 DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u). \
3118 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
3119 (unsigned int)numtoread
, (unsigned int)max_recv
));
3120 numtoread
= MIN(numtoread
,max_recv
);
3123 reply_outbuf(req
, 5, numtoread
+3);
3125 data
= smb_buf(req
->outbuf
) + 3;
3127 if (is_locked(fsp
, (uint32
)req
->smbpid
, (SMB_BIG_UINT
)numtoread
,
3128 (SMB_BIG_UINT
)startpos
, READ_LOCK
)) {
3129 reply_doserror(req
, ERRDOS
,ERRlock
);
3130 END_PROFILE(SMBread
);
3135 nread
= read_file(fsp
,data
,startpos
,numtoread
);
3138 reply_unixerror(req
, ERRDOS
,ERRnoaccess
);
3139 END_PROFILE(SMBread
);
3143 srv_set_message((char *)req
->outbuf
, 5, nread
+3, False
);
3145 SSVAL(req
->outbuf
,smb_vwv0
,nread
);
3146 SSVAL(req
->outbuf
,smb_vwv5
,nread
+3);
3147 SCVAL(smb_buf(req
->outbuf
),0,1);
3148 SSVAL(smb_buf(req
->outbuf
),1,nread
);
3150 DEBUG( 3, ( "read fnum=%d num=%d nread=%d\n",
3151 fsp
->fnum
, (int)numtoread
, (int)nread
) );
3153 END_PROFILE(SMBread
);
3157 /****************************************************************************
3159 ****************************************************************************/
3161 static int setup_readX_header(char *outbuf
, size_t smb_maxcnt
)
3166 outsize
= srv_set_message(outbuf
,12,smb_maxcnt
,False
);
3167 data
= smb_buf(outbuf
);
3169 memset(outbuf
+smb_vwv0
,'\0',24); /* valgrind init. */
3171 SCVAL(outbuf
,smb_vwv0
,0xFF);
3172 SSVAL(outbuf
,smb_vwv2
,0xFFFF); /* Remaining - must be -1. */
3173 SSVAL(outbuf
,smb_vwv5
,smb_maxcnt
);
3174 SSVAL(outbuf
,smb_vwv6
,smb_offset(data
,outbuf
));
3175 SSVAL(outbuf
,smb_vwv7
,(smb_maxcnt
>> 16));
3176 SSVAL(smb_buf(outbuf
),-2,smb_maxcnt
);
3177 /* Reset the outgoing length, set_message truncates at 0x1FFFF. */
3178 _smb_setlen_large(outbuf
,(smb_size
+ 12*2 + smb_maxcnt
- 4));
3182 /****************************************************************************
3183 Reply to a read and X - possibly using sendfile.
3184 ****************************************************************************/
3186 static void send_file_readX(connection_struct
*conn
, struct smb_request
*req
,
3187 files_struct
*fsp
, SMB_OFF_T startpos
,
3190 SMB_STRUCT_STAT sbuf
;
3193 if(SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
3194 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
3198 if (startpos
> sbuf
.st_size
) {
3200 } else if (smb_maxcnt
> (sbuf
.st_size
- startpos
)) {
3201 smb_maxcnt
= (sbuf
.st_size
- startpos
);
3204 if (smb_maxcnt
== 0) {
3208 #if defined(WITH_SENDFILE)
3210 * We can only use sendfile on a non-chained packet
3211 * but we can use on a non-oplocked file. tridge proved this
3212 * on a train in Germany :-). JRA.
3215 if ((chain_size
== 0) && (CVAL(req
->inbuf
,smb_vwv0
) == 0xFF) &&
3216 !is_encrypted_packet(req
->inbuf
) && (fsp
->base_fsp
== NULL
) &&
3217 lp_use_sendfile(SNUM(conn
)) && (fsp
->wcp
== NULL
) ) {
3218 uint8 headerbuf
[smb_size
+ 12 * 2];
3222 * Set up the packet header before send. We
3223 * assume here the sendfile will work (get the
3224 * correct amount of data).
3227 header
= data_blob_const(headerbuf
, sizeof(headerbuf
));
3229 construct_reply_common((char *)req
->inbuf
, (char *)headerbuf
);
3230 setup_readX_header((char *)headerbuf
, smb_maxcnt
);
3232 if ((nread
= SMB_VFS_SENDFILE(smbd_server_fd(), fsp
, &header
, startpos
, smb_maxcnt
)) == -1) {
3233 /* Returning ENOSYS means no data at all was sent.
3234 Do this as a normal read. */
3235 if (errno
== ENOSYS
) {
3240 * Special hack for broken Linux with no working sendfile. If we
3241 * return EINTR we sent the header but not the rest of the data.
3242 * Fake this up by doing read/write calls.
3245 if (errno
== EINTR
) {
3246 /* Ensure we don't do this again. */
3247 set_use_sendfile(SNUM(conn
), False
);
3248 DEBUG(0,("send_file_readX: sendfile not available. Faking..\n"));
3249 nread
= fake_sendfile(fsp
, startpos
,
3252 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
3253 fsp
->fsp_name
, strerror(errno
) ));
3254 exit_server_cleanly("send_file_readX: fake_sendfile failed");
3256 DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
3257 fsp
->fnum
, (int)smb_maxcnt
, (int)nread
) );
3258 /* No outbuf here means successful sendfile. */
3259 TALLOC_FREE(req
->outbuf
);
3263 DEBUG(0,("send_file_readX: sendfile failed for file %s (%s). Terminating\n",
3264 fsp
->fsp_name
, strerror(errno
) ));
3265 exit_server_cleanly("send_file_readX sendfile failed");
3268 DEBUG( 3, ( "send_file_readX: sendfile fnum=%d max=%d nread=%d\n",
3269 fsp
->fnum
, (int)smb_maxcnt
, (int)nread
) );
3270 /* No outbuf here means successful sendfile. */
3271 TALLOC_FREE(req
->outbuf
);
3278 if ((smb_maxcnt
& 0xFF0000) > 0x10000) {
3279 uint8 headerbuf
[smb_size
+ 2*12];
3281 construct_reply_common((char *)req
->inbuf
, (char *)headerbuf
);
3282 setup_readX_header((char *)headerbuf
, smb_maxcnt
);
3284 /* Send out the header. */
3285 if (write_data(smbd_server_fd(), (char *)headerbuf
,
3286 sizeof(headerbuf
)) != sizeof(headerbuf
)) {
3287 DEBUG(0,("send_file_readX: write_data failed for file %s (%s). Terminating\n",
3288 fsp
->fsp_name
, strerror(errno
) ));
3289 exit_server_cleanly("send_file_readX sendfile failed");
3291 nread
= fake_sendfile(fsp
, startpos
, smb_maxcnt
);
3293 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
3294 fsp
->fsp_name
, strerror(errno
) ));
3295 exit_server_cleanly("send_file_readX: fake_sendfile failed");
3297 TALLOC_FREE(req
->outbuf
);
3300 reply_outbuf(req
, 12, smb_maxcnt
);
3302 nread
= read_file(fsp
, smb_buf(req
->outbuf
), startpos
,
3305 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
3309 setup_readX_header((char *)req
->outbuf
, nread
);
3311 DEBUG( 3, ( "send_file_readX fnum=%d max=%d nread=%d\n",
3312 fsp
->fnum
, (int)smb_maxcnt
, (int)nread
) );
3320 /****************************************************************************
3321 Reply to a read and X.
3322 ****************************************************************************/
3324 void reply_read_and_X(struct smb_request
*req
)
3326 connection_struct
*conn
= req
->conn
;
3330 bool big_readX
= False
;
3332 size_t smb_mincnt
= SVAL(req
->inbuf
,smb_vwv6
);
3335 START_PROFILE(SMBreadX
);
3337 if ((req
->wct
!= 10) && (req
->wct
!= 12)) {
3338 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3342 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv2
));
3343 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv3
);
3344 smb_maxcnt
= SVAL(req
->inbuf
,smb_vwv5
);
3346 /* If it's an IPC, pass off the pipe handler. */
3348 reply_pipe_read_and_X(req
);
3349 END_PROFILE(SMBreadX
);
3353 if (!check_fsp(conn
, req
, fsp
)) {
3354 END_PROFILE(SMBreadX
);
3358 if (!CHECK_READ(fsp
,req
->inbuf
)) {
3359 reply_doserror(req
, ERRDOS
,ERRbadaccess
);
3360 END_PROFILE(SMBreadX
);
3364 if (global_client_caps
& CAP_LARGE_READX
) {
3365 size_t upper_size
= SVAL(req
->inbuf
,smb_vwv7
);
3366 smb_maxcnt
|= (upper_size
<<16);
3367 if (upper_size
> 1) {
3368 /* Can't do this on a chained packet. */
3369 if ((CVAL(req
->inbuf
,smb_vwv0
) != 0xFF)) {
3370 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
3371 END_PROFILE(SMBreadX
);
3374 /* We currently don't do this on signed or sealed data. */
3375 if (srv_is_signing_active() || is_encrypted_packet(req
->inbuf
)) {
3376 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
3377 END_PROFILE(SMBreadX
);
3380 /* Is there room in the reply for this data ? */
3381 if (smb_maxcnt
> (0xFFFFFF - (smb_size
-4 + 12*2))) {
3383 NT_STATUS_INVALID_PARAMETER
);
3384 END_PROFILE(SMBreadX
);
3391 if (req
->wct
== 12) {
3392 #ifdef LARGE_SMB_OFF_T
3394 * This is a large offset (64 bit) read.
3396 startpos
|= (((SMB_OFF_T
)IVAL(req
->inbuf
,smb_vwv10
)) << 32);
3398 #else /* !LARGE_SMB_OFF_T */
3401 * Ensure we haven't been sent a >32 bit offset.
3404 if(IVAL(req
->inbuf
,smb_vwv10
) != 0) {
3405 DEBUG(0,("reply_read_and_X - large offset (%x << 32) "
3406 "used and we don't support 64 bit offsets.\n",
3407 (unsigned int)IVAL(req
->inbuf
,smb_vwv10
) ));
3408 END_PROFILE(SMBreadX
);
3409 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
3413 #endif /* LARGE_SMB_OFF_T */
3417 if (is_locked(fsp
, (uint32
)req
->smbpid
, (SMB_BIG_UINT
)smb_maxcnt
,
3418 (SMB_BIG_UINT
)startpos
, READ_LOCK
)) {
3419 END_PROFILE(SMBreadX
);
3420 reply_doserror(req
, ERRDOS
, ERRlock
);
3425 schedule_aio_read_and_X(conn
, req
, fsp
, startpos
, smb_maxcnt
)) {
3426 END_PROFILE(SMBreadX
);
3430 send_file_readX(conn
, req
, fsp
, startpos
, smb_maxcnt
);
3432 END_PROFILE(SMBreadX
);
3436 /****************************************************************************
3437 Error replies to writebraw must have smb_wct == 1. Fix this up.
3438 ****************************************************************************/
3440 void error_to_writebrawerr(struct smb_request
*req
)
3442 uint8
*old_outbuf
= req
->outbuf
;
3444 reply_outbuf(req
, 1, 0);
3446 memcpy(req
->outbuf
, old_outbuf
, smb_size
);
3447 TALLOC_FREE(old_outbuf
);
3450 /****************************************************************************
3451 Reply to a writebraw (core+ or LANMAN1.0 protocol).
3452 ****************************************************************************/
3454 void reply_writebraw(struct smb_request
*req
)
3456 connection_struct
*conn
= req
->conn
;
3459 ssize_t total_written
=0;
3460 size_t numtowrite
=0;
3468 START_PROFILE(SMBwritebraw
);
3471 * If we ever reply with an error, it must have the SMB command
3472 * type of SMBwritec, not SMBwriteBraw, as this tells the client
3475 SCVAL(req
->inbuf
,smb_com
,SMBwritec
);
3477 if (srv_is_signing_active()) {
3478 END_PROFILE(SMBwritebraw
);
3479 exit_server_cleanly("reply_writebraw: SMB signing is active - "
3480 "raw reads/writes are disallowed.");
3483 if (req
->wct
< 12) {
3484 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3485 error_to_writebrawerr(req
);
3486 END_PROFILE(SMBwritebraw
);
3490 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
3491 if (!check_fsp(conn
, req
, fsp
)) {
3492 error_to_writebrawerr(req
);
3493 END_PROFILE(SMBwritebraw
);
3497 if (!CHECK_WRITE(fsp
)) {
3498 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
3499 error_to_writebrawerr(req
);
3500 END_PROFILE(SMBwritebraw
);
3504 tcount
= IVAL(req
->inbuf
,smb_vwv1
);
3505 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv3
);
3506 write_through
= BITSETW(req
->inbuf
+smb_vwv7
,0);
3508 /* We have to deal with slightly different formats depending
3509 on whether we are using the core+ or lanman1.0 protocol */
3511 if(Protocol
<= PROTOCOL_COREPLUS
) {
3512 numtowrite
= SVAL(smb_buf(req
->inbuf
),-2);
3513 data
= smb_buf(req
->inbuf
);
3515 numtowrite
= SVAL(req
->inbuf
,smb_vwv10
);
3516 data
= smb_base(req
->inbuf
) + SVAL(req
->inbuf
, smb_vwv11
);
3519 /* Ensure we don't write bytes past the end of this packet. */
3520 if (data
+ numtowrite
> smb_base(req
->inbuf
) + smb_len(req
->inbuf
)) {
3521 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3522 error_to_writebrawerr(req
);
3523 END_PROFILE(SMBwritebraw
);
3527 if (is_locked(fsp
,(uint32
)req
->smbpid
,(SMB_BIG_UINT
)tcount
,
3528 (SMB_BIG_UINT
)startpos
, WRITE_LOCK
)) {
3529 reply_doserror(req
, ERRDOS
, ERRlock
);
3530 error_to_writebrawerr(req
);
3531 END_PROFILE(SMBwritebraw
);
3536 nwritten
= write_file(req
,fsp
,data
,startpos
,numtowrite
);
3539 DEBUG(3,("reply_writebraw: initial write fnum=%d start=%.0f num=%d "
3540 "wrote=%d sync=%d\n",
3541 fsp
->fnum
, (double)startpos
, (int)numtowrite
,
3542 (int)nwritten
, (int)write_through
));
3544 if (nwritten
< (ssize_t
)numtowrite
) {
3545 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
3546 error_to_writebrawerr(req
);
3547 END_PROFILE(SMBwritebraw
);
3551 total_written
= nwritten
;
3553 /* Allocate a buffer of 64k + length. */
3554 buf
= TALLOC_ARRAY(NULL
, char, 65540);
3556 reply_doserror(req
, ERRDOS
, ERRnomem
);
3557 error_to_writebrawerr(req
);
3558 END_PROFILE(SMBwritebraw
);
3562 /* Return a SMBwritebraw message to the redirector to tell
3563 * it to send more bytes */
3565 memcpy(buf
, req
->inbuf
, smb_size
);
3566 srv_set_message(buf
,Protocol
>PROTOCOL_COREPLUS
?1:0,0,True
);
3567 SCVAL(buf
,smb_com
,SMBwritebraw
);
3568 SSVALS(buf
,smb_vwv0
,0xFFFF);
3570 if (!srv_send_smb(smbd_server_fd(),
3572 IS_CONN_ENCRYPTED(conn
))) {
3573 exit_server_cleanly("reply_writebraw: srv_send_smb "
3577 /* Now read the raw data into the buffer and write it */
3578 status
= read_smb_length(smbd_server_fd(), buf
, SMB_SECONDARY_WAIT
,
3580 if (!NT_STATUS_IS_OK(status
)) {
3581 exit_server_cleanly("secondary writebraw failed");
3584 /* Set up outbuf to return the correct size */
3585 reply_outbuf(req
, 1, 0);
3587 if (numtowrite
!= 0) {
3589 if (numtowrite
> 0xFFFF) {
3590 DEBUG(0,("reply_writebraw: Oversize secondary write "
3591 "raw requested (%u). Terminating\n",
3592 (unsigned int)numtowrite
));
3593 exit_server_cleanly("secondary writebraw failed");
3596 if (tcount
> nwritten
+numtowrite
) {
3597 DEBUG(3,("reply_writebraw: Client overestimated the "
3599 (int)tcount
,(int)nwritten
,(int)numtowrite
));
3602 status
= read_data(smbd_server_fd(), buf
+4, numtowrite
);
3604 if (!NT_STATUS_IS_OK(status
)) {
3605 DEBUG(0,("reply_writebraw: Oversize secondary write "
3606 "raw read failed (%s). Terminating\n",
3607 nt_errstr(status
)));
3608 exit_server_cleanly("secondary writebraw failed");
3611 nwritten
= write_file(req
,fsp
,buf
+4,startpos
+nwritten
,numtowrite
);
3612 if (nwritten
== -1) {
3614 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
3615 error_to_writebrawerr(req
);
3616 END_PROFILE(SMBwritebraw
);
3620 if (nwritten
< (ssize_t
)numtowrite
) {
3621 SCVAL(req
->outbuf
,smb_rcls
,ERRHRD
);
3622 SSVAL(req
->outbuf
,smb_err
,ERRdiskfull
);
3626 total_written
+= nwritten
;
3631 SSVAL(req
->outbuf
,smb_vwv0
,total_written
);
3633 status
= sync_file(conn
, fsp
, write_through
);
3634 if (!NT_STATUS_IS_OK(status
)) {
3635 DEBUG(5,("reply_writebraw: sync_file for %s returned %s\n",
3636 fsp
->fsp_name
, nt_errstr(status
) ));
3637 reply_nterror(req
, status
);
3638 error_to_writebrawerr(req
);
3639 END_PROFILE(SMBwritebraw
);
3643 DEBUG(3,("reply_writebraw: secondart write fnum=%d start=%.0f num=%d "
3645 fsp
->fnum
, (double)startpos
, (int)numtowrite
,
3646 (int)total_written
));
3648 /* We won't return a status if write through is not selected - this
3649 * follows what WfWg does */
3650 END_PROFILE(SMBwritebraw
);
3652 if (!write_through
&& total_written
==tcount
) {
3654 #if RABBIT_PELLET_FIX
3656 * Fix for "rabbit pellet" mode, trigger an early TCP ack by
3657 * sending a SMBkeepalive. Thanks to DaveCB at Sun for this.
3660 if (!send_keepalive(smbd_server_fd())) {
3661 exit_server_cleanly("reply_writebraw: send of "
3662 "keepalive failed");
3665 TALLOC_FREE(req
->outbuf
);
3671 #define DBGC_CLASS DBGC_LOCKING
3673 /****************************************************************************
3674 Reply to a writeunlock (core+).
3675 ****************************************************************************/
3677 void reply_writeunlock(struct smb_request
*req
)
3679 connection_struct
*conn
= req
->conn
;
3680 ssize_t nwritten
= -1;
3684 NTSTATUS status
= NT_STATUS_OK
;
3687 START_PROFILE(SMBwriteunlock
);
3690 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3691 END_PROFILE(SMBwriteunlock
);
3695 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
3697 if (!check_fsp(conn
, req
, fsp
)) {
3698 END_PROFILE(SMBwriteunlock
);
3702 if (!CHECK_WRITE(fsp
)) {
3703 reply_doserror(req
, ERRDOS
,ERRbadaccess
);
3704 END_PROFILE(SMBwriteunlock
);
3708 numtowrite
= SVAL(req
->inbuf
,smb_vwv1
);
3709 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv2
);
3710 data
= smb_buf(req
->inbuf
) + 3;
3713 && is_locked(fsp
, (uint32
)req
->smbpid
, (SMB_BIG_UINT
)numtowrite
,
3714 (SMB_BIG_UINT
)startpos
, WRITE_LOCK
)) {
3715 reply_doserror(req
, ERRDOS
, ERRlock
);
3716 END_PROFILE(SMBwriteunlock
);
3720 /* The special X/Open SMB protocol handling of
3721 zero length writes is *NOT* done for
3723 if(numtowrite
== 0) {
3726 nwritten
= write_file(req
,fsp
,data
,startpos
,numtowrite
);
3729 status
= sync_file(conn
, fsp
, False
/* write through */);
3730 if (!NT_STATUS_IS_OK(status
)) {
3731 DEBUG(5,("reply_writeunlock: sync_file for %s returned %s\n",
3732 fsp
->fsp_name
, nt_errstr(status
) ));
3733 reply_nterror(req
, status
);
3734 END_PROFILE(SMBwriteunlock
);
3738 if(((nwritten
< numtowrite
) && (numtowrite
!= 0))||(nwritten
< 0)) {
3739 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
3740 END_PROFILE(SMBwriteunlock
);
3745 status
= do_unlock(smbd_messaging_context(),
3748 (SMB_BIG_UINT
)numtowrite
,
3749 (SMB_BIG_UINT
)startpos
,
3752 if (NT_STATUS_V(status
)) {
3753 reply_nterror(req
, status
);
3754 END_PROFILE(SMBwriteunlock
);
3759 reply_outbuf(req
, 1, 0);
3761 SSVAL(req
->outbuf
,smb_vwv0
,nwritten
);
3763 DEBUG(3,("writeunlock fnum=%d num=%d wrote=%d\n",
3764 fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
3766 END_PROFILE(SMBwriteunlock
);
3771 #define DBGC_CLASS DBGC_ALL
3773 /****************************************************************************
3775 ****************************************************************************/
3777 void reply_write(struct smb_request
*req
)
3779 connection_struct
*conn
= req
->conn
;
3781 ssize_t nwritten
= -1;
3787 START_PROFILE(SMBwrite
);
3790 END_PROFILE(SMBwrite
);
3791 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3795 /* If it's an IPC, pass off the pipe handler. */
3797 reply_pipe_write(req
);
3798 END_PROFILE(SMBwrite
);
3802 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
3804 if (!check_fsp(conn
, req
, fsp
)) {
3805 END_PROFILE(SMBwrite
);
3809 if (!CHECK_WRITE(fsp
)) {
3810 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
3811 END_PROFILE(SMBwrite
);
3815 numtowrite
= SVAL(req
->inbuf
,smb_vwv1
);
3816 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv2
);
3817 data
= smb_buf(req
->inbuf
) + 3;
3819 if (is_locked(fsp
, (uint32
)req
->smbpid
, (SMB_BIG_UINT
)numtowrite
,
3820 (SMB_BIG_UINT
)startpos
, WRITE_LOCK
)) {
3821 reply_doserror(req
, ERRDOS
, ERRlock
);
3822 END_PROFILE(SMBwrite
);
3827 * X/Open SMB protocol says that if smb_vwv1 is
3828 * zero then the file size should be extended or
3829 * truncated to the size given in smb_vwv[2-3].
3832 if(numtowrite
== 0) {
3834 * This is actually an allocate call, and set EOF. JRA.
3836 nwritten
= vfs_allocate_file_space(fsp
, (SMB_OFF_T
)startpos
);
3838 reply_nterror(req
, NT_STATUS_DISK_FULL
);
3839 END_PROFILE(SMBwrite
);
3842 nwritten
= vfs_set_filelen(fsp
, (SMB_OFF_T
)startpos
);
3844 reply_nterror(req
, NT_STATUS_DISK_FULL
);
3845 END_PROFILE(SMBwrite
);
3848 trigger_write_time_update_immediate(fsp
);
3850 nwritten
= write_file(req
,fsp
,data
,startpos
,numtowrite
);
3853 status
= sync_file(conn
, fsp
, False
);
3854 if (!NT_STATUS_IS_OK(status
)) {
3855 DEBUG(5,("reply_write: sync_file for %s returned %s\n",
3856 fsp
->fsp_name
, nt_errstr(status
) ));
3857 reply_nterror(req
, status
);
3858 END_PROFILE(SMBwrite
);
3862 if(((nwritten
== 0) && (numtowrite
!= 0))||(nwritten
< 0)) {
3863 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
3864 END_PROFILE(SMBwrite
);
3868 reply_outbuf(req
, 1, 0);
3870 SSVAL(req
->outbuf
,smb_vwv0
,nwritten
);
3872 if (nwritten
< (ssize_t
)numtowrite
) {
3873 SCVAL(req
->outbuf
,smb_rcls
,ERRHRD
);
3874 SSVAL(req
->outbuf
,smb_err
,ERRdiskfull
);
3877 DEBUG(3,("write fnum=%d num=%d wrote=%d\n", fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
3879 END_PROFILE(SMBwrite
);
3883 /****************************************************************************
3884 Ensure a buffer is a valid writeX for recvfile purposes.
3885 ****************************************************************************/
3887 #define STANDARD_WRITE_AND_X_HEADER_SIZE (smb_size - 4 + /* basic header */ \
3888 (2*14) + /* word count (including bcc) */ \
3891 bool is_valid_writeX_buffer(const uint8_t *inbuf
)
3894 connection_struct
*conn
= NULL
;
3895 unsigned int doff
= 0;
3896 size_t len
= smb_len_large(inbuf
);
3898 if (is_encrypted_packet(inbuf
)) {
3899 /* Can't do this on encrypted
3904 if (CVAL(inbuf
,smb_com
) != SMBwriteX
) {
3908 if (CVAL(inbuf
,smb_vwv0
) != 0xFF ||
3909 CVAL(inbuf
,smb_wct
) != 14) {
3910 DEBUG(10,("is_valid_writeX_buffer: chained or "
3911 "invalid word length.\n"));
3915 conn
= conn_find(SVAL(inbuf
, smb_tid
));
3917 DEBUG(10,("is_valid_writeX_buffer: bad tid\n"));
3921 DEBUG(10,("is_valid_writeX_buffer: IPC$ tid\n"));
3924 if (IS_PRINT(conn
)) {
3925 DEBUG(10,("is_valid_writeX_buffer: printing tid\n"));
3928 doff
= SVAL(inbuf
,smb_vwv11
);
3930 numtowrite
= SVAL(inbuf
,smb_vwv10
);
3932 if (len
> doff
&& len
- doff
> 0xFFFF) {
3933 numtowrite
|= (((size_t)SVAL(inbuf
,smb_vwv9
))<<16);
3936 if (numtowrite
== 0) {
3937 DEBUG(10,("is_valid_writeX_buffer: zero write\n"));
3941 /* Ensure the sizes match up. */
3942 if (doff
< STANDARD_WRITE_AND_X_HEADER_SIZE
) {
3943 /* no pad byte...old smbclient :-( */
3944 DEBUG(10,("is_valid_writeX_buffer: small doff %u (min %u)\n",
3946 (unsigned int)STANDARD_WRITE_AND_X_HEADER_SIZE
));
3950 if (len
- doff
!= numtowrite
) {
3951 DEBUG(10,("is_valid_writeX_buffer: doff mismatch "
3952 "len = %u, doff = %u, numtowrite = %u\n",
3955 (unsigned int)numtowrite
));
3959 DEBUG(10,("is_valid_writeX_buffer: true "
3960 "len = %u, doff = %u, numtowrite = %u\n",
3963 (unsigned int)numtowrite
));
3968 /****************************************************************************
3969 Reply to a write and X.
3970 ****************************************************************************/
3972 void reply_write_and_X(struct smb_request
*req
)
3974 connection_struct
*conn
= req
->conn
;
3980 unsigned int smb_doff
;
3981 unsigned int smblen
;
3985 START_PROFILE(SMBwriteX
);
3987 if ((req
->wct
!= 12) && (req
->wct
!= 14)) {
3988 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3989 END_PROFILE(SMBwriteX
);
3993 numtowrite
= SVAL(req
->inbuf
,smb_vwv10
);
3994 smb_doff
= SVAL(req
->inbuf
,smb_vwv11
);
3995 smblen
= smb_len(req
->inbuf
);
3997 if (req
->unread_bytes
> 0xFFFF ||
3998 (smblen
> smb_doff
&&
3999 smblen
- smb_doff
> 0xFFFF)) {
4000 numtowrite
|= (((size_t)SVAL(req
->inbuf
,smb_vwv9
))<<16);
4003 if (req
->unread_bytes
) {
4004 /* Can't do a recvfile write on IPC$ */
4006 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4007 END_PROFILE(SMBwriteX
);
4010 if (numtowrite
!= req
->unread_bytes
) {
4011 reply_doserror(req
, ERRDOS
, ERRbadmem
);
4012 END_PROFILE(SMBwriteX
);
4016 if (smb_doff
> smblen
|| smb_doff
+ numtowrite
< numtowrite
||
4017 smb_doff
+ numtowrite
> smblen
) {
4018 reply_doserror(req
, ERRDOS
, ERRbadmem
);
4019 END_PROFILE(SMBwriteX
);
4024 /* If it's an IPC, pass off the pipe handler. */
4026 if (req
->unread_bytes
) {
4027 reply_doserror(req
, ERRDOS
, ERRbadmem
);
4028 END_PROFILE(SMBwriteX
);
4031 reply_pipe_write_and_X(req
);
4032 END_PROFILE(SMBwriteX
);
4036 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv2
));
4037 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv3
);
4038 write_through
= BITSETW(req
->inbuf
+smb_vwv7
,0);
4040 if (!check_fsp(conn
, req
, fsp
)) {
4041 END_PROFILE(SMBwriteX
);
4045 if (!CHECK_WRITE(fsp
)) {
4046 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
4047 END_PROFILE(SMBwriteX
);
4051 data
= smb_base(req
->inbuf
) + smb_doff
;
4053 if(req
->wct
== 14) {
4054 #ifdef LARGE_SMB_OFF_T
4056 * This is a large offset (64 bit) write.
4058 startpos
|= (((SMB_OFF_T
)IVAL(req
->inbuf
,smb_vwv12
)) << 32);
4060 #else /* !LARGE_SMB_OFF_T */
4063 * Ensure we haven't been sent a >32 bit offset.
4066 if(IVAL(req
->inbuf
,smb_vwv12
) != 0) {
4067 DEBUG(0,("reply_write_and_X - large offset (%x << 32) "
4068 "used and we don't support 64 bit offsets.\n",
4069 (unsigned int)IVAL(req
->inbuf
,smb_vwv12
) ));
4070 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
4071 END_PROFILE(SMBwriteX
);
4075 #endif /* LARGE_SMB_OFF_T */
4078 if (is_locked(fsp
,(uint32
)req
->smbpid
,
4079 (SMB_BIG_UINT
)numtowrite
,
4080 (SMB_BIG_UINT
)startpos
, WRITE_LOCK
)) {
4081 reply_doserror(req
, ERRDOS
, ERRlock
);
4082 END_PROFILE(SMBwriteX
);
4086 /* X/Open SMB protocol says that, unlike SMBwrite
4087 if the length is zero then NO truncation is
4088 done, just a write of zero. To truncate a file,
4091 if(numtowrite
== 0) {
4095 if ((req
->unread_bytes
== 0) &&
4096 schedule_aio_write_and_X(conn
, req
, fsp
, data
, startpos
,
4098 END_PROFILE(SMBwriteX
);
4102 nwritten
= write_file(req
,fsp
,data
,startpos
,numtowrite
);
4105 if(((nwritten
== 0) && (numtowrite
!= 0))||(nwritten
< 0)) {
4106 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
4107 END_PROFILE(SMBwriteX
);
4111 reply_outbuf(req
, 6, 0);
4112 SSVAL(req
->outbuf
,smb_vwv2
,nwritten
);
4113 SSVAL(req
->outbuf
,smb_vwv4
,nwritten
>>16);
4115 if (nwritten
< (ssize_t
)numtowrite
) {
4116 SCVAL(req
->outbuf
,smb_rcls
,ERRHRD
);
4117 SSVAL(req
->outbuf
,smb_err
,ERRdiskfull
);
4120 DEBUG(3,("writeX fnum=%d num=%d wrote=%d\n",
4121 fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
4123 status
= sync_file(conn
, fsp
, write_through
);
4124 if (!NT_STATUS_IS_OK(status
)) {
4125 DEBUG(5,("reply_write_and_X: sync_file for %s returned %s\n",
4126 fsp
->fsp_name
, nt_errstr(status
) ));
4127 reply_nterror(req
, status
);
4128 END_PROFILE(SMBwriteX
);
4132 END_PROFILE(SMBwriteX
);
4137 /****************************************************************************
4139 ****************************************************************************/
4141 void reply_lseek(struct smb_request
*req
)
4143 connection_struct
*conn
= req
->conn
;
4149 START_PROFILE(SMBlseek
);
4152 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4153 END_PROFILE(SMBlseek
);
4157 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
4159 if (!check_fsp(conn
, req
, fsp
)) {
4163 flush_write_cache(fsp
, SEEK_FLUSH
);
4165 mode
= SVAL(req
->inbuf
,smb_vwv1
) & 3;
4166 /* NB. This doesn't use IVAL_TO_SMB_OFF_T as startpos can be signed in this case. */
4167 startpos
= (SMB_OFF_T
)IVALS(req
->inbuf
,smb_vwv2
);
4176 res
= fsp
->fh
->pos
+ startpos
;
4187 if (umode
== SEEK_END
) {
4188 if((res
= SMB_VFS_LSEEK(fsp
,startpos
,umode
)) == -1) {
4189 if(errno
== EINVAL
) {
4190 SMB_OFF_T current_pos
= startpos
;
4191 SMB_STRUCT_STAT sbuf
;
4193 if(SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
4194 reply_unixerror(req
, ERRDOS
,
4196 END_PROFILE(SMBlseek
);
4200 current_pos
+= sbuf
.st_size
;
4202 res
= SMB_VFS_LSEEK(fsp
,0,SEEK_SET
);
4207 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
4208 END_PROFILE(SMBlseek
);
4215 reply_outbuf(req
, 2, 0);
4216 SIVAL(req
->outbuf
,smb_vwv0
,res
);
4218 DEBUG(3,("lseek fnum=%d ofs=%.0f newpos = %.0f mode=%d\n",
4219 fsp
->fnum
, (double)startpos
, (double)res
, mode
));
4221 END_PROFILE(SMBlseek
);
4225 /****************************************************************************
4227 ****************************************************************************/
4229 void reply_flush(struct smb_request
*req
)
4231 connection_struct
*conn
= req
->conn
;
4235 START_PROFILE(SMBflush
);
4238 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4242 fnum
= SVAL(req
->inbuf
,smb_vwv0
);
4243 fsp
= file_fsp(fnum
);
4245 if ((fnum
!= 0xFFFF) && !check_fsp(conn
, req
, fsp
)) {
4250 file_sync_all(conn
);
4252 NTSTATUS status
= sync_file(conn
, fsp
, True
);
4253 if (!NT_STATUS_IS_OK(status
)) {
4254 DEBUG(5,("reply_flush: sync_file for %s returned %s\n",
4255 fsp
->fsp_name
, nt_errstr(status
) ));
4256 reply_nterror(req
, status
);
4257 END_PROFILE(SMBflush
);
4262 reply_outbuf(req
, 0, 0);
4264 DEBUG(3,("flush\n"));
4265 END_PROFILE(SMBflush
);
4269 /****************************************************************************
4271 conn POINTER CAN BE NULL HERE !
4272 ****************************************************************************/
4274 void reply_exit(struct smb_request
*req
)
4276 START_PROFILE(SMBexit
);
4278 file_close_pid(req
->smbpid
, req
->vuid
);
4280 reply_outbuf(req
, 0, 0);
4282 DEBUG(3,("exit\n"));
4284 END_PROFILE(SMBexit
);
4288 /****************************************************************************
4289 Reply to a close - has to deal with closing a directory opened by NT SMB's.
4290 ****************************************************************************/
4292 void reply_close(struct smb_request
*req
)
4294 connection_struct
*conn
= req
->conn
;
4295 NTSTATUS status
= NT_STATUS_OK
;
4296 files_struct
*fsp
= NULL
;
4297 START_PROFILE(SMBclose
);
4300 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4301 END_PROFILE(SMBclose
);
4305 /* If it's an IPC, pass off to the pipe handler. */
4307 reply_pipe_close(conn
, req
);
4308 END_PROFILE(SMBclose
);
4312 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
4315 * We can only use check_fsp if we know it's not a directory.
4318 if(!fsp
|| (fsp
->conn
!= conn
) || (fsp
->vuid
!= req
->vuid
)) {
4319 reply_doserror(req
, ERRDOS
, ERRbadfid
);
4320 END_PROFILE(SMBclose
);
4324 if(fsp
->is_directory
) {
4326 * Special case - close NT SMB directory handle.
4328 DEBUG(3,("close directory fnum=%d\n", fsp
->fnum
));
4329 status
= close_file(fsp
,NORMAL_CLOSE
);
4333 * Close ordinary file.
4336 DEBUG(3,("close fd=%d fnum=%d (numopen=%d)\n",
4337 fsp
->fh
->fd
, fsp
->fnum
,
4338 conn
->num_files_open
));
4341 * Take care of any time sent in the close.
4344 t
= srv_make_unix_date3(req
->inbuf
+smb_vwv1
);
4345 set_close_write_time(fsp
, convert_time_t_to_timespec(t
));
4348 * close_file() returns the unix errno if an error
4349 * was detected on close - normally this is due to
4350 * a disk full error. If not then it was probably an I/O error.
4353 status
= close_file(fsp
,NORMAL_CLOSE
);
4356 if (!NT_STATUS_IS_OK(status
)) {
4357 reply_nterror(req
, status
);
4358 END_PROFILE(SMBclose
);
4362 reply_outbuf(req
, 0, 0);
4363 END_PROFILE(SMBclose
);
4367 /****************************************************************************
4368 Reply to a writeclose (Core+ protocol).
4369 ****************************************************************************/
4371 void reply_writeclose(struct smb_request
*req
)
4373 connection_struct
*conn
= req
->conn
;
4375 ssize_t nwritten
= -1;
4376 NTSTATUS close_status
= NT_STATUS_OK
;
4379 struct timespec mtime
;
4382 START_PROFILE(SMBwriteclose
);
4385 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4386 END_PROFILE(SMBwriteclose
);
4390 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
4392 if (!check_fsp(conn
, req
, fsp
)) {
4393 END_PROFILE(SMBwriteclose
);
4396 if (!CHECK_WRITE(fsp
)) {
4397 reply_doserror(req
, ERRDOS
,ERRbadaccess
);
4398 END_PROFILE(SMBwriteclose
);
4402 numtowrite
= SVAL(req
->inbuf
,smb_vwv1
);
4403 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv2
);
4404 mtime
= convert_time_t_to_timespec(srv_make_unix_date3(
4405 req
->inbuf
+smb_vwv4
));
4406 data
= smb_buf(req
->inbuf
) + 1;
4409 && is_locked(fsp
, (uint32
)req
->smbpid
, (SMB_BIG_UINT
)numtowrite
,
4410 (SMB_BIG_UINT
)startpos
, WRITE_LOCK
)) {
4411 reply_doserror(req
, ERRDOS
,ERRlock
);
4412 END_PROFILE(SMBwriteclose
);
4416 nwritten
= write_file(req
,fsp
,data
,startpos
,numtowrite
);
4418 set_close_write_time(fsp
, mtime
);
4421 * More insanity. W2K only closes the file if writelen > 0.
4426 DEBUG(3,("reply_writeclose: zero length write doesn't close file %s\n",
4428 close_status
= close_file(fsp
,NORMAL_CLOSE
);
4431 DEBUG(3,("writeclose fnum=%d num=%d wrote=%d (numopen=%d)\n",
4432 fsp
->fnum
, (int)numtowrite
, (int)nwritten
,
4433 conn
->num_files_open
));
4435 if(((nwritten
== 0) && (numtowrite
!= 0))||(nwritten
< 0)) {
4436 reply_doserror(req
, ERRHRD
, ERRdiskfull
);
4437 END_PROFILE(SMBwriteclose
);
4441 if(!NT_STATUS_IS_OK(close_status
)) {
4442 reply_nterror(req
, close_status
);
4443 END_PROFILE(SMBwriteclose
);
4447 reply_outbuf(req
, 1, 0);
4449 SSVAL(req
->outbuf
,smb_vwv0
,nwritten
);
4450 END_PROFILE(SMBwriteclose
);
4455 #define DBGC_CLASS DBGC_LOCKING
4457 /****************************************************************************
4459 ****************************************************************************/
4461 void reply_lock(struct smb_request
*req
)
4463 connection_struct
*conn
= req
->conn
;
4464 SMB_BIG_UINT count
,offset
;
4467 struct byte_range_lock
*br_lck
= NULL
;
4469 START_PROFILE(SMBlock
);
4472 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4473 END_PROFILE(SMBlock
);
4477 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
4479 if (!check_fsp(conn
, req
, fsp
)) {
4480 END_PROFILE(SMBlock
);
4484 release_level_2_oplocks_on_change(fsp
);
4486 count
= (SMB_BIG_UINT
)IVAL(req
->inbuf
,smb_vwv1
);
4487 offset
= (SMB_BIG_UINT
)IVAL(req
->inbuf
,smb_vwv3
);
4489 DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
4490 fsp
->fh
->fd
, fsp
->fnum
, (double)offset
, (double)count
));
4492 br_lck
= do_lock(smbd_messaging_context(),
4499 False
, /* Non-blocking lock. */
4503 TALLOC_FREE(br_lck
);
4505 if (NT_STATUS_V(status
)) {
4506 reply_nterror(req
, status
);
4507 END_PROFILE(SMBlock
);
4511 reply_outbuf(req
, 0, 0);
4513 END_PROFILE(SMBlock
);
4517 /****************************************************************************
4519 ****************************************************************************/
4521 void reply_unlock(struct smb_request
*req
)
4523 connection_struct
*conn
= req
->conn
;
4524 SMB_BIG_UINT count
,offset
;
4528 START_PROFILE(SMBunlock
);
4531 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4532 END_PROFILE(SMBunlock
);
4536 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
4538 if (!check_fsp(conn
, req
, fsp
)) {
4539 END_PROFILE(SMBunlock
);
4543 count
= (SMB_BIG_UINT
)IVAL(req
->inbuf
,smb_vwv1
);
4544 offset
= (SMB_BIG_UINT
)IVAL(req
->inbuf
,smb_vwv3
);
4546 status
= do_unlock(smbd_messaging_context(),
4553 if (NT_STATUS_V(status
)) {
4554 reply_nterror(req
, status
);
4555 END_PROFILE(SMBunlock
);
4559 DEBUG( 3, ( "unlock fd=%d fnum=%d offset=%.0f count=%.0f\n",
4560 fsp
->fh
->fd
, fsp
->fnum
, (double)offset
, (double)count
) );
4562 reply_outbuf(req
, 0, 0);
4564 END_PROFILE(SMBunlock
);
4569 #define DBGC_CLASS DBGC_ALL
4571 /****************************************************************************
4573 conn POINTER CAN BE NULL HERE !
4574 ****************************************************************************/
4576 void reply_tdis(struct smb_request
*req
)
4578 connection_struct
*conn
= req
->conn
;
4579 START_PROFILE(SMBtdis
);
4582 DEBUG(4,("Invalid connection in tdis\n"));
4583 reply_doserror(req
, ERRSRV
, ERRinvnid
);
4584 END_PROFILE(SMBtdis
);
4590 close_cnum(conn
,req
->vuid
);
4593 reply_outbuf(req
, 0, 0);
4594 END_PROFILE(SMBtdis
);
4598 /****************************************************************************
4600 conn POINTER CAN BE NULL HERE !
4601 ****************************************************************************/
4603 void reply_echo(struct smb_request
*req
)
4605 connection_struct
*conn
= req
->conn
;
4608 unsigned int data_len
= smb_buflen(req
->inbuf
);
4610 START_PROFILE(SMBecho
);
4613 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4614 END_PROFILE(SMBecho
);
4618 if (data_len
> BUFFER_SIZE
) {
4619 DEBUG(0,("reply_echo: data_len too large.\n"));
4620 reply_nterror(req
, NT_STATUS_INSUFFICIENT_RESOURCES
);
4621 END_PROFILE(SMBecho
);
4625 smb_reverb
= SVAL(req
->inbuf
,smb_vwv0
);
4627 reply_outbuf(req
, 1, data_len
);
4629 /* copy any incoming data back out */
4631 memcpy(smb_buf(req
->outbuf
),smb_buf(req
->inbuf
),data_len
);
4634 if (smb_reverb
> 100) {
4635 DEBUG(0,("large reverb (%d)?? Setting to 100\n",smb_reverb
));
4639 for (seq_num
=1 ; seq_num
<= smb_reverb
; seq_num
++) {
4640 SSVAL(req
->outbuf
,smb_vwv0
,seq_num
);
4642 show_msg((char *)req
->outbuf
);
4643 if (!srv_send_smb(smbd_server_fd(),
4644 (char *)req
->outbuf
,
4645 IS_CONN_ENCRYPTED(conn
)||req
->encrypted
))
4646 exit_server_cleanly("reply_echo: srv_send_smb failed.");
4649 DEBUG(3,("echo %d times\n", smb_reverb
));
4651 TALLOC_FREE(req
->outbuf
);
4653 END_PROFILE(SMBecho
);
4657 /****************************************************************************
4658 Reply to a printopen.
4659 ****************************************************************************/
4661 void reply_printopen(struct smb_request
*req
)
4663 connection_struct
*conn
= req
->conn
;
4665 SMB_STRUCT_STAT sbuf
;
4668 START_PROFILE(SMBsplopen
);
4671 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4672 END_PROFILE(SMBsplopen
);
4676 if (!CAN_PRINT(conn
)) {
4677 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
4678 END_PROFILE(SMBsplopen
);
4682 status
= file_new(conn
, &fsp
);
4683 if(!NT_STATUS_IS_OK(status
)) {
4684 reply_nterror(req
, status
);
4685 END_PROFILE(SMBsplopen
);
4689 /* Open for exclusive use, write only. */
4690 status
= print_fsp_open(conn
, NULL
, req
->vuid
, fsp
, &sbuf
);
4692 if (!NT_STATUS_IS_OK(status
)) {
4694 reply_nterror(req
, status
);
4695 END_PROFILE(SMBsplopen
);
4699 reply_outbuf(req
, 1, 0);
4700 SSVAL(req
->outbuf
,smb_vwv0
,fsp
->fnum
);
4702 DEBUG(3,("openprint fd=%d fnum=%d\n",
4703 fsp
->fh
->fd
, fsp
->fnum
));
4705 END_PROFILE(SMBsplopen
);
4709 /****************************************************************************
4710 Reply to a printclose.
4711 ****************************************************************************/
4713 void reply_printclose(struct smb_request
*req
)
4715 connection_struct
*conn
= req
->conn
;
4719 START_PROFILE(SMBsplclose
);
4722 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4723 END_PROFILE(SMBsplclose
);
4727 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
4729 if (!check_fsp(conn
, req
, fsp
)) {
4730 END_PROFILE(SMBsplclose
);
4734 if (!CAN_PRINT(conn
)) {
4735 reply_nterror(req
, NT_STATUS_DOS(ERRSRV
, ERRerror
));
4736 END_PROFILE(SMBsplclose
);
4740 DEBUG(3,("printclose fd=%d fnum=%d\n",
4741 fsp
->fh
->fd
,fsp
->fnum
));
4743 status
= close_file(fsp
,NORMAL_CLOSE
);
4745 if(!NT_STATUS_IS_OK(status
)) {
4746 reply_nterror(req
, status
);
4747 END_PROFILE(SMBsplclose
);
4751 reply_outbuf(req
, 0, 0);
4753 END_PROFILE(SMBsplclose
);
4757 /****************************************************************************
4758 Reply to a printqueue.
4759 ****************************************************************************/
4761 void reply_printqueue(struct smb_request
*req
)
4763 connection_struct
*conn
= req
->conn
;
4767 START_PROFILE(SMBsplretq
);
4770 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4771 END_PROFILE(SMBsplretq
);
4775 max_count
= SVAL(req
->inbuf
,smb_vwv0
);
4776 start_index
= SVAL(req
->inbuf
,smb_vwv1
);
4778 /* we used to allow the client to get the cnum wrong, but that
4779 is really quite gross and only worked when there was only
4780 one printer - I think we should now only accept it if they
4781 get it right (tridge) */
4782 if (!CAN_PRINT(conn
)) {
4783 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
4784 END_PROFILE(SMBsplretq
);
4788 reply_outbuf(req
, 2, 3);
4789 SSVAL(req
->outbuf
,smb_vwv0
,0);
4790 SSVAL(req
->outbuf
,smb_vwv1
,0);
4791 SCVAL(smb_buf(req
->outbuf
),0,1);
4792 SSVAL(smb_buf(req
->outbuf
),1,0);
4794 DEBUG(3,("printqueue start_index=%d max_count=%d\n",
4795 start_index
, max_count
));
4798 print_queue_struct
*queue
= NULL
;
4799 print_status_struct status
;
4800 int count
= print_queue_status(SNUM(conn
), &queue
, &status
);
4801 int num_to_get
= ABS(max_count
);
4802 int first
= (max_count
>0?start_index
:start_index
+max_count
+1);
4808 num_to_get
= MIN(num_to_get
,count
-first
);
4811 for (i
=first
;i
<first
+num_to_get
;i
++) {
4815 srv_put_dos_date2(p
,0,queue
[i
].time
);
4816 SCVAL(p
,4,(queue
[i
].status
==LPQ_PRINTING
?2:3));
4817 SSVAL(p
,5, queue
[i
].job
);
4818 SIVAL(p
,7,queue
[i
].size
);
4820 srvstr_push(blob
, req
->flags2
, p
+12,
4821 queue
[i
].fs_user
, 16, STR_ASCII
);
4823 if (message_push_blob(
4826 blob
, sizeof(blob
))) == -1) {
4827 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
4828 END_PROFILE(SMBsplretq
);
4834 SSVAL(req
->outbuf
,smb_vwv0
,count
);
4835 SSVAL(req
->outbuf
,smb_vwv1
,
4836 (max_count
>0?first
+count
:first
-1));
4837 SCVAL(smb_buf(req
->outbuf
),0,1);
4838 SSVAL(smb_buf(req
->outbuf
),1,28*count
);
4843 DEBUG(3,("%d entries returned in queue\n",count
));
4846 END_PROFILE(SMBsplretq
);
4850 /****************************************************************************
4851 Reply to a printwrite.
4852 ****************************************************************************/
4854 void reply_printwrite(struct smb_request
*req
)
4856 connection_struct
*conn
= req
->conn
;
4861 START_PROFILE(SMBsplwr
);
4864 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4865 END_PROFILE(SMBsplwr
);
4869 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
4871 if (!check_fsp(conn
, req
, fsp
)) {
4872 END_PROFILE(SMBsplwr
);
4876 if (!CAN_PRINT(conn
)) {
4877 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
4878 END_PROFILE(SMBsplwr
);
4882 if (!CHECK_WRITE(fsp
)) {
4883 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
4884 END_PROFILE(SMBsplwr
);
4888 numtowrite
= SVAL(smb_buf(req
->inbuf
),1);
4890 if (smb_buflen(req
->inbuf
) < numtowrite
+ 3) {
4891 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4892 END_PROFILE(SMBsplwr
);
4896 data
= smb_buf(req
->inbuf
) + 3;
4898 if (write_file(req
,fsp
,data
,-1,numtowrite
) != numtowrite
) {
4899 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
4900 END_PROFILE(SMBsplwr
);
4904 DEBUG( 3, ( "printwrite fnum=%d num=%d\n", fsp
->fnum
, numtowrite
) );
4906 END_PROFILE(SMBsplwr
);
4910 /****************************************************************************
4912 ****************************************************************************/
4914 void reply_mkdir(struct smb_request
*req
)
4916 connection_struct
*conn
= req
->conn
;
4917 char *directory
= NULL
;
4919 SMB_STRUCT_STAT sbuf
;
4920 TALLOC_CTX
*ctx
= talloc_tos();
4922 START_PROFILE(SMBmkdir
);
4924 srvstr_get_path(ctx
, (char *)req
->inbuf
, req
->flags2
, &directory
,
4925 smb_buf(req
->inbuf
) + 1, 0,
4926 STR_TERMINATE
, &status
);
4927 if (!NT_STATUS_IS_OK(status
)) {
4928 reply_nterror(req
, status
);
4929 END_PROFILE(SMBmkdir
);
4933 status
= resolve_dfspath(ctx
, conn
,
4934 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
4937 if (!NT_STATUS_IS_OK(status
)) {
4938 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
4939 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
4940 ERRSRV
, ERRbadpath
);
4941 END_PROFILE(SMBmkdir
);
4944 reply_nterror(req
, status
);
4945 END_PROFILE(SMBmkdir
);
4949 status
= unix_convert(ctx
, conn
, directory
, False
, &directory
, NULL
, &sbuf
);
4950 if (!NT_STATUS_IS_OK(status
)) {
4951 reply_nterror(req
, status
);
4952 END_PROFILE(SMBmkdir
);
4956 status
= check_name(conn
, directory
);
4957 if (!NT_STATUS_IS_OK(status
)) {
4958 reply_nterror(req
, status
);
4959 END_PROFILE(SMBmkdir
);
4963 status
= create_directory(conn
, req
, directory
);
4965 DEBUG(5, ("create_directory returned %s\n", nt_errstr(status
)));
4967 if (!NT_STATUS_IS_OK(status
)) {
4969 if (!use_nt_status()
4970 && NT_STATUS_EQUAL(status
,
4971 NT_STATUS_OBJECT_NAME_COLLISION
)) {
4973 * Yes, in the DOS error code case we get a
4974 * ERRDOS:ERRnoaccess here. See BASE-SAMBA3ERROR
4975 * samba4 torture test.
4977 status
= NT_STATUS_DOS(ERRDOS
, ERRnoaccess
);
4980 reply_nterror(req
, status
);
4981 END_PROFILE(SMBmkdir
);
4985 reply_outbuf(req
, 0, 0);
4987 DEBUG( 3, ( "mkdir %s\n", directory
) );
4989 END_PROFILE(SMBmkdir
);
4993 /****************************************************************************
4994 Static function used by reply_rmdir to delete an entire directory
4995 tree recursively. Return True on ok, False on fail.
4996 ****************************************************************************/
4998 static bool recursive_rmdir(TALLOC_CTX
*ctx
,
4999 connection_struct
*conn
,
5002 const char *dname
= NULL
;
5005 struct smb_Dir
*dir_hnd
= OpenDir(talloc_tos(), conn
, directory
,
5011 while((dname
= ReadDirName(dir_hnd
, &offset
))) {
5012 char *fullname
= NULL
;
5015 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
5019 if (!is_visible_file(conn
, directory
, dname
, &st
, False
)) {
5023 /* Construct the full name. */
5024 fullname
= talloc_asprintf(ctx
,
5034 if(SMB_VFS_LSTAT(conn
,fullname
, &st
) != 0) {
5039 if(st
.st_mode
& S_IFDIR
) {
5040 if(!recursive_rmdir(ctx
, conn
, fullname
)) {
5044 if(SMB_VFS_RMDIR(conn
,fullname
) != 0) {
5048 } else if(SMB_VFS_UNLINK(conn
,fullname
) != 0) {
5052 TALLOC_FREE(fullname
);
5054 TALLOC_FREE(dir_hnd
);
5058 /****************************************************************************
5059 The internals of the rmdir code - called elsewhere.
5060 ****************************************************************************/
5062 NTSTATUS
rmdir_internals(TALLOC_CTX
*ctx
,
5063 connection_struct
*conn
,
5064 const char *directory
)
5069 /* Might be a symlink. */
5070 if(SMB_VFS_LSTAT(conn
, directory
, &st
) != 0) {
5071 return map_nt_error_from_unix(errno
);
5074 if (S_ISLNK(st
.st_mode
)) {
5075 /* Is what it points to a directory ? */
5076 if(SMB_VFS_STAT(conn
, directory
, &st
) != 0) {
5077 return map_nt_error_from_unix(errno
);
5079 if (!(S_ISDIR(st
.st_mode
))) {
5080 return NT_STATUS_NOT_A_DIRECTORY
;
5082 ret
= SMB_VFS_UNLINK(conn
,directory
);
5084 ret
= SMB_VFS_RMDIR(conn
,directory
);
5087 notify_fname(conn
, NOTIFY_ACTION_REMOVED
,
5088 FILE_NOTIFY_CHANGE_DIR_NAME
,
5090 return NT_STATUS_OK
;
5093 if(((errno
== ENOTEMPTY
)||(errno
== EEXIST
)) && lp_veto_files(SNUM(conn
))) {
5095 * Check to see if the only thing in this directory are
5096 * vetoed files/directories. If so then delete them and
5097 * retry. If we fail to delete any of them (and we *don't*
5098 * do a recursive delete) then fail the rmdir.
5102 struct smb_Dir
*dir_hnd
= OpenDir(talloc_tos(), conn
,
5103 directory
, NULL
, 0);
5105 if(dir_hnd
== NULL
) {
5110 while ((dname
= ReadDirName(dir_hnd
,&dirpos
))) {
5111 if((strcmp(dname
, ".") == 0) || (strcmp(dname
, "..")==0))
5113 if (!is_visible_file(conn
, directory
, dname
, &st
, False
))
5115 if(!IS_VETO_PATH(conn
, dname
)) {
5116 TALLOC_FREE(dir_hnd
);
5122 /* We only have veto files/directories.
5123 * Are we allowed to delete them ? */
5125 if(!lp_recursive_veto_delete(SNUM(conn
))) {
5126 TALLOC_FREE(dir_hnd
);
5131 /* Do a recursive delete. */
5132 RewindDir(dir_hnd
,&dirpos
);
5133 while ((dname
= ReadDirName(dir_hnd
,&dirpos
))) {
5134 char *fullname
= NULL
;
5136 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
5139 if (!is_visible_file(conn
, directory
, dname
, &st
, False
)) {
5143 fullname
= talloc_asprintf(ctx
,
5153 if(SMB_VFS_LSTAT(conn
,fullname
, &st
) != 0) {
5156 if(st
.st_mode
& S_IFDIR
) {
5157 if(!recursive_rmdir(ctx
, conn
, fullname
)) {
5160 if(SMB_VFS_RMDIR(conn
,fullname
) != 0) {
5163 } else if(SMB_VFS_UNLINK(conn
,fullname
) != 0) {
5166 TALLOC_FREE(fullname
);
5168 TALLOC_FREE(dir_hnd
);
5169 /* Retry the rmdir */
5170 ret
= SMB_VFS_RMDIR(conn
,directory
);
5176 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
5177 "%s\n", directory
,strerror(errno
)));
5178 return map_nt_error_from_unix(errno
);
5181 notify_fname(conn
, NOTIFY_ACTION_REMOVED
,
5182 FILE_NOTIFY_CHANGE_DIR_NAME
,
5185 return NT_STATUS_OK
;
5188 /****************************************************************************
5190 ****************************************************************************/
5192 void reply_rmdir(struct smb_request
*req
)
5194 connection_struct
*conn
= req
->conn
;
5195 char *directory
= NULL
;
5196 SMB_STRUCT_STAT sbuf
;
5198 TALLOC_CTX
*ctx
= talloc_tos();
5200 START_PROFILE(SMBrmdir
);
5202 srvstr_get_path(ctx
, (char *)req
->inbuf
, req
->flags2
, &directory
,
5203 smb_buf(req
->inbuf
) + 1, 0,
5204 STR_TERMINATE
, &status
);
5205 if (!NT_STATUS_IS_OK(status
)) {
5206 reply_nterror(req
, status
);
5207 END_PROFILE(SMBrmdir
);
5211 status
= resolve_dfspath(ctx
, conn
,
5212 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
5215 if (!NT_STATUS_IS_OK(status
)) {
5216 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
5217 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
5218 ERRSRV
, ERRbadpath
);
5219 END_PROFILE(SMBrmdir
);
5222 reply_nterror(req
, status
);
5223 END_PROFILE(SMBrmdir
);
5227 status
= unix_convert(ctx
, conn
, directory
, False
, &directory
,
5229 if (!NT_STATUS_IS_OK(status
)) {
5230 reply_nterror(req
, status
);
5231 END_PROFILE(SMBrmdir
);
5235 status
= check_name(conn
, directory
);
5236 if (!NT_STATUS_IS_OK(status
)) {
5237 reply_nterror(req
, status
);
5238 END_PROFILE(SMBrmdir
);
5242 dptr_closepath(directory
, req
->smbpid
);
5243 status
= rmdir_internals(ctx
, conn
, directory
);
5244 if (!NT_STATUS_IS_OK(status
)) {
5245 reply_nterror(req
, status
);
5246 END_PROFILE(SMBrmdir
);
5250 reply_outbuf(req
, 0, 0);
5252 DEBUG( 3, ( "rmdir %s\n", directory
) );
5254 END_PROFILE(SMBrmdir
);
5258 /*******************************************************************
5259 Resolve wildcards in a filename rename.
5260 ********************************************************************/
5262 static bool resolve_wildcards(TALLOC_CTX
*ctx
,
5267 char *name2_copy
= NULL
;
5272 char *p
,*p2
, *pname1
, *pname2
;
5274 name2_copy
= talloc_strdup(ctx
, name2
);
5279 pname1
= strrchr_m(name1
,'/');
5280 pname2
= strrchr_m(name2_copy
,'/');
5282 if (!pname1
|| !pname2
) {
5286 /* Truncate the copy of name2 at the last '/' */
5289 /* Now go past the '/' */
5293 root1
= talloc_strdup(ctx
, pname1
);
5294 root2
= talloc_strdup(ctx
, pname2
);
5296 if (!root1
|| !root2
) {
5300 p
= strrchr_m(root1
,'.');
5303 ext1
= talloc_strdup(ctx
, p
+1);
5305 ext1
= talloc_strdup(ctx
, "");
5307 p
= strrchr_m(root2
,'.');
5310 ext2
= talloc_strdup(ctx
, p
+1);
5312 ext2
= talloc_strdup(ctx
, "");
5315 if (!ext1
|| !ext2
) {
5323 /* Hmmm. Should this be mb-aware ? */
5326 } else if (*p2
== '*') {
5328 root2
= talloc_asprintf(ctx
, "%s%s",
5347 /* Hmmm. Should this be mb-aware ? */
5350 } else if (*p2
== '*') {
5352 ext2
= talloc_asprintf(ctx
, "%s%s",
5368 *pp_newname
= talloc_asprintf(ctx
, "%s/%s.%s",
5373 *pp_newname
= talloc_asprintf(ctx
, "%s/%s",
5385 /****************************************************************************
5386 Ensure open files have their names updated. Updated to notify other smbd's
5388 ****************************************************************************/
5390 static void rename_open_files(connection_struct
*conn
,
5391 struct share_mode_lock
*lck
,
5392 const char *newname
)
5395 bool did_rename
= False
;
5397 for(fsp
= file_find_di_first(lck
->id
); fsp
;
5398 fsp
= file_find_di_next(fsp
)) {
5399 /* fsp_name is a relative path under the fsp. To change this for other
5400 sharepaths we need to manipulate relative paths. */
5401 /* TODO - create the absolute path and manipulate the newname
5402 relative to the sharepath. */
5403 if (!strequal(fsp
->conn
->connectpath
, conn
->connectpath
)) {
5406 DEBUG(10,("rename_open_files: renaming file fnum %d (file_id %s) from %s -> %s\n",
5407 fsp
->fnum
, file_id_string_tos(&fsp
->file_id
),
5408 fsp
->fsp_name
, newname
));
5409 string_set(&fsp
->fsp_name
, newname
);
5414 DEBUG(10,("rename_open_files: no open files on file_id %s for %s\n",
5415 file_id_string_tos(&lck
->id
), newname
));
5418 /* Send messages to all smbd's (not ourself) that the name has changed. */
5419 rename_share_filename(smbd_messaging_context(), lck
, conn
->connectpath
,
5423 /****************************************************************************
5424 We need to check if the source path is a parent directory of the destination
5425 (ie. a rename of /foo/bar/baz -> /foo/bar/baz/bibble/bobble. If so we must
5426 refuse the rename with a sharing violation. Under UNIX the above call can
5427 *succeed* if /foo/bar/baz is a symlink to another area in the share. We
5428 probably need to check that the client is a Windows one before disallowing
5429 this as a UNIX client (one with UNIX extensions) can know the source is a
5430 symlink and make this decision intelligently. Found by an excellent bug
5431 report from <AndyLiebman@aol.com>.
5432 ****************************************************************************/
5434 static bool rename_path_prefix_equal(const char *src
, const char *dest
)
5436 const char *psrc
= src
;
5437 const char *pdst
= dest
;
5440 if (psrc
[0] == '.' && psrc
[1] == '/') {
5443 if (pdst
[0] == '.' && pdst
[1] == '/') {
5446 if ((slen
= strlen(psrc
)) > strlen(pdst
)) {
5449 return ((memcmp(psrc
, pdst
, slen
) == 0) && pdst
[slen
] == '/');
5453 * Do the notify calls from a rename
5456 static void notify_rename(connection_struct
*conn
, bool is_dir
,
5457 const char *oldpath
, const char *newpath
)
5459 char *olddir
, *newdir
;
5460 const char *oldname
, *newname
;
5463 mask
= is_dir
? FILE_NOTIFY_CHANGE_DIR_NAME
5464 : FILE_NOTIFY_CHANGE_FILE_NAME
;
5466 if (!parent_dirname_talloc(NULL
, oldpath
, &olddir
, &oldname
)
5467 || !parent_dirname_talloc(NULL
, newpath
, &newdir
, &newname
)) {
5468 TALLOC_FREE(olddir
);
5472 if (strcmp(olddir
, newdir
) == 0) {
5473 notify_fname(conn
, NOTIFY_ACTION_OLD_NAME
, mask
, oldpath
);
5474 notify_fname(conn
, NOTIFY_ACTION_NEW_NAME
, mask
, newpath
);
5477 notify_fname(conn
, NOTIFY_ACTION_REMOVED
, mask
, oldpath
);
5478 notify_fname(conn
, NOTIFY_ACTION_ADDED
, mask
, newpath
);
5480 TALLOC_FREE(olddir
);
5481 TALLOC_FREE(newdir
);
5483 /* this is a strange one. w2k3 gives an additional event for
5484 CHANGE_ATTRIBUTES and CHANGE_CREATION on the new file when renaming
5485 files, but not directories */
5487 notify_fname(conn
, NOTIFY_ACTION_MODIFIED
,
5488 FILE_NOTIFY_CHANGE_ATTRIBUTES
5489 |FILE_NOTIFY_CHANGE_CREATION
,
5494 /****************************************************************************
5495 Rename an open file - given an fsp.
5496 ****************************************************************************/
5498 NTSTATUS
rename_internals_fsp(connection_struct
*conn
,
5501 const char *newname_last_component
,
5503 bool replace_if_exists
)
5505 TALLOC_CTX
*ctx
= talloc_tos();
5506 SMB_STRUCT_STAT sbuf
, sbuf1
;
5507 NTSTATUS status
= NT_STATUS_OK
;
5508 struct share_mode_lock
*lck
= NULL
;
5509 bool dst_exists
, old_is_stream
, new_is_stream
;
5513 status
= check_name(conn
, newname
);
5514 if (!NT_STATUS_IS_OK(status
)) {
5518 /* Ensure newname contains a '/' */
5519 if(strrchr_m(newname
,'/') == 0) {
5520 newname
= talloc_asprintf(ctx
,
5524 return NT_STATUS_NO_MEMORY
;
5529 * Check for special case with case preserving and not
5530 * case sensitive. If the old last component differs from the original
5531 * last component only by case, then we should allow
5532 * the rename (user is trying to change the case of the
5536 if((conn
->case_sensitive
== False
) && (conn
->case_preserve
== True
) &&
5537 strequal(newname
, fsp
->fsp_name
)) {
5539 char *newname_modified_last_component
= NULL
;
5542 * Get the last component of the modified name.
5543 * Note that we guarantee that newname contains a '/'
5546 p
= strrchr_m(newname
,'/');
5547 newname_modified_last_component
= talloc_strdup(ctx
,
5549 if (!newname_modified_last_component
) {
5550 return NT_STATUS_NO_MEMORY
;
5553 if(strcsequal(newname_modified_last_component
,
5554 newname_last_component
) == False
) {
5556 * Replace the modified last component with
5559 *p
= '\0'; /* Truncate at the '/' */
5560 newname
= talloc_asprintf(ctx
,
5563 newname_last_component
);
5568 * If the src and dest names are identical - including case,
5569 * don't do the rename, just return success.
5572 if (strcsequal(fsp
->fsp_name
, newname
)) {
5573 DEBUG(3,("rename_internals_fsp: identical names in rename %s - returning success\n",
5575 return NT_STATUS_OK
;
5578 old_is_stream
= is_ntfs_stream_name(fsp
->fsp_name
);
5579 new_is_stream
= is_ntfs_stream_name(newname
);
5581 /* Return the correct error code if both names aren't streams. */
5582 if (!old_is_stream
&& new_is_stream
) {
5583 return NT_STATUS_OBJECT_NAME_INVALID
;
5586 if (old_is_stream
&& !new_is_stream
) {
5587 return NT_STATUS_INVALID_PARAMETER
;
5591 * Have vfs_object_exist also fill sbuf1
5593 dst_exists
= vfs_object_exist(conn
, newname
, &sbuf1
);
5595 if(!replace_if_exists
&& dst_exists
) {
5596 DEBUG(3,("rename_internals_fsp: dest exists doing rename %s -> %s\n",
5597 fsp
->fsp_name
,newname
));
5598 return NT_STATUS_OBJECT_NAME_COLLISION
;
5602 struct file_id fileid
= vfs_file_id_from_sbuf(conn
, &sbuf1
);
5603 files_struct
*dst_fsp
= file_find_di_first(fileid
);
5604 /* The file can be open when renaming a stream */
5605 if (dst_fsp
&& !new_is_stream
) {
5606 DEBUG(3, ("rename_internals_fsp: Target file open\n"));
5607 return NT_STATUS_ACCESS_DENIED
;
5611 /* Ensure we have a valid stat struct for the source. */
5612 if (fsp
->fh
->fd
!= -1) {
5613 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
5614 return map_nt_error_from_unix(errno
);
5618 if (fsp
->posix_open
) {
5619 ret
= SMB_VFS_LSTAT(conn
,fsp
->fsp_name
,&sbuf
);
5621 ret
= SMB_VFS_STAT(conn
,fsp
->fsp_name
,&sbuf
);
5624 return map_nt_error_from_unix(errno
);
5628 status
= can_rename(conn
, fsp
, attrs
, &sbuf
);
5630 if (!NT_STATUS_IS_OK(status
)) {
5631 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
5632 nt_errstr(status
), fsp
->fsp_name
,newname
));
5633 if (NT_STATUS_EQUAL(status
,NT_STATUS_SHARING_VIOLATION
))
5634 status
= NT_STATUS_ACCESS_DENIED
;
5638 if (rename_path_prefix_equal(fsp
->fsp_name
, newname
)) {
5639 return NT_STATUS_ACCESS_DENIED
;
5642 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
, NULL
, NULL
,
5646 * We have the file open ourselves, so not being able to get the
5647 * corresponding share mode lock is a fatal error.
5650 SMB_ASSERT(lck
!= NULL
);
5652 if(SMB_VFS_RENAME(conn
,fsp
->fsp_name
, newname
) == 0) {
5653 uint32 create_options
= fsp
->fh
->private_options
;
5655 DEBUG(3,("rename_internals_fsp: succeeded doing rename on %s -> %s\n",
5656 fsp
->fsp_name
,newname
));
5658 notify_rename(conn
, fsp
->is_directory
, fsp
->fsp_name
, newname
);
5660 rename_open_files(conn
, lck
, newname
);
5663 * A rename acts as a new file create w.r.t. allowing an initial delete
5664 * on close, probably because in Windows there is a new handle to the
5665 * new file. If initial delete on close was requested but not
5666 * originally set, we need to set it here. This is probably not 100% correct,
5667 * but will work for the CIFSFS client which in non-posix mode
5668 * depends on these semantics. JRA.
5671 if (create_options
& FILE_DELETE_ON_CLOSE
) {
5672 status
= can_set_delete_on_close(fsp
, True
, 0);
5674 if (NT_STATUS_IS_OK(status
)) {
5675 /* Note that here we set the *inital* delete on close flag,
5676 * not the regular one. The magic gets handled in close. */
5677 fsp
->initial_delete_on_close
= True
;
5681 return NT_STATUS_OK
;
5686 if (errno
== ENOTDIR
|| errno
== EISDIR
) {
5687 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
5689 status
= map_nt_error_from_unix(errno
);
5692 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
5693 nt_errstr(status
), fsp
->fsp_name
,newname
));
5698 /****************************************************************************
5699 The guts of the rename command, split out so it may be called by the NT SMB
5701 ****************************************************************************/
5703 NTSTATUS
rename_internals(TALLOC_CTX
*ctx
,
5704 connection_struct
*conn
,
5705 struct smb_request
*req
,
5706 const char *name_in
,
5707 const char *newname_in
,
5709 bool replace_if_exists
,
5712 uint32_t access_mask
)
5714 char *directory
= NULL
;
5716 char *last_component_src
= NULL
;
5717 char *last_component_dest
= NULL
;
5719 char *newname
= NULL
;
5722 NTSTATUS status
= NT_STATUS_OK
;
5723 SMB_STRUCT_STAT sbuf1
, sbuf2
;
5724 struct smb_Dir
*dir_hnd
= NULL
;
5727 bool posix_pathnames
= lp_posix_pathnames();
5732 status
= unix_convert(ctx
, conn
, name_in
, src_has_wild
, &name
,
5733 &last_component_src
, &sbuf1
);
5734 if (!NT_STATUS_IS_OK(status
)) {
5738 status
= unix_convert(ctx
, conn
, newname_in
, dest_has_wild
, &newname
,
5739 &last_component_dest
, &sbuf2
);
5740 if (!NT_STATUS_IS_OK(status
)) {
5745 * Split the old name into directory and last component
5746 * strings. Note that unix_convert may have stripped off a
5747 * leading ./ from both name and newname if the rename is
5748 * at the root of the share. We need to make sure either both
5749 * name and newname contain a / character or neither of them do
5750 * as this is checked in resolve_wildcards().
5753 p
= strrchr_m(name
,'/');
5755 directory
= talloc_strdup(ctx
, ".");
5757 return NT_STATUS_NO_MEMORY
;
5762 directory
= talloc_strdup(ctx
, name
);
5764 return NT_STATUS_NO_MEMORY
;
5767 *p
= '/'; /* Replace needed for exceptional test below. */
5771 * We should only check the mangled cache
5772 * here if unix_convert failed. This means
5773 * that the path in 'mask' doesn't exist
5774 * on the file system and so we need to look
5775 * for a possible mangle. This patch from
5776 * Tine Smukavec <valentin.smukavec@hermes.si>.
5779 if (!VALID_STAT(sbuf1
) && mangle_is_mangled(mask
, conn
->params
)) {
5780 char *new_mask
= NULL
;
5781 mangle_lookup_name_from_8_3(ctx
,
5790 if (!src_has_wild
) {
5794 * No wildcards - just process the one file.
5796 bool is_short_name
= mangle_is_8_3(name
, True
, conn
->params
);
5798 /* Add a terminating '/' to the directory name. */
5799 directory
= talloc_asprintf_append(directory
,
5803 return NT_STATUS_NO_MEMORY
;
5806 /* Ensure newname contains a '/' also */
5807 if(strrchr_m(newname
,'/') == 0) {
5808 newname
= talloc_asprintf(ctx
,
5812 return NT_STATUS_NO_MEMORY
;
5816 DEBUG(3, ("rename_internals: case_sensitive = %d, "
5817 "case_preserve = %d, short case preserve = %d, "
5818 "directory = %s, newname = %s, "
5819 "last_component_dest = %s, is_8_3 = %d\n",
5820 conn
->case_sensitive
, conn
->case_preserve
,
5821 conn
->short_case_preserve
, directory
,
5822 newname
, last_component_dest
, is_short_name
));
5824 /* The dest name still may have wildcards. */
5825 if (dest_has_wild
) {
5826 char *mod_newname
= NULL
;
5827 if (!resolve_wildcards(ctx
,
5828 directory
,newname
,&mod_newname
)) {
5829 DEBUG(6, ("rename_internals: resolve_wildcards "
5833 return NT_STATUS_NO_MEMORY
;
5835 newname
= mod_newname
;
5839 if (posix_pathnames
) {
5840 SMB_VFS_LSTAT(conn
, directory
, &sbuf1
);
5842 SMB_VFS_STAT(conn
, directory
, &sbuf1
);
5845 status
= S_ISDIR(sbuf1
.st_mode
) ?
5846 open_directory(conn
, req
, directory
, &sbuf1
,
5848 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
5851 posix_pathnames
? FILE_FLAG_POSIX_SEMANTICS
|0777 : 0,
5854 : open_file_ntcreate(conn
, req
, directory
, &sbuf1
,
5856 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
5859 posix_pathnames
? FILE_FLAG_POSIX_SEMANTICS
|0777 : 0,
5864 if (!NT_STATUS_IS_OK(status
)) {
5865 DEBUG(3, ("Could not open rename source %s: %s\n",
5866 directory
, nt_errstr(status
)));
5870 status
= rename_internals_fsp(conn
, fsp
, newname
,
5871 last_component_dest
,
5872 attrs
, replace_if_exists
);
5874 close_file(fsp
, NORMAL_CLOSE
);
5876 DEBUG(3, ("rename_internals: Error %s rename %s -> %s\n",
5877 nt_errstr(status
), directory
,newname
));
5883 * Wildcards - process each file that matches.
5885 if (strequal(mask
,"????????.???")) {
5890 status
= check_name(conn
, directory
);
5891 if (!NT_STATUS_IS_OK(status
)) {
5895 dir_hnd
= OpenDir(talloc_tos(), conn
, directory
, mask
, attrs
);
5896 if (dir_hnd
== NULL
) {
5897 return map_nt_error_from_unix(errno
);
5900 status
= NT_STATUS_NO_SUCH_FILE
;
5902 * Was status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5903 * - gentest fix. JRA
5906 while ((dname
= ReadDirName(dir_hnd
, &offset
))) {
5907 files_struct
*fsp
= NULL
;
5909 char *destname
= NULL
;
5910 bool sysdir_entry
= False
;
5912 /* Quick check for "." and ".." */
5913 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
5915 sysdir_entry
= True
;
5921 if (!is_visible_file(conn
, directory
, dname
, &sbuf1
, False
)) {
5925 if(!mask_match(dname
, mask
, conn
->case_sensitive
)) {
5930 status
= NT_STATUS_OBJECT_NAME_INVALID
;
5934 fname
= talloc_asprintf(ctx
,
5939 return NT_STATUS_NO_MEMORY
;
5942 if (!resolve_wildcards(ctx
,
5943 fname
,newname
,&destname
)) {
5944 DEBUG(6, ("resolve_wildcards %s %s failed\n",
5950 return NT_STATUS_NO_MEMORY
;
5954 if (posix_pathnames
) {
5955 SMB_VFS_LSTAT(conn
, fname
, &sbuf1
);
5957 SMB_VFS_STAT(conn
, fname
, &sbuf1
);
5960 status
= S_ISDIR(sbuf1
.st_mode
) ?
5961 open_directory(conn
, req
, fname
, &sbuf1
,
5963 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
5966 posix_pathnames
? FILE_FLAG_POSIX_SEMANTICS
|0777 : 0,
5969 : open_file_ntcreate(conn
, req
, fname
, &sbuf1
,
5971 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
5974 posix_pathnames
? FILE_FLAG_POSIX_SEMANTICS
|0777 : 0,
5979 if (!NT_STATUS_IS_OK(status
)) {
5980 DEBUG(3,("rename_internals: open_file_ntcreate "
5981 "returned %s rename %s -> %s\n",
5982 nt_errstr(status
), directory
, newname
));
5986 status
= rename_internals_fsp(conn
, fsp
, destname
, dname
,
5987 attrs
, replace_if_exists
);
5989 close_file(fsp
, NORMAL_CLOSE
);
5991 if (!NT_STATUS_IS_OK(status
)) {
5992 DEBUG(3, ("rename_internals_fsp returned %s for "
5993 "rename %s -> %s\n", nt_errstr(status
),
5994 directory
, newname
));
6000 DEBUG(3,("rename_internals: doing rename on %s -> "
6001 "%s\n",fname
,destname
));
6004 TALLOC_FREE(destname
);
6006 TALLOC_FREE(dir_hnd
);
6008 if (count
== 0 && NT_STATUS_IS_OK(status
) && errno
!= 0) {
6009 status
= map_nt_error_from_unix(errno
);
6015 /****************************************************************************
6017 ****************************************************************************/
6019 void reply_mv(struct smb_request
*req
)
6021 connection_struct
*conn
= req
->conn
;
6023 char *newname
= NULL
;
6027 bool src_has_wcard
= False
;
6028 bool dest_has_wcard
= False
;
6029 TALLOC_CTX
*ctx
= talloc_tos();
6031 START_PROFILE(SMBmv
);
6034 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
6039 attrs
= SVAL(req
->inbuf
,smb_vwv0
);
6041 p
= smb_buf(req
->inbuf
) + 1;
6042 p
+= srvstr_get_path_wcard(ctx
, (char *)req
->inbuf
, req
->flags2
, &name
, p
,
6043 0, STR_TERMINATE
, &status
,
6045 if (!NT_STATUS_IS_OK(status
)) {
6046 reply_nterror(req
, status
);
6051 p
+= srvstr_get_path_wcard(ctx
, (char *)req
->inbuf
, req
->flags2
, &newname
, p
,
6052 0, STR_TERMINATE
, &status
,
6054 if (!NT_STATUS_IS_OK(status
)) {
6055 reply_nterror(req
, status
);
6060 status
= resolve_dfspath_wcard(ctx
, conn
,
6061 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
6065 if (!NT_STATUS_IS_OK(status
)) {
6066 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
6067 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
6068 ERRSRV
, ERRbadpath
);
6072 reply_nterror(req
, status
);
6077 status
= resolve_dfspath_wcard(ctx
, conn
,
6078 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
6082 if (!NT_STATUS_IS_OK(status
)) {
6083 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
6084 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
6085 ERRSRV
, ERRbadpath
);
6089 reply_nterror(req
, status
);
6094 DEBUG(3,("reply_mv : %s -> %s\n",name
,newname
));
6096 status
= rename_internals(ctx
, conn
, req
, name
, newname
, attrs
, False
,
6097 src_has_wcard
, dest_has_wcard
, DELETE_ACCESS
);
6098 if (!NT_STATUS_IS_OK(status
)) {
6099 if (open_was_deferred(req
->mid
)) {
6100 /* We have re-scheduled this call. */
6104 reply_nterror(req
, status
);
6109 reply_outbuf(req
, 0, 0);
6115 /*******************************************************************
6116 Copy a file as part of a reply_copy.
6117 ******************************************************************/
6120 * TODO: check error codes on all callers
6123 NTSTATUS
copy_file(TALLOC_CTX
*ctx
,
6124 connection_struct
*conn
,
6129 bool target_is_directory
)
6131 SMB_STRUCT_STAT src_sbuf
, sbuf2
;
6133 files_struct
*fsp1
,*fsp2
;
6136 uint32 new_create_disposition
;
6139 dest
= talloc_strdup(ctx
, dest1
);
6141 return NT_STATUS_NO_MEMORY
;
6143 if (target_is_directory
) {
6144 const char *p
= strrchr_m(src
,'/');
6150 dest
= talloc_asprintf_append(dest
,
6154 return NT_STATUS_NO_MEMORY
;
6158 if (!vfs_file_exist(conn
,src
,&src_sbuf
)) {
6160 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
6163 if (!target_is_directory
&& count
) {
6164 new_create_disposition
= FILE_OPEN
;
6166 if (!map_open_params_to_ntcreate(dest1
,0,ofun
,
6167 NULL
, NULL
, &new_create_disposition
, NULL
)) {
6169 return NT_STATUS_INVALID_PARAMETER
;
6173 status
= open_file_ntcreate(conn
, NULL
, src
, &src_sbuf
,
6175 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
6178 FILE_ATTRIBUTE_NORMAL
,
6182 if (!NT_STATUS_IS_OK(status
)) {
6187 dosattrs
= dos_mode(conn
, src
, &src_sbuf
);
6188 if (SMB_VFS_STAT(conn
,dest
,&sbuf2
) == -1) {
6189 ZERO_STRUCTP(&sbuf2
);
6192 status
= open_file_ntcreate(conn
, NULL
, dest
, &sbuf2
,
6194 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
6195 new_create_disposition
,
6203 if (!NT_STATUS_IS_OK(status
)) {
6204 close_file(fsp1
,ERROR_CLOSE
);
6208 if ((ofun
&3) == 1) {
6209 if(SMB_VFS_LSEEK(fsp2
,0,SEEK_END
) == -1) {
6210 DEBUG(0,("copy_file: error - vfs lseek returned error %s\n", strerror(errno
) ));
6212 * Stop the copy from occurring.
6215 src_sbuf
.st_size
= 0;
6219 if (src_sbuf
.st_size
) {
6220 ret
= vfs_transfer_file(fsp1
, fsp2
, src_sbuf
.st_size
);
6223 close_file(fsp1
,NORMAL_CLOSE
);
6225 /* Ensure the modtime is set correctly on the destination file. */
6226 set_close_write_time(fsp2
, get_mtimespec(&src_sbuf
));
6229 * As we are opening fsp1 read-only we only expect
6230 * an error on close on fsp2 if we are out of space.
6231 * Thus we don't look at the error return from the
6234 status
= close_file(fsp2
,NORMAL_CLOSE
);
6236 if (!NT_STATUS_IS_OK(status
)) {
6240 if (ret
!= (SMB_OFF_T
)src_sbuf
.st_size
) {
6241 return NT_STATUS_DISK_FULL
;
6244 return NT_STATUS_OK
;
6247 /****************************************************************************
6248 Reply to a file copy.
6249 ****************************************************************************/
6251 void reply_copy(struct smb_request
*req
)
6253 connection_struct
*conn
= req
->conn
;
6255 char *newname
= NULL
;
6256 char *directory
= NULL
;
6260 int error
= ERRnoaccess
;
6265 bool target_is_directory
=False
;
6266 bool source_has_wild
= False
;
6267 bool dest_has_wild
= False
;
6268 SMB_STRUCT_STAT sbuf1
, sbuf2
;
6270 TALLOC_CTX
*ctx
= talloc_tos();
6272 START_PROFILE(SMBcopy
);
6275 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
6276 END_PROFILE(SMBcopy
);
6280 tid2
= SVAL(req
->inbuf
,smb_vwv0
);
6281 ofun
= SVAL(req
->inbuf
,smb_vwv1
);
6282 flags
= SVAL(req
->inbuf
,smb_vwv2
);
6284 p
= smb_buf(req
->inbuf
);
6285 p
+= srvstr_get_path_wcard(ctx
, (char *)req
->inbuf
, req
->flags2
, &name
, p
,
6286 0, STR_TERMINATE
, &status
,
6288 if (!NT_STATUS_IS_OK(status
)) {
6289 reply_nterror(req
, status
);
6290 END_PROFILE(SMBcopy
);
6293 p
+= srvstr_get_path_wcard(ctx
, (char *)req
->inbuf
, req
->flags2
, &newname
, p
,
6294 0, STR_TERMINATE
, &status
,
6296 if (!NT_STATUS_IS_OK(status
)) {
6297 reply_nterror(req
, status
);
6298 END_PROFILE(SMBcopy
);
6302 DEBUG(3,("reply_copy : %s -> %s\n",name
,newname
));
6304 if (tid2
!= conn
->cnum
) {
6305 /* can't currently handle inter share copies XXXX */
6306 DEBUG(3,("Rejecting inter-share copy\n"));
6307 reply_doserror(req
, ERRSRV
, ERRinvdevice
);
6308 END_PROFILE(SMBcopy
);
6312 status
= resolve_dfspath_wcard(ctx
, conn
,
6313 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
6317 if (!NT_STATUS_IS_OK(status
)) {
6318 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
6319 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
6320 ERRSRV
, ERRbadpath
);
6321 END_PROFILE(SMBcopy
);
6324 reply_nterror(req
, status
);
6325 END_PROFILE(SMBcopy
);
6329 status
= resolve_dfspath_wcard(ctx
, conn
,
6330 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
6334 if (!NT_STATUS_IS_OK(status
)) {
6335 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
6336 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
6337 ERRSRV
, ERRbadpath
);
6338 END_PROFILE(SMBcopy
);
6341 reply_nterror(req
, status
);
6342 END_PROFILE(SMBcopy
);
6346 status
= unix_convert(ctx
, conn
, name
, source_has_wild
,
6347 &name
, NULL
, &sbuf1
);
6348 if (!NT_STATUS_IS_OK(status
)) {
6349 reply_nterror(req
, status
);
6350 END_PROFILE(SMBcopy
);
6354 status
= unix_convert(ctx
, conn
, newname
, dest_has_wild
,
6355 &newname
, NULL
, &sbuf2
);
6356 if (!NT_STATUS_IS_OK(status
)) {
6357 reply_nterror(req
, status
);
6358 END_PROFILE(SMBcopy
);
6362 target_is_directory
= VALID_STAT_OF_DIR(sbuf2
);
6364 if ((flags
&1) && target_is_directory
) {
6365 reply_doserror(req
, ERRDOS
, ERRbadfile
);
6366 END_PROFILE(SMBcopy
);
6370 if ((flags
&2) && !target_is_directory
) {
6371 reply_doserror(req
, ERRDOS
, ERRbadpath
);
6372 END_PROFILE(SMBcopy
);
6376 if ((flags
&(1<<5)) && VALID_STAT_OF_DIR(sbuf1
)) {
6377 /* wants a tree copy! XXXX */
6378 DEBUG(3,("Rejecting tree copy\n"));
6379 reply_doserror(req
, ERRSRV
, ERRerror
);
6380 END_PROFILE(SMBcopy
);
6384 p
= strrchr_m(name
,'/');
6386 directory
= talloc_strdup(ctx
, "./");
6388 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
6389 END_PROFILE(SMBcopy
);
6395 directory
= talloc_strdup(ctx
, name
);
6397 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
6398 END_PROFILE(SMBcopy
);
6405 * We should only check the mangled cache
6406 * here if unix_convert failed. This means
6407 * that the path in 'mask' doesn't exist
6408 * on the file system and so we need to look
6409 * for a possible mangle. This patch from
6410 * Tine Smukavec <valentin.smukavec@hermes.si>.
6413 if (!VALID_STAT(sbuf1
) && mangle_is_mangled(mask
, conn
->params
)) {
6414 char *new_mask
= NULL
;
6415 mangle_lookup_name_from_8_3(ctx
,
6424 if (!source_has_wild
) {
6425 directory
= talloc_asprintf_append(directory
,
6428 if (dest_has_wild
) {
6429 char *mod_newname
= NULL
;
6430 if (!resolve_wildcards(ctx
,
6431 directory
,newname
,&mod_newname
)) {
6432 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
6433 END_PROFILE(SMBcopy
);
6436 newname
= mod_newname
;
6439 status
= check_name(conn
, directory
);
6440 if (!NT_STATUS_IS_OK(status
)) {
6441 reply_nterror(req
, status
);
6442 END_PROFILE(SMBcopy
);
6446 status
= check_name(conn
, newname
);
6447 if (!NT_STATUS_IS_OK(status
)) {
6448 reply_nterror(req
, status
);
6449 END_PROFILE(SMBcopy
);
6453 status
= copy_file(ctx
,conn
,directory
,newname
,ofun
,
6454 count
,target_is_directory
);
6456 if(!NT_STATUS_IS_OK(status
)) {
6457 reply_nterror(req
, status
);
6458 END_PROFILE(SMBcopy
);
6464 struct smb_Dir
*dir_hnd
= NULL
;
6465 const char *dname
= NULL
;
6468 if (strequal(mask
,"????????.???")) {
6473 status
= check_name(conn
, directory
);
6474 if (!NT_STATUS_IS_OK(status
)) {
6475 reply_nterror(req
, status
);
6476 END_PROFILE(SMBcopy
);
6480 dir_hnd
= OpenDir(talloc_tos(), conn
, directory
, mask
, 0);
6481 if (dir_hnd
== NULL
) {
6482 status
= map_nt_error_from_unix(errno
);
6483 reply_nterror(req
, status
);
6484 END_PROFILE(SMBcopy
);
6490 while ((dname
= ReadDirName(dir_hnd
, &offset
))) {
6491 char *destname
= NULL
;
6494 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
6498 if (!is_visible_file(conn
, directory
, dname
, &sbuf1
, False
)) {
6502 if(!mask_match(dname
, mask
, conn
->case_sensitive
)) {
6506 error
= ERRnoaccess
;
6507 fname
= talloc_asprintf(ctx
,
6512 TALLOC_FREE(dir_hnd
);
6513 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
6514 END_PROFILE(SMBcopy
);
6518 if (!resolve_wildcards(ctx
,
6519 fname
,newname
,&destname
)) {
6523 TALLOC_FREE(dir_hnd
);
6524 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
6525 END_PROFILE(SMBcopy
);
6529 status
= check_name(conn
, fname
);
6530 if (!NT_STATUS_IS_OK(status
)) {
6531 TALLOC_FREE(dir_hnd
);
6532 reply_nterror(req
, status
);
6533 END_PROFILE(SMBcopy
);
6537 status
= check_name(conn
, destname
);
6538 if (!NT_STATUS_IS_OK(status
)) {
6539 TALLOC_FREE(dir_hnd
);
6540 reply_nterror(req
, status
);
6541 END_PROFILE(SMBcopy
);
6545 DEBUG(3,("reply_copy : doing copy on %s -> %s\n",fname
, destname
));
6547 status
= copy_file(ctx
,conn
,fname
,destname
,ofun
,
6548 count
,target_is_directory
);
6549 if (NT_STATUS_IS_OK(status
)) {
6553 TALLOC_FREE(destname
);
6555 TALLOC_FREE(dir_hnd
);
6560 /* Error on close... */
6562 reply_unixerror(req
, ERRHRD
, ERRgeneral
);
6563 END_PROFILE(SMBcopy
);
6567 reply_doserror(req
, ERRDOS
, error
);
6568 END_PROFILE(SMBcopy
);
6572 reply_outbuf(req
, 1, 0);
6573 SSVAL(req
->outbuf
,smb_vwv0
,count
);
6575 END_PROFILE(SMBcopy
);
6580 #define DBGC_CLASS DBGC_LOCKING
6582 /****************************************************************************
6583 Get a lock pid, dealing with large count requests.
6584 ****************************************************************************/
6586 uint32
get_lock_pid( char *data
, int data_offset
, bool large_file_format
)
6588 if(!large_file_format
)
6589 return (uint32
)SVAL(data
,SMB_LPID_OFFSET(data_offset
));
6591 return (uint32
)SVAL(data
,SMB_LARGE_LPID_OFFSET(data_offset
));
6594 /****************************************************************************
6595 Get a lock count, dealing with large count requests.
6596 ****************************************************************************/
6598 SMB_BIG_UINT
get_lock_count( char *data
, int data_offset
, bool large_file_format
)
6600 SMB_BIG_UINT count
= 0;
6602 if(!large_file_format
) {
6603 count
= (SMB_BIG_UINT
)IVAL(data
,SMB_LKLEN_OFFSET(data_offset
));
6606 #if defined(HAVE_LONGLONG)
6607 count
= (((SMB_BIG_UINT
) IVAL(data
,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset
))) << 32) |
6608 ((SMB_BIG_UINT
) IVAL(data
,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset
)));
6609 #else /* HAVE_LONGLONG */
6612 * NT4.x seems to be broken in that it sends large file (64 bit)
6613 * lockingX calls even if the CAP_LARGE_FILES was *not*
6614 * negotiated. For boxes without large unsigned ints truncate the
6615 * lock count by dropping the top 32 bits.
6618 if(IVAL(data
,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset
)) != 0) {
6619 DEBUG(3,("get_lock_count: truncating lock count (high)0x%x (low)0x%x to just low count.\n",
6620 (unsigned int)IVAL(data
,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset
)),
6621 (unsigned int)IVAL(data
,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset
)) ));
6622 SIVAL(data
,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset
),0);
6625 count
= (SMB_BIG_UINT
)IVAL(data
,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset
));
6626 #endif /* HAVE_LONGLONG */
6632 #if !defined(HAVE_LONGLONG)
6633 /****************************************************************************
6634 Pathetically try and map a 64 bit lock offset into 31 bits. I hate Windows :-).
6635 ****************************************************************************/
6637 static uint32
map_lock_offset(uint32 high
, uint32 low
)
6641 uint32 highcopy
= high
;
6644 * Try and find out how many significant bits there are in high.
6647 for(i
= 0; highcopy
; i
++)
6651 * We use 31 bits not 32 here as POSIX
6652 * lock offsets may not be negative.
6655 mask
= (~0) << (31 - i
);
6658 return 0; /* Fail. */
6664 #endif /* !defined(HAVE_LONGLONG) */
6666 /****************************************************************************
6667 Get a lock offset, dealing with large offset requests.
6668 ****************************************************************************/
6670 SMB_BIG_UINT
get_lock_offset( char *data
, int data_offset
, bool large_file_format
, bool *err
)
6672 SMB_BIG_UINT offset
= 0;
6676 if(!large_file_format
) {
6677 offset
= (SMB_BIG_UINT
)IVAL(data
,SMB_LKOFF_OFFSET(data_offset
));
6680 #if defined(HAVE_LONGLONG)
6681 offset
= (((SMB_BIG_UINT
) IVAL(data
,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset
))) << 32) |
6682 ((SMB_BIG_UINT
) IVAL(data
,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset
)));
6683 #else /* HAVE_LONGLONG */
6686 * NT4.x seems to be broken in that it sends large file (64 bit)
6687 * lockingX calls even if the CAP_LARGE_FILES was *not*
6688 * negotiated. For boxes without large unsigned ints mangle the
6689 * lock offset by mapping the top 32 bits onto the lower 32.
6692 if(IVAL(data
,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset
)) != 0) {
6693 uint32 low
= IVAL(data
,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset
));
6694 uint32 high
= IVAL(data
,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset
));
6697 if((new_low
= map_lock_offset(high
, low
)) == 0) {
6699 return (SMB_BIG_UINT
)-1;
6702 DEBUG(3,("get_lock_offset: truncating lock offset (high)0x%x (low)0x%x to offset 0x%x.\n",
6703 (unsigned int)high
, (unsigned int)low
, (unsigned int)new_low
));
6704 SIVAL(data
,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset
),0);
6705 SIVAL(data
,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset
),new_low
);
6708 offset
= (SMB_BIG_UINT
)IVAL(data
,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset
));
6709 #endif /* HAVE_LONGLONG */
6715 /****************************************************************************
6716 Reply to a lockingX request.
6717 ****************************************************************************/
6719 void reply_lockingX(struct smb_request
*req
)
6721 connection_struct
*conn
= req
->conn
;
6723 unsigned char locktype
;
6724 unsigned char oplocklevel
;
6727 SMB_BIG_UINT count
= 0, offset
= 0;
6732 bool large_file_format
;
6734 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
6736 START_PROFILE(SMBlockingX
);
6739 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
6740 END_PROFILE(SMBlockingX
);
6744 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv2
));
6745 locktype
= CVAL(req
->inbuf
,smb_vwv3
);
6746 oplocklevel
= CVAL(req
->inbuf
,smb_vwv3
+1);
6747 num_ulocks
= SVAL(req
->inbuf
,smb_vwv6
);
6748 num_locks
= SVAL(req
->inbuf
,smb_vwv7
);
6749 lock_timeout
= IVAL(req
->inbuf
,smb_vwv4
);
6750 large_file_format
= (locktype
& LOCKING_ANDX_LARGE_FILES
)?True
:False
;
6752 if (!check_fsp(conn
, req
, fsp
)) {
6753 END_PROFILE(SMBlockingX
);
6757 data
= smb_buf(req
->inbuf
);
6759 if (locktype
& LOCKING_ANDX_CHANGE_LOCKTYPE
) {
6760 /* we don't support these - and CANCEL_LOCK makes w2k
6761 and XP reboot so I don't really want to be
6762 compatible! (tridge) */
6763 reply_nterror(req
, NT_STATUS_DOS(ERRDOS
, ERRnoatomiclocks
));
6764 END_PROFILE(SMBlockingX
);
6768 /* Check if this is an oplock break on a file
6769 we have granted an oplock on.
6771 if ((locktype
& LOCKING_ANDX_OPLOCK_RELEASE
)) {
6772 /* Client can insist on breaking to none. */
6773 bool break_to_none
= (oplocklevel
== 0);
6776 DEBUG(5,("reply_lockingX: oplock break reply (%u) from client "
6777 "for fnum = %d\n", (unsigned int)oplocklevel
,
6781 * Make sure we have granted an exclusive or batch oplock on
6785 if (fsp
->oplock_type
== 0) {
6787 /* The Samba4 nbench simulator doesn't understand
6788 the difference between break to level2 and break
6789 to none from level2 - it sends oplock break
6790 replies in both cases. Don't keep logging an error
6791 message here - just ignore it. JRA. */
6793 DEBUG(5,("reply_lockingX: Error : oplock break from "
6794 "client for fnum = %d (oplock=%d) and no "
6795 "oplock granted on this file (%s).\n",
6796 fsp
->fnum
, fsp
->oplock_type
, fsp
->fsp_name
));
6798 /* if this is a pure oplock break request then don't
6800 if (num_locks
== 0 && num_ulocks
== 0) {
6801 END_PROFILE(SMBlockingX
);
6804 END_PROFILE(SMBlockingX
);
6805 reply_doserror(req
, ERRDOS
, ERRlock
);
6810 if ((fsp
->sent_oplock_break
== BREAK_TO_NONE_SENT
) ||
6812 result
= remove_oplock(fsp
);
6814 result
= downgrade_oplock(fsp
);
6818 DEBUG(0, ("reply_lockingX: error in removing "
6819 "oplock on file %s\n", fsp
->fsp_name
));
6820 /* Hmmm. Is this panic justified? */
6821 smb_panic("internal tdb error");
6824 reply_to_oplock_break_requests(fsp
);
6826 /* if this is a pure oplock break request then don't send a
6828 if (num_locks
== 0 && num_ulocks
== 0) {
6829 /* Sanity check - ensure a pure oplock break is not a
6831 if(CVAL(req
->inbuf
,smb_vwv0
) != 0xff)
6832 DEBUG(0,("reply_lockingX: Error : pure oplock "
6833 "break is a chained %d request !\n",
6834 (unsigned int)CVAL(req
->inbuf
,
6836 END_PROFILE(SMBlockingX
);
6842 * We do this check *after* we have checked this is not a oplock break
6843 * response message. JRA.
6846 release_level_2_oplocks_on_change(fsp
);
6848 if (smb_buflen(req
->inbuf
) <
6849 (num_ulocks
+ num_locks
) * (large_file_format
? 20 : 10)) {
6850 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
6851 END_PROFILE(SMBlockingX
);
6855 /* Data now points at the beginning of the list
6856 of smb_unlkrng structs */
6857 for(i
= 0; i
< (int)num_ulocks
; i
++) {
6858 lock_pid
= get_lock_pid( data
, i
, large_file_format
);
6859 count
= get_lock_count( data
, i
, large_file_format
);
6860 offset
= get_lock_offset( data
, i
, large_file_format
, &err
);
6863 * There is no error code marked "stupid client bug".... :-).
6866 END_PROFILE(SMBlockingX
);
6867 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
6871 DEBUG(10,("reply_lockingX: unlock start=%.0f, len=%.0f for "
6872 "pid %u, file %s\n", (double)offset
, (double)count
,
6873 (unsigned int)lock_pid
, fsp
->fsp_name
));
6875 status
= do_unlock(smbd_messaging_context(),
6882 if (NT_STATUS_V(status
)) {
6883 END_PROFILE(SMBlockingX
);
6884 reply_nterror(req
, status
);
6889 /* Setup the timeout in seconds. */
6891 if (!lp_blocking_locks(SNUM(conn
))) {
6895 /* Now do any requested locks */
6896 data
+= ((large_file_format
? 20 : 10)*num_ulocks
);
6898 /* Data now points at the beginning of the list
6899 of smb_lkrng structs */
6901 for(i
= 0; i
< (int)num_locks
; i
++) {
6902 enum brl_type lock_type
= ((locktype
& LOCKING_ANDX_SHARED_LOCK
) ?
6903 READ_LOCK
:WRITE_LOCK
);
6904 lock_pid
= get_lock_pid( data
, i
, large_file_format
);
6905 count
= get_lock_count( data
, i
, large_file_format
);
6906 offset
= get_lock_offset( data
, i
, large_file_format
, &err
);
6909 * There is no error code marked "stupid client bug".... :-).
6912 END_PROFILE(SMBlockingX
);
6913 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
6917 DEBUG(10,("reply_lockingX: lock start=%.0f, len=%.0f for pid "
6918 "%u, file %s timeout = %d\n", (double)offset
,
6919 (double)count
, (unsigned int)lock_pid
,
6920 fsp
->fsp_name
, (int)lock_timeout
));
6922 if (locktype
& LOCKING_ANDX_CANCEL_LOCK
) {
6923 if (lp_blocking_locks(SNUM(conn
))) {
6925 /* Schedule a message to ourselves to
6926 remove the blocking lock record and
6927 return the right error. */
6929 if (!blocking_lock_cancel(fsp
,
6935 NT_STATUS_FILE_LOCK_CONFLICT
)) {
6936 END_PROFILE(SMBlockingX
);
6941 ERRcancelviolation
));
6945 /* Remove a matching pending lock. */
6946 status
= do_lock_cancel(fsp
,
6952 bool blocking_lock
= lock_timeout
? True
: False
;
6953 bool defer_lock
= False
;
6954 struct byte_range_lock
*br_lck
;
6955 uint32 block_smbpid
;
6957 br_lck
= do_lock(smbd_messaging_context(),
6968 if (br_lck
&& blocking_lock
&& ERROR_WAS_LOCK_DENIED(status
)) {
6969 /* Windows internal resolution for blocking locks seems
6970 to be about 200ms... Don't wait for less than that. JRA. */
6971 if (lock_timeout
!= -1 && lock_timeout
< lp_lock_spin_time()) {
6972 lock_timeout
= lp_lock_spin_time();
6977 /* This heuristic seems to match W2K3 very well. If a
6978 lock sent with timeout of zero would fail with NT_STATUS_FILE_LOCK_CONFLICT
6979 it pretends we asked for a timeout of between 150 - 300 milliseconds as
6980 far as I can tell. Replacement for do_lock_spin(). JRA. */
6982 if (br_lck
&& lp_blocking_locks(SNUM(conn
)) && !blocking_lock
&&
6983 NT_STATUS_EQUAL((status
), NT_STATUS_FILE_LOCK_CONFLICT
)) {
6985 lock_timeout
= lp_lock_spin_time();
6988 if (br_lck
&& defer_lock
) {
6990 * A blocking lock was requested. Package up
6991 * this smb into a queued request and push it
6992 * onto the blocking lock queue.
6994 if(push_blocking_lock_request(br_lck
,
7005 TALLOC_FREE(br_lck
);
7006 END_PROFILE(SMBlockingX
);
7011 TALLOC_FREE(br_lck
);
7014 if (NT_STATUS_V(status
)) {
7015 END_PROFILE(SMBlockingX
);
7016 reply_nterror(req
, status
);
7021 /* If any of the above locks failed, then we must unlock
7022 all of the previous locks (X/Open spec). */
7024 if (!(locktype
& LOCKING_ANDX_CANCEL_LOCK
) &&
7028 * Ensure we don't do a remove on the lock that just failed,
7029 * as under POSIX rules, if we have a lock already there, we
7030 * will delete it (and we shouldn't) .....
7032 for(i
--; i
>= 0; i
--) {
7033 lock_pid
= get_lock_pid( data
, i
, large_file_format
);
7034 count
= get_lock_count( data
, i
, large_file_format
);
7035 offset
= get_lock_offset( data
, i
, large_file_format
,
7039 * There is no error code marked "stupid client
7043 END_PROFILE(SMBlockingX
);
7044 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
7048 do_unlock(smbd_messaging_context(),
7055 END_PROFILE(SMBlockingX
);
7056 reply_nterror(req
, status
);
7060 reply_outbuf(req
, 2, 0);
7062 DEBUG(3, ("lockingX fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
7063 fsp
->fnum
, (unsigned int)locktype
, num_locks
, num_ulocks
));
7065 END_PROFILE(SMBlockingX
);
7070 #define DBGC_CLASS DBGC_ALL
7072 /****************************************************************************
7073 Reply to a SMBreadbmpx (read block multiplex) request.
7074 Always reply with an error, if someone has a platform really needs this,
7075 please contact vl@samba.org
7076 ****************************************************************************/
7078 void reply_readbmpx(struct smb_request
*req
)
7080 START_PROFILE(SMBreadBmpx
);
7081 reply_doserror(req
, ERRSRV
, ERRuseSTD
);
7082 END_PROFILE(SMBreadBmpx
);
7086 /****************************************************************************
7087 Reply to a SMBreadbs (read block multiplex secondary) request.
7088 Always reply with an error, if someone has a platform really needs this,
7089 please contact vl@samba.org
7090 ****************************************************************************/
7092 void reply_readbs(struct smb_request
*req
)
7094 START_PROFILE(SMBreadBs
);
7095 reply_doserror(req
, ERRSRV
, ERRuseSTD
);
7096 END_PROFILE(SMBreadBs
);
7100 /****************************************************************************
7101 Reply to a SMBsetattrE.
7102 ****************************************************************************/
7104 void reply_setattrE(struct smb_request
*req
)
7106 connection_struct
*conn
= req
->conn
;
7107 struct timespec ts
[2];
7109 SMB_STRUCT_STAT sbuf
;
7112 START_PROFILE(SMBsetattrE
);
7115 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
7116 END_PROFILE(SMBsetattrE
);
7120 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
7122 if(!fsp
|| (fsp
->conn
!= conn
)) {
7123 reply_doserror(req
, ERRDOS
, ERRbadfid
);
7124 END_PROFILE(SMBsetattrE
);
7130 * Convert the DOS times into unix times. Ignore create
7131 * time as UNIX can't set this.
7134 ts
[0] = convert_time_t_to_timespec(
7135 srv_make_unix_date2(req
->inbuf
+smb_vwv3
)); /* atime. */
7136 ts
[1] = convert_time_t_to_timespec(
7137 srv_make_unix_date2(req
->inbuf
+smb_vwv5
)); /* mtime. */
7139 reply_outbuf(req
, 0, 0);
7142 * Patch from Ray Frush <frush@engr.colostate.edu>
7143 * Sometimes times are sent as zero - ignore them.
7146 /* Ensure we have a valid stat struct for the source. */
7147 if (fsp
->fh
->fd
!= -1) {
7148 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
7149 status
= map_nt_error_from_unix(errno
);
7150 reply_nterror(req
, status
);
7151 END_PROFILE(SMBsetattrE
);
7157 if (fsp
->posix_open
) {
7158 ret
= SMB_VFS_LSTAT(conn
, fsp
->fsp_name
, &sbuf
);
7160 ret
= SMB_VFS_STAT(conn
, fsp
->fsp_name
, &sbuf
);
7163 status
= map_nt_error_from_unix(errno
);
7164 reply_nterror(req
, status
);
7165 END_PROFILE(SMBsetattrE
);
7170 status
= smb_set_file_time(conn
, fsp
, fsp
->fsp_name
,
7172 if (!NT_STATUS_IS_OK(status
)) {
7173 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
7174 END_PROFILE(SMBsetattrE
);
7178 DEBUG( 3, ( "reply_setattrE fnum=%d actime=%u modtime=%u\n",
7180 (unsigned int)ts
[0].tv_sec
,
7181 (unsigned int)ts
[1].tv_sec
));
7183 END_PROFILE(SMBsetattrE
);
7188 /* Back from the dead for OS/2..... JRA. */
7190 /****************************************************************************
7191 Reply to a SMBwritebmpx (write block multiplex primary) request.
7192 Always reply with an error, if someone has a platform really needs this,
7193 please contact vl@samba.org
7194 ****************************************************************************/
7196 void reply_writebmpx(struct smb_request
*req
)
7198 START_PROFILE(SMBwriteBmpx
);
7199 reply_doserror(req
, ERRSRV
, ERRuseSTD
);
7200 END_PROFILE(SMBwriteBmpx
);
7204 /****************************************************************************
7205 Reply to a SMBwritebs (write block multiplex secondary) request.
7206 Always reply with an error, if someone has a platform really needs this,
7207 please contact vl@samba.org
7208 ****************************************************************************/
7210 void reply_writebs(struct smb_request
*req
)
7212 START_PROFILE(SMBwriteBs
);
7213 reply_doserror(req
, ERRSRV
, ERRuseSTD
);
7214 END_PROFILE(SMBwriteBs
);
7218 /****************************************************************************
7219 Reply to a SMBgetattrE.
7220 ****************************************************************************/
7222 void reply_getattrE(struct smb_request
*req
)
7224 connection_struct
*conn
= req
->conn
;
7225 SMB_STRUCT_STAT sbuf
;
7228 struct timespec create_ts
;
7230 START_PROFILE(SMBgetattrE
);
7233 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
7234 END_PROFILE(SMBgetattrE
);
7238 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
7240 if(!fsp
|| (fsp
->conn
!= conn
)) {
7241 reply_doserror(req
, ERRDOS
, ERRbadfid
);
7242 END_PROFILE(SMBgetattrE
);
7246 /* Do an fstat on this file */
7247 if(fsp_stat(fsp
, &sbuf
)) {
7248 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
7249 END_PROFILE(SMBgetattrE
);
7253 mode
= dos_mode(conn
,fsp
->fsp_name
,&sbuf
);
7256 * Convert the times into dos times. Set create
7257 * date to be last modify date as UNIX doesn't save
7261 reply_outbuf(req
, 11, 0);
7263 create_ts
= get_create_timespec(&sbuf
,
7264 lp_fake_dir_create_times(SNUM(conn
)));
7265 srv_put_dos_date2((char *)req
->outbuf
, smb_vwv0
, create_ts
.tv_sec
);
7266 srv_put_dos_date2((char *)req
->outbuf
, smb_vwv2
, sbuf
.st_atime
);
7267 /* Should we check pending modtime here ? JRA */
7268 srv_put_dos_date2((char *)req
->outbuf
, smb_vwv4
, sbuf
.st_mtime
);
7271 SIVAL(req
->outbuf
, smb_vwv6
, 0);
7272 SIVAL(req
->outbuf
, smb_vwv8
, 0);
7274 uint32 allocation_size
= get_allocation_size(conn
,fsp
, &sbuf
);
7275 SIVAL(req
->outbuf
, smb_vwv6
, (uint32
)sbuf
.st_size
);
7276 SIVAL(req
->outbuf
, smb_vwv8
, allocation_size
);
7278 SSVAL(req
->outbuf
,smb_vwv10
, mode
);
7280 DEBUG( 3, ( "reply_getattrE fnum=%d\n", fsp
->fnum
));
7282 END_PROFILE(SMBgetattrE
);