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 if (fsp
->posix_open
) {
2267 return NT_STATUS_OK
;
2270 /* If no pathnames are open below this
2271 directory, allow the rename. */
2273 if (file_find_subpath(fsp
)) {
2274 return NT_STATUS_ACCESS_DENIED
;
2276 return NT_STATUS_OK
;
2279 if (fsp
->access_mask
& (DELETE_ACCESS
|FILE_WRITE_ATTRIBUTES
)) {
2280 return NT_STATUS_OK
;
2283 return NT_STATUS_ACCESS_DENIED
;
2286 /*******************************************************************
2287 * unlink a file with all relevant access checks
2288 *******************************************************************/
2290 static NTSTATUS
do_unlink(connection_struct
*conn
,
2291 struct smb_request
*req
,
2295 SMB_STRUCT_STAT sbuf
;
2298 uint32 dirtype_orig
= dirtype
;
2301 DEBUG(10,("do_unlink: %s, dirtype = %d\n", fname
, dirtype
));
2303 if (!CAN_WRITE(conn
)) {
2304 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
2307 if (SMB_VFS_LSTAT(conn
,fname
,&sbuf
) != 0) {
2308 return map_nt_error_from_unix(errno
);
2311 fattr
= dos_mode(conn
,fname
,&sbuf
);
2313 if (dirtype
& FILE_ATTRIBUTE_NORMAL
) {
2314 dirtype
= aDIR
|aARCH
|aRONLY
;
2317 dirtype
&= (aDIR
|aARCH
|aRONLY
|aHIDDEN
|aSYSTEM
);
2319 return NT_STATUS_NO_SUCH_FILE
;
2322 if (!dir_check_ftype(conn
, fattr
, dirtype
)) {
2324 return NT_STATUS_FILE_IS_A_DIRECTORY
;
2326 return NT_STATUS_NO_SUCH_FILE
;
2329 if (dirtype_orig
& 0x8000) {
2330 /* These will never be set for POSIX. */
2331 return NT_STATUS_NO_SUCH_FILE
;
2335 if ((fattr
& dirtype
) & FILE_ATTRIBUTE_DIRECTORY
) {
2336 return NT_STATUS_FILE_IS_A_DIRECTORY
;
2339 if ((fattr
& ~dirtype
) & (FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
)) {
2340 return NT_STATUS_NO_SUCH_FILE
;
2343 if (dirtype
& 0xFF00) {
2344 /* These will never be set for POSIX. */
2345 return NT_STATUS_NO_SUCH_FILE
;
2350 return NT_STATUS_NO_SUCH_FILE
;
2353 /* Can't delete a directory. */
2355 return NT_STATUS_FILE_IS_A_DIRECTORY
;
2360 else if (dirtype
& aDIR
) /* Asked for a directory and it isn't. */
2361 return NT_STATUS_OBJECT_NAME_INVALID
;
2362 #endif /* JRATEST */
2364 /* Fix for bug #3035 from SATOH Fumiyasu <fumiyas@miraclelinux.com>
2366 On a Windows share, a file with read-only dosmode can be opened with
2367 DELETE_ACCESS. But on a Samba share (delete readonly = no), it
2368 fails with NT_STATUS_CANNOT_DELETE error.
2370 This semantic causes a problem that a user can not
2371 rename a file with read-only dosmode on a Samba share
2372 from a Windows command prompt (i.e. cmd.exe, but can rename
2373 from Windows Explorer).
2376 if (!lp_delete_readonly(SNUM(conn
))) {
2377 if (fattr
& aRONLY
) {
2378 return NT_STATUS_CANNOT_DELETE
;
2382 /* On open checks the open itself will check the share mode, so
2383 don't do it here as we'll get it wrong. */
2385 status
= create_file_unixpath
2389 DELETE_ACCESS
, /* access_mask */
2390 FILE_SHARE_NONE
, /* share_access */
2391 FILE_OPEN
, /* create_disposition*/
2392 FILE_NON_DIRECTORY_FILE
, /* create_options */
2393 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
2394 0, /* oplock_request */
2395 0, /* allocation_size */
2402 if (!NT_STATUS_IS_OK(status
)) {
2403 DEBUG(10, ("create_file_unixpath failed: %s\n",
2404 nt_errstr(status
)));
2408 /* The set is across all open files on this dev/inode pair. */
2409 if (!set_delete_on_close(fsp
, True
, &conn
->server_info
->utok
)) {
2410 close_file(fsp
, NORMAL_CLOSE
);
2411 return NT_STATUS_ACCESS_DENIED
;
2414 return close_file(fsp
,NORMAL_CLOSE
);
2417 /****************************************************************************
2418 The guts of the unlink command, split out so it may be called by the NT SMB
2420 ****************************************************************************/
2422 NTSTATUS
unlink_internals(connection_struct
*conn
, struct smb_request
*req
,
2423 uint32 dirtype
, const char *name_in
, bool has_wild
)
2425 const char *directory
= NULL
;
2430 NTSTATUS status
= NT_STATUS_OK
;
2431 SMB_STRUCT_STAT sbuf
;
2432 TALLOC_CTX
*ctx
= talloc_tos();
2434 status
= unix_convert(ctx
, conn
, name_in
, has_wild
, &name
, NULL
, &sbuf
);
2435 if (!NT_STATUS_IS_OK(status
)) {
2439 p
= strrchr_m(name
,'/');
2441 directory
= talloc_strdup(ctx
, ".");
2443 return NT_STATUS_NO_MEMORY
;
2453 * We should only check the mangled cache
2454 * here if unix_convert failed. This means
2455 * that the path in 'mask' doesn't exist
2456 * on the file system and so we need to look
2457 * for a possible mangle. This patch from
2458 * Tine Smukavec <valentin.smukavec@hermes.si>.
2461 if (!VALID_STAT(sbuf
) && mangle_is_mangled(mask
,conn
->params
)) {
2462 char *new_mask
= NULL
;
2463 mangle_lookup_name_from_8_3(ctx
,
2473 directory
= talloc_asprintf(ctx
,
2478 return NT_STATUS_NO_MEMORY
;
2481 dirtype
= FILE_ATTRIBUTE_NORMAL
;
2484 status
= check_name(conn
, directory
);
2485 if (!NT_STATUS_IS_OK(status
)) {
2489 status
= do_unlink(conn
, req
, directory
, dirtype
);
2490 if (!NT_STATUS_IS_OK(status
)) {
2496 struct smb_Dir
*dir_hnd
= NULL
;
2500 if ((dirtype
& SAMBA_ATTRIBUTES_MASK
) == aDIR
) {
2501 return NT_STATUS_OBJECT_NAME_INVALID
;
2504 if (strequal(mask
,"????????.???")) {
2509 status
= check_name(conn
, directory
);
2510 if (!NT_STATUS_IS_OK(status
)) {
2514 dir_hnd
= OpenDir(talloc_tos(), conn
, directory
, mask
,
2516 if (dir_hnd
== NULL
) {
2517 return map_nt_error_from_unix(errno
);
2520 /* XXXX the CIFS spec says that if bit0 of the flags2 field is set then
2521 the pattern matches against the long name, otherwise the short name
2522 We don't implement this yet XXXX
2525 status
= NT_STATUS_NO_SUCH_FILE
;
2527 while ((dname
= ReadDirName(dir_hnd
, &offset
))) {
2531 if (!is_visible_file(conn
, directory
, dname
, &st
, True
)) {
2535 /* Quick check for "." and ".." */
2536 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
2540 if(!mask_match(dname
, mask
, conn
->case_sensitive
)) {
2544 fname
= talloc_asprintf(ctx
, "%s/%s",
2548 return NT_STATUS_NO_MEMORY
;
2551 status
= check_name(conn
, fname
);
2552 if (!NT_STATUS_IS_OK(status
)) {
2553 TALLOC_FREE(dir_hnd
);
2557 status
= do_unlink(conn
, req
, fname
, dirtype
);
2558 if (!NT_STATUS_IS_OK(status
)) {
2564 DEBUG(3,("unlink_internals: successful unlink [%s]\n",
2569 TALLOC_FREE(dir_hnd
);
2572 if (count
== 0 && NT_STATUS_IS_OK(status
) && errno
!= 0) {
2573 status
= map_nt_error_from_unix(errno
);
2579 /****************************************************************************
2581 ****************************************************************************/
2583 void reply_unlink(struct smb_request
*req
)
2585 connection_struct
*conn
= req
->conn
;
2589 bool path_contains_wcard
= False
;
2590 TALLOC_CTX
*ctx
= talloc_tos();
2592 START_PROFILE(SMBunlink
);
2595 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2596 END_PROFILE(SMBunlink
);
2600 dirtype
= SVAL(req
->inbuf
,smb_vwv0
);
2602 srvstr_get_path_wcard(ctx
, (char *)req
->inbuf
, req
->flags2
, &name
,
2603 smb_buf(req
->inbuf
) + 1, 0,
2604 STR_TERMINATE
, &status
, &path_contains_wcard
);
2605 if (!NT_STATUS_IS_OK(status
)) {
2606 reply_nterror(req
, status
);
2607 END_PROFILE(SMBunlink
);
2611 status
= resolve_dfspath_wcard(ctx
, conn
,
2612 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
2615 &path_contains_wcard
);
2616 if (!NT_STATUS_IS_OK(status
)) {
2617 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
2618 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
2619 ERRSRV
, ERRbadpath
);
2620 END_PROFILE(SMBunlink
);
2623 reply_nterror(req
, status
);
2624 END_PROFILE(SMBunlink
);
2628 DEBUG(3,("reply_unlink : %s\n",name
));
2630 status
= unlink_internals(conn
, req
, dirtype
, name
,
2631 path_contains_wcard
);
2632 if (!NT_STATUS_IS_OK(status
)) {
2633 if (open_was_deferred(req
->mid
)) {
2634 /* We have re-scheduled this call. */
2635 END_PROFILE(SMBunlink
);
2638 reply_nterror(req
, status
);
2639 END_PROFILE(SMBunlink
);
2643 reply_outbuf(req
, 0, 0);
2644 END_PROFILE(SMBunlink
);
2649 /****************************************************************************
2651 ****************************************************************************/
2653 static void fail_readraw(void)
2655 const char *errstr
= talloc_asprintf(talloc_tos(),
2656 "FAIL ! reply_readbraw: socket write fail (%s)",
2661 exit_server_cleanly(errstr
);
2664 /****************************************************************************
2665 Fake (read/write) sendfile. Returns -1 on read or write fail.
2666 ****************************************************************************/
2668 static ssize_t
fake_sendfile(files_struct
*fsp
, SMB_OFF_T startpos
,
2672 size_t tosend
= nread
;
2679 bufsize
= MIN(nread
, 65536);
2681 if (!(buf
= SMB_MALLOC_ARRAY(char, bufsize
))) {
2685 while (tosend
> 0) {
2689 if (tosend
> bufsize
) {
2694 ret
= read_file(fsp
,buf
,startpos
,cur_read
);
2700 /* If we had a short read, fill with zeros. */
2701 if (ret
< cur_read
) {
2702 memset(buf
, '\0', cur_read
- ret
);
2705 if (write_data(smbd_server_fd(),buf
,cur_read
) != cur_read
) {
2710 startpos
+= cur_read
;
2714 return (ssize_t
)nread
;
2717 /****************************************************************************
2718 Return a readbraw error (4 bytes of zero).
2719 ****************************************************************************/
2721 static void reply_readbraw_error(void)
2725 if (write_data(smbd_server_fd(),header
,4) != 4) {
2730 /****************************************************************************
2731 Use sendfile in readbraw.
2732 ****************************************************************************/
2734 void send_file_readbraw(connection_struct
*conn
,
2740 char *outbuf
= NULL
;
2743 #if defined(WITH_SENDFILE)
2745 * We can only use sendfile on a non-chained packet
2746 * but we can use on a non-oplocked file. tridge proved this
2747 * on a train in Germany :-). JRA.
2748 * reply_readbraw has already checked the length.
2751 if ( (chain_size
== 0) && (nread
> 0) && (fsp
->base_fsp
== NULL
) &&
2752 (fsp
->wcp
== NULL
) && lp_use_sendfile(SNUM(conn
)) ) {
2754 DATA_BLOB header_blob
;
2756 _smb_setlen(header
,nread
);
2757 header_blob
= data_blob_const(header
, 4);
2759 if (SMB_VFS_SENDFILE(smbd_server_fd(), fsp
,
2760 &header_blob
, startpos
, nread
) == -1) {
2761 /* Returning ENOSYS means no data at all was sent.
2762 * Do this as a normal read. */
2763 if (errno
== ENOSYS
) {
2764 goto normal_readbraw
;
2768 * Special hack for broken Linux with no working sendfile. If we
2769 * return EINTR we sent the header but not the rest of the data.
2770 * Fake this up by doing read/write calls.
2772 if (errno
== EINTR
) {
2773 /* Ensure we don't do this again. */
2774 set_use_sendfile(SNUM(conn
), False
);
2775 DEBUG(0,("send_file_readbraw: sendfile not available. Faking..\n"));
2777 if (fake_sendfile(fsp
, startpos
, nread
) == -1) {
2778 DEBUG(0,("send_file_readbraw: fake_sendfile failed for file %s (%s).\n",
2779 fsp
->fsp_name
, strerror(errno
) ));
2780 exit_server_cleanly("send_file_readbraw fake_sendfile failed");
2785 DEBUG(0,("send_file_readbraw: sendfile failed for file %s (%s). Terminating\n",
2786 fsp
->fsp_name
, strerror(errno
) ));
2787 exit_server_cleanly("send_file_readbraw sendfile failed");
2796 outbuf
= TALLOC_ARRAY(NULL
, char, nread
+4);
2798 DEBUG(0,("send_file_readbraw: TALLOC_ARRAY failed for size %u.\n",
2799 (unsigned)(nread
+4)));
2800 reply_readbraw_error();
2805 ret
= read_file(fsp
,outbuf
+4,startpos
,nread
);
2806 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2815 _smb_setlen(outbuf
,ret
);
2816 if (write_data(smbd_server_fd(),outbuf
,4+ret
) != 4+ret
)
2819 TALLOC_FREE(outbuf
);
2822 /****************************************************************************
2823 Reply to a readbraw (core+ protocol).
2824 ****************************************************************************/
2826 void reply_readbraw(struct smb_request
*req
)
2828 connection_struct
*conn
= req
->conn
;
2829 ssize_t maxcount
,mincount
;
2836 START_PROFILE(SMBreadbraw
);
2838 if (srv_is_signing_active() || is_encrypted_packet(req
->inbuf
)) {
2839 exit_server_cleanly("reply_readbraw: SMB signing/sealing is active - "
2840 "raw reads/writes are disallowed.");
2844 reply_readbraw_error();
2845 END_PROFILE(SMBreadbraw
);
2850 * Special check if an oplock break has been issued
2851 * and the readraw request croses on the wire, we must
2852 * return a zero length response here.
2855 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
2858 * We have to do a check_fsp by hand here, as
2859 * we must always return 4 zero bytes on error,
2863 if (!fsp
|| !conn
|| conn
!= fsp
->conn
||
2864 req
->vuid
!= fsp
->vuid
||
2865 fsp
->is_directory
|| fsp
->fh
->fd
== -1) {
2867 * fsp could be NULL here so use the value from the packet. JRA.
2869 DEBUG(3,("reply_readbraw: fnum %d not valid "
2871 (int)SVAL(req
->inbuf
,smb_vwv0
)));
2872 reply_readbraw_error();
2873 END_PROFILE(SMBreadbraw
);
2877 /* Do a "by hand" version of CHECK_READ. */
2878 if (!(fsp
->can_read
||
2879 ((req
->flags2
& FLAGS2_READ_PERMIT_EXECUTE
) &&
2880 (fsp
->access_mask
& FILE_EXECUTE
)))) {
2881 DEBUG(3,("reply_readbraw: fnum %d not readable.\n",
2882 (int)SVAL(req
->inbuf
,smb_vwv0
)));
2883 reply_readbraw_error();
2884 END_PROFILE(SMBreadbraw
);
2888 flush_write_cache(fsp
, READRAW_FLUSH
);
2890 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv1
);
2891 if(req
->wct
== 10) {
2893 * This is a large offset (64 bit) read.
2895 #ifdef LARGE_SMB_OFF_T
2897 startpos
|= (((SMB_OFF_T
)IVAL(req
->inbuf
,smb_vwv8
)) << 32);
2899 #else /* !LARGE_SMB_OFF_T */
2902 * Ensure we haven't been sent a >32 bit offset.
2905 if(IVAL(req
->inbuf
,smb_vwv8
) != 0) {
2906 DEBUG(0,("reply_readbraw: large offset "
2907 "(%x << 32) used and we don't support "
2908 "64 bit offsets.\n",
2909 (unsigned int)IVAL(req
->inbuf
,smb_vwv8
) ));
2910 reply_readbraw_error();
2911 END_PROFILE(SMBreadbraw
);
2915 #endif /* LARGE_SMB_OFF_T */
2918 DEBUG(0,("reply_readbraw: negative 64 bit "
2919 "readraw offset (%.0f) !\n",
2920 (double)startpos
));
2921 reply_readbraw_error();
2922 END_PROFILE(SMBreadbraw
);
2927 maxcount
= (SVAL(req
->inbuf
,smb_vwv3
) & 0xFFFF);
2928 mincount
= (SVAL(req
->inbuf
,smb_vwv4
) & 0xFFFF);
2930 /* ensure we don't overrun the packet size */
2931 maxcount
= MIN(65535,maxcount
);
2933 if (is_locked(fsp
,(uint32
)req
->smbpid
,
2934 (SMB_BIG_UINT
)maxcount
,
2935 (SMB_BIG_UINT
)startpos
,
2937 reply_readbraw_error();
2938 END_PROFILE(SMBreadbraw
);
2942 if (SMB_VFS_FSTAT(fsp
, &st
) == 0) {
2946 if (startpos
>= size
) {
2949 nread
= MIN(maxcount
,(size
- startpos
));
2952 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2953 if (nread
< mincount
)
2957 DEBUG( 3, ( "reply_readbraw: fnum=%d start=%.0f max=%lu "
2958 "min=%lu nread=%lu\n",
2959 fsp
->fnum
, (double)startpos
,
2960 (unsigned long)maxcount
,
2961 (unsigned long)mincount
,
2962 (unsigned long)nread
) );
2964 send_file_readbraw(conn
, fsp
, startpos
, nread
, mincount
);
2966 DEBUG(5,("reply_readbraw finished\n"));
2967 END_PROFILE(SMBreadbraw
);
2971 #define DBGC_CLASS DBGC_LOCKING
2973 /****************************************************************************
2974 Reply to a lockread (core+ protocol).
2975 ****************************************************************************/
2977 void reply_lockread(struct smb_request
*req
)
2979 connection_struct
*conn
= req
->conn
;
2986 struct byte_range_lock
*br_lck
= NULL
;
2989 START_PROFILE(SMBlockread
);
2992 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2993 END_PROFILE(SMBlockread
);
2997 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
2999 if (!check_fsp(conn
, req
, fsp
)) {
3000 END_PROFILE(SMBlockread
);
3004 if (!CHECK_READ(fsp
,req
->inbuf
)) {
3005 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
3006 END_PROFILE(SMBlockread
);
3010 release_level_2_oplocks_on_change(fsp
);
3012 numtoread
= SVAL(req
->inbuf
,smb_vwv1
);
3013 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv2
);
3015 numtoread
= MIN(BUFFER_SIZE
- (smb_size
+ 3*2 + 3), numtoread
);
3017 reply_outbuf(req
, 5, numtoread
+ 3);
3019 data
= smb_buf(req
->outbuf
) + 3;
3022 * NB. Discovered by Menny Hamburger at Mainsoft. This is a core+
3023 * protocol request that predates the read/write lock concept.
3024 * Thus instead of asking for a read lock here we need to ask
3025 * for a write lock. JRA.
3026 * Note that the requested lock size is unaffected by max_recv.
3029 br_lck
= do_lock(smbd_messaging_context(),
3032 (SMB_BIG_UINT
)numtoread
,
3033 (SMB_BIG_UINT
)startpos
,
3036 False
, /* Non-blocking lock. */
3039 TALLOC_FREE(br_lck
);
3041 if (NT_STATUS_V(status
)) {
3042 reply_nterror(req
, status
);
3043 END_PROFILE(SMBlockread
);
3048 * However the requested READ size IS affected by max_recv. Insanity.... JRA.
3051 if (numtoread
> max_recv
) {
3052 DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u). \
3053 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
3054 (unsigned int)numtoread
, (unsigned int)max_recv
));
3055 numtoread
= MIN(numtoread
,max_recv
);
3057 nread
= read_file(fsp
,data
,startpos
,numtoread
);
3060 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
3061 END_PROFILE(SMBlockread
);
3065 srv_set_message((char *)req
->outbuf
, 5, nread
+3, False
);
3067 SSVAL(req
->outbuf
,smb_vwv0
,nread
);
3068 SSVAL(req
->outbuf
,smb_vwv5
,nread
+3);
3069 p
= smb_buf(req
->outbuf
);
3070 SCVAL(p
,0,0); /* pad byte. */
3073 DEBUG(3,("lockread fnum=%d num=%d nread=%d\n",
3074 fsp
->fnum
, (int)numtoread
, (int)nread
));
3076 END_PROFILE(SMBlockread
);
3081 #define DBGC_CLASS DBGC_ALL
3083 /****************************************************************************
3085 ****************************************************************************/
3087 void reply_read(struct smb_request
*req
)
3089 connection_struct
*conn
= req
->conn
;
3097 START_PROFILE(SMBread
);
3100 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3101 END_PROFILE(SMBread
);
3105 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
3107 if (!check_fsp(conn
, req
, fsp
)) {
3108 END_PROFILE(SMBread
);
3112 if (!CHECK_READ(fsp
,req
->inbuf
)) {
3113 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
3114 END_PROFILE(SMBread
);
3118 numtoread
= SVAL(req
->inbuf
,smb_vwv1
);
3119 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv2
);
3121 numtoread
= MIN(BUFFER_SIZE
-outsize
,numtoread
);
3124 * The requested read size cannot be greater than max_recv. JRA.
3126 if (numtoread
> max_recv
) {
3127 DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u). \
3128 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
3129 (unsigned int)numtoread
, (unsigned int)max_recv
));
3130 numtoread
= MIN(numtoread
,max_recv
);
3133 reply_outbuf(req
, 5, numtoread
+3);
3135 data
= smb_buf(req
->outbuf
) + 3;
3137 if (is_locked(fsp
, (uint32
)req
->smbpid
, (SMB_BIG_UINT
)numtoread
,
3138 (SMB_BIG_UINT
)startpos
, READ_LOCK
)) {
3139 reply_doserror(req
, ERRDOS
,ERRlock
);
3140 END_PROFILE(SMBread
);
3145 nread
= read_file(fsp
,data
,startpos
,numtoread
);
3148 reply_unixerror(req
, ERRDOS
,ERRnoaccess
);
3149 END_PROFILE(SMBread
);
3153 srv_set_message((char *)req
->outbuf
, 5, nread
+3, False
);
3155 SSVAL(req
->outbuf
,smb_vwv0
,nread
);
3156 SSVAL(req
->outbuf
,smb_vwv5
,nread
+3);
3157 SCVAL(smb_buf(req
->outbuf
),0,1);
3158 SSVAL(smb_buf(req
->outbuf
),1,nread
);
3160 DEBUG( 3, ( "read fnum=%d num=%d nread=%d\n",
3161 fsp
->fnum
, (int)numtoread
, (int)nread
) );
3163 END_PROFILE(SMBread
);
3167 /****************************************************************************
3169 ****************************************************************************/
3171 static int setup_readX_header(char *outbuf
, size_t smb_maxcnt
)
3176 outsize
= srv_set_message(outbuf
,12,smb_maxcnt
,False
);
3177 data
= smb_buf(outbuf
);
3179 memset(outbuf
+smb_vwv0
,'\0',24); /* valgrind init. */
3181 SCVAL(outbuf
,smb_vwv0
,0xFF);
3182 SSVAL(outbuf
,smb_vwv2
,0xFFFF); /* Remaining - must be -1. */
3183 SSVAL(outbuf
,smb_vwv5
,smb_maxcnt
);
3184 SSVAL(outbuf
,smb_vwv6
,smb_offset(data
,outbuf
));
3185 SSVAL(outbuf
,smb_vwv7
,(smb_maxcnt
>> 16));
3186 SSVAL(smb_buf(outbuf
),-2,smb_maxcnt
);
3187 /* Reset the outgoing length, set_message truncates at 0x1FFFF. */
3188 _smb_setlen_large(outbuf
,(smb_size
+ 12*2 + smb_maxcnt
- 4));
3192 /****************************************************************************
3193 Reply to a read and X - possibly using sendfile.
3194 ****************************************************************************/
3196 static void send_file_readX(connection_struct
*conn
, struct smb_request
*req
,
3197 files_struct
*fsp
, SMB_OFF_T startpos
,
3200 SMB_STRUCT_STAT sbuf
;
3203 if(SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
3204 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
3208 if (startpos
> sbuf
.st_size
) {
3210 } else if (smb_maxcnt
> (sbuf
.st_size
- startpos
)) {
3211 smb_maxcnt
= (sbuf
.st_size
- startpos
);
3214 if (smb_maxcnt
== 0) {
3218 #if defined(WITH_SENDFILE)
3220 * We can only use sendfile on a non-chained packet
3221 * but we can use on a non-oplocked file. tridge proved this
3222 * on a train in Germany :-). JRA.
3225 if ((chain_size
== 0) && (CVAL(req
->inbuf
,smb_vwv0
) == 0xFF) &&
3226 !is_encrypted_packet(req
->inbuf
) && (fsp
->base_fsp
== NULL
) &&
3227 lp_use_sendfile(SNUM(conn
)) && (fsp
->wcp
== NULL
) ) {
3228 uint8 headerbuf
[smb_size
+ 12 * 2];
3232 * Set up the packet header before send. We
3233 * assume here the sendfile will work (get the
3234 * correct amount of data).
3237 header
= data_blob_const(headerbuf
, sizeof(headerbuf
));
3239 construct_reply_common((char *)req
->inbuf
, (char *)headerbuf
);
3240 setup_readX_header((char *)headerbuf
, smb_maxcnt
);
3242 if ((nread
= SMB_VFS_SENDFILE(smbd_server_fd(), fsp
, &header
, startpos
, smb_maxcnt
)) == -1) {
3243 /* Returning ENOSYS means no data at all was sent.
3244 Do this as a normal read. */
3245 if (errno
== ENOSYS
) {
3250 * Special hack for broken Linux with no working sendfile. If we
3251 * return EINTR we sent the header but not the rest of the data.
3252 * Fake this up by doing read/write calls.
3255 if (errno
== EINTR
) {
3256 /* Ensure we don't do this again. */
3257 set_use_sendfile(SNUM(conn
), False
);
3258 DEBUG(0,("send_file_readX: sendfile not available. Faking..\n"));
3259 nread
= fake_sendfile(fsp
, startpos
,
3262 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
3263 fsp
->fsp_name
, strerror(errno
) ));
3264 exit_server_cleanly("send_file_readX: fake_sendfile failed");
3266 DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
3267 fsp
->fnum
, (int)smb_maxcnt
, (int)nread
) );
3268 /* No outbuf here means successful sendfile. */
3269 TALLOC_FREE(req
->outbuf
);
3273 DEBUG(0,("send_file_readX: sendfile failed for file %s (%s). Terminating\n",
3274 fsp
->fsp_name
, strerror(errno
) ));
3275 exit_server_cleanly("send_file_readX sendfile failed");
3278 DEBUG( 3, ( "send_file_readX: sendfile fnum=%d max=%d nread=%d\n",
3279 fsp
->fnum
, (int)smb_maxcnt
, (int)nread
) );
3280 /* No outbuf here means successful sendfile. */
3281 TALLOC_FREE(req
->outbuf
);
3288 if ((smb_maxcnt
& 0xFF0000) > 0x10000) {
3289 uint8 headerbuf
[smb_size
+ 2*12];
3291 construct_reply_common((char *)req
->inbuf
, (char *)headerbuf
);
3292 setup_readX_header((char *)headerbuf
, smb_maxcnt
);
3294 /* Send out the header. */
3295 if (write_data(smbd_server_fd(), (char *)headerbuf
,
3296 sizeof(headerbuf
)) != sizeof(headerbuf
)) {
3297 DEBUG(0,("send_file_readX: write_data failed for file %s (%s). Terminating\n",
3298 fsp
->fsp_name
, strerror(errno
) ));
3299 exit_server_cleanly("send_file_readX sendfile failed");
3301 nread
= fake_sendfile(fsp
, startpos
, smb_maxcnt
);
3303 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
3304 fsp
->fsp_name
, strerror(errno
) ));
3305 exit_server_cleanly("send_file_readX: fake_sendfile failed");
3307 TALLOC_FREE(req
->outbuf
);
3310 reply_outbuf(req
, 12, smb_maxcnt
);
3312 nread
= read_file(fsp
, smb_buf(req
->outbuf
), startpos
,
3315 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
3319 setup_readX_header((char *)req
->outbuf
, nread
);
3321 DEBUG( 3, ( "send_file_readX fnum=%d max=%d nread=%d\n",
3322 fsp
->fnum
, (int)smb_maxcnt
, (int)nread
) );
3330 /****************************************************************************
3331 Reply to a read and X.
3332 ****************************************************************************/
3334 void reply_read_and_X(struct smb_request
*req
)
3336 connection_struct
*conn
= req
->conn
;
3340 bool big_readX
= False
;
3342 size_t smb_mincnt
= SVAL(req
->inbuf
,smb_vwv6
);
3345 START_PROFILE(SMBreadX
);
3347 if ((req
->wct
!= 10) && (req
->wct
!= 12)) {
3348 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3352 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv2
));
3353 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv3
);
3354 smb_maxcnt
= SVAL(req
->inbuf
,smb_vwv5
);
3356 /* If it's an IPC, pass off the pipe handler. */
3358 reply_pipe_read_and_X(req
);
3359 END_PROFILE(SMBreadX
);
3363 if (!check_fsp(conn
, req
, fsp
)) {
3364 END_PROFILE(SMBreadX
);
3368 if (!CHECK_READ(fsp
,req
->inbuf
)) {
3369 reply_doserror(req
, ERRDOS
,ERRbadaccess
);
3370 END_PROFILE(SMBreadX
);
3374 if (global_client_caps
& CAP_LARGE_READX
) {
3375 size_t upper_size
= SVAL(req
->inbuf
,smb_vwv7
);
3376 smb_maxcnt
|= (upper_size
<<16);
3377 if (upper_size
> 1) {
3378 /* Can't do this on a chained packet. */
3379 if ((CVAL(req
->inbuf
,smb_vwv0
) != 0xFF)) {
3380 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
3381 END_PROFILE(SMBreadX
);
3384 /* We currently don't do this on signed or sealed data. */
3385 if (srv_is_signing_active() || is_encrypted_packet(req
->inbuf
)) {
3386 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
3387 END_PROFILE(SMBreadX
);
3390 /* Is there room in the reply for this data ? */
3391 if (smb_maxcnt
> (0xFFFFFF - (smb_size
-4 + 12*2))) {
3393 NT_STATUS_INVALID_PARAMETER
);
3394 END_PROFILE(SMBreadX
);
3401 if (req
->wct
== 12) {
3402 #ifdef LARGE_SMB_OFF_T
3404 * This is a large offset (64 bit) read.
3406 startpos
|= (((SMB_OFF_T
)IVAL(req
->inbuf
,smb_vwv10
)) << 32);
3408 #else /* !LARGE_SMB_OFF_T */
3411 * Ensure we haven't been sent a >32 bit offset.
3414 if(IVAL(req
->inbuf
,smb_vwv10
) != 0) {
3415 DEBUG(0,("reply_read_and_X - large offset (%x << 32) "
3416 "used and we don't support 64 bit offsets.\n",
3417 (unsigned int)IVAL(req
->inbuf
,smb_vwv10
) ));
3418 END_PROFILE(SMBreadX
);
3419 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
3423 #endif /* LARGE_SMB_OFF_T */
3427 if (is_locked(fsp
, (uint32
)req
->smbpid
, (SMB_BIG_UINT
)smb_maxcnt
,
3428 (SMB_BIG_UINT
)startpos
, READ_LOCK
)) {
3429 END_PROFILE(SMBreadX
);
3430 reply_doserror(req
, ERRDOS
, ERRlock
);
3435 schedule_aio_read_and_X(conn
, req
, fsp
, startpos
, smb_maxcnt
)) {
3436 END_PROFILE(SMBreadX
);
3440 send_file_readX(conn
, req
, fsp
, startpos
, smb_maxcnt
);
3442 END_PROFILE(SMBreadX
);
3446 /****************************************************************************
3447 Error replies to writebraw must have smb_wct == 1. Fix this up.
3448 ****************************************************************************/
3450 void error_to_writebrawerr(struct smb_request
*req
)
3452 uint8
*old_outbuf
= req
->outbuf
;
3454 reply_outbuf(req
, 1, 0);
3456 memcpy(req
->outbuf
, old_outbuf
, smb_size
);
3457 TALLOC_FREE(old_outbuf
);
3460 /****************************************************************************
3461 Reply to a writebraw (core+ or LANMAN1.0 protocol).
3462 ****************************************************************************/
3464 void reply_writebraw(struct smb_request
*req
)
3466 connection_struct
*conn
= req
->conn
;
3469 ssize_t total_written
=0;
3470 size_t numtowrite
=0;
3478 START_PROFILE(SMBwritebraw
);
3481 * If we ever reply with an error, it must have the SMB command
3482 * type of SMBwritec, not SMBwriteBraw, as this tells the client
3485 SCVAL(req
->inbuf
,smb_com
,SMBwritec
);
3487 if (srv_is_signing_active()) {
3488 END_PROFILE(SMBwritebraw
);
3489 exit_server_cleanly("reply_writebraw: SMB signing is active - "
3490 "raw reads/writes are disallowed.");
3493 if (req
->wct
< 12) {
3494 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3495 error_to_writebrawerr(req
);
3496 END_PROFILE(SMBwritebraw
);
3500 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
3501 if (!check_fsp(conn
, req
, fsp
)) {
3502 error_to_writebrawerr(req
);
3503 END_PROFILE(SMBwritebraw
);
3507 if (!CHECK_WRITE(fsp
)) {
3508 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
3509 error_to_writebrawerr(req
);
3510 END_PROFILE(SMBwritebraw
);
3514 tcount
= IVAL(req
->inbuf
,smb_vwv1
);
3515 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv3
);
3516 write_through
= BITSETW(req
->inbuf
+smb_vwv7
,0);
3518 /* We have to deal with slightly different formats depending
3519 on whether we are using the core+ or lanman1.0 protocol */
3521 if(Protocol
<= PROTOCOL_COREPLUS
) {
3522 numtowrite
= SVAL(smb_buf(req
->inbuf
),-2);
3523 data
= smb_buf(req
->inbuf
);
3525 numtowrite
= SVAL(req
->inbuf
,smb_vwv10
);
3526 data
= smb_base(req
->inbuf
) + SVAL(req
->inbuf
, smb_vwv11
);
3529 /* Ensure we don't write bytes past the end of this packet. */
3530 if (data
+ numtowrite
> smb_base(req
->inbuf
) + smb_len(req
->inbuf
)) {
3531 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3532 error_to_writebrawerr(req
);
3533 END_PROFILE(SMBwritebraw
);
3537 if (is_locked(fsp
,(uint32
)req
->smbpid
,(SMB_BIG_UINT
)tcount
,
3538 (SMB_BIG_UINT
)startpos
, WRITE_LOCK
)) {
3539 reply_doserror(req
, ERRDOS
, ERRlock
);
3540 error_to_writebrawerr(req
);
3541 END_PROFILE(SMBwritebraw
);
3546 nwritten
= write_file(req
,fsp
,data
,startpos
,numtowrite
);
3549 DEBUG(3,("reply_writebraw: initial write fnum=%d start=%.0f num=%d "
3550 "wrote=%d sync=%d\n",
3551 fsp
->fnum
, (double)startpos
, (int)numtowrite
,
3552 (int)nwritten
, (int)write_through
));
3554 if (nwritten
< (ssize_t
)numtowrite
) {
3555 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
3556 error_to_writebrawerr(req
);
3557 END_PROFILE(SMBwritebraw
);
3561 total_written
= nwritten
;
3563 /* Allocate a buffer of 64k + length. */
3564 buf
= TALLOC_ARRAY(NULL
, char, 65540);
3566 reply_doserror(req
, ERRDOS
, ERRnomem
);
3567 error_to_writebrawerr(req
);
3568 END_PROFILE(SMBwritebraw
);
3572 /* Return a SMBwritebraw message to the redirector to tell
3573 * it to send more bytes */
3575 memcpy(buf
, req
->inbuf
, smb_size
);
3576 srv_set_message(buf
,Protocol
>PROTOCOL_COREPLUS
?1:0,0,True
);
3577 SCVAL(buf
,smb_com
,SMBwritebraw
);
3578 SSVALS(buf
,smb_vwv0
,0xFFFF);
3580 if (!srv_send_smb(smbd_server_fd(),
3582 IS_CONN_ENCRYPTED(conn
))) {
3583 exit_server_cleanly("reply_writebraw: srv_send_smb "
3587 /* Now read the raw data into the buffer and write it */
3588 status
= read_smb_length(smbd_server_fd(), buf
, SMB_SECONDARY_WAIT
,
3590 if (!NT_STATUS_IS_OK(status
)) {
3591 exit_server_cleanly("secondary writebraw failed");
3594 /* Set up outbuf to return the correct size */
3595 reply_outbuf(req
, 1, 0);
3597 if (numtowrite
!= 0) {
3599 if (numtowrite
> 0xFFFF) {
3600 DEBUG(0,("reply_writebraw: Oversize secondary write "
3601 "raw requested (%u). Terminating\n",
3602 (unsigned int)numtowrite
));
3603 exit_server_cleanly("secondary writebraw failed");
3606 if (tcount
> nwritten
+numtowrite
) {
3607 DEBUG(3,("reply_writebraw: Client overestimated the "
3609 (int)tcount
,(int)nwritten
,(int)numtowrite
));
3612 status
= read_data(smbd_server_fd(), buf
+4, numtowrite
);
3614 if (!NT_STATUS_IS_OK(status
)) {
3615 DEBUG(0,("reply_writebraw: Oversize secondary write "
3616 "raw read failed (%s). Terminating\n",
3617 nt_errstr(status
)));
3618 exit_server_cleanly("secondary writebraw failed");
3621 nwritten
= write_file(req
,fsp
,buf
+4,startpos
+nwritten
,numtowrite
);
3622 if (nwritten
== -1) {
3624 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
3625 error_to_writebrawerr(req
);
3626 END_PROFILE(SMBwritebraw
);
3630 if (nwritten
< (ssize_t
)numtowrite
) {
3631 SCVAL(req
->outbuf
,smb_rcls
,ERRHRD
);
3632 SSVAL(req
->outbuf
,smb_err
,ERRdiskfull
);
3636 total_written
+= nwritten
;
3641 SSVAL(req
->outbuf
,smb_vwv0
,total_written
);
3643 status
= sync_file(conn
, fsp
, write_through
);
3644 if (!NT_STATUS_IS_OK(status
)) {
3645 DEBUG(5,("reply_writebraw: sync_file for %s returned %s\n",
3646 fsp
->fsp_name
, nt_errstr(status
) ));
3647 reply_nterror(req
, status
);
3648 error_to_writebrawerr(req
);
3649 END_PROFILE(SMBwritebraw
);
3653 DEBUG(3,("reply_writebraw: secondart write fnum=%d start=%.0f num=%d "
3655 fsp
->fnum
, (double)startpos
, (int)numtowrite
,
3656 (int)total_written
));
3658 /* We won't return a status if write through is not selected - this
3659 * follows what WfWg does */
3660 END_PROFILE(SMBwritebraw
);
3662 if (!write_through
&& total_written
==tcount
) {
3664 #if RABBIT_PELLET_FIX
3666 * Fix for "rabbit pellet" mode, trigger an early TCP ack by
3667 * sending a SMBkeepalive. Thanks to DaveCB at Sun for this.
3670 if (!send_keepalive(smbd_server_fd())) {
3671 exit_server_cleanly("reply_writebraw: send of "
3672 "keepalive failed");
3675 TALLOC_FREE(req
->outbuf
);
3681 #define DBGC_CLASS DBGC_LOCKING
3683 /****************************************************************************
3684 Reply to a writeunlock (core+).
3685 ****************************************************************************/
3687 void reply_writeunlock(struct smb_request
*req
)
3689 connection_struct
*conn
= req
->conn
;
3690 ssize_t nwritten
= -1;
3694 NTSTATUS status
= NT_STATUS_OK
;
3697 START_PROFILE(SMBwriteunlock
);
3700 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3701 END_PROFILE(SMBwriteunlock
);
3705 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
3707 if (!check_fsp(conn
, req
, fsp
)) {
3708 END_PROFILE(SMBwriteunlock
);
3712 if (!CHECK_WRITE(fsp
)) {
3713 reply_doserror(req
, ERRDOS
,ERRbadaccess
);
3714 END_PROFILE(SMBwriteunlock
);
3718 numtowrite
= SVAL(req
->inbuf
,smb_vwv1
);
3719 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv2
);
3720 data
= smb_buf(req
->inbuf
) + 3;
3723 && is_locked(fsp
, (uint32
)req
->smbpid
, (SMB_BIG_UINT
)numtowrite
,
3724 (SMB_BIG_UINT
)startpos
, WRITE_LOCK
)) {
3725 reply_doserror(req
, ERRDOS
, ERRlock
);
3726 END_PROFILE(SMBwriteunlock
);
3730 /* The special X/Open SMB protocol handling of
3731 zero length writes is *NOT* done for
3733 if(numtowrite
== 0) {
3736 nwritten
= write_file(req
,fsp
,data
,startpos
,numtowrite
);
3739 status
= sync_file(conn
, fsp
, False
/* write through */);
3740 if (!NT_STATUS_IS_OK(status
)) {
3741 DEBUG(5,("reply_writeunlock: sync_file for %s returned %s\n",
3742 fsp
->fsp_name
, nt_errstr(status
) ));
3743 reply_nterror(req
, status
);
3744 END_PROFILE(SMBwriteunlock
);
3748 if(((nwritten
< numtowrite
) && (numtowrite
!= 0))||(nwritten
< 0)) {
3749 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
3750 END_PROFILE(SMBwriteunlock
);
3755 status
= do_unlock(smbd_messaging_context(),
3758 (SMB_BIG_UINT
)numtowrite
,
3759 (SMB_BIG_UINT
)startpos
,
3762 if (NT_STATUS_V(status
)) {
3763 reply_nterror(req
, status
);
3764 END_PROFILE(SMBwriteunlock
);
3769 reply_outbuf(req
, 1, 0);
3771 SSVAL(req
->outbuf
,smb_vwv0
,nwritten
);
3773 DEBUG(3,("writeunlock fnum=%d num=%d wrote=%d\n",
3774 fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
3776 END_PROFILE(SMBwriteunlock
);
3781 #define DBGC_CLASS DBGC_ALL
3783 /****************************************************************************
3785 ****************************************************************************/
3787 void reply_write(struct smb_request
*req
)
3789 connection_struct
*conn
= req
->conn
;
3791 ssize_t nwritten
= -1;
3797 START_PROFILE(SMBwrite
);
3800 END_PROFILE(SMBwrite
);
3801 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3805 /* If it's an IPC, pass off the pipe handler. */
3807 reply_pipe_write(req
);
3808 END_PROFILE(SMBwrite
);
3812 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
3814 if (!check_fsp(conn
, req
, fsp
)) {
3815 END_PROFILE(SMBwrite
);
3819 if (!CHECK_WRITE(fsp
)) {
3820 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
3821 END_PROFILE(SMBwrite
);
3825 numtowrite
= SVAL(req
->inbuf
,smb_vwv1
);
3826 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv2
);
3827 data
= smb_buf(req
->inbuf
) + 3;
3829 if (is_locked(fsp
, (uint32
)req
->smbpid
, (SMB_BIG_UINT
)numtowrite
,
3830 (SMB_BIG_UINT
)startpos
, WRITE_LOCK
)) {
3831 reply_doserror(req
, ERRDOS
, ERRlock
);
3832 END_PROFILE(SMBwrite
);
3837 * X/Open SMB protocol says that if smb_vwv1 is
3838 * zero then the file size should be extended or
3839 * truncated to the size given in smb_vwv[2-3].
3842 if(numtowrite
== 0) {
3844 * This is actually an allocate call, and set EOF. JRA.
3846 nwritten
= vfs_allocate_file_space(fsp
, (SMB_OFF_T
)startpos
);
3848 reply_nterror(req
, NT_STATUS_DISK_FULL
);
3849 END_PROFILE(SMBwrite
);
3852 nwritten
= vfs_set_filelen(fsp
, (SMB_OFF_T
)startpos
);
3854 reply_nterror(req
, NT_STATUS_DISK_FULL
);
3855 END_PROFILE(SMBwrite
);
3858 trigger_write_time_update_immediate(fsp
);
3860 nwritten
= write_file(req
,fsp
,data
,startpos
,numtowrite
);
3863 status
= sync_file(conn
, fsp
, False
);
3864 if (!NT_STATUS_IS_OK(status
)) {
3865 DEBUG(5,("reply_write: sync_file for %s returned %s\n",
3866 fsp
->fsp_name
, nt_errstr(status
) ));
3867 reply_nterror(req
, status
);
3868 END_PROFILE(SMBwrite
);
3872 if(((nwritten
== 0) && (numtowrite
!= 0))||(nwritten
< 0)) {
3873 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
3874 END_PROFILE(SMBwrite
);
3878 reply_outbuf(req
, 1, 0);
3880 SSVAL(req
->outbuf
,smb_vwv0
,nwritten
);
3882 if (nwritten
< (ssize_t
)numtowrite
) {
3883 SCVAL(req
->outbuf
,smb_rcls
,ERRHRD
);
3884 SSVAL(req
->outbuf
,smb_err
,ERRdiskfull
);
3887 DEBUG(3,("write fnum=%d num=%d wrote=%d\n", fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
3889 END_PROFILE(SMBwrite
);
3893 /****************************************************************************
3894 Ensure a buffer is a valid writeX for recvfile purposes.
3895 ****************************************************************************/
3897 #define STANDARD_WRITE_AND_X_HEADER_SIZE (smb_size - 4 + /* basic header */ \
3898 (2*14) + /* word count (including bcc) */ \
3901 bool is_valid_writeX_buffer(const uint8_t *inbuf
)
3904 connection_struct
*conn
= NULL
;
3905 unsigned int doff
= 0;
3906 size_t len
= smb_len_large(inbuf
);
3908 if (is_encrypted_packet(inbuf
)) {
3909 /* Can't do this on encrypted
3914 if (CVAL(inbuf
,smb_com
) != SMBwriteX
) {
3918 if (CVAL(inbuf
,smb_vwv0
) != 0xFF ||
3919 CVAL(inbuf
,smb_wct
) != 14) {
3920 DEBUG(10,("is_valid_writeX_buffer: chained or "
3921 "invalid word length.\n"));
3925 conn
= conn_find(SVAL(inbuf
, smb_tid
));
3927 DEBUG(10,("is_valid_writeX_buffer: bad tid\n"));
3931 DEBUG(10,("is_valid_writeX_buffer: IPC$ tid\n"));
3934 if (IS_PRINT(conn
)) {
3935 DEBUG(10,("is_valid_writeX_buffer: printing tid\n"));
3938 doff
= SVAL(inbuf
,smb_vwv11
);
3940 numtowrite
= SVAL(inbuf
,smb_vwv10
);
3942 if (len
> doff
&& len
- doff
> 0xFFFF) {
3943 numtowrite
|= (((size_t)SVAL(inbuf
,smb_vwv9
))<<16);
3946 if (numtowrite
== 0) {
3947 DEBUG(10,("is_valid_writeX_buffer: zero write\n"));
3951 /* Ensure the sizes match up. */
3952 if (doff
< STANDARD_WRITE_AND_X_HEADER_SIZE
) {
3953 /* no pad byte...old smbclient :-( */
3954 DEBUG(10,("is_valid_writeX_buffer: small doff %u (min %u)\n",
3956 (unsigned int)STANDARD_WRITE_AND_X_HEADER_SIZE
));
3960 if (len
- doff
!= numtowrite
) {
3961 DEBUG(10,("is_valid_writeX_buffer: doff mismatch "
3962 "len = %u, doff = %u, numtowrite = %u\n",
3965 (unsigned int)numtowrite
));
3969 DEBUG(10,("is_valid_writeX_buffer: true "
3970 "len = %u, doff = %u, numtowrite = %u\n",
3973 (unsigned int)numtowrite
));
3978 /****************************************************************************
3979 Reply to a write and X.
3980 ****************************************************************************/
3982 void reply_write_and_X(struct smb_request
*req
)
3984 connection_struct
*conn
= req
->conn
;
3990 unsigned int smb_doff
;
3991 unsigned int smblen
;
3995 START_PROFILE(SMBwriteX
);
3997 if ((req
->wct
!= 12) && (req
->wct
!= 14)) {
3998 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3999 END_PROFILE(SMBwriteX
);
4003 numtowrite
= SVAL(req
->inbuf
,smb_vwv10
);
4004 smb_doff
= SVAL(req
->inbuf
,smb_vwv11
);
4005 smblen
= smb_len(req
->inbuf
);
4007 if (req
->unread_bytes
> 0xFFFF ||
4008 (smblen
> smb_doff
&&
4009 smblen
- smb_doff
> 0xFFFF)) {
4010 numtowrite
|= (((size_t)SVAL(req
->inbuf
,smb_vwv9
))<<16);
4013 if (req
->unread_bytes
) {
4014 /* Can't do a recvfile write on IPC$ */
4016 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4017 END_PROFILE(SMBwriteX
);
4020 if (numtowrite
!= req
->unread_bytes
) {
4021 reply_doserror(req
, ERRDOS
, ERRbadmem
);
4022 END_PROFILE(SMBwriteX
);
4026 if (smb_doff
> smblen
|| smb_doff
+ numtowrite
< numtowrite
||
4027 smb_doff
+ numtowrite
> smblen
) {
4028 reply_doserror(req
, ERRDOS
, ERRbadmem
);
4029 END_PROFILE(SMBwriteX
);
4034 /* If it's an IPC, pass off the pipe handler. */
4036 if (req
->unread_bytes
) {
4037 reply_doserror(req
, ERRDOS
, ERRbadmem
);
4038 END_PROFILE(SMBwriteX
);
4041 reply_pipe_write_and_X(req
);
4042 END_PROFILE(SMBwriteX
);
4046 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv2
));
4047 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv3
);
4048 write_through
= BITSETW(req
->inbuf
+smb_vwv7
,0);
4050 if (!check_fsp(conn
, req
, fsp
)) {
4051 END_PROFILE(SMBwriteX
);
4055 if (!CHECK_WRITE(fsp
)) {
4056 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
4057 END_PROFILE(SMBwriteX
);
4061 data
= smb_base(req
->inbuf
) + smb_doff
;
4063 if(req
->wct
== 14) {
4064 #ifdef LARGE_SMB_OFF_T
4066 * This is a large offset (64 bit) write.
4068 startpos
|= (((SMB_OFF_T
)IVAL(req
->inbuf
,smb_vwv12
)) << 32);
4070 #else /* !LARGE_SMB_OFF_T */
4073 * Ensure we haven't been sent a >32 bit offset.
4076 if(IVAL(req
->inbuf
,smb_vwv12
) != 0) {
4077 DEBUG(0,("reply_write_and_X - large offset (%x << 32) "
4078 "used and we don't support 64 bit offsets.\n",
4079 (unsigned int)IVAL(req
->inbuf
,smb_vwv12
) ));
4080 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
4081 END_PROFILE(SMBwriteX
);
4085 #endif /* LARGE_SMB_OFF_T */
4088 if (is_locked(fsp
,(uint32
)req
->smbpid
,
4089 (SMB_BIG_UINT
)numtowrite
,
4090 (SMB_BIG_UINT
)startpos
, WRITE_LOCK
)) {
4091 reply_doserror(req
, ERRDOS
, ERRlock
);
4092 END_PROFILE(SMBwriteX
);
4096 /* X/Open SMB protocol says that, unlike SMBwrite
4097 if the length is zero then NO truncation is
4098 done, just a write of zero. To truncate a file,
4101 if(numtowrite
== 0) {
4105 if ((req
->unread_bytes
== 0) &&
4106 schedule_aio_write_and_X(conn
, req
, fsp
, data
, startpos
,
4108 END_PROFILE(SMBwriteX
);
4112 nwritten
= write_file(req
,fsp
,data
,startpos
,numtowrite
);
4115 if(((nwritten
== 0) && (numtowrite
!= 0))||(nwritten
< 0)) {
4116 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
4117 END_PROFILE(SMBwriteX
);
4121 reply_outbuf(req
, 6, 0);
4122 SSVAL(req
->outbuf
,smb_vwv2
,nwritten
);
4123 SSVAL(req
->outbuf
,smb_vwv4
,nwritten
>>16);
4125 if (nwritten
< (ssize_t
)numtowrite
) {
4126 SCVAL(req
->outbuf
,smb_rcls
,ERRHRD
);
4127 SSVAL(req
->outbuf
,smb_err
,ERRdiskfull
);
4130 DEBUG(3,("writeX fnum=%d num=%d wrote=%d\n",
4131 fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
4133 status
= sync_file(conn
, fsp
, write_through
);
4134 if (!NT_STATUS_IS_OK(status
)) {
4135 DEBUG(5,("reply_write_and_X: sync_file for %s returned %s\n",
4136 fsp
->fsp_name
, nt_errstr(status
) ));
4137 reply_nterror(req
, status
);
4138 END_PROFILE(SMBwriteX
);
4142 END_PROFILE(SMBwriteX
);
4147 /****************************************************************************
4149 ****************************************************************************/
4151 void reply_lseek(struct smb_request
*req
)
4153 connection_struct
*conn
= req
->conn
;
4159 START_PROFILE(SMBlseek
);
4162 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4163 END_PROFILE(SMBlseek
);
4167 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
4169 if (!check_fsp(conn
, req
, fsp
)) {
4173 flush_write_cache(fsp
, SEEK_FLUSH
);
4175 mode
= SVAL(req
->inbuf
,smb_vwv1
) & 3;
4176 /* NB. This doesn't use IVAL_TO_SMB_OFF_T as startpos can be signed in this case. */
4177 startpos
= (SMB_OFF_T
)IVALS(req
->inbuf
,smb_vwv2
);
4186 res
= fsp
->fh
->pos
+ startpos
;
4197 if (umode
== SEEK_END
) {
4198 if((res
= SMB_VFS_LSEEK(fsp
,startpos
,umode
)) == -1) {
4199 if(errno
== EINVAL
) {
4200 SMB_OFF_T current_pos
= startpos
;
4201 SMB_STRUCT_STAT sbuf
;
4203 if(SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
4204 reply_unixerror(req
, ERRDOS
,
4206 END_PROFILE(SMBlseek
);
4210 current_pos
+= sbuf
.st_size
;
4212 res
= SMB_VFS_LSEEK(fsp
,0,SEEK_SET
);
4217 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
4218 END_PROFILE(SMBlseek
);
4225 reply_outbuf(req
, 2, 0);
4226 SIVAL(req
->outbuf
,smb_vwv0
,res
);
4228 DEBUG(3,("lseek fnum=%d ofs=%.0f newpos = %.0f mode=%d\n",
4229 fsp
->fnum
, (double)startpos
, (double)res
, mode
));
4231 END_PROFILE(SMBlseek
);
4235 /****************************************************************************
4237 ****************************************************************************/
4239 void reply_flush(struct smb_request
*req
)
4241 connection_struct
*conn
= req
->conn
;
4245 START_PROFILE(SMBflush
);
4248 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4252 fnum
= SVAL(req
->inbuf
,smb_vwv0
);
4253 fsp
= file_fsp(fnum
);
4255 if ((fnum
!= 0xFFFF) && !check_fsp(conn
, req
, fsp
)) {
4260 file_sync_all(conn
);
4262 NTSTATUS status
= sync_file(conn
, fsp
, True
);
4263 if (!NT_STATUS_IS_OK(status
)) {
4264 DEBUG(5,("reply_flush: sync_file for %s returned %s\n",
4265 fsp
->fsp_name
, nt_errstr(status
) ));
4266 reply_nterror(req
, status
);
4267 END_PROFILE(SMBflush
);
4272 reply_outbuf(req
, 0, 0);
4274 DEBUG(3,("flush\n"));
4275 END_PROFILE(SMBflush
);
4279 /****************************************************************************
4281 conn POINTER CAN BE NULL HERE !
4282 ****************************************************************************/
4284 void reply_exit(struct smb_request
*req
)
4286 START_PROFILE(SMBexit
);
4288 file_close_pid(req
->smbpid
, req
->vuid
);
4290 reply_outbuf(req
, 0, 0);
4292 DEBUG(3,("exit\n"));
4294 END_PROFILE(SMBexit
);
4298 /****************************************************************************
4299 Reply to a close - has to deal with closing a directory opened by NT SMB's.
4300 ****************************************************************************/
4302 void reply_close(struct smb_request
*req
)
4304 connection_struct
*conn
= req
->conn
;
4305 NTSTATUS status
= NT_STATUS_OK
;
4306 files_struct
*fsp
= NULL
;
4307 START_PROFILE(SMBclose
);
4310 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4311 END_PROFILE(SMBclose
);
4315 /* If it's an IPC, pass off to the pipe handler. */
4317 reply_pipe_close(conn
, req
);
4318 END_PROFILE(SMBclose
);
4322 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
4325 * We can only use check_fsp if we know it's not a directory.
4328 if(!fsp
|| (fsp
->conn
!= conn
) || (fsp
->vuid
!= req
->vuid
)) {
4329 reply_doserror(req
, ERRDOS
, ERRbadfid
);
4330 END_PROFILE(SMBclose
);
4334 if(fsp
->is_directory
) {
4336 * Special case - close NT SMB directory handle.
4338 DEBUG(3,("close directory fnum=%d\n", fsp
->fnum
));
4339 status
= close_file(fsp
,NORMAL_CLOSE
);
4343 * Close ordinary file.
4346 DEBUG(3,("close fd=%d fnum=%d (numopen=%d)\n",
4347 fsp
->fh
->fd
, fsp
->fnum
,
4348 conn
->num_files_open
));
4351 * Take care of any time sent in the close.
4354 t
= srv_make_unix_date3(req
->inbuf
+smb_vwv1
);
4355 set_close_write_time(fsp
, convert_time_t_to_timespec(t
));
4358 * close_file() returns the unix errno if an error
4359 * was detected on close - normally this is due to
4360 * a disk full error. If not then it was probably an I/O error.
4363 status
= close_file(fsp
,NORMAL_CLOSE
);
4366 if (!NT_STATUS_IS_OK(status
)) {
4367 reply_nterror(req
, status
);
4368 END_PROFILE(SMBclose
);
4372 reply_outbuf(req
, 0, 0);
4373 END_PROFILE(SMBclose
);
4377 /****************************************************************************
4378 Reply to a writeclose (Core+ protocol).
4379 ****************************************************************************/
4381 void reply_writeclose(struct smb_request
*req
)
4383 connection_struct
*conn
= req
->conn
;
4385 ssize_t nwritten
= -1;
4386 NTSTATUS close_status
= NT_STATUS_OK
;
4389 struct timespec mtime
;
4392 START_PROFILE(SMBwriteclose
);
4395 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4396 END_PROFILE(SMBwriteclose
);
4400 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
4402 if (!check_fsp(conn
, req
, fsp
)) {
4403 END_PROFILE(SMBwriteclose
);
4406 if (!CHECK_WRITE(fsp
)) {
4407 reply_doserror(req
, ERRDOS
,ERRbadaccess
);
4408 END_PROFILE(SMBwriteclose
);
4412 numtowrite
= SVAL(req
->inbuf
,smb_vwv1
);
4413 startpos
= IVAL_TO_SMB_OFF_T(req
->inbuf
,smb_vwv2
);
4414 mtime
= convert_time_t_to_timespec(srv_make_unix_date3(
4415 req
->inbuf
+smb_vwv4
));
4416 data
= smb_buf(req
->inbuf
) + 1;
4419 && is_locked(fsp
, (uint32
)req
->smbpid
, (SMB_BIG_UINT
)numtowrite
,
4420 (SMB_BIG_UINT
)startpos
, WRITE_LOCK
)) {
4421 reply_doserror(req
, ERRDOS
,ERRlock
);
4422 END_PROFILE(SMBwriteclose
);
4426 nwritten
= write_file(req
,fsp
,data
,startpos
,numtowrite
);
4428 set_close_write_time(fsp
, mtime
);
4431 * More insanity. W2K only closes the file if writelen > 0.
4436 DEBUG(3,("reply_writeclose: zero length write doesn't close file %s\n",
4438 close_status
= close_file(fsp
,NORMAL_CLOSE
);
4441 DEBUG(3,("writeclose fnum=%d num=%d wrote=%d (numopen=%d)\n",
4442 fsp
->fnum
, (int)numtowrite
, (int)nwritten
,
4443 conn
->num_files_open
));
4445 if(((nwritten
== 0) && (numtowrite
!= 0))||(nwritten
< 0)) {
4446 reply_doserror(req
, ERRHRD
, ERRdiskfull
);
4447 END_PROFILE(SMBwriteclose
);
4451 if(!NT_STATUS_IS_OK(close_status
)) {
4452 reply_nterror(req
, close_status
);
4453 END_PROFILE(SMBwriteclose
);
4457 reply_outbuf(req
, 1, 0);
4459 SSVAL(req
->outbuf
,smb_vwv0
,nwritten
);
4460 END_PROFILE(SMBwriteclose
);
4465 #define DBGC_CLASS DBGC_LOCKING
4467 /****************************************************************************
4469 ****************************************************************************/
4471 void reply_lock(struct smb_request
*req
)
4473 connection_struct
*conn
= req
->conn
;
4474 SMB_BIG_UINT count
,offset
;
4477 struct byte_range_lock
*br_lck
= NULL
;
4479 START_PROFILE(SMBlock
);
4482 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4483 END_PROFILE(SMBlock
);
4487 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
4489 if (!check_fsp(conn
, req
, fsp
)) {
4490 END_PROFILE(SMBlock
);
4494 release_level_2_oplocks_on_change(fsp
);
4496 count
= (SMB_BIG_UINT
)IVAL(req
->inbuf
,smb_vwv1
);
4497 offset
= (SMB_BIG_UINT
)IVAL(req
->inbuf
,smb_vwv3
);
4499 DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
4500 fsp
->fh
->fd
, fsp
->fnum
, (double)offset
, (double)count
));
4502 br_lck
= do_lock(smbd_messaging_context(),
4509 False
, /* Non-blocking lock. */
4513 TALLOC_FREE(br_lck
);
4515 if (NT_STATUS_V(status
)) {
4516 reply_nterror(req
, status
);
4517 END_PROFILE(SMBlock
);
4521 reply_outbuf(req
, 0, 0);
4523 END_PROFILE(SMBlock
);
4527 /****************************************************************************
4529 ****************************************************************************/
4531 void reply_unlock(struct smb_request
*req
)
4533 connection_struct
*conn
= req
->conn
;
4534 SMB_BIG_UINT count
,offset
;
4538 START_PROFILE(SMBunlock
);
4541 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4542 END_PROFILE(SMBunlock
);
4546 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
4548 if (!check_fsp(conn
, req
, fsp
)) {
4549 END_PROFILE(SMBunlock
);
4553 count
= (SMB_BIG_UINT
)IVAL(req
->inbuf
,smb_vwv1
);
4554 offset
= (SMB_BIG_UINT
)IVAL(req
->inbuf
,smb_vwv3
);
4556 status
= do_unlock(smbd_messaging_context(),
4563 if (NT_STATUS_V(status
)) {
4564 reply_nterror(req
, status
);
4565 END_PROFILE(SMBunlock
);
4569 DEBUG( 3, ( "unlock fd=%d fnum=%d offset=%.0f count=%.0f\n",
4570 fsp
->fh
->fd
, fsp
->fnum
, (double)offset
, (double)count
) );
4572 reply_outbuf(req
, 0, 0);
4574 END_PROFILE(SMBunlock
);
4579 #define DBGC_CLASS DBGC_ALL
4581 /****************************************************************************
4583 conn POINTER CAN BE NULL HERE !
4584 ****************************************************************************/
4586 void reply_tdis(struct smb_request
*req
)
4588 connection_struct
*conn
= req
->conn
;
4589 START_PROFILE(SMBtdis
);
4592 DEBUG(4,("Invalid connection in tdis\n"));
4593 reply_doserror(req
, ERRSRV
, ERRinvnid
);
4594 END_PROFILE(SMBtdis
);
4600 close_cnum(conn
,req
->vuid
);
4603 reply_outbuf(req
, 0, 0);
4604 END_PROFILE(SMBtdis
);
4608 /****************************************************************************
4610 conn POINTER CAN BE NULL HERE !
4611 ****************************************************************************/
4613 void reply_echo(struct smb_request
*req
)
4615 connection_struct
*conn
= req
->conn
;
4618 unsigned int data_len
= smb_buflen(req
->inbuf
);
4620 START_PROFILE(SMBecho
);
4623 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4624 END_PROFILE(SMBecho
);
4628 if (data_len
> BUFFER_SIZE
) {
4629 DEBUG(0,("reply_echo: data_len too large.\n"));
4630 reply_nterror(req
, NT_STATUS_INSUFFICIENT_RESOURCES
);
4631 END_PROFILE(SMBecho
);
4635 smb_reverb
= SVAL(req
->inbuf
,smb_vwv0
);
4637 reply_outbuf(req
, 1, data_len
);
4639 /* copy any incoming data back out */
4641 memcpy(smb_buf(req
->outbuf
),smb_buf(req
->inbuf
),data_len
);
4644 if (smb_reverb
> 100) {
4645 DEBUG(0,("large reverb (%d)?? Setting to 100\n",smb_reverb
));
4649 for (seq_num
=1 ; seq_num
<= smb_reverb
; seq_num
++) {
4650 SSVAL(req
->outbuf
,smb_vwv0
,seq_num
);
4652 show_msg((char *)req
->outbuf
);
4653 if (!srv_send_smb(smbd_server_fd(),
4654 (char *)req
->outbuf
,
4655 IS_CONN_ENCRYPTED(conn
)||req
->encrypted
))
4656 exit_server_cleanly("reply_echo: srv_send_smb failed.");
4659 DEBUG(3,("echo %d times\n", smb_reverb
));
4661 TALLOC_FREE(req
->outbuf
);
4663 END_PROFILE(SMBecho
);
4667 /****************************************************************************
4668 Reply to a printopen.
4669 ****************************************************************************/
4671 void reply_printopen(struct smb_request
*req
)
4673 connection_struct
*conn
= req
->conn
;
4675 SMB_STRUCT_STAT sbuf
;
4678 START_PROFILE(SMBsplopen
);
4681 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4682 END_PROFILE(SMBsplopen
);
4686 if (!CAN_PRINT(conn
)) {
4687 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
4688 END_PROFILE(SMBsplopen
);
4692 status
= file_new(conn
, &fsp
);
4693 if(!NT_STATUS_IS_OK(status
)) {
4694 reply_nterror(req
, status
);
4695 END_PROFILE(SMBsplopen
);
4699 /* Open for exclusive use, write only. */
4700 status
= print_fsp_open(conn
, NULL
, req
->vuid
, fsp
, &sbuf
);
4702 if (!NT_STATUS_IS_OK(status
)) {
4704 reply_nterror(req
, status
);
4705 END_PROFILE(SMBsplopen
);
4709 reply_outbuf(req
, 1, 0);
4710 SSVAL(req
->outbuf
,smb_vwv0
,fsp
->fnum
);
4712 DEBUG(3,("openprint fd=%d fnum=%d\n",
4713 fsp
->fh
->fd
, fsp
->fnum
));
4715 END_PROFILE(SMBsplopen
);
4719 /****************************************************************************
4720 Reply to a printclose.
4721 ****************************************************************************/
4723 void reply_printclose(struct smb_request
*req
)
4725 connection_struct
*conn
= req
->conn
;
4729 START_PROFILE(SMBsplclose
);
4732 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4733 END_PROFILE(SMBsplclose
);
4737 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
4739 if (!check_fsp(conn
, req
, fsp
)) {
4740 END_PROFILE(SMBsplclose
);
4744 if (!CAN_PRINT(conn
)) {
4745 reply_nterror(req
, NT_STATUS_DOS(ERRSRV
, ERRerror
));
4746 END_PROFILE(SMBsplclose
);
4750 DEBUG(3,("printclose fd=%d fnum=%d\n",
4751 fsp
->fh
->fd
,fsp
->fnum
));
4753 status
= close_file(fsp
,NORMAL_CLOSE
);
4755 if(!NT_STATUS_IS_OK(status
)) {
4756 reply_nterror(req
, status
);
4757 END_PROFILE(SMBsplclose
);
4761 reply_outbuf(req
, 0, 0);
4763 END_PROFILE(SMBsplclose
);
4767 /****************************************************************************
4768 Reply to a printqueue.
4769 ****************************************************************************/
4771 void reply_printqueue(struct smb_request
*req
)
4773 connection_struct
*conn
= req
->conn
;
4777 START_PROFILE(SMBsplretq
);
4780 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4781 END_PROFILE(SMBsplretq
);
4785 max_count
= SVAL(req
->inbuf
,smb_vwv0
);
4786 start_index
= SVAL(req
->inbuf
,smb_vwv1
);
4788 /* we used to allow the client to get the cnum wrong, but that
4789 is really quite gross and only worked when there was only
4790 one printer - I think we should now only accept it if they
4791 get it right (tridge) */
4792 if (!CAN_PRINT(conn
)) {
4793 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
4794 END_PROFILE(SMBsplretq
);
4798 reply_outbuf(req
, 2, 3);
4799 SSVAL(req
->outbuf
,smb_vwv0
,0);
4800 SSVAL(req
->outbuf
,smb_vwv1
,0);
4801 SCVAL(smb_buf(req
->outbuf
),0,1);
4802 SSVAL(smb_buf(req
->outbuf
),1,0);
4804 DEBUG(3,("printqueue start_index=%d max_count=%d\n",
4805 start_index
, max_count
));
4808 print_queue_struct
*queue
= NULL
;
4809 print_status_struct status
;
4810 int count
= print_queue_status(SNUM(conn
), &queue
, &status
);
4811 int num_to_get
= ABS(max_count
);
4812 int first
= (max_count
>0?start_index
:start_index
+max_count
+1);
4818 num_to_get
= MIN(num_to_get
,count
-first
);
4821 for (i
=first
;i
<first
+num_to_get
;i
++) {
4825 srv_put_dos_date2(p
,0,queue
[i
].time
);
4826 SCVAL(p
,4,(queue
[i
].status
==LPQ_PRINTING
?2:3));
4827 SSVAL(p
,5, queue
[i
].job
);
4828 SIVAL(p
,7,queue
[i
].size
);
4830 srvstr_push(blob
, req
->flags2
, p
+12,
4831 queue
[i
].fs_user
, 16, STR_ASCII
);
4833 if (message_push_blob(
4836 blob
, sizeof(blob
))) == -1) {
4837 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
4838 END_PROFILE(SMBsplretq
);
4844 SSVAL(req
->outbuf
,smb_vwv0
,count
);
4845 SSVAL(req
->outbuf
,smb_vwv1
,
4846 (max_count
>0?first
+count
:first
-1));
4847 SCVAL(smb_buf(req
->outbuf
),0,1);
4848 SSVAL(smb_buf(req
->outbuf
),1,28*count
);
4853 DEBUG(3,("%d entries returned in queue\n",count
));
4856 END_PROFILE(SMBsplretq
);
4860 /****************************************************************************
4861 Reply to a printwrite.
4862 ****************************************************************************/
4864 void reply_printwrite(struct smb_request
*req
)
4866 connection_struct
*conn
= req
->conn
;
4871 START_PROFILE(SMBsplwr
);
4874 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4875 END_PROFILE(SMBsplwr
);
4879 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
4881 if (!check_fsp(conn
, req
, fsp
)) {
4882 END_PROFILE(SMBsplwr
);
4886 if (!CAN_PRINT(conn
)) {
4887 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
4888 END_PROFILE(SMBsplwr
);
4892 if (!CHECK_WRITE(fsp
)) {
4893 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
4894 END_PROFILE(SMBsplwr
);
4898 numtowrite
= SVAL(smb_buf(req
->inbuf
),1);
4900 if (smb_buflen(req
->inbuf
) < numtowrite
+ 3) {
4901 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4902 END_PROFILE(SMBsplwr
);
4906 data
= smb_buf(req
->inbuf
) + 3;
4908 if (write_file(req
,fsp
,data
,-1,numtowrite
) != numtowrite
) {
4909 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
4910 END_PROFILE(SMBsplwr
);
4914 DEBUG( 3, ( "printwrite fnum=%d num=%d\n", fsp
->fnum
, numtowrite
) );
4916 END_PROFILE(SMBsplwr
);
4920 /****************************************************************************
4922 ****************************************************************************/
4924 void reply_mkdir(struct smb_request
*req
)
4926 connection_struct
*conn
= req
->conn
;
4927 char *directory
= NULL
;
4929 SMB_STRUCT_STAT sbuf
;
4930 TALLOC_CTX
*ctx
= talloc_tos();
4932 START_PROFILE(SMBmkdir
);
4934 srvstr_get_path(ctx
, (char *)req
->inbuf
, req
->flags2
, &directory
,
4935 smb_buf(req
->inbuf
) + 1, 0,
4936 STR_TERMINATE
, &status
);
4937 if (!NT_STATUS_IS_OK(status
)) {
4938 reply_nterror(req
, status
);
4939 END_PROFILE(SMBmkdir
);
4943 status
= resolve_dfspath(ctx
, conn
,
4944 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
4947 if (!NT_STATUS_IS_OK(status
)) {
4948 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
4949 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
4950 ERRSRV
, ERRbadpath
);
4951 END_PROFILE(SMBmkdir
);
4954 reply_nterror(req
, status
);
4955 END_PROFILE(SMBmkdir
);
4959 status
= unix_convert(ctx
, conn
, directory
, False
, &directory
, NULL
, &sbuf
);
4960 if (!NT_STATUS_IS_OK(status
)) {
4961 reply_nterror(req
, status
);
4962 END_PROFILE(SMBmkdir
);
4966 status
= check_name(conn
, directory
);
4967 if (!NT_STATUS_IS_OK(status
)) {
4968 reply_nterror(req
, status
);
4969 END_PROFILE(SMBmkdir
);
4973 status
= create_directory(conn
, req
, directory
);
4975 DEBUG(5, ("create_directory returned %s\n", nt_errstr(status
)));
4977 if (!NT_STATUS_IS_OK(status
)) {
4979 if (!use_nt_status()
4980 && NT_STATUS_EQUAL(status
,
4981 NT_STATUS_OBJECT_NAME_COLLISION
)) {
4983 * Yes, in the DOS error code case we get a
4984 * ERRDOS:ERRnoaccess here. See BASE-SAMBA3ERROR
4985 * samba4 torture test.
4987 status
= NT_STATUS_DOS(ERRDOS
, ERRnoaccess
);
4990 reply_nterror(req
, status
);
4991 END_PROFILE(SMBmkdir
);
4995 reply_outbuf(req
, 0, 0);
4997 DEBUG( 3, ( "mkdir %s\n", directory
) );
4999 END_PROFILE(SMBmkdir
);
5003 /****************************************************************************
5004 Static function used by reply_rmdir to delete an entire directory
5005 tree recursively. Return True on ok, False on fail.
5006 ****************************************************************************/
5008 static bool recursive_rmdir(TALLOC_CTX
*ctx
,
5009 connection_struct
*conn
,
5012 const char *dname
= NULL
;
5015 struct smb_Dir
*dir_hnd
= OpenDir(talloc_tos(), conn
, directory
,
5021 while((dname
= ReadDirName(dir_hnd
, &offset
))) {
5022 char *fullname
= NULL
;
5025 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
5029 if (!is_visible_file(conn
, directory
, dname
, &st
, False
)) {
5033 /* Construct the full name. */
5034 fullname
= talloc_asprintf(ctx
,
5044 if(SMB_VFS_LSTAT(conn
,fullname
, &st
) != 0) {
5049 if(st
.st_mode
& S_IFDIR
) {
5050 if(!recursive_rmdir(ctx
, conn
, fullname
)) {
5054 if(SMB_VFS_RMDIR(conn
,fullname
) != 0) {
5058 } else if(SMB_VFS_UNLINK(conn
,fullname
) != 0) {
5062 TALLOC_FREE(fullname
);
5064 TALLOC_FREE(dir_hnd
);
5068 /****************************************************************************
5069 The internals of the rmdir code - called elsewhere.
5070 ****************************************************************************/
5072 NTSTATUS
rmdir_internals(TALLOC_CTX
*ctx
,
5073 connection_struct
*conn
,
5074 const char *directory
)
5079 /* Might be a symlink. */
5080 if(SMB_VFS_LSTAT(conn
, directory
, &st
) != 0) {
5081 return map_nt_error_from_unix(errno
);
5084 if (S_ISLNK(st
.st_mode
)) {
5085 /* Is what it points to a directory ? */
5086 if(SMB_VFS_STAT(conn
, directory
, &st
) != 0) {
5087 return map_nt_error_from_unix(errno
);
5089 if (!(S_ISDIR(st
.st_mode
))) {
5090 return NT_STATUS_NOT_A_DIRECTORY
;
5092 ret
= SMB_VFS_UNLINK(conn
,directory
);
5094 ret
= SMB_VFS_RMDIR(conn
,directory
);
5097 notify_fname(conn
, NOTIFY_ACTION_REMOVED
,
5098 FILE_NOTIFY_CHANGE_DIR_NAME
,
5100 return NT_STATUS_OK
;
5103 if(((errno
== ENOTEMPTY
)||(errno
== EEXIST
)) && lp_veto_files(SNUM(conn
))) {
5105 * Check to see if the only thing in this directory are
5106 * vetoed files/directories. If so then delete them and
5107 * retry. If we fail to delete any of them (and we *don't*
5108 * do a recursive delete) then fail the rmdir.
5112 struct smb_Dir
*dir_hnd
= OpenDir(talloc_tos(), conn
,
5113 directory
, NULL
, 0);
5115 if(dir_hnd
== NULL
) {
5120 while ((dname
= ReadDirName(dir_hnd
,&dirpos
))) {
5121 if((strcmp(dname
, ".") == 0) || (strcmp(dname
, "..")==0))
5123 if (!is_visible_file(conn
, directory
, dname
, &st
, False
))
5125 if(!IS_VETO_PATH(conn
, dname
)) {
5126 TALLOC_FREE(dir_hnd
);
5132 /* We only have veto files/directories.
5133 * Are we allowed to delete them ? */
5135 if(!lp_recursive_veto_delete(SNUM(conn
))) {
5136 TALLOC_FREE(dir_hnd
);
5141 /* Do a recursive delete. */
5142 RewindDir(dir_hnd
,&dirpos
);
5143 while ((dname
= ReadDirName(dir_hnd
,&dirpos
))) {
5144 char *fullname
= NULL
;
5146 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
5149 if (!is_visible_file(conn
, directory
, dname
, &st
, False
)) {
5153 fullname
= talloc_asprintf(ctx
,
5163 if(SMB_VFS_LSTAT(conn
,fullname
, &st
) != 0) {
5166 if(st
.st_mode
& S_IFDIR
) {
5167 if(!recursive_rmdir(ctx
, conn
, fullname
)) {
5170 if(SMB_VFS_RMDIR(conn
,fullname
) != 0) {
5173 } else if(SMB_VFS_UNLINK(conn
,fullname
) != 0) {
5176 TALLOC_FREE(fullname
);
5178 TALLOC_FREE(dir_hnd
);
5179 /* Retry the rmdir */
5180 ret
= SMB_VFS_RMDIR(conn
,directory
);
5186 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
5187 "%s\n", directory
,strerror(errno
)));
5188 return map_nt_error_from_unix(errno
);
5191 notify_fname(conn
, NOTIFY_ACTION_REMOVED
,
5192 FILE_NOTIFY_CHANGE_DIR_NAME
,
5195 return NT_STATUS_OK
;
5198 /****************************************************************************
5200 ****************************************************************************/
5202 void reply_rmdir(struct smb_request
*req
)
5204 connection_struct
*conn
= req
->conn
;
5205 char *directory
= NULL
;
5206 SMB_STRUCT_STAT sbuf
;
5208 TALLOC_CTX
*ctx
= talloc_tos();
5210 START_PROFILE(SMBrmdir
);
5212 srvstr_get_path(ctx
, (char *)req
->inbuf
, req
->flags2
, &directory
,
5213 smb_buf(req
->inbuf
) + 1, 0,
5214 STR_TERMINATE
, &status
);
5215 if (!NT_STATUS_IS_OK(status
)) {
5216 reply_nterror(req
, status
);
5217 END_PROFILE(SMBrmdir
);
5221 status
= resolve_dfspath(ctx
, conn
,
5222 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
5225 if (!NT_STATUS_IS_OK(status
)) {
5226 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
5227 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
5228 ERRSRV
, ERRbadpath
);
5229 END_PROFILE(SMBrmdir
);
5232 reply_nterror(req
, status
);
5233 END_PROFILE(SMBrmdir
);
5237 status
= unix_convert(ctx
, conn
, directory
, False
, &directory
,
5239 if (!NT_STATUS_IS_OK(status
)) {
5240 reply_nterror(req
, status
);
5241 END_PROFILE(SMBrmdir
);
5245 status
= check_name(conn
, directory
);
5246 if (!NT_STATUS_IS_OK(status
)) {
5247 reply_nterror(req
, status
);
5248 END_PROFILE(SMBrmdir
);
5252 dptr_closepath(directory
, req
->smbpid
);
5253 status
= rmdir_internals(ctx
, conn
, directory
);
5254 if (!NT_STATUS_IS_OK(status
)) {
5255 reply_nterror(req
, status
);
5256 END_PROFILE(SMBrmdir
);
5260 reply_outbuf(req
, 0, 0);
5262 DEBUG( 3, ( "rmdir %s\n", directory
) );
5264 END_PROFILE(SMBrmdir
);
5268 /*******************************************************************
5269 Resolve wildcards in a filename rename.
5270 ********************************************************************/
5272 static bool resolve_wildcards(TALLOC_CTX
*ctx
,
5277 char *name2_copy
= NULL
;
5282 char *p
,*p2
, *pname1
, *pname2
;
5284 name2_copy
= talloc_strdup(ctx
, name2
);
5289 pname1
= strrchr_m(name1
,'/');
5290 pname2
= strrchr_m(name2_copy
,'/');
5292 if (!pname1
|| !pname2
) {
5296 /* Truncate the copy of name2 at the last '/' */
5299 /* Now go past the '/' */
5303 root1
= talloc_strdup(ctx
, pname1
);
5304 root2
= talloc_strdup(ctx
, pname2
);
5306 if (!root1
|| !root2
) {
5310 p
= strrchr_m(root1
,'.');
5313 ext1
= talloc_strdup(ctx
, p
+1);
5315 ext1
= talloc_strdup(ctx
, "");
5317 p
= strrchr_m(root2
,'.');
5320 ext2
= talloc_strdup(ctx
, p
+1);
5322 ext2
= talloc_strdup(ctx
, "");
5325 if (!ext1
|| !ext2
) {
5333 /* Hmmm. Should this be mb-aware ? */
5336 } else if (*p2
== '*') {
5338 root2
= talloc_asprintf(ctx
, "%s%s",
5357 /* Hmmm. Should this be mb-aware ? */
5360 } else if (*p2
== '*') {
5362 ext2
= talloc_asprintf(ctx
, "%s%s",
5378 *pp_newname
= talloc_asprintf(ctx
, "%s/%s.%s",
5383 *pp_newname
= talloc_asprintf(ctx
, "%s/%s",
5395 /****************************************************************************
5396 Ensure open files have their names updated. Updated to notify other smbd's
5398 ****************************************************************************/
5400 static void rename_open_files(connection_struct
*conn
,
5401 struct share_mode_lock
*lck
,
5402 const char *newname
)
5405 bool did_rename
= False
;
5407 for(fsp
= file_find_di_first(lck
->id
); fsp
;
5408 fsp
= file_find_di_next(fsp
)) {
5409 /* fsp_name is a relative path under the fsp. To change this for other
5410 sharepaths we need to manipulate relative paths. */
5411 /* TODO - create the absolute path and manipulate the newname
5412 relative to the sharepath. */
5413 if (!strequal(fsp
->conn
->connectpath
, conn
->connectpath
)) {
5416 DEBUG(10,("rename_open_files: renaming file fnum %d (file_id %s) from %s -> %s\n",
5417 fsp
->fnum
, file_id_string_tos(&fsp
->file_id
),
5418 fsp
->fsp_name
, newname
));
5419 string_set(&fsp
->fsp_name
, newname
);
5424 DEBUG(10,("rename_open_files: no open files on file_id %s for %s\n",
5425 file_id_string_tos(&lck
->id
), newname
));
5428 /* Send messages to all smbd's (not ourself) that the name has changed. */
5429 rename_share_filename(smbd_messaging_context(), lck
, conn
->connectpath
,
5433 /****************************************************************************
5434 We need to check if the source path is a parent directory of the destination
5435 (ie. a rename of /foo/bar/baz -> /foo/bar/baz/bibble/bobble. If so we must
5436 refuse the rename with a sharing violation. Under UNIX the above call can
5437 *succeed* if /foo/bar/baz is a symlink to another area in the share. We
5438 probably need to check that the client is a Windows one before disallowing
5439 this as a UNIX client (one with UNIX extensions) can know the source is a
5440 symlink and make this decision intelligently. Found by an excellent bug
5441 report from <AndyLiebman@aol.com>.
5442 ****************************************************************************/
5444 static bool rename_path_prefix_equal(const char *src
, const char *dest
)
5446 const char *psrc
= src
;
5447 const char *pdst
= dest
;
5450 if (psrc
[0] == '.' && psrc
[1] == '/') {
5453 if (pdst
[0] == '.' && pdst
[1] == '/') {
5456 if ((slen
= strlen(psrc
)) > strlen(pdst
)) {
5459 return ((memcmp(psrc
, pdst
, slen
) == 0) && pdst
[slen
] == '/');
5463 * Do the notify calls from a rename
5466 static void notify_rename(connection_struct
*conn
, bool is_dir
,
5467 const char *oldpath
, const char *newpath
)
5469 char *olddir
, *newdir
;
5470 const char *oldname
, *newname
;
5473 mask
= is_dir
? FILE_NOTIFY_CHANGE_DIR_NAME
5474 : FILE_NOTIFY_CHANGE_FILE_NAME
;
5476 if (!parent_dirname_talloc(NULL
, oldpath
, &olddir
, &oldname
)
5477 || !parent_dirname_talloc(NULL
, newpath
, &newdir
, &newname
)) {
5478 TALLOC_FREE(olddir
);
5482 if (strcmp(olddir
, newdir
) == 0) {
5483 notify_fname(conn
, NOTIFY_ACTION_OLD_NAME
, mask
, oldpath
);
5484 notify_fname(conn
, NOTIFY_ACTION_NEW_NAME
, mask
, newpath
);
5487 notify_fname(conn
, NOTIFY_ACTION_REMOVED
, mask
, oldpath
);
5488 notify_fname(conn
, NOTIFY_ACTION_ADDED
, mask
, newpath
);
5490 TALLOC_FREE(olddir
);
5491 TALLOC_FREE(newdir
);
5493 /* this is a strange one. w2k3 gives an additional event for
5494 CHANGE_ATTRIBUTES and CHANGE_CREATION on the new file when renaming
5495 files, but not directories */
5497 notify_fname(conn
, NOTIFY_ACTION_MODIFIED
,
5498 FILE_NOTIFY_CHANGE_ATTRIBUTES
5499 |FILE_NOTIFY_CHANGE_CREATION
,
5504 /****************************************************************************
5505 Rename an open file - given an fsp.
5506 ****************************************************************************/
5508 NTSTATUS
rename_internals_fsp(connection_struct
*conn
,
5511 const char *newname_last_component
,
5513 bool replace_if_exists
)
5515 TALLOC_CTX
*ctx
= talloc_tos();
5516 SMB_STRUCT_STAT sbuf
, sbuf1
;
5517 NTSTATUS status
= NT_STATUS_OK
;
5518 struct share_mode_lock
*lck
= NULL
;
5519 bool dst_exists
, old_is_stream
, new_is_stream
;
5523 status
= check_name(conn
, newname
);
5524 if (!NT_STATUS_IS_OK(status
)) {
5528 /* Ensure newname contains a '/' */
5529 if(strrchr_m(newname
,'/') == 0) {
5530 newname
= talloc_asprintf(ctx
,
5534 return NT_STATUS_NO_MEMORY
;
5539 * Check for special case with case preserving and not
5540 * case sensitive. If the old last component differs from the original
5541 * last component only by case, then we should allow
5542 * the rename (user is trying to change the case of the
5546 if((conn
->case_sensitive
== False
) && (conn
->case_preserve
== True
) &&
5547 strequal(newname
, fsp
->fsp_name
)) {
5549 char *newname_modified_last_component
= NULL
;
5552 * Get the last component of the modified name.
5553 * Note that we guarantee that newname contains a '/'
5556 p
= strrchr_m(newname
,'/');
5557 newname_modified_last_component
= talloc_strdup(ctx
,
5559 if (!newname_modified_last_component
) {
5560 return NT_STATUS_NO_MEMORY
;
5563 if(strcsequal(newname_modified_last_component
,
5564 newname_last_component
) == False
) {
5566 * Replace the modified last component with
5569 *p
= '\0'; /* Truncate at the '/' */
5570 newname
= talloc_asprintf(ctx
,
5573 newname_last_component
);
5578 * If the src and dest names are identical - including case,
5579 * don't do the rename, just return success.
5582 if (strcsequal(fsp
->fsp_name
, newname
)) {
5583 DEBUG(3,("rename_internals_fsp: identical names in rename %s - returning success\n",
5585 return NT_STATUS_OK
;
5588 old_is_stream
= is_ntfs_stream_name(fsp
->fsp_name
);
5589 new_is_stream
= is_ntfs_stream_name(newname
);
5591 /* Return the correct error code if both names aren't streams. */
5592 if (!old_is_stream
&& new_is_stream
) {
5593 return NT_STATUS_OBJECT_NAME_INVALID
;
5596 if (old_is_stream
&& !new_is_stream
) {
5597 return NT_STATUS_INVALID_PARAMETER
;
5601 * Have vfs_object_exist also fill sbuf1
5603 dst_exists
= vfs_object_exist(conn
, newname
, &sbuf1
);
5605 if(!replace_if_exists
&& dst_exists
) {
5606 DEBUG(3,("rename_internals_fsp: dest exists doing rename %s -> %s\n",
5607 fsp
->fsp_name
,newname
));
5608 return NT_STATUS_OBJECT_NAME_COLLISION
;
5612 struct file_id fileid
= vfs_file_id_from_sbuf(conn
, &sbuf1
);
5613 files_struct
*dst_fsp
= file_find_di_first(fileid
);
5614 /* The file can be open when renaming a stream */
5615 if (dst_fsp
&& !new_is_stream
) {
5616 DEBUG(3, ("rename_internals_fsp: Target file open\n"));
5617 return NT_STATUS_ACCESS_DENIED
;
5621 /* Ensure we have a valid stat struct for the source. */
5622 if (fsp
->fh
->fd
!= -1) {
5623 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
5624 return map_nt_error_from_unix(errno
);
5628 if (fsp
->posix_open
) {
5629 ret
= SMB_VFS_LSTAT(conn
,fsp
->fsp_name
,&sbuf
);
5631 ret
= SMB_VFS_STAT(conn
,fsp
->fsp_name
,&sbuf
);
5634 return map_nt_error_from_unix(errno
);
5638 status
= can_rename(conn
, fsp
, attrs
, &sbuf
);
5640 if (!NT_STATUS_IS_OK(status
)) {
5641 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
5642 nt_errstr(status
), fsp
->fsp_name
,newname
));
5643 if (NT_STATUS_EQUAL(status
,NT_STATUS_SHARING_VIOLATION
))
5644 status
= NT_STATUS_ACCESS_DENIED
;
5648 if (rename_path_prefix_equal(fsp
->fsp_name
, newname
)) {
5649 return NT_STATUS_ACCESS_DENIED
;
5652 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
, NULL
, NULL
,
5656 * We have the file open ourselves, so not being able to get the
5657 * corresponding share mode lock is a fatal error.
5660 SMB_ASSERT(lck
!= NULL
);
5662 if(SMB_VFS_RENAME(conn
,fsp
->fsp_name
, newname
) == 0) {
5663 uint32 create_options
= fsp
->fh
->private_options
;
5665 DEBUG(3,("rename_internals_fsp: succeeded doing rename on %s -> %s\n",
5666 fsp
->fsp_name
,newname
));
5668 notify_rename(conn
, fsp
->is_directory
, fsp
->fsp_name
, newname
);
5670 rename_open_files(conn
, lck
, newname
);
5673 * A rename acts as a new file create w.r.t. allowing an initial delete
5674 * on close, probably because in Windows there is a new handle to the
5675 * new file. If initial delete on close was requested but not
5676 * originally set, we need to set it here. This is probably not 100% correct,
5677 * but will work for the CIFSFS client which in non-posix mode
5678 * depends on these semantics. JRA.
5681 if (create_options
& FILE_DELETE_ON_CLOSE
) {
5682 status
= can_set_delete_on_close(fsp
, True
, 0);
5684 if (NT_STATUS_IS_OK(status
)) {
5685 /* Note that here we set the *inital* delete on close flag,
5686 * not the regular one. The magic gets handled in close. */
5687 fsp
->initial_delete_on_close
= True
;
5691 return NT_STATUS_OK
;
5696 if (errno
== ENOTDIR
|| errno
== EISDIR
) {
5697 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
5699 status
= map_nt_error_from_unix(errno
);
5702 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
5703 nt_errstr(status
), fsp
->fsp_name
,newname
));
5708 /****************************************************************************
5709 The guts of the rename command, split out so it may be called by the NT SMB
5711 ****************************************************************************/
5713 NTSTATUS
rename_internals(TALLOC_CTX
*ctx
,
5714 connection_struct
*conn
,
5715 struct smb_request
*req
,
5716 const char *name_in
,
5717 const char *newname_in
,
5719 bool replace_if_exists
,
5722 uint32_t access_mask
)
5724 char *directory
= NULL
;
5726 char *last_component_src
= NULL
;
5727 char *last_component_dest
= NULL
;
5729 char *newname
= NULL
;
5732 NTSTATUS status
= NT_STATUS_OK
;
5733 SMB_STRUCT_STAT sbuf1
, sbuf2
;
5734 struct smb_Dir
*dir_hnd
= NULL
;
5737 bool posix_pathnames
= lp_posix_pathnames();
5742 status
= unix_convert(ctx
, conn
, name_in
, src_has_wild
, &name
,
5743 &last_component_src
, &sbuf1
);
5744 if (!NT_STATUS_IS_OK(status
)) {
5748 status
= unix_convert(ctx
, conn
, newname_in
, dest_has_wild
, &newname
,
5749 &last_component_dest
, &sbuf2
);
5750 if (!NT_STATUS_IS_OK(status
)) {
5755 * Split the old name into directory and last component
5756 * strings. Note that unix_convert may have stripped off a
5757 * leading ./ from both name and newname if the rename is
5758 * at the root of the share. We need to make sure either both
5759 * name and newname contain a / character or neither of them do
5760 * as this is checked in resolve_wildcards().
5763 p
= strrchr_m(name
,'/');
5765 directory
= talloc_strdup(ctx
, ".");
5767 return NT_STATUS_NO_MEMORY
;
5772 directory
= talloc_strdup(ctx
, name
);
5774 return NT_STATUS_NO_MEMORY
;
5777 *p
= '/'; /* Replace needed for exceptional test below. */
5781 * We should only check the mangled cache
5782 * here if unix_convert failed. This means
5783 * that the path in 'mask' doesn't exist
5784 * on the file system and so we need to look
5785 * for a possible mangle. This patch from
5786 * Tine Smukavec <valentin.smukavec@hermes.si>.
5789 if (!VALID_STAT(sbuf1
) && mangle_is_mangled(mask
, conn
->params
)) {
5790 char *new_mask
= NULL
;
5791 mangle_lookup_name_from_8_3(ctx
,
5800 if (!src_has_wild
) {
5804 * No wildcards - just process the one file.
5806 bool is_short_name
= mangle_is_8_3(name
, True
, conn
->params
);
5808 /* Add a terminating '/' to the directory name. */
5809 directory
= talloc_asprintf_append(directory
,
5813 return NT_STATUS_NO_MEMORY
;
5816 /* Ensure newname contains a '/' also */
5817 if(strrchr_m(newname
,'/') == 0) {
5818 newname
= talloc_asprintf(ctx
,
5822 return NT_STATUS_NO_MEMORY
;
5826 DEBUG(3, ("rename_internals: case_sensitive = %d, "
5827 "case_preserve = %d, short case preserve = %d, "
5828 "directory = %s, newname = %s, "
5829 "last_component_dest = %s, is_8_3 = %d\n",
5830 conn
->case_sensitive
, conn
->case_preserve
,
5831 conn
->short_case_preserve
, directory
,
5832 newname
, last_component_dest
, is_short_name
));
5834 /* The dest name still may have wildcards. */
5835 if (dest_has_wild
) {
5836 char *mod_newname
= NULL
;
5837 if (!resolve_wildcards(ctx
,
5838 directory
,newname
,&mod_newname
)) {
5839 DEBUG(6, ("rename_internals: resolve_wildcards "
5843 return NT_STATUS_NO_MEMORY
;
5845 newname
= mod_newname
;
5849 if (posix_pathnames
) {
5850 SMB_VFS_LSTAT(conn
, directory
, &sbuf1
);
5852 SMB_VFS_STAT(conn
, directory
, &sbuf1
);
5855 status
= S_ISDIR(sbuf1
.st_mode
) ?
5856 open_directory(conn
, req
, directory
, &sbuf1
,
5858 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
5861 posix_pathnames
? FILE_FLAG_POSIX_SEMANTICS
|0777 : 0,
5864 : open_file_ntcreate(conn
, req
, directory
, &sbuf1
,
5866 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
5869 posix_pathnames
? FILE_FLAG_POSIX_SEMANTICS
|0777 : 0,
5874 if (!NT_STATUS_IS_OK(status
)) {
5875 DEBUG(3, ("Could not open rename source %s: %s\n",
5876 directory
, nt_errstr(status
)));
5880 status
= rename_internals_fsp(conn
, fsp
, newname
,
5881 last_component_dest
,
5882 attrs
, replace_if_exists
);
5884 close_file(fsp
, NORMAL_CLOSE
);
5886 DEBUG(3, ("rename_internals: Error %s rename %s -> %s\n",
5887 nt_errstr(status
), directory
,newname
));
5893 * Wildcards - process each file that matches.
5895 if (strequal(mask
,"????????.???")) {
5900 status
= check_name(conn
, directory
);
5901 if (!NT_STATUS_IS_OK(status
)) {
5905 dir_hnd
= OpenDir(talloc_tos(), conn
, directory
, mask
, attrs
);
5906 if (dir_hnd
== NULL
) {
5907 return map_nt_error_from_unix(errno
);
5910 status
= NT_STATUS_NO_SUCH_FILE
;
5912 * Was status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5913 * - gentest fix. JRA
5916 while ((dname
= ReadDirName(dir_hnd
, &offset
))) {
5917 files_struct
*fsp
= NULL
;
5919 char *destname
= NULL
;
5920 bool sysdir_entry
= False
;
5922 /* Quick check for "." and ".." */
5923 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
5925 sysdir_entry
= True
;
5931 if (!is_visible_file(conn
, directory
, dname
, &sbuf1
, False
)) {
5935 if(!mask_match(dname
, mask
, conn
->case_sensitive
)) {
5940 status
= NT_STATUS_OBJECT_NAME_INVALID
;
5944 fname
= talloc_asprintf(ctx
,
5949 return NT_STATUS_NO_MEMORY
;
5952 if (!resolve_wildcards(ctx
,
5953 fname
,newname
,&destname
)) {
5954 DEBUG(6, ("resolve_wildcards %s %s failed\n",
5960 return NT_STATUS_NO_MEMORY
;
5964 if (posix_pathnames
) {
5965 SMB_VFS_LSTAT(conn
, fname
, &sbuf1
);
5967 SMB_VFS_STAT(conn
, fname
, &sbuf1
);
5970 status
= S_ISDIR(sbuf1
.st_mode
) ?
5971 open_directory(conn
, req
, fname
, &sbuf1
,
5973 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
5976 posix_pathnames
? FILE_FLAG_POSIX_SEMANTICS
|0777 : 0,
5979 : open_file_ntcreate(conn
, req
, fname
, &sbuf1
,
5981 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
5984 posix_pathnames
? FILE_FLAG_POSIX_SEMANTICS
|0777 : 0,
5989 if (!NT_STATUS_IS_OK(status
)) {
5990 DEBUG(3,("rename_internals: open_file_ntcreate "
5991 "returned %s rename %s -> %s\n",
5992 nt_errstr(status
), directory
, newname
));
5996 status
= rename_internals_fsp(conn
, fsp
, destname
, dname
,
5997 attrs
, replace_if_exists
);
5999 close_file(fsp
, NORMAL_CLOSE
);
6001 if (!NT_STATUS_IS_OK(status
)) {
6002 DEBUG(3, ("rename_internals_fsp returned %s for "
6003 "rename %s -> %s\n", nt_errstr(status
),
6004 directory
, newname
));
6010 DEBUG(3,("rename_internals: doing rename on %s -> "
6011 "%s\n",fname
,destname
));
6014 TALLOC_FREE(destname
);
6016 TALLOC_FREE(dir_hnd
);
6018 if (count
== 0 && NT_STATUS_IS_OK(status
) && errno
!= 0) {
6019 status
= map_nt_error_from_unix(errno
);
6025 /****************************************************************************
6027 ****************************************************************************/
6029 void reply_mv(struct smb_request
*req
)
6031 connection_struct
*conn
= req
->conn
;
6033 char *newname
= NULL
;
6037 bool src_has_wcard
= False
;
6038 bool dest_has_wcard
= False
;
6039 TALLOC_CTX
*ctx
= talloc_tos();
6041 START_PROFILE(SMBmv
);
6044 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
6049 attrs
= SVAL(req
->inbuf
,smb_vwv0
);
6051 p
= smb_buf(req
->inbuf
) + 1;
6052 p
+= srvstr_get_path_wcard(ctx
, (char *)req
->inbuf
, req
->flags2
, &name
, p
,
6053 0, STR_TERMINATE
, &status
,
6055 if (!NT_STATUS_IS_OK(status
)) {
6056 reply_nterror(req
, status
);
6061 p
+= srvstr_get_path_wcard(ctx
, (char *)req
->inbuf
, req
->flags2
, &newname
, p
,
6062 0, STR_TERMINATE
, &status
,
6064 if (!NT_STATUS_IS_OK(status
)) {
6065 reply_nterror(req
, status
);
6070 status
= resolve_dfspath_wcard(ctx
, conn
,
6071 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
6075 if (!NT_STATUS_IS_OK(status
)) {
6076 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
6077 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
6078 ERRSRV
, ERRbadpath
);
6082 reply_nterror(req
, status
);
6087 status
= resolve_dfspath_wcard(ctx
, conn
,
6088 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
6092 if (!NT_STATUS_IS_OK(status
)) {
6093 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
6094 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
6095 ERRSRV
, ERRbadpath
);
6099 reply_nterror(req
, status
);
6104 DEBUG(3,("reply_mv : %s -> %s\n",name
,newname
));
6106 status
= rename_internals(ctx
, conn
, req
, name
, newname
, attrs
, False
,
6107 src_has_wcard
, dest_has_wcard
, DELETE_ACCESS
);
6108 if (!NT_STATUS_IS_OK(status
)) {
6109 if (open_was_deferred(req
->mid
)) {
6110 /* We have re-scheduled this call. */
6114 reply_nterror(req
, status
);
6119 reply_outbuf(req
, 0, 0);
6125 /*******************************************************************
6126 Copy a file as part of a reply_copy.
6127 ******************************************************************/
6130 * TODO: check error codes on all callers
6133 NTSTATUS
copy_file(TALLOC_CTX
*ctx
,
6134 connection_struct
*conn
,
6139 bool target_is_directory
)
6141 SMB_STRUCT_STAT src_sbuf
, sbuf2
;
6143 files_struct
*fsp1
,*fsp2
;
6146 uint32 new_create_disposition
;
6149 dest
= talloc_strdup(ctx
, dest1
);
6151 return NT_STATUS_NO_MEMORY
;
6153 if (target_is_directory
) {
6154 const char *p
= strrchr_m(src
,'/');
6160 dest
= talloc_asprintf_append(dest
,
6164 return NT_STATUS_NO_MEMORY
;
6168 if (!vfs_file_exist(conn
,src
,&src_sbuf
)) {
6170 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
6173 if (!target_is_directory
&& count
) {
6174 new_create_disposition
= FILE_OPEN
;
6176 if (!map_open_params_to_ntcreate(dest1
,0,ofun
,
6177 NULL
, NULL
, &new_create_disposition
, NULL
)) {
6179 return NT_STATUS_INVALID_PARAMETER
;
6183 status
= open_file_ntcreate(conn
, NULL
, src
, &src_sbuf
,
6185 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
6188 FILE_ATTRIBUTE_NORMAL
,
6192 if (!NT_STATUS_IS_OK(status
)) {
6197 dosattrs
= dos_mode(conn
, src
, &src_sbuf
);
6198 if (SMB_VFS_STAT(conn
,dest
,&sbuf2
) == -1) {
6199 ZERO_STRUCTP(&sbuf2
);
6202 status
= open_file_ntcreate(conn
, NULL
, dest
, &sbuf2
,
6204 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
6205 new_create_disposition
,
6213 if (!NT_STATUS_IS_OK(status
)) {
6214 close_file(fsp1
,ERROR_CLOSE
);
6218 if ((ofun
&3) == 1) {
6219 if(SMB_VFS_LSEEK(fsp2
,0,SEEK_END
) == -1) {
6220 DEBUG(0,("copy_file: error - vfs lseek returned error %s\n", strerror(errno
) ));
6222 * Stop the copy from occurring.
6225 src_sbuf
.st_size
= 0;
6229 if (src_sbuf
.st_size
) {
6230 ret
= vfs_transfer_file(fsp1
, fsp2
, src_sbuf
.st_size
);
6233 close_file(fsp1
,NORMAL_CLOSE
);
6235 /* Ensure the modtime is set correctly on the destination file. */
6236 set_close_write_time(fsp2
, get_mtimespec(&src_sbuf
));
6239 * As we are opening fsp1 read-only we only expect
6240 * an error on close on fsp2 if we are out of space.
6241 * Thus we don't look at the error return from the
6244 status
= close_file(fsp2
,NORMAL_CLOSE
);
6246 if (!NT_STATUS_IS_OK(status
)) {
6250 if (ret
!= (SMB_OFF_T
)src_sbuf
.st_size
) {
6251 return NT_STATUS_DISK_FULL
;
6254 return NT_STATUS_OK
;
6257 /****************************************************************************
6258 Reply to a file copy.
6259 ****************************************************************************/
6261 void reply_copy(struct smb_request
*req
)
6263 connection_struct
*conn
= req
->conn
;
6265 char *newname
= NULL
;
6266 char *directory
= NULL
;
6270 int error
= ERRnoaccess
;
6275 bool target_is_directory
=False
;
6276 bool source_has_wild
= False
;
6277 bool dest_has_wild
= False
;
6278 SMB_STRUCT_STAT sbuf1
, sbuf2
;
6280 TALLOC_CTX
*ctx
= talloc_tos();
6282 START_PROFILE(SMBcopy
);
6285 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
6286 END_PROFILE(SMBcopy
);
6290 tid2
= SVAL(req
->inbuf
,smb_vwv0
);
6291 ofun
= SVAL(req
->inbuf
,smb_vwv1
);
6292 flags
= SVAL(req
->inbuf
,smb_vwv2
);
6294 p
= smb_buf(req
->inbuf
);
6295 p
+= srvstr_get_path_wcard(ctx
, (char *)req
->inbuf
, req
->flags2
, &name
, p
,
6296 0, STR_TERMINATE
, &status
,
6298 if (!NT_STATUS_IS_OK(status
)) {
6299 reply_nterror(req
, status
);
6300 END_PROFILE(SMBcopy
);
6303 p
+= srvstr_get_path_wcard(ctx
, (char *)req
->inbuf
, req
->flags2
, &newname
, p
,
6304 0, STR_TERMINATE
, &status
,
6306 if (!NT_STATUS_IS_OK(status
)) {
6307 reply_nterror(req
, status
);
6308 END_PROFILE(SMBcopy
);
6312 DEBUG(3,("reply_copy : %s -> %s\n",name
,newname
));
6314 if (tid2
!= conn
->cnum
) {
6315 /* can't currently handle inter share copies XXXX */
6316 DEBUG(3,("Rejecting inter-share copy\n"));
6317 reply_doserror(req
, ERRSRV
, ERRinvdevice
);
6318 END_PROFILE(SMBcopy
);
6322 status
= resolve_dfspath_wcard(ctx
, conn
,
6323 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
6327 if (!NT_STATUS_IS_OK(status
)) {
6328 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
6329 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
6330 ERRSRV
, ERRbadpath
);
6331 END_PROFILE(SMBcopy
);
6334 reply_nterror(req
, status
);
6335 END_PROFILE(SMBcopy
);
6339 status
= resolve_dfspath_wcard(ctx
, conn
,
6340 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
6344 if (!NT_STATUS_IS_OK(status
)) {
6345 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
6346 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
6347 ERRSRV
, ERRbadpath
);
6348 END_PROFILE(SMBcopy
);
6351 reply_nterror(req
, status
);
6352 END_PROFILE(SMBcopy
);
6356 status
= unix_convert(ctx
, conn
, name
, source_has_wild
,
6357 &name
, NULL
, &sbuf1
);
6358 if (!NT_STATUS_IS_OK(status
)) {
6359 reply_nterror(req
, status
);
6360 END_PROFILE(SMBcopy
);
6364 status
= unix_convert(ctx
, conn
, newname
, dest_has_wild
,
6365 &newname
, NULL
, &sbuf2
);
6366 if (!NT_STATUS_IS_OK(status
)) {
6367 reply_nterror(req
, status
);
6368 END_PROFILE(SMBcopy
);
6372 target_is_directory
= VALID_STAT_OF_DIR(sbuf2
);
6374 if ((flags
&1) && target_is_directory
) {
6375 reply_doserror(req
, ERRDOS
, ERRbadfile
);
6376 END_PROFILE(SMBcopy
);
6380 if ((flags
&2) && !target_is_directory
) {
6381 reply_doserror(req
, ERRDOS
, ERRbadpath
);
6382 END_PROFILE(SMBcopy
);
6386 if ((flags
&(1<<5)) && VALID_STAT_OF_DIR(sbuf1
)) {
6387 /* wants a tree copy! XXXX */
6388 DEBUG(3,("Rejecting tree copy\n"));
6389 reply_doserror(req
, ERRSRV
, ERRerror
);
6390 END_PROFILE(SMBcopy
);
6394 p
= strrchr_m(name
,'/');
6396 directory
= talloc_strdup(ctx
, "./");
6398 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
6399 END_PROFILE(SMBcopy
);
6405 directory
= talloc_strdup(ctx
, name
);
6407 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
6408 END_PROFILE(SMBcopy
);
6415 * We should only check the mangled cache
6416 * here if unix_convert failed. This means
6417 * that the path in 'mask' doesn't exist
6418 * on the file system and so we need to look
6419 * for a possible mangle. This patch from
6420 * Tine Smukavec <valentin.smukavec@hermes.si>.
6423 if (!VALID_STAT(sbuf1
) && mangle_is_mangled(mask
, conn
->params
)) {
6424 char *new_mask
= NULL
;
6425 mangle_lookup_name_from_8_3(ctx
,
6434 if (!source_has_wild
) {
6435 directory
= talloc_asprintf_append(directory
,
6438 if (dest_has_wild
) {
6439 char *mod_newname
= NULL
;
6440 if (!resolve_wildcards(ctx
,
6441 directory
,newname
,&mod_newname
)) {
6442 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
6443 END_PROFILE(SMBcopy
);
6446 newname
= mod_newname
;
6449 status
= check_name(conn
, directory
);
6450 if (!NT_STATUS_IS_OK(status
)) {
6451 reply_nterror(req
, status
);
6452 END_PROFILE(SMBcopy
);
6456 status
= check_name(conn
, newname
);
6457 if (!NT_STATUS_IS_OK(status
)) {
6458 reply_nterror(req
, status
);
6459 END_PROFILE(SMBcopy
);
6463 status
= copy_file(ctx
,conn
,directory
,newname
,ofun
,
6464 count
,target_is_directory
);
6466 if(!NT_STATUS_IS_OK(status
)) {
6467 reply_nterror(req
, status
);
6468 END_PROFILE(SMBcopy
);
6474 struct smb_Dir
*dir_hnd
= NULL
;
6475 const char *dname
= NULL
;
6478 if (strequal(mask
,"????????.???")) {
6483 status
= check_name(conn
, directory
);
6484 if (!NT_STATUS_IS_OK(status
)) {
6485 reply_nterror(req
, status
);
6486 END_PROFILE(SMBcopy
);
6490 dir_hnd
= OpenDir(talloc_tos(), conn
, directory
, mask
, 0);
6491 if (dir_hnd
== NULL
) {
6492 status
= map_nt_error_from_unix(errno
);
6493 reply_nterror(req
, status
);
6494 END_PROFILE(SMBcopy
);
6500 while ((dname
= ReadDirName(dir_hnd
, &offset
))) {
6501 char *destname
= NULL
;
6504 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
6508 if (!is_visible_file(conn
, directory
, dname
, &sbuf1
, False
)) {
6512 if(!mask_match(dname
, mask
, conn
->case_sensitive
)) {
6516 error
= ERRnoaccess
;
6517 fname
= talloc_asprintf(ctx
,
6522 TALLOC_FREE(dir_hnd
);
6523 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
6524 END_PROFILE(SMBcopy
);
6528 if (!resolve_wildcards(ctx
,
6529 fname
,newname
,&destname
)) {
6533 TALLOC_FREE(dir_hnd
);
6534 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
6535 END_PROFILE(SMBcopy
);
6539 status
= check_name(conn
, fname
);
6540 if (!NT_STATUS_IS_OK(status
)) {
6541 TALLOC_FREE(dir_hnd
);
6542 reply_nterror(req
, status
);
6543 END_PROFILE(SMBcopy
);
6547 status
= check_name(conn
, destname
);
6548 if (!NT_STATUS_IS_OK(status
)) {
6549 TALLOC_FREE(dir_hnd
);
6550 reply_nterror(req
, status
);
6551 END_PROFILE(SMBcopy
);
6555 DEBUG(3,("reply_copy : doing copy on %s -> %s\n",fname
, destname
));
6557 status
= copy_file(ctx
,conn
,fname
,destname
,ofun
,
6558 count
,target_is_directory
);
6559 if (NT_STATUS_IS_OK(status
)) {
6563 TALLOC_FREE(destname
);
6565 TALLOC_FREE(dir_hnd
);
6570 /* Error on close... */
6572 reply_unixerror(req
, ERRHRD
, ERRgeneral
);
6573 END_PROFILE(SMBcopy
);
6577 reply_doserror(req
, ERRDOS
, error
);
6578 END_PROFILE(SMBcopy
);
6582 reply_outbuf(req
, 1, 0);
6583 SSVAL(req
->outbuf
,smb_vwv0
,count
);
6585 END_PROFILE(SMBcopy
);
6590 #define DBGC_CLASS DBGC_LOCKING
6592 /****************************************************************************
6593 Get a lock pid, dealing with large count requests.
6594 ****************************************************************************/
6596 uint32
get_lock_pid( char *data
, int data_offset
, bool large_file_format
)
6598 if(!large_file_format
)
6599 return (uint32
)SVAL(data
,SMB_LPID_OFFSET(data_offset
));
6601 return (uint32
)SVAL(data
,SMB_LARGE_LPID_OFFSET(data_offset
));
6604 /****************************************************************************
6605 Get a lock count, dealing with large count requests.
6606 ****************************************************************************/
6608 SMB_BIG_UINT
get_lock_count( char *data
, int data_offset
, bool large_file_format
)
6610 SMB_BIG_UINT count
= 0;
6612 if(!large_file_format
) {
6613 count
= (SMB_BIG_UINT
)IVAL(data
,SMB_LKLEN_OFFSET(data_offset
));
6616 #if defined(HAVE_LONGLONG)
6617 count
= (((SMB_BIG_UINT
) IVAL(data
,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset
))) << 32) |
6618 ((SMB_BIG_UINT
) IVAL(data
,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset
)));
6619 #else /* HAVE_LONGLONG */
6622 * NT4.x seems to be broken in that it sends large file (64 bit)
6623 * lockingX calls even if the CAP_LARGE_FILES was *not*
6624 * negotiated. For boxes without large unsigned ints truncate the
6625 * lock count by dropping the top 32 bits.
6628 if(IVAL(data
,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset
)) != 0) {
6629 DEBUG(3,("get_lock_count: truncating lock count (high)0x%x (low)0x%x to just low count.\n",
6630 (unsigned int)IVAL(data
,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset
)),
6631 (unsigned int)IVAL(data
,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset
)) ));
6632 SIVAL(data
,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset
),0);
6635 count
= (SMB_BIG_UINT
)IVAL(data
,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset
));
6636 #endif /* HAVE_LONGLONG */
6642 #if !defined(HAVE_LONGLONG)
6643 /****************************************************************************
6644 Pathetically try and map a 64 bit lock offset into 31 bits. I hate Windows :-).
6645 ****************************************************************************/
6647 static uint32
map_lock_offset(uint32 high
, uint32 low
)
6651 uint32 highcopy
= high
;
6654 * Try and find out how many significant bits there are in high.
6657 for(i
= 0; highcopy
; i
++)
6661 * We use 31 bits not 32 here as POSIX
6662 * lock offsets may not be negative.
6665 mask
= (~0) << (31 - i
);
6668 return 0; /* Fail. */
6674 #endif /* !defined(HAVE_LONGLONG) */
6676 /****************************************************************************
6677 Get a lock offset, dealing with large offset requests.
6678 ****************************************************************************/
6680 SMB_BIG_UINT
get_lock_offset( char *data
, int data_offset
, bool large_file_format
, bool *err
)
6682 SMB_BIG_UINT offset
= 0;
6686 if(!large_file_format
) {
6687 offset
= (SMB_BIG_UINT
)IVAL(data
,SMB_LKOFF_OFFSET(data_offset
));
6690 #if defined(HAVE_LONGLONG)
6691 offset
= (((SMB_BIG_UINT
) IVAL(data
,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset
))) << 32) |
6692 ((SMB_BIG_UINT
) IVAL(data
,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset
)));
6693 #else /* HAVE_LONGLONG */
6696 * NT4.x seems to be broken in that it sends large file (64 bit)
6697 * lockingX calls even if the CAP_LARGE_FILES was *not*
6698 * negotiated. For boxes without large unsigned ints mangle the
6699 * lock offset by mapping the top 32 bits onto the lower 32.
6702 if(IVAL(data
,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset
)) != 0) {
6703 uint32 low
= IVAL(data
,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset
));
6704 uint32 high
= IVAL(data
,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset
));
6707 if((new_low
= map_lock_offset(high
, low
)) == 0) {
6709 return (SMB_BIG_UINT
)-1;
6712 DEBUG(3,("get_lock_offset: truncating lock offset (high)0x%x (low)0x%x to offset 0x%x.\n",
6713 (unsigned int)high
, (unsigned int)low
, (unsigned int)new_low
));
6714 SIVAL(data
,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset
),0);
6715 SIVAL(data
,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset
),new_low
);
6718 offset
= (SMB_BIG_UINT
)IVAL(data
,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset
));
6719 #endif /* HAVE_LONGLONG */
6725 /****************************************************************************
6726 Reply to a lockingX request.
6727 ****************************************************************************/
6729 void reply_lockingX(struct smb_request
*req
)
6731 connection_struct
*conn
= req
->conn
;
6733 unsigned char locktype
;
6734 unsigned char oplocklevel
;
6737 SMB_BIG_UINT count
= 0, offset
= 0;
6742 bool large_file_format
;
6744 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
6746 START_PROFILE(SMBlockingX
);
6749 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
6750 END_PROFILE(SMBlockingX
);
6754 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv2
));
6755 locktype
= CVAL(req
->inbuf
,smb_vwv3
);
6756 oplocklevel
= CVAL(req
->inbuf
,smb_vwv3
+1);
6757 num_ulocks
= SVAL(req
->inbuf
,smb_vwv6
);
6758 num_locks
= SVAL(req
->inbuf
,smb_vwv7
);
6759 lock_timeout
= IVAL(req
->inbuf
,smb_vwv4
);
6760 large_file_format
= (locktype
& LOCKING_ANDX_LARGE_FILES
)?True
:False
;
6762 if (!check_fsp(conn
, req
, fsp
)) {
6763 END_PROFILE(SMBlockingX
);
6767 data
= smb_buf(req
->inbuf
);
6769 if (locktype
& LOCKING_ANDX_CHANGE_LOCKTYPE
) {
6770 /* we don't support these - and CANCEL_LOCK makes w2k
6771 and XP reboot so I don't really want to be
6772 compatible! (tridge) */
6773 reply_nterror(req
, NT_STATUS_DOS(ERRDOS
, ERRnoatomiclocks
));
6774 END_PROFILE(SMBlockingX
);
6778 /* Check if this is an oplock break on a file
6779 we have granted an oplock on.
6781 if ((locktype
& LOCKING_ANDX_OPLOCK_RELEASE
)) {
6782 /* Client can insist on breaking to none. */
6783 bool break_to_none
= (oplocklevel
== 0);
6786 DEBUG(5,("reply_lockingX: oplock break reply (%u) from client "
6787 "for fnum = %d\n", (unsigned int)oplocklevel
,
6791 * Make sure we have granted an exclusive or batch oplock on
6795 if (fsp
->oplock_type
== 0) {
6797 /* The Samba4 nbench simulator doesn't understand
6798 the difference between break to level2 and break
6799 to none from level2 - it sends oplock break
6800 replies in both cases. Don't keep logging an error
6801 message here - just ignore it. JRA. */
6803 DEBUG(5,("reply_lockingX: Error : oplock break from "
6804 "client for fnum = %d (oplock=%d) and no "
6805 "oplock granted on this file (%s).\n",
6806 fsp
->fnum
, fsp
->oplock_type
, fsp
->fsp_name
));
6808 /* if this is a pure oplock break request then don't
6810 if (num_locks
== 0 && num_ulocks
== 0) {
6811 END_PROFILE(SMBlockingX
);
6814 END_PROFILE(SMBlockingX
);
6815 reply_doserror(req
, ERRDOS
, ERRlock
);
6820 if ((fsp
->sent_oplock_break
== BREAK_TO_NONE_SENT
) ||
6822 result
= remove_oplock(fsp
);
6824 result
= downgrade_oplock(fsp
);
6828 DEBUG(0, ("reply_lockingX: error in removing "
6829 "oplock on file %s\n", fsp
->fsp_name
));
6830 /* Hmmm. Is this panic justified? */
6831 smb_panic("internal tdb error");
6834 reply_to_oplock_break_requests(fsp
);
6836 /* if this is a pure oplock break request then don't send a
6838 if (num_locks
== 0 && num_ulocks
== 0) {
6839 /* Sanity check - ensure a pure oplock break is not a
6841 if(CVAL(req
->inbuf
,smb_vwv0
) != 0xff)
6842 DEBUG(0,("reply_lockingX: Error : pure oplock "
6843 "break is a chained %d request !\n",
6844 (unsigned int)CVAL(req
->inbuf
,
6846 END_PROFILE(SMBlockingX
);
6852 * We do this check *after* we have checked this is not a oplock break
6853 * response message. JRA.
6856 release_level_2_oplocks_on_change(fsp
);
6858 if (smb_buflen(req
->inbuf
) <
6859 (num_ulocks
+ num_locks
) * (large_file_format
? 20 : 10)) {
6860 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
6861 END_PROFILE(SMBlockingX
);
6865 /* Data now points at the beginning of the list
6866 of smb_unlkrng structs */
6867 for(i
= 0; i
< (int)num_ulocks
; i
++) {
6868 lock_pid
= get_lock_pid( data
, i
, large_file_format
);
6869 count
= get_lock_count( data
, i
, large_file_format
);
6870 offset
= get_lock_offset( data
, i
, large_file_format
, &err
);
6873 * There is no error code marked "stupid client bug".... :-).
6876 END_PROFILE(SMBlockingX
);
6877 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
6881 DEBUG(10,("reply_lockingX: unlock start=%.0f, len=%.0f for "
6882 "pid %u, file %s\n", (double)offset
, (double)count
,
6883 (unsigned int)lock_pid
, fsp
->fsp_name
));
6885 status
= do_unlock(smbd_messaging_context(),
6892 if (NT_STATUS_V(status
)) {
6893 END_PROFILE(SMBlockingX
);
6894 reply_nterror(req
, status
);
6899 /* Setup the timeout in seconds. */
6901 if (!lp_blocking_locks(SNUM(conn
))) {
6905 /* Now do any requested locks */
6906 data
+= ((large_file_format
? 20 : 10)*num_ulocks
);
6908 /* Data now points at the beginning of the list
6909 of smb_lkrng structs */
6911 for(i
= 0; i
< (int)num_locks
; i
++) {
6912 enum brl_type lock_type
= ((locktype
& LOCKING_ANDX_SHARED_LOCK
) ?
6913 READ_LOCK
:WRITE_LOCK
);
6914 lock_pid
= get_lock_pid( data
, i
, large_file_format
);
6915 count
= get_lock_count( data
, i
, large_file_format
);
6916 offset
= get_lock_offset( data
, i
, large_file_format
, &err
);
6919 * There is no error code marked "stupid client bug".... :-).
6922 END_PROFILE(SMBlockingX
);
6923 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
6927 DEBUG(10,("reply_lockingX: lock start=%.0f, len=%.0f for pid "
6928 "%u, file %s timeout = %d\n", (double)offset
,
6929 (double)count
, (unsigned int)lock_pid
,
6930 fsp
->fsp_name
, (int)lock_timeout
));
6932 if (locktype
& LOCKING_ANDX_CANCEL_LOCK
) {
6933 if (lp_blocking_locks(SNUM(conn
))) {
6935 /* Schedule a message to ourselves to
6936 remove the blocking lock record and
6937 return the right error. */
6939 if (!blocking_lock_cancel(fsp
,
6945 NT_STATUS_FILE_LOCK_CONFLICT
)) {
6946 END_PROFILE(SMBlockingX
);
6951 ERRcancelviolation
));
6955 /* Remove a matching pending lock. */
6956 status
= do_lock_cancel(fsp
,
6962 bool blocking_lock
= lock_timeout
? True
: False
;
6963 bool defer_lock
= False
;
6964 struct byte_range_lock
*br_lck
;
6965 uint32 block_smbpid
;
6967 br_lck
= do_lock(smbd_messaging_context(),
6978 if (br_lck
&& blocking_lock
&& ERROR_WAS_LOCK_DENIED(status
)) {
6979 /* Windows internal resolution for blocking locks seems
6980 to be about 200ms... Don't wait for less than that. JRA. */
6981 if (lock_timeout
!= -1 && lock_timeout
< lp_lock_spin_time()) {
6982 lock_timeout
= lp_lock_spin_time();
6987 /* This heuristic seems to match W2K3 very well. If a
6988 lock sent with timeout of zero would fail with NT_STATUS_FILE_LOCK_CONFLICT
6989 it pretends we asked for a timeout of between 150 - 300 milliseconds as
6990 far as I can tell. Replacement for do_lock_spin(). JRA. */
6992 if (br_lck
&& lp_blocking_locks(SNUM(conn
)) && !blocking_lock
&&
6993 NT_STATUS_EQUAL((status
), NT_STATUS_FILE_LOCK_CONFLICT
)) {
6995 lock_timeout
= lp_lock_spin_time();
6998 if (br_lck
&& defer_lock
) {
7000 * A blocking lock was requested. Package up
7001 * this smb into a queued request and push it
7002 * onto the blocking lock queue.
7004 if(push_blocking_lock_request(br_lck
,
7015 TALLOC_FREE(br_lck
);
7016 END_PROFILE(SMBlockingX
);
7021 TALLOC_FREE(br_lck
);
7024 if (NT_STATUS_V(status
)) {
7025 END_PROFILE(SMBlockingX
);
7026 reply_nterror(req
, status
);
7031 /* If any of the above locks failed, then we must unlock
7032 all of the previous locks (X/Open spec). */
7034 if (!(locktype
& LOCKING_ANDX_CANCEL_LOCK
) &&
7038 * Ensure we don't do a remove on the lock that just failed,
7039 * as under POSIX rules, if we have a lock already there, we
7040 * will delete it (and we shouldn't) .....
7042 for(i
--; i
>= 0; i
--) {
7043 lock_pid
= get_lock_pid( data
, i
, large_file_format
);
7044 count
= get_lock_count( data
, i
, large_file_format
);
7045 offset
= get_lock_offset( data
, i
, large_file_format
,
7049 * There is no error code marked "stupid client
7053 END_PROFILE(SMBlockingX
);
7054 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
7058 do_unlock(smbd_messaging_context(),
7065 END_PROFILE(SMBlockingX
);
7066 reply_nterror(req
, status
);
7070 reply_outbuf(req
, 2, 0);
7072 DEBUG(3, ("lockingX fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
7073 fsp
->fnum
, (unsigned int)locktype
, num_locks
, num_ulocks
));
7075 END_PROFILE(SMBlockingX
);
7080 #define DBGC_CLASS DBGC_ALL
7082 /****************************************************************************
7083 Reply to a SMBreadbmpx (read block multiplex) request.
7084 Always reply with an error, if someone has a platform really needs this,
7085 please contact vl@samba.org
7086 ****************************************************************************/
7088 void reply_readbmpx(struct smb_request
*req
)
7090 START_PROFILE(SMBreadBmpx
);
7091 reply_doserror(req
, ERRSRV
, ERRuseSTD
);
7092 END_PROFILE(SMBreadBmpx
);
7096 /****************************************************************************
7097 Reply to a SMBreadbs (read block multiplex secondary) request.
7098 Always reply with an error, if someone has a platform really needs this,
7099 please contact vl@samba.org
7100 ****************************************************************************/
7102 void reply_readbs(struct smb_request
*req
)
7104 START_PROFILE(SMBreadBs
);
7105 reply_doserror(req
, ERRSRV
, ERRuseSTD
);
7106 END_PROFILE(SMBreadBs
);
7110 /****************************************************************************
7111 Reply to a SMBsetattrE.
7112 ****************************************************************************/
7114 void reply_setattrE(struct smb_request
*req
)
7116 connection_struct
*conn
= req
->conn
;
7117 struct timespec ts
[2];
7119 SMB_STRUCT_STAT sbuf
;
7122 START_PROFILE(SMBsetattrE
);
7125 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
7126 END_PROFILE(SMBsetattrE
);
7130 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
7132 if(!fsp
|| (fsp
->conn
!= conn
)) {
7133 reply_doserror(req
, ERRDOS
, ERRbadfid
);
7134 END_PROFILE(SMBsetattrE
);
7140 * Convert the DOS times into unix times. Ignore create
7141 * time as UNIX can't set this.
7144 ts
[0] = convert_time_t_to_timespec(
7145 srv_make_unix_date2(req
->inbuf
+smb_vwv3
)); /* atime. */
7146 ts
[1] = convert_time_t_to_timespec(
7147 srv_make_unix_date2(req
->inbuf
+smb_vwv5
)); /* mtime. */
7149 reply_outbuf(req
, 0, 0);
7152 * Patch from Ray Frush <frush@engr.colostate.edu>
7153 * Sometimes times are sent as zero - ignore them.
7156 /* Ensure we have a valid stat struct for the source. */
7157 if (fsp
->fh
->fd
!= -1) {
7158 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
7159 status
= map_nt_error_from_unix(errno
);
7160 reply_nterror(req
, status
);
7161 END_PROFILE(SMBsetattrE
);
7167 if (fsp
->posix_open
) {
7168 ret
= SMB_VFS_LSTAT(conn
, fsp
->fsp_name
, &sbuf
);
7170 ret
= SMB_VFS_STAT(conn
, fsp
->fsp_name
, &sbuf
);
7173 status
= map_nt_error_from_unix(errno
);
7174 reply_nterror(req
, status
);
7175 END_PROFILE(SMBsetattrE
);
7180 status
= smb_set_file_time(conn
, fsp
, fsp
->fsp_name
,
7182 if (!NT_STATUS_IS_OK(status
)) {
7183 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
7184 END_PROFILE(SMBsetattrE
);
7188 DEBUG( 3, ( "reply_setattrE fnum=%d actime=%u modtime=%u\n",
7190 (unsigned int)ts
[0].tv_sec
,
7191 (unsigned int)ts
[1].tv_sec
));
7193 END_PROFILE(SMBsetattrE
);
7198 /* Back from the dead for OS/2..... JRA. */
7200 /****************************************************************************
7201 Reply to a SMBwritebmpx (write block multiplex primary) request.
7202 Always reply with an error, if someone has a platform really needs this,
7203 please contact vl@samba.org
7204 ****************************************************************************/
7206 void reply_writebmpx(struct smb_request
*req
)
7208 START_PROFILE(SMBwriteBmpx
);
7209 reply_doserror(req
, ERRSRV
, ERRuseSTD
);
7210 END_PROFILE(SMBwriteBmpx
);
7214 /****************************************************************************
7215 Reply to a SMBwritebs (write block multiplex secondary) request.
7216 Always reply with an error, if someone has a platform really needs this,
7217 please contact vl@samba.org
7218 ****************************************************************************/
7220 void reply_writebs(struct smb_request
*req
)
7222 START_PROFILE(SMBwriteBs
);
7223 reply_doserror(req
, ERRSRV
, ERRuseSTD
);
7224 END_PROFILE(SMBwriteBs
);
7228 /****************************************************************************
7229 Reply to a SMBgetattrE.
7230 ****************************************************************************/
7232 void reply_getattrE(struct smb_request
*req
)
7234 connection_struct
*conn
= req
->conn
;
7235 SMB_STRUCT_STAT sbuf
;
7238 struct timespec create_ts
;
7240 START_PROFILE(SMBgetattrE
);
7243 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
7244 END_PROFILE(SMBgetattrE
);
7248 fsp
= file_fsp(SVAL(req
->inbuf
,smb_vwv0
));
7250 if(!fsp
|| (fsp
->conn
!= conn
)) {
7251 reply_doserror(req
, ERRDOS
, ERRbadfid
);
7252 END_PROFILE(SMBgetattrE
);
7256 /* Do an fstat on this file */
7257 if(fsp_stat(fsp
, &sbuf
)) {
7258 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
7259 END_PROFILE(SMBgetattrE
);
7263 mode
= dos_mode(conn
,fsp
->fsp_name
,&sbuf
);
7266 * Convert the times into dos times. Set create
7267 * date to be last modify date as UNIX doesn't save
7271 reply_outbuf(req
, 11, 0);
7273 create_ts
= get_create_timespec(&sbuf
,
7274 lp_fake_dir_create_times(SNUM(conn
)));
7275 srv_put_dos_date2((char *)req
->outbuf
, smb_vwv0
, create_ts
.tv_sec
);
7276 srv_put_dos_date2((char *)req
->outbuf
, smb_vwv2
, sbuf
.st_atime
);
7277 /* Should we check pending modtime here ? JRA */
7278 srv_put_dos_date2((char *)req
->outbuf
, smb_vwv4
, sbuf
.st_mtime
);
7281 SIVAL(req
->outbuf
, smb_vwv6
, 0);
7282 SIVAL(req
->outbuf
, smb_vwv8
, 0);
7284 uint32 allocation_size
= get_allocation_size(conn
,fsp
, &sbuf
);
7285 SIVAL(req
->outbuf
, smb_vwv6
, (uint32
)sbuf
.st_size
);
7286 SIVAL(req
->outbuf
, smb_vwv8
, allocation_size
);
7288 SSVAL(req
->outbuf
,smb_vwv10
, mode
);
7290 DEBUG( 3, ( "reply_getattrE fnum=%d\n", fsp
->fnum
));
7292 END_PROFILE(SMBgetattrE
);