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 /****************************************************************************
31 Ensure we check the path in *exactly* the same way as W2K for a findfirst/findnext
32 path or anything including wildcards.
33 We're assuming here that '/' is not the second byte in any multibyte char
34 set (a safe assumption). '\\' *may* be the second byte in a multibyte char
36 ****************************************************************************/
38 /* Custom version for processing POSIX paths. */
39 #define IS_PATH_SEP(c,posix_only) ((c) == '/' || (!(posix_only) && (c) == '\\'))
41 static NTSTATUS check_path_syntax_internal(char *path,
43 bool *p_last_component_contains_wcard)
47 NTSTATUS ret = NT_STATUS_OK;
48 bool start_of_name_component = True;
49 bool stream_started = false;
51 *p_last_component_contains_wcard = False;
58 return NT_STATUS_OBJECT_NAME_INVALID;
61 return NT_STATUS_OBJECT_NAME_INVALID;
63 if (strchr_m(&s[1], ':')) {
64 return NT_STATUS_OBJECT_NAME_INVALID;
70 if ((*s == ':') && !posix_path && !stream_started) {
71 if (*p_last_component_contains_wcard) {
72 return NT_STATUS_OBJECT_NAME_INVALID;
74 /* Stream names allow more characters than file names.
75 We're overloading posix_path here to allow a wider
76 range of characters. If stream_started is true this
77 is still a Windows path even if posix_path is true.
80 stream_started = true;
81 start_of_name_component = false;
85 return NT_STATUS_OBJECT_NAME_INVALID;
89 if (!stream_started && IS_PATH_SEP(*s,posix_path)) {
91 * Safe to assume is not the second part of a mb char
92 * as this is handled below.
94 /* Eat multiple '/' or '\\' */
95 while (IS_PATH_SEP(*s,posix_path)) {
98 if ((d != path) && (*s != '\0')) {
99 /* We only care about non-leading or trailing '/' or '\\' */
103 start_of_name_component = True;
105 *p_last_component_contains_wcard = False;
109 if (start_of_name_component) {
110 if ((s[0] == '.') && (s[1] == '.') && (IS_PATH_SEP(s[2],posix_path) || s[2] == '\0')) {
111 /* Uh oh - "/../" or "\\..\\" or "/..\0" or "\\..\0" ! */
114 * No mb char starts with '.' so we're safe checking the directory separator here.
117 /* If we just added a '/' - delete it */
118 if ((d > path) && (*(d-1) == '/')) {
123 /* Are we at the start ? Can't go back further if so. */
125 ret = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
128 /* Go back one level... */
129 /* We know this is safe as '/' cannot be part of a mb sequence. */
130 /* NOTE - if this assumption is invalid we are not in good shape... */
131 /* Decrement d first as d points to the *next* char to write into. */
132 for (d--; d > path; d--) {
136 s += 2; /* Else go past the .. */
137 /* We're still at the start of a name component, just the previous one. */
140 } else if ((s[0] == '.') && ((s[1] == '\0') || IS_PATH_SEP(s[1],posix_path))) {
152 if (*s <= 0x1f || *s == '|') {
153 return NT_STATUS_OBJECT_NAME_INVALID;
161 *p_last_component_contains_wcard = True;
170 /* Get the size of the next MB character. */
171 next_codepoint(s,&siz);
189 DEBUG(0,("check_path_syntax_internal: character length assumptions invalid !\n"));
191 return NT_STATUS_INVALID_PARAMETER;
194 start_of_name_component = False;
202 /****************************************************************************
203 Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
204 No wildcards allowed.
205 ****************************************************************************/
207 NTSTATUS check_path_syntax(char *path)
210 return check_path_syntax_internal(path, False, &ignore);
213 /****************************************************************************
214 Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
215 Wildcards allowed - p_contains_wcard returns true if the last component contained
217 ****************************************************************************/
219 NTSTATUS check_path_syntax_wcard(char *path, bool *p_contains_wcard)
221 return check_path_syntax_internal(path, False, p_contains_wcard);
224 /****************************************************************************
225 Check the path for a POSIX client.
226 We're assuming here that '/' is not the second byte in any multibyte char
227 set (a safe assumption).
228 ****************************************************************************/
230 NTSTATUS check_path_syntax_posix(char *path)
233 return check_path_syntax_internal(path, True, &ignore);
236 /****************************************************************************
237 Pull a string and check the path allowing a wilcard - provide for error return.
238 ****************************************************************************/
240 size_t srvstr_get_path_wcard(TALLOC_CTX *ctx,
241 const char *base_ptr,
248 bool *contains_wcard)
254 ret = srvstr_pull_talloc(ctx, base_ptr, smb_flags2, pp_dest, src,
258 *err = NT_STATUS_INVALID_PARAMETER;
262 *contains_wcard = False;
264 if (smb_flags2 & FLAGS2_DFS_PATHNAMES) {
266 * For a DFS path the function parse_dfs_path()
267 * will do the path processing, just make a copy.
273 if (lp_posix_pathnames()) {
274 *err = check_path_syntax_posix(*pp_dest);
276 *err = check_path_syntax_wcard(*pp_dest, contains_wcard);
282 /****************************************************************************
283 Pull a string and check the path - provide for error return.
284 ****************************************************************************/
286 size_t srvstr_get_path(TALLOC_CTX *ctx,
287 const char *base_ptr,
296 return srvstr_get_path_wcard(ctx, base_ptr, smb_flags2, pp_dest, src,
297 src_len, flags, err, &ignore);
300 size_t srvstr_get_path_req_wcard(TALLOC_CTX *mem_ctx, struct smb_request *req,
301 char **pp_dest, const char *src, int flags,
302 NTSTATUS *err, bool *contains_wcard)
304 return srvstr_get_path_wcard(mem_ctx, (char *)req->inbuf, req->flags2,
305 pp_dest, src, smbreq_bufrem(req, src),
306 flags, err, contains_wcard);
309 size_t srvstr_get_path_req(TALLOC_CTX *mem_ctx, struct smb_request *req,
310 char **pp_dest, const char *src, int flags,
314 return srvstr_get_path_req_wcard(mem_ctx, req, pp_dest, src,
315 flags, err, &ignore);
318 /****************************************************************************
319 Check if we have a correct fsp pointing to a file. Basic check for open fsp.
320 ****************************************************************************/
322 bool check_fsp_open(connection_struct *conn, struct smb_request *req,
325 if (!(fsp) || !(conn)) {
326 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
329 if (((conn) != (fsp)->conn) || req->vuid != (fsp)->vuid) {
330 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
336 /****************************************************************************
337 Check if we have a correct fsp pointing to a file.
338 ****************************************************************************/
340 bool check_fsp(connection_struct *conn, struct smb_request *req,
343 if (!check_fsp_open(conn, req, fsp)) {
346 if ((fsp)->is_directory) {
347 reply_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
350 if ((fsp)->fh->fd == -1) {
351 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
354 (fsp)->num_smb_operations++;
358 /****************************************************************************
359 Check if we have a correct fsp pointing to a quota fake file. Replacement for
360 the CHECK_NTQUOTA_HANDLE_OK macro.
361 ****************************************************************************/
363 bool check_fsp_ntquota_handle(connection_struct *conn, struct smb_request *req,
366 if (!check_fsp_open(conn, req, fsp)) {
370 if (fsp->is_directory) {
374 if (fsp->fake_file_handle == NULL) {
378 if (fsp->fake_file_handle->type != FAKE_FILE_TYPE_QUOTA) {
382 if (fsp->fake_file_handle->private_data == NULL) {
389 /****************************************************************************
390 Check if we have a correct fsp. Replacement for the FSP_BELONGS_CONN macro
391 ****************************************************************************/
393 bool fsp_belongs_conn(connection_struct *conn, struct smb_request *req,
396 if ((fsp) && (conn) && ((conn)==(fsp)->conn)
397 && (req->vuid == (fsp)->vuid)) {
401 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
405 static bool netbios_session_retarget(const char *name, int name_type)
408 char *trim_name_type;
409 const char *retarget_parm;
412 int retarget_type = 0x20;
413 int retarget_port = 139;
414 struct sockaddr_storage retarget_addr;
415 struct sockaddr_in *in_addr;
419 if (get_socket_port(smbd_server_fd()) != 139) {
423 trim_name = talloc_strdup(talloc_tos(), name);
424 if (trim_name == NULL) {
427 trim_char(trim_name, ' ', ' ');
429 trim_name_type = talloc_asprintf(trim_name, "%s#%2.2x", trim_name,
431 if (trim_name_type == NULL) {
435 retarget_parm = lp_parm_const_string(-1, "netbios retarget",
436 trim_name_type, NULL);
437 if (retarget_parm == NULL) {
438 retarget_parm = lp_parm_const_string(-1, "netbios retarget",
441 if (retarget_parm == NULL) {
445 retarget = talloc_strdup(trim_name, retarget_parm);
446 if (retarget == NULL) {
450 DEBUG(10, ("retargeting %s to %s\n", trim_name_type, retarget));
452 p = strchr(retarget, ':');
455 retarget_port = atoi(p);
458 p = strchr_m(retarget, '#');
461 sscanf(p, "%x", &retarget_type);
464 ret = resolve_name(retarget, &retarget_addr, retarget_type, false);
466 DEBUG(10, ("could not resolve %s\n", retarget));
470 if (retarget_addr.ss_family != AF_INET) {
471 DEBUG(10, ("Retarget target not an IPv4 addr\n"));
475 in_addr = (struct sockaddr_in *)(void *)&retarget_addr;
477 _smb_setlen(outbuf, 6);
478 SCVAL(outbuf, 0, 0x84);
479 *(uint32_t *)(outbuf+4) = in_addr->sin_addr.s_addr;
480 *(uint16_t *)(outbuf+8) = htons(retarget_port);
482 if (!srv_send_smb(smbd_server_fd(), (char *)outbuf, false, 0, false,
484 exit_server_cleanly("netbios_session_regarget: srv_send_smb "
490 TALLOC_FREE(trim_name);
494 /****************************************************************************
495 Reply to a (netbios-level) special message.
496 ****************************************************************************/
498 void reply_special(char *inbuf)
500 int msg_type = CVAL(inbuf,0);
501 int msg_flags = CVAL(inbuf,1);
503 char name_type1, name_type2;
504 struct smbd_server_connection *sconn = smbd_server_conn;
507 * We only really use 4 bytes of the outbuf, but for the smb_setlen
508 * calculation & friends (srv_send_smb uses that) we need the full smb
511 char outbuf[smb_size];
515 memset(outbuf, '\0', sizeof(outbuf));
517 smb_setlen(outbuf,0);
520 case 0x81: /* session request */
522 if (sconn->nbt.got_session) {
523 exit_server_cleanly("multiple session request not permitted");
526 SCVAL(outbuf,0,0x82);
528 if (name_len(inbuf+4) > 50 ||
529 name_len(inbuf+4 + name_len(inbuf + 4)) > 50) {
530 DEBUG(0,("Invalid name length in session request\n"));
533 name_type1 = name_extract(inbuf,4,name1);
534 name_type2 = name_extract(inbuf,4 + name_len(inbuf + 4),name2);
535 DEBUG(2,("netbios connect: name1=%s0x%x name2=%s0x%x\n",
536 name1, name_type1, name2, name_type2));
538 if (netbios_session_retarget(name1, name_type1)) {
539 exit_server_cleanly("retargeted client");
542 set_local_machine_name(name1, True);
543 set_remote_machine_name(name2, True);
545 DEBUG(2,("netbios connect: local=%s remote=%s, name type = %x\n",
546 get_local_machine_name(), get_remote_machine_name(),
549 if (name_type2 == 'R') {
550 /* We are being asked for a pathworks session ---
552 SCVAL(outbuf, 0,0x83);
556 /* only add the client's machine name to the list
557 of possibly valid usernames if we are operating
558 in share mode security */
559 if (lp_security() == SEC_SHARE) {
560 add_session_user(sconn, get_remote_machine_name());
563 reload_services(True);
566 sconn->nbt.got_session = true;
569 case 0x89: /* session keepalive request
570 (some old clients produce this?) */
571 SCVAL(outbuf,0,SMBkeepalive);
575 case 0x82: /* positive session response */
576 case 0x83: /* negative session response */
577 case 0x84: /* retarget session response */
578 DEBUG(0,("Unexpected session response\n"));
581 case SMBkeepalive: /* session keepalive */
586 DEBUG(5,("init msg_type=0x%x msg_flags=0x%x\n",
587 msg_type, msg_flags));
589 srv_send_smb(smbd_server_fd(), outbuf, false, 0, false, NULL);
593 /****************************************************************************
595 conn POINTER CAN BE NULL HERE !
596 ****************************************************************************/
598 void reply_tcon(struct smb_request *req)
600 connection_struct *conn = req->conn;
602 char *service_buf = NULL;
603 char *password = NULL;
608 DATA_BLOB password_blob;
609 TALLOC_CTX *ctx = talloc_tos();
610 struct smbd_server_connection *sconn = smbd_server_conn;
612 START_PROFILE(SMBtcon);
614 if (req->buflen < 4) {
615 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
616 END_PROFILE(SMBtcon);
620 p = (const char *)req->buf + 1;
621 p += srvstr_pull_req_talloc(ctx, req, &service_buf, p, STR_TERMINATE);
623 pwlen = srvstr_pull_req_talloc(ctx, req, &password, p, STR_TERMINATE);
625 p += srvstr_pull_req_talloc(ctx, req, &dev, p, STR_TERMINATE);
628 if (service_buf == NULL || password == NULL || dev == NULL) {
629 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
630 END_PROFILE(SMBtcon);
633 p = strrchr_m(service_buf,'\\');
637 service = service_buf;
640 password_blob = data_blob(password, pwlen+1);
642 conn = make_connection(sconn,service,password_blob,dev,
643 req->vuid,&nt_status);
646 data_blob_clear_free(&password_blob);
649 reply_nterror(req, nt_status);
650 END_PROFILE(SMBtcon);
654 reply_outbuf(req, 2, 0);
655 SSVAL(req->outbuf,smb_vwv0,sconn->smb1.negprot.max_recv);
656 SSVAL(req->outbuf,smb_vwv1,conn->cnum);
657 SSVAL(req->outbuf,smb_tid,conn->cnum);
659 DEBUG(3,("tcon service=%s cnum=%d\n",
660 service, conn->cnum));
662 END_PROFILE(SMBtcon);
666 /****************************************************************************
667 Reply to a tcon and X.
668 conn POINTER CAN BE NULL HERE !
669 ****************************************************************************/
671 void reply_tcon_and_X(struct smb_request *req)
673 connection_struct *conn = req->conn;
674 const char *service = NULL;
676 TALLOC_CTX *ctx = talloc_tos();
677 /* what the cleint thinks the device is */
678 char *client_devicetype = NULL;
679 /* what the server tells the client the share represents */
680 const char *server_devicetype;
686 struct smbd_server_connection *sconn = smbd_server_conn;
688 START_PROFILE(SMBtconX);
691 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
692 END_PROFILE(SMBtconX);
696 passlen = SVAL(req->vwv+3, 0);
697 tcon_flags = SVAL(req->vwv+2, 0);
699 /* we might have to close an old one */
700 if ((tcon_flags & 0x1) && conn) {
701 close_cnum(conn,req->vuid);
706 if ((passlen > MAX_PASS_LEN) || (passlen >= req->buflen)) {
707 reply_force_doserror(req, ERRDOS, ERRbuftoosmall);
708 END_PROFILE(SMBtconX);
712 if (sconn->smb1.negprot.encrypted_passwords) {
713 password = data_blob_talloc(talloc_tos(), req->buf, passlen);
714 if (lp_security() == SEC_SHARE) {
716 * Security = share always has a pad byte
717 * after the password.
719 p = (const char *)req->buf + passlen + 1;
721 p = (const char *)req->buf + passlen;
724 password = data_blob_talloc(talloc_tos(), req->buf, passlen+1);
725 /* Ensure correct termination */
726 password.data[passlen]=0;
727 p = (const char *)req->buf + passlen + 1;
730 p += srvstr_pull_req_talloc(ctx, req, &path, p, STR_TERMINATE);
733 data_blob_clear_free(&password);
734 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
735 END_PROFILE(SMBtconX);
740 * the service name can be either: \\server\share
741 * or share directly like on the DELL PowerVault 705
744 q = strchr_m(path+2,'\\');
746 data_blob_clear_free(&password);
747 reply_nterror(req, NT_STATUS_BAD_NETWORK_NAME);
748 END_PROFILE(SMBtconX);
756 p += srvstr_pull_talloc(ctx, req->inbuf, req->flags2,
757 &client_devicetype, p,
758 MIN(6, smbreq_bufrem(req, p)), STR_ASCII);
760 if (client_devicetype == NULL) {
761 data_blob_clear_free(&password);
762 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
763 END_PROFILE(SMBtconX);
767 DEBUG(4,("Client requested device type [%s] for share [%s]\n", client_devicetype, service));
769 conn = make_connection(sconn, service, password, client_devicetype,
770 req->vuid, &nt_status);
773 data_blob_clear_free(&password);
776 reply_nterror(req, nt_status);
777 END_PROFILE(SMBtconX);
782 server_devicetype = "IPC";
783 else if ( IS_PRINT(conn) )
784 server_devicetype = "LPT1:";
786 server_devicetype = "A:";
788 if (get_Protocol() < PROTOCOL_NT1) {
789 reply_outbuf(req, 2, 0);
790 if (message_push_string(&req->outbuf, server_devicetype,
791 STR_TERMINATE|STR_ASCII) == -1) {
792 reply_nterror(req, NT_STATUS_NO_MEMORY);
793 END_PROFILE(SMBtconX);
797 /* NT sets the fstype of IPC$ to the null string */
798 const char *fstype = IS_IPC(conn) ? "" : lp_fstype(SNUM(conn));
800 if (tcon_flags & TCONX_FLAG_EXTENDED_RESPONSE) {
801 /* Return permissions. */
805 reply_outbuf(req, 7, 0);
808 perm1 = FILE_ALL_ACCESS;
809 perm2 = FILE_ALL_ACCESS;
811 perm1 = CAN_WRITE(conn) ?
816 SIVAL(req->outbuf, smb_vwv3, perm1);
817 SIVAL(req->outbuf, smb_vwv5, perm2);
819 reply_outbuf(req, 3, 0);
822 if ((message_push_string(&req->outbuf, server_devicetype,
823 STR_TERMINATE|STR_ASCII) == -1)
824 || (message_push_string(&req->outbuf, fstype,
825 STR_TERMINATE) == -1)) {
826 reply_nterror(req, NT_STATUS_NO_MEMORY);
827 END_PROFILE(SMBtconX);
831 /* what does setting this bit do? It is set by NT4 and
832 may affect the ability to autorun mounted cdroms */
833 SSVAL(req->outbuf, smb_vwv2, SMB_SUPPORT_SEARCH_BITS|
834 (lp_csc_policy(SNUM(conn)) << 2));
836 if (lp_msdfs_root(SNUM(conn)) && lp_host_msdfs()) {
837 DEBUG(2,("Serving %s as a Dfs root\n",
838 lp_servicename(SNUM(conn)) ));
839 SSVAL(req->outbuf, smb_vwv2,
840 SMB_SHARE_IN_DFS | SVAL(req->outbuf, smb_vwv2));
845 DEBUG(3,("tconX service=%s \n",
848 /* set the incoming and outgoing tid to the just created one */
849 SSVAL(req->inbuf,smb_tid,conn->cnum);
850 SSVAL(req->outbuf,smb_tid,conn->cnum);
852 END_PROFILE(SMBtconX);
854 req->tid = conn->cnum;
859 /****************************************************************************
860 Reply to an unknown type.
861 ****************************************************************************/
863 void reply_unknown_new(struct smb_request *req, uint8 type)
865 DEBUG(0, ("unknown command type (%s): type=%d (0x%X)\n",
866 smb_fn_name(type), type, type));
867 reply_force_doserror(req, ERRSRV, ERRunknownsmb);
871 /****************************************************************************
873 conn POINTER CAN BE NULL HERE !
874 ****************************************************************************/
876 void reply_ioctl(struct smb_request *req)
878 connection_struct *conn = req->conn;
885 START_PROFILE(SMBioctl);
888 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
889 END_PROFILE(SMBioctl);
893 device = SVAL(req->vwv+1, 0);
894 function = SVAL(req->vwv+2, 0);
895 ioctl_code = (device << 16) + function;
897 DEBUG(4, ("Received IOCTL (code 0x%x)\n", ioctl_code));
899 switch (ioctl_code) {
900 case IOCTL_QUERY_JOB_INFO:
904 reply_force_doserror(req, ERRSRV, ERRnosupport);
905 END_PROFILE(SMBioctl);
909 reply_outbuf(req, 8, replysize+1);
910 SSVAL(req->outbuf,smb_vwv1,replysize); /* Total data bytes returned */
911 SSVAL(req->outbuf,smb_vwv5,replysize); /* Data bytes this buffer */
912 SSVAL(req->outbuf,smb_vwv6,52); /* Offset to data */
913 p = smb_buf(req->outbuf);
914 memset(p, '\0', replysize+1); /* valgrind-safe. */
915 p += 1; /* Allow for alignment */
917 switch (ioctl_code) {
918 case IOCTL_QUERY_JOB_INFO:
920 files_struct *fsp = file_fsp(
921 req, SVAL(req->vwv+0, 0));
923 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
924 END_PROFILE(SMBioctl);
927 SSVAL(p,0,fsp->rap_print_jobid); /* Job number */
928 srvstr_push((char *)req->outbuf, req->flags2, p+2,
930 STR_TERMINATE|STR_ASCII);
932 srvstr_push((char *)req->outbuf, req->flags2,
933 p+18, lp_servicename(SNUM(conn)),
934 13, STR_TERMINATE|STR_ASCII);
942 END_PROFILE(SMBioctl);
946 /****************************************************************************
947 Strange checkpath NTSTATUS mapping.
948 ****************************************************************************/
950 static NTSTATUS map_checkpath_error(uint16_t flags2, NTSTATUS status)
952 /* Strange DOS error code semantics only for checkpath... */
953 if (!(flags2 & FLAGS2_32_BIT_ERROR_CODES)) {
954 if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_INVALID,status)) {
955 /* We need to map to ERRbadpath */
956 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
962 /****************************************************************************
963 Reply to a checkpath.
964 ****************************************************************************/
966 void reply_checkpath(struct smb_request *req)
968 connection_struct *conn = req->conn;
969 struct smb_filename *smb_fname = NULL;
972 TALLOC_CTX *ctx = talloc_tos();
974 START_PROFILE(SMBcheckpath);
976 srvstr_get_path_req(ctx, req, &name, (const char *)req->buf + 1,
977 STR_TERMINATE, &status);
979 if (!NT_STATUS_IS_OK(status)) {
980 status = map_checkpath_error(req->flags2, status);
981 reply_nterror(req, status);
982 END_PROFILE(SMBcheckpath);
986 DEBUG(3,("reply_checkpath %s mode=%d\n", name, (int)SVAL(req->vwv+0, 0)));
988 status = filename_convert(ctx,
990 req->flags2 & FLAGS2_DFS_PATHNAMES,
996 if (!NT_STATUS_IS_OK(status)) {
997 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
998 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1000 END_PROFILE(SMBcheckpath);
1006 if (!VALID_STAT(smb_fname->st) &&
1007 (SMB_VFS_STAT(conn, smb_fname) != 0)) {
1008 DEBUG(3,("reply_checkpath: stat of %s failed (%s)\n",
1009 smb_fname_str_dbg(smb_fname), strerror(errno)));
1010 status = map_nt_error_from_unix(errno);
1014 if (!S_ISDIR(smb_fname->st.st_ex_mode)) {
1015 reply_botherror(req, NT_STATUS_NOT_A_DIRECTORY,
1016 ERRDOS, ERRbadpath);
1020 reply_outbuf(req, 0, 0);
1023 /* We special case this - as when a Windows machine
1024 is parsing a path is steps through the components
1025 one at a time - if a component fails it expects
1026 ERRbadpath, not ERRbadfile.
1028 status = map_checkpath_error(req->flags2, status);
1029 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1031 * Windows returns different error codes if
1032 * the parent directory is valid but not the
1033 * last component - it returns NT_STATUS_OBJECT_NAME_NOT_FOUND
1034 * for that case and NT_STATUS_OBJECT_PATH_NOT_FOUND
1035 * if the path is invalid.
1037 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
1038 ERRDOS, ERRbadpath);
1042 reply_nterror(req, status);
1045 TALLOC_FREE(smb_fname);
1046 END_PROFILE(SMBcheckpath);
1050 /****************************************************************************
1052 ****************************************************************************/
1054 void reply_getatr(struct smb_request *req)
1056 connection_struct *conn = req->conn;
1057 struct smb_filename *smb_fname = NULL;
1064 TALLOC_CTX *ctx = talloc_tos();
1065 bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
1067 START_PROFILE(SMBgetatr);
1069 p = (const char *)req->buf + 1;
1070 p += srvstr_get_path_req(ctx, req, &fname, p, STR_TERMINATE, &status);
1071 if (!NT_STATUS_IS_OK(status)) {
1072 reply_nterror(req, status);
1076 /* dos smetimes asks for a stat of "" - it returns a "hidden directory"
1077 under WfWg - weird! */
1078 if (*fname == '\0') {
1079 mode = aHIDDEN | aDIR;
1080 if (!CAN_WRITE(conn)) {
1086 status = filename_convert(ctx,
1088 req->flags2 & FLAGS2_DFS_PATHNAMES,
1093 if (!NT_STATUS_IS_OK(status)) {
1094 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1095 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1096 ERRSRV, ERRbadpath);
1099 reply_nterror(req, status);
1102 if (!VALID_STAT(smb_fname->st) &&
1103 (SMB_VFS_STAT(conn, smb_fname) != 0)) {
1104 DEBUG(3,("reply_getatr: stat of %s failed (%s)\n",
1105 smb_fname_str_dbg(smb_fname),
1107 reply_nterror(req, map_nt_error_from_unix(errno));
1111 mode = dos_mode(conn, smb_fname);
1112 size = smb_fname->st.st_ex_size;
1114 if (ask_sharemode) {
1115 struct timespec write_time_ts;
1116 struct file_id fileid;
1118 ZERO_STRUCT(write_time_ts);
1119 fileid = vfs_file_id_from_sbuf(conn, &smb_fname->st);
1120 get_file_infos(fileid, NULL, &write_time_ts);
1121 if (!null_timespec(write_time_ts)) {
1122 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1126 mtime = convert_timespec_to_time_t(smb_fname->st.st_ex_mtime);
1132 reply_outbuf(req, 10, 0);
1134 SSVAL(req->outbuf,smb_vwv0,mode);
1135 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1136 srv_put_dos_date3((char *)req->outbuf,smb_vwv1,mtime & ~1);
1138 srv_put_dos_date3((char *)req->outbuf,smb_vwv1,mtime);
1140 SIVAL(req->outbuf,smb_vwv3,(uint32)size);
1142 if (get_Protocol() >= PROTOCOL_NT1) {
1143 SSVAL(req->outbuf, smb_flg2,
1144 SVAL(req->outbuf, smb_flg2) | FLAGS2_IS_LONG_NAME);
1147 DEBUG(3,("reply_getatr: name=%s mode=%d size=%u\n",
1148 smb_fname_str_dbg(smb_fname), mode, (unsigned int)size));
1151 TALLOC_FREE(smb_fname);
1153 END_PROFILE(SMBgetatr);
1157 /****************************************************************************
1159 ****************************************************************************/
1161 void reply_setatr(struct smb_request *req)
1163 struct smb_file_time ft;
1164 connection_struct *conn = req->conn;
1165 struct smb_filename *smb_fname = NULL;
1171 TALLOC_CTX *ctx = talloc_tos();
1173 START_PROFILE(SMBsetatr);
1178 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1182 p = (const char *)req->buf + 1;
1183 p += srvstr_get_path_req(ctx, req, &fname, p, STR_TERMINATE, &status);
1184 if (!NT_STATUS_IS_OK(status)) {
1185 reply_nterror(req, status);
1189 status = filename_convert(ctx,
1191 req->flags2 & FLAGS2_DFS_PATHNAMES,
1196 if (!NT_STATUS_IS_OK(status)) {
1197 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1198 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1199 ERRSRV, ERRbadpath);
1202 reply_nterror(req, status);
1206 if (smb_fname->base_name[0] == '.' &&
1207 smb_fname->base_name[1] == '\0') {
1209 * Not sure here is the right place to catch this
1210 * condition. Might be moved to somewhere else later -- vl
1212 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1216 mode = SVAL(req->vwv+0, 0);
1217 mtime = srv_make_unix_date3(req->vwv+1);
1219 ft.mtime = convert_time_t_to_timespec(mtime);
1220 status = smb_set_file_time(conn, NULL, smb_fname, &ft, true);
1221 if (!NT_STATUS_IS_OK(status)) {
1222 reply_nterror(req, status);
1226 if (mode != FILE_ATTRIBUTE_NORMAL) {
1227 if (VALID_STAT_OF_DIR(smb_fname->st))
1232 if (file_set_dosmode(conn, smb_fname, mode, NULL,
1234 reply_nterror(req, map_nt_error_from_unix(errno));
1239 reply_outbuf(req, 0, 0);
1241 DEBUG(3, ("setatr name=%s mode=%d\n", smb_fname_str_dbg(smb_fname),
1244 TALLOC_FREE(smb_fname);
1245 END_PROFILE(SMBsetatr);
1249 /****************************************************************************
1251 ****************************************************************************/
1253 void reply_dskattr(struct smb_request *req)
1255 connection_struct *conn = req->conn;
1256 uint64_t dfree,dsize,bsize;
1257 START_PROFILE(SMBdskattr);
1259 if (get_dfree_info(conn,".",True,&bsize,&dfree,&dsize) == (uint64_t)-1) {
1260 reply_nterror(req, map_nt_error_from_unix(errno));
1261 END_PROFILE(SMBdskattr);
1265 reply_outbuf(req, 5, 0);
1267 if (get_Protocol() <= PROTOCOL_LANMAN2) {
1268 double total_space, free_space;
1269 /* we need to scale this to a number that DOS6 can handle. We
1270 use floating point so we can handle large drives on systems
1271 that don't have 64 bit integers
1273 we end up displaying a maximum of 2G to DOS systems
1275 total_space = dsize * (double)bsize;
1276 free_space = dfree * (double)bsize;
1278 dsize = (uint64_t)((total_space+63*512) / (64*512));
1279 dfree = (uint64_t)((free_space+63*512) / (64*512));
1281 if (dsize > 0xFFFF) dsize = 0xFFFF;
1282 if (dfree > 0xFFFF) dfree = 0xFFFF;
1284 SSVAL(req->outbuf,smb_vwv0,dsize);
1285 SSVAL(req->outbuf,smb_vwv1,64); /* this must be 64 for dos systems */
1286 SSVAL(req->outbuf,smb_vwv2,512); /* and this must be 512 */
1287 SSVAL(req->outbuf,smb_vwv3,dfree);
1289 SSVAL(req->outbuf,smb_vwv0,dsize);
1290 SSVAL(req->outbuf,smb_vwv1,bsize/512);
1291 SSVAL(req->outbuf,smb_vwv2,512);
1292 SSVAL(req->outbuf,smb_vwv3,dfree);
1295 DEBUG(3,("dskattr dfree=%d\n", (unsigned int)dfree));
1297 END_PROFILE(SMBdskattr);
1302 * Utility function to split the filename from the directory.
1304 static NTSTATUS split_fname_dir_mask(TALLOC_CTX *ctx, const char *fname_in,
1305 char **fname_dir_out,
1306 char **fname_mask_out)
1308 const char *p = NULL;
1309 char *fname_dir = NULL;
1310 char *fname_mask = NULL;
1312 p = strrchr_m(fname_in, '/');
1314 fname_dir = talloc_strdup(ctx, ".");
1315 fname_mask = talloc_strdup(ctx, fname_in);
1317 fname_dir = talloc_strndup(ctx, fname_in,
1318 PTR_DIFF(p, fname_in));
1319 fname_mask = talloc_strdup(ctx, p+1);
1322 if (!fname_dir || !fname_mask) {
1323 TALLOC_FREE(fname_dir);
1324 TALLOC_FREE(fname_mask);
1325 return NT_STATUS_NO_MEMORY;
1328 *fname_dir_out = fname_dir;
1329 *fname_mask_out = fname_mask;
1330 return NT_STATUS_OK;
1333 /****************************************************************************
1335 Can be called from SMBsearch, SMBffirst or SMBfunique.
1336 ****************************************************************************/
1338 void reply_search(struct smb_request *req)
1340 connection_struct *conn = req->conn;
1342 const char *mask = NULL;
1343 char *directory = NULL;
1344 struct smb_filename *smb_fname = NULL;
1348 struct timespec date;
1350 unsigned int numentries = 0;
1351 unsigned int maxentries = 0;
1352 bool finished = False;
1357 bool check_descend = False;
1358 bool expect_close = False;
1360 bool mask_contains_wcard = False;
1361 bool allow_long_path_components = (req->flags2 & FLAGS2_LONG_PATH_COMPONENTS) ? True : False;
1362 TALLOC_CTX *ctx = talloc_tos();
1363 bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
1364 struct dptr_struct *dirptr = NULL;
1365 struct smbd_server_connection *sconn = smbd_server_conn;
1367 START_PROFILE(SMBsearch);
1370 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1374 if (lp_posix_pathnames()) {
1375 reply_unknown_new(req, req->cmd);
1379 /* If we were called as SMBffirst then we must expect close. */
1380 if(req->cmd == SMBffirst) {
1381 expect_close = True;
1384 reply_outbuf(req, 1, 3);
1385 maxentries = SVAL(req->vwv+0, 0);
1386 dirtype = SVAL(req->vwv+1, 0);
1387 p = (const char *)req->buf + 1;
1388 p += srvstr_get_path_req_wcard(ctx, req, &path, p, STR_TERMINATE,
1389 &nt_status, &mask_contains_wcard);
1390 if (!NT_STATUS_IS_OK(nt_status)) {
1391 reply_nterror(req, nt_status);
1396 status_len = SVAL(p, 0);
1399 /* dirtype &= ~aDIR; */
1401 if (status_len == 0) {
1402 nt_status = filename_convert(ctx, conn,
1403 req->flags2 & FLAGS2_DFS_PATHNAMES,
1405 UCF_ALWAYS_ALLOW_WCARD_LCOMP,
1406 &mask_contains_wcard,
1408 if (!NT_STATUS_IS_OK(nt_status)) {
1409 if (NT_STATUS_EQUAL(nt_status,NT_STATUS_PATH_NOT_COVERED)) {
1410 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1411 ERRSRV, ERRbadpath);
1414 reply_nterror(req, nt_status);
1418 directory = smb_fname->base_name;
1420 p = strrchr_m(directory,'/');
1421 if ((p != NULL) && (*directory != '/')) {
1423 directory = talloc_strndup(ctx, directory,
1424 PTR_DIFF(p, directory));
1427 directory = talloc_strdup(ctx,".");
1431 reply_nterror(req, NT_STATUS_NO_MEMORY);
1435 memset((char *)status,'\0',21);
1436 SCVAL(status,0,(dirtype & 0x1F));
1438 nt_status = dptr_create(conn,
1444 mask_contains_wcard,
1447 if (!NT_STATUS_IS_OK(nt_status)) {
1448 reply_nterror(req, nt_status);
1451 dptr_num = dptr_dnum(dirptr);
1454 const char *dirpath;
1456 memcpy(status,p,21);
1457 status_dirtype = CVAL(status,0) & 0x1F;
1458 if (status_dirtype != (dirtype & 0x1F)) {
1459 dirtype = status_dirtype;
1462 dirptr = dptr_fetch(sconn, status+12,&dptr_num);
1466 dirpath = dptr_path(sconn, dptr_num);
1467 directory = talloc_strdup(ctx, dirpath);
1469 reply_nterror(req, NT_STATUS_NO_MEMORY);
1473 mask = dptr_wcard(sconn, dptr_num);
1478 * For a 'continue' search we have no string. So
1479 * check from the initial saved string.
1481 mask_contains_wcard = ms_has_wild(mask);
1482 dirtype = dptr_attr(sconn, dptr_num);
1485 DEBUG(4,("dptr_num is %d\n",dptr_num));
1487 /* Initialize per SMBsearch/SMBffirst/SMBfunique operation data */
1488 dptr_init_search_op(dirptr);
1490 if ((dirtype&0x1F) == aVOLID) {
1491 char buf[DIR_STRUCT_SIZE];
1492 memcpy(buf,status,21);
1493 if (!make_dir_struct(ctx,buf,"???????????",volume_label(SNUM(conn)),
1494 0,aVOLID,0,!allow_long_path_components)) {
1495 reply_nterror(req, NT_STATUS_NO_MEMORY);
1498 dptr_fill(sconn, buf+12,dptr_num);
1499 if (dptr_zero(buf+12) && (status_len==0)) {
1504 if (message_push_blob(&req->outbuf,
1505 data_blob_const(buf, sizeof(buf)))
1507 reply_nterror(req, NT_STATUS_NO_MEMORY);
1515 ((uint8 *)smb_buf(req->outbuf) + 3 - req->outbuf))
1518 DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
1519 directory,lp_dontdescend(SNUM(conn))));
1520 if (in_list(directory, lp_dontdescend(SNUM(conn)),True)) {
1521 check_descend = True;
1524 for (i=numentries;(i<maxentries) && !finished;i++) {
1525 finished = !get_dir_entry(ctx,
1536 char buf[DIR_STRUCT_SIZE];
1537 memcpy(buf,status,21);
1538 if (!make_dir_struct(ctx,
1544 convert_timespec_to_time_t(date),
1545 !allow_long_path_components)) {
1546 reply_nterror(req, NT_STATUS_NO_MEMORY);
1549 if (!dptr_fill(sconn, buf+12,dptr_num)) {
1552 if (message_push_blob(&req->outbuf,
1553 data_blob_const(buf, sizeof(buf)))
1555 reply_nterror(req, NT_STATUS_NO_MEMORY);
1565 /* If we were called as SMBffirst with smb_search_id == NULL
1566 and no entries were found then return error and close dirptr
1569 if (numentries == 0) {
1570 dptr_close(sconn, &dptr_num);
1571 } else if(expect_close && status_len == 0) {
1572 /* Close the dptr - we know it's gone */
1573 dptr_close(sconn, &dptr_num);
1576 /* If we were called as SMBfunique, then we can close the dirptr now ! */
1577 if(dptr_num >= 0 && req->cmd == SMBfunique) {
1578 dptr_close(sconn, &dptr_num);
1581 if ((numentries == 0) && !mask_contains_wcard) {
1582 reply_botherror(req, STATUS_NO_MORE_FILES, ERRDOS, ERRnofiles);
1586 SSVAL(req->outbuf,smb_vwv0,numentries);
1587 SSVAL(req->outbuf,smb_vwv1,3 + numentries * DIR_STRUCT_SIZE);
1588 SCVAL(smb_buf(req->outbuf),0,5);
1589 SSVAL(smb_buf(req->outbuf),1,numentries*DIR_STRUCT_SIZE);
1591 /* The replies here are never long name. */
1592 SSVAL(req->outbuf, smb_flg2,
1593 SVAL(req->outbuf, smb_flg2) & (~FLAGS2_IS_LONG_NAME));
1594 if (!allow_long_path_components) {
1595 SSVAL(req->outbuf, smb_flg2,
1596 SVAL(req->outbuf, smb_flg2)
1597 & (~FLAGS2_LONG_PATH_COMPONENTS));
1600 /* This SMB *always* returns ASCII names. Remove the unicode bit in flags2. */
1601 SSVAL(req->outbuf, smb_flg2,
1602 (SVAL(req->outbuf, smb_flg2) & (~FLAGS2_UNICODE_STRINGS)));
1604 DEBUG(4,("%s mask=%s path=%s dtype=%d nument=%u of %u\n",
1605 smb_fn_name(req->cmd),
1612 TALLOC_FREE(directory);
1613 TALLOC_FREE(smb_fname);
1614 END_PROFILE(SMBsearch);
1618 /****************************************************************************
1619 Reply to a fclose (stop directory search).
1620 ****************************************************************************/
1622 void reply_fclose(struct smb_request *req)
1630 bool path_contains_wcard = False;
1631 TALLOC_CTX *ctx = talloc_tos();
1632 struct smbd_server_connection *sconn = smbd_server_conn;
1634 START_PROFILE(SMBfclose);
1636 if (lp_posix_pathnames()) {
1637 reply_unknown_new(req, req->cmd);
1638 END_PROFILE(SMBfclose);
1642 p = (const char *)req->buf + 1;
1643 p += srvstr_get_path_req_wcard(ctx, req, &path, p, STR_TERMINATE,
1644 &err, &path_contains_wcard);
1645 if (!NT_STATUS_IS_OK(err)) {
1646 reply_nterror(req, err);
1647 END_PROFILE(SMBfclose);
1651 status_len = SVAL(p,0);
1654 if (status_len == 0) {
1655 reply_force_doserror(req, ERRSRV, ERRsrverror);
1656 END_PROFILE(SMBfclose);
1660 memcpy(status,p,21);
1662 if(dptr_fetch(sconn, status+12,&dptr_num)) {
1663 /* Close the dptr - we know it's gone */
1664 dptr_close(sconn, &dptr_num);
1667 reply_outbuf(req, 1, 0);
1668 SSVAL(req->outbuf,smb_vwv0,0);
1670 DEBUG(3,("search close\n"));
1672 END_PROFILE(SMBfclose);
1676 /****************************************************************************
1678 ****************************************************************************/
1680 void reply_open(struct smb_request *req)
1682 connection_struct *conn = req->conn;
1683 struct smb_filename *smb_fname = NULL;
1695 uint32 create_disposition;
1696 uint32 create_options = 0;
1697 uint32_t private_flags = 0;
1699 bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
1700 TALLOC_CTX *ctx = talloc_tos();
1702 START_PROFILE(SMBopen);
1705 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1709 oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
1710 deny_mode = SVAL(req->vwv+0, 0);
1711 dos_attr = SVAL(req->vwv+1, 0);
1713 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf+1,
1714 STR_TERMINATE, &status);
1715 if (!NT_STATUS_IS_OK(status)) {
1716 reply_nterror(req, status);
1720 status = filename_convert(ctx,
1722 req->flags2 & FLAGS2_DFS_PATHNAMES,
1727 if (!NT_STATUS_IS_OK(status)) {
1728 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1729 reply_botherror(req,
1730 NT_STATUS_PATH_NOT_COVERED,
1731 ERRSRV, ERRbadpath);
1734 reply_nterror(req, status);
1738 if (!map_open_params_to_ntcreate(smb_fname, deny_mode,
1739 OPENX_FILE_EXISTS_OPEN, &access_mask,
1740 &share_mode, &create_disposition,
1741 &create_options, &private_flags)) {
1742 reply_force_doserror(req, ERRDOS, ERRbadaccess);
1746 status = SMB_VFS_CREATE_FILE(
1749 0, /* root_dir_fid */
1750 smb_fname, /* fname */
1751 access_mask, /* access_mask */
1752 share_mode, /* share_access */
1753 create_disposition, /* create_disposition*/
1754 create_options, /* create_options */
1755 dos_attr, /* file_attributes */
1756 oplock_request, /* oplock_request */
1757 0, /* allocation_size */
1764 if (!NT_STATUS_IS_OK(status)) {
1765 if (open_was_deferred(req->mid)) {
1766 /* We have re-scheduled this call. */
1769 reply_openerror(req, status);
1773 size = smb_fname->st.st_ex_size;
1774 fattr = dos_mode(conn, smb_fname);
1776 /* Deal with other possible opens having a modified
1778 if (ask_sharemode) {
1779 struct timespec write_time_ts;
1781 ZERO_STRUCT(write_time_ts);
1782 get_file_infos(fsp->file_id, NULL, &write_time_ts);
1783 if (!null_timespec(write_time_ts)) {
1784 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1788 mtime = convert_timespec_to_time_t(smb_fname->st.st_ex_mtime);
1791 DEBUG(3,("attempt to open a directory %s\n",
1793 close_file(req, fsp, ERROR_CLOSE);
1794 reply_botherror(req, NT_STATUS_ACCESS_DENIED,
1795 ERRDOS, ERRnoaccess);
1799 reply_outbuf(req, 7, 0);
1800 SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
1801 SSVAL(req->outbuf,smb_vwv1,fattr);
1802 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1803 srv_put_dos_date3((char *)req->outbuf,smb_vwv2,mtime & ~1);
1805 srv_put_dos_date3((char *)req->outbuf,smb_vwv2,mtime);
1807 SIVAL(req->outbuf,smb_vwv4,(uint32)size);
1808 SSVAL(req->outbuf,smb_vwv6,deny_mode);
1810 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1811 SCVAL(req->outbuf,smb_flg,
1812 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1815 if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1816 SCVAL(req->outbuf,smb_flg,
1817 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1820 TALLOC_FREE(smb_fname);
1821 END_PROFILE(SMBopen);
1825 /****************************************************************************
1826 Reply to an open and X.
1827 ****************************************************************************/
1829 void reply_open_and_X(struct smb_request *req)
1831 connection_struct *conn = req->conn;
1832 struct smb_filename *smb_fname = NULL;
1837 /* Breakout the oplock request bits so we can set the
1838 reply bits separately. */
1839 int ex_oplock_request;
1840 int core_oplock_request;
1843 int smb_sattr = SVAL(req->vwv+4, 0);
1844 uint32 smb_time = make_unix_date3(req->vwv+6);
1852 uint64_t allocation_size;
1853 ssize_t retval = -1;
1856 uint32 create_disposition;
1857 uint32 create_options = 0;
1858 uint32_t private_flags = 0;
1859 TALLOC_CTX *ctx = talloc_tos();
1861 START_PROFILE(SMBopenX);
1863 if (req->wct < 15) {
1864 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1868 open_flags = SVAL(req->vwv+2, 0);
1869 deny_mode = SVAL(req->vwv+3, 0);
1870 smb_attr = SVAL(req->vwv+5, 0);
1871 ex_oplock_request = EXTENDED_OPLOCK_REQUEST(req->inbuf);
1872 core_oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
1873 oplock_request = ex_oplock_request | core_oplock_request;
1874 smb_ofun = SVAL(req->vwv+8, 0);
1875 allocation_size = (uint64_t)IVAL(req->vwv+9, 0);
1877 /* If it's an IPC, pass off the pipe handler. */
1879 if (lp_nt_pipe_support()) {
1880 reply_open_pipe_and_X(conn, req);
1882 reply_nterror(req, NT_STATUS_NETWORK_ACCESS_DENIED);
1887 /* XXXX we need to handle passed times, sattr and flags */
1888 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
1889 STR_TERMINATE, &status);
1890 if (!NT_STATUS_IS_OK(status)) {
1891 reply_nterror(req, status);
1895 status = filename_convert(ctx,
1897 req->flags2 & FLAGS2_DFS_PATHNAMES,
1902 if (!NT_STATUS_IS_OK(status)) {
1903 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1904 reply_botherror(req,
1905 NT_STATUS_PATH_NOT_COVERED,
1906 ERRSRV, ERRbadpath);
1909 reply_nterror(req, status);
1913 if (!map_open_params_to_ntcreate(smb_fname, deny_mode, smb_ofun,
1914 &access_mask, &share_mode,
1915 &create_disposition,
1918 reply_force_doserror(req, ERRDOS, ERRbadaccess);
1922 status = SMB_VFS_CREATE_FILE(
1925 0, /* root_dir_fid */
1926 smb_fname, /* fname */
1927 access_mask, /* access_mask */
1928 share_mode, /* share_access */
1929 create_disposition, /* create_disposition*/
1930 create_options, /* create_options */
1931 smb_attr, /* file_attributes */
1932 oplock_request, /* oplock_request */
1933 0, /* allocation_size */
1938 &smb_action); /* pinfo */
1940 if (!NT_STATUS_IS_OK(status)) {
1941 if (open_was_deferred(req->mid)) {
1942 /* We have re-scheduled this call. */
1945 reply_openerror(req, status);
1949 /* Setting the "size" field in vwv9 and vwv10 causes the file to be set to this size,
1950 if the file is truncated or created. */
1951 if (((smb_action == FILE_WAS_CREATED) || (smb_action == FILE_WAS_OVERWRITTEN)) && allocation_size) {
1952 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1953 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1954 close_file(req, fsp, ERROR_CLOSE);
1955 reply_nterror(req, NT_STATUS_DISK_FULL);
1958 retval = vfs_set_filelen(fsp, (SMB_OFF_T)allocation_size);
1960 close_file(req, fsp, ERROR_CLOSE);
1961 reply_nterror(req, NT_STATUS_DISK_FULL);
1964 smb_fname->st.st_ex_size =
1965 SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st);
1968 fattr = dos_mode(conn, smb_fname);
1969 mtime = convert_timespec_to_time_t(smb_fname->st.st_ex_mtime);
1971 close_file(req, fsp, ERROR_CLOSE);
1972 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1976 /* If the caller set the extended oplock request bit
1977 and we granted one (by whatever means) - set the
1978 correct bit for extended oplock reply.
1981 if (ex_oplock_request && lp_fake_oplocks(SNUM(conn))) {
1982 smb_action |= EXTENDED_OPLOCK_GRANTED;
1985 if(ex_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1986 smb_action |= EXTENDED_OPLOCK_GRANTED;
1989 /* If the caller set the core oplock request bit
1990 and we granted one (by whatever means) - set the
1991 correct bit for core oplock reply.
1994 if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
1995 reply_outbuf(req, 19, 0);
1997 reply_outbuf(req, 15, 0);
2000 if (core_oplock_request && lp_fake_oplocks(SNUM(conn))) {
2001 SCVAL(req->outbuf, smb_flg,
2002 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2005 if(core_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
2006 SCVAL(req->outbuf, smb_flg,
2007 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2010 SSVAL(req->outbuf,smb_vwv2,fsp->fnum);
2011 SSVAL(req->outbuf,smb_vwv3,fattr);
2012 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
2013 srv_put_dos_date3((char *)req->outbuf,smb_vwv4,mtime & ~1);
2015 srv_put_dos_date3((char *)req->outbuf,smb_vwv4,mtime);
2017 SIVAL(req->outbuf,smb_vwv6,(uint32)smb_fname->st.st_ex_size);
2018 SSVAL(req->outbuf,smb_vwv8,GET_OPENX_MODE(deny_mode));
2019 SSVAL(req->outbuf,smb_vwv11,smb_action);
2021 if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
2022 SIVAL(req->outbuf, smb_vwv15, STD_RIGHT_ALL_ACCESS);
2027 TALLOC_FREE(smb_fname);
2028 END_PROFILE(SMBopenX);
2032 /****************************************************************************
2033 Reply to a SMBulogoffX.
2034 ****************************************************************************/
2036 void reply_ulogoffX(struct smb_request *req)
2038 struct smbd_server_connection *sconn = smbd_server_conn;
2041 START_PROFILE(SMBulogoffX);
2043 vuser = get_valid_user_struct(sconn, req->vuid);
2046 DEBUG(3,("ulogoff, vuser id %d does not map to user.\n",
2050 /* in user level security we are supposed to close any files
2051 open by this user */
2052 if ((vuser != NULL) && (lp_security() != SEC_SHARE)) {
2053 file_close_user(req->vuid);
2056 invalidate_vuid(sconn, req->vuid);
2058 reply_outbuf(req, 2, 0);
2060 DEBUG( 3, ( "ulogoffX vuid=%d\n", req->vuid ) );
2062 END_PROFILE(SMBulogoffX);
2063 req->vuid = UID_FIELD_INVALID;
2067 /****************************************************************************
2068 Reply to a mknew or a create.
2069 ****************************************************************************/
2071 void reply_mknew(struct smb_request *req)
2073 connection_struct *conn = req->conn;
2074 struct smb_filename *smb_fname = NULL;
2077 struct smb_file_time ft;
2079 int oplock_request = 0;
2081 uint32 access_mask = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
2082 uint32 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
2083 uint32 create_disposition;
2084 uint32 create_options = 0;
2085 TALLOC_CTX *ctx = talloc_tos();
2087 START_PROFILE(SMBcreate);
2091 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2095 fattr = SVAL(req->vwv+0, 0);
2096 oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
2099 ft.mtime = convert_time_t_to_timespec(srv_make_unix_date3(req->vwv+1));
2101 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf + 1,
2102 STR_TERMINATE, &status);
2103 if (!NT_STATUS_IS_OK(status)) {
2104 reply_nterror(req, status);
2108 status = filename_convert(ctx,
2110 req->flags2 & FLAGS2_DFS_PATHNAMES,
2115 if (!NT_STATUS_IS_OK(status)) {
2116 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2117 reply_botherror(req,
2118 NT_STATUS_PATH_NOT_COVERED,
2119 ERRSRV, ERRbadpath);
2122 reply_nterror(req, status);
2126 if (fattr & aVOLID) {
2127 DEBUG(0,("Attempt to create file (%s) with volid set - "
2128 "please report this\n",
2129 smb_fname_str_dbg(smb_fname)));
2132 if(req->cmd == SMBmknew) {
2133 /* We should fail if file exists. */
2134 create_disposition = FILE_CREATE;
2136 /* Create if file doesn't exist, truncate if it does. */
2137 create_disposition = FILE_OVERWRITE_IF;
2140 status = SMB_VFS_CREATE_FILE(
2143 0, /* root_dir_fid */
2144 smb_fname, /* fname */
2145 access_mask, /* access_mask */
2146 share_mode, /* share_access */
2147 create_disposition, /* create_disposition*/
2148 create_options, /* create_options */
2149 fattr, /* file_attributes */
2150 oplock_request, /* oplock_request */
2151 0, /* allocation_size */
2152 0, /* private_flags */
2158 if (!NT_STATUS_IS_OK(status)) {
2159 if (open_was_deferred(req->mid)) {
2160 /* We have re-scheduled this call. */
2163 reply_openerror(req, status);
2167 ft.atime = smb_fname->st.st_ex_atime; /* atime. */
2168 status = smb_set_file_time(conn, fsp, smb_fname, &ft, true);
2169 if (!NT_STATUS_IS_OK(status)) {
2170 END_PROFILE(SMBcreate);
2174 reply_outbuf(req, 1, 0);
2175 SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
2177 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
2178 SCVAL(req->outbuf,smb_flg,
2179 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2182 if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
2183 SCVAL(req->outbuf,smb_flg,
2184 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2187 DEBUG(2, ("reply_mknew: file %s\n", smb_fname_str_dbg(smb_fname)));
2188 DEBUG(3, ("reply_mknew %s fd=%d dmode=0x%x\n",
2189 smb_fname_str_dbg(smb_fname), fsp->fh->fd,
2190 (unsigned int)fattr));
2193 TALLOC_FREE(smb_fname);
2194 END_PROFILE(SMBcreate);
2198 /****************************************************************************
2199 Reply to a create temporary file.
2200 ****************************************************************************/
2202 void reply_ctemp(struct smb_request *req)
2204 connection_struct *conn = req->conn;
2205 struct smb_filename *smb_fname = NULL;
2213 TALLOC_CTX *ctx = talloc_tos();
2215 START_PROFILE(SMBctemp);
2218 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2222 fattr = SVAL(req->vwv+0, 0);
2223 oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
2225 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf+1,
2226 STR_TERMINATE, &status);
2227 if (!NT_STATUS_IS_OK(status)) {
2228 reply_nterror(req, status);
2232 fname = talloc_asprintf(ctx,
2236 fname = talloc_strdup(ctx, "TMXXXXXX");
2240 reply_nterror(req, NT_STATUS_NO_MEMORY);
2244 status = filename_convert(ctx, conn,
2245 req->flags2 & FLAGS2_DFS_PATHNAMES,
2250 if (!NT_STATUS_IS_OK(status)) {
2251 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2252 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2253 ERRSRV, ERRbadpath);
2256 reply_nterror(req, status);
2260 tmpfd = mkstemp(smb_fname->base_name);
2262 reply_nterror(req, map_nt_error_from_unix(errno));
2266 SMB_VFS_STAT(conn, smb_fname);
2268 /* We should fail if file does not exist. */
2269 status = SMB_VFS_CREATE_FILE(
2272 0, /* root_dir_fid */
2273 smb_fname, /* fname */
2274 FILE_GENERIC_READ | FILE_GENERIC_WRITE, /* access_mask */
2275 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
2276 FILE_OPEN, /* create_disposition*/
2277 0, /* create_options */
2278 fattr, /* file_attributes */
2279 oplock_request, /* oplock_request */
2280 0, /* allocation_size */
2281 0, /* private_flags */
2287 /* close fd from mkstemp() */
2290 if (!NT_STATUS_IS_OK(status)) {
2291 if (open_was_deferred(req->mid)) {
2292 /* We have re-scheduled this call. */
2295 reply_openerror(req, status);
2299 reply_outbuf(req, 1, 0);
2300 SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
2302 /* the returned filename is relative to the directory */
2303 s = strrchr_m(fsp->fsp_name->base_name, '/');
2305 s = fsp->fsp_name->base_name;
2311 /* Tested vs W2K3 - this doesn't seem to be here - null terminated filename is the only
2312 thing in the byte section. JRA */
2313 SSVALS(p, 0, -1); /* what is this? not in spec */
2315 if (message_push_string(&req->outbuf, s, STR_ASCII|STR_TERMINATE)
2317 reply_nterror(req, NT_STATUS_NO_MEMORY);
2321 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
2322 SCVAL(req->outbuf, smb_flg,
2323 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2326 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
2327 SCVAL(req->outbuf, smb_flg,
2328 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2331 DEBUG(2, ("reply_ctemp: created temp file %s\n", fsp_str_dbg(fsp)));
2332 DEBUG(3, ("reply_ctemp %s fd=%d umode=0%o\n", fsp_str_dbg(fsp),
2333 fsp->fh->fd, (unsigned int)smb_fname->st.st_ex_mode));
2335 TALLOC_FREE(smb_fname);
2336 END_PROFILE(SMBctemp);
2340 /*******************************************************************
2341 Check if a user is allowed to rename a file.
2342 ********************************************************************/
2344 static NTSTATUS can_rename(connection_struct *conn, files_struct *fsp,
2349 if (!CAN_WRITE(conn)) {
2350 return NT_STATUS_MEDIA_WRITE_PROTECTED;
2353 fmode = dos_mode(conn, fsp->fsp_name);
2354 if ((fmode & ~dirtype) & (aHIDDEN | aSYSTEM)) {
2355 return NT_STATUS_NO_SUCH_FILE;
2358 if (S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
2359 if (fsp->posix_open) {
2360 return NT_STATUS_OK;
2363 /* If no pathnames are open below this
2364 directory, allow the rename. */
2366 if (file_find_subpath(fsp)) {
2367 return NT_STATUS_ACCESS_DENIED;
2369 return NT_STATUS_OK;
2372 if (fsp->access_mask & (DELETE_ACCESS|FILE_WRITE_ATTRIBUTES)) {
2373 return NT_STATUS_OK;
2376 return NT_STATUS_ACCESS_DENIED;
2379 /*******************************************************************
2380 * unlink a file with all relevant access checks
2381 *******************************************************************/
2383 static NTSTATUS do_unlink(connection_struct *conn,
2384 struct smb_request *req,
2385 struct smb_filename *smb_fname,
2390 uint32 dirtype_orig = dirtype;
2393 bool posix_paths = lp_posix_pathnames();
2395 DEBUG(10,("do_unlink: %s, dirtype = %d\n",
2396 smb_fname_str_dbg(smb_fname),
2399 if (!CAN_WRITE(conn)) {
2400 return NT_STATUS_MEDIA_WRITE_PROTECTED;
2404 ret = SMB_VFS_LSTAT(conn, smb_fname);
2406 ret = SMB_VFS_STAT(conn, smb_fname);
2409 return map_nt_error_from_unix(errno);
2412 fattr = dos_mode(conn, smb_fname);
2414 if (dirtype & FILE_ATTRIBUTE_NORMAL) {
2415 dirtype = aDIR|aARCH|aRONLY;
2418 dirtype &= (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM);
2420 return NT_STATUS_NO_SUCH_FILE;
2423 if (!dir_check_ftype(conn, fattr, dirtype)) {
2425 return NT_STATUS_FILE_IS_A_DIRECTORY;
2427 return NT_STATUS_NO_SUCH_FILE;
2430 if (dirtype_orig & 0x8000) {
2431 /* These will never be set for POSIX. */
2432 return NT_STATUS_NO_SUCH_FILE;
2436 if ((fattr & dirtype) & FILE_ATTRIBUTE_DIRECTORY) {
2437 return NT_STATUS_FILE_IS_A_DIRECTORY;
2440 if ((fattr & ~dirtype) & (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)) {
2441 return NT_STATUS_NO_SUCH_FILE;
2444 if (dirtype & 0xFF00) {
2445 /* These will never be set for POSIX. */
2446 return NT_STATUS_NO_SUCH_FILE;
2451 return NT_STATUS_NO_SUCH_FILE;
2454 /* Can't delete a directory. */
2456 return NT_STATUS_FILE_IS_A_DIRECTORY;
2461 else if (dirtype & aDIR) /* Asked for a directory and it isn't. */
2462 return NT_STATUS_OBJECT_NAME_INVALID;
2463 #endif /* JRATEST */
2465 /* On open checks the open itself will check the share mode, so
2466 don't do it here as we'll get it wrong. */
2468 status = SMB_VFS_CREATE_FILE
2471 0, /* root_dir_fid */
2472 smb_fname, /* fname */
2473 DELETE_ACCESS, /* access_mask */
2474 FILE_SHARE_NONE, /* share_access */
2475 FILE_OPEN, /* create_disposition*/
2476 FILE_NON_DIRECTORY_FILE, /* create_options */
2477 /* file_attributes */
2478 posix_paths ? FILE_FLAG_POSIX_SEMANTICS|0777 :
2479 FILE_ATTRIBUTE_NORMAL,
2480 0, /* oplock_request */
2481 0, /* allocation_size */
2482 0, /* private_flags */
2488 if (!NT_STATUS_IS_OK(status)) {
2489 DEBUG(10, ("SMB_VFS_CREATEFILE failed: %s\n",
2490 nt_errstr(status)));
2494 status = can_set_delete_on_close(fsp, fattr);
2495 if (!NT_STATUS_IS_OK(status)) {
2496 DEBUG(10, ("do_unlink can_set_delete_on_close for file %s - "
2498 smb_fname_str_dbg(smb_fname),
2499 nt_errstr(status)));
2500 close_file(req, fsp, NORMAL_CLOSE);
2504 /* The set is across all open files on this dev/inode pair. */
2505 if (!set_delete_on_close(fsp, True, &conn->server_info->utok)) {
2506 close_file(req, fsp, NORMAL_CLOSE);
2507 return NT_STATUS_ACCESS_DENIED;
2510 return close_file(req, fsp, NORMAL_CLOSE);
2513 /****************************************************************************
2514 The guts of the unlink command, split out so it may be called by the NT SMB
2516 ****************************************************************************/
2518 NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
2519 uint32 dirtype, struct smb_filename *smb_fname,
2522 char *fname_dir = NULL;
2523 char *fname_mask = NULL;
2525 NTSTATUS status = NT_STATUS_OK;
2526 TALLOC_CTX *ctx = talloc_tos();
2528 /* Split up the directory from the filename/mask. */
2529 status = split_fname_dir_mask(ctx, smb_fname->base_name,
2530 &fname_dir, &fname_mask);
2531 if (!NT_STATUS_IS_OK(status)) {
2536 * We should only check the mangled cache
2537 * here if unix_convert failed. This means
2538 * that the path in 'mask' doesn't exist
2539 * on the file system and so we need to look
2540 * for a possible mangle. This patch from
2541 * Tine Smukavec <valentin.smukavec@hermes.si>.
2544 if (!VALID_STAT(smb_fname->st) &&
2545 mangle_is_mangled(fname_mask, conn->params)) {
2546 char *new_mask = NULL;
2547 mangle_lookup_name_from_8_3(ctx, fname_mask,
2548 &new_mask, conn->params);
2550 TALLOC_FREE(fname_mask);
2551 fname_mask = new_mask;
2558 * Only one file needs to be unlinked. Append the mask back
2559 * onto the directory.
2561 TALLOC_FREE(smb_fname->base_name);
2562 smb_fname->base_name = talloc_asprintf(smb_fname,
2566 if (!smb_fname->base_name) {
2567 status = NT_STATUS_NO_MEMORY;
2571 dirtype = FILE_ATTRIBUTE_NORMAL;
2574 status = check_name(conn, smb_fname->base_name);
2575 if (!NT_STATUS_IS_OK(status)) {
2579 status = do_unlink(conn, req, smb_fname, dirtype);
2580 if (!NT_STATUS_IS_OK(status)) {
2586 struct smb_Dir *dir_hnd = NULL;
2588 const char *dname = NULL;
2589 char *talloced = NULL;
2591 if ((dirtype & SAMBA_ATTRIBUTES_MASK) == aDIR) {
2592 status = NT_STATUS_OBJECT_NAME_INVALID;
2596 if (strequal(fname_mask,"????????.???")) {
2597 TALLOC_FREE(fname_mask);
2598 fname_mask = talloc_strdup(ctx, "*");
2600 status = NT_STATUS_NO_MEMORY;
2605 status = check_name(conn, fname_dir);
2606 if (!NT_STATUS_IS_OK(status)) {
2610 dir_hnd = OpenDir(talloc_tos(), conn, fname_dir, fname_mask,
2612 if (dir_hnd == NULL) {
2613 status = map_nt_error_from_unix(errno);
2617 /* XXXX the CIFS spec says that if bit0 of the flags2 field is set then
2618 the pattern matches against the long name, otherwise the short name
2619 We don't implement this yet XXXX
2622 status = NT_STATUS_NO_SUCH_FILE;
2624 while ((dname = ReadDirName(dir_hnd, &offset,
2625 &smb_fname->st, &talloced))) {
2626 TALLOC_CTX *frame = talloc_stackframe();
2628 if (!is_visible_file(conn, fname_dir, dname,
2629 &smb_fname->st, true)) {
2631 TALLOC_FREE(talloced);
2635 /* Quick check for "." and ".." */
2636 if (ISDOT(dname) || ISDOTDOT(dname)) {
2638 TALLOC_FREE(talloced);
2642 if(!mask_match(dname, fname_mask,
2643 conn->case_sensitive)) {
2645 TALLOC_FREE(talloced);
2649 TALLOC_FREE(smb_fname->base_name);
2650 smb_fname->base_name =
2651 talloc_asprintf(smb_fname, "%s/%s",
2654 if (!smb_fname->base_name) {
2655 TALLOC_FREE(dir_hnd);
2656 status = NT_STATUS_NO_MEMORY;
2658 TALLOC_FREE(talloced);
2662 status = check_name(conn, smb_fname->base_name);
2663 if (!NT_STATUS_IS_OK(status)) {
2664 TALLOC_FREE(dir_hnd);
2666 TALLOC_FREE(talloced);
2670 status = do_unlink(conn, req, smb_fname, dirtype);
2671 if (!NT_STATUS_IS_OK(status)) {
2673 TALLOC_FREE(talloced);
2678 DEBUG(3,("unlink_internals: successful unlink [%s]\n",
2679 smb_fname->base_name));
2682 TALLOC_FREE(talloced);
2684 TALLOC_FREE(dir_hnd);
2687 if (count == 0 && NT_STATUS_IS_OK(status) && errno != 0) {
2688 status = map_nt_error_from_unix(errno);
2692 TALLOC_FREE(fname_dir);
2693 TALLOC_FREE(fname_mask);
2697 /****************************************************************************
2699 ****************************************************************************/
2701 void reply_unlink(struct smb_request *req)
2703 connection_struct *conn = req->conn;
2705 struct smb_filename *smb_fname = NULL;
2708 bool path_contains_wcard = False;
2709 TALLOC_CTX *ctx = talloc_tos();
2711 START_PROFILE(SMBunlink);
2714 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2718 dirtype = SVAL(req->vwv+0, 0);
2720 srvstr_get_path_req_wcard(ctx, req, &name, (const char *)req->buf + 1,
2721 STR_TERMINATE, &status,
2722 &path_contains_wcard);
2723 if (!NT_STATUS_IS_OK(status)) {
2724 reply_nterror(req, status);
2728 status = filename_convert(ctx, conn,
2729 req->flags2 & FLAGS2_DFS_PATHNAMES,
2731 UCF_COND_ALLOW_WCARD_LCOMP,
2732 &path_contains_wcard,
2734 if (!NT_STATUS_IS_OK(status)) {
2735 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2736 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2737 ERRSRV, ERRbadpath);
2740 reply_nterror(req, status);
2744 DEBUG(3,("reply_unlink : %s\n", smb_fname_str_dbg(smb_fname)));
2746 status = unlink_internals(conn, req, dirtype, smb_fname,
2747 path_contains_wcard);
2748 if (!NT_STATUS_IS_OK(status)) {
2749 if (open_was_deferred(req->mid)) {
2750 /* We have re-scheduled this call. */
2753 reply_nterror(req, status);
2757 reply_outbuf(req, 0, 0);
2759 TALLOC_FREE(smb_fname);
2760 END_PROFILE(SMBunlink);
2764 /****************************************************************************
2766 ****************************************************************************/
2768 static void fail_readraw(void)
2770 const char *errstr = talloc_asprintf(talloc_tos(),
2771 "FAIL ! reply_readbraw: socket write fail (%s)",
2776 exit_server_cleanly(errstr);
2779 /****************************************************************************
2780 Fake (read/write) sendfile. Returns -1 on read or write fail.
2781 ****************************************************************************/
2783 static ssize_t fake_sendfile(files_struct *fsp, SMB_OFF_T startpos,
2787 size_t tosend = nread;
2794 bufsize = MIN(nread, 65536);
2796 if (!(buf = SMB_MALLOC_ARRAY(char, bufsize))) {
2800 while (tosend > 0) {
2804 if (tosend > bufsize) {
2809 ret = read_file(fsp,buf,startpos,cur_read);
2815 /* If we had a short read, fill with zeros. */
2816 if (ret < cur_read) {
2817 memset(buf + ret, '\0', cur_read - ret);
2820 if (write_data(smbd_server_fd(),buf,cur_read) != cur_read) {
2825 startpos += cur_read;
2829 return (ssize_t)nread;
2832 #if defined(WITH_SENDFILE)
2833 /****************************************************************************
2834 Deal with the case of sendfile reading less bytes from the file than
2835 requested. Fill with zeros (all we can do).
2836 ****************************************************************************/
2838 static void sendfile_short_send(files_struct *fsp,
2843 #define SHORT_SEND_BUFSIZE 1024
2844 if (nread < headersize) {
2845 DEBUG(0,("sendfile_short_send: sendfile failed to send "
2846 "header for file %s (%s). Terminating\n",
2847 fsp_str_dbg(fsp), strerror(errno)));
2848 exit_server_cleanly("sendfile_short_send failed");
2851 nread -= headersize;
2853 if (nread < smb_maxcnt) {
2854 char *buf = SMB_CALLOC_ARRAY(char, SHORT_SEND_BUFSIZE);
2856 exit_server_cleanly("sendfile_short_send: "
2860 DEBUG(0,("sendfile_short_send: filling truncated file %s "
2861 "with zeros !\n", fsp_str_dbg(fsp)));
2863 while (nread < smb_maxcnt) {
2865 * We asked for the real file size and told sendfile
2866 * to not go beyond the end of the file. But it can
2867 * happen that in between our fstat call and the
2868 * sendfile call the file was truncated. This is very
2869 * bad because we have already announced the larger
2870 * number of bytes to the client.
2872 * The best we can do now is to send 0-bytes, just as
2873 * a read from a hole in a sparse file would do.
2875 * This should happen rarely enough that I don't care
2876 * about efficiency here :-)
2880 to_write = MIN(SHORT_SEND_BUFSIZE, smb_maxcnt - nread);
2881 if (write_data(smbd_server_fd(), buf, to_write) != to_write) {
2882 exit_server_cleanly("sendfile_short_send: "
2883 "write_data failed");
2890 #endif /* defined WITH_SENDFILE */
2892 /****************************************************************************
2893 Return a readbraw error (4 bytes of zero).
2894 ****************************************************************************/
2896 static void reply_readbraw_error(void)
2903 ok = smbd_lock_socket(smbd_server_conn);
2905 exit_server_cleanly("failed to lock socket");
2907 if (write_data(smbd_server_fd(),header,4) != 4) {
2910 ok = smbd_unlock_socket(smbd_server_conn);
2912 exit_server_cleanly("failed to unlock socket");
2916 /****************************************************************************
2917 Use sendfile in readbraw.
2918 ****************************************************************************/
2920 static void send_file_readbraw(connection_struct *conn,
2921 struct smb_request *req,
2927 char *outbuf = NULL;
2930 #if defined(WITH_SENDFILE)
2932 * We can only use sendfile on a non-chained packet
2933 * but we can use on a non-oplocked file. tridge proved this
2934 * on a train in Germany :-). JRA.
2935 * reply_readbraw has already checked the length.
2938 if ( !req_is_in_chain(req) && (nread > 0) && (fsp->base_fsp == NULL) &&
2939 (fsp->wcp == NULL) &&
2940 lp_use_sendfile(SNUM(conn), smbd_server_conn->smb1.signing_state) ) {
2941 ssize_t sendfile_read = -1;
2943 DATA_BLOB header_blob;
2945 _smb_setlen(header,nread);
2946 header_blob = data_blob_const(header, 4);
2948 if ((sendfile_read = SMB_VFS_SENDFILE(smbd_server_fd(), fsp,
2949 &header_blob, startpos, nread)) == -1) {
2950 /* Returning ENOSYS means no data at all was sent.
2951 * Do this as a normal read. */
2952 if (errno == ENOSYS) {
2953 goto normal_readbraw;
2957 * Special hack for broken Linux with no working sendfile. If we
2958 * return EINTR we sent the header but not the rest of the data.
2959 * Fake this up by doing read/write calls.
2961 if (errno == EINTR) {
2962 /* Ensure we don't do this again. */
2963 set_use_sendfile(SNUM(conn), False);
2964 DEBUG(0,("send_file_readbraw: sendfile not available. Faking..\n"));
2966 if (fake_sendfile(fsp, startpos, nread) == -1) {
2967 DEBUG(0,("send_file_readbraw: "
2968 "fake_sendfile failed for "
2972 exit_server_cleanly("send_file_readbraw fake_sendfile failed");
2977 DEBUG(0,("send_file_readbraw: sendfile failed for "
2978 "file %s (%s). Terminating\n",
2979 fsp_str_dbg(fsp), strerror(errno)));
2980 exit_server_cleanly("send_file_readbraw sendfile failed");
2981 } else if (sendfile_read == 0) {
2983 * Some sendfile implementations return 0 to indicate
2984 * that there was a short read, but nothing was
2985 * actually written to the socket. In this case,
2986 * fallback to the normal read path so the header gets
2987 * the correct byte count.
2989 DEBUG(3, ("send_file_readbraw: sendfile sent zero "
2990 "bytes falling back to the normal read: "
2991 "%s\n", fsp_str_dbg(fsp)));
2992 goto normal_readbraw;
2995 /* Deal with possible short send. */
2996 if (sendfile_read != 4+nread) {
2997 sendfile_short_send(fsp, sendfile_read, 4, nread);
3005 outbuf = TALLOC_ARRAY(NULL, char, nread+4);
3007 DEBUG(0,("send_file_readbraw: TALLOC_ARRAY failed for size %u.\n",
3008 (unsigned)(nread+4)));
3009 reply_readbraw_error();
3014 ret = read_file(fsp,outbuf+4,startpos,nread);
3015 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
3024 _smb_setlen(outbuf,ret);
3025 if (write_data(smbd_server_fd(),outbuf,4+ret) != 4+ret)
3028 TALLOC_FREE(outbuf);
3031 /****************************************************************************
3032 Reply to a readbraw (core+ protocol).
3033 ****************************************************************************/
3035 void reply_readbraw(struct smb_request *req)
3037 connection_struct *conn = req->conn;
3038 ssize_t maxcount,mincount;
3042 struct lock_struct lock;
3045 START_PROFILE(SMBreadbraw);
3047 if (srv_is_signing_active(smbd_server_conn) ||
3048 is_encrypted_packet(req->inbuf)) {
3049 exit_server_cleanly("reply_readbraw: SMB signing/sealing is active - "
3050 "raw reads/writes are disallowed.");
3054 reply_readbraw_error();
3055 END_PROFILE(SMBreadbraw);
3059 if (smbd_server_conn->smb1.echo_handler.trusted_fde) {
3060 DEBUG(2,("SMBreadbraw rejected with NOT_SUPPORTED because of"
3061 "'fork echo handler = yes'\n"));
3062 reply_readbraw_error();
3063 END_PROFILE(SMBreadbraw);
3068 * Special check if an oplock break has been issued
3069 * and the readraw request croses on the wire, we must
3070 * return a zero length response here.
3073 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
3076 * We have to do a check_fsp by hand here, as
3077 * we must always return 4 zero bytes on error,
3081 if (!fsp || !conn || conn != fsp->conn ||
3082 req->vuid != fsp->vuid ||
3083 fsp->is_directory || fsp->fh->fd == -1) {
3085 * fsp could be NULL here so use the value from the packet. JRA.
3087 DEBUG(3,("reply_readbraw: fnum %d not valid "
3089 (int)SVAL(req->vwv+0, 0)));
3090 reply_readbraw_error();
3091 END_PROFILE(SMBreadbraw);
3095 /* Do a "by hand" version of CHECK_READ. */
3096 if (!(fsp->can_read ||
3097 ((req->flags2 & FLAGS2_READ_PERMIT_EXECUTE) &&
3098 (fsp->access_mask & FILE_EXECUTE)))) {
3099 DEBUG(3,("reply_readbraw: fnum %d not readable.\n",
3100 (int)SVAL(req->vwv+0, 0)));
3101 reply_readbraw_error();
3102 END_PROFILE(SMBreadbraw);
3106 flush_write_cache(fsp, READRAW_FLUSH);
3108 startpos = IVAL_TO_SMB_OFF_T(req->vwv+1, 0);
3109 if(req->wct == 10) {
3111 * This is a large offset (64 bit) read.
3113 #ifdef LARGE_SMB_OFF_T
3115 startpos |= (((SMB_OFF_T)IVAL(req->vwv+8, 0)) << 32);
3117 #else /* !LARGE_SMB_OFF_T */
3120 * Ensure we haven't been sent a >32 bit offset.
3123 if(IVAL(req->vwv+8, 0) != 0) {
3124 DEBUG(0,("reply_readbraw: large offset "
3125 "(%x << 32) used and we don't support "
3126 "64 bit offsets.\n",
3127 (unsigned int)IVAL(req->vwv+8, 0) ));
3128 reply_readbraw_error();
3129 END_PROFILE(SMBreadbraw);
3133 #endif /* LARGE_SMB_OFF_T */
3136 DEBUG(0,("reply_readbraw: negative 64 bit "
3137 "readraw offset (%.0f) !\n",
3138 (double)startpos ));
3139 reply_readbraw_error();
3140 END_PROFILE(SMBreadbraw);
3145 maxcount = (SVAL(req->vwv+3, 0) & 0xFFFF);
3146 mincount = (SVAL(req->vwv+4, 0) & 0xFFFF);
3148 /* ensure we don't overrun the packet size */
3149 maxcount = MIN(65535,maxcount);
3151 init_strict_lock_struct(fsp, (uint32)req->smbpid,
3152 (uint64_t)startpos, (uint64_t)maxcount, READ_LOCK,
3155 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
3156 reply_readbraw_error();
3157 END_PROFILE(SMBreadbraw);
3161 if (fsp_stat(fsp) == 0) {
3162 size = fsp->fsp_name->st.st_ex_size;
3165 if (startpos >= size) {
3168 nread = MIN(maxcount,(size - startpos));
3171 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
3172 if (nread < mincount)
3176 DEBUG( 3, ( "reply_readbraw: fnum=%d start=%.0f max=%lu "
3177 "min=%lu nread=%lu\n",
3178 fsp->fnum, (double)startpos,
3179 (unsigned long)maxcount,
3180 (unsigned long)mincount,
3181 (unsigned long)nread ) );
3183 send_file_readbraw(conn, req, fsp, startpos, nread, mincount);
3185 DEBUG(5,("reply_readbraw finished\n"));
3187 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
3189 END_PROFILE(SMBreadbraw);
3194 #define DBGC_CLASS DBGC_LOCKING
3196 /****************************************************************************
3197 Reply to a lockread (core+ protocol).
3198 ****************************************************************************/
3200 void reply_lockread(struct smb_request *req)
3202 connection_struct *conn = req->conn;
3209 struct byte_range_lock *br_lck = NULL;
3211 struct smbd_server_connection *sconn = smbd_server_conn;
3213 START_PROFILE(SMBlockread);
3216 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3217 END_PROFILE(SMBlockread);
3221 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
3223 if (!check_fsp(conn, req, fsp)) {
3224 END_PROFILE(SMBlockread);
3228 if (!CHECK_READ(fsp,req)) {
3229 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3230 END_PROFILE(SMBlockread);
3234 numtoread = SVAL(req->vwv+1, 0);
3235 startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
3237 numtoread = MIN(BUFFER_SIZE - (smb_size + 3*2 + 3), numtoread);
3239 reply_outbuf(req, 5, numtoread + 3);
3241 data = smb_buf(req->outbuf) + 3;
3244 * NB. Discovered by Menny Hamburger at Mainsoft. This is a core+
3245 * protocol request that predates the read/write lock concept.
3246 * Thus instead of asking for a read lock here we need to ask
3247 * for a write lock. JRA.
3248 * Note that the requested lock size is unaffected by max_recv.
3251 br_lck = do_lock(smbd_messaging_context(),
3254 (uint64_t)numtoread,
3258 False, /* Non-blocking lock. */
3262 TALLOC_FREE(br_lck);
3264 if (NT_STATUS_V(status)) {
3265 reply_nterror(req, status);
3266 END_PROFILE(SMBlockread);
3271 * However the requested READ size IS affected by max_recv. Insanity.... JRA.
3274 if (numtoread > sconn->smb1.negprot.max_recv) {
3275 DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u). \
3276 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
3277 (unsigned int)numtoread,
3278 (unsigned int)sconn->smb1.negprot.max_recv));
3279 numtoread = MIN(numtoread, sconn->smb1.negprot.max_recv);
3281 nread = read_file(fsp,data,startpos,numtoread);
3284 reply_nterror(req, map_nt_error_from_unix(errno));
3285 END_PROFILE(SMBlockread);
3289 srv_set_message((char *)req->outbuf, 5, nread+3, False);
3291 SSVAL(req->outbuf,smb_vwv0,nread);
3292 SSVAL(req->outbuf,smb_vwv5,nread+3);
3293 p = smb_buf(req->outbuf);
3294 SCVAL(p,0,0); /* pad byte. */
3297 DEBUG(3,("lockread fnum=%d num=%d nread=%d\n",
3298 fsp->fnum, (int)numtoread, (int)nread));
3300 END_PROFILE(SMBlockread);
3305 #define DBGC_CLASS DBGC_ALL
3307 /****************************************************************************
3309 ****************************************************************************/
3311 void reply_read(struct smb_request *req)
3313 connection_struct *conn = req->conn;
3320 struct lock_struct lock;
3321 struct smbd_server_connection *sconn = smbd_server_conn;
3323 START_PROFILE(SMBread);
3326 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3327 END_PROFILE(SMBread);
3331 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
3333 if (!check_fsp(conn, req, fsp)) {
3334 END_PROFILE(SMBread);
3338 if (!CHECK_READ(fsp,req)) {
3339 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3340 END_PROFILE(SMBread);
3344 numtoread = SVAL(req->vwv+1, 0);
3345 startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
3347 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
3350 * The requested read size cannot be greater than max_recv. JRA.
3352 if (numtoread > sconn->smb1.negprot.max_recv) {
3353 DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u). \
3354 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
3355 (unsigned int)numtoread,
3356 (unsigned int)sconn->smb1.negprot.max_recv));
3357 numtoread = MIN(numtoread, sconn->smb1.negprot.max_recv);
3360 reply_outbuf(req, 5, numtoread+3);
3362 data = smb_buf(req->outbuf) + 3;
3364 init_strict_lock_struct(fsp, (uint32)req->smbpid,
3365 (uint64_t)startpos, (uint64_t)numtoread, READ_LOCK,
3368 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
3369 reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
3370 END_PROFILE(SMBread);
3375 nread = read_file(fsp,data,startpos,numtoread);
3378 reply_nterror(req, map_nt_error_from_unix(errno));
3382 srv_set_message((char *)req->outbuf, 5, nread+3, False);
3384 SSVAL(req->outbuf,smb_vwv0,nread);
3385 SSVAL(req->outbuf,smb_vwv5,nread+3);
3386 SCVAL(smb_buf(req->outbuf),0,1);
3387 SSVAL(smb_buf(req->outbuf),1,nread);
3389 DEBUG( 3, ( "read fnum=%d num=%d nread=%d\n",
3390 fsp->fnum, (int)numtoread, (int)nread ) );
3393 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
3395 END_PROFILE(SMBread);
3399 /****************************************************************************
3401 ****************************************************************************/
3403 static int setup_readX_header(struct smb_request *req, char *outbuf,
3409 outsize = srv_set_message(outbuf,12,smb_maxcnt,False);
3410 data = smb_buf(outbuf);
3412 memset(outbuf+smb_vwv0,'\0',24); /* valgrind init. */
3414 SCVAL(outbuf,smb_vwv0,0xFF);
3415 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be -1. */
3416 SSVAL(outbuf,smb_vwv5,smb_maxcnt);
3417 SSVAL(outbuf,smb_vwv6,
3419 + 1 /* the wct field */
3420 + 12 * sizeof(uint16_t) /* vwv */
3421 + 2); /* the buflen field */
3422 SSVAL(outbuf,smb_vwv7,(smb_maxcnt >> 16));
3423 SSVAL(outbuf,smb_vwv11,smb_maxcnt);
3424 /* Reset the outgoing length, set_message truncates at 0x1FFFF. */
3425 _smb_setlen_large(outbuf,(smb_size + 12*2 + smb_maxcnt - 4));
3429 /****************************************************************************
3430 Reply to a read and X - possibly using sendfile.
3431 ****************************************************************************/
3433 static void send_file_readX(connection_struct *conn, struct smb_request *req,
3434 files_struct *fsp, SMB_OFF_T startpos,
3438 struct lock_struct lock;
3439 int saved_errno = 0;
3441 if(fsp_stat(fsp) == -1) {
3442 reply_nterror(req, map_nt_error_from_unix(errno));
3446 init_strict_lock_struct(fsp, (uint32)req->smbpid,
3447 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
3450 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
3451 reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
3455 if (!S_ISREG(fsp->fsp_name->st.st_ex_mode) ||
3456 (startpos > fsp->fsp_name->st.st_ex_size)
3457 || (smb_maxcnt > (fsp->fsp_name->st.st_ex_size - startpos))) {
3459 * We already know that we would do a short read, so don't
3460 * try the sendfile() path.
3462 goto nosendfile_read;
3465 if (smbd_server_conn->smb1.echo_handler.trusted_fde) {
3466 goto nosendfile_read;
3469 #if defined(WITH_SENDFILE)
3471 * We can only use sendfile on a non-chained packet
3472 * but we can use on a non-oplocked file. tridge proved this
3473 * on a train in Germany :-). JRA.
3476 if (!req_is_in_chain(req) &&
3477 !is_encrypted_packet(req->inbuf) && (fsp->base_fsp == NULL) &&
3478 (fsp->wcp == NULL) &&
3479 lp_use_sendfile(SNUM(conn), smbd_server_conn->smb1.signing_state) ) {
3480 uint8 headerbuf[smb_size + 12 * 2];
3484 * Set up the packet header before send. We
3485 * assume here the sendfile will work (get the
3486 * correct amount of data).
3489 header = data_blob_const(headerbuf, sizeof(headerbuf));
3491 construct_reply_common_req(req, (char *)headerbuf);
3492 setup_readX_header(req, (char *)headerbuf, smb_maxcnt);
3494 if ((nread = SMB_VFS_SENDFILE(smbd_server_fd(), fsp, &header, startpos, smb_maxcnt)) == -1) {
3495 /* Returning ENOSYS means no data at all was sent.
3496 Do this as a normal read. */
3497 if (errno == ENOSYS) {
3502 * Special hack for broken Linux with no working sendfile. If we
3503 * return EINTR we sent the header but not the rest of the data.
3504 * Fake this up by doing read/write calls.
3507 if (errno == EINTR) {
3508 /* Ensure we don't do this again. */
3509 set_use_sendfile(SNUM(conn), False);
3510 DEBUG(0,("send_file_readX: sendfile not available. Faking..\n"));
3511 nread = fake_sendfile(fsp, startpos,
3514 DEBUG(0,("send_file_readX: "
3515 "fake_sendfile failed for "
3519 exit_server_cleanly("send_file_readX: fake_sendfile failed");
3521 DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
3522 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
3523 /* No outbuf here means successful sendfile. */
3527 DEBUG(0,("send_file_readX: sendfile failed for file "
3528 "%s (%s). Terminating\n", fsp_str_dbg(fsp),
3530 exit_server_cleanly("send_file_readX sendfile failed");
3531 } else if (nread == 0) {
3533 * Some sendfile implementations return 0 to indicate
3534 * that there was a short read, but nothing was
3535 * actually written to the socket. In this case,
3536 * fallback to the normal read path so the header gets
3537 * the correct byte count.
3539 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
3540 "falling back to the normal read: %s\n",
3545 DEBUG( 3, ( "send_file_readX: sendfile fnum=%d max=%d nread=%d\n",
3546 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
3548 /* Deal with possible short send. */
3549 if (nread != smb_maxcnt + sizeof(headerbuf)) {
3550 sendfile_short_send(fsp, nread, sizeof(headerbuf), smb_maxcnt);
3552 /* No outbuf here means successful sendfile. */
3553 SMB_PERFCOUNT_SET_MSGLEN_OUT(&req->pcd, nread);
3554 SMB_PERFCOUNT_END(&req->pcd);
3562 if ((smb_maxcnt & 0xFF0000) > 0x10000) {
3563 uint8 headerbuf[smb_size + 2*12];
3565 construct_reply_common_req(req, (char *)headerbuf);
3566 setup_readX_header(req, (char *)headerbuf, smb_maxcnt);
3568 /* Send out the header. */
3569 if (write_data(smbd_server_fd(), (char *)headerbuf,
3570 sizeof(headerbuf)) != sizeof(headerbuf)) {
3571 DEBUG(0,("send_file_readX: write_data failed for file "
3572 "%s (%s). Terminating\n", fsp_str_dbg(fsp),
3574 exit_server_cleanly("send_file_readX sendfile failed");
3576 nread = fake_sendfile(fsp, startpos, smb_maxcnt);
3578 DEBUG(0,("send_file_readX: fake_sendfile failed for "
3579 "file %s (%s).\n", fsp_str_dbg(fsp),
3581 exit_server_cleanly("send_file_readX: fake_sendfile failed");
3588 reply_outbuf(req, 12, smb_maxcnt);
3590 nread = read_file(fsp, smb_buf(req->outbuf), startpos, smb_maxcnt);
3591 saved_errno = errno;
3593 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
3596 reply_nterror(req, map_nt_error_from_unix(saved_errno));
3600 setup_readX_header(req, (char *)req->outbuf, nread);
3602 DEBUG( 3, ( "send_file_readX fnum=%d max=%d nread=%d\n",
3603 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
3609 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
3610 TALLOC_FREE(req->outbuf);
3614 /****************************************************************************
3615 Reply to a read and X.
3616 ****************************************************************************/
3618 void reply_read_and_X(struct smb_request *req)
3620 connection_struct *conn = req->conn;
3624 bool big_readX = False;
3626 size_t smb_mincnt = SVAL(req->vwv+6, 0);
3629 START_PROFILE(SMBreadX);
3631 if ((req->wct != 10) && (req->wct != 12)) {
3632 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3636 fsp = file_fsp(req, SVAL(req->vwv+2, 0));
3637 startpos = IVAL_TO_SMB_OFF_T(req->vwv+3, 0);
3638 smb_maxcnt = SVAL(req->vwv+5, 0);
3640 /* If it's an IPC, pass off the pipe handler. */
3642 reply_pipe_read_and_X(req);
3643 END_PROFILE(SMBreadX);
3647 if (!check_fsp(conn, req, fsp)) {
3648 END_PROFILE(SMBreadX);
3652 if (!CHECK_READ(fsp,req)) {
3653 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3654 END_PROFILE(SMBreadX);
3658 if (global_client_caps & CAP_LARGE_READX) {
3659 size_t upper_size = SVAL(req->vwv+7, 0);
3660 smb_maxcnt |= (upper_size<<16);
3661 if (upper_size > 1) {
3662 /* Can't do this on a chained packet. */
3663 if ((CVAL(req->vwv+0, 0) != 0xFF)) {
3664 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
3665 END_PROFILE(SMBreadX);
3668 /* We currently don't do this on signed or sealed data. */
3669 if (srv_is_signing_active(smbd_server_conn) ||
3670 is_encrypted_packet(req->inbuf)) {
3671 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
3672 END_PROFILE(SMBreadX);
3675 /* Is there room in the reply for this data ? */
3676 if (smb_maxcnt > (0xFFFFFF - (smb_size -4 + 12*2))) {
3678 NT_STATUS_INVALID_PARAMETER);
3679 END_PROFILE(SMBreadX);
3686 if (req->wct == 12) {
3687 #ifdef LARGE_SMB_OFF_T
3689 * This is a large offset (64 bit) read.
3691 startpos |= (((SMB_OFF_T)IVAL(req->vwv+10, 0)) << 32);
3693 #else /* !LARGE_SMB_OFF_T */
3696 * Ensure we haven't been sent a >32 bit offset.
3699 if(IVAL(req->vwv+10, 0) != 0) {
3700 DEBUG(0,("reply_read_and_X - large offset (%x << 32) "
3701 "used and we don't support 64 bit offsets.\n",
3702 (unsigned int)IVAL(req->vwv+10, 0) ));
3703 END_PROFILE(SMBreadX);
3704 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3708 #endif /* LARGE_SMB_OFF_T */
3713 schedule_aio_read_and_X(conn, req, fsp, startpos, smb_maxcnt)) {
3717 send_file_readX(conn, req, fsp, startpos, smb_maxcnt);
3720 END_PROFILE(SMBreadX);
3724 /****************************************************************************
3725 Error replies to writebraw must have smb_wct == 1. Fix this up.
3726 ****************************************************************************/
3728 void error_to_writebrawerr(struct smb_request *req)
3730 uint8 *old_outbuf = req->outbuf;
3732 reply_outbuf(req, 1, 0);
3734 memcpy(req->outbuf, old_outbuf, smb_size);
3735 TALLOC_FREE(old_outbuf);
3738 /****************************************************************************
3739 Reply to a writebraw (core+ or LANMAN1.0 protocol).
3740 ****************************************************************************/
3742 void reply_writebraw(struct smb_request *req)
3744 connection_struct *conn = req->conn;
3747 ssize_t total_written=0;
3748 size_t numtowrite=0;
3754 struct lock_struct lock;
3757 START_PROFILE(SMBwritebraw);
3760 * If we ever reply with an error, it must have the SMB command
3761 * type of SMBwritec, not SMBwriteBraw, as this tells the client
3764 SCVAL(req->inbuf,smb_com,SMBwritec);
3766 if (srv_is_signing_active(smbd_server_conn)) {
3767 END_PROFILE(SMBwritebraw);
3768 exit_server_cleanly("reply_writebraw: SMB signing is active - "
3769 "raw reads/writes are disallowed.");
3772 if (req->wct < 12) {
3773 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3774 error_to_writebrawerr(req);
3775 END_PROFILE(SMBwritebraw);
3779 if (smbd_server_conn->smb1.echo_handler.trusted_fde) {
3780 DEBUG(2,("SMBwritebraw rejected with NOT_SUPPORTED because of"
3781 "'fork echo handler = yes'\n"));
3782 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
3783 error_to_writebrawerr(req);
3784 END_PROFILE(SMBwritebraw);
3788 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
3789 if (!check_fsp(conn, req, fsp)) {
3790 error_to_writebrawerr(req);
3791 END_PROFILE(SMBwritebraw);
3795 if (!CHECK_WRITE(fsp)) {
3796 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3797 error_to_writebrawerr(req);
3798 END_PROFILE(SMBwritebraw);
3802 tcount = IVAL(req->vwv+1, 0);
3803 startpos = IVAL_TO_SMB_OFF_T(req->vwv+3, 0);
3804 write_through = BITSETW(req->vwv+7,0);
3806 /* We have to deal with slightly different formats depending
3807 on whether we are using the core+ or lanman1.0 protocol */
3809 if(get_Protocol() <= PROTOCOL_COREPLUS) {
3810 numtowrite = SVAL(smb_buf(req->inbuf),-2);
3811 data = smb_buf(req->inbuf);
3813 numtowrite = SVAL(req->vwv+10, 0);
3814 data = smb_base(req->inbuf) + SVAL(req->vwv+11, 0);
3817 /* Ensure we don't write bytes past the end of this packet. */
3818 if (data + numtowrite > smb_base(req->inbuf) + smb_len(req->inbuf)) {
3819 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3820 error_to_writebrawerr(req);
3821 END_PROFILE(SMBwritebraw);
3825 if (!fsp->print_file) {
3826 init_strict_lock_struct(fsp, (uint32)req->smbpid,
3827 (uint64_t)startpos, (uint64_t)tcount, WRITE_LOCK,
3830 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
3831 reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
3832 error_to_writebrawerr(req);
3833 END_PROFILE(SMBwritebraw);
3839 nwritten = write_file(req,fsp,data,startpos,numtowrite);
3842 DEBUG(3,("reply_writebraw: initial write fnum=%d start=%.0f num=%d "
3843 "wrote=%d sync=%d\n",
3844 fsp->fnum, (double)startpos, (int)numtowrite,
3845 (int)nwritten, (int)write_through));
3847 if (nwritten < (ssize_t)numtowrite) {
3848 reply_nterror(req, NT_STATUS_DISK_FULL);
3849 error_to_writebrawerr(req);
3853 total_written = nwritten;
3855 /* Allocate a buffer of 64k + length. */
3856 buf = TALLOC_ARRAY(NULL, char, 65540);
3858 reply_nterror(req, NT_STATUS_NO_MEMORY);
3859 error_to_writebrawerr(req);
3863 /* Return a SMBwritebraw message to the redirector to tell
3864 * it to send more bytes */
3866 memcpy(buf, req->inbuf, smb_size);
3867 srv_set_message(buf,get_Protocol()>PROTOCOL_COREPLUS?1:0,0,True);
3868 SCVAL(buf,smb_com,SMBwritebraw);
3869 SSVALS(buf,smb_vwv0,0xFFFF);
3871 if (!srv_send_smb(smbd_server_fd(),
3873 false, 0, /* no signing */
3874 IS_CONN_ENCRYPTED(conn),
3876 exit_server_cleanly("reply_writebraw: srv_send_smb "
3880 /* Now read the raw data into the buffer and write it */
3881 status = read_smb_length(smbd_server_fd(), buf, SMB_SECONDARY_WAIT,
3883 if (!NT_STATUS_IS_OK(status)) {
3884 exit_server_cleanly("secondary writebraw failed");
3887 /* Set up outbuf to return the correct size */
3888 reply_outbuf(req, 1, 0);
3890 if (numtowrite != 0) {
3892 if (numtowrite > 0xFFFF) {
3893 DEBUG(0,("reply_writebraw: Oversize secondary write "
3894 "raw requested (%u). Terminating\n",
3895 (unsigned int)numtowrite ));
3896 exit_server_cleanly("secondary writebraw failed");
3899 if (tcount > nwritten+numtowrite) {
3900 DEBUG(3,("reply_writebraw: Client overestimated the "
3902 (int)tcount,(int)nwritten,(int)numtowrite));
3905 status = read_data(smbd_server_fd(), buf+4, numtowrite);
3907 if (!NT_STATUS_IS_OK(status)) {
3908 DEBUG(0,("reply_writebraw: Oversize secondary write "
3909 "raw read failed (%s). Terminating\n",
3910 nt_errstr(status)));
3911 exit_server_cleanly("secondary writebraw failed");
3914 nwritten = write_file(req,fsp,buf+4,startpos+nwritten,numtowrite);
3915 if (nwritten == -1) {
3917 reply_nterror(req, map_nt_error_from_unix(errno));
3918 error_to_writebrawerr(req);
3922 if (nwritten < (ssize_t)numtowrite) {
3923 SCVAL(req->outbuf,smb_rcls,ERRHRD);
3924 SSVAL(req->outbuf,smb_err,ERRdiskfull);
3928 total_written += nwritten;
3933 SSVAL(req->outbuf,smb_vwv0,total_written);
3935 status = sync_file(conn, fsp, write_through);
3936 if (!NT_STATUS_IS_OK(status)) {
3937 DEBUG(5,("reply_writebraw: sync_file for %s returned %s\n",
3938 fsp_str_dbg(fsp), nt_errstr(status)));
3939 reply_nterror(req, status);
3940 error_to_writebrawerr(req);
3944 DEBUG(3,("reply_writebraw: secondart write fnum=%d start=%.0f num=%d "
3946 fsp->fnum, (double)startpos, (int)numtowrite,
3947 (int)total_written));
3949 if (!fsp->print_file) {
3950 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
3953 /* We won't return a status if write through is not selected - this
3954 * follows what WfWg does */
3955 END_PROFILE(SMBwritebraw);
3957 if (!write_through && total_written==tcount) {
3959 #if RABBIT_PELLET_FIX
3961 * Fix for "rabbit pellet" mode, trigger an early TCP ack by
3962 * sending a SMBkeepalive. Thanks to DaveCB at Sun for this.
3965 if (!send_keepalive(smbd_server_fd())) {
3966 exit_server_cleanly("reply_writebraw: send of "
3967 "keepalive failed");
3970 TALLOC_FREE(req->outbuf);
3975 if (!fsp->print_file) {
3976 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
3979 END_PROFILE(SMBwritebraw);
3984 #define DBGC_CLASS DBGC_LOCKING
3986 /****************************************************************************
3987 Reply to a writeunlock (core+).
3988 ****************************************************************************/
3990 void reply_writeunlock(struct smb_request *req)
3992 connection_struct *conn = req->conn;
3993 ssize_t nwritten = -1;
3997 NTSTATUS status = NT_STATUS_OK;
3999 struct lock_struct lock;
4000 int saved_errno = 0;
4002 START_PROFILE(SMBwriteunlock);
4005 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4006 END_PROFILE(SMBwriteunlock);
4010 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
4012 if (!check_fsp(conn, req, fsp)) {
4013 END_PROFILE(SMBwriteunlock);
4017 if (!CHECK_WRITE(fsp)) {
4018 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
4019 END_PROFILE(SMBwriteunlock);
4023 numtowrite = SVAL(req->vwv+1, 0);
4024 startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
4025 data = (const char *)req->buf + 3;
4027 if (numtowrite && !fsp->print_file) {
4028 init_strict_lock_struct(fsp, (uint32)req->smbpid,
4029 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
4032 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
4033 reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
4034 END_PROFILE(SMBwriteunlock);
4039 /* The special X/Open SMB protocol handling of
4040 zero length writes is *NOT* done for
4042 if(numtowrite == 0) {
4045 nwritten = write_file(req,fsp,data,startpos,numtowrite);
4046 saved_errno = errno;
4049 status = sync_file(conn, fsp, False /* write through */);
4050 if (!NT_STATUS_IS_OK(status)) {
4051 DEBUG(5,("reply_writeunlock: sync_file for %s returned %s\n",
4052 fsp_str_dbg(fsp), nt_errstr(status)));
4053 reply_nterror(req, status);
4058 reply_nterror(req, map_nt_error_from_unix(saved_errno));
4062 if((nwritten < numtowrite) && (numtowrite != 0)) {
4063 reply_nterror(req, NT_STATUS_DISK_FULL);
4067 if (numtowrite && !fsp->print_file) {
4068 status = do_unlock(smbd_messaging_context(),
4071 (uint64_t)numtowrite,
4075 if (NT_STATUS_V(status)) {
4076 reply_nterror(req, status);
4081 reply_outbuf(req, 1, 0);
4083 SSVAL(req->outbuf,smb_vwv0,nwritten);
4085 DEBUG(3,("writeunlock fnum=%d num=%d wrote=%d\n",
4086 fsp->fnum, (int)numtowrite, (int)nwritten));
4089 if (numtowrite && !fsp->print_file) {
4090 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
4093 END_PROFILE(SMBwriteunlock);
4098 #define DBGC_CLASS DBGC_ALL
4100 /****************************************************************************
4102 ****************************************************************************/
4104 void reply_write(struct smb_request *req)
4106 connection_struct *conn = req->conn;
4108 ssize_t nwritten = -1;
4112 struct lock_struct lock;
4114 int saved_errno = 0;
4116 START_PROFILE(SMBwrite);
4119 END_PROFILE(SMBwrite);
4120 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4124 /* If it's an IPC, pass off the pipe handler. */
4126 reply_pipe_write(req);
4127 END_PROFILE(SMBwrite);
4131 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
4133 if (!check_fsp(conn, req, fsp)) {
4134 END_PROFILE(SMBwrite);
4138 if (!CHECK_WRITE(fsp)) {
4139 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
4140 END_PROFILE(SMBwrite);
4144 numtowrite = SVAL(req->vwv+1, 0);
4145 startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
4146 data = (const char *)req->buf + 3;
4148 if (!fsp->print_file) {
4149 init_strict_lock_struct(fsp, (uint32)req->smbpid,
4150 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
4153 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
4154 reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
4155 END_PROFILE(SMBwrite);
4161 * X/Open SMB protocol says that if smb_vwv1 is
4162 * zero then the file size should be extended or
4163 * truncated to the size given in smb_vwv[2-3].
4166 if(numtowrite == 0) {
4168 * This is actually an allocate call, and set EOF. JRA.
4170 nwritten = vfs_allocate_file_space(fsp, (SMB_OFF_T)startpos);
4172 reply_nterror(req, NT_STATUS_DISK_FULL);
4175 nwritten = vfs_set_filelen(fsp, (SMB_OFF_T)startpos);
4177 reply_nterror(req, NT_STATUS_DISK_FULL);
4180 trigger_write_time_update_immediate(fsp);
4182 nwritten = write_file(req,fsp,data,startpos,numtowrite);
4185 status = sync_file(conn, fsp, False);
4186 if (!NT_STATUS_IS_OK(status)) {
4187 DEBUG(5,("reply_write: sync_file for %s returned %s\n",
4188 fsp_str_dbg(fsp), nt_errstr(status)));
4189 reply_nterror(req, status);
4194 reply_nterror(req, map_nt_error_from_unix(saved_errno));
4198 if((nwritten == 0) && (numtowrite != 0)) {
4199 reply_nterror(req, NT_STATUS_DISK_FULL);
4203 reply_outbuf(req, 1, 0);
4205 SSVAL(req->outbuf,smb_vwv0,nwritten);
4207 if (nwritten < (ssize_t)numtowrite) {
4208 SCVAL(req->outbuf,smb_rcls,ERRHRD);
4209 SSVAL(req->outbuf,smb_err,ERRdiskfull);
4212 DEBUG(3,("write fnum=%d num=%d wrote=%d\n", fsp->fnum, (int)numtowrite, (int)nwritten));
4215 if (!fsp->print_file) {
4216 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
4219 END_PROFILE(SMBwrite);
4223 /****************************************************************************
4224 Ensure a buffer is a valid writeX for recvfile purposes.
4225 ****************************************************************************/
4227 #define STANDARD_WRITE_AND_X_HEADER_SIZE (smb_size - 4 + /* basic header */ \
4228 (2*14) + /* word count (including bcc) */ \
4231 bool is_valid_writeX_buffer(const uint8_t *inbuf)
4234 connection_struct *conn = NULL;
4235 unsigned int doff = 0;
4236 size_t len = smb_len_large(inbuf);
4237 struct smbd_server_connection *sconn = smbd_server_conn;
4239 if (is_encrypted_packet(inbuf)) {
4240 /* Can't do this on encrypted
4245 if (CVAL(inbuf,smb_com) != SMBwriteX) {
4249 if (CVAL(inbuf,smb_vwv0) != 0xFF ||
4250 CVAL(inbuf,smb_wct) != 14) {
4251 DEBUG(10,("is_valid_writeX_buffer: chained or "
4252 "invalid word length.\n"));
4256 conn = conn_find(sconn, SVAL(inbuf, smb_tid));
4258 DEBUG(10,("is_valid_writeX_buffer: bad tid\n"));
4262 DEBUG(10,("is_valid_writeX_buffer: IPC$ tid\n"));
4265 if (IS_PRINT(conn)) {
4266 DEBUG(10,("is_valid_writeX_buffer: printing tid\n"));
4269 doff = SVAL(inbuf,smb_vwv11);
4271 numtowrite = SVAL(inbuf,smb_vwv10);
4273 if (len > doff && len - doff > 0xFFFF) {
4274 numtowrite |= (((size_t)SVAL(inbuf,smb_vwv9))<<16);
4277 if (numtowrite == 0) {
4278 DEBUG(10,("is_valid_writeX_buffer: zero write\n"));
4282 /* Ensure the sizes match up. */
4283 if (doff < STANDARD_WRITE_AND_X_HEADER_SIZE) {
4284 /* no pad byte...old smbclient :-( */
4285 DEBUG(10,("is_valid_writeX_buffer: small doff %u (min %u)\n",
4287 (unsigned int)STANDARD_WRITE_AND_X_HEADER_SIZE));
4291 if (len - doff != numtowrite) {
4292 DEBUG(10,("is_valid_writeX_buffer: doff mismatch "
4293 "len = %u, doff = %u, numtowrite = %u\n",
4296 (unsigned int)numtowrite ));
4300 DEBUG(10,("is_valid_writeX_buffer: true "
4301 "len = %u, doff = %u, numtowrite = %u\n",
4304 (unsigned int)numtowrite ));
4309 /****************************************************************************
4310 Reply to a write and X.
4311 ****************************************************************************/
4313 void reply_write_and_X(struct smb_request *req)
4315 connection_struct *conn = req->conn;
4317 struct lock_struct lock;
4322 unsigned int smb_doff;
4323 unsigned int smblen;
4327 START_PROFILE(SMBwriteX);
4329 if ((req->wct != 12) && (req->wct != 14)) {
4330 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4331 END_PROFILE(SMBwriteX);
4335 numtowrite = SVAL(req->vwv+10, 0);
4336 smb_doff = SVAL(req->vwv+11, 0);
4337 smblen = smb_len(req->inbuf);
4339 if (req->unread_bytes > 0xFFFF ||
4340 (smblen > smb_doff &&
4341 smblen - smb_doff > 0xFFFF)) {
4342 numtowrite |= (((size_t)SVAL(req->vwv+9, 0))<<16);
4345 if (req->unread_bytes) {
4346 /* Can't do a recvfile write on IPC$ */
4348 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4349 END_PROFILE(SMBwriteX);
4352 if (numtowrite != req->unread_bytes) {
4353 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4354 END_PROFILE(SMBwriteX);
4358 if (smb_doff > smblen || smb_doff + numtowrite < numtowrite ||
4359 smb_doff + numtowrite > smblen) {
4360 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4361 END_PROFILE(SMBwriteX);
4366 /* If it's an IPC, pass off the pipe handler. */
4368 if (req->unread_bytes) {
4369 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4370 END_PROFILE(SMBwriteX);
4373 reply_pipe_write_and_X(req);
4374 END_PROFILE(SMBwriteX);
4378 fsp = file_fsp(req, SVAL(req->vwv+2, 0));
4379 startpos = IVAL_TO_SMB_OFF_T(req->vwv+3, 0);
4380 write_through = BITSETW(req->vwv+7,0);
4382 if (!check_fsp(conn, req, fsp)) {
4383 END_PROFILE(SMBwriteX);
4387 if (!CHECK_WRITE(fsp)) {
4388 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
4389 END_PROFILE(SMBwriteX);
4393 data = smb_base(req->inbuf) + smb_doff;
4395 if(req->wct == 14) {
4396 #ifdef LARGE_SMB_OFF_T
4398 * This is a large offset (64 bit) write.
4400 startpos |= (((SMB_OFF_T)IVAL(req->vwv+12, 0)) << 32);
4402 #else /* !LARGE_SMB_OFF_T */
4405 * Ensure we haven't been sent a >32 bit offset.
4408 if(IVAL(req->vwv+12, 0) != 0) {
4409 DEBUG(0,("reply_write_and_X - large offset (%x << 32) "
4410 "used and we don't support 64 bit offsets.\n",
4411 (unsigned int)IVAL(req->vwv+12, 0) ));
4412 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
4413 END_PROFILE(SMBwriteX);
4417 #endif /* LARGE_SMB_OFF_T */
4420 init_strict_lock_struct(fsp, (uint32)req->smbpid,
4421 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
4424 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
4425 reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
4426 END_PROFILE(SMBwriteX);
4430 /* X/Open SMB protocol says that, unlike SMBwrite
4431 if the length is zero then NO truncation is
4432 done, just a write of zero. To truncate a file,
4435 if(numtowrite == 0) {
4439 if ((req->unread_bytes == 0) &&
4440 schedule_aio_write_and_X(conn, req, fsp, data, startpos,
4445 nwritten = write_file(req,fsp,data,startpos,numtowrite);
4449 reply_nterror(req, map_nt_error_from_unix(errno));
4453 if((nwritten == 0) && (numtowrite != 0)) {
4454 reply_nterror(req, NT_STATUS_DISK_FULL);
4458 reply_outbuf(req, 6, 0);
4459 SSVAL(req->outbuf,smb_vwv2,nwritten);
4460 SSVAL(req->outbuf,smb_vwv4,nwritten>>16);
4462 if (nwritten < (ssize_t)numtowrite) {
4463 SCVAL(req->outbuf,smb_rcls,ERRHRD);
4464 SSVAL(req->outbuf,smb_err,ERRdiskfull);
4467 DEBUG(3,("writeX fnum=%d num=%d wrote=%d\n",
4468 fsp->fnum, (int)numtowrite, (int)nwritten));
4470 status = sync_file(conn, fsp, write_through);
4471 if (!NT_STATUS_IS_OK(status)) {
4472 DEBUG(5,("reply_write_and_X: sync_file for %s returned %s\n",
4473 fsp_str_dbg(fsp), nt_errstr(status)));
4474 reply_nterror(req, status);
4478 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
4480 END_PROFILE(SMBwriteX);
4485 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
4487 END_PROFILE(SMBwriteX);
4491 /****************************************************************************
4493 ****************************************************************************/
4495 void reply_lseek(struct smb_request *req)
4497 connection_struct *conn = req->conn;
4503 START_PROFILE(SMBlseek);
4506 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4507 END_PROFILE(SMBlseek);
4511 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
4513 if (!check_fsp(conn, req, fsp)) {
4517 flush_write_cache(fsp, SEEK_FLUSH);
4519 mode = SVAL(req->vwv+1, 0) & 3;
4520 /* NB. This doesn't use IVAL_TO_SMB_OFF_T as startpos can be signed in this case. */
4521 startpos = (SMB_OFF_T)IVALS(req->vwv+2, 0);
4530 res = fsp->fh->pos + startpos;
4541 if (umode == SEEK_END) {
4542 if((res = SMB_VFS_LSEEK(fsp,startpos,umode)) == -1) {
4543 if(errno == EINVAL) {
4544 SMB_OFF_T current_pos = startpos;
4546 if(fsp_stat(fsp) == -1) {
4548 map_nt_error_from_unix(errno));
4549 END_PROFILE(SMBlseek);
4553 current_pos += fsp->fsp_name->st.st_ex_size;
4555 res = SMB_VFS_LSEEK(fsp,0,SEEK_SET);
4560 reply_nterror(req, map_nt_error_from_unix(errno));
4561 END_PROFILE(SMBlseek);
4568 reply_outbuf(req, 2, 0);
4569 SIVAL(req->outbuf,smb_vwv0,res);
4571 DEBUG(3,("lseek fnum=%d ofs=%.0f newpos = %.0f mode=%d\n",
4572 fsp->fnum, (double)startpos, (double)res, mode));
4574 END_PROFILE(SMBlseek);
4578 /****************************************************************************
4580 ****************************************************************************/
4582 void reply_flush(struct smb_request *req)
4584 connection_struct *conn = req->conn;
4588 START_PROFILE(SMBflush);
4591 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4595 fnum = SVAL(req->vwv+0, 0);
4596 fsp = file_fsp(req, fnum);
4598 if ((fnum != 0xFFFF) && !check_fsp(conn, req, fsp)) {
4603 file_sync_all(conn);
4605 NTSTATUS status = sync_file(conn, fsp, True);
4606 if (!NT_STATUS_IS_OK(status)) {
4607 DEBUG(5,("reply_flush: sync_file for %s returned %s\n",
4608 fsp_str_dbg(fsp), nt_errstr(status)));
4609 reply_nterror(req, status);
4610 END_PROFILE(SMBflush);
4615 reply_outbuf(req, 0, 0);
4617 DEBUG(3,("flush\n"));
4618 END_PROFILE(SMBflush);
4622 /****************************************************************************
4624 conn POINTER CAN BE NULL HERE !
4625 ****************************************************************************/
4627 void reply_exit(struct smb_request *req)
4629 START_PROFILE(SMBexit);
4631 file_close_pid(req->smbpid, req->vuid);
4633 reply_outbuf(req, 0, 0);
4635 DEBUG(3,("exit\n"));
4637 END_PROFILE(SMBexit);
4641 /****************************************************************************
4642 Reply to a close - has to deal with closing a directory opened by NT SMB's.
4643 ****************************************************************************/
4645 void reply_close(struct smb_request *req)
4647 connection_struct *conn = req->conn;
4648 NTSTATUS status = NT_STATUS_OK;
4649 files_struct *fsp = NULL;
4650 START_PROFILE(SMBclose);
4653 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4654 END_PROFILE(SMBclose);
4658 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
4661 * We can only use check_fsp if we know it's not a directory.
4664 if(!fsp || (fsp->conn != conn) || (fsp->vuid != req->vuid)) {
4665 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
4666 END_PROFILE(SMBclose);
4670 if(fsp->is_directory) {
4672 * Special case - close NT SMB directory handle.
4674 DEBUG(3,("close directory fnum=%d\n", fsp->fnum));
4675 status = close_file(req, fsp, NORMAL_CLOSE);
4679 * Close ordinary file.
4682 DEBUG(3,("close fd=%d fnum=%d (numopen=%d)\n",
4683 fsp->fh->fd, fsp->fnum,
4684 conn->num_files_open));
4687 * Take care of any time sent in the close.
4690 t = srv_make_unix_date3(req->vwv+1);
4691 set_close_write_time(fsp, convert_time_t_to_timespec(t));
4694 * close_file() returns the unix errno if an error
4695 * was detected on close - normally this is due to
4696 * a disk full error. If not then it was probably an I/O error.
4699 status = close_file(req, fsp, NORMAL_CLOSE);
4702 if (!NT_STATUS_IS_OK(status)) {
4703 reply_nterror(req, status);
4704 END_PROFILE(SMBclose);
4708 reply_outbuf(req, 0, 0);
4709 END_PROFILE(SMBclose);
4713 /****************************************************************************
4714 Reply to a writeclose (Core+ protocol).
4715 ****************************************************************************/
4717 void reply_writeclose(struct smb_request *req)
4719 connection_struct *conn = req->conn;
4721 ssize_t nwritten = -1;
4722 NTSTATUS close_status = NT_STATUS_OK;
4725 struct timespec mtime;
4727 struct lock_struct lock;
4729 START_PROFILE(SMBwriteclose);
4732 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4733 END_PROFILE(SMBwriteclose);
4737 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
4739 if (!check_fsp(conn, req, fsp)) {
4740 END_PROFILE(SMBwriteclose);
4743 if (!CHECK_WRITE(fsp)) {
4744 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
4745 END_PROFILE(SMBwriteclose);
4749 numtowrite = SVAL(req->vwv+1, 0);
4750 startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
4751 mtime = convert_time_t_to_timespec(srv_make_unix_date3(req->vwv+4));
4752 data = (const char *)req->buf + 1;
4754 if (numtowrite && !fsp->print_file) {
4755 init_strict_lock_struct(fsp, (uint32)req->smbpid,
4756 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
4759 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
4760 reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
4761 END_PROFILE(SMBwriteclose);
4766 nwritten = write_file(req,fsp,data,startpos,numtowrite);
4768 set_close_write_time(fsp, mtime);
4771 * More insanity. W2K only closes the file if writelen > 0.
4776 DEBUG(3,("reply_writeclose: zero length write doesn't close "
4777 "file %s\n", fsp_str_dbg(fsp)));
4778 close_status = close_file(req, fsp, NORMAL_CLOSE);
4781 DEBUG(3,("writeclose fnum=%d num=%d wrote=%d (numopen=%d)\n",
4782 fsp->fnum, (int)numtowrite, (int)nwritten,
4783 conn->num_files_open));
4785 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
4786 reply_nterror(req, NT_STATUS_DISK_FULL);
4790 if(!NT_STATUS_IS_OK(close_status)) {
4791 reply_nterror(req, close_status);
4795 reply_outbuf(req, 1, 0);
4797 SSVAL(req->outbuf,smb_vwv0,nwritten);
4800 if (numtowrite && !fsp->print_file) {
4801 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
4804 END_PROFILE(SMBwriteclose);
4809 #define DBGC_CLASS DBGC_LOCKING
4811 /****************************************************************************
4813 ****************************************************************************/
4815 void reply_lock(struct smb_request *req)
4817 connection_struct *conn = req->conn;
4818 uint64_t count,offset;
4821 struct byte_range_lock *br_lck = NULL;
4823 START_PROFILE(SMBlock);
4826 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4827 END_PROFILE(SMBlock);
4831 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
4833 if (!check_fsp(conn, req, fsp)) {
4834 END_PROFILE(SMBlock);
4838 count = (uint64_t)IVAL(req->vwv+1, 0);
4839 offset = (uint64_t)IVAL(req->vwv+3, 0);
4841 DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
4842 fsp->fh->fd, fsp->fnum, (double)offset, (double)count));
4844 br_lck = do_lock(smbd_messaging_context(),
4851 False, /* Non-blocking lock. */
4856 TALLOC_FREE(br_lck);
4858 if (NT_STATUS_V(status)) {
4859 reply_nterror(req, status);
4860 END_PROFILE(SMBlock);
4864 reply_outbuf(req, 0, 0);
4866 END_PROFILE(SMBlock);
4870 /****************************************************************************
4872 ****************************************************************************/
4874 void reply_unlock(struct smb_request *req)
4876 connection_struct *conn = req->conn;
4877 uint64_t count,offset;
4881 START_PROFILE(SMBunlock);
4884 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4885 END_PROFILE(SMBunlock);
4889 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
4891 if (!check_fsp(conn, req, fsp)) {
4892 END_PROFILE(SMBunlock);
4896 count = (uint64_t)IVAL(req->vwv+1, 0);
4897 offset = (uint64_t)IVAL(req->vwv+3, 0);
4899 status = do_unlock(smbd_messaging_context(),
4906 if (NT_STATUS_V(status)) {
4907 reply_nterror(req, status);
4908 END_PROFILE(SMBunlock);
4912 DEBUG( 3, ( "unlock fd=%d fnum=%d offset=%.0f count=%.0f\n",
4913 fsp->fh->fd, fsp->fnum, (double)offset, (double)count ) );
4915 reply_outbuf(req, 0, 0);
4917 END_PROFILE(SMBunlock);
4922 #define DBGC_CLASS DBGC_ALL
4924 /****************************************************************************
4926 conn POINTER CAN BE NULL HERE !
4927 ****************************************************************************/
4929 void reply_tdis(struct smb_request *req)
4931 connection_struct *conn = req->conn;
4932 START_PROFILE(SMBtdis);
4935 DEBUG(4,("Invalid connection in tdis\n"));
4936 reply_nterror(req, NT_STATUS_NETWORK_NAME_DELETED);
4937 END_PROFILE(SMBtdis);
4943 close_cnum(conn,req->vuid);
4946 reply_outbuf(req, 0, 0);
4947 END_PROFILE(SMBtdis);
4951 /****************************************************************************
4953 conn POINTER CAN BE NULL HERE !
4954 ****************************************************************************/
4956 void reply_echo(struct smb_request *req)
4958 connection_struct *conn = req->conn;
4959 struct smb_perfcount_data local_pcd;
4960 struct smb_perfcount_data *cur_pcd;
4964 START_PROFILE(SMBecho);
4966 smb_init_perfcount_data(&local_pcd);
4969 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4970 END_PROFILE(SMBecho);
4974 smb_reverb = SVAL(req->vwv+0, 0);
4976 reply_outbuf(req, 1, req->buflen);
4978 /* copy any incoming data back out */
4979 if (req->buflen > 0) {
4980 memcpy(smb_buf(req->outbuf), req->buf, req->buflen);
4983 if (smb_reverb > 100) {
4984 DEBUG(0,("large reverb (%d)?? Setting to 100\n",smb_reverb));
4988 for (seq_num = 1 ; seq_num <= smb_reverb ; seq_num++) {
4990 /* this makes sure we catch the request pcd */
4991 if (seq_num == smb_reverb) {
4992 cur_pcd = &req->pcd;
4994 SMB_PERFCOUNT_COPY_CONTEXT(&req->pcd, &local_pcd);
4995 cur_pcd = &local_pcd;
4998 SSVAL(req->outbuf,smb_vwv0,seq_num);
5000 show_msg((char *)req->outbuf);
5001 if (!srv_send_smb(smbd_server_fd(),
5002 (char *)req->outbuf,
5003 true, req->seqnum+1,
5004 IS_CONN_ENCRYPTED(conn)||req->encrypted,
5006 exit_server_cleanly("reply_echo: srv_send_smb failed.");
5009 DEBUG(3,("echo %d times\n", smb_reverb));
5011 TALLOC_FREE(req->outbuf);
5013 END_PROFILE(SMBecho);
5017 /****************************************************************************
5018 Reply to a printopen.
5019 ****************************************************************************/
5021 void reply_printopen(struct smb_request *req)
5023 connection_struct *conn = req->conn;
5027 START_PROFILE(SMBsplopen);
5030 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
5031 END_PROFILE(SMBsplopen);
5035 if (!CAN_PRINT(conn)) {
5036 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
5037 END_PROFILE(SMBsplopen);
5041 status = file_new(req, conn, &fsp);
5042 if(!NT_STATUS_IS_OK(status)) {
5043 reply_nterror(req, status);
5044 END_PROFILE(SMBsplopen);
5048 /* Open for exclusive use, write only. */
5049 status = print_fsp_open(req, conn, NULL, req->vuid, fsp);
5051 if (!NT_STATUS_IS_OK(status)) {
5052 file_free(req, fsp);
5053 reply_nterror(req, status);
5054 END_PROFILE(SMBsplopen);
5058 reply_outbuf(req, 1, 0);
5059 SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
5061 DEBUG(3,("openprint fd=%d fnum=%d\n",
5062 fsp->fh->fd, fsp->fnum));
5064 END_PROFILE(SMBsplopen);
5068 /****************************************************************************
5069 Reply to a printclose.
5070 ****************************************************************************/
5072 void reply_printclose(struct smb_request *req)
5074 connection_struct *conn = req->conn;
5078 START_PROFILE(SMBsplclose);
5081 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
5082 END_PROFILE(SMBsplclose);
5086 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
5088 if (!check_fsp(conn, req, fsp)) {
5089 END_PROFILE(SMBsplclose);
5093 if (!CAN_PRINT(conn)) {
5094 reply_force_doserror(req, ERRSRV, ERRerror);
5095 END_PROFILE(SMBsplclose);
5099 DEBUG(3,("printclose fd=%d fnum=%d\n",
5100 fsp->fh->fd,fsp->fnum));
5102 status = close_file(req, fsp, NORMAL_CLOSE);
5104 if(!NT_STATUS_IS_OK(status)) {
5105 reply_nterror(req, status);
5106 END_PROFILE(SMBsplclose);
5110 reply_outbuf(req, 0, 0);
5112 END_PROFILE(SMBsplclose);
5116 /****************************************************************************
5117 Reply to a printqueue.
5118 ****************************************************************************/
5120 void reply_printqueue(struct smb_request *req)
5122 connection_struct *conn = req->conn;
5126 START_PROFILE(SMBsplretq);
5129 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
5130 END_PROFILE(SMBsplretq);
5134 max_count = SVAL(req->vwv+0, 0);
5135 start_index = SVAL(req->vwv+1, 0);
5137 /* we used to allow the client to get the cnum wrong, but that
5138 is really quite gross and only worked when there was only
5139 one printer - I think we should now only accept it if they
5140 get it right (tridge) */
5141 if (!CAN_PRINT(conn)) {
5142 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
5143 END_PROFILE(SMBsplretq);
5147 reply_outbuf(req, 2, 3);
5148 SSVAL(req->outbuf,smb_vwv0,0);
5149 SSVAL(req->outbuf,smb_vwv1,0);
5150 SCVAL(smb_buf(req->outbuf),0,1);
5151 SSVAL(smb_buf(req->outbuf),1,0);
5153 DEBUG(3,("printqueue start_index=%d max_count=%d\n",
5154 start_index, max_count));
5157 print_queue_struct *queue = NULL;
5158 print_status_struct status;
5159 int count = print_queue_status(SNUM(conn), &queue, &status);
5160 int num_to_get = ABS(max_count);
5161 int first = (max_count>0?start_index:start_index+max_count+1);
5167 num_to_get = MIN(num_to_get,count-first);
5170 for (i=first;i<first+num_to_get;i++) {
5174 srv_put_dos_date2(p,0,queue[i].time);
5175 SCVAL(p,4,(queue[i].status==LPQ_PRINTING?2:3));
5176 SSVAL(p,5, queue[i].job);
5177 SIVAL(p,7,queue[i].size);
5179 srvstr_push(blob, req->flags2, p+12,
5180 queue[i].fs_user, 16, STR_ASCII);
5182 if (message_push_blob(
5185 blob, sizeof(blob))) == -1) {
5186 reply_nterror(req, NT_STATUS_NO_MEMORY);
5187 END_PROFILE(SMBsplretq);
5193 SSVAL(req->outbuf,smb_vwv0,count);
5194 SSVAL(req->outbuf,smb_vwv1,
5195 (max_count>0?first+count:first-1));
5196 SCVAL(smb_buf(req->outbuf),0,1);
5197 SSVAL(smb_buf(req->outbuf),1,28*count);
5202 DEBUG(3,("%d entries returned in queue\n",count));
5205 END_PROFILE(SMBsplretq);
5209 /****************************************************************************
5210 Reply to a printwrite.
5211 ****************************************************************************/
5213 void reply_printwrite(struct smb_request *req)
5215 connection_struct *conn = req->conn;
5220 START_PROFILE(SMBsplwr);
5223 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
5224 END_PROFILE(SMBsplwr);
5228 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
5230 if (!check_fsp(conn, req, fsp)) {
5231 END_PROFILE(SMBsplwr);
5235 if (!CAN_PRINT(conn)) {
5236 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
5237 END_PROFILE(SMBsplwr);
5241 if (!CHECK_WRITE(fsp)) {
5242 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
5243 END_PROFILE(SMBsplwr);
5247 numtowrite = SVAL(req->buf, 1);
5249 if (req->buflen < numtowrite + 3) {
5250 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
5251 END_PROFILE(SMBsplwr);
5255 data = (const char *)req->buf + 3;
5257 if (write_file(req,fsp,data,(SMB_OFF_T)-1,numtowrite) != numtowrite) {
5258 reply_nterror(req, map_nt_error_from_unix(errno));
5259 END_PROFILE(SMBsplwr);
5263 DEBUG( 3, ( "printwrite fnum=%d num=%d\n", fsp->fnum, numtowrite ) );
5265 END_PROFILE(SMBsplwr);
5269 /****************************************************************************
5271 ****************************************************************************/
5273 void reply_mkdir(struct smb_request *req)
5275 connection_struct *conn = req->conn;
5276 struct smb_filename *smb_dname = NULL;
5277 char *directory = NULL;
5279 TALLOC_CTX *ctx = talloc_tos();
5281 START_PROFILE(SMBmkdir);
5283 srvstr_get_path_req(ctx, req, &directory, (const char *)req->buf + 1,
5284 STR_TERMINATE, &status);
5285 if (!NT_STATUS_IS_OK(status)) {
5286 reply_nterror(req, status);
5290 status = filename_convert(ctx, conn,
5291 req->flags2 & FLAGS2_DFS_PATHNAMES,
5296 if (!NT_STATUS_IS_OK(status)) {
5297 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
5298 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
5299 ERRSRV, ERRbadpath);
5302 reply_nterror(req, status);
5306 status = create_directory(conn, req, smb_dname);
5308 DEBUG(5, ("create_directory returned %s\n", nt_errstr(status)));
5310 if (!NT_STATUS_IS_OK(status)) {
5312 if (!use_nt_status()
5313 && NT_STATUS_EQUAL(status,
5314 NT_STATUS_OBJECT_NAME_COLLISION)) {
5316 * Yes, in the DOS error code case we get a
5317 * ERRDOS:ERRnoaccess here. See BASE-SAMBA3ERROR
5318 * samba4 torture test.
5320 status = NT_STATUS_DOS(ERRDOS, ERRnoaccess);
5323 reply_nterror(req, status);
5327 reply_outbuf(req, 0, 0);
5329 DEBUG(3, ("mkdir %s\n", smb_dname->base_name));
5331 TALLOC_FREE(smb_dname);
5332 END_PROFILE(SMBmkdir);
5336 /****************************************************************************
5338 ****************************************************************************/
5340 void reply_rmdir(struct smb_request *req)
5342 connection_struct *conn = req->conn;
5343 struct smb_filename *smb_dname = NULL;
5344 char *directory = NULL;
5346 TALLOC_CTX *ctx = talloc_tos();
5347 files_struct *fsp = NULL;
5349 struct smbd_server_connection *sconn = smbd_server_conn;
5351 START_PROFILE(SMBrmdir);
5353 srvstr_get_path_req(ctx, req, &directory, (const char *)req->buf + 1,
5354 STR_TERMINATE, &status);
5355 if (!NT_STATUS_IS_OK(status)) {
5356 reply_nterror(req, status);
5360 status = filename_convert(ctx, conn,
5361 req->flags2 & FLAGS2_DFS_PATHNAMES,
5366 if (!NT_STATUS_IS_OK(status)) {
5367 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
5368 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
5369 ERRSRV, ERRbadpath);
5372 reply_nterror(req, status);
5376 if (is_ntfs_stream_smb_fname(smb_dname)) {
5377 reply_nterror(req, NT_STATUS_NOT_A_DIRECTORY);
5381 status = SMB_VFS_CREATE_FILE(
5384 0, /* root_dir_fid */
5385 smb_dname, /* fname */
5386 DELETE_ACCESS, /* access_mask */
5387 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
5389 FILE_OPEN, /* create_disposition*/
5390 FILE_DIRECTORY_FILE, /* create_options */
5391 FILE_ATTRIBUTE_DIRECTORY, /* file_attributes */
5392 0, /* oplock_request */
5393 0, /* allocation_size */
5394 0, /* private_flags */
5400 if (!NT_STATUS_IS_OK(status)) {
5401 if (open_was_deferred(req->mid)) {
5402 /* We have re-scheduled this call. */
5405 reply_nterror(req, status);
5409 status = can_set_delete_on_close(fsp, FILE_ATTRIBUTE_DIRECTORY);
5410 if (!NT_STATUS_IS_OK(status)) {
5411 close_file(req, fsp, ERROR_CLOSE);
5412 reply_nterror(req, status);
5416 if (!set_delete_on_close(fsp, true, &conn->server_info->utok)) {
5417 close_file(req, fsp, ERROR_CLOSE);
5418 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
5422 status = close_file(req, fsp, NORMAL_CLOSE);
5423 if (!NT_STATUS_IS_OK(status)) {
5424 reply_nterror(req, status);
5426 reply_outbuf(req, 0, 0);
5429 dptr_closepath(sconn, smb_dname->base_name, req->smbpid);
5431 DEBUG(3, ("rmdir %s\n", smb_fname_str_dbg(smb_dname)));
5433 TALLOC_FREE(smb_dname);
5434 END_PROFILE(SMBrmdir);
5438 /*******************************************************************
5439 Resolve wildcards in a filename rename.
5440 ********************************************************************/
5442 static bool resolve_wildcards(TALLOC_CTX *ctx,
5447 char *name2_copy = NULL;
5452 char *p,*p2, *pname1, *pname2;
5454 name2_copy = talloc_strdup(ctx, name2);
5459 pname1 = strrchr_m(name1,'/');
5460 pname2 = strrchr_m(name2_copy,'/');
5462 if (!pname1 || !pname2) {
5466 /* Truncate the copy of name2 at the last '/' */
5469 /* Now go past the '/' */
5473 root1 = talloc_strdup(ctx, pname1);
5474 root2 = talloc_strdup(ctx, pname2);
5476 if (!root1 || !root2) {
5480 p = strrchr_m(root1,'.');
5483 ext1 = talloc_strdup(ctx, p+1);
5485 ext1 = talloc_strdup(ctx, "");
5487 p = strrchr_m(root2,'.');
5490 ext2 = talloc_strdup(ctx, p+1);
5492 ext2 = talloc_strdup(ctx, "");
5495 if (!ext1 || !ext2) {
5503 /* Hmmm. Should this be mb-aware ? */
5506 } else if (*p2 == '*') {
5508 root2 = talloc_asprintf(ctx, "%s%s",
5527 /* Hmmm. Should this be mb-aware ? */
5530 } else if (*p2 == '*') {
5532 ext2 = talloc_asprintf(ctx, "%s%s",
5548 *pp_newname = talloc_asprintf(ctx, "%s/%s.%s",
5553 *pp_newname = talloc_asprintf(ctx, "%s/%s",
5565 /****************************************************************************
5566 Ensure open files have their names updated. Updated to notify other smbd's
5568 ****************************************************************************/
5570 static void rename_open_files(connection_struct *conn,
5571 struct share_mode_lock *lck,
5572 const struct smb_filename *smb_fname_dst)
5575 bool did_rename = False;
5578 for(fsp = file_find_di_first(lck->id); fsp;
5579 fsp = file_find_di_next(fsp)) {
5580 /* fsp_name is a relative path under the fsp. To change this for other
5581 sharepaths we need to manipulate relative paths. */
5582 /* TODO - create the absolute path and manipulate the newname
5583 relative to the sharepath. */
5584 if (!strequal(fsp->conn->connectpath, conn->connectpath)) {
5587 DEBUG(10, ("rename_open_files: renaming file fnum %d "
5588 "(file_id %s) from %s -> %s\n", fsp->fnum,
5589 file_id_string_tos(&fsp->file_id), fsp_str_dbg(fsp),
5590 smb_fname_str_dbg(smb_fname_dst)));
5592 status = fsp_set_smb_fname(fsp, smb_fname_dst);
5593 if (NT_STATUS_IS_OK(status)) {
5599 DEBUG(10, ("rename_open_files: no open files on file_id %s "
5600 "for %s\n", file_id_string_tos(&lck->id),
5601 smb_fname_str_dbg(smb_fname_dst)));
5604 /* Send messages to all smbd's (not ourself) that the name has changed. */
5605 rename_share_filename(smbd_messaging_context(), lck, conn->connectpath,
5610 /****************************************************************************
5611 We need to check if the source path is a parent directory of the destination
5612 (ie. a rename of /foo/bar/baz -> /foo/bar/baz/bibble/bobble. If so we must
5613 refuse the rename with a sharing violation. Under UNIX the above call can
5614 *succeed* if /foo/bar/baz is a symlink to another area in the share. We
5615 probably need to check that the client is a Windows one before disallowing
5616 this as a UNIX client (one with UNIX extensions) can know the source is a
5617 symlink and make this decision intelligently. Found by an excellent bug
5618 report from <AndyLiebman@aol.com>.
5619 ****************************************************************************/
5621 static bool rename_path_prefix_equal(const struct smb_filename *smb_fname_src,
5622 const struct smb_filename *smb_fname_dst)
5624 const char *psrc = smb_fname_src->base_name;
5625 const char *pdst = smb_fname_dst->base_name;
5628 if (psrc[0] == '.' && psrc[1] == '/') {
5631 if (pdst[0] == '.' && pdst[1] == '/') {
5634 if ((slen = strlen(psrc)) > strlen(pdst)) {
5637 return ((memcmp(psrc, pdst, slen) == 0) && pdst[slen] == '/');
5641 * Do the notify calls from a rename
5644 static void notify_rename(connection_struct *conn, bool is_dir,
5645 const struct smb_filename *smb_fname_src,
5646 const struct smb_filename *smb_fname_dst)
5648 char *parent_dir_src = NULL;
5649 char *parent_dir_dst = NULL;
5652 mask = is_dir ? FILE_NOTIFY_CHANGE_DIR_NAME
5653 : FILE_NOTIFY_CHANGE_FILE_NAME;
5655 if (!parent_dirname(talloc_tos(), smb_fname_src->base_name,
5656 &parent_dir_src, NULL) ||
5657 !parent_dirname(talloc_tos(), smb_fname_dst->base_name,
5658 &parent_dir_dst, NULL)) {
5662 if (strcmp(parent_dir_src, parent_dir_dst) == 0) {
5663 notify_fname(conn, NOTIFY_ACTION_OLD_NAME, mask,
5664 smb_fname_src->base_name);
5665 notify_fname(conn, NOTIFY_ACTION_NEW_NAME, mask,
5666 smb_fname_dst->base_name);
5669 notify_fname(conn, NOTIFY_ACTION_REMOVED, mask,
5670 smb_fname_src->base_name);
5671 notify_fname(conn, NOTIFY_ACTION_ADDED, mask,
5672 smb_fname_dst->base_name);
5675 /* this is a strange one. w2k3 gives an additional event for
5676 CHANGE_ATTRIBUTES and CHANGE_CREATION on the new file when renaming
5677 files, but not directories */
5679 notify_fname(conn, NOTIFY_ACTION_MODIFIED,
5680 FILE_NOTIFY_CHANGE_ATTRIBUTES
5681 |FILE_NOTIFY_CHANGE_CREATION,
5682 smb_fname_dst->base_name);
5685 TALLOC_FREE(parent_dir_src);
5686 TALLOC_FREE(parent_dir_dst);
5689 /****************************************************************************
5690 Rename an open file - given an fsp.
5691 ****************************************************************************/
5693 NTSTATUS rename_internals_fsp(connection_struct *conn,
5695 const struct smb_filename *smb_fname_dst_in,
5697 bool replace_if_exists)
5699 TALLOC_CTX *ctx = talloc_tos();
5700 struct smb_filename *smb_fname_dst = NULL;
5701 NTSTATUS status = NT_STATUS_OK;
5702 struct share_mode_lock *lck = NULL;
5703 bool dst_exists, old_is_stream, new_is_stream;
5705 status = check_name(conn, smb_fname_dst_in->base_name);
5706 if (!NT_STATUS_IS_OK(status)) {
5710 /* Make a copy of the dst smb_fname structs */
5712 status = copy_smb_filename(ctx, smb_fname_dst_in, &smb_fname_dst);
5713 if (!NT_STATUS_IS_OK(status)) {
5717 /* Ensure the dst smb_fname contains a '/' */
5718 if(strrchr_m(smb_fname_dst->base_name,'/') == 0) {
5720 tmp = talloc_asprintf(smb_fname_dst, "./%s",
5721 smb_fname_dst->base_name);
5723 status = NT_STATUS_NO_MEMORY;
5726 TALLOC_FREE(smb_fname_dst->base_name);
5727 smb_fname_dst->base_name = tmp;
5731 * Check for special case with case preserving and not
5732 * case sensitive. If the old last component differs from the original
5733 * last component only by case, then we should allow
5734 * the rename (user is trying to change the case of the
5737 if((conn->case_sensitive == False) && (conn->case_preserve == True) &&
5738 strequal(fsp->fsp_name->base_name, smb_fname_dst->base_name) &&
5739 strequal(fsp->fsp_name->stream_name, smb_fname_dst->stream_name)) {
5741 char *fname_dst_lcomp_base_mod = NULL;
5742 struct smb_filename *smb_fname_orig_lcomp = NULL;
5745 * Get the last component of the destination name. Note that
5746 * we guarantee that destination name contains a '/' character
5749 last_slash = strrchr_m(smb_fname_dst->base_name, '/');
5750 fname_dst_lcomp_base_mod = talloc_strdup(ctx, last_slash + 1);
5751 if (!fname_dst_lcomp_base_mod) {
5752 status = NT_STATUS_NO_MEMORY;
5757 * Create an smb_filename struct using the original last
5758 * component of the destination.
5760 status = create_synthetic_smb_fname_split(ctx,
5761 smb_fname_dst->original_lcomp, NULL,
5762 &smb_fname_orig_lcomp);
5763 if (!NT_STATUS_IS_OK(status)) {
5764 TALLOC_FREE(fname_dst_lcomp_base_mod);
5768 /* If the base names only differ by case, use original. */
5769 if(!strcsequal(fname_dst_lcomp_base_mod,
5770 smb_fname_orig_lcomp->base_name)) {
5773 * Replace the modified last component with the
5776 *last_slash = '\0'; /* Truncate at the '/' */
5777 tmp = talloc_asprintf(smb_fname_dst,
5779 smb_fname_dst->base_name,
5780 smb_fname_orig_lcomp->base_name);
5782 status = NT_STATUS_NO_MEMORY;
5783 TALLOC_FREE(fname_dst_lcomp_base_mod);
5784 TALLOC_FREE(smb_fname_orig_lcomp);
5787 TALLOC_FREE(smb_fname_dst->base_name);
5788 smb_fname_dst->base_name = tmp;
5791 /* If the stream_names only differ by case, use original. */
5792 if(!strcsequal(smb_fname_dst->stream_name,
5793 smb_fname_orig_lcomp->stream_name)) {
5795 /* Use the original stream. */
5796 tmp = talloc_strdup(smb_fname_dst,
5797 smb_fname_orig_lcomp->stream_name);
5799 status = NT_STATUS_NO_MEMORY;
5800 TALLOC_FREE(fname_dst_lcomp_base_mod);
5801 TALLOC_FREE(smb_fname_orig_lcomp);
5804 TALLOC_FREE(smb_fname_dst->stream_name);
5805 smb_fname_dst->stream_name = tmp;
5807 TALLOC_FREE(fname_dst_lcomp_base_mod);
5808 TALLOC_FREE(smb_fname_orig_lcomp);
5812 * If the src and dest names are identical - including case,
5813 * don't do the rename, just return success.
5816 if (strcsequal(fsp->fsp_name->base_name, smb_fname_dst->base_name) &&
5817 strcsequal(fsp->fsp_name->stream_name,
5818 smb_fname_dst->stream_name)) {
5819 DEBUG(3, ("rename_internals_fsp: identical names in rename %s "
5820 "- returning success\n",
5821 smb_fname_str_dbg(smb_fname_dst)));
5822 status = NT_STATUS_OK;
5826 old_is_stream = is_ntfs_stream_smb_fname(fsp->fsp_name);
5827 new_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
5829 /* Return the correct error code if both names aren't streams. */
5830 if (!old_is_stream && new_is_stream) {
5831 status = NT_STATUS_OBJECT_NAME_INVALID;
5835 if (old_is_stream && !new_is_stream) {
5836 status = NT_STATUS_INVALID_PARAMETER;
5840 dst_exists = SMB_VFS_STAT(conn, smb_fname_dst) == 0;
5842 if(!replace_if_exists && dst_exists) {
5843 DEBUG(3, ("rename_internals_fsp: dest exists doing rename "
5844 "%s -> %s\n", smb_fname_str_dbg(fsp->fsp_name),
5845 smb_fname_str_dbg(smb_fname_dst)));
5846 status = NT_STATUS_OBJECT_NAME_COLLISION;
5851 struct file_id fileid = vfs_file_id_from_sbuf(conn,
5852 &smb_fname_dst->st);
5853 files_struct *dst_fsp = file_find_di_first(fileid);
5854 /* The file can be open when renaming a stream */
5855 if (dst_fsp && !new_is_stream) {
5856 DEBUG(3, ("rename_internals_fsp: Target file open\n"));
5857 status = NT_STATUS_ACCESS_DENIED;
5862 /* Ensure we have a valid stat struct for the source. */
5863 status = vfs_stat_fsp(fsp);
5864 if (!NT_STATUS_IS_OK(status)) {
5868 status = can_rename(conn, fsp, attrs);
5870 if (!NT_STATUS_IS_OK(status)) {
5871 DEBUG(3, ("rename_internals_fsp: Error %s rename %s -> %s\n",
5872 nt_errstr(status), smb_fname_str_dbg(fsp->fsp_name),
5873 smb_fname_str_dbg(smb_fname_dst)));
5874 if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION))
5875 status = NT_STATUS_ACCESS_DENIED;
5879 if (rename_path_prefix_equal(fsp->fsp_name, smb_fname_dst)) {
5880 status = NT_STATUS_ACCESS_DENIED;
5883 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
5887 * We have the file open ourselves, so not being able to get the
5888 * corresponding share mode lock is a fatal error.
5891 SMB_ASSERT(lck != NULL);
5893 if(SMB_VFS_RENAME(conn, fsp->fsp_name, smb_fname_dst) == 0) {
5894 uint32 create_options = fsp->fh->private_options;
5896 DEBUG(3, ("rename_internals_fsp: succeeded doing rename on "
5897 "%s -> %s\n", smb_fname_str_dbg(fsp->fsp_name),
5898 smb_fname_str_dbg(smb_fname_dst)));
5900 notify_rename(conn, fsp->is_directory, fsp->fsp_name,
5903 rename_open_files(conn, lck, smb_fname_dst);
5906 * A rename acts as a new file create w.r.t. allowing an initial delete
5907 * on close, probably because in Windows there is a new handle to the
5908 * new file. If initial delete on close was requested but not
5909 * originally set, we need to set it here. This is probably not 100% correct,
5910 * but will work for the CIFSFS client which in non-posix mode
5911 * depends on these semantics. JRA.
5914 if (create_options & FILE_DELETE_ON_CLOSE) {
5915 status = can_set_delete_on_close(fsp, 0);
5917 if (NT_STATUS_IS_OK(status)) {
5918 /* Note that here we set the *inital* delete on close flag,
5919 * not the regular one. The magic gets handled in close. */
5920 fsp->initial_delete_on_close = True;
5924 status = NT_STATUS_OK;
5930 if (errno == ENOTDIR || errno == EISDIR) {
5931 status = NT_STATUS_OBJECT_NAME_COLLISION;
5933 status = map_nt_error_from_unix(errno);
5936 DEBUG(3, ("rename_internals_fsp: Error %s rename %s -> %s\n",
5937 nt_errstr(status), smb_fname_str_dbg(fsp->fsp_name),
5938 smb_fname_str_dbg(smb_fname_dst)));
5941 TALLOC_FREE(smb_fname_dst);
5946 /****************************************************************************
5947 The guts of the rename command, split out so it may be called by the NT SMB
5949 ****************************************************************************/
5951 NTSTATUS rename_internals(TALLOC_CTX *ctx,
5952 connection_struct *conn,
5953 struct smb_request *req,
5954 struct smb_filename *smb_fname_src,
5955 struct smb_filename *smb_fname_dst,
5957 bool replace_if_exists,
5960 uint32_t access_mask)
5962 char *fname_src_dir = NULL;
5963 char *fname_src_mask = NULL;
5965 NTSTATUS status = NT_STATUS_OK;
5966 struct smb_Dir *dir_hnd = NULL;
5967 const char *dname = NULL;
5968 char *talloced = NULL;
5970 int create_options = 0;
5971 bool posix_pathnames = lp_posix_pathnames();
5974 * Split the old name into directory and last component
5975 * strings. Note that unix_convert may have stripped off a
5976 * leading ./ from both name and newname if the rename is
5977 * at the root of the share. We need to make sure either both
5978 * name and newname contain a / character or neither of them do
5979 * as this is checked in resolve_wildcards().
5982 /* Split up the directory from the filename/mask. */
5983 status = split_fname_dir_mask(ctx, smb_fname_src->base_name,
5984 &fname_src_dir, &fname_src_mask);
5985 if (!NT_STATUS_IS_OK(status)) {
5986 status = NT_STATUS_NO_MEMORY;
5991 * We should only check the mangled cache
5992 * here if unix_convert failed. This means
5993 * that the path in 'mask' doesn't exist
5994 * on the file system and so we need to look
5995 * for a possible mangle. This patch from
5996 * Tine Smukavec <valentin.smukavec@hermes.si>.
5999 if (!VALID_STAT(smb_fname_src->st) &&
6000 mangle_is_mangled(fname_src_mask, conn->params)) {
6001 char *new_mask = NULL;
6002 mangle_lookup_name_from_8_3(ctx, fname_src_mask, &new_mask,
6005 TALLOC_FREE(fname_src_mask);
6006 fname_src_mask = new_mask;
6010 if (!src_has_wild) {
6014 * Only one file needs to be renamed. Append the mask back
6015 * onto the directory.
6017 TALLOC_FREE(smb_fname_src->base_name);
6018 smb_fname_src->base_name = talloc_asprintf(smb_fname_src,
6022 if (!smb_fname_src->base_name) {
6023 status = NT_STATUS_NO_MEMORY;
6027 /* Ensure dst fname contains a '/' also */
6028 if(strrchr_m(smb_fname_dst->base_name, '/') == 0) {
6030 tmp = talloc_asprintf(smb_fname_dst, "./%s",
6031 smb_fname_dst->base_name);
6033 status = NT_STATUS_NO_MEMORY;
6036 TALLOC_FREE(smb_fname_dst->base_name);
6037 smb_fname_dst->base_name = tmp;
6040 DEBUG(3, ("rename_internals: case_sensitive = %d, "
6041 "case_preserve = %d, short case preserve = %d, "
6042 "directory = %s, newname = %s, "
6043 "last_component_dest = %s\n",
6044 conn->case_sensitive, conn->case_preserve,
6045 conn->short_case_preserve,
6046 smb_fname_str_dbg(smb_fname_src),
6047 smb_fname_str_dbg(smb_fname_dst),
6048 smb_fname_dst->original_lcomp));
6050 /* The dest name still may have wildcards. */
6051 if (dest_has_wild) {
6052 char *fname_dst_mod = NULL;
6053 if (!resolve_wildcards(smb_fname_dst,
6054 smb_fname_src->base_name,
6055 smb_fname_dst->base_name,
6057 DEBUG(6, ("rename_internals: resolve_wildcards "
6059 smb_fname_src->base_name,
6060 smb_fname_dst->base_name));
6061 status = NT_STATUS_NO_MEMORY;
6064 TALLOC_FREE(smb_fname_dst->base_name);
6065 smb_fname_dst->base_name = fname_dst_mod;
6068 ZERO_STRUCT(smb_fname_src->st);
6069 if (posix_pathnames) {
6070 SMB_VFS_LSTAT(conn, smb_fname_src);
6072 SMB_VFS_STAT(conn, smb_fname_src);
6075 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
6076 create_options |= FILE_DIRECTORY_FILE;
6079 status = SMB_VFS_CREATE_FILE(
6082 0, /* root_dir_fid */
6083 smb_fname_src, /* fname */
6084 access_mask, /* access_mask */
6085 (FILE_SHARE_READ | /* share_access */
6087 FILE_OPEN, /* create_disposition*/
6088 create_options, /* create_options */
6089 posix_pathnames ? FILE_FLAG_POSIX_SEMANTICS|0777 : 0, /* file_attributes */
6090 0, /* oplock_request */
6091 0, /* allocation_size */
6092 0, /* private_flags */
6098 if (!NT_STATUS_IS_OK(status)) {
6099 DEBUG(3, ("Could not open rename source %s: %s\n",
6100 smb_fname_str_dbg(smb_fname_src),
6101 nt_errstr(status)));
6105 status = rename_internals_fsp(conn, fsp, smb_fname_dst,
6106 attrs, replace_if_exists);
6108 close_file(req, fsp, NORMAL_CLOSE);
6110 DEBUG(3, ("rename_internals: Error %s rename %s -> %s\n",
6111 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
6112 smb_fname_str_dbg(smb_fname_dst)));
6118 * Wildcards - process each file that matches.
6120 if (strequal(fname_src_mask, "????????.???")) {
6121 TALLOC_FREE(fname_src_mask);
6122 fname_src_mask = talloc_strdup(ctx, "*");
6123 if (!fname_src_mask) {
6124 status = NT_STATUS_NO_MEMORY;
6129 status = check_name(conn, fname_src_dir);
6130 if (!NT_STATUS_IS_OK(status)) {
6134 dir_hnd = OpenDir(talloc_tos(), conn, fname_src_dir, fname_src_mask,
6136 if (dir_hnd == NULL) {
6137 status = map_nt_error_from_unix(errno);
6141 status = NT_STATUS_NO_SUCH_FILE;
6143 * Was status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
6144 * - gentest fix. JRA
6147 while ((dname = ReadDirName(dir_hnd, &offset, &smb_fname_src->st,
6149 files_struct *fsp = NULL;
6150 char *destname = NULL;
6151 bool sysdir_entry = False;
6153 /* Quick check for "." and ".." */
6154 if (ISDOT(dname) || ISDOTDOT(dname)) {
6156 sysdir_entry = True;
6158 TALLOC_FREE(talloced);
6163 if (!is_visible_file(conn, fname_src_dir, dname,
6164 &smb_fname_src->st, false)) {
6165 TALLOC_FREE(talloced);
6169 if(!mask_match(dname, fname_src_mask, conn->case_sensitive)) {
6170 TALLOC_FREE(talloced);
6175 status = NT_STATUS_OBJECT_NAME_INVALID;
6179 TALLOC_FREE(smb_fname_src->base_name);
6180 smb_fname_src->base_name = talloc_asprintf(smb_fname_src,
6184 if (!smb_fname_src->base_name) {
6185 status = NT_STATUS_NO_MEMORY;
6189 if (!resolve_wildcards(ctx, smb_fname_src->base_name,
6190 smb_fname_dst->base_name,
6192 DEBUG(6, ("resolve_wildcards %s %s failed\n",
6193 smb_fname_src->base_name, destname));
6194 TALLOC_FREE(talloced);
6198 status = NT_STATUS_NO_MEMORY;
6202 TALLOC_FREE(smb_fname_dst->base_name);
6203 smb_fname_dst->base_name = destname;
6205 ZERO_STRUCT(smb_fname_src->st);
6206 if (posix_pathnames) {
6207 SMB_VFS_LSTAT(conn, smb_fname_src);
6209 SMB_VFS_STAT(conn, smb_fname_src);
6214 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
6215 create_options |= FILE_DIRECTORY_FILE;
6218 status = SMB_VFS_CREATE_FILE(
6221 0, /* root_dir_fid */
6222 smb_fname_src, /* fname */
6223 access_mask, /* access_mask */
6224 (FILE_SHARE_READ | /* share_access */
6226 FILE_OPEN, /* create_disposition*/
6227 create_options, /* create_options */
6228 posix_pathnames ? FILE_FLAG_POSIX_SEMANTICS|0777 : 0, /* file_attributes */
6229 0, /* oplock_request */
6230 0, /* allocation_size */
6231 0, /* private_flags */
6237 if (!NT_STATUS_IS_OK(status)) {
6238 DEBUG(3,("rename_internals: SMB_VFS_CREATE_FILE "
6239 "returned %s rename %s -> %s\n",
6241 smb_fname_str_dbg(smb_fname_src),
6242 smb_fname_str_dbg(smb_fname_dst)));
6246 smb_fname_dst->original_lcomp = talloc_strdup(smb_fname_dst,
6248 if (!smb_fname_dst->original_lcomp) {
6249 status = NT_STATUS_NO_MEMORY;
6253 status = rename_internals_fsp(conn, fsp, smb_fname_dst,
6254 attrs, replace_if_exists);
6256 close_file(req, fsp, NORMAL_CLOSE);
6258 if (!NT_STATUS_IS_OK(status)) {
6259 DEBUG(3, ("rename_internals_fsp returned %s for "
6260 "rename %s -> %s\n", nt_errstr(status),
6261 smb_fname_str_dbg(smb_fname_src),
6262 smb_fname_str_dbg(smb_fname_dst)));
6268 DEBUG(3,("rename_internals: doing rename on %s -> "
6269 "%s\n", smb_fname_str_dbg(smb_fname_src),
6270 smb_fname_str_dbg(smb_fname_src)));
6271 TALLOC_FREE(talloced);
6273 TALLOC_FREE(dir_hnd);
6275 if (count == 0 && NT_STATUS_IS_OK(status) && errno != 0) {
6276 status = map_nt_error_from_unix(errno);
6280 TALLOC_FREE(talloced);
6281 TALLOC_FREE(fname_src_dir);
6282 TALLOC_FREE(fname_src_mask);
6286 /****************************************************************************
6288 ****************************************************************************/
6290 void reply_mv(struct smb_request *req)
6292 connection_struct *conn = req->conn;
6294 char *newname = NULL;
6298 bool src_has_wcard = False;
6299 bool dest_has_wcard = False;
6300 TALLOC_CTX *ctx = talloc_tos();
6301 struct smb_filename *smb_fname_src = NULL;
6302 struct smb_filename *smb_fname_dst = NULL;
6304 START_PROFILE(SMBmv);
6307 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
6311 attrs = SVAL(req->vwv+0, 0);
6313 p = (const char *)req->buf + 1;
6314 p += srvstr_get_path_req_wcard(ctx, req, &name, p, STR_TERMINATE,
6315 &status, &src_has_wcard);
6316 if (!NT_STATUS_IS_OK(status)) {
6317 reply_nterror(req, status);
6321 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
6322 &status, &dest_has_wcard);
6323 if (!NT_STATUS_IS_OK(status)) {
6324 reply_nterror(req, status);
6328 status = filename_convert(ctx,
6330 req->flags2 & FLAGS2_DFS_PATHNAMES,
6332 UCF_COND_ALLOW_WCARD_LCOMP,
6336 if (!NT_STATUS_IS_OK(status)) {
6337 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
6338 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
6339 ERRSRV, ERRbadpath);
6342 reply_nterror(req, status);
6346 status = filename_convert(ctx,
6348 req->flags2 & FLAGS2_DFS_PATHNAMES,
6350 UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP,
6354 if (!NT_STATUS_IS_OK(status)) {
6355 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
6356 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
6357 ERRSRV, ERRbadpath);
6360 reply_nterror(req, status);
6364 DEBUG(3,("reply_mv : %s -> %s\n", smb_fname_str_dbg(smb_fname_src),
6365 smb_fname_str_dbg(smb_fname_dst)));
6367 status = rename_internals(ctx, conn, req, smb_fname_src, smb_fname_dst,
6368 attrs, False, src_has_wcard, dest_has_wcard,
6370 if (!NT_STATUS_IS_OK(status)) {
6371 if (open_was_deferred(req->mid)) {
6372 /* We have re-scheduled this call. */
6375 reply_nterror(req, status);
6379 reply_outbuf(req, 0, 0);
6381 TALLOC_FREE(smb_fname_src);
6382 TALLOC_FREE(smb_fname_dst);
6387 /*******************************************************************
6388 Copy a file as part of a reply_copy.
6389 ******************************************************************/
6392 * TODO: check error codes on all callers
6395 NTSTATUS copy_file(TALLOC_CTX *ctx,
6396 connection_struct *conn,
6397 struct smb_filename *smb_fname_src,
6398 struct smb_filename *smb_fname_dst,
6401 bool target_is_directory)
6403 struct smb_filename *smb_fname_dst_tmp = NULL;
6405 files_struct *fsp1,*fsp2;
6407 uint32 new_create_disposition;
6411 status = copy_smb_filename(ctx, smb_fname_dst, &smb_fname_dst_tmp);
6412 if (!NT_STATUS_IS_OK(status)) {
6417 * If the target is a directory, extract the last component from the
6418 * src filename and append it to the dst filename
6420 if (target_is_directory) {
6423 /* dest/target can't be a stream if it's a directory. */
6424 SMB_ASSERT(smb_fname_dst->stream_name == NULL);
6426 p = strrchr_m(smb_fname_src->base_name,'/');
6430 p = smb_fname_src->base_name;
6432 smb_fname_dst_tmp->base_name =
6433 talloc_asprintf_append(smb_fname_dst_tmp->base_name, "/%s",
6435 if (!smb_fname_dst_tmp->base_name) {
6436 status = NT_STATUS_NO_MEMORY;
6441 status = vfs_file_exist(conn, smb_fname_src);
6442 if (!NT_STATUS_IS_OK(status)) {
6446 if (!target_is_directory && count) {
6447 new_create_disposition = FILE_OPEN;
6449 if (!map_open_params_to_ntcreate(smb_fname_dst_tmp, 0, ofun,
6451 &new_create_disposition,
6454 status = NT_STATUS_INVALID_PARAMETER;
6459 /* Open the src file for reading. */
6460 status = SMB_VFS_CREATE_FILE(
6463 0, /* root_dir_fid */
6464 smb_fname_src, /* fname */
6465 FILE_GENERIC_READ, /* access_mask */
6466 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
6467 FILE_OPEN, /* create_disposition*/
6468 0, /* create_options */
6469 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
6470 INTERNAL_OPEN_ONLY, /* oplock_request */
6471 0, /* allocation_size */
6472 0, /* private_flags */
6478 if (!NT_STATUS_IS_OK(status)) {
6482 dosattrs = dos_mode(conn, smb_fname_src);
6484 if (SMB_VFS_STAT(conn, smb_fname_dst_tmp) == -1) {
6485 ZERO_STRUCTP(&smb_fname_dst_tmp->st);
6488 /* Open the dst file for writing. */
6489 status = SMB_VFS_CREATE_FILE(
6492 0, /* root_dir_fid */
6493 smb_fname_dst, /* fname */
6494 FILE_GENERIC_WRITE, /* access_mask */
6495 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
6496 new_create_disposition, /* create_disposition*/
6497 0, /* create_options */
6498 dosattrs, /* file_attributes */
6499 INTERNAL_OPEN_ONLY, /* oplock_request */
6500 0, /* allocation_size */
6501 0, /* private_flags */
6507 if (!NT_STATUS_IS_OK(status)) {
6508 close_file(NULL, fsp1, ERROR_CLOSE);
6512 if ((ofun&3) == 1) {
6513 if(SMB_VFS_LSEEK(fsp2,0,SEEK_END) == -1) {
6514 DEBUG(0,("copy_file: error - vfs lseek returned error %s\n", strerror(errno) ));
6516 * Stop the copy from occurring.
6519 smb_fname_src->st.st_ex_size = 0;
6523 /* Do the actual copy. */
6524 if (smb_fname_src->st.st_ex_size) {
6525 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
6528 close_file(NULL, fsp1, NORMAL_CLOSE);
6530 /* Ensure the modtime is set correctly on the destination file. */
6531 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
6534 * As we are opening fsp1 read-only we only expect
6535 * an error on close on fsp2 if we are out of space.
6536 * Thus we don't look at the error return from the
6539 status = close_file(NULL, fsp2, NORMAL_CLOSE);
6541 if (!NT_STATUS_IS_OK(status)) {
6545 if (ret != (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
6546 status = NT_STATUS_DISK_FULL;
6550 status = NT_STATUS_OK;
6553 TALLOC_FREE(smb_fname_dst_tmp);
6557 /****************************************************************************
6558 Reply to a file copy.
6559 ****************************************************************************/
6561 void reply_copy(struct smb_request *req)
6563 connection_struct *conn = req->conn;
6564 struct smb_filename *smb_fname_src = NULL;
6565 struct smb_filename *smb_fname_dst = NULL;
6566 char *fname_src = NULL;
6567 char *fname_dst = NULL;
6568 char *fname_src_mask = NULL;
6569 char *fname_src_dir = NULL;
6572 int error = ERRnoaccess;
6576 bool target_is_directory=False;
6577 bool source_has_wild = False;
6578 bool dest_has_wild = False;
6580 TALLOC_CTX *ctx = talloc_tos();
6582 START_PROFILE(SMBcopy);
6585 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
6589 tid2 = SVAL(req->vwv+0, 0);
6590 ofun = SVAL(req->vwv+1, 0);
6591 flags = SVAL(req->vwv+2, 0);
6593 p = (const char *)req->buf;
6594 p += srvstr_get_path_req_wcard(ctx, req, &fname_src, p, STR_TERMINATE,
6595 &status, &source_has_wild);
6596 if (!NT_STATUS_IS_OK(status)) {
6597 reply_nterror(req, status);
6600 p += srvstr_get_path_req_wcard(ctx, req, &fname_dst, p, STR_TERMINATE,
6601 &status, &dest_has_wild);
6602 if (!NT_STATUS_IS_OK(status)) {
6603 reply_nterror(req, status);
6607 DEBUG(3,("reply_copy : %s -> %s\n", fname_src, fname_dst));
6609 if (tid2 != conn->cnum) {
6610 /* can't currently handle inter share copies XXXX */
6611 DEBUG(3,("Rejecting inter-share copy\n"));
6612 reply_nterror(req, NT_STATUS_BAD_DEVICE_TYPE);
6616 status = filename_convert(ctx, conn,
6617 req->flags2 & FLAGS2_DFS_PATHNAMES,
6619 UCF_COND_ALLOW_WCARD_LCOMP,
6622 if (!NT_STATUS_IS_OK(status)) {
6623 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
6624 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
6625 ERRSRV, ERRbadpath);
6628 reply_nterror(req, status);
6632 status = filename_convert(ctx, conn,
6633 req->flags2 & FLAGS2_DFS_PATHNAMES,
6635 UCF_COND_ALLOW_WCARD_LCOMP,
6638 if (!NT_STATUS_IS_OK(status)) {
6639 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
6640 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
6641 ERRSRV, ERRbadpath);
6644 reply_nterror(req, status);
6648 target_is_directory = VALID_STAT_OF_DIR(smb_fname_dst->st);
6650 if ((flags&1) && target_is_directory) {
6651 reply_nterror(req, NT_STATUS_NO_SUCH_FILE);
6655 if ((flags&2) && !target_is_directory) {
6656 reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
6660 if ((flags&(1<<5)) && VALID_STAT_OF_DIR(smb_fname_src->st)) {
6661 /* wants a tree copy! XXXX */
6662 DEBUG(3,("Rejecting tree copy\n"));
6663 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
6667 /* Split up the directory from the filename/mask. */
6668 status = split_fname_dir_mask(ctx, smb_fname_src->base_name,
6669 &fname_src_dir, &fname_src_mask);
6670 if (!NT_STATUS_IS_OK(status)) {
6671 reply_nterror(req, NT_STATUS_NO_MEMORY);
6676 * We should only check the mangled cache
6677 * here if unix_convert failed. This means
6678 * that the path in 'mask' doesn't exist
6679 * on the file system and so we need to look
6680 * for a possible mangle. This patch from
6681 * Tine Smukavec <valentin.smukavec@hermes.si>.
6683 if (!VALID_STAT(smb_fname_src->st) &&
6684 mangle_is_mangled(fname_src_mask, conn->params)) {
6685 char *new_mask = NULL;
6686 mangle_lookup_name_from_8_3(ctx, fname_src_mask,
6687 &new_mask, conn->params);
6689 /* Use demangled name if one was successfully found. */
6691 TALLOC_FREE(fname_src_mask);
6692 fname_src_mask = new_mask;
6696 if (!source_has_wild) {
6699 * Only one file needs to be copied. Append the mask back onto
6702 TALLOC_FREE(smb_fname_src->base_name);
6703 smb_fname_src->base_name = talloc_asprintf(smb_fname_src,
6707 if (!smb_fname_src->base_name) {
6708 reply_nterror(req, NT_STATUS_NO_MEMORY);
6712 if (dest_has_wild) {
6713 char *fname_dst_mod = NULL;
6714 if (!resolve_wildcards(smb_fname_dst,
6715 smb_fname_src->base_name,
6716 smb_fname_dst->base_name,
6718 reply_nterror(req, NT_STATUS_NO_MEMORY);
6721 TALLOC_FREE(smb_fname_dst->base_name);
6722 smb_fname_dst->base_name = fname_dst_mod;
6725 status = check_name(conn, smb_fname_src->base_name);
6726 if (!NT_STATUS_IS_OK(status)) {
6727 reply_nterror(req, status);
6731 status = check_name(conn, smb_fname_dst->base_name);
6732 if (!NT_STATUS_IS_OK(status)) {
6733 reply_nterror(req, status);
6737 status = copy_file(ctx, conn, smb_fname_src, smb_fname_dst,
6738 ofun, count, target_is_directory);
6740 if(!NT_STATUS_IS_OK(status)) {
6741 reply_nterror(req, status);
6747 struct smb_Dir *dir_hnd = NULL;
6748 const char *dname = NULL;
6749 char *talloced = NULL;
6753 * There is a wildcard that requires us to actually read the
6754 * src dir and copy each file matching the mask to the dst.
6755 * Right now streams won't be copied, but this could
6756 * presumably be added with a nested loop for reach dir entry.
6758 SMB_ASSERT(!smb_fname_src->stream_name);
6759 SMB_ASSERT(!smb_fname_dst->stream_name);
6761 smb_fname_src->stream_name = NULL;
6762 smb_fname_dst->stream_name = NULL;
6764 if (strequal(fname_src_mask,"????????.???")) {
6765 TALLOC_FREE(fname_src_mask);
6766 fname_src_mask = talloc_strdup(ctx, "*");
6767 if (!fname_src_mask) {
6768 reply_nterror(req, NT_STATUS_NO_MEMORY);
6773 status = check_name(conn, fname_src_dir);
6774 if (!NT_STATUS_IS_OK(status)) {
6775 reply_nterror(req, status);
6779 dir_hnd = OpenDir(ctx, conn, fname_src_dir, fname_src_mask, 0);
6780 if (dir_hnd == NULL) {
6781 status = map_nt_error_from_unix(errno);
6782 reply_nterror(req, status);
6788 /* Iterate over the src dir copying each entry to the dst. */
6789 while ((dname = ReadDirName(dir_hnd, &offset,
6790 &smb_fname_src->st, &talloced))) {
6791 char *destname = NULL;
6793 if (ISDOT(dname) || ISDOTDOT(dname)) {
6794 TALLOC_FREE(talloced);
6798 if (!is_visible_file(conn, fname_src_dir, dname,
6799 &smb_fname_src->st, false)) {
6800 TALLOC_FREE(talloced);
6804 if(!mask_match(dname, fname_src_mask,
6805 conn->case_sensitive)) {
6806 TALLOC_FREE(talloced);
6810 error = ERRnoaccess;
6812 /* Get the src smb_fname struct setup. */
6813 TALLOC_FREE(smb_fname_src->base_name);
6814 smb_fname_src->base_name =
6815 talloc_asprintf(smb_fname_src, "%s/%s",
6816 fname_src_dir, dname);
6818 if (!smb_fname_src->base_name) {
6819 TALLOC_FREE(dir_hnd);
6820 TALLOC_FREE(talloced);
6821 reply_nterror(req, NT_STATUS_NO_MEMORY);
6825 if (!resolve_wildcards(ctx, smb_fname_src->base_name,
6826 smb_fname_dst->base_name,
6828 TALLOC_FREE(talloced);
6832 TALLOC_FREE(dir_hnd);
6833 TALLOC_FREE(talloced);
6834 reply_nterror(req, NT_STATUS_NO_MEMORY);
6838 TALLOC_FREE(smb_fname_dst->base_name);
6839 smb_fname_dst->base_name = destname;
6841 status = check_name(conn, smb_fname_src->base_name);
6842 if (!NT_STATUS_IS_OK(status)) {
6843 TALLOC_FREE(dir_hnd);
6844 TALLOC_FREE(talloced);
6845 reply_nterror(req, status);
6849 status = check_name(conn, smb_fname_dst->base_name);
6850 if (!NT_STATUS_IS_OK(status)) {
6851 TALLOC_FREE(dir_hnd);
6852 TALLOC_FREE(talloced);
6853 reply_nterror(req, status);
6857 DEBUG(3,("reply_copy : doing copy on %s -> %s\n",
6858 smb_fname_src->base_name,
6859 smb_fname_dst->base_name));
6861 status = copy_file(ctx, conn, smb_fname_src,
6862 smb_fname_dst, ofun, count,
6863 target_is_directory);
6864 if (NT_STATUS_IS_OK(status)) {
6868 TALLOC_FREE(talloced);
6870 TALLOC_FREE(dir_hnd);
6874 reply_nterror(req, dos_to_ntstatus(ERRDOS, error));
6878 reply_outbuf(req, 1, 0);
6879 SSVAL(req->outbuf,smb_vwv0,count);
6881 TALLOC_FREE(smb_fname_src);
6882 TALLOC_FREE(smb_fname_dst);
6883 TALLOC_FREE(fname_src);
6884 TALLOC_FREE(fname_dst);
6885 TALLOC_FREE(fname_src_mask);
6886 TALLOC_FREE(fname_src_dir);
6888 END_PROFILE(SMBcopy);
6893 #define DBGC_CLASS DBGC_LOCKING
6895 /****************************************************************************
6896 Get a lock pid, dealing with large count requests.
6897 ****************************************************************************/
6899 uint32 get_lock_pid(const uint8_t *data, int data_offset,
6900 bool large_file_format)
6902 if(!large_file_format)
6903 return (uint32)SVAL(data,SMB_LPID_OFFSET(data_offset));
6905 return (uint32)SVAL(data,SMB_LARGE_LPID_OFFSET(data_offset));
6908 /****************************************************************************
6909 Get a lock count, dealing with large count requests.
6910 ****************************************************************************/
6912 uint64_t get_lock_count(const uint8_t *data, int data_offset,
6913 bool large_file_format)
6917 if(!large_file_format) {
6918 count = (uint64_t)IVAL(data,SMB_LKLEN_OFFSET(data_offset));
6921 #if defined(HAVE_LONGLONG)
6922 count = (((uint64_t) IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset))) << 32) |
6923 ((uint64_t) IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)));
6924 #else /* HAVE_LONGLONG */
6927 * NT4.x seems to be broken in that it sends large file (64 bit)
6928 * lockingX calls even if the CAP_LARGE_FILES was *not*
6929 * negotiated. For boxes without large unsigned ints truncate the
6930 * lock count by dropping the top 32 bits.
6933 if(IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)) != 0) {
6934 DEBUG(3,("get_lock_count: truncating lock count (high)0x%x (low)0x%x to just low count.\n",
6935 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)),
6936 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)) ));
6937 SIVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset),0);
6940 count = (uint64_t)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset));
6941 #endif /* HAVE_LONGLONG */
6947 #if !defined(HAVE_LONGLONG)
6948 /****************************************************************************
6949 Pathetically try and map a 64 bit lock offset into 31 bits. I hate Windows :-).
6950 ****************************************************************************/
6952 static uint32 map_lock_offset(uint32 high, uint32 low)
6956 uint32 highcopy = high;
6959 * Try and find out how many significant bits there are in high.
6962 for(i = 0; highcopy; i++)
6966 * We use 31 bits not 32 here as POSIX
6967 * lock offsets may not be negative.
6970 mask = (~0) << (31 - i);
6973 return 0; /* Fail. */
6979 #endif /* !defined(HAVE_LONGLONG) */
6981 /****************************************************************************
6982 Get a lock offset, dealing with large offset requests.
6983 ****************************************************************************/
6985 uint64_t get_lock_offset(const uint8_t *data, int data_offset,
6986 bool large_file_format, bool *err)
6988 uint64_t offset = 0;
6992 if(!large_file_format) {
6993 offset = (uint64_t)IVAL(data,SMB_LKOFF_OFFSET(data_offset));
6996 #if defined(HAVE_LONGLONG)
6997 offset = (((uint64_t) IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset))) << 32) |
6998 ((uint64_t) IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset)));
6999 #else /* HAVE_LONGLONG */
7002 * NT4.x seems to be broken in that it sends large file (64 bit)
7003 * lockingX calls even if the CAP_LARGE_FILES was *not*
7004 * negotiated. For boxes without large unsigned ints mangle the
7005 * lock offset by mapping the top 32 bits onto the lower 32.
7008 if(IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset)) != 0) {
7009 uint32 low = IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
7010 uint32 high = IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset));
7013 if((new_low = map_lock_offset(high, low)) == 0) {
7015 return (uint64_t)-1;
7018 DEBUG(3,("get_lock_offset: truncating lock offset (high)0x%x (low)0x%x to offset 0x%x.\n",
7019 (unsigned int)high, (unsigned int)low, (unsigned int)new_low ));
7020 SIVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset),0);
7021 SIVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset),new_low);
7024 offset = (uint64_t)IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
7025 #endif /* HAVE_LONGLONG */
7031 NTSTATUS smbd_do_locking(struct smb_request *req,
7035 uint16_t num_ulocks,
7036 struct smbd_lock_element *ulocks,
7038 struct smbd_lock_element *locks,
7041 connection_struct *conn = req->conn;
7043 NTSTATUS status = NT_STATUS_OK;
7047 /* Data now points at the beginning of the list
7048 of smb_unlkrng structs */
7049 for(i = 0; i < (int)num_ulocks; i++) {
7050 struct smbd_lock_element *e = &ulocks[i];
7052 DEBUG(10,("smbd_do_locking: unlock start=%.0f, len=%.0f for "
7053 "pid %u, file %s\n",
7056 (unsigned int)e->smbpid,
7059 if (e->brltype != UNLOCK_LOCK) {
7060 /* this can only happen with SMB2 */
7061 return NT_STATUS_INVALID_PARAMETER;
7064 status = do_unlock(smbd_messaging_context(),
7071 DEBUG(10, ("smbd_do_locking: unlock returned %s\n",
7072 nt_errstr(status)));
7074 if (!NT_STATUS_IS_OK(status)) {
7079 /* Setup the timeout in seconds. */
7081 if (!lp_blocking_locks(SNUM(conn))) {
7085 /* Data now points at the beginning of the list
7086 of smb_lkrng structs */
7088 for(i = 0; i < (int)num_locks; i++) {
7089 struct smbd_lock_element *e = &locks[i];
7091 DEBUG(10,("smbd_do_locking: lock start=%.0f, len=%.0f for pid "
7092 "%u, file %s timeout = %d\n",
7095 (unsigned int)e->smbpid,
7099 if (type & LOCKING_ANDX_CANCEL_LOCK) {
7100 struct blocking_lock_record *blr = NULL;
7102 if (num_locks > 1) {
7104 * MS-CIFS (2.2.4.32.1) states that a cancel is honored if and only
7105 * if the lock vector contains one entry. When given mutliple cancel
7106 * requests in a single PDU we expect the server to return an
7107 * error. Windows servers seem to accept the request but only
7108 * cancel the first lock.
7109 * JRA - Do what Windows does (tm) :-).
7113 /* MS-CIFS (2.2.4.32.1) behavior. */
7114 return NT_STATUS_DOS(ERRDOS,
7115 ERRcancelviolation);
7117 /* Windows behavior. */
7119 DEBUG(10,("smbd_do_locking: ignoring subsequent "
7120 "cancel request\n"));
7126 if (lp_blocking_locks(SNUM(conn))) {
7128 /* Schedule a message to ourselves to
7129 remove the blocking lock record and
7130 return the right error. */
7132 blr = blocking_lock_cancel(fsp,
7138 NT_STATUS_FILE_LOCK_CONFLICT);
7140 return NT_STATUS_DOS(
7142 ERRcancelviolation);
7145 /* Remove a matching pending lock. */
7146 status = do_lock_cancel(fsp,
7153 bool blocking_lock = timeout ? true : false;
7154 bool defer_lock = false;
7155 struct byte_range_lock *br_lck;
7156 uint32_t block_smbpid;
7158 br_lck = do_lock(smbd_messaging_context(),
7170 if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
7171 /* Windows internal resolution for blocking locks seems
7172 to be about 200ms... Don't wait for less than that. JRA. */
7173 if (timeout != -1 && timeout < lp_lock_spin_time()) {
7174 timeout = lp_lock_spin_time();
7179 /* If a lock sent with timeout of zero would fail, and
7180 * this lock has been requested multiple times,
7181 * according to brl_lock_failed() we convert this
7182 * request to a blocking lock with a timeout of between
7183 * 150 - 300 milliseconds.
7185 * If lp_lock_spin_time() has been set to 0, we skip
7186 * this blocking retry and fail immediately.
7188 * Replacement for do_lock_spin(). JRA. */
7190 if (br_lck && lp_blocking_locks(SNUM(conn)) &&
7191 lp_lock_spin_time() && !blocking_lock &&
7192 NT_STATUS_EQUAL((status),
7193 NT_STATUS_FILE_LOCK_CONFLICT))
7196 timeout = lp_lock_spin_time();
7199 if (br_lck && defer_lock) {
7201 * A blocking lock was requested. Package up
7202 * this smb into a queued request and push it
7203 * onto the blocking lock queue.
7205 if(push_blocking_lock_request(br_lck,
7216 TALLOC_FREE(br_lck);
7218 return NT_STATUS_OK;
7222 TALLOC_FREE(br_lck);
7225 if (!NT_STATUS_IS_OK(status)) {
7230 /* If any of the above locks failed, then we must unlock
7231 all of the previous locks (X/Open spec). */
7233 if (num_locks != 0 && !NT_STATUS_IS_OK(status)) {
7235 if (type & LOCKING_ANDX_CANCEL_LOCK) {
7236 i = -1; /* we want to skip the for loop */
7240 * Ensure we don't do a remove on the lock that just failed,
7241 * as under POSIX rules, if we have a lock already there, we
7242 * will delete it (and we shouldn't) .....
7244 for(i--; i >= 0; i--) {
7245 struct smbd_lock_element *e = &locks[i];
7247 do_unlock(smbd_messaging_context(),
7257 DEBUG(3, ("smbd_do_locking: fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
7258 fsp->fnum, (unsigned int)type, num_locks, num_ulocks));
7260 return NT_STATUS_OK;
7263 /****************************************************************************
7264 Reply to a lockingX request.
7265 ****************************************************************************/
7267 void reply_lockingX(struct smb_request *req)
7269 connection_struct *conn = req->conn;
7271 unsigned char locktype;
7272 unsigned char oplocklevel;
7277 const uint8_t *data;
7278 bool large_file_format;
7280 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
7281 struct smbd_lock_element *ulocks;
7282 struct smbd_lock_element *locks;
7285 START_PROFILE(SMBlockingX);
7288 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7289 END_PROFILE(SMBlockingX);
7293 fsp = file_fsp(req, SVAL(req->vwv+2, 0));
7294 locktype = CVAL(req->vwv+3, 0);
7295 oplocklevel = CVAL(req->vwv+3, 1);
7296 num_ulocks = SVAL(req->vwv+6, 0);
7297 num_locks = SVAL(req->vwv+7, 0);
7298 lock_timeout = IVAL(req->vwv+4, 0);
7299 large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
7301 if (!check_fsp(conn, req, fsp)) {
7302 END_PROFILE(SMBlockingX);
7308 if (locktype & LOCKING_ANDX_CHANGE_LOCKTYPE) {
7309 /* we don't support these - and CANCEL_LOCK makes w2k
7310 and XP reboot so I don't really want to be
7311 compatible! (tridge) */
7312 reply_force_doserror(req, ERRDOS, ERRnoatomiclocks);
7313 END_PROFILE(SMBlockingX);
7317 /* Check if this is an oplock break on a file
7318 we have granted an oplock on.
7320 if ((locktype & LOCKING_ANDX_OPLOCK_RELEASE)) {
7321 /* Client can insist on breaking to none. */
7322 bool break_to_none = (oplocklevel == 0);
7325 DEBUG(5,("reply_lockingX: oplock break reply (%u) from client "
7326 "for fnum = %d\n", (unsigned int)oplocklevel,
7330 * Make sure we have granted an exclusive or batch oplock on
7334 if (fsp->oplock_type == 0) {
7336 /* The Samba4 nbench simulator doesn't understand
7337 the difference between break to level2 and break
7338 to none from level2 - it sends oplock break
7339 replies in both cases. Don't keep logging an error
7340 message here - just ignore it. JRA. */
7342 DEBUG(5,("reply_lockingX: Error : oplock break from "
7343 "client for fnum = %d (oplock=%d) and no "
7344 "oplock granted on this file (%s).\n",
7345 fsp->fnum, fsp->oplock_type,
7348 /* if this is a pure oplock break request then don't
7350 if (num_locks == 0 && num_ulocks == 0) {
7351 END_PROFILE(SMBlockingX);
7354 END_PROFILE(SMBlockingX);
7355 reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
7360 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
7362 result = remove_oplock(fsp);
7364 result = downgrade_oplock(fsp);
7368 DEBUG(0, ("reply_lockingX: error in removing "
7369 "oplock on file %s\n", fsp_str_dbg(fsp)));
7370 /* Hmmm. Is this panic justified? */
7371 smb_panic("internal tdb error");
7374 reply_to_oplock_break_requests(fsp);
7376 /* if this is a pure oplock break request then don't send a
7378 if (num_locks == 0 && num_ulocks == 0) {
7379 /* Sanity check - ensure a pure oplock break is not a
7381 if(CVAL(req->vwv+0, 0) != 0xff)
7382 DEBUG(0,("reply_lockingX: Error : pure oplock "
7383 "break is a chained %d request !\n",
7384 (unsigned int)CVAL(req->vwv+0, 0)));
7385 END_PROFILE(SMBlockingX);
7391 (num_ulocks + num_locks) * (large_file_format ? 20 : 10)) {
7392 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7393 END_PROFILE(SMBlockingX);
7397 ulocks = talloc_array(req, struct smbd_lock_element, num_ulocks);
7398 if (ulocks == NULL) {
7399 reply_nterror(req, NT_STATUS_NO_MEMORY);
7400 END_PROFILE(SMBlockingX);
7404 locks = talloc_array(req, struct smbd_lock_element, num_locks);
7405 if (locks == NULL) {
7406 reply_nterror(req, NT_STATUS_NO_MEMORY);
7407 END_PROFILE(SMBlockingX);
7411 /* Data now points at the beginning of the list
7412 of smb_unlkrng structs */
7413 for(i = 0; i < (int)num_ulocks; i++) {
7414 ulocks[i].smbpid = get_lock_pid(data, i, large_file_format);
7415 ulocks[i].count = get_lock_count(data, i, large_file_format);
7416 ulocks[i].offset = get_lock_offset(data, i, large_file_format, &err);
7417 ulocks[i].brltype = UNLOCK_LOCK;
7420 * There is no error code marked "stupid client bug".... :-).
7423 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
7424 END_PROFILE(SMBlockingX);
7429 /* Now do any requested locks */
7430 data += ((large_file_format ? 20 : 10)*num_ulocks);
7432 /* Data now points at the beginning of the list
7433 of smb_lkrng structs */
7435 for(i = 0; i < (int)num_locks; i++) {
7436 locks[i].smbpid = get_lock_pid(data, i, large_file_format);
7437 locks[i].count = get_lock_count(data, i, large_file_format);
7438 locks[i].offset = get_lock_offset(data, i, large_file_format, &err);
7440 if (locktype & LOCKING_ANDX_SHARED_LOCK) {
7441 if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
7442 locks[i].brltype = PENDING_READ_LOCK;
7444 locks[i].brltype = READ_LOCK;
7447 if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
7448 locks[i].brltype = PENDING_WRITE_LOCK;
7450 locks[i].brltype = WRITE_LOCK;
7455 * There is no error code marked "stupid client bug".... :-).
7458 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
7459 END_PROFILE(SMBlockingX);
7464 status = smbd_do_locking(req, fsp,
7465 locktype, lock_timeout,
7469 if (!NT_STATUS_IS_OK(status)) {
7470 END_PROFILE(SMBlockingX);
7471 reply_nterror(req, status);
7475 END_PROFILE(SMBlockingX);
7479 reply_outbuf(req, 2, 0);
7481 DEBUG(3, ("lockingX fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
7482 fsp->fnum, (unsigned int)locktype, num_locks, num_ulocks));
7484 END_PROFILE(SMBlockingX);
7489 #define DBGC_CLASS DBGC_ALL
7491 /****************************************************************************
7492 Reply to a SMBreadbmpx (read block multiplex) request.
7493 Always reply with an error, if someone has a platform really needs this,
7494 please contact vl@samba.org
7495 ****************************************************************************/
7497 void reply_readbmpx(struct smb_request *req)
7499 START_PROFILE(SMBreadBmpx);
7500 reply_force_doserror(req, ERRSRV, ERRuseSTD);
7501 END_PROFILE(SMBreadBmpx);
7505 /****************************************************************************
7506 Reply to a SMBreadbs (read block multiplex secondary) request.
7507 Always reply with an error, if someone has a platform really needs this,
7508 please contact vl@samba.org
7509 ****************************************************************************/
7511 void reply_readbs(struct smb_request *req)
7513 START_PROFILE(SMBreadBs);
7514 reply_force_doserror(req, ERRSRV, ERRuseSTD);
7515 END_PROFILE(SMBreadBs);
7519 /****************************************************************************
7520 Reply to a SMBsetattrE.
7521 ****************************************************************************/
7523 void reply_setattrE(struct smb_request *req)
7525 connection_struct *conn = req->conn;
7526 struct smb_file_time ft;
7530 START_PROFILE(SMBsetattrE);
7534 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7538 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
7540 if(!fsp || (fsp->conn != conn)) {
7541 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
7546 * Convert the DOS times into unix times.
7549 ft.atime = convert_time_t_to_timespec(
7550 srv_make_unix_date2(req->vwv+3));
7551 ft.mtime = convert_time_t_to_timespec(
7552 srv_make_unix_date2(req->vwv+5));
7553 ft.create_time = convert_time_t_to_timespec(
7554 srv_make_unix_date2(req->vwv+1));
7556 reply_outbuf(req, 0, 0);
7559 * Patch from Ray Frush <frush@engr.colostate.edu>
7560 * Sometimes times are sent as zero - ignore them.
7563 /* Ensure we have a valid stat struct for the source. */
7564 status = vfs_stat_fsp(fsp);
7565 if (!NT_STATUS_IS_OK(status)) {
7566 reply_nterror(req, status);
7570 status = smb_set_file_time(conn, fsp, fsp->fsp_name, &ft, true);
7571 if (!NT_STATUS_IS_OK(status)) {
7572 reply_nterror(req, status);
7576 DEBUG( 3, ( "reply_setattrE fnum=%d actime=%u modtime=%u "
7579 (unsigned int)ft.atime.tv_sec,
7580 (unsigned int)ft.mtime.tv_sec,
7581 (unsigned int)ft.create_time.tv_sec
7584 END_PROFILE(SMBsetattrE);
7589 /* Back from the dead for OS/2..... JRA. */
7591 /****************************************************************************
7592 Reply to a SMBwritebmpx (write block multiplex primary) request.
7593 Always reply with an error, if someone has a platform really needs this,
7594 please contact vl@samba.org
7595 ****************************************************************************/
7597 void reply_writebmpx(struct smb_request *req)
7599 START_PROFILE(SMBwriteBmpx);
7600 reply_force_doserror(req, ERRSRV, ERRuseSTD);
7601 END_PROFILE(SMBwriteBmpx);
7605 /****************************************************************************
7606 Reply to a SMBwritebs (write block multiplex secondary) request.
7607 Always reply with an error, if someone has a platform really needs this,
7608 please contact vl@samba.org
7609 ****************************************************************************/
7611 void reply_writebs(struct smb_request *req)
7613 START_PROFILE(SMBwriteBs);
7614 reply_force_doserror(req, ERRSRV, ERRuseSTD);
7615 END_PROFILE(SMBwriteBs);
7619 /****************************************************************************
7620 Reply to a SMBgetattrE.
7621 ****************************************************************************/
7623 void reply_getattrE(struct smb_request *req)
7625 connection_struct *conn = req->conn;
7628 struct timespec create_ts;
7630 START_PROFILE(SMBgetattrE);
7633 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7634 END_PROFILE(SMBgetattrE);
7638 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
7640 if(!fsp || (fsp->conn != conn)) {
7641 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
7642 END_PROFILE(SMBgetattrE);
7646 /* Do an fstat on this file */
7648 reply_nterror(req, map_nt_error_from_unix(errno));
7649 END_PROFILE(SMBgetattrE);
7653 mode = dos_mode(conn, fsp->fsp_name);
7656 * Convert the times into dos times. Set create
7657 * date to be last modify date as UNIX doesn't save
7661 reply_outbuf(req, 11, 0);
7663 create_ts = get_create_timespec(conn, fsp, fsp->fsp_name);
7664 srv_put_dos_date2((char *)req->outbuf, smb_vwv0, create_ts.tv_sec);
7665 srv_put_dos_date2((char *)req->outbuf, smb_vwv2,
7666 convert_timespec_to_time_t(fsp->fsp_name->st.st_ex_atime));
7667 /* Should we check pending modtime here ? JRA */
7668 srv_put_dos_date2((char *)req->outbuf, smb_vwv4,
7669 convert_timespec_to_time_t(fsp->fsp_name->st.st_ex_mtime));
7672 SIVAL(req->outbuf, smb_vwv6, 0);
7673 SIVAL(req->outbuf, smb_vwv8, 0);
7675 uint32 allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn,fsp, &fsp->fsp_name->st);
7676 SIVAL(req->outbuf, smb_vwv6, (uint32)fsp->fsp_name->st.st_ex_size);
7677 SIVAL(req->outbuf, smb_vwv8, allocation_size);
7679 SSVAL(req->outbuf,smb_vwv10, mode);
7681 DEBUG( 3, ( "reply_getattrE fnum=%d\n", fsp->fnum));
7683 END_PROFILE(SMBgetattrE);