recvfile can't be used for printing so far
[Samba/bb.git] / source / smbd / reply.c
blobf78fb33fc8e22ef2afff4bb41440cfd7290494b0
1 /*
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
27 #include "includes.h"
29 /* look in server.c for some explanation of these variables */
30 extern enum protocol_types Protocol;
31 extern int max_recv;
32 extern uint32 global_client_caps;
34 extern bool global_encrypted_passwords_negotiated;
36 /****************************************************************************
37 Ensure we check the path in *exactly* the same way as W2K for a findfirst/findnext
38 path or anything including wildcards.
39 We're assuming here that '/' is not the second byte in any multibyte char
40 set (a safe assumption). '\\' *may* be the second byte in a multibyte char
41 set.
42 ****************************************************************************/
44 /* Custom version for processing POSIX paths. */
45 #define IS_PATH_SEP(c,posix_only) ((c) == '/' || (!(posix_only) && (c) == '\\'))
47 static NTSTATUS check_path_syntax_internal(char *path,
48 bool posix_path,
49 bool *p_last_component_contains_wcard)
51 char *d = path;
52 const char *s = path;
53 NTSTATUS ret = NT_STATUS_OK;
54 bool start_of_name_component = True;
55 bool stream_started = false;
57 *p_last_component_contains_wcard = False;
59 while (*s) {
60 if (stream_started) {
61 switch (*s) {
62 case '/':
63 case '\\':
64 return NT_STATUS_OBJECT_NAME_INVALID;
65 case ':':
66 if (s[1] == '\0') {
67 return NT_STATUS_OBJECT_NAME_INVALID;
69 if (strchr_m(&s[1], ':')) {
70 return NT_STATUS_OBJECT_NAME_INVALID;
72 if (StrCaseCmp(s, ":$DATA") != 0) {
73 return NT_STATUS_INVALID_PARAMETER;
75 break;
79 if (!stream_started && *s == ':') {
80 if (*p_last_component_contains_wcard) {
81 return NT_STATUS_OBJECT_NAME_INVALID;
83 /* stream names allow more characters than file names */
84 stream_started = true;
85 start_of_name_component = false;
86 posix_path = true;
88 if (s[1] == '\0') {
89 return NT_STATUS_OBJECT_NAME_INVALID;
93 if (!stream_started && IS_PATH_SEP(*s,posix_path)) {
95 * Safe to assume is not the second part of a mb char
96 * as this is handled below.
98 /* Eat multiple '/' or '\\' */
99 while (IS_PATH_SEP(*s,posix_path)) {
100 s++;
102 if ((d != path) && (*s != '\0')) {
103 /* We only care about non-leading or trailing '/' or '\\' */
104 *d++ = '/';
107 start_of_name_component = True;
108 /* New component. */
109 *p_last_component_contains_wcard = False;
110 continue;
113 if (start_of_name_component) {
114 if ((s[0] == '.') && (s[1] == '.') && (IS_PATH_SEP(s[2],posix_path) || s[2] == '\0')) {
115 /* Uh oh - "/../" or "\\..\\" or "/..\0" or "\\..\0" ! */
118 * No mb char starts with '.' so we're safe checking the directory separator here.
121 /* If we just added a '/' - delete it */
122 if ((d > path) && (*(d-1) == '/')) {
123 *(d-1) = '\0';
124 d--;
127 /* Are we at the start ? Can't go back further if so. */
128 if (d <= path) {
129 ret = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
130 break;
132 /* Go back one level... */
133 /* We know this is safe as '/' cannot be part of a mb sequence. */
134 /* NOTE - if this assumption is invalid we are not in good shape... */
135 /* Decrement d first as d points to the *next* char to write into. */
136 for (d--; d > path; d--) {
137 if (*d == '/')
138 break;
140 s += 2; /* Else go past the .. */
141 /* We're still at the start of a name component, just the previous one. */
142 continue;
144 } else if ((s[0] == '.') && ((s[1] == '\0') || IS_PATH_SEP(s[1],posix_path))) {
145 if (posix_path) {
146 /* Eat the '.' */
147 s++;
148 continue;
154 if (!(*s & 0x80)) {
155 if (!posix_path) {
156 if (*s <= 0x1f || *s == '|') {
157 return NT_STATUS_OBJECT_NAME_INVALID;
159 switch (*s) {
160 case '*':
161 case '?':
162 case '<':
163 case '>':
164 case '"':
165 *p_last_component_contains_wcard = True;
166 break;
167 default:
168 break;
171 *d++ = *s++;
172 } else {
173 size_t siz;
174 /* Get the size of the next MB character. */
175 next_codepoint(s,&siz);
176 switch(siz) {
177 case 5:
178 *d++ = *s++;
179 /*fall through*/
180 case 4:
181 *d++ = *s++;
182 /*fall through*/
183 case 3:
184 *d++ = *s++;
185 /*fall through*/
186 case 2:
187 *d++ = *s++;
188 /*fall through*/
189 case 1:
190 *d++ = *s++;
191 break;
192 default:
193 DEBUG(0,("check_path_syntax_internal: character length assumptions invalid !\n"));
194 *d = '\0';
195 return NT_STATUS_INVALID_PARAMETER;
198 start_of_name_component = False;
201 *d = '\0';
203 return ret;
206 /****************************************************************************
207 Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
208 No wildcards allowed.
209 ****************************************************************************/
211 NTSTATUS check_path_syntax(char *path)
213 bool ignore;
214 return check_path_syntax_internal(path, False, &ignore);
217 /****************************************************************************
218 Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
219 Wildcards allowed - p_contains_wcard returns true if the last component contained
220 a wildcard.
221 ****************************************************************************/
223 NTSTATUS check_path_syntax_wcard(char *path, bool *p_contains_wcard)
225 return check_path_syntax_internal(path, False, p_contains_wcard);
228 /****************************************************************************
229 Check the path for a POSIX client.
230 We're assuming here that '/' is not the second byte in any multibyte char
231 set (a safe assumption).
232 ****************************************************************************/
234 NTSTATUS check_path_syntax_posix(char *path)
236 bool ignore;
237 return check_path_syntax_internal(path, True, &ignore);
240 /****************************************************************************
241 Pull a string and check the path allowing a wilcard - provide for error return.
242 ****************************************************************************/
244 size_t srvstr_get_path_wcard(TALLOC_CTX *ctx,
245 const char *inbuf,
246 uint16 smb_flags2,
247 char **pp_dest,
248 const char *src,
249 size_t src_len,
250 int flags,
251 NTSTATUS *err,
252 bool *contains_wcard)
254 size_t ret;
256 *pp_dest = NULL;
258 if (src_len == 0) {
259 ret = srvstr_pull_buf_talloc(ctx,
260 inbuf,
261 smb_flags2,
262 pp_dest,
263 src,
264 flags);
265 } else {
266 ret = srvstr_pull_talloc(ctx,
267 inbuf,
268 smb_flags2,
269 pp_dest,
270 src,
271 src_len,
272 flags);
275 if (!*pp_dest) {
276 *err = NT_STATUS_INVALID_PARAMETER;
277 return ret;
280 *contains_wcard = False;
282 if (smb_flags2 & FLAGS2_DFS_PATHNAMES) {
284 * For a DFS path the function parse_dfs_path()
285 * will do the path processing, just make a copy.
287 *err = NT_STATUS_OK;
288 return ret;
291 if (lp_posix_pathnames()) {
292 *err = check_path_syntax_posix(*pp_dest);
293 } else {
294 *err = check_path_syntax_wcard(*pp_dest, contains_wcard);
297 return ret;
300 /****************************************************************************
301 Pull a string and check the path - provide for error return.
302 ****************************************************************************/
304 size_t srvstr_get_path(TALLOC_CTX *ctx,
305 const char *inbuf,
306 uint16 smb_flags2,
307 char **pp_dest,
308 const char *src,
309 size_t src_len,
310 int flags,
311 NTSTATUS *err)
313 size_t ret;
315 *pp_dest = NULL;
317 if (src_len == 0) {
318 ret = srvstr_pull_buf_talloc(ctx,
319 inbuf,
320 smb_flags2,
321 pp_dest,
322 src,
323 flags);
324 } else {
325 ret = srvstr_pull_talloc(ctx,
326 inbuf,
327 smb_flags2,
328 pp_dest,
329 src,
330 src_len,
331 flags);
334 if (!*pp_dest) {
335 *err = NT_STATUS_INVALID_PARAMETER;
336 return ret;
339 if (smb_flags2 & FLAGS2_DFS_PATHNAMES) {
341 * For a DFS path the function parse_dfs_path()
342 * will do the path processing, just make a copy.
344 *err = NT_STATUS_OK;
345 return ret;
348 if (lp_posix_pathnames()) {
349 *err = check_path_syntax_posix(*pp_dest);
350 } else {
351 *err = check_path_syntax(*pp_dest);
354 return ret;
357 /****************************************************************************
358 Check if we have a correct fsp pointing to a file. Basic check for open fsp.
359 ****************************************************************************/
361 bool check_fsp_open(connection_struct *conn, struct smb_request *req,
362 files_struct *fsp)
364 if (!(fsp) || !(conn)) {
365 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
366 return False;
368 if (((conn) != (fsp)->conn) || req->vuid != (fsp)->vuid) {
369 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
370 return False;
372 return True;
375 /****************************************************************************
376 Check if we have a correct fsp pointing to a file.
377 ****************************************************************************/
379 bool check_fsp(connection_struct *conn, struct smb_request *req,
380 files_struct *fsp)
382 if (!check_fsp_open(conn, req, fsp)) {
383 return False;
385 if ((fsp)->is_directory) {
386 reply_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
387 return False;
389 if ((fsp)->fh->fd == -1) {
390 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
391 return False;
393 (fsp)->num_smb_operations++;
394 return True;
397 /****************************************************************************
398 Check if we have a correct fsp pointing to a quota fake file. Replacement for
399 the CHECK_NTQUOTA_HANDLE_OK macro.
400 ****************************************************************************/
402 bool check_fsp_ntquota_handle(connection_struct *conn, struct smb_request *req,
403 files_struct *fsp)
405 if (!check_fsp_open(conn, req, fsp)) {
406 return false;
409 if (fsp->is_directory) {
410 return false;
413 if (fsp->fake_file_handle == NULL) {
414 return false;
417 if (fsp->fake_file_handle->type != FAKE_FILE_TYPE_QUOTA) {
418 return false;
421 if (fsp->fake_file_handle->private_data == NULL) {
422 return false;
425 return true;
428 /****************************************************************************
429 Check if we have a correct fsp. Replacement for the FSP_BELONGS_CONN macro
430 ****************************************************************************/
432 bool fsp_belongs_conn(connection_struct *conn, struct smb_request *req,
433 files_struct *fsp)
435 if ((fsp) && (conn) && ((conn)==(fsp)->conn)
436 && (req->vuid == (fsp)->vuid)) {
437 return True;
440 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
441 return False;
444 /****************************************************************************
445 Reply to a (netbios-level) special message.
446 ****************************************************************************/
448 void reply_special(char *inbuf)
450 int msg_type = CVAL(inbuf,0);
451 int msg_flags = CVAL(inbuf,1);
452 fstring name1,name2;
453 char name_type = 0;
456 * We only really use 4 bytes of the outbuf, but for the smb_setlen
457 * calculation & friends (srv_send_smb uses that) we need the full smb
458 * header.
460 char outbuf[smb_size];
462 static bool already_got_session = False;
464 *name1 = *name2 = 0;
466 memset(outbuf, '\0', sizeof(outbuf));
468 smb_setlen(outbuf,0);
470 switch (msg_type) {
471 case 0x81: /* session request */
473 if (already_got_session) {
474 exit_server_cleanly("multiple session request not permitted");
477 SCVAL(outbuf,0,0x82);
478 SCVAL(outbuf,3,0);
479 if (name_len(inbuf+4) > 50 ||
480 name_len(inbuf+4 + name_len(inbuf + 4)) > 50) {
481 DEBUG(0,("Invalid name length in session request\n"));
482 return;
484 name_extract(inbuf,4,name1);
485 name_type = name_extract(inbuf,4 + name_len(inbuf + 4),name2);
486 DEBUG(2,("netbios connect: name1=%s name2=%s\n",
487 name1,name2));
489 set_local_machine_name(name1, True);
490 set_remote_machine_name(name2, True);
492 DEBUG(2,("netbios connect: local=%s remote=%s, name type = %x\n",
493 get_local_machine_name(), get_remote_machine_name(),
494 name_type));
496 if (name_type == 'R') {
497 /* We are being asked for a pathworks session ---
498 no thanks! */
499 SCVAL(outbuf, 0,0x83);
500 break;
503 /* only add the client's machine name to the list
504 of possibly valid usernames if we are operating
505 in share mode security */
506 if (lp_security() == SEC_SHARE) {
507 add_session_user(get_remote_machine_name());
510 reload_services(True);
511 reopen_logs();
513 already_got_session = True;
514 break;
516 case 0x89: /* session keepalive request
517 (some old clients produce this?) */
518 SCVAL(outbuf,0,SMBkeepalive);
519 SCVAL(outbuf,3,0);
520 break;
522 case 0x82: /* positive session response */
523 case 0x83: /* negative session response */
524 case 0x84: /* retarget session response */
525 DEBUG(0,("Unexpected session response\n"));
526 break;
528 case SMBkeepalive: /* session keepalive */
529 default:
530 return;
533 DEBUG(5,("init msg_type=0x%x msg_flags=0x%x\n",
534 msg_type, msg_flags));
536 srv_send_smb(smbd_server_fd(), outbuf, false);
537 return;
540 /****************************************************************************
541 Reply to a tcon.
542 conn POINTER CAN BE NULL HERE !
543 ****************************************************************************/
545 void reply_tcon(struct smb_request *req)
547 connection_struct *conn = req->conn;
548 const char *service;
549 char *service_buf = NULL;
550 char *password = NULL;
551 char *dev = NULL;
552 int pwlen=0;
553 NTSTATUS nt_status;
554 char *p;
555 DATA_BLOB password_blob;
556 TALLOC_CTX *ctx = talloc_tos();
558 START_PROFILE(SMBtcon);
560 if (smb_buflen(req->inbuf) < 4) {
561 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
562 END_PROFILE(SMBtcon);
563 return;
566 p = smb_buf(req->inbuf)+1;
567 p += srvstr_pull_buf_talloc(ctx, req->inbuf, req->flags2,
568 &service_buf, p, STR_TERMINATE) + 1;
569 pwlen = srvstr_pull_buf_talloc(ctx, req->inbuf, req->flags2,
570 &password, p, STR_TERMINATE) + 1;
571 p += pwlen;
572 p += srvstr_pull_buf_talloc(ctx, req->inbuf, req->flags2,
573 &dev, p, STR_TERMINATE) + 1;
575 if (service_buf == NULL || password == NULL || dev == NULL) {
576 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
577 END_PROFILE(SMBtcon);
578 return;
580 p = strrchr_m(service_buf,'\\');
581 if (p) {
582 service = p+1;
583 } else {
584 service = service_buf;
587 password_blob = data_blob(password, pwlen+1);
589 conn = make_connection(service,password_blob,dev,req->vuid,&nt_status);
590 req->conn = conn;
592 data_blob_clear_free(&password_blob);
594 if (!conn) {
595 reply_nterror(req, nt_status);
596 END_PROFILE(SMBtcon);
597 return;
600 reply_outbuf(req, 2, 0);
601 SSVAL(req->outbuf,smb_vwv0,max_recv);
602 SSVAL(req->outbuf,smb_vwv1,conn->cnum);
603 SSVAL(req->outbuf,smb_tid,conn->cnum);
605 DEBUG(3,("tcon service=%s cnum=%d\n",
606 service, conn->cnum));
608 END_PROFILE(SMBtcon);
609 return;
612 /****************************************************************************
613 Reply to a tcon and X.
614 conn POINTER CAN BE NULL HERE !
615 ****************************************************************************/
617 void reply_tcon_and_X(struct smb_request *req)
619 connection_struct *conn = req->conn;
620 char *service = NULL;
621 DATA_BLOB password;
622 TALLOC_CTX *ctx = talloc_tos();
623 /* what the cleint thinks the device is */
624 char *client_devicetype = NULL;
625 /* what the server tells the client the share represents */
626 const char *server_devicetype;
627 NTSTATUS nt_status;
628 int passlen;
629 char *path = NULL;
630 char *p, *q;
631 uint16 tcon_flags;
633 START_PROFILE(SMBtconX);
635 if (req->wct < 4) {
636 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
637 END_PROFILE(SMBtconX);
638 return;
641 passlen = SVAL(req->inbuf,smb_vwv3);
642 tcon_flags = SVAL(req->inbuf,smb_vwv2);
644 /* we might have to close an old one */
645 if ((tcon_flags & 0x1) && conn) {
646 close_cnum(conn,req->vuid);
647 req->conn = NULL;
648 conn = NULL;
651 if ((passlen > MAX_PASS_LEN) || (passlen >= smb_buflen(req->inbuf))) {
652 reply_doserror(req, ERRDOS, ERRbuftoosmall);
653 END_PROFILE(SMBtconX);
654 return;
657 if (global_encrypted_passwords_negotiated) {
658 password = data_blob_talloc(talloc_tos(), smb_buf(req->inbuf),
659 passlen);
660 if (lp_security() == SEC_SHARE) {
662 * Security = share always has a pad byte
663 * after the password.
665 p = smb_buf(req->inbuf) + passlen + 1;
666 } else {
667 p = smb_buf(req->inbuf) + passlen;
669 } else {
670 password = data_blob_talloc(talloc_tos(), smb_buf(req->inbuf),
671 passlen+1);
672 /* Ensure correct termination */
673 password.data[passlen]=0;
674 p = smb_buf(req->inbuf) + passlen + 1;
677 p += srvstr_pull_buf_talloc(ctx, req->inbuf, req->flags2, &path, p,
678 STR_TERMINATE);
680 if (path == NULL) {
681 data_blob_clear_free(&password);
682 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
683 END_PROFILE(SMBtconX);
684 return;
688 * the service name can be either: \\server\share
689 * or share directly like on the DELL PowerVault 705
691 if (*path=='\\') {
692 q = strchr_m(path+2,'\\');
693 if (!q) {
694 data_blob_clear_free(&password);
695 reply_doserror(req, ERRDOS, ERRnosuchshare);
696 END_PROFILE(SMBtconX);
697 return;
699 service = q+1;
700 } else {
701 service = path;
704 p += srvstr_pull_talloc(ctx, req->inbuf, req->flags2,
705 &client_devicetype, p,
706 MIN(6,smb_bufrem(req->inbuf, p)), STR_ASCII);
708 if (client_devicetype == NULL) {
709 data_blob_clear_free(&password);
710 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
711 END_PROFILE(SMBtconX);
712 return;
715 DEBUG(4,("Client requested device type [%s] for share [%s]\n", client_devicetype, service));
717 conn = make_connection(service, password, client_devicetype,
718 req->vuid, &nt_status);
719 req->conn =conn;
721 data_blob_clear_free(&password);
723 if (!conn) {
724 reply_nterror(req, nt_status);
725 END_PROFILE(SMBtconX);
726 return;
729 if ( IS_IPC(conn) )
730 server_devicetype = "IPC";
731 else if ( IS_PRINT(conn) )
732 server_devicetype = "LPT1:";
733 else
734 server_devicetype = "A:";
736 if (Protocol < PROTOCOL_NT1) {
737 reply_outbuf(req, 2, 0);
738 if (message_push_string(&req->outbuf, server_devicetype,
739 STR_TERMINATE|STR_ASCII) == -1) {
740 reply_nterror(req, NT_STATUS_NO_MEMORY);
741 END_PROFILE(SMBtconX);
742 return;
744 } else {
745 /* NT sets the fstype of IPC$ to the null string */
746 const char *fstype = IS_IPC(conn) ? "" : lp_fstype(SNUM(conn));
748 if (tcon_flags & TCONX_FLAG_EXTENDED_RESPONSE) {
749 /* Return permissions. */
750 uint32 perm1 = 0;
751 uint32 perm2 = 0;
753 reply_outbuf(req, 7, 0);
755 if (IS_IPC(conn)) {
756 perm1 = FILE_ALL_ACCESS;
757 perm2 = FILE_ALL_ACCESS;
758 } else {
759 perm1 = CAN_WRITE(conn) ?
760 SHARE_ALL_ACCESS :
761 SHARE_READ_ONLY;
764 SIVAL(req->outbuf, smb_vwv3, perm1);
765 SIVAL(req->outbuf, smb_vwv5, perm2);
766 } else {
767 reply_outbuf(req, 3, 0);
770 if ((message_push_string(&req->outbuf, server_devicetype,
771 STR_TERMINATE|STR_ASCII) == -1)
772 || (message_push_string(&req->outbuf, fstype,
773 STR_TERMINATE) == -1)) {
774 reply_nterror(req, NT_STATUS_NO_MEMORY);
775 END_PROFILE(SMBtconX);
776 return;
779 /* what does setting this bit do? It is set by NT4 and
780 may affect the ability to autorun mounted cdroms */
781 SSVAL(req->outbuf, smb_vwv2, SMB_SUPPORT_SEARCH_BITS|
782 (lp_csc_policy(SNUM(conn)) << 2));
784 init_dfsroot(conn, req->inbuf, req->outbuf);
788 DEBUG(3,("tconX service=%s \n",
789 service));
791 /* set the incoming and outgoing tid to the just created one */
792 SSVAL(req->inbuf,smb_tid,conn->cnum);
793 SSVAL(req->outbuf,smb_tid,conn->cnum);
795 END_PROFILE(SMBtconX);
797 chain_reply(req);
798 return;
801 /****************************************************************************
802 Reply to an unknown type.
803 ****************************************************************************/
805 void reply_unknown_new(struct smb_request *req, uint8 type)
807 DEBUG(0, ("unknown command type (%s): type=%d (0x%X)\n",
808 smb_fn_name(type), type, type));
809 reply_doserror(req, ERRSRV, ERRunknownsmb);
810 return;
813 /****************************************************************************
814 Reply to an ioctl.
815 conn POINTER CAN BE NULL HERE !
816 ****************************************************************************/
818 void reply_ioctl(struct smb_request *req)
820 connection_struct *conn = req->conn;
821 uint16 device;
822 uint16 function;
823 uint32 ioctl_code;
824 int replysize;
825 char *p;
827 START_PROFILE(SMBioctl);
829 if (req->wct < 3) {
830 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
831 END_PROFILE(SMBioctl);
832 return;
835 device = SVAL(req->inbuf,smb_vwv1);
836 function = SVAL(req->inbuf,smb_vwv2);
837 ioctl_code = (device << 16) + function;
839 DEBUG(4, ("Received IOCTL (code 0x%x)\n", ioctl_code));
841 switch (ioctl_code) {
842 case IOCTL_QUERY_JOB_INFO:
843 replysize = 32;
844 break;
845 default:
846 reply_doserror(req, ERRSRV, ERRnosupport);
847 END_PROFILE(SMBioctl);
848 return;
851 reply_outbuf(req, 8, replysize+1);
852 SSVAL(req->outbuf,smb_vwv1,replysize); /* Total data bytes returned */
853 SSVAL(req->outbuf,smb_vwv5,replysize); /* Data bytes this buffer */
854 SSVAL(req->outbuf,smb_vwv6,52); /* Offset to data */
855 p = smb_buf(req->outbuf);
856 memset(p, '\0', replysize+1); /* valgrind-safe. */
857 p += 1; /* Allow for alignment */
859 switch (ioctl_code) {
860 case IOCTL_QUERY_JOB_INFO:
862 files_struct *fsp = file_fsp(SVAL(req->inbuf,
863 smb_vwv0));
864 if (!fsp) {
865 reply_doserror(req, ERRDOS, ERRbadfid);
866 END_PROFILE(SMBioctl);
867 return;
869 SSVAL(p,0,fsp->rap_print_jobid); /* Job number */
870 srvstr_push((char *)req->outbuf, req->flags2, p+2,
871 global_myname(), 15,
872 STR_TERMINATE|STR_ASCII);
873 if (conn) {
874 srvstr_push((char *)req->outbuf, req->flags2,
875 p+18, lp_servicename(SNUM(conn)),
876 13, STR_TERMINATE|STR_ASCII);
877 } else {
878 memset(p+18, 0, 13);
880 break;
884 END_PROFILE(SMBioctl);
885 return;
888 /****************************************************************************
889 Strange checkpath NTSTATUS mapping.
890 ****************************************************************************/
892 static NTSTATUS map_checkpath_error(const char *inbuf, NTSTATUS status)
894 /* Strange DOS error code semantics only for checkpath... */
895 if (!(SVAL(inbuf,smb_flg2) & FLAGS2_32_BIT_ERROR_CODES)) {
896 if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_INVALID,status)) {
897 /* We need to map to ERRbadpath */
898 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
901 return status;
904 /****************************************************************************
905 Reply to a checkpath.
906 ****************************************************************************/
908 void reply_checkpath(struct smb_request *req)
910 connection_struct *conn = req->conn;
911 char *name = NULL;
912 SMB_STRUCT_STAT sbuf;
913 NTSTATUS status;
914 TALLOC_CTX *ctx = talloc_tos();
916 START_PROFILE(SMBcheckpath);
918 srvstr_get_path(ctx,(char *)req->inbuf, req->flags2, &name,
919 smb_buf(req->inbuf) + 1, 0,
920 STR_TERMINATE, &status);
921 if (!NT_STATUS_IS_OK(status)) {
922 status = map_checkpath_error((char *)req->inbuf, status);
923 reply_nterror(req, status);
924 END_PROFILE(SMBcheckpath);
925 return;
928 status = resolve_dfspath(ctx, conn,
929 req->flags2 & FLAGS2_DFS_PATHNAMES,
930 name,
931 &name);
932 if (!NT_STATUS_IS_OK(status)) {
933 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
934 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
935 ERRSRV, ERRbadpath);
936 END_PROFILE(SMBcheckpath);
937 return;
939 goto path_err;
942 DEBUG(3,("reply_checkpath %s mode=%d\n", name, (int)SVAL(req->inbuf,smb_vwv0)));
944 status = unix_convert(ctx, conn, name, False, &name, NULL, &sbuf);
945 if (!NT_STATUS_IS_OK(status)) {
946 goto path_err;
949 status = check_name(conn, name);
950 if (!NT_STATUS_IS_OK(status)) {
951 DEBUG(3,("reply_checkpath: check_name of %s failed (%s)\n",name,nt_errstr(status)));
952 goto path_err;
955 if (!VALID_STAT(sbuf) && (SMB_VFS_STAT(conn,name,&sbuf) != 0)) {
956 DEBUG(3,("reply_checkpath: stat of %s failed (%s)\n",name,strerror(errno)));
957 status = map_nt_error_from_unix(errno);
958 goto path_err;
961 if (!S_ISDIR(sbuf.st_mode)) {
962 reply_botherror(req, NT_STATUS_NOT_A_DIRECTORY,
963 ERRDOS, ERRbadpath);
964 END_PROFILE(SMBcheckpath);
965 return;
968 reply_outbuf(req, 0, 0);
970 END_PROFILE(SMBcheckpath);
971 return;
973 path_err:
975 END_PROFILE(SMBcheckpath);
977 /* We special case this - as when a Windows machine
978 is parsing a path is steps through the components
979 one at a time - if a component fails it expects
980 ERRbadpath, not ERRbadfile.
982 status = map_checkpath_error((char *)req->inbuf, status);
983 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
985 * Windows returns different error codes if
986 * the parent directory is valid but not the
987 * last component - it returns NT_STATUS_OBJECT_NAME_NOT_FOUND
988 * for that case and NT_STATUS_OBJECT_PATH_NOT_FOUND
989 * if the path is invalid.
991 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
992 ERRDOS, ERRbadpath);
993 return;
996 reply_nterror(req, status);
999 /****************************************************************************
1000 Reply to a getatr.
1001 ****************************************************************************/
1003 void reply_getatr(struct smb_request *req)
1005 connection_struct *conn = req->conn;
1006 char *fname = NULL;
1007 SMB_STRUCT_STAT sbuf;
1008 int mode=0;
1009 SMB_OFF_T size=0;
1010 time_t mtime=0;
1011 char *p;
1012 NTSTATUS status;
1013 TALLOC_CTX *ctx = talloc_tos();
1015 START_PROFILE(SMBgetatr);
1017 p = smb_buf(req->inbuf) + 1;
1018 p += srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname, p,
1019 0, STR_TERMINATE, &status);
1020 if (!NT_STATUS_IS_OK(status)) {
1021 reply_nterror(req, status);
1022 END_PROFILE(SMBgetatr);
1023 return;
1026 status = resolve_dfspath(ctx, conn,
1027 req->flags2 & FLAGS2_DFS_PATHNAMES,
1028 fname,
1029 &fname);
1030 if (!NT_STATUS_IS_OK(status)) {
1031 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1032 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1033 ERRSRV, ERRbadpath);
1034 END_PROFILE(SMBgetatr);
1035 return;
1037 reply_nterror(req, status);
1038 END_PROFILE(SMBgetatr);
1039 return;
1042 /* dos smetimes asks for a stat of "" - it returns a "hidden directory"
1043 under WfWg - weird! */
1044 if (*fname == '\0') {
1045 mode = aHIDDEN | aDIR;
1046 if (!CAN_WRITE(conn)) {
1047 mode |= aRONLY;
1049 size = 0;
1050 mtime = 0;
1051 } else {
1052 status = unix_convert(ctx, conn, fname, False, &fname, NULL,&sbuf);
1053 if (!NT_STATUS_IS_OK(status)) {
1054 reply_nterror(req, status);
1055 END_PROFILE(SMBgetatr);
1056 return;
1058 status = check_name(conn, fname);
1059 if (!NT_STATUS_IS_OK(status)) {
1060 DEBUG(3,("reply_getatr: check_name of %s failed (%s)\n",fname,nt_errstr(status)));
1061 reply_nterror(req, status);
1062 END_PROFILE(SMBgetatr);
1063 return;
1065 if (!VALID_STAT(sbuf) && (SMB_VFS_STAT(conn,fname,&sbuf) != 0)) {
1066 DEBUG(3,("reply_getatr: stat of %s failed (%s)\n",fname,strerror(errno)));
1067 reply_unixerror(req, ERRDOS,ERRbadfile);
1068 END_PROFILE(SMBgetatr);
1069 return;
1072 mode = dos_mode(conn,fname,&sbuf);
1073 size = sbuf.st_size;
1074 mtime = sbuf.st_mtime;
1075 if (mode & aDIR) {
1076 size = 0;
1080 reply_outbuf(req, 10, 0);
1082 SSVAL(req->outbuf,smb_vwv0,mode);
1083 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1084 srv_put_dos_date3((char *)req->outbuf,smb_vwv1,mtime & ~1);
1085 } else {
1086 srv_put_dos_date3((char *)req->outbuf,smb_vwv1,mtime);
1088 SIVAL(req->outbuf,smb_vwv3,(uint32)size);
1090 if (Protocol >= PROTOCOL_NT1) {
1091 SSVAL(req->outbuf, smb_flg2,
1092 SVAL(req->outbuf, smb_flg2) | FLAGS2_IS_LONG_NAME);
1095 DEBUG(3,("reply_getatr: name=%s mode=%d size=%u\n", fname, mode, (unsigned int)size ) );
1097 END_PROFILE(SMBgetatr);
1098 return;
1101 /****************************************************************************
1102 Reply to a setatr.
1103 ****************************************************************************/
1105 void reply_setatr(struct smb_request *req)
1107 struct timespec ts[2];
1108 connection_struct *conn = req->conn;
1109 char *fname = NULL;
1110 int mode;
1111 time_t mtime;
1112 SMB_STRUCT_STAT sbuf;
1113 char *p;
1114 NTSTATUS status;
1115 TALLOC_CTX *ctx = talloc_tos();
1117 START_PROFILE(SMBsetatr);
1119 ZERO_STRUCT(ts);
1121 if (req->wct < 2) {
1122 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1123 return;
1126 p = smb_buf(req->inbuf) + 1;
1127 p += srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname, p,
1128 0, STR_TERMINATE, &status);
1129 if (!NT_STATUS_IS_OK(status)) {
1130 reply_nterror(req, status);
1131 END_PROFILE(SMBsetatr);
1132 return;
1135 status = resolve_dfspath(ctx, conn,
1136 req->flags2 & FLAGS2_DFS_PATHNAMES,
1137 fname,
1138 &fname);
1139 if (!NT_STATUS_IS_OK(status)) {
1140 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1141 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1142 ERRSRV, ERRbadpath);
1143 END_PROFILE(SMBsetatr);
1144 return;
1146 reply_nterror(req, status);
1147 END_PROFILE(SMBsetatr);
1148 return;
1151 status = unix_convert(ctx, conn, fname, False, &fname, NULL, &sbuf);
1152 if (!NT_STATUS_IS_OK(status)) {
1153 reply_nterror(req, status);
1154 END_PROFILE(SMBsetatr);
1155 return;
1158 status = check_name(conn, fname);
1159 if (!NT_STATUS_IS_OK(status)) {
1160 reply_nterror(req, status);
1161 END_PROFILE(SMBsetatr);
1162 return;
1165 if (fname[0] == '.' && fname[1] == '\0') {
1167 * Not sure here is the right place to catch this
1168 * condition. Might be moved to somewhere else later -- vl
1170 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1171 END_PROFILE(SMBsetatr);
1172 return;
1175 mode = SVAL(req->inbuf,smb_vwv0);
1176 mtime = srv_make_unix_date3(req->inbuf+smb_vwv1);
1178 ts[1] = convert_time_t_to_timespec(mtime);
1179 status = smb_set_file_time(conn, NULL, fname,
1180 &sbuf, ts, true);
1181 if (!NT_STATUS_IS_OK(status)) {
1182 reply_unixerror(req, ERRDOS, ERRnoaccess);
1183 END_PROFILE(SMBsetatr);
1184 return;
1187 if (mode != FILE_ATTRIBUTE_NORMAL) {
1188 if (VALID_STAT_OF_DIR(sbuf))
1189 mode |= aDIR;
1190 else
1191 mode &= ~aDIR;
1193 if (file_set_dosmode(conn,fname,mode,&sbuf,NULL,false) != 0) {
1194 reply_unixerror(req, ERRDOS, ERRnoaccess);
1195 END_PROFILE(SMBsetatr);
1196 return;
1200 reply_outbuf(req, 0, 0);
1202 DEBUG( 3, ( "setatr name=%s mode=%d\n", fname, mode ) );
1204 END_PROFILE(SMBsetatr);
1205 return;
1208 /****************************************************************************
1209 Reply to a dskattr.
1210 ****************************************************************************/
1212 void reply_dskattr(struct smb_request *req)
1214 connection_struct *conn = req->conn;
1215 SMB_BIG_UINT dfree,dsize,bsize;
1216 START_PROFILE(SMBdskattr);
1218 if (get_dfree_info(conn,".",True,&bsize,&dfree,&dsize) == (SMB_BIG_UINT)-1) {
1219 reply_unixerror(req, ERRHRD, ERRgeneral);
1220 END_PROFILE(SMBdskattr);
1221 return;
1224 reply_outbuf(req, 5, 0);
1226 if (Protocol <= PROTOCOL_LANMAN2) {
1227 double total_space, free_space;
1228 /* we need to scale this to a number that DOS6 can handle. We
1229 use floating point so we can handle large drives on systems
1230 that don't have 64 bit integers
1232 we end up displaying a maximum of 2G to DOS systems
1234 total_space = dsize * (double)bsize;
1235 free_space = dfree * (double)bsize;
1237 dsize = (SMB_BIG_UINT)((total_space+63*512) / (64*512));
1238 dfree = (SMB_BIG_UINT)((free_space+63*512) / (64*512));
1240 if (dsize > 0xFFFF) dsize = 0xFFFF;
1241 if (dfree > 0xFFFF) dfree = 0xFFFF;
1243 SSVAL(req->outbuf,smb_vwv0,dsize);
1244 SSVAL(req->outbuf,smb_vwv1,64); /* this must be 64 for dos systems */
1245 SSVAL(req->outbuf,smb_vwv2,512); /* and this must be 512 */
1246 SSVAL(req->outbuf,smb_vwv3,dfree);
1247 } else {
1248 SSVAL(req->outbuf,smb_vwv0,dsize);
1249 SSVAL(req->outbuf,smb_vwv1,bsize/512);
1250 SSVAL(req->outbuf,smb_vwv2,512);
1251 SSVAL(req->outbuf,smb_vwv3,dfree);
1254 DEBUG(3,("dskattr dfree=%d\n", (unsigned int)dfree));
1256 END_PROFILE(SMBdskattr);
1257 return;
1260 /****************************************************************************
1261 Reply to a search.
1262 Can be called from SMBsearch, SMBffirst or SMBfunique.
1263 ****************************************************************************/
1265 void reply_search(struct smb_request *req)
1267 connection_struct *conn = req->conn;
1268 char *mask = NULL;
1269 char *directory = NULL;
1270 char *fname = NULL;
1271 SMB_OFF_T size;
1272 uint32 mode;
1273 time_t date;
1274 uint32 dirtype;
1275 unsigned int numentries = 0;
1276 unsigned int maxentries = 0;
1277 bool finished = False;
1278 char *p;
1279 int status_len;
1280 char *path = NULL;
1281 char status[21];
1282 int dptr_num= -1;
1283 bool check_descend = False;
1284 bool expect_close = False;
1285 NTSTATUS nt_status;
1286 bool mask_contains_wcard = False;
1287 bool allow_long_path_components = (req->flags2 & FLAGS2_LONG_PATH_COMPONENTS) ? True : False;
1288 TALLOC_CTX *ctx = talloc_tos();
1289 bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
1291 START_PROFILE(SMBsearch);
1293 if (req->wct < 2) {
1294 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1295 END_PROFILE(SMBsearch);
1296 return;
1299 if (lp_posix_pathnames()) {
1300 reply_unknown_new(req, CVAL(req->inbuf, smb_com));
1301 END_PROFILE(SMBsearch);
1302 return;
1305 /* If we were called as SMBffirst then we must expect close. */
1306 if(CVAL(req->inbuf,smb_com) == SMBffirst) {
1307 expect_close = True;
1310 reply_outbuf(req, 1, 3);
1311 maxentries = SVAL(req->inbuf,smb_vwv0);
1312 dirtype = SVAL(req->inbuf,smb_vwv1);
1313 p = smb_buf(req->inbuf) + 1;
1314 p += srvstr_get_path_wcard(ctx,
1315 (char *)req->inbuf,
1316 req->flags2,
1317 &path,
1320 STR_TERMINATE,
1321 &nt_status,
1322 &mask_contains_wcard);
1323 if (!NT_STATUS_IS_OK(nt_status)) {
1324 reply_nterror(req, nt_status);
1325 END_PROFILE(SMBsearch);
1326 return;
1329 nt_status = resolve_dfspath_wcard(ctx, conn,
1330 req->flags2 & FLAGS2_DFS_PATHNAMES,
1331 path,
1332 &path,
1333 &mask_contains_wcard);
1334 if (!NT_STATUS_IS_OK(nt_status)) {
1335 if (NT_STATUS_EQUAL(nt_status,NT_STATUS_PATH_NOT_COVERED)) {
1336 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1337 ERRSRV, ERRbadpath);
1338 END_PROFILE(SMBsearch);
1339 return;
1341 reply_nterror(req, nt_status);
1342 END_PROFILE(SMBsearch);
1343 return;
1346 p++;
1347 status_len = SVAL(p, 0);
1348 p += 2;
1350 /* dirtype &= ~aDIR; */
1352 if (status_len == 0) {
1353 SMB_STRUCT_STAT sbuf;
1355 nt_status = unix_convert(ctx, conn, path, True,
1356 &directory, NULL, &sbuf);
1357 if (!NT_STATUS_IS_OK(nt_status)) {
1358 reply_nterror(req, nt_status);
1359 END_PROFILE(SMBsearch);
1360 return;
1363 nt_status = check_name(conn, directory);
1364 if (!NT_STATUS_IS_OK(nt_status)) {
1365 reply_nterror(req, nt_status);
1366 END_PROFILE(SMBsearch);
1367 return;
1370 p = strrchr_m(directory,'/');
1371 if (!p) {
1372 mask = directory;
1373 directory = talloc_strdup(ctx,".");
1374 if (!directory) {
1375 reply_nterror(req, NT_STATUS_NO_MEMORY);
1376 END_PROFILE(SMBsearch);
1377 return;
1379 } else {
1380 *p = 0;
1381 mask = p+1;
1384 if (*directory == '\0') {
1385 directory = talloc_strdup(ctx,".");
1386 if (!directory) {
1387 reply_nterror(req, NT_STATUS_NO_MEMORY);
1388 END_PROFILE(SMBsearch);
1389 return;
1392 memset((char *)status,'\0',21);
1393 SCVAL(status,0,(dirtype & 0x1F));
1395 nt_status = dptr_create(conn,
1396 directory,
1397 True,
1398 expect_close,
1399 req->smbpid,
1400 mask,
1401 mask_contains_wcard,
1402 dirtype,
1403 &conn->dirptr);
1404 if (!NT_STATUS_IS_OK(nt_status)) {
1405 reply_nterror(req, nt_status);
1406 END_PROFILE(SMBsearch);
1407 return;
1409 dptr_num = dptr_dnum(conn->dirptr);
1410 } else {
1411 int status_dirtype;
1413 memcpy(status,p,21);
1414 status_dirtype = CVAL(status,0) & 0x1F;
1415 if (status_dirtype != (dirtype & 0x1F)) {
1416 dirtype = status_dirtype;
1419 conn->dirptr = dptr_fetch(status+12,&dptr_num);
1420 if (!conn->dirptr) {
1421 goto SearchEmpty;
1423 string_set(&conn->dirpath,dptr_path(dptr_num));
1424 mask = dptr_wcard(dptr_num);
1425 if (!mask) {
1426 goto SearchEmpty;
1429 * For a 'continue' search we have no string. So
1430 * check from the initial saved string.
1432 mask_contains_wcard = ms_has_wild(mask);
1433 dirtype = dptr_attr(dptr_num);
1436 DEBUG(4,("dptr_num is %d\n",dptr_num));
1438 if ((dirtype&0x1F) == aVOLID) {
1439 char buf[DIR_STRUCT_SIZE];
1440 memcpy(buf,status,21);
1441 if (!make_dir_struct(ctx,buf,"???????????",volume_label(SNUM(conn)),
1442 0,aVOLID,0,!allow_long_path_components)) {
1443 reply_nterror(req, NT_STATUS_NO_MEMORY);
1444 END_PROFILE(SMBsearch);
1445 return;
1447 dptr_fill(buf+12,dptr_num);
1448 if (dptr_zero(buf+12) && (status_len==0)) {
1449 numentries = 1;
1450 } else {
1451 numentries = 0;
1453 if (message_push_blob(&req->outbuf,
1454 data_blob_const(buf, sizeof(buf)))
1455 == -1) {
1456 reply_nterror(req, NT_STATUS_NO_MEMORY);
1457 END_PROFILE(SMBsearch);
1458 return;
1460 } else {
1461 unsigned int i;
1462 maxentries = MIN(
1463 maxentries,
1464 ((BUFFER_SIZE -
1465 ((uint8 *)smb_buf(req->outbuf) + 3 - req->outbuf))
1466 /DIR_STRUCT_SIZE));
1468 DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
1469 conn->dirpath,lp_dontdescend(SNUM(conn))));
1470 if (in_list(conn->dirpath, lp_dontdescend(SNUM(conn)),True)) {
1471 check_descend = True;
1474 for (i=numentries;(i<maxentries) && !finished;i++) {
1475 finished = !get_dir_entry(ctx,
1476 conn,
1477 mask,
1478 dirtype,
1479 &fname,
1480 &size,
1481 &mode,
1482 &date,
1483 check_descend,
1484 ask_sharemode);
1485 if (!finished) {
1486 char buf[DIR_STRUCT_SIZE];
1487 memcpy(buf,status,21);
1488 if (!make_dir_struct(ctx,
1489 buf,
1490 mask,
1491 fname,
1492 size,
1493 mode,
1494 date,
1495 !allow_long_path_components)) {
1496 reply_nterror(req, NT_STATUS_NO_MEMORY);
1497 END_PROFILE(SMBsearch);
1498 return;
1500 if (!dptr_fill(buf+12,dptr_num)) {
1501 break;
1503 if (message_push_blob(&req->outbuf,
1504 data_blob_const(buf, sizeof(buf)))
1505 == -1) {
1506 reply_nterror(req, NT_STATUS_NO_MEMORY);
1507 END_PROFILE(SMBsearch);
1508 return;
1510 numentries++;
1515 SearchEmpty:
1517 /* If we were called as SMBffirst with smb_search_id == NULL
1518 and no entries were found then return error and close dirptr
1519 (X/Open spec) */
1521 if (numentries == 0) {
1522 dptr_close(&dptr_num);
1523 } else if(expect_close && status_len == 0) {
1524 /* Close the dptr - we know it's gone */
1525 dptr_close(&dptr_num);
1528 /* If we were called as SMBfunique, then we can close the dirptr now ! */
1529 if(dptr_num >= 0 && CVAL(req->inbuf,smb_com) == SMBfunique) {
1530 dptr_close(&dptr_num);
1533 if ((numentries == 0) && !mask_contains_wcard) {
1534 reply_botherror(req, STATUS_NO_MORE_FILES, ERRDOS, ERRnofiles);
1535 END_PROFILE(SMBsearch);
1536 return;
1539 SSVAL(req->outbuf,smb_vwv0,numentries);
1540 SSVAL(req->outbuf,smb_vwv1,3 + numentries * DIR_STRUCT_SIZE);
1541 SCVAL(smb_buf(req->outbuf),0,5);
1542 SSVAL(smb_buf(req->outbuf),1,numentries*DIR_STRUCT_SIZE);
1544 /* The replies here are never long name. */
1545 SSVAL(req->outbuf, smb_flg2,
1546 SVAL(req->outbuf, smb_flg2) & (~FLAGS2_IS_LONG_NAME));
1547 if (!allow_long_path_components) {
1548 SSVAL(req->outbuf, smb_flg2,
1549 SVAL(req->outbuf, smb_flg2)
1550 & (~FLAGS2_LONG_PATH_COMPONENTS));
1553 /* This SMB *always* returns ASCII names. Remove the unicode bit in flags2. */
1554 SSVAL(req->outbuf, smb_flg2,
1555 (SVAL(req->outbuf, smb_flg2) & (~FLAGS2_UNICODE_STRINGS)));
1557 if (!directory) {
1558 directory = dptr_path(dptr_num);
1561 DEBUG(4,("%s mask=%s path=%s dtype=%d nument=%u of %u\n",
1562 smb_fn_name(CVAL(req->inbuf,smb_com)),
1563 mask,
1564 directory ? directory : "./",
1565 dirtype,
1566 numentries,
1567 maxentries ));
1569 END_PROFILE(SMBsearch);
1570 return;
1573 /****************************************************************************
1574 Reply to a fclose (stop directory search).
1575 ****************************************************************************/
1577 void reply_fclose(struct smb_request *req)
1579 int status_len;
1580 char status[21];
1581 int dptr_num= -2;
1582 char *p;
1583 char *path = NULL;
1584 NTSTATUS err;
1585 bool path_contains_wcard = False;
1586 TALLOC_CTX *ctx = talloc_tos();
1588 START_PROFILE(SMBfclose);
1590 if (lp_posix_pathnames()) {
1591 reply_unknown_new(req, CVAL(req->inbuf, smb_com));
1592 END_PROFILE(SMBfclose);
1593 return;
1596 p = smb_buf(req->inbuf) + 1;
1597 p += srvstr_get_path_wcard(ctx,
1598 (char *)req->inbuf,
1599 req->flags2,
1600 &path,
1603 STR_TERMINATE,
1604 &err,
1605 &path_contains_wcard);
1606 if (!NT_STATUS_IS_OK(err)) {
1607 reply_nterror(req, err);
1608 END_PROFILE(SMBfclose);
1609 return;
1611 p++;
1612 status_len = SVAL(p,0);
1613 p += 2;
1615 if (status_len == 0) {
1616 reply_doserror(req, ERRSRV, ERRsrverror);
1617 END_PROFILE(SMBfclose);
1618 return;
1621 memcpy(status,p,21);
1623 if(dptr_fetch(status+12,&dptr_num)) {
1624 /* Close the dptr - we know it's gone */
1625 dptr_close(&dptr_num);
1628 reply_outbuf(req, 1, 0);
1629 SSVAL(req->outbuf,smb_vwv0,0);
1631 DEBUG(3,("search close\n"));
1633 END_PROFILE(SMBfclose);
1634 return;
1637 /****************************************************************************
1638 Reply to an open.
1639 ****************************************************************************/
1641 void reply_open(struct smb_request *req)
1643 connection_struct *conn = req->conn;
1644 char *fname = NULL;
1645 uint32 fattr=0;
1646 SMB_OFF_T size = 0;
1647 time_t mtime=0;
1648 int info;
1649 SMB_STRUCT_STAT sbuf;
1650 files_struct *fsp;
1651 int oplock_request;
1652 int deny_mode;
1653 uint32 dos_attr;
1654 uint32 access_mask;
1655 uint32 share_mode;
1656 uint32 create_disposition;
1657 uint32 create_options = 0;
1658 NTSTATUS status;
1659 TALLOC_CTX *ctx = talloc_tos();
1661 START_PROFILE(SMBopen);
1663 if (req->wct < 2) {
1664 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1665 END_PROFILE(SMBopen);
1666 return;
1669 oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
1670 deny_mode = SVAL(req->inbuf,smb_vwv0);
1671 dos_attr = SVAL(req->inbuf,smb_vwv1);
1673 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
1674 smb_buf(req->inbuf)+1, 0,
1675 STR_TERMINATE, &status);
1676 if (!NT_STATUS_IS_OK(status)) {
1677 reply_nterror(req, status);
1678 END_PROFILE(SMBopen);
1679 return;
1682 if (!map_open_params_to_ntcreate(
1683 fname, deny_mode, OPENX_FILE_EXISTS_OPEN, &access_mask,
1684 &share_mode, &create_disposition, &create_options)) {
1685 reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRbadaccess));
1686 END_PROFILE(SMBopen);
1687 return;
1690 status = create_file(conn, /* conn */
1691 req, /* req */
1692 0, /* root_dir_fid */
1693 fname, /* fname */
1694 access_mask, /* access_mask */
1695 share_mode, /* share_access */
1696 create_disposition, /* create_disposition*/
1697 create_options, /* create_options */
1698 dos_attr, /* file_attributes */
1699 oplock_request, /* oplock_request */
1700 0, /* allocation_size */
1701 NULL, /* sd */
1702 NULL, /* ea_list */
1703 &fsp, /* result */
1704 &info, /* pinfo */
1705 &sbuf); /* psbuf */
1707 if (!NT_STATUS_IS_OK(status)) {
1708 if (open_was_deferred(req->mid)) {
1709 /* We have re-scheduled this call. */
1710 END_PROFILE(SMBopen);
1711 return;
1713 reply_openerror(req, status);
1714 END_PROFILE(SMBopen);
1715 return;
1718 size = sbuf.st_size;
1719 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
1720 mtime = sbuf.st_mtime;
1722 if (fattr & aDIR) {
1723 DEBUG(3,("attempt to open a directory %s\n",fsp->fsp_name));
1724 close_file(fsp,ERROR_CLOSE);
1725 reply_doserror(req, ERRDOS,ERRnoaccess);
1726 END_PROFILE(SMBopen);
1727 return;
1730 reply_outbuf(req, 7, 0);
1731 SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
1732 SSVAL(req->outbuf,smb_vwv1,fattr);
1733 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1734 srv_put_dos_date3((char *)req->outbuf,smb_vwv2,mtime & ~1);
1735 } else {
1736 srv_put_dos_date3((char *)req->outbuf,smb_vwv2,mtime);
1738 SIVAL(req->outbuf,smb_vwv4,(uint32)size);
1739 SSVAL(req->outbuf,smb_vwv6,deny_mode);
1741 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1742 SCVAL(req->outbuf,smb_flg,
1743 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1746 if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1747 SCVAL(req->outbuf,smb_flg,
1748 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1750 END_PROFILE(SMBopen);
1751 return;
1754 /****************************************************************************
1755 Reply to an open and X.
1756 ****************************************************************************/
1758 void reply_open_and_X(struct smb_request *req)
1760 connection_struct *conn = req->conn;
1761 char *fname = NULL;
1762 uint16 open_flags;
1763 int deny_mode;
1764 uint32 smb_attr;
1765 /* Breakout the oplock request bits so we can set the
1766 reply bits separately. */
1767 int ex_oplock_request;
1768 int core_oplock_request;
1769 int oplock_request;
1770 #if 0
1771 int smb_sattr = SVAL(req->inbuf,smb_vwv4);
1772 uint32 smb_time = make_unix_date3(req->inbuf+smb_vwv6);
1773 #endif
1774 int smb_ofun;
1775 uint32 fattr=0;
1776 int mtime=0;
1777 SMB_STRUCT_STAT sbuf;
1778 int smb_action = 0;
1779 files_struct *fsp;
1780 NTSTATUS status;
1781 SMB_BIG_UINT allocation_size;
1782 ssize_t retval = -1;
1783 uint32 access_mask;
1784 uint32 share_mode;
1785 uint32 create_disposition;
1786 uint32 create_options = 0;
1787 TALLOC_CTX *ctx = talloc_tos();
1789 START_PROFILE(SMBopenX);
1791 if (req->wct < 15) {
1792 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1793 END_PROFILE(SMBopenX);
1794 return;
1797 open_flags = SVAL(req->inbuf,smb_vwv2);
1798 deny_mode = SVAL(req->inbuf,smb_vwv3);
1799 smb_attr = SVAL(req->inbuf,smb_vwv5);
1800 ex_oplock_request = EXTENDED_OPLOCK_REQUEST(req->inbuf);
1801 core_oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
1802 oplock_request = ex_oplock_request | core_oplock_request;
1803 smb_ofun = SVAL(req->inbuf,smb_vwv8);
1804 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,smb_vwv9);
1806 /* If it's an IPC, pass off the pipe handler. */
1807 if (IS_IPC(conn)) {
1808 if (lp_nt_pipe_support()) {
1809 reply_open_pipe_and_X(conn, req);
1810 } else {
1811 reply_doserror(req, ERRSRV, ERRaccess);
1813 END_PROFILE(SMBopenX);
1814 return;
1817 /* XXXX we need to handle passed times, sattr and flags */
1818 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
1819 smb_buf(req->inbuf), 0, STR_TERMINATE,
1820 &status);
1821 if (!NT_STATUS_IS_OK(status)) {
1822 reply_nterror(req, status);
1823 END_PROFILE(SMBopenX);
1824 return;
1827 if (!map_open_params_to_ntcreate(
1828 fname, deny_mode, smb_ofun, &access_mask,
1829 &share_mode, &create_disposition, &create_options)) {
1830 reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRbadaccess));
1831 END_PROFILE(SMBopenX);
1832 return;
1835 status = create_file(conn, /* conn */
1836 req, /* req */
1837 0, /* root_dir_fid */
1838 fname, /* fname */
1839 access_mask, /* access_mask */
1840 share_mode, /* share_access */
1841 create_disposition, /* create_disposition*/
1842 create_options, /* create_options */
1843 smb_attr, /* file_attributes */
1844 oplock_request, /* oplock_request */
1845 0, /* allocation_size */
1846 NULL, /* sd */
1847 NULL, /* ea_list */
1848 &fsp, /* result */
1849 &smb_action, /* pinfo */
1850 &sbuf); /* psbuf */
1852 if (!NT_STATUS_IS_OK(status)) {
1853 END_PROFILE(SMBopenX);
1854 if (open_was_deferred(req->mid)) {
1855 /* We have re-scheduled this call. */
1856 return;
1858 reply_openerror(req, status);
1859 return;
1862 /* Setting the "size" field in vwv9 and vwv10 causes the file to be set to this size,
1863 if the file is truncated or created. */
1864 if (((smb_action == FILE_WAS_CREATED) || (smb_action == FILE_WAS_OVERWRITTEN)) && allocation_size) {
1865 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1866 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1867 close_file(fsp,ERROR_CLOSE);
1868 reply_nterror(req, NT_STATUS_DISK_FULL);
1869 END_PROFILE(SMBopenX);
1870 return;
1872 retval = vfs_set_filelen(fsp, (SMB_OFF_T)allocation_size);
1873 if (retval < 0) {
1874 close_file(fsp,ERROR_CLOSE);
1875 reply_nterror(req, NT_STATUS_DISK_FULL);
1876 END_PROFILE(SMBopenX);
1877 return;
1879 sbuf.st_size = get_allocation_size(conn,fsp,&sbuf);
1882 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
1883 mtime = sbuf.st_mtime;
1884 if (fattr & aDIR) {
1885 close_file(fsp,ERROR_CLOSE);
1886 reply_doserror(req, ERRDOS, ERRnoaccess);
1887 END_PROFILE(SMBopenX);
1888 return;
1891 /* If the caller set the extended oplock request bit
1892 and we granted one (by whatever means) - set the
1893 correct bit for extended oplock reply.
1896 if (ex_oplock_request && lp_fake_oplocks(SNUM(conn))) {
1897 smb_action |= EXTENDED_OPLOCK_GRANTED;
1900 if(ex_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1901 smb_action |= EXTENDED_OPLOCK_GRANTED;
1904 /* If the caller set the core oplock request bit
1905 and we granted one (by whatever means) - set the
1906 correct bit for core oplock reply.
1909 if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
1910 reply_outbuf(req, 19, 0);
1911 } else {
1912 reply_outbuf(req, 15, 0);
1915 if (core_oplock_request && lp_fake_oplocks(SNUM(conn))) {
1916 SCVAL(req->outbuf, smb_flg,
1917 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1920 if(core_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1921 SCVAL(req->outbuf, smb_flg,
1922 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1925 SSVAL(req->outbuf,smb_vwv2,fsp->fnum);
1926 SSVAL(req->outbuf,smb_vwv3,fattr);
1927 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1928 srv_put_dos_date3((char *)req->outbuf,smb_vwv4,mtime & ~1);
1929 } else {
1930 srv_put_dos_date3((char *)req->outbuf,smb_vwv4,mtime);
1932 SIVAL(req->outbuf,smb_vwv6,(uint32)sbuf.st_size);
1933 SSVAL(req->outbuf,smb_vwv8,GET_OPENX_MODE(deny_mode));
1934 SSVAL(req->outbuf,smb_vwv11,smb_action);
1936 if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
1937 SIVAL(req->outbuf, smb_vwv15, STD_RIGHT_ALL_ACCESS);
1940 END_PROFILE(SMBopenX);
1941 chain_reply(req);
1942 return;
1945 /****************************************************************************
1946 Reply to a SMBulogoffX.
1947 ****************************************************************************/
1949 void reply_ulogoffX(struct smb_request *req)
1951 user_struct *vuser;
1953 START_PROFILE(SMBulogoffX);
1955 vuser = get_valid_user_struct(req->vuid);
1957 if(vuser == NULL) {
1958 DEBUG(3,("ulogoff, vuser id %d does not map to user.\n",
1959 req->vuid));
1962 /* in user level security we are supposed to close any files
1963 open by this user */
1964 if ((vuser != NULL) && (lp_security() != SEC_SHARE)) {
1965 file_close_user(req->vuid);
1968 invalidate_vuid(req->vuid);
1970 reply_outbuf(req, 2, 0);
1972 DEBUG( 3, ( "ulogoffX vuid=%d\n", req->vuid ) );
1974 END_PROFILE(SMBulogoffX);
1975 chain_reply(req);
1978 /****************************************************************************
1979 Reply to a mknew or a create.
1980 ****************************************************************************/
1982 void reply_mknew(struct smb_request *req)
1984 connection_struct *conn = req->conn;
1985 char *fname = NULL;
1986 int com;
1987 uint32 fattr = 0;
1988 struct timespec ts[2];
1989 files_struct *fsp;
1990 int oplock_request = 0;
1991 SMB_STRUCT_STAT sbuf;
1992 NTSTATUS status;
1993 uint32 access_mask = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
1994 uint32 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1995 uint32 create_disposition;
1996 uint32 create_options = 0;
1997 TALLOC_CTX *ctx = talloc_tos();
1999 START_PROFILE(SMBcreate);
2001 if (req->wct < 3) {
2002 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2003 END_PROFILE(SMBcreate);
2004 return;
2007 fattr = SVAL(req->inbuf,smb_vwv0);
2008 oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
2009 com = SVAL(req->inbuf,smb_com);
2011 ts[1] =convert_time_t_to_timespec(
2012 srv_make_unix_date3(req->inbuf + smb_vwv1));
2013 /* mtime. */
2015 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
2016 smb_buf(req->inbuf) + 1, 0,
2017 STR_TERMINATE, &status);
2018 if (!NT_STATUS_IS_OK(status)) {
2019 reply_nterror(req, status);
2020 END_PROFILE(SMBcreate);
2021 return;
2024 if (fattr & aVOLID) {
2025 DEBUG(0,("Attempt to create file (%s) with volid set - "
2026 "please report this\n", fname));
2029 if(com == SMBmknew) {
2030 /* We should fail if file exists. */
2031 create_disposition = FILE_CREATE;
2032 } else {
2033 /* Create if file doesn't exist, truncate if it does. */
2034 create_disposition = FILE_OVERWRITE_IF;
2037 status = create_file(conn, /* conn */
2038 req, /* req */
2039 0, /* root_dir_fid */
2040 fname, /* fname */
2041 access_mask, /* access_mask */
2042 share_mode, /* share_access */
2043 create_disposition, /* create_disposition*/
2044 create_options, /* create_options */
2045 fattr, /* file_attributes */
2046 oplock_request, /* oplock_request */
2047 0, /* allocation_size */
2048 NULL, /* sd */
2049 NULL, /* ea_list */
2050 &fsp, /* result */
2051 NULL, /* pinfo */
2052 &sbuf); /* psbuf */
2054 if (!NT_STATUS_IS_OK(status)) {
2055 END_PROFILE(SMBcreate);
2056 if (open_was_deferred(req->mid)) {
2057 /* We have re-scheduled this call. */
2058 return;
2060 reply_openerror(req, status);
2061 return;
2064 ts[0] = get_atimespec(&sbuf); /* atime. */
2065 status = smb_set_file_time(conn, fsp, fsp->fsp_name, &sbuf, ts, true);
2066 if (!NT_STATUS_IS_OK(status)) {
2067 END_PROFILE(SMBcreate);
2068 reply_openerror(req, status);
2069 return;
2072 reply_outbuf(req, 1, 0);
2073 SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
2075 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
2076 SCVAL(req->outbuf,smb_flg,
2077 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2080 if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
2081 SCVAL(req->outbuf,smb_flg,
2082 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2085 DEBUG( 2, ( "reply_mknew: file %s\n", fsp->fsp_name ) );
2086 DEBUG( 3, ( "reply_mknew %s fd=%d dmode=0x%x\n",
2087 fsp->fsp_name, fsp->fh->fd, (unsigned int)fattr ) );
2089 END_PROFILE(SMBcreate);
2090 return;
2093 /****************************************************************************
2094 Reply to a create temporary file.
2095 ****************************************************************************/
2097 void reply_ctemp(struct smb_request *req)
2099 connection_struct *conn = req->conn;
2100 char *fname = NULL;
2101 uint32 fattr;
2102 files_struct *fsp;
2103 int oplock_request;
2104 int tmpfd;
2105 SMB_STRUCT_STAT sbuf;
2106 char *s;
2107 NTSTATUS status;
2108 TALLOC_CTX *ctx = talloc_tos();
2110 START_PROFILE(SMBctemp);
2112 if (req->wct < 3) {
2113 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2114 END_PROFILE(SMBctemp);
2115 return;
2118 fattr = SVAL(req->inbuf,smb_vwv0);
2119 oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
2121 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
2122 smb_buf(req->inbuf)+1, 0, STR_TERMINATE,
2123 &status);
2124 if (!NT_STATUS_IS_OK(status)) {
2125 reply_nterror(req, status);
2126 END_PROFILE(SMBctemp);
2127 return;
2129 if (*fname) {
2130 fname = talloc_asprintf(ctx,
2131 "%s/TMXXXXXX",
2132 fname);
2133 } else {
2134 fname = talloc_strdup(ctx, "TMXXXXXX");
2137 if (!fname) {
2138 reply_nterror(req, NT_STATUS_NO_MEMORY);
2139 END_PROFILE(SMBctemp);
2140 return;
2143 status = resolve_dfspath(ctx, conn,
2144 req->flags2 & FLAGS2_DFS_PATHNAMES,
2145 fname,
2146 &fname);
2147 if (!NT_STATUS_IS_OK(status)) {
2148 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2149 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2150 ERRSRV, ERRbadpath);
2151 END_PROFILE(SMBctemp);
2152 return;
2154 reply_nterror(req, status);
2155 END_PROFILE(SMBctemp);
2156 return;
2159 status = unix_convert(ctx, conn, fname, False, &fname, NULL, &sbuf);
2160 if (!NT_STATUS_IS_OK(status)) {
2161 reply_nterror(req, status);
2162 END_PROFILE(SMBctemp);
2163 return;
2166 status = check_name(conn, fname);
2167 if (!NT_STATUS_IS_OK(status)) {
2168 reply_nterror(req, status);
2169 END_PROFILE(SMBctemp);
2170 return;
2173 tmpfd = smb_mkstemp(fname);
2174 if (tmpfd == -1) {
2175 reply_unixerror(req, ERRDOS, ERRnoaccess);
2176 END_PROFILE(SMBctemp);
2177 return;
2180 SMB_VFS_STAT(conn,fname,&sbuf);
2182 /* We should fail if file does not exist. */
2183 status = open_file_ntcreate(conn, req, fname, &sbuf,
2184 FILE_GENERIC_READ | FILE_GENERIC_WRITE,
2185 FILE_SHARE_READ|FILE_SHARE_WRITE,
2186 FILE_OPEN,
2188 fattr,
2189 oplock_request,
2190 NULL, &fsp);
2192 /* close fd from smb_mkstemp() */
2193 close(tmpfd);
2195 if (!NT_STATUS_IS_OK(status)) {
2196 if (open_was_deferred(req->mid)) {
2197 /* We have re-scheduled this call. */
2198 END_PROFILE(SMBctemp);
2199 return;
2201 reply_openerror(req, status);
2202 END_PROFILE(SMBctemp);
2203 return;
2206 reply_outbuf(req, 1, 0);
2207 SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
2209 /* the returned filename is relative to the directory */
2210 s = strrchr_m(fsp->fsp_name, '/');
2211 if (!s) {
2212 s = fsp->fsp_name;
2213 } else {
2214 s++;
2217 #if 0
2218 /* Tested vs W2K3 - this doesn't seem to be here - null terminated filename is the only
2219 thing in the byte section. JRA */
2220 SSVALS(p, 0, -1); /* what is this? not in spec */
2221 #endif
2222 if (message_push_string(&req->outbuf, s, STR_ASCII|STR_TERMINATE)
2223 == -1) {
2224 reply_nterror(req, NT_STATUS_NO_MEMORY);
2225 END_PROFILE(SMBctemp);
2226 return;
2229 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
2230 SCVAL(req->outbuf, smb_flg,
2231 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2234 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
2235 SCVAL(req->outbuf, smb_flg,
2236 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2239 DEBUG( 2, ( "reply_ctemp: created temp file %s\n", fsp->fsp_name ) );
2240 DEBUG( 3, ( "reply_ctemp %s fd=%d umode=0%o\n", fsp->fsp_name,
2241 fsp->fh->fd, (unsigned int)sbuf.st_mode ) );
2243 END_PROFILE(SMBctemp);
2244 return;
2247 /*******************************************************************
2248 Check if a user is allowed to rename a file.
2249 ********************************************************************/
2251 static NTSTATUS can_rename(connection_struct *conn, files_struct *fsp,
2252 uint16 dirtype, SMB_STRUCT_STAT *pst)
2254 uint32 fmode;
2256 if (!CAN_WRITE(conn)) {
2257 return NT_STATUS_MEDIA_WRITE_PROTECTED;
2260 fmode = dos_mode(conn, fsp->fsp_name, pst);
2261 if ((fmode & ~dirtype) & (aHIDDEN | aSYSTEM)) {
2262 return NT_STATUS_NO_SUCH_FILE;
2265 if (S_ISDIR(pst->st_mode)) {
2266 return NT_STATUS_OK;
2269 if (fsp->access_mask & (DELETE_ACCESS|FILE_WRITE_ATTRIBUTES)) {
2270 return NT_STATUS_OK;
2273 return NT_STATUS_ACCESS_DENIED;
2276 /*******************************************************************
2277 * unlink a file with all relevant access checks
2278 *******************************************************************/
2280 static NTSTATUS do_unlink(connection_struct *conn,
2281 struct smb_request *req,
2282 const char *fname,
2283 uint32 dirtype)
2285 SMB_STRUCT_STAT sbuf;
2286 uint32 fattr;
2287 files_struct *fsp;
2288 uint32 dirtype_orig = dirtype;
2289 NTSTATUS status;
2291 DEBUG(10,("do_unlink: %s, dirtype = %d\n", fname, dirtype ));
2293 if (!CAN_WRITE(conn)) {
2294 return NT_STATUS_MEDIA_WRITE_PROTECTED;
2297 if (SMB_VFS_LSTAT(conn,fname,&sbuf) != 0) {
2298 return map_nt_error_from_unix(errno);
2301 fattr = dos_mode(conn,fname,&sbuf);
2303 if (dirtype & FILE_ATTRIBUTE_NORMAL) {
2304 dirtype = aDIR|aARCH|aRONLY;
2307 dirtype &= (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM);
2308 if (!dirtype) {
2309 return NT_STATUS_NO_SUCH_FILE;
2312 if (!dir_check_ftype(conn, fattr, dirtype)) {
2313 if (fattr & aDIR) {
2314 return NT_STATUS_FILE_IS_A_DIRECTORY;
2316 return NT_STATUS_NO_SUCH_FILE;
2319 if (dirtype_orig & 0x8000) {
2320 /* These will never be set for POSIX. */
2321 return NT_STATUS_NO_SUCH_FILE;
2324 #if 0
2325 if ((fattr & dirtype) & FILE_ATTRIBUTE_DIRECTORY) {
2326 return NT_STATUS_FILE_IS_A_DIRECTORY;
2329 if ((fattr & ~dirtype) & (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)) {
2330 return NT_STATUS_NO_SUCH_FILE;
2333 if (dirtype & 0xFF00) {
2334 /* These will never be set for POSIX. */
2335 return NT_STATUS_NO_SUCH_FILE;
2338 dirtype &= 0xFF;
2339 if (!dirtype) {
2340 return NT_STATUS_NO_SUCH_FILE;
2343 /* Can't delete a directory. */
2344 if (fattr & aDIR) {
2345 return NT_STATUS_FILE_IS_A_DIRECTORY;
2347 #endif
2349 #if 0 /* JRATEST */
2350 else if (dirtype & aDIR) /* Asked for a directory and it isn't. */
2351 return NT_STATUS_OBJECT_NAME_INVALID;
2352 #endif /* JRATEST */
2354 /* Fix for bug #3035 from SATOH Fumiyasu <fumiyas@miraclelinux.com>
2356 On a Windows share, a file with read-only dosmode can be opened with
2357 DELETE_ACCESS. But on a Samba share (delete readonly = no), it
2358 fails with NT_STATUS_CANNOT_DELETE error.
2360 This semantic causes a problem that a user can not
2361 rename a file with read-only dosmode on a Samba share
2362 from a Windows command prompt (i.e. cmd.exe, but can rename
2363 from Windows Explorer).
2366 if (!lp_delete_readonly(SNUM(conn))) {
2367 if (fattr & aRONLY) {
2368 return NT_STATUS_CANNOT_DELETE;
2372 /* On open checks the open itself will check the share mode, so
2373 don't do it here as we'll get it wrong. */
2375 status = create_file_unixpath
2376 (conn, /* conn */
2377 req, /* req */
2378 fname, /* fname */
2379 DELETE_ACCESS, /* access_mask */
2380 FILE_SHARE_NONE, /* share_access */
2381 FILE_OPEN, /* create_disposition*/
2382 FILE_NON_DIRECTORY_FILE, /* create_options */
2383 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
2384 0, /* oplock_request */
2385 0, /* allocation_size */
2386 NULL, /* sd */
2387 NULL, /* ea_list */
2388 &fsp, /* result */
2389 NULL, /* pinfo */
2390 &sbuf); /* psbuf */
2392 if (!NT_STATUS_IS_OK(status)) {
2393 DEBUG(10, ("create_file_unixpath failed: %s\n",
2394 nt_errstr(status)));
2395 return status;
2398 /* The set is across all open files on this dev/inode pair. */
2399 if (!set_delete_on_close(fsp, True, &conn->server_info->utok)) {
2400 close_file(fsp, NORMAL_CLOSE);
2401 return NT_STATUS_ACCESS_DENIED;
2404 return close_file(fsp,NORMAL_CLOSE);
2407 /****************************************************************************
2408 The guts of the unlink command, split out so it may be called by the NT SMB
2409 code.
2410 ****************************************************************************/
2412 NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
2413 uint32 dirtype, const char *name_in, bool has_wild)
2415 const char *directory = NULL;
2416 char *mask = NULL;
2417 char *name = NULL;
2418 char *p = NULL;
2419 int count=0;
2420 NTSTATUS status = NT_STATUS_OK;
2421 SMB_STRUCT_STAT sbuf;
2422 TALLOC_CTX *ctx = talloc_tos();
2424 status = unix_convert(ctx, conn, name_in, has_wild, &name, NULL, &sbuf);
2425 if (!NT_STATUS_IS_OK(status)) {
2426 return status;
2429 p = strrchr_m(name,'/');
2430 if (!p) {
2431 directory = talloc_strdup(ctx, ".");
2432 if (!directory) {
2433 return NT_STATUS_NO_MEMORY;
2435 mask = name;
2436 } else {
2437 *p = 0;
2438 directory = name;
2439 mask = p+1;
2443 * We should only check the mangled cache
2444 * here if unix_convert failed. This means
2445 * that the path in 'mask' doesn't exist
2446 * on the file system and so we need to look
2447 * for a possible mangle. This patch from
2448 * Tine Smukavec <valentin.smukavec@hermes.si>.
2451 if (!VALID_STAT(sbuf) && mangle_is_mangled(mask,conn->params)) {
2452 char *new_mask = NULL;
2453 mangle_lookup_name_from_8_3(ctx,
2454 mask,
2455 &new_mask,
2456 conn->params );
2457 if (new_mask) {
2458 mask = new_mask;
2462 if (!has_wild) {
2463 directory = talloc_asprintf(ctx,
2464 "%s/%s",
2465 directory,
2466 mask);
2467 if (!directory) {
2468 return NT_STATUS_NO_MEMORY;
2470 if (dirtype == 0) {
2471 dirtype = FILE_ATTRIBUTE_NORMAL;
2474 status = check_name(conn, directory);
2475 if (!NT_STATUS_IS_OK(status)) {
2476 return status;
2479 status = do_unlink(conn, req, directory, dirtype);
2480 if (!NT_STATUS_IS_OK(status)) {
2481 return status;
2484 count++;
2485 } else {
2486 struct smb_Dir *dir_hnd = NULL;
2487 long offset = 0;
2488 const char *dname;
2490 if ((dirtype & SAMBA_ATTRIBUTES_MASK) == aDIR) {
2491 return NT_STATUS_OBJECT_NAME_INVALID;
2494 if (strequal(mask,"????????.???")) {
2495 mask[0] = '*';
2496 mask[1] = '\0';
2499 status = check_name(conn, directory);
2500 if (!NT_STATUS_IS_OK(status)) {
2501 return status;
2504 dir_hnd = OpenDir(talloc_tos(), conn, directory, mask,
2505 dirtype);
2506 if (dir_hnd == NULL) {
2507 return map_nt_error_from_unix(errno);
2510 /* XXXX the CIFS spec says that if bit0 of the flags2 field is set then
2511 the pattern matches against the long name, otherwise the short name
2512 We don't implement this yet XXXX
2515 status = NT_STATUS_NO_SUCH_FILE;
2517 while ((dname = ReadDirName(dir_hnd, &offset))) {
2518 SMB_STRUCT_STAT st;
2519 char *fname = NULL;
2521 if (!is_visible_file(conn, directory, dname, &st, True)) {
2522 continue;
2525 /* Quick check for "." and ".." */
2526 if (ISDOT(dname) || ISDOTDOT(dname)) {
2527 continue;
2530 if(!mask_match(dname, mask, conn->case_sensitive)) {
2531 continue;
2534 fname = talloc_asprintf(ctx, "%s/%s",
2535 directory,
2536 dname);
2537 if (!fname) {
2538 return NT_STATUS_NO_MEMORY;
2541 status = check_name(conn, fname);
2542 if (!NT_STATUS_IS_OK(status)) {
2543 TALLOC_FREE(dir_hnd);
2544 return status;
2547 status = do_unlink(conn, req, fname, dirtype);
2548 if (!NT_STATUS_IS_OK(status)) {
2549 TALLOC_FREE(fname);
2550 continue;
2553 count++;
2554 DEBUG(3,("unlink_internals: successful unlink [%s]\n",
2555 fname));
2557 TALLOC_FREE(fname);
2559 TALLOC_FREE(dir_hnd);
2562 if (count == 0 && NT_STATUS_IS_OK(status) && errno != 0) {
2563 status = map_nt_error_from_unix(errno);
2566 return status;
2569 /****************************************************************************
2570 Reply to a unlink
2571 ****************************************************************************/
2573 void reply_unlink(struct smb_request *req)
2575 connection_struct *conn = req->conn;
2576 char *name = NULL;
2577 uint32 dirtype;
2578 NTSTATUS status;
2579 bool path_contains_wcard = False;
2580 TALLOC_CTX *ctx = talloc_tos();
2582 START_PROFILE(SMBunlink);
2584 if (req->wct < 1) {
2585 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2586 END_PROFILE(SMBunlink);
2587 return;
2590 dirtype = SVAL(req->inbuf,smb_vwv0);
2592 srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &name,
2593 smb_buf(req->inbuf) + 1, 0,
2594 STR_TERMINATE, &status, &path_contains_wcard);
2595 if (!NT_STATUS_IS_OK(status)) {
2596 reply_nterror(req, status);
2597 END_PROFILE(SMBunlink);
2598 return;
2601 status = resolve_dfspath_wcard(ctx, conn,
2602 req->flags2 & FLAGS2_DFS_PATHNAMES,
2603 name,
2604 &name,
2605 &path_contains_wcard);
2606 if (!NT_STATUS_IS_OK(status)) {
2607 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2608 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2609 ERRSRV, ERRbadpath);
2610 END_PROFILE(SMBunlink);
2611 return;
2613 reply_nterror(req, status);
2614 END_PROFILE(SMBunlink);
2615 return;
2618 DEBUG(3,("reply_unlink : %s\n",name));
2620 status = unlink_internals(conn, req, dirtype, name,
2621 path_contains_wcard);
2622 if (!NT_STATUS_IS_OK(status)) {
2623 if (open_was_deferred(req->mid)) {
2624 /* We have re-scheduled this call. */
2625 END_PROFILE(SMBunlink);
2626 return;
2628 reply_nterror(req, status);
2629 END_PROFILE(SMBunlink);
2630 return;
2633 reply_outbuf(req, 0, 0);
2634 END_PROFILE(SMBunlink);
2636 return;
2639 /****************************************************************************
2640 Fail for readbraw.
2641 ****************************************************************************/
2643 static void fail_readraw(void)
2645 const char *errstr = talloc_asprintf(talloc_tos(),
2646 "FAIL ! reply_readbraw: socket write fail (%s)",
2647 strerror(errno));
2648 if (!errstr) {
2649 errstr = "";
2651 exit_server_cleanly(errstr);
2654 /****************************************************************************
2655 Fake (read/write) sendfile. Returns -1 on read or write fail.
2656 ****************************************************************************/
2658 static ssize_t fake_sendfile(files_struct *fsp, SMB_OFF_T startpos,
2659 size_t nread)
2661 size_t bufsize;
2662 size_t tosend = nread;
2663 char *buf;
2665 if (nread == 0) {
2666 return 0;
2669 bufsize = MIN(nread, 65536);
2671 if (!(buf = SMB_MALLOC_ARRAY(char, bufsize))) {
2672 return -1;
2675 while (tosend > 0) {
2676 ssize_t ret;
2677 size_t cur_read;
2679 if (tosend > bufsize) {
2680 cur_read = bufsize;
2681 } else {
2682 cur_read = tosend;
2684 ret = read_file(fsp,buf,startpos,cur_read);
2685 if (ret == -1) {
2686 SAFE_FREE(buf);
2687 return -1;
2690 /* If we had a short read, fill with zeros. */
2691 if (ret < cur_read) {
2692 memset(buf, '\0', cur_read - ret);
2695 if (write_data(smbd_server_fd(),buf,cur_read) != cur_read) {
2696 SAFE_FREE(buf);
2697 return -1;
2699 tosend -= cur_read;
2700 startpos += cur_read;
2703 SAFE_FREE(buf);
2704 return (ssize_t)nread;
2707 /****************************************************************************
2708 Return a readbraw error (4 bytes of zero).
2709 ****************************************************************************/
2711 static void reply_readbraw_error(void)
2713 char header[4];
2714 SIVAL(header,0,0);
2715 if (write_data(smbd_server_fd(),header,4) != 4) {
2716 fail_readraw();
2720 /****************************************************************************
2721 Use sendfile in readbraw.
2722 ****************************************************************************/
2724 void send_file_readbraw(connection_struct *conn,
2725 files_struct *fsp,
2726 SMB_OFF_T startpos,
2727 size_t nread,
2728 ssize_t mincount)
2730 char *outbuf = NULL;
2731 ssize_t ret=0;
2733 #if defined(WITH_SENDFILE)
2735 * We can only use sendfile on a non-chained packet
2736 * but we can use on a non-oplocked file. tridge proved this
2737 * on a train in Germany :-). JRA.
2738 * reply_readbraw has already checked the length.
2741 if ( (chain_size == 0) && (nread > 0) && (fsp->base_fsp == NULL) &&
2742 (fsp->wcp == NULL) && lp_use_sendfile(SNUM(conn)) ) {
2743 char header[4];
2744 DATA_BLOB header_blob;
2746 _smb_setlen(header,nread);
2747 header_blob = data_blob_const(header, 4);
2749 if (SMB_VFS_SENDFILE(smbd_server_fd(), fsp,
2750 &header_blob, startpos, nread) == -1) {
2751 /* Returning ENOSYS means no data at all was sent.
2752 * Do this as a normal read. */
2753 if (errno == ENOSYS) {
2754 goto normal_readbraw;
2758 * Special hack for broken Linux with no working sendfile. If we
2759 * return EINTR we sent the header but not the rest of the data.
2760 * Fake this up by doing read/write calls.
2762 if (errno == EINTR) {
2763 /* Ensure we don't do this again. */
2764 set_use_sendfile(SNUM(conn), False);
2765 DEBUG(0,("send_file_readbraw: sendfile not available. Faking..\n"));
2767 if (fake_sendfile(fsp, startpos, nread) == -1) {
2768 DEBUG(0,("send_file_readbraw: fake_sendfile failed for file %s (%s).\n",
2769 fsp->fsp_name, strerror(errno) ));
2770 exit_server_cleanly("send_file_readbraw fake_sendfile failed");
2772 return;
2775 DEBUG(0,("send_file_readbraw: sendfile failed for file %s (%s). Terminating\n",
2776 fsp->fsp_name, strerror(errno) ));
2777 exit_server_cleanly("send_file_readbraw sendfile failed");
2780 return;
2782 #endif
2784 normal_readbraw:
2786 outbuf = TALLOC_ARRAY(NULL, char, nread+4);
2787 if (!outbuf) {
2788 DEBUG(0,("send_file_readbraw: TALLOC_ARRAY failed for size %u.\n",
2789 (unsigned)(nread+4)));
2790 reply_readbraw_error();
2791 return;
2794 if (nread > 0) {
2795 ret = read_file(fsp,outbuf+4,startpos,nread);
2796 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2797 if (ret < mincount)
2798 ret = 0;
2799 #else
2800 if (ret < nread)
2801 ret = 0;
2802 #endif
2805 _smb_setlen(outbuf,ret);
2806 if (write_data(smbd_server_fd(),outbuf,4+ret) != 4+ret)
2807 fail_readraw();
2809 TALLOC_FREE(outbuf);
2812 /****************************************************************************
2813 Reply to a readbraw (core+ protocol).
2814 ****************************************************************************/
2816 void reply_readbraw(struct smb_request *req)
2818 connection_struct *conn = req->conn;
2819 ssize_t maxcount,mincount;
2820 size_t nread = 0;
2821 SMB_OFF_T startpos;
2822 files_struct *fsp;
2823 SMB_STRUCT_STAT st;
2824 SMB_OFF_T size = 0;
2826 START_PROFILE(SMBreadbraw);
2828 if (srv_is_signing_active() || is_encrypted_packet(req->inbuf)) {
2829 exit_server_cleanly("reply_readbraw: SMB signing/sealing is active - "
2830 "raw reads/writes are disallowed.");
2833 if (req->wct < 8) {
2834 reply_readbraw_error();
2835 END_PROFILE(SMBreadbraw);
2836 return;
2840 * Special check if an oplock break has been issued
2841 * and the readraw request croses on the wire, we must
2842 * return a zero length response here.
2845 fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
2848 * We have to do a check_fsp by hand here, as
2849 * we must always return 4 zero bytes on error,
2850 * not a NTSTATUS.
2853 if (!fsp || !conn || conn != fsp->conn ||
2854 req->vuid != fsp->vuid ||
2855 fsp->is_directory || fsp->fh->fd == -1) {
2857 * fsp could be NULL here so use the value from the packet. JRA.
2859 DEBUG(3,("reply_readbraw: fnum %d not valid "
2860 "- cache prime?\n",
2861 (int)SVAL(req->inbuf,smb_vwv0)));
2862 reply_readbraw_error();
2863 END_PROFILE(SMBreadbraw);
2864 return;
2867 /* Do a "by hand" version of CHECK_READ. */
2868 if (!(fsp->can_read ||
2869 ((req->flags2 & FLAGS2_READ_PERMIT_EXECUTE) &&
2870 (fsp->access_mask & FILE_EXECUTE)))) {
2871 DEBUG(3,("reply_readbraw: fnum %d not readable.\n",
2872 (int)SVAL(req->inbuf,smb_vwv0)));
2873 reply_readbraw_error();
2874 END_PROFILE(SMBreadbraw);
2875 return;
2878 flush_write_cache(fsp, READRAW_FLUSH);
2880 startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv1);
2881 if(req->wct == 10) {
2883 * This is a large offset (64 bit) read.
2885 #ifdef LARGE_SMB_OFF_T
2887 startpos |= (((SMB_OFF_T)IVAL(req->inbuf,smb_vwv8)) << 32);
2889 #else /* !LARGE_SMB_OFF_T */
2892 * Ensure we haven't been sent a >32 bit offset.
2895 if(IVAL(req->inbuf,smb_vwv8) != 0) {
2896 DEBUG(0,("reply_readbraw: large offset "
2897 "(%x << 32) used and we don't support "
2898 "64 bit offsets.\n",
2899 (unsigned int)IVAL(req->inbuf,smb_vwv8) ));
2900 reply_readbraw_error();
2901 END_PROFILE(SMBreadbraw);
2902 return;
2905 #endif /* LARGE_SMB_OFF_T */
2907 if(startpos < 0) {
2908 DEBUG(0,("reply_readbraw: negative 64 bit "
2909 "readraw offset (%.0f) !\n",
2910 (double)startpos ));
2911 reply_readbraw_error();
2912 END_PROFILE(SMBreadbraw);
2913 return;
2917 maxcount = (SVAL(req->inbuf,smb_vwv3) & 0xFFFF);
2918 mincount = (SVAL(req->inbuf,smb_vwv4) & 0xFFFF);
2920 /* ensure we don't overrun the packet size */
2921 maxcount = MIN(65535,maxcount);
2923 if (is_locked(fsp,(uint32)req->smbpid,
2924 (SMB_BIG_UINT)maxcount,
2925 (SMB_BIG_UINT)startpos,
2926 READ_LOCK)) {
2927 reply_readbraw_error();
2928 END_PROFILE(SMBreadbraw);
2929 return;
2932 if (SMB_VFS_FSTAT(fsp, &st) == 0) {
2933 size = st.st_size;
2936 if (startpos >= size) {
2937 nread = 0;
2938 } else {
2939 nread = MIN(maxcount,(size - startpos));
2942 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2943 if (nread < mincount)
2944 nread = 0;
2945 #endif
2947 DEBUG( 3, ( "reply_readbraw: fnum=%d start=%.0f max=%lu "
2948 "min=%lu nread=%lu\n",
2949 fsp->fnum, (double)startpos,
2950 (unsigned long)maxcount,
2951 (unsigned long)mincount,
2952 (unsigned long)nread ) );
2954 send_file_readbraw(conn, fsp, startpos, nread, mincount);
2956 DEBUG(5,("reply_readbraw finished\n"));
2957 END_PROFILE(SMBreadbraw);
2960 #undef DBGC_CLASS
2961 #define DBGC_CLASS DBGC_LOCKING
2963 /****************************************************************************
2964 Reply to a lockread (core+ protocol).
2965 ****************************************************************************/
2967 void reply_lockread(struct smb_request *req)
2969 connection_struct *conn = req->conn;
2970 ssize_t nread = -1;
2971 char *data;
2972 SMB_OFF_T startpos;
2973 size_t numtoread;
2974 NTSTATUS status;
2975 files_struct *fsp;
2976 struct byte_range_lock *br_lck = NULL;
2977 char *p = NULL;
2979 START_PROFILE(SMBlockread);
2981 if (req->wct < 5) {
2982 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2983 END_PROFILE(SMBlockread);
2984 return;
2987 fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
2989 if (!check_fsp(conn, req, fsp)) {
2990 END_PROFILE(SMBlockread);
2991 return;
2994 if (!CHECK_READ(fsp,req->inbuf)) {
2995 reply_doserror(req, ERRDOS, ERRbadaccess);
2996 END_PROFILE(SMBlockread);
2997 return;
3000 release_level_2_oplocks_on_change(fsp);
3002 numtoread = SVAL(req->inbuf,smb_vwv1);
3003 startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv2);
3005 numtoread = MIN(BUFFER_SIZE - (smb_size + 3*2 + 3), numtoread);
3007 reply_outbuf(req, 5, numtoread + 3);
3009 data = smb_buf(req->outbuf) + 3;
3012 * NB. Discovered by Menny Hamburger at Mainsoft. This is a core+
3013 * protocol request that predates the read/write lock concept.
3014 * Thus instead of asking for a read lock here we need to ask
3015 * for a write lock. JRA.
3016 * Note that the requested lock size is unaffected by max_recv.
3019 br_lck = do_lock(smbd_messaging_context(),
3020 fsp,
3021 req->smbpid,
3022 (SMB_BIG_UINT)numtoread,
3023 (SMB_BIG_UINT)startpos,
3024 WRITE_LOCK,
3025 WINDOWS_LOCK,
3026 False, /* Non-blocking lock. */
3027 &status,
3028 NULL);
3029 TALLOC_FREE(br_lck);
3031 if (NT_STATUS_V(status)) {
3032 reply_nterror(req, status);
3033 END_PROFILE(SMBlockread);
3034 return;
3038 * However the requested READ size IS affected by max_recv. Insanity.... JRA.
3041 if (numtoread > max_recv) {
3042 DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u). \
3043 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
3044 (unsigned int)numtoread, (unsigned int)max_recv ));
3045 numtoread = MIN(numtoread,max_recv);
3047 nread = read_file(fsp,data,startpos,numtoread);
3049 if (nread < 0) {
3050 reply_unixerror(req, ERRDOS, ERRnoaccess);
3051 END_PROFILE(SMBlockread);
3052 return;
3055 srv_set_message((char *)req->outbuf, 5, nread+3, False);
3057 SSVAL(req->outbuf,smb_vwv0,nread);
3058 SSVAL(req->outbuf,smb_vwv5,nread+3);
3059 p = smb_buf(req->outbuf);
3060 SCVAL(p,0,0); /* pad byte. */
3061 SSVAL(p,1,nread);
3063 DEBUG(3,("lockread fnum=%d num=%d nread=%d\n",
3064 fsp->fnum, (int)numtoread, (int)nread));
3066 END_PROFILE(SMBlockread);
3067 return;
3070 #undef DBGC_CLASS
3071 #define DBGC_CLASS DBGC_ALL
3073 /****************************************************************************
3074 Reply to a read.
3075 ****************************************************************************/
3077 void reply_read(struct smb_request *req)
3079 connection_struct *conn = req->conn;
3080 size_t numtoread;
3081 ssize_t nread = 0;
3082 char *data;
3083 SMB_OFF_T startpos;
3084 int outsize = 0;
3085 files_struct *fsp;
3087 START_PROFILE(SMBread);
3089 if (req->wct < 3) {
3090 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3091 END_PROFILE(SMBread);
3092 return;
3095 fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
3097 if (!check_fsp(conn, req, fsp)) {
3098 END_PROFILE(SMBread);
3099 return;
3102 if (!CHECK_READ(fsp,req->inbuf)) {
3103 reply_doserror(req, ERRDOS, ERRbadaccess);
3104 END_PROFILE(SMBread);
3105 return;
3108 numtoread = SVAL(req->inbuf,smb_vwv1);
3109 startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv2);
3111 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
3114 * The requested read size cannot be greater than max_recv. JRA.
3116 if (numtoread > max_recv) {
3117 DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u). \
3118 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
3119 (unsigned int)numtoread, (unsigned int)max_recv ));
3120 numtoread = MIN(numtoread,max_recv);
3123 reply_outbuf(req, 5, numtoread+3);
3125 data = smb_buf(req->outbuf) + 3;
3127 if (is_locked(fsp, (uint32)req->smbpid, (SMB_BIG_UINT)numtoread,
3128 (SMB_BIG_UINT)startpos, READ_LOCK)) {
3129 reply_doserror(req, ERRDOS,ERRlock);
3130 END_PROFILE(SMBread);
3131 return;
3134 if (numtoread > 0)
3135 nread = read_file(fsp,data,startpos,numtoread);
3137 if (nread < 0) {
3138 reply_unixerror(req, ERRDOS,ERRnoaccess);
3139 END_PROFILE(SMBread);
3140 return;
3143 srv_set_message((char *)req->outbuf, 5, nread+3, False);
3145 SSVAL(req->outbuf,smb_vwv0,nread);
3146 SSVAL(req->outbuf,smb_vwv5,nread+3);
3147 SCVAL(smb_buf(req->outbuf),0,1);
3148 SSVAL(smb_buf(req->outbuf),1,nread);
3150 DEBUG( 3, ( "read fnum=%d num=%d nread=%d\n",
3151 fsp->fnum, (int)numtoread, (int)nread ) );
3153 END_PROFILE(SMBread);
3154 return;
3157 /****************************************************************************
3158 Setup readX header.
3159 ****************************************************************************/
3161 static int setup_readX_header(char *outbuf, size_t smb_maxcnt)
3163 int outsize;
3164 char *data;
3166 outsize = srv_set_message(outbuf,12,smb_maxcnt,False);
3167 data = smb_buf(outbuf);
3169 memset(outbuf+smb_vwv0,'\0',24); /* valgrind init. */
3171 SCVAL(outbuf,smb_vwv0,0xFF);
3172 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be -1. */
3173 SSVAL(outbuf,smb_vwv5,smb_maxcnt);
3174 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
3175 SSVAL(outbuf,smb_vwv7,(smb_maxcnt >> 16));
3176 SSVAL(smb_buf(outbuf),-2,smb_maxcnt);
3177 /* Reset the outgoing length, set_message truncates at 0x1FFFF. */
3178 _smb_setlen_large(outbuf,(smb_size + 12*2 + smb_maxcnt - 4));
3179 return outsize;
3182 /****************************************************************************
3183 Reply to a read and X - possibly using sendfile.
3184 ****************************************************************************/
3186 static void send_file_readX(connection_struct *conn, struct smb_request *req,
3187 files_struct *fsp, SMB_OFF_T startpos,
3188 size_t smb_maxcnt)
3190 SMB_STRUCT_STAT sbuf;
3191 ssize_t nread = -1;
3193 if(SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
3194 reply_unixerror(req, ERRDOS, ERRnoaccess);
3195 return;
3198 if (startpos > sbuf.st_size) {
3199 smb_maxcnt = 0;
3200 } else if (smb_maxcnt > (sbuf.st_size - startpos)) {
3201 smb_maxcnt = (sbuf.st_size - startpos);
3204 if (smb_maxcnt == 0) {
3205 goto normal_read;
3208 #if defined(WITH_SENDFILE)
3210 * We can only use sendfile on a non-chained packet
3211 * but we can use on a non-oplocked file. tridge proved this
3212 * on a train in Germany :-). JRA.
3215 if ((chain_size == 0) && (CVAL(req->inbuf,smb_vwv0) == 0xFF) &&
3216 !is_encrypted_packet(req->inbuf) && (fsp->base_fsp == NULL) &&
3217 lp_use_sendfile(SNUM(conn)) && (fsp->wcp == NULL) ) {
3218 uint8 headerbuf[smb_size + 12 * 2];
3219 DATA_BLOB header;
3222 * Set up the packet header before send. We
3223 * assume here the sendfile will work (get the
3224 * correct amount of data).
3227 header = data_blob_const(headerbuf, sizeof(headerbuf));
3229 construct_reply_common((char *)req->inbuf, (char *)headerbuf);
3230 setup_readX_header((char *)headerbuf, smb_maxcnt);
3232 if ((nread = SMB_VFS_SENDFILE(smbd_server_fd(), fsp, &header, startpos, smb_maxcnt)) == -1) {
3233 /* Returning ENOSYS or EINVAL means no data at all was sent.
3234 Do this as a normal read. */
3235 if (errno == ENOSYS || errno == EINVAL) {
3236 goto normal_read;
3240 * Special hack for broken Linux with no working sendfile. If we
3241 * return EINTR we sent the header but not the rest of the data.
3242 * Fake this up by doing read/write calls.
3245 if (errno == EINTR) {
3246 /* Ensure we don't do this again. */
3247 set_use_sendfile(SNUM(conn), False);
3248 DEBUG(0,("send_file_readX: sendfile not available. Faking..\n"));
3249 nread = fake_sendfile(fsp, startpos,
3250 smb_maxcnt);
3251 if (nread == -1) {
3252 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
3253 fsp->fsp_name, strerror(errno) ));
3254 exit_server_cleanly("send_file_readX: fake_sendfile failed");
3256 DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
3257 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
3258 /* No outbuf here means successful sendfile. */
3259 TALLOC_FREE(req->outbuf);
3260 return;
3263 DEBUG(0,("send_file_readX: sendfile failed for file %s (%s). Terminating\n",
3264 fsp->fsp_name, strerror(errno) ));
3265 exit_server_cleanly("send_file_readX sendfile failed");
3268 DEBUG( 3, ( "send_file_readX: sendfile fnum=%d max=%d nread=%d\n",
3269 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
3270 /* No outbuf here means successful sendfile. */
3271 TALLOC_FREE(req->outbuf);
3272 return;
3274 #endif
3276 normal_read:
3278 if ((smb_maxcnt & 0xFF0000) > 0x10000) {
3279 uint8 headerbuf[smb_size + 2*12];
3281 construct_reply_common((char *)req->inbuf, (char *)headerbuf);
3282 setup_readX_header((char *)headerbuf, smb_maxcnt);
3284 /* Send out the header. */
3285 if (write_data(smbd_server_fd(), (char *)headerbuf,
3286 sizeof(headerbuf)) != sizeof(headerbuf)) {
3287 DEBUG(0,("send_file_readX: write_data failed for file %s (%s). Terminating\n",
3288 fsp->fsp_name, strerror(errno) ));
3289 exit_server_cleanly("send_file_readX sendfile failed");
3291 nread = fake_sendfile(fsp, startpos, smb_maxcnt);
3292 if (nread == -1) {
3293 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
3294 fsp->fsp_name, strerror(errno) ));
3295 exit_server_cleanly("send_file_readX: fake_sendfile failed");
3297 TALLOC_FREE(req->outbuf);
3298 return;
3299 } else {
3300 reply_outbuf(req, 12, smb_maxcnt);
3302 nread = read_file(fsp, smb_buf(req->outbuf), startpos,
3303 smb_maxcnt);
3304 if (nread < 0) {
3305 reply_unixerror(req, ERRDOS, ERRnoaccess);
3306 return;
3309 setup_readX_header((char *)req->outbuf, nread);
3311 DEBUG( 3, ( "send_file_readX fnum=%d max=%d nread=%d\n",
3312 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
3314 chain_reply(req);
3316 return;
3320 /****************************************************************************
3321 Reply to a read and X.
3322 ****************************************************************************/
3324 void reply_read_and_X(struct smb_request *req)
3326 connection_struct *conn = req->conn;
3327 files_struct *fsp;
3328 SMB_OFF_T startpos;
3329 size_t smb_maxcnt;
3330 bool big_readX = False;
3331 #if 0
3332 size_t smb_mincnt = SVAL(req->inbuf,smb_vwv6);
3333 #endif
3335 START_PROFILE(SMBreadX);
3337 if ((req->wct != 10) && (req->wct != 12)) {
3338 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3339 return;
3342 fsp = file_fsp(SVAL(req->inbuf,smb_vwv2));
3343 startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv3);
3344 smb_maxcnt = SVAL(req->inbuf,smb_vwv5);
3346 /* If it's an IPC, pass off the pipe handler. */
3347 if (IS_IPC(conn)) {
3348 reply_pipe_read_and_X(req);
3349 END_PROFILE(SMBreadX);
3350 return;
3353 if (!check_fsp(conn, req, fsp)) {
3354 END_PROFILE(SMBreadX);
3355 return;
3358 if (!CHECK_READ(fsp,req->inbuf)) {
3359 reply_doserror(req, ERRDOS,ERRbadaccess);
3360 END_PROFILE(SMBreadX);
3361 return;
3364 if (global_client_caps & CAP_LARGE_READX) {
3365 size_t upper_size = SVAL(req->inbuf,smb_vwv7);
3366 smb_maxcnt |= (upper_size<<16);
3367 if (upper_size > 1) {
3368 /* Can't do this on a chained packet. */
3369 if ((CVAL(req->inbuf,smb_vwv0) != 0xFF)) {
3370 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
3371 END_PROFILE(SMBreadX);
3372 return;
3374 /* We currently don't do this on signed or sealed data. */
3375 if (srv_is_signing_active() || is_encrypted_packet(req->inbuf)) {
3376 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
3377 END_PROFILE(SMBreadX);
3378 return;
3380 /* Is there room in the reply for this data ? */
3381 if (smb_maxcnt > (0xFFFFFF - (smb_size -4 + 12*2))) {
3382 reply_nterror(req,
3383 NT_STATUS_INVALID_PARAMETER);
3384 END_PROFILE(SMBreadX);
3385 return;
3387 big_readX = True;
3391 if (req->wct == 12) {
3392 #ifdef LARGE_SMB_OFF_T
3394 * This is a large offset (64 bit) read.
3396 startpos |= (((SMB_OFF_T)IVAL(req->inbuf,smb_vwv10)) << 32);
3398 #else /* !LARGE_SMB_OFF_T */
3401 * Ensure we haven't been sent a >32 bit offset.
3404 if(IVAL(req->inbuf,smb_vwv10) != 0) {
3405 DEBUG(0,("reply_read_and_X - large offset (%x << 32) "
3406 "used and we don't support 64 bit offsets.\n",
3407 (unsigned int)IVAL(req->inbuf,smb_vwv10) ));
3408 END_PROFILE(SMBreadX);
3409 reply_doserror(req, ERRDOS, ERRbadaccess);
3410 return;
3413 #endif /* LARGE_SMB_OFF_T */
3417 if (is_locked(fsp, (uint32)req->smbpid, (SMB_BIG_UINT)smb_maxcnt,
3418 (SMB_BIG_UINT)startpos, READ_LOCK)) {
3419 END_PROFILE(SMBreadX);
3420 reply_doserror(req, ERRDOS, ERRlock);
3421 return;
3424 if (!big_readX &&
3425 schedule_aio_read_and_X(conn, req, fsp, startpos, smb_maxcnt)) {
3426 END_PROFILE(SMBreadX);
3427 return;
3430 send_file_readX(conn, req, fsp, startpos, smb_maxcnt);
3432 END_PROFILE(SMBreadX);
3433 return;
3436 /****************************************************************************
3437 Error replies to writebraw must have smb_wct == 1. Fix this up.
3438 ****************************************************************************/
3440 void error_to_writebrawerr(struct smb_request *req)
3442 uint8 *old_outbuf = req->outbuf;
3444 reply_outbuf(req, 1, 0);
3446 memcpy(req->outbuf, old_outbuf, smb_size);
3447 TALLOC_FREE(old_outbuf);
3450 /****************************************************************************
3451 Reply to a writebraw (core+ or LANMAN1.0 protocol).
3452 ****************************************************************************/
3454 void reply_writebraw(struct smb_request *req)
3456 connection_struct *conn = req->conn;
3457 char *buf = NULL;
3458 ssize_t nwritten=0;
3459 ssize_t total_written=0;
3460 size_t numtowrite=0;
3461 size_t tcount;
3462 SMB_OFF_T startpos;
3463 char *data=NULL;
3464 bool write_through;
3465 files_struct *fsp;
3466 NTSTATUS status;
3468 START_PROFILE(SMBwritebraw);
3471 * If we ever reply with an error, it must have the SMB command
3472 * type of SMBwritec, not SMBwriteBraw, as this tells the client
3473 * we're finished.
3475 SCVAL(req->inbuf,smb_com,SMBwritec);
3477 if (srv_is_signing_active()) {
3478 END_PROFILE(SMBwritebraw);
3479 exit_server_cleanly("reply_writebraw: SMB signing is active - "
3480 "raw reads/writes are disallowed.");
3483 if (req->wct < 12) {
3484 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3485 error_to_writebrawerr(req);
3486 END_PROFILE(SMBwritebraw);
3487 return;
3490 fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
3491 if (!check_fsp(conn, req, fsp)) {
3492 error_to_writebrawerr(req);
3493 END_PROFILE(SMBwritebraw);
3494 return;
3497 if (!CHECK_WRITE(fsp)) {
3498 reply_doserror(req, ERRDOS, ERRbadaccess);
3499 error_to_writebrawerr(req);
3500 END_PROFILE(SMBwritebraw);
3501 return;
3504 tcount = IVAL(req->inbuf,smb_vwv1);
3505 startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv3);
3506 write_through = BITSETW(req->inbuf+smb_vwv7,0);
3508 /* We have to deal with slightly different formats depending
3509 on whether we are using the core+ or lanman1.0 protocol */
3511 if(Protocol <= PROTOCOL_COREPLUS) {
3512 numtowrite = SVAL(smb_buf(req->inbuf),-2);
3513 data = smb_buf(req->inbuf);
3514 } else {
3515 numtowrite = SVAL(req->inbuf,smb_vwv10);
3516 data = smb_base(req->inbuf) + SVAL(req->inbuf, smb_vwv11);
3519 /* Ensure we don't write bytes past the end of this packet. */
3520 if (data + numtowrite > smb_base(req->inbuf) + smb_len(req->inbuf)) {
3521 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3522 error_to_writebrawerr(req);
3523 END_PROFILE(SMBwritebraw);
3524 return;
3527 if (is_locked(fsp,(uint32)req->smbpid,(SMB_BIG_UINT)tcount,
3528 (SMB_BIG_UINT)startpos, WRITE_LOCK)) {
3529 reply_doserror(req, ERRDOS, ERRlock);
3530 error_to_writebrawerr(req);
3531 END_PROFILE(SMBwritebraw);
3532 return;
3535 if (numtowrite>0) {
3536 nwritten = write_file(req,fsp,data,startpos,numtowrite);
3539 DEBUG(3,("reply_writebraw: initial write fnum=%d start=%.0f num=%d "
3540 "wrote=%d sync=%d\n",
3541 fsp->fnum, (double)startpos, (int)numtowrite,
3542 (int)nwritten, (int)write_through));
3544 if (nwritten < (ssize_t)numtowrite) {
3545 reply_unixerror(req, ERRHRD, ERRdiskfull);
3546 error_to_writebrawerr(req);
3547 END_PROFILE(SMBwritebraw);
3548 return;
3551 total_written = nwritten;
3553 /* Allocate a buffer of 64k + length. */
3554 buf = TALLOC_ARRAY(NULL, char, 65540);
3555 if (!buf) {
3556 reply_doserror(req, ERRDOS, ERRnomem);
3557 error_to_writebrawerr(req);
3558 END_PROFILE(SMBwritebraw);
3559 return;
3562 /* Return a SMBwritebraw message to the redirector to tell
3563 * it to send more bytes */
3565 memcpy(buf, req->inbuf, smb_size);
3566 srv_set_message(buf,Protocol>PROTOCOL_COREPLUS?1:0,0,True);
3567 SCVAL(buf,smb_com,SMBwritebraw);
3568 SSVALS(buf,smb_vwv0,0xFFFF);
3569 show_msg(buf);
3570 if (!srv_send_smb(smbd_server_fd(),
3571 buf,
3572 IS_CONN_ENCRYPTED(conn))) {
3573 exit_server_cleanly("reply_writebraw: srv_send_smb "
3574 "failed.");
3577 /* Now read the raw data into the buffer and write it */
3578 status = read_smb_length(smbd_server_fd(), buf, SMB_SECONDARY_WAIT,
3579 &numtowrite);
3580 if (!NT_STATUS_IS_OK(status)) {
3581 exit_server_cleanly("secondary writebraw failed");
3584 /* Set up outbuf to return the correct size */
3585 reply_outbuf(req, 1, 0);
3587 if (numtowrite != 0) {
3589 if (numtowrite > 0xFFFF) {
3590 DEBUG(0,("reply_writebraw: Oversize secondary write "
3591 "raw requested (%u). Terminating\n",
3592 (unsigned int)numtowrite ));
3593 exit_server_cleanly("secondary writebraw failed");
3596 if (tcount > nwritten+numtowrite) {
3597 DEBUG(3,("reply_writebraw: Client overestimated the "
3598 "write %d %d %d\n",
3599 (int)tcount,(int)nwritten,(int)numtowrite));
3602 status = read_data(smbd_server_fd(), buf+4, numtowrite);
3604 if (!NT_STATUS_IS_OK(status)) {
3605 DEBUG(0,("reply_writebraw: Oversize secondary write "
3606 "raw read failed (%s). Terminating\n",
3607 nt_errstr(status)));
3608 exit_server_cleanly("secondary writebraw failed");
3611 nwritten = write_file(req,fsp,buf+4,startpos+nwritten,numtowrite);
3612 if (nwritten == -1) {
3613 TALLOC_FREE(buf);
3614 reply_unixerror(req, ERRHRD, ERRdiskfull);
3615 error_to_writebrawerr(req);
3616 END_PROFILE(SMBwritebraw);
3617 return;
3620 if (nwritten < (ssize_t)numtowrite) {
3621 SCVAL(req->outbuf,smb_rcls,ERRHRD);
3622 SSVAL(req->outbuf,smb_err,ERRdiskfull);
3625 if (nwritten > 0) {
3626 total_written += nwritten;
3630 TALLOC_FREE(buf);
3631 SSVAL(req->outbuf,smb_vwv0,total_written);
3633 status = sync_file(conn, fsp, write_through);
3634 if (!NT_STATUS_IS_OK(status)) {
3635 DEBUG(5,("reply_writebraw: sync_file for %s returned %s\n",
3636 fsp->fsp_name, nt_errstr(status) ));
3637 reply_nterror(req, status);
3638 error_to_writebrawerr(req);
3639 END_PROFILE(SMBwritebraw);
3640 return;
3643 DEBUG(3,("reply_writebraw: secondart write fnum=%d start=%.0f num=%d "
3644 "wrote=%d\n",
3645 fsp->fnum, (double)startpos, (int)numtowrite,
3646 (int)total_written));
3648 /* We won't return a status if write through is not selected - this
3649 * follows what WfWg does */
3650 END_PROFILE(SMBwritebraw);
3652 if (!write_through && total_written==tcount) {
3654 #if RABBIT_PELLET_FIX
3656 * Fix for "rabbit pellet" mode, trigger an early TCP ack by
3657 * sending a SMBkeepalive. Thanks to DaveCB at Sun for this.
3658 * JRA.
3660 if (!send_keepalive(smbd_server_fd())) {
3661 exit_server_cleanly("reply_writebraw: send of "
3662 "keepalive failed");
3664 #endif
3665 TALLOC_FREE(req->outbuf);
3667 return;
3670 #undef DBGC_CLASS
3671 #define DBGC_CLASS DBGC_LOCKING
3673 /****************************************************************************
3674 Reply to a writeunlock (core+).
3675 ****************************************************************************/
3677 void reply_writeunlock(struct smb_request *req)
3679 connection_struct *conn = req->conn;
3680 ssize_t nwritten = -1;
3681 size_t numtowrite;
3682 SMB_OFF_T startpos;
3683 char *data;
3684 NTSTATUS status = NT_STATUS_OK;
3685 files_struct *fsp;
3687 START_PROFILE(SMBwriteunlock);
3689 if (req->wct < 5) {
3690 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3691 END_PROFILE(SMBwriteunlock);
3692 return;
3695 fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
3697 if (!check_fsp(conn, req, fsp)) {
3698 END_PROFILE(SMBwriteunlock);
3699 return;
3702 if (!CHECK_WRITE(fsp)) {
3703 reply_doserror(req, ERRDOS,ERRbadaccess);
3704 END_PROFILE(SMBwriteunlock);
3705 return;
3708 numtowrite = SVAL(req->inbuf,smb_vwv1);
3709 startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv2);
3710 data = smb_buf(req->inbuf) + 3;
3712 if (numtowrite
3713 && is_locked(fsp, (uint32)req->smbpid, (SMB_BIG_UINT)numtowrite,
3714 (SMB_BIG_UINT)startpos, WRITE_LOCK)) {
3715 reply_doserror(req, ERRDOS, ERRlock);
3716 END_PROFILE(SMBwriteunlock);
3717 return;
3720 /* The special X/Open SMB protocol handling of
3721 zero length writes is *NOT* done for
3722 this call */
3723 if(numtowrite == 0) {
3724 nwritten = 0;
3725 } else {
3726 nwritten = write_file(req,fsp,data,startpos,numtowrite);
3729 status = sync_file(conn, fsp, False /* write through */);
3730 if (!NT_STATUS_IS_OK(status)) {
3731 DEBUG(5,("reply_writeunlock: sync_file for %s returned %s\n",
3732 fsp->fsp_name, nt_errstr(status) ));
3733 reply_nterror(req, status);
3734 END_PROFILE(SMBwriteunlock);
3735 return;
3738 if(((nwritten < numtowrite) && (numtowrite != 0))||(nwritten < 0)) {
3739 reply_unixerror(req, ERRHRD, ERRdiskfull);
3740 END_PROFILE(SMBwriteunlock);
3741 return;
3744 if (numtowrite) {
3745 status = do_unlock(smbd_messaging_context(),
3746 fsp,
3747 req->smbpid,
3748 (SMB_BIG_UINT)numtowrite,
3749 (SMB_BIG_UINT)startpos,
3750 WINDOWS_LOCK);
3752 if (NT_STATUS_V(status)) {
3753 reply_nterror(req, status);
3754 END_PROFILE(SMBwriteunlock);
3755 return;
3759 reply_outbuf(req, 1, 0);
3761 SSVAL(req->outbuf,smb_vwv0,nwritten);
3763 DEBUG(3,("writeunlock fnum=%d num=%d wrote=%d\n",
3764 fsp->fnum, (int)numtowrite, (int)nwritten));
3766 END_PROFILE(SMBwriteunlock);
3767 return;
3770 #undef DBGC_CLASS
3771 #define DBGC_CLASS DBGC_ALL
3773 /****************************************************************************
3774 Reply to a write.
3775 ****************************************************************************/
3777 void reply_write(struct smb_request *req)
3779 connection_struct *conn = req->conn;
3780 size_t numtowrite;
3781 ssize_t nwritten = -1;
3782 SMB_OFF_T startpos;
3783 char *data;
3784 files_struct *fsp;
3785 NTSTATUS status;
3787 START_PROFILE(SMBwrite);
3789 if (req->wct < 5) {
3790 END_PROFILE(SMBwrite);
3791 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3792 return;
3795 /* If it's an IPC, pass off the pipe handler. */
3796 if (IS_IPC(conn)) {
3797 reply_pipe_write(req);
3798 END_PROFILE(SMBwrite);
3799 return;
3802 fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
3804 if (!check_fsp(conn, req, fsp)) {
3805 END_PROFILE(SMBwrite);
3806 return;
3809 if (!CHECK_WRITE(fsp)) {
3810 reply_doserror(req, ERRDOS, ERRbadaccess);
3811 END_PROFILE(SMBwrite);
3812 return;
3815 numtowrite = SVAL(req->inbuf,smb_vwv1);
3816 startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv2);
3817 data = smb_buf(req->inbuf) + 3;
3819 if (is_locked(fsp, (uint32)req->smbpid, (SMB_BIG_UINT)numtowrite,
3820 (SMB_BIG_UINT)startpos, WRITE_LOCK)) {
3821 reply_doserror(req, ERRDOS, ERRlock);
3822 END_PROFILE(SMBwrite);
3823 return;
3827 * X/Open SMB protocol says that if smb_vwv1 is
3828 * zero then the file size should be extended or
3829 * truncated to the size given in smb_vwv[2-3].
3832 if(numtowrite == 0) {
3834 * This is actually an allocate call, and set EOF. JRA.
3836 nwritten = vfs_allocate_file_space(fsp, (SMB_OFF_T)startpos);
3837 if (nwritten < 0) {
3838 reply_nterror(req, NT_STATUS_DISK_FULL);
3839 END_PROFILE(SMBwrite);
3840 return;
3842 nwritten = vfs_set_filelen(fsp, (SMB_OFF_T)startpos);
3843 if (nwritten < 0) {
3844 reply_nterror(req, NT_STATUS_DISK_FULL);
3845 END_PROFILE(SMBwrite);
3846 return;
3848 trigger_write_time_update_immediate(fsp);
3849 } else {
3850 nwritten = write_file(req,fsp,data,startpos,numtowrite);
3853 status = sync_file(conn, fsp, False);
3854 if (!NT_STATUS_IS_OK(status)) {
3855 DEBUG(5,("reply_write: sync_file for %s returned %s\n",
3856 fsp->fsp_name, nt_errstr(status) ));
3857 reply_nterror(req, status);
3858 END_PROFILE(SMBwrite);
3859 return;
3862 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
3863 reply_unixerror(req, ERRHRD, ERRdiskfull);
3864 END_PROFILE(SMBwrite);
3865 return;
3868 reply_outbuf(req, 1, 0);
3870 SSVAL(req->outbuf,smb_vwv0,nwritten);
3872 if (nwritten < (ssize_t)numtowrite) {
3873 SCVAL(req->outbuf,smb_rcls,ERRHRD);
3874 SSVAL(req->outbuf,smb_err,ERRdiskfull);
3877 DEBUG(3,("write fnum=%d num=%d wrote=%d\n", fsp->fnum, (int)numtowrite, (int)nwritten));
3879 END_PROFILE(SMBwrite);
3880 return;
3883 /****************************************************************************
3884 Ensure a buffer is a valid writeX for recvfile purposes.
3885 ****************************************************************************/
3887 #define STANDARD_WRITE_AND_X_HEADER_SIZE (smb_size - 4 + /* basic header */ \
3888 (2*14) + /* word count (including bcc) */ \
3889 1 /* pad byte */)
3891 bool is_valid_writeX_buffer(const uint8_t *inbuf)
3893 size_t numtowrite;
3894 connection_struct *conn = NULL;
3895 unsigned int doff = 0;
3896 size_t len = smb_len_large(inbuf);
3898 if (is_encrypted_packet(inbuf)) {
3899 /* Can't do this on encrypted
3900 * connections. */
3901 return false;
3904 if (CVAL(inbuf,smb_com) != SMBwriteX) {
3905 return false;
3908 if (CVAL(inbuf,smb_vwv0) != 0xFF ||
3909 CVAL(inbuf,smb_wct) != 14) {
3910 DEBUG(10,("is_valid_writeX_buffer: chained or "
3911 "invalid word length.\n"));
3912 return false;
3915 conn = conn_find(SVAL(inbuf, smb_tid));
3916 if (conn == NULL) {
3917 DEBUG(10,("is_valid_writeX_buffer: bad tid\n"));
3918 return false;
3920 if (IS_IPC(conn)) {
3921 DEBUG(10,("is_valid_writeX_buffer: IPC$ tid\n"));
3922 return false;
3924 if (IS_PRINT(conn)) {
3925 DEBUG(10,("is_valid_writeX_buffer: printing tid\n"));
3926 return false;
3928 doff = SVAL(inbuf,smb_vwv11);
3930 numtowrite = SVAL(inbuf,smb_vwv10);
3932 if (len > doff && len - doff > 0xFFFF) {
3933 numtowrite |= (((size_t)SVAL(inbuf,smb_vwv9))<<16);
3936 if (numtowrite == 0) {
3937 DEBUG(10,("is_valid_writeX_buffer: zero write\n"));
3938 return false;
3941 /* Ensure the sizes match up. */
3942 if (doff < STANDARD_WRITE_AND_X_HEADER_SIZE) {
3943 /* no pad byte...old smbclient :-( */
3944 DEBUG(10,("is_valid_writeX_buffer: small doff %u (min %u)\n",
3945 (unsigned int)doff,
3946 (unsigned int)STANDARD_WRITE_AND_X_HEADER_SIZE));
3947 return false;
3950 if (len - doff != numtowrite) {
3951 DEBUG(10,("is_valid_writeX_buffer: doff mismatch "
3952 "len = %u, doff = %u, numtowrite = %u\n",
3953 (unsigned int)len,
3954 (unsigned int)doff,
3955 (unsigned int)numtowrite ));
3956 return false;
3959 DEBUG(10,("is_valid_writeX_buffer: true "
3960 "len = %u, doff = %u, numtowrite = %u\n",
3961 (unsigned int)len,
3962 (unsigned int)doff,
3963 (unsigned int)numtowrite ));
3965 return true;
3968 /****************************************************************************
3969 Reply to a write and X.
3970 ****************************************************************************/
3972 void reply_write_and_X(struct smb_request *req)
3974 connection_struct *conn = req->conn;
3975 files_struct *fsp;
3976 SMB_OFF_T startpos;
3977 size_t numtowrite;
3978 bool write_through;
3979 ssize_t nwritten;
3980 unsigned int smb_doff;
3981 unsigned int smblen;
3982 char *data;
3983 NTSTATUS status;
3985 START_PROFILE(SMBwriteX);
3987 if ((req->wct != 12) && (req->wct != 14)) {
3988 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3989 END_PROFILE(SMBwriteX);
3990 return;
3993 numtowrite = SVAL(req->inbuf,smb_vwv10);
3994 smb_doff = SVAL(req->inbuf,smb_vwv11);
3995 smblen = smb_len(req->inbuf);
3997 if (req->unread_bytes > 0xFFFF ||
3998 (smblen > smb_doff &&
3999 smblen - smb_doff > 0xFFFF)) {
4000 numtowrite |= (((size_t)SVAL(req->inbuf,smb_vwv9))<<16);
4003 if (req->unread_bytes) {
4004 /* Can't do a recvfile write on IPC$ */
4005 if (IS_IPC(conn)) {
4006 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4007 END_PROFILE(SMBwriteX);
4008 return;
4010 if (numtowrite != req->unread_bytes) {
4011 reply_doserror(req, ERRDOS, ERRbadmem);
4012 END_PROFILE(SMBwriteX);
4013 return;
4015 } else {
4016 if (smb_doff > smblen || smb_doff + numtowrite < numtowrite ||
4017 smb_doff + numtowrite > smblen) {
4018 reply_doserror(req, ERRDOS, ERRbadmem);
4019 END_PROFILE(SMBwriteX);
4020 return;
4024 /* If it's an IPC, pass off the pipe handler. */
4025 if (IS_IPC(conn)) {
4026 if (req->unread_bytes) {
4027 reply_doserror(req, ERRDOS, ERRbadmem);
4028 END_PROFILE(SMBwriteX);
4029 return;
4031 reply_pipe_write_and_X(req);
4032 END_PROFILE(SMBwriteX);
4033 return;
4036 fsp = file_fsp(SVAL(req->inbuf,smb_vwv2));
4037 startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv3);
4038 write_through = BITSETW(req->inbuf+smb_vwv7,0);
4040 if (!check_fsp(conn, req, fsp)) {
4041 END_PROFILE(SMBwriteX);
4042 return;
4045 if (!CHECK_WRITE(fsp)) {
4046 reply_doserror(req, ERRDOS, ERRbadaccess);
4047 END_PROFILE(SMBwriteX);
4048 return;
4051 data = smb_base(req->inbuf) + smb_doff;
4053 if(req->wct == 14) {
4054 #ifdef LARGE_SMB_OFF_T
4056 * This is a large offset (64 bit) write.
4058 startpos |= (((SMB_OFF_T)IVAL(req->inbuf,smb_vwv12)) << 32);
4060 #else /* !LARGE_SMB_OFF_T */
4063 * Ensure we haven't been sent a >32 bit offset.
4066 if(IVAL(req->inbuf,smb_vwv12) != 0) {
4067 DEBUG(0,("reply_write_and_X - large offset (%x << 32) "
4068 "used and we don't support 64 bit offsets.\n",
4069 (unsigned int)IVAL(req->inbuf,smb_vwv12) ));
4070 reply_doserror(req, ERRDOS, ERRbadaccess);
4071 END_PROFILE(SMBwriteX);
4072 return;
4075 #endif /* LARGE_SMB_OFF_T */
4078 if (is_locked(fsp,(uint32)req->smbpid,
4079 (SMB_BIG_UINT)numtowrite,
4080 (SMB_BIG_UINT)startpos, WRITE_LOCK)) {
4081 reply_doserror(req, ERRDOS, ERRlock);
4082 END_PROFILE(SMBwriteX);
4083 return;
4086 /* X/Open SMB protocol says that, unlike SMBwrite
4087 if the length is zero then NO truncation is
4088 done, just a write of zero. To truncate a file,
4089 use SMBwrite. */
4091 if(numtowrite == 0) {
4092 nwritten = 0;
4093 } else {
4095 if ((req->unread_bytes == 0) &&
4096 schedule_aio_write_and_X(conn, req, fsp, data, startpos,
4097 numtowrite)) {
4098 END_PROFILE(SMBwriteX);
4099 return;
4102 nwritten = write_file(req,fsp,data,startpos,numtowrite);
4105 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
4106 reply_unixerror(req, ERRHRD, ERRdiskfull);
4107 END_PROFILE(SMBwriteX);
4108 return;
4111 reply_outbuf(req, 6, 0);
4112 SSVAL(req->outbuf,smb_vwv2,nwritten);
4113 SSVAL(req->outbuf,smb_vwv4,nwritten>>16);
4115 if (nwritten < (ssize_t)numtowrite) {
4116 SCVAL(req->outbuf,smb_rcls,ERRHRD);
4117 SSVAL(req->outbuf,smb_err,ERRdiskfull);
4120 DEBUG(3,("writeX fnum=%d num=%d wrote=%d\n",
4121 fsp->fnum, (int)numtowrite, (int)nwritten));
4123 status = sync_file(conn, fsp, write_through);
4124 if (!NT_STATUS_IS_OK(status)) {
4125 DEBUG(5,("reply_write_and_X: sync_file for %s returned %s\n",
4126 fsp->fsp_name, nt_errstr(status) ));
4127 reply_nterror(req, status);
4128 END_PROFILE(SMBwriteX);
4129 return;
4132 END_PROFILE(SMBwriteX);
4133 chain_reply(req);
4134 return;
4137 /****************************************************************************
4138 Reply to a lseek.
4139 ****************************************************************************/
4141 void reply_lseek(struct smb_request *req)
4143 connection_struct *conn = req->conn;
4144 SMB_OFF_T startpos;
4145 SMB_OFF_T res= -1;
4146 int mode,umode;
4147 files_struct *fsp;
4149 START_PROFILE(SMBlseek);
4151 if (req->wct < 4) {
4152 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4153 END_PROFILE(SMBlseek);
4154 return;
4157 fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
4159 if (!check_fsp(conn, req, fsp)) {
4160 return;
4163 flush_write_cache(fsp, SEEK_FLUSH);
4165 mode = SVAL(req->inbuf,smb_vwv1) & 3;
4166 /* NB. This doesn't use IVAL_TO_SMB_OFF_T as startpos can be signed in this case. */
4167 startpos = (SMB_OFF_T)IVALS(req->inbuf,smb_vwv2);
4169 switch (mode) {
4170 case 0:
4171 umode = SEEK_SET;
4172 res = startpos;
4173 break;
4174 case 1:
4175 umode = SEEK_CUR;
4176 res = fsp->fh->pos + startpos;
4177 break;
4178 case 2:
4179 umode = SEEK_END;
4180 break;
4181 default:
4182 umode = SEEK_SET;
4183 res = startpos;
4184 break;
4187 if (umode == SEEK_END) {
4188 if((res = SMB_VFS_LSEEK(fsp,startpos,umode)) == -1) {
4189 if(errno == EINVAL) {
4190 SMB_OFF_T current_pos = startpos;
4191 SMB_STRUCT_STAT sbuf;
4193 if(SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
4194 reply_unixerror(req, ERRDOS,
4195 ERRnoaccess);
4196 END_PROFILE(SMBlseek);
4197 return;
4200 current_pos += sbuf.st_size;
4201 if(current_pos < 0)
4202 res = SMB_VFS_LSEEK(fsp,0,SEEK_SET);
4206 if(res == -1) {
4207 reply_unixerror(req, ERRDOS, ERRnoaccess);
4208 END_PROFILE(SMBlseek);
4209 return;
4213 fsp->fh->pos = res;
4215 reply_outbuf(req, 2, 0);
4216 SIVAL(req->outbuf,smb_vwv0,res);
4218 DEBUG(3,("lseek fnum=%d ofs=%.0f newpos = %.0f mode=%d\n",
4219 fsp->fnum, (double)startpos, (double)res, mode));
4221 END_PROFILE(SMBlseek);
4222 return;
4225 /****************************************************************************
4226 Reply to a flush.
4227 ****************************************************************************/
4229 void reply_flush(struct smb_request *req)
4231 connection_struct *conn = req->conn;
4232 uint16 fnum;
4233 files_struct *fsp;
4235 START_PROFILE(SMBflush);
4237 if (req->wct < 1) {
4238 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4239 return;
4242 fnum = SVAL(req->inbuf,smb_vwv0);
4243 fsp = file_fsp(fnum);
4245 if ((fnum != 0xFFFF) && !check_fsp(conn, req, fsp)) {
4246 return;
4249 if (!fsp) {
4250 file_sync_all(conn);
4251 } else {
4252 NTSTATUS status = sync_file(conn, fsp, True);
4253 if (!NT_STATUS_IS_OK(status)) {
4254 DEBUG(5,("reply_flush: sync_file for %s returned %s\n",
4255 fsp->fsp_name, nt_errstr(status) ));
4256 reply_nterror(req, status);
4257 END_PROFILE(SMBflush);
4258 return;
4262 reply_outbuf(req, 0, 0);
4264 DEBUG(3,("flush\n"));
4265 END_PROFILE(SMBflush);
4266 return;
4269 /****************************************************************************
4270 Reply to a exit.
4271 conn POINTER CAN BE NULL HERE !
4272 ****************************************************************************/
4274 void reply_exit(struct smb_request *req)
4276 START_PROFILE(SMBexit);
4278 file_close_pid(req->smbpid, req->vuid);
4280 reply_outbuf(req, 0, 0);
4282 DEBUG(3,("exit\n"));
4284 END_PROFILE(SMBexit);
4285 return;
4288 /****************************************************************************
4289 Reply to a close - has to deal with closing a directory opened by NT SMB's.
4290 ****************************************************************************/
4292 void reply_close(struct smb_request *req)
4294 connection_struct *conn = req->conn;
4295 NTSTATUS status = NT_STATUS_OK;
4296 files_struct *fsp = NULL;
4297 START_PROFILE(SMBclose);
4299 if (req->wct < 3) {
4300 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4301 END_PROFILE(SMBclose);
4302 return;
4305 /* If it's an IPC, pass off to the pipe handler. */
4306 if (IS_IPC(conn)) {
4307 reply_pipe_close(conn, req);
4308 END_PROFILE(SMBclose);
4309 return;
4312 fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
4315 * We can only use check_fsp if we know it's not a directory.
4318 if(!fsp || (fsp->conn != conn) || (fsp->vuid != req->vuid)) {
4319 reply_doserror(req, ERRDOS, ERRbadfid);
4320 END_PROFILE(SMBclose);
4321 return;
4324 if(fsp->is_directory) {
4326 * Special case - close NT SMB directory handle.
4328 DEBUG(3,("close directory fnum=%d\n", fsp->fnum));
4329 status = close_file(fsp,NORMAL_CLOSE);
4330 } else {
4331 time_t t;
4333 * Close ordinary file.
4336 DEBUG(3,("close fd=%d fnum=%d (numopen=%d)\n",
4337 fsp->fh->fd, fsp->fnum,
4338 conn->num_files_open));
4341 * Take care of any time sent in the close.
4344 t = srv_make_unix_date3(req->inbuf+smb_vwv1);
4345 set_close_write_time(fsp, convert_time_t_to_timespec(t));
4348 * close_file() returns the unix errno if an error
4349 * was detected on close - normally this is due to
4350 * a disk full error. If not then it was probably an I/O error.
4353 status = close_file(fsp,NORMAL_CLOSE);
4356 if (!NT_STATUS_IS_OK(status)) {
4357 reply_nterror(req, status);
4358 END_PROFILE(SMBclose);
4359 return;
4362 reply_outbuf(req, 0, 0);
4363 END_PROFILE(SMBclose);
4364 return;
4367 /****************************************************************************
4368 Reply to a writeclose (Core+ protocol).
4369 ****************************************************************************/
4371 void reply_writeclose(struct smb_request *req)
4373 connection_struct *conn = req->conn;
4374 size_t numtowrite;
4375 ssize_t nwritten = -1;
4376 NTSTATUS close_status = NT_STATUS_OK;
4377 SMB_OFF_T startpos;
4378 char *data;
4379 struct timespec mtime;
4380 files_struct *fsp;
4382 START_PROFILE(SMBwriteclose);
4384 if (req->wct < 6) {
4385 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4386 END_PROFILE(SMBwriteclose);
4387 return;
4390 fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
4392 if (!check_fsp(conn, req, fsp)) {
4393 END_PROFILE(SMBwriteclose);
4394 return;
4396 if (!CHECK_WRITE(fsp)) {
4397 reply_doserror(req, ERRDOS,ERRbadaccess);
4398 END_PROFILE(SMBwriteclose);
4399 return;
4402 numtowrite = SVAL(req->inbuf,smb_vwv1);
4403 startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv2);
4404 mtime = convert_time_t_to_timespec(srv_make_unix_date3(
4405 req->inbuf+smb_vwv4));
4406 data = smb_buf(req->inbuf) + 1;
4408 if (numtowrite
4409 && is_locked(fsp, (uint32)req->smbpid, (SMB_BIG_UINT)numtowrite,
4410 (SMB_BIG_UINT)startpos, WRITE_LOCK)) {
4411 reply_doserror(req, ERRDOS,ERRlock);
4412 END_PROFILE(SMBwriteclose);
4413 return;
4416 nwritten = write_file(req,fsp,data,startpos,numtowrite);
4418 set_close_write_time(fsp, mtime);
4421 * More insanity. W2K only closes the file if writelen > 0.
4422 * JRA.
4425 if (numtowrite) {
4426 DEBUG(3,("reply_writeclose: zero length write doesn't close file %s\n",
4427 fsp->fsp_name ));
4428 close_status = close_file(fsp,NORMAL_CLOSE);
4431 DEBUG(3,("writeclose fnum=%d num=%d wrote=%d (numopen=%d)\n",
4432 fsp->fnum, (int)numtowrite, (int)nwritten,
4433 conn->num_files_open));
4435 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
4436 reply_doserror(req, ERRHRD, ERRdiskfull);
4437 END_PROFILE(SMBwriteclose);
4438 return;
4441 if(!NT_STATUS_IS_OK(close_status)) {
4442 reply_nterror(req, close_status);
4443 END_PROFILE(SMBwriteclose);
4444 return;
4447 reply_outbuf(req, 1, 0);
4449 SSVAL(req->outbuf,smb_vwv0,nwritten);
4450 END_PROFILE(SMBwriteclose);
4451 return;
4454 #undef DBGC_CLASS
4455 #define DBGC_CLASS DBGC_LOCKING
4457 /****************************************************************************
4458 Reply to a lock.
4459 ****************************************************************************/
4461 void reply_lock(struct smb_request *req)
4463 connection_struct *conn = req->conn;
4464 SMB_BIG_UINT count,offset;
4465 NTSTATUS status;
4466 files_struct *fsp;
4467 struct byte_range_lock *br_lck = NULL;
4469 START_PROFILE(SMBlock);
4471 if (req->wct < 5) {
4472 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4473 END_PROFILE(SMBlock);
4474 return;
4477 fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
4479 if (!check_fsp(conn, req, fsp)) {
4480 END_PROFILE(SMBlock);
4481 return;
4484 release_level_2_oplocks_on_change(fsp);
4486 count = (SMB_BIG_UINT)IVAL(req->inbuf,smb_vwv1);
4487 offset = (SMB_BIG_UINT)IVAL(req->inbuf,smb_vwv3);
4489 DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
4490 fsp->fh->fd, fsp->fnum, (double)offset, (double)count));
4492 br_lck = do_lock(smbd_messaging_context(),
4493 fsp,
4494 req->smbpid,
4495 count,
4496 offset,
4497 WRITE_LOCK,
4498 WINDOWS_LOCK,
4499 False, /* Non-blocking lock. */
4500 &status,
4501 NULL);
4503 TALLOC_FREE(br_lck);
4505 if (NT_STATUS_V(status)) {
4506 reply_nterror(req, status);
4507 END_PROFILE(SMBlock);
4508 return;
4511 reply_outbuf(req, 0, 0);
4513 END_PROFILE(SMBlock);
4514 return;
4517 /****************************************************************************
4518 Reply to a unlock.
4519 ****************************************************************************/
4521 void reply_unlock(struct smb_request *req)
4523 connection_struct *conn = req->conn;
4524 SMB_BIG_UINT count,offset;
4525 NTSTATUS status;
4526 files_struct *fsp;
4528 START_PROFILE(SMBunlock);
4530 if (req->wct < 5) {
4531 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4532 END_PROFILE(SMBunlock);
4533 return;
4536 fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
4538 if (!check_fsp(conn, req, fsp)) {
4539 END_PROFILE(SMBunlock);
4540 return;
4543 count = (SMB_BIG_UINT)IVAL(req->inbuf,smb_vwv1);
4544 offset = (SMB_BIG_UINT)IVAL(req->inbuf,smb_vwv3);
4546 status = do_unlock(smbd_messaging_context(),
4547 fsp,
4548 req->smbpid,
4549 count,
4550 offset,
4551 WINDOWS_LOCK);
4553 if (NT_STATUS_V(status)) {
4554 reply_nterror(req, status);
4555 END_PROFILE(SMBunlock);
4556 return;
4559 DEBUG( 3, ( "unlock fd=%d fnum=%d offset=%.0f count=%.0f\n",
4560 fsp->fh->fd, fsp->fnum, (double)offset, (double)count ) );
4562 reply_outbuf(req, 0, 0);
4564 END_PROFILE(SMBunlock);
4565 return;
4568 #undef DBGC_CLASS
4569 #define DBGC_CLASS DBGC_ALL
4571 /****************************************************************************
4572 Reply to a tdis.
4573 conn POINTER CAN BE NULL HERE !
4574 ****************************************************************************/
4576 void reply_tdis(struct smb_request *req)
4578 connection_struct *conn = req->conn;
4579 START_PROFILE(SMBtdis);
4581 if (!conn) {
4582 DEBUG(4,("Invalid connection in tdis\n"));
4583 reply_doserror(req, ERRSRV, ERRinvnid);
4584 END_PROFILE(SMBtdis);
4585 return;
4588 conn->used = False;
4590 close_cnum(conn,req->vuid);
4591 req->conn = NULL;
4593 reply_outbuf(req, 0, 0);
4594 END_PROFILE(SMBtdis);
4595 return;
4598 /****************************************************************************
4599 Reply to a echo.
4600 conn POINTER CAN BE NULL HERE !
4601 ****************************************************************************/
4603 void reply_echo(struct smb_request *req)
4605 connection_struct *conn = req->conn;
4606 int smb_reverb;
4607 int seq_num;
4608 unsigned int data_len = smb_buflen(req->inbuf);
4610 START_PROFILE(SMBecho);
4612 if (req->wct < 1) {
4613 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4614 END_PROFILE(SMBecho);
4615 return;
4618 if (data_len > BUFFER_SIZE) {
4619 DEBUG(0,("reply_echo: data_len too large.\n"));
4620 reply_nterror(req, NT_STATUS_INSUFFICIENT_RESOURCES);
4621 END_PROFILE(SMBecho);
4622 return;
4625 smb_reverb = SVAL(req->inbuf,smb_vwv0);
4627 reply_outbuf(req, 1, data_len);
4629 /* copy any incoming data back out */
4630 if (data_len > 0) {
4631 memcpy(smb_buf(req->outbuf),smb_buf(req->inbuf),data_len);
4634 if (smb_reverb > 100) {
4635 DEBUG(0,("large reverb (%d)?? Setting to 100\n",smb_reverb));
4636 smb_reverb = 100;
4639 for (seq_num =1 ; seq_num <= smb_reverb ; seq_num++) {
4640 SSVAL(req->outbuf,smb_vwv0,seq_num);
4642 show_msg((char *)req->outbuf);
4643 if (!srv_send_smb(smbd_server_fd(),
4644 (char *)req->outbuf,
4645 IS_CONN_ENCRYPTED(conn)||req->encrypted))
4646 exit_server_cleanly("reply_echo: srv_send_smb failed.");
4649 DEBUG(3,("echo %d times\n", smb_reverb));
4651 TALLOC_FREE(req->outbuf);
4653 END_PROFILE(SMBecho);
4654 return;
4657 /****************************************************************************
4658 Reply to a printopen.
4659 ****************************************************************************/
4661 void reply_printopen(struct smb_request *req)
4663 connection_struct *conn = req->conn;
4664 files_struct *fsp;
4665 SMB_STRUCT_STAT sbuf;
4666 NTSTATUS status;
4668 START_PROFILE(SMBsplopen);
4670 if (req->wct < 2) {
4671 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4672 END_PROFILE(SMBsplopen);
4673 return;
4676 if (!CAN_PRINT(conn)) {
4677 reply_doserror(req, ERRDOS, ERRnoaccess);
4678 END_PROFILE(SMBsplopen);
4679 return;
4682 status = file_new(conn, &fsp);
4683 if(!NT_STATUS_IS_OK(status)) {
4684 reply_nterror(req, status);
4685 END_PROFILE(SMBsplopen);
4686 return;
4689 /* Open for exclusive use, write only. */
4690 status = print_fsp_open(conn, NULL, req->vuid, fsp, &sbuf);
4692 if (!NT_STATUS_IS_OK(status)) {
4693 file_free(fsp);
4694 reply_nterror(req, status);
4695 END_PROFILE(SMBsplopen);
4696 return;
4699 reply_outbuf(req, 1, 0);
4700 SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
4702 DEBUG(3,("openprint fd=%d fnum=%d\n",
4703 fsp->fh->fd, fsp->fnum));
4705 END_PROFILE(SMBsplopen);
4706 return;
4709 /****************************************************************************
4710 Reply to a printclose.
4711 ****************************************************************************/
4713 void reply_printclose(struct smb_request *req)
4715 connection_struct *conn = req->conn;
4716 files_struct *fsp;
4717 NTSTATUS status;
4719 START_PROFILE(SMBsplclose);
4721 if (req->wct < 1) {
4722 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4723 END_PROFILE(SMBsplclose);
4724 return;
4727 fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
4729 if (!check_fsp(conn, req, fsp)) {
4730 END_PROFILE(SMBsplclose);
4731 return;
4734 if (!CAN_PRINT(conn)) {
4735 reply_nterror(req, NT_STATUS_DOS(ERRSRV, ERRerror));
4736 END_PROFILE(SMBsplclose);
4737 return;
4740 DEBUG(3,("printclose fd=%d fnum=%d\n",
4741 fsp->fh->fd,fsp->fnum));
4743 status = close_file(fsp,NORMAL_CLOSE);
4745 if(!NT_STATUS_IS_OK(status)) {
4746 reply_nterror(req, status);
4747 END_PROFILE(SMBsplclose);
4748 return;
4751 reply_outbuf(req, 0, 0);
4753 END_PROFILE(SMBsplclose);
4754 return;
4757 /****************************************************************************
4758 Reply to a printqueue.
4759 ****************************************************************************/
4761 void reply_printqueue(struct smb_request *req)
4763 connection_struct *conn = req->conn;
4764 int max_count;
4765 int start_index;
4767 START_PROFILE(SMBsplretq);
4769 if (req->wct < 2) {
4770 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4771 END_PROFILE(SMBsplretq);
4772 return;
4775 max_count = SVAL(req->inbuf,smb_vwv0);
4776 start_index = SVAL(req->inbuf,smb_vwv1);
4778 /* we used to allow the client to get the cnum wrong, but that
4779 is really quite gross and only worked when there was only
4780 one printer - I think we should now only accept it if they
4781 get it right (tridge) */
4782 if (!CAN_PRINT(conn)) {
4783 reply_doserror(req, ERRDOS, ERRnoaccess);
4784 END_PROFILE(SMBsplretq);
4785 return;
4788 reply_outbuf(req, 2, 3);
4789 SSVAL(req->outbuf,smb_vwv0,0);
4790 SSVAL(req->outbuf,smb_vwv1,0);
4791 SCVAL(smb_buf(req->outbuf),0,1);
4792 SSVAL(smb_buf(req->outbuf),1,0);
4794 DEBUG(3,("printqueue start_index=%d max_count=%d\n",
4795 start_index, max_count));
4798 print_queue_struct *queue = NULL;
4799 print_status_struct status;
4800 int count = print_queue_status(SNUM(conn), &queue, &status);
4801 int num_to_get = ABS(max_count);
4802 int first = (max_count>0?start_index:start_index+max_count+1);
4803 int i;
4805 if (first >= count)
4806 num_to_get = 0;
4807 else
4808 num_to_get = MIN(num_to_get,count-first);
4811 for (i=first;i<first+num_to_get;i++) {
4812 char blob[28];
4813 char *p = blob;
4815 srv_put_dos_date2(p,0,queue[i].time);
4816 SCVAL(p,4,(queue[i].status==LPQ_PRINTING?2:3));
4817 SSVAL(p,5, queue[i].job);
4818 SIVAL(p,7,queue[i].size);
4819 SCVAL(p,11,0);
4820 srvstr_push(blob, req->flags2, p+12,
4821 queue[i].fs_user, 16, STR_ASCII);
4823 if (message_push_blob(
4824 &req->outbuf,
4825 data_blob_const(
4826 blob, sizeof(blob))) == -1) {
4827 reply_nterror(req, NT_STATUS_NO_MEMORY);
4828 END_PROFILE(SMBsplretq);
4829 return;
4833 if (count > 0) {
4834 SSVAL(req->outbuf,smb_vwv0,count);
4835 SSVAL(req->outbuf,smb_vwv1,
4836 (max_count>0?first+count:first-1));
4837 SCVAL(smb_buf(req->outbuf),0,1);
4838 SSVAL(smb_buf(req->outbuf),1,28*count);
4841 SAFE_FREE(queue);
4843 DEBUG(3,("%d entries returned in queue\n",count));
4846 END_PROFILE(SMBsplretq);
4847 return;
4850 /****************************************************************************
4851 Reply to a printwrite.
4852 ****************************************************************************/
4854 void reply_printwrite(struct smb_request *req)
4856 connection_struct *conn = req->conn;
4857 int numtowrite;
4858 char *data;
4859 files_struct *fsp;
4861 START_PROFILE(SMBsplwr);
4863 if (req->wct < 1) {
4864 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4865 END_PROFILE(SMBsplwr);
4866 return;
4869 fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
4871 if (!check_fsp(conn, req, fsp)) {
4872 END_PROFILE(SMBsplwr);
4873 return;
4876 if (!CAN_PRINT(conn)) {
4877 reply_doserror(req, ERRDOS, ERRnoaccess);
4878 END_PROFILE(SMBsplwr);
4879 return;
4882 if (!CHECK_WRITE(fsp)) {
4883 reply_doserror(req, ERRDOS, ERRbadaccess);
4884 END_PROFILE(SMBsplwr);
4885 return;
4888 numtowrite = SVAL(smb_buf(req->inbuf),1);
4890 if (smb_buflen(req->inbuf) < numtowrite + 3) {
4891 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4892 END_PROFILE(SMBsplwr);
4893 return;
4896 data = smb_buf(req->inbuf) + 3;
4898 if (write_file(req,fsp,data,-1,numtowrite) != numtowrite) {
4899 reply_unixerror(req, ERRHRD, ERRdiskfull);
4900 END_PROFILE(SMBsplwr);
4901 return;
4904 DEBUG( 3, ( "printwrite fnum=%d num=%d\n", fsp->fnum, numtowrite ) );
4906 END_PROFILE(SMBsplwr);
4907 return;
4910 /****************************************************************************
4911 Reply to a mkdir.
4912 ****************************************************************************/
4914 void reply_mkdir(struct smb_request *req)
4916 connection_struct *conn = req->conn;
4917 char *directory = NULL;
4918 NTSTATUS status;
4919 SMB_STRUCT_STAT sbuf;
4920 TALLOC_CTX *ctx = talloc_tos();
4922 START_PROFILE(SMBmkdir);
4924 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &directory,
4925 smb_buf(req->inbuf) + 1, 0,
4926 STR_TERMINATE, &status);
4927 if (!NT_STATUS_IS_OK(status)) {
4928 reply_nterror(req, status);
4929 END_PROFILE(SMBmkdir);
4930 return;
4933 status = resolve_dfspath(ctx, conn,
4934 req->flags2 & FLAGS2_DFS_PATHNAMES,
4935 directory,
4936 &directory);
4937 if (!NT_STATUS_IS_OK(status)) {
4938 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
4939 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
4940 ERRSRV, ERRbadpath);
4941 END_PROFILE(SMBmkdir);
4942 return;
4944 reply_nterror(req, status);
4945 END_PROFILE(SMBmkdir);
4946 return;
4949 status = unix_convert(ctx, conn, directory, False, &directory, NULL, &sbuf);
4950 if (!NT_STATUS_IS_OK(status)) {
4951 reply_nterror(req, status);
4952 END_PROFILE(SMBmkdir);
4953 return;
4956 status = check_name(conn, directory);
4957 if (!NT_STATUS_IS_OK(status)) {
4958 reply_nterror(req, status);
4959 END_PROFILE(SMBmkdir);
4960 return;
4963 status = create_directory(conn, req, directory);
4965 DEBUG(5, ("create_directory returned %s\n", nt_errstr(status)));
4967 if (!NT_STATUS_IS_OK(status)) {
4969 if (!use_nt_status()
4970 && NT_STATUS_EQUAL(status,
4971 NT_STATUS_OBJECT_NAME_COLLISION)) {
4973 * Yes, in the DOS error code case we get a
4974 * ERRDOS:ERRnoaccess here. See BASE-SAMBA3ERROR
4975 * samba4 torture test.
4977 status = NT_STATUS_DOS(ERRDOS, ERRnoaccess);
4980 reply_nterror(req, status);
4981 END_PROFILE(SMBmkdir);
4982 return;
4985 reply_outbuf(req, 0, 0);
4987 DEBUG( 3, ( "mkdir %s\n", directory ) );
4989 END_PROFILE(SMBmkdir);
4990 return;
4993 /****************************************************************************
4994 Static function used by reply_rmdir to delete an entire directory
4995 tree recursively. Return True on ok, False on fail.
4996 ****************************************************************************/
4998 static bool recursive_rmdir(TALLOC_CTX *ctx,
4999 connection_struct *conn,
5000 char *directory)
5002 const char *dname = NULL;
5003 bool ret = True;
5004 long offset = 0;
5005 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn, directory,
5006 NULL, 0);
5008 if(dir_hnd == NULL)
5009 return False;
5011 while((dname = ReadDirName(dir_hnd, &offset))) {
5012 char *fullname = NULL;
5013 SMB_STRUCT_STAT st;
5015 if (ISDOT(dname) || ISDOTDOT(dname)) {
5016 continue;
5019 if (!is_visible_file(conn, directory, dname, &st, False)) {
5020 continue;
5023 /* Construct the full name. */
5024 fullname = talloc_asprintf(ctx,
5025 "%s/%s",
5026 directory,
5027 dname);
5028 if (!fullname) {
5029 errno = ENOMEM;
5030 ret = False;
5031 break;
5034 if(SMB_VFS_LSTAT(conn,fullname, &st) != 0) {
5035 ret = False;
5036 break;
5039 if(st.st_mode & S_IFDIR) {
5040 if(!recursive_rmdir(ctx, conn, fullname)) {
5041 ret = False;
5042 break;
5044 if(SMB_VFS_RMDIR(conn,fullname) != 0) {
5045 ret = False;
5046 break;
5048 } else if(SMB_VFS_UNLINK(conn,fullname) != 0) {
5049 ret = False;
5050 break;
5052 TALLOC_FREE(fullname);
5054 TALLOC_FREE(dir_hnd);
5055 return ret;
5058 /****************************************************************************
5059 The internals of the rmdir code - called elsewhere.
5060 ****************************************************************************/
5062 NTSTATUS rmdir_internals(TALLOC_CTX *ctx,
5063 connection_struct *conn,
5064 const char *directory)
5066 int ret;
5067 SMB_STRUCT_STAT st;
5069 /* Might be a symlink. */
5070 if(SMB_VFS_LSTAT(conn, directory, &st) != 0) {
5071 return map_nt_error_from_unix(errno);
5074 if (S_ISLNK(st.st_mode)) {
5075 /* Is what it points to a directory ? */
5076 if(SMB_VFS_STAT(conn, directory, &st) != 0) {
5077 return map_nt_error_from_unix(errno);
5079 if (!(S_ISDIR(st.st_mode))) {
5080 return NT_STATUS_NOT_A_DIRECTORY;
5082 ret = SMB_VFS_UNLINK(conn,directory);
5083 } else {
5084 ret = SMB_VFS_RMDIR(conn,directory);
5086 if (ret == 0) {
5087 notify_fname(conn, NOTIFY_ACTION_REMOVED,
5088 FILE_NOTIFY_CHANGE_DIR_NAME,
5089 directory);
5090 return NT_STATUS_OK;
5093 if(((errno == ENOTEMPTY)||(errno == EEXIST)) && lp_veto_files(SNUM(conn))) {
5095 * Check to see if the only thing in this directory are
5096 * vetoed files/directories. If so then delete them and
5097 * retry. If we fail to delete any of them (and we *don't*
5098 * do a recursive delete) then fail the rmdir.
5100 const char *dname;
5101 long dirpos = 0;
5102 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
5103 directory, NULL, 0);
5105 if(dir_hnd == NULL) {
5106 errno = ENOTEMPTY;
5107 goto err;
5110 while ((dname = ReadDirName(dir_hnd,&dirpos))) {
5111 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
5112 continue;
5113 if (!is_visible_file(conn, directory, dname, &st, False))
5114 continue;
5115 if(!IS_VETO_PATH(conn, dname)) {
5116 TALLOC_FREE(dir_hnd);
5117 errno = ENOTEMPTY;
5118 goto err;
5122 /* We only have veto files/directories.
5123 * Are we allowed to delete them ? */
5125 if(!lp_recursive_veto_delete(SNUM(conn))) {
5126 TALLOC_FREE(dir_hnd);
5127 errno = ENOTEMPTY;
5128 goto err;
5131 /* Do a recursive delete. */
5132 RewindDir(dir_hnd,&dirpos);
5133 while ((dname = ReadDirName(dir_hnd,&dirpos))) {
5134 char *fullname = NULL;
5136 if (ISDOT(dname) || ISDOTDOT(dname)) {
5137 continue;
5139 if (!is_visible_file(conn, directory, dname, &st, False)) {
5140 continue;
5143 fullname = talloc_asprintf(ctx,
5144 "%s/%s",
5145 directory,
5146 dname);
5148 if(!fullname) {
5149 errno = ENOMEM;
5150 break;
5153 if(SMB_VFS_LSTAT(conn,fullname, &st) != 0) {
5154 break;
5156 if(st.st_mode & S_IFDIR) {
5157 if(!recursive_rmdir(ctx, conn, fullname)) {
5158 break;
5160 if(SMB_VFS_RMDIR(conn,fullname) != 0) {
5161 break;
5163 } else if(SMB_VFS_UNLINK(conn,fullname) != 0) {
5164 break;
5166 TALLOC_FREE(fullname);
5168 TALLOC_FREE(dir_hnd);
5169 /* Retry the rmdir */
5170 ret = SMB_VFS_RMDIR(conn,directory);
5173 err:
5175 if (ret != 0) {
5176 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
5177 "%s\n", directory,strerror(errno)));
5178 return map_nt_error_from_unix(errno);
5181 notify_fname(conn, NOTIFY_ACTION_REMOVED,
5182 FILE_NOTIFY_CHANGE_DIR_NAME,
5183 directory);
5185 return NT_STATUS_OK;
5188 /****************************************************************************
5189 Reply to a rmdir.
5190 ****************************************************************************/
5192 void reply_rmdir(struct smb_request *req)
5194 connection_struct *conn = req->conn;
5195 char *directory = NULL;
5196 SMB_STRUCT_STAT sbuf;
5197 NTSTATUS status;
5198 TALLOC_CTX *ctx = talloc_tos();
5200 START_PROFILE(SMBrmdir);
5202 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &directory,
5203 smb_buf(req->inbuf) + 1, 0,
5204 STR_TERMINATE, &status);
5205 if (!NT_STATUS_IS_OK(status)) {
5206 reply_nterror(req, status);
5207 END_PROFILE(SMBrmdir);
5208 return;
5211 status = resolve_dfspath(ctx, conn,
5212 req->flags2 & FLAGS2_DFS_PATHNAMES,
5213 directory,
5214 &directory);
5215 if (!NT_STATUS_IS_OK(status)) {
5216 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
5217 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
5218 ERRSRV, ERRbadpath);
5219 END_PROFILE(SMBrmdir);
5220 return;
5222 reply_nterror(req, status);
5223 END_PROFILE(SMBrmdir);
5224 return;
5227 status = unix_convert(ctx, conn, directory, False, &directory,
5228 NULL, &sbuf);
5229 if (!NT_STATUS_IS_OK(status)) {
5230 reply_nterror(req, status);
5231 END_PROFILE(SMBrmdir);
5232 return;
5235 status = check_name(conn, directory);
5236 if (!NT_STATUS_IS_OK(status)) {
5237 reply_nterror(req, status);
5238 END_PROFILE(SMBrmdir);
5239 return;
5242 dptr_closepath(directory, req->smbpid);
5243 status = rmdir_internals(ctx, conn, directory);
5244 if (!NT_STATUS_IS_OK(status)) {
5245 reply_nterror(req, status);
5246 END_PROFILE(SMBrmdir);
5247 return;
5250 reply_outbuf(req, 0, 0);
5252 DEBUG( 3, ( "rmdir %s\n", directory ) );
5254 END_PROFILE(SMBrmdir);
5255 return;
5258 /*******************************************************************
5259 Resolve wildcards in a filename rename.
5260 ********************************************************************/
5262 static bool resolve_wildcards(TALLOC_CTX *ctx,
5263 const char *name1,
5264 const char *name2,
5265 char **pp_newname)
5267 char *name2_copy = NULL;
5268 char *root1 = NULL;
5269 char *root2 = NULL;
5270 char *ext1 = NULL;
5271 char *ext2 = NULL;
5272 char *p,*p2, *pname1, *pname2;
5274 name2_copy = talloc_strdup(ctx, name2);
5275 if (!name2_copy) {
5276 return False;
5279 pname1 = strrchr_m(name1,'/');
5280 pname2 = strrchr_m(name2_copy,'/');
5282 if (!pname1 || !pname2) {
5283 return False;
5286 /* Truncate the copy of name2 at the last '/' */
5287 *pname2 = '\0';
5289 /* Now go past the '/' */
5290 pname1++;
5291 pname2++;
5293 root1 = talloc_strdup(ctx, pname1);
5294 root2 = talloc_strdup(ctx, pname2);
5296 if (!root1 || !root2) {
5297 return False;
5300 p = strrchr_m(root1,'.');
5301 if (p) {
5302 *p = 0;
5303 ext1 = talloc_strdup(ctx, p+1);
5304 } else {
5305 ext1 = talloc_strdup(ctx, "");
5307 p = strrchr_m(root2,'.');
5308 if (p) {
5309 *p = 0;
5310 ext2 = talloc_strdup(ctx, p+1);
5311 } else {
5312 ext2 = talloc_strdup(ctx, "");
5315 if (!ext1 || !ext2) {
5316 return False;
5319 p = root1;
5320 p2 = root2;
5321 while (*p2) {
5322 if (*p2 == '?') {
5323 /* Hmmm. Should this be mb-aware ? */
5324 *p2 = *p;
5325 p2++;
5326 } else if (*p2 == '*') {
5327 *p2 = '\0';
5328 root2 = talloc_asprintf(ctx, "%s%s",
5329 root2,
5331 if (!root2) {
5332 return False;
5334 break;
5335 } else {
5336 p2++;
5338 if (*p) {
5339 p++;
5343 p = ext1;
5344 p2 = ext2;
5345 while (*p2) {
5346 if (*p2 == '?') {
5347 /* Hmmm. Should this be mb-aware ? */
5348 *p2 = *p;
5349 p2++;
5350 } else if (*p2 == '*') {
5351 *p2 = '\0';
5352 ext2 = talloc_asprintf(ctx, "%s%s",
5353 ext2,
5355 if (!ext2) {
5356 return False;
5358 break;
5359 } else {
5360 p2++;
5362 if (*p) {
5363 p++;
5367 if (*ext2) {
5368 *pp_newname = talloc_asprintf(ctx, "%s/%s.%s",
5369 name2_copy,
5370 root2,
5371 ext2);
5372 } else {
5373 *pp_newname = talloc_asprintf(ctx, "%s/%s",
5374 name2_copy,
5375 root2);
5378 if (!*pp_newname) {
5379 return False;
5382 return True;
5385 /****************************************************************************
5386 Ensure open files have their names updated. Updated to notify other smbd's
5387 asynchronously.
5388 ****************************************************************************/
5390 static void rename_open_files(connection_struct *conn,
5391 struct share_mode_lock *lck,
5392 const char *newname)
5394 files_struct *fsp;
5395 bool did_rename = False;
5397 for(fsp = file_find_di_first(lck->id); fsp;
5398 fsp = file_find_di_next(fsp)) {
5399 /* fsp_name is a relative path under the fsp. To change this for other
5400 sharepaths we need to manipulate relative paths. */
5401 /* TODO - create the absolute path and manipulate the newname
5402 relative to the sharepath. */
5403 if (!strequal(fsp->conn->connectpath, conn->connectpath)) {
5404 continue;
5406 DEBUG(10,("rename_open_files: renaming file fnum %d (file_id %s) from %s -> %s\n",
5407 fsp->fnum, file_id_string_tos(&fsp->file_id),
5408 fsp->fsp_name, newname ));
5409 string_set(&fsp->fsp_name, newname);
5410 did_rename = True;
5413 if (!did_rename) {
5414 DEBUG(10,("rename_open_files: no open files on file_id %s for %s\n",
5415 file_id_string_tos(&lck->id), newname ));
5418 /* Send messages to all smbd's (not ourself) that the name has changed. */
5419 rename_share_filename(smbd_messaging_context(), lck, conn->connectpath,
5420 newname);
5423 /****************************************************************************
5424 We need to check if the source path is a parent directory of the destination
5425 (ie. a rename of /foo/bar/baz -> /foo/bar/baz/bibble/bobble. If so we must
5426 refuse the rename with a sharing violation. Under UNIX the above call can
5427 *succeed* if /foo/bar/baz is a symlink to another area in the share. We
5428 probably need to check that the client is a Windows one before disallowing
5429 this as a UNIX client (one with UNIX extensions) can know the source is a
5430 symlink and make this decision intelligently. Found by an excellent bug
5431 report from <AndyLiebman@aol.com>.
5432 ****************************************************************************/
5434 static bool rename_path_prefix_equal(const char *src, const char *dest)
5436 const char *psrc = src;
5437 const char *pdst = dest;
5438 size_t slen;
5440 if (psrc[0] == '.' && psrc[1] == '/') {
5441 psrc += 2;
5443 if (pdst[0] == '.' && pdst[1] == '/') {
5444 pdst += 2;
5446 if ((slen = strlen(psrc)) > strlen(pdst)) {
5447 return False;
5449 return ((memcmp(psrc, pdst, slen) == 0) && pdst[slen] == '/');
5453 * Do the notify calls from a rename
5456 static void notify_rename(connection_struct *conn, bool is_dir,
5457 const char *oldpath, const char *newpath)
5459 char *olddir, *newdir;
5460 const char *oldname, *newname;
5461 uint32 mask;
5463 mask = is_dir ? FILE_NOTIFY_CHANGE_DIR_NAME
5464 : FILE_NOTIFY_CHANGE_FILE_NAME;
5466 if (!parent_dirname_talloc(NULL, oldpath, &olddir, &oldname)
5467 || !parent_dirname_talloc(NULL, newpath, &newdir, &newname)) {
5468 TALLOC_FREE(olddir);
5469 return;
5472 if (strcmp(olddir, newdir) == 0) {
5473 notify_fname(conn, NOTIFY_ACTION_OLD_NAME, mask, oldpath);
5474 notify_fname(conn, NOTIFY_ACTION_NEW_NAME, mask, newpath);
5476 else {
5477 notify_fname(conn, NOTIFY_ACTION_REMOVED, mask, oldpath);
5478 notify_fname(conn, NOTIFY_ACTION_ADDED, mask, newpath);
5480 TALLOC_FREE(olddir);
5481 TALLOC_FREE(newdir);
5483 /* this is a strange one. w2k3 gives an additional event for
5484 CHANGE_ATTRIBUTES and CHANGE_CREATION on the new file when renaming
5485 files, but not directories */
5486 if (!is_dir) {
5487 notify_fname(conn, NOTIFY_ACTION_MODIFIED,
5488 FILE_NOTIFY_CHANGE_ATTRIBUTES
5489 |FILE_NOTIFY_CHANGE_CREATION,
5490 newpath);
5494 /****************************************************************************
5495 Rename an open file - given an fsp.
5496 ****************************************************************************/
5498 NTSTATUS rename_internals_fsp(connection_struct *conn,
5499 files_struct *fsp,
5500 char *newname,
5501 const char *newname_last_component,
5502 uint32 attrs,
5503 bool replace_if_exists)
5505 TALLOC_CTX *ctx = talloc_tos();
5506 SMB_STRUCT_STAT sbuf, sbuf1;
5507 NTSTATUS status = NT_STATUS_OK;
5508 struct share_mode_lock *lck = NULL;
5509 bool dst_exists;
5511 ZERO_STRUCT(sbuf);
5513 status = check_name(conn, newname);
5514 if (!NT_STATUS_IS_OK(status)) {
5515 return status;
5518 /* Ensure newname contains a '/' */
5519 if(strrchr_m(newname,'/') == 0) {
5520 newname = talloc_asprintf(ctx,
5521 "./%s",
5522 newname);
5523 if (!newname) {
5524 return NT_STATUS_NO_MEMORY;
5529 * Check for special case with case preserving and not
5530 * case sensitive. If the old last component differs from the original
5531 * last component only by case, then we should allow
5532 * the rename (user is trying to change the case of the
5533 * filename).
5536 if((conn->case_sensitive == False) && (conn->case_preserve == True) &&
5537 strequal(newname, fsp->fsp_name)) {
5538 char *p;
5539 char *newname_modified_last_component = NULL;
5542 * Get the last component of the modified name.
5543 * Note that we guarantee that newname contains a '/'
5544 * character above.
5546 p = strrchr_m(newname,'/');
5547 newname_modified_last_component = talloc_strdup(ctx,
5548 p+1);
5549 if (!newname_modified_last_component) {
5550 return NT_STATUS_NO_MEMORY;
5553 if(strcsequal(newname_modified_last_component,
5554 newname_last_component) == False) {
5556 * Replace the modified last component with
5557 * the original.
5559 *p = '\0'; /* Truncate at the '/' */
5560 newname = talloc_asprintf(ctx,
5561 "%s/%s",
5562 newname,
5563 newname_last_component);
5568 * If the src and dest names are identical - including case,
5569 * don't do the rename, just return success.
5572 if (strcsequal(fsp->fsp_name, newname)) {
5573 DEBUG(3,("rename_internals_fsp: identical names in rename %s - returning success\n",
5574 newname));
5575 return NT_STATUS_OK;
5579 * Have vfs_object_exist also fill sbuf1
5581 dst_exists = vfs_object_exist(conn, newname, &sbuf1);
5583 if(!replace_if_exists && dst_exists) {
5584 DEBUG(3,("rename_internals_fsp: dest exists doing rename %s -> %s\n",
5585 fsp->fsp_name,newname));
5586 return NT_STATUS_OBJECT_NAME_COLLISION;
5589 if(replace_if_exists && dst_exists) {
5590 /* Ensure both or neither are stream names. */
5591 if (is_ntfs_stream_name(fsp->fsp_name) !=
5592 is_ntfs_stream_name(newname)) {
5593 return NT_STATUS_INVALID_PARAMETER;
5597 if (dst_exists) {
5598 struct file_id fileid = vfs_file_id_from_sbuf(conn, &sbuf1);
5599 files_struct *dst_fsp = file_find_di_first(fileid);
5600 if (dst_fsp) {
5601 DEBUG(3, ("rename_internals_fsp: Target file open\n"));
5602 return NT_STATUS_ACCESS_DENIED;
5606 /* Ensure we have a valid stat struct for the source. */
5607 if (fsp->fh->fd != -1) {
5608 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
5609 return map_nt_error_from_unix(errno);
5611 } else {
5612 if (SMB_VFS_STAT(conn,fsp->fsp_name,&sbuf) == -1) {
5613 return map_nt_error_from_unix(errno);
5617 status = can_rename(conn, fsp, attrs, &sbuf);
5619 if (!NT_STATUS_IS_OK(status)) {
5620 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
5621 nt_errstr(status), fsp->fsp_name,newname));
5622 if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION))
5623 status = NT_STATUS_ACCESS_DENIED;
5624 return status;
5627 if (rename_path_prefix_equal(fsp->fsp_name, newname)) {
5628 return NT_STATUS_ACCESS_DENIED;
5631 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
5632 NULL);
5635 * We have the file open ourselves, so not being able to get the
5636 * corresponding share mode lock is a fatal error.
5639 SMB_ASSERT(lck != NULL);
5641 if(SMB_VFS_RENAME(conn,fsp->fsp_name, newname) == 0) {
5642 uint32 create_options = fsp->fh->private_options;
5644 DEBUG(3,("rename_internals_fsp: succeeded doing rename on %s -> %s\n",
5645 fsp->fsp_name,newname));
5647 notify_rename(conn, fsp->is_directory, fsp->fsp_name, newname);
5649 rename_open_files(conn, lck, newname);
5652 * A rename acts as a new file create w.r.t. allowing an initial delete
5653 * on close, probably because in Windows there is a new handle to the
5654 * new file. If initial delete on close was requested but not
5655 * originally set, we need to set it here. This is probably not 100% correct,
5656 * but will work for the CIFSFS client which in non-posix mode
5657 * depends on these semantics. JRA.
5660 if (create_options & FILE_DELETE_ON_CLOSE) {
5661 status = can_set_delete_on_close(fsp, True, 0);
5663 if (NT_STATUS_IS_OK(status)) {
5664 /* Note that here we set the *inital* delete on close flag,
5665 * not the regular one. The magic gets handled in close. */
5666 fsp->initial_delete_on_close = True;
5669 TALLOC_FREE(lck);
5670 return NT_STATUS_OK;
5673 TALLOC_FREE(lck);
5675 if (errno == ENOTDIR || errno == EISDIR) {
5676 status = NT_STATUS_OBJECT_NAME_COLLISION;
5677 } else {
5678 status = map_nt_error_from_unix(errno);
5681 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
5682 nt_errstr(status), fsp->fsp_name,newname));
5684 return status;
5687 /****************************************************************************
5688 The guts of the rename command, split out so it may be called by the NT SMB
5689 code.
5690 ****************************************************************************/
5692 NTSTATUS rename_internals(TALLOC_CTX *ctx,
5693 connection_struct *conn,
5694 struct smb_request *req,
5695 const char *name_in,
5696 const char *newname_in,
5697 uint32 attrs,
5698 bool replace_if_exists,
5699 bool src_has_wild,
5700 bool dest_has_wild,
5701 uint32_t access_mask)
5703 char *directory = NULL;
5704 char *mask = NULL;
5705 char *last_component_src = NULL;
5706 char *last_component_dest = NULL;
5707 char *name = NULL;
5708 char *newname = NULL;
5709 char *p;
5710 int count=0;
5711 NTSTATUS status = NT_STATUS_OK;
5712 SMB_STRUCT_STAT sbuf1, sbuf2;
5713 struct smb_Dir *dir_hnd = NULL;
5714 const char *dname;
5715 long offset = 0;
5717 ZERO_STRUCT(sbuf1);
5718 ZERO_STRUCT(sbuf2);
5720 status = unix_convert(ctx, conn, name_in, src_has_wild, &name,
5721 &last_component_src, &sbuf1);
5722 if (!NT_STATUS_IS_OK(status)) {
5723 return status;
5726 status = unix_convert(ctx, conn, newname_in, dest_has_wild, &newname,
5727 &last_component_dest, &sbuf2);
5728 if (!NT_STATUS_IS_OK(status)) {
5729 return status;
5733 * Split the old name into directory and last component
5734 * strings. Note that unix_convert may have stripped off a
5735 * leading ./ from both name and newname if the rename is
5736 * at the root of the share. We need to make sure either both
5737 * name and newname contain a / character or neither of them do
5738 * as this is checked in resolve_wildcards().
5741 p = strrchr_m(name,'/');
5742 if (!p) {
5743 directory = talloc_strdup(ctx, ".");
5744 if (!directory) {
5745 return NT_STATUS_NO_MEMORY;
5747 mask = name;
5748 } else {
5749 *p = 0;
5750 directory = talloc_strdup(ctx, name);
5751 if (!directory) {
5752 return NT_STATUS_NO_MEMORY;
5754 mask = p+1;
5755 *p = '/'; /* Replace needed for exceptional test below. */
5759 * We should only check the mangled cache
5760 * here if unix_convert failed. This means
5761 * that the path in 'mask' doesn't exist
5762 * on the file system and so we need to look
5763 * for a possible mangle. This patch from
5764 * Tine Smukavec <valentin.smukavec@hermes.si>.
5767 if (!VALID_STAT(sbuf1) && mangle_is_mangled(mask, conn->params)) {
5768 char *new_mask = NULL;
5769 mangle_lookup_name_from_8_3(ctx,
5770 mask,
5771 &new_mask,
5772 conn->params );
5773 if (new_mask) {
5774 mask = new_mask;
5778 if (!src_has_wild) {
5779 files_struct *fsp;
5782 * No wildcards - just process the one file.
5784 bool is_short_name = mangle_is_8_3(name, True, conn->params);
5786 /* Add a terminating '/' to the directory name. */
5787 directory = talloc_asprintf_append(directory,
5788 "/%s",
5789 mask);
5790 if (!directory) {
5791 return NT_STATUS_NO_MEMORY;
5794 /* Ensure newname contains a '/' also */
5795 if(strrchr_m(newname,'/') == 0) {
5796 newname = talloc_asprintf(ctx,
5797 "./%s",
5798 newname);
5799 if (!newname) {
5800 return NT_STATUS_NO_MEMORY;
5804 DEBUG(3, ("rename_internals: case_sensitive = %d, "
5805 "case_preserve = %d, short case preserve = %d, "
5806 "directory = %s, newname = %s, "
5807 "last_component_dest = %s, is_8_3 = %d\n",
5808 conn->case_sensitive, conn->case_preserve,
5809 conn->short_case_preserve, directory,
5810 newname, last_component_dest, is_short_name));
5812 /* The dest name still may have wildcards. */
5813 if (dest_has_wild) {
5814 char *mod_newname = NULL;
5815 if (!resolve_wildcards(ctx,
5816 directory,newname,&mod_newname)) {
5817 DEBUG(6, ("rename_internals: resolve_wildcards "
5818 "%s %s failed\n",
5819 directory,
5820 newname));
5821 return NT_STATUS_NO_MEMORY;
5823 newname = mod_newname;
5826 ZERO_STRUCT(sbuf1);
5827 SMB_VFS_STAT(conn, directory, &sbuf1);
5829 status = S_ISDIR(sbuf1.st_mode) ?
5830 open_directory(conn, req, directory, &sbuf1,
5831 access_mask,
5832 FILE_SHARE_READ|FILE_SHARE_WRITE,
5833 FILE_OPEN, 0, 0, NULL,
5834 &fsp)
5835 : open_file_ntcreate(conn, req, directory, &sbuf1,
5836 access_mask,
5837 FILE_SHARE_READ|FILE_SHARE_WRITE,
5838 FILE_OPEN, 0, 0, 0, NULL,
5839 &fsp);
5841 if (!NT_STATUS_IS_OK(status)) {
5842 DEBUG(3, ("Could not open rename source %s: %s\n",
5843 directory, nt_errstr(status)));
5844 return status;
5847 status = rename_internals_fsp(conn, fsp, newname,
5848 last_component_dest,
5849 attrs, replace_if_exists);
5851 close_file(fsp, NORMAL_CLOSE);
5853 DEBUG(3, ("rename_internals: Error %s rename %s -> %s\n",
5854 nt_errstr(status), directory,newname));
5856 return status;
5860 * Wildcards - process each file that matches.
5862 if (strequal(mask,"????????.???")) {
5863 mask[0] = '*';
5864 mask[1] = '\0';
5867 status = check_name(conn, directory);
5868 if (!NT_STATUS_IS_OK(status)) {
5869 return status;
5872 dir_hnd = OpenDir(talloc_tos(), conn, directory, mask, attrs);
5873 if (dir_hnd == NULL) {
5874 return map_nt_error_from_unix(errno);
5877 status = NT_STATUS_NO_SUCH_FILE;
5879 * Was status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5880 * - gentest fix. JRA
5883 while ((dname = ReadDirName(dir_hnd, &offset))) {
5884 files_struct *fsp = NULL;
5885 char *fname = NULL;
5886 char *destname = NULL;
5887 bool sysdir_entry = False;
5889 /* Quick check for "." and ".." */
5890 if (ISDOT(dname) || ISDOTDOT(dname)) {
5891 if (attrs & aDIR) {
5892 sysdir_entry = True;
5893 } else {
5894 continue;
5898 if (!is_visible_file(conn, directory, dname, &sbuf1, False)) {
5899 continue;
5902 if(!mask_match(dname, mask, conn->case_sensitive)) {
5903 continue;
5906 if (sysdir_entry) {
5907 status = NT_STATUS_OBJECT_NAME_INVALID;
5908 break;
5911 fname = talloc_asprintf(ctx,
5912 "%s/%s",
5913 directory,
5914 dname);
5915 if (!fname) {
5916 return NT_STATUS_NO_MEMORY;
5919 if (!resolve_wildcards(ctx,
5920 fname,newname,&destname)) {
5921 DEBUG(6, ("resolve_wildcards %s %s failed\n",
5922 fname, destname));
5923 TALLOC_FREE(fname);
5924 continue;
5926 if (!destname) {
5927 return NT_STATUS_NO_MEMORY;
5930 ZERO_STRUCT(sbuf1);
5931 SMB_VFS_STAT(conn, fname, &sbuf1);
5933 status = S_ISDIR(sbuf1.st_mode) ?
5934 open_directory(conn, req, fname, &sbuf1,
5935 access_mask,
5936 FILE_SHARE_READ|FILE_SHARE_WRITE,
5937 FILE_OPEN, 0, 0, NULL,
5938 &fsp)
5939 : open_file_ntcreate(conn, req, fname, &sbuf1,
5940 access_mask,
5941 FILE_SHARE_READ|FILE_SHARE_WRITE,
5942 FILE_OPEN, 0, 0, 0, NULL,
5943 &fsp);
5945 if (!NT_STATUS_IS_OK(status)) {
5946 DEBUG(3,("rename_internals: open_file_ntcreate "
5947 "returned %s rename %s -> %s\n",
5948 nt_errstr(status), directory, newname));
5949 break;
5952 status = rename_internals_fsp(conn, fsp, destname, dname,
5953 attrs, replace_if_exists);
5955 close_file(fsp, NORMAL_CLOSE);
5957 if (!NT_STATUS_IS_OK(status)) {
5958 DEBUG(3, ("rename_internals_fsp returned %s for "
5959 "rename %s -> %s\n", nt_errstr(status),
5960 directory, newname));
5961 break;
5964 count++;
5966 DEBUG(3,("rename_internals: doing rename on %s -> "
5967 "%s\n",fname,destname));
5969 TALLOC_FREE(fname);
5970 TALLOC_FREE(destname);
5972 TALLOC_FREE(dir_hnd);
5974 if (count == 0 && NT_STATUS_IS_OK(status) && errno != 0) {
5975 status = map_nt_error_from_unix(errno);
5978 return status;
5981 /****************************************************************************
5982 Reply to a mv.
5983 ****************************************************************************/
5985 void reply_mv(struct smb_request *req)
5987 connection_struct *conn = req->conn;
5988 char *name = NULL;
5989 char *newname = NULL;
5990 char *p;
5991 uint32 attrs;
5992 NTSTATUS status;
5993 bool src_has_wcard = False;
5994 bool dest_has_wcard = False;
5995 TALLOC_CTX *ctx = talloc_tos();
5997 START_PROFILE(SMBmv);
5999 if (req->wct < 1) {
6000 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
6001 END_PROFILE(SMBmv);
6002 return;
6005 attrs = SVAL(req->inbuf,smb_vwv0);
6007 p = smb_buf(req->inbuf) + 1;
6008 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &name, p,
6009 0, STR_TERMINATE, &status,
6010 &src_has_wcard);
6011 if (!NT_STATUS_IS_OK(status)) {
6012 reply_nterror(req, status);
6013 END_PROFILE(SMBmv);
6014 return;
6016 p++;
6017 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
6018 0, STR_TERMINATE, &status,
6019 &dest_has_wcard);
6020 if (!NT_STATUS_IS_OK(status)) {
6021 reply_nterror(req, status);
6022 END_PROFILE(SMBmv);
6023 return;
6026 status = resolve_dfspath_wcard(ctx, conn,
6027 req->flags2 & FLAGS2_DFS_PATHNAMES,
6028 name,
6029 &name,
6030 &src_has_wcard);
6031 if (!NT_STATUS_IS_OK(status)) {
6032 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
6033 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
6034 ERRSRV, ERRbadpath);
6035 END_PROFILE(SMBmv);
6036 return;
6038 reply_nterror(req, status);
6039 END_PROFILE(SMBmv);
6040 return;
6043 status = resolve_dfspath_wcard(ctx, conn,
6044 req->flags2 & FLAGS2_DFS_PATHNAMES,
6045 newname,
6046 &newname,
6047 &dest_has_wcard);
6048 if (!NT_STATUS_IS_OK(status)) {
6049 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
6050 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
6051 ERRSRV, ERRbadpath);
6052 END_PROFILE(SMBmv);
6053 return;
6055 reply_nterror(req, status);
6056 END_PROFILE(SMBmv);
6057 return;
6060 DEBUG(3,("reply_mv : %s -> %s\n",name,newname));
6062 status = rename_internals(ctx, conn, req, name, newname, attrs, False,
6063 src_has_wcard, dest_has_wcard, DELETE_ACCESS);
6064 if (!NT_STATUS_IS_OK(status)) {
6065 if (open_was_deferred(req->mid)) {
6066 /* We have re-scheduled this call. */
6067 END_PROFILE(SMBmv);
6068 return;
6070 reply_nterror(req, status);
6071 END_PROFILE(SMBmv);
6072 return;
6075 reply_outbuf(req, 0, 0);
6077 END_PROFILE(SMBmv);
6078 return;
6081 /*******************************************************************
6082 Copy a file as part of a reply_copy.
6083 ******************************************************************/
6086 * TODO: check error codes on all callers
6089 NTSTATUS copy_file(TALLOC_CTX *ctx,
6090 connection_struct *conn,
6091 const char *src,
6092 const char *dest1,
6093 int ofun,
6094 int count,
6095 bool target_is_directory)
6097 SMB_STRUCT_STAT src_sbuf, sbuf2;
6098 SMB_OFF_T ret=-1;
6099 files_struct *fsp1,*fsp2;
6100 char *dest = NULL;
6101 uint32 dosattrs;
6102 uint32 new_create_disposition;
6103 NTSTATUS status;
6105 dest = talloc_strdup(ctx, dest1);
6106 if (!dest) {
6107 return NT_STATUS_NO_MEMORY;
6109 if (target_is_directory) {
6110 const char *p = strrchr_m(src,'/');
6111 if (p) {
6112 p++;
6113 } else {
6114 p = src;
6116 dest = talloc_asprintf_append(dest,
6117 "/%s",
6119 if (!dest) {
6120 return NT_STATUS_NO_MEMORY;
6124 if (!vfs_file_exist(conn,src,&src_sbuf)) {
6125 TALLOC_FREE(dest);
6126 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
6129 if (!target_is_directory && count) {
6130 new_create_disposition = FILE_OPEN;
6131 } else {
6132 if (!map_open_params_to_ntcreate(dest1,0,ofun,
6133 NULL, NULL, &new_create_disposition, NULL)) {
6134 TALLOC_FREE(dest);
6135 return NT_STATUS_INVALID_PARAMETER;
6139 status = open_file_ntcreate(conn, NULL, src, &src_sbuf,
6140 FILE_GENERIC_READ,
6141 FILE_SHARE_READ|FILE_SHARE_WRITE,
6142 FILE_OPEN,
6144 FILE_ATTRIBUTE_NORMAL,
6145 INTERNAL_OPEN_ONLY,
6146 NULL, &fsp1);
6148 if (!NT_STATUS_IS_OK(status)) {
6149 TALLOC_FREE(dest);
6150 return status;
6153 dosattrs = dos_mode(conn, src, &src_sbuf);
6154 if (SMB_VFS_STAT(conn,dest,&sbuf2) == -1) {
6155 ZERO_STRUCTP(&sbuf2);
6158 status = open_file_ntcreate(conn, NULL, dest, &sbuf2,
6159 FILE_GENERIC_WRITE,
6160 FILE_SHARE_READ|FILE_SHARE_WRITE,
6161 new_create_disposition,
6163 dosattrs,
6164 INTERNAL_OPEN_ONLY,
6165 NULL, &fsp2);
6167 TALLOC_FREE(dest);
6169 if (!NT_STATUS_IS_OK(status)) {
6170 close_file(fsp1,ERROR_CLOSE);
6171 return status;
6174 if ((ofun&3) == 1) {
6175 if(SMB_VFS_LSEEK(fsp2,0,SEEK_END) == -1) {
6176 DEBUG(0,("copy_file: error - vfs lseek returned error %s\n", strerror(errno) ));
6178 * Stop the copy from occurring.
6180 ret = -1;
6181 src_sbuf.st_size = 0;
6185 if (src_sbuf.st_size) {
6186 ret = vfs_transfer_file(fsp1, fsp2, src_sbuf.st_size);
6189 close_file(fsp1,NORMAL_CLOSE);
6191 /* Ensure the modtime is set correctly on the destination file. */
6192 set_close_write_time(fsp2, get_mtimespec(&src_sbuf));
6195 * As we are opening fsp1 read-only we only expect
6196 * an error on close on fsp2 if we are out of space.
6197 * Thus we don't look at the error return from the
6198 * close of fsp1.
6200 status = close_file(fsp2,NORMAL_CLOSE);
6202 if (!NT_STATUS_IS_OK(status)) {
6203 return status;
6206 if (ret != (SMB_OFF_T)src_sbuf.st_size) {
6207 return NT_STATUS_DISK_FULL;
6210 return NT_STATUS_OK;
6213 /****************************************************************************
6214 Reply to a file copy.
6215 ****************************************************************************/
6217 void reply_copy(struct smb_request *req)
6219 connection_struct *conn = req->conn;
6220 char *name = NULL;
6221 char *newname = NULL;
6222 char *directory = NULL;
6223 char *mask = NULL;
6224 char *p;
6225 int count=0;
6226 int error = ERRnoaccess;
6227 int err = 0;
6228 int tid2;
6229 int ofun;
6230 int flags;
6231 bool target_is_directory=False;
6232 bool source_has_wild = False;
6233 bool dest_has_wild = False;
6234 SMB_STRUCT_STAT sbuf1, sbuf2;
6235 NTSTATUS status;
6236 TALLOC_CTX *ctx = talloc_tos();
6238 START_PROFILE(SMBcopy);
6240 if (req->wct < 3) {
6241 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
6242 END_PROFILE(SMBcopy);
6243 return;
6246 tid2 = SVAL(req->inbuf,smb_vwv0);
6247 ofun = SVAL(req->inbuf,smb_vwv1);
6248 flags = SVAL(req->inbuf,smb_vwv2);
6250 p = smb_buf(req->inbuf);
6251 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &name, p,
6252 0, STR_TERMINATE, &status,
6253 &source_has_wild);
6254 if (!NT_STATUS_IS_OK(status)) {
6255 reply_nterror(req, status);
6256 END_PROFILE(SMBcopy);
6257 return;
6259 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
6260 0, STR_TERMINATE, &status,
6261 &dest_has_wild);
6262 if (!NT_STATUS_IS_OK(status)) {
6263 reply_nterror(req, status);
6264 END_PROFILE(SMBcopy);
6265 return;
6268 DEBUG(3,("reply_copy : %s -> %s\n",name,newname));
6270 if (tid2 != conn->cnum) {
6271 /* can't currently handle inter share copies XXXX */
6272 DEBUG(3,("Rejecting inter-share copy\n"));
6273 reply_doserror(req, ERRSRV, ERRinvdevice);
6274 END_PROFILE(SMBcopy);
6275 return;
6278 status = resolve_dfspath_wcard(ctx, conn,
6279 req->flags2 & FLAGS2_DFS_PATHNAMES,
6280 name,
6281 &name,
6282 &source_has_wild);
6283 if (!NT_STATUS_IS_OK(status)) {
6284 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
6285 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
6286 ERRSRV, ERRbadpath);
6287 END_PROFILE(SMBcopy);
6288 return;
6290 reply_nterror(req, status);
6291 END_PROFILE(SMBcopy);
6292 return;
6295 status = resolve_dfspath_wcard(ctx, conn,
6296 req->flags2 & FLAGS2_DFS_PATHNAMES,
6297 newname,
6298 &newname,
6299 &dest_has_wild);
6300 if (!NT_STATUS_IS_OK(status)) {
6301 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
6302 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
6303 ERRSRV, ERRbadpath);
6304 END_PROFILE(SMBcopy);
6305 return;
6307 reply_nterror(req, status);
6308 END_PROFILE(SMBcopy);
6309 return;
6312 status = unix_convert(ctx, conn, name, source_has_wild,
6313 &name, NULL, &sbuf1);
6314 if (!NT_STATUS_IS_OK(status)) {
6315 reply_nterror(req, status);
6316 END_PROFILE(SMBcopy);
6317 return;
6320 status = unix_convert(ctx, conn, newname, dest_has_wild,
6321 &newname, NULL, &sbuf2);
6322 if (!NT_STATUS_IS_OK(status)) {
6323 reply_nterror(req, status);
6324 END_PROFILE(SMBcopy);
6325 return;
6328 target_is_directory = VALID_STAT_OF_DIR(sbuf2);
6330 if ((flags&1) && target_is_directory) {
6331 reply_doserror(req, ERRDOS, ERRbadfile);
6332 END_PROFILE(SMBcopy);
6333 return;
6336 if ((flags&2) && !target_is_directory) {
6337 reply_doserror(req, ERRDOS, ERRbadpath);
6338 END_PROFILE(SMBcopy);
6339 return;
6342 if ((flags&(1<<5)) && VALID_STAT_OF_DIR(sbuf1)) {
6343 /* wants a tree copy! XXXX */
6344 DEBUG(3,("Rejecting tree copy\n"));
6345 reply_doserror(req, ERRSRV, ERRerror);
6346 END_PROFILE(SMBcopy);
6347 return;
6350 p = strrchr_m(name,'/');
6351 if (!p) {
6352 directory = talloc_strdup(ctx, "./");
6353 if (!directory) {
6354 reply_nterror(req, NT_STATUS_NO_MEMORY);
6355 END_PROFILE(SMBcopy);
6356 return;
6358 mask = name;
6359 } else {
6360 *p = 0;
6361 directory = talloc_strdup(ctx, name);
6362 if (!directory) {
6363 reply_nterror(req, NT_STATUS_NO_MEMORY);
6364 END_PROFILE(SMBcopy);
6365 return;
6367 mask = p+1;
6371 * We should only check the mangled cache
6372 * here if unix_convert failed. This means
6373 * that the path in 'mask' doesn't exist
6374 * on the file system and so we need to look
6375 * for a possible mangle. This patch from
6376 * Tine Smukavec <valentin.smukavec@hermes.si>.
6379 if (!VALID_STAT(sbuf1) && mangle_is_mangled(mask, conn->params)) {
6380 char *new_mask = NULL;
6381 mangle_lookup_name_from_8_3(ctx,
6382 mask,
6383 &new_mask,
6384 conn->params );
6385 if (new_mask) {
6386 mask = new_mask;
6390 if (!source_has_wild) {
6391 directory = talloc_asprintf_append(directory,
6392 "/%s",
6393 mask);
6394 if (dest_has_wild) {
6395 char *mod_newname = NULL;
6396 if (!resolve_wildcards(ctx,
6397 directory,newname,&mod_newname)) {
6398 reply_nterror(req, NT_STATUS_NO_MEMORY);
6399 END_PROFILE(SMBcopy);
6400 return;
6402 newname = mod_newname;
6405 status = check_name(conn, directory);
6406 if (!NT_STATUS_IS_OK(status)) {
6407 reply_nterror(req, status);
6408 END_PROFILE(SMBcopy);
6409 return;
6412 status = check_name(conn, newname);
6413 if (!NT_STATUS_IS_OK(status)) {
6414 reply_nterror(req, status);
6415 END_PROFILE(SMBcopy);
6416 return;
6419 status = copy_file(ctx,conn,directory,newname,ofun,
6420 count,target_is_directory);
6422 if(!NT_STATUS_IS_OK(status)) {
6423 reply_nterror(req, status);
6424 END_PROFILE(SMBcopy);
6425 return;
6426 } else {
6427 count++;
6429 } else {
6430 struct smb_Dir *dir_hnd = NULL;
6431 const char *dname = NULL;
6432 long offset = 0;
6434 if (strequal(mask,"????????.???")) {
6435 mask[0] = '*';
6436 mask[1] = '\0';
6439 status = check_name(conn, directory);
6440 if (!NT_STATUS_IS_OK(status)) {
6441 reply_nterror(req, status);
6442 END_PROFILE(SMBcopy);
6443 return;
6446 dir_hnd = OpenDir(talloc_tos(), conn, directory, mask, 0);
6447 if (dir_hnd == NULL) {
6448 status = map_nt_error_from_unix(errno);
6449 reply_nterror(req, status);
6450 END_PROFILE(SMBcopy);
6451 return;
6454 error = ERRbadfile;
6456 while ((dname = ReadDirName(dir_hnd, &offset))) {
6457 char *destname = NULL;
6458 char *fname = NULL;
6460 if (ISDOT(dname) || ISDOTDOT(dname)) {
6461 continue;
6464 if (!is_visible_file(conn, directory, dname, &sbuf1, False)) {
6465 continue;
6468 if(!mask_match(dname, mask, conn->case_sensitive)) {
6469 continue;
6472 error = ERRnoaccess;
6473 fname = talloc_asprintf(ctx,
6474 "%s/%s",
6475 directory,
6476 dname);
6477 if (!fname) {
6478 TALLOC_FREE(dir_hnd);
6479 reply_nterror(req, NT_STATUS_NO_MEMORY);
6480 END_PROFILE(SMBcopy);
6481 return;
6484 if (!resolve_wildcards(ctx,
6485 fname,newname,&destname)) {
6486 continue;
6488 if (!destname) {
6489 TALLOC_FREE(dir_hnd);
6490 reply_nterror(req, NT_STATUS_NO_MEMORY);
6491 END_PROFILE(SMBcopy);
6492 return;
6495 status = check_name(conn, fname);
6496 if (!NT_STATUS_IS_OK(status)) {
6497 TALLOC_FREE(dir_hnd);
6498 reply_nterror(req, status);
6499 END_PROFILE(SMBcopy);
6500 return;
6503 status = check_name(conn, destname);
6504 if (!NT_STATUS_IS_OK(status)) {
6505 TALLOC_FREE(dir_hnd);
6506 reply_nterror(req, status);
6507 END_PROFILE(SMBcopy);
6508 return;
6511 DEBUG(3,("reply_copy : doing copy on %s -> %s\n",fname, destname));
6513 status = copy_file(ctx,conn,fname,destname,ofun,
6514 count,target_is_directory);
6515 if (NT_STATUS_IS_OK(status)) {
6516 count++;
6518 TALLOC_FREE(fname);
6519 TALLOC_FREE(destname);
6521 TALLOC_FREE(dir_hnd);
6524 if (count == 0) {
6525 if(err) {
6526 /* Error on close... */
6527 errno = err;
6528 reply_unixerror(req, ERRHRD, ERRgeneral);
6529 END_PROFILE(SMBcopy);
6530 return;
6533 reply_doserror(req, ERRDOS, error);
6534 END_PROFILE(SMBcopy);
6535 return;
6538 reply_outbuf(req, 1, 0);
6539 SSVAL(req->outbuf,smb_vwv0,count);
6541 END_PROFILE(SMBcopy);
6542 return;
6545 #undef DBGC_CLASS
6546 #define DBGC_CLASS DBGC_LOCKING
6548 /****************************************************************************
6549 Get a lock pid, dealing with large count requests.
6550 ****************************************************************************/
6552 uint32 get_lock_pid( char *data, int data_offset, bool large_file_format)
6554 if(!large_file_format)
6555 return (uint32)SVAL(data,SMB_LPID_OFFSET(data_offset));
6556 else
6557 return (uint32)SVAL(data,SMB_LARGE_LPID_OFFSET(data_offset));
6560 /****************************************************************************
6561 Get a lock count, dealing with large count requests.
6562 ****************************************************************************/
6564 SMB_BIG_UINT get_lock_count( char *data, int data_offset, bool large_file_format)
6566 SMB_BIG_UINT count = 0;
6568 if(!large_file_format) {
6569 count = (SMB_BIG_UINT)IVAL(data,SMB_LKLEN_OFFSET(data_offset));
6570 } else {
6572 #if defined(HAVE_LONGLONG)
6573 count = (((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset))) << 32) |
6574 ((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)));
6575 #else /* HAVE_LONGLONG */
6578 * NT4.x seems to be broken in that it sends large file (64 bit)
6579 * lockingX calls even if the CAP_LARGE_FILES was *not*
6580 * negotiated. For boxes without large unsigned ints truncate the
6581 * lock count by dropping the top 32 bits.
6584 if(IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)) != 0) {
6585 DEBUG(3,("get_lock_count: truncating lock count (high)0x%x (low)0x%x to just low count.\n",
6586 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)),
6587 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)) ));
6588 SIVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset),0);
6591 count = (SMB_BIG_UINT)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset));
6592 #endif /* HAVE_LONGLONG */
6595 return count;
6598 #if !defined(HAVE_LONGLONG)
6599 /****************************************************************************
6600 Pathetically try and map a 64 bit lock offset into 31 bits. I hate Windows :-).
6601 ****************************************************************************/
6603 static uint32 map_lock_offset(uint32 high, uint32 low)
6605 unsigned int i;
6606 uint32 mask = 0;
6607 uint32 highcopy = high;
6610 * Try and find out how many significant bits there are in high.
6613 for(i = 0; highcopy; i++)
6614 highcopy >>= 1;
6617 * We use 31 bits not 32 here as POSIX
6618 * lock offsets may not be negative.
6621 mask = (~0) << (31 - i);
6623 if(low & mask)
6624 return 0; /* Fail. */
6626 high <<= (31 - i);
6628 return (high|low);
6630 #endif /* !defined(HAVE_LONGLONG) */
6632 /****************************************************************************
6633 Get a lock offset, dealing with large offset requests.
6634 ****************************************************************************/
6636 SMB_BIG_UINT get_lock_offset( char *data, int data_offset, bool large_file_format, bool *err)
6638 SMB_BIG_UINT offset = 0;
6640 *err = False;
6642 if(!large_file_format) {
6643 offset = (SMB_BIG_UINT)IVAL(data,SMB_LKOFF_OFFSET(data_offset));
6644 } else {
6646 #if defined(HAVE_LONGLONG)
6647 offset = (((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset))) << 32) |
6648 ((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset)));
6649 #else /* HAVE_LONGLONG */
6652 * NT4.x seems to be broken in that it sends large file (64 bit)
6653 * lockingX calls even if the CAP_LARGE_FILES was *not*
6654 * negotiated. For boxes without large unsigned ints mangle the
6655 * lock offset by mapping the top 32 bits onto the lower 32.
6658 if(IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset)) != 0) {
6659 uint32 low = IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
6660 uint32 high = IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset));
6661 uint32 new_low = 0;
6663 if((new_low = map_lock_offset(high, low)) == 0) {
6664 *err = True;
6665 return (SMB_BIG_UINT)-1;
6668 DEBUG(3,("get_lock_offset: truncating lock offset (high)0x%x (low)0x%x to offset 0x%x.\n",
6669 (unsigned int)high, (unsigned int)low, (unsigned int)new_low ));
6670 SIVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset),0);
6671 SIVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset),new_low);
6674 offset = (SMB_BIG_UINT)IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
6675 #endif /* HAVE_LONGLONG */
6678 return offset;
6681 /****************************************************************************
6682 Reply to a lockingX request.
6683 ****************************************************************************/
6685 void reply_lockingX(struct smb_request *req)
6687 connection_struct *conn = req->conn;
6688 files_struct *fsp;
6689 unsigned char locktype;
6690 unsigned char oplocklevel;
6691 uint16 num_ulocks;
6692 uint16 num_locks;
6693 SMB_BIG_UINT count = 0, offset = 0;
6694 uint32 lock_pid;
6695 int32 lock_timeout;
6696 int i;
6697 char *data;
6698 bool large_file_format;
6699 bool err;
6700 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
6702 START_PROFILE(SMBlockingX);
6704 if (req->wct < 8) {
6705 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
6706 END_PROFILE(SMBlockingX);
6707 return;
6710 fsp = file_fsp(SVAL(req->inbuf,smb_vwv2));
6711 locktype = CVAL(req->inbuf,smb_vwv3);
6712 oplocklevel = CVAL(req->inbuf,smb_vwv3+1);
6713 num_ulocks = SVAL(req->inbuf,smb_vwv6);
6714 num_locks = SVAL(req->inbuf,smb_vwv7);
6715 lock_timeout = IVAL(req->inbuf,smb_vwv4);
6716 large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
6718 if (!check_fsp(conn, req, fsp)) {
6719 END_PROFILE(SMBlockingX);
6720 return;
6723 data = smb_buf(req->inbuf);
6725 if (locktype & LOCKING_ANDX_CHANGE_LOCKTYPE) {
6726 /* we don't support these - and CANCEL_LOCK makes w2k
6727 and XP reboot so I don't really want to be
6728 compatible! (tridge) */
6729 reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks));
6730 END_PROFILE(SMBlockingX);
6731 return;
6734 /* Check if this is an oplock break on a file
6735 we have granted an oplock on.
6737 if ((locktype & LOCKING_ANDX_OPLOCK_RELEASE)) {
6738 /* Client can insist on breaking to none. */
6739 bool break_to_none = (oplocklevel == 0);
6740 bool result;
6742 DEBUG(5,("reply_lockingX: oplock break reply (%u) from client "
6743 "for fnum = %d\n", (unsigned int)oplocklevel,
6744 fsp->fnum ));
6747 * Make sure we have granted an exclusive or batch oplock on
6748 * this file.
6751 if (fsp->oplock_type == 0) {
6753 /* The Samba4 nbench simulator doesn't understand
6754 the difference between break to level2 and break
6755 to none from level2 - it sends oplock break
6756 replies in both cases. Don't keep logging an error
6757 message here - just ignore it. JRA. */
6759 DEBUG(5,("reply_lockingX: Error : oplock break from "
6760 "client for fnum = %d (oplock=%d) and no "
6761 "oplock granted on this file (%s).\n",
6762 fsp->fnum, fsp->oplock_type, fsp->fsp_name));
6764 /* if this is a pure oplock break request then don't
6765 * send a reply */
6766 if (num_locks == 0 && num_ulocks == 0) {
6767 END_PROFILE(SMBlockingX);
6768 return;
6769 } else {
6770 END_PROFILE(SMBlockingX);
6771 reply_doserror(req, ERRDOS, ERRlock);
6772 return;
6776 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
6777 (break_to_none)) {
6778 result = remove_oplock(fsp);
6779 } else {
6780 result = downgrade_oplock(fsp);
6783 if (!result) {
6784 DEBUG(0, ("reply_lockingX: error in removing "
6785 "oplock on file %s\n", fsp->fsp_name));
6786 /* Hmmm. Is this panic justified? */
6787 smb_panic("internal tdb error");
6790 reply_to_oplock_break_requests(fsp);
6792 /* if this is a pure oplock break request then don't send a
6793 * reply */
6794 if (num_locks == 0 && num_ulocks == 0) {
6795 /* Sanity check - ensure a pure oplock break is not a
6796 chained request. */
6797 if(CVAL(req->inbuf,smb_vwv0) != 0xff)
6798 DEBUG(0,("reply_lockingX: Error : pure oplock "
6799 "break is a chained %d request !\n",
6800 (unsigned int)CVAL(req->inbuf,
6801 smb_vwv0) ));
6802 END_PROFILE(SMBlockingX);
6803 return;
6808 * We do this check *after* we have checked this is not a oplock break
6809 * response message. JRA.
6812 release_level_2_oplocks_on_change(fsp);
6814 if (smb_buflen(req->inbuf) <
6815 (num_ulocks + num_locks) * (large_file_format ? 20 : 10)) {
6816 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
6817 END_PROFILE(SMBlockingX);
6818 return;
6821 /* Data now points at the beginning of the list
6822 of smb_unlkrng structs */
6823 for(i = 0; i < (int)num_ulocks; i++) {
6824 lock_pid = get_lock_pid( data, i, large_file_format);
6825 count = get_lock_count( data, i, large_file_format);
6826 offset = get_lock_offset( data, i, large_file_format, &err);
6829 * There is no error code marked "stupid client bug".... :-).
6831 if(err) {
6832 END_PROFILE(SMBlockingX);
6833 reply_doserror(req, ERRDOS, ERRnoaccess);
6834 return;
6837 DEBUG(10,("reply_lockingX: unlock start=%.0f, len=%.0f for "
6838 "pid %u, file %s\n", (double)offset, (double)count,
6839 (unsigned int)lock_pid, fsp->fsp_name ));
6841 status = do_unlock(smbd_messaging_context(),
6842 fsp,
6843 lock_pid,
6844 count,
6845 offset,
6846 WINDOWS_LOCK);
6848 if (NT_STATUS_V(status)) {
6849 END_PROFILE(SMBlockingX);
6850 reply_nterror(req, status);
6851 return;
6855 /* Setup the timeout in seconds. */
6857 if (!lp_blocking_locks(SNUM(conn))) {
6858 lock_timeout = 0;
6861 /* Now do any requested locks */
6862 data += ((large_file_format ? 20 : 10)*num_ulocks);
6864 /* Data now points at the beginning of the list
6865 of smb_lkrng structs */
6867 for(i = 0; i < (int)num_locks; i++) {
6868 enum brl_type lock_type = ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
6869 READ_LOCK:WRITE_LOCK);
6870 lock_pid = get_lock_pid( data, i, large_file_format);
6871 count = get_lock_count( data, i, large_file_format);
6872 offset = get_lock_offset( data, i, large_file_format, &err);
6875 * There is no error code marked "stupid client bug".... :-).
6877 if(err) {
6878 END_PROFILE(SMBlockingX);
6879 reply_doserror(req, ERRDOS, ERRnoaccess);
6880 return;
6883 DEBUG(10,("reply_lockingX: lock start=%.0f, len=%.0f for pid "
6884 "%u, file %s timeout = %d\n", (double)offset,
6885 (double)count, (unsigned int)lock_pid,
6886 fsp->fsp_name, (int)lock_timeout ));
6888 if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
6889 if (lp_blocking_locks(SNUM(conn))) {
6891 /* Schedule a message to ourselves to
6892 remove the blocking lock record and
6893 return the right error. */
6895 if (!blocking_lock_cancel(fsp,
6896 lock_pid,
6897 offset,
6898 count,
6899 WINDOWS_LOCK,
6900 locktype,
6901 NT_STATUS_FILE_LOCK_CONFLICT)) {
6902 END_PROFILE(SMBlockingX);
6903 reply_nterror(
6904 req,
6905 NT_STATUS_DOS(
6906 ERRDOS,
6907 ERRcancelviolation));
6908 return;
6911 /* Remove a matching pending lock. */
6912 status = do_lock_cancel(fsp,
6913 lock_pid,
6914 count,
6915 offset,
6916 WINDOWS_LOCK);
6917 } else {
6918 bool blocking_lock = lock_timeout ? True : False;
6919 bool defer_lock = False;
6920 struct byte_range_lock *br_lck;
6921 uint32 block_smbpid;
6923 br_lck = do_lock(smbd_messaging_context(),
6924 fsp,
6925 lock_pid,
6926 count,
6927 offset,
6928 lock_type,
6929 WINDOWS_LOCK,
6930 blocking_lock,
6931 &status,
6932 &block_smbpid);
6934 if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
6935 /* Windows internal resolution for blocking locks seems
6936 to be about 200ms... Don't wait for less than that. JRA. */
6937 if (lock_timeout != -1 && lock_timeout < lp_lock_spin_time()) {
6938 lock_timeout = lp_lock_spin_time();
6940 defer_lock = True;
6943 /* This heuristic seems to match W2K3 very well. If a
6944 lock sent with timeout of zero would fail with NT_STATUS_FILE_LOCK_CONFLICT
6945 it pretends we asked for a timeout of between 150 - 300 milliseconds as
6946 far as I can tell. Replacement for do_lock_spin(). JRA. */
6948 if (br_lck && lp_blocking_locks(SNUM(conn)) && !blocking_lock &&
6949 NT_STATUS_EQUAL((status), NT_STATUS_FILE_LOCK_CONFLICT)) {
6950 defer_lock = True;
6951 lock_timeout = lp_lock_spin_time();
6954 if (br_lck && defer_lock) {
6956 * A blocking lock was requested. Package up
6957 * this smb into a queued request and push it
6958 * onto the blocking lock queue.
6960 if(push_blocking_lock_request(br_lck,
6961 req,
6962 fsp,
6963 lock_timeout,
6965 lock_pid,
6966 lock_type,
6967 WINDOWS_LOCK,
6968 offset,
6969 count,
6970 block_smbpid)) {
6971 TALLOC_FREE(br_lck);
6972 END_PROFILE(SMBlockingX);
6973 return;
6977 TALLOC_FREE(br_lck);
6980 if (NT_STATUS_V(status)) {
6981 END_PROFILE(SMBlockingX);
6982 reply_nterror(req, status);
6983 return;
6987 /* If any of the above locks failed, then we must unlock
6988 all of the previous locks (X/Open spec). */
6990 if (!(locktype & LOCKING_ANDX_CANCEL_LOCK) &&
6991 (i != num_locks) &&
6992 (num_locks != 0)) {
6994 * Ensure we don't do a remove on the lock that just failed,
6995 * as under POSIX rules, if we have a lock already there, we
6996 * will delete it (and we shouldn't) .....
6998 for(i--; i >= 0; i--) {
6999 lock_pid = get_lock_pid( data, i, large_file_format);
7000 count = get_lock_count( data, i, large_file_format);
7001 offset = get_lock_offset( data, i, large_file_format,
7002 &err);
7005 * There is no error code marked "stupid client
7006 * bug".... :-).
7008 if(err) {
7009 END_PROFILE(SMBlockingX);
7010 reply_doserror(req, ERRDOS, ERRnoaccess);
7011 return;
7014 do_unlock(smbd_messaging_context(),
7015 fsp,
7016 lock_pid,
7017 count,
7018 offset,
7019 WINDOWS_LOCK);
7021 END_PROFILE(SMBlockingX);
7022 reply_nterror(req, status);
7023 return;
7026 reply_outbuf(req, 2, 0);
7028 DEBUG(3, ("lockingX fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
7029 fsp->fnum, (unsigned int)locktype, num_locks, num_ulocks));
7031 END_PROFILE(SMBlockingX);
7032 chain_reply(req);
7035 #undef DBGC_CLASS
7036 #define DBGC_CLASS DBGC_ALL
7038 /****************************************************************************
7039 Reply to a SMBreadbmpx (read block multiplex) request.
7040 Always reply with an error, if someone has a platform really needs this,
7041 please contact vl@samba.org
7042 ****************************************************************************/
7044 void reply_readbmpx(struct smb_request *req)
7046 START_PROFILE(SMBreadBmpx);
7047 reply_doserror(req, ERRSRV, ERRuseSTD);
7048 END_PROFILE(SMBreadBmpx);
7049 return;
7052 /****************************************************************************
7053 Reply to a SMBreadbs (read block multiplex secondary) request.
7054 Always reply with an error, if someone has a platform really needs this,
7055 please contact vl@samba.org
7056 ****************************************************************************/
7058 void reply_readbs(struct smb_request *req)
7060 START_PROFILE(SMBreadBs);
7061 reply_doserror(req, ERRSRV, ERRuseSTD);
7062 END_PROFILE(SMBreadBs);
7063 return;
7066 /****************************************************************************
7067 Reply to a SMBsetattrE.
7068 ****************************************************************************/
7070 void reply_setattrE(struct smb_request *req)
7072 connection_struct *conn = req->conn;
7073 struct timespec ts[2];
7074 files_struct *fsp;
7075 SMB_STRUCT_STAT sbuf;
7076 NTSTATUS status;
7078 START_PROFILE(SMBsetattrE);
7080 if (req->wct < 7) {
7081 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7082 END_PROFILE(SMBsetattrE);
7083 return;
7086 fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
7088 if(!fsp || (fsp->conn != conn)) {
7089 reply_doserror(req, ERRDOS, ERRbadfid);
7090 END_PROFILE(SMBsetattrE);
7091 return;
7096 * Convert the DOS times into unix times. Ignore create
7097 * time as UNIX can't set this.
7100 ts[0] = convert_time_t_to_timespec(
7101 srv_make_unix_date2(req->inbuf+smb_vwv3)); /* atime. */
7102 ts[1] = convert_time_t_to_timespec(
7103 srv_make_unix_date2(req->inbuf+smb_vwv5)); /* mtime. */
7105 reply_outbuf(req, 0, 0);
7108 * Patch from Ray Frush <frush@engr.colostate.edu>
7109 * Sometimes times are sent as zero - ignore them.
7112 /* Ensure we have a valid stat struct for the source. */
7113 if (fsp->fh->fd != -1) {
7114 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
7115 status = map_nt_error_from_unix(errno);
7116 reply_nterror(req, status);
7117 END_PROFILE(SMBsetattrE);
7118 return;
7120 } else {
7121 if (SMB_VFS_STAT(conn, fsp->fsp_name, &sbuf) == -1) {
7122 status = map_nt_error_from_unix(errno);
7123 reply_nterror(req, status);
7124 END_PROFILE(SMBsetattrE);
7125 return;
7129 status = smb_set_file_time(conn, fsp, fsp->fsp_name,
7130 &sbuf, ts, true);
7131 if (!NT_STATUS_IS_OK(status)) {
7132 reply_doserror(req, ERRDOS, ERRnoaccess);
7133 END_PROFILE(SMBsetattrE);
7134 return;
7137 DEBUG( 3, ( "reply_setattrE fnum=%d actime=%u modtime=%u\n",
7138 fsp->fnum,
7139 (unsigned int)ts[0].tv_sec,
7140 (unsigned int)ts[1].tv_sec));
7142 END_PROFILE(SMBsetattrE);
7143 return;
7147 /* Back from the dead for OS/2..... JRA. */
7149 /****************************************************************************
7150 Reply to a SMBwritebmpx (write block multiplex primary) request.
7151 Always reply with an error, if someone has a platform really needs this,
7152 please contact vl@samba.org
7153 ****************************************************************************/
7155 void reply_writebmpx(struct smb_request *req)
7157 START_PROFILE(SMBwriteBmpx);
7158 reply_doserror(req, ERRSRV, ERRuseSTD);
7159 END_PROFILE(SMBwriteBmpx);
7160 return;
7163 /****************************************************************************
7164 Reply to a SMBwritebs (write block multiplex secondary) request.
7165 Always reply with an error, if someone has a platform really needs this,
7166 please contact vl@samba.org
7167 ****************************************************************************/
7169 void reply_writebs(struct smb_request *req)
7171 START_PROFILE(SMBwriteBs);
7172 reply_doserror(req, ERRSRV, ERRuseSTD);
7173 END_PROFILE(SMBwriteBs);
7174 return;
7177 /****************************************************************************
7178 Reply to a SMBgetattrE.
7179 ****************************************************************************/
7181 void reply_getattrE(struct smb_request *req)
7183 connection_struct *conn = req->conn;
7184 SMB_STRUCT_STAT sbuf;
7185 int mode;
7186 files_struct *fsp;
7187 struct timespec create_ts;
7189 START_PROFILE(SMBgetattrE);
7191 if (req->wct < 1) {
7192 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7193 END_PROFILE(SMBgetattrE);
7194 return;
7197 fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
7199 if(!fsp || (fsp->conn != conn)) {
7200 reply_doserror(req, ERRDOS, ERRbadfid);
7201 END_PROFILE(SMBgetattrE);
7202 return;
7205 /* Do an fstat on this file */
7206 if(fsp_stat(fsp, &sbuf)) {
7207 reply_unixerror(req, ERRDOS, ERRnoaccess);
7208 END_PROFILE(SMBgetattrE);
7209 return;
7212 mode = dos_mode(conn,fsp->fsp_name,&sbuf);
7215 * Convert the times into dos times. Set create
7216 * date to be last modify date as UNIX doesn't save
7217 * this.
7220 reply_outbuf(req, 11, 0);
7222 create_ts = get_create_timespec(&sbuf,
7223 lp_fake_dir_create_times(SNUM(conn)));
7224 srv_put_dos_date2((char *)req->outbuf, smb_vwv0, create_ts.tv_sec);
7225 srv_put_dos_date2((char *)req->outbuf, smb_vwv2, sbuf.st_atime);
7226 /* Should we check pending modtime here ? JRA */
7227 srv_put_dos_date2((char *)req->outbuf, smb_vwv4, sbuf.st_mtime);
7229 if (mode & aDIR) {
7230 SIVAL(req->outbuf, smb_vwv6, 0);
7231 SIVAL(req->outbuf, smb_vwv8, 0);
7232 } else {
7233 uint32 allocation_size = get_allocation_size(conn,fsp, &sbuf);
7234 SIVAL(req->outbuf, smb_vwv6, (uint32)sbuf.st_size);
7235 SIVAL(req->outbuf, smb_vwv8, allocation_size);
7237 SSVAL(req->outbuf,smb_vwv10, mode);
7239 DEBUG( 3, ( "reply_getattrE fnum=%d\n", fsp->fnum));
7241 END_PROFILE(SMBgetattrE);
7242 return;