Check we read off the compelte event from inotify
[Samba.git] / source3 / smbd / reply.c
blob285056c6d944edfa16114da69a0f4c111d4336af
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"
28 #include "smbd/globals.h"
30 extern enum protocol_types Protocol;
32 /****************************************************************************
33 Ensure we check the path in *exactly* the same way as W2K for a findfirst/findnext
34 path or anything including wildcards.
35 We're assuming here that '/' is not the second byte in any multibyte char
36 set (a safe assumption). '\\' *may* be the second byte in a multibyte char
37 set.
38 ****************************************************************************/
40 /* Custom version for processing POSIX paths. */
41 #define IS_PATH_SEP(c,posix_only) ((c) == '/' || (!(posix_only) && (c) == '\\'))
43 static NTSTATUS check_path_syntax_internal(char *path,
44 bool posix_path,
45 bool *p_last_component_contains_wcard)
47 char *d = path;
48 const char *s = path;
49 NTSTATUS ret = NT_STATUS_OK;
50 bool start_of_name_component = True;
51 bool stream_started = false;
53 *p_last_component_contains_wcard = False;
55 while (*s) {
56 if (stream_started) {
57 switch (*s) {
58 case '/':
59 case '\\':
60 return NT_STATUS_OBJECT_NAME_INVALID;
61 case ':':
62 if (s[1] == '\0') {
63 return NT_STATUS_OBJECT_NAME_INVALID;
65 if (strchr_m(&s[1], ':')) {
66 return NT_STATUS_OBJECT_NAME_INVALID;
68 if (StrCaseCmp(s, ":$DATA") != 0) {
69 return NT_STATUS_INVALID_PARAMETER;
71 break;
75 if (!posix_path && !stream_started && *s == ':') {
76 if (*p_last_component_contains_wcard) {
77 return NT_STATUS_OBJECT_NAME_INVALID;
79 /* Stream names allow more characters than file names.
80 We're overloading posix_path here to allow a wider
81 range of characters. If stream_started is true this
82 is still a Windows path even if posix_path is true.
83 JRA.
85 stream_started = true;
86 start_of_name_component = false;
87 posix_path = true;
89 if (s[1] == '\0') {
90 return NT_STATUS_OBJECT_NAME_INVALID;
94 if (!stream_started && IS_PATH_SEP(*s,posix_path)) {
96 * Safe to assume is not the second part of a mb char
97 * as this is handled below.
99 /* Eat multiple '/' or '\\' */
100 while (IS_PATH_SEP(*s,posix_path)) {
101 s++;
103 if ((d != path) && (*s != '\0')) {
104 /* We only care about non-leading or trailing '/' or '\\' */
105 *d++ = '/';
108 start_of_name_component = True;
109 /* New component. */
110 *p_last_component_contains_wcard = False;
111 continue;
114 if (start_of_name_component) {
115 if ((s[0] == '.') && (s[1] == '.') && (IS_PATH_SEP(s[2],posix_path) || s[2] == '\0')) {
116 /* Uh oh - "/../" or "\\..\\" or "/..\0" or "\\..\0" ! */
119 * No mb char starts with '.' so we're safe checking the directory separator here.
122 /* If we just added a '/' - delete it */
123 if ((d > path) && (*(d-1) == '/')) {
124 *(d-1) = '\0';
125 d--;
128 /* Are we at the start ? Can't go back further if so. */
129 if (d <= path) {
130 ret = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
131 break;
133 /* Go back one level... */
134 /* We know this is safe as '/' cannot be part of a mb sequence. */
135 /* NOTE - if this assumption is invalid we are not in good shape... */
136 /* Decrement d first as d points to the *next* char to write into. */
137 for (d--; d > path; d--) {
138 if (*d == '/')
139 break;
141 s += 2; /* Else go past the .. */
142 /* We're still at the start of a name component, just the previous one. */
143 continue;
145 } else if ((s[0] == '.') && ((s[1] == '\0') || IS_PATH_SEP(s[1],posix_path))) {
146 if (posix_path) {
147 /* Eat the '.' */
148 s++;
149 continue;
155 if (!(*s & 0x80)) {
156 if (!posix_path) {
157 if (*s <= 0x1f || *s == '|') {
158 return NT_STATUS_OBJECT_NAME_INVALID;
160 switch (*s) {
161 case '*':
162 case '?':
163 case '<':
164 case '>':
165 case '"':
166 *p_last_component_contains_wcard = True;
167 break;
168 default:
169 break;
172 *d++ = *s++;
173 } else {
174 size_t siz;
175 /* Get the size of the next MB character. */
176 next_codepoint(s,&siz);
177 switch(siz) {
178 case 5:
179 *d++ = *s++;
180 /*fall through*/
181 case 4:
182 *d++ = *s++;
183 /*fall through*/
184 case 3:
185 *d++ = *s++;
186 /*fall through*/
187 case 2:
188 *d++ = *s++;
189 /*fall through*/
190 case 1:
191 *d++ = *s++;
192 break;
193 default:
194 DEBUG(0,("check_path_syntax_internal: character length assumptions invalid !\n"));
195 *d = '\0';
196 return NT_STATUS_INVALID_PARAMETER;
199 start_of_name_component = False;
202 *d = '\0';
204 return ret;
207 /****************************************************************************
208 Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
209 No wildcards allowed.
210 ****************************************************************************/
212 NTSTATUS check_path_syntax(char *path)
214 bool ignore;
215 return check_path_syntax_internal(path, False, &ignore);
218 /****************************************************************************
219 Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
220 Wildcards allowed - p_contains_wcard returns true if the last component contained
221 a wildcard.
222 ****************************************************************************/
224 NTSTATUS check_path_syntax_wcard(char *path, bool *p_contains_wcard)
226 return check_path_syntax_internal(path, False, p_contains_wcard);
229 /****************************************************************************
230 Check the path for a POSIX client.
231 We're assuming here that '/' is not the second byte in any multibyte char
232 set (a safe assumption).
233 ****************************************************************************/
235 NTSTATUS check_path_syntax_posix(char *path)
237 bool ignore;
238 return check_path_syntax_internal(path, True, &ignore);
241 /****************************************************************************
242 Pull a string and check the path allowing a wilcard - provide for error return.
243 ****************************************************************************/
245 size_t srvstr_get_path_wcard(TALLOC_CTX *ctx,
246 const char *base_ptr,
247 uint16 smb_flags2,
248 char **pp_dest,
249 const char *src,
250 size_t src_len,
251 int flags,
252 NTSTATUS *err,
253 bool *contains_wcard)
255 size_t ret;
257 *pp_dest = NULL;
259 ret = srvstr_pull_talloc(ctx, base_ptr, smb_flags2, pp_dest, src,
260 src_len, flags);
262 if (!*pp_dest) {
263 *err = NT_STATUS_INVALID_PARAMETER;
264 return ret;
267 *contains_wcard = False;
269 if (smb_flags2 & FLAGS2_DFS_PATHNAMES) {
271 * For a DFS path the function parse_dfs_path()
272 * will do the path processing, just make a copy.
274 *err = NT_STATUS_OK;
275 return ret;
278 if (lp_posix_pathnames()) {
279 *err = check_path_syntax_posix(*pp_dest);
280 } else {
281 *err = check_path_syntax_wcard(*pp_dest, contains_wcard);
284 return ret;
287 /****************************************************************************
288 Pull a string and check the path - provide for error return.
289 ****************************************************************************/
291 size_t srvstr_get_path(TALLOC_CTX *ctx,
292 const char *base_ptr,
293 uint16 smb_flags2,
294 char **pp_dest,
295 const char *src,
296 size_t src_len,
297 int flags,
298 NTSTATUS *err)
300 bool ignore;
301 return srvstr_get_path_wcard(ctx, base_ptr, smb_flags2, pp_dest, src,
302 src_len, flags, err, &ignore);
305 size_t srvstr_get_path_req_wcard(TALLOC_CTX *mem_ctx, struct smb_request *req,
306 char **pp_dest, const char *src, int flags,
307 NTSTATUS *err, bool *contains_wcard)
309 return srvstr_get_path_wcard(mem_ctx, (char *)req->inbuf, req->flags2,
310 pp_dest, src, smbreq_bufrem(req, src),
311 flags, err, contains_wcard);
314 size_t srvstr_get_path_req(TALLOC_CTX *mem_ctx, struct smb_request *req,
315 char **pp_dest, const char *src, int flags,
316 NTSTATUS *err)
318 bool ignore;
319 return srvstr_get_path_req_wcard(mem_ctx, req, pp_dest, src,
320 flags, err, &ignore);
323 /****************************************************************************
324 Check if we have a correct fsp pointing to a file. Basic check for open fsp.
325 ****************************************************************************/
327 bool check_fsp_open(connection_struct *conn, struct smb_request *req,
328 files_struct *fsp)
330 if (!(fsp) || !(conn)) {
331 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
332 return False;
334 if (((conn) != (fsp)->conn) || req->vuid != (fsp)->vuid) {
335 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
336 return False;
338 return True;
341 /****************************************************************************
342 Check if we have a correct fsp pointing to a file.
343 ****************************************************************************/
345 bool check_fsp(connection_struct *conn, struct smb_request *req,
346 files_struct *fsp)
348 if (!check_fsp_open(conn, req, fsp)) {
349 return False;
351 if ((fsp)->is_directory) {
352 reply_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
353 return False;
355 if ((fsp)->fh->fd == -1) {
356 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
357 return False;
359 (fsp)->num_smb_operations++;
360 return True;
363 /****************************************************************************
364 Check if we have a correct fsp pointing to a quota fake file. Replacement for
365 the CHECK_NTQUOTA_HANDLE_OK macro.
366 ****************************************************************************/
368 bool check_fsp_ntquota_handle(connection_struct *conn, struct smb_request *req,
369 files_struct *fsp)
371 if (!check_fsp_open(conn, req, fsp)) {
372 return false;
375 if (fsp->is_directory) {
376 return false;
379 if (fsp->fake_file_handle == NULL) {
380 return false;
383 if (fsp->fake_file_handle->type != FAKE_FILE_TYPE_QUOTA) {
384 return false;
387 if (fsp->fake_file_handle->private_data == NULL) {
388 return false;
391 return true;
394 /****************************************************************************
395 Check if we have a correct fsp. Replacement for the FSP_BELONGS_CONN macro
396 ****************************************************************************/
398 bool fsp_belongs_conn(connection_struct *conn, struct smb_request *req,
399 files_struct *fsp)
401 if ((fsp) && (conn) && ((conn)==(fsp)->conn)
402 && (req->vuid == (fsp)->vuid)) {
403 return True;
406 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
407 return False;
410 /****************************************************************************
411 Reply to a (netbios-level) special message.
412 ****************************************************************************/
414 void reply_special(char *inbuf)
416 int msg_type = CVAL(inbuf,0);
417 int msg_flags = CVAL(inbuf,1);
418 fstring name1,name2;
419 char name_type = 0;
422 * We only really use 4 bytes of the outbuf, but for the smb_setlen
423 * calculation & friends (srv_send_smb uses that) we need the full smb
424 * header.
426 char outbuf[smb_size];
428 *name1 = *name2 = 0;
430 memset(outbuf, '\0', sizeof(outbuf));
432 smb_setlen(outbuf,0);
434 switch (msg_type) {
435 case 0x81: /* session request */
437 if (already_got_session) {
438 exit_server_cleanly("multiple session request not permitted");
441 SCVAL(outbuf,0,0x82);
442 SCVAL(outbuf,3,0);
443 if (name_len(inbuf+4) > 50 ||
444 name_len(inbuf+4 + name_len(inbuf + 4)) > 50) {
445 DEBUG(0,("Invalid name length in session request\n"));
446 return;
448 name_extract(inbuf,4,name1);
449 name_type = name_extract(inbuf,4 + name_len(inbuf + 4),name2);
450 DEBUG(2,("netbios connect: name1=%s name2=%s\n",
451 name1,name2));
453 set_local_machine_name(name1, True);
454 set_remote_machine_name(name2, True);
456 DEBUG(2,("netbios connect: local=%s remote=%s, name type = %x\n",
457 get_local_machine_name(), get_remote_machine_name(),
458 name_type));
460 if (name_type == 'R') {
461 /* We are being asked for a pathworks session ---
462 no thanks! */
463 SCVAL(outbuf, 0,0x83);
464 break;
467 /* only add the client's machine name to the list
468 of possibly valid usernames if we are operating
469 in share mode security */
470 if (lp_security() == SEC_SHARE) {
471 add_session_user(get_remote_machine_name());
474 reload_services(True);
475 reopen_logs();
477 already_got_session = True;
478 break;
480 case 0x89: /* session keepalive request
481 (some old clients produce this?) */
482 SCVAL(outbuf,0,SMBkeepalive);
483 SCVAL(outbuf,3,0);
484 break;
486 case 0x82: /* positive session response */
487 case 0x83: /* negative session response */
488 case 0x84: /* retarget session response */
489 DEBUG(0,("Unexpected session response\n"));
490 break;
492 case SMBkeepalive: /* session keepalive */
493 default:
494 return;
497 DEBUG(5,("init msg_type=0x%x msg_flags=0x%x\n",
498 msg_type, msg_flags));
500 srv_send_smb(smbd_server_fd(), outbuf, false, NULL);
501 return;
504 /****************************************************************************
505 Reply to a tcon.
506 conn POINTER CAN BE NULL HERE !
507 ****************************************************************************/
509 void reply_tcon(struct smb_request *req)
511 connection_struct *conn = req->conn;
512 const char *service;
513 char *service_buf = NULL;
514 char *password = NULL;
515 char *dev = NULL;
516 int pwlen=0;
517 NTSTATUS nt_status;
518 const char *p;
519 DATA_BLOB password_blob;
520 TALLOC_CTX *ctx = talloc_tos();
522 START_PROFILE(SMBtcon);
524 if (req->buflen < 4) {
525 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
526 END_PROFILE(SMBtcon);
527 return;
530 p = (const char *)req->buf + 1;
531 p += srvstr_pull_req_talloc(ctx, req, &service_buf, p, STR_TERMINATE);
532 p += 1;
533 pwlen = srvstr_pull_req_talloc(ctx, req, &password, p, STR_TERMINATE);
534 p += pwlen+1;
535 p += srvstr_pull_req_talloc(ctx, req, &dev, p, STR_TERMINATE);
536 p += 1;
538 if (service_buf == NULL || password == NULL || dev == NULL) {
539 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
540 END_PROFILE(SMBtcon);
541 return;
543 p = strrchr_m(service_buf,'\\');
544 if (p) {
545 service = p+1;
546 } else {
547 service = service_buf;
550 password_blob = data_blob(password, pwlen+1);
552 conn = make_connection(service,password_blob,dev,req->vuid,&nt_status);
553 req->conn = conn;
555 data_blob_clear_free(&password_blob);
557 if (!conn) {
558 reply_nterror(req, nt_status);
559 END_PROFILE(SMBtcon);
560 return;
563 reply_outbuf(req, 2, 0);
564 SSVAL(req->outbuf,smb_vwv0,max_recv);
565 SSVAL(req->outbuf,smb_vwv1,conn->cnum);
566 SSVAL(req->outbuf,smb_tid,conn->cnum);
568 DEBUG(3,("tcon service=%s cnum=%d\n",
569 service, conn->cnum));
571 END_PROFILE(SMBtcon);
572 return;
575 /****************************************************************************
576 Reply to a tcon and X.
577 conn POINTER CAN BE NULL HERE !
578 ****************************************************************************/
580 void reply_tcon_and_X(struct smb_request *req)
582 connection_struct *conn = req->conn;
583 const char *service = NULL;
584 DATA_BLOB password;
585 TALLOC_CTX *ctx = talloc_tos();
586 /* what the cleint thinks the device is */
587 char *client_devicetype = NULL;
588 /* what the server tells the client the share represents */
589 const char *server_devicetype;
590 NTSTATUS nt_status;
591 int passlen;
592 char *path = NULL;
593 const char *p, *q;
594 uint16 tcon_flags;
596 START_PROFILE(SMBtconX);
598 if (req->wct < 4) {
599 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
600 END_PROFILE(SMBtconX);
601 return;
604 passlen = SVAL(req->vwv+3, 0);
605 tcon_flags = SVAL(req->vwv+2, 0);
607 /* we might have to close an old one */
608 if ((tcon_flags & 0x1) && conn) {
609 close_cnum(conn,req->vuid);
610 req->conn = NULL;
611 conn = NULL;
614 if ((passlen > MAX_PASS_LEN) || (passlen >= req->buflen)) {
615 reply_doserror(req, ERRDOS, ERRbuftoosmall);
616 END_PROFILE(SMBtconX);
617 return;
620 if (global_encrypted_passwords_negotiated) {
621 password = data_blob_talloc(talloc_tos(), req->buf, passlen);
622 if (lp_security() == SEC_SHARE) {
624 * Security = share always has a pad byte
625 * after the password.
627 p = (const char *)req->buf + passlen + 1;
628 } else {
629 p = (const char *)req->buf + passlen;
631 } else {
632 password = data_blob_talloc(talloc_tos(), req->buf, passlen+1);
633 /* Ensure correct termination */
634 password.data[passlen]=0;
635 p = (const char *)req->buf + passlen + 1;
638 p += srvstr_pull_req_talloc(ctx, req, &path, p, STR_TERMINATE);
640 if (path == NULL) {
641 data_blob_clear_free(&password);
642 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
643 END_PROFILE(SMBtconX);
644 return;
648 * the service name can be either: \\server\share
649 * or share directly like on the DELL PowerVault 705
651 if (*path=='\\') {
652 q = strchr_m(path+2,'\\');
653 if (!q) {
654 data_blob_clear_free(&password);
655 reply_doserror(req, ERRDOS, ERRnosuchshare);
656 END_PROFILE(SMBtconX);
657 return;
659 service = q+1;
660 } else {
661 service = path;
664 p += srvstr_pull_talloc(ctx, req->inbuf, req->flags2,
665 &client_devicetype, p,
666 MIN(6, smbreq_bufrem(req, p)), STR_ASCII);
668 if (client_devicetype == NULL) {
669 data_blob_clear_free(&password);
670 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
671 END_PROFILE(SMBtconX);
672 return;
675 DEBUG(4,("Client requested device type [%s] for share [%s]\n", client_devicetype, service));
677 conn = make_connection(service, password, client_devicetype,
678 req->vuid, &nt_status);
679 req->conn =conn;
681 data_blob_clear_free(&password);
683 if (!conn) {
684 reply_nterror(req, nt_status);
685 END_PROFILE(SMBtconX);
686 return;
689 if ( IS_IPC(conn) )
690 server_devicetype = "IPC";
691 else if ( IS_PRINT(conn) )
692 server_devicetype = "LPT1:";
693 else
694 server_devicetype = "A:";
696 if (Protocol < PROTOCOL_NT1) {
697 reply_outbuf(req, 2, 0);
698 if (message_push_string(&req->outbuf, server_devicetype,
699 STR_TERMINATE|STR_ASCII) == -1) {
700 reply_nterror(req, NT_STATUS_NO_MEMORY);
701 END_PROFILE(SMBtconX);
702 return;
704 } else {
705 /* NT sets the fstype of IPC$ to the null string */
706 const char *fstype = IS_IPC(conn) ? "" : lp_fstype(SNUM(conn));
708 if (tcon_flags & TCONX_FLAG_EXTENDED_RESPONSE) {
709 /* Return permissions. */
710 uint32 perm1 = 0;
711 uint32 perm2 = 0;
713 reply_outbuf(req, 7, 0);
715 if (IS_IPC(conn)) {
716 perm1 = FILE_ALL_ACCESS;
717 perm2 = FILE_ALL_ACCESS;
718 } else {
719 perm1 = CAN_WRITE(conn) ?
720 SHARE_ALL_ACCESS :
721 SHARE_READ_ONLY;
724 SIVAL(req->outbuf, smb_vwv3, perm1);
725 SIVAL(req->outbuf, smb_vwv5, perm2);
726 } else {
727 reply_outbuf(req, 3, 0);
730 if ((message_push_string(&req->outbuf, server_devicetype,
731 STR_TERMINATE|STR_ASCII) == -1)
732 || (message_push_string(&req->outbuf, fstype,
733 STR_TERMINATE) == -1)) {
734 reply_nterror(req, NT_STATUS_NO_MEMORY);
735 END_PROFILE(SMBtconX);
736 return;
739 /* what does setting this bit do? It is set by NT4 and
740 may affect the ability to autorun mounted cdroms */
741 SSVAL(req->outbuf, smb_vwv2, SMB_SUPPORT_SEARCH_BITS|
742 (lp_csc_policy(SNUM(conn)) << 2));
744 if (lp_msdfs_root(SNUM(conn)) && lp_host_msdfs()) {
745 DEBUG(2,("Serving %s as a Dfs root\n",
746 lp_servicename(SNUM(conn)) ));
747 SSVAL(req->outbuf, smb_vwv2,
748 SMB_SHARE_IN_DFS | SVAL(req->outbuf, smb_vwv2));
753 DEBUG(3,("tconX service=%s \n",
754 service));
756 /* set the incoming and outgoing tid to the just created one */
757 SSVAL(req->inbuf,smb_tid,conn->cnum);
758 SSVAL(req->outbuf,smb_tid,conn->cnum);
760 END_PROFILE(SMBtconX);
762 req->tid = conn->cnum;
763 chain_reply(req);
764 return;
767 /****************************************************************************
768 Reply to an unknown type.
769 ****************************************************************************/
771 void reply_unknown_new(struct smb_request *req, uint8 type)
773 DEBUG(0, ("unknown command type (%s): type=%d (0x%X)\n",
774 smb_fn_name(type), type, type));
775 reply_doserror(req, ERRSRV, ERRunknownsmb);
776 return;
779 /****************************************************************************
780 Reply to an ioctl.
781 conn POINTER CAN BE NULL HERE !
782 ****************************************************************************/
784 void reply_ioctl(struct smb_request *req)
786 connection_struct *conn = req->conn;
787 uint16 device;
788 uint16 function;
789 uint32 ioctl_code;
790 int replysize;
791 char *p;
793 START_PROFILE(SMBioctl);
795 if (req->wct < 3) {
796 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
797 END_PROFILE(SMBioctl);
798 return;
801 device = SVAL(req->vwv+1, 0);
802 function = SVAL(req->vwv+2, 0);
803 ioctl_code = (device << 16) + function;
805 DEBUG(4, ("Received IOCTL (code 0x%x)\n", ioctl_code));
807 switch (ioctl_code) {
808 case IOCTL_QUERY_JOB_INFO:
809 replysize = 32;
810 break;
811 default:
812 reply_doserror(req, ERRSRV, ERRnosupport);
813 END_PROFILE(SMBioctl);
814 return;
817 reply_outbuf(req, 8, replysize+1);
818 SSVAL(req->outbuf,smb_vwv1,replysize); /* Total data bytes returned */
819 SSVAL(req->outbuf,smb_vwv5,replysize); /* Data bytes this buffer */
820 SSVAL(req->outbuf,smb_vwv6,52); /* Offset to data */
821 p = smb_buf(req->outbuf);
822 memset(p, '\0', replysize+1); /* valgrind-safe. */
823 p += 1; /* Allow for alignment */
825 switch (ioctl_code) {
826 case IOCTL_QUERY_JOB_INFO:
828 files_struct *fsp = file_fsp(
829 req, SVAL(req->vwv+0, 0));
830 if (!fsp) {
831 reply_doserror(req, ERRDOS, ERRbadfid);
832 END_PROFILE(SMBioctl);
833 return;
835 SSVAL(p,0,fsp->rap_print_jobid); /* Job number */
836 srvstr_push((char *)req->outbuf, req->flags2, p+2,
837 global_myname(), 15,
838 STR_TERMINATE|STR_ASCII);
839 if (conn) {
840 srvstr_push((char *)req->outbuf, req->flags2,
841 p+18, lp_servicename(SNUM(conn)),
842 13, STR_TERMINATE|STR_ASCII);
843 } else {
844 memset(p+18, 0, 13);
846 break;
850 END_PROFILE(SMBioctl);
851 return;
854 /****************************************************************************
855 Strange checkpath NTSTATUS mapping.
856 ****************************************************************************/
858 static NTSTATUS map_checkpath_error(uint16_t flags2, NTSTATUS status)
860 /* Strange DOS error code semantics only for checkpath... */
861 if (!(flags2 & FLAGS2_32_BIT_ERROR_CODES)) {
862 if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_INVALID,status)) {
863 /* We need to map to ERRbadpath */
864 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
867 return status;
870 /****************************************************************************
871 Reply to a checkpath.
872 ****************************************************************************/
874 void reply_checkpath(struct smb_request *req)
876 connection_struct *conn = req->conn;
877 char *name = NULL;
878 SMB_STRUCT_STAT sbuf;
879 NTSTATUS status;
880 TALLOC_CTX *ctx = talloc_tos();
882 START_PROFILE(SMBcheckpath);
884 srvstr_get_path_req(ctx, req, &name, (const char *)req->buf + 1,
885 STR_TERMINATE, &status);
887 if (!NT_STATUS_IS_OK(status)) {
888 status = map_checkpath_error(req->flags2, status);
889 reply_nterror(req, status);
890 END_PROFILE(SMBcheckpath);
891 return;
894 status = resolve_dfspath(ctx, conn,
895 req->flags2 & FLAGS2_DFS_PATHNAMES,
896 name,
897 &name);
898 if (!NT_STATUS_IS_OK(status)) {
899 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
900 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
901 ERRSRV, ERRbadpath);
902 END_PROFILE(SMBcheckpath);
903 return;
905 goto path_err;
908 DEBUG(3,("reply_checkpath %s mode=%d\n", name, (int)SVAL(req->vwv+0, 0)));
910 status = unix_convert(ctx, conn, name, False, &name, NULL, &sbuf);
911 if (!NT_STATUS_IS_OK(status)) {
912 goto path_err;
915 status = check_name(conn, name);
916 if (!NT_STATUS_IS_OK(status)) {
917 DEBUG(3,("reply_checkpath: check_name of %s failed (%s)\n",name,nt_errstr(status)));
918 goto path_err;
921 if (!VALID_STAT(sbuf) && (SMB_VFS_STAT(conn,name,&sbuf) != 0)) {
922 DEBUG(3,("reply_checkpath: stat of %s failed (%s)\n",name,strerror(errno)));
923 status = map_nt_error_from_unix(errno);
924 goto path_err;
927 if (!S_ISDIR(sbuf.st_mode)) {
928 reply_botherror(req, NT_STATUS_NOT_A_DIRECTORY,
929 ERRDOS, ERRbadpath);
930 END_PROFILE(SMBcheckpath);
931 return;
934 reply_outbuf(req, 0, 0);
936 END_PROFILE(SMBcheckpath);
937 return;
939 path_err:
941 END_PROFILE(SMBcheckpath);
943 /* We special case this - as when a Windows machine
944 is parsing a path is steps through the components
945 one at a time - if a component fails it expects
946 ERRbadpath, not ERRbadfile.
948 status = map_checkpath_error(req->flags2, status);
949 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
951 * Windows returns different error codes if
952 * the parent directory is valid but not the
953 * last component - it returns NT_STATUS_OBJECT_NAME_NOT_FOUND
954 * for that case and NT_STATUS_OBJECT_PATH_NOT_FOUND
955 * if the path is invalid.
957 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
958 ERRDOS, ERRbadpath);
959 return;
962 reply_nterror(req, status);
965 /****************************************************************************
966 Reply to a getatr.
967 ****************************************************************************/
969 void reply_getatr(struct smb_request *req)
971 connection_struct *conn = req->conn;
972 char *fname = NULL;
973 SMB_STRUCT_STAT sbuf;
974 int mode=0;
975 SMB_OFF_T size=0;
976 time_t mtime=0;
977 const char *p;
978 NTSTATUS status;
979 TALLOC_CTX *ctx = talloc_tos();
981 START_PROFILE(SMBgetatr);
983 p = (const char *)req->buf + 1;
984 p += srvstr_get_path_req(ctx, req, &fname, p, STR_TERMINATE, &status);
985 if (!NT_STATUS_IS_OK(status)) {
986 reply_nterror(req, status);
987 END_PROFILE(SMBgetatr);
988 return;
991 status = resolve_dfspath(ctx, conn,
992 req->flags2 & FLAGS2_DFS_PATHNAMES,
993 fname,
994 &fname);
995 if (!NT_STATUS_IS_OK(status)) {
996 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
997 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
998 ERRSRV, ERRbadpath);
999 END_PROFILE(SMBgetatr);
1000 return;
1002 reply_nterror(req, status);
1003 END_PROFILE(SMBgetatr);
1004 return;
1007 /* dos smetimes asks for a stat of "" - it returns a "hidden directory"
1008 under WfWg - weird! */
1009 if (*fname == '\0') {
1010 mode = aHIDDEN | aDIR;
1011 if (!CAN_WRITE(conn)) {
1012 mode |= aRONLY;
1014 size = 0;
1015 mtime = 0;
1016 } else {
1017 status = unix_convert(ctx, conn, fname, False, &fname, NULL,&sbuf);
1018 if (!NT_STATUS_IS_OK(status)) {
1019 reply_nterror(req, status);
1020 END_PROFILE(SMBgetatr);
1021 return;
1023 status = check_name(conn, fname);
1024 if (!NT_STATUS_IS_OK(status)) {
1025 DEBUG(3,("reply_getatr: check_name of %s failed (%s)\n",fname,nt_errstr(status)));
1026 reply_nterror(req, status);
1027 END_PROFILE(SMBgetatr);
1028 return;
1030 if (!VALID_STAT(sbuf) && (SMB_VFS_STAT(conn,fname,&sbuf) != 0)) {
1031 DEBUG(3,("reply_getatr: stat of %s failed (%s)\n",fname,strerror(errno)));
1032 reply_unixerror(req, ERRDOS,ERRbadfile);
1033 END_PROFILE(SMBgetatr);
1034 return;
1037 mode = dos_mode(conn,fname,&sbuf);
1038 size = sbuf.st_size;
1039 mtime = sbuf.st_mtime;
1040 if (mode & aDIR) {
1041 size = 0;
1045 reply_outbuf(req, 10, 0);
1047 SSVAL(req->outbuf,smb_vwv0,mode);
1048 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1049 srv_put_dos_date3((char *)req->outbuf,smb_vwv1,mtime & ~1);
1050 } else {
1051 srv_put_dos_date3((char *)req->outbuf,smb_vwv1,mtime);
1053 SIVAL(req->outbuf,smb_vwv3,(uint32)size);
1055 if (Protocol >= PROTOCOL_NT1) {
1056 SSVAL(req->outbuf, smb_flg2,
1057 SVAL(req->outbuf, smb_flg2) | FLAGS2_IS_LONG_NAME);
1060 DEBUG(3,("reply_getatr: name=%s mode=%d size=%u\n", fname, mode, (unsigned int)size ) );
1062 END_PROFILE(SMBgetatr);
1063 return;
1066 /****************************************************************************
1067 Reply to a setatr.
1068 ****************************************************************************/
1070 void reply_setatr(struct smb_request *req)
1072 struct smb_file_time ft;
1073 connection_struct *conn = req->conn;
1074 char *fname = NULL;
1075 int mode;
1076 time_t mtime;
1077 SMB_STRUCT_STAT sbuf;
1078 const char *p;
1079 NTSTATUS status;
1080 TALLOC_CTX *ctx = talloc_tos();
1082 START_PROFILE(SMBsetatr);
1084 ZERO_STRUCT(ft);
1086 if (req->wct < 2) {
1087 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1088 return;
1091 p = (const char *)req->buf + 1;
1092 p += srvstr_get_path_req(ctx, req, &fname, p, STR_TERMINATE, &status);
1093 if (!NT_STATUS_IS_OK(status)) {
1094 reply_nterror(req, status);
1095 END_PROFILE(SMBsetatr);
1096 return;
1099 status = resolve_dfspath(ctx, conn,
1100 req->flags2 & FLAGS2_DFS_PATHNAMES,
1101 fname,
1102 &fname);
1103 if (!NT_STATUS_IS_OK(status)) {
1104 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1105 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1106 ERRSRV, ERRbadpath);
1107 END_PROFILE(SMBsetatr);
1108 return;
1110 reply_nterror(req, status);
1111 END_PROFILE(SMBsetatr);
1112 return;
1115 status = unix_convert(ctx, conn, fname, False, &fname, NULL, &sbuf);
1116 if (!NT_STATUS_IS_OK(status)) {
1117 reply_nterror(req, status);
1118 END_PROFILE(SMBsetatr);
1119 return;
1122 status = check_name(conn, fname);
1123 if (!NT_STATUS_IS_OK(status)) {
1124 reply_nterror(req, status);
1125 END_PROFILE(SMBsetatr);
1126 return;
1129 if (fname[0] == '.' && fname[1] == '\0') {
1131 * Not sure here is the right place to catch this
1132 * condition. Might be moved to somewhere else later -- vl
1134 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1135 END_PROFILE(SMBsetatr);
1136 return;
1139 mode = SVAL(req->vwv+0, 0);
1140 mtime = srv_make_unix_date3(req->vwv+1);
1142 ft.mtime = convert_time_t_to_timespec(mtime);
1143 status = smb_set_file_time(conn, NULL, fname,
1144 &sbuf, &ft, true);
1145 if (!NT_STATUS_IS_OK(status)) {
1146 reply_unixerror(req, ERRDOS, ERRnoaccess);
1147 END_PROFILE(SMBsetatr);
1148 return;
1151 if (mode != FILE_ATTRIBUTE_NORMAL) {
1152 if (VALID_STAT_OF_DIR(sbuf))
1153 mode |= aDIR;
1154 else
1155 mode &= ~aDIR;
1157 if (file_set_dosmode(conn,fname,mode,&sbuf,NULL,false) != 0) {
1158 reply_unixerror(req, ERRDOS, ERRnoaccess);
1159 END_PROFILE(SMBsetatr);
1160 return;
1164 reply_outbuf(req, 0, 0);
1166 DEBUG( 3, ( "setatr name=%s mode=%d\n", fname, mode ) );
1168 END_PROFILE(SMBsetatr);
1169 return;
1172 /****************************************************************************
1173 Reply to a dskattr.
1174 ****************************************************************************/
1176 void reply_dskattr(struct smb_request *req)
1178 connection_struct *conn = req->conn;
1179 uint64_t dfree,dsize,bsize;
1180 START_PROFILE(SMBdskattr);
1182 if (get_dfree_info(conn,".",True,&bsize,&dfree,&dsize) == (uint64_t)-1) {
1183 reply_unixerror(req, ERRHRD, ERRgeneral);
1184 END_PROFILE(SMBdskattr);
1185 return;
1188 reply_outbuf(req, 5, 0);
1190 if (Protocol <= PROTOCOL_LANMAN2) {
1191 double total_space, free_space;
1192 /* we need to scale this to a number that DOS6 can handle. We
1193 use floating point so we can handle large drives on systems
1194 that don't have 64 bit integers
1196 we end up displaying a maximum of 2G to DOS systems
1198 total_space = dsize * (double)bsize;
1199 free_space = dfree * (double)bsize;
1201 dsize = (uint64_t)((total_space+63*512) / (64*512));
1202 dfree = (uint64_t)((free_space+63*512) / (64*512));
1204 if (dsize > 0xFFFF) dsize = 0xFFFF;
1205 if (dfree > 0xFFFF) dfree = 0xFFFF;
1207 SSVAL(req->outbuf,smb_vwv0,dsize);
1208 SSVAL(req->outbuf,smb_vwv1,64); /* this must be 64 for dos systems */
1209 SSVAL(req->outbuf,smb_vwv2,512); /* and this must be 512 */
1210 SSVAL(req->outbuf,smb_vwv3,dfree);
1211 } else {
1212 SSVAL(req->outbuf,smb_vwv0,dsize);
1213 SSVAL(req->outbuf,smb_vwv1,bsize/512);
1214 SSVAL(req->outbuf,smb_vwv2,512);
1215 SSVAL(req->outbuf,smb_vwv3,dfree);
1218 DEBUG(3,("dskattr dfree=%d\n", (unsigned int)dfree));
1220 END_PROFILE(SMBdskattr);
1221 return;
1224 /****************************************************************************
1225 Reply to a search.
1226 Can be called from SMBsearch, SMBffirst or SMBfunique.
1227 ****************************************************************************/
1229 void reply_search(struct smb_request *req)
1231 connection_struct *conn = req->conn;
1232 const char *mask = NULL;
1233 char *directory = NULL;
1234 char *fname = NULL;
1235 SMB_OFF_T size;
1236 uint32 mode;
1237 time_t date;
1238 uint32 dirtype;
1239 unsigned int numentries = 0;
1240 unsigned int maxentries = 0;
1241 bool finished = False;
1242 const char *p;
1243 int status_len;
1244 char *path = NULL;
1245 char status[21];
1246 int dptr_num= -1;
1247 bool check_descend = False;
1248 bool expect_close = False;
1249 NTSTATUS nt_status;
1250 bool mask_contains_wcard = False;
1251 bool allow_long_path_components = (req->flags2 & FLAGS2_LONG_PATH_COMPONENTS) ? True : False;
1252 TALLOC_CTX *ctx = talloc_tos();
1253 bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
1255 START_PROFILE(SMBsearch);
1257 if (req->wct < 2) {
1258 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1259 END_PROFILE(SMBsearch);
1260 return;
1263 if (lp_posix_pathnames()) {
1264 reply_unknown_new(req, req->cmd);
1265 END_PROFILE(SMBsearch);
1266 return;
1269 /* If we were called as SMBffirst then we must expect close. */
1270 if(req->cmd == SMBffirst) {
1271 expect_close = True;
1274 reply_outbuf(req, 1, 3);
1275 maxentries = SVAL(req->vwv+0, 0);
1276 dirtype = SVAL(req->vwv+1, 0);
1277 p = (const char *)req->buf + 1;
1278 p += srvstr_get_path_req_wcard(ctx, req, &path, p, STR_TERMINATE,
1279 &nt_status, &mask_contains_wcard);
1280 if (!NT_STATUS_IS_OK(nt_status)) {
1281 reply_nterror(req, nt_status);
1282 END_PROFILE(SMBsearch);
1283 return;
1286 nt_status = resolve_dfspath_wcard(ctx, conn,
1287 req->flags2 & FLAGS2_DFS_PATHNAMES,
1288 path,
1289 &path,
1290 &mask_contains_wcard);
1291 if (!NT_STATUS_IS_OK(nt_status)) {
1292 if (NT_STATUS_EQUAL(nt_status,NT_STATUS_PATH_NOT_COVERED)) {
1293 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1294 ERRSRV, ERRbadpath);
1295 END_PROFILE(SMBsearch);
1296 return;
1298 reply_nterror(req, nt_status);
1299 END_PROFILE(SMBsearch);
1300 return;
1303 p++;
1304 status_len = SVAL(p, 0);
1305 p += 2;
1307 /* dirtype &= ~aDIR; */
1309 if (status_len == 0) {
1310 SMB_STRUCT_STAT sbuf;
1312 nt_status = unix_convert(ctx, conn, path, True,
1313 &directory, NULL, &sbuf);
1314 if (!NT_STATUS_IS_OK(nt_status)) {
1315 reply_nterror(req, nt_status);
1316 END_PROFILE(SMBsearch);
1317 return;
1320 nt_status = check_name(conn, directory);
1321 if (!NT_STATUS_IS_OK(nt_status)) {
1322 reply_nterror(req, nt_status);
1323 END_PROFILE(SMBsearch);
1324 return;
1327 p = strrchr_m(directory,'/');
1328 if ((p != NULL) && (*directory != '/')) {
1329 mask = p + 1;
1330 directory = talloc_strndup(ctx, directory,
1331 PTR_DIFF(p, directory));
1332 } else {
1333 mask = directory;
1334 directory = talloc_strdup(ctx,".");
1337 if (!directory) {
1338 reply_nterror(req, NT_STATUS_NO_MEMORY);
1339 END_PROFILE(SMBsearch);
1340 return;
1343 memset((char *)status,'\0',21);
1344 SCVAL(status,0,(dirtype & 0x1F));
1346 nt_status = dptr_create(conn,
1347 directory,
1348 True,
1349 expect_close,
1350 req->smbpid,
1351 mask,
1352 mask_contains_wcard,
1353 dirtype,
1354 &conn->dirptr);
1355 if (!NT_STATUS_IS_OK(nt_status)) {
1356 reply_nterror(req, nt_status);
1357 END_PROFILE(SMBsearch);
1358 return;
1360 dptr_num = dptr_dnum(conn->dirptr);
1361 } else {
1362 int status_dirtype;
1364 memcpy(status,p,21);
1365 status_dirtype = CVAL(status,0) & 0x1F;
1366 if (status_dirtype != (dirtype & 0x1F)) {
1367 dirtype = status_dirtype;
1370 conn->dirptr = dptr_fetch(status+12,&dptr_num);
1371 if (!conn->dirptr) {
1372 goto SearchEmpty;
1374 string_set(&conn->dirpath,dptr_path(dptr_num));
1375 mask = dptr_wcard(dptr_num);
1376 if (!mask) {
1377 goto SearchEmpty;
1380 * For a 'continue' search we have no string. So
1381 * check from the initial saved string.
1383 mask_contains_wcard = ms_has_wild(mask);
1384 dirtype = dptr_attr(dptr_num);
1387 DEBUG(4,("dptr_num is %d\n",dptr_num));
1389 /* Initialize per SMBsearch/SMBffirst/SMBfunique operation data */
1390 dptr_init_search_op(conn->dirptr);
1392 if ((dirtype&0x1F) == aVOLID) {
1393 char buf[DIR_STRUCT_SIZE];
1394 memcpy(buf,status,21);
1395 if (!make_dir_struct(ctx,buf,"???????????",volume_label(SNUM(conn)),
1396 0,aVOLID,0,!allow_long_path_components)) {
1397 reply_nterror(req, NT_STATUS_NO_MEMORY);
1398 END_PROFILE(SMBsearch);
1399 return;
1401 dptr_fill(buf+12,dptr_num);
1402 if (dptr_zero(buf+12) && (status_len==0)) {
1403 numentries = 1;
1404 } else {
1405 numentries = 0;
1407 if (message_push_blob(&req->outbuf,
1408 data_blob_const(buf, sizeof(buf)))
1409 == -1) {
1410 reply_nterror(req, NT_STATUS_NO_MEMORY);
1411 END_PROFILE(SMBsearch);
1412 return;
1414 } else {
1415 unsigned int i;
1416 maxentries = MIN(
1417 maxentries,
1418 ((BUFFER_SIZE -
1419 ((uint8 *)smb_buf(req->outbuf) + 3 - req->outbuf))
1420 /DIR_STRUCT_SIZE));
1422 DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
1423 conn->dirpath,lp_dontdescend(SNUM(conn))));
1424 if (in_list(conn->dirpath, lp_dontdescend(SNUM(conn)),True)) {
1425 check_descend = True;
1428 for (i=numentries;(i<maxentries) && !finished;i++) {
1429 finished = !get_dir_entry(ctx,
1430 conn,
1431 mask,
1432 dirtype,
1433 &fname,
1434 &size,
1435 &mode,
1436 &date,
1437 check_descend,
1438 ask_sharemode);
1439 if (!finished) {
1440 char buf[DIR_STRUCT_SIZE];
1441 memcpy(buf,status,21);
1442 if (!make_dir_struct(ctx,
1443 buf,
1444 mask,
1445 fname,
1446 size,
1447 mode,
1448 date,
1449 !allow_long_path_components)) {
1450 reply_nterror(req, NT_STATUS_NO_MEMORY);
1451 END_PROFILE(SMBsearch);
1452 return;
1454 if (!dptr_fill(buf+12,dptr_num)) {
1455 break;
1457 if (message_push_blob(&req->outbuf,
1458 data_blob_const(buf, sizeof(buf)))
1459 == -1) {
1460 reply_nterror(req, NT_STATUS_NO_MEMORY);
1461 END_PROFILE(SMBsearch);
1462 return;
1464 numentries++;
1469 SearchEmpty:
1471 /* If we were called as SMBffirst with smb_search_id == NULL
1472 and no entries were found then return error and close dirptr
1473 (X/Open spec) */
1475 if (numentries == 0) {
1476 dptr_close(&dptr_num);
1477 } else if(expect_close && status_len == 0) {
1478 /* Close the dptr - we know it's gone */
1479 dptr_close(&dptr_num);
1482 /* If we were called as SMBfunique, then we can close the dirptr now ! */
1483 if(dptr_num >= 0 && req->cmd == SMBfunique) {
1484 dptr_close(&dptr_num);
1487 if ((numentries == 0) && !mask_contains_wcard) {
1488 reply_botherror(req, STATUS_NO_MORE_FILES, ERRDOS, ERRnofiles);
1489 END_PROFILE(SMBsearch);
1490 return;
1493 SSVAL(req->outbuf,smb_vwv0,numentries);
1494 SSVAL(req->outbuf,smb_vwv1,3 + numentries * DIR_STRUCT_SIZE);
1495 SCVAL(smb_buf(req->outbuf),0,5);
1496 SSVAL(smb_buf(req->outbuf),1,numentries*DIR_STRUCT_SIZE);
1498 /* The replies here are never long name. */
1499 SSVAL(req->outbuf, smb_flg2,
1500 SVAL(req->outbuf, smb_flg2) & (~FLAGS2_IS_LONG_NAME));
1501 if (!allow_long_path_components) {
1502 SSVAL(req->outbuf, smb_flg2,
1503 SVAL(req->outbuf, smb_flg2)
1504 & (~FLAGS2_LONG_PATH_COMPONENTS));
1507 /* This SMB *always* returns ASCII names. Remove the unicode bit in flags2. */
1508 SSVAL(req->outbuf, smb_flg2,
1509 (SVAL(req->outbuf, smb_flg2) & (~FLAGS2_UNICODE_STRINGS)));
1511 if (!directory) {
1512 directory = dptr_path(dptr_num);
1515 DEBUG(4,("%s mask=%s path=%s dtype=%d nument=%u of %u\n",
1516 smb_fn_name(req->cmd),
1517 mask,
1518 directory ? directory : "./",
1519 dirtype,
1520 numentries,
1521 maxentries ));
1523 END_PROFILE(SMBsearch);
1524 return;
1527 /****************************************************************************
1528 Reply to a fclose (stop directory search).
1529 ****************************************************************************/
1531 void reply_fclose(struct smb_request *req)
1533 int status_len;
1534 char status[21];
1535 int dptr_num= -2;
1536 const char *p;
1537 char *path = NULL;
1538 NTSTATUS err;
1539 bool path_contains_wcard = False;
1540 TALLOC_CTX *ctx = talloc_tos();
1542 START_PROFILE(SMBfclose);
1544 if (lp_posix_pathnames()) {
1545 reply_unknown_new(req, req->cmd);
1546 END_PROFILE(SMBfclose);
1547 return;
1550 p = (const char *)req->buf + 1;
1551 p += srvstr_get_path_req_wcard(ctx, req, &path, p, STR_TERMINATE,
1552 &err, &path_contains_wcard);
1553 if (!NT_STATUS_IS_OK(err)) {
1554 reply_nterror(req, err);
1555 END_PROFILE(SMBfclose);
1556 return;
1558 p++;
1559 status_len = SVAL(p,0);
1560 p += 2;
1562 if (status_len == 0) {
1563 reply_doserror(req, ERRSRV, ERRsrverror);
1564 END_PROFILE(SMBfclose);
1565 return;
1568 memcpy(status,p,21);
1570 if(dptr_fetch(status+12,&dptr_num)) {
1571 /* Close the dptr - we know it's gone */
1572 dptr_close(&dptr_num);
1575 reply_outbuf(req, 1, 0);
1576 SSVAL(req->outbuf,smb_vwv0,0);
1578 DEBUG(3,("search close\n"));
1580 END_PROFILE(SMBfclose);
1581 return;
1584 /****************************************************************************
1585 Reply to an open.
1586 ****************************************************************************/
1588 void reply_open(struct smb_request *req)
1590 connection_struct *conn = req->conn;
1591 char *fname = NULL;
1592 uint32 fattr=0;
1593 SMB_OFF_T size = 0;
1594 time_t mtime=0;
1595 int info;
1596 SMB_STRUCT_STAT sbuf;
1597 files_struct *fsp;
1598 int oplock_request;
1599 int deny_mode;
1600 uint32 dos_attr;
1601 uint32 access_mask;
1602 uint32 share_mode;
1603 uint32 create_disposition;
1604 uint32 create_options = 0;
1605 NTSTATUS status;
1606 TALLOC_CTX *ctx = talloc_tos();
1608 START_PROFILE(SMBopen);
1610 SET_STAT_INVALID(sbuf);
1612 if (req->wct < 2) {
1613 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1614 END_PROFILE(SMBopen);
1615 return;
1618 oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
1619 deny_mode = SVAL(req->vwv+0, 0);
1620 dos_attr = SVAL(req->vwv+1, 0);
1622 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf+1,
1623 STR_TERMINATE, &status);
1624 if (!NT_STATUS_IS_OK(status)) {
1625 reply_nterror(req, status);
1626 END_PROFILE(SMBopen);
1627 return;
1630 if (!map_open_params_to_ntcreate(
1631 fname, deny_mode, OPENX_FILE_EXISTS_OPEN, &access_mask,
1632 &share_mode, &create_disposition, &create_options)) {
1633 reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRbadaccess));
1634 END_PROFILE(SMBopen);
1635 return;
1638 status = SMB_VFS_CREATE_FILE(
1639 conn, /* conn */
1640 req, /* req */
1641 0, /* root_dir_fid */
1642 fname, /* fname */
1643 CFF_DOS_PATH, /* create_file_flags */
1644 access_mask, /* access_mask */
1645 share_mode, /* share_access */
1646 create_disposition, /* create_disposition*/
1647 create_options, /* create_options */
1648 dos_attr, /* file_attributes */
1649 oplock_request, /* oplock_request */
1650 0, /* allocation_size */
1651 NULL, /* sd */
1652 NULL, /* ea_list */
1653 &fsp, /* result */
1654 &info, /* pinfo */
1655 &sbuf); /* psbuf */
1657 if (!NT_STATUS_IS_OK(status)) {
1658 if (open_was_deferred(req->mid)) {
1659 /* We have re-scheduled this call. */
1660 END_PROFILE(SMBopen);
1661 return;
1663 reply_openerror(req, status);
1664 END_PROFILE(SMBopen);
1665 return;
1668 size = sbuf.st_size;
1669 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
1670 mtime = sbuf.st_mtime;
1672 if (fattr & aDIR) {
1673 DEBUG(3,("attempt to open a directory %s\n",fsp->fsp_name));
1674 close_file(req, fsp, ERROR_CLOSE);
1675 reply_doserror(req, ERRDOS,ERRnoaccess);
1676 END_PROFILE(SMBopen);
1677 return;
1680 reply_outbuf(req, 7, 0);
1681 SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
1682 SSVAL(req->outbuf,smb_vwv1,fattr);
1683 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1684 srv_put_dos_date3((char *)req->outbuf,smb_vwv2,mtime & ~1);
1685 } else {
1686 srv_put_dos_date3((char *)req->outbuf,smb_vwv2,mtime);
1688 SIVAL(req->outbuf,smb_vwv4,(uint32)size);
1689 SSVAL(req->outbuf,smb_vwv6,deny_mode);
1691 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1692 SCVAL(req->outbuf,smb_flg,
1693 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1696 if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1697 SCVAL(req->outbuf,smb_flg,
1698 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1700 END_PROFILE(SMBopen);
1701 return;
1704 /****************************************************************************
1705 Reply to an open and X.
1706 ****************************************************************************/
1708 void reply_open_and_X(struct smb_request *req)
1710 connection_struct *conn = req->conn;
1711 char *fname = NULL;
1712 uint16 open_flags;
1713 int deny_mode;
1714 uint32 smb_attr;
1715 /* Breakout the oplock request bits so we can set the
1716 reply bits separately. */
1717 int ex_oplock_request;
1718 int core_oplock_request;
1719 int oplock_request;
1720 #if 0
1721 int smb_sattr = SVAL(req->vwv+4, 0);
1722 uint32 smb_time = make_unix_date3(req->vwv+6);
1723 #endif
1724 int smb_ofun;
1725 uint32 fattr=0;
1726 int mtime=0;
1727 SMB_STRUCT_STAT sbuf;
1728 int smb_action = 0;
1729 files_struct *fsp;
1730 NTSTATUS status;
1731 uint64_t allocation_size;
1732 ssize_t retval = -1;
1733 uint32 access_mask;
1734 uint32 share_mode;
1735 uint32 create_disposition;
1736 uint32 create_options = 0;
1737 TALLOC_CTX *ctx = talloc_tos();
1739 START_PROFILE(SMBopenX);
1741 if (req->wct < 15) {
1742 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1743 END_PROFILE(SMBopenX);
1744 return;
1747 SET_STAT_INVALID(sbuf);
1749 open_flags = SVAL(req->vwv+2, 0);
1750 deny_mode = SVAL(req->vwv+3, 0);
1751 smb_attr = SVAL(req->vwv+5, 0);
1752 ex_oplock_request = EXTENDED_OPLOCK_REQUEST(req->inbuf);
1753 core_oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
1754 oplock_request = ex_oplock_request | core_oplock_request;
1755 smb_ofun = SVAL(req->vwv+8, 0);
1756 allocation_size = (uint64_t)IVAL(req->vwv+9, 0);
1758 /* If it's an IPC, pass off the pipe handler. */
1759 if (IS_IPC(conn)) {
1760 if (lp_nt_pipe_support()) {
1761 reply_open_pipe_and_X(conn, req);
1762 } else {
1763 reply_doserror(req, ERRSRV, ERRaccess);
1765 END_PROFILE(SMBopenX);
1766 return;
1769 /* XXXX we need to handle passed times, sattr and flags */
1770 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
1771 STR_TERMINATE, &status);
1772 if (!NT_STATUS_IS_OK(status)) {
1773 reply_nterror(req, status);
1774 END_PROFILE(SMBopenX);
1775 return;
1778 if (!map_open_params_to_ntcreate(
1779 fname, deny_mode, smb_ofun, &access_mask,
1780 &share_mode, &create_disposition, &create_options)) {
1781 reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRbadaccess));
1782 END_PROFILE(SMBopenX);
1783 return;
1786 status = SMB_VFS_CREATE_FILE(
1787 conn, /* conn */
1788 req, /* req */
1789 0, /* root_dir_fid */
1790 fname, /* fname */
1791 CFF_DOS_PATH, /* create_file_flags */
1792 access_mask, /* access_mask */
1793 share_mode, /* share_access */
1794 create_disposition, /* create_disposition*/
1795 create_options, /* create_options */
1796 smb_attr, /* file_attributes */
1797 oplock_request, /* oplock_request */
1798 0, /* allocation_size */
1799 NULL, /* sd */
1800 NULL, /* ea_list */
1801 &fsp, /* result */
1802 &smb_action, /* pinfo */
1803 &sbuf); /* psbuf */
1805 if (!NT_STATUS_IS_OK(status)) {
1806 END_PROFILE(SMBopenX);
1807 if (open_was_deferred(req->mid)) {
1808 /* We have re-scheduled this call. */
1809 return;
1811 reply_openerror(req, status);
1812 return;
1815 /* Setting the "size" field in vwv9 and vwv10 causes the file to be set to this size,
1816 if the file is truncated or created. */
1817 if (((smb_action == FILE_WAS_CREATED) || (smb_action == FILE_WAS_OVERWRITTEN)) && allocation_size) {
1818 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1819 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1820 close_file(req, fsp, ERROR_CLOSE);
1821 reply_nterror(req, NT_STATUS_DISK_FULL);
1822 END_PROFILE(SMBopenX);
1823 return;
1825 retval = vfs_set_filelen(fsp, (SMB_OFF_T)allocation_size);
1826 if (retval < 0) {
1827 close_file(req, fsp, ERROR_CLOSE);
1828 reply_nterror(req, NT_STATUS_DISK_FULL);
1829 END_PROFILE(SMBopenX);
1830 return;
1832 sbuf.st_size = SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&sbuf);
1835 fattr = dos_mode(conn,fsp->fsp_name,&sbuf);
1836 mtime = sbuf.st_mtime;
1837 if (fattr & aDIR) {
1838 close_file(req, fsp, ERROR_CLOSE);
1839 reply_doserror(req, ERRDOS, ERRnoaccess);
1840 END_PROFILE(SMBopenX);
1841 return;
1844 /* If the caller set the extended oplock request bit
1845 and we granted one (by whatever means) - set the
1846 correct bit for extended oplock reply.
1849 if (ex_oplock_request && lp_fake_oplocks(SNUM(conn))) {
1850 smb_action |= EXTENDED_OPLOCK_GRANTED;
1853 if(ex_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1854 smb_action |= EXTENDED_OPLOCK_GRANTED;
1857 /* If the caller set the core oplock request bit
1858 and we granted one (by whatever means) - set the
1859 correct bit for core oplock reply.
1862 if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
1863 reply_outbuf(req, 19, 0);
1864 } else {
1865 reply_outbuf(req, 15, 0);
1868 if (core_oplock_request && lp_fake_oplocks(SNUM(conn))) {
1869 SCVAL(req->outbuf, smb_flg,
1870 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1873 if(core_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1874 SCVAL(req->outbuf, smb_flg,
1875 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1878 SSVAL(req->outbuf,smb_vwv2,fsp->fnum);
1879 SSVAL(req->outbuf,smb_vwv3,fattr);
1880 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1881 srv_put_dos_date3((char *)req->outbuf,smb_vwv4,mtime & ~1);
1882 } else {
1883 srv_put_dos_date3((char *)req->outbuf,smb_vwv4,mtime);
1885 SIVAL(req->outbuf,smb_vwv6,(uint32)sbuf.st_size);
1886 SSVAL(req->outbuf,smb_vwv8,GET_OPENX_MODE(deny_mode));
1887 SSVAL(req->outbuf,smb_vwv11,smb_action);
1889 if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
1890 SIVAL(req->outbuf, smb_vwv15, STD_RIGHT_ALL_ACCESS);
1893 END_PROFILE(SMBopenX);
1894 chain_reply(req);
1895 return;
1898 /****************************************************************************
1899 Reply to a SMBulogoffX.
1900 ****************************************************************************/
1902 void reply_ulogoffX(struct smb_request *req)
1904 user_struct *vuser;
1906 START_PROFILE(SMBulogoffX);
1908 vuser = get_valid_user_struct(req->vuid);
1910 if(vuser == NULL) {
1911 DEBUG(3,("ulogoff, vuser id %d does not map to user.\n",
1912 req->vuid));
1915 /* in user level security we are supposed to close any files
1916 open by this user */
1917 if ((vuser != NULL) && (lp_security() != SEC_SHARE)) {
1918 file_close_user(req->vuid);
1921 invalidate_vuid(req->vuid);
1923 reply_outbuf(req, 2, 0);
1925 DEBUG( 3, ( "ulogoffX vuid=%d\n", req->vuid ) );
1927 END_PROFILE(SMBulogoffX);
1928 req->vuid = UID_FIELD_INVALID;
1929 chain_reply(req);
1932 /****************************************************************************
1933 Reply to a mknew or a create.
1934 ****************************************************************************/
1936 void reply_mknew(struct smb_request *req)
1938 connection_struct *conn = req->conn;
1939 char *fname = NULL;
1940 uint32 fattr = 0;
1941 struct smb_file_time ft;
1942 files_struct *fsp;
1943 int oplock_request = 0;
1944 SMB_STRUCT_STAT sbuf;
1945 NTSTATUS status;
1946 uint32 access_mask = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
1947 uint32 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1948 uint32 create_disposition;
1949 uint32 create_options = 0;
1950 TALLOC_CTX *ctx = talloc_tos();
1952 START_PROFILE(SMBcreate);
1953 ZERO_STRUCT(ft);
1954 SET_STAT_INVALID(sbuf);
1956 if (req->wct < 3) {
1957 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1958 END_PROFILE(SMBcreate);
1959 return;
1962 fattr = SVAL(req->vwv+0, 0);
1963 oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
1965 /* mtime. */
1966 ft.mtime = convert_time_t_to_timespec(srv_make_unix_date3(req->vwv+1));
1968 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf + 1,
1969 STR_TERMINATE, &status);
1970 if (!NT_STATUS_IS_OK(status)) {
1971 reply_nterror(req, status);
1972 END_PROFILE(SMBcreate);
1973 return;
1976 if (fattr & aVOLID) {
1977 DEBUG(0,("Attempt to create file (%s) with volid set - "
1978 "please report this\n", fname));
1981 if(req->cmd == SMBmknew) {
1982 /* We should fail if file exists. */
1983 create_disposition = FILE_CREATE;
1984 } else {
1985 /* Create if file doesn't exist, truncate if it does. */
1986 create_disposition = FILE_OVERWRITE_IF;
1989 status = SMB_VFS_CREATE_FILE(
1990 conn, /* conn */
1991 req, /* req */
1992 0, /* root_dir_fid */
1993 fname, /* fname */
1994 CFF_DOS_PATH, /* create_file_flags */
1995 access_mask, /* access_mask */
1996 share_mode, /* share_access */
1997 create_disposition, /* create_disposition*/
1998 create_options, /* create_options */
1999 fattr, /* file_attributes */
2000 oplock_request, /* oplock_request */
2001 0, /* allocation_size */
2002 NULL, /* sd */
2003 NULL, /* ea_list */
2004 &fsp, /* result */
2005 NULL, /* pinfo */
2006 &sbuf); /* psbuf */
2008 if (!NT_STATUS_IS_OK(status)) {
2009 END_PROFILE(SMBcreate);
2010 if (open_was_deferred(req->mid)) {
2011 /* We have re-scheduled this call. */
2012 return;
2014 reply_openerror(req, status);
2015 return;
2018 ft.atime = get_atimespec(&sbuf); /* atime. */
2019 status = smb_set_file_time(conn, fsp, fsp->fsp_name, &sbuf, &ft, true);
2020 if (!NT_STATUS_IS_OK(status)) {
2021 END_PROFILE(SMBcreate);
2022 reply_openerror(req, status);
2023 return;
2026 reply_outbuf(req, 1, 0);
2027 SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
2029 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
2030 SCVAL(req->outbuf,smb_flg,
2031 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2034 if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
2035 SCVAL(req->outbuf,smb_flg,
2036 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2039 DEBUG( 2, ( "reply_mknew: file %s\n", fsp->fsp_name ) );
2040 DEBUG( 3, ( "reply_mknew %s fd=%d dmode=0x%x\n",
2041 fsp->fsp_name, fsp->fh->fd, (unsigned int)fattr ) );
2043 END_PROFILE(SMBcreate);
2044 return;
2047 /****************************************************************************
2048 Reply to a create temporary file.
2049 ****************************************************************************/
2051 void reply_ctemp(struct smb_request *req)
2053 connection_struct *conn = req->conn;
2054 char *fname = NULL;
2055 uint32 fattr;
2056 files_struct *fsp;
2057 int oplock_request;
2058 int tmpfd;
2059 SMB_STRUCT_STAT sbuf;
2060 char *s;
2061 NTSTATUS status;
2062 TALLOC_CTX *ctx = talloc_tos();
2064 START_PROFILE(SMBctemp);
2066 if (req->wct < 3) {
2067 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2068 END_PROFILE(SMBctemp);
2069 return;
2072 fattr = SVAL(req->vwv+0, 0);
2073 oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
2075 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf+1,
2076 STR_TERMINATE, &status);
2077 if (!NT_STATUS_IS_OK(status)) {
2078 reply_nterror(req, status);
2079 END_PROFILE(SMBctemp);
2080 return;
2082 if (*fname) {
2083 fname = talloc_asprintf(ctx,
2084 "%s/TMXXXXXX",
2085 fname);
2086 } else {
2087 fname = talloc_strdup(ctx, "TMXXXXXX");
2090 if (!fname) {
2091 reply_nterror(req, NT_STATUS_NO_MEMORY);
2092 END_PROFILE(SMBctemp);
2093 return;
2096 status = resolve_dfspath(ctx, conn,
2097 req->flags2 & FLAGS2_DFS_PATHNAMES,
2098 fname,
2099 &fname);
2100 if (!NT_STATUS_IS_OK(status)) {
2101 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2102 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2103 ERRSRV, ERRbadpath);
2104 END_PROFILE(SMBctemp);
2105 return;
2107 reply_nterror(req, status);
2108 END_PROFILE(SMBctemp);
2109 return;
2112 status = unix_convert(ctx, conn, fname, False, &fname, NULL, &sbuf);
2113 if (!NT_STATUS_IS_OK(status)) {
2114 reply_nterror(req, status);
2115 END_PROFILE(SMBctemp);
2116 return;
2119 status = check_name(conn, fname);
2120 if (!NT_STATUS_IS_OK(status)) {
2121 reply_nterror(req, status);
2122 END_PROFILE(SMBctemp);
2123 return;
2126 tmpfd = smb_mkstemp(fname);
2127 if (tmpfd == -1) {
2128 reply_unixerror(req, ERRDOS, ERRnoaccess);
2129 END_PROFILE(SMBctemp);
2130 return;
2133 SET_STAT_INVALID(sbuf);
2134 SMB_VFS_STAT(conn,fname,&sbuf);
2136 /* We should fail if file does not exist. */
2137 status = SMB_VFS_CREATE_FILE(
2138 conn, /* conn */
2139 req, /* req */
2140 0, /* root_dir_fid */
2141 fname, /* fname */
2142 0, /* create_file_flags */
2143 FILE_GENERIC_READ | FILE_GENERIC_WRITE, /* access_mask */
2144 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
2145 FILE_OPEN, /* create_disposition*/
2146 0, /* create_options */
2147 fattr, /* file_attributes */
2148 oplock_request, /* oplock_request */
2149 0, /* allocation_size */
2150 NULL, /* sd */
2151 NULL, /* ea_list */
2152 &fsp, /* result */
2153 NULL, /* pinfo */
2154 &sbuf); /* psbuf */
2156 /* close fd from smb_mkstemp() */
2157 close(tmpfd);
2159 if (!NT_STATUS_IS_OK(status)) {
2160 if (open_was_deferred(req->mid)) {
2161 /* We have re-scheduled this call. */
2162 END_PROFILE(SMBctemp);
2163 return;
2165 reply_openerror(req, status);
2166 END_PROFILE(SMBctemp);
2167 return;
2170 reply_outbuf(req, 1, 0);
2171 SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
2173 /* the returned filename is relative to the directory */
2174 s = strrchr_m(fsp->fsp_name, '/');
2175 if (!s) {
2176 s = fsp->fsp_name;
2177 } else {
2178 s++;
2181 #if 0
2182 /* Tested vs W2K3 - this doesn't seem to be here - null terminated filename is the only
2183 thing in the byte section. JRA */
2184 SSVALS(p, 0, -1); /* what is this? not in spec */
2185 #endif
2186 if (message_push_string(&req->outbuf, s, STR_ASCII|STR_TERMINATE)
2187 == -1) {
2188 reply_nterror(req, NT_STATUS_NO_MEMORY);
2189 END_PROFILE(SMBctemp);
2190 return;
2193 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
2194 SCVAL(req->outbuf, smb_flg,
2195 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2198 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
2199 SCVAL(req->outbuf, smb_flg,
2200 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2203 DEBUG( 2, ( "reply_ctemp: created temp file %s\n", fsp->fsp_name ) );
2204 DEBUG( 3, ( "reply_ctemp %s fd=%d umode=0%o\n", fsp->fsp_name,
2205 fsp->fh->fd, (unsigned int)sbuf.st_mode ) );
2207 END_PROFILE(SMBctemp);
2208 return;
2211 /*******************************************************************
2212 Check if a user is allowed to rename a file.
2213 ********************************************************************/
2215 static NTSTATUS can_rename(connection_struct *conn, files_struct *fsp,
2216 uint16 dirtype, SMB_STRUCT_STAT *pst)
2218 uint32 fmode;
2220 if (!CAN_WRITE(conn)) {
2221 return NT_STATUS_MEDIA_WRITE_PROTECTED;
2224 fmode = dos_mode(conn, fsp->fsp_name, pst);
2225 if ((fmode & ~dirtype) & (aHIDDEN | aSYSTEM)) {
2226 return NT_STATUS_NO_SUCH_FILE;
2229 if (S_ISDIR(pst->st_mode)) {
2230 if (fsp->posix_open) {
2231 return NT_STATUS_OK;
2234 /* If no pathnames are open below this
2235 directory, allow the rename. */
2237 if (file_find_subpath(fsp)) {
2238 return NT_STATUS_ACCESS_DENIED;
2240 return NT_STATUS_OK;
2243 if (fsp->access_mask & (DELETE_ACCESS|FILE_WRITE_ATTRIBUTES)) {
2244 return NT_STATUS_OK;
2247 return NT_STATUS_ACCESS_DENIED;
2250 /*******************************************************************
2251 * unlink a file with all relevant access checks
2252 *******************************************************************/
2254 static NTSTATUS do_unlink(connection_struct *conn,
2255 struct smb_request *req,
2256 const char *fname,
2257 uint32 dirtype)
2259 SMB_STRUCT_STAT sbuf;
2260 uint32 fattr;
2261 files_struct *fsp;
2262 uint32 dirtype_orig = dirtype;
2263 NTSTATUS status;
2265 DEBUG(10,("do_unlink: %s, dirtype = %d\n", fname, dirtype ));
2267 if (!CAN_WRITE(conn)) {
2268 return NT_STATUS_MEDIA_WRITE_PROTECTED;
2271 if (SMB_VFS_LSTAT(conn,fname,&sbuf) != 0) {
2272 return map_nt_error_from_unix(errno);
2275 fattr = dos_mode(conn,fname,&sbuf);
2277 if (dirtype & FILE_ATTRIBUTE_NORMAL) {
2278 dirtype = aDIR|aARCH|aRONLY;
2281 dirtype &= (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM);
2282 if (!dirtype) {
2283 return NT_STATUS_NO_SUCH_FILE;
2286 if (!dir_check_ftype(conn, fattr, dirtype)) {
2287 if (fattr & aDIR) {
2288 return NT_STATUS_FILE_IS_A_DIRECTORY;
2290 return NT_STATUS_NO_SUCH_FILE;
2293 if (dirtype_orig & 0x8000) {
2294 /* These will never be set for POSIX. */
2295 return NT_STATUS_NO_SUCH_FILE;
2298 #if 0
2299 if ((fattr & dirtype) & FILE_ATTRIBUTE_DIRECTORY) {
2300 return NT_STATUS_FILE_IS_A_DIRECTORY;
2303 if ((fattr & ~dirtype) & (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)) {
2304 return NT_STATUS_NO_SUCH_FILE;
2307 if (dirtype & 0xFF00) {
2308 /* These will never be set for POSIX. */
2309 return NT_STATUS_NO_SUCH_FILE;
2312 dirtype &= 0xFF;
2313 if (!dirtype) {
2314 return NT_STATUS_NO_SUCH_FILE;
2317 /* Can't delete a directory. */
2318 if (fattr & aDIR) {
2319 return NT_STATUS_FILE_IS_A_DIRECTORY;
2321 #endif
2323 #if 0 /* JRATEST */
2324 else if (dirtype & aDIR) /* Asked for a directory and it isn't. */
2325 return NT_STATUS_OBJECT_NAME_INVALID;
2326 #endif /* JRATEST */
2328 /* Fix for bug #3035 from SATOH Fumiyasu <fumiyas@miraclelinux.com>
2330 On a Windows share, a file with read-only dosmode can be opened with
2331 DELETE_ACCESS. But on a Samba share (delete readonly = no), it
2332 fails with NT_STATUS_CANNOT_DELETE error.
2334 This semantic causes a problem that a user can not
2335 rename a file with read-only dosmode on a Samba share
2336 from a Windows command prompt (i.e. cmd.exe, but can rename
2337 from Windows Explorer).
2340 if (!lp_delete_readonly(SNUM(conn))) {
2341 if (fattr & aRONLY) {
2342 return NT_STATUS_CANNOT_DELETE;
2346 /* On open checks the open itself will check the share mode, so
2347 don't do it here as we'll get it wrong. */
2349 status = SMB_VFS_CREATE_FILE
2350 (conn, /* conn */
2351 req, /* req */
2352 0, /* root_dir_fid */
2353 fname, /* fname */
2354 0, /* create_file_flags */
2355 DELETE_ACCESS, /* access_mask */
2356 FILE_SHARE_NONE, /* share_access */
2357 FILE_OPEN, /* create_disposition*/
2358 FILE_NON_DIRECTORY_FILE, /* create_options */
2359 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
2360 0, /* oplock_request */
2361 0, /* allocation_size */
2362 NULL, /* sd */
2363 NULL, /* ea_list */
2364 &fsp, /* result */
2365 NULL, /* pinfo */
2366 &sbuf); /* psbuf */
2368 if (!NT_STATUS_IS_OK(status)) {
2369 DEBUG(10, ("SMB_VFS_CREATEFILE failed: %s\n",
2370 nt_errstr(status)));
2371 return status;
2374 /* The set is across all open files on this dev/inode pair. */
2375 if (!set_delete_on_close(fsp, True, &conn->server_info->utok)) {
2376 close_file(req, fsp, NORMAL_CLOSE);
2377 return NT_STATUS_ACCESS_DENIED;
2380 return close_file(req, fsp, NORMAL_CLOSE);
2383 /****************************************************************************
2384 The guts of the unlink command, split out so it may be called by the NT SMB
2385 code.
2386 ****************************************************************************/
2388 NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
2389 uint32 dirtype, const char *name_in, bool has_wild)
2391 const char *directory = NULL;
2392 char *mask = NULL;
2393 char *name = NULL;
2394 char *p = NULL;
2395 int count=0;
2396 NTSTATUS status = NT_STATUS_OK;
2397 SMB_STRUCT_STAT sbuf, st;
2398 TALLOC_CTX *ctx = talloc_tos();
2400 status = unix_convert(ctx, conn, name_in, has_wild, &name, NULL, &sbuf);
2401 if (!NT_STATUS_IS_OK(status)) {
2402 return status;
2405 p = strrchr_m(name,'/');
2406 if (!p) {
2407 directory = talloc_strdup(ctx, ".");
2408 if (!directory) {
2409 return NT_STATUS_NO_MEMORY;
2411 mask = name;
2412 } else {
2413 *p = 0;
2414 directory = name;
2415 mask = p+1;
2419 * We should only check the mangled cache
2420 * here if unix_convert failed. This means
2421 * that the path in 'mask' doesn't exist
2422 * on the file system and so we need to look
2423 * for a possible mangle. This patch from
2424 * Tine Smukavec <valentin.smukavec@hermes.si>.
2427 if (!VALID_STAT(sbuf) && mangle_is_mangled(mask,conn->params)) {
2428 char *new_mask = NULL;
2429 mangle_lookup_name_from_8_3(ctx,
2430 mask,
2431 &new_mask,
2432 conn->params );
2433 if (new_mask) {
2434 mask = new_mask;
2438 if (!has_wild) {
2439 directory = talloc_asprintf(ctx,
2440 "%s/%s",
2441 directory,
2442 mask);
2443 if (!directory) {
2444 return NT_STATUS_NO_MEMORY;
2446 if (dirtype == 0) {
2447 dirtype = FILE_ATTRIBUTE_NORMAL;
2450 status = check_name(conn, directory);
2451 if (!NT_STATUS_IS_OK(status)) {
2452 return status;
2455 status = do_unlink(conn, req, directory, dirtype);
2456 if (!NT_STATUS_IS_OK(status)) {
2457 return status;
2460 count++;
2461 } else {
2462 struct smb_Dir *dir_hnd = NULL;
2463 long offset = 0;
2464 const char *dname;
2466 if ((dirtype & SAMBA_ATTRIBUTES_MASK) == aDIR) {
2467 return NT_STATUS_OBJECT_NAME_INVALID;
2470 if (strequal(mask,"????????.???")) {
2471 mask[0] = '*';
2472 mask[1] = '\0';
2475 status = check_name(conn, directory);
2476 if (!NT_STATUS_IS_OK(status)) {
2477 return status;
2480 dir_hnd = OpenDir(talloc_tos(), conn, directory, mask,
2481 dirtype);
2482 if (dir_hnd == NULL) {
2483 return map_nt_error_from_unix(errno);
2486 /* XXXX the CIFS spec says that if bit0 of the flags2 field is set then
2487 the pattern matches against the long name, otherwise the short name
2488 We don't implement this yet XXXX
2491 status = NT_STATUS_NO_SUCH_FILE;
2493 while ((dname = ReadDirName(dir_hnd, &offset, &st))) {
2494 char *fname = NULL;
2496 if (!is_visible_file(conn, directory, dname, &st,
2497 true))
2499 continue;
2502 /* Quick check for "." and ".." */
2503 if (ISDOT(dname) || ISDOTDOT(dname)) {
2504 continue;
2507 if(!mask_match(dname, mask, conn->case_sensitive)) {
2508 continue;
2511 fname = talloc_asprintf(ctx, "%s/%s",
2512 directory,
2513 dname);
2514 if (!fname) {
2515 return NT_STATUS_NO_MEMORY;
2518 status = check_name(conn, fname);
2519 if (!NT_STATUS_IS_OK(status)) {
2520 TALLOC_FREE(dir_hnd);
2521 return status;
2524 status = do_unlink(conn, req, fname, dirtype);
2525 if (!NT_STATUS_IS_OK(status)) {
2526 TALLOC_FREE(fname);
2527 continue;
2530 count++;
2531 DEBUG(3,("unlink_internals: successful unlink [%s]\n",
2532 fname));
2534 TALLOC_FREE(fname);
2536 TALLOC_FREE(dir_hnd);
2539 if (count == 0 && NT_STATUS_IS_OK(status) && errno != 0) {
2540 status = map_nt_error_from_unix(errno);
2543 return status;
2546 /****************************************************************************
2547 Reply to a unlink
2548 ****************************************************************************/
2550 void reply_unlink(struct smb_request *req)
2552 connection_struct *conn = req->conn;
2553 char *name = NULL;
2554 uint32 dirtype;
2555 NTSTATUS status;
2556 bool path_contains_wcard = False;
2557 TALLOC_CTX *ctx = talloc_tos();
2559 START_PROFILE(SMBunlink);
2561 if (req->wct < 1) {
2562 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2563 END_PROFILE(SMBunlink);
2564 return;
2567 dirtype = SVAL(req->vwv+0, 0);
2569 srvstr_get_path_req_wcard(ctx, req, &name, (const char *)req->buf + 1,
2570 STR_TERMINATE, &status,
2571 &path_contains_wcard);
2572 if (!NT_STATUS_IS_OK(status)) {
2573 reply_nterror(req, status);
2574 END_PROFILE(SMBunlink);
2575 return;
2578 status = resolve_dfspath_wcard(ctx, conn,
2579 req->flags2 & FLAGS2_DFS_PATHNAMES,
2580 name,
2581 &name,
2582 &path_contains_wcard);
2583 if (!NT_STATUS_IS_OK(status)) {
2584 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2585 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2586 ERRSRV, ERRbadpath);
2587 END_PROFILE(SMBunlink);
2588 return;
2590 reply_nterror(req, status);
2591 END_PROFILE(SMBunlink);
2592 return;
2595 DEBUG(3,("reply_unlink : %s\n",name));
2597 status = unlink_internals(conn, req, dirtype, name,
2598 path_contains_wcard);
2599 if (!NT_STATUS_IS_OK(status)) {
2600 if (open_was_deferred(req->mid)) {
2601 /* We have re-scheduled this call. */
2602 END_PROFILE(SMBunlink);
2603 return;
2605 reply_nterror(req, status);
2606 END_PROFILE(SMBunlink);
2607 return;
2610 reply_outbuf(req, 0, 0);
2611 END_PROFILE(SMBunlink);
2613 return;
2616 /****************************************************************************
2617 Fail for readbraw.
2618 ****************************************************************************/
2620 static void fail_readraw(void)
2622 const char *errstr = talloc_asprintf(talloc_tos(),
2623 "FAIL ! reply_readbraw: socket write fail (%s)",
2624 strerror(errno));
2625 if (!errstr) {
2626 errstr = "";
2628 exit_server_cleanly(errstr);
2631 /****************************************************************************
2632 Fake (read/write) sendfile. Returns -1 on read or write fail.
2633 ****************************************************************************/
2635 static ssize_t fake_sendfile(files_struct *fsp, SMB_OFF_T startpos,
2636 size_t nread)
2638 size_t bufsize;
2639 size_t tosend = nread;
2640 char *buf;
2642 if (nread == 0) {
2643 return 0;
2646 bufsize = MIN(nread, 65536);
2648 if (!(buf = SMB_MALLOC_ARRAY(char, bufsize))) {
2649 return -1;
2652 while (tosend > 0) {
2653 ssize_t ret;
2654 size_t cur_read;
2656 if (tosend > bufsize) {
2657 cur_read = bufsize;
2658 } else {
2659 cur_read = tosend;
2661 ret = read_file(fsp,buf,startpos,cur_read);
2662 if (ret == -1) {
2663 SAFE_FREE(buf);
2664 return -1;
2667 /* If we had a short read, fill with zeros. */
2668 if (ret < cur_read) {
2669 memset(buf + ret, '\0', cur_read - ret);
2672 if (write_data(smbd_server_fd(),buf,cur_read) != cur_read) {
2673 SAFE_FREE(buf);
2674 return -1;
2676 tosend -= cur_read;
2677 startpos += cur_read;
2680 SAFE_FREE(buf);
2681 return (ssize_t)nread;
2684 #if defined(WITH_SENDFILE)
2685 /****************************************************************************
2686 Deal with the case of sendfile reading less bytes from the file than
2687 requested. Fill with zeros (all we can do).
2688 ****************************************************************************/
2690 static void sendfile_short_send(files_struct *fsp,
2691 ssize_t nread,
2692 size_t headersize,
2693 size_t smb_maxcnt)
2695 #define SHORT_SEND_BUFSIZE 1024
2696 if (nread < headersize) {
2697 DEBUG(0,("sendfile_short_send: sendfile failed to send "
2698 "header for file %s (%s). Terminating\n",
2699 fsp->fsp_name, strerror(errno) ));
2700 exit_server_cleanly("sendfile_short_send failed");
2703 nread -= headersize;
2705 if (nread < smb_maxcnt) {
2706 char *buf = SMB_CALLOC_ARRAY(char, SHORT_SEND_BUFSIZE);
2707 if (!buf) {
2708 exit_server_cleanly("sendfile_short_send: "
2709 "malloc failed");
2712 DEBUG(0,("sendfile_short_send: filling truncated file %s "
2713 "with zeros !\n", fsp->fsp_name));
2715 while (nread < smb_maxcnt) {
2717 * We asked for the real file size and told sendfile
2718 * to not go beyond the end of the file. But it can
2719 * happen that in between our fstat call and the
2720 * sendfile call the file was truncated. This is very
2721 * bad because we have already announced the larger
2722 * number of bytes to the client.
2724 * The best we can do now is to send 0-bytes, just as
2725 * a read from a hole in a sparse file would do.
2727 * This should happen rarely enough that I don't care
2728 * about efficiency here :-)
2730 size_t to_write;
2732 to_write = MIN(SHORT_SEND_BUFSIZE, smb_maxcnt - nread);
2733 if (write_data(smbd_server_fd(), buf, to_write) != to_write) {
2734 exit_server_cleanly("sendfile_short_send: "
2735 "write_data failed");
2737 nread += to_write;
2739 SAFE_FREE(buf);
2742 #endif /* defined WITH_SENDFILE */
2744 /****************************************************************************
2745 Return a readbraw error (4 bytes of zero).
2746 ****************************************************************************/
2748 static void reply_readbraw_error(void)
2750 char header[4];
2751 SIVAL(header,0,0);
2752 if (write_data(smbd_server_fd(),header,4) != 4) {
2753 fail_readraw();
2757 /****************************************************************************
2758 Use sendfile in readbraw.
2759 ****************************************************************************/
2761 static void send_file_readbraw(connection_struct *conn,
2762 struct smb_request *req,
2763 files_struct *fsp,
2764 SMB_OFF_T startpos,
2765 size_t nread,
2766 ssize_t mincount)
2768 char *outbuf = NULL;
2769 ssize_t ret=0;
2771 #if defined(WITH_SENDFILE)
2773 * We can only use sendfile on a non-chained packet
2774 * but we can use on a non-oplocked file. tridge proved this
2775 * on a train in Germany :-). JRA.
2776 * reply_readbraw has already checked the length.
2779 if ( !req_is_in_chain(req) && (nread > 0) && (fsp->base_fsp == NULL) &&
2780 (fsp->wcp == NULL) && lp_use_sendfile(SNUM(conn)) ) {
2781 ssize_t sendfile_read = -1;
2782 char header[4];
2783 DATA_BLOB header_blob;
2785 _smb_setlen(header,nread);
2786 header_blob = data_blob_const(header, 4);
2788 if ((sendfile_read = SMB_VFS_SENDFILE(smbd_server_fd(), fsp,
2789 &header_blob, startpos, nread)) == -1) {
2790 /* Returning ENOSYS means no data at all was sent.
2791 * Do this as a normal read. */
2792 if (errno == ENOSYS) {
2793 goto normal_readbraw;
2797 * Special hack for broken Linux with no working sendfile. If we
2798 * return EINTR we sent the header but not the rest of the data.
2799 * Fake this up by doing read/write calls.
2801 if (errno == EINTR) {
2802 /* Ensure we don't do this again. */
2803 set_use_sendfile(SNUM(conn), False);
2804 DEBUG(0,("send_file_readbraw: sendfile not available. Faking..\n"));
2806 if (fake_sendfile(fsp, startpos, nread) == -1) {
2807 DEBUG(0,("send_file_readbraw: fake_sendfile failed for file %s (%s).\n",
2808 fsp->fsp_name, strerror(errno) ));
2809 exit_server_cleanly("send_file_readbraw fake_sendfile failed");
2811 return;
2814 DEBUG(0,("send_file_readbraw: sendfile failed for file %s (%s). Terminating\n",
2815 fsp->fsp_name, strerror(errno) ));
2816 exit_server_cleanly("send_file_readbraw sendfile failed");
2817 } else if (sendfile_read == 0) {
2819 * Some sendfile implementations return 0 to indicate
2820 * that there was a short read, but nothing was
2821 * actually written to the socket. In this case,
2822 * fallback to the normal read path so the header gets
2823 * the correct byte count.
2825 DEBUG(3, ("send_file_readbraw: sendfile sent zero "
2826 "bytes falling back to the normal read: "
2827 "%s\n", fsp->fsp_name));
2828 goto normal_readbraw;
2831 /* Deal with possible short send. */
2832 if (sendfile_read != 4+nread) {
2833 sendfile_short_send(fsp, sendfile_read, 4, nread);
2835 return;
2838 normal_readbraw:
2839 #endif
2841 outbuf = TALLOC_ARRAY(NULL, char, nread+4);
2842 if (!outbuf) {
2843 DEBUG(0,("send_file_readbraw: TALLOC_ARRAY failed for size %u.\n",
2844 (unsigned)(nread+4)));
2845 reply_readbraw_error();
2846 return;
2849 if (nread > 0) {
2850 ret = read_file(fsp,outbuf+4,startpos,nread);
2851 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2852 if (ret < mincount)
2853 ret = 0;
2854 #else
2855 if (ret < nread)
2856 ret = 0;
2857 #endif
2860 _smb_setlen(outbuf,ret);
2861 if (write_data(smbd_server_fd(),outbuf,4+ret) != 4+ret)
2862 fail_readraw();
2864 TALLOC_FREE(outbuf);
2867 /****************************************************************************
2868 Reply to a readbraw (core+ protocol).
2869 ****************************************************************************/
2871 void reply_readbraw(struct smb_request *req)
2873 connection_struct *conn = req->conn;
2874 ssize_t maxcount,mincount;
2875 size_t nread = 0;
2876 SMB_OFF_T startpos;
2877 files_struct *fsp;
2878 struct lock_struct lock;
2879 SMB_STRUCT_STAT st;
2880 SMB_OFF_T size = 0;
2882 START_PROFILE(SMBreadbraw);
2884 if (srv_is_signing_active() || is_encrypted_packet(req->inbuf)) {
2885 exit_server_cleanly("reply_readbraw: SMB signing/sealing is active - "
2886 "raw reads/writes are disallowed.");
2889 if (req->wct < 8) {
2890 reply_readbraw_error();
2891 END_PROFILE(SMBreadbraw);
2892 return;
2896 * Special check if an oplock break has been issued
2897 * and the readraw request croses on the wire, we must
2898 * return a zero length response here.
2901 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
2904 * We have to do a check_fsp by hand here, as
2905 * we must always return 4 zero bytes on error,
2906 * not a NTSTATUS.
2909 if (!fsp || !conn || conn != fsp->conn ||
2910 req->vuid != fsp->vuid ||
2911 fsp->is_directory || fsp->fh->fd == -1) {
2913 * fsp could be NULL here so use the value from the packet. JRA.
2915 DEBUG(3,("reply_readbraw: fnum %d not valid "
2916 "- cache prime?\n",
2917 (int)SVAL(req->vwv+0, 0)));
2918 reply_readbraw_error();
2919 END_PROFILE(SMBreadbraw);
2920 return;
2923 /* Do a "by hand" version of CHECK_READ. */
2924 if (!(fsp->can_read ||
2925 ((req->flags2 & FLAGS2_READ_PERMIT_EXECUTE) &&
2926 (fsp->access_mask & FILE_EXECUTE)))) {
2927 DEBUG(3,("reply_readbraw: fnum %d not readable.\n",
2928 (int)SVAL(req->vwv+0, 0)));
2929 reply_readbraw_error();
2930 END_PROFILE(SMBreadbraw);
2931 return;
2934 flush_write_cache(fsp, READRAW_FLUSH);
2936 startpos = IVAL_TO_SMB_OFF_T(req->vwv+1, 0);
2937 if(req->wct == 10) {
2939 * This is a large offset (64 bit) read.
2941 #ifdef LARGE_SMB_OFF_T
2943 startpos |= (((SMB_OFF_T)IVAL(req->vwv+8, 0)) << 32);
2945 #else /* !LARGE_SMB_OFF_T */
2948 * Ensure we haven't been sent a >32 bit offset.
2951 if(IVAL(req->vwv+8, 0) != 0) {
2952 DEBUG(0,("reply_readbraw: large offset "
2953 "(%x << 32) used and we don't support "
2954 "64 bit offsets.\n",
2955 (unsigned int)IVAL(req->vwv+8, 0) ));
2956 reply_readbraw_error();
2957 END_PROFILE(SMBreadbraw);
2958 return;
2961 #endif /* LARGE_SMB_OFF_T */
2963 if(startpos < 0) {
2964 DEBUG(0,("reply_readbraw: negative 64 bit "
2965 "readraw offset (%.0f) !\n",
2966 (double)startpos ));
2967 reply_readbraw_error();
2968 END_PROFILE(SMBreadbraw);
2969 return;
2973 maxcount = (SVAL(req->vwv+3, 0) & 0xFFFF);
2974 mincount = (SVAL(req->vwv+4, 0) & 0xFFFF);
2976 /* ensure we don't overrun the packet size */
2977 maxcount = MIN(65535,maxcount);
2979 init_strict_lock_struct(fsp, (uint32)req->smbpid,
2980 (uint64_t)startpos, (uint64_t)maxcount, READ_LOCK,
2981 &lock);
2983 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
2984 reply_readbraw_error();
2985 END_PROFILE(SMBreadbraw);
2986 return;
2989 if (SMB_VFS_FSTAT(fsp, &st) == 0) {
2990 size = st.st_size;
2993 if (startpos >= size) {
2994 nread = 0;
2995 } else {
2996 nread = MIN(maxcount,(size - startpos));
2999 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
3000 if (nread < mincount)
3001 nread = 0;
3002 #endif
3004 DEBUG( 3, ( "reply_readbraw: fnum=%d start=%.0f max=%lu "
3005 "min=%lu nread=%lu\n",
3006 fsp->fnum, (double)startpos,
3007 (unsigned long)maxcount,
3008 (unsigned long)mincount,
3009 (unsigned long)nread ) );
3011 send_file_readbraw(conn, req, fsp, startpos, nread, mincount);
3013 DEBUG(5,("reply_readbraw finished\n"));
3015 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
3017 END_PROFILE(SMBreadbraw);
3018 return;
3021 #undef DBGC_CLASS
3022 #define DBGC_CLASS DBGC_LOCKING
3024 /****************************************************************************
3025 Reply to a lockread (core+ protocol).
3026 ****************************************************************************/
3028 void reply_lockread(struct smb_request *req)
3030 connection_struct *conn = req->conn;
3031 ssize_t nread = -1;
3032 char *data;
3033 SMB_OFF_T startpos;
3034 size_t numtoread;
3035 NTSTATUS status;
3036 files_struct *fsp;
3037 struct byte_range_lock *br_lck = NULL;
3038 char *p = NULL;
3040 START_PROFILE(SMBlockread);
3042 if (req->wct < 5) {
3043 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3044 END_PROFILE(SMBlockread);
3045 return;
3048 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
3050 if (!check_fsp(conn, req, fsp)) {
3051 END_PROFILE(SMBlockread);
3052 return;
3055 if (!CHECK_READ(fsp,req)) {
3056 reply_doserror(req, ERRDOS, ERRbadaccess);
3057 END_PROFILE(SMBlockread);
3058 return;
3061 numtoread = SVAL(req->vwv+1, 0);
3062 startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
3064 numtoread = MIN(BUFFER_SIZE - (smb_size + 3*2 + 3), numtoread);
3066 reply_outbuf(req, 5, numtoread + 3);
3068 data = smb_buf(req->outbuf) + 3;
3071 * NB. Discovered by Menny Hamburger at Mainsoft. This is a core+
3072 * protocol request that predates the read/write lock concept.
3073 * Thus instead of asking for a read lock here we need to ask
3074 * for a write lock. JRA.
3075 * Note that the requested lock size is unaffected by max_recv.
3078 br_lck = do_lock(smbd_messaging_context(),
3079 fsp,
3080 req->smbpid,
3081 (uint64_t)numtoread,
3082 (uint64_t)startpos,
3083 WRITE_LOCK,
3084 WINDOWS_LOCK,
3085 False, /* Non-blocking lock. */
3086 &status,
3087 NULL,
3088 NULL);
3089 TALLOC_FREE(br_lck);
3091 if (NT_STATUS_V(status)) {
3092 reply_nterror(req, status);
3093 END_PROFILE(SMBlockread);
3094 return;
3098 * However the requested READ size IS affected by max_recv. Insanity.... JRA.
3101 if (numtoread > max_recv) {
3102 DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u). \
3103 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
3104 (unsigned int)numtoread, (unsigned int)max_recv ));
3105 numtoread = MIN(numtoread,max_recv);
3107 nread = read_file(fsp,data,startpos,numtoread);
3109 if (nread < 0) {
3110 reply_unixerror(req, ERRDOS, ERRnoaccess);
3111 END_PROFILE(SMBlockread);
3112 return;
3115 srv_set_message((char *)req->outbuf, 5, nread+3, False);
3117 SSVAL(req->outbuf,smb_vwv0,nread);
3118 SSVAL(req->outbuf,smb_vwv5,nread+3);
3119 p = smb_buf(req->outbuf);
3120 SCVAL(p,0,0); /* pad byte. */
3121 SSVAL(p,1,nread);
3123 DEBUG(3,("lockread fnum=%d num=%d nread=%d\n",
3124 fsp->fnum, (int)numtoread, (int)nread));
3126 END_PROFILE(SMBlockread);
3127 return;
3130 #undef DBGC_CLASS
3131 #define DBGC_CLASS DBGC_ALL
3133 /****************************************************************************
3134 Reply to a read.
3135 ****************************************************************************/
3137 void reply_read(struct smb_request *req)
3139 connection_struct *conn = req->conn;
3140 size_t numtoread;
3141 ssize_t nread = 0;
3142 char *data;
3143 SMB_OFF_T startpos;
3144 int outsize = 0;
3145 files_struct *fsp;
3146 struct lock_struct lock;
3148 START_PROFILE(SMBread);
3150 if (req->wct < 3) {
3151 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3152 END_PROFILE(SMBread);
3153 return;
3156 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
3158 if (!check_fsp(conn, req, fsp)) {
3159 END_PROFILE(SMBread);
3160 return;
3163 if (!CHECK_READ(fsp,req)) {
3164 reply_doserror(req, ERRDOS, ERRbadaccess);
3165 END_PROFILE(SMBread);
3166 return;
3169 numtoread = SVAL(req->vwv+1, 0);
3170 startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
3172 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
3175 * The requested read size cannot be greater than max_recv. JRA.
3177 if (numtoread > max_recv) {
3178 DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u). \
3179 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
3180 (unsigned int)numtoread, (unsigned int)max_recv ));
3181 numtoread = MIN(numtoread,max_recv);
3184 reply_outbuf(req, 5, numtoread+3);
3186 data = smb_buf(req->outbuf) + 3;
3188 init_strict_lock_struct(fsp, (uint32)req->smbpid,
3189 (uint64_t)startpos, (uint64_t)numtoread, READ_LOCK,
3190 &lock);
3192 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
3193 reply_doserror(req, ERRDOS,ERRlock);
3194 END_PROFILE(SMBread);
3195 return;
3198 if (numtoread > 0)
3199 nread = read_file(fsp,data,startpos,numtoread);
3201 if (nread < 0) {
3202 reply_unixerror(req, ERRDOS,ERRnoaccess);
3203 goto strict_unlock;
3206 srv_set_message((char *)req->outbuf, 5, nread+3, False);
3208 SSVAL(req->outbuf,smb_vwv0,nread);
3209 SSVAL(req->outbuf,smb_vwv5,nread+3);
3210 SCVAL(smb_buf(req->outbuf),0,1);
3211 SSVAL(smb_buf(req->outbuf),1,nread);
3213 DEBUG( 3, ( "read fnum=%d num=%d nread=%d\n",
3214 fsp->fnum, (int)numtoread, (int)nread ) );
3216 strict_unlock:
3217 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
3219 END_PROFILE(SMBread);
3220 return;
3223 /****************************************************************************
3224 Setup readX header.
3225 ****************************************************************************/
3227 static int setup_readX_header(struct smb_request *req, char *outbuf,
3228 size_t smb_maxcnt)
3230 int outsize;
3231 char *data;
3233 outsize = srv_set_message(outbuf,12,smb_maxcnt,False);
3234 data = smb_buf(outbuf);
3236 memset(outbuf+smb_vwv0,'\0',24); /* valgrind init. */
3238 SCVAL(outbuf,smb_vwv0,0xFF);
3239 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be -1. */
3240 SSVAL(outbuf,smb_vwv5,smb_maxcnt);
3241 SSVAL(outbuf,smb_vwv6,
3242 req_wct_ofs(req)
3243 + 1 /* the wct field */
3244 + 12 * sizeof(uint16_t) /* vwv */
3245 + 2); /* the buflen field */
3246 SSVAL(outbuf,smb_vwv7,(smb_maxcnt >> 16));
3247 SSVAL(outbuf,smb_vwv11,smb_maxcnt);
3248 /* Reset the outgoing length, set_message truncates at 0x1FFFF. */
3249 _smb_setlen_large(outbuf,(smb_size + 12*2 + smb_maxcnt - 4));
3250 return outsize;
3253 /****************************************************************************
3254 Reply to a read and X - possibly using sendfile.
3255 ****************************************************************************/
3257 static void send_file_readX(connection_struct *conn, struct smb_request *req,
3258 files_struct *fsp, SMB_OFF_T startpos,
3259 size_t smb_maxcnt)
3261 SMB_STRUCT_STAT sbuf;
3262 ssize_t nread = -1;
3263 struct lock_struct lock;
3265 if(SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
3266 reply_unixerror(req, ERRDOS, ERRnoaccess);
3267 return;
3270 init_strict_lock_struct(fsp, (uint32)req->smbpid,
3271 (uint64_t)startpos, (uint64_t)smb_maxcnt, READ_LOCK,
3272 &lock);
3274 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
3275 reply_doserror(req, ERRDOS, ERRlock);
3276 return;
3279 if (!S_ISREG(sbuf.st_mode) || (startpos > sbuf.st_size)
3280 || (smb_maxcnt > (sbuf.st_size - startpos))) {
3282 * We already know that we would do a short read, so don't
3283 * try the sendfile() path.
3285 goto nosendfile_read;
3288 #if defined(WITH_SENDFILE)
3290 * We can only use sendfile on a non-chained packet
3291 * but we can use on a non-oplocked file. tridge proved this
3292 * on a train in Germany :-). JRA.
3295 if (!req_is_in_chain(req) &&
3296 !is_encrypted_packet(req->inbuf) && (fsp->base_fsp == NULL) &&
3297 lp_use_sendfile(SNUM(conn)) && (fsp->wcp == NULL) ) {
3298 uint8 headerbuf[smb_size + 12 * 2];
3299 DATA_BLOB header;
3302 * Set up the packet header before send. We
3303 * assume here the sendfile will work (get the
3304 * correct amount of data).
3307 header = data_blob_const(headerbuf, sizeof(headerbuf));
3309 construct_reply_common_req(req, (char *)headerbuf);
3310 setup_readX_header(req, (char *)headerbuf, smb_maxcnt);
3312 if ((nread = SMB_VFS_SENDFILE(smbd_server_fd(), fsp, &header, startpos, smb_maxcnt)) == -1) {
3313 /* Returning ENOSYS means no data at all was sent.
3314 Do this as a normal read. */
3315 if (errno == ENOSYS) {
3316 goto normal_read;
3320 * Special hack for broken Linux with no working sendfile. If we
3321 * return EINTR we sent the header but not the rest of the data.
3322 * Fake this up by doing read/write calls.
3325 if (errno == EINTR) {
3326 /* Ensure we don't do this again. */
3327 set_use_sendfile(SNUM(conn), False);
3328 DEBUG(0,("send_file_readX: sendfile not available. Faking..\n"));
3329 nread = fake_sendfile(fsp, startpos,
3330 smb_maxcnt);
3331 if (nread == -1) {
3332 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
3333 fsp->fsp_name, strerror(errno) ));
3334 exit_server_cleanly("send_file_readX: fake_sendfile failed");
3336 DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
3337 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
3338 /* No outbuf here means successful sendfile. */
3339 goto strict_unlock;
3342 DEBUG(0,("send_file_readX: sendfile failed for file %s (%s). Terminating\n",
3343 fsp->fsp_name, strerror(errno) ));
3344 exit_server_cleanly("send_file_readX sendfile failed");
3345 } else if (nread == 0) {
3347 * Some sendfile implementations return 0 to indicate
3348 * that there was a short read, but nothing was
3349 * actually written to the socket. In this case,
3350 * fallback to the normal read path so the header gets
3351 * the correct byte count.
3353 DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
3354 "falling back to the normal read: %s\n",
3355 fsp->fsp_name));
3356 goto normal_read;
3359 DEBUG( 3, ( "send_file_readX: sendfile fnum=%d max=%d nread=%d\n",
3360 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
3362 /* Deal with possible short send. */
3363 if (nread != smb_maxcnt + sizeof(headerbuf)) {
3364 sendfile_short_send(fsp, nread, sizeof(headerbuf), smb_maxcnt);
3366 /* No outbuf here means successful sendfile. */
3367 SMB_PERFCOUNT_SET_MSGLEN_OUT(&req->pcd, nread);
3368 SMB_PERFCOUNT_END(&req->pcd);
3369 goto strict_unlock;
3372 normal_read:
3374 #endif
3376 if ((smb_maxcnt & 0xFF0000) > 0x10000) {
3377 uint8 headerbuf[smb_size + 2*12];
3379 construct_reply_common_req(req, (char *)headerbuf);
3380 setup_readX_header(req, (char *)headerbuf, smb_maxcnt);
3382 /* Send out the header. */
3383 if (write_data(smbd_server_fd(), (char *)headerbuf,
3384 sizeof(headerbuf)) != sizeof(headerbuf)) {
3385 DEBUG(0,("send_file_readX: write_data failed for file %s (%s). Terminating\n",
3386 fsp->fsp_name, strerror(errno) ));
3387 exit_server_cleanly("send_file_readX sendfile failed");
3389 nread = fake_sendfile(fsp, startpos, smb_maxcnt);
3390 if (nread == -1) {
3391 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
3392 fsp->fsp_name, strerror(errno) ));
3393 exit_server_cleanly("send_file_readX: fake_sendfile failed");
3395 goto strict_unlock;
3398 nosendfile_read:
3400 reply_outbuf(req, 12, smb_maxcnt);
3402 nread = read_file(fsp, smb_buf(req->outbuf), startpos, smb_maxcnt);
3404 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
3406 if (nread < 0) {
3407 reply_unixerror(req, ERRDOS, ERRnoaccess);
3408 return;
3411 setup_readX_header(req, (char *)req->outbuf, nread);
3413 DEBUG( 3, ( "send_file_readX fnum=%d max=%d nread=%d\n",
3414 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
3416 chain_reply(req);
3417 return;
3419 strict_unlock:
3420 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
3421 TALLOC_FREE(req->outbuf);
3422 return;
3425 /****************************************************************************
3426 Reply to a read and X.
3427 ****************************************************************************/
3429 void reply_read_and_X(struct smb_request *req)
3431 connection_struct *conn = req->conn;
3432 files_struct *fsp;
3433 SMB_OFF_T startpos;
3434 size_t smb_maxcnt;
3435 bool big_readX = False;
3436 #if 0
3437 size_t smb_mincnt = SVAL(req->vwv+6, 0);
3438 #endif
3440 START_PROFILE(SMBreadX);
3442 if ((req->wct != 10) && (req->wct != 12)) {
3443 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3444 return;
3447 fsp = file_fsp(req, SVAL(req->vwv+2, 0));
3448 startpos = IVAL_TO_SMB_OFF_T(req->vwv+3, 0);
3449 smb_maxcnt = SVAL(req->vwv+5, 0);
3451 /* If it's an IPC, pass off the pipe handler. */
3452 if (IS_IPC(conn)) {
3453 reply_pipe_read_and_X(req);
3454 END_PROFILE(SMBreadX);
3455 return;
3458 if (!check_fsp(conn, req, fsp)) {
3459 END_PROFILE(SMBreadX);
3460 return;
3463 if (!CHECK_READ(fsp,req)) {
3464 reply_doserror(req, ERRDOS,ERRbadaccess);
3465 END_PROFILE(SMBreadX);
3466 return;
3469 if (global_client_caps & CAP_LARGE_READX) {
3470 size_t upper_size = SVAL(req->vwv+7, 0);
3471 smb_maxcnt |= (upper_size<<16);
3472 if (upper_size > 1) {
3473 /* Can't do this on a chained packet. */
3474 if ((CVAL(req->vwv+0, 0) != 0xFF)) {
3475 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
3476 END_PROFILE(SMBreadX);
3477 return;
3479 /* We currently don't do this on signed or sealed data. */
3480 if (srv_is_signing_active() || is_encrypted_packet(req->inbuf)) {
3481 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
3482 END_PROFILE(SMBreadX);
3483 return;
3485 /* Is there room in the reply for this data ? */
3486 if (smb_maxcnt > (0xFFFFFF - (smb_size -4 + 12*2))) {
3487 reply_nterror(req,
3488 NT_STATUS_INVALID_PARAMETER);
3489 END_PROFILE(SMBreadX);
3490 return;
3492 big_readX = True;
3496 if (req->wct == 12) {
3497 #ifdef LARGE_SMB_OFF_T
3499 * This is a large offset (64 bit) read.
3501 startpos |= (((SMB_OFF_T)IVAL(req->vwv+10, 0)) << 32);
3503 #else /* !LARGE_SMB_OFF_T */
3506 * Ensure we haven't been sent a >32 bit offset.
3509 if(IVAL(req->vwv+10, 0) != 0) {
3510 DEBUG(0,("reply_read_and_X - large offset (%x << 32) "
3511 "used and we don't support 64 bit offsets.\n",
3512 (unsigned int)IVAL(req->vwv+10, 0) ));
3513 END_PROFILE(SMBreadX);
3514 reply_doserror(req, ERRDOS, ERRbadaccess);
3515 return;
3518 #endif /* LARGE_SMB_OFF_T */
3522 if (!big_readX &&
3523 schedule_aio_read_and_X(conn, req, fsp, startpos, smb_maxcnt)) {
3524 goto out;
3527 send_file_readX(conn, req, fsp, startpos, smb_maxcnt);
3529 out:
3530 END_PROFILE(SMBreadX);
3531 return;
3534 /****************************************************************************
3535 Error replies to writebraw must have smb_wct == 1. Fix this up.
3536 ****************************************************************************/
3538 void error_to_writebrawerr(struct smb_request *req)
3540 uint8 *old_outbuf = req->outbuf;
3542 reply_outbuf(req, 1, 0);
3544 memcpy(req->outbuf, old_outbuf, smb_size);
3545 TALLOC_FREE(old_outbuf);
3548 /****************************************************************************
3549 Reply to a writebraw (core+ or LANMAN1.0 protocol).
3550 ****************************************************************************/
3552 void reply_writebraw(struct smb_request *req)
3554 connection_struct *conn = req->conn;
3555 char *buf = NULL;
3556 ssize_t nwritten=0;
3557 ssize_t total_written=0;
3558 size_t numtowrite=0;
3559 size_t tcount;
3560 SMB_OFF_T startpos;
3561 char *data=NULL;
3562 bool write_through;
3563 files_struct *fsp;
3564 struct lock_struct lock;
3565 NTSTATUS status;
3567 START_PROFILE(SMBwritebraw);
3570 * If we ever reply with an error, it must have the SMB command
3571 * type of SMBwritec, not SMBwriteBraw, as this tells the client
3572 * we're finished.
3574 SCVAL(req->inbuf,smb_com,SMBwritec);
3576 if (srv_is_signing_active()) {
3577 END_PROFILE(SMBwritebraw);
3578 exit_server_cleanly("reply_writebraw: SMB signing is active - "
3579 "raw reads/writes are disallowed.");
3582 if (req->wct < 12) {
3583 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3584 error_to_writebrawerr(req);
3585 END_PROFILE(SMBwritebraw);
3586 return;
3589 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
3590 if (!check_fsp(conn, req, fsp)) {
3591 error_to_writebrawerr(req);
3592 END_PROFILE(SMBwritebraw);
3593 return;
3596 if (!CHECK_WRITE(fsp)) {
3597 reply_doserror(req, ERRDOS, ERRbadaccess);
3598 error_to_writebrawerr(req);
3599 END_PROFILE(SMBwritebraw);
3600 return;
3603 tcount = IVAL(req->vwv+1, 0);
3604 startpos = IVAL_TO_SMB_OFF_T(req->vwv+3, 0);
3605 write_through = BITSETW(req->vwv+7,0);
3607 /* We have to deal with slightly different formats depending
3608 on whether we are using the core+ or lanman1.0 protocol */
3610 if(Protocol <= PROTOCOL_COREPLUS) {
3611 numtowrite = SVAL(smb_buf(req->inbuf),-2);
3612 data = smb_buf(req->inbuf);
3613 } else {
3614 numtowrite = SVAL(req->vwv+10, 0);
3615 data = smb_base(req->inbuf) + SVAL(req->vwv+11, 0);
3618 /* Ensure we don't write bytes past the end of this packet. */
3619 if (data + numtowrite > smb_base(req->inbuf) + smb_len(req->inbuf)) {
3620 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3621 error_to_writebrawerr(req);
3622 END_PROFILE(SMBwritebraw);
3623 return;
3626 init_strict_lock_struct(fsp, (uint32)req->smbpid,
3627 (uint64_t)startpos, (uint64_t)tcount, WRITE_LOCK,
3628 &lock);
3630 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
3631 reply_doserror(req, ERRDOS, ERRlock);
3632 error_to_writebrawerr(req);
3633 END_PROFILE(SMBwritebraw);
3634 return;
3637 if (numtowrite>0) {
3638 nwritten = write_file(req,fsp,data,startpos,numtowrite);
3641 DEBUG(3,("reply_writebraw: initial write fnum=%d start=%.0f num=%d "
3642 "wrote=%d sync=%d\n",
3643 fsp->fnum, (double)startpos, (int)numtowrite,
3644 (int)nwritten, (int)write_through));
3646 if (nwritten < (ssize_t)numtowrite) {
3647 reply_unixerror(req, ERRHRD, ERRdiskfull);
3648 error_to_writebrawerr(req);
3649 goto strict_unlock;
3652 total_written = nwritten;
3654 /* Allocate a buffer of 64k + length. */
3655 buf = TALLOC_ARRAY(NULL, char, 65540);
3656 if (!buf) {
3657 reply_doserror(req, ERRDOS, ERRnomem);
3658 error_to_writebrawerr(req);
3659 goto strict_unlock;
3662 /* Return a SMBwritebraw message to the redirector to tell
3663 * it to send more bytes */
3665 memcpy(buf, req->inbuf, smb_size);
3666 srv_set_message(buf,Protocol>PROTOCOL_COREPLUS?1:0,0,True);
3667 SCVAL(buf,smb_com,SMBwritebraw);
3668 SSVALS(buf,smb_vwv0,0xFFFF);
3669 show_msg(buf);
3670 if (!srv_send_smb(smbd_server_fd(),
3671 buf,
3672 IS_CONN_ENCRYPTED(conn),
3673 &req->pcd)) {
3674 exit_server_cleanly("reply_writebraw: srv_send_smb "
3675 "failed.");
3678 /* Now read the raw data into the buffer and write it */
3679 status = read_smb_length(smbd_server_fd(), buf, SMB_SECONDARY_WAIT,
3680 &numtowrite);
3681 if (!NT_STATUS_IS_OK(status)) {
3682 exit_server_cleanly("secondary writebraw failed");
3685 /* Set up outbuf to return the correct size */
3686 reply_outbuf(req, 1, 0);
3688 if (numtowrite != 0) {
3690 if (numtowrite > 0xFFFF) {
3691 DEBUG(0,("reply_writebraw: Oversize secondary write "
3692 "raw requested (%u). Terminating\n",
3693 (unsigned int)numtowrite ));
3694 exit_server_cleanly("secondary writebraw failed");
3697 if (tcount > nwritten+numtowrite) {
3698 DEBUG(3,("reply_writebraw: Client overestimated the "
3699 "write %d %d %d\n",
3700 (int)tcount,(int)nwritten,(int)numtowrite));
3703 status = read_data(smbd_server_fd(), buf+4, numtowrite);
3705 if (!NT_STATUS_IS_OK(status)) {
3706 DEBUG(0,("reply_writebraw: Oversize secondary write "
3707 "raw read failed (%s). Terminating\n",
3708 nt_errstr(status)));
3709 exit_server_cleanly("secondary writebraw failed");
3712 nwritten = write_file(req,fsp,buf+4,startpos+nwritten,numtowrite);
3713 if (nwritten == -1) {
3714 TALLOC_FREE(buf);
3715 reply_unixerror(req, ERRHRD, ERRdiskfull);
3716 error_to_writebrawerr(req);
3717 goto strict_unlock;
3720 if (nwritten < (ssize_t)numtowrite) {
3721 SCVAL(req->outbuf,smb_rcls,ERRHRD);
3722 SSVAL(req->outbuf,smb_err,ERRdiskfull);
3725 if (nwritten > 0) {
3726 total_written += nwritten;
3730 TALLOC_FREE(buf);
3731 SSVAL(req->outbuf,smb_vwv0,total_written);
3733 status = sync_file(conn, fsp, write_through);
3734 if (!NT_STATUS_IS_OK(status)) {
3735 DEBUG(5,("reply_writebraw: sync_file for %s returned %s\n",
3736 fsp->fsp_name, nt_errstr(status) ));
3737 reply_nterror(req, status);
3738 error_to_writebrawerr(req);
3739 goto strict_unlock;
3742 DEBUG(3,("reply_writebraw: secondart write fnum=%d start=%.0f num=%d "
3743 "wrote=%d\n",
3744 fsp->fnum, (double)startpos, (int)numtowrite,
3745 (int)total_written));
3747 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
3749 /* We won't return a status if write through is not selected - this
3750 * follows what WfWg does */
3751 END_PROFILE(SMBwritebraw);
3753 if (!write_through && total_written==tcount) {
3755 #if RABBIT_PELLET_FIX
3757 * Fix for "rabbit pellet" mode, trigger an early TCP ack by
3758 * sending a SMBkeepalive. Thanks to DaveCB at Sun for this.
3759 * JRA.
3761 if (!send_keepalive(smbd_server_fd())) {
3762 exit_server_cleanly("reply_writebraw: send of "
3763 "keepalive failed");
3765 #endif
3766 TALLOC_FREE(req->outbuf);
3768 return;
3770 strict_unlock:
3771 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
3773 END_PROFILE(SMBwritebraw);
3774 return;
3777 #undef DBGC_CLASS
3778 #define DBGC_CLASS DBGC_LOCKING
3780 /****************************************************************************
3781 Reply to a writeunlock (core+).
3782 ****************************************************************************/
3784 void reply_writeunlock(struct smb_request *req)
3786 connection_struct *conn = req->conn;
3787 ssize_t nwritten = -1;
3788 size_t numtowrite;
3789 SMB_OFF_T startpos;
3790 const char *data;
3791 NTSTATUS status = NT_STATUS_OK;
3792 files_struct *fsp;
3793 struct lock_struct lock;
3795 START_PROFILE(SMBwriteunlock);
3797 if (req->wct < 5) {
3798 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3799 END_PROFILE(SMBwriteunlock);
3800 return;
3803 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
3805 if (!check_fsp(conn, req, fsp)) {
3806 END_PROFILE(SMBwriteunlock);
3807 return;
3810 if (!CHECK_WRITE(fsp)) {
3811 reply_doserror(req, ERRDOS,ERRbadaccess);
3812 END_PROFILE(SMBwriteunlock);
3813 return;
3816 numtowrite = SVAL(req->vwv+1, 0);
3817 startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
3818 data = (const char *)req->buf + 3;
3820 if (numtowrite) {
3821 init_strict_lock_struct(fsp, (uint32)req->smbpid,
3822 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
3823 &lock);
3825 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
3826 reply_doserror(req, ERRDOS, ERRlock);
3827 END_PROFILE(SMBwriteunlock);
3828 return;
3832 /* The special X/Open SMB protocol handling of
3833 zero length writes is *NOT* done for
3834 this call */
3835 if(numtowrite == 0) {
3836 nwritten = 0;
3837 } else {
3838 nwritten = write_file(req,fsp,data,startpos,numtowrite);
3841 status = sync_file(conn, fsp, False /* write through */);
3842 if (!NT_STATUS_IS_OK(status)) {
3843 DEBUG(5,("reply_writeunlock: sync_file for %s returned %s\n",
3844 fsp->fsp_name, nt_errstr(status) ));
3845 reply_nterror(req, status);
3846 goto strict_unlock;
3849 if(((nwritten < numtowrite) && (numtowrite != 0))||(nwritten < 0)) {
3850 reply_unixerror(req, ERRHRD, ERRdiskfull);
3851 goto strict_unlock;
3854 if (numtowrite) {
3855 status = do_unlock(smbd_messaging_context(),
3856 fsp,
3857 req->smbpid,
3858 (uint64_t)numtowrite,
3859 (uint64_t)startpos,
3860 WINDOWS_LOCK);
3862 if (NT_STATUS_V(status)) {
3863 reply_nterror(req, status);
3864 goto strict_unlock;
3868 reply_outbuf(req, 1, 0);
3870 SSVAL(req->outbuf,smb_vwv0,nwritten);
3872 DEBUG(3,("writeunlock fnum=%d num=%d wrote=%d\n",
3873 fsp->fnum, (int)numtowrite, (int)nwritten));
3875 strict_unlock:
3876 if (numtowrite) {
3877 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
3880 END_PROFILE(SMBwriteunlock);
3881 return;
3884 #undef DBGC_CLASS
3885 #define DBGC_CLASS DBGC_ALL
3887 /****************************************************************************
3888 Reply to a write.
3889 ****************************************************************************/
3891 void reply_write(struct smb_request *req)
3893 connection_struct *conn = req->conn;
3894 size_t numtowrite;
3895 ssize_t nwritten = -1;
3896 SMB_OFF_T startpos;
3897 const char *data;
3898 files_struct *fsp;
3899 struct lock_struct lock;
3900 NTSTATUS status;
3902 START_PROFILE(SMBwrite);
3904 if (req->wct < 5) {
3905 END_PROFILE(SMBwrite);
3906 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3907 return;
3910 /* If it's an IPC, pass off the pipe handler. */
3911 if (IS_IPC(conn)) {
3912 reply_pipe_write(req);
3913 END_PROFILE(SMBwrite);
3914 return;
3917 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
3919 if (!check_fsp(conn, req, fsp)) {
3920 END_PROFILE(SMBwrite);
3921 return;
3924 if (!CHECK_WRITE(fsp)) {
3925 reply_doserror(req, ERRDOS, ERRbadaccess);
3926 END_PROFILE(SMBwrite);
3927 return;
3930 numtowrite = SVAL(req->vwv+1, 0);
3931 startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
3932 data = (const char *)req->buf + 3;
3934 init_strict_lock_struct(fsp, (uint32)req->smbpid,
3935 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
3936 &lock);
3938 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
3939 reply_doserror(req, ERRDOS, ERRlock);
3940 END_PROFILE(SMBwrite);
3941 return;
3945 * X/Open SMB protocol says that if smb_vwv1 is
3946 * zero then the file size should be extended or
3947 * truncated to the size given in smb_vwv[2-3].
3950 if(numtowrite == 0) {
3952 * This is actually an allocate call, and set EOF. JRA.
3954 nwritten = vfs_allocate_file_space(fsp, (SMB_OFF_T)startpos);
3955 if (nwritten < 0) {
3956 reply_nterror(req, NT_STATUS_DISK_FULL);
3957 goto strict_unlock;
3959 nwritten = vfs_set_filelen(fsp, (SMB_OFF_T)startpos);
3960 if (nwritten < 0) {
3961 reply_nterror(req, NT_STATUS_DISK_FULL);
3962 goto strict_unlock;
3964 trigger_write_time_update_immediate(fsp);
3965 } else {
3966 nwritten = write_file(req,fsp,data,startpos,numtowrite);
3969 status = sync_file(conn, fsp, False);
3970 if (!NT_STATUS_IS_OK(status)) {
3971 DEBUG(5,("reply_write: sync_file for %s returned %s\n",
3972 fsp->fsp_name, nt_errstr(status) ));
3973 reply_nterror(req, status);
3974 goto strict_unlock;
3977 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
3978 reply_unixerror(req, ERRHRD, ERRdiskfull);
3979 goto strict_unlock;
3982 reply_outbuf(req, 1, 0);
3984 SSVAL(req->outbuf,smb_vwv0,nwritten);
3986 if (nwritten < (ssize_t)numtowrite) {
3987 SCVAL(req->outbuf,smb_rcls,ERRHRD);
3988 SSVAL(req->outbuf,smb_err,ERRdiskfull);
3991 DEBUG(3,("write fnum=%d num=%d wrote=%d\n", fsp->fnum, (int)numtowrite, (int)nwritten));
3993 strict_unlock:
3994 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
3996 END_PROFILE(SMBwrite);
3997 return;
4000 /****************************************************************************
4001 Ensure a buffer is a valid writeX for recvfile purposes.
4002 ****************************************************************************/
4004 #define STANDARD_WRITE_AND_X_HEADER_SIZE (smb_size - 4 + /* basic header */ \
4005 (2*14) + /* word count (including bcc) */ \
4006 1 /* pad byte */)
4008 bool is_valid_writeX_buffer(const uint8_t *inbuf)
4010 size_t numtowrite;
4011 connection_struct *conn = NULL;
4012 unsigned int doff = 0;
4013 size_t len = smb_len_large(inbuf);
4015 if (is_encrypted_packet(inbuf)) {
4016 /* Can't do this on encrypted
4017 * connections. */
4018 return false;
4021 if (CVAL(inbuf,smb_com) != SMBwriteX) {
4022 return false;
4025 if (CVAL(inbuf,smb_vwv0) != 0xFF ||
4026 CVAL(inbuf,smb_wct) != 14) {
4027 DEBUG(10,("is_valid_writeX_buffer: chained or "
4028 "invalid word length.\n"));
4029 return false;
4032 conn = conn_find(SVAL(inbuf, smb_tid));
4033 if (conn == NULL) {
4034 DEBUG(10,("is_valid_writeX_buffer: bad tid\n"));
4035 return false;
4037 if (IS_IPC(conn)) {
4038 DEBUG(10,("is_valid_writeX_buffer: IPC$ tid\n"));
4039 return false;
4041 if (IS_PRINT(conn)) {
4042 DEBUG(10,("is_valid_writeX_buffer: printing tid\n"));
4043 return false;
4045 doff = SVAL(inbuf,smb_vwv11);
4047 numtowrite = SVAL(inbuf,smb_vwv10);
4049 if (len > doff && len - doff > 0xFFFF) {
4050 numtowrite |= (((size_t)SVAL(inbuf,smb_vwv9))<<16);
4053 if (numtowrite == 0) {
4054 DEBUG(10,("is_valid_writeX_buffer: zero write\n"));
4055 return false;
4058 /* Ensure the sizes match up. */
4059 if (doff < STANDARD_WRITE_AND_X_HEADER_SIZE) {
4060 /* no pad byte...old smbclient :-( */
4061 DEBUG(10,("is_valid_writeX_buffer: small doff %u (min %u)\n",
4062 (unsigned int)doff,
4063 (unsigned int)STANDARD_WRITE_AND_X_HEADER_SIZE));
4064 return false;
4067 if (len - doff != numtowrite) {
4068 DEBUG(10,("is_valid_writeX_buffer: doff mismatch "
4069 "len = %u, doff = %u, numtowrite = %u\n",
4070 (unsigned int)len,
4071 (unsigned int)doff,
4072 (unsigned int)numtowrite ));
4073 return false;
4076 DEBUG(10,("is_valid_writeX_buffer: true "
4077 "len = %u, doff = %u, numtowrite = %u\n",
4078 (unsigned int)len,
4079 (unsigned int)doff,
4080 (unsigned int)numtowrite ));
4082 return true;
4085 /****************************************************************************
4086 Reply to a write and X.
4087 ****************************************************************************/
4089 void reply_write_and_X(struct smb_request *req)
4091 connection_struct *conn = req->conn;
4092 files_struct *fsp;
4093 struct lock_struct lock;
4094 SMB_OFF_T startpos;
4095 size_t numtowrite;
4096 bool write_through;
4097 ssize_t nwritten;
4098 unsigned int smb_doff;
4099 unsigned int smblen;
4100 char *data;
4101 NTSTATUS status;
4103 START_PROFILE(SMBwriteX);
4105 if ((req->wct != 12) && (req->wct != 14)) {
4106 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4107 END_PROFILE(SMBwriteX);
4108 return;
4111 numtowrite = SVAL(req->vwv+10, 0);
4112 smb_doff = SVAL(req->vwv+11, 0);
4113 smblen = smb_len(req->inbuf);
4115 if (req->unread_bytes > 0xFFFF ||
4116 (smblen > smb_doff &&
4117 smblen - smb_doff > 0xFFFF)) {
4118 numtowrite |= (((size_t)SVAL(req->vwv+9, 0))<<16);
4121 if (req->unread_bytes) {
4122 /* Can't do a recvfile write on IPC$ */
4123 if (IS_IPC(conn)) {
4124 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4125 END_PROFILE(SMBwriteX);
4126 return;
4128 if (numtowrite != req->unread_bytes) {
4129 reply_doserror(req, ERRDOS, ERRbadmem);
4130 END_PROFILE(SMBwriteX);
4131 return;
4133 } else {
4134 if (smb_doff > smblen || smb_doff + numtowrite < numtowrite ||
4135 smb_doff + numtowrite > smblen) {
4136 reply_doserror(req, ERRDOS, ERRbadmem);
4137 END_PROFILE(SMBwriteX);
4138 return;
4142 /* If it's an IPC, pass off the pipe handler. */
4143 if (IS_IPC(conn)) {
4144 if (req->unread_bytes) {
4145 reply_doserror(req, ERRDOS, ERRbadmem);
4146 END_PROFILE(SMBwriteX);
4147 return;
4149 reply_pipe_write_and_X(req);
4150 END_PROFILE(SMBwriteX);
4151 return;
4154 fsp = file_fsp(req, SVAL(req->vwv+2, 0));
4155 startpos = IVAL_TO_SMB_OFF_T(req->vwv+3, 0);
4156 write_through = BITSETW(req->vwv+7,0);
4158 if (!check_fsp(conn, req, fsp)) {
4159 END_PROFILE(SMBwriteX);
4160 return;
4163 if (!CHECK_WRITE(fsp)) {
4164 reply_doserror(req, ERRDOS, ERRbadaccess);
4165 END_PROFILE(SMBwriteX);
4166 return;
4169 data = smb_base(req->inbuf) + smb_doff;
4171 if(req->wct == 14) {
4172 #ifdef LARGE_SMB_OFF_T
4174 * This is a large offset (64 bit) write.
4176 startpos |= (((SMB_OFF_T)IVAL(req->vwv+12, 0)) << 32);
4178 #else /* !LARGE_SMB_OFF_T */
4181 * Ensure we haven't been sent a >32 bit offset.
4184 if(IVAL(req->vwv+12, 0) != 0) {
4185 DEBUG(0,("reply_write_and_X - large offset (%x << 32) "
4186 "used and we don't support 64 bit offsets.\n",
4187 (unsigned int)IVAL(req->vwv+12, 0) ));
4188 reply_doserror(req, ERRDOS, ERRbadaccess);
4189 END_PROFILE(SMBwriteX);
4190 return;
4193 #endif /* LARGE_SMB_OFF_T */
4196 init_strict_lock_struct(fsp, (uint32)req->smbpid,
4197 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
4198 &lock);
4200 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
4201 reply_doserror(req, ERRDOS, ERRlock);
4202 END_PROFILE(SMBwriteX);
4203 return;
4206 /* X/Open SMB protocol says that, unlike SMBwrite
4207 if the length is zero then NO truncation is
4208 done, just a write of zero. To truncate a file,
4209 use SMBwrite. */
4211 if(numtowrite == 0) {
4212 nwritten = 0;
4213 } else {
4215 if ((req->unread_bytes == 0) &&
4216 schedule_aio_write_and_X(conn, req, fsp, data, startpos,
4217 numtowrite)) {
4218 goto strict_unlock;
4221 nwritten = write_file(req,fsp,data,startpos,numtowrite);
4224 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
4225 reply_unixerror(req, ERRHRD, ERRdiskfull);
4226 goto strict_unlock;
4229 reply_outbuf(req, 6, 0);
4230 SSVAL(req->outbuf,smb_vwv2,nwritten);
4231 SSVAL(req->outbuf,smb_vwv4,nwritten>>16);
4233 if (nwritten < (ssize_t)numtowrite) {
4234 SCVAL(req->outbuf,smb_rcls,ERRHRD);
4235 SSVAL(req->outbuf,smb_err,ERRdiskfull);
4238 DEBUG(3,("writeX fnum=%d num=%d wrote=%d\n",
4239 fsp->fnum, (int)numtowrite, (int)nwritten));
4241 status = sync_file(conn, fsp, write_through);
4242 if (!NT_STATUS_IS_OK(status)) {
4243 DEBUG(5,("reply_write_and_X: sync_file for %s returned %s\n",
4244 fsp->fsp_name, nt_errstr(status) ));
4245 reply_nterror(req, status);
4246 goto strict_unlock;
4249 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
4251 END_PROFILE(SMBwriteX);
4252 chain_reply(req);
4253 return;
4255 strict_unlock:
4256 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
4258 END_PROFILE(SMBwriteX);
4259 return;
4262 /****************************************************************************
4263 Reply to a lseek.
4264 ****************************************************************************/
4266 void reply_lseek(struct smb_request *req)
4268 connection_struct *conn = req->conn;
4269 SMB_OFF_T startpos;
4270 SMB_OFF_T res= -1;
4271 int mode,umode;
4272 files_struct *fsp;
4274 START_PROFILE(SMBlseek);
4276 if (req->wct < 4) {
4277 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4278 END_PROFILE(SMBlseek);
4279 return;
4282 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
4284 if (!check_fsp(conn, req, fsp)) {
4285 return;
4288 flush_write_cache(fsp, SEEK_FLUSH);
4290 mode = SVAL(req->vwv+1, 0) & 3;
4291 /* NB. This doesn't use IVAL_TO_SMB_OFF_T as startpos can be signed in this case. */
4292 startpos = (SMB_OFF_T)IVALS(req->vwv+2, 0);
4294 switch (mode) {
4295 case 0:
4296 umode = SEEK_SET;
4297 res = startpos;
4298 break;
4299 case 1:
4300 umode = SEEK_CUR;
4301 res = fsp->fh->pos + startpos;
4302 break;
4303 case 2:
4304 umode = SEEK_END;
4305 break;
4306 default:
4307 umode = SEEK_SET;
4308 res = startpos;
4309 break;
4312 if (umode == SEEK_END) {
4313 if((res = SMB_VFS_LSEEK(fsp,startpos,umode)) == -1) {
4314 if(errno == EINVAL) {
4315 SMB_OFF_T current_pos = startpos;
4316 SMB_STRUCT_STAT sbuf;
4318 if(SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
4319 reply_unixerror(req, ERRDOS,
4320 ERRnoaccess);
4321 END_PROFILE(SMBlseek);
4322 return;
4325 current_pos += sbuf.st_size;
4326 if(current_pos < 0)
4327 res = SMB_VFS_LSEEK(fsp,0,SEEK_SET);
4331 if(res == -1) {
4332 reply_unixerror(req, ERRDOS, ERRnoaccess);
4333 END_PROFILE(SMBlseek);
4334 return;
4338 fsp->fh->pos = res;
4340 reply_outbuf(req, 2, 0);
4341 SIVAL(req->outbuf,smb_vwv0,res);
4343 DEBUG(3,("lseek fnum=%d ofs=%.0f newpos = %.0f mode=%d\n",
4344 fsp->fnum, (double)startpos, (double)res, mode));
4346 END_PROFILE(SMBlseek);
4347 return;
4350 /****************************************************************************
4351 Reply to a flush.
4352 ****************************************************************************/
4354 void reply_flush(struct smb_request *req)
4356 connection_struct *conn = req->conn;
4357 uint16 fnum;
4358 files_struct *fsp;
4360 START_PROFILE(SMBflush);
4362 if (req->wct < 1) {
4363 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4364 return;
4367 fnum = SVAL(req->vwv+0, 0);
4368 fsp = file_fsp(req, fnum);
4370 if ((fnum != 0xFFFF) && !check_fsp(conn, req, fsp)) {
4371 return;
4374 if (!fsp) {
4375 file_sync_all(conn);
4376 } else {
4377 NTSTATUS status = sync_file(conn, fsp, True);
4378 if (!NT_STATUS_IS_OK(status)) {
4379 DEBUG(5,("reply_flush: sync_file for %s returned %s\n",
4380 fsp->fsp_name, nt_errstr(status) ));
4381 reply_nterror(req, status);
4382 END_PROFILE(SMBflush);
4383 return;
4387 reply_outbuf(req, 0, 0);
4389 DEBUG(3,("flush\n"));
4390 END_PROFILE(SMBflush);
4391 return;
4394 /****************************************************************************
4395 Reply to a exit.
4396 conn POINTER CAN BE NULL HERE !
4397 ****************************************************************************/
4399 void reply_exit(struct smb_request *req)
4401 START_PROFILE(SMBexit);
4403 file_close_pid(req->smbpid, req->vuid);
4405 reply_outbuf(req, 0, 0);
4407 DEBUG(3,("exit\n"));
4409 END_PROFILE(SMBexit);
4410 return;
4413 /****************************************************************************
4414 Reply to a close - has to deal with closing a directory opened by NT SMB's.
4415 ****************************************************************************/
4417 void reply_close(struct smb_request *req)
4419 connection_struct *conn = req->conn;
4420 NTSTATUS status = NT_STATUS_OK;
4421 files_struct *fsp = NULL;
4422 START_PROFILE(SMBclose);
4424 if (req->wct < 3) {
4425 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4426 END_PROFILE(SMBclose);
4427 return;
4430 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
4433 * We can only use check_fsp if we know it's not a directory.
4436 if(!fsp || (fsp->conn != conn) || (fsp->vuid != req->vuid)) {
4437 reply_doserror(req, ERRDOS, ERRbadfid);
4438 END_PROFILE(SMBclose);
4439 return;
4442 if(fsp->is_directory) {
4444 * Special case - close NT SMB directory handle.
4446 DEBUG(3,("close directory fnum=%d\n", fsp->fnum));
4447 status = close_file(req, fsp, NORMAL_CLOSE);
4448 } else {
4449 time_t t;
4451 * Close ordinary file.
4454 DEBUG(3,("close fd=%d fnum=%d (numopen=%d)\n",
4455 fsp->fh->fd, fsp->fnum,
4456 conn->num_files_open));
4459 * Take care of any time sent in the close.
4462 t = srv_make_unix_date3(req->vwv+1);
4463 set_close_write_time(fsp, convert_time_t_to_timespec(t));
4466 * close_file() returns the unix errno if an error
4467 * was detected on close - normally this is due to
4468 * a disk full error. If not then it was probably an I/O error.
4471 status = close_file(req, fsp, NORMAL_CLOSE);
4474 if (!NT_STATUS_IS_OK(status)) {
4475 reply_nterror(req, status);
4476 END_PROFILE(SMBclose);
4477 return;
4480 reply_outbuf(req, 0, 0);
4481 END_PROFILE(SMBclose);
4482 return;
4485 /****************************************************************************
4486 Reply to a writeclose (Core+ protocol).
4487 ****************************************************************************/
4489 void reply_writeclose(struct smb_request *req)
4491 connection_struct *conn = req->conn;
4492 size_t numtowrite;
4493 ssize_t nwritten = -1;
4494 NTSTATUS close_status = NT_STATUS_OK;
4495 SMB_OFF_T startpos;
4496 const char *data;
4497 struct timespec mtime;
4498 files_struct *fsp;
4499 struct lock_struct lock;
4501 START_PROFILE(SMBwriteclose);
4503 if (req->wct < 6) {
4504 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4505 END_PROFILE(SMBwriteclose);
4506 return;
4509 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
4511 if (!check_fsp(conn, req, fsp)) {
4512 END_PROFILE(SMBwriteclose);
4513 return;
4515 if (!CHECK_WRITE(fsp)) {
4516 reply_doserror(req, ERRDOS,ERRbadaccess);
4517 END_PROFILE(SMBwriteclose);
4518 return;
4521 numtowrite = SVAL(req->vwv+1, 0);
4522 startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
4523 mtime = convert_time_t_to_timespec(srv_make_unix_date3(req->vwv+4));
4524 data = (const char *)req->buf + 1;
4526 if (numtowrite) {
4527 init_strict_lock_struct(fsp, (uint32)req->smbpid,
4528 (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
4529 &lock);
4531 if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
4532 reply_doserror(req, ERRDOS,ERRlock);
4533 END_PROFILE(SMBwriteclose);
4534 return;
4538 nwritten = write_file(req,fsp,data,startpos,numtowrite);
4540 set_close_write_time(fsp, mtime);
4543 * More insanity. W2K only closes the file if writelen > 0.
4544 * JRA.
4547 if (numtowrite) {
4548 DEBUG(3,("reply_writeclose: zero length write doesn't close file %s\n",
4549 fsp->fsp_name ));
4550 close_status = close_file(req, fsp, NORMAL_CLOSE);
4553 DEBUG(3,("writeclose fnum=%d num=%d wrote=%d (numopen=%d)\n",
4554 fsp->fnum, (int)numtowrite, (int)nwritten,
4555 conn->num_files_open));
4557 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
4558 reply_doserror(req, ERRHRD, ERRdiskfull);
4559 goto strict_unlock;
4562 if(!NT_STATUS_IS_OK(close_status)) {
4563 reply_nterror(req, close_status);
4564 goto strict_unlock;
4567 reply_outbuf(req, 1, 0);
4569 SSVAL(req->outbuf,smb_vwv0,nwritten);
4571 strict_unlock:
4572 if (numtowrite) {
4573 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
4576 END_PROFILE(SMBwriteclose);
4577 return;
4580 #undef DBGC_CLASS
4581 #define DBGC_CLASS DBGC_LOCKING
4583 /****************************************************************************
4584 Reply to a lock.
4585 ****************************************************************************/
4587 void reply_lock(struct smb_request *req)
4589 connection_struct *conn = req->conn;
4590 uint64_t count,offset;
4591 NTSTATUS status;
4592 files_struct *fsp;
4593 struct byte_range_lock *br_lck = NULL;
4595 START_PROFILE(SMBlock);
4597 if (req->wct < 5) {
4598 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4599 END_PROFILE(SMBlock);
4600 return;
4603 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
4605 if (!check_fsp(conn, req, fsp)) {
4606 END_PROFILE(SMBlock);
4607 return;
4610 count = (uint64_t)IVAL(req->vwv+1, 0);
4611 offset = (uint64_t)IVAL(req->vwv+3, 0);
4613 DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
4614 fsp->fh->fd, fsp->fnum, (double)offset, (double)count));
4616 br_lck = do_lock(smbd_messaging_context(),
4617 fsp,
4618 req->smbpid,
4619 count,
4620 offset,
4621 WRITE_LOCK,
4622 WINDOWS_LOCK,
4623 False, /* Non-blocking lock. */
4624 &status,
4625 NULL,
4626 NULL);
4628 TALLOC_FREE(br_lck);
4630 if (NT_STATUS_V(status)) {
4631 reply_nterror(req, status);
4632 END_PROFILE(SMBlock);
4633 return;
4636 reply_outbuf(req, 0, 0);
4638 END_PROFILE(SMBlock);
4639 return;
4642 /****************************************************************************
4643 Reply to a unlock.
4644 ****************************************************************************/
4646 void reply_unlock(struct smb_request *req)
4648 connection_struct *conn = req->conn;
4649 uint64_t count,offset;
4650 NTSTATUS status;
4651 files_struct *fsp;
4653 START_PROFILE(SMBunlock);
4655 if (req->wct < 5) {
4656 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4657 END_PROFILE(SMBunlock);
4658 return;
4661 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
4663 if (!check_fsp(conn, req, fsp)) {
4664 END_PROFILE(SMBunlock);
4665 return;
4668 count = (uint64_t)IVAL(req->vwv+1, 0);
4669 offset = (uint64_t)IVAL(req->vwv+3, 0);
4671 status = do_unlock(smbd_messaging_context(),
4672 fsp,
4673 req->smbpid,
4674 count,
4675 offset,
4676 WINDOWS_LOCK);
4678 if (NT_STATUS_V(status)) {
4679 reply_nterror(req, status);
4680 END_PROFILE(SMBunlock);
4681 return;
4684 DEBUG( 3, ( "unlock fd=%d fnum=%d offset=%.0f count=%.0f\n",
4685 fsp->fh->fd, fsp->fnum, (double)offset, (double)count ) );
4687 reply_outbuf(req, 0, 0);
4689 END_PROFILE(SMBunlock);
4690 return;
4693 #undef DBGC_CLASS
4694 #define DBGC_CLASS DBGC_ALL
4696 /****************************************************************************
4697 Reply to a tdis.
4698 conn POINTER CAN BE NULL HERE !
4699 ****************************************************************************/
4701 void reply_tdis(struct smb_request *req)
4703 connection_struct *conn = req->conn;
4704 START_PROFILE(SMBtdis);
4706 if (!conn) {
4707 DEBUG(4,("Invalid connection in tdis\n"));
4708 reply_doserror(req, ERRSRV, ERRinvnid);
4709 END_PROFILE(SMBtdis);
4710 return;
4713 conn->used = False;
4715 close_cnum(conn,req->vuid);
4716 req->conn = NULL;
4718 reply_outbuf(req, 0, 0);
4719 END_PROFILE(SMBtdis);
4720 return;
4723 /****************************************************************************
4724 Reply to a echo.
4725 conn POINTER CAN BE NULL HERE !
4726 ****************************************************************************/
4728 void reply_echo(struct smb_request *req)
4730 connection_struct *conn = req->conn;
4731 struct smb_perfcount_data local_pcd;
4732 struct smb_perfcount_data *cur_pcd;
4733 int smb_reverb;
4734 int seq_num;
4736 START_PROFILE(SMBecho);
4738 smb_init_perfcount_data(&local_pcd);
4740 if (req->wct < 1) {
4741 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4742 END_PROFILE(SMBecho);
4743 return;
4746 smb_reverb = SVAL(req->vwv+0, 0);
4748 reply_outbuf(req, 1, req->buflen);
4750 /* copy any incoming data back out */
4751 if (req->buflen > 0) {
4752 memcpy(smb_buf(req->outbuf), req->buf, req->buflen);
4755 if (smb_reverb > 100) {
4756 DEBUG(0,("large reverb (%d)?? Setting to 100\n",smb_reverb));
4757 smb_reverb = 100;
4760 for (seq_num = 1 ; seq_num <= smb_reverb ; seq_num++) {
4762 /* this makes sure we catch the request pcd */
4763 if (seq_num == smb_reverb) {
4764 cur_pcd = &req->pcd;
4765 } else {
4766 SMB_PERFCOUNT_COPY_CONTEXT(&req->pcd, &local_pcd);
4767 cur_pcd = &local_pcd;
4770 SSVAL(req->outbuf,smb_vwv0,seq_num);
4772 show_msg((char *)req->outbuf);
4773 if (!srv_send_smb(smbd_server_fd(),
4774 (char *)req->outbuf,
4775 IS_CONN_ENCRYPTED(conn)||req->encrypted,
4776 cur_pcd))
4777 exit_server_cleanly("reply_echo: srv_send_smb failed.");
4780 DEBUG(3,("echo %d times\n", smb_reverb));
4782 TALLOC_FREE(req->outbuf);
4784 END_PROFILE(SMBecho);
4785 return;
4788 /****************************************************************************
4789 Reply to a printopen.
4790 ****************************************************************************/
4792 void reply_printopen(struct smb_request *req)
4794 connection_struct *conn = req->conn;
4795 files_struct *fsp;
4796 SMB_STRUCT_STAT sbuf;
4797 NTSTATUS status;
4799 START_PROFILE(SMBsplopen);
4801 if (req->wct < 2) {
4802 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4803 END_PROFILE(SMBsplopen);
4804 return;
4807 if (!CAN_PRINT(conn)) {
4808 reply_doserror(req, ERRDOS, ERRnoaccess);
4809 END_PROFILE(SMBsplopen);
4810 return;
4813 status = file_new(req, conn, &fsp);
4814 if(!NT_STATUS_IS_OK(status)) {
4815 reply_nterror(req, status);
4816 END_PROFILE(SMBsplopen);
4817 return;
4820 /* Open for exclusive use, write only. */
4821 status = print_fsp_open(req, conn, NULL, req->vuid, fsp, &sbuf);
4823 if (!NT_STATUS_IS_OK(status)) {
4824 reply_nterror(req, status);
4825 END_PROFILE(SMBsplopen);
4826 return;
4829 reply_outbuf(req, 1, 0);
4830 SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
4832 DEBUG(3,("openprint fd=%d fnum=%d\n",
4833 fsp->fh->fd, fsp->fnum));
4835 END_PROFILE(SMBsplopen);
4836 return;
4839 /****************************************************************************
4840 Reply to a printclose.
4841 ****************************************************************************/
4843 void reply_printclose(struct smb_request *req)
4845 connection_struct *conn = req->conn;
4846 files_struct *fsp;
4847 NTSTATUS status;
4849 START_PROFILE(SMBsplclose);
4851 if (req->wct < 1) {
4852 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4853 END_PROFILE(SMBsplclose);
4854 return;
4857 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
4859 if (!check_fsp(conn, req, fsp)) {
4860 END_PROFILE(SMBsplclose);
4861 return;
4864 if (!CAN_PRINT(conn)) {
4865 reply_nterror(req, NT_STATUS_DOS(ERRSRV, ERRerror));
4866 END_PROFILE(SMBsplclose);
4867 return;
4870 DEBUG(3,("printclose fd=%d fnum=%d\n",
4871 fsp->fh->fd,fsp->fnum));
4873 status = close_file(req, fsp, NORMAL_CLOSE);
4875 if(!NT_STATUS_IS_OK(status)) {
4876 reply_nterror(req, status);
4877 END_PROFILE(SMBsplclose);
4878 return;
4881 reply_outbuf(req, 0, 0);
4883 END_PROFILE(SMBsplclose);
4884 return;
4887 /****************************************************************************
4888 Reply to a printqueue.
4889 ****************************************************************************/
4891 void reply_printqueue(struct smb_request *req)
4893 connection_struct *conn = req->conn;
4894 int max_count;
4895 int start_index;
4897 START_PROFILE(SMBsplretq);
4899 if (req->wct < 2) {
4900 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4901 END_PROFILE(SMBsplretq);
4902 return;
4905 max_count = SVAL(req->vwv+0, 0);
4906 start_index = SVAL(req->vwv+1, 0);
4908 /* we used to allow the client to get the cnum wrong, but that
4909 is really quite gross and only worked when there was only
4910 one printer - I think we should now only accept it if they
4911 get it right (tridge) */
4912 if (!CAN_PRINT(conn)) {
4913 reply_doserror(req, ERRDOS, ERRnoaccess);
4914 END_PROFILE(SMBsplretq);
4915 return;
4918 reply_outbuf(req, 2, 3);
4919 SSVAL(req->outbuf,smb_vwv0,0);
4920 SSVAL(req->outbuf,smb_vwv1,0);
4921 SCVAL(smb_buf(req->outbuf),0,1);
4922 SSVAL(smb_buf(req->outbuf),1,0);
4924 DEBUG(3,("printqueue start_index=%d max_count=%d\n",
4925 start_index, max_count));
4928 print_queue_struct *queue = NULL;
4929 print_status_struct status;
4930 int count = print_queue_status(SNUM(conn), &queue, &status);
4931 int num_to_get = ABS(max_count);
4932 int first = (max_count>0?start_index:start_index+max_count+1);
4933 int i;
4935 if (first >= count)
4936 num_to_get = 0;
4937 else
4938 num_to_get = MIN(num_to_get,count-first);
4941 for (i=first;i<first+num_to_get;i++) {
4942 char blob[28];
4943 char *p = blob;
4945 srv_put_dos_date2(p,0,queue[i].time);
4946 SCVAL(p,4,(queue[i].status==LPQ_PRINTING?2:3));
4947 SSVAL(p,5, queue[i].job);
4948 SIVAL(p,7,queue[i].size);
4949 SCVAL(p,11,0);
4950 srvstr_push(blob, req->flags2, p+12,
4951 queue[i].fs_user, 16, STR_ASCII);
4953 if (message_push_blob(
4954 &req->outbuf,
4955 data_blob_const(
4956 blob, sizeof(blob))) == -1) {
4957 reply_nterror(req, NT_STATUS_NO_MEMORY);
4958 END_PROFILE(SMBsplretq);
4959 return;
4963 if (count > 0) {
4964 SSVAL(req->outbuf,smb_vwv0,count);
4965 SSVAL(req->outbuf,smb_vwv1,
4966 (max_count>0?first+count:first-1));
4967 SCVAL(smb_buf(req->outbuf),0,1);
4968 SSVAL(smb_buf(req->outbuf),1,28*count);
4971 SAFE_FREE(queue);
4973 DEBUG(3,("%d entries returned in queue\n",count));
4976 END_PROFILE(SMBsplretq);
4977 return;
4980 /****************************************************************************
4981 Reply to a printwrite.
4982 ****************************************************************************/
4984 void reply_printwrite(struct smb_request *req)
4986 connection_struct *conn = req->conn;
4987 int numtowrite;
4988 const char *data;
4989 files_struct *fsp;
4991 START_PROFILE(SMBsplwr);
4993 if (req->wct < 1) {
4994 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4995 END_PROFILE(SMBsplwr);
4996 return;
4999 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
5001 if (!check_fsp(conn, req, fsp)) {
5002 END_PROFILE(SMBsplwr);
5003 return;
5006 if (!CAN_PRINT(conn)) {
5007 reply_doserror(req, ERRDOS, ERRnoaccess);
5008 END_PROFILE(SMBsplwr);
5009 return;
5012 if (!CHECK_WRITE(fsp)) {
5013 reply_doserror(req, ERRDOS, ERRbadaccess);
5014 END_PROFILE(SMBsplwr);
5015 return;
5018 numtowrite = SVAL(req->buf, 1);
5020 if (req->buflen < numtowrite + 3) {
5021 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
5022 END_PROFILE(SMBsplwr);
5023 return;
5026 data = (const char *)req->buf + 3;
5028 if (write_file(req,fsp,data,-1,numtowrite) != numtowrite) {
5029 reply_unixerror(req, ERRHRD, ERRdiskfull);
5030 END_PROFILE(SMBsplwr);
5031 return;
5034 DEBUG( 3, ( "printwrite fnum=%d num=%d\n", fsp->fnum, numtowrite ) );
5036 END_PROFILE(SMBsplwr);
5037 return;
5040 /****************************************************************************
5041 Reply to a mkdir.
5042 ****************************************************************************/
5044 void reply_mkdir(struct smb_request *req)
5046 connection_struct *conn = req->conn;
5047 char *directory = NULL;
5048 NTSTATUS status;
5049 SMB_STRUCT_STAT sbuf;
5050 TALLOC_CTX *ctx = talloc_tos();
5052 START_PROFILE(SMBmkdir);
5054 srvstr_get_path_req(ctx, req, &directory, (const char *)req->buf + 1,
5055 STR_TERMINATE, &status);
5056 if (!NT_STATUS_IS_OK(status)) {
5057 reply_nterror(req, status);
5058 END_PROFILE(SMBmkdir);
5059 return;
5062 status = resolve_dfspath(ctx, conn,
5063 req->flags2 & FLAGS2_DFS_PATHNAMES,
5064 directory,
5065 &directory);
5066 if (!NT_STATUS_IS_OK(status)) {
5067 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
5068 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
5069 ERRSRV, ERRbadpath);
5070 END_PROFILE(SMBmkdir);
5071 return;
5073 reply_nterror(req, status);
5074 END_PROFILE(SMBmkdir);
5075 return;
5078 status = unix_convert(ctx, conn, directory, False, &directory, NULL, &sbuf);
5079 if (!NT_STATUS_IS_OK(status)) {
5080 reply_nterror(req, status);
5081 END_PROFILE(SMBmkdir);
5082 return;
5085 status = check_name(conn, directory);
5086 if (!NT_STATUS_IS_OK(status)) {
5087 reply_nterror(req, status);
5088 END_PROFILE(SMBmkdir);
5089 return;
5092 status = create_directory(conn, req, directory);
5094 DEBUG(5, ("create_directory returned %s\n", nt_errstr(status)));
5096 if (!NT_STATUS_IS_OK(status)) {
5098 if (!use_nt_status()
5099 && NT_STATUS_EQUAL(status,
5100 NT_STATUS_OBJECT_NAME_COLLISION)) {
5102 * Yes, in the DOS error code case we get a
5103 * ERRDOS:ERRnoaccess here. See BASE-SAMBA3ERROR
5104 * samba4 torture test.
5106 status = NT_STATUS_DOS(ERRDOS, ERRnoaccess);
5109 reply_nterror(req, status);
5110 END_PROFILE(SMBmkdir);
5111 return;
5114 reply_outbuf(req, 0, 0);
5116 DEBUG( 3, ( "mkdir %s\n", directory ) );
5118 END_PROFILE(SMBmkdir);
5119 return;
5122 /****************************************************************************
5123 Static function used by reply_rmdir to delete an entire directory
5124 tree recursively. Return True on ok, False on fail.
5125 ****************************************************************************/
5127 static bool recursive_rmdir(TALLOC_CTX *ctx,
5128 connection_struct *conn,
5129 char *directory)
5131 const char *dname = NULL;
5132 bool ret = True;
5133 long offset = 0;
5134 SMB_STRUCT_STAT st;
5135 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn, directory,
5136 NULL, 0);
5138 if(dir_hnd == NULL)
5139 return False;
5141 while((dname = ReadDirName(dir_hnd, &offset, &st))) {
5142 char *fullname = NULL;
5144 if (ISDOT(dname) || ISDOTDOT(dname)) {
5145 continue;
5148 if (!is_visible_file(conn, directory, dname, &st, False)) {
5149 continue;
5152 /* Construct the full name. */
5153 fullname = talloc_asprintf(ctx,
5154 "%s/%s",
5155 directory,
5156 dname);
5157 if (!fullname) {
5158 errno = ENOMEM;
5159 ret = False;
5160 break;
5163 if(SMB_VFS_LSTAT(conn,fullname, &st) != 0) {
5164 ret = False;
5165 break;
5168 if(st.st_mode & S_IFDIR) {
5169 if(!recursive_rmdir(ctx, conn, fullname)) {
5170 ret = False;
5171 break;
5173 if(SMB_VFS_RMDIR(conn,fullname) != 0) {
5174 ret = False;
5175 break;
5177 } else if(SMB_VFS_UNLINK(conn,fullname) != 0) {
5178 ret = False;
5179 break;
5181 TALLOC_FREE(fullname);
5183 TALLOC_FREE(dir_hnd);
5184 return ret;
5187 /****************************************************************************
5188 The internals of the rmdir code - called elsewhere.
5189 ****************************************************************************/
5191 NTSTATUS rmdir_internals(TALLOC_CTX *ctx,
5192 connection_struct *conn,
5193 const char *directory)
5195 int ret;
5196 SMB_STRUCT_STAT st;
5198 /* Might be a symlink. */
5199 if(SMB_VFS_LSTAT(conn, directory, &st) != 0) {
5200 return map_nt_error_from_unix(errno);
5203 if (S_ISLNK(st.st_mode)) {
5204 /* Is what it points to a directory ? */
5205 if(SMB_VFS_STAT(conn, directory, &st) != 0) {
5206 return map_nt_error_from_unix(errno);
5208 if (!(S_ISDIR(st.st_mode))) {
5209 return NT_STATUS_NOT_A_DIRECTORY;
5211 ret = SMB_VFS_UNLINK(conn,directory);
5212 } else {
5213 ret = SMB_VFS_RMDIR(conn,directory);
5215 if (ret == 0) {
5216 notify_fname(conn, NOTIFY_ACTION_REMOVED,
5217 FILE_NOTIFY_CHANGE_DIR_NAME,
5218 directory);
5219 return NT_STATUS_OK;
5222 if(((errno == ENOTEMPTY)||(errno == EEXIST)) && lp_veto_files(SNUM(conn))) {
5224 * Check to see if the only thing in this directory are
5225 * vetoed files/directories. If so then delete them and
5226 * retry. If we fail to delete any of them (and we *don't*
5227 * do a recursive delete) then fail the rmdir.
5229 const char *dname;
5230 long dirpos = 0;
5231 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
5232 directory, NULL, 0);
5234 if(dir_hnd == NULL) {
5235 errno = ENOTEMPTY;
5236 goto err;
5239 while ((dname = ReadDirName(dir_hnd, &dirpos, &st))) {
5240 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
5241 continue;
5242 if (!is_visible_file(conn, directory, dname, &st, False))
5243 continue;
5244 if(!IS_VETO_PATH(conn, dname)) {
5245 TALLOC_FREE(dir_hnd);
5246 errno = ENOTEMPTY;
5247 goto err;
5251 /* We only have veto files/directories.
5252 * Are we allowed to delete them ? */
5254 if(!lp_recursive_veto_delete(SNUM(conn))) {
5255 TALLOC_FREE(dir_hnd);
5256 errno = ENOTEMPTY;
5257 goto err;
5260 /* Do a recursive delete. */
5261 RewindDir(dir_hnd,&dirpos);
5262 while ((dname = ReadDirName(dir_hnd, &dirpos, &st))) {
5263 char *fullname = NULL;
5265 if (ISDOT(dname) || ISDOTDOT(dname)) {
5266 continue;
5268 if (!is_visible_file(conn, directory, dname, &st, False)) {
5269 continue;
5272 fullname = talloc_asprintf(ctx,
5273 "%s/%s",
5274 directory,
5275 dname);
5277 if(!fullname) {
5278 errno = ENOMEM;
5279 break;
5282 if(SMB_VFS_LSTAT(conn,fullname, &st) != 0) {
5283 break;
5285 if(st.st_mode & S_IFDIR) {
5286 if(!recursive_rmdir(ctx, conn, fullname)) {
5287 break;
5289 if(SMB_VFS_RMDIR(conn,fullname) != 0) {
5290 break;
5292 } else if(SMB_VFS_UNLINK(conn,fullname) != 0) {
5293 break;
5295 TALLOC_FREE(fullname);
5297 TALLOC_FREE(dir_hnd);
5298 /* Retry the rmdir */
5299 ret = SMB_VFS_RMDIR(conn,directory);
5302 err:
5304 if (ret != 0) {
5305 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
5306 "%s\n", directory,strerror(errno)));
5307 return map_nt_error_from_unix(errno);
5310 notify_fname(conn, NOTIFY_ACTION_REMOVED,
5311 FILE_NOTIFY_CHANGE_DIR_NAME,
5312 directory);
5314 return NT_STATUS_OK;
5317 /****************************************************************************
5318 Reply to a rmdir.
5319 ****************************************************************************/
5321 void reply_rmdir(struct smb_request *req)
5323 connection_struct *conn = req->conn;
5324 char *directory = NULL;
5325 SMB_STRUCT_STAT sbuf;
5326 NTSTATUS status;
5327 TALLOC_CTX *ctx = talloc_tos();
5329 START_PROFILE(SMBrmdir);
5331 srvstr_get_path_req(ctx, req, &directory, (const char *)req->buf + 1,
5332 STR_TERMINATE, &status);
5333 if (!NT_STATUS_IS_OK(status)) {
5334 reply_nterror(req, status);
5335 END_PROFILE(SMBrmdir);
5336 return;
5339 status = resolve_dfspath(ctx, conn,
5340 req->flags2 & FLAGS2_DFS_PATHNAMES,
5341 directory,
5342 &directory);
5343 if (!NT_STATUS_IS_OK(status)) {
5344 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
5345 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
5346 ERRSRV, ERRbadpath);
5347 END_PROFILE(SMBrmdir);
5348 return;
5350 reply_nterror(req, status);
5351 END_PROFILE(SMBrmdir);
5352 return;
5355 status = unix_convert(ctx, conn, directory, False, &directory,
5356 NULL, &sbuf);
5357 if (!NT_STATUS_IS_OK(status)) {
5358 reply_nterror(req, status);
5359 END_PROFILE(SMBrmdir);
5360 return;
5363 status = check_name(conn, directory);
5364 if (!NT_STATUS_IS_OK(status)) {
5365 reply_nterror(req, status);
5366 END_PROFILE(SMBrmdir);
5367 return;
5370 dptr_closepath(directory, req->smbpid);
5371 status = rmdir_internals(ctx, conn, directory);
5372 if (!NT_STATUS_IS_OK(status)) {
5373 reply_nterror(req, status);
5374 END_PROFILE(SMBrmdir);
5375 return;
5378 reply_outbuf(req, 0, 0);
5380 DEBUG( 3, ( "rmdir %s\n", directory ) );
5382 END_PROFILE(SMBrmdir);
5383 return;
5386 /*******************************************************************
5387 Resolve wildcards in a filename rename.
5388 ********************************************************************/
5390 static bool resolve_wildcards(TALLOC_CTX *ctx,
5391 const char *name1,
5392 const char *name2,
5393 char **pp_newname)
5395 char *name2_copy = NULL;
5396 char *root1 = NULL;
5397 char *root2 = NULL;
5398 char *ext1 = NULL;
5399 char *ext2 = NULL;
5400 char *p,*p2, *pname1, *pname2;
5402 name2_copy = talloc_strdup(ctx, name2);
5403 if (!name2_copy) {
5404 return False;
5407 pname1 = strrchr_m(name1,'/');
5408 pname2 = strrchr_m(name2_copy,'/');
5410 if (!pname1 || !pname2) {
5411 return False;
5414 /* Truncate the copy of name2 at the last '/' */
5415 *pname2 = '\0';
5417 /* Now go past the '/' */
5418 pname1++;
5419 pname2++;
5421 root1 = talloc_strdup(ctx, pname1);
5422 root2 = talloc_strdup(ctx, pname2);
5424 if (!root1 || !root2) {
5425 return False;
5428 p = strrchr_m(root1,'.');
5429 if (p) {
5430 *p = 0;
5431 ext1 = talloc_strdup(ctx, p+1);
5432 } else {
5433 ext1 = talloc_strdup(ctx, "");
5435 p = strrchr_m(root2,'.');
5436 if (p) {
5437 *p = 0;
5438 ext2 = talloc_strdup(ctx, p+1);
5439 } else {
5440 ext2 = talloc_strdup(ctx, "");
5443 if (!ext1 || !ext2) {
5444 return False;
5447 p = root1;
5448 p2 = root2;
5449 while (*p2) {
5450 if (*p2 == '?') {
5451 /* Hmmm. Should this be mb-aware ? */
5452 *p2 = *p;
5453 p2++;
5454 } else if (*p2 == '*') {
5455 *p2 = '\0';
5456 root2 = talloc_asprintf(ctx, "%s%s",
5457 root2,
5459 if (!root2) {
5460 return False;
5462 break;
5463 } else {
5464 p2++;
5466 if (*p) {
5467 p++;
5471 p = ext1;
5472 p2 = ext2;
5473 while (*p2) {
5474 if (*p2 == '?') {
5475 /* Hmmm. Should this be mb-aware ? */
5476 *p2 = *p;
5477 p2++;
5478 } else if (*p2 == '*') {
5479 *p2 = '\0';
5480 ext2 = talloc_asprintf(ctx, "%s%s",
5481 ext2,
5483 if (!ext2) {
5484 return False;
5486 break;
5487 } else {
5488 p2++;
5490 if (*p) {
5491 p++;
5495 if (*ext2) {
5496 *pp_newname = talloc_asprintf(ctx, "%s/%s.%s",
5497 name2_copy,
5498 root2,
5499 ext2);
5500 } else {
5501 *pp_newname = talloc_asprintf(ctx, "%s/%s",
5502 name2_copy,
5503 root2);
5506 if (!*pp_newname) {
5507 return False;
5510 return True;
5513 /****************************************************************************
5514 Ensure open files have their names updated. Updated to notify other smbd's
5515 asynchronously.
5516 ****************************************************************************/
5518 static void rename_open_files(connection_struct *conn,
5519 struct share_mode_lock *lck,
5520 const char *newname)
5522 files_struct *fsp;
5523 bool did_rename = False;
5525 for(fsp = file_find_di_first(lck->id); fsp;
5526 fsp = file_find_di_next(fsp)) {
5527 /* fsp_name is a relative path under the fsp. To change this for other
5528 sharepaths we need to manipulate relative paths. */
5529 /* TODO - create the absolute path and manipulate the newname
5530 relative to the sharepath. */
5531 if (!strequal(fsp->conn->connectpath, conn->connectpath)) {
5532 continue;
5534 DEBUG(10,("rename_open_files: renaming file fnum %d (file_id %s) from %s -> %s\n",
5535 fsp->fnum, file_id_string_tos(&fsp->file_id),
5536 fsp->fsp_name, newname ));
5537 string_set(&fsp->fsp_name, newname);
5538 did_rename = True;
5541 if (!did_rename) {
5542 DEBUG(10,("rename_open_files: no open files on file_id %s for %s\n",
5543 file_id_string_tos(&lck->id), newname ));
5546 /* Send messages to all smbd's (not ourself) that the name has changed. */
5547 rename_share_filename(smbd_messaging_context(), lck, conn->connectpath,
5548 newname);
5551 /****************************************************************************
5552 We need to check if the source path is a parent directory of the destination
5553 (ie. a rename of /foo/bar/baz -> /foo/bar/baz/bibble/bobble. If so we must
5554 refuse the rename with a sharing violation. Under UNIX the above call can
5555 *succeed* if /foo/bar/baz is a symlink to another area in the share. We
5556 probably need to check that the client is a Windows one before disallowing
5557 this as a UNIX client (one with UNIX extensions) can know the source is a
5558 symlink and make this decision intelligently. Found by an excellent bug
5559 report from <AndyLiebman@aol.com>.
5560 ****************************************************************************/
5562 static bool rename_path_prefix_equal(const char *src, const char *dest)
5564 const char *psrc = src;
5565 const char *pdst = dest;
5566 size_t slen;
5568 if (psrc[0] == '.' && psrc[1] == '/') {
5569 psrc += 2;
5571 if (pdst[0] == '.' && pdst[1] == '/') {
5572 pdst += 2;
5574 if ((slen = strlen(psrc)) > strlen(pdst)) {
5575 return False;
5577 return ((memcmp(psrc, pdst, slen) == 0) && pdst[slen] == '/');
5581 * Do the notify calls from a rename
5584 static void notify_rename(connection_struct *conn, bool is_dir,
5585 const char *oldpath, const char *newpath)
5587 char *olddir, *newdir;
5588 const char *oldname, *newname;
5589 uint32 mask;
5591 mask = is_dir ? FILE_NOTIFY_CHANGE_DIR_NAME
5592 : FILE_NOTIFY_CHANGE_FILE_NAME;
5594 if (!parent_dirname(talloc_tos(), oldpath, &olddir, &oldname)
5595 || !parent_dirname(talloc_tos(), newpath, &newdir, &newname)) {
5596 TALLOC_FREE(olddir);
5597 return;
5600 if (strcmp(olddir, newdir) == 0) {
5601 notify_fname(conn, NOTIFY_ACTION_OLD_NAME, mask, oldpath);
5602 notify_fname(conn, NOTIFY_ACTION_NEW_NAME, mask, newpath);
5604 else {
5605 notify_fname(conn, NOTIFY_ACTION_REMOVED, mask, oldpath);
5606 notify_fname(conn, NOTIFY_ACTION_ADDED, mask, newpath);
5608 TALLOC_FREE(olddir);
5609 TALLOC_FREE(newdir);
5611 /* this is a strange one. w2k3 gives an additional event for
5612 CHANGE_ATTRIBUTES and CHANGE_CREATION on the new file when renaming
5613 files, but not directories */
5614 if (!is_dir) {
5615 notify_fname(conn, NOTIFY_ACTION_MODIFIED,
5616 FILE_NOTIFY_CHANGE_ATTRIBUTES
5617 |FILE_NOTIFY_CHANGE_CREATION,
5618 newpath);
5622 /****************************************************************************
5623 Rename an open file - given an fsp.
5624 ****************************************************************************/
5626 NTSTATUS rename_internals_fsp(connection_struct *conn,
5627 files_struct *fsp,
5628 char *newname,
5629 const char *newname_last_component,
5630 uint32 attrs,
5631 bool replace_if_exists)
5633 TALLOC_CTX *ctx = talloc_tos();
5634 SMB_STRUCT_STAT sbuf, sbuf1;
5635 NTSTATUS status = NT_STATUS_OK;
5636 struct share_mode_lock *lck = NULL;
5637 bool dst_exists, old_is_stream, new_is_stream;
5639 ZERO_STRUCT(sbuf);
5641 status = check_name(conn, newname);
5642 if (!NT_STATUS_IS_OK(status)) {
5643 return status;
5646 /* Ensure newname contains a '/' */
5647 if(strrchr_m(newname,'/') == 0) {
5648 newname = talloc_asprintf(ctx,
5649 "./%s",
5650 newname);
5651 if (!newname) {
5652 return NT_STATUS_NO_MEMORY;
5657 * Check for special case with case preserving and not
5658 * case sensitive. If the old last component differs from the original
5659 * last component only by case, then we should allow
5660 * the rename (user is trying to change the case of the
5661 * filename).
5664 if((conn->case_sensitive == False) && (conn->case_preserve == True) &&
5665 strequal(newname, fsp->fsp_name)) {
5666 char *p;
5667 char *newname_modified_last_component = NULL;
5670 * Get the last component of the modified name.
5671 * Note that we guarantee that newname contains a '/'
5672 * character above.
5674 p = strrchr_m(newname,'/');
5675 newname_modified_last_component = talloc_strdup(ctx,
5676 p+1);
5677 if (!newname_modified_last_component) {
5678 return NT_STATUS_NO_MEMORY;
5681 if(strcsequal(newname_modified_last_component,
5682 newname_last_component) == False) {
5684 * Replace the modified last component with
5685 * the original.
5687 *p = '\0'; /* Truncate at the '/' */
5688 newname = talloc_asprintf(ctx,
5689 "%s/%s",
5690 newname,
5691 newname_last_component);
5696 * If the src and dest names are identical - including case,
5697 * don't do the rename, just return success.
5700 if (strcsequal(fsp->fsp_name, newname)) {
5701 DEBUG(3,("rename_internals_fsp: identical names in rename %s - returning success\n",
5702 newname));
5703 return NT_STATUS_OK;
5706 old_is_stream = is_ntfs_stream_name(fsp->fsp_name);
5707 new_is_stream = is_ntfs_stream_name(newname);
5709 /* Return the correct error code if both names aren't streams. */
5710 if (!old_is_stream && new_is_stream) {
5711 return NT_STATUS_OBJECT_NAME_INVALID;
5714 if (old_is_stream && !new_is_stream) {
5715 return NT_STATUS_INVALID_PARAMETER;
5719 * Have vfs_object_exist also fill sbuf1
5721 dst_exists = vfs_object_exist(conn, newname, &sbuf1);
5723 if(!replace_if_exists && dst_exists) {
5724 DEBUG(3,("rename_internals_fsp: dest exists doing rename %s -> %s\n",
5725 fsp->fsp_name,newname));
5726 return NT_STATUS_OBJECT_NAME_COLLISION;
5729 if (dst_exists) {
5730 struct file_id fileid = vfs_file_id_from_sbuf(conn, &sbuf1);
5731 files_struct *dst_fsp = file_find_di_first(fileid);
5732 /* The file can be open when renaming a stream */
5733 if (dst_fsp && !new_is_stream) {
5734 DEBUG(3, ("rename_internals_fsp: Target file open\n"));
5735 return NT_STATUS_ACCESS_DENIED;
5739 /* Ensure we have a valid stat struct for the source. */
5740 if (fsp->fh->fd != -1) {
5741 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
5742 return map_nt_error_from_unix(errno);
5744 } else {
5745 int ret = -1;
5746 if (fsp->posix_open) {
5747 ret = SMB_VFS_LSTAT(conn,fsp->fsp_name,&sbuf);
5748 } else {
5749 ret = SMB_VFS_STAT(conn,fsp->fsp_name,&sbuf);
5751 if (ret == -1) {
5752 return map_nt_error_from_unix(errno);
5756 status = can_rename(conn, fsp, attrs, &sbuf);
5758 if (!NT_STATUS_IS_OK(status)) {
5759 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
5760 nt_errstr(status), fsp->fsp_name,newname));
5761 if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION))
5762 status = NT_STATUS_ACCESS_DENIED;
5763 return status;
5766 if (rename_path_prefix_equal(fsp->fsp_name, newname)) {
5767 return NT_STATUS_ACCESS_DENIED;
5770 lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
5771 NULL);
5774 * We have the file open ourselves, so not being able to get the
5775 * corresponding share mode lock is a fatal error.
5778 SMB_ASSERT(lck != NULL);
5780 if(SMB_VFS_RENAME(conn,fsp->fsp_name, newname) == 0) {
5781 uint32 create_options = fsp->fh->private_options;
5783 DEBUG(3,("rename_internals_fsp: succeeded doing rename on %s -> %s\n",
5784 fsp->fsp_name,newname));
5786 notify_rename(conn, fsp->is_directory, fsp->fsp_name, newname);
5788 rename_open_files(conn, lck, newname);
5791 * A rename acts as a new file create w.r.t. allowing an initial delete
5792 * on close, probably because in Windows there is a new handle to the
5793 * new file. If initial delete on close was requested but not
5794 * originally set, we need to set it here. This is probably not 100% correct,
5795 * but will work for the CIFSFS client which in non-posix mode
5796 * depends on these semantics. JRA.
5799 if (create_options & FILE_DELETE_ON_CLOSE) {
5800 status = can_set_delete_on_close(fsp, True, 0);
5802 if (NT_STATUS_IS_OK(status)) {
5803 /* Note that here we set the *inital* delete on close flag,
5804 * not the regular one. The magic gets handled in close. */
5805 fsp->initial_delete_on_close = True;
5808 TALLOC_FREE(lck);
5809 return NT_STATUS_OK;
5812 TALLOC_FREE(lck);
5814 if (errno == ENOTDIR || errno == EISDIR) {
5815 status = NT_STATUS_OBJECT_NAME_COLLISION;
5816 } else {
5817 status = map_nt_error_from_unix(errno);
5820 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
5821 nt_errstr(status), fsp->fsp_name,newname));
5823 return status;
5826 /****************************************************************************
5827 The guts of the rename command, split out so it may be called by the NT SMB
5828 code.
5829 ****************************************************************************/
5831 NTSTATUS rename_internals(TALLOC_CTX *ctx,
5832 connection_struct *conn,
5833 struct smb_request *req,
5834 const char *name_in,
5835 const char *newname_in,
5836 uint32 attrs,
5837 bool replace_if_exists,
5838 bool src_has_wild,
5839 bool dest_has_wild,
5840 uint32_t access_mask)
5842 char *directory = NULL;
5843 char *mask = NULL;
5844 char *last_component_src = NULL;
5845 char *last_component_dest = NULL;
5846 char *name = NULL;
5847 char *newname = NULL;
5848 char *p;
5849 int count=0;
5850 NTSTATUS status = NT_STATUS_OK;
5851 SMB_STRUCT_STAT sbuf1, sbuf2;
5852 struct smb_Dir *dir_hnd = NULL;
5853 const char *dname;
5854 long offset = 0;
5855 int create_options = 0;
5856 bool posix_pathnames = lp_posix_pathnames();
5858 ZERO_STRUCT(sbuf1);
5859 ZERO_STRUCT(sbuf2);
5861 status = unix_convert(ctx, conn, name_in, src_has_wild, &name,
5862 &last_component_src, &sbuf1);
5863 if (!NT_STATUS_IS_OK(status)) {
5864 return status;
5867 status = unix_convert(ctx, conn, newname_in, dest_has_wild, &newname,
5868 &last_component_dest, &sbuf2);
5869 if (!NT_STATUS_IS_OK(status)) {
5870 return status;
5874 * Split the old name into directory and last component
5875 * strings. Note that unix_convert may have stripped off a
5876 * leading ./ from both name and newname if the rename is
5877 * at the root of the share. We need to make sure either both
5878 * name and newname contain a / character or neither of them do
5879 * as this is checked in resolve_wildcards().
5882 p = strrchr_m(name,'/');
5883 if (!p) {
5884 directory = talloc_strdup(ctx, ".");
5885 if (!directory) {
5886 return NT_STATUS_NO_MEMORY;
5888 mask = name;
5889 } else {
5890 *p = 0;
5891 directory = talloc_strdup(ctx, name);
5892 if (!directory) {
5893 return NT_STATUS_NO_MEMORY;
5895 mask = p+1;
5896 *p = '/'; /* Replace needed for exceptional test below. */
5900 * We should only check the mangled cache
5901 * here if unix_convert failed. This means
5902 * that the path in 'mask' doesn't exist
5903 * on the file system and so we need to look
5904 * for a possible mangle. This patch from
5905 * Tine Smukavec <valentin.smukavec@hermes.si>.
5908 if (!VALID_STAT(sbuf1) && mangle_is_mangled(mask, conn->params)) {
5909 char *new_mask = NULL;
5910 mangle_lookup_name_from_8_3(ctx,
5911 mask,
5912 &new_mask,
5913 conn->params );
5914 if (new_mask) {
5915 mask = new_mask;
5919 if (!src_has_wild) {
5920 files_struct *fsp;
5923 * No wildcards - just process the one file.
5925 /* Add a terminating '/' to the directory name. */
5926 directory = talloc_asprintf_append(directory,
5927 "/%s",
5928 mask);
5929 if (!directory) {
5930 return NT_STATUS_NO_MEMORY;
5933 /* Ensure newname contains a '/' also */
5934 if(strrchr_m(newname,'/') == 0) {
5935 newname = talloc_asprintf(ctx,
5936 "./%s",
5937 newname);
5938 if (!newname) {
5939 return NT_STATUS_NO_MEMORY;
5943 DEBUG(3, ("rename_internals: case_sensitive = %d, "
5944 "case_preserve = %d, short case preserve = %d, "
5945 "directory = %s, newname = %s, "
5946 "last_component_dest = %s\n",
5947 conn->case_sensitive, conn->case_preserve,
5948 conn->short_case_preserve, directory,
5949 newname, last_component_dest));
5951 /* The dest name still may have wildcards. */
5952 if (dest_has_wild) {
5953 char *mod_newname = NULL;
5954 if (!resolve_wildcards(ctx,
5955 directory,newname,&mod_newname)) {
5956 DEBUG(6, ("rename_internals: resolve_wildcards "
5957 "%s %s failed\n",
5958 directory,
5959 newname));
5960 return NT_STATUS_NO_MEMORY;
5962 newname = mod_newname;
5965 ZERO_STRUCT(sbuf1);
5966 if (posix_pathnames) {
5967 SMB_VFS_LSTAT(conn, directory, &sbuf1);
5968 } else {
5969 SMB_VFS_STAT(conn, directory, &sbuf1);
5972 if (S_ISDIR(sbuf1.st_mode)) {
5973 create_options |= FILE_DIRECTORY_FILE;
5976 status = SMB_VFS_CREATE_FILE(
5977 conn, /* conn */
5978 req, /* req */
5979 0, /* root_dir_fid */
5980 directory, /* fname */
5981 0, /* create_file_flags */
5982 access_mask, /* access_mask */
5983 (FILE_SHARE_READ | /* share_access */
5984 FILE_SHARE_WRITE),
5985 FILE_OPEN, /* create_disposition*/
5986 create_options, /* create_options */
5987 posix_pathnames ? FILE_FLAG_POSIX_SEMANTICS|0777 : 0, /* file_attributes */
5988 0, /* oplock_request */
5989 0, /* allocation_size */
5990 NULL, /* sd */
5991 NULL, /* ea_list */
5992 &fsp, /* result */
5993 NULL, /* pinfo */
5994 &sbuf1); /* psbuf */
5996 if (!NT_STATUS_IS_OK(status)) {
5997 DEBUG(3, ("Could not open rename source %s: %s\n",
5998 directory, nt_errstr(status)));
5999 return status;
6002 status = rename_internals_fsp(conn, fsp, newname,
6003 last_component_dest,
6004 attrs, replace_if_exists);
6006 close_file(req, fsp, NORMAL_CLOSE);
6008 DEBUG(3, ("rename_internals: Error %s rename %s -> %s\n",
6009 nt_errstr(status), directory,newname));
6011 return status;
6015 * Wildcards - process each file that matches.
6017 if (strequal(mask,"????????.???")) {
6018 mask[0] = '*';
6019 mask[1] = '\0';
6022 status = check_name(conn, directory);
6023 if (!NT_STATUS_IS_OK(status)) {
6024 return status;
6027 dir_hnd = OpenDir(talloc_tos(), conn, directory, mask, attrs);
6028 if (dir_hnd == NULL) {
6029 return map_nt_error_from_unix(errno);
6032 status = NT_STATUS_NO_SUCH_FILE;
6034 * Was status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
6035 * - gentest fix. JRA
6038 while ((dname = ReadDirName(dir_hnd, &offset, &sbuf1))) {
6039 files_struct *fsp = NULL;
6040 char *fname = NULL;
6041 char *destname = NULL;
6042 bool sysdir_entry = False;
6044 /* Quick check for "." and ".." */
6045 if (ISDOT(dname) || ISDOTDOT(dname)) {
6046 if (attrs & aDIR) {
6047 sysdir_entry = True;
6048 } else {
6049 continue;
6053 if (!is_visible_file(conn, directory, dname, &sbuf1, False)) {
6054 continue;
6057 if(!mask_match(dname, mask, conn->case_sensitive)) {
6058 continue;
6061 if (sysdir_entry) {
6062 status = NT_STATUS_OBJECT_NAME_INVALID;
6063 break;
6066 fname = talloc_asprintf(ctx,
6067 "%s/%s",
6068 directory,
6069 dname);
6070 if (!fname) {
6071 return NT_STATUS_NO_MEMORY;
6074 if (!resolve_wildcards(ctx,
6075 fname,newname,&destname)) {
6076 DEBUG(6, ("resolve_wildcards %s %s failed\n",
6077 fname, destname));
6078 TALLOC_FREE(fname);
6079 continue;
6081 if (!destname) {
6082 return NT_STATUS_NO_MEMORY;
6085 ZERO_STRUCT(sbuf1);
6086 if (posix_pathnames) {
6087 SMB_VFS_LSTAT(conn, fname, &sbuf1);
6088 } else {
6089 SMB_VFS_STAT(conn, fname, &sbuf1);
6092 create_options = 0;
6094 if (S_ISDIR(sbuf1.st_mode)) {
6095 create_options |= FILE_DIRECTORY_FILE;
6098 status = SMB_VFS_CREATE_FILE(
6099 conn, /* conn */
6100 req, /* req */
6101 0, /* root_dir_fid */
6102 fname, /* fname */
6103 0, /* create_file_flags */
6104 access_mask, /* access_mask */
6105 (FILE_SHARE_READ | /* share_access */
6106 FILE_SHARE_WRITE),
6107 FILE_OPEN, /* create_disposition*/
6108 create_options, /* create_options */
6109 posix_pathnames ? FILE_FLAG_POSIX_SEMANTICS|0777 : 0, /* file_attributes */
6110 0, /* oplock_request */
6111 0, /* allocation_size */
6112 NULL, /* sd */
6113 NULL, /* ea_list */
6114 &fsp, /* result */
6115 NULL, /* pinfo */
6116 &sbuf1); /* psbuf */
6118 if (!NT_STATUS_IS_OK(status)) {
6119 DEBUG(3,("rename_internals: SMB_VFS_CREATE_FILE "
6120 "returned %s rename %s -> %s\n",
6121 nt_errstr(status), directory, newname));
6122 break;
6125 status = rename_internals_fsp(conn, fsp, destname, dname,
6126 attrs, replace_if_exists);
6128 close_file(req, fsp, NORMAL_CLOSE);
6130 if (!NT_STATUS_IS_OK(status)) {
6131 DEBUG(3, ("rename_internals_fsp returned %s for "
6132 "rename %s -> %s\n", nt_errstr(status),
6133 directory, newname));
6134 break;
6137 count++;
6139 DEBUG(3,("rename_internals: doing rename on %s -> "
6140 "%s\n",fname,destname));
6142 TALLOC_FREE(fname);
6143 TALLOC_FREE(destname);
6145 TALLOC_FREE(dir_hnd);
6147 if (count == 0 && NT_STATUS_IS_OK(status) && errno != 0) {
6148 status = map_nt_error_from_unix(errno);
6151 return status;
6154 /****************************************************************************
6155 Reply to a mv.
6156 ****************************************************************************/
6158 void reply_mv(struct smb_request *req)
6160 connection_struct *conn = req->conn;
6161 char *name = NULL;
6162 char *newname = NULL;
6163 const char *p;
6164 uint32 attrs;
6165 NTSTATUS status;
6166 bool src_has_wcard = False;
6167 bool dest_has_wcard = False;
6168 TALLOC_CTX *ctx = talloc_tos();
6170 START_PROFILE(SMBmv);
6172 if (req->wct < 1) {
6173 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
6174 END_PROFILE(SMBmv);
6175 return;
6178 attrs = SVAL(req->vwv+0, 0);
6180 p = (const char *)req->buf + 1;
6181 p += srvstr_get_path_req_wcard(ctx, req, &name, p, STR_TERMINATE,
6182 &status, &src_has_wcard);
6183 if (!NT_STATUS_IS_OK(status)) {
6184 reply_nterror(req, status);
6185 END_PROFILE(SMBmv);
6186 return;
6188 p++;
6189 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
6190 &status, &dest_has_wcard);
6191 if (!NT_STATUS_IS_OK(status)) {
6192 reply_nterror(req, status);
6193 END_PROFILE(SMBmv);
6194 return;
6197 status = resolve_dfspath_wcard(ctx, conn,
6198 req->flags2 & FLAGS2_DFS_PATHNAMES,
6199 name,
6200 &name,
6201 &src_has_wcard);
6202 if (!NT_STATUS_IS_OK(status)) {
6203 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
6204 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
6205 ERRSRV, ERRbadpath);
6206 END_PROFILE(SMBmv);
6207 return;
6209 reply_nterror(req, status);
6210 END_PROFILE(SMBmv);
6211 return;
6214 status = resolve_dfspath_wcard(ctx, conn,
6215 req->flags2 & FLAGS2_DFS_PATHNAMES,
6216 newname,
6217 &newname,
6218 &dest_has_wcard);
6219 if (!NT_STATUS_IS_OK(status)) {
6220 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
6221 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
6222 ERRSRV, ERRbadpath);
6223 END_PROFILE(SMBmv);
6224 return;
6226 reply_nterror(req, status);
6227 END_PROFILE(SMBmv);
6228 return;
6231 DEBUG(3,("reply_mv : %s -> %s\n",name,newname));
6233 status = rename_internals(ctx, conn, req, name, newname, attrs, False,
6234 src_has_wcard, dest_has_wcard, DELETE_ACCESS);
6235 if (!NT_STATUS_IS_OK(status)) {
6236 if (open_was_deferred(req->mid)) {
6237 /* We have re-scheduled this call. */
6238 END_PROFILE(SMBmv);
6239 return;
6241 reply_nterror(req, status);
6242 END_PROFILE(SMBmv);
6243 return;
6246 reply_outbuf(req, 0, 0);
6248 END_PROFILE(SMBmv);
6249 return;
6252 /*******************************************************************
6253 Copy a file as part of a reply_copy.
6254 ******************************************************************/
6257 * TODO: check error codes on all callers
6260 NTSTATUS copy_file(TALLOC_CTX *ctx,
6261 connection_struct *conn,
6262 const char *src,
6263 const char *dest1,
6264 int ofun,
6265 int count,
6266 bool target_is_directory)
6268 SMB_STRUCT_STAT src_sbuf, sbuf2;
6269 SMB_OFF_T ret=-1;
6270 files_struct *fsp1,*fsp2;
6271 char *dest = NULL;
6272 uint32 dosattrs;
6273 uint32 new_create_disposition;
6274 NTSTATUS status;
6276 dest = talloc_strdup(ctx, dest1);
6277 if (!dest) {
6278 return NT_STATUS_NO_MEMORY;
6280 if (target_is_directory) {
6281 const char *p = strrchr_m(src,'/');
6282 if (p) {
6283 p++;
6284 } else {
6285 p = src;
6287 dest = talloc_asprintf_append(dest,
6288 "/%s",
6290 if (!dest) {
6291 return NT_STATUS_NO_MEMORY;
6295 if (!vfs_file_exist(conn,src,&src_sbuf)) {
6296 TALLOC_FREE(dest);
6297 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
6300 if (!target_is_directory && count) {
6301 new_create_disposition = FILE_OPEN;
6302 } else {
6303 if (!map_open_params_to_ntcreate(dest1,0,ofun,
6304 NULL, NULL, &new_create_disposition, NULL)) {
6305 TALLOC_FREE(dest);
6306 return NT_STATUS_INVALID_PARAMETER;
6310 status = SMB_VFS_CREATE_FILE(
6311 conn, /* conn */
6312 NULL, /* req */
6313 0, /* root_dir_fid */
6314 src, /* fname */
6315 0, /* create_file_flags */
6316 FILE_GENERIC_READ, /* access_mask */
6317 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
6318 FILE_OPEN, /* create_disposition*/
6319 0, /* create_options */
6320 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
6321 INTERNAL_OPEN_ONLY, /* oplock_request */
6322 0, /* allocation_size */
6323 NULL, /* sd */
6324 NULL, /* ea_list */
6325 &fsp1, /* result */
6326 NULL, /* pinfo */
6327 &src_sbuf); /* psbuf */
6329 if (!NT_STATUS_IS_OK(status)) {
6330 TALLOC_FREE(dest);
6331 return status;
6334 dosattrs = dos_mode(conn, src, &src_sbuf);
6335 if (SMB_VFS_STAT(conn,dest,&sbuf2) == -1) {
6336 ZERO_STRUCTP(&sbuf2);
6339 status = SMB_VFS_CREATE_FILE(
6340 conn, /* conn */
6341 NULL, /* req */
6342 0, /* root_dir_fid */
6343 dest, /* fname */
6344 0, /* create_file_flags */
6345 FILE_GENERIC_WRITE, /* access_mask */
6346 FILE_SHARE_READ | FILE_SHARE_WRITE, /* share_access */
6347 new_create_disposition, /* create_disposition*/
6348 0, /* create_options */
6349 dosattrs, /* file_attributes */
6350 INTERNAL_OPEN_ONLY, /* oplock_request */
6351 0, /* allocation_size */
6352 NULL, /* sd */
6353 NULL, /* ea_list */
6354 &fsp2, /* result */
6355 NULL, /* pinfo */
6356 &sbuf2); /* psbuf */
6358 TALLOC_FREE(dest);
6360 if (!NT_STATUS_IS_OK(status)) {
6361 close_file(NULL, fsp1, ERROR_CLOSE);
6362 return status;
6365 if ((ofun&3) == 1) {
6366 if(SMB_VFS_LSEEK(fsp2,0,SEEK_END) == -1) {
6367 DEBUG(0,("copy_file: error - vfs lseek returned error %s\n", strerror(errno) ));
6369 * Stop the copy from occurring.
6371 ret = -1;
6372 src_sbuf.st_size = 0;
6376 if (src_sbuf.st_size) {
6377 ret = vfs_transfer_file(fsp1, fsp2, src_sbuf.st_size);
6380 close_file(NULL, fsp1, NORMAL_CLOSE);
6382 /* Ensure the modtime is set correctly on the destination file. */
6383 set_close_write_time(fsp2, get_mtimespec(&src_sbuf));
6386 * As we are opening fsp1 read-only we only expect
6387 * an error on close on fsp2 if we are out of space.
6388 * Thus we don't look at the error return from the
6389 * close of fsp1.
6391 status = close_file(NULL, fsp2, NORMAL_CLOSE);
6393 if (!NT_STATUS_IS_OK(status)) {
6394 return status;
6397 if (ret != (SMB_OFF_T)src_sbuf.st_size) {
6398 return NT_STATUS_DISK_FULL;
6401 return NT_STATUS_OK;
6404 /****************************************************************************
6405 Reply to a file copy.
6406 ****************************************************************************/
6408 void reply_copy(struct smb_request *req)
6410 connection_struct *conn = req->conn;
6411 char *name = NULL;
6412 char *newname = NULL;
6413 char *directory = NULL;
6414 const char *mask = NULL;
6415 const char mask_star[] = "*";
6416 const char *p;
6417 int count=0;
6418 int error = ERRnoaccess;
6419 int err = 0;
6420 int tid2;
6421 int ofun;
6422 int flags;
6423 bool target_is_directory=False;
6424 bool source_has_wild = False;
6425 bool dest_has_wild = False;
6426 SMB_STRUCT_STAT sbuf1, sbuf2;
6427 NTSTATUS status;
6428 TALLOC_CTX *ctx = talloc_tos();
6430 START_PROFILE(SMBcopy);
6432 if (req->wct < 3) {
6433 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
6434 END_PROFILE(SMBcopy);
6435 return;
6438 tid2 = SVAL(req->vwv+0, 0);
6439 ofun = SVAL(req->vwv+1, 0);
6440 flags = SVAL(req->vwv+2, 0);
6442 p = (const char *)req->buf;
6443 p += srvstr_get_path_req_wcard(ctx, req, &name, p, STR_TERMINATE,
6444 &status, &source_has_wild);
6445 if (!NT_STATUS_IS_OK(status)) {
6446 reply_nterror(req, status);
6447 END_PROFILE(SMBcopy);
6448 return;
6450 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
6451 &status, &dest_has_wild);
6452 if (!NT_STATUS_IS_OK(status)) {
6453 reply_nterror(req, status);
6454 END_PROFILE(SMBcopy);
6455 return;
6458 DEBUG(3,("reply_copy : %s -> %s\n",name,newname));
6460 if (tid2 != conn->cnum) {
6461 /* can't currently handle inter share copies XXXX */
6462 DEBUG(3,("Rejecting inter-share copy\n"));
6463 reply_doserror(req, ERRSRV, ERRinvdevice);
6464 END_PROFILE(SMBcopy);
6465 return;
6468 status = resolve_dfspath_wcard(ctx, conn,
6469 req->flags2 & FLAGS2_DFS_PATHNAMES,
6470 name,
6471 &name,
6472 &source_has_wild);
6473 if (!NT_STATUS_IS_OK(status)) {
6474 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
6475 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
6476 ERRSRV, ERRbadpath);
6477 END_PROFILE(SMBcopy);
6478 return;
6480 reply_nterror(req, status);
6481 END_PROFILE(SMBcopy);
6482 return;
6485 status = resolve_dfspath_wcard(ctx, conn,
6486 req->flags2 & FLAGS2_DFS_PATHNAMES,
6487 newname,
6488 &newname,
6489 &dest_has_wild);
6490 if (!NT_STATUS_IS_OK(status)) {
6491 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
6492 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
6493 ERRSRV, ERRbadpath);
6494 END_PROFILE(SMBcopy);
6495 return;
6497 reply_nterror(req, status);
6498 END_PROFILE(SMBcopy);
6499 return;
6502 status = unix_convert(ctx, conn, name, source_has_wild,
6503 &name, NULL, &sbuf1);
6504 if (!NT_STATUS_IS_OK(status)) {
6505 reply_nterror(req, status);
6506 END_PROFILE(SMBcopy);
6507 return;
6510 status = unix_convert(ctx, conn, newname, dest_has_wild,
6511 &newname, NULL, &sbuf2);
6512 if (!NT_STATUS_IS_OK(status)) {
6513 reply_nterror(req, status);
6514 END_PROFILE(SMBcopy);
6515 return;
6518 target_is_directory = VALID_STAT_OF_DIR(sbuf2);
6520 if ((flags&1) && target_is_directory) {
6521 reply_doserror(req, ERRDOS, ERRbadfile);
6522 END_PROFILE(SMBcopy);
6523 return;
6526 if ((flags&2) && !target_is_directory) {
6527 reply_doserror(req, ERRDOS, ERRbadpath);
6528 END_PROFILE(SMBcopy);
6529 return;
6532 if ((flags&(1<<5)) && VALID_STAT_OF_DIR(sbuf1)) {
6533 /* wants a tree copy! XXXX */
6534 DEBUG(3,("Rejecting tree copy\n"));
6535 reply_doserror(req, ERRSRV, ERRerror);
6536 END_PROFILE(SMBcopy);
6537 return;
6540 p = strrchr_m(name,'/');
6541 if (p != NULL) {
6542 directory = talloc_strndup(ctx, name, PTR_DIFF(p, name));
6543 mask = p+1;
6544 } else {
6545 directory = talloc_strdup(ctx, "./");
6546 mask = name;
6549 if (!directory) {
6550 reply_nterror(req, NT_STATUS_NO_MEMORY);
6551 END_PROFILE(SMBcopy);
6552 return;
6556 * We should only check the mangled cache
6557 * here if unix_convert failed. This means
6558 * that the path in 'mask' doesn't exist
6559 * on the file system and so we need to look
6560 * for a possible mangle. This patch from
6561 * Tine Smukavec <valentin.smukavec@hermes.si>.
6564 if (!VALID_STAT(sbuf1) && mangle_is_mangled(mask, conn->params)) {
6565 char *new_mask = NULL;
6566 mangle_lookup_name_from_8_3(ctx,
6567 mask,
6568 &new_mask,
6569 conn->params );
6570 if (new_mask) {
6571 mask = new_mask;
6575 if (!source_has_wild) {
6576 directory = talloc_asprintf_append(directory,
6577 "/%s",
6578 mask);
6579 if (dest_has_wild) {
6580 char *mod_newname = NULL;
6581 if (!resolve_wildcards(ctx,
6582 directory,newname,&mod_newname)) {
6583 reply_nterror(req, NT_STATUS_NO_MEMORY);
6584 END_PROFILE(SMBcopy);
6585 return;
6587 newname = mod_newname;
6590 status = check_name(conn, directory);
6591 if (!NT_STATUS_IS_OK(status)) {
6592 reply_nterror(req, status);
6593 END_PROFILE(SMBcopy);
6594 return;
6597 status = check_name(conn, newname);
6598 if (!NT_STATUS_IS_OK(status)) {
6599 reply_nterror(req, status);
6600 END_PROFILE(SMBcopy);
6601 return;
6604 status = copy_file(ctx,conn,directory,newname,ofun,
6605 count,target_is_directory);
6607 if(!NT_STATUS_IS_OK(status)) {
6608 reply_nterror(req, status);
6609 END_PROFILE(SMBcopy);
6610 return;
6611 } else {
6612 count++;
6614 } else {
6615 struct smb_Dir *dir_hnd = NULL;
6616 const char *dname = NULL;
6617 long offset = 0;
6619 if (strequal(mask,"????????.???")) {
6620 mask = mask_star;
6623 status = check_name(conn, directory);
6624 if (!NT_STATUS_IS_OK(status)) {
6625 reply_nterror(req, status);
6626 END_PROFILE(SMBcopy);
6627 return;
6630 dir_hnd = OpenDir(talloc_tos(), conn, directory, mask, 0);
6631 if (dir_hnd == NULL) {
6632 status = map_nt_error_from_unix(errno);
6633 reply_nterror(req, status);
6634 END_PROFILE(SMBcopy);
6635 return;
6638 error = ERRbadfile;
6640 while ((dname = ReadDirName(dir_hnd, &offset, &sbuf1))) {
6641 char *destname = NULL;
6642 char *fname = NULL;
6644 if (ISDOT(dname) || ISDOTDOT(dname)) {
6645 continue;
6648 if (!is_visible_file(conn, directory, dname, &sbuf1, False)) {
6649 continue;
6652 if(!mask_match(dname, mask, conn->case_sensitive)) {
6653 continue;
6656 error = ERRnoaccess;
6657 fname = talloc_asprintf(ctx,
6658 "%s/%s",
6659 directory,
6660 dname);
6661 if (!fname) {
6662 TALLOC_FREE(dir_hnd);
6663 reply_nterror(req, NT_STATUS_NO_MEMORY);
6664 END_PROFILE(SMBcopy);
6665 return;
6668 if (!resolve_wildcards(ctx,
6669 fname,newname,&destname)) {
6670 continue;
6672 if (!destname) {
6673 TALLOC_FREE(dir_hnd);
6674 reply_nterror(req, NT_STATUS_NO_MEMORY);
6675 END_PROFILE(SMBcopy);
6676 return;
6679 status = check_name(conn, fname);
6680 if (!NT_STATUS_IS_OK(status)) {
6681 TALLOC_FREE(dir_hnd);
6682 reply_nterror(req, status);
6683 END_PROFILE(SMBcopy);
6684 return;
6687 status = check_name(conn, destname);
6688 if (!NT_STATUS_IS_OK(status)) {
6689 TALLOC_FREE(dir_hnd);
6690 reply_nterror(req, status);
6691 END_PROFILE(SMBcopy);
6692 return;
6695 DEBUG(3,("reply_copy : doing copy on %s -> %s\n",fname, destname));
6697 status = copy_file(ctx,conn,fname,destname,ofun,
6698 count,target_is_directory);
6699 if (NT_STATUS_IS_OK(status)) {
6700 count++;
6702 TALLOC_FREE(fname);
6703 TALLOC_FREE(destname);
6705 TALLOC_FREE(dir_hnd);
6708 if (count == 0) {
6709 if(err) {
6710 /* Error on close... */
6711 errno = err;
6712 reply_unixerror(req, ERRHRD, ERRgeneral);
6713 END_PROFILE(SMBcopy);
6714 return;
6717 reply_doserror(req, ERRDOS, error);
6718 END_PROFILE(SMBcopy);
6719 return;
6722 reply_outbuf(req, 1, 0);
6723 SSVAL(req->outbuf,smb_vwv0,count);
6725 END_PROFILE(SMBcopy);
6726 return;
6729 #undef DBGC_CLASS
6730 #define DBGC_CLASS DBGC_LOCKING
6732 /****************************************************************************
6733 Get a lock pid, dealing with large count requests.
6734 ****************************************************************************/
6736 uint32 get_lock_pid(const uint8_t *data, int data_offset,
6737 bool large_file_format)
6739 if(!large_file_format)
6740 return (uint32)SVAL(data,SMB_LPID_OFFSET(data_offset));
6741 else
6742 return (uint32)SVAL(data,SMB_LARGE_LPID_OFFSET(data_offset));
6745 /****************************************************************************
6746 Get a lock count, dealing with large count requests.
6747 ****************************************************************************/
6749 uint64_t get_lock_count(const uint8_t *data, int data_offset,
6750 bool large_file_format)
6752 uint64_t count = 0;
6754 if(!large_file_format) {
6755 count = (uint64_t)IVAL(data,SMB_LKLEN_OFFSET(data_offset));
6756 } else {
6758 #if defined(HAVE_LONGLONG)
6759 count = (((uint64_t) IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset))) << 32) |
6760 ((uint64_t) IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)));
6761 #else /* HAVE_LONGLONG */
6764 * NT4.x seems to be broken in that it sends large file (64 bit)
6765 * lockingX calls even if the CAP_LARGE_FILES was *not*
6766 * negotiated. For boxes without large unsigned ints truncate the
6767 * lock count by dropping the top 32 bits.
6770 if(IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)) != 0) {
6771 DEBUG(3,("get_lock_count: truncating lock count (high)0x%x (low)0x%x to just low count.\n",
6772 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)),
6773 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)) ));
6774 SIVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset),0);
6777 count = (uint64_t)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset));
6778 #endif /* HAVE_LONGLONG */
6781 return count;
6784 #if !defined(HAVE_LONGLONG)
6785 /****************************************************************************
6786 Pathetically try and map a 64 bit lock offset into 31 bits. I hate Windows :-).
6787 ****************************************************************************/
6789 static uint32 map_lock_offset(uint32 high, uint32 low)
6791 unsigned int i;
6792 uint32 mask = 0;
6793 uint32 highcopy = high;
6796 * Try and find out how many significant bits there are in high.
6799 for(i = 0; highcopy; i++)
6800 highcopy >>= 1;
6803 * We use 31 bits not 32 here as POSIX
6804 * lock offsets may not be negative.
6807 mask = (~0) << (31 - i);
6809 if(low & mask)
6810 return 0; /* Fail. */
6812 high <<= (31 - i);
6814 return (high|low);
6816 #endif /* !defined(HAVE_LONGLONG) */
6818 /****************************************************************************
6819 Get a lock offset, dealing with large offset requests.
6820 ****************************************************************************/
6822 uint64_t get_lock_offset(const uint8_t *data, int data_offset,
6823 bool large_file_format, bool *err)
6825 uint64_t offset = 0;
6827 *err = False;
6829 if(!large_file_format) {
6830 offset = (uint64_t)IVAL(data,SMB_LKOFF_OFFSET(data_offset));
6831 } else {
6833 #if defined(HAVE_LONGLONG)
6834 offset = (((uint64_t) IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset))) << 32) |
6835 ((uint64_t) IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset)));
6836 #else /* HAVE_LONGLONG */
6839 * NT4.x seems to be broken in that it sends large file (64 bit)
6840 * lockingX calls even if the CAP_LARGE_FILES was *not*
6841 * negotiated. For boxes without large unsigned ints mangle the
6842 * lock offset by mapping the top 32 bits onto the lower 32.
6845 if(IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset)) != 0) {
6846 uint32 low = IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
6847 uint32 high = IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset));
6848 uint32 new_low = 0;
6850 if((new_low = map_lock_offset(high, low)) == 0) {
6851 *err = True;
6852 return (uint64_t)-1;
6855 DEBUG(3,("get_lock_offset: truncating lock offset (high)0x%x (low)0x%x to offset 0x%x.\n",
6856 (unsigned int)high, (unsigned int)low, (unsigned int)new_low ));
6857 SIVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset),0);
6858 SIVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset),new_low);
6861 offset = (uint64_t)IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
6862 #endif /* HAVE_LONGLONG */
6865 return offset;
6868 /****************************************************************************
6869 Reply to a lockingX request.
6870 ****************************************************************************/
6872 void reply_lockingX(struct smb_request *req)
6874 connection_struct *conn = req->conn;
6875 files_struct *fsp;
6876 unsigned char locktype;
6877 unsigned char oplocklevel;
6878 uint16 num_ulocks;
6879 uint16 num_locks;
6880 uint64_t count = 0, offset = 0;
6881 uint32 lock_pid;
6882 int32 lock_timeout;
6883 int i;
6884 const uint8_t *data;
6885 bool large_file_format;
6886 bool err;
6887 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
6889 START_PROFILE(SMBlockingX);
6891 if (req->wct < 8) {
6892 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
6893 END_PROFILE(SMBlockingX);
6894 return;
6897 fsp = file_fsp(req, SVAL(req->vwv+2, 0));
6898 locktype = CVAL(req->vwv+3, 0);
6899 oplocklevel = CVAL(req->vwv+3, 1);
6900 num_ulocks = SVAL(req->vwv+6, 0);
6901 num_locks = SVAL(req->vwv+7, 0);
6902 lock_timeout = IVAL(req->vwv+4, 0);
6903 large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
6905 if (!check_fsp(conn, req, fsp)) {
6906 END_PROFILE(SMBlockingX);
6907 return;
6910 data = req->buf;
6912 if (locktype & LOCKING_ANDX_CHANGE_LOCKTYPE) {
6913 /* we don't support these - and CANCEL_LOCK makes w2k
6914 and XP reboot so I don't really want to be
6915 compatible! (tridge) */
6916 reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks));
6917 END_PROFILE(SMBlockingX);
6918 return;
6921 /* Check if this is an oplock break on a file
6922 we have granted an oplock on.
6924 if ((locktype & LOCKING_ANDX_OPLOCK_RELEASE)) {
6925 /* Client can insist on breaking to none. */
6926 bool break_to_none = (oplocklevel == 0);
6927 bool result;
6929 DEBUG(5,("reply_lockingX: oplock break reply (%u) from client "
6930 "for fnum = %d\n", (unsigned int)oplocklevel,
6931 fsp->fnum ));
6934 * Make sure we have granted an exclusive or batch oplock on
6935 * this file.
6938 if (fsp->oplock_type == 0) {
6940 /* The Samba4 nbench simulator doesn't understand
6941 the difference between break to level2 and break
6942 to none from level2 - it sends oplock break
6943 replies in both cases. Don't keep logging an error
6944 message here - just ignore it. JRA. */
6946 DEBUG(5,("reply_lockingX: Error : oplock break from "
6947 "client for fnum = %d (oplock=%d) and no "
6948 "oplock granted on this file (%s).\n",
6949 fsp->fnum, fsp->oplock_type, fsp->fsp_name));
6951 /* if this is a pure oplock break request then don't
6952 * send a reply */
6953 if (num_locks == 0 && num_ulocks == 0) {
6954 END_PROFILE(SMBlockingX);
6955 return;
6956 } else {
6957 END_PROFILE(SMBlockingX);
6958 reply_doserror(req, ERRDOS, ERRlock);
6959 return;
6963 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
6964 (break_to_none)) {
6965 result = remove_oplock(fsp);
6966 } else {
6967 result = downgrade_oplock(fsp);
6970 if (!result) {
6971 DEBUG(0, ("reply_lockingX: error in removing "
6972 "oplock on file %s\n", fsp->fsp_name));
6973 /* Hmmm. Is this panic justified? */
6974 smb_panic("internal tdb error");
6977 reply_to_oplock_break_requests(fsp);
6979 /* if this is a pure oplock break request then don't send a
6980 * reply */
6981 if (num_locks == 0 && num_ulocks == 0) {
6982 /* Sanity check - ensure a pure oplock break is not a
6983 chained request. */
6984 if(CVAL(req->vwv+0, 0) != 0xff)
6985 DEBUG(0,("reply_lockingX: Error : pure oplock "
6986 "break is a chained %d request !\n",
6987 (unsigned int)CVAL(req->vwv+0, 0)));
6988 END_PROFILE(SMBlockingX);
6989 return;
6993 if (req->buflen <
6994 (num_ulocks + num_locks) * (large_file_format ? 20 : 10)) {
6995 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
6996 END_PROFILE(SMBlockingX);
6997 return;
7000 /* Data now points at the beginning of the list
7001 of smb_unlkrng structs */
7002 for(i = 0; i < (int)num_ulocks; i++) {
7003 lock_pid = get_lock_pid( data, i, large_file_format);
7004 count = get_lock_count( data, i, large_file_format);
7005 offset = get_lock_offset( data, i, large_file_format, &err);
7008 * There is no error code marked "stupid client bug".... :-).
7010 if(err) {
7011 END_PROFILE(SMBlockingX);
7012 reply_doserror(req, ERRDOS, ERRnoaccess);
7013 return;
7016 DEBUG(10,("reply_lockingX: unlock start=%.0f, len=%.0f for "
7017 "pid %u, file %s\n", (double)offset, (double)count,
7018 (unsigned int)lock_pid, fsp->fsp_name ));
7020 status = do_unlock(smbd_messaging_context(),
7021 fsp,
7022 lock_pid,
7023 count,
7024 offset,
7025 WINDOWS_LOCK);
7027 DEBUG(10, ("reply_lockingX: unlock returned %s\n",
7028 nt_errstr(status)));
7030 if (NT_STATUS_V(status)) {
7031 END_PROFILE(SMBlockingX);
7032 reply_nterror(req, status);
7033 return;
7037 /* Setup the timeout in seconds. */
7039 if (!lp_blocking_locks(SNUM(conn))) {
7040 lock_timeout = 0;
7043 /* Now do any requested locks */
7044 data += ((large_file_format ? 20 : 10)*num_ulocks);
7046 /* Data now points at the beginning of the list
7047 of smb_lkrng structs */
7049 for(i = 0; i < (int)num_locks; i++) {
7050 enum brl_type lock_type = ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
7051 READ_LOCK:WRITE_LOCK);
7052 lock_pid = get_lock_pid( data, i, large_file_format);
7053 count = get_lock_count( data, i, large_file_format);
7054 offset = get_lock_offset( data, i, large_file_format, &err);
7057 * There is no error code marked "stupid client bug".... :-).
7059 if(err) {
7060 END_PROFILE(SMBlockingX);
7061 reply_doserror(req, ERRDOS, ERRnoaccess);
7062 return;
7065 DEBUG(10,("reply_lockingX: lock start=%.0f, len=%.0f for pid "
7066 "%u, file %s timeout = %d\n", (double)offset,
7067 (double)count, (unsigned int)lock_pid,
7068 fsp->fsp_name, (int)lock_timeout ));
7070 if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
7071 struct blocking_lock_record *blr = NULL;
7073 if (lp_blocking_locks(SNUM(conn))) {
7075 /* Schedule a message to ourselves to
7076 remove the blocking lock record and
7077 return the right error. */
7079 blr = blocking_lock_cancel(fsp,
7080 lock_pid,
7081 offset,
7082 count,
7083 WINDOWS_LOCK,
7084 locktype,
7085 NT_STATUS_FILE_LOCK_CONFLICT);
7086 if (blr == NULL) {
7087 END_PROFILE(SMBlockingX);
7088 reply_nterror(
7089 req,
7090 NT_STATUS_DOS(
7091 ERRDOS,
7092 ERRcancelviolation));
7093 return;
7096 /* Remove a matching pending lock. */
7097 status = do_lock_cancel(fsp,
7098 lock_pid,
7099 count,
7100 offset,
7101 WINDOWS_LOCK,
7102 blr);
7103 } else {
7104 bool blocking_lock = lock_timeout ? True : False;
7105 bool defer_lock = False;
7106 struct byte_range_lock *br_lck;
7107 uint32 block_smbpid;
7109 br_lck = do_lock(smbd_messaging_context(),
7110 fsp,
7111 lock_pid,
7112 count,
7113 offset,
7114 lock_type,
7115 WINDOWS_LOCK,
7116 blocking_lock,
7117 &status,
7118 &block_smbpid,
7119 NULL);
7121 if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
7122 /* Windows internal resolution for blocking locks seems
7123 to be about 200ms... Don't wait for less than that. JRA. */
7124 if (lock_timeout != -1 && lock_timeout < lp_lock_spin_time()) {
7125 lock_timeout = lp_lock_spin_time();
7127 defer_lock = True;
7130 /* This heuristic seems to match W2K3 very well. If a
7131 lock sent with timeout of zero would fail with NT_STATUS_FILE_LOCK_CONFLICT
7132 it pretends we asked for a timeout of between 150 - 300 milliseconds as
7133 far as I can tell. Replacement for do_lock_spin(). JRA. */
7135 if (br_lck && lp_blocking_locks(SNUM(conn)) && !blocking_lock &&
7136 NT_STATUS_EQUAL((status), NT_STATUS_FILE_LOCK_CONFLICT)) {
7137 defer_lock = True;
7138 lock_timeout = lp_lock_spin_time();
7141 if (br_lck && defer_lock) {
7143 * A blocking lock was requested. Package up
7144 * this smb into a queued request and push it
7145 * onto the blocking lock queue.
7147 if(push_blocking_lock_request(br_lck,
7148 req,
7149 fsp,
7150 lock_timeout,
7152 lock_pid,
7153 lock_type,
7154 WINDOWS_LOCK,
7155 offset,
7156 count,
7157 block_smbpid)) {
7158 TALLOC_FREE(br_lck);
7159 END_PROFILE(SMBlockingX);
7160 return;
7164 TALLOC_FREE(br_lck);
7167 if (NT_STATUS_V(status)) {
7168 break;
7172 /* If any of the above locks failed, then we must unlock
7173 all of the previous locks (X/Open spec). */
7174 if (num_locks != 0 && !NT_STATUS_IS_OK(status)) {
7176 if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
7177 i = -1; /* we want to skip the for loop */
7181 * Ensure we don't do a remove on the lock that just failed,
7182 * as under POSIX rules, if we have a lock already there, we
7183 * will delete it (and we shouldn't) .....
7185 for(i--; i >= 0; i--) {
7186 lock_pid = get_lock_pid( data, i, large_file_format);
7187 count = get_lock_count( data, i, large_file_format);
7188 offset = get_lock_offset( data, i, large_file_format,
7189 &err);
7192 * There is no error code marked "stupid client
7193 * bug".... :-).
7195 if(err) {
7196 END_PROFILE(SMBlockingX);
7197 reply_doserror(req, ERRDOS, ERRnoaccess);
7198 return;
7201 do_unlock(smbd_messaging_context(),
7202 fsp,
7203 lock_pid,
7204 count,
7205 offset,
7206 WINDOWS_LOCK);
7208 END_PROFILE(SMBlockingX);
7209 reply_nterror(req, status);
7210 return;
7213 reply_outbuf(req, 2, 0);
7215 DEBUG(3, ("lockingX fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
7216 fsp->fnum, (unsigned int)locktype, num_locks, num_ulocks));
7218 END_PROFILE(SMBlockingX);
7219 chain_reply(req);
7222 #undef DBGC_CLASS
7223 #define DBGC_CLASS DBGC_ALL
7225 /****************************************************************************
7226 Reply to a SMBreadbmpx (read block multiplex) request.
7227 Always reply with an error, if someone has a platform really needs this,
7228 please contact vl@samba.org
7229 ****************************************************************************/
7231 void reply_readbmpx(struct smb_request *req)
7233 START_PROFILE(SMBreadBmpx);
7234 reply_doserror(req, ERRSRV, ERRuseSTD);
7235 END_PROFILE(SMBreadBmpx);
7236 return;
7239 /****************************************************************************
7240 Reply to a SMBreadbs (read block multiplex secondary) request.
7241 Always reply with an error, if someone has a platform really needs this,
7242 please contact vl@samba.org
7243 ****************************************************************************/
7245 void reply_readbs(struct smb_request *req)
7247 START_PROFILE(SMBreadBs);
7248 reply_doserror(req, ERRSRV, ERRuseSTD);
7249 END_PROFILE(SMBreadBs);
7250 return;
7253 /****************************************************************************
7254 Reply to a SMBsetattrE.
7255 ****************************************************************************/
7257 void reply_setattrE(struct smb_request *req)
7259 connection_struct *conn = req->conn;
7260 struct smb_file_time ft;
7261 files_struct *fsp;
7262 SMB_STRUCT_STAT sbuf;
7263 NTSTATUS status;
7265 START_PROFILE(SMBsetattrE);
7266 ZERO_STRUCT(ft);
7268 if (req->wct < 7) {
7269 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7270 END_PROFILE(SMBsetattrE);
7271 return;
7274 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
7276 if(!fsp || (fsp->conn != conn)) {
7277 reply_doserror(req, ERRDOS, ERRbadfid);
7278 END_PROFILE(SMBsetattrE);
7279 return;
7284 * Convert the DOS times into unix times.
7287 ft.atime = convert_time_t_to_timespec(
7288 srv_make_unix_date2(req->vwv+3));
7289 ft.mtime = convert_time_t_to_timespec(
7290 srv_make_unix_date2(req->vwv+5));
7291 ft.create_time = convert_time_t_to_timespec(
7292 srv_make_unix_date2(req->vwv+1));
7294 reply_outbuf(req, 0, 0);
7297 * Patch from Ray Frush <frush@engr.colostate.edu>
7298 * Sometimes times are sent as zero - ignore them.
7301 /* Ensure we have a valid stat struct for the source. */
7302 if (fsp->fh->fd != -1) {
7303 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
7304 status = map_nt_error_from_unix(errno);
7305 reply_nterror(req, status);
7306 END_PROFILE(SMBsetattrE);
7307 return;
7309 } else {
7310 int ret = -1;
7312 if (fsp->posix_open) {
7313 ret = SMB_VFS_LSTAT(conn, fsp->fsp_name, &sbuf);
7314 } else {
7315 ret = SMB_VFS_STAT(conn, fsp->fsp_name, &sbuf);
7317 if (ret == -1) {
7318 status = map_nt_error_from_unix(errno);
7319 reply_nterror(req, status);
7320 END_PROFILE(SMBsetattrE);
7321 return;
7325 status = smb_set_file_time(conn, fsp, fsp->fsp_name,
7326 &sbuf, &ft, true);
7327 if (!NT_STATUS_IS_OK(status)) {
7328 reply_doserror(req, ERRDOS, ERRnoaccess);
7329 END_PROFILE(SMBsetattrE);
7330 return;
7333 DEBUG( 3, ( "reply_setattrE fnum=%d actime=%u modtime=%u "
7334 " createtime=%u\n",
7335 fsp->fnum,
7336 (unsigned int)ft.atime.tv_sec,
7337 (unsigned int)ft.mtime.tv_sec,
7338 (unsigned int)ft.create_time.tv_sec
7341 END_PROFILE(SMBsetattrE);
7342 return;
7346 /* Back from the dead for OS/2..... JRA. */
7348 /****************************************************************************
7349 Reply to a SMBwritebmpx (write block multiplex primary) request.
7350 Always reply with an error, if someone has a platform really needs this,
7351 please contact vl@samba.org
7352 ****************************************************************************/
7354 void reply_writebmpx(struct smb_request *req)
7356 START_PROFILE(SMBwriteBmpx);
7357 reply_doserror(req, ERRSRV, ERRuseSTD);
7358 END_PROFILE(SMBwriteBmpx);
7359 return;
7362 /****************************************************************************
7363 Reply to a SMBwritebs (write block multiplex secondary) request.
7364 Always reply with an error, if someone has a platform really needs this,
7365 please contact vl@samba.org
7366 ****************************************************************************/
7368 void reply_writebs(struct smb_request *req)
7370 START_PROFILE(SMBwriteBs);
7371 reply_doserror(req, ERRSRV, ERRuseSTD);
7372 END_PROFILE(SMBwriteBs);
7373 return;
7376 /****************************************************************************
7377 Reply to a SMBgetattrE.
7378 ****************************************************************************/
7380 void reply_getattrE(struct smb_request *req)
7382 connection_struct *conn = req->conn;
7383 SMB_STRUCT_STAT sbuf;
7384 int mode;
7385 files_struct *fsp;
7386 struct timespec create_ts;
7388 START_PROFILE(SMBgetattrE);
7390 if (req->wct < 1) {
7391 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7392 END_PROFILE(SMBgetattrE);
7393 return;
7396 fsp = file_fsp(req, SVAL(req->vwv+0, 0));
7398 if(!fsp || (fsp->conn != conn)) {
7399 reply_doserror(req, ERRDOS, ERRbadfid);
7400 END_PROFILE(SMBgetattrE);
7401 return;
7404 /* Do an fstat on this file */
7405 if(fsp_stat(fsp, &sbuf)) {
7406 reply_unixerror(req, ERRDOS, ERRnoaccess);
7407 END_PROFILE(SMBgetattrE);
7408 return;
7411 mode = dos_mode(conn,fsp->fsp_name,&sbuf);
7414 * Convert the times into dos times. Set create
7415 * date to be last modify date as UNIX doesn't save
7416 * this.
7419 reply_outbuf(req, 11, 0);
7421 create_ts = get_create_timespec(&sbuf,
7422 lp_fake_dir_create_times(SNUM(conn)));
7423 srv_put_dos_date2((char *)req->outbuf, smb_vwv0, create_ts.tv_sec);
7424 srv_put_dos_date2((char *)req->outbuf, smb_vwv2, sbuf.st_atime);
7425 /* Should we check pending modtime here ? JRA */
7426 srv_put_dos_date2((char *)req->outbuf, smb_vwv4, sbuf.st_mtime);
7428 if (mode & aDIR) {
7429 SIVAL(req->outbuf, smb_vwv6, 0);
7430 SIVAL(req->outbuf, smb_vwv8, 0);
7431 } else {
7432 uint32 allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn,fsp, &sbuf);
7433 SIVAL(req->outbuf, smb_vwv6, (uint32)sbuf.st_size);
7434 SIVAL(req->outbuf, smb_vwv8, allocation_size);
7436 SSVAL(req->outbuf,smb_vwv10, mode);
7438 DEBUG( 3, ( "reply_getattrE fnum=%d\n", fsp->fnum));
7440 END_PROFILE(SMBgetattrE);
7441 return;