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
28 #include "smbd/globals.h"
30 extern enum protocol_types Protocol
;
32 /****************************************************************************
33 Ensure we check the path in *exactly* the same way as W2K for a findfirst/findnext
34 path or anything including wildcards.
35 We're assuming here that '/' is not the second byte in any multibyte char
36 set (a safe assumption). '\\' *may* be the second byte in a multibyte char
38 ****************************************************************************/
40 /* Custom version for processing POSIX paths. */
41 #define IS_PATH_SEP(c,posix_only) ((c) == '/' || (!(posix_only) && (c) == '\\'))
43 static NTSTATUS
check_path_syntax_internal(char *path
,
45 bool *p_last_component_contains_wcard
)
49 NTSTATUS ret
= NT_STATUS_OK
;
50 bool start_of_name_component
= True
;
51 bool stream_started
= false;
53 *p_last_component_contains_wcard
= False
;
60 return NT_STATUS_OBJECT_NAME_INVALID
;
63 return NT_STATUS_OBJECT_NAME_INVALID
;
65 if (strchr_m(&s
[1], ':')) {
66 return NT_STATUS_OBJECT_NAME_INVALID
;
68 if (StrCaseCmp(s
, ":$DATA") != 0) {
69 return NT_STATUS_INVALID_PARAMETER
;
75 if (!posix_path
&& !stream_started
&& *s
== ':') {
76 if (*p_last_component_contains_wcard
) {
77 return NT_STATUS_OBJECT_NAME_INVALID
;
79 /* Stream names allow more characters than file names.
80 We're overloading posix_path here to allow a wider
81 range of characters. If stream_started is true this
82 is still a Windows path even if posix_path is true.
85 stream_started
= true;
86 start_of_name_component
= false;
90 return NT_STATUS_OBJECT_NAME_INVALID
;
94 if (!stream_started
&& IS_PATH_SEP(*s
,posix_path
)) {
96 * Safe to assume is not the second part of a mb char
97 * as this is handled below.
99 /* Eat multiple '/' or '\\' */
100 while (IS_PATH_SEP(*s
,posix_path
)) {
103 if ((d
!= path
) && (*s
!= '\0')) {
104 /* We only care about non-leading or trailing '/' or '\\' */
108 start_of_name_component
= True
;
110 *p_last_component_contains_wcard
= False
;
114 if (start_of_name_component
) {
115 if ((s
[0] == '.') && (s
[1] == '.') && (IS_PATH_SEP(s
[2],posix_path
) || s
[2] == '\0')) {
116 /* Uh oh - "/../" or "\\..\\" or "/..\0" or "\\..\0" ! */
119 * No mb char starts with '.' so we're safe checking the directory separator here.
122 /* If we just added a '/' - delete it */
123 if ((d
> path
) && (*(d
-1) == '/')) {
128 /* Are we at the start ? Can't go back further if so. */
130 ret
= NT_STATUS_OBJECT_PATH_SYNTAX_BAD
;
133 /* Go back one level... */
134 /* We know this is safe as '/' cannot be part of a mb sequence. */
135 /* NOTE - if this assumption is invalid we are not in good shape... */
136 /* Decrement d first as d points to the *next* char to write into. */
137 for (d
--; d
> path
; d
--) {
141 s
+= 2; /* Else go past the .. */
142 /* We're still at the start of a name component, just the previous one. */
145 } else if ((s
[0] == '.') && ((s
[1] == '\0') || IS_PATH_SEP(s
[1],posix_path
))) {
157 if (*s
<= 0x1f || *s
== '|') {
158 return NT_STATUS_OBJECT_NAME_INVALID
;
166 *p_last_component_contains_wcard
= True
;
175 /* Get the size of the next MB character. */
176 next_codepoint(s
,&siz
);
194 DEBUG(0,("check_path_syntax_internal: character length assumptions invalid !\n"));
196 return NT_STATUS_INVALID_PARAMETER
;
199 start_of_name_component
= False
;
207 /****************************************************************************
208 Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
209 No wildcards allowed.
210 ****************************************************************************/
212 NTSTATUS
check_path_syntax(char *path
)
215 return check_path_syntax_internal(path
, False
, &ignore
);
218 /****************************************************************************
219 Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
220 Wildcards allowed - p_contains_wcard returns true if the last component contained
222 ****************************************************************************/
224 NTSTATUS
check_path_syntax_wcard(char *path
, bool *p_contains_wcard
)
226 return check_path_syntax_internal(path
, False
, p_contains_wcard
);
229 /****************************************************************************
230 Check the path for a POSIX client.
231 We're assuming here that '/' is not the second byte in any multibyte char
232 set (a safe assumption).
233 ****************************************************************************/
235 NTSTATUS
check_path_syntax_posix(char *path
)
238 return check_path_syntax_internal(path
, True
, &ignore
);
241 /****************************************************************************
242 Pull a string and check the path allowing a wilcard - provide for error return.
243 ****************************************************************************/
245 size_t srvstr_get_path_wcard(TALLOC_CTX
*ctx
,
246 const char *base_ptr
,
253 bool *contains_wcard
)
259 ret
= srvstr_pull_talloc(ctx
, base_ptr
, smb_flags2
, pp_dest
, src
,
263 *err
= NT_STATUS_INVALID_PARAMETER
;
267 *contains_wcard
= False
;
269 if (smb_flags2
& FLAGS2_DFS_PATHNAMES
) {
271 * For a DFS path the function parse_dfs_path()
272 * will do the path processing, just make a copy.
278 if (lp_posix_pathnames()) {
279 *err
= check_path_syntax_posix(*pp_dest
);
281 *err
= check_path_syntax_wcard(*pp_dest
, contains_wcard
);
287 /****************************************************************************
288 Pull a string and check the path - provide for error return.
289 ****************************************************************************/
291 size_t srvstr_get_path(TALLOC_CTX
*ctx
,
292 const char *base_ptr
,
301 return srvstr_get_path_wcard(ctx
, base_ptr
, smb_flags2
, pp_dest
, src
,
302 src_len
, flags
, err
, &ignore
);
305 size_t srvstr_get_path_req_wcard(TALLOC_CTX
*mem_ctx
, struct smb_request
*req
,
306 char **pp_dest
, const char *src
, int flags
,
307 NTSTATUS
*err
, bool *contains_wcard
)
309 return srvstr_get_path_wcard(mem_ctx
, (char *)req
->inbuf
, req
->flags2
,
310 pp_dest
, src
, smbreq_bufrem(req
, src
),
311 flags
, err
, contains_wcard
);
314 size_t srvstr_get_path_req(TALLOC_CTX
*mem_ctx
, struct smb_request
*req
,
315 char **pp_dest
, const char *src
, int flags
,
319 return srvstr_get_path_req_wcard(mem_ctx
, req
, pp_dest
, src
,
320 flags
, err
, &ignore
);
323 /****************************************************************************
324 Check if we have a correct fsp pointing to a file. Basic check for open fsp.
325 ****************************************************************************/
327 bool check_fsp_open(connection_struct
*conn
, struct smb_request
*req
,
330 if (!(fsp
) || !(conn
)) {
331 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
334 if (((conn
) != (fsp
)->conn
) || req
->vuid
!= (fsp
)->vuid
) {
335 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
341 /****************************************************************************
342 Check if we have a correct fsp pointing to a file.
343 ****************************************************************************/
345 bool check_fsp(connection_struct
*conn
, struct smb_request
*req
,
348 if (!check_fsp_open(conn
, req
, fsp
)) {
351 if ((fsp
)->is_directory
) {
352 reply_nterror(req
, NT_STATUS_INVALID_DEVICE_REQUEST
);
355 if ((fsp
)->fh
->fd
== -1) {
356 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
359 (fsp
)->num_smb_operations
++;
363 /****************************************************************************
364 Check if we have a correct fsp pointing to a quota fake file. Replacement for
365 the CHECK_NTQUOTA_HANDLE_OK macro.
366 ****************************************************************************/
368 bool check_fsp_ntquota_handle(connection_struct
*conn
, struct smb_request
*req
,
371 if (!check_fsp_open(conn
, req
, fsp
)) {
375 if (fsp
->is_directory
) {
379 if (fsp
->fake_file_handle
== NULL
) {
383 if (fsp
->fake_file_handle
->type
!= FAKE_FILE_TYPE_QUOTA
) {
387 if (fsp
->fake_file_handle
->private_data
== NULL
) {
394 /****************************************************************************
395 Check if we have a correct fsp. Replacement for the FSP_BELONGS_CONN macro
396 ****************************************************************************/
398 bool fsp_belongs_conn(connection_struct
*conn
, struct smb_request
*req
,
401 if ((fsp
) && (conn
) && ((conn
)==(fsp
)->conn
)
402 && (req
->vuid
== (fsp
)->vuid
)) {
406 reply_nterror(req
, NT_STATUS_INVALID_HANDLE
);
410 /****************************************************************************
411 Reply to a (netbios-level) special message.
412 ****************************************************************************/
414 void reply_special(char *inbuf
)
416 int msg_type
= CVAL(inbuf
,0);
417 int msg_flags
= CVAL(inbuf
,1);
422 * We only really use 4 bytes of the outbuf, but for the smb_setlen
423 * calculation & friends (srv_send_smb uses that) we need the full smb
426 char outbuf
[smb_size
];
430 memset(outbuf
, '\0', sizeof(outbuf
));
432 smb_setlen(outbuf
,0);
435 case 0x81: /* session request */
437 if (already_got_session
) {
438 exit_server_cleanly("multiple session request not permitted");
441 SCVAL(outbuf
,0,0x82);
443 if (name_len(inbuf
+4) > 50 ||
444 name_len(inbuf
+4 + name_len(inbuf
+ 4)) > 50) {
445 DEBUG(0,("Invalid name length in session request\n"));
448 name_extract(inbuf
,4,name1
);
449 name_type
= name_extract(inbuf
,4 + name_len(inbuf
+ 4),name2
);
450 DEBUG(2,("netbios connect: name1=%s name2=%s\n",
453 set_local_machine_name(name1
, True
);
454 set_remote_machine_name(name2
, True
);
456 DEBUG(2,("netbios connect: local=%s remote=%s, name type = %x\n",
457 get_local_machine_name(), get_remote_machine_name(),
460 if (name_type
== 'R') {
461 /* We are being asked for a pathworks session ---
463 SCVAL(outbuf
, 0,0x83);
467 /* only add the client's machine name to the list
468 of possibly valid usernames if we are operating
469 in share mode security */
470 if (lp_security() == SEC_SHARE
) {
471 add_session_user(get_remote_machine_name());
474 reload_services(True
);
477 already_got_session
= True
;
480 case 0x89: /* session keepalive request
481 (some old clients produce this?) */
482 SCVAL(outbuf
,0,SMBkeepalive
);
486 case 0x82: /* positive session response */
487 case 0x83: /* negative session response */
488 case 0x84: /* retarget session response */
489 DEBUG(0,("Unexpected session response\n"));
492 case SMBkeepalive
: /* session keepalive */
497 DEBUG(5,("init msg_type=0x%x msg_flags=0x%x\n",
498 msg_type
, msg_flags
));
500 srv_send_smb(smbd_server_fd(), outbuf
, false, NULL
);
504 /****************************************************************************
506 conn POINTER CAN BE NULL HERE !
507 ****************************************************************************/
509 void reply_tcon(struct smb_request
*req
)
511 connection_struct
*conn
= req
->conn
;
513 char *service_buf
= NULL
;
514 char *password
= NULL
;
519 DATA_BLOB password_blob
;
520 TALLOC_CTX
*ctx
= talloc_tos();
522 START_PROFILE(SMBtcon
);
524 if (req
->buflen
< 4) {
525 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
526 END_PROFILE(SMBtcon
);
530 p
= (const char *)req
->buf
+ 1;
531 p
+= srvstr_pull_req_talloc(ctx
, req
, &service_buf
, p
, STR_TERMINATE
);
533 pwlen
= srvstr_pull_req_talloc(ctx
, req
, &password
, p
, STR_TERMINATE
);
535 p
+= srvstr_pull_req_talloc(ctx
, req
, &dev
, p
, STR_TERMINATE
);
538 if (service_buf
== NULL
|| password
== NULL
|| dev
== NULL
) {
539 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
540 END_PROFILE(SMBtcon
);
543 p
= strrchr_m(service_buf
,'\\');
547 service
= service_buf
;
550 password_blob
= data_blob(password
, pwlen
+1);
552 conn
= make_connection(service
,password_blob
,dev
,req
->vuid
,&nt_status
);
555 data_blob_clear_free(&password_blob
);
558 reply_nterror(req
, nt_status
);
559 END_PROFILE(SMBtcon
);
563 reply_outbuf(req
, 2, 0);
564 SSVAL(req
->outbuf
,smb_vwv0
,max_recv
);
565 SSVAL(req
->outbuf
,smb_vwv1
,conn
->cnum
);
566 SSVAL(req
->outbuf
,smb_tid
,conn
->cnum
);
568 DEBUG(3,("tcon service=%s cnum=%d\n",
569 service
, conn
->cnum
));
571 END_PROFILE(SMBtcon
);
575 /****************************************************************************
576 Reply to a tcon and X.
577 conn POINTER CAN BE NULL HERE !
578 ****************************************************************************/
580 void reply_tcon_and_X(struct smb_request
*req
)
582 connection_struct
*conn
= req
->conn
;
583 const char *service
= NULL
;
585 TALLOC_CTX
*ctx
= talloc_tos();
586 /* what the cleint thinks the device is */
587 char *client_devicetype
= NULL
;
588 /* what the server tells the client the share represents */
589 const char *server_devicetype
;
596 START_PROFILE(SMBtconX
);
599 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
600 END_PROFILE(SMBtconX
);
604 passlen
= SVAL(req
->vwv
+3, 0);
605 tcon_flags
= SVAL(req
->vwv
+2, 0);
607 /* we might have to close an old one */
608 if ((tcon_flags
& 0x1) && conn
) {
609 close_cnum(conn
,req
->vuid
);
614 if ((passlen
> MAX_PASS_LEN
) || (passlen
>= req
->buflen
)) {
615 reply_doserror(req
, ERRDOS
, ERRbuftoosmall
);
616 END_PROFILE(SMBtconX
);
620 if (global_encrypted_passwords_negotiated
) {
621 password
= data_blob_talloc(talloc_tos(), req
->buf
, passlen
);
622 if (lp_security() == SEC_SHARE
) {
624 * Security = share always has a pad byte
625 * after the password.
627 p
= (const char *)req
->buf
+ passlen
+ 1;
629 p
= (const char *)req
->buf
+ passlen
;
632 password
= data_blob_talloc(talloc_tos(), req
->buf
, passlen
+1);
633 /* Ensure correct termination */
634 password
.data
[passlen
]=0;
635 p
= (const char *)req
->buf
+ passlen
+ 1;
638 p
+= srvstr_pull_req_talloc(ctx
, req
, &path
, p
, STR_TERMINATE
);
641 data_blob_clear_free(&password
);
642 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
643 END_PROFILE(SMBtconX
);
648 * the service name can be either: \\server\share
649 * or share directly like on the DELL PowerVault 705
652 q
= strchr_m(path
+2,'\\');
654 data_blob_clear_free(&password
);
655 reply_doserror(req
, ERRDOS
, ERRnosuchshare
);
656 END_PROFILE(SMBtconX
);
664 p
+= srvstr_pull_talloc(ctx
, req
->inbuf
, req
->flags2
,
665 &client_devicetype
, p
,
666 MIN(6, smbreq_bufrem(req
, p
)), STR_ASCII
);
668 if (client_devicetype
== NULL
) {
669 data_blob_clear_free(&password
);
670 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
671 END_PROFILE(SMBtconX
);
675 DEBUG(4,("Client requested device type [%s] for share [%s]\n", client_devicetype
, service
));
677 conn
= make_connection(service
, password
, client_devicetype
,
678 req
->vuid
, &nt_status
);
681 data_blob_clear_free(&password
);
684 reply_nterror(req
, nt_status
);
685 END_PROFILE(SMBtconX
);
690 server_devicetype
= "IPC";
691 else if ( IS_PRINT(conn
) )
692 server_devicetype
= "LPT1:";
694 server_devicetype
= "A:";
696 if (Protocol
< PROTOCOL_NT1
) {
697 reply_outbuf(req
, 2, 0);
698 if (message_push_string(&req
->outbuf
, server_devicetype
,
699 STR_TERMINATE
|STR_ASCII
) == -1) {
700 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
701 END_PROFILE(SMBtconX
);
705 /* NT sets the fstype of IPC$ to the null string */
706 const char *fstype
= IS_IPC(conn
) ? "" : lp_fstype(SNUM(conn
));
708 if (tcon_flags
& TCONX_FLAG_EXTENDED_RESPONSE
) {
709 /* Return permissions. */
713 reply_outbuf(req
, 7, 0);
716 perm1
= FILE_ALL_ACCESS
;
717 perm2
= FILE_ALL_ACCESS
;
719 perm1
= CAN_WRITE(conn
) ?
724 SIVAL(req
->outbuf
, smb_vwv3
, perm1
);
725 SIVAL(req
->outbuf
, smb_vwv5
, perm2
);
727 reply_outbuf(req
, 3, 0);
730 if ((message_push_string(&req
->outbuf
, server_devicetype
,
731 STR_TERMINATE
|STR_ASCII
) == -1)
732 || (message_push_string(&req
->outbuf
, fstype
,
733 STR_TERMINATE
) == -1)) {
734 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
735 END_PROFILE(SMBtconX
);
739 /* what does setting this bit do? It is set by NT4 and
740 may affect the ability to autorun mounted cdroms */
741 SSVAL(req
->outbuf
, smb_vwv2
, SMB_SUPPORT_SEARCH_BITS
|
742 (lp_csc_policy(SNUM(conn
)) << 2));
744 if (lp_msdfs_root(SNUM(conn
)) && lp_host_msdfs()) {
745 DEBUG(2,("Serving %s as a Dfs root\n",
746 lp_servicename(SNUM(conn
)) ));
747 SSVAL(req
->outbuf
, smb_vwv2
,
748 SMB_SHARE_IN_DFS
| SVAL(req
->outbuf
, smb_vwv2
));
753 DEBUG(3,("tconX service=%s \n",
756 /* set the incoming and outgoing tid to the just created one */
757 SSVAL(req
->inbuf
,smb_tid
,conn
->cnum
);
758 SSVAL(req
->outbuf
,smb_tid
,conn
->cnum
);
760 END_PROFILE(SMBtconX
);
762 req
->tid
= conn
->cnum
;
767 /****************************************************************************
768 Reply to an unknown type.
769 ****************************************************************************/
771 void reply_unknown_new(struct smb_request
*req
, uint8 type
)
773 DEBUG(0, ("unknown command type (%s): type=%d (0x%X)\n",
774 smb_fn_name(type
), type
, type
));
775 reply_doserror(req
, ERRSRV
, ERRunknownsmb
);
779 /****************************************************************************
781 conn POINTER CAN BE NULL HERE !
782 ****************************************************************************/
784 void reply_ioctl(struct smb_request
*req
)
786 connection_struct
*conn
= req
->conn
;
793 START_PROFILE(SMBioctl
);
796 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
797 END_PROFILE(SMBioctl
);
801 device
= SVAL(req
->vwv
+1, 0);
802 function
= SVAL(req
->vwv
+2, 0);
803 ioctl_code
= (device
<< 16) + function
;
805 DEBUG(4, ("Received IOCTL (code 0x%x)\n", ioctl_code
));
807 switch (ioctl_code
) {
808 case IOCTL_QUERY_JOB_INFO
:
812 reply_doserror(req
, ERRSRV
, ERRnosupport
);
813 END_PROFILE(SMBioctl
);
817 reply_outbuf(req
, 8, replysize
+1);
818 SSVAL(req
->outbuf
,smb_vwv1
,replysize
); /* Total data bytes returned */
819 SSVAL(req
->outbuf
,smb_vwv5
,replysize
); /* Data bytes this buffer */
820 SSVAL(req
->outbuf
,smb_vwv6
,52); /* Offset to data */
821 p
= smb_buf(req
->outbuf
);
822 memset(p
, '\0', replysize
+1); /* valgrind-safe. */
823 p
+= 1; /* Allow for alignment */
825 switch (ioctl_code
) {
826 case IOCTL_QUERY_JOB_INFO
:
828 files_struct
*fsp
= file_fsp(
829 req
, SVAL(req
->vwv
+0, 0));
831 reply_doserror(req
, ERRDOS
, ERRbadfid
);
832 END_PROFILE(SMBioctl
);
835 SSVAL(p
,0,fsp
->rap_print_jobid
); /* Job number */
836 srvstr_push((char *)req
->outbuf
, req
->flags2
, p
+2,
838 STR_TERMINATE
|STR_ASCII
);
840 srvstr_push((char *)req
->outbuf
, req
->flags2
,
841 p
+18, lp_servicename(SNUM(conn
)),
842 13, STR_TERMINATE
|STR_ASCII
);
850 END_PROFILE(SMBioctl
);
854 /****************************************************************************
855 Strange checkpath NTSTATUS mapping.
856 ****************************************************************************/
858 static NTSTATUS
map_checkpath_error(uint16_t flags2
, NTSTATUS status
)
860 /* Strange DOS error code semantics only for checkpath... */
861 if (!(flags2
& FLAGS2_32_BIT_ERROR_CODES
)) {
862 if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_INVALID
,status
)) {
863 /* We need to map to ERRbadpath */
864 return NT_STATUS_OBJECT_PATH_NOT_FOUND
;
870 /****************************************************************************
871 Reply to a checkpath.
872 ****************************************************************************/
874 void reply_checkpath(struct smb_request
*req
)
876 connection_struct
*conn
= req
->conn
;
878 SMB_STRUCT_STAT sbuf
;
880 TALLOC_CTX
*ctx
= talloc_tos();
882 START_PROFILE(SMBcheckpath
);
884 srvstr_get_path_req(ctx
, req
, &name
, (const char *)req
->buf
+ 1,
885 STR_TERMINATE
, &status
);
887 if (!NT_STATUS_IS_OK(status
)) {
888 status
= map_checkpath_error(req
->flags2
, status
);
889 reply_nterror(req
, status
);
890 END_PROFILE(SMBcheckpath
);
894 status
= resolve_dfspath(ctx
, conn
,
895 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
898 if (!NT_STATUS_IS_OK(status
)) {
899 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
900 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
902 END_PROFILE(SMBcheckpath
);
908 DEBUG(3,("reply_checkpath %s mode=%d\n", name
, (int)SVAL(req
->vwv
+0, 0)));
910 status
= unix_convert(ctx
, conn
, name
, False
, &name
, NULL
, &sbuf
);
911 if (!NT_STATUS_IS_OK(status
)) {
915 status
= check_name(conn
, name
);
916 if (!NT_STATUS_IS_OK(status
)) {
917 DEBUG(3,("reply_checkpath: check_name of %s failed (%s)\n",name
,nt_errstr(status
)));
921 if (!VALID_STAT(sbuf
) && (SMB_VFS_STAT(conn
,name
,&sbuf
) != 0)) {
922 DEBUG(3,("reply_checkpath: stat of %s failed (%s)\n",name
,strerror(errno
)));
923 status
= map_nt_error_from_unix(errno
);
927 if (!S_ISDIR(sbuf
.st_mode
)) {
928 reply_botherror(req
, NT_STATUS_NOT_A_DIRECTORY
,
930 END_PROFILE(SMBcheckpath
);
934 reply_outbuf(req
, 0, 0);
936 END_PROFILE(SMBcheckpath
);
941 END_PROFILE(SMBcheckpath
);
943 /* We special case this - as when a Windows machine
944 is parsing a path is steps through the components
945 one at a time - if a component fails it expects
946 ERRbadpath, not ERRbadfile.
948 status
= map_checkpath_error(req
->flags2
, status
);
949 if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
951 * Windows returns different error codes if
952 * the parent directory is valid but not the
953 * last component - it returns NT_STATUS_OBJECT_NAME_NOT_FOUND
954 * for that case and NT_STATUS_OBJECT_PATH_NOT_FOUND
955 * if the path is invalid.
957 reply_botherror(req
, NT_STATUS_OBJECT_NAME_NOT_FOUND
,
962 reply_nterror(req
, status
);
965 /****************************************************************************
967 ****************************************************************************/
969 void reply_getatr(struct smb_request
*req
)
971 connection_struct
*conn
= req
->conn
;
973 SMB_STRUCT_STAT sbuf
;
979 TALLOC_CTX
*ctx
= talloc_tos();
981 START_PROFILE(SMBgetatr
);
983 p
= (const char *)req
->buf
+ 1;
984 p
+= srvstr_get_path_req(ctx
, req
, &fname
, p
, STR_TERMINATE
, &status
);
985 if (!NT_STATUS_IS_OK(status
)) {
986 reply_nterror(req
, status
);
987 END_PROFILE(SMBgetatr
);
991 status
= resolve_dfspath(ctx
, conn
,
992 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
995 if (!NT_STATUS_IS_OK(status
)) {
996 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
997 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
999 END_PROFILE(SMBgetatr
);
1002 reply_nterror(req
, status
);
1003 END_PROFILE(SMBgetatr
);
1007 /* dos smetimes asks for a stat of "" - it returns a "hidden directory"
1008 under WfWg - weird! */
1009 if (*fname
== '\0') {
1010 mode
= aHIDDEN
| aDIR
;
1011 if (!CAN_WRITE(conn
)) {
1017 status
= unix_convert(ctx
, conn
, fname
, False
, &fname
, NULL
,&sbuf
);
1018 if (!NT_STATUS_IS_OK(status
)) {
1019 reply_nterror(req
, status
);
1020 END_PROFILE(SMBgetatr
);
1023 status
= check_name(conn
, fname
);
1024 if (!NT_STATUS_IS_OK(status
)) {
1025 DEBUG(3,("reply_getatr: check_name of %s failed (%s)\n",fname
,nt_errstr(status
)));
1026 reply_nterror(req
, status
);
1027 END_PROFILE(SMBgetatr
);
1030 if (!VALID_STAT(sbuf
) && (SMB_VFS_STAT(conn
,fname
,&sbuf
) != 0)) {
1031 DEBUG(3,("reply_getatr: stat of %s failed (%s)\n",fname
,strerror(errno
)));
1032 reply_unixerror(req
, ERRDOS
,ERRbadfile
);
1033 END_PROFILE(SMBgetatr
);
1037 mode
= dos_mode(conn
,fname
,&sbuf
);
1038 size
= sbuf
.st_size
;
1039 mtime
= sbuf
.st_mtime
;
1045 reply_outbuf(req
, 10, 0);
1047 SSVAL(req
->outbuf
,smb_vwv0
,mode
);
1048 if(lp_dos_filetime_resolution(SNUM(conn
)) ) {
1049 srv_put_dos_date3((char *)req
->outbuf
,smb_vwv1
,mtime
& ~1);
1051 srv_put_dos_date3((char *)req
->outbuf
,smb_vwv1
,mtime
);
1053 SIVAL(req
->outbuf
,smb_vwv3
,(uint32
)size
);
1055 if (Protocol
>= PROTOCOL_NT1
) {
1056 SSVAL(req
->outbuf
, smb_flg2
,
1057 SVAL(req
->outbuf
, smb_flg2
) | FLAGS2_IS_LONG_NAME
);
1060 DEBUG(3,("reply_getatr: name=%s mode=%d size=%u\n", fname
, mode
, (unsigned int)size
) );
1062 END_PROFILE(SMBgetatr
);
1066 /****************************************************************************
1068 ****************************************************************************/
1070 void reply_setatr(struct smb_request
*req
)
1072 struct smb_file_time ft
;
1073 connection_struct
*conn
= req
->conn
;
1077 SMB_STRUCT_STAT sbuf
;
1080 TALLOC_CTX
*ctx
= talloc_tos();
1082 START_PROFILE(SMBsetatr
);
1087 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1091 p
= (const char *)req
->buf
+ 1;
1092 p
+= srvstr_get_path_req(ctx
, req
, &fname
, p
, STR_TERMINATE
, &status
);
1093 if (!NT_STATUS_IS_OK(status
)) {
1094 reply_nterror(req
, status
);
1095 END_PROFILE(SMBsetatr
);
1099 status
= resolve_dfspath(ctx
, conn
,
1100 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1103 if (!NT_STATUS_IS_OK(status
)) {
1104 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
1105 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
1106 ERRSRV
, ERRbadpath
);
1107 END_PROFILE(SMBsetatr
);
1110 reply_nterror(req
, status
);
1111 END_PROFILE(SMBsetatr
);
1115 status
= unix_convert(ctx
, conn
, fname
, False
, &fname
, NULL
, &sbuf
);
1116 if (!NT_STATUS_IS_OK(status
)) {
1117 reply_nterror(req
, status
);
1118 END_PROFILE(SMBsetatr
);
1122 status
= check_name(conn
, fname
);
1123 if (!NT_STATUS_IS_OK(status
)) {
1124 reply_nterror(req
, status
);
1125 END_PROFILE(SMBsetatr
);
1129 if (fname
[0] == '.' && fname
[1] == '\0') {
1131 * Not sure here is the right place to catch this
1132 * condition. Might be moved to somewhere else later -- vl
1134 reply_nterror(req
, NT_STATUS_ACCESS_DENIED
);
1135 END_PROFILE(SMBsetatr
);
1139 mode
= SVAL(req
->vwv
+0, 0);
1140 mtime
= srv_make_unix_date3(req
->vwv
+1);
1142 ft
.mtime
= convert_time_t_to_timespec(mtime
);
1143 status
= smb_set_file_time(conn
, NULL
, fname
,
1145 if (!NT_STATUS_IS_OK(status
)) {
1146 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
1147 END_PROFILE(SMBsetatr
);
1151 if (mode
!= FILE_ATTRIBUTE_NORMAL
) {
1152 if (VALID_STAT_OF_DIR(sbuf
))
1157 if (file_set_dosmode(conn
,fname
,mode
,&sbuf
,NULL
,false) != 0) {
1158 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
1159 END_PROFILE(SMBsetatr
);
1164 reply_outbuf(req
, 0, 0);
1166 DEBUG( 3, ( "setatr name=%s mode=%d\n", fname
, mode
) );
1168 END_PROFILE(SMBsetatr
);
1172 /****************************************************************************
1174 ****************************************************************************/
1176 void reply_dskattr(struct smb_request
*req
)
1178 connection_struct
*conn
= req
->conn
;
1179 uint64_t dfree
,dsize
,bsize
;
1180 START_PROFILE(SMBdskattr
);
1182 if (get_dfree_info(conn
,".",True
,&bsize
,&dfree
,&dsize
) == (uint64_t)-1) {
1183 reply_unixerror(req
, ERRHRD
, ERRgeneral
);
1184 END_PROFILE(SMBdskattr
);
1188 reply_outbuf(req
, 5, 0);
1190 if (Protocol
<= PROTOCOL_LANMAN2
) {
1191 double total_space
, free_space
;
1192 /* we need to scale this to a number that DOS6 can handle. We
1193 use floating point so we can handle large drives on systems
1194 that don't have 64 bit integers
1196 we end up displaying a maximum of 2G to DOS systems
1198 total_space
= dsize
* (double)bsize
;
1199 free_space
= dfree
* (double)bsize
;
1201 dsize
= (uint64_t)((total_space
+63*512) / (64*512));
1202 dfree
= (uint64_t)((free_space
+63*512) / (64*512));
1204 if (dsize
> 0xFFFF) dsize
= 0xFFFF;
1205 if (dfree
> 0xFFFF) dfree
= 0xFFFF;
1207 SSVAL(req
->outbuf
,smb_vwv0
,dsize
);
1208 SSVAL(req
->outbuf
,smb_vwv1
,64); /* this must be 64 for dos systems */
1209 SSVAL(req
->outbuf
,smb_vwv2
,512); /* and this must be 512 */
1210 SSVAL(req
->outbuf
,smb_vwv3
,dfree
);
1212 SSVAL(req
->outbuf
,smb_vwv0
,dsize
);
1213 SSVAL(req
->outbuf
,smb_vwv1
,bsize
/512);
1214 SSVAL(req
->outbuf
,smb_vwv2
,512);
1215 SSVAL(req
->outbuf
,smb_vwv3
,dfree
);
1218 DEBUG(3,("dskattr dfree=%d\n", (unsigned int)dfree
));
1220 END_PROFILE(SMBdskattr
);
1224 /****************************************************************************
1226 Can be called from SMBsearch, SMBffirst or SMBfunique.
1227 ****************************************************************************/
1229 void reply_search(struct smb_request
*req
)
1231 connection_struct
*conn
= req
->conn
;
1232 const char *mask
= NULL
;
1233 char *directory
= NULL
;
1239 unsigned int numentries
= 0;
1240 unsigned int maxentries
= 0;
1241 bool finished
= False
;
1247 bool check_descend
= False
;
1248 bool expect_close
= False
;
1250 bool mask_contains_wcard
= False
;
1251 bool allow_long_path_components
= (req
->flags2
& FLAGS2_LONG_PATH_COMPONENTS
) ? True
: False
;
1252 TALLOC_CTX
*ctx
= talloc_tos();
1253 bool ask_sharemode
= lp_parm_bool(SNUM(conn
), "smbd", "search ask sharemode", true);
1255 START_PROFILE(SMBsearch
);
1258 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1259 END_PROFILE(SMBsearch
);
1263 if (lp_posix_pathnames()) {
1264 reply_unknown_new(req
, req
->cmd
);
1265 END_PROFILE(SMBsearch
);
1269 /* If we were called as SMBffirst then we must expect close. */
1270 if(req
->cmd
== SMBffirst
) {
1271 expect_close
= True
;
1274 reply_outbuf(req
, 1, 3);
1275 maxentries
= SVAL(req
->vwv
+0, 0);
1276 dirtype
= SVAL(req
->vwv
+1, 0);
1277 p
= (const char *)req
->buf
+ 1;
1278 p
+= srvstr_get_path_req_wcard(ctx
, req
, &path
, p
, STR_TERMINATE
,
1279 &nt_status
, &mask_contains_wcard
);
1280 if (!NT_STATUS_IS_OK(nt_status
)) {
1281 reply_nterror(req
, nt_status
);
1282 END_PROFILE(SMBsearch
);
1286 nt_status
= resolve_dfspath_wcard(ctx
, conn
,
1287 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
1290 &mask_contains_wcard
);
1291 if (!NT_STATUS_IS_OK(nt_status
)) {
1292 if (NT_STATUS_EQUAL(nt_status
,NT_STATUS_PATH_NOT_COVERED
)) {
1293 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
1294 ERRSRV
, ERRbadpath
);
1295 END_PROFILE(SMBsearch
);
1298 reply_nterror(req
, nt_status
);
1299 END_PROFILE(SMBsearch
);
1304 status_len
= SVAL(p
, 0);
1307 /* dirtype &= ~aDIR; */
1309 if (status_len
== 0) {
1310 SMB_STRUCT_STAT sbuf
;
1312 nt_status
= unix_convert(ctx
, conn
, path
, True
,
1313 &directory
, NULL
, &sbuf
);
1314 if (!NT_STATUS_IS_OK(nt_status
)) {
1315 reply_nterror(req
, nt_status
);
1316 END_PROFILE(SMBsearch
);
1320 nt_status
= check_name(conn
, directory
);
1321 if (!NT_STATUS_IS_OK(nt_status
)) {
1322 reply_nterror(req
, nt_status
);
1323 END_PROFILE(SMBsearch
);
1327 p
= strrchr_m(directory
,'/');
1328 if ((p
!= NULL
) && (*directory
!= '/')) {
1330 directory
= talloc_strndup(ctx
, directory
,
1331 PTR_DIFF(p
, directory
));
1334 directory
= talloc_strdup(ctx
,".");
1338 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1339 END_PROFILE(SMBsearch
);
1343 memset((char *)status
,'\0',21);
1344 SCVAL(status
,0,(dirtype
& 0x1F));
1346 nt_status
= dptr_create(conn
,
1352 mask_contains_wcard
,
1355 if (!NT_STATUS_IS_OK(nt_status
)) {
1356 reply_nterror(req
, nt_status
);
1357 END_PROFILE(SMBsearch
);
1360 dptr_num
= dptr_dnum(conn
->dirptr
);
1364 memcpy(status
,p
,21);
1365 status_dirtype
= CVAL(status
,0) & 0x1F;
1366 if (status_dirtype
!= (dirtype
& 0x1F)) {
1367 dirtype
= status_dirtype
;
1370 conn
->dirptr
= dptr_fetch(status
+12,&dptr_num
);
1371 if (!conn
->dirptr
) {
1374 string_set(&conn
->dirpath
,dptr_path(dptr_num
));
1375 mask
= dptr_wcard(dptr_num
);
1380 * For a 'continue' search we have no string. So
1381 * check from the initial saved string.
1383 mask_contains_wcard
= ms_has_wild(mask
);
1384 dirtype
= dptr_attr(dptr_num
);
1387 DEBUG(4,("dptr_num is %d\n",dptr_num
));
1389 /* Initialize per SMBsearch/SMBffirst/SMBfunique operation data */
1390 dptr_init_search_op(conn
->dirptr
);
1392 if ((dirtype
&0x1F) == aVOLID
) {
1393 char buf
[DIR_STRUCT_SIZE
];
1394 memcpy(buf
,status
,21);
1395 if (!make_dir_struct(ctx
,buf
,"???????????",volume_label(SNUM(conn
)),
1396 0,aVOLID
,0,!allow_long_path_components
)) {
1397 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1398 END_PROFILE(SMBsearch
);
1401 dptr_fill(buf
+12,dptr_num
);
1402 if (dptr_zero(buf
+12) && (status_len
==0)) {
1407 if (message_push_blob(&req
->outbuf
,
1408 data_blob_const(buf
, sizeof(buf
)))
1410 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1411 END_PROFILE(SMBsearch
);
1419 ((uint8
*)smb_buf(req
->outbuf
) + 3 - req
->outbuf
))
1422 DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
1423 conn
->dirpath
,lp_dontdescend(SNUM(conn
))));
1424 if (in_list(conn
->dirpath
, lp_dontdescend(SNUM(conn
)),True
)) {
1425 check_descend
= True
;
1428 for (i
=numentries
;(i
<maxentries
) && !finished
;i
++) {
1429 finished
= !get_dir_entry(ctx
,
1440 char buf
[DIR_STRUCT_SIZE
];
1441 memcpy(buf
,status
,21);
1442 if (!make_dir_struct(ctx
,
1449 !allow_long_path_components
)) {
1450 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1451 END_PROFILE(SMBsearch
);
1454 if (!dptr_fill(buf
+12,dptr_num
)) {
1457 if (message_push_blob(&req
->outbuf
,
1458 data_blob_const(buf
, sizeof(buf
)))
1460 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
1461 END_PROFILE(SMBsearch
);
1471 /* If we were called as SMBffirst with smb_search_id == NULL
1472 and no entries were found then return error and close dirptr
1475 if (numentries
== 0) {
1476 dptr_close(&dptr_num
);
1477 } else if(expect_close
&& status_len
== 0) {
1478 /* Close the dptr - we know it's gone */
1479 dptr_close(&dptr_num
);
1482 /* If we were called as SMBfunique, then we can close the dirptr now ! */
1483 if(dptr_num
>= 0 && req
->cmd
== SMBfunique
) {
1484 dptr_close(&dptr_num
);
1487 if ((numentries
== 0) && !mask_contains_wcard
) {
1488 reply_botherror(req
, STATUS_NO_MORE_FILES
, ERRDOS
, ERRnofiles
);
1489 END_PROFILE(SMBsearch
);
1493 SSVAL(req
->outbuf
,smb_vwv0
,numentries
);
1494 SSVAL(req
->outbuf
,smb_vwv1
,3 + numentries
* DIR_STRUCT_SIZE
);
1495 SCVAL(smb_buf(req
->outbuf
),0,5);
1496 SSVAL(smb_buf(req
->outbuf
),1,numentries
*DIR_STRUCT_SIZE
);
1498 /* The replies here are never long name. */
1499 SSVAL(req
->outbuf
, smb_flg2
,
1500 SVAL(req
->outbuf
, smb_flg2
) & (~FLAGS2_IS_LONG_NAME
));
1501 if (!allow_long_path_components
) {
1502 SSVAL(req
->outbuf
, smb_flg2
,
1503 SVAL(req
->outbuf
, smb_flg2
)
1504 & (~FLAGS2_LONG_PATH_COMPONENTS
));
1507 /* This SMB *always* returns ASCII names. Remove the unicode bit in flags2. */
1508 SSVAL(req
->outbuf
, smb_flg2
,
1509 (SVAL(req
->outbuf
, smb_flg2
) & (~FLAGS2_UNICODE_STRINGS
)));
1512 directory
= dptr_path(dptr_num
);
1515 DEBUG(4,("%s mask=%s path=%s dtype=%d nument=%u of %u\n",
1516 smb_fn_name(req
->cmd
),
1518 directory
? directory
: "./",
1523 END_PROFILE(SMBsearch
);
1527 /****************************************************************************
1528 Reply to a fclose (stop directory search).
1529 ****************************************************************************/
1531 void reply_fclose(struct smb_request
*req
)
1539 bool path_contains_wcard
= False
;
1540 TALLOC_CTX
*ctx
= talloc_tos();
1542 START_PROFILE(SMBfclose
);
1544 if (lp_posix_pathnames()) {
1545 reply_unknown_new(req
, req
->cmd
);
1546 END_PROFILE(SMBfclose
);
1550 p
= (const char *)req
->buf
+ 1;
1551 p
+= srvstr_get_path_req_wcard(ctx
, req
, &path
, p
, STR_TERMINATE
,
1552 &err
, &path_contains_wcard
);
1553 if (!NT_STATUS_IS_OK(err
)) {
1554 reply_nterror(req
, err
);
1555 END_PROFILE(SMBfclose
);
1559 status_len
= SVAL(p
,0);
1562 if (status_len
== 0) {
1563 reply_doserror(req
, ERRSRV
, ERRsrverror
);
1564 END_PROFILE(SMBfclose
);
1568 memcpy(status
,p
,21);
1570 if(dptr_fetch(status
+12,&dptr_num
)) {
1571 /* Close the dptr - we know it's gone */
1572 dptr_close(&dptr_num
);
1575 reply_outbuf(req
, 1, 0);
1576 SSVAL(req
->outbuf
,smb_vwv0
,0);
1578 DEBUG(3,("search close\n"));
1580 END_PROFILE(SMBfclose
);
1584 /****************************************************************************
1586 ****************************************************************************/
1588 void reply_open(struct smb_request
*req
)
1590 connection_struct
*conn
= req
->conn
;
1596 SMB_STRUCT_STAT sbuf
;
1603 uint32 create_disposition
;
1604 uint32 create_options
= 0;
1606 TALLOC_CTX
*ctx
= talloc_tos();
1608 START_PROFILE(SMBopen
);
1610 SET_STAT_INVALID(sbuf
);
1613 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1614 END_PROFILE(SMBopen
);
1618 oplock_request
= CORE_OPLOCK_REQUEST(req
->inbuf
);
1619 deny_mode
= SVAL(req
->vwv
+0, 0);
1620 dos_attr
= SVAL(req
->vwv
+1, 0);
1622 srvstr_get_path_req(ctx
, req
, &fname
, (const char *)req
->buf
+1,
1623 STR_TERMINATE
, &status
);
1624 if (!NT_STATUS_IS_OK(status
)) {
1625 reply_nterror(req
, status
);
1626 END_PROFILE(SMBopen
);
1630 if (!map_open_params_to_ntcreate(
1631 fname
, deny_mode
, OPENX_FILE_EXISTS_OPEN
, &access_mask
,
1632 &share_mode
, &create_disposition
, &create_options
)) {
1633 reply_nterror(req
, NT_STATUS_DOS(ERRDOS
, ERRbadaccess
));
1634 END_PROFILE(SMBopen
);
1638 status
= SMB_VFS_CREATE_FILE(
1641 0, /* root_dir_fid */
1643 CFF_DOS_PATH
, /* create_file_flags */
1644 access_mask
, /* access_mask */
1645 share_mode
, /* share_access */
1646 create_disposition
, /* create_disposition*/
1647 create_options
, /* create_options */
1648 dos_attr
, /* file_attributes */
1649 oplock_request
, /* oplock_request */
1650 0, /* allocation_size */
1657 if (!NT_STATUS_IS_OK(status
)) {
1658 if (open_was_deferred(req
->mid
)) {
1659 /* We have re-scheduled this call. */
1660 END_PROFILE(SMBopen
);
1663 reply_openerror(req
, status
);
1664 END_PROFILE(SMBopen
);
1668 size
= sbuf
.st_size
;
1669 fattr
= dos_mode(conn
,fsp
->fsp_name
,&sbuf
);
1670 mtime
= sbuf
.st_mtime
;
1673 DEBUG(3,("attempt to open a directory %s\n",fsp
->fsp_name
));
1674 close_file(req
, fsp
, ERROR_CLOSE
);
1675 reply_doserror(req
, ERRDOS
,ERRnoaccess
);
1676 END_PROFILE(SMBopen
);
1680 reply_outbuf(req
, 7, 0);
1681 SSVAL(req
->outbuf
,smb_vwv0
,fsp
->fnum
);
1682 SSVAL(req
->outbuf
,smb_vwv1
,fattr
);
1683 if(lp_dos_filetime_resolution(SNUM(conn
)) ) {
1684 srv_put_dos_date3((char *)req
->outbuf
,smb_vwv2
,mtime
& ~1);
1686 srv_put_dos_date3((char *)req
->outbuf
,smb_vwv2
,mtime
);
1688 SIVAL(req
->outbuf
,smb_vwv4
,(uint32
)size
);
1689 SSVAL(req
->outbuf
,smb_vwv6
,deny_mode
);
1691 if (oplock_request
&& lp_fake_oplocks(SNUM(conn
))) {
1692 SCVAL(req
->outbuf
,smb_flg
,
1693 CVAL(req
->outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
1696 if(EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1697 SCVAL(req
->outbuf
,smb_flg
,
1698 CVAL(req
->outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
1700 END_PROFILE(SMBopen
);
1704 /****************************************************************************
1705 Reply to an open and X.
1706 ****************************************************************************/
1708 void reply_open_and_X(struct smb_request
*req
)
1710 connection_struct
*conn
= req
->conn
;
1715 /* Breakout the oplock request bits so we can set the
1716 reply bits separately. */
1717 int ex_oplock_request
;
1718 int core_oplock_request
;
1721 int smb_sattr
= SVAL(req
->vwv
+4, 0);
1722 uint32 smb_time
= make_unix_date3(req
->vwv
+6);
1727 SMB_STRUCT_STAT sbuf
;
1731 uint64_t allocation_size
;
1732 ssize_t retval
= -1;
1735 uint32 create_disposition
;
1736 uint32 create_options
= 0;
1737 TALLOC_CTX
*ctx
= talloc_tos();
1739 START_PROFILE(SMBopenX
);
1741 if (req
->wct
< 15) {
1742 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1743 END_PROFILE(SMBopenX
);
1747 SET_STAT_INVALID(sbuf
);
1749 open_flags
= SVAL(req
->vwv
+2, 0);
1750 deny_mode
= SVAL(req
->vwv
+3, 0);
1751 smb_attr
= SVAL(req
->vwv
+5, 0);
1752 ex_oplock_request
= EXTENDED_OPLOCK_REQUEST(req
->inbuf
);
1753 core_oplock_request
= CORE_OPLOCK_REQUEST(req
->inbuf
);
1754 oplock_request
= ex_oplock_request
| core_oplock_request
;
1755 smb_ofun
= SVAL(req
->vwv
+8, 0);
1756 allocation_size
= (uint64_t)IVAL(req
->vwv
+9, 0);
1758 /* If it's an IPC, pass off the pipe handler. */
1760 if (lp_nt_pipe_support()) {
1761 reply_open_pipe_and_X(conn
, req
);
1763 reply_doserror(req
, ERRSRV
, ERRaccess
);
1765 END_PROFILE(SMBopenX
);
1769 /* XXXX we need to handle passed times, sattr and flags */
1770 srvstr_get_path_req(ctx
, req
, &fname
, (const char *)req
->buf
,
1771 STR_TERMINATE
, &status
);
1772 if (!NT_STATUS_IS_OK(status
)) {
1773 reply_nterror(req
, status
);
1774 END_PROFILE(SMBopenX
);
1778 if (!map_open_params_to_ntcreate(
1779 fname
, deny_mode
, smb_ofun
, &access_mask
,
1780 &share_mode
, &create_disposition
, &create_options
)) {
1781 reply_nterror(req
, NT_STATUS_DOS(ERRDOS
, ERRbadaccess
));
1782 END_PROFILE(SMBopenX
);
1786 status
= SMB_VFS_CREATE_FILE(
1789 0, /* root_dir_fid */
1791 CFF_DOS_PATH
, /* create_file_flags */
1792 access_mask
, /* access_mask */
1793 share_mode
, /* share_access */
1794 create_disposition
, /* create_disposition*/
1795 create_options
, /* create_options */
1796 smb_attr
, /* file_attributes */
1797 oplock_request
, /* oplock_request */
1798 0, /* allocation_size */
1802 &smb_action
, /* pinfo */
1805 if (!NT_STATUS_IS_OK(status
)) {
1806 END_PROFILE(SMBopenX
);
1807 if (open_was_deferred(req
->mid
)) {
1808 /* We have re-scheduled this call. */
1811 reply_openerror(req
, status
);
1815 /* Setting the "size" field in vwv9 and vwv10 causes the file to be set to this size,
1816 if the file is truncated or created. */
1817 if (((smb_action
== FILE_WAS_CREATED
) || (smb_action
== FILE_WAS_OVERWRITTEN
)) && allocation_size
) {
1818 fsp
->initial_allocation_size
= smb_roundup(fsp
->conn
, allocation_size
);
1819 if (vfs_allocate_file_space(fsp
, fsp
->initial_allocation_size
) == -1) {
1820 close_file(req
, fsp
, ERROR_CLOSE
);
1821 reply_nterror(req
, NT_STATUS_DISK_FULL
);
1822 END_PROFILE(SMBopenX
);
1825 retval
= vfs_set_filelen(fsp
, (SMB_OFF_T
)allocation_size
);
1827 close_file(req
, fsp
, ERROR_CLOSE
);
1828 reply_nterror(req
, NT_STATUS_DISK_FULL
);
1829 END_PROFILE(SMBopenX
);
1832 sbuf
.st_size
= SMB_VFS_GET_ALLOC_SIZE(conn
,fsp
,&sbuf
);
1835 fattr
= dos_mode(conn
,fsp
->fsp_name
,&sbuf
);
1836 mtime
= sbuf
.st_mtime
;
1838 close_file(req
, fsp
, ERROR_CLOSE
);
1839 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
1840 END_PROFILE(SMBopenX
);
1844 /* If the caller set the extended oplock request bit
1845 and we granted one (by whatever means) - set the
1846 correct bit for extended oplock reply.
1849 if (ex_oplock_request
&& lp_fake_oplocks(SNUM(conn
))) {
1850 smb_action
|= EXTENDED_OPLOCK_GRANTED
;
1853 if(ex_oplock_request
&& EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1854 smb_action
|= EXTENDED_OPLOCK_GRANTED
;
1857 /* If the caller set the core oplock request bit
1858 and we granted one (by whatever means) - set the
1859 correct bit for core oplock reply.
1862 if (open_flags
& EXTENDED_RESPONSE_REQUIRED
) {
1863 reply_outbuf(req
, 19, 0);
1865 reply_outbuf(req
, 15, 0);
1868 if (core_oplock_request
&& lp_fake_oplocks(SNUM(conn
))) {
1869 SCVAL(req
->outbuf
, smb_flg
,
1870 CVAL(req
->outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
1873 if(core_oplock_request
&& EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
1874 SCVAL(req
->outbuf
, smb_flg
,
1875 CVAL(req
->outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
1878 SSVAL(req
->outbuf
,smb_vwv2
,fsp
->fnum
);
1879 SSVAL(req
->outbuf
,smb_vwv3
,fattr
);
1880 if(lp_dos_filetime_resolution(SNUM(conn
)) ) {
1881 srv_put_dos_date3((char *)req
->outbuf
,smb_vwv4
,mtime
& ~1);
1883 srv_put_dos_date3((char *)req
->outbuf
,smb_vwv4
,mtime
);
1885 SIVAL(req
->outbuf
,smb_vwv6
,(uint32
)sbuf
.st_size
);
1886 SSVAL(req
->outbuf
,smb_vwv8
,GET_OPENX_MODE(deny_mode
));
1887 SSVAL(req
->outbuf
,smb_vwv11
,smb_action
);
1889 if (open_flags
& EXTENDED_RESPONSE_REQUIRED
) {
1890 SIVAL(req
->outbuf
, smb_vwv15
, STD_RIGHT_ALL_ACCESS
);
1893 END_PROFILE(SMBopenX
);
1898 /****************************************************************************
1899 Reply to a SMBulogoffX.
1900 ****************************************************************************/
1902 void reply_ulogoffX(struct smb_request
*req
)
1906 START_PROFILE(SMBulogoffX
);
1908 vuser
= get_valid_user_struct(req
->vuid
);
1911 DEBUG(3,("ulogoff, vuser id %d does not map to user.\n",
1915 /* in user level security we are supposed to close any files
1916 open by this user */
1917 if ((vuser
!= NULL
) && (lp_security() != SEC_SHARE
)) {
1918 file_close_user(req
->vuid
);
1921 invalidate_vuid(req
->vuid
);
1923 reply_outbuf(req
, 2, 0);
1925 DEBUG( 3, ( "ulogoffX vuid=%d\n", req
->vuid
) );
1927 END_PROFILE(SMBulogoffX
);
1928 req
->vuid
= UID_FIELD_INVALID
;
1932 /****************************************************************************
1933 Reply to a mknew or a create.
1934 ****************************************************************************/
1936 void reply_mknew(struct smb_request
*req
)
1938 connection_struct
*conn
= req
->conn
;
1941 struct smb_file_time ft
;
1943 int oplock_request
= 0;
1944 SMB_STRUCT_STAT sbuf
;
1946 uint32 access_mask
= FILE_GENERIC_READ
| FILE_GENERIC_WRITE
;
1947 uint32 share_mode
= FILE_SHARE_READ
|FILE_SHARE_WRITE
;
1948 uint32 create_disposition
;
1949 uint32 create_options
= 0;
1950 TALLOC_CTX
*ctx
= talloc_tos();
1952 START_PROFILE(SMBcreate
);
1954 SET_STAT_INVALID(sbuf
);
1957 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
1958 END_PROFILE(SMBcreate
);
1962 fattr
= SVAL(req
->vwv
+0, 0);
1963 oplock_request
= CORE_OPLOCK_REQUEST(req
->inbuf
);
1966 ft
.mtime
= convert_time_t_to_timespec(srv_make_unix_date3(req
->vwv
+1));
1968 srvstr_get_path_req(ctx
, req
, &fname
, (const char *)req
->buf
+ 1,
1969 STR_TERMINATE
, &status
);
1970 if (!NT_STATUS_IS_OK(status
)) {
1971 reply_nterror(req
, status
);
1972 END_PROFILE(SMBcreate
);
1976 if (fattr
& aVOLID
) {
1977 DEBUG(0,("Attempt to create file (%s) with volid set - "
1978 "please report this\n", fname
));
1981 if(req
->cmd
== SMBmknew
) {
1982 /* We should fail if file exists. */
1983 create_disposition
= FILE_CREATE
;
1985 /* Create if file doesn't exist, truncate if it does. */
1986 create_disposition
= FILE_OVERWRITE_IF
;
1989 status
= SMB_VFS_CREATE_FILE(
1992 0, /* root_dir_fid */
1994 CFF_DOS_PATH
, /* create_file_flags */
1995 access_mask
, /* access_mask */
1996 share_mode
, /* share_access */
1997 create_disposition
, /* create_disposition*/
1998 create_options
, /* create_options */
1999 fattr
, /* file_attributes */
2000 oplock_request
, /* oplock_request */
2001 0, /* allocation_size */
2008 if (!NT_STATUS_IS_OK(status
)) {
2009 END_PROFILE(SMBcreate
);
2010 if (open_was_deferred(req
->mid
)) {
2011 /* We have re-scheduled this call. */
2014 reply_openerror(req
, status
);
2018 ft
.atime
= get_atimespec(&sbuf
); /* atime. */
2019 status
= smb_set_file_time(conn
, fsp
, fsp
->fsp_name
, &sbuf
, &ft
, true);
2020 if (!NT_STATUS_IS_OK(status
)) {
2021 END_PROFILE(SMBcreate
);
2022 reply_openerror(req
, status
);
2026 reply_outbuf(req
, 1, 0);
2027 SSVAL(req
->outbuf
,smb_vwv0
,fsp
->fnum
);
2029 if (oplock_request
&& lp_fake_oplocks(SNUM(conn
))) {
2030 SCVAL(req
->outbuf
,smb_flg
,
2031 CVAL(req
->outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
2034 if(EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
2035 SCVAL(req
->outbuf
,smb_flg
,
2036 CVAL(req
->outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
2039 DEBUG( 2, ( "reply_mknew: file %s\n", fsp
->fsp_name
) );
2040 DEBUG( 3, ( "reply_mknew %s fd=%d dmode=0x%x\n",
2041 fsp
->fsp_name
, fsp
->fh
->fd
, (unsigned int)fattr
) );
2043 END_PROFILE(SMBcreate
);
2047 /****************************************************************************
2048 Reply to a create temporary file.
2049 ****************************************************************************/
2051 void reply_ctemp(struct smb_request
*req
)
2053 connection_struct
*conn
= req
->conn
;
2059 SMB_STRUCT_STAT sbuf
;
2062 TALLOC_CTX
*ctx
= talloc_tos();
2064 START_PROFILE(SMBctemp
);
2067 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2068 END_PROFILE(SMBctemp
);
2072 fattr
= SVAL(req
->vwv
+0, 0);
2073 oplock_request
= CORE_OPLOCK_REQUEST(req
->inbuf
);
2075 srvstr_get_path_req(ctx
, req
, &fname
, (const char *)req
->buf
+1,
2076 STR_TERMINATE
, &status
);
2077 if (!NT_STATUS_IS_OK(status
)) {
2078 reply_nterror(req
, status
);
2079 END_PROFILE(SMBctemp
);
2083 fname
= talloc_asprintf(ctx
,
2087 fname
= talloc_strdup(ctx
, "TMXXXXXX");
2091 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2092 END_PROFILE(SMBctemp
);
2096 status
= resolve_dfspath(ctx
, conn
,
2097 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
2100 if (!NT_STATUS_IS_OK(status
)) {
2101 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
2102 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
2103 ERRSRV
, ERRbadpath
);
2104 END_PROFILE(SMBctemp
);
2107 reply_nterror(req
, status
);
2108 END_PROFILE(SMBctemp
);
2112 status
= unix_convert(ctx
, conn
, fname
, False
, &fname
, NULL
, &sbuf
);
2113 if (!NT_STATUS_IS_OK(status
)) {
2114 reply_nterror(req
, status
);
2115 END_PROFILE(SMBctemp
);
2119 status
= check_name(conn
, fname
);
2120 if (!NT_STATUS_IS_OK(status
)) {
2121 reply_nterror(req
, status
);
2122 END_PROFILE(SMBctemp
);
2126 tmpfd
= smb_mkstemp(fname
);
2128 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
2129 END_PROFILE(SMBctemp
);
2133 SET_STAT_INVALID(sbuf
);
2134 SMB_VFS_STAT(conn
,fname
,&sbuf
);
2136 /* We should fail if file does not exist. */
2137 status
= SMB_VFS_CREATE_FILE(
2140 0, /* root_dir_fid */
2142 0, /* create_file_flags */
2143 FILE_GENERIC_READ
| FILE_GENERIC_WRITE
, /* access_mask */
2144 FILE_SHARE_READ
| FILE_SHARE_WRITE
, /* share_access */
2145 FILE_OPEN
, /* create_disposition*/
2146 0, /* create_options */
2147 fattr
, /* file_attributes */
2148 oplock_request
, /* oplock_request */
2149 0, /* allocation_size */
2156 /* close fd from smb_mkstemp() */
2159 if (!NT_STATUS_IS_OK(status
)) {
2160 if (open_was_deferred(req
->mid
)) {
2161 /* We have re-scheduled this call. */
2162 END_PROFILE(SMBctemp
);
2165 reply_openerror(req
, status
);
2166 END_PROFILE(SMBctemp
);
2170 reply_outbuf(req
, 1, 0);
2171 SSVAL(req
->outbuf
,smb_vwv0
,fsp
->fnum
);
2173 /* the returned filename is relative to the directory */
2174 s
= strrchr_m(fsp
->fsp_name
, '/');
2182 /* Tested vs W2K3 - this doesn't seem to be here - null terminated filename is the only
2183 thing in the byte section. JRA */
2184 SSVALS(p
, 0, -1); /* what is this? not in spec */
2186 if (message_push_string(&req
->outbuf
, s
, STR_ASCII
|STR_TERMINATE
)
2188 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
2189 END_PROFILE(SMBctemp
);
2193 if (oplock_request
&& lp_fake_oplocks(SNUM(conn
))) {
2194 SCVAL(req
->outbuf
, smb_flg
,
2195 CVAL(req
->outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
2198 if (EXCLUSIVE_OPLOCK_TYPE(fsp
->oplock_type
)) {
2199 SCVAL(req
->outbuf
, smb_flg
,
2200 CVAL(req
->outbuf
,smb_flg
)|CORE_OPLOCK_GRANTED
);
2203 DEBUG( 2, ( "reply_ctemp: created temp file %s\n", fsp
->fsp_name
) );
2204 DEBUG( 3, ( "reply_ctemp %s fd=%d umode=0%o\n", fsp
->fsp_name
,
2205 fsp
->fh
->fd
, (unsigned int)sbuf
.st_mode
) );
2207 END_PROFILE(SMBctemp
);
2211 /*******************************************************************
2212 Check if a user is allowed to rename a file.
2213 ********************************************************************/
2215 static NTSTATUS
can_rename(connection_struct
*conn
, files_struct
*fsp
,
2216 uint16 dirtype
, SMB_STRUCT_STAT
*pst
)
2220 if (!CAN_WRITE(conn
)) {
2221 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
2224 fmode
= dos_mode(conn
, fsp
->fsp_name
, pst
);
2225 if ((fmode
& ~dirtype
) & (aHIDDEN
| aSYSTEM
)) {
2226 return NT_STATUS_NO_SUCH_FILE
;
2229 if (S_ISDIR(pst
->st_mode
)) {
2230 if (fsp
->posix_open
) {
2231 return NT_STATUS_OK
;
2234 /* If no pathnames are open below this
2235 directory, allow the rename. */
2237 if (file_find_subpath(fsp
)) {
2238 return NT_STATUS_ACCESS_DENIED
;
2240 return NT_STATUS_OK
;
2243 if (fsp
->access_mask
& (DELETE_ACCESS
|FILE_WRITE_ATTRIBUTES
)) {
2244 return NT_STATUS_OK
;
2247 return NT_STATUS_ACCESS_DENIED
;
2250 /*******************************************************************
2251 * unlink a file with all relevant access checks
2252 *******************************************************************/
2254 static NTSTATUS
do_unlink(connection_struct
*conn
,
2255 struct smb_request
*req
,
2259 SMB_STRUCT_STAT sbuf
;
2262 uint32 dirtype_orig
= dirtype
;
2265 DEBUG(10,("do_unlink: %s, dirtype = %d\n", fname
, dirtype
));
2267 if (!CAN_WRITE(conn
)) {
2268 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
2271 if (SMB_VFS_LSTAT(conn
,fname
,&sbuf
) != 0) {
2272 return map_nt_error_from_unix(errno
);
2275 fattr
= dos_mode(conn
,fname
,&sbuf
);
2277 if (dirtype
& FILE_ATTRIBUTE_NORMAL
) {
2278 dirtype
= aDIR
|aARCH
|aRONLY
;
2281 dirtype
&= (aDIR
|aARCH
|aRONLY
|aHIDDEN
|aSYSTEM
);
2283 return NT_STATUS_NO_SUCH_FILE
;
2286 if (!dir_check_ftype(conn
, fattr
, dirtype
)) {
2288 return NT_STATUS_FILE_IS_A_DIRECTORY
;
2290 return NT_STATUS_NO_SUCH_FILE
;
2293 if (dirtype_orig
& 0x8000) {
2294 /* These will never be set for POSIX. */
2295 return NT_STATUS_NO_SUCH_FILE
;
2299 if ((fattr
& dirtype
) & FILE_ATTRIBUTE_DIRECTORY
) {
2300 return NT_STATUS_FILE_IS_A_DIRECTORY
;
2303 if ((fattr
& ~dirtype
) & (FILE_ATTRIBUTE_HIDDEN
|FILE_ATTRIBUTE_SYSTEM
)) {
2304 return NT_STATUS_NO_SUCH_FILE
;
2307 if (dirtype
& 0xFF00) {
2308 /* These will never be set for POSIX. */
2309 return NT_STATUS_NO_SUCH_FILE
;
2314 return NT_STATUS_NO_SUCH_FILE
;
2317 /* Can't delete a directory. */
2319 return NT_STATUS_FILE_IS_A_DIRECTORY
;
2324 else if (dirtype
& aDIR
) /* Asked for a directory and it isn't. */
2325 return NT_STATUS_OBJECT_NAME_INVALID
;
2326 #endif /* JRATEST */
2328 /* Fix for bug #3035 from SATOH Fumiyasu <fumiyas@miraclelinux.com>
2330 On a Windows share, a file with read-only dosmode can be opened with
2331 DELETE_ACCESS. But on a Samba share (delete readonly = no), it
2332 fails with NT_STATUS_CANNOT_DELETE error.
2334 This semantic causes a problem that a user can not
2335 rename a file with read-only dosmode on a Samba share
2336 from a Windows command prompt (i.e. cmd.exe, but can rename
2337 from Windows Explorer).
2340 if (!lp_delete_readonly(SNUM(conn
))) {
2341 if (fattr
& aRONLY
) {
2342 return NT_STATUS_CANNOT_DELETE
;
2346 /* On open checks the open itself will check the share mode, so
2347 don't do it here as we'll get it wrong. */
2349 status
= SMB_VFS_CREATE_FILE
2352 0, /* root_dir_fid */
2354 0, /* create_file_flags */
2355 DELETE_ACCESS
, /* access_mask */
2356 FILE_SHARE_NONE
, /* share_access */
2357 FILE_OPEN
, /* create_disposition*/
2358 FILE_NON_DIRECTORY_FILE
, /* create_options */
2359 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
2360 0, /* oplock_request */
2361 0, /* allocation_size */
2368 if (!NT_STATUS_IS_OK(status
)) {
2369 DEBUG(10, ("SMB_VFS_CREATEFILE failed: %s\n",
2370 nt_errstr(status
)));
2374 /* The set is across all open files on this dev/inode pair. */
2375 if (!set_delete_on_close(fsp
, True
, &conn
->server_info
->utok
)) {
2376 close_file(req
, fsp
, NORMAL_CLOSE
);
2377 return NT_STATUS_ACCESS_DENIED
;
2380 return close_file(req
, fsp
, NORMAL_CLOSE
);
2383 /****************************************************************************
2384 The guts of the unlink command, split out so it may be called by the NT SMB
2386 ****************************************************************************/
2388 NTSTATUS
unlink_internals(connection_struct
*conn
, struct smb_request
*req
,
2389 uint32 dirtype
, const char *name_in
, bool has_wild
)
2391 const char *directory
= NULL
;
2396 NTSTATUS status
= NT_STATUS_OK
;
2397 SMB_STRUCT_STAT sbuf
, st
;
2398 TALLOC_CTX
*ctx
= talloc_tos();
2400 status
= unix_convert(ctx
, conn
, name_in
, has_wild
, &name
, NULL
, &sbuf
);
2401 if (!NT_STATUS_IS_OK(status
)) {
2405 p
= strrchr_m(name
,'/');
2407 directory
= talloc_strdup(ctx
, ".");
2409 return NT_STATUS_NO_MEMORY
;
2419 * We should only check the mangled cache
2420 * here if unix_convert failed. This means
2421 * that the path in 'mask' doesn't exist
2422 * on the file system and so we need to look
2423 * for a possible mangle. This patch from
2424 * Tine Smukavec <valentin.smukavec@hermes.si>.
2427 if (!VALID_STAT(sbuf
) && mangle_is_mangled(mask
,conn
->params
)) {
2428 char *new_mask
= NULL
;
2429 mangle_lookup_name_from_8_3(ctx
,
2439 directory
= talloc_asprintf(ctx
,
2444 return NT_STATUS_NO_MEMORY
;
2447 dirtype
= FILE_ATTRIBUTE_NORMAL
;
2450 status
= check_name(conn
, directory
);
2451 if (!NT_STATUS_IS_OK(status
)) {
2455 status
= do_unlink(conn
, req
, directory
, dirtype
);
2456 if (!NT_STATUS_IS_OK(status
)) {
2462 struct smb_Dir
*dir_hnd
= NULL
;
2466 if ((dirtype
& SAMBA_ATTRIBUTES_MASK
) == aDIR
) {
2467 return NT_STATUS_OBJECT_NAME_INVALID
;
2470 if (strequal(mask
,"????????.???")) {
2475 status
= check_name(conn
, directory
);
2476 if (!NT_STATUS_IS_OK(status
)) {
2480 dir_hnd
= OpenDir(talloc_tos(), conn
, directory
, mask
,
2482 if (dir_hnd
== NULL
) {
2483 return map_nt_error_from_unix(errno
);
2486 /* XXXX the CIFS spec says that if bit0 of the flags2 field is set then
2487 the pattern matches against the long name, otherwise the short name
2488 We don't implement this yet XXXX
2491 status
= NT_STATUS_NO_SUCH_FILE
;
2493 while ((dname
= ReadDirName(dir_hnd
, &offset
, &st
))) {
2496 if (!is_visible_file(conn
, directory
, dname
, &st
,
2502 /* Quick check for "." and ".." */
2503 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
2507 if(!mask_match(dname
, mask
, conn
->case_sensitive
)) {
2511 fname
= talloc_asprintf(ctx
, "%s/%s",
2515 return NT_STATUS_NO_MEMORY
;
2518 status
= check_name(conn
, fname
);
2519 if (!NT_STATUS_IS_OK(status
)) {
2520 TALLOC_FREE(dir_hnd
);
2524 status
= do_unlink(conn
, req
, fname
, dirtype
);
2525 if (!NT_STATUS_IS_OK(status
)) {
2531 DEBUG(3,("unlink_internals: successful unlink [%s]\n",
2536 TALLOC_FREE(dir_hnd
);
2539 if (count
== 0 && NT_STATUS_IS_OK(status
) && errno
!= 0) {
2540 status
= map_nt_error_from_unix(errno
);
2546 /****************************************************************************
2548 ****************************************************************************/
2550 void reply_unlink(struct smb_request
*req
)
2552 connection_struct
*conn
= req
->conn
;
2556 bool path_contains_wcard
= False
;
2557 TALLOC_CTX
*ctx
= talloc_tos();
2559 START_PROFILE(SMBunlink
);
2562 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
2563 END_PROFILE(SMBunlink
);
2567 dirtype
= SVAL(req
->vwv
+0, 0);
2569 srvstr_get_path_req_wcard(ctx
, req
, &name
, (const char *)req
->buf
+ 1,
2570 STR_TERMINATE
, &status
,
2571 &path_contains_wcard
);
2572 if (!NT_STATUS_IS_OK(status
)) {
2573 reply_nterror(req
, status
);
2574 END_PROFILE(SMBunlink
);
2578 status
= resolve_dfspath_wcard(ctx
, conn
,
2579 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
2582 &path_contains_wcard
);
2583 if (!NT_STATUS_IS_OK(status
)) {
2584 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
2585 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
2586 ERRSRV
, ERRbadpath
);
2587 END_PROFILE(SMBunlink
);
2590 reply_nterror(req
, status
);
2591 END_PROFILE(SMBunlink
);
2595 DEBUG(3,("reply_unlink : %s\n",name
));
2597 status
= unlink_internals(conn
, req
, dirtype
, name
,
2598 path_contains_wcard
);
2599 if (!NT_STATUS_IS_OK(status
)) {
2600 if (open_was_deferred(req
->mid
)) {
2601 /* We have re-scheduled this call. */
2602 END_PROFILE(SMBunlink
);
2605 reply_nterror(req
, status
);
2606 END_PROFILE(SMBunlink
);
2610 reply_outbuf(req
, 0, 0);
2611 END_PROFILE(SMBunlink
);
2616 /****************************************************************************
2618 ****************************************************************************/
2620 static void fail_readraw(void)
2622 const char *errstr
= talloc_asprintf(talloc_tos(),
2623 "FAIL ! reply_readbraw: socket write fail (%s)",
2628 exit_server_cleanly(errstr
);
2631 /****************************************************************************
2632 Fake (read/write) sendfile. Returns -1 on read or write fail.
2633 ****************************************************************************/
2635 static ssize_t
fake_sendfile(files_struct
*fsp
, SMB_OFF_T startpos
,
2639 size_t tosend
= nread
;
2646 bufsize
= MIN(nread
, 65536);
2648 if (!(buf
= SMB_MALLOC_ARRAY(char, bufsize
))) {
2652 while (tosend
> 0) {
2656 if (tosend
> bufsize
) {
2661 ret
= read_file(fsp
,buf
,startpos
,cur_read
);
2667 /* If we had a short read, fill with zeros. */
2668 if (ret
< cur_read
) {
2669 memset(buf
+ ret
, '\0', cur_read
- ret
);
2672 if (write_data(smbd_server_fd(),buf
,cur_read
) != cur_read
) {
2677 startpos
+= cur_read
;
2681 return (ssize_t
)nread
;
2684 #if defined(WITH_SENDFILE)
2685 /****************************************************************************
2686 Deal with the case of sendfile reading less bytes from the file than
2687 requested. Fill with zeros (all we can do).
2688 ****************************************************************************/
2690 static void sendfile_short_send(files_struct
*fsp
,
2695 #define SHORT_SEND_BUFSIZE 1024
2696 if (nread
< headersize
) {
2697 DEBUG(0,("sendfile_short_send: sendfile failed to send "
2698 "header for file %s (%s). Terminating\n",
2699 fsp
->fsp_name
, strerror(errno
) ));
2700 exit_server_cleanly("sendfile_short_send failed");
2703 nread
-= headersize
;
2705 if (nread
< smb_maxcnt
) {
2706 char *buf
= SMB_CALLOC_ARRAY(char, SHORT_SEND_BUFSIZE
);
2708 exit_server_cleanly("sendfile_short_send: "
2712 DEBUG(0,("sendfile_short_send: filling truncated file %s "
2713 "with zeros !\n", fsp
->fsp_name
));
2715 while (nread
< smb_maxcnt
) {
2717 * We asked for the real file size and told sendfile
2718 * to not go beyond the end of the file. But it can
2719 * happen that in between our fstat call and the
2720 * sendfile call the file was truncated. This is very
2721 * bad because we have already announced the larger
2722 * number of bytes to the client.
2724 * The best we can do now is to send 0-bytes, just as
2725 * a read from a hole in a sparse file would do.
2727 * This should happen rarely enough that I don't care
2728 * about efficiency here :-)
2732 to_write
= MIN(SHORT_SEND_BUFSIZE
, smb_maxcnt
- nread
);
2733 if (write_data(smbd_server_fd(), buf
, to_write
) != to_write
) {
2734 exit_server_cleanly("sendfile_short_send: "
2735 "write_data failed");
2742 #endif /* defined WITH_SENDFILE */
2744 /****************************************************************************
2745 Return a readbraw error (4 bytes of zero).
2746 ****************************************************************************/
2748 static void reply_readbraw_error(void)
2752 if (write_data(smbd_server_fd(),header
,4) != 4) {
2757 /****************************************************************************
2758 Use sendfile in readbraw.
2759 ****************************************************************************/
2761 static void send_file_readbraw(connection_struct
*conn
,
2762 struct smb_request
*req
,
2768 char *outbuf
= NULL
;
2771 #if defined(WITH_SENDFILE)
2773 * We can only use sendfile on a non-chained packet
2774 * but we can use on a non-oplocked file. tridge proved this
2775 * on a train in Germany :-). JRA.
2776 * reply_readbraw has already checked the length.
2779 if ( !req_is_in_chain(req
) && (nread
> 0) && (fsp
->base_fsp
== NULL
) &&
2780 (fsp
->wcp
== NULL
) && lp_use_sendfile(SNUM(conn
)) ) {
2781 ssize_t sendfile_read
= -1;
2783 DATA_BLOB header_blob
;
2785 _smb_setlen(header
,nread
);
2786 header_blob
= data_blob_const(header
, 4);
2788 if ((sendfile_read
= SMB_VFS_SENDFILE(smbd_server_fd(), fsp
,
2789 &header_blob
, startpos
, nread
)) == -1) {
2790 /* Returning ENOSYS means no data at all was sent.
2791 * Do this as a normal read. */
2792 if (errno
== ENOSYS
) {
2793 goto normal_readbraw
;
2797 * Special hack for broken Linux with no working sendfile. If we
2798 * return EINTR we sent the header but not the rest of the data.
2799 * Fake this up by doing read/write calls.
2801 if (errno
== EINTR
) {
2802 /* Ensure we don't do this again. */
2803 set_use_sendfile(SNUM(conn
), False
);
2804 DEBUG(0,("send_file_readbraw: sendfile not available. Faking..\n"));
2806 if (fake_sendfile(fsp
, startpos
, nread
) == -1) {
2807 DEBUG(0,("send_file_readbraw: fake_sendfile failed for file %s (%s).\n",
2808 fsp
->fsp_name
, strerror(errno
) ));
2809 exit_server_cleanly("send_file_readbraw fake_sendfile failed");
2814 DEBUG(0,("send_file_readbraw: sendfile failed for file %s (%s). Terminating\n",
2815 fsp
->fsp_name
, strerror(errno
) ));
2816 exit_server_cleanly("send_file_readbraw sendfile failed");
2817 } else if (sendfile_read
== 0) {
2819 * Some sendfile implementations return 0 to indicate
2820 * that there was a short read, but nothing was
2821 * actually written to the socket. In this case,
2822 * fallback to the normal read path so the header gets
2823 * the correct byte count.
2825 DEBUG(3, ("send_file_readbraw: sendfile sent zero "
2826 "bytes falling back to the normal read: "
2827 "%s\n", fsp
->fsp_name
));
2828 goto normal_readbraw
;
2831 /* Deal with possible short send. */
2832 if (sendfile_read
!= 4+nread
) {
2833 sendfile_short_send(fsp
, sendfile_read
, 4, nread
);
2841 outbuf
= TALLOC_ARRAY(NULL
, char, nread
+4);
2843 DEBUG(0,("send_file_readbraw: TALLOC_ARRAY failed for size %u.\n",
2844 (unsigned)(nread
+4)));
2845 reply_readbraw_error();
2850 ret
= read_file(fsp
,outbuf
+4,startpos
,nread
);
2851 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2860 _smb_setlen(outbuf
,ret
);
2861 if (write_data(smbd_server_fd(),outbuf
,4+ret
) != 4+ret
)
2864 TALLOC_FREE(outbuf
);
2867 /****************************************************************************
2868 Reply to a readbraw (core+ protocol).
2869 ****************************************************************************/
2871 void reply_readbraw(struct smb_request
*req
)
2873 connection_struct
*conn
= req
->conn
;
2874 ssize_t maxcount
,mincount
;
2878 struct lock_struct lock
;
2882 START_PROFILE(SMBreadbraw
);
2884 if (srv_is_signing_active() || is_encrypted_packet(req
->inbuf
)) {
2885 exit_server_cleanly("reply_readbraw: SMB signing/sealing is active - "
2886 "raw reads/writes are disallowed.");
2890 reply_readbraw_error();
2891 END_PROFILE(SMBreadbraw
);
2896 * Special check if an oplock break has been issued
2897 * and the readraw request croses on the wire, we must
2898 * return a zero length response here.
2901 fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
2904 * We have to do a check_fsp by hand here, as
2905 * we must always return 4 zero bytes on error,
2909 if (!fsp
|| !conn
|| conn
!= fsp
->conn
||
2910 req
->vuid
!= fsp
->vuid
||
2911 fsp
->is_directory
|| fsp
->fh
->fd
== -1) {
2913 * fsp could be NULL here so use the value from the packet. JRA.
2915 DEBUG(3,("reply_readbraw: fnum %d not valid "
2917 (int)SVAL(req
->vwv
+0, 0)));
2918 reply_readbraw_error();
2919 END_PROFILE(SMBreadbraw
);
2923 /* Do a "by hand" version of CHECK_READ. */
2924 if (!(fsp
->can_read
||
2925 ((req
->flags2
& FLAGS2_READ_PERMIT_EXECUTE
) &&
2926 (fsp
->access_mask
& FILE_EXECUTE
)))) {
2927 DEBUG(3,("reply_readbraw: fnum %d not readable.\n",
2928 (int)SVAL(req
->vwv
+0, 0)));
2929 reply_readbraw_error();
2930 END_PROFILE(SMBreadbraw
);
2934 flush_write_cache(fsp
, READRAW_FLUSH
);
2936 startpos
= IVAL_TO_SMB_OFF_T(req
->vwv
+1, 0);
2937 if(req
->wct
== 10) {
2939 * This is a large offset (64 bit) read.
2941 #ifdef LARGE_SMB_OFF_T
2943 startpos
|= (((SMB_OFF_T
)IVAL(req
->vwv
+8, 0)) << 32);
2945 #else /* !LARGE_SMB_OFF_T */
2948 * Ensure we haven't been sent a >32 bit offset.
2951 if(IVAL(req
->vwv
+8, 0) != 0) {
2952 DEBUG(0,("reply_readbraw: large offset "
2953 "(%x << 32) used and we don't support "
2954 "64 bit offsets.\n",
2955 (unsigned int)IVAL(req
->vwv
+8, 0) ));
2956 reply_readbraw_error();
2957 END_PROFILE(SMBreadbraw
);
2961 #endif /* LARGE_SMB_OFF_T */
2964 DEBUG(0,("reply_readbraw: negative 64 bit "
2965 "readraw offset (%.0f) !\n",
2966 (double)startpos
));
2967 reply_readbraw_error();
2968 END_PROFILE(SMBreadbraw
);
2973 maxcount
= (SVAL(req
->vwv
+3, 0) & 0xFFFF);
2974 mincount
= (SVAL(req
->vwv
+4, 0) & 0xFFFF);
2976 /* ensure we don't overrun the packet size */
2977 maxcount
= MIN(65535,maxcount
);
2979 init_strict_lock_struct(fsp
, (uint32
)req
->smbpid
,
2980 (uint64_t)startpos
, (uint64_t)maxcount
, READ_LOCK
,
2983 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
2984 reply_readbraw_error();
2985 END_PROFILE(SMBreadbraw
);
2989 if (SMB_VFS_FSTAT(fsp
, &st
) == 0) {
2993 if (startpos
>= size
) {
2996 nread
= MIN(maxcount
,(size
- startpos
));
2999 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
3000 if (nread
< mincount
)
3004 DEBUG( 3, ( "reply_readbraw: fnum=%d start=%.0f max=%lu "
3005 "min=%lu nread=%lu\n",
3006 fsp
->fnum
, (double)startpos
,
3007 (unsigned long)maxcount
,
3008 (unsigned long)mincount
,
3009 (unsigned long)nread
) );
3011 send_file_readbraw(conn
, req
, fsp
, startpos
, nread
, mincount
);
3013 DEBUG(5,("reply_readbraw finished\n"));
3015 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
3017 END_PROFILE(SMBreadbraw
);
3022 #define DBGC_CLASS DBGC_LOCKING
3024 /****************************************************************************
3025 Reply to a lockread (core+ protocol).
3026 ****************************************************************************/
3028 void reply_lockread(struct smb_request
*req
)
3030 connection_struct
*conn
= req
->conn
;
3037 struct byte_range_lock
*br_lck
= NULL
;
3040 START_PROFILE(SMBlockread
);
3043 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3044 END_PROFILE(SMBlockread
);
3048 fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
3050 if (!check_fsp(conn
, req
, fsp
)) {
3051 END_PROFILE(SMBlockread
);
3055 if (!CHECK_READ(fsp
,req
)) {
3056 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
3057 END_PROFILE(SMBlockread
);
3061 numtoread
= SVAL(req
->vwv
+1, 0);
3062 startpos
= IVAL_TO_SMB_OFF_T(req
->vwv
+2, 0);
3064 numtoread
= MIN(BUFFER_SIZE
- (smb_size
+ 3*2 + 3), numtoread
);
3066 reply_outbuf(req
, 5, numtoread
+ 3);
3068 data
= smb_buf(req
->outbuf
) + 3;
3071 * NB. Discovered by Menny Hamburger at Mainsoft. This is a core+
3072 * protocol request that predates the read/write lock concept.
3073 * Thus instead of asking for a read lock here we need to ask
3074 * for a write lock. JRA.
3075 * Note that the requested lock size is unaffected by max_recv.
3078 br_lck
= do_lock(smbd_messaging_context(),
3081 (uint64_t)numtoread
,
3085 False
, /* Non-blocking lock. */
3089 TALLOC_FREE(br_lck
);
3091 if (NT_STATUS_V(status
)) {
3092 reply_nterror(req
, status
);
3093 END_PROFILE(SMBlockread
);
3098 * However the requested READ size IS affected by max_recv. Insanity.... JRA.
3101 if (numtoread
> max_recv
) {
3102 DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u). \
3103 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
3104 (unsigned int)numtoread
, (unsigned int)max_recv
));
3105 numtoread
= MIN(numtoread
,max_recv
);
3107 nread
= read_file(fsp
,data
,startpos
,numtoread
);
3110 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
3111 END_PROFILE(SMBlockread
);
3115 srv_set_message((char *)req
->outbuf
, 5, nread
+3, False
);
3117 SSVAL(req
->outbuf
,smb_vwv0
,nread
);
3118 SSVAL(req
->outbuf
,smb_vwv5
,nread
+3);
3119 p
= smb_buf(req
->outbuf
);
3120 SCVAL(p
,0,0); /* pad byte. */
3123 DEBUG(3,("lockread fnum=%d num=%d nread=%d\n",
3124 fsp
->fnum
, (int)numtoread
, (int)nread
));
3126 END_PROFILE(SMBlockread
);
3131 #define DBGC_CLASS DBGC_ALL
3133 /****************************************************************************
3135 ****************************************************************************/
3137 void reply_read(struct smb_request
*req
)
3139 connection_struct
*conn
= req
->conn
;
3146 struct lock_struct lock
;
3148 START_PROFILE(SMBread
);
3151 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3152 END_PROFILE(SMBread
);
3156 fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
3158 if (!check_fsp(conn
, req
, fsp
)) {
3159 END_PROFILE(SMBread
);
3163 if (!CHECK_READ(fsp
,req
)) {
3164 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
3165 END_PROFILE(SMBread
);
3169 numtoread
= SVAL(req
->vwv
+1, 0);
3170 startpos
= IVAL_TO_SMB_OFF_T(req
->vwv
+2, 0);
3172 numtoread
= MIN(BUFFER_SIZE
-outsize
,numtoread
);
3175 * The requested read size cannot be greater than max_recv. JRA.
3177 if (numtoread
> max_recv
) {
3178 DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u). \
3179 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
3180 (unsigned int)numtoread
, (unsigned int)max_recv
));
3181 numtoread
= MIN(numtoread
,max_recv
);
3184 reply_outbuf(req
, 5, numtoread
+3);
3186 data
= smb_buf(req
->outbuf
) + 3;
3188 init_strict_lock_struct(fsp
, (uint32
)req
->smbpid
,
3189 (uint64_t)startpos
, (uint64_t)numtoread
, READ_LOCK
,
3192 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
3193 reply_doserror(req
, ERRDOS
,ERRlock
);
3194 END_PROFILE(SMBread
);
3199 nread
= read_file(fsp
,data
,startpos
,numtoread
);
3202 reply_unixerror(req
, ERRDOS
,ERRnoaccess
);
3206 srv_set_message((char *)req
->outbuf
, 5, nread
+3, False
);
3208 SSVAL(req
->outbuf
,smb_vwv0
,nread
);
3209 SSVAL(req
->outbuf
,smb_vwv5
,nread
+3);
3210 SCVAL(smb_buf(req
->outbuf
),0,1);
3211 SSVAL(smb_buf(req
->outbuf
),1,nread
);
3213 DEBUG( 3, ( "read fnum=%d num=%d nread=%d\n",
3214 fsp
->fnum
, (int)numtoread
, (int)nread
) );
3217 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
3219 END_PROFILE(SMBread
);
3223 /****************************************************************************
3225 ****************************************************************************/
3227 static int setup_readX_header(struct smb_request
*req
, char *outbuf
,
3233 outsize
= srv_set_message(outbuf
,12,smb_maxcnt
,False
);
3234 data
= smb_buf(outbuf
);
3236 memset(outbuf
+smb_vwv0
,'\0',24); /* valgrind init. */
3238 SCVAL(outbuf
,smb_vwv0
,0xFF);
3239 SSVAL(outbuf
,smb_vwv2
,0xFFFF); /* Remaining - must be -1. */
3240 SSVAL(outbuf
,smb_vwv5
,smb_maxcnt
);
3241 SSVAL(outbuf
,smb_vwv6
,
3243 + 1 /* the wct field */
3244 + 12 * sizeof(uint16_t) /* vwv */
3245 + 2); /* the buflen field */
3246 SSVAL(outbuf
,smb_vwv7
,(smb_maxcnt
>> 16));
3247 SSVAL(outbuf
,smb_vwv11
,smb_maxcnt
);
3248 /* Reset the outgoing length, set_message truncates at 0x1FFFF. */
3249 _smb_setlen_large(outbuf
,(smb_size
+ 12*2 + smb_maxcnt
- 4));
3253 /****************************************************************************
3254 Reply to a read and X - possibly using sendfile.
3255 ****************************************************************************/
3257 static void send_file_readX(connection_struct
*conn
, struct smb_request
*req
,
3258 files_struct
*fsp
, SMB_OFF_T startpos
,
3261 SMB_STRUCT_STAT sbuf
;
3263 struct lock_struct lock
;
3265 if(SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
3266 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
3270 init_strict_lock_struct(fsp
, (uint32
)req
->smbpid
,
3271 (uint64_t)startpos
, (uint64_t)smb_maxcnt
, READ_LOCK
,
3274 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
3275 reply_doserror(req
, ERRDOS
, ERRlock
);
3279 if (!S_ISREG(sbuf
.st_mode
) || (startpos
> sbuf
.st_size
)
3280 || (smb_maxcnt
> (sbuf
.st_size
- startpos
))) {
3282 * We already know that we would do a short read, so don't
3283 * try the sendfile() path.
3285 goto nosendfile_read
;
3288 #if defined(WITH_SENDFILE)
3290 * We can only use sendfile on a non-chained packet
3291 * but we can use on a non-oplocked file. tridge proved this
3292 * on a train in Germany :-). JRA.
3295 if (!req_is_in_chain(req
) &&
3296 !is_encrypted_packet(req
->inbuf
) && (fsp
->base_fsp
== NULL
) &&
3297 lp_use_sendfile(SNUM(conn
)) && (fsp
->wcp
== NULL
) ) {
3298 uint8 headerbuf
[smb_size
+ 12 * 2];
3302 * Set up the packet header before send. We
3303 * assume here the sendfile will work (get the
3304 * correct amount of data).
3307 header
= data_blob_const(headerbuf
, sizeof(headerbuf
));
3309 construct_reply_common_req(req
, (char *)headerbuf
);
3310 setup_readX_header(req
, (char *)headerbuf
, smb_maxcnt
);
3312 if ((nread
= SMB_VFS_SENDFILE(smbd_server_fd(), fsp
, &header
, startpos
, smb_maxcnt
)) == -1) {
3313 /* Returning ENOSYS means no data at all was sent.
3314 Do this as a normal read. */
3315 if (errno
== ENOSYS
) {
3320 * Special hack for broken Linux with no working sendfile. If we
3321 * return EINTR we sent the header but not the rest of the data.
3322 * Fake this up by doing read/write calls.
3325 if (errno
== EINTR
) {
3326 /* Ensure we don't do this again. */
3327 set_use_sendfile(SNUM(conn
), False
);
3328 DEBUG(0,("send_file_readX: sendfile not available. Faking..\n"));
3329 nread
= fake_sendfile(fsp
, startpos
,
3332 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
3333 fsp
->fsp_name
, strerror(errno
) ));
3334 exit_server_cleanly("send_file_readX: fake_sendfile failed");
3336 DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
3337 fsp
->fnum
, (int)smb_maxcnt
, (int)nread
) );
3338 /* No outbuf here means successful sendfile. */
3342 DEBUG(0,("send_file_readX: sendfile failed for file %s (%s). Terminating\n",
3343 fsp
->fsp_name
, strerror(errno
) ));
3344 exit_server_cleanly("send_file_readX sendfile failed");
3345 } else if (nread
== 0) {
3347 * Some sendfile implementations return 0 to indicate
3348 * that there was a short read, but nothing was
3349 * actually written to the socket. In this case,
3350 * fallback to the normal read path so the header gets
3351 * the correct byte count.
3353 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
3354 "falling back to the normal read: %s\n",
3359 DEBUG( 3, ( "send_file_readX: sendfile fnum=%d max=%d nread=%d\n",
3360 fsp
->fnum
, (int)smb_maxcnt
, (int)nread
) );
3362 /* Deal with possible short send. */
3363 if (nread
!= smb_maxcnt
+ sizeof(headerbuf
)) {
3364 sendfile_short_send(fsp
, nread
, sizeof(headerbuf
), smb_maxcnt
);
3366 /* No outbuf here means successful sendfile. */
3367 SMB_PERFCOUNT_SET_MSGLEN_OUT(&req
->pcd
, nread
);
3368 SMB_PERFCOUNT_END(&req
->pcd
);
3376 if ((smb_maxcnt
& 0xFF0000) > 0x10000) {
3377 uint8 headerbuf
[smb_size
+ 2*12];
3379 construct_reply_common_req(req
, (char *)headerbuf
);
3380 setup_readX_header(req
, (char *)headerbuf
, smb_maxcnt
);
3382 /* Send out the header. */
3383 if (write_data(smbd_server_fd(), (char *)headerbuf
,
3384 sizeof(headerbuf
)) != sizeof(headerbuf
)) {
3385 DEBUG(0,("send_file_readX: write_data failed for file %s (%s). Terminating\n",
3386 fsp
->fsp_name
, strerror(errno
) ));
3387 exit_server_cleanly("send_file_readX sendfile failed");
3389 nread
= fake_sendfile(fsp
, startpos
, smb_maxcnt
);
3391 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
3392 fsp
->fsp_name
, strerror(errno
) ));
3393 exit_server_cleanly("send_file_readX: fake_sendfile failed");
3400 reply_outbuf(req
, 12, smb_maxcnt
);
3402 nread
= read_file(fsp
, smb_buf(req
->outbuf
), startpos
, smb_maxcnt
);
3404 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
3407 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
3411 setup_readX_header(req
, (char *)req
->outbuf
, nread
);
3413 DEBUG( 3, ( "send_file_readX fnum=%d max=%d nread=%d\n",
3414 fsp
->fnum
, (int)smb_maxcnt
, (int)nread
) );
3420 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
3421 TALLOC_FREE(req
->outbuf
);
3425 /****************************************************************************
3426 Reply to a read and X.
3427 ****************************************************************************/
3429 void reply_read_and_X(struct smb_request
*req
)
3431 connection_struct
*conn
= req
->conn
;
3435 bool big_readX
= False
;
3437 size_t smb_mincnt
= SVAL(req
->vwv
+6, 0);
3440 START_PROFILE(SMBreadX
);
3442 if ((req
->wct
!= 10) && (req
->wct
!= 12)) {
3443 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3447 fsp
= file_fsp(req
, SVAL(req
->vwv
+2, 0));
3448 startpos
= IVAL_TO_SMB_OFF_T(req
->vwv
+3, 0);
3449 smb_maxcnt
= SVAL(req
->vwv
+5, 0);
3451 /* If it's an IPC, pass off the pipe handler. */
3453 reply_pipe_read_and_X(req
);
3454 END_PROFILE(SMBreadX
);
3458 if (!check_fsp(conn
, req
, fsp
)) {
3459 END_PROFILE(SMBreadX
);
3463 if (!CHECK_READ(fsp
,req
)) {
3464 reply_doserror(req
, ERRDOS
,ERRbadaccess
);
3465 END_PROFILE(SMBreadX
);
3469 if (global_client_caps
& CAP_LARGE_READX
) {
3470 size_t upper_size
= SVAL(req
->vwv
+7, 0);
3471 smb_maxcnt
|= (upper_size
<<16);
3472 if (upper_size
> 1) {
3473 /* Can't do this on a chained packet. */
3474 if ((CVAL(req
->vwv
+0, 0) != 0xFF)) {
3475 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
3476 END_PROFILE(SMBreadX
);
3479 /* We currently don't do this on signed or sealed data. */
3480 if (srv_is_signing_active() || is_encrypted_packet(req
->inbuf
)) {
3481 reply_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
3482 END_PROFILE(SMBreadX
);
3485 /* Is there room in the reply for this data ? */
3486 if (smb_maxcnt
> (0xFFFFFF - (smb_size
-4 + 12*2))) {
3488 NT_STATUS_INVALID_PARAMETER
);
3489 END_PROFILE(SMBreadX
);
3496 if (req
->wct
== 12) {
3497 #ifdef LARGE_SMB_OFF_T
3499 * This is a large offset (64 bit) read.
3501 startpos
|= (((SMB_OFF_T
)IVAL(req
->vwv
+10, 0)) << 32);
3503 #else /* !LARGE_SMB_OFF_T */
3506 * Ensure we haven't been sent a >32 bit offset.
3509 if(IVAL(req
->vwv
+10, 0) != 0) {
3510 DEBUG(0,("reply_read_and_X - large offset (%x << 32) "
3511 "used and we don't support 64 bit offsets.\n",
3512 (unsigned int)IVAL(req
->vwv
+10, 0) ));
3513 END_PROFILE(SMBreadX
);
3514 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
3518 #endif /* LARGE_SMB_OFF_T */
3523 schedule_aio_read_and_X(conn
, req
, fsp
, startpos
, smb_maxcnt
)) {
3527 send_file_readX(conn
, req
, fsp
, startpos
, smb_maxcnt
);
3530 END_PROFILE(SMBreadX
);
3534 /****************************************************************************
3535 Error replies to writebraw must have smb_wct == 1. Fix this up.
3536 ****************************************************************************/
3538 void error_to_writebrawerr(struct smb_request
*req
)
3540 uint8
*old_outbuf
= req
->outbuf
;
3542 reply_outbuf(req
, 1, 0);
3544 memcpy(req
->outbuf
, old_outbuf
, smb_size
);
3545 TALLOC_FREE(old_outbuf
);
3548 /****************************************************************************
3549 Reply to a writebraw (core+ or LANMAN1.0 protocol).
3550 ****************************************************************************/
3552 void reply_writebraw(struct smb_request
*req
)
3554 connection_struct
*conn
= req
->conn
;
3557 ssize_t total_written
=0;
3558 size_t numtowrite
=0;
3564 struct lock_struct lock
;
3567 START_PROFILE(SMBwritebraw
);
3570 * If we ever reply with an error, it must have the SMB command
3571 * type of SMBwritec, not SMBwriteBraw, as this tells the client
3574 SCVAL(req
->inbuf
,smb_com
,SMBwritec
);
3576 if (srv_is_signing_active()) {
3577 END_PROFILE(SMBwritebraw
);
3578 exit_server_cleanly("reply_writebraw: SMB signing is active - "
3579 "raw reads/writes are disallowed.");
3582 if (req
->wct
< 12) {
3583 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3584 error_to_writebrawerr(req
);
3585 END_PROFILE(SMBwritebraw
);
3589 fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
3590 if (!check_fsp(conn
, req
, fsp
)) {
3591 error_to_writebrawerr(req
);
3592 END_PROFILE(SMBwritebraw
);
3596 if (!CHECK_WRITE(fsp
)) {
3597 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
3598 error_to_writebrawerr(req
);
3599 END_PROFILE(SMBwritebraw
);
3603 tcount
= IVAL(req
->vwv
+1, 0);
3604 startpos
= IVAL_TO_SMB_OFF_T(req
->vwv
+3, 0);
3605 write_through
= BITSETW(req
->vwv
+7,0);
3607 /* We have to deal with slightly different formats depending
3608 on whether we are using the core+ or lanman1.0 protocol */
3610 if(Protocol
<= PROTOCOL_COREPLUS
) {
3611 numtowrite
= SVAL(smb_buf(req
->inbuf
),-2);
3612 data
= smb_buf(req
->inbuf
);
3614 numtowrite
= SVAL(req
->vwv
+10, 0);
3615 data
= smb_base(req
->inbuf
) + SVAL(req
->vwv
+11, 0);
3618 /* Ensure we don't write bytes past the end of this packet. */
3619 if (data
+ numtowrite
> smb_base(req
->inbuf
) + smb_len(req
->inbuf
)) {
3620 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3621 error_to_writebrawerr(req
);
3622 END_PROFILE(SMBwritebraw
);
3626 init_strict_lock_struct(fsp
, (uint32
)req
->smbpid
,
3627 (uint64_t)startpos
, (uint64_t)tcount
, WRITE_LOCK
,
3630 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
3631 reply_doserror(req
, ERRDOS
, ERRlock
);
3632 error_to_writebrawerr(req
);
3633 END_PROFILE(SMBwritebraw
);
3638 nwritten
= write_file(req
,fsp
,data
,startpos
,numtowrite
);
3641 DEBUG(3,("reply_writebraw: initial write fnum=%d start=%.0f num=%d "
3642 "wrote=%d sync=%d\n",
3643 fsp
->fnum
, (double)startpos
, (int)numtowrite
,
3644 (int)nwritten
, (int)write_through
));
3646 if (nwritten
< (ssize_t
)numtowrite
) {
3647 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
3648 error_to_writebrawerr(req
);
3652 total_written
= nwritten
;
3654 /* Allocate a buffer of 64k + length. */
3655 buf
= TALLOC_ARRAY(NULL
, char, 65540);
3657 reply_doserror(req
, ERRDOS
, ERRnomem
);
3658 error_to_writebrawerr(req
);
3662 /* Return a SMBwritebraw message to the redirector to tell
3663 * it to send more bytes */
3665 memcpy(buf
, req
->inbuf
, smb_size
);
3666 srv_set_message(buf
,Protocol
>PROTOCOL_COREPLUS
?1:0,0,True
);
3667 SCVAL(buf
,smb_com
,SMBwritebraw
);
3668 SSVALS(buf
,smb_vwv0
,0xFFFF);
3670 if (!srv_send_smb(smbd_server_fd(),
3672 IS_CONN_ENCRYPTED(conn
),
3674 exit_server_cleanly("reply_writebraw: srv_send_smb "
3678 /* Now read the raw data into the buffer and write it */
3679 status
= read_smb_length(smbd_server_fd(), buf
, SMB_SECONDARY_WAIT
,
3681 if (!NT_STATUS_IS_OK(status
)) {
3682 exit_server_cleanly("secondary writebraw failed");
3685 /* Set up outbuf to return the correct size */
3686 reply_outbuf(req
, 1, 0);
3688 if (numtowrite
!= 0) {
3690 if (numtowrite
> 0xFFFF) {
3691 DEBUG(0,("reply_writebraw: Oversize secondary write "
3692 "raw requested (%u). Terminating\n",
3693 (unsigned int)numtowrite
));
3694 exit_server_cleanly("secondary writebraw failed");
3697 if (tcount
> nwritten
+numtowrite
) {
3698 DEBUG(3,("reply_writebraw: Client overestimated the "
3700 (int)tcount
,(int)nwritten
,(int)numtowrite
));
3703 status
= read_data(smbd_server_fd(), buf
+4, numtowrite
);
3705 if (!NT_STATUS_IS_OK(status
)) {
3706 DEBUG(0,("reply_writebraw: Oversize secondary write "
3707 "raw read failed (%s). Terminating\n",
3708 nt_errstr(status
)));
3709 exit_server_cleanly("secondary writebraw failed");
3712 nwritten
= write_file(req
,fsp
,buf
+4,startpos
+nwritten
,numtowrite
);
3713 if (nwritten
== -1) {
3715 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
3716 error_to_writebrawerr(req
);
3720 if (nwritten
< (ssize_t
)numtowrite
) {
3721 SCVAL(req
->outbuf
,smb_rcls
,ERRHRD
);
3722 SSVAL(req
->outbuf
,smb_err
,ERRdiskfull
);
3726 total_written
+= nwritten
;
3731 SSVAL(req
->outbuf
,smb_vwv0
,total_written
);
3733 status
= sync_file(conn
, fsp
, write_through
);
3734 if (!NT_STATUS_IS_OK(status
)) {
3735 DEBUG(5,("reply_writebraw: sync_file for %s returned %s\n",
3736 fsp
->fsp_name
, nt_errstr(status
) ));
3737 reply_nterror(req
, status
);
3738 error_to_writebrawerr(req
);
3742 DEBUG(3,("reply_writebraw: secondart write fnum=%d start=%.0f num=%d "
3744 fsp
->fnum
, (double)startpos
, (int)numtowrite
,
3745 (int)total_written
));
3747 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
3749 /* We won't return a status if write through is not selected - this
3750 * follows what WfWg does */
3751 END_PROFILE(SMBwritebraw
);
3753 if (!write_through
&& total_written
==tcount
) {
3755 #if RABBIT_PELLET_FIX
3757 * Fix for "rabbit pellet" mode, trigger an early TCP ack by
3758 * sending a SMBkeepalive. Thanks to DaveCB at Sun for this.
3761 if (!send_keepalive(smbd_server_fd())) {
3762 exit_server_cleanly("reply_writebraw: send of "
3763 "keepalive failed");
3766 TALLOC_FREE(req
->outbuf
);
3771 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
3773 END_PROFILE(SMBwritebraw
);
3778 #define DBGC_CLASS DBGC_LOCKING
3780 /****************************************************************************
3781 Reply to a writeunlock (core+).
3782 ****************************************************************************/
3784 void reply_writeunlock(struct smb_request
*req
)
3786 connection_struct
*conn
= req
->conn
;
3787 ssize_t nwritten
= -1;
3791 NTSTATUS status
= NT_STATUS_OK
;
3793 struct lock_struct lock
;
3795 START_PROFILE(SMBwriteunlock
);
3798 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3799 END_PROFILE(SMBwriteunlock
);
3803 fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
3805 if (!check_fsp(conn
, req
, fsp
)) {
3806 END_PROFILE(SMBwriteunlock
);
3810 if (!CHECK_WRITE(fsp
)) {
3811 reply_doserror(req
, ERRDOS
,ERRbadaccess
);
3812 END_PROFILE(SMBwriteunlock
);
3816 numtowrite
= SVAL(req
->vwv
+1, 0);
3817 startpos
= IVAL_TO_SMB_OFF_T(req
->vwv
+2, 0);
3818 data
= (const char *)req
->buf
+ 3;
3821 init_strict_lock_struct(fsp
, (uint32
)req
->smbpid
,
3822 (uint64_t)startpos
, (uint64_t)numtowrite
, WRITE_LOCK
,
3825 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
3826 reply_doserror(req
, ERRDOS
, ERRlock
);
3827 END_PROFILE(SMBwriteunlock
);
3832 /* The special X/Open SMB protocol handling of
3833 zero length writes is *NOT* done for
3835 if(numtowrite
== 0) {
3838 nwritten
= write_file(req
,fsp
,data
,startpos
,numtowrite
);
3841 status
= sync_file(conn
, fsp
, False
/* write through */);
3842 if (!NT_STATUS_IS_OK(status
)) {
3843 DEBUG(5,("reply_writeunlock: sync_file for %s returned %s\n",
3844 fsp
->fsp_name
, nt_errstr(status
) ));
3845 reply_nterror(req
, status
);
3849 if(((nwritten
< numtowrite
) && (numtowrite
!= 0))||(nwritten
< 0)) {
3850 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
3855 status
= do_unlock(smbd_messaging_context(),
3858 (uint64_t)numtowrite
,
3862 if (NT_STATUS_V(status
)) {
3863 reply_nterror(req
, status
);
3868 reply_outbuf(req
, 1, 0);
3870 SSVAL(req
->outbuf
,smb_vwv0
,nwritten
);
3872 DEBUG(3,("writeunlock fnum=%d num=%d wrote=%d\n",
3873 fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
3877 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
3880 END_PROFILE(SMBwriteunlock
);
3885 #define DBGC_CLASS DBGC_ALL
3887 /****************************************************************************
3889 ****************************************************************************/
3891 void reply_write(struct smb_request
*req
)
3893 connection_struct
*conn
= req
->conn
;
3895 ssize_t nwritten
= -1;
3899 struct lock_struct lock
;
3902 START_PROFILE(SMBwrite
);
3905 END_PROFILE(SMBwrite
);
3906 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
3910 /* If it's an IPC, pass off the pipe handler. */
3912 reply_pipe_write(req
);
3913 END_PROFILE(SMBwrite
);
3917 fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
3919 if (!check_fsp(conn
, req
, fsp
)) {
3920 END_PROFILE(SMBwrite
);
3924 if (!CHECK_WRITE(fsp
)) {
3925 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
3926 END_PROFILE(SMBwrite
);
3930 numtowrite
= SVAL(req
->vwv
+1, 0);
3931 startpos
= IVAL_TO_SMB_OFF_T(req
->vwv
+2, 0);
3932 data
= (const char *)req
->buf
+ 3;
3934 init_strict_lock_struct(fsp
, (uint32
)req
->smbpid
,
3935 (uint64_t)startpos
, (uint64_t)numtowrite
, WRITE_LOCK
,
3938 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
3939 reply_doserror(req
, ERRDOS
, ERRlock
);
3940 END_PROFILE(SMBwrite
);
3945 * X/Open SMB protocol says that if smb_vwv1 is
3946 * zero then the file size should be extended or
3947 * truncated to the size given in smb_vwv[2-3].
3950 if(numtowrite
== 0) {
3952 * This is actually an allocate call, and set EOF. JRA.
3954 nwritten
= vfs_allocate_file_space(fsp
, (SMB_OFF_T
)startpos
);
3956 reply_nterror(req
, NT_STATUS_DISK_FULL
);
3959 nwritten
= vfs_set_filelen(fsp
, (SMB_OFF_T
)startpos
);
3961 reply_nterror(req
, NT_STATUS_DISK_FULL
);
3964 trigger_write_time_update_immediate(fsp
);
3966 nwritten
= write_file(req
,fsp
,data
,startpos
,numtowrite
);
3969 status
= sync_file(conn
, fsp
, False
);
3970 if (!NT_STATUS_IS_OK(status
)) {
3971 DEBUG(5,("reply_write: sync_file for %s returned %s\n",
3972 fsp
->fsp_name
, nt_errstr(status
) ));
3973 reply_nterror(req
, status
);
3977 if(((nwritten
== 0) && (numtowrite
!= 0))||(nwritten
< 0)) {
3978 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
3982 reply_outbuf(req
, 1, 0);
3984 SSVAL(req
->outbuf
,smb_vwv0
,nwritten
);
3986 if (nwritten
< (ssize_t
)numtowrite
) {
3987 SCVAL(req
->outbuf
,smb_rcls
,ERRHRD
);
3988 SSVAL(req
->outbuf
,smb_err
,ERRdiskfull
);
3991 DEBUG(3,("write fnum=%d num=%d wrote=%d\n", fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
3994 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
3996 END_PROFILE(SMBwrite
);
4000 /****************************************************************************
4001 Ensure a buffer is a valid writeX for recvfile purposes.
4002 ****************************************************************************/
4004 #define STANDARD_WRITE_AND_X_HEADER_SIZE (smb_size - 4 + /* basic header */ \
4005 (2*14) + /* word count (including bcc) */ \
4008 bool is_valid_writeX_buffer(const uint8_t *inbuf
)
4011 connection_struct
*conn
= NULL
;
4012 unsigned int doff
= 0;
4013 size_t len
= smb_len_large(inbuf
);
4015 if (is_encrypted_packet(inbuf
)) {
4016 /* Can't do this on encrypted
4021 if (CVAL(inbuf
,smb_com
) != SMBwriteX
) {
4025 if (CVAL(inbuf
,smb_vwv0
) != 0xFF ||
4026 CVAL(inbuf
,smb_wct
) != 14) {
4027 DEBUG(10,("is_valid_writeX_buffer: chained or "
4028 "invalid word length.\n"));
4032 conn
= conn_find(SVAL(inbuf
, smb_tid
));
4034 DEBUG(10,("is_valid_writeX_buffer: bad tid\n"));
4038 DEBUG(10,("is_valid_writeX_buffer: IPC$ tid\n"));
4041 if (IS_PRINT(conn
)) {
4042 DEBUG(10,("is_valid_writeX_buffer: printing tid\n"));
4045 doff
= SVAL(inbuf
,smb_vwv11
);
4047 numtowrite
= SVAL(inbuf
,smb_vwv10
);
4049 if (len
> doff
&& len
- doff
> 0xFFFF) {
4050 numtowrite
|= (((size_t)SVAL(inbuf
,smb_vwv9
))<<16);
4053 if (numtowrite
== 0) {
4054 DEBUG(10,("is_valid_writeX_buffer: zero write\n"));
4058 /* Ensure the sizes match up. */
4059 if (doff
< STANDARD_WRITE_AND_X_HEADER_SIZE
) {
4060 /* no pad byte...old smbclient :-( */
4061 DEBUG(10,("is_valid_writeX_buffer: small doff %u (min %u)\n",
4063 (unsigned int)STANDARD_WRITE_AND_X_HEADER_SIZE
));
4067 if (len
- doff
!= numtowrite
) {
4068 DEBUG(10,("is_valid_writeX_buffer: doff mismatch "
4069 "len = %u, doff = %u, numtowrite = %u\n",
4072 (unsigned int)numtowrite
));
4076 DEBUG(10,("is_valid_writeX_buffer: true "
4077 "len = %u, doff = %u, numtowrite = %u\n",
4080 (unsigned int)numtowrite
));
4085 /****************************************************************************
4086 Reply to a write and X.
4087 ****************************************************************************/
4089 void reply_write_and_X(struct smb_request
*req
)
4091 connection_struct
*conn
= req
->conn
;
4093 struct lock_struct lock
;
4098 unsigned int smb_doff
;
4099 unsigned int smblen
;
4103 START_PROFILE(SMBwriteX
);
4105 if ((req
->wct
!= 12) && (req
->wct
!= 14)) {
4106 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4107 END_PROFILE(SMBwriteX
);
4111 numtowrite
= SVAL(req
->vwv
+10, 0);
4112 smb_doff
= SVAL(req
->vwv
+11, 0);
4113 smblen
= smb_len(req
->inbuf
);
4115 if (req
->unread_bytes
> 0xFFFF ||
4116 (smblen
> smb_doff
&&
4117 smblen
- smb_doff
> 0xFFFF)) {
4118 numtowrite
|= (((size_t)SVAL(req
->vwv
+9, 0))<<16);
4121 if (req
->unread_bytes
) {
4122 /* Can't do a recvfile write on IPC$ */
4124 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4125 END_PROFILE(SMBwriteX
);
4128 if (numtowrite
!= req
->unread_bytes
) {
4129 reply_doserror(req
, ERRDOS
, ERRbadmem
);
4130 END_PROFILE(SMBwriteX
);
4134 if (smb_doff
> smblen
|| smb_doff
+ numtowrite
< numtowrite
||
4135 smb_doff
+ numtowrite
> smblen
) {
4136 reply_doserror(req
, ERRDOS
, ERRbadmem
);
4137 END_PROFILE(SMBwriteX
);
4142 /* If it's an IPC, pass off the pipe handler. */
4144 if (req
->unread_bytes
) {
4145 reply_doserror(req
, ERRDOS
, ERRbadmem
);
4146 END_PROFILE(SMBwriteX
);
4149 reply_pipe_write_and_X(req
);
4150 END_PROFILE(SMBwriteX
);
4154 fsp
= file_fsp(req
, SVAL(req
->vwv
+2, 0));
4155 startpos
= IVAL_TO_SMB_OFF_T(req
->vwv
+3, 0);
4156 write_through
= BITSETW(req
->vwv
+7,0);
4158 if (!check_fsp(conn
, req
, fsp
)) {
4159 END_PROFILE(SMBwriteX
);
4163 if (!CHECK_WRITE(fsp
)) {
4164 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
4165 END_PROFILE(SMBwriteX
);
4169 data
= smb_base(req
->inbuf
) + smb_doff
;
4171 if(req
->wct
== 14) {
4172 #ifdef LARGE_SMB_OFF_T
4174 * This is a large offset (64 bit) write.
4176 startpos
|= (((SMB_OFF_T
)IVAL(req
->vwv
+12, 0)) << 32);
4178 #else /* !LARGE_SMB_OFF_T */
4181 * Ensure we haven't been sent a >32 bit offset.
4184 if(IVAL(req
->vwv
+12, 0) != 0) {
4185 DEBUG(0,("reply_write_and_X - large offset (%x << 32) "
4186 "used and we don't support 64 bit offsets.\n",
4187 (unsigned int)IVAL(req
->vwv
+12, 0) ));
4188 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
4189 END_PROFILE(SMBwriteX
);
4193 #endif /* LARGE_SMB_OFF_T */
4196 init_strict_lock_struct(fsp
, (uint32
)req
->smbpid
,
4197 (uint64_t)startpos
, (uint64_t)numtowrite
, WRITE_LOCK
,
4200 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
4201 reply_doserror(req
, ERRDOS
, ERRlock
);
4202 END_PROFILE(SMBwriteX
);
4206 /* X/Open SMB protocol says that, unlike SMBwrite
4207 if the length is zero then NO truncation is
4208 done, just a write of zero. To truncate a file,
4211 if(numtowrite
== 0) {
4215 if ((req
->unread_bytes
== 0) &&
4216 schedule_aio_write_and_X(conn
, req
, fsp
, data
, startpos
,
4221 nwritten
= write_file(req
,fsp
,data
,startpos
,numtowrite
);
4224 if(((nwritten
== 0) && (numtowrite
!= 0))||(nwritten
< 0)) {
4225 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
4229 reply_outbuf(req
, 6, 0);
4230 SSVAL(req
->outbuf
,smb_vwv2
,nwritten
);
4231 SSVAL(req
->outbuf
,smb_vwv4
,nwritten
>>16);
4233 if (nwritten
< (ssize_t
)numtowrite
) {
4234 SCVAL(req
->outbuf
,smb_rcls
,ERRHRD
);
4235 SSVAL(req
->outbuf
,smb_err
,ERRdiskfull
);
4238 DEBUG(3,("writeX fnum=%d num=%d wrote=%d\n",
4239 fsp
->fnum
, (int)numtowrite
, (int)nwritten
));
4241 status
= sync_file(conn
, fsp
, write_through
);
4242 if (!NT_STATUS_IS_OK(status
)) {
4243 DEBUG(5,("reply_write_and_X: sync_file for %s returned %s\n",
4244 fsp
->fsp_name
, nt_errstr(status
) ));
4245 reply_nterror(req
, status
);
4249 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
4251 END_PROFILE(SMBwriteX
);
4256 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
4258 END_PROFILE(SMBwriteX
);
4262 /****************************************************************************
4264 ****************************************************************************/
4266 void reply_lseek(struct smb_request
*req
)
4268 connection_struct
*conn
= req
->conn
;
4274 START_PROFILE(SMBlseek
);
4277 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4278 END_PROFILE(SMBlseek
);
4282 fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
4284 if (!check_fsp(conn
, req
, fsp
)) {
4288 flush_write_cache(fsp
, SEEK_FLUSH
);
4290 mode
= SVAL(req
->vwv
+1, 0) & 3;
4291 /* NB. This doesn't use IVAL_TO_SMB_OFF_T as startpos can be signed in this case. */
4292 startpos
= (SMB_OFF_T
)IVALS(req
->vwv
+2, 0);
4301 res
= fsp
->fh
->pos
+ startpos
;
4312 if (umode
== SEEK_END
) {
4313 if((res
= SMB_VFS_LSEEK(fsp
,startpos
,umode
)) == -1) {
4314 if(errno
== EINVAL
) {
4315 SMB_OFF_T current_pos
= startpos
;
4316 SMB_STRUCT_STAT sbuf
;
4318 if(SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
4319 reply_unixerror(req
, ERRDOS
,
4321 END_PROFILE(SMBlseek
);
4325 current_pos
+= sbuf
.st_size
;
4327 res
= SMB_VFS_LSEEK(fsp
,0,SEEK_SET
);
4332 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
4333 END_PROFILE(SMBlseek
);
4340 reply_outbuf(req
, 2, 0);
4341 SIVAL(req
->outbuf
,smb_vwv0
,res
);
4343 DEBUG(3,("lseek fnum=%d ofs=%.0f newpos = %.0f mode=%d\n",
4344 fsp
->fnum
, (double)startpos
, (double)res
, mode
));
4346 END_PROFILE(SMBlseek
);
4350 /****************************************************************************
4352 ****************************************************************************/
4354 void reply_flush(struct smb_request
*req
)
4356 connection_struct
*conn
= req
->conn
;
4360 START_PROFILE(SMBflush
);
4363 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4367 fnum
= SVAL(req
->vwv
+0, 0);
4368 fsp
= file_fsp(req
, fnum
);
4370 if ((fnum
!= 0xFFFF) && !check_fsp(conn
, req
, fsp
)) {
4375 file_sync_all(conn
);
4377 NTSTATUS status
= sync_file(conn
, fsp
, True
);
4378 if (!NT_STATUS_IS_OK(status
)) {
4379 DEBUG(5,("reply_flush: sync_file for %s returned %s\n",
4380 fsp
->fsp_name
, nt_errstr(status
) ));
4381 reply_nterror(req
, status
);
4382 END_PROFILE(SMBflush
);
4387 reply_outbuf(req
, 0, 0);
4389 DEBUG(3,("flush\n"));
4390 END_PROFILE(SMBflush
);
4394 /****************************************************************************
4396 conn POINTER CAN BE NULL HERE !
4397 ****************************************************************************/
4399 void reply_exit(struct smb_request
*req
)
4401 START_PROFILE(SMBexit
);
4403 file_close_pid(req
->smbpid
, req
->vuid
);
4405 reply_outbuf(req
, 0, 0);
4407 DEBUG(3,("exit\n"));
4409 END_PROFILE(SMBexit
);
4413 /****************************************************************************
4414 Reply to a close - has to deal with closing a directory opened by NT SMB's.
4415 ****************************************************************************/
4417 void reply_close(struct smb_request
*req
)
4419 connection_struct
*conn
= req
->conn
;
4420 NTSTATUS status
= NT_STATUS_OK
;
4421 files_struct
*fsp
= NULL
;
4422 START_PROFILE(SMBclose
);
4425 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4426 END_PROFILE(SMBclose
);
4430 fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
4433 * We can only use check_fsp if we know it's not a directory.
4436 if(!fsp
|| (fsp
->conn
!= conn
) || (fsp
->vuid
!= req
->vuid
)) {
4437 reply_doserror(req
, ERRDOS
, ERRbadfid
);
4438 END_PROFILE(SMBclose
);
4442 if(fsp
->is_directory
) {
4444 * Special case - close NT SMB directory handle.
4446 DEBUG(3,("close directory fnum=%d\n", fsp
->fnum
));
4447 status
= close_file(req
, fsp
, NORMAL_CLOSE
);
4451 * Close ordinary file.
4454 DEBUG(3,("close fd=%d fnum=%d (numopen=%d)\n",
4455 fsp
->fh
->fd
, fsp
->fnum
,
4456 conn
->num_files_open
));
4459 * Take care of any time sent in the close.
4462 t
= srv_make_unix_date3(req
->vwv
+1);
4463 set_close_write_time(fsp
, convert_time_t_to_timespec(t
));
4466 * close_file() returns the unix errno if an error
4467 * was detected on close - normally this is due to
4468 * a disk full error. If not then it was probably an I/O error.
4471 status
= close_file(req
, fsp
, NORMAL_CLOSE
);
4474 if (!NT_STATUS_IS_OK(status
)) {
4475 reply_nterror(req
, status
);
4476 END_PROFILE(SMBclose
);
4480 reply_outbuf(req
, 0, 0);
4481 END_PROFILE(SMBclose
);
4485 /****************************************************************************
4486 Reply to a writeclose (Core+ protocol).
4487 ****************************************************************************/
4489 void reply_writeclose(struct smb_request
*req
)
4491 connection_struct
*conn
= req
->conn
;
4493 ssize_t nwritten
= -1;
4494 NTSTATUS close_status
= NT_STATUS_OK
;
4497 struct timespec mtime
;
4499 struct lock_struct lock
;
4501 START_PROFILE(SMBwriteclose
);
4504 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4505 END_PROFILE(SMBwriteclose
);
4509 fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
4511 if (!check_fsp(conn
, req
, fsp
)) {
4512 END_PROFILE(SMBwriteclose
);
4515 if (!CHECK_WRITE(fsp
)) {
4516 reply_doserror(req
, ERRDOS
,ERRbadaccess
);
4517 END_PROFILE(SMBwriteclose
);
4521 numtowrite
= SVAL(req
->vwv
+1, 0);
4522 startpos
= IVAL_TO_SMB_OFF_T(req
->vwv
+2, 0);
4523 mtime
= convert_time_t_to_timespec(srv_make_unix_date3(req
->vwv
+4));
4524 data
= (const char *)req
->buf
+ 1;
4527 init_strict_lock_struct(fsp
, (uint32
)req
->smbpid
,
4528 (uint64_t)startpos
, (uint64_t)numtowrite
, WRITE_LOCK
,
4531 if (!SMB_VFS_STRICT_LOCK(conn
, fsp
, &lock
)) {
4532 reply_doserror(req
, ERRDOS
,ERRlock
);
4533 END_PROFILE(SMBwriteclose
);
4538 nwritten
= write_file(req
,fsp
,data
,startpos
,numtowrite
);
4540 set_close_write_time(fsp
, mtime
);
4543 * More insanity. W2K only closes the file if writelen > 0.
4548 DEBUG(3,("reply_writeclose: zero length write doesn't close file %s\n",
4550 close_status
= close_file(req
, fsp
, NORMAL_CLOSE
);
4553 DEBUG(3,("writeclose fnum=%d num=%d wrote=%d (numopen=%d)\n",
4554 fsp
->fnum
, (int)numtowrite
, (int)nwritten
,
4555 conn
->num_files_open
));
4557 if(((nwritten
== 0) && (numtowrite
!= 0))||(nwritten
< 0)) {
4558 reply_doserror(req
, ERRHRD
, ERRdiskfull
);
4562 if(!NT_STATUS_IS_OK(close_status
)) {
4563 reply_nterror(req
, close_status
);
4567 reply_outbuf(req
, 1, 0);
4569 SSVAL(req
->outbuf
,smb_vwv0
,nwritten
);
4573 SMB_VFS_STRICT_UNLOCK(conn
, fsp
, &lock
);
4576 END_PROFILE(SMBwriteclose
);
4581 #define DBGC_CLASS DBGC_LOCKING
4583 /****************************************************************************
4585 ****************************************************************************/
4587 void reply_lock(struct smb_request
*req
)
4589 connection_struct
*conn
= req
->conn
;
4590 uint64_t count
,offset
;
4593 struct byte_range_lock
*br_lck
= NULL
;
4595 START_PROFILE(SMBlock
);
4598 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4599 END_PROFILE(SMBlock
);
4603 fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
4605 if (!check_fsp(conn
, req
, fsp
)) {
4606 END_PROFILE(SMBlock
);
4610 count
= (uint64_t)IVAL(req
->vwv
+1, 0);
4611 offset
= (uint64_t)IVAL(req
->vwv
+3, 0);
4613 DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
4614 fsp
->fh
->fd
, fsp
->fnum
, (double)offset
, (double)count
));
4616 br_lck
= do_lock(smbd_messaging_context(),
4623 False
, /* Non-blocking lock. */
4628 TALLOC_FREE(br_lck
);
4630 if (NT_STATUS_V(status
)) {
4631 reply_nterror(req
, status
);
4632 END_PROFILE(SMBlock
);
4636 reply_outbuf(req
, 0, 0);
4638 END_PROFILE(SMBlock
);
4642 /****************************************************************************
4644 ****************************************************************************/
4646 void reply_unlock(struct smb_request
*req
)
4648 connection_struct
*conn
= req
->conn
;
4649 uint64_t count
,offset
;
4653 START_PROFILE(SMBunlock
);
4656 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4657 END_PROFILE(SMBunlock
);
4661 fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
4663 if (!check_fsp(conn
, req
, fsp
)) {
4664 END_PROFILE(SMBunlock
);
4668 count
= (uint64_t)IVAL(req
->vwv
+1, 0);
4669 offset
= (uint64_t)IVAL(req
->vwv
+3, 0);
4671 status
= do_unlock(smbd_messaging_context(),
4678 if (NT_STATUS_V(status
)) {
4679 reply_nterror(req
, status
);
4680 END_PROFILE(SMBunlock
);
4684 DEBUG( 3, ( "unlock fd=%d fnum=%d offset=%.0f count=%.0f\n",
4685 fsp
->fh
->fd
, fsp
->fnum
, (double)offset
, (double)count
) );
4687 reply_outbuf(req
, 0, 0);
4689 END_PROFILE(SMBunlock
);
4694 #define DBGC_CLASS DBGC_ALL
4696 /****************************************************************************
4698 conn POINTER CAN BE NULL HERE !
4699 ****************************************************************************/
4701 void reply_tdis(struct smb_request
*req
)
4703 connection_struct
*conn
= req
->conn
;
4704 START_PROFILE(SMBtdis
);
4707 DEBUG(4,("Invalid connection in tdis\n"));
4708 reply_doserror(req
, ERRSRV
, ERRinvnid
);
4709 END_PROFILE(SMBtdis
);
4715 close_cnum(conn
,req
->vuid
);
4718 reply_outbuf(req
, 0, 0);
4719 END_PROFILE(SMBtdis
);
4723 /****************************************************************************
4725 conn POINTER CAN BE NULL HERE !
4726 ****************************************************************************/
4728 void reply_echo(struct smb_request
*req
)
4730 connection_struct
*conn
= req
->conn
;
4731 struct smb_perfcount_data local_pcd
;
4732 struct smb_perfcount_data
*cur_pcd
;
4736 START_PROFILE(SMBecho
);
4738 smb_init_perfcount_data(&local_pcd
);
4741 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4742 END_PROFILE(SMBecho
);
4746 smb_reverb
= SVAL(req
->vwv
+0, 0);
4748 reply_outbuf(req
, 1, req
->buflen
);
4750 /* copy any incoming data back out */
4751 if (req
->buflen
> 0) {
4752 memcpy(smb_buf(req
->outbuf
), req
->buf
, req
->buflen
);
4755 if (smb_reverb
> 100) {
4756 DEBUG(0,("large reverb (%d)?? Setting to 100\n",smb_reverb
));
4760 for (seq_num
= 1 ; seq_num
<= smb_reverb
; seq_num
++) {
4762 /* this makes sure we catch the request pcd */
4763 if (seq_num
== smb_reverb
) {
4764 cur_pcd
= &req
->pcd
;
4766 SMB_PERFCOUNT_COPY_CONTEXT(&req
->pcd
, &local_pcd
);
4767 cur_pcd
= &local_pcd
;
4770 SSVAL(req
->outbuf
,smb_vwv0
,seq_num
);
4772 show_msg((char *)req
->outbuf
);
4773 if (!srv_send_smb(smbd_server_fd(),
4774 (char *)req
->outbuf
,
4775 IS_CONN_ENCRYPTED(conn
)||req
->encrypted
,
4777 exit_server_cleanly("reply_echo: srv_send_smb failed.");
4780 DEBUG(3,("echo %d times\n", smb_reverb
));
4782 TALLOC_FREE(req
->outbuf
);
4784 END_PROFILE(SMBecho
);
4788 /****************************************************************************
4789 Reply to a printopen.
4790 ****************************************************************************/
4792 void reply_printopen(struct smb_request
*req
)
4794 connection_struct
*conn
= req
->conn
;
4796 SMB_STRUCT_STAT sbuf
;
4799 START_PROFILE(SMBsplopen
);
4802 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4803 END_PROFILE(SMBsplopen
);
4807 if (!CAN_PRINT(conn
)) {
4808 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
4809 END_PROFILE(SMBsplopen
);
4813 status
= file_new(req
, conn
, &fsp
);
4814 if(!NT_STATUS_IS_OK(status
)) {
4815 reply_nterror(req
, status
);
4816 END_PROFILE(SMBsplopen
);
4820 /* Open for exclusive use, write only. */
4821 status
= print_fsp_open(req
, conn
, NULL
, req
->vuid
, fsp
, &sbuf
);
4823 if (!NT_STATUS_IS_OK(status
)) {
4824 reply_nterror(req
, status
);
4825 END_PROFILE(SMBsplopen
);
4829 reply_outbuf(req
, 1, 0);
4830 SSVAL(req
->outbuf
,smb_vwv0
,fsp
->fnum
);
4832 DEBUG(3,("openprint fd=%d fnum=%d\n",
4833 fsp
->fh
->fd
, fsp
->fnum
));
4835 END_PROFILE(SMBsplopen
);
4839 /****************************************************************************
4840 Reply to a printclose.
4841 ****************************************************************************/
4843 void reply_printclose(struct smb_request
*req
)
4845 connection_struct
*conn
= req
->conn
;
4849 START_PROFILE(SMBsplclose
);
4852 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4853 END_PROFILE(SMBsplclose
);
4857 fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
4859 if (!check_fsp(conn
, req
, fsp
)) {
4860 END_PROFILE(SMBsplclose
);
4864 if (!CAN_PRINT(conn
)) {
4865 reply_nterror(req
, NT_STATUS_DOS(ERRSRV
, ERRerror
));
4866 END_PROFILE(SMBsplclose
);
4870 DEBUG(3,("printclose fd=%d fnum=%d\n",
4871 fsp
->fh
->fd
,fsp
->fnum
));
4873 status
= close_file(req
, fsp
, NORMAL_CLOSE
);
4875 if(!NT_STATUS_IS_OK(status
)) {
4876 reply_nterror(req
, status
);
4877 END_PROFILE(SMBsplclose
);
4881 reply_outbuf(req
, 0, 0);
4883 END_PROFILE(SMBsplclose
);
4887 /****************************************************************************
4888 Reply to a printqueue.
4889 ****************************************************************************/
4891 void reply_printqueue(struct smb_request
*req
)
4893 connection_struct
*conn
= req
->conn
;
4897 START_PROFILE(SMBsplretq
);
4900 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4901 END_PROFILE(SMBsplretq
);
4905 max_count
= SVAL(req
->vwv
+0, 0);
4906 start_index
= SVAL(req
->vwv
+1, 0);
4908 /* we used to allow the client to get the cnum wrong, but that
4909 is really quite gross and only worked when there was only
4910 one printer - I think we should now only accept it if they
4911 get it right (tridge) */
4912 if (!CAN_PRINT(conn
)) {
4913 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
4914 END_PROFILE(SMBsplretq
);
4918 reply_outbuf(req
, 2, 3);
4919 SSVAL(req
->outbuf
,smb_vwv0
,0);
4920 SSVAL(req
->outbuf
,smb_vwv1
,0);
4921 SCVAL(smb_buf(req
->outbuf
),0,1);
4922 SSVAL(smb_buf(req
->outbuf
),1,0);
4924 DEBUG(3,("printqueue start_index=%d max_count=%d\n",
4925 start_index
, max_count
));
4928 print_queue_struct
*queue
= NULL
;
4929 print_status_struct status
;
4930 int count
= print_queue_status(SNUM(conn
), &queue
, &status
);
4931 int num_to_get
= ABS(max_count
);
4932 int first
= (max_count
>0?start_index
:start_index
+max_count
+1);
4938 num_to_get
= MIN(num_to_get
,count
-first
);
4941 for (i
=first
;i
<first
+num_to_get
;i
++) {
4945 srv_put_dos_date2(p
,0,queue
[i
].time
);
4946 SCVAL(p
,4,(queue
[i
].status
==LPQ_PRINTING
?2:3));
4947 SSVAL(p
,5, queue
[i
].job
);
4948 SIVAL(p
,7,queue
[i
].size
);
4950 srvstr_push(blob
, req
->flags2
, p
+12,
4951 queue
[i
].fs_user
, 16, STR_ASCII
);
4953 if (message_push_blob(
4956 blob
, sizeof(blob
))) == -1) {
4957 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
4958 END_PROFILE(SMBsplretq
);
4964 SSVAL(req
->outbuf
,smb_vwv0
,count
);
4965 SSVAL(req
->outbuf
,smb_vwv1
,
4966 (max_count
>0?first
+count
:first
-1));
4967 SCVAL(smb_buf(req
->outbuf
),0,1);
4968 SSVAL(smb_buf(req
->outbuf
),1,28*count
);
4973 DEBUG(3,("%d entries returned in queue\n",count
));
4976 END_PROFILE(SMBsplretq
);
4980 /****************************************************************************
4981 Reply to a printwrite.
4982 ****************************************************************************/
4984 void reply_printwrite(struct smb_request
*req
)
4986 connection_struct
*conn
= req
->conn
;
4991 START_PROFILE(SMBsplwr
);
4994 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
4995 END_PROFILE(SMBsplwr
);
4999 fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
5001 if (!check_fsp(conn
, req
, fsp
)) {
5002 END_PROFILE(SMBsplwr
);
5006 if (!CAN_PRINT(conn
)) {
5007 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
5008 END_PROFILE(SMBsplwr
);
5012 if (!CHECK_WRITE(fsp
)) {
5013 reply_doserror(req
, ERRDOS
, ERRbadaccess
);
5014 END_PROFILE(SMBsplwr
);
5018 numtowrite
= SVAL(req
->buf
, 1);
5020 if (req
->buflen
< numtowrite
+ 3) {
5021 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
5022 END_PROFILE(SMBsplwr
);
5026 data
= (const char *)req
->buf
+ 3;
5028 if (write_file(req
,fsp
,data
,-1,numtowrite
) != numtowrite
) {
5029 reply_unixerror(req
, ERRHRD
, ERRdiskfull
);
5030 END_PROFILE(SMBsplwr
);
5034 DEBUG( 3, ( "printwrite fnum=%d num=%d\n", fsp
->fnum
, numtowrite
) );
5036 END_PROFILE(SMBsplwr
);
5040 /****************************************************************************
5042 ****************************************************************************/
5044 void reply_mkdir(struct smb_request
*req
)
5046 connection_struct
*conn
= req
->conn
;
5047 char *directory
= NULL
;
5049 SMB_STRUCT_STAT sbuf
;
5050 TALLOC_CTX
*ctx
= talloc_tos();
5052 START_PROFILE(SMBmkdir
);
5054 srvstr_get_path_req(ctx
, req
, &directory
, (const char *)req
->buf
+ 1,
5055 STR_TERMINATE
, &status
);
5056 if (!NT_STATUS_IS_OK(status
)) {
5057 reply_nterror(req
, status
);
5058 END_PROFILE(SMBmkdir
);
5062 status
= resolve_dfspath(ctx
, conn
,
5063 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
5066 if (!NT_STATUS_IS_OK(status
)) {
5067 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
5068 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
5069 ERRSRV
, ERRbadpath
);
5070 END_PROFILE(SMBmkdir
);
5073 reply_nterror(req
, status
);
5074 END_PROFILE(SMBmkdir
);
5078 status
= unix_convert(ctx
, conn
, directory
, False
, &directory
, NULL
, &sbuf
);
5079 if (!NT_STATUS_IS_OK(status
)) {
5080 reply_nterror(req
, status
);
5081 END_PROFILE(SMBmkdir
);
5085 status
= check_name(conn
, directory
);
5086 if (!NT_STATUS_IS_OK(status
)) {
5087 reply_nterror(req
, status
);
5088 END_PROFILE(SMBmkdir
);
5092 status
= create_directory(conn
, req
, directory
);
5094 DEBUG(5, ("create_directory returned %s\n", nt_errstr(status
)));
5096 if (!NT_STATUS_IS_OK(status
)) {
5098 if (!use_nt_status()
5099 && NT_STATUS_EQUAL(status
,
5100 NT_STATUS_OBJECT_NAME_COLLISION
)) {
5102 * Yes, in the DOS error code case we get a
5103 * ERRDOS:ERRnoaccess here. See BASE-SAMBA3ERROR
5104 * samba4 torture test.
5106 status
= NT_STATUS_DOS(ERRDOS
, ERRnoaccess
);
5109 reply_nterror(req
, status
);
5110 END_PROFILE(SMBmkdir
);
5114 reply_outbuf(req
, 0, 0);
5116 DEBUG( 3, ( "mkdir %s\n", directory
) );
5118 END_PROFILE(SMBmkdir
);
5122 /****************************************************************************
5123 Static function used by reply_rmdir to delete an entire directory
5124 tree recursively. Return True on ok, False on fail.
5125 ****************************************************************************/
5127 static bool recursive_rmdir(TALLOC_CTX
*ctx
,
5128 connection_struct
*conn
,
5131 const char *dname
= NULL
;
5135 struct smb_Dir
*dir_hnd
= OpenDir(talloc_tos(), conn
, directory
,
5141 while((dname
= ReadDirName(dir_hnd
, &offset
, &st
))) {
5142 char *fullname
= NULL
;
5144 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
5148 if (!is_visible_file(conn
, directory
, dname
, &st
, False
)) {
5152 /* Construct the full name. */
5153 fullname
= talloc_asprintf(ctx
,
5163 if(SMB_VFS_LSTAT(conn
,fullname
, &st
) != 0) {
5168 if(st
.st_mode
& S_IFDIR
) {
5169 if(!recursive_rmdir(ctx
, conn
, fullname
)) {
5173 if(SMB_VFS_RMDIR(conn
,fullname
) != 0) {
5177 } else if(SMB_VFS_UNLINK(conn
,fullname
) != 0) {
5181 TALLOC_FREE(fullname
);
5183 TALLOC_FREE(dir_hnd
);
5187 /****************************************************************************
5188 The internals of the rmdir code - called elsewhere.
5189 ****************************************************************************/
5191 NTSTATUS
rmdir_internals(TALLOC_CTX
*ctx
,
5192 connection_struct
*conn
,
5193 const char *directory
)
5198 /* Might be a symlink. */
5199 if(SMB_VFS_LSTAT(conn
, directory
, &st
) != 0) {
5200 return map_nt_error_from_unix(errno
);
5203 if (S_ISLNK(st
.st_mode
)) {
5204 /* Is what it points to a directory ? */
5205 if(SMB_VFS_STAT(conn
, directory
, &st
) != 0) {
5206 return map_nt_error_from_unix(errno
);
5208 if (!(S_ISDIR(st
.st_mode
))) {
5209 return NT_STATUS_NOT_A_DIRECTORY
;
5211 ret
= SMB_VFS_UNLINK(conn
,directory
);
5213 ret
= SMB_VFS_RMDIR(conn
,directory
);
5216 notify_fname(conn
, NOTIFY_ACTION_REMOVED
,
5217 FILE_NOTIFY_CHANGE_DIR_NAME
,
5219 return NT_STATUS_OK
;
5222 if(((errno
== ENOTEMPTY
)||(errno
== EEXIST
)) && lp_veto_files(SNUM(conn
))) {
5224 * Check to see if the only thing in this directory are
5225 * vetoed files/directories. If so then delete them and
5226 * retry. If we fail to delete any of them (and we *don't*
5227 * do a recursive delete) then fail the rmdir.
5231 struct smb_Dir
*dir_hnd
= OpenDir(talloc_tos(), conn
,
5232 directory
, NULL
, 0);
5234 if(dir_hnd
== NULL
) {
5239 while ((dname
= ReadDirName(dir_hnd
, &dirpos
, &st
))) {
5240 if((strcmp(dname
, ".") == 0) || (strcmp(dname
, "..")==0))
5242 if (!is_visible_file(conn
, directory
, dname
, &st
, False
))
5244 if(!IS_VETO_PATH(conn
, dname
)) {
5245 TALLOC_FREE(dir_hnd
);
5251 /* We only have veto files/directories.
5252 * Are we allowed to delete them ? */
5254 if(!lp_recursive_veto_delete(SNUM(conn
))) {
5255 TALLOC_FREE(dir_hnd
);
5260 /* Do a recursive delete. */
5261 RewindDir(dir_hnd
,&dirpos
);
5262 while ((dname
= ReadDirName(dir_hnd
, &dirpos
, &st
))) {
5263 char *fullname
= NULL
;
5265 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
5268 if (!is_visible_file(conn
, directory
, dname
, &st
, False
)) {
5272 fullname
= talloc_asprintf(ctx
,
5282 if(SMB_VFS_LSTAT(conn
,fullname
, &st
) != 0) {
5285 if(st
.st_mode
& S_IFDIR
) {
5286 if(!recursive_rmdir(ctx
, conn
, fullname
)) {
5289 if(SMB_VFS_RMDIR(conn
,fullname
) != 0) {
5292 } else if(SMB_VFS_UNLINK(conn
,fullname
) != 0) {
5295 TALLOC_FREE(fullname
);
5297 TALLOC_FREE(dir_hnd
);
5298 /* Retry the rmdir */
5299 ret
= SMB_VFS_RMDIR(conn
,directory
);
5305 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
5306 "%s\n", directory
,strerror(errno
)));
5307 return map_nt_error_from_unix(errno
);
5310 notify_fname(conn
, NOTIFY_ACTION_REMOVED
,
5311 FILE_NOTIFY_CHANGE_DIR_NAME
,
5314 return NT_STATUS_OK
;
5317 /****************************************************************************
5319 ****************************************************************************/
5321 void reply_rmdir(struct smb_request
*req
)
5323 connection_struct
*conn
= req
->conn
;
5324 char *directory
= NULL
;
5325 SMB_STRUCT_STAT sbuf
;
5327 TALLOC_CTX
*ctx
= talloc_tos();
5329 START_PROFILE(SMBrmdir
);
5331 srvstr_get_path_req(ctx
, req
, &directory
, (const char *)req
->buf
+ 1,
5332 STR_TERMINATE
, &status
);
5333 if (!NT_STATUS_IS_OK(status
)) {
5334 reply_nterror(req
, status
);
5335 END_PROFILE(SMBrmdir
);
5339 status
= resolve_dfspath(ctx
, conn
,
5340 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
5343 if (!NT_STATUS_IS_OK(status
)) {
5344 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
5345 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
5346 ERRSRV
, ERRbadpath
);
5347 END_PROFILE(SMBrmdir
);
5350 reply_nterror(req
, status
);
5351 END_PROFILE(SMBrmdir
);
5355 status
= unix_convert(ctx
, conn
, directory
, False
, &directory
,
5357 if (!NT_STATUS_IS_OK(status
)) {
5358 reply_nterror(req
, status
);
5359 END_PROFILE(SMBrmdir
);
5363 status
= check_name(conn
, directory
);
5364 if (!NT_STATUS_IS_OK(status
)) {
5365 reply_nterror(req
, status
);
5366 END_PROFILE(SMBrmdir
);
5370 dptr_closepath(directory
, req
->smbpid
);
5371 status
= rmdir_internals(ctx
, conn
, directory
);
5372 if (!NT_STATUS_IS_OK(status
)) {
5373 reply_nterror(req
, status
);
5374 END_PROFILE(SMBrmdir
);
5378 reply_outbuf(req
, 0, 0);
5380 DEBUG( 3, ( "rmdir %s\n", directory
) );
5382 END_PROFILE(SMBrmdir
);
5386 /*******************************************************************
5387 Resolve wildcards in a filename rename.
5388 ********************************************************************/
5390 static bool resolve_wildcards(TALLOC_CTX
*ctx
,
5395 char *name2_copy
= NULL
;
5400 char *p
,*p2
, *pname1
, *pname2
;
5402 name2_copy
= talloc_strdup(ctx
, name2
);
5407 pname1
= strrchr_m(name1
,'/');
5408 pname2
= strrchr_m(name2_copy
,'/');
5410 if (!pname1
|| !pname2
) {
5414 /* Truncate the copy of name2 at the last '/' */
5417 /* Now go past the '/' */
5421 root1
= talloc_strdup(ctx
, pname1
);
5422 root2
= talloc_strdup(ctx
, pname2
);
5424 if (!root1
|| !root2
) {
5428 p
= strrchr_m(root1
,'.');
5431 ext1
= talloc_strdup(ctx
, p
+1);
5433 ext1
= talloc_strdup(ctx
, "");
5435 p
= strrchr_m(root2
,'.');
5438 ext2
= talloc_strdup(ctx
, p
+1);
5440 ext2
= talloc_strdup(ctx
, "");
5443 if (!ext1
|| !ext2
) {
5451 /* Hmmm. Should this be mb-aware ? */
5454 } else if (*p2
== '*') {
5456 root2
= talloc_asprintf(ctx
, "%s%s",
5475 /* Hmmm. Should this be mb-aware ? */
5478 } else if (*p2
== '*') {
5480 ext2
= talloc_asprintf(ctx
, "%s%s",
5496 *pp_newname
= talloc_asprintf(ctx
, "%s/%s.%s",
5501 *pp_newname
= talloc_asprintf(ctx
, "%s/%s",
5513 /****************************************************************************
5514 Ensure open files have their names updated. Updated to notify other smbd's
5516 ****************************************************************************/
5518 static void rename_open_files(connection_struct
*conn
,
5519 struct share_mode_lock
*lck
,
5520 const char *newname
)
5523 bool did_rename
= False
;
5525 for(fsp
= file_find_di_first(lck
->id
); fsp
;
5526 fsp
= file_find_di_next(fsp
)) {
5527 /* fsp_name is a relative path under the fsp. To change this for other
5528 sharepaths we need to manipulate relative paths. */
5529 /* TODO - create the absolute path and manipulate the newname
5530 relative to the sharepath. */
5531 if (!strequal(fsp
->conn
->connectpath
, conn
->connectpath
)) {
5534 DEBUG(10,("rename_open_files: renaming file fnum %d (file_id %s) from %s -> %s\n",
5535 fsp
->fnum
, file_id_string_tos(&fsp
->file_id
),
5536 fsp
->fsp_name
, newname
));
5537 string_set(&fsp
->fsp_name
, newname
);
5542 DEBUG(10,("rename_open_files: no open files on file_id %s for %s\n",
5543 file_id_string_tos(&lck
->id
), newname
));
5546 /* Send messages to all smbd's (not ourself) that the name has changed. */
5547 rename_share_filename(smbd_messaging_context(), lck
, conn
->connectpath
,
5551 /****************************************************************************
5552 We need to check if the source path is a parent directory of the destination
5553 (ie. a rename of /foo/bar/baz -> /foo/bar/baz/bibble/bobble. If so we must
5554 refuse the rename with a sharing violation. Under UNIX the above call can
5555 *succeed* if /foo/bar/baz is a symlink to another area in the share. We
5556 probably need to check that the client is a Windows one before disallowing
5557 this as a UNIX client (one with UNIX extensions) can know the source is a
5558 symlink and make this decision intelligently. Found by an excellent bug
5559 report from <AndyLiebman@aol.com>.
5560 ****************************************************************************/
5562 static bool rename_path_prefix_equal(const char *src
, const char *dest
)
5564 const char *psrc
= src
;
5565 const char *pdst
= dest
;
5568 if (psrc
[0] == '.' && psrc
[1] == '/') {
5571 if (pdst
[0] == '.' && pdst
[1] == '/') {
5574 if ((slen
= strlen(psrc
)) > strlen(pdst
)) {
5577 return ((memcmp(psrc
, pdst
, slen
) == 0) && pdst
[slen
] == '/');
5581 * Do the notify calls from a rename
5584 static void notify_rename(connection_struct
*conn
, bool is_dir
,
5585 const char *oldpath
, const char *newpath
)
5587 char *olddir
, *newdir
;
5588 const char *oldname
, *newname
;
5591 mask
= is_dir
? FILE_NOTIFY_CHANGE_DIR_NAME
5592 : FILE_NOTIFY_CHANGE_FILE_NAME
;
5594 if (!parent_dirname(talloc_tos(), oldpath
, &olddir
, &oldname
)
5595 || !parent_dirname(talloc_tos(), newpath
, &newdir
, &newname
)) {
5596 TALLOC_FREE(olddir
);
5600 if (strcmp(olddir
, newdir
) == 0) {
5601 notify_fname(conn
, NOTIFY_ACTION_OLD_NAME
, mask
, oldpath
);
5602 notify_fname(conn
, NOTIFY_ACTION_NEW_NAME
, mask
, newpath
);
5605 notify_fname(conn
, NOTIFY_ACTION_REMOVED
, mask
, oldpath
);
5606 notify_fname(conn
, NOTIFY_ACTION_ADDED
, mask
, newpath
);
5608 TALLOC_FREE(olddir
);
5609 TALLOC_FREE(newdir
);
5611 /* this is a strange one. w2k3 gives an additional event for
5612 CHANGE_ATTRIBUTES and CHANGE_CREATION on the new file when renaming
5613 files, but not directories */
5615 notify_fname(conn
, NOTIFY_ACTION_MODIFIED
,
5616 FILE_NOTIFY_CHANGE_ATTRIBUTES
5617 |FILE_NOTIFY_CHANGE_CREATION
,
5622 /****************************************************************************
5623 Rename an open file - given an fsp.
5624 ****************************************************************************/
5626 NTSTATUS
rename_internals_fsp(connection_struct
*conn
,
5629 const char *newname_last_component
,
5631 bool replace_if_exists
)
5633 TALLOC_CTX
*ctx
= talloc_tos();
5634 SMB_STRUCT_STAT sbuf
, sbuf1
;
5635 NTSTATUS status
= NT_STATUS_OK
;
5636 struct share_mode_lock
*lck
= NULL
;
5637 bool dst_exists
, old_is_stream
, new_is_stream
;
5641 status
= check_name(conn
, newname
);
5642 if (!NT_STATUS_IS_OK(status
)) {
5646 /* Ensure newname contains a '/' */
5647 if(strrchr_m(newname
,'/') == 0) {
5648 newname
= talloc_asprintf(ctx
,
5652 return NT_STATUS_NO_MEMORY
;
5657 * Check for special case with case preserving and not
5658 * case sensitive. If the old last component differs from the original
5659 * last component only by case, then we should allow
5660 * the rename (user is trying to change the case of the
5664 if((conn
->case_sensitive
== False
) && (conn
->case_preserve
== True
) &&
5665 strequal(newname
, fsp
->fsp_name
)) {
5667 char *newname_modified_last_component
= NULL
;
5670 * Get the last component of the modified name.
5671 * Note that we guarantee that newname contains a '/'
5674 p
= strrchr_m(newname
,'/');
5675 newname_modified_last_component
= talloc_strdup(ctx
,
5677 if (!newname_modified_last_component
) {
5678 return NT_STATUS_NO_MEMORY
;
5681 if(strcsequal(newname_modified_last_component
,
5682 newname_last_component
) == False
) {
5684 * Replace the modified last component with
5687 *p
= '\0'; /* Truncate at the '/' */
5688 newname
= talloc_asprintf(ctx
,
5691 newname_last_component
);
5696 * If the src and dest names are identical - including case,
5697 * don't do the rename, just return success.
5700 if (strcsequal(fsp
->fsp_name
, newname
)) {
5701 DEBUG(3,("rename_internals_fsp: identical names in rename %s - returning success\n",
5703 return NT_STATUS_OK
;
5706 old_is_stream
= is_ntfs_stream_name(fsp
->fsp_name
);
5707 new_is_stream
= is_ntfs_stream_name(newname
);
5709 /* Return the correct error code if both names aren't streams. */
5710 if (!old_is_stream
&& new_is_stream
) {
5711 return NT_STATUS_OBJECT_NAME_INVALID
;
5714 if (old_is_stream
&& !new_is_stream
) {
5715 return NT_STATUS_INVALID_PARAMETER
;
5719 * Have vfs_object_exist also fill sbuf1
5721 dst_exists
= vfs_object_exist(conn
, newname
, &sbuf1
);
5723 if(!replace_if_exists
&& dst_exists
) {
5724 DEBUG(3,("rename_internals_fsp: dest exists doing rename %s -> %s\n",
5725 fsp
->fsp_name
,newname
));
5726 return NT_STATUS_OBJECT_NAME_COLLISION
;
5730 struct file_id fileid
= vfs_file_id_from_sbuf(conn
, &sbuf1
);
5731 files_struct
*dst_fsp
= file_find_di_first(fileid
);
5732 /* The file can be open when renaming a stream */
5733 if (dst_fsp
&& !new_is_stream
) {
5734 DEBUG(3, ("rename_internals_fsp: Target file open\n"));
5735 return NT_STATUS_ACCESS_DENIED
;
5739 /* Ensure we have a valid stat struct for the source. */
5740 if (fsp
->fh
->fd
!= -1) {
5741 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
5742 return map_nt_error_from_unix(errno
);
5746 if (fsp
->posix_open
) {
5747 ret
= SMB_VFS_LSTAT(conn
,fsp
->fsp_name
,&sbuf
);
5749 ret
= SMB_VFS_STAT(conn
,fsp
->fsp_name
,&sbuf
);
5752 return map_nt_error_from_unix(errno
);
5756 status
= can_rename(conn
, fsp
, attrs
, &sbuf
);
5758 if (!NT_STATUS_IS_OK(status
)) {
5759 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
5760 nt_errstr(status
), fsp
->fsp_name
,newname
));
5761 if (NT_STATUS_EQUAL(status
,NT_STATUS_SHARING_VIOLATION
))
5762 status
= NT_STATUS_ACCESS_DENIED
;
5766 if (rename_path_prefix_equal(fsp
->fsp_name
, newname
)) {
5767 return NT_STATUS_ACCESS_DENIED
;
5770 lck
= get_share_mode_lock(talloc_tos(), fsp
->file_id
, NULL
, NULL
,
5774 * We have the file open ourselves, so not being able to get the
5775 * corresponding share mode lock is a fatal error.
5778 SMB_ASSERT(lck
!= NULL
);
5780 if(SMB_VFS_RENAME(conn
,fsp
->fsp_name
, newname
) == 0) {
5781 uint32 create_options
= fsp
->fh
->private_options
;
5783 DEBUG(3,("rename_internals_fsp: succeeded doing rename on %s -> %s\n",
5784 fsp
->fsp_name
,newname
));
5786 notify_rename(conn
, fsp
->is_directory
, fsp
->fsp_name
, newname
);
5788 rename_open_files(conn
, lck
, newname
);
5791 * A rename acts as a new file create w.r.t. allowing an initial delete
5792 * on close, probably because in Windows there is a new handle to the
5793 * new file. If initial delete on close was requested but not
5794 * originally set, we need to set it here. This is probably not 100% correct,
5795 * but will work for the CIFSFS client which in non-posix mode
5796 * depends on these semantics. JRA.
5799 if (create_options
& FILE_DELETE_ON_CLOSE
) {
5800 status
= can_set_delete_on_close(fsp
, True
, 0);
5802 if (NT_STATUS_IS_OK(status
)) {
5803 /* Note that here we set the *inital* delete on close flag,
5804 * not the regular one. The magic gets handled in close. */
5805 fsp
->initial_delete_on_close
= True
;
5809 return NT_STATUS_OK
;
5814 if (errno
== ENOTDIR
|| errno
== EISDIR
) {
5815 status
= NT_STATUS_OBJECT_NAME_COLLISION
;
5817 status
= map_nt_error_from_unix(errno
);
5820 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
5821 nt_errstr(status
), fsp
->fsp_name
,newname
));
5826 /****************************************************************************
5827 The guts of the rename command, split out so it may be called by the NT SMB
5829 ****************************************************************************/
5831 NTSTATUS
rename_internals(TALLOC_CTX
*ctx
,
5832 connection_struct
*conn
,
5833 struct smb_request
*req
,
5834 const char *name_in
,
5835 const char *newname_in
,
5837 bool replace_if_exists
,
5840 uint32_t access_mask
)
5842 char *directory
= NULL
;
5844 char *last_component_src
= NULL
;
5845 char *last_component_dest
= NULL
;
5847 char *newname
= NULL
;
5850 NTSTATUS status
= NT_STATUS_OK
;
5851 SMB_STRUCT_STAT sbuf1
, sbuf2
;
5852 struct smb_Dir
*dir_hnd
= NULL
;
5855 int create_options
= 0;
5856 bool posix_pathnames
= lp_posix_pathnames();
5861 status
= unix_convert(ctx
, conn
, name_in
, src_has_wild
, &name
,
5862 &last_component_src
, &sbuf1
);
5863 if (!NT_STATUS_IS_OK(status
)) {
5867 status
= unix_convert(ctx
, conn
, newname_in
, dest_has_wild
, &newname
,
5868 &last_component_dest
, &sbuf2
);
5869 if (!NT_STATUS_IS_OK(status
)) {
5874 * Split the old name into directory and last component
5875 * strings. Note that unix_convert may have stripped off a
5876 * leading ./ from both name and newname if the rename is
5877 * at the root of the share. We need to make sure either both
5878 * name and newname contain a / character or neither of them do
5879 * as this is checked in resolve_wildcards().
5882 p
= strrchr_m(name
,'/');
5884 directory
= talloc_strdup(ctx
, ".");
5886 return NT_STATUS_NO_MEMORY
;
5891 directory
= talloc_strdup(ctx
, name
);
5893 return NT_STATUS_NO_MEMORY
;
5896 *p
= '/'; /* Replace needed for exceptional test below. */
5900 * We should only check the mangled cache
5901 * here if unix_convert failed. This means
5902 * that the path in 'mask' doesn't exist
5903 * on the file system and so we need to look
5904 * for a possible mangle. This patch from
5905 * Tine Smukavec <valentin.smukavec@hermes.si>.
5908 if (!VALID_STAT(sbuf1
) && mangle_is_mangled(mask
, conn
->params
)) {
5909 char *new_mask
= NULL
;
5910 mangle_lookup_name_from_8_3(ctx
,
5919 if (!src_has_wild
) {
5923 * No wildcards - just process the one file.
5925 /* Add a terminating '/' to the directory name. */
5926 directory
= talloc_asprintf_append(directory
,
5930 return NT_STATUS_NO_MEMORY
;
5933 /* Ensure newname contains a '/' also */
5934 if(strrchr_m(newname
,'/') == 0) {
5935 newname
= talloc_asprintf(ctx
,
5939 return NT_STATUS_NO_MEMORY
;
5943 DEBUG(3, ("rename_internals: case_sensitive = %d, "
5944 "case_preserve = %d, short case preserve = %d, "
5945 "directory = %s, newname = %s, "
5946 "last_component_dest = %s\n",
5947 conn
->case_sensitive
, conn
->case_preserve
,
5948 conn
->short_case_preserve
, directory
,
5949 newname
, last_component_dest
));
5951 /* The dest name still may have wildcards. */
5952 if (dest_has_wild
) {
5953 char *mod_newname
= NULL
;
5954 if (!resolve_wildcards(ctx
,
5955 directory
,newname
,&mod_newname
)) {
5956 DEBUG(6, ("rename_internals: resolve_wildcards "
5960 return NT_STATUS_NO_MEMORY
;
5962 newname
= mod_newname
;
5966 if (posix_pathnames
) {
5967 SMB_VFS_LSTAT(conn
, directory
, &sbuf1
);
5969 SMB_VFS_STAT(conn
, directory
, &sbuf1
);
5972 if (S_ISDIR(sbuf1
.st_mode
)) {
5973 create_options
|= FILE_DIRECTORY_FILE
;
5976 status
= SMB_VFS_CREATE_FILE(
5979 0, /* root_dir_fid */
5980 directory
, /* fname */
5981 0, /* create_file_flags */
5982 access_mask
, /* access_mask */
5983 (FILE_SHARE_READ
| /* share_access */
5985 FILE_OPEN
, /* create_disposition*/
5986 create_options
, /* create_options */
5987 posix_pathnames
? FILE_FLAG_POSIX_SEMANTICS
|0777 : 0, /* file_attributes */
5988 0, /* oplock_request */
5989 0, /* allocation_size */
5994 &sbuf1
); /* psbuf */
5996 if (!NT_STATUS_IS_OK(status
)) {
5997 DEBUG(3, ("Could not open rename source %s: %s\n",
5998 directory
, nt_errstr(status
)));
6002 status
= rename_internals_fsp(conn
, fsp
, newname
,
6003 last_component_dest
,
6004 attrs
, replace_if_exists
);
6006 close_file(req
, fsp
, NORMAL_CLOSE
);
6008 DEBUG(3, ("rename_internals: Error %s rename %s -> %s\n",
6009 nt_errstr(status
), directory
,newname
));
6015 * Wildcards - process each file that matches.
6017 if (strequal(mask
,"????????.???")) {
6022 status
= check_name(conn
, directory
);
6023 if (!NT_STATUS_IS_OK(status
)) {
6027 dir_hnd
= OpenDir(talloc_tos(), conn
, directory
, mask
, attrs
);
6028 if (dir_hnd
== NULL
) {
6029 return map_nt_error_from_unix(errno
);
6032 status
= NT_STATUS_NO_SUCH_FILE
;
6034 * Was status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
6035 * - gentest fix. JRA
6038 while ((dname
= ReadDirName(dir_hnd
, &offset
, &sbuf1
))) {
6039 files_struct
*fsp
= NULL
;
6041 char *destname
= NULL
;
6042 bool sysdir_entry
= False
;
6044 /* Quick check for "." and ".." */
6045 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
6047 sysdir_entry
= True
;
6053 if (!is_visible_file(conn
, directory
, dname
, &sbuf1
, False
)) {
6057 if(!mask_match(dname
, mask
, conn
->case_sensitive
)) {
6062 status
= NT_STATUS_OBJECT_NAME_INVALID
;
6066 fname
= talloc_asprintf(ctx
,
6071 return NT_STATUS_NO_MEMORY
;
6074 if (!resolve_wildcards(ctx
,
6075 fname
,newname
,&destname
)) {
6076 DEBUG(6, ("resolve_wildcards %s %s failed\n",
6082 return NT_STATUS_NO_MEMORY
;
6086 if (posix_pathnames
) {
6087 SMB_VFS_LSTAT(conn
, fname
, &sbuf1
);
6089 SMB_VFS_STAT(conn
, fname
, &sbuf1
);
6094 if (S_ISDIR(sbuf1
.st_mode
)) {
6095 create_options
|= FILE_DIRECTORY_FILE
;
6098 status
= SMB_VFS_CREATE_FILE(
6101 0, /* root_dir_fid */
6103 0, /* create_file_flags */
6104 access_mask
, /* access_mask */
6105 (FILE_SHARE_READ
| /* share_access */
6107 FILE_OPEN
, /* create_disposition*/
6108 create_options
, /* create_options */
6109 posix_pathnames
? FILE_FLAG_POSIX_SEMANTICS
|0777 : 0, /* file_attributes */
6110 0, /* oplock_request */
6111 0, /* allocation_size */
6116 &sbuf1
); /* psbuf */
6118 if (!NT_STATUS_IS_OK(status
)) {
6119 DEBUG(3,("rename_internals: SMB_VFS_CREATE_FILE "
6120 "returned %s rename %s -> %s\n",
6121 nt_errstr(status
), directory
, newname
));
6125 status
= rename_internals_fsp(conn
, fsp
, destname
, dname
,
6126 attrs
, replace_if_exists
);
6128 close_file(req
, fsp
, NORMAL_CLOSE
);
6130 if (!NT_STATUS_IS_OK(status
)) {
6131 DEBUG(3, ("rename_internals_fsp returned %s for "
6132 "rename %s -> %s\n", nt_errstr(status
),
6133 directory
, newname
));
6139 DEBUG(3,("rename_internals: doing rename on %s -> "
6140 "%s\n",fname
,destname
));
6143 TALLOC_FREE(destname
);
6145 TALLOC_FREE(dir_hnd
);
6147 if (count
== 0 && NT_STATUS_IS_OK(status
) && errno
!= 0) {
6148 status
= map_nt_error_from_unix(errno
);
6154 /****************************************************************************
6156 ****************************************************************************/
6158 void reply_mv(struct smb_request
*req
)
6160 connection_struct
*conn
= req
->conn
;
6162 char *newname
= NULL
;
6166 bool src_has_wcard
= False
;
6167 bool dest_has_wcard
= False
;
6168 TALLOC_CTX
*ctx
= talloc_tos();
6170 START_PROFILE(SMBmv
);
6173 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
6178 attrs
= SVAL(req
->vwv
+0, 0);
6180 p
= (const char *)req
->buf
+ 1;
6181 p
+= srvstr_get_path_req_wcard(ctx
, req
, &name
, p
, STR_TERMINATE
,
6182 &status
, &src_has_wcard
);
6183 if (!NT_STATUS_IS_OK(status
)) {
6184 reply_nterror(req
, status
);
6189 p
+= srvstr_get_path_req_wcard(ctx
, req
, &newname
, p
, STR_TERMINATE
,
6190 &status
, &dest_has_wcard
);
6191 if (!NT_STATUS_IS_OK(status
)) {
6192 reply_nterror(req
, status
);
6197 status
= resolve_dfspath_wcard(ctx
, conn
,
6198 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
6202 if (!NT_STATUS_IS_OK(status
)) {
6203 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
6204 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
6205 ERRSRV
, ERRbadpath
);
6209 reply_nterror(req
, status
);
6214 status
= resolve_dfspath_wcard(ctx
, conn
,
6215 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
6219 if (!NT_STATUS_IS_OK(status
)) {
6220 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
6221 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
6222 ERRSRV
, ERRbadpath
);
6226 reply_nterror(req
, status
);
6231 DEBUG(3,("reply_mv : %s -> %s\n",name
,newname
));
6233 status
= rename_internals(ctx
, conn
, req
, name
, newname
, attrs
, False
,
6234 src_has_wcard
, dest_has_wcard
, DELETE_ACCESS
);
6235 if (!NT_STATUS_IS_OK(status
)) {
6236 if (open_was_deferred(req
->mid
)) {
6237 /* We have re-scheduled this call. */
6241 reply_nterror(req
, status
);
6246 reply_outbuf(req
, 0, 0);
6252 /*******************************************************************
6253 Copy a file as part of a reply_copy.
6254 ******************************************************************/
6257 * TODO: check error codes on all callers
6260 NTSTATUS
copy_file(TALLOC_CTX
*ctx
,
6261 connection_struct
*conn
,
6266 bool target_is_directory
)
6268 SMB_STRUCT_STAT src_sbuf
, sbuf2
;
6270 files_struct
*fsp1
,*fsp2
;
6273 uint32 new_create_disposition
;
6276 dest
= talloc_strdup(ctx
, dest1
);
6278 return NT_STATUS_NO_MEMORY
;
6280 if (target_is_directory
) {
6281 const char *p
= strrchr_m(src
,'/');
6287 dest
= talloc_asprintf_append(dest
,
6291 return NT_STATUS_NO_MEMORY
;
6295 if (!vfs_file_exist(conn
,src
,&src_sbuf
)) {
6297 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
6300 if (!target_is_directory
&& count
) {
6301 new_create_disposition
= FILE_OPEN
;
6303 if (!map_open_params_to_ntcreate(dest1
,0,ofun
,
6304 NULL
, NULL
, &new_create_disposition
, NULL
)) {
6306 return NT_STATUS_INVALID_PARAMETER
;
6310 status
= SMB_VFS_CREATE_FILE(
6313 0, /* root_dir_fid */
6315 0, /* create_file_flags */
6316 FILE_GENERIC_READ
, /* access_mask */
6317 FILE_SHARE_READ
| FILE_SHARE_WRITE
, /* share_access */
6318 FILE_OPEN
, /* create_disposition*/
6319 0, /* create_options */
6320 FILE_ATTRIBUTE_NORMAL
, /* file_attributes */
6321 INTERNAL_OPEN_ONLY
, /* oplock_request */
6322 0, /* allocation_size */
6327 &src_sbuf
); /* psbuf */
6329 if (!NT_STATUS_IS_OK(status
)) {
6334 dosattrs
= dos_mode(conn
, src
, &src_sbuf
);
6335 if (SMB_VFS_STAT(conn
,dest
,&sbuf2
) == -1) {
6336 ZERO_STRUCTP(&sbuf2
);
6339 status
= SMB_VFS_CREATE_FILE(
6342 0, /* root_dir_fid */
6344 0, /* create_file_flags */
6345 FILE_GENERIC_WRITE
, /* access_mask */
6346 FILE_SHARE_READ
| FILE_SHARE_WRITE
, /* share_access */
6347 new_create_disposition
, /* create_disposition*/
6348 0, /* create_options */
6349 dosattrs
, /* file_attributes */
6350 INTERNAL_OPEN_ONLY
, /* oplock_request */
6351 0, /* allocation_size */
6356 &sbuf2
); /* psbuf */
6360 if (!NT_STATUS_IS_OK(status
)) {
6361 close_file(NULL
, fsp1
, ERROR_CLOSE
);
6365 if ((ofun
&3) == 1) {
6366 if(SMB_VFS_LSEEK(fsp2
,0,SEEK_END
) == -1) {
6367 DEBUG(0,("copy_file: error - vfs lseek returned error %s\n", strerror(errno
) ));
6369 * Stop the copy from occurring.
6372 src_sbuf
.st_size
= 0;
6376 if (src_sbuf
.st_size
) {
6377 ret
= vfs_transfer_file(fsp1
, fsp2
, src_sbuf
.st_size
);
6380 close_file(NULL
, fsp1
, NORMAL_CLOSE
);
6382 /* Ensure the modtime is set correctly on the destination file. */
6383 set_close_write_time(fsp2
, get_mtimespec(&src_sbuf
));
6386 * As we are opening fsp1 read-only we only expect
6387 * an error on close on fsp2 if we are out of space.
6388 * Thus we don't look at the error return from the
6391 status
= close_file(NULL
, fsp2
, NORMAL_CLOSE
);
6393 if (!NT_STATUS_IS_OK(status
)) {
6397 if (ret
!= (SMB_OFF_T
)src_sbuf
.st_size
) {
6398 return NT_STATUS_DISK_FULL
;
6401 return NT_STATUS_OK
;
6404 /****************************************************************************
6405 Reply to a file copy.
6406 ****************************************************************************/
6408 void reply_copy(struct smb_request
*req
)
6410 connection_struct
*conn
= req
->conn
;
6412 char *newname
= NULL
;
6413 char *directory
= NULL
;
6414 const char *mask
= NULL
;
6415 const char mask_star
[] = "*";
6418 int error
= ERRnoaccess
;
6423 bool target_is_directory
=False
;
6424 bool source_has_wild
= False
;
6425 bool dest_has_wild
= False
;
6426 SMB_STRUCT_STAT sbuf1
, sbuf2
;
6428 TALLOC_CTX
*ctx
= talloc_tos();
6430 START_PROFILE(SMBcopy
);
6433 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
6434 END_PROFILE(SMBcopy
);
6438 tid2
= SVAL(req
->vwv
+0, 0);
6439 ofun
= SVAL(req
->vwv
+1, 0);
6440 flags
= SVAL(req
->vwv
+2, 0);
6442 p
= (const char *)req
->buf
;
6443 p
+= srvstr_get_path_req_wcard(ctx
, req
, &name
, p
, STR_TERMINATE
,
6444 &status
, &source_has_wild
);
6445 if (!NT_STATUS_IS_OK(status
)) {
6446 reply_nterror(req
, status
);
6447 END_PROFILE(SMBcopy
);
6450 p
+= srvstr_get_path_req_wcard(ctx
, req
, &newname
, p
, STR_TERMINATE
,
6451 &status
, &dest_has_wild
);
6452 if (!NT_STATUS_IS_OK(status
)) {
6453 reply_nterror(req
, status
);
6454 END_PROFILE(SMBcopy
);
6458 DEBUG(3,("reply_copy : %s -> %s\n",name
,newname
));
6460 if (tid2
!= conn
->cnum
) {
6461 /* can't currently handle inter share copies XXXX */
6462 DEBUG(3,("Rejecting inter-share copy\n"));
6463 reply_doserror(req
, ERRSRV
, ERRinvdevice
);
6464 END_PROFILE(SMBcopy
);
6468 status
= resolve_dfspath_wcard(ctx
, conn
,
6469 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
6473 if (!NT_STATUS_IS_OK(status
)) {
6474 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
6475 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
6476 ERRSRV
, ERRbadpath
);
6477 END_PROFILE(SMBcopy
);
6480 reply_nterror(req
, status
);
6481 END_PROFILE(SMBcopy
);
6485 status
= resolve_dfspath_wcard(ctx
, conn
,
6486 req
->flags2
& FLAGS2_DFS_PATHNAMES
,
6490 if (!NT_STATUS_IS_OK(status
)) {
6491 if (NT_STATUS_EQUAL(status
,NT_STATUS_PATH_NOT_COVERED
)) {
6492 reply_botherror(req
, NT_STATUS_PATH_NOT_COVERED
,
6493 ERRSRV
, ERRbadpath
);
6494 END_PROFILE(SMBcopy
);
6497 reply_nterror(req
, status
);
6498 END_PROFILE(SMBcopy
);
6502 status
= unix_convert(ctx
, conn
, name
, source_has_wild
,
6503 &name
, NULL
, &sbuf1
);
6504 if (!NT_STATUS_IS_OK(status
)) {
6505 reply_nterror(req
, status
);
6506 END_PROFILE(SMBcopy
);
6510 status
= unix_convert(ctx
, conn
, newname
, dest_has_wild
,
6511 &newname
, NULL
, &sbuf2
);
6512 if (!NT_STATUS_IS_OK(status
)) {
6513 reply_nterror(req
, status
);
6514 END_PROFILE(SMBcopy
);
6518 target_is_directory
= VALID_STAT_OF_DIR(sbuf2
);
6520 if ((flags
&1) && target_is_directory
) {
6521 reply_doserror(req
, ERRDOS
, ERRbadfile
);
6522 END_PROFILE(SMBcopy
);
6526 if ((flags
&2) && !target_is_directory
) {
6527 reply_doserror(req
, ERRDOS
, ERRbadpath
);
6528 END_PROFILE(SMBcopy
);
6532 if ((flags
&(1<<5)) && VALID_STAT_OF_DIR(sbuf1
)) {
6533 /* wants a tree copy! XXXX */
6534 DEBUG(3,("Rejecting tree copy\n"));
6535 reply_doserror(req
, ERRSRV
, ERRerror
);
6536 END_PROFILE(SMBcopy
);
6540 p
= strrchr_m(name
,'/');
6542 directory
= talloc_strndup(ctx
, name
, PTR_DIFF(p
, name
));
6545 directory
= talloc_strdup(ctx
, "./");
6550 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
6551 END_PROFILE(SMBcopy
);
6556 * We should only check the mangled cache
6557 * here if unix_convert failed. This means
6558 * that the path in 'mask' doesn't exist
6559 * on the file system and so we need to look
6560 * for a possible mangle. This patch from
6561 * Tine Smukavec <valentin.smukavec@hermes.si>.
6564 if (!VALID_STAT(sbuf1
) && mangle_is_mangled(mask
, conn
->params
)) {
6565 char *new_mask
= NULL
;
6566 mangle_lookup_name_from_8_3(ctx
,
6575 if (!source_has_wild
) {
6576 directory
= talloc_asprintf_append(directory
,
6579 if (dest_has_wild
) {
6580 char *mod_newname
= NULL
;
6581 if (!resolve_wildcards(ctx
,
6582 directory
,newname
,&mod_newname
)) {
6583 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
6584 END_PROFILE(SMBcopy
);
6587 newname
= mod_newname
;
6590 status
= check_name(conn
, directory
);
6591 if (!NT_STATUS_IS_OK(status
)) {
6592 reply_nterror(req
, status
);
6593 END_PROFILE(SMBcopy
);
6597 status
= check_name(conn
, newname
);
6598 if (!NT_STATUS_IS_OK(status
)) {
6599 reply_nterror(req
, status
);
6600 END_PROFILE(SMBcopy
);
6604 status
= copy_file(ctx
,conn
,directory
,newname
,ofun
,
6605 count
,target_is_directory
);
6607 if(!NT_STATUS_IS_OK(status
)) {
6608 reply_nterror(req
, status
);
6609 END_PROFILE(SMBcopy
);
6615 struct smb_Dir
*dir_hnd
= NULL
;
6616 const char *dname
= NULL
;
6619 if (strequal(mask
,"????????.???")) {
6623 status
= check_name(conn
, directory
);
6624 if (!NT_STATUS_IS_OK(status
)) {
6625 reply_nterror(req
, status
);
6626 END_PROFILE(SMBcopy
);
6630 dir_hnd
= OpenDir(talloc_tos(), conn
, directory
, mask
, 0);
6631 if (dir_hnd
== NULL
) {
6632 status
= map_nt_error_from_unix(errno
);
6633 reply_nterror(req
, status
);
6634 END_PROFILE(SMBcopy
);
6640 while ((dname
= ReadDirName(dir_hnd
, &offset
, &sbuf1
))) {
6641 char *destname
= NULL
;
6644 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
6648 if (!is_visible_file(conn
, directory
, dname
, &sbuf1
, False
)) {
6652 if(!mask_match(dname
, mask
, conn
->case_sensitive
)) {
6656 error
= ERRnoaccess
;
6657 fname
= talloc_asprintf(ctx
,
6662 TALLOC_FREE(dir_hnd
);
6663 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
6664 END_PROFILE(SMBcopy
);
6668 if (!resolve_wildcards(ctx
,
6669 fname
,newname
,&destname
)) {
6673 TALLOC_FREE(dir_hnd
);
6674 reply_nterror(req
, NT_STATUS_NO_MEMORY
);
6675 END_PROFILE(SMBcopy
);
6679 status
= check_name(conn
, fname
);
6680 if (!NT_STATUS_IS_OK(status
)) {
6681 TALLOC_FREE(dir_hnd
);
6682 reply_nterror(req
, status
);
6683 END_PROFILE(SMBcopy
);
6687 status
= check_name(conn
, destname
);
6688 if (!NT_STATUS_IS_OK(status
)) {
6689 TALLOC_FREE(dir_hnd
);
6690 reply_nterror(req
, status
);
6691 END_PROFILE(SMBcopy
);
6695 DEBUG(3,("reply_copy : doing copy on %s -> %s\n",fname
, destname
));
6697 status
= copy_file(ctx
,conn
,fname
,destname
,ofun
,
6698 count
,target_is_directory
);
6699 if (NT_STATUS_IS_OK(status
)) {
6703 TALLOC_FREE(destname
);
6705 TALLOC_FREE(dir_hnd
);
6710 /* Error on close... */
6712 reply_unixerror(req
, ERRHRD
, ERRgeneral
);
6713 END_PROFILE(SMBcopy
);
6717 reply_doserror(req
, ERRDOS
, error
);
6718 END_PROFILE(SMBcopy
);
6722 reply_outbuf(req
, 1, 0);
6723 SSVAL(req
->outbuf
,smb_vwv0
,count
);
6725 END_PROFILE(SMBcopy
);
6730 #define DBGC_CLASS DBGC_LOCKING
6732 /****************************************************************************
6733 Get a lock pid, dealing with large count requests.
6734 ****************************************************************************/
6736 uint32
get_lock_pid(const uint8_t *data
, int data_offset
,
6737 bool large_file_format
)
6739 if(!large_file_format
)
6740 return (uint32
)SVAL(data
,SMB_LPID_OFFSET(data_offset
));
6742 return (uint32
)SVAL(data
,SMB_LARGE_LPID_OFFSET(data_offset
));
6745 /****************************************************************************
6746 Get a lock count, dealing with large count requests.
6747 ****************************************************************************/
6749 uint64_t get_lock_count(const uint8_t *data
, int data_offset
,
6750 bool large_file_format
)
6754 if(!large_file_format
) {
6755 count
= (uint64_t)IVAL(data
,SMB_LKLEN_OFFSET(data_offset
));
6758 #if defined(HAVE_LONGLONG)
6759 count
= (((uint64_t) IVAL(data
,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset
))) << 32) |
6760 ((uint64_t) IVAL(data
,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset
)));
6761 #else /* HAVE_LONGLONG */
6764 * NT4.x seems to be broken in that it sends large file (64 bit)
6765 * lockingX calls even if the CAP_LARGE_FILES was *not*
6766 * negotiated. For boxes without large unsigned ints truncate the
6767 * lock count by dropping the top 32 bits.
6770 if(IVAL(data
,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset
)) != 0) {
6771 DEBUG(3,("get_lock_count: truncating lock count (high)0x%x (low)0x%x to just low count.\n",
6772 (unsigned int)IVAL(data
,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset
)),
6773 (unsigned int)IVAL(data
,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset
)) ));
6774 SIVAL(data
,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset
),0);
6777 count
= (uint64_t)IVAL(data
,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset
));
6778 #endif /* HAVE_LONGLONG */
6784 #if !defined(HAVE_LONGLONG)
6785 /****************************************************************************
6786 Pathetically try and map a 64 bit lock offset into 31 bits. I hate Windows :-).
6787 ****************************************************************************/
6789 static uint32
map_lock_offset(uint32 high
, uint32 low
)
6793 uint32 highcopy
= high
;
6796 * Try and find out how many significant bits there are in high.
6799 for(i
= 0; highcopy
; i
++)
6803 * We use 31 bits not 32 here as POSIX
6804 * lock offsets may not be negative.
6807 mask
= (~0) << (31 - i
);
6810 return 0; /* Fail. */
6816 #endif /* !defined(HAVE_LONGLONG) */
6818 /****************************************************************************
6819 Get a lock offset, dealing with large offset requests.
6820 ****************************************************************************/
6822 uint64_t get_lock_offset(const uint8_t *data
, int data_offset
,
6823 bool large_file_format
, bool *err
)
6825 uint64_t offset
= 0;
6829 if(!large_file_format
) {
6830 offset
= (uint64_t)IVAL(data
,SMB_LKOFF_OFFSET(data_offset
));
6833 #if defined(HAVE_LONGLONG)
6834 offset
= (((uint64_t) IVAL(data
,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset
))) << 32) |
6835 ((uint64_t) IVAL(data
,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset
)));
6836 #else /* HAVE_LONGLONG */
6839 * NT4.x seems to be broken in that it sends large file (64 bit)
6840 * lockingX calls even if the CAP_LARGE_FILES was *not*
6841 * negotiated. For boxes without large unsigned ints mangle the
6842 * lock offset by mapping the top 32 bits onto the lower 32.
6845 if(IVAL(data
,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset
)) != 0) {
6846 uint32 low
= IVAL(data
,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset
));
6847 uint32 high
= IVAL(data
,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset
));
6850 if((new_low
= map_lock_offset(high
, low
)) == 0) {
6852 return (uint64_t)-1;
6855 DEBUG(3,("get_lock_offset: truncating lock offset (high)0x%x (low)0x%x to offset 0x%x.\n",
6856 (unsigned int)high
, (unsigned int)low
, (unsigned int)new_low
));
6857 SIVAL(data
,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset
),0);
6858 SIVAL(data
,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset
),new_low
);
6861 offset
= (uint64_t)IVAL(data
,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset
));
6862 #endif /* HAVE_LONGLONG */
6868 /****************************************************************************
6869 Reply to a lockingX request.
6870 ****************************************************************************/
6872 void reply_lockingX(struct smb_request
*req
)
6874 connection_struct
*conn
= req
->conn
;
6876 unsigned char locktype
;
6877 unsigned char oplocklevel
;
6880 uint64_t count
= 0, offset
= 0;
6884 const uint8_t *data
;
6885 bool large_file_format
;
6887 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
6889 START_PROFILE(SMBlockingX
);
6892 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
6893 END_PROFILE(SMBlockingX
);
6897 fsp
= file_fsp(req
, SVAL(req
->vwv
+2, 0));
6898 locktype
= CVAL(req
->vwv
+3, 0);
6899 oplocklevel
= CVAL(req
->vwv
+3, 1);
6900 num_ulocks
= SVAL(req
->vwv
+6, 0);
6901 num_locks
= SVAL(req
->vwv
+7, 0);
6902 lock_timeout
= IVAL(req
->vwv
+4, 0);
6903 large_file_format
= (locktype
& LOCKING_ANDX_LARGE_FILES
)?True
:False
;
6905 if (!check_fsp(conn
, req
, fsp
)) {
6906 END_PROFILE(SMBlockingX
);
6912 if (locktype
& LOCKING_ANDX_CHANGE_LOCKTYPE
) {
6913 /* we don't support these - and CANCEL_LOCK makes w2k
6914 and XP reboot so I don't really want to be
6915 compatible! (tridge) */
6916 reply_nterror(req
, NT_STATUS_DOS(ERRDOS
, ERRnoatomiclocks
));
6917 END_PROFILE(SMBlockingX
);
6921 /* Check if this is an oplock break on a file
6922 we have granted an oplock on.
6924 if ((locktype
& LOCKING_ANDX_OPLOCK_RELEASE
)) {
6925 /* Client can insist on breaking to none. */
6926 bool break_to_none
= (oplocklevel
== 0);
6929 DEBUG(5,("reply_lockingX: oplock break reply (%u) from client "
6930 "for fnum = %d\n", (unsigned int)oplocklevel
,
6934 * Make sure we have granted an exclusive or batch oplock on
6938 if (fsp
->oplock_type
== 0) {
6940 /* The Samba4 nbench simulator doesn't understand
6941 the difference between break to level2 and break
6942 to none from level2 - it sends oplock break
6943 replies in both cases. Don't keep logging an error
6944 message here - just ignore it. JRA. */
6946 DEBUG(5,("reply_lockingX: Error : oplock break from "
6947 "client for fnum = %d (oplock=%d) and no "
6948 "oplock granted on this file (%s).\n",
6949 fsp
->fnum
, fsp
->oplock_type
, fsp
->fsp_name
));
6951 /* if this is a pure oplock break request then don't
6953 if (num_locks
== 0 && num_ulocks
== 0) {
6954 END_PROFILE(SMBlockingX
);
6957 END_PROFILE(SMBlockingX
);
6958 reply_doserror(req
, ERRDOS
, ERRlock
);
6963 if ((fsp
->sent_oplock_break
== BREAK_TO_NONE_SENT
) ||
6965 result
= remove_oplock(fsp
);
6967 result
= downgrade_oplock(fsp
);
6971 DEBUG(0, ("reply_lockingX: error in removing "
6972 "oplock on file %s\n", fsp
->fsp_name
));
6973 /* Hmmm. Is this panic justified? */
6974 smb_panic("internal tdb error");
6977 reply_to_oplock_break_requests(fsp
);
6979 /* if this is a pure oplock break request then don't send a
6981 if (num_locks
== 0 && num_ulocks
== 0) {
6982 /* Sanity check - ensure a pure oplock break is not a
6984 if(CVAL(req
->vwv
+0, 0) != 0xff)
6985 DEBUG(0,("reply_lockingX: Error : pure oplock "
6986 "break is a chained %d request !\n",
6987 (unsigned int)CVAL(req
->vwv
+0, 0)));
6988 END_PROFILE(SMBlockingX
);
6994 (num_ulocks
+ num_locks
) * (large_file_format
? 20 : 10)) {
6995 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
6996 END_PROFILE(SMBlockingX
);
7000 /* Data now points at the beginning of the list
7001 of smb_unlkrng structs */
7002 for(i
= 0; i
< (int)num_ulocks
; i
++) {
7003 lock_pid
= get_lock_pid( data
, i
, large_file_format
);
7004 count
= get_lock_count( data
, i
, large_file_format
);
7005 offset
= get_lock_offset( data
, i
, large_file_format
, &err
);
7008 * There is no error code marked "stupid client bug".... :-).
7011 END_PROFILE(SMBlockingX
);
7012 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
7016 DEBUG(10,("reply_lockingX: unlock start=%.0f, len=%.0f for "
7017 "pid %u, file %s\n", (double)offset
, (double)count
,
7018 (unsigned int)lock_pid
, fsp
->fsp_name
));
7020 status
= do_unlock(smbd_messaging_context(),
7027 DEBUG(10, ("reply_lockingX: unlock returned %s\n",
7028 nt_errstr(status
)));
7030 if (NT_STATUS_V(status
)) {
7031 END_PROFILE(SMBlockingX
);
7032 reply_nterror(req
, status
);
7037 /* Setup the timeout in seconds. */
7039 if (!lp_blocking_locks(SNUM(conn
))) {
7043 /* Now do any requested locks */
7044 data
+= ((large_file_format
? 20 : 10)*num_ulocks
);
7046 /* Data now points at the beginning of the list
7047 of smb_lkrng structs */
7049 for(i
= 0; i
< (int)num_locks
; i
++) {
7050 enum brl_type lock_type
= ((locktype
& LOCKING_ANDX_SHARED_LOCK
) ?
7051 READ_LOCK
:WRITE_LOCK
);
7052 lock_pid
= get_lock_pid( data
, i
, large_file_format
);
7053 count
= get_lock_count( data
, i
, large_file_format
);
7054 offset
= get_lock_offset( data
, i
, large_file_format
, &err
);
7057 * There is no error code marked "stupid client bug".... :-).
7060 END_PROFILE(SMBlockingX
);
7061 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
7065 DEBUG(10,("reply_lockingX: lock start=%.0f, len=%.0f for pid "
7066 "%u, file %s timeout = %d\n", (double)offset
,
7067 (double)count
, (unsigned int)lock_pid
,
7068 fsp
->fsp_name
, (int)lock_timeout
));
7070 if (locktype
& LOCKING_ANDX_CANCEL_LOCK
) {
7071 struct blocking_lock_record
*blr
= NULL
;
7073 if (lp_blocking_locks(SNUM(conn
))) {
7075 /* Schedule a message to ourselves to
7076 remove the blocking lock record and
7077 return the right error. */
7079 blr
= blocking_lock_cancel(fsp
,
7085 NT_STATUS_FILE_LOCK_CONFLICT
);
7087 END_PROFILE(SMBlockingX
);
7092 ERRcancelviolation
));
7096 /* Remove a matching pending lock. */
7097 status
= do_lock_cancel(fsp
,
7104 bool blocking_lock
= lock_timeout
? True
: False
;
7105 bool defer_lock
= False
;
7106 struct byte_range_lock
*br_lck
;
7107 uint32 block_smbpid
;
7109 br_lck
= do_lock(smbd_messaging_context(),
7121 if (br_lck
&& blocking_lock
&& ERROR_WAS_LOCK_DENIED(status
)) {
7122 /* Windows internal resolution for blocking locks seems
7123 to be about 200ms... Don't wait for less than that. JRA. */
7124 if (lock_timeout
!= -1 && lock_timeout
< lp_lock_spin_time()) {
7125 lock_timeout
= lp_lock_spin_time();
7130 /* This heuristic seems to match W2K3 very well. If a
7131 lock sent with timeout of zero would fail with NT_STATUS_FILE_LOCK_CONFLICT
7132 it pretends we asked for a timeout of between 150 - 300 milliseconds as
7133 far as I can tell. Replacement for do_lock_spin(). JRA. */
7135 if (br_lck
&& lp_blocking_locks(SNUM(conn
)) && !blocking_lock
&&
7136 NT_STATUS_EQUAL((status
), NT_STATUS_FILE_LOCK_CONFLICT
)) {
7138 lock_timeout
= lp_lock_spin_time();
7141 if (br_lck
&& defer_lock
) {
7143 * A blocking lock was requested. Package up
7144 * this smb into a queued request and push it
7145 * onto the blocking lock queue.
7147 if(push_blocking_lock_request(br_lck
,
7158 TALLOC_FREE(br_lck
);
7159 END_PROFILE(SMBlockingX
);
7164 TALLOC_FREE(br_lck
);
7167 if (NT_STATUS_V(status
)) {
7172 /* If any of the above locks failed, then we must unlock
7173 all of the previous locks (X/Open spec). */
7174 if (num_locks
!= 0 && !NT_STATUS_IS_OK(status
)) {
7176 if (locktype
& LOCKING_ANDX_CANCEL_LOCK
) {
7177 i
= -1; /* we want to skip the for loop */
7181 * Ensure we don't do a remove on the lock that just failed,
7182 * as under POSIX rules, if we have a lock already there, we
7183 * will delete it (and we shouldn't) .....
7185 for(i
--; i
>= 0; i
--) {
7186 lock_pid
= get_lock_pid( data
, i
, large_file_format
);
7187 count
= get_lock_count( data
, i
, large_file_format
);
7188 offset
= get_lock_offset( data
, i
, large_file_format
,
7192 * There is no error code marked "stupid client
7196 END_PROFILE(SMBlockingX
);
7197 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
7201 do_unlock(smbd_messaging_context(),
7208 END_PROFILE(SMBlockingX
);
7209 reply_nterror(req
, status
);
7213 reply_outbuf(req
, 2, 0);
7215 DEBUG(3, ("lockingX fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
7216 fsp
->fnum
, (unsigned int)locktype
, num_locks
, num_ulocks
));
7218 END_PROFILE(SMBlockingX
);
7223 #define DBGC_CLASS DBGC_ALL
7225 /****************************************************************************
7226 Reply to a SMBreadbmpx (read block multiplex) request.
7227 Always reply with an error, if someone has a platform really needs this,
7228 please contact vl@samba.org
7229 ****************************************************************************/
7231 void reply_readbmpx(struct smb_request
*req
)
7233 START_PROFILE(SMBreadBmpx
);
7234 reply_doserror(req
, ERRSRV
, ERRuseSTD
);
7235 END_PROFILE(SMBreadBmpx
);
7239 /****************************************************************************
7240 Reply to a SMBreadbs (read block multiplex secondary) request.
7241 Always reply with an error, if someone has a platform really needs this,
7242 please contact vl@samba.org
7243 ****************************************************************************/
7245 void reply_readbs(struct smb_request
*req
)
7247 START_PROFILE(SMBreadBs
);
7248 reply_doserror(req
, ERRSRV
, ERRuseSTD
);
7249 END_PROFILE(SMBreadBs
);
7253 /****************************************************************************
7254 Reply to a SMBsetattrE.
7255 ****************************************************************************/
7257 void reply_setattrE(struct smb_request
*req
)
7259 connection_struct
*conn
= req
->conn
;
7260 struct smb_file_time ft
;
7262 SMB_STRUCT_STAT sbuf
;
7265 START_PROFILE(SMBsetattrE
);
7269 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
7270 END_PROFILE(SMBsetattrE
);
7274 fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
7276 if(!fsp
|| (fsp
->conn
!= conn
)) {
7277 reply_doserror(req
, ERRDOS
, ERRbadfid
);
7278 END_PROFILE(SMBsetattrE
);
7284 * Convert the DOS times into unix times.
7287 ft
.atime
= convert_time_t_to_timespec(
7288 srv_make_unix_date2(req
->vwv
+3));
7289 ft
.mtime
= convert_time_t_to_timespec(
7290 srv_make_unix_date2(req
->vwv
+5));
7291 ft
.create_time
= convert_time_t_to_timespec(
7292 srv_make_unix_date2(req
->vwv
+1));
7294 reply_outbuf(req
, 0, 0);
7297 * Patch from Ray Frush <frush@engr.colostate.edu>
7298 * Sometimes times are sent as zero - ignore them.
7301 /* Ensure we have a valid stat struct for the source. */
7302 if (fsp
->fh
->fd
!= -1) {
7303 if (SMB_VFS_FSTAT(fsp
, &sbuf
) == -1) {
7304 status
= map_nt_error_from_unix(errno
);
7305 reply_nterror(req
, status
);
7306 END_PROFILE(SMBsetattrE
);
7312 if (fsp
->posix_open
) {
7313 ret
= SMB_VFS_LSTAT(conn
, fsp
->fsp_name
, &sbuf
);
7315 ret
= SMB_VFS_STAT(conn
, fsp
->fsp_name
, &sbuf
);
7318 status
= map_nt_error_from_unix(errno
);
7319 reply_nterror(req
, status
);
7320 END_PROFILE(SMBsetattrE
);
7325 status
= smb_set_file_time(conn
, fsp
, fsp
->fsp_name
,
7327 if (!NT_STATUS_IS_OK(status
)) {
7328 reply_doserror(req
, ERRDOS
, ERRnoaccess
);
7329 END_PROFILE(SMBsetattrE
);
7333 DEBUG( 3, ( "reply_setattrE fnum=%d actime=%u modtime=%u "
7336 (unsigned int)ft
.atime
.tv_sec
,
7337 (unsigned int)ft
.mtime
.tv_sec
,
7338 (unsigned int)ft
.create_time
.tv_sec
7341 END_PROFILE(SMBsetattrE
);
7346 /* Back from the dead for OS/2..... JRA. */
7348 /****************************************************************************
7349 Reply to a SMBwritebmpx (write block multiplex primary) request.
7350 Always reply with an error, if someone has a platform really needs this,
7351 please contact vl@samba.org
7352 ****************************************************************************/
7354 void reply_writebmpx(struct smb_request
*req
)
7356 START_PROFILE(SMBwriteBmpx
);
7357 reply_doserror(req
, ERRSRV
, ERRuseSTD
);
7358 END_PROFILE(SMBwriteBmpx
);
7362 /****************************************************************************
7363 Reply to a SMBwritebs (write block multiplex secondary) request.
7364 Always reply with an error, if someone has a platform really needs this,
7365 please contact vl@samba.org
7366 ****************************************************************************/
7368 void reply_writebs(struct smb_request
*req
)
7370 START_PROFILE(SMBwriteBs
);
7371 reply_doserror(req
, ERRSRV
, ERRuseSTD
);
7372 END_PROFILE(SMBwriteBs
);
7376 /****************************************************************************
7377 Reply to a SMBgetattrE.
7378 ****************************************************************************/
7380 void reply_getattrE(struct smb_request
*req
)
7382 connection_struct
*conn
= req
->conn
;
7383 SMB_STRUCT_STAT sbuf
;
7386 struct timespec create_ts
;
7388 START_PROFILE(SMBgetattrE
);
7391 reply_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
7392 END_PROFILE(SMBgetattrE
);
7396 fsp
= file_fsp(req
, SVAL(req
->vwv
+0, 0));
7398 if(!fsp
|| (fsp
->conn
!= conn
)) {
7399 reply_doserror(req
, ERRDOS
, ERRbadfid
);
7400 END_PROFILE(SMBgetattrE
);
7404 /* Do an fstat on this file */
7405 if(fsp_stat(fsp
, &sbuf
)) {
7406 reply_unixerror(req
, ERRDOS
, ERRnoaccess
);
7407 END_PROFILE(SMBgetattrE
);
7411 mode
= dos_mode(conn
,fsp
->fsp_name
,&sbuf
);
7414 * Convert the times into dos times. Set create
7415 * date to be last modify date as UNIX doesn't save
7419 reply_outbuf(req
, 11, 0);
7421 create_ts
= get_create_timespec(&sbuf
,
7422 lp_fake_dir_create_times(SNUM(conn
)));
7423 srv_put_dos_date2((char *)req
->outbuf
, smb_vwv0
, create_ts
.tv_sec
);
7424 srv_put_dos_date2((char *)req
->outbuf
, smb_vwv2
, sbuf
.st_atime
);
7425 /* Should we check pending modtime here ? JRA */
7426 srv_put_dos_date2((char *)req
->outbuf
, smb_vwv4
, sbuf
.st_mtime
);
7429 SIVAL(req
->outbuf
, smb_vwv6
, 0);
7430 SIVAL(req
->outbuf
, smb_vwv8
, 0);
7432 uint32 allocation_size
= SMB_VFS_GET_ALLOC_SIZE(conn
,fsp
, &sbuf
);
7433 SIVAL(req
->outbuf
, smb_vwv6
, (uint32
)sbuf
.st_size
);
7434 SIVAL(req
->outbuf
, smb_vwv8
, allocation_size
);
7436 SSVAL(req
->outbuf
,smb_vwv10
, mode
);
7438 DEBUG( 3, ( "reply_getattrE fnum=%d\n", fsp
->fnum
));
7440 END_PROFILE(SMBgetattrE
);