r17098: Samba3 now cleanly passes Samba4 RAW-LOCK torture
[Samba/nascimento.git] / source / smbd / reply.c
blobec618db3f854b6aa934b90030e76837ad5810647
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-2004.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 This file handles most of the reply_ calls that the server
24 makes to handle specific protocols
27 #include "includes.h"
29 /* look in server.c for some explanation of these variables */
30 extern enum protocol_types Protocol;
31 extern int max_send;
32 extern int max_recv;
33 unsigned int smb_echo_count = 0;
34 extern uint32 global_client_caps;
36 extern struct current_user current_user;
37 extern BOOL global_encrypted_passwords_negotiated;
39 /****************************************************************************
40 Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
41 We're assuming here that '/' is not the second byte in any multibyte char
42 set (a safe assumption). '\\' *may* be the second byte in a multibyte char
43 set.
44 ****************************************************************************/
46 NTSTATUS check_path_syntax(pstring destname, const pstring srcname)
48 char *d = destname;
49 const char *s = srcname;
50 NTSTATUS ret = NT_STATUS_OK;
51 BOOL start_of_name_component = True;
52 unsigned int num_bad_components = 0;
54 while (*s) {
55 if (IS_DIRECTORY_SEP(*s)) {
57 * Safe to assume is not the second part of a mb char as this is handled below.
59 /* Eat multiple '/' or '\\' */
60 while (IS_DIRECTORY_SEP(*s)) {
61 s++;
63 if ((d != destname) && (*s != '\0')) {
64 /* We only care about non-leading or trailing '/' or '\\' */
65 *d++ = '/';
68 start_of_name_component = True;
69 continue;
72 if (start_of_name_component) {
73 if ((s[0] == '.') && (s[1] == '.') && (IS_DIRECTORY_SEP(s[2]) || s[2] == '\0')) {
74 /* Uh oh - "/../" or "\\..\\" or "/..\0" or "\\..\0" ! */
77 * No mb char starts with '.' so we're safe checking the directory separator here.
80 /* If we just added a '/' - delete it */
81 if ((d > destname) && (*(d-1) == '/')) {
82 *(d-1) = '\0';
83 d--;
86 /* Are we at the start ? Can't go back further if so. */
87 if (d <= destname) {
88 ret = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
89 break;
91 /* Go back one level... */
92 /* We know this is safe as '/' cannot be part of a mb sequence. */
93 /* NOTE - if this assumption is invalid we are not in good shape... */
94 /* Decrement d first as d points to the *next* char to write into. */
95 for (d--; d > destname; d--) {
96 if (*d == '/')
97 break;
99 s += 2; /* Else go past the .. */
100 /* We're still at the start of a name component, just the previous one. */
102 if (num_bad_components) {
103 /* Hmmm. Should we only decrement the bad_components if
104 we're removing a bad component ? Need to check this. JRA. */
105 num_bad_components--;
108 continue;
110 } else if ((s[0] == '.') && ((s[1] == '\0') || IS_DIRECTORY_SEP(s[1]))) {
111 /* Component of pathname can't be "." only. */
112 ret = NT_STATUS_OBJECT_NAME_INVALID;
113 num_bad_components++;
114 *d++ = *s++;
115 continue;
119 if (!(*s & 0x80)) {
120 if (*s <= 0x1f) {
121 return NT_STATUS_OBJECT_NAME_INVALID;
123 switch (*s) {
124 case '*':
125 case '?':
126 case '<':
127 case '>':
128 case '"':
129 return NT_STATUS_OBJECT_NAME_INVALID;
130 default:
131 *d++ = *s++;
132 break;
134 } else {
135 switch(next_mb_char_size(s)) {
136 case 4:
137 *d++ = *s++;
138 case 3:
139 *d++ = *s++;
140 case 2:
141 *d++ = *s++;
142 case 1:
143 *d++ = *s++;
144 break;
145 default:
146 DEBUG(0,("check_path_syntax: character length assumptions invalid !\n"));
147 *d = '\0';
148 return NT_STATUS_INVALID_PARAMETER;
151 if (start_of_name_component && num_bad_components) {
152 num_bad_components++;
154 start_of_name_component = False;
157 if (NT_STATUS_EQUAL(ret, NT_STATUS_OBJECT_NAME_INVALID)) {
158 if (num_bad_components > 1) {
159 ret = NT_STATUS_OBJECT_PATH_NOT_FOUND;
163 *d = '\0';
164 return ret;
167 /****************************************************************************
168 Ensure we check the path in *exactly* the same way as W2K for a findfirst/findnext
169 path or anything including wildcards.
170 We're assuming here that '/' is not the second byte in any multibyte char
171 set (a safe assumption). '\\' *may* be the second byte in a multibyte char
172 set.
173 ****************************************************************************/
175 NTSTATUS check_path_syntax_wcard(pstring destname, const pstring srcname, BOOL *p_contains_wcard)
177 char *d = destname;
178 const char *s = srcname;
179 NTSTATUS ret = NT_STATUS_OK;
180 BOOL start_of_name_component = True;
181 unsigned int num_bad_components = 0;
183 *p_contains_wcard = False;
185 while (*s) {
186 if (IS_DIRECTORY_SEP(*s)) {
188 * Safe to assume is not the second part of a mb char as this is handled below.
190 /* Eat multiple '/' or '\\' */
191 while (IS_DIRECTORY_SEP(*s)) {
192 s++;
194 if ((d != destname) && (*s != '\0')) {
195 /* We only care about non-leading or trailing '/' or '\\' */
196 *d++ = '/';
199 start_of_name_component = True;
200 continue;
203 if (start_of_name_component) {
204 if ((s[0] == '.') && (s[1] == '.') && (IS_DIRECTORY_SEP(s[2]) || s[2] == '\0')) {
205 /* Uh oh - "/../" or "\\..\\" or "/..\0" or "\\..\0" ! */
208 * No mb char starts with '.' so we're safe checking the directory separator here.
211 /* If we just added a '/' - delete it */
212 if ((d > destname) && (*(d-1) == '/')) {
213 *(d-1) = '\0';
214 d--;
217 /* Are we at the start ? Can't go back further if so. */
218 if (d <= destname) {
219 ret = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
220 break;
222 /* Go back one level... */
223 /* We know this is safe as '/' cannot be part of a mb sequence. */
224 /* NOTE - if this assumption is invalid we are not in good shape... */
225 /* Decrement d first as d points to the *next* char to write into. */
226 for (d--; d > destname; d--) {
227 if (*d == '/')
228 break;
230 s += 2; /* Else go past the .. */
231 /* We're still at the start of a name component, just the previous one. */
233 if (num_bad_components) {
234 /* Hmmm. Should we only decrement the bad_components if
235 we're removing a bad component ? Need to check this. JRA. */
236 num_bad_components--;
239 continue;
241 } else if ((s[0] == '.') && ((s[1] == '\0') || IS_DIRECTORY_SEP(s[1]))) {
242 /* Component of pathname can't be "." only. */
243 ret = NT_STATUS_OBJECT_NAME_INVALID;
244 num_bad_components++;
245 *d++ = *s++;
246 continue;
250 if (!(*s & 0x80)) {
251 if (*s <= 0x1f) {
252 return NT_STATUS_OBJECT_NAME_INVALID;
254 if (!*p_contains_wcard) {
255 switch (*s) {
256 case '*':
257 case '?':
258 case '<':
259 case '>':
260 case '"':
261 *p_contains_wcard = True;
262 break;
263 default:
264 break;
267 *d++ = *s++;
268 } else {
269 switch(next_mb_char_size(s)) {
270 case 4:
271 *d++ = *s++;
272 /*fall through*/
273 case 3:
274 *d++ = *s++;
275 /*fall through*/
276 case 2:
277 *d++ = *s++;
278 /*fall through*/
279 case 1:
280 *d++ = *s++;
281 break;
282 default:
283 DEBUG(0,("check_path_syntax_wcard: character length assumptions invalid !\n"));
284 *d = '\0';
285 return NT_STATUS_INVALID_PARAMETER;
288 if (start_of_name_component && num_bad_components) {
289 num_bad_components++;
291 start_of_name_component = False;
294 if (NT_STATUS_EQUAL(ret, NT_STATUS_OBJECT_NAME_INVALID)) {
295 /* For some strange reason being called from findfirst changes
296 the num_components number to cause the error return to change. JRA. */
297 if (num_bad_components > 2) {
298 ret = NT_STATUS_OBJECT_PATH_NOT_FOUND;
302 *d = '\0';
303 return ret;
306 /****************************************************************************
307 Check the path for a POSIX client.
308 We're assuming here that '/' is not the second byte in any multibyte char
309 set (a safe assumption).
310 ****************************************************************************/
312 NTSTATUS check_path_syntax_posix(pstring destname, const pstring srcname)
314 char *d = destname;
315 const char *s = srcname;
316 NTSTATUS ret = NT_STATUS_OK;
317 BOOL start_of_name_component = True;
319 while (*s) {
320 if (*s == '/') {
322 * Safe to assume is not the second part of a mb char as this is handled below.
324 /* Eat multiple '/' or '\\' */
325 while (*s == '/') {
326 s++;
328 if ((d != destname) && (*s != '\0')) {
329 /* We only care about non-leading or trailing '/' */
330 *d++ = '/';
333 start_of_name_component = True;
334 continue;
337 if (start_of_name_component) {
338 if ((s[0] == '.') && (s[1] == '.') && (s[2] == '/' || s[2] == '\0')) {
339 /* Uh oh - "/../" or "/..\0" ! */
342 * No mb char starts with '.' so we're safe checking the directory separator here.
345 /* If we just added a '/' - delete it */
346 if ((d > destname) && (*(d-1) == '/')) {
347 *(d-1) = '\0';
348 d--;
351 /* Are we at the start ? Can't go back further if so. */
352 if (d <= destname) {
353 ret = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
354 break;
356 /* Go back one level... */
357 /* We know this is safe as '/' cannot be part of a mb sequence. */
358 /* NOTE - if this assumption is invalid we are not in good shape... */
359 /* Decrement d first as d points to the *next* char to write into. */
360 for (d--; d > destname; d--) {
361 if (*d == '/')
362 break;
364 s += 2; /* Else go past the .. */
365 continue;
367 } else if ((s[0] == '.') && ((s[1] == '\0') || (s[1] == '/'))) {
368 /* Eat the '.' */
369 s++;
370 continue;
374 if (!(*s & 0x80)) {
375 *d++ = *s++;
376 } else {
377 switch(next_mb_char_size(s)) {
378 case 4:
379 *d++ = *s++;
380 /*fall through*/
381 case 3:
382 *d++ = *s++;
383 /*fall through*/
384 case 2:
385 *d++ = *s++;
386 /*fall through*/
387 case 1:
388 *d++ = *s++;
389 break;
390 default:
391 DEBUG(0,("check_path_syntax_posix: character length assumptions invalid !\n"));
392 *d = '\0';
393 return NT_STATUS_INVALID_PARAMETER;
396 start_of_name_component = False;
399 *d = '\0';
400 return ret;
403 /****************************************************************************
404 Pull a string and check the path allowing a wilcard - provide for error return.
405 ****************************************************************************/
407 size_t srvstr_get_path_wcard(char *inbuf, char *dest, const char *src, size_t dest_len, size_t src_len, int flags,
408 NTSTATUS *err, BOOL *contains_wcard)
410 pstring tmppath;
411 char *tmppath_ptr = tmppath;
412 size_t ret;
413 #ifdef DEVELOPER
414 SMB_ASSERT(dest_len == sizeof(pstring));
415 #endif
417 if (src_len == 0) {
418 ret = srvstr_pull_buf( inbuf, tmppath_ptr, src, dest_len, flags);
419 } else {
420 ret = srvstr_pull( inbuf, tmppath_ptr, src, dest_len, src_len, flags);
423 *contains_wcard = False;
425 if (lp_posix_pathnames()) {
426 *err = check_path_syntax_posix(dest, tmppath);
427 } else {
428 *err = check_path_syntax_wcard(dest, tmppath, contains_wcard);
431 return ret;
434 /****************************************************************************
435 Pull a string and check the path - provide for error return.
436 ****************************************************************************/
438 size_t srvstr_get_path(char *inbuf, char *dest, const char *src, size_t dest_len, size_t src_len, int flags, NTSTATUS *err)
440 pstring tmppath;
441 char *tmppath_ptr = tmppath;
442 size_t ret;
443 #ifdef DEVELOPER
444 SMB_ASSERT(dest_len == sizeof(pstring));
445 #endif
447 if (src_len == 0) {
448 ret = srvstr_pull_buf( inbuf, tmppath_ptr, src, dest_len, flags);
449 } else {
450 ret = srvstr_pull( inbuf, tmppath_ptr, src, dest_len, src_len, flags);
452 if (lp_posix_pathnames()) {
453 *err = check_path_syntax_posix(dest, tmppath);
454 } else {
455 *err = check_path_syntax(dest, tmppath);
458 return ret;
461 /****************************************************************************
462 Reply to a special message.
463 ****************************************************************************/
465 int reply_special(char *inbuf,char *outbuf)
467 int outsize = 4;
468 int msg_type = CVAL(inbuf,0);
469 int msg_flags = CVAL(inbuf,1);
470 fstring name1,name2;
471 char name_type = 0;
473 static BOOL already_got_session = False;
475 *name1 = *name2 = 0;
477 memset(outbuf,'\0',smb_size);
479 smb_setlen(outbuf,0);
481 switch (msg_type) {
482 case 0x81: /* session request */
484 if (already_got_session) {
485 exit_server("multiple session request not permitted");
488 SCVAL(outbuf,0,0x82);
489 SCVAL(outbuf,3,0);
490 if (name_len(inbuf+4) > 50 ||
491 name_len(inbuf+4 + name_len(inbuf + 4)) > 50) {
492 DEBUG(0,("Invalid name length in session request\n"));
493 return(0);
495 name_extract(inbuf,4,name1);
496 name_type = name_extract(inbuf,4 + name_len(inbuf + 4),name2);
497 DEBUG(2,("netbios connect: name1=%s name2=%s\n",
498 name1,name2));
500 set_local_machine_name(name1, True);
501 set_remote_machine_name(name2, True);
503 DEBUG(2,("netbios connect: local=%s remote=%s, name type = %x\n",
504 get_local_machine_name(), get_remote_machine_name(),
505 name_type));
507 if (name_type == 'R') {
508 /* We are being asked for a pathworks session ---
509 no thanks! */
510 SCVAL(outbuf, 0,0x83);
511 break;
514 /* only add the client's machine name to the list
515 of possibly valid usernames if we are operating
516 in share mode security */
517 if (lp_security() == SEC_SHARE) {
518 add_session_user(get_remote_machine_name());
521 reload_services(True);
522 reopen_logs();
524 already_got_session = True;
525 break;
527 case 0x89: /* session keepalive request
528 (some old clients produce this?) */
529 SCVAL(outbuf,0,SMBkeepalive);
530 SCVAL(outbuf,3,0);
531 break;
533 case 0x82: /* positive session response */
534 case 0x83: /* negative session response */
535 case 0x84: /* retarget session response */
536 DEBUG(0,("Unexpected session response\n"));
537 break;
539 case SMBkeepalive: /* session keepalive */
540 default:
541 return(0);
544 DEBUG(5,("init msg_type=0x%x msg_flags=0x%x\n",
545 msg_type, msg_flags));
547 return(outsize);
550 /****************************************************************************
551 Reply to a tcon.
552 conn POINTER CAN BE NULL HERE !
553 ****************************************************************************/
555 int reply_tcon(connection_struct *conn,
556 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
558 const char *service;
559 pstring service_buf;
560 pstring password;
561 pstring dev;
562 int outsize = 0;
563 uint16 vuid = SVAL(inbuf,smb_uid);
564 int pwlen=0;
565 NTSTATUS nt_status;
566 char *p;
567 DATA_BLOB password_blob;
569 START_PROFILE(SMBtcon);
571 *service_buf = *password = *dev = 0;
573 p = smb_buf(inbuf)+1;
574 p += srvstr_pull_buf(inbuf, service_buf, p, sizeof(service_buf), STR_TERMINATE) + 1;
575 pwlen = srvstr_pull_buf(inbuf, password, p, sizeof(password), STR_TERMINATE) + 1;
576 p += pwlen;
577 p += srvstr_pull_buf(inbuf, dev, p, sizeof(dev), STR_TERMINATE) + 1;
579 p = strrchr_m(service_buf,'\\');
580 if (p) {
581 service = p+1;
582 } else {
583 service = service_buf;
586 password_blob = data_blob(password, pwlen+1);
588 conn = make_connection(service,password_blob,dev,vuid,&nt_status);
590 data_blob_clear_free(&password_blob);
592 if (!conn) {
593 END_PROFILE(SMBtcon);
594 return ERROR_NT(nt_status);
597 outsize = set_message(outbuf,2,0,True);
598 SSVAL(outbuf,smb_vwv0,max_recv);
599 SSVAL(outbuf,smb_vwv1,conn->cnum);
600 SSVAL(outbuf,smb_tid,conn->cnum);
602 DEBUG(3,("tcon service=%s cnum=%d\n",
603 service, conn->cnum));
605 END_PROFILE(SMBtcon);
606 return(outsize);
609 /****************************************************************************
610 Reply to a tcon and X.
611 conn POINTER CAN BE NULL HERE !
612 ****************************************************************************/
614 int reply_tcon_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
616 fstring service;
617 DATA_BLOB password;
619 /* what the cleint thinks the device is */
620 fstring client_devicetype;
621 /* what the server tells the client the share represents */
622 const char *server_devicetype;
623 NTSTATUS nt_status;
624 uint16 vuid = SVAL(inbuf,smb_uid);
625 int passlen = SVAL(inbuf,smb_vwv3);
626 pstring path;
627 char *p, *q;
629 START_PROFILE(SMBtconX);
631 *service = *client_devicetype = 0;
633 /* we might have to close an old one */
634 if ((SVAL(inbuf,smb_vwv2) & 0x1) && conn) {
635 close_cnum(conn,vuid);
638 if (passlen > MAX_PASS_LEN) {
639 return ERROR_DOS(ERRDOS,ERRbuftoosmall);
642 if (global_encrypted_passwords_negotiated) {
643 password = data_blob(smb_buf(inbuf),passlen);
644 } else {
645 password = data_blob(smb_buf(inbuf),passlen+1);
646 /* Ensure correct termination */
647 password.data[passlen]=0;
650 p = smb_buf(inbuf) + passlen;
651 p += srvstr_pull_buf(inbuf, path, p, sizeof(path), STR_TERMINATE);
654 * the service name can be either: \\server\share
655 * or share directly like on the DELL PowerVault 705
657 if (*path=='\\') {
658 q = strchr_m(path+2,'\\');
659 if (!q) {
660 END_PROFILE(SMBtconX);
661 return(ERROR_DOS(ERRDOS,ERRnosuchshare));
663 fstrcpy(service,q+1);
665 else
666 fstrcpy(service,path);
668 p += srvstr_pull(inbuf, client_devicetype, p, sizeof(client_devicetype), 6, STR_ASCII);
670 DEBUG(4,("Client requested device type [%s] for share [%s]\n", client_devicetype, service));
672 conn = make_connection(service,password,client_devicetype,vuid,&nt_status);
674 data_blob_clear_free(&password);
676 if (!conn) {
677 END_PROFILE(SMBtconX);
678 return ERROR_NT(nt_status);
681 if ( IS_IPC(conn) )
682 server_devicetype = "IPC";
683 else if ( IS_PRINT(conn) )
684 server_devicetype = "LPT1:";
685 else
686 server_devicetype = "A:";
688 if (Protocol < PROTOCOL_NT1) {
689 set_message(outbuf,2,0,True);
690 p = smb_buf(outbuf);
691 p += srvstr_push(outbuf, p, server_devicetype, -1,
692 STR_TERMINATE|STR_ASCII);
693 set_message_end(outbuf,p);
694 } else {
695 /* NT sets the fstype of IPC$ to the null string */
696 const char *fstype = IS_IPC(conn) ? "" : lp_fstype(SNUM(conn));
698 set_message(outbuf,3,0,True);
700 p = smb_buf(outbuf);
701 p += srvstr_push(outbuf, p, server_devicetype, -1,
702 STR_TERMINATE|STR_ASCII);
703 p += srvstr_push(outbuf, p, fstype, -1,
704 STR_TERMINATE);
706 set_message_end(outbuf,p);
708 /* what does setting this bit do? It is set by NT4 and
709 may affect the ability to autorun mounted cdroms */
710 SSVAL(outbuf, smb_vwv2, SMB_SUPPORT_SEARCH_BITS|
711 (lp_csc_policy(SNUM(conn)) << 2));
713 init_dfsroot(conn, inbuf, outbuf);
717 DEBUG(3,("tconX service=%s \n",
718 service));
720 /* set the incoming and outgoing tid to the just created one */
721 SSVAL(inbuf,smb_tid,conn->cnum);
722 SSVAL(outbuf,smb_tid,conn->cnum);
724 END_PROFILE(SMBtconX);
725 return chain_reply(inbuf,outbuf,length,bufsize);
728 /****************************************************************************
729 Reply to an unknown type.
730 ****************************************************************************/
732 int reply_unknown(char *inbuf,char *outbuf)
734 int type;
735 type = CVAL(inbuf,smb_com);
737 DEBUG(0,("unknown command type (%s): type=%d (0x%X)\n",
738 smb_fn_name(type), type, type));
740 return(ERROR_DOS(ERRSRV,ERRunknownsmb));
743 /****************************************************************************
744 Reply to an ioctl.
745 conn POINTER CAN BE NULL HERE !
746 ****************************************************************************/
748 int reply_ioctl(connection_struct *conn,
749 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
751 uint16 device = SVAL(inbuf,smb_vwv1);
752 uint16 function = SVAL(inbuf,smb_vwv2);
753 uint32 ioctl_code = (device << 16) + function;
754 int replysize, outsize;
755 char *p;
756 START_PROFILE(SMBioctl);
758 DEBUG(4, ("Received IOCTL (code 0x%x)\n", ioctl_code));
760 switch (ioctl_code) {
761 case IOCTL_QUERY_JOB_INFO:
762 replysize = 32;
763 break;
764 default:
765 END_PROFILE(SMBioctl);
766 return(ERROR_DOS(ERRSRV,ERRnosupport));
769 outsize = set_message(outbuf,8,replysize+1,True);
770 SSVAL(outbuf,smb_vwv1,replysize); /* Total data bytes returned */
771 SSVAL(outbuf,smb_vwv5,replysize); /* Data bytes this buffer */
772 SSVAL(outbuf,smb_vwv6,52); /* Offset to data */
773 p = smb_buf(outbuf) + 1; /* Allow for alignment */
775 switch (ioctl_code) {
776 case IOCTL_QUERY_JOB_INFO:
778 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
779 if (!fsp) {
780 END_PROFILE(SMBioctl);
781 return(UNIXERROR(ERRDOS,ERRbadfid));
783 SSVAL(p,0,fsp->rap_print_jobid); /* Job number */
784 srvstr_push(outbuf, p+2, global_myname(), 15, STR_TERMINATE|STR_ASCII);
785 if (conn) {
786 srvstr_push(outbuf, p+18, lp_servicename(SNUM(conn)), 13, STR_TERMINATE|STR_ASCII);
788 break;
792 END_PROFILE(SMBioctl);
793 return outsize;
796 /****************************************************************************
797 Reply to a chkpth.
798 ****************************************************************************/
800 int reply_chkpth(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
802 int outsize = 0;
803 pstring name;
804 BOOL ok = False;
805 BOOL bad_path = False;
806 SMB_STRUCT_STAT sbuf;
807 NTSTATUS status;
809 START_PROFILE(SMBchkpth);
811 srvstr_get_path(inbuf, name, smb_buf(inbuf) + 1, sizeof(name), 0, STR_TERMINATE, &status);
812 if (!NT_STATUS_IS_OK(status)) {
813 END_PROFILE(SMBchkpth);
815 /* Strange DOS error code semantics only for chkpth... */
816 if (!(SVAL(inbuf,smb_flg2) & FLAGS2_32_BIT_ERROR_CODES)) {
817 if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_INVALID,status)) {
818 /* We need to map to ERRbadpath */
819 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
822 return ERROR_NT(status);
825 RESOLVE_DFSPATH(name, conn, inbuf, outbuf);
827 unix_convert(name,conn,0,&bad_path,&sbuf);
828 if (bad_path) {
829 END_PROFILE(SMBchkpth);
830 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
833 if (check_name(name,conn)) {
834 if (VALID_STAT(sbuf) || SMB_VFS_STAT(conn,name,&sbuf) == 0)
835 if (!(ok = S_ISDIR(sbuf.st_mode))) {
836 END_PROFILE(SMBchkpth);
837 return ERROR_BOTH(NT_STATUS_NOT_A_DIRECTORY,ERRDOS,ERRbadpath);
841 if (!ok) {
842 /* We special case this - as when a Windows machine
843 is parsing a path is steps through the components
844 one at a time - if a component fails it expects
845 ERRbadpath, not ERRbadfile.
847 if(errno == ENOENT) {
849 * Windows returns different error codes if
850 * the parent directory is valid but not the
851 * last component - it returns NT_STATUS_OBJECT_NAME_NOT_FOUND
852 * for that case and NT_STATUS_OBJECT_PATH_NOT_FOUND
853 * if the path is invalid. This is different from set_bad_path_error()
854 * in the non-NT error case.
856 END_PROFILE(SMBchkpth);
857 return ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpath);
860 END_PROFILE(SMBchkpth);
861 return(UNIXERROR(ERRDOS,ERRbadpath));
864 outsize = set_message(outbuf,0,0,False);
865 DEBUG(3,("chkpth %s mode=%d\n", name, (int)SVAL(inbuf,smb_vwv0)));
867 END_PROFILE(SMBchkpth);
868 return(outsize);
871 /****************************************************************************
872 Reply to a getatr.
873 ****************************************************************************/
875 int reply_getatr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
877 pstring fname;
878 int outsize = 0;
879 SMB_STRUCT_STAT sbuf;
880 BOOL ok = False;
881 int mode=0;
882 SMB_OFF_T size=0;
883 time_t mtime=0;
884 BOOL bad_path = False;
885 char *p;
886 NTSTATUS status;
888 START_PROFILE(SMBgetatr);
890 p = smb_buf(inbuf) + 1;
891 p += srvstr_get_path(inbuf, fname, p, sizeof(fname), 0, STR_TERMINATE, &status);
892 if (!NT_STATUS_IS_OK(status)) {
893 END_PROFILE(SMBgetatr);
894 return ERROR_NT(status);
897 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
899 /* dos smetimes asks for a stat of "" - it returns a "hidden directory"
900 under WfWg - weird! */
901 if (! (*fname)) {
902 mode = aHIDDEN | aDIR;
903 if (!CAN_WRITE(conn))
904 mode |= aRONLY;
905 size = 0;
906 mtime = 0;
907 ok = True;
908 } else {
909 unix_convert(fname,conn,0,&bad_path,&sbuf);
910 if (bad_path) {
911 END_PROFILE(SMBgetatr);
912 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
914 if (check_name(fname,conn)) {
915 if (VALID_STAT(sbuf) || SMB_VFS_STAT(conn,fname,&sbuf) == 0) {
916 mode = dos_mode(conn,fname,&sbuf);
917 size = sbuf.st_size;
918 mtime = sbuf.st_mtime;
919 if (mode & aDIR)
920 size = 0;
921 ok = True;
922 } else {
923 DEBUG(3,("stat of %s failed (%s)\n",fname,strerror(errno)));
928 if (!ok) {
929 END_PROFILE(SMBgetatr);
930 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRbadfile);
933 outsize = set_message(outbuf,10,0,True);
935 SSVAL(outbuf,smb_vwv0,mode);
936 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
937 srv_put_dos_date3(outbuf,smb_vwv1,mtime & ~1);
938 } else {
939 srv_put_dos_date3(outbuf,smb_vwv1,mtime);
941 SIVAL(outbuf,smb_vwv3,(uint32)size);
943 if (Protocol >= PROTOCOL_NT1) {
944 SSVAL(outbuf,smb_flg2,SVAL(outbuf, smb_flg2) | FLAGS2_IS_LONG_NAME);
947 DEBUG( 3, ( "getatr name=%s mode=%d size=%d\n", fname, mode, (uint32)size ) );
949 END_PROFILE(SMBgetatr);
950 return(outsize);
953 /****************************************************************************
954 Reply to a setatr.
955 ****************************************************************************/
957 int reply_setatr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
959 pstring fname;
960 int outsize = 0;
961 BOOL ok=False;
962 int mode;
963 time_t mtime;
964 SMB_STRUCT_STAT sbuf;
965 BOOL bad_path = False;
966 char *p;
967 NTSTATUS status;
969 START_PROFILE(SMBsetatr);
971 p = smb_buf(inbuf) + 1;
972 p += srvstr_get_path(inbuf, fname, p, sizeof(fname), 0, STR_TERMINATE, &status);
973 if (!NT_STATUS_IS_OK(status)) {
974 END_PROFILE(SMBsetatr);
975 return ERROR_NT(status);
978 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
980 unix_convert(fname,conn,0,&bad_path,&sbuf);
981 if (bad_path) {
982 END_PROFILE(SMBsetatr);
983 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
986 mode = SVAL(inbuf,smb_vwv0);
987 mtime = srv_make_unix_date3(inbuf+smb_vwv1);
989 if (mode != FILE_ATTRIBUTE_NORMAL) {
990 if (VALID_STAT_OF_DIR(sbuf))
991 mode |= aDIR;
992 else
993 mode &= ~aDIR;
995 if (check_name(fname,conn)) {
996 ok = (file_set_dosmode(conn,fname,mode,&sbuf,False) == 0);
998 } else {
999 ok = True;
1002 if (ok)
1003 ok = set_filetime(conn,fname,mtime);
1005 if (!ok) {
1006 END_PROFILE(SMBsetatr);
1007 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnoaccess);
1010 outsize = set_message(outbuf,0,0,False);
1012 DEBUG( 3, ( "setatr name=%s mode=%d\n", fname, mode ) );
1014 END_PROFILE(SMBsetatr);
1015 return(outsize);
1018 /****************************************************************************
1019 Reply to a dskattr.
1020 ****************************************************************************/
1022 int reply_dskattr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1024 int outsize = 0;
1025 SMB_BIG_UINT dfree,dsize,bsize;
1026 START_PROFILE(SMBdskattr);
1028 if (get_dfree_info(conn,".",True,&bsize,&dfree,&dsize) == (SMB_BIG_UINT)-1) {
1029 END_PROFILE(SMBdskattr);
1030 return(UNIXERROR(ERRHRD,ERRgeneral));
1033 outsize = set_message(outbuf,5,0,True);
1035 if (Protocol <= PROTOCOL_LANMAN2) {
1036 double total_space, free_space;
1037 /* we need to scale this to a number that DOS6 can handle. We
1038 use floating point so we can handle large drives on systems
1039 that don't have 64 bit integers
1041 we end up displaying a maximum of 2G to DOS systems
1043 total_space = dsize * (double)bsize;
1044 free_space = dfree * (double)bsize;
1046 dsize = (total_space+63*512) / (64*512);
1047 dfree = (free_space+63*512) / (64*512);
1049 if (dsize > 0xFFFF) dsize = 0xFFFF;
1050 if (dfree > 0xFFFF) dfree = 0xFFFF;
1052 SSVAL(outbuf,smb_vwv0,dsize);
1053 SSVAL(outbuf,smb_vwv1,64); /* this must be 64 for dos systems */
1054 SSVAL(outbuf,smb_vwv2,512); /* and this must be 512 */
1055 SSVAL(outbuf,smb_vwv3,dfree);
1056 } else {
1057 SSVAL(outbuf,smb_vwv0,dsize);
1058 SSVAL(outbuf,smb_vwv1,bsize/512);
1059 SSVAL(outbuf,smb_vwv2,512);
1060 SSVAL(outbuf,smb_vwv3,dfree);
1063 DEBUG(3,("dskattr dfree=%d\n", (unsigned int)dfree));
1065 END_PROFILE(SMBdskattr);
1066 return(outsize);
1069 /****************************************************************************
1070 Reply to a search.
1071 Can be called from SMBsearch, SMBffirst or SMBfunique.
1072 ****************************************************************************/
1074 int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1076 pstring mask;
1077 pstring directory;
1078 pstring fname;
1079 SMB_OFF_T size;
1080 uint32 mode;
1081 time_t date;
1082 uint32 dirtype;
1083 int outsize = 0;
1084 unsigned int numentries = 0;
1085 unsigned int maxentries = 0;
1086 BOOL finished = False;
1087 char *p;
1088 BOOL ok = False;
1089 int status_len;
1090 pstring path;
1091 char status[21];
1092 int dptr_num= -1;
1093 BOOL check_descend = False;
1094 BOOL expect_close = False;
1095 BOOL can_open = True;
1096 BOOL bad_path = False;
1097 NTSTATUS nt_status;
1098 BOOL mask_contains_wcard = False;
1099 BOOL allow_long_path_components = (SVAL(inbuf,smb_flg2) & FLAGS2_LONG_PATH_COMPONENTS) ? True : False;
1101 START_PROFILE(SMBsearch);
1103 if (lp_posix_pathnames()) {
1104 END_PROFILE(SMBsearch);
1105 return reply_unknown(inbuf, outbuf);
1108 *mask = *directory = *fname = 0;
1110 /* If we were called as SMBffirst then we must expect close. */
1111 if(CVAL(inbuf,smb_com) == SMBffirst)
1112 expect_close = True;
1114 outsize = set_message(outbuf,1,3,True);
1115 maxentries = SVAL(inbuf,smb_vwv0);
1116 dirtype = SVAL(inbuf,smb_vwv1);
1117 p = smb_buf(inbuf) + 1;
1118 p += srvstr_get_path_wcard(inbuf, path, p, sizeof(path), 0, STR_TERMINATE, &nt_status, &mask_contains_wcard);
1119 if (!NT_STATUS_IS_OK(nt_status)) {
1120 END_PROFILE(SMBsearch);
1121 return ERROR_NT(nt_status);
1124 RESOLVE_DFSPATH_WCARD(path, conn, inbuf, outbuf);
1126 p++;
1127 status_len = SVAL(p, 0);
1128 p += 2;
1130 /* dirtype &= ~aDIR; */
1132 if (status_len == 0) {
1133 SMB_STRUCT_STAT sbuf;
1134 pstring dir2;
1136 pstrcpy(directory,path);
1137 pstrcpy(dir2,path);
1138 unix_convert(directory,conn,0,&bad_path,&sbuf);
1139 unix_format(dir2);
1141 if (!check_name(directory,conn))
1142 can_open = False;
1144 p = strrchr_m(dir2,'/');
1145 if (p == NULL) {
1146 pstrcpy(mask,dir2);
1147 *dir2 = 0;
1148 } else {
1149 *p = 0;
1150 pstrcpy(mask,p+1);
1153 p = strrchr_m(directory,'/');
1154 if (!p)
1155 *directory = 0;
1156 else
1157 *p = 0;
1159 if (strlen(directory) == 0)
1160 pstrcpy(directory,".");
1161 memset((char *)status,'\0',21);
1162 SCVAL(status,0,(dirtype & 0x1F));
1163 } else {
1164 int status_dirtype;
1166 memcpy(status,p,21);
1167 status_dirtype = CVAL(status,0) & 0x1F;
1168 if (status_dirtype != (dirtype & 0x1F))
1169 dirtype = status_dirtype;
1171 conn->dirptr = dptr_fetch(status+12,&dptr_num);
1172 if (!conn->dirptr)
1173 goto SearchEmpty;
1174 string_set(&conn->dirpath,dptr_path(dptr_num));
1175 pstrcpy(mask, dptr_wcard(dptr_num));
1178 if (can_open) {
1179 p = smb_buf(outbuf) + 3;
1180 ok = True;
1182 if (status_len == 0) {
1183 dptr_num = dptr_create(conn,directory,True,expect_close,SVAL(inbuf,smb_pid), mask, mask_contains_wcard, dirtype);
1184 if (dptr_num < 0) {
1185 if(dptr_num == -2) {
1186 END_PROFILE(SMBsearch);
1187 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnofids);
1189 END_PROFILE(SMBsearch);
1190 return ERROR_DOS(ERRDOS,ERRnofids);
1192 } else {
1193 dirtype = dptr_attr(dptr_num);
1196 DEBUG(4,("dptr_num is %d\n",dptr_num));
1198 if (ok) {
1199 if ((dirtype&0x1F) == aVOLID) {
1200 memcpy(p,status,21);
1201 make_dir_struct(p,"???????????",volume_label(SNUM(conn)),
1202 0,aVOLID,0,!allow_long_path_components);
1203 dptr_fill(p+12,dptr_num);
1204 if (dptr_zero(p+12) && (status_len==0))
1205 numentries = 1;
1206 else
1207 numentries = 0;
1208 p += DIR_STRUCT_SIZE;
1209 } else {
1210 unsigned int i;
1211 maxentries = MIN(maxentries, ((BUFFER_SIZE - (p - outbuf))/DIR_STRUCT_SIZE));
1213 DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
1214 conn->dirpath,lp_dontdescend(SNUM(conn))));
1215 if (in_list(conn->dirpath, lp_dontdescend(SNUM(conn)),True))
1216 check_descend = True;
1218 for (i=numentries;(i<maxentries) && !finished;i++) {
1219 finished = !get_dir_entry(conn,mask,dirtype,fname,&size,&mode,&date,check_descend);
1220 if (!finished) {
1221 memcpy(p,status,21);
1222 make_dir_struct(p,mask,fname,size, mode,date,
1223 !allow_long_path_components);
1224 if (!dptr_fill(p+12,dptr_num)) {
1225 break;
1227 numentries++;
1228 p += DIR_STRUCT_SIZE;
1232 } /* if (ok ) */
1236 SearchEmpty:
1238 /* If we were called as SMBffirst with smb_search_id == NULL
1239 and no entries were found then return error and close dirptr
1240 (X/Open spec) */
1242 if (numentries == 0 || !ok) {
1243 dptr_close(&dptr_num);
1244 } else if(ok && expect_close && status_len == 0) {
1245 /* Close the dptr - we know it's gone */
1246 dptr_close(&dptr_num);
1249 /* If we were called as SMBfunique, then we can close the dirptr now ! */
1250 if(dptr_num >= 0 && CVAL(inbuf,smb_com) == SMBfunique) {
1251 dptr_close(&dptr_num);
1254 if ((numentries == 0) && !mask_contains_wcard) {
1255 return ERROR_BOTH(STATUS_NO_MORE_FILES,ERRDOS,ERRnofiles);
1258 SSVAL(outbuf,smb_vwv0,numentries);
1259 SSVAL(outbuf,smb_vwv1,3 + numentries * DIR_STRUCT_SIZE);
1260 SCVAL(smb_buf(outbuf),0,5);
1261 SSVAL(smb_buf(outbuf),1,numentries*DIR_STRUCT_SIZE);
1263 /* The replies here are never long name. */
1264 SSVAL(outbuf,smb_flg2,SVAL(outbuf, smb_flg2) & (~FLAGS2_IS_LONG_NAME));
1265 if (!allow_long_path_components) {
1266 SSVAL(outbuf,smb_flg2,SVAL(outbuf, smb_flg2) & (~FLAGS2_LONG_PATH_COMPONENTS));
1269 /* This SMB *always* returns ASCII names. Remove the unicode bit in flags2. */
1270 SSVAL(outbuf,smb_flg2, (SVAL(outbuf, smb_flg2) & (~FLAGS2_UNICODE_STRINGS)));
1272 outsize += DIR_STRUCT_SIZE*numentries;
1273 smb_setlen(outbuf,outsize - 4);
1275 if ((! *directory) && dptr_path(dptr_num))
1276 slprintf(directory, sizeof(directory)-1, "(%s)",dptr_path(dptr_num));
1278 DEBUG( 4, ( "%s mask=%s path=%s dtype=%d nument=%u of %u\n",
1279 smb_fn_name(CVAL(inbuf,smb_com)),
1280 mask, directory, dirtype, numentries, maxentries ) );
1282 END_PROFILE(SMBsearch);
1283 return(outsize);
1286 /****************************************************************************
1287 Reply to a fclose (stop directory search).
1288 ****************************************************************************/
1290 int reply_fclose(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1292 int outsize = 0;
1293 int status_len;
1294 pstring path;
1295 char status[21];
1296 int dptr_num= -2;
1297 char *p;
1298 NTSTATUS err;
1299 BOOL path_contains_wcard = False;
1301 START_PROFILE(SMBfclose);
1303 if (lp_posix_pathnames()) {
1304 END_PROFILE(SMBfclose);
1305 return reply_unknown(inbuf, outbuf);
1308 outsize = set_message(outbuf,1,0,True);
1309 p = smb_buf(inbuf) + 1;
1310 p += srvstr_get_path_wcard(inbuf, path, p, sizeof(path), 0, STR_TERMINATE, &err, &path_contains_wcard);
1311 if (!NT_STATUS_IS_OK(err)) {
1312 END_PROFILE(SMBfclose);
1313 return ERROR_NT(err);
1315 p++;
1316 status_len = SVAL(p,0);
1317 p += 2;
1319 if (status_len == 0) {
1320 END_PROFILE(SMBfclose);
1321 return ERROR_DOS(ERRSRV,ERRsrverror);
1324 memcpy(status,p,21);
1326 if(dptr_fetch(status+12,&dptr_num)) {
1327 /* Close the dptr - we know it's gone */
1328 dptr_close(&dptr_num);
1331 SSVAL(outbuf,smb_vwv0,0);
1333 DEBUG(3,("search close\n"));
1335 END_PROFILE(SMBfclose);
1336 return(outsize);
1339 /****************************************************************************
1340 Reply to an open.
1341 ****************************************************************************/
1343 int reply_open(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1345 pstring fname;
1346 int outsize = 0;
1347 uint32 fattr=0;
1348 SMB_OFF_T size = 0;
1349 time_t mtime=0;
1350 int info;
1351 SMB_STRUCT_STAT sbuf;
1352 BOOL bad_path = False;
1353 files_struct *fsp;
1354 int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
1355 int deny_mode;
1356 uint32 dos_attr = SVAL(inbuf,smb_vwv1);
1357 uint32 access_mask;
1358 uint32 share_mode;
1359 uint32 create_disposition;
1360 uint32 create_options = 0;
1361 NTSTATUS status;
1362 START_PROFILE(SMBopen);
1364 deny_mode = SVAL(inbuf,smb_vwv0);
1366 srvstr_get_path(inbuf, fname, smb_buf(inbuf)+1, sizeof(fname), 0, STR_TERMINATE, &status);
1367 if (!NT_STATUS_IS_OK(status)) {
1368 END_PROFILE(SMBopen);
1369 return ERROR_NT(status);
1372 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1374 unix_convert(fname,conn,0,&bad_path,&sbuf);
1375 if (bad_path) {
1376 END_PROFILE(SMBopen);
1377 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1380 if (!map_open_params_to_ntcreate(fname, deny_mode, OPENX_FILE_EXISTS_OPEN,
1381 &access_mask, &share_mode, &create_disposition, &create_options)) {
1382 END_PROFILE(SMBopen);
1383 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRbadaccess));
1386 status = open_file_ntcreate(conn,fname,&sbuf,
1387 access_mask,
1388 share_mode,
1389 create_disposition,
1390 create_options,
1391 dos_attr,
1392 oplock_request,
1393 &info, &fsp);
1395 if (!NT_STATUS_IS_OK(status)) {
1396 END_PROFILE(SMBopen);
1397 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1398 /* We have re-scheduled this call. */
1399 return -1;
1401 return ERROR_NT(status);
1404 size = sbuf.st_size;
1405 fattr = dos_mode(conn,fname,&sbuf);
1406 mtime = sbuf.st_mtime;
1408 if (fattr & aDIR) {
1409 DEBUG(3,("attempt to open a directory %s\n",fname));
1410 close_file(fsp,ERROR_CLOSE);
1411 END_PROFILE(SMBopen);
1412 return ERROR_DOS(ERRDOS,ERRnoaccess);
1415 outsize = set_message(outbuf,7,0,True);
1416 SSVAL(outbuf,smb_vwv0,fsp->fnum);
1417 SSVAL(outbuf,smb_vwv1,fattr);
1418 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1419 srv_put_dos_date3(outbuf,smb_vwv2,mtime & ~1);
1420 } else {
1421 srv_put_dos_date3(outbuf,smb_vwv2,mtime);
1423 SIVAL(outbuf,smb_vwv4,(uint32)size);
1424 SSVAL(outbuf,smb_vwv6,deny_mode);
1426 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1427 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1430 if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1431 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1433 END_PROFILE(SMBopen);
1434 return(outsize);
1437 /****************************************************************************
1438 Reply to an open and X.
1439 ****************************************************************************/
1441 int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
1443 pstring fname;
1444 uint16 open_flags = SVAL(inbuf,smb_vwv2);
1445 int deny_mode = SVAL(inbuf,smb_vwv3);
1446 uint32 smb_attr = SVAL(inbuf,smb_vwv5);
1447 /* Breakout the oplock request bits so we can set the
1448 reply bits separately. */
1449 int ex_oplock_request = EXTENDED_OPLOCK_REQUEST(inbuf);
1450 int core_oplock_request = CORE_OPLOCK_REQUEST(inbuf);
1451 int oplock_request = ex_oplock_request | core_oplock_request;
1452 #if 0
1453 int smb_sattr = SVAL(inbuf,smb_vwv4);
1454 uint32 smb_time = make_unix_date3(inbuf+smb_vwv6);
1455 #endif
1456 int smb_ofun = SVAL(inbuf,smb_vwv8);
1457 SMB_OFF_T size=0;
1458 uint32 fattr=0;
1459 int mtime=0;
1460 SMB_STRUCT_STAT sbuf;
1461 int smb_action = 0;
1462 BOOL bad_path = False;
1463 files_struct *fsp;
1464 NTSTATUS status;
1465 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv9);
1466 ssize_t retval = -1;
1467 uint32 access_mask;
1468 uint32 share_mode;
1469 uint32 create_disposition;
1470 uint32 create_options = 0;
1472 START_PROFILE(SMBopenX);
1474 /* If it's an IPC, pass off the pipe handler. */
1475 if (IS_IPC(conn)) {
1476 if (lp_nt_pipe_support()) {
1477 END_PROFILE(SMBopenX);
1478 return reply_open_pipe_and_X(conn, inbuf,outbuf,length,bufsize);
1479 } else {
1480 END_PROFILE(SMBopenX);
1481 return ERROR_DOS(ERRSRV,ERRaccess);
1485 /* XXXX we need to handle passed times, sattr and flags */
1486 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
1487 if (!NT_STATUS_IS_OK(status)) {
1488 END_PROFILE(SMBopenX);
1489 return ERROR_NT(status);
1492 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1494 unix_convert(fname,conn,0,&bad_path,&sbuf);
1495 if (bad_path) {
1496 END_PROFILE(SMBopenX);
1497 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1500 if (!map_open_params_to_ntcreate(fname, deny_mode, smb_ofun,
1501 &access_mask,
1502 &share_mode,
1503 &create_disposition,
1504 &create_options)) {
1505 END_PROFILE(SMBopenX);
1506 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRbadaccess));
1509 status = open_file_ntcreate(conn,fname,&sbuf,
1510 access_mask,
1511 share_mode,
1512 create_disposition,
1513 create_options,
1514 smb_attr,
1515 oplock_request,
1516 &smb_action, &fsp);
1518 if (!NT_STATUS_IS_OK(status)) {
1519 END_PROFILE(SMBopenX);
1520 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1521 /* We have re-scheduled this call. */
1522 return -1;
1524 return ERROR_NT(status);
1527 size = sbuf.st_size;
1529 /* Setting the "size" field in vwv9 and vwv10 causes the file to be set to this size,
1530 if the file is truncated or created. */
1531 if (((smb_action == FILE_WAS_CREATED) || (smb_action == FILE_WAS_OVERWRITTEN)) && allocation_size) {
1532 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1533 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1534 close_file(fsp,ERROR_CLOSE);
1535 END_PROFILE(SMBopenX);
1536 return ERROR_NT(NT_STATUS_DISK_FULL);
1538 retval = vfs_set_filelen(fsp, (SMB_OFF_T)allocation_size);
1539 if (retval < 0) {
1540 close_file(fsp,ERROR_CLOSE);
1541 END_PROFILE(SMBopenX);
1542 return ERROR_NT(NT_STATUS_DISK_FULL);
1544 size = get_allocation_size(conn,fsp,&sbuf);
1547 fattr = dos_mode(conn,fname,&sbuf);
1548 mtime = sbuf.st_mtime;
1549 if (fattr & aDIR) {
1550 close_file(fsp,ERROR_CLOSE);
1551 END_PROFILE(SMBopenX);
1552 return ERROR_DOS(ERRDOS,ERRnoaccess);
1555 /* If the caller set the extended oplock request bit
1556 and we granted one (by whatever means) - set the
1557 correct bit for extended oplock reply.
1560 if (ex_oplock_request && lp_fake_oplocks(SNUM(conn))) {
1561 smb_action |= EXTENDED_OPLOCK_GRANTED;
1564 if(ex_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1565 smb_action |= EXTENDED_OPLOCK_GRANTED;
1568 /* If the caller set the core oplock request bit
1569 and we granted one (by whatever means) - set the
1570 correct bit for core oplock reply.
1573 if (core_oplock_request && lp_fake_oplocks(SNUM(conn))) {
1574 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1577 if(core_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1578 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1581 if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
1582 set_message(outbuf,19,0,True);
1583 } else {
1584 set_message(outbuf,15,0,True);
1586 SSVAL(outbuf,smb_vwv2,fsp->fnum);
1587 SSVAL(outbuf,smb_vwv3,fattr);
1588 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1589 srv_put_dos_date3(outbuf,smb_vwv4,mtime & ~1);
1590 } else {
1591 srv_put_dos_date3(outbuf,smb_vwv4,mtime);
1593 SIVAL(outbuf,smb_vwv6,(uint32)size);
1594 SSVAL(outbuf,smb_vwv8,GET_OPENX_MODE(deny_mode));
1595 SSVAL(outbuf,smb_vwv11,smb_action);
1597 if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
1598 SIVAL(outbuf, smb_vwv15, STD_RIGHT_ALL_ACCESS);
1601 END_PROFILE(SMBopenX);
1602 return chain_reply(inbuf,outbuf,length,bufsize);
1605 /****************************************************************************
1606 Reply to a SMBulogoffX.
1607 conn POINTER CAN BE NULL HERE !
1608 ****************************************************************************/
1610 int reply_ulogoffX(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
1612 uint16 vuid = SVAL(inbuf,smb_uid);
1613 user_struct *vuser = get_valid_user_struct(vuid);
1614 START_PROFILE(SMBulogoffX);
1616 if(vuser == 0)
1617 DEBUG(3,("ulogoff, vuser id %d does not map to user.\n", vuid));
1619 /* in user level security we are supposed to close any files
1620 open by this user */
1621 if ((vuser != 0) && (lp_security() != SEC_SHARE))
1622 file_close_user(vuid);
1624 invalidate_vuid(vuid);
1626 set_message(outbuf,2,0,True);
1628 DEBUG( 3, ( "ulogoffX vuid=%d\n", vuid ) );
1630 END_PROFILE(SMBulogoffX);
1631 return chain_reply(inbuf,outbuf,length,bufsize);
1634 /****************************************************************************
1635 Reply to a mknew or a create.
1636 ****************************************************************************/
1638 int reply_mknew(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1640 pstring fname;
1641 int com;
1642 int outsize = 0;
1643 uint32 fattr = SVAL(inbuf,smb_vwv0);
1644 BOOL bad_path = False;
1645 files_struct *fsp;
1646 int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
1647 SMB_STRUCT_STAT sbuf;
1648 NTSTATUS status;
1649 uint32 access_mask = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
1650 uint32 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1651 uint32 create_disposition;
1652 uint32 create_options = 0;
1654 START_PROFILE(SMBcreate);
1656 com = SVAL(inbuf,smb_com);
1658 srvstr_get_path(inbuf, fname, smb_buf(inbuf) + 1, sizeof(fname), 0, STR_TERMINATE, &status);
1659 if (!NT_STATUS_IS_OK(status)) {
1660 END_PROFILE(SMBcreate);
1661 return ERROR_NT(status);
1664 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1666 unix_convert(fname,conn,0,&bad_path,&sbuf);
1667 if (bad_path) {
1668 END_PROFILE(SMBcreate);
1669 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1672 if (fattr & aVOLID) {
1673 DEBUG(0,("Attempt to create file (%s) with volid set - please report this\n",fname));
1676 if(com == SMBmknew) {
1677 /* We should fail if file exists. */
1678 create_disposition = FILE_CREATE;
1679 } else {
1680 /* Create if file doesn't exist, truncate if it does. */
1681 create_disposition = FILE_OVERWRITE_IF;
1684 /* Open file using ntcreate. */
1685 status = open_file_ntcreate(conn,fname,&sbuf,
1686 access_mask,
1687 share_mode,
1688 create_disposition,
1689 create_options,
1690 fattr,
1691 oplock_request,
1692 NULL, &fsp);
1694 if (!NT_STATUS_IS_OK(status)) {
1695 END_PROFILE(SMBcreate);
1696 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1697 /* We have re-scheduled this call. */
1698 return -1;
1700 return ERROR_NT(status);
1703 outsize = set_message(outbuf,1,0,True);
1704 SSVAL(outbuf,smb_vwv0,fsp->fnum);
1706 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1707 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1710 if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1711 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1714 DEBUG( 2, ( "reply_mknew: file %s\n", fname ) );
1715 DEBUG( 3, ( "reply_mknew %s fd=%d dmode=0x%x\n", fname, fsp->fh->fd, (unsigned int)fattr ) );
1717 END_PROFILE(SMBcreate);
1718 return(outsize);
1721 /****************************************************************************
1722 Reply to a create temporary file.
1723 ****************************************************************************/
1725 int reply_ctemp(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1727 pstring fname;
1728 int outsize = 0;
1729 uint32 fattr = SVAL(inbuf,smb_vwv0);
1730 BOOL bad_path = False;
1731 files_struct *fsp;
1732 int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
1733 int tmpfd;
1734 SMB_STRUCT_STAT sbuf;
1735 char *p, *s;
1736 NTSTATUS status;
1737 unsigned int namelen;
1739 START_PROFILE(SMBctemp);
1741 srvstr_get_path(inbuf, fname, smb_buf(inbuf)+1, sizeof(fname), 0, STR_TERMINATE, &status);
1742 if (!NT_STATUS_IS_OK(status)) {
1743 END_PROFILE(SMBctemp);
1744 return ERROR_NT(status);
1746 if (*fname) {
1747 pstrcat(fname,"/TMXXXXXX");
1748 } else {
1749 pstrcat(fname,"TMXXXXXX");
1752 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1754 unix_convert(fname,conn,0,&bad_path,&sbuf);
1755 if (bad_path) {
1756 END_PROFILE(SMBctemp);
1757 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1760 tmpfd = smb_mkstemp(fname);
1761 if (tmpfd == -1) {
1762 END_PROFILE(SMBctemp);
1763 return(UNIXERROR(ERRDOS,ERRnoaccess));
1766 SMB_VFS_STAT(conn,fname,&sbuf);
1768 /* We should fail if file does not exist. */
1769 status = open_file_ntcreate(conn,fname,&sbuf,
1770 FILE_GENERIC_READ | FILE_GENERIC_WRITE,
1771 FILE_SHARE_READ|FILE_SHARE_WRITE,
1772 FILE_OPEN,
1774 fattr,
1775 oplock_request,
1776 NULL, &fsp);
1778 /* close fd from smb_mkstemp() */
1779 close(tmpfd);
1781 if (!NT_STATUS_IS_OK(status)) {
1782 END_PROFILE(SMBctemp);
1783 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1784 /* We have re-scheduled this call. */
1785 return -1;
1787 return ERROR_NT(status);
1790 outsize = set_message(outbuf,1,0,True);
1791 SSVAL(outbuf,smb_vwv0,fsp->fnum);
1793 /* the returned filename is relative to the directory */
1794 s = strrchr_m(fname, '/');
1795 if (!s) {
1796 s = fname;
1797 } else {
1798 s++;
1801 p = smb_buf(outbuf);
1802 #if 0
1803 /* Tested vs W2K3 - this doesn't seem to be here - null terminated filename is the only
1804 thing in the byte section. JRA */
1805 SSVALS(p, 0, -1); /* what is this? not in spec */
1806 #endif
1807 namelen = srvstr_push(outbuf, p, s, -1, STR_ASCII|STR_TERMINATE);
1808 p += namelen;
1809 outsize = set_message_end(outbuf, p);
1811 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1812 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1815 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1816 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1819 DEBUG( 2, ( "reply_ctemp: created temp file %s\n", fname ) );
1820 DEBUG( 3, ( "reply_ctemp %s fd=%d umode=0%o\n", fname, fsp->fh->fd,
1821 (unsigned int)sbuf.st_mode ) );
1823 END_PROFILE(SMBctemp);
1824 return(outsize);
1827 /*******************************************************************
1828 Check if a user is allowed to rename a file.
1829 ********************************************************************/
1831 static NTSTATUS can_rename(connection_struct *conn, char *fname, uint16 dirtype, SMB_STRUCT_STAT *pst)
1833 files_struct *fsp;
1834 uint32 fmode;
1835 NTSTATUS status;
1837 if (!CAN_WRITE(conn)) {
1838 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1841 fmode = dos_mode(conn,fname,pst);
1842 if ((fmode & ~dirtype) & (aHIDDEN | aSYSTEM)) {
1843 return NT_STATUS_NO_SUCH_FILE;
1846 if (S_ISDIR(pst->st_mode)) {
1847 return NT_STATUS_OK;
1850 status = open_file_ntcreate(conn, fname, pst,
1851 DELETE_ACCESS,
1852 FILE_SHARE_READ|FILE_SHARE_WRITE,
1853 FILE_OPEN,
1855 FILE_ATTRIBUTE_NORMAL,
1857 NULL, &fsp);
1859 if (!NT_STATUS_IS_OK(status)) {
1860 return NT_STATUS_ACCESS_DENIED;
1862 close_file(fsp,NORMAL_CLOSE);
1863 return NT_STATUS_OK;
1866 /*******************************************************************
1867 Check if a user is allowed to delete a file.
1868 ********************************************************************/
1870 NTSTATUS can_delete(connection_struct *conn, char *fname, uint32 dirtype, BOOL bad_path, BOOL check_is_at_open)
1872 SMB_STRUCT_STAT sbuf;
1873 uint32 fattr;
1874 files_struct *fsp;
1875 NTSTATUS status;
1877 DEBUG(10,("can_delete: %s, dirtype = %d\n", fname, dirtype ));
1879 if (!CAN_WRITE(conn)) {
1880 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1883 if (SMB_VFS_LSTAT(conn,fname,&sbuf) != 0) {
1884 if(errno == ENOENT) {
1885 if (bad_path) {
1886 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1887 } else {
1888 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1891 return map_nt_error_from_unix(errno);
1894 fattr = dos_mode(conn,fname,&sbuf);
1896 /* Can't delete a directory. */
1897 if (fattr & aDIR) {
1898 return NT_STATUS_FILE_IS_A_DIRECTORY;
1901 #if 0 /* JRATEST */
1902 else if (dirtype & aDIR) /* Asked for a directory and it isn't. */
1903 return NT_STATUS_OBJECT_NAME_INVALID;
1904 #endif /* JRATEST */
1906 /* Fix for bug #3035 from SATOH Fumiyasu <fumiyas@miraclelinux.com>
1908 On a Windows share, a file with read-only dosmode can be opened with
1909 DELETE_ACCESS. But on a Samba share (delete readonly = no), it
1910 fails with NT_STATUS_CANNOT_DELETE error.
1912 This semantic causes a problem that a user can not
1913 rename a file with read-only dosmode on a Samba share
1914 from a Windows command prompt (i.e. cmd.exe, but can rename
1915 from Windows Explorer).
1918 if (!check_is_at_open && !lp_delete_readonly(SNUM(conn))) {
1919 if (fattr & aRONLY) {
1920 return NT_STATUS_CANNOT_DELETE;
1923 if ((fattr & ~dirtype) & (aHIDDEN | aSYSTEM)) {
1924 return NT_STATUS_NO_SUCH_FILE;
1927 if (check_is_at_open) {
1928 if (!can_delete_file_in_directory(conn, fname)) {
1929 return NT_STATUS_ACCESS_DENIED;
1931 } else {
1932 /* On open checks the open itself will check the share mode, so
1933 don't do it here as we'll get it wrong. */
1935 status = open_file_ntcreate(conn, fname, &sbuf,
1936 DELETE_ACCESS,
1937 FILE_SHARE_NONE,
1938 FILE_OPEN,
1940 FILE_ATTRIBUTE_NORMAL,
1942 NULL, &fsp);
1944 if (!NT_STATUS_IS_OK(status)) {
1945 return status;
1947 close_file(fsp,NORMAL_CLOSE);
1949 return NT_STATUS_OK;
1952 /****************************************************************************
1953 The guts of the unlink command, split out so it may be called by the NT SMB
1954 code.
1955 ****************************************************************************/
1957 NTSTATUS unlink_internals(connection_struct *conn, uint32 dirtype, char *name, BOOL has_wild)
1959 pstring directory;
1960 pstring mask;
1961 char *p;
1962 int count=0;
1963 NTSTATUS error = NT_STATUS_OK;
1964 BOOL bad_path = False;
1965 BOOL rc = True;
1966 SMB_STRUCT_STAT sbuf;
1968 *directory = *mask = 0;
1970 rc = unix_convert(name,conn,0,&bad_path,&sbuf);
1972 p = strrchr_m(name,'/');
1973 if (!p) {
1974 pstrcpy(directory,".");
1975 pstrcpy(mask,name);
1976 } else {
1977 *p = 0;
1978 pstrcpy(directory,name);
1979 pstrcpy(mask,p+1);
1983 * We should only check the mangled cache
1984 * here if unix_convert failed. This means
1985 * that the path in 'mask' doesn't exist
1986 * on the file system and so we need to look
1987 * for a possible mangle. This patch from
1988 * Tine Smukavec <valentin.smukavec@hermes.si>.
1991 if (!rc && mangle_is_mangled(mask,conn->params))
1992 mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
1994 if (!has_wild) {
1995 pstrcat(directory,"/");
1996 pstrcat(directory,mask);
1997 error = can_delete(conn,directory,dirtype,bad_path,False);
1998 if (!NT_STATUS_IS_OK(error))
1999 return error;
2001 if (SMB_VFS_UNLINK(conn,directory) == 0) {
2002 count++;
2004 } else {
2005 struct smb_Dir *dir_hnd = NULL;
2006 const char *dname;
2008 if (strequal(mask,"????????.???"))
2009 pstrcpy(mask,"*");
2011 if (check_name(directory,conn))
2012 dir_hnd = OpenDir(conn, directory, mask, dirtype);
2014 /* XXXX the CIFS spec says that if bit0 of the flags2 field is set then
2015 the pattern matches against the long name, otherwise the short name
2016 We don't implement this yet XXXX
2019 if (dir_hnd) {
2020 long offset = 0;
2021 error = NT_STATUS_NO_SUCH_FILE;
2023 while ((dname = ReadDirName(dir_hnd, &offset))) {
2024 SMB_STRUCT_STAT st;
2025 pstring fname;
2026 BOOL sys_direntry = False;
2027 pstrcpy(fname,dname);
2029 if (!is_visible_file(conn, directory, dname, &st, True)) {
2030 continue;
2033 /* Quick check for "." and ".." */
2034 if (fname[0] == '.') {
2035 if (!fname[1] || (fname[1] == '.' && !fname[2])) {
2036 if ((dirtype & FILE_ATTRIBUTE_DIRECTORY) && (dirtype & FILE_ATTRIBUTE_SYSTEM)) {
2037 sys_direntry = True;
2038 } else {
2039 continue;
2044 if(!mask_match(fname, mask, conn->case_sensitive))
2045 continue;
2047 if (sys_direntry) {
2048 error = NT_STATUS_OBJECT_NAME_INVALID;
2049 DEBUG(3,("unlink_internals: system directory delete denied [%s] mask [%s]\n",
2050 fname, mask));
2051 break;
2054 slprintf(fname,sizeof(fname)-1, "%s/%s",directory,dname);
2055 error = can_delete(conn,fname,dirtype,bad_path,False);
2056 if (!NT_STATUS_IS_OK(error)) {
2057 continue;
2059 if (SMB_VFS_UNLINK(conn,fname) == 0)
2060 count++;
2061 DEBUG(3,("unlink_internals: succesful unlink [%s]\n",fname));
2063 CloseDir(dir_hnd);
2067 if (count == 0 && NT_STATUS_IS_OK(error)) {
2068 error = map_nt_error_from_unix(errno);
2071 return error;
2074 /****************************************************************************
2075 Reply to a unlink
2076 ****************************************************************************/
2078 int reply_unlink(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
2079 int dum_buffsize)
2081 int outsize = 0;
2082 pstring name;
2083 uint32 dirtype;
2084 NTSTATUS status;
2085 BOOL path_contains_wcard = False;
2087 START_PROFILE(SMBunlink);
2089 dirtype = SVAL(inbuf,smb_vwv0);
2091 srvstr_get_path_wcard(inbuf, name, smb_buf(inbuf) + 1, sizeof(name), 0, STR_TERMINATE, &status, &path_contains_wcard);
2092 if (!NT_STATUS_IS_OK(status)) {
2093 END_PROFILE(SMBunlink);
2094 return ERROR_NT(status);
2097 RESOLVE_DFSPATH_WCARD(name, conn, inbuf, outbuf);
2099 DEBUG(3,("reply_unlink : %s\n",name));
2101 status = unlink_internals(conn, dirtype, name, path_contains_wcard);
2102 if (!NT_STATUS_IS_OK(status)) {
2103 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
2104 /* We have re-scheduled this call. */
2105 return -1;
2107 return ERROR_NT(status);
2111 * Win2k needs a changenotify request response before it will
2112 * update after a rename..
2114 process_pending_change_notify_queue((time_t)0);
2116 outsize = set_message(outbuf,0,0,False);
2118 END_PROFILE(SMBunlink);
2119 return outsize;
2122 /****************************************************************************
2123 Fail for readbraw.
2124 ****************************************************************************/
2126 static void fail_readraw(void)
2128 pstring errstr;
2129 slprintf(errstr, sizeof(errstr)-1, "FAIL ! reply_readbraw: socket write fail (%s)",
2130 strerror(errno) );
2131 exit_server(errstr);
2134 #if defined(WITH_SENDFILE)
2135 /****************************************************************************
2136 Fake (read/write) sendfile. Returns -1 on read or write fail.
2137 ****************************************************************************/
2139 static ssize_t fake_sendfile(files_struct *fsp, SMB_OFF_T startpos, size_t nread, char *buf, int bufsize)
2141 ssize_t ret=0;
2143 /* Paranioa check... */
2144 if (nread > bufsize) {
2145 fail_readraw();
2148 if (nread > 0) {
2149 ret = read_file(fsp,buf,startpos,nread);
2150 if (ret == -1) {
2151 return -1;
2155 /* If we had a short read, fill with zeros. */
2156 if (ret < nread) {
2157 memset(buf, '\0', nread - ret);
2160 if (write_data(smbd_server_fd(),buf,nread) != nread) {
2161 return -1;
2164 return (ssize_t)nread;
2166 #endif
2168 /****************************************************************************
2169 Use sendfile in readbraw.
2170 ****************************************************************************/
2172 void send_file_readbraw(connection_struct *conn, files_struct *fsp, SMB_OFF_T startpos, size_t nread,
2173 ssize_t mincount, char *outbuf, int out_buffsize)
2175 ssize_t ret=0;
2177 #if defined(WITH_SENDFILE)
2179 * We can only use sendfile on a non-chained packet
2180 * but we can use on a non-oplocked file. tridge proved this
2181 * on a train in Germany :-). JRA.
2182 * reply_readbraw has already checked the length.
2185 if ( (chain_size == 0) && (nread > 0) &&
2186 (fsp->wcp == NULL) && lp_use_sendfile(SNUM(conn)) ) {
2187 DATA_BLOB header;
2189 _smb_setlen(outbuf,nread);
2190 header.data = (uint8 *)outbuf;
2191 header.length = 4;
2192 header.free = NULL;
2194 if ( SMB_VFS_SENDFILE( smbd_server_fd(), fsp, fsp->fh->fd, &header, startpos, nread) == -1) {
2195 /* Returning ENOSYS means no data at all was sent. Do this as a normal read. */
2196 if (errno == ENOSYS) {
2197 goto normal_readbraw;
2201 * Special hack for broken Linux with no working sendfile. If we
2202 * return EINTR we sent the header but not the rest of the data.
2203 * Fake this up by doing read/write calls.
2205 if (errno == EINTR) {
2206 /* Ensure we don't do this again. */
2207 set_use_sendfile(SNUM(conn), False);
2208 DEBUG(0,("send_file_readbraw: sendfile not available. Faking..\n"));
2210 if (fake_sendfile(fsp, startpos, nread, outbuf + 4, out_buffsize - 4) == -1) {
2211 DEBUG(0,("send_file_readbraw: fake_sendfile failed for file %s (%s).\n",
2212 fsp->fsp_name, strerror(errno) ));
2213 exit_server("send_file_readbraw fake_sendfile failed");
2215 return;
2218 DEBUG(0,("send_file_readbraw: sendfile failed for file %s (%s). Terminating\n",
2219 fsp->fsp_name, strerror(errno) ));
2220 exit_server("send_file_readbraw sendfile failed");
2225 normal_readbraw:
2227 #endif
2229 if (nread > 0) {
2230 ret = read_file(fsp,outbuf+4,startpos,nread);
2231 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2232 if (ret < mincount)
2233 ret = 0;
2234 #else
2235 if (ret < nread)
2236 ret = 0;
2237 #endif
2240 _smb_setlen(outbuf,ret);
2241 if (write_data(smbd_server_fd(),outbuf,4+ret) != 4+ret)
2242 fail_readraw();
2245 /****************************************************************************
2246 Reply to a readbraw (core+ protocol).
2247 ****************************************************************************/
2249 int reply_readbraw(connection_struct *conn, char *inbuf, char *outbuf, int dum_size, int out_buffsize)
2251 ssize_t maxcount,mincount;
2252 size_t nread = 0;
2253 SMB_OFF_T startpos;
2254 char *header = outbuf;
2255 files_struct *fsp;
2256 START_PROFILE(SMBreadbraw);
2258 if (srv_is_signing_active()) {
2259 exit_server("reply_readbraw: SMB signing is active - raw reads/writes are disallowed.");
2263 * Special check if an oplock break has been issued
2264 * and the readraw request croses on the wire, we must
2265 * return a zero length response here.
2268 fsp = file_fsp(inbuf,smb_vwv0);
2270 if (!FNUM_OK(fsp,conn) || !fsp->can_read) {
2272 * fsp could be NULL here so use the value from the packet. JRA.
2274 DEBUG(3,("fnum %d not open in readbraw - cache prime?\n",(int)SVAL(inbuf,smb_vwv0)));
2275 _smb_setlen(header,0);
2276 if (write_data(smbd_server_fd(),header,4) != 4)
2277 fail_readraw();
2278 END_PROFILE(SMBreadbraw);
2279 return(-1);
2282 CHECK_FSP(fsp,conn);
2284 flush_write_cache(fsp, READRAW_FLUSH);
2286 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
2287 if(CVAL(inbuf,smb_wct) == 10) {
2289 * This is a large offset (64 bit) read.
2291 #ifdef LARGE_SMB_OFF_T
2293 startpos |= (((SMB_OFF_T)IVAL(inbuf,smb_vwv8)) << 32);
2295 #else /* !LARGE_SMB_OFF_T */
2298 * Ensure we haven't been sent a >32 bit offset.
2301 if(IVAL(inbuf,smb_vwv8) != 0) {
2302 DEBUG(0,("readbraw - large offset (%x << 32) used and we don't support \
2303 64 bit offsets.\n", (unsigned int)IVAL(inbuf,smb_vwv8) ));
2304 _smb_setlen(header,0);
2305 if (write_data(smbd_server_fd(),header,4) != 4)
2306 fail_readraw();
2307 END_PROFILE(SMBreadbraw);
2308 return(-1);
2311 #endif /* LARGE_SMB_OFF_T */
2313 if(startpos < 0) {
2314 DEBUG(0,("readbraw - negative 64 bit readraw offset (%.0f) !\n", (double)startpos ));
2315 _smb_setlen(header,0);
2316 if (write_data(smbd_server_fd(),header,4) != 4)
2317 fail_readraw();
2318 END_PROFILE(SMBreadbraw);
2319 return(-1);
2322 maxcount = (SVAL(inbuf,smb_vwv3) & 0xFFFF);
2323 mincount = (SVAL(inbuf,smb_vwv4) & 0xFFFF);
2325 /* ensure we don't overrun the packet size */
2326 maxcount = MIN(65535,maxcount);
2328 if (!is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
2329 SMB_STRUCT_STAT st;
2330 SMB_OFF_T size = 0;
2332 if (SMB_VFS_FSTAT(fsp,fsp->fh->fd,&st) == 0) {
2333 size = st.st_size;
2336 if (startpos >= size) {
2337 nread = 0;
2338 } else {
2339 nread = MIN(maxcount,(size - startpos));
2343 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2344 if (nread < mincount)
2345 nread = 0;
2346 #endif
2348 DEBUG( 3, ( "readbraw fnum=%d start=%.0f max=%lu min=%lu nread=%lu\n", fsp->fnum, (double)startpos,
2349 (unsigned long)maxcount, (unsigned long)mincount, (unsigned long)nread ) );
2351 send_file_readbraw(conn, fsp, startpos, nread, mincount, outbuf, out_buffsize);
2353 DEBUG(5,("readbraw finished\n"));
2354 END_PROFILE(SMBreadbraw);
2355 return -1;
2358 #undef DBGC_CLASS
2359 #define DBGC_CLASS DBGC_LOCKING
2361 /****************************************************************************
2362 Reply to a lockread (core+ protocol).
2363 ****************************************************************************/
2365 int reply_lockread(connection_struct *conn, char *inbuf,char *outbuf, int length, int dum_buffsiz)
2367 ssize_t nread = -1;
2368 char *data;
2369 int outsize = 0;
2370 SMB_OFF_T startpos;
2371 size_t numtoread;
2372 NTSTATUS status;
2373 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2374 START_PROFILE(SMBlockread);
2376 CHECK_FSP(fsp,conn);
2377 if (!CHECK_READ(fsp,inbuf)) {
2378 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2381 release_level_2_oplocks_on_change(fsp);
2383 numtoread = SVAL(inbuf,smb_vwv1);
2384 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2386 outsize = set_message(outbuf,5,3,True);
2387 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
2388 data = smb_buf(outbuf) + 3;
2391 * NB. Discovered by Menny Hamburger at Mainsoft. This is a core+
2392 * protocol request that predates the read/write lock concept.
2393 * Thus instead of asking for a read lock here we need to ask
2394 * for a write lock. JRA.
2395 * Note that the requested lock size is unaffected by max_recv.
2398 status = do_lock(fsp,
2399 (uint32)SVAL(inbuf,smb_pid),
2400 (SMB_BIG_UINT)numtoread,
2401 (SMB_BIG_UINT)startpos,
2402 WRITE_LOCK,
2403 WINDOWS_LOCK,
2404 0 /* zero timeout. */);
2406 if (NT_STATUS_V(status)) {
2407 #if 0
2409 * We used to make lockread a blocking lock. It turns out
2410 * that this isn't on W2k. Found by the Samba 4 RAW-READ torture
2411 * tester. JRA.
2414 if (lp_blocking_locks(SNUM(conn)) && ERROR_WAS_LOCK_DENIED(status)) {
2416 * A blocking lock was requested. Package up
2417 * this smb into a queued request and push it
2418 * onto the blocking lock queue.
2420 if(push_blocking_lock_request(inbuf, length,
2421 fsp,
2424 SVAL(inbuf,smb_pid),
2425 WRITE_LOCK,
2426 WINDOWS_LOCK,
2427 (SMB_BIG_UINT)startpos,
2428 (SMB_BIG_UINT)numtoread)) {
2429 END_PROFILE(SMBlockread);
2430 return -1;
2433 #endif
2434 END_PROFILE(SMBlockread);
2435 return ERROR_NT(status);
2439 * However the requested READ size IS affected by max_recv. Insanity.... JRA.
2442 if (numtoread > max_recv) {
2443 DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u). \
2444 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
2445 (unsigned int)numtoread, (unsigned int)max_recv ));
2446 numtoread = MIN(numtoread,max_recv);
2448 nread = read_file(fsp,data,startpos,numtoread);
2450 if (nread < 0) {
2451 END_PROFILE(SMBlockread);
2452 return(UNIXERROR(ERRDOS,ERRnoaccess));
2455 outsize += nread;
2456 SSVAL(outbuf,smb_vwv0,nread);
2457 SSVAL(outbuf,smb_vwv5,nread+3);
2458 SSVAL(smb_buf(outbuf),1,nread);
2460 DEBUG(3,("lockread fnum=%d num=%d nread=%d\n",
2461 fsp->fnum, (int)numtoread, (int)nread));
2463 END_PROFILE(SMBlockread);
2464 return(outsize);
2467 #undef DBGC_CLASS
2468 #define DBGC_CLASS DBGC_ALL
2470 /****************************************************************************
2471 Reply to a read.
2472 ****************************************************************************/
2474 int reply_read(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
2476 size_t numtoread;
2477 ssize_t nread = 0;
2478 char *data;
2479 SMB_OFF_T startpos;
2480 int outsize = 0;
2481 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2482 START_PROFILE(SMBread);
2484 CHECK_FSP(fsp,conn);
2485 if (!CHECK_READ(fsp,inbuf)) {
2486 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2489 numtoread = SVAL(inbuf,smb_vwv1);
2490 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2492 outsize = set_message(outbuf,5,3,True);
2493 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
2495 * The requested read size cannot be greater than max_recv. JRA.
2497 if (numtoread > max_recv) {
2498 DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u). \
2499 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
2500 (unsigned int)numtoread, (unsigned int)max_recv ));
2501 numtoread = MIN(numtoread,max_recv);
2504 data = smb_buf(outbuf) + 3;
2506 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtoread,(SMB_BIG_UINT)startpos, READ_LOCK)) {
2507 END_PROFILE(SMBread);
2508 return ERROR_DOS(ERRDOS,ERRlock);
2511 if (numtoread > 0)
2512 nread = read_file(fsp,data,startpos,numtoread);
2514 if (nread < 0) {
2515 END_PROFILE(SMBread);
2516 return(UNIXERROR(ERRDOS,ERRnoaccess));
2519 outsize += nread;
2520 SSVAL(outbuf,smb_vwv0,nread);
2521 SSVAL(outbuf,smb_vwv5,nread+3);
2522 SCVAL(smb_buf(outbuf),0,1);
2523 SSVAL(smb_buf(outbuf),1,nread);
2525 DEBUG( 3, ( "read fnum=%d num=%d nread=%d\n",
2526 fsp->fnum, (int)numtoread, (int)nread ) );
2528 END_PROFILE(SMBread);
2529 return(outsize);
2532 /****************************************************************************
2533 Reply to a read and X - possibly using sendfile.
2534 ****************************************************************************/
2536 int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length, int len_outbuf,
2537 files_struct *fsp, SMB_OFF_T startpos, size_t smb_maxcnt)
2539 int outsize = 0;
2540 ssize_t nread = -1;
2541 char *data = smb_buf(outbuf);
2543 #if defined(WITH_SENDFILE)
2545 * We can only use sendfile on a non-chained packet
2546 * but we can use on a non-oplocked file. tridge proved this
2547 * on a train in Germany :-). JRA.
2550 if ((chain_size == 0) && (CVAL(inbuf,smb_vwv0) == 0xFF) &&
2551 lp_use_sendfile(SNUM(conn)) && (fsp->wcp == NULL) ) {
2552 SMB_STRUCT_STAT sbuf;
2553 DATA_BLOB header;
2555 if(SMB_VFS_FSTAT(fsp,fsp->fh->fd, &sbuf) == -1)
2556 return(UNIXERROR(ERRDOS,ERRnoaccess));
2558 if (startpos > sbuf.st_size)
2559 goto normal_read;
2561 if (smb_maxcnt > (sbuf.st_size - startpos))
2562 smb_maxcnt = (sbuf.st_size - startpos);
2564 if (smb_maxcnt == 0)
2565 goto normal_read;
2568 * Set up the packet header before send. We
2569 * assume here the sendfile will work (get the
2570 * correct amount of data).
2573 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be -1. */
2574 SSVAL(outbuf,smb_vwv5,smb_maxcnt);
2575 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
2576 SSVAL(outbuf,smb_vwv7,((smb_maxcnt >> 16) & 1));
2577 SSVAL(smb_buf(outbuf),-2,smb_maxcnt);
2578 SCVAL(outbuf,smb_vwv0,0xFF);
2579 set_message(outbuf,12,smb_maxcnt,False);
2580 header.data = (uint8 *)outbuf;
2581 header.length = data - outbuf;
2582 header.free = NULL;
2584 if ((nread = SMB_VFS_SENDFILE( smbd_server_fd(), fsp, fsp->fh->fd, &header, startpos, smb_maxcnt)) == -1) {
2585 /* Returning ENOSYS means no data at all was sent. Do this as a normal read. */
2586 if (errno == ENOSYS) {
2587 goto normal_read;
2591 * Special hack for broken Linux with no working sendfile. If we
2592 * return EINTR we sent the header but not the rest of the data.
2593 * Fake this up by doing read/write calls.
2596 if (errno == EINTR) {
2597 /* Ensure we don't do this again. */
2598 set_use_sendfile(SNUM(conn), False);
2599 DEBUG(0,("send_file_readX: sendfile not available. Faking..\n"));
2601 if ((nread = fake_sendfile(fsp, startpos, smb_maxcnt, data,
2602 len_outbuf - (data-outbuf))) == -1) {
2603 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
2604 fsp->fsp_name, strerror(errno) ));
2605 exit_server("send_file_readX: fake_sendfile failed");
2607 DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
2608 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
2609 /* Returning -1 here means successful sendfile. */
2610 return -1;
2613 DEBUG(0,("send_file_readX: sendfile failed for file %s (%s). Terminating\n",
2614 fsp->fsp_name, strerror(errno) ));
2615 exit_server("send_file_readX sendfile failed");
2618 DEBUG( 3, ( "send_file_readX: sendfile fnum=%d max=%d nread=%d\n",
2619 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
2620 /* Returning -1 here means successful sendfile. */
2621 return -1;
2624 normal_read:
2626 #endif
2628 nread = read_file(fsp,data,startpos,smb_maxcnt);
2630 if (nread < 0) {
2631 return(UNIXERROR(ERRDOS,ERRnoaccess));
2634 outsize = set_message(outbuf,12,nread,False);
2635 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be -1. */
2636 SSVAL(outbuf,smb_vwv5,nread);
2637 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
2638 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
2639 SSVAL(smb_buf(outbuf),-2,nread);
2641 DEBUG( 3, ( "send_file_readX fnum=%d max=%d nread=%d\n",
2642 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
2644 /* Returning the number of bytes we want to send back - including header. */
2645 return outsize;
2648 /****************************************************************************
2649 Reply to a read and X.
2650 ****************************************************************************/
2652 int reply_read_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
2654 files_struct *fsp = file_fsp(inbuf,smb_vwv2);
2655 SMB_OFF_T startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
2656 ssize_t nread = -1;
2657 size_t smb_maxcnt = SVAL(inbuf,smb_vwv5);
2658 #if 0
2659 size_t smb_mincnt = SVAL(inbuf,smb_vwv6);
2660 #endif
2662 START_PROFILE(SMBreadX);
2664 /* If it's an IPC, pass off the pipe handler. */
2665 if (IS_IPC(conn)) {
2666 END_PROFILE(SMBreadX);
2667 return reply_pipe_read_and_X(inbuf,outbuf,length,bufsize);
2670 CHECK_FSP(fsp,conn);
2671 if (!CHECK_READ(fsp,inbuf)) {
2672 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2675 set_message(outbuf,12,0,True);
2677 if (global_client_caps & CAP_LARGE_READX) {
2678 if (SVAL(inbuf,smb_vwv7) == 1) {
2679 smb_maxcnt |= (1<<16);
2681 if (smb_maxcnt > BUFFER_SIZE) {
2682 DEBUG(0,("reply_read_and_X - read too large (%u) for reply buffer %u\n",
2683 (unsigned int)smb_maxcnt, (unsigned int)BUFFER_SIZE));
2684 END_PROFILE(SMBreadX);
2685 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2689 if(CVAL(inbuf,smb_wct) == 12) {
2690 #ifdef LARGE_SMB_OFF_T
2692 * This is a large offset (64 bit) read.
2694 startpos |= (((SMB_OFF_T)IVAL(inbuf,smb_vwv10)) << 32);
2696 #else /* !LARGE_SMB_OFF_T */
2699 * Ensure we haven't been sent a >32 bit offset.
2702 if(IVAL(inbuf,smb_vwv10) != 0) {
2703 DEBUG(0,("reply_read_and_X - large offset (%x << 32) used and we don't support \
2704 64 bit offsets.\n", (unsigned int)IVAL(inbuf,smb_vwv10) ));
2705 END_PROFILE(SMBreadX);
2706 return ERROR_DOS(ERRDOS,ERRbadaccess);
2709 #endif /* LARGE_SMB_OFF_T */
2713 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)smb_maxcnt,(SMB_BIG_UINT)startpos, READ_LOCK)) {
2714 END_PROFILE(SMBreadX);
2715 return ERROR_DOS(ERRDOS,ERRlock);
2718 if (schedule_aio_read_and_X(conn, inbuf, outbuf, length, bufsize, fsp, startpos, smb_maxcnt)) {
2719 END_PROFILE(SMBreadX);
2720 return -1;
2723 nread = send_file_readX(conn, inbuf, outbuf, length, bufsize, fsp, startpos, smb_maxcnt);
2724 if (nread != -1)
2725 nread = chain_reply(inbuf,outbuf,length,bufsize);
2727 END_PROFILE(SMBreadX);
2728 return nread;
2731 /****************************************************************************
2732 Reply to a writebraw (core+ or LANMAN1.0 protocol).
2733 ****************************************************************************/
2735 int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
2737 ssize_t nwritten=0;
2738 ssize_t total_written=0;
2739 size_t numtowrite=0;
2740 size_t tcount;
2741 SMB_OFF_T startpos;
2742 char *data=NULL;
2743 BOOL write_through;
2744 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2745 int outsize = 0;
2746 START_PROFILE(SMBwritebraw);
2748 if (srv_is_signing_active()) {
2749 exit_server("reply_writebraw: SMB signing is active - raw reads/writes are disallowed.");
2752 CHECK_FSP(fsp,conn);
2753 if (!CHECK_WRITE(fsp)) {
2754 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2757 tcount = IVAL(inbuf,smb_vwv1);
2758 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
2759 write_through = BITSETW(inbuf+smb_vwv7,0);
2761 /* We have to deal with slightly different formats depending
2762 on whether we are using the core+ or lanman1.0 protocol */
2764 if(Protocol <= PROTOCOL_COREPLUS) {
2765 numtowrite = SVAL(smb_buf(inbuf),-2);
2766 data = smb_buf(inbuf);
2767 } else {
2768 numtowrite = SVAL(inbuf,smb_vwv10);
2769 data = smb_base(inbuf) + SVAL(inbuf, smb_vwv11);
2772 /* force the error type */
2773 SCVAL(inbuf,smb_com,SMBwritec);
2774 SCVAL(outbuf,smb_com,SMBwritec);
2776 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
2777 END_PROFILE(SMBwritebraw);
2778 return(ERROR_DOS(ERRDOS,ERRlock));
2781 if (numtowrite>0)
2782 nwritten = write_file(fsp,data,startpos,numtowrite);
2784 DEBUG(3,("writebraw1 fnum=%d start=%.0f num=%d wrote=%d sync=%d\n",
2785 fsp->fnum, (double)startpos, (int)numtowrite, (int)nwritten, (int)write_through));
2787 if (nwritten < (ssize_t)numtowrite) {
2788 END_PROFILE(SMBwritebraw);
2789 return(UNIXERROR(ERRHRD,ERRdiskfull));
2792 total_written = nwritten;
2794 /* Return a message to the redirector to tell it to send more bytes */
2795 SCVAL(outbuf,smb_com,SMBwritebraw);
2796 SSVALS(outbuf,smb_vwv0,-1);
2797 outsize = set_message(outbuf,Protocol>PROTOCOL_COREPLUS?1:0,0,True);
2798 show_msg(outbuf);
2799 if (!send_smb(smbd_server_fd(),outbuf))
2800 exit_server("reply_writebraw: send_smb failed.");
2802 /* Now read the raw data into the buffer and write it */
2803 if (read_smb_length(smbd_server_fd(),inbuf,SMB_SECONDARY_WAIT) == -1) {
2804 exit_server("secondary writebraw failed");
2807 /* Even though this is not an smb message, smb_len returns the generic length of an smb message */
2808 numtowrite = smb_len(inbuf);
2810 /* Set up outbuf to return the correct return */
2811 outsize = set_message(outbuf,1,0,True);
2812 SCVAL(outbuf,smb_com,SMBwritec);
2814 if (numtowrite != 0) {
2816 if (numtowrite > BUFFER_SIZE) {
2817 DEBUG(0,("reply_writebraw: Oversize secondary write raw requested (%u). Terminating\n",
2818 (unsigned int)numtowrite ));
2819 exit_server("secondary writebraw failed");
2822 if (tcount > nwritten+numtowrite) {
2823 DEBUG(3,("Client overestimated the write %d %d %d\n",
2824 (int)tcount,(int)nwritten,(int)numtowrite));
2827 if (read_data( smbd_server_fd(), inbuf+4, numtowrite) != numtowrite ) {
2828 DEBUG(0,("reply_writebraw: Oversize secondary write raw read failed (%s). Terminating\n",
2829 strerror(errno) ));
2830 exit_server("secondary writebraw failed");
2833 nwritten = write_file(fsp,inbuf+4,startpos+nwritten,numtowrite);
2835 if (nwritten < (ssize_t)numtowrite) {
2836 SCVAL(outbuf,smb_rcls,ERRHRD);
2837 SSVAL(outbuf,smb_err,ERRdiskfull);
2840 if (nwritten > 0)
2841 total_written += nwritten;
2844 SSVAL(outbuf,smb_vwv0,total_written);
2846 sync_file(conn, fsp, write_through);
2848 DEBUG(3,("writebraw2 fnum=%d start=%.0f num=%d wrote=%d\n",
2849 fsp->fnum, (double)startpos, (int)numtowrite,(int)total_written));
2851 /* we won't return a status if write through is not selected - this follows what WfWg does */
2852 END_PROFILE(SMBwritebraw);
2853 if (!write_through && total_written==tcount) {
2855 #if RABBIT_PELLET_FIX
2857 * Fix for "rabbit pellet" mode, trigger an early TCP ack by
2858 * sending a SMBkeepalive. Thanks to DaveCB at Sun for this. JRA.
2860 if (!send_keepalive(smbd_server_fd()))
2861 exit_server("reply_writebraw: send of keepalive failed");
2862 #endif
2863 return(-1);
2866 return(outsize);
2869 #undef DBGC_CLASS
2870 #define DBGC_CLASS DBGC_LOCKING
2872 /****************************************************************************
2873 Reply to a writeunlock (core+).
2874 ****************************************************************************/
2876 int reply_writeunlock(connection_struct *conn, char *inbuf,char *outbuf,
2877 int size, int dum_buffsize)
2879 ssize_t nwritten = -1;
2880 size_t numtowrite;
2881 SMB_OFF_T startpos;
2882 char *data;
2883 NTSTATUS status = NT_STATUS_OK;
2884 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2885 int outsize = 0;
2886 START_PROFILE(SMBwriteunlock);
2888 CHECK_FSP(fsp,conn);
2889 if (!CHECK_WRITE(fsp)) {
2890 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2893 numtowrite = SVAL(inbuf,smb_vwv1);
2894 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2895 data = smb_buf(inbuf) + 3;
2897 if (numtowrite && is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
2898 END_PROFILE(SMBwriteunlock);
2899 return ERROR_DOS(ERRDOS,ERRlock);
2902 /* The special X/Open SMB protocol handling of
2903 zero length writes is *NOT* done for
2904 this call */
2905 if(numtowrite == 0) {
2906 nwritten = 0;
2907 } else {
2908 nwritten = write_file(fsp,data,startpos,numtowrite);
2911 sync_file(conn, fsp, False /* write through */);
2913 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
2914 END_PROFILE(SMBwriteunlock);
2915 return(UNIXERROR(ERRHRD,ERRdiskfull));
2918 if (numtowrite) {
2919 status = do_unlock(fsp,
2920 (uint32)SVAL(inbuf,smb_pid),
2921 (SMB_BIG_UINT)numtowrite,
2922 (SMB_BIG_UINT)startpos,
2923 WINDOWS_LOCK);
2925 if (NT_STATUS_V(status)) {
2926 END_PROFILE(SMBwriteunlock);
2927 return ERROR_NT(status);
2931 outsize = set_message(outbuf,1,0,True);
2933 SSVAL(outbuf,smb_vwv0,nwritten);
2935 DEBUG(3,("writeunlock fnum=%d num=%d wrote=%d\n",
2936 fsp->fnum, (int)numtowrite, (int)nwritten));
2938 END_PROFILE(SMBwriteunlock);
2939 return outsize;
2942 #undef DBGC_CLASS
2943 #define DBGC_CLASS DBGC_ALL
2945 /****************************************************************************
2946 Reply to a write.
2947 ****************************************************************************/
2949 int reply_write(connection_struct *conn, char *inbuf,char *outbuf,int size,int dum_buffsize)
2951 size_t numtowrite;
2952 ssize_t nwritten = -1;
2953 SMB_OFF_T startpos;
2954 char *data;
2955 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2956 int outsize = 0;
2957 START_PROFILE(SMBwrite);
2959 /* If it's an IPC, pass off the pipe handler. */
2960 if (IS_IPC(conn)) {
2961 END_PROFILE(SMBwrite);
2962 return reply_pipe_write(inbuf,outbuf,size,dum_buffsize);
2965 CHECK_FSP(fsp,conn);
2966 if (!CHECK_WRITE(fsp)) {
2967 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2970 numtowrite = SVAL(inbuf,smb_vwv1);
2971 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2972 data = smb_buf(inbuf) + 3;
2974 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
2975 END_PROFILE(SMBwrite);
2976 return ERROR_DOS(ERRDOS,ERRlock);
2980 * X/Open SMB protocol says that if smb_vwv1 is
2981 * zero then the file size should be extended or
2982 * truncated to the size given in smb_vwv[2-3].
2985 if(numtowrite == 0) {
2987 * This is actually an allocate call, and set EOF. JRA.
2989 nwritten = vfs_allocate_file_space(fsp, (SMB_OFF_T)startpos);
2990 if (nwritten < 0) {
2991 END_PROFILE(SMBwrite);
2992 return ERROR_NT(NT_STATUS_DISK_FULL);
2994 nwritten = vfs_set_filelen(fsp, (SMB_OFF_T)startpos);
2995 if (nwritten < 0) {
2996 END_PROFILE(SMBwrite);
2997 return ERROR_NT(NT_STATUS_DISK_FULL);
2999 } else
3000 nwritten = write_file(fsp,data,startpos,numtowrite);
3002 sync_file(conn, fsp, False);
3004 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
3005 END_PROFILE(SMBwrite);
3006 return(UNIXERROR(ERRHRD,ERRdiskfull));
3009 outsize = set_message(outbuf,1,0,True);
3011 SSVAL(outbuf,smb_vwv0,nwritten);
3013 if (nwritten < (ssize_t)numtowrite) {
3014 SCVAL(outbuf,smb_rcls,ERRHRD);
3015 SSVAL(outbuf,smb_err,ERRdiskfull);
3018 DEBUG(3,("write fnum=%d num=%d wrote=%d\n", fsp->fnum, (int)numtowrite, (int)nwritten));
3020 END_PROFILE(SMBwrite);
3021 return(outsize);
3024 /****************************************************************************
3025 Reply to a write and X.
3026 ****************************************************************************/
3028 int reply_write_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
3030 files_struct *fsp = file_fsp(inbuf,smb_vwv2);
3031 SMB_OFF_T startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
3032 size_t numtowrite = SVAL(inbuf,smb_vwv10);
3033 BOOL write_through = BITSETW(inbuf+smb_vwv7,0);
3034 ssize_t nwritten = -1;
3035 unsigned int smb_doff = SVAL(inbuf,smb_vwv11);
3036 unsigned int smblen = smb_len(inbuf);
3037 char *data;
3038 BOOL large_writeX = ((CVAL(inbuf,smb_wct) == 14) && (smblen > 0xFFFF));
3039 START_PROFILE(SMBwriteX);
3041 /* If it's an IPC, pass off the pipe handler. */
3042 if (IS_IPC(conn)) {
3043 END_PROFILE(SMBwriteX);
3044 return reply_pipe_write_and_X(inbuf,outbuf,length,bufsize);
3047 CHECK_FSP(fsp,conn);
3048 if (!CHECK_WRITE(fsp)) {
3049 return(ERROR_DOS(ERRDOS,ERRbadaccess));
3052 set_message(outbuf,6,0,True);
3054 /* Deal with possible LARGE_WRITEX */
3055 if (large_writeX) {
3056 numtowrite |= ((((size_t)SVAL(inbuf,smb_vwv9)) & 1 )<<16);
3059 if(smb_doff > smblen || (smb_doff + numtowrite > smblen)) {
3060 END_PROFILE(SMBwriteX);
3061 return ERROR_DOS(ERRDOS,ERRbadmem);
3064 data = smb_base(inbuf) + smb_doff;
3066 if(CVAL(inbuf,smb_wct) == 14) {
3067 #ifdef LARGE_SMB_OFF_T
3069 * This is a large offset (64 bit) write.
3071 startpos |= (((SMB_OFF_T)IVAL(inbuf,smb_vwv12)) << 32);
3073 #else /* !LARGE_SMB_OFF_T */
3076 * Ensure we haven't been sent a >32 bit offset.
3079 if(IVAL(inbuf,smb_vwv12) != 0) {
3080 DEBUG(0,("reply_write_and_X - large offset (%x << 32) used and we don't support \
3081 64 bit offsets.\n", (unsigned int)IVAL(inbuf,smb_vwv12) ));
3082 END_PROFILE(SMBwriteX);
3083 return ERROR_DOS(ERRDOS,ERRbadaccess);
3086 #endif /* LARGE_SMB_OFF_T */
3089 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
3090 END_PROFILE(SMBwriteX);
3091 return ERROR_DOS(ERRDOS,ERRlock);
3094 /* X/Open SMB protocol says that, unlike SMBwrite
3095 if the length is zero then NO truncation is
3096 done, just a write of zero. To truncate a file,
3097 use SMBwrite. */
3099 if(numtowrite == 0) {
3100 nwritten = 0;
3101 } else {
3103 if (schedule_aio_write_and_X(conn, inbuf, outbuf, length, bufsize,
3104 fsp,data,startpos,numtowrite)) {
3105 END_PROFILE(SMBwriteX);
3106 return -1;
3109 nwritten = write_file(fsp,data,startpos,numtowrite);
3112 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
3113 END_PROFILE(SMBwriteX);
3114 return(UNIXERROR(ERRHRD,ERRdiskfull));
3117 SSVAL(outbuf,smb_vwv2,nwritten);
3118 if (large_writeX)
3119 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
3121 if (nwritten < (ssize_t)numtowrite) {
3122 SCVAL(outbuf,smb_rcls,ERRHRD);
3123 SSVAL(outbuf,smb_err,ERRdiskfull);
3126 DEBUG(3,("writeX fnum=%d num=%d wrote=%d\n",
3127 fsp->fnum, (int)numtowrite, (int)nwritten));
3129 sync_file(conn, fsp, write_through);
3131 END_PROFILE(SMBwriteX);
3132 return chain_reply(inbuf,outbuf,length,bufsize);
3135 /****************************************************************************
3136 Reply to a lseek.
3137 ****************************************************************************/
3139 int reply_lseek(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
3141 SMB_OFF_T startpos;
3142 SMB_OFF_T res= -1;
3143 int mode,umode;
3144 int outsize = 0;
3145 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3146 START_PROFILE(SMBlseek);
3148 CHECK_FSP(fsp,conn);
3150 flush_write_cache(fsp, SEEK_FLUSH);
3152 mode = SVAL(inbuf,smb_vwv1) & 3;
3153 /* NB. This doesn't use IVAL_TO_SMB_OFF_T as startpos can be signed in this case. */
3154 startpos = (SMB_OFF_T)IVALS(inbuf,smb_vwv2);
3156 switch (mode) {
3157 case 0:
3158 umode = SEEK_SET;
3159 res = startpos;
3160 break;
3161 case 1:
3162 umode = SEEK_CUR;
3163 res = fsp->fh->pos + startpos;
3164 break;
3165 case 2:
3166 umode = SEEK_END;
3167 break;
3168 default:
3169 umode = SEEK_SET;
3170 res = startpos;
3171 break;
3174 if (umode == SEEK_END) {
3175 if((res = SMB_VFS_LSEEK(fsp,fsp->fh->fd,startpos,umode)) == -1) {
3176 if(errno == EINVAL) {
3177 SMB_OFF_T current_pos = startpos;
3178 SMB_STRUCT_STAT sbuf;
3180 if(SMB_VFS_FSTAT(fsp,fsp->fh->fd, &sbuf) == -1) {
3181 END_PROFILE(SMBlseek);
3182 return(UNIXERROR(ERRDOS,ERRnoaccess));
3185 current_pos += sbuf.st_size;
3186 if(current_pos < 0)
3187 res = SMB_VFS_LSEEK(fsp,fsp->fh->fd,0,SEEK_SET);
3191 if(res == -1) {
3192 END_PROFILE(SMBlseek);
3193 return(UNIXERROR(ERRDOS,ERRnoaccess));
3197 fsp->fh->pos = res;
3199 outsize = set_message(outbuf,2,0,True);
3200 SIVAL(outbuf,smb_vwv0,res);
3202 DEBUG(3,("lseek fnum=%d ofs=%.0f newpos = %.0f mode=%d\n",
3203 fsp->fnum, (double)startpos, (double)res, mode));
3205 END_PROFILE(SMBlseek);
3206 return(outsize);
3209 /****************************************************************************
3210 Reply to a flush.
3211 ****************************************************************************/
3213 int reply_flush(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
3215 int outsize = set_message(outbuf,0,0,False);
3216 uint16 fnum = SVAL(inbuf,smb_vwv0);
3217 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3218 START_PROFILE(SMBflush);
3220 if (fnum != 0xFFFF)
3221 CHECK_FSP(fsp,conn);
3223 if (!fsp) {
3224 file_sync_all(conn);
3225 } else {
3226 sync_file(conn,fsp, True);
3229 DEBUG(3,("flush\n"));
3230 END_PROFILE(SMBflush);
3231 return(outsize);
3234 /****************************************************************************
3235 Reply to a exit.
3236 conn POINTER CAN BE NULL HERE !
3237 ****************************************************************************/
3239 int reply_exit(connection_struct *conn,
3240 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3242 int outsize;
3243 START_PROFILE(SMBexit);
3245 file_close_pid(SVAL(inbuf,smb_pid),SVAL(inbuf,smb_uid));
3247 outsize = set_message(outbuf,0,0,False);
3249 DEBUG(3,("exit\n"));
3251 END_PROFILE(SMBexit);
3252 return(outsize);
3255 /****************************************************************************
3256 Reply to a close - has to deal with closing a directory opened by NT SMB's.
3257 ****************************************************************************/
3259 int reply_close(connection_struct *conn, char *inbuf,char *outbuf, int size,
3260 int dum_buffsize)
3262 int outsize = 0;
3263 time_t mtime;
3264 int32 eclass = 0, err = 0;
3265 files_struct *fsp = NULL;
3266 START_PROFILE(SMBclose);
3268 outsize = set_message(outbuf,0,0,False);
3270 /* If it's an IPC, pass off to the pipe handler. */
3271 if (IS_IPC(conn)) {
3272 END_PROFILE(SMBclose);
3273 return reply_pipe_close(conn, inbuf,outbuf);
3276 fsp = file_fsp(inbuf,smb_vwv0);
3279 * We can only use CHECK_FSP if we know it's not a directory.
3282 if(!fsp || (fsp->conn != conn) || (fsp->vuid != current_user.vuid)) {
3283 END_PROFILE(SMBclose);
3284 return ERROR_DOS(ERRDOS,ERRbadfid);
3287 if(fsp->is_directory) {
3289 * Special case - close NT SMB directory handle.
3291 DEBUG(3,("close %s fnum=%d\n", fsp->is_directory ? "directory" : "stat file open", fsp->fnum));
3292 close_file(fsp,NORMAL_CLOSE);
3293 } else {
3295 * Close ordinary file.
3297 int close_err;
3298 pstring file_name;
3300 /* Save the name for time set in close. */
3301 pstrcpy( file_name, fsp->fsp_name);
3303 DEBUG(3,("close fd=%d fnum=%d (numopen=%d)\n",
3304 fsp->fh->fd, fsp->fnum,
3305 conn->num_files_open));
3308 * Take care of any time sent in the close.
3311 mtime = srv_make_unix_date3(inbuf+smb_vwv1);
3312 fsp_set_pending_modtime(fsp, mtime);
3315 * close_file() returns the unix errno if an error
3316 * was detected on close - normally this is due to
3317 * a disk full error. If not then it was probably an I/O error.
3320 if((close_err = close_file(fsp,NORMAL_CLOSE)) != 0) {
3321 errno = close_err;
3322 END_PROFILE(SMBclose);
3323 return (UNIXERROR(ERRHRD,ERRgeneral));
3327 /* We have a cached error */
3328 if(eclass || err) {
3329 END_PROFILE(SMBclose);
3330 return ERROR_DOS(eclass,err);
3333 END_PROFILE(SMBclose);
3334 return(outsize);
3337 /****************************************************************************
3338 Reply to a writeclose (Core+ protocol).
3339 ****************************************************************************/
3341 int reply_writeclose(connection_struct *conn,
3342 char *inbuf,char *outbuf, int size, int dum_buffsize)
3344 size_t numtowrite;
3345 ssize_t nwritten = -1;
3346 int outsize = 0;
3347 int close_err = 0;
3348 SMB_OFF_T startpos;
3349 char *data;
3350 time_t mtime;
3351 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3352 START_PROFILE(SMBwriteclose);
3354 CHECK_FSP(fsp,conn);
3355 if (!CHECK_WRITE(fsp)) {
3356 return(ERROR_DOS(ERRDOS,ERRbadaccess));
3359 numtowrite = SVAL(inbuf,smb_vwv1);
3360 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
3361 mtime = srv_make_unix_date3(inbuf+smb_vwv4);
3362 data = smb_buf(inbuf) + 1;
3364 if (numtowrite && is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
3365 END_PROFILE(SMBwriteclose);
3366 return ERROR_DOS(ERRDOS,ERRlock);
3369 nwritten = write_file(fsp,data,startpos,numtowrite);
3371 set_filetime(conn, fsp->fsp_name,mtime);
3374 * More insanity. W2K only closes the file if writelen > 0.
3375 * JRA.
3378 if (numtowrite) {
3379 DEBUG(3,("reply_writeclose: zero length write doesn't close file %s\n",
3380 fsp->fsp_name ));
3381 close_err = close_file(fsp,NORMAL_CLOSE);
3384 DEBUG(3,("writeclose fnum=%d num=%d wrote=%d (numopen=%d)\n",
3385 fsp->fnum, (int)numtowrite, (int)nwritten,
3386 conn->num_files_open));
3388 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
3389 END_PROFILE(SMBwriteclose);
3390 return(UNIXERROR(ERRHRD,ERRdiskfull));
3393 if(close_err != 0) {
3394 errno = close_err;
3395 END_PROFILE(SMBwriteclose);
3396 return(UNIXERROR(ERRHRD,ERRgeneral));
3399 outsize = set_message(outbuf,1,0,True);
3401 SSVAL(outbuf,smb_vwv0,nwritten);
3402 END_PROFILE(SMBwriteclose);
3403 return(outsize);
3406 #undef DBGC_CLASS
3407 #define DBGC_CLASS DBGC_LOCKING
3409 /****************************************************************************
3410 Reply to a lock.
3411 ****************************************************************************/
3413 int reply_lock(connection_struct *conn,
3414 char *inbuf,char *outbuf, int length, int dum_buffsize)
3416 int outsize = set_message(outbuf,0,0,False);
3417 SMB_BIG_UINT count,offset;
3418 NTSTATUS status;
3419 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3421 START_PROFILE(SMBlock);
3423 CHECK_FSP(fsp,conn);
3425 release_level_2_oplocks_on_change(fsp);
3427 count = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv1);
3428 offset = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv3);
3430 DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
3431 fsp->fh->fd, fsp->fnum, (double)offset, (double)count));
3433 status = do_lock(fsp,
3434 (uint32)SVAL(inbuf,smb_pid),
3435 count,
3436 offset,
3437 WRITE_LOCK,
3438 WINDOWS_LOCK,
3439 0 /* zero timeout. */);
3441 if (NT_STATUS_V(status)) {
3442 #if 0
3443 /* Tests using Samba4 against W2K show this call never creates a blocking lock. */
3444 if (lp_blocking_locks(SNUM(conn)) && ERROR_WAS_LOCK_DENIED(status)) {
3446 * A blocking lock was requested. Package up
3447 * this smb into a queued request and push it
3448 * onto the blocking lock queue.
3450 if(push_blocking_lock_request(inbuf, length,
3451 fsp,
3454 SVAL(inbuf,smb_pid),
3455 WRITE_LOCK,
3456 WINDOWS_LOCK,
3457 offset, count)) {
3458 END_PROFILE(SMBlock);
3459 return -1;
3462 #endif
3463 END_PROFILE(SMBlock);
3464 return ERROR_NT(status);
3467 END_PROFILE(SMBlock);
3468 return(outsize);
3471 /****************************************************************************
3472 Reply to a unlock.
3473 ****************************************************************************/
3475 int reply_unlock(connection_struct *conn, char *inbuf,char *outbuf, int size,
3476 int dum_buffsize)
3478 int outsize = set_message(outbuf,0,0,False);
3479 SMB_BIG_UINT count,offset;
3480 NTSTATUS status;
3481 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3482 START_PROFILE(SMBunlock);
3484 CHECK_FSP(fsp,conn);
3486 count = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv1);
3487 offset = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv3);
3489 status = do_unlock(fsp,
3490 (uint32)SVAL(inbuf,smb_pid),
3491 count,
3492 offset,
3493 WINDOWS_LOCK);
3495 if (NT_STATUS_V(status)) {
3496 END_PROFILE(SMBunlock);
3497 return ERROR_NT(status);
3500 DEBUG( 3, ( "unlock fd=%d fnum=%d offset=%.0f count=%.0f\n",
3501 fsp->fh->fd, fsp->fnum, (double)offset, (double)count ) );
3503 END_PROFILE(SMBunlock);
3504 return(outsize);
3507 #undef DBGC_CLASS
3508 #define DBGC_CLASS DBGC_ALL
3510 /****************************************************************************
3511 Reply to a tdis.
3512 conn POINTER CAN BE NULL HERE !
3513 ****************************************************************************/
3515 int reply_tdis(connection_struct *conn,
3516 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3518 int outsize = set_message(outbuf,0,0,False);
3519 uint16 vuid;
3520 START_PROFILE(SMBtdis);
3522 vuid = SVAL(inbuf,smb_uid);
3524 if (!conn) {
3525 DEBUG(4,("Invalid connection in tdis\n"));
3526 END_PROFILE(SMBtdis);
3527 return ERROR_DOS(ERRSRV,ERRinvnid);
3530 conn->used = False;
3532 close_cnum(conn,vuid);
3534 END_PROFILE(SMBtdis);
3535 return outsize;
3538 /****************************************************************************
3539 Reply to a echo.
3540 conn POINTER CAN BE NULL HERE !
3541 ****************************************************************************/
3543 int reply_echo(connection_struct *conn,
3544 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3546 int smb_reverb = SVAL(inbuf,smb_vwv0);
3547 int seq_num;
3548 unsigned int data_len = smb_buflen(inbuf);
3549 int outsize = set_message(outbuf,1,data_len,True);
3550 START_PROFILE(SMBecho);
3552 if (data_len > BUFFER_SIZE) {
3553 DEBUG(0,("reply_echo: data_len too large.\n"));
3554 END_PROFILE(SMBecho);
3555 return -1;
3558 /* copy any incoming data back out */
3559 if (data_len > 0)
3560 memcpy(smb_buf(outbuf),smb_buf(inbuf),data_len);
3562 if (smb_reverb > 100) {
3563 DEBUG(0,("large reverb (%d)?? Setting to 100\n",smb_reverb));
3564 smb_reverb = 100;
3567 for (seq_num =1 ; seq_num <= smb_reverb ; seq_num++) {
3568 SSVAL(outbuf,smb_vwv0,seq_num);
3570 smb_setlen(outbuf,outsize - 4);
3572 show_msg(outbuf);
3573 if (!send_smb(smbd_server_fd(),outbuf))
3574 exit_server("reply_echo: send_smb failed.");
3577 DEBUG(3,("echo %d times\n", smb_reverb));
3579 smb_echo_count++;
3581 END_PROFILE(SMBecho);
3582 return -1;
3585 /****************************************************************************
3586 Reply to a printopen.
3587 ****************************************************************************/
3589 int reply_printopen(connection_struct *conn,
3590 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3592 int outsize = 0;
3593 files_struct *fsp;
3594 NTSTATUS status;
3596 START_PROFILE(SMBsplopen);
3598 if (!CAN_PRINT(conn)) {
3599 END_PROFILE(SMBsplopen);
3600 return ERROR_DOS(ERRDOS,ERRnoaccess);
3603 /* Open for exclusive use, write only. */
3604 status = print_fsp_open(conn, NULL, &fsp);
3606 if (!NT_STATUS_IS_OK(status)) {
3607 END_PROFILE(SMBsplopen);
3608 return(ERROR_NT(status));
3611 outsize = set_message(outbuf,1,0,True);
3612 SSVAL(outbuf,smb_vwv0,fsp->fnum);
3614 DEBUG(3,("openprint fd=%d fnum=%d\n",
3615 fsp->fh->fd, fsp->fnum));
3617 END_PROFILE(SMBsplopen);
3618 return(outsize);
3621 /****************************************************************************
3622 Reply to a printclose.
3623 ****************************************************************************/
3625 int reply_printclose(connection_struct *conn,
3626 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3628 int outsize = set_message(outbuf,0,0,False);
3629 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3630 int close_err = 0;
3631 START_PROFILE(SMBsplclose);
3633 CHECK_FSP(fsp,conn);
3635 if (!CAN_PRINT(conn)) {
3636 END_PROFILE(SMBsplclose);
3637 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
3640 DEBUG(3,("printclose fd=%d fnum=%d\n",
3641 fsp->fh->fd,fsp->fnum));
3643 close_err = close_file(fsp,NORMAL_CLOSE);
3645 if(close_err != 0) {
3646 errno = close_err;
3647 END_PROFILE(SMBsplclose);
3648 return(UNIXERROR(ERRHRD,ERRgeneral));
3651 END_PROFILE(SMBsplclose);
3652 return(outsize);
3655 /****************************************************************************
3656 Reply to a printqueue.
3657 ****************************************************************************/
3659 int reply_printqueue(connection_struct *conn,
3660 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3662 int outsize = set_message(outbuf,2,3,True);
3663 int max_count = SVAL(inbuf,smb_vwv0);
3664 int start_index = SVAL(inbuf,smb_vwv1);
3665 START_PROFILE(SMBsplretq);
3667 /* we used to allow the client to get the cnum wrong, but that
3668 is really quite gross and only worked when there was only
3669 one printer - I think we should now only accept it if they
3670 get it right (tridge) */
3671 if (!CAN_PRINT(conn)) {
3672 END_PROFILE(SMBsplretq);
3673 return ERROR_DOS(ERRDOS,ERRnoaccess);
3676 SSVAL(outbuf,smb_vwv0,0);
3677 SSVAL(outbuf,smb_vwv1,0);
3678 SCVAL(smb_buf(outbuf),0,1);
3679 SSVAL(smb_buf(outbuf),1,0);
3681 DEBUG(3,("printqueue start_index=%d max_count=%d\n",
3682 start_index, max_count));
3685 print_queue_struct *queue = NULL;
3686 print_status_struct status;
3687 char *p = smb_buf(outbuf) + 3;
3688 int count = print_queue_status(SNUM(conn), &queue, &status);
3689 int num_to_get = ABS(max_count);
3690 int first = (max_count>0?start_index:start_index+max_count+1);
3691 int i;
3693 if (first >= count)
3694 num_to_get = 0;
3695 else
3696 num_to_get = MIN(num_to_get,count-first);
3699 for (i=first;i<first+num_to_get;i++) {
3700 srv_put_dos_date2(p,0,queue[i].time);
3701 SCVAL(p,4,(queue[i].status==LPQ_PRINTING?2:3));
3702 SSVAL(p,5, queue[i].job);
3703 SIVAL(p,7,queue[i].size);
3704 SCVAL(p,11,0);
3705 srvstr_push(outbuf, p+12, queue[i].fs_user, 16, STR_ASCII);
3706 p += 28;
3709 if (count > 0) {
3710 outsize = set_message(outbuf,2,28*count+3,False);
3711 SSVAL(outbuf,smb_vwv0,count);
3712 SSVAL(outbuf,smb_vwv1,(max_count>0?first+count:first-1));
3713 SCVAL(smb_buf(outbuf),0,1);
3714 SSVAL(smb_buf(outbuf),1,28*count);
3717 SAFE_FREE(queue);
3719 DEBUG(3,("%d entries returned in queue\n",count));
3722 END_PROFILE(SMBsplretq);
3723 return(outsize);
3726 /****************************************************************************
3727 Reply to a printwrite.
3728 ****************************************************************************/
3730 int reply_printwrite(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3732 int numtowrite;
3733 int outsize = set_message(outbuf,0,0,False);
3734 char *data;
3735 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3737 START_PROFILE(SMBsplwr);
3739 if (!CAN_PRINT(conn)) {
3740 END_PROFILE(SMBsplwr);
3741 return ERROR_DOS(ERRDOS,ERRnoaccess);
3744 CHECK_FSP(fsp,conn);
3745 if (!CHECK_WRITE(fsp)) {
3746 return(ERROR_DOS(ERRDOS,ERRbadaccess));
3749 numtowrite = SVAL(smb_buf(inbuf),1);
3750 data = smb_buf(inbuf) + 3;
3752 if (write_file(fsp,data,-1,numtowrite) != numtowrite) {
3753 END_PROFILE(SMBsplwr);
3754 return(UNIXERROR(ERRHRD,ERRdiskfull));
3757 DEBUG( 3, ( "printwrite fnum=%d num=%d\n", fsp->fnum, numtowrite ) );
3759 END_PROFILE(SMBsplwr);
3760 return(outsize);
3763 /****************************************************************************
3764 The guts of the mkdir command, split out so it may be called by the NT SMB
3765 code.
3766 ****************************************************************************/
3768 NTSTATUS mkdir_internal(connection_struct *conn, const pstring directory, BOOL bad_path)
3770 int ret= -1;
3772 if(!CAN_WRITE(conn)) {
3773 DEBUG(5,("mkdir_internal: failing create on read-only share %s\n", lp_servicename(SNUM(conn))));
3774 errno = EACCES;
3775 return map_nt_error_from_unix(errno);
3778 if (bad_path) {
3779 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
3782 if (!check_name(directory, conn)) {
3783 if(errno == ENOENT) {
3784 if (bad_path) {
3785 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
3786 } else {
3787 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3790 return map_nt_error_from_unix(errno);
3793 ret = vfs_MkDir(conn,directory,unix_mode(conn,aDIR,directory,True));
3794 if (ret == -1) {
3795 if(errno == ENOENT) {
3796 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
3798 return map_nt_error_from_unix(errno);
3801 return NT_STATUS_OK;
3804 /****************************************************************************
3805 Reply to a mkdir.
3806 ****************************************************************************/
3808 int reply_mkdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3810 pstring directory;
3811 int outsize;
3812 NTSTATUS status;
3813 BOOL bad_path = False;
3814 SMB_STRUCT_STAT sbuf;
3816 START_PROFILE(SMBmkdir);
3818 srvstr_get_path(inbuf, directory, smb_buf(inbuf) + 1, sizeof(directory), 0, STR_TERMINATE, &status);
3819 if (!NT_STATUS_IS_OK(status)) {
3820 END_PROFILE(SMBmkdir);
3821 return ERROR_NT(status);
3824 RESOLVE_DFSPATH(directory, conn, inbuf, outbuf);
3826 unix_convert(directory,conn,0,&bad_path,&sbuf);
3828 if( is_ntfs_stream_name(directory)) {
3829 DEBUG(5,("reply_mkdir: failing create on filename %s with colon in name\n", directory));
3830 END_PROFILE(SMBmkdir);
3831 return ERROR_NT(NT_STATUS_NOT_A_DIRECTORY);
3834 status = mkdir_internal(conn, directory,bad_path);
3835 if (!NT_STATUS_IS_OK(status)) {
3837 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION) &&
3838 !use_nt_status()) {
3840 * Yes, in the DOS error code case we get a
3841 * ERRDOS:ERRnoaccess here. See BASE-SAMBA3ERROR
3842 * samba4 torture test.
3844 status = NT_STATUS_DOS(ERRDOS, ERRnoaccess);
3847 END_PROFILE(SMBmkdir);
3848 return ERROR_NT(status);
3851 if (lp_inherit_owner(SNUM(conn))) {
3852 /* Ensure we're checking for a symlink here.... */
3853 /* We don't want to get caught by a symlink racer. */
3855 if(SMB_VFS_LSTAT(conn,directory, &sbuf) != 0) {
3856 END_PROFILE(SMBmkdir);
3857 return(UNIXERROR(ERRDOS,ERRnoaccess));
3860 if(!S_ISDIR(sbuf.st_mode)) {
3861 DEBUG(0,("reply_mkdir: %s is not a directory !\n", directory ));
3862 END_PROFILE(SMBmkdir);
3863 return(UNIXERROR(ERRDOS,ERRnoaccess));
3866 change_owner_to_parent(conn, NULL, directory, &sbuf);
3869 outsize = set_message(outbuf,0,0,False);
3871 DEBUG( 3, ( "mkdir %s ret=%d\n", directory, outsize ) );
3873 END_PROFILE(SMBmkdir);
3874 return(outsize);
3877 /****************************************************************************
3878 Static function used by reply_rmdir to delete an entire directory
3879 tree recursively. Return False on ok, True on fail.
3880 ****************************************************************************/
3882 static BOOL recursive_rmdir(connection_struct *conn, char *directory)
3884 const char *dname = NULL;
3885 BOOL ret = False;
3886 long offset = 0;
3887 struct smb_Dir *dir_hnd = OpenDir(conn, directory, NULL, 0);
3889 if(dir_hnd == NULL)
3890 return True;
3892 while((dname = ReadDirName(dir_hnd, &offset))) {
3893 pstring fullname;
3894 SMB_STRUCT_STAT st;
3896 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
3897 continue;
3899 if (!is_visible_file(conn, directory, dname, &st, False))
3900 continue;
3902 /* Construct the full name. */
3903 if(strlen(directory) + strlen(dname) + 1 >= sizeof(fullname)) {
3904 errno = ENOMEM;
3905 ret = True;
3906 break;
3909 pstrcpy(fullname, directory);
3910 pstrcat(fullname, "/");
3911 pstrcat(fullname, dname);
3913 if(SMB_VFS_LSTAT(conn,fullname, &st) != 0) {
3914 ret = True;
3915 break;
3918 if(st.st_mode & S_IFDIR) {
3919 if(recursive_rmdir(conn, fullname)!=0) {
3920 ret = True;
3921 break;
3923 if(SMB_VFS_RMDIR(conn,fullname) != 0) {
3924 ret = True;
3925 break;
3927 } else if(SMB_VFS_UNLINK(conn,fullname) != 0) {
3928 ret = True;
3929 break;
3932 CloseDir(dir_hnd);
3933 return ret;
3936 /****************************************************************************
3937 The internals of the rmdir code - called elsewhere.
3938 ****************************************************************************/
3940 BOOL rmdir_internals(connection_struct *conn, char *directory)
3942 BOOL ok;
3943 SMB_STRUCT_STAT st;
3945 ok = (SMB_VFS_RMDIR(conn,directory) == 0);
3946 if(!ok && ((errno == ENOTEMPTY)||(errno == EEXIST)) && lp_veto_files(SNUM(conn))) {
3948 * Check to see if the only thing in this directory are
3949 * vetoed files/directories. If so then delete them and
3950 * retry. If we fail to delete any of them (and we *don't*
3951 * do a recursive delete) then fail the rmdir.
3953 BOOL all_veto_files = True;
3954 const char *dname;
3955 struct smb_Dir *dir_hnd = OpenDir(conn, directory, NULL, 0);
3957 if(dir_hnd != NULL) {
3958 long dirpos = 0;
3959 while ((dname = ReadDirName(dir_hnd,&dirpos))) {
3960 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
3961 continue;
3962 if (!is_visible_file(conn, directory, dname, &st, False))
3963 continue;
3964 if(!IS_VETO_PATH(conn, dname)) {
3965 all_veto_files = False;
3966 break;
3970 if(all_veto_files) {
3971 RewindDir(dir_hnd,&dirpos);
3972 while ((dname = ReadDirName(dir_hnd,&dirpos))) {
3973 pstring fullname;
3975 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
3976 continue;
3977 if (!is_visible_file(conn, directory, dname, &st, False))
3978 continue;
3980 /* Construct the full name. */
3981 if(strlen(directory) + strlen(dname) + 1 >= sizeof(fullname)) {
3982 errno = ENOMEM;
3983 break;
3986 pstrcpy(fullname, directory);
3987 pstrcat(fullname, "/");
3988 pstrcat(fullname, dname);
3990 if(SMB_VFS_LSTAT(conn,fullname, &st) != 0)
3991 break;
3992 if(st.st_mode & S_IFDIR) {
3993 if(lp_recursive_veto_delete(SNUM(conn))) {
3994 if(recursive_rmdir(conn, fullname) != 0)
3995 break;
3997 if(SMB_VFS_RMDIR(conn,fullname) != 0)
3998 break;
3999 } else if(SMB_VFS_UNLINK(conn,fullname) != 0)
4000 break;
4002 CloseDir(dir_hnd);
4003 /* Retry the rmdir */
4004 ok = (SMB_VFS_RMDIR(conn,directory) == 0);
4005 } else {
4006 CloseDir(dir_hnd);
4008 } else {
4009 errno = ENOTEMPTY;
4013 if (!ok)
4014 DEBUG(3,("rmdir_internals: couldn't remove directory %s : %s\n", directory,strerror(errno)));
4016 return ok;
4019 /****************************************************************************
4020 Reply to a rmdir.
4021 ****************************************************************************/
4023 int reply_rmdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
4025 pstring directory;
4026 int outsize = 0;
4027 BOOL ok = False;
4028 BOOL bad_path = False;
4029 SMB_STRUCT_STAT sbuf;
4030 NTSTATUS status;
4031 START_PROFILE(SMBrmdir);
4033 srvstr_get_path(inbuf, directory, smb_buf(inbuf) + 1, sizeof(directory), 0, STR_TERMINATE, &status);
4034 if (!NT_STATUS_IS_OK(status)) {
4035 END_PROFILE(SMBrmdir);
4036 return ERROR_NT(status);
4039 RESOLVE_DFSPATH(directory, conn, inbuf, outbuf)
4041 unix_convert(directory,conn, NULL,&bad_path,&sbuf);
4042 if (bad_path) {
4043 END_PROFILE(SMBrmdir);
4044 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
4047 if (check_name(directory,conn)) {
4048 dptr_closepath(directory,SVAL(inbuf,smb_pid));
4049 ok = rmdir_internals(conn, directory);
4052 if (!ok) {
4053 END_PROFILE(SMBrmdir);
4054 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRbadpath);
4057 outsize = set_message(outbuf,0,0,False);
4059 DEBUG( 3, ( "rmdir %s\n", directory ) );
4061 END_PROFILE(SMBrmdir);
4062 return(outsize);
4065 /*******************************************************************
4066 Resolve wildcards in a filename rename.
4067 Note that name is in UNIX charset and thus potentially can be more
4068 than fstring buffer (255 bytes) especially in default UTF-8 case.
4069 Therefore, we use pstring inside and all calls should ensure that
4070 name2 is at least pstring-long (they do already)
4071 ********************************************************************/
4073 static BOOL resolve_wildcards(const char *name1, char *name2)
4075 pstring root1,root2;
4076 pstring ext1,ext2;
4077 char *p,*p2, *pname1, *pname2;
4078 int available_space, actual_space;
4081 pname1 = strrchr_m(name1,'/');
4082 pname2 = strrchr_m(name2,'/');
4084 if (!pname1 || !pname2)
4085 return(False);
4087 pstrcpy(root1,pname1);
4088 pstrcpy(root2,pname2);
4089 p = strrchr_m(root1,'.');
4090 if (p) {
4091 *p = 0;
4092 pstrcpy(ext1,p+1);
4093 } else {
4094 pstrcpy(ext1,"");
4096 p = strrchr_m(root2,'.');
4097 if (p) {
4098 *p = 0;
4099 pstrcpy(ext2,p+1);
4100 } else {
4101 pstrcpy(ext2,"");
4104 p = root1;
4105 p2 = root2;
4106 while (*p2) {
4107 if (*p2 == '?') {
4108 *p2 = *p;
4109 p2++;
4110 } else if (*p2 == '*') {
4111 pstrcpy(p2, p);
4112 break;
4113 } else {
4114 p2++;
4116 if (*p)
4117 p++;
4120 p = ext1;
4121 p2 = ext2;
4122 while (*p2) {
4123 if (*p2 == '?') {
4124 *p2 = *p;
4125 p2++;
4126 } else if (*p2 == '*') {
4127 pstrcpy(p2, p);
4128 break;
4129 } else {
4130 p2++;
4132 if (*p)
4133 p++;
4136 available_space = sizeof(pstring) - PTR_DIFF(pname2, name2);
4138 if (ext2[0]) {
4139 actual_space = snprintf(pname2, available_space - 1, "%s.%s", root2, ext2);
4140 if (actual_space >= available_space - 1) {
4141 DEBUG(1,("resolve_wildcards: can't fit resolved name into specified buffer (overrun by %d bytes)\n",
4142 actual_space - available_space));
4144 } else {
4145 pstrcpy_base(pname2, root2, name2);
4148 return(True);
4151 /****************************************************************************
4152 Ensure open files have their names updated. Updated to notify other smbd's
4153 asynchronously.
4154 ****************************************************************************/
4156 static void rename_open_files(connection_struct *conn, struct share_mode_lock *lck,
4157 SMB_DEV_T dev, SMB_INO_T inode, const char *newname)
4159 files_struct *fsp;
4160 BOOL did_rename = False;
4162 for(fsp = file_find_di_first(dev, inode); fsp; fsp = file_find_di_next(fsp)) {
4163 /* fsp_name is a relative path under the fsp. To change this for other
4164 sharepaths we need to manipulate relative paths. */
4165 /* TODO - create the absolute path and manipulate the newname
4166 relative to the sharepath. */
4167 if (fsp->conn != conn) {
4168 continue;
4170 DEBUG(10,("rename_open_files: renaming file fnum %d (dev = %x, inode = %.0f) from %s -> %s\n",
4171 fsp->fnum, (unsigned int)fsp->dev, (double)fsp->inode,
4172 fsp->fsp_name, newname ));
4173 string_set(&fsp->fsp_name, newname);
4174 did_rename = True;
4177 if (!did_rename) {
4178 DEBUG(10,("rename_open_files: no open files on dev %x, inode %.0f for %s\n",
4179 (unsigned int)dev, (double)inode, newname ));
4182 /* Send messages to all smbd's (not ourself) that the name has changed. */
4183 rename_share_filename(lck, conn->connectpath, newname);
4186 /****************************************************************************
4187 We need to check if the source path is a parent directory of the destination
4188 (ie. a rename of /foo/bar/baz -> /foo/bar/baz/bibble/bobble. If so we must
4189 refuse the rename with a sharing violation. Under UNIX the above call can
4190 *succeed* if /foo/bar/baz is a symlink to another area in the share. We
4191 probably need to check that the client is a Windows one before disallowing
4192 this as a UNIX client (one with UNIX extensions) can know the source is a
4193 symlink and make this decision intelligently. Found by an excellent bug
4194 report from <AndyLiebman@aol.com>.
4195 ****************************************************************************/
4197 static BOOL rename_path_prefix_equal(const char *src, const char *dest)
4199 const char *psrc = src;
4200 const char *pdst = dest;
4201 size_t slen;
4203 if (psrc[0] == '.' && psrc[1] == '/') {
4204 psrc += 2;
4206 if (pdst[0] == '.' && pdst[1] == '/') {
4207 pdst += 2;
4209 if ((slen = strlen(psrc)) > strlen(pdst)) {
4210 return False;
4212 return ((memcmp(psrc, pdst, slen) == 0) && pdst[slen] == '/');
4215 /****************************************************************************
4216 Rename an open file - given an fsp.
4217 ****************************************************************************/
4219 NTSTATUS rename_internals_fsp(connection_struct *conn, files_struct *fsp, char *newname, uint32 attrs, BOOL replace_if_exists)
4221 SMB_STRUCT_STAT sbuf;
4222 BOOL bad_path = False;
4223 pstring newname_last_component;
4224 NTSTATUS error = NT_STATUS_OK;
4225 BOOL dest_exists;
4226 BOOL rcdest = True;
4227 struct share_mode_lock *lck = NULL;
4229 ZERO_STRUCT(sbuf);
4230 rcdest = unix_convert(newname,conn,newname_last_component,&bad_path,&sbuf);
4232 /* Quick check for "." and ".." */
4233 if (!bad_path && newname_last_component[0] == '.') {
4234 if (!newname_last_component[1] || (newname_last_component[1] == '.' && !newname_last_component[2])) {
4235 return NT_STATUS_ACCESS_DENIED;
4238 if (!rcdest && bad_path) {
4239 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
4242 /* Ensure newname contains a '/' */
4243 if(strrchr_m(newname,'/') == 0) {
4244 pstring tmpstr;
4246 pstrcpy(tmpstr, "./");
4247 pstrcat(tmpstr, newname);
4248 pstrcpy(newname, tmpstr);
4252 * Check for special case with case preserving and not
4253 * case sensitive. If the old last component differs from the original
4254 * last component only by case, then we should allow
4255 * the rename (user is trying to change the case of the
4256 * filename).
4259 if((conn->case_sensitive == False) && (conn->case_preserve == True) &&
4260 strequal(newname, fsp->fsp_name)) {
4261 char *p;
4262 pstring newname_modified_last_component;
4265 * Get the last component of the modified name.
4266 * Note that we guarantee that newname contains a '/'
4267 * character above.
4269 p = strrchr_m(newname,'/');
4270 pstrcpy(newname_modified_last_component,p+1);
4272 if(strcsequal(newname_modified_last_component,
4273 newname_last_component) == False) {
4275 * Replace the modified last component with
4276 * the original.
4278 pstrcpy(p+1, newname_last_component);
4283 * If the src and dest names are identical - including case,
4284 * don't do the rename, just return success.
4287 if (strcsequal(fsp->fsp_name, newname)) {
4288 DEBUG(3,("rename_internals_fsp: identical names in rename %s - returning success\n",
4289 newname));
4290 return NT_STATUS_OK;
4293 dest_exists = vfs_object_exist(conn,newname,NULL);
4295 if(!replace_if_exists && dest_exists) {
4296 DEBUG(3,("rename_internals_fsp: dest exists doing rename %s -> %s\n",
4297 fsp->fsp_name,newname));
4298 return NT_STATUS_OBJECT_NAME_COLLISION;
4301 error = can_rename(conn,newname,attrs,&sbuf);
4303 if (dest_exists && !NT_STATUS_IS_OK(error)) {
4304 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4305 nt_errstr(error), fsp->fsp_name,newname));
4306 if (NT_STATUS_EQUAL(error,NT_STATUS_SHARING_VIOLATION))
4307 error = NT_STATUS_ACCESS_DENIED;
4308 return error;
4311 if (rename_path_prefix_equal(fsp->fsp_name, newname)) {
4312 return NT_STATUS_ACCESS_DENIED;
4315 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
4317 if(SMB_VFS_RENAME(conn,fsp->fsp_name, newname) == 0) {
4318 DEBUG(3,("rename_internals_fsp: succeeded doing rename on %s -> %s\n",
4319 fsp->fsp_name,newname));
4320 rename_open_files(conn, lck, fsp->dev, fsp->inode, newname);
4321 TALLOC_FREE(lck);
4322 return NT_STATUS_OK;
4325 TALLOC_FREE(lck);
4327 if (errno == ENOTDIR || errno == EISDIR) {
4328 error = NT_STATUS_OBJECT_NAME_COLLISION;
4329 } else {
4330 error = map_nt_error_from_unix(errno);
4333 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
4334 nt_errstr(error), fsp->fsp_name,newname));
4336 return error;
4339 /****************************************************************************
4340 The guts of the rename command, split out so it may be called by the NT SMB
4341 code.
4342 ****************************************************************************/
4344 NTSTATUS rename_internals(connection_struct *conn, char *name, char *newname, uint32 attrs, BOOL replace_if_exists, BOOL has_wild)
4346 pstring directory;
4347 pstring mask;
4348 pstring last_component_src;
4349 pstring last_component_dest;
4350 char *p;
4351 BOOL bad_path_src = False;
4352 BOOL bad_path_dest = False;
4353 int count=0;
4354 NTSTATUS error = NT_STATUS_OK;
4355 BOOL rc = True;
4356 BOOL rcdest = True;
4357 SMB_STRUCT_STAT sbuf1, sbuf2;
4358 struct share_mode_lock *lck = NULL;
4360 *directory = *mask = 0;
4362 ZERO_STRUCT(sbuf1);
4363 ZERO_STRUCT(sbuf2);
4365 rc = unix_convert(name,conn,last_component_src,&bad_path_src,&sbuf1);
4366 if (!rc && bad_path_src) {
4367 if (ms_has_wild(last_component_src))
4368 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4369 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
4372 /* Quick check for "." and ".." */
4373 if (last_component_src[0] == '.') {
4374 if (!last_component_src[1] || (last_component_src[1] == '.' && !last_component_src[2])) {
4375 return NT_STATUS_OBJECT_NAME_INVALID;
4379 rcdest = unix_convert(newname,conn,last_component_dest,&bad_path_dest,&sbuf2);
4381 /* Quick check for "." and ".." */
4382 if (last_component_dest[0] == '.') {
4383 if (!last_component_dest[1] || (last_component_dest[1] == '.' && !last_component_dest[2])) {
4384 return NT_STATUS_OBJECT_NAME_INVALID;
4389 * Split the old name into directory and last component
4390 * strings. Note that unix_convert may have stripped off a
4391 * leading ./ from both name and newname if the rename is
4392 * at the root of the share. We need to make sure either both
4393 * name and newname contain a / character or neither of them do
4394 * as this is checked in resolve_wildcards().
4397 p = strrchr_m(name,'/');
4398 if (!p) {
4399 pstrcpy(directory,".");
4400 pstrcpy(mask,name);
4401 } else {
4402 *p = 0;
4403 pstrcpy(directory,name);
4404 pstrcpy(mask,p+1);
4405 *p = '/'; /* Replace needed for exceptional test below. */
4409 * We should only check the mangled cache
4410 * here if unix_convert failed. This means
4411 * that the path in 'mask' doesn't exist
4412 * on the file system and so we need to look
4413 * for a possible mangle. This patch from
4414 * Tine Smukavec <valentin.smukavec@hermes.si>.
4417 if (!rc && mangle_is_mangled(mask, conn->params))
4418 mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
4420 if (!has_wild) {
4422 * No wildcards - just process the one file.
4424 BOOL is_short_name = mangle_is_8_3(name, True, conn->params);
4426 /* Add a terminating '/' to the directory name. */
4427 pstrcat(directory,"/");
4428 pstrcat(directory,mask);
4430 /* Ensure newname contains a '/' also */
4431 if(strrchr_m(newname,'/') == 0) {
4432 pstring tmpstr;
4434 pstrcpy(tmpstr, "./");
4435 pstrcat(tmpstr, newname);
4436 pstrcpy(newname, tmpstr);
4439 DEBUG(3,("rename_internals: case_sensitive = %d, case_preserve = %d, short case preserve = %d, \
4440 directory = %s, newname = %s, last_component_dest = %s, is_8_3 = %d\n",
4441 conn->case_sensitive, conn->case_preserve, conn->short_case_preserve, directory,
4442 newname, last_component_dest, is_short_name));
4445 * Check for special case with case preserving and not
4446 * case sensitive, if directory and newname are identical,
4447 * and the old last component differs from the original
4448 * last component only by case, then we should allow
4449 * the rename (user is trying to change the case of the
4450 * filename).
4452 if((conn->case_sensitive == False) &&
4453 (((conn->case_preserve == True) &&
4454 (is_short_name == False)) ||
4455 ((conn->short_case_preserve == True) &&
4456 (is_short_name == True))) &&
4457 strcsequal(directory, newname)) {
4458 pstring modified_last_component;
4461 * Get the last component of the modified name.
4462 * Note that we guarantee that newname contains a '/'
4463 * character above.
4465 p = strrchr_m(newname,'/');
4466 pstrcpy(modified_last_component,p+1);
4468 if(strcsequal(modified_last_component,
4469 last_component_dest) == False) {
4471 * Replace the modified last component with
4472 * the original.
4474 pstrcpy(p+1, last_component_dest);
4478 resolve_wildcards(directory,newname);
4481 * The source object must exist.
4484 if (!vfs_object_exist(conn, directory, &sbuf1)) {
4485 DEBUG(3,("rename_internals: source doesn't exist doing rename %s -> %s\n",
4486 directory,newname));
4488 if (errno == ENOTDIR || errno == EISDIR || errno == ENOENT) {
4490 * Must return different errors depending on whether the parent
4491 * directory existed or not.
4494 p = strrchr_m(directory, '/');
4495 if (!p)
4496 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4497 *p = '\0';
4498 if (vfs_object_exist(conn, directory, NULL))
4499 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4500 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
4502 error = map_nt_error_from_unix(errno);
4503 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4504 nt_errstr(error), directory,newname));
4506 return error;
4509 if (!rcdest && bad_path_dest) {
4510 if (ms_has_wild(last_component_dest))
4511 return NT_STATUS_OBJECT_NAME_INVALID;
4512 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
4515 error = can_rename(conn,directory,attrs,&sbuf1);
4517 if (!NT_STATUS_IS_OK(error)) {
4518 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4519 nt_errstr(error), directory,newname));
4520 return error;
4524 * If the src and dest names are identical - including case,
4525 * don't do the rename, just return success.
4528 if (strcsequal(directory, newname)) {
4529 rename_open_files(conn, NULL, sbuf1.st_dev, sbuf1.st_ino, newname);
4530 DEBUG(3,("rename_internals: identical names in rename %s - returning success\n", directory));
4531 return NT_STATUS_OK;
4534 if(!replace_if_exists && vfs_object_exist(conn,newname,NULL)) {
4535 DEBUG(3,("rename_internals: dest exists doing rename %s -> %s\n",
4536 directory,newname));
4537 return NT_STATUS_OBJECT_NAME_COLLISION;
4540 if (rename_path_prefix_equal(directory, newname)) {
4541 return NT_STATUS_SHARING_VIOLATION;
4544 lck = get_share_mode_lock(NULL, sbuf1.st_dev, sbuf1.st_ino, NULL, NULL);
4546 if(SMB_VFS_RENAME(conn,directory, newname) == 0) {
4547 DEBUG(3,("rename_internals: succeeded doing rename on %s -> %s\n",
4548 directory,newname));
4549 rename_open_files(conn, lck, sbuf1.st_dev, sbuf1.st_ino, newname);
4550 TALLOC_FREE(lck);
4551 return NT_STATUS_OK;
4554 TALLOC_FREE(lck);
4555 if (errno == ENOTDIR || errno == EISDIR)
4556 error = NT_STATUS_OBJECT_NAME_COLLISION;
4557 else
4558 error = map_nt_error_from_unix(errno);
4560 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4561 nt_errstr(error), directory,newname));
4563 return error;
4564 } else {
4566 * Wildcards - process each file that matches.
4568 struct smb_Dir *dir_hnd = NULL;
4569 const char *dname;
4570 pstring destname;
4572 if (strequal(mask,"????????.???"))
4573 pstrcpy(mask,"*");
4575 if (check_name(directory,conn))
4576 dir_hnd = OpenDir(conn, directory, mask, attrs);
4578 if (dir_hnd) {
4579 long offset = 0;
4580 error = NT_STATUS_NO_SUCH_FILE;
4581 /* Was error = NT_STATUS_OBJECT_NAME_NOT_FOUND; - gentest fix. JRA */
4583 while ((dname = ReadDirName(dir_hnd, &offset))) {
4584 pstring fname;
4585 BOOL sysdir_entry = False;
4587 pstrcpy(fname,dname);
4589 /* Quick check for "." and ".." */
4590 if (fname[0] == '.') {
4591 if (!fname[1] || (fname[1] == '.' && !fname[2])) {
4592 if (attrs & aDIR) {
4593 sysdir_entry = True;
4594 } else {
4595 continue;
4600 if (!is_visible_file(conn, directory, dname, &sbuf1, False))
4601 continue;
4603 if(!mask_match(fname, mask, conn->case_sensitive))
4604 continue;
4606 if (sysdir_entry) {
4607 error = NT_STATUS_OBJECT_NAME_INVALID;
4608 break;
4611 error = NT_STATUS_ACCESS_DENIED;
4612 slprintf(fname,sizeof(fname)-1,"%s/%s",directory,dname);
4613 if (!vfs_object_exist(conn, fname, &sbuf1)) {
4614 error = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4615 DEBUG(6,("rename %s failed. Error %s\n", fname, nt_errstr(error)));
4616 continue;
4618 error = can_rename(conn,fname,attrs,&sbuf1);
4619 if (!NT_STATUS_IS_OK(error)) {
4620 DEBUG(6,("rename %s refused\n", fname));
4621 continue;
4623 pstrcpy(destname,newname);
4625 if (!resolve_wildcards(fname,destname)) {
4626 DEBUG(6,("resolve_wildcards %s %s failed\n",
4627 fname, destname));
4628 continue;
4631 if (strcsequal(fname,destname)) {
4632 rename_open_files(conn, NULL, sbuf1.st_dev, sbuf1.st_ino, newname);
4633 DEBUG(3,("rename_internals: identical names in wildcard rename %s - success\n", fname));
4634 count++;
4635 error = NT_STATUS_OK;
4636 continue;
4639 if (!replace_if_exists &&
4640 vfs_file_exist(conn,destname, NULL)) {
4641 DEBUG(6,("file_exist %s\n", destname));
4642 error = NT_STATUS_OBJECT_NAME_COLLISION;
4643 continue;
4646 if (rename_path_prefix_equal(fname, destname)) {
4647 return NT_STATUS_SHARING_VIOLATION;
4650 lck = get_share_mode_lock(NULL, sbuf1.st_dev, sbuf1.st_ino, NULL, NULL);
4652 if (!SMB_VFS_RENAME(conn,fname,destname)) {
4653 rename_open_files(conn, lck, sbuf1.st_dev, sbuf1.st_ino, newname);
4654 count++;
4655 error = NT_STATUS_OK;
4657 TALLOC_FREE(lck);
4658 DEBUG(3,("rename_internals: doing rename on %s -> %s\n",fname,destname));
4660 CloseDir(dir_hnd);
4663 if (!NT_STATUS_EQUAL(error,NT_STATUS_NO_SUCH_FILE)) {
4664 if (!rcdest && bad_path_dest) {
4665 if (ms_has_wild(last_component_dest))
4666 return NT_STATUS_OBJECT_NAME_INVALID;
4667 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
4672 if (count == 0 && NT_STATUS_IS_OK(error)) {
4673 error = map_nt_error_from_unix(errno);
4676 return error;
4679 /****************************************************************************
4680 Reply to a mv.
4681 ****************************************************************************/
4683 int reply_mv(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
4684 int dum_buffsize)
4686 int outsize = 0;
4687 pstring name;
4688 pstring newname;
4689 char *p;
4690 uint32 attrs = SVAL(inbuf,smb_vwv0);
4691 NTSTATUS status;
4692 BOOL path_contains_wcard = False;
4694 START_PROFILE(SMBmv);
4696 p = smb_buf(inbuf) + 1;
4697 p += srvstr_get_path_wcard(inbuf, name, p, sizeof(name), 0, STR_TERMINATE, &status, &path_contains_wcard);
4698 if (!NT_STATUS_IS_OK(status)) {
4699 END_PROFILE(SMBmv);
4700 return ERROR_NT(status);
4702 p++;
4703 p += srvstr_get_path(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status);
4704 if (!NT_STATUS_IS_OK(status)) {
4705 END_PROFILE(SMBmv);
4706 return ERROR_NT(status);
4709 RESOLVE_DFSPATH_WCARD(name, conn, inbuf, outbuf);
4710 RESOLVE_DFSPATH_WCARD(newname, conn, inbuf, outbuf);
4712 DEBUG(3,("reply_mv : %s -> %s\n",name,newname));
4714 status = rename_internals(conn, name, newname, attrs, False, path_contains_wcard);
4715 if (!NT_STATUS_IS_OK(status)) {
4716 END_PROFILE(SMBmv);
4717 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
4718 /* We have re-scheduled this call. */
4719 return -1;
4721 return ERROR_NT(status);
4725 * Win2k needs a changenotify request response before it will
4726 * update after a rename..
4728 process_pending_change_notify_queue((time_t)0);
4729 outsize = set_message(outbuf,0,0,False);
4731 END_PROFILE(SMBmv);
4732 return(outsize);
4735 /*******************************************************************
4736 Copy a file as part of a reply_copy.
4737 ******************************************************************/
4739 BOOL copy_file(char *src,char *dest1,connection_struct *conn, int ofun,
4740 int count,BOOL target_is_directory, int *err_ret)
4742 SMB_STRUCT_STAT src_sbuf, sbuf2;
4743 SMB_OFF_T ret=-1;
4744 files_struct *fsp1,*fsp2;
4745 pstring dest;
4746 uint32 dosattrs;
4747 uint32 new_create_disposition;
4748 NTSTATUS status;
4750 *err_ret = 0;
4752 pstrcpy(dest,dest1);
4753 if (target_is_directory) {
4754 char *p = strrchr_m(src,'/');
4755 if (p) {
4756 p++;
4757 } else {
4758 p = src;
4760 pstrcat(dest,"/");
4761 pstrcat(dest,p);
4764 if (!vfs_file_exist(conn,src,&src_sbuf)) {
4765 return(False);
4768 if (!target_is_directory && count) {
4769 new_create_disposition = FILE_OPEN;
4770 } else {
4771 if (!map_open_params_to_ntcreate(dest1,0,ofun,
4772 NULL, NULL, &new_create_disposition, NULL)) {
4773 return(False);
4777 status = open_file_ntcreate(conn,src,&src_sbuf,
4778 FILE_GENERIC_READ,
4779 FILE_SHARE_READ|FILE_SHARE_WRITE,
4780 FILE_OPEN,
4782 FILE_ATTRIBUTE_NORMAL,
4783 INTERNAL_OPEN_ONLY,
4784 NULL, &fsp1);
4786 if (!NT_STATUS_IS_OK(status)) {
4787 return(False);
4790 dosattrs = dos_mode(conn, src, &src_sbuf);
4791 if (SMB_VFS_STAT(conn,dest,&sbuf2) == -1) {
4792 ZERO_STRUCTP(&sbuf2);
4795 status = open_file_ntcreate(conn,dest,&sbuf2,
4796 FILE_GENERIC_WRITE,
4797 FILE_SHARE_READ|FILE_SHARE_WRITE,
4798 new_create_disposition,
4800 dosattrs,
4801 INTERNAL_OPEN_ONLY,
4802 NULL, &fsp2);
4804 if (!NT_STATUS_IS_OK(status)) {
4805 close_file(fsp1,ERROR_CLOSE);
4806 return(False);
4809 if ((ofun&3) == 1) {
4810 if(SMB_VFS_LSEEK(fsp2,fsp2->fh->fd,0,SEEK_END) == -1) {
4811 DEBUG(0,("copy_file: error - vfs lseek returned error %s\n", strerror(errno) ));
4813 * Stop the copy from occurring.
4815 ret = -1;
4816 src_sbuf.st_size = 0;
4820 if (src_sbuf.st_size) {
4821 ret = vfs_transfer_file(fsp1, fsp2, src_sbuf.st_size);
4824 close_file(fsp1,NORMAL_CLOSE);
4826 /* Ensure the modtime is set correctly on the destination file. */
4827 fsp_set_pending_modtime( fsp2, src_sbuf.st_mtime);
4830 * As we are opening fsp1 read-only we only expect
4831 * an error on close on fsp2 if we are out of space.
4832 * Thus we don't look at the error return from the
4833 * close of fsp1.
4835 *err_ret = close_file(fsp2,NORMAL_CLOSE);
4837 return(ret == (SMB_OFF_T)src_sbuf.st_size);
4840 /****************************************************************************
4841 Reply to a file copy.
4842 ****************************************************************************/
4844 int reply_copy(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
4846 int outsize = 0;
4847 pstring name;
4848 pstring directory;
4849 pstring mask,newname;
4850 char *p;
4851 int count=0;
4852 int error = ERRnoaccess;
4853 int err = 0;
4854 BOOL has_wild;
4855 BOOL exists=False;
4856 int tid2 = SVAL(inbuf,smb_vwv0);
4857 int ofun = SVAL(inbuf,smb_vwv1);
4858 int flags = SVAL(inbuf,smb_vwv2);
4859 BOOL target_is_directory=False;
4860 BOOL bad_path1 = False;
4861 BOOL bad_path2 = False;
4862 BOOL path_contains_wcard1 = False;
4863 BOOL path_contains_wcard2 = False;
4864 BOOL rc = True;
4865 SMB_STRUCT_STAT sbuf1, sbuf2;
4866 NTSTATUS status;
4867 START_PROFILE(SMBcopy);
4869 *directory = *mask = 0;
4871 p = smb_buf(inbuf);
4872 p += srvstr_get_path_wcard(inbuf, name, p, sizeof(name), 0, STR_TERMINATE, &status, &path_contains_wcard1);
4873 if (!NT_STATUS_IS_OK(status)) {
4874 END_PROFILE(SMBcopy);
4875 return ERROR_NT(status);
4877 p += srvstr_get_path_wcard(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status, &path_contains_wcard2);
4878 if (!NT_STATUS_IS_OK(status)) {
4879 END_PROFILE(SMBcopy);
4880 return ERROR_NT(status);
4883 DEBUG(3,("reply_copy : %s -> %s\n",name,newname));
4885 if (tid2 != conn->cnum) {
4886 /* can't currently handle inter share copies XXXX */
4887 DEBUG(3,("Rejecting inter-share copy\n"));
4888 END_PROFILE(SMBcopy);
4889 return ERROR_DOS(ERRSRV,ERRinvdevice);
4892 RESOLVE_DFSPATH_WCARD(name, conn, inbuf, outbuf);
4893 RESOLVE_DFSPATH_WCARD(newname, conn, inbuf, outbuf);
4895 rc = unix_convert(name,conn,0,&bad_path1,&sbuf1);
4896 unix_convert(newname,conn,0,&bad_path2,&sbuf2);
4898 target_is_directory = VALID_STAT_OF_DIR(sbuf2);
4900 if ((flags&1) && target_is_directory) {
4901 END_PROFILE(SMBcopy);
4902 return ERROR_DOS(ERRDOS,ERRbadfile);
4905 if ((flags&2) && !target_is_directory) {
4906 END_PROFILE(SMBcopy);
4907 return ERROR_DOS(ERRDOS,ERRbadpath);
4910 if ((flags&(1<<5)) && VALID_STAT_OF_DIR(sbuf1)) {
4911 /* wants a tree copy! XXXX */
4912 DEBUG(3,("Rejecting tree copy\n"));
4913 END_PROFILE(SMBcopy);
4914 return ERROR_DOS(ERRSRV,ERRerror);
4917 p = strrchr_m(name,'/');
4918 if (!p) {
4919 pstrcpy(directory,"./");
4920 pstrcpy(mask,name);
4921 } else {
4922 *p = 0;
4923 pstrcpy(directory,name);
4924 pstrcpy(mask,p+1);
4928 * We should only check the mangled cache
4929 * here if unix_convert failed. This means
4930 * that the path in 'mask' doesn't exist
4931 * on the file system and so we need to look
4932 * for a possible mangle. This patch from
4933 * Tine Smukavec <valentin.smukavec@hermes.si>.
4936 if (!rc && mangle_is_mangled(mask, conn->params))
4937 mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
4939 has_wild = path_contains_wcard1;
4941 if (!has_wild) {
4942 pstrcat(directory,"/");
4943 pstrcat(directory,mask);
4944 if (resolve_wildcards(directory,newname) &&
4945 copy_file(directory,newname,conn,ofun, count,target_is_directory,&err))
4946 count++;
4947 if(!count && err) {
4948 errno = err;
4949 END_PROFILE(SMBcopy);
4950 return(UNIXERROR(ERRHRD,ERRgeneral));
4952 if (!count) {
4953 exists = vfs_file_exist(conn,directory,NULL);
4955 } else {
4956 struct smb_Dir *dir_hnd = NULL;
4957 const char *dname;
4958 pstring destname;
4960 if (strequal(mask,"????????.???"))
4961 pstrcpy(mask,"*");
4963 if (check_name(directory,conn))
4964 dir_hnd = OpenDir(conn, directory, mask, 0);
4966 if (dir_hnd) {
4967 long offset = 0;
4968 error = ERRbadfile;
4970 while ((dname = ReadDirName(dir_hnd, &offset))) {
4971 pstring fname;
4972 pstrcpy(fname,dname);
4974 if (!is_visible_file(conn, directory, dname, &sbuf1, False))
4975 continue;
4977 if(!mask_match(fname, mask, conn->case_sensitive))
4978 continue;
4980 error = ERRnoaccess;
4981 slprintf(fname,sizeof(fname)-1, "%s/%s",directory,dname);
4982 pstrcpy(destname,newname);
4983 if (resolve_wildcards(fname,destname) &&
4984 copy_file(fname,destname,conn,ofun,
4985 count,target_is_directory,&err))
4986 count++;
4987 DEBUG(3,("reply_copy : doing copy on %s -> %s\n",fname,destname));
4989 CloseDir(dir_hnd);
4993 if (count == 0) {
4994 if(err) {
4995 /* Error on close... */
4996 errno = err;
4997 END_PROFILE(SMBcopy);
4998 return(UNIXERROR(ERRHRD,ERRgeneral));
5001 if (exists) {
5002 END_PROFILE(SMBcopy);
5003 return ERROR_DOS(ERRDOS,error);
5004 } else {
5005 if((errno == ENOENT) && (bad_path1 || bad_path2) &&
5006 !use_nt_status()) {
5007 /* Samba 3.0.22 has ERRDOS/ERRbadpath in the
5008 * DOS error code case
5010 return ERROR_DOS(ERRDOS, ERRbadpath);
5012 END_PROFILE(SMBcopy);
5013 return(UNIXERROR(ERRDOS,error));
5017 outsize = set_message(outbuf,1,0,True);
5018 SSVAL(outbuf,smb_vwv0,count);
5020 END_PROFILE(SMBcopy);
5021 return(outsize);
5024 /****************************************************************************
5025 Reply to a setdir.
5026 ****************************************************************************/
5028 int reply_setdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
5030 int snum;
5031 int outsize = 0;
5032 BOOL ok = False;
5033 pstring newdir;
5034 NTSTATUS status;
5036 START_PROFILE(pathworks_setdir);
5038 snum = SNUM(conn);
5039 if (!CAN_SETDIR(snum)) {
5040 END_PROFILE(pathworks_setdir);
5041 return ERROR_DOS(ERRDOS,ERRnoaccess);
5044 srvstr_get_path(inbuf, newdir, smb_buf(inbuf) + 1, sizeof(newdir), 0, STR_TERMINATE, &status);
5045 if (!NT_STATUS_IS_OK(status)) {
5046 END_PROFILE(pathworks_setdir);
5047 return ERROR_NT(status);
5050 RESOLVE_DFSPATH(newdir, conn, inbuf, outbuf);
5052 if (strlen(newdir) == 0) {
5053 ok = True;
5054 } else {
5055 ok = vfs_directory_exist(conn,newdir,NULL);
5056 if (ok)
5057 set_conn_connectpath(conn,newdir);
5060 if (!ok) {
5061 END_PROFILE(pathworks_setdir);
5062 return ERROR_DOS(ERRDOS,ERRbadpath);
5065 outsize = set_message(outbuf,0,0,False);
5066 SCVAL(outbuf,smb_reh,CVAL(inbuf,smb_reh));
5068 DEBUG(3,("setdir %s\n", newdir));
5070 END_PROFILE(pathworks_setdir);
5071 return(outsize);
5074 #undef DBGC_CLASS
5075 #define DBGC_CLASS DBGC_LOCKING
5077 /****************************************************************************
5078 Get a lock pid, dealing with large count requests.
5079 ****************************************************************************/
5081 uint32 get_lock_pid( char *data, int data_offset, BOOL large_file_format)
5083 if(!large_file_format)
5084 return (uint32)SVAL(data,SMB_LPID_OFFSET(data_offset));
5085 else
5086 return (uint32)SVAL(data,SMB_LARGE_LPID_OFFSET(data_offset));
5089 /****************************************************************************
5090 Get a lock count, dealing with large count requests.
5091 ****************************************************************************/
5093 SMB_BIG_UINT get_lock_count( char *data, int data_offset, BOOL large_file_format)
5095 SMB_BIG_UINT count = 0;
5097 if(!large_file_format) {
5098 count = (SMB_BIG_UINT)IVAL(data,SMB_LKLEN_OFFSET(data_offset));
5099 } else {
5101 #if defined(HAVE_LONGLONG)
5102 count = (((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset))) << 32) |
5103 ((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)));
5104 #else /* HAVE_LONGLONG */
5107 * NT4.x seems to be broken in that it sends large file (64 bit)
5108 * lockingX calls even if the CAP_LARGE_FILES was *not*
5109 * negotiated. For boxes without large unsigned ints truncate the
5110 * lock count by dropping the top 32 bits.
5113 if(IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)) != 0) {
5114 DEBUG(3,("get_lock_count: truncating lock count (high)0x%x (low)0x%x to just low count.\n",
5115 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)),
5116 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)) ));
5117 SIVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset),0);
5120 count = (SMB_BIG_UINT)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset));
5121 #endif /* HAVE_LONGLONG */
5124 return count;
5127 #if !defined(HAVE_LONGLONG)
5128 /****************************************************************************
5129 Pathetically try and map a 64 bit lock offset into 31 bits. I hate Windows :-).
5130 ****************************************************************************/
5132 static uint32 map_lock_offset(uint32 high, uint32 low)
5134 unsigned int i;
5135 uint32 mask = 0;
5136 uint32 highcopy = high;
5139 * Try and find out how many significant bits there are in high.
5142 for(i = 0; highcopy; i++)
5143 highcopy >>= 1;
5146 * We use 31 bits not 32 here as POSIX
5147 * lock offsets may not be negative.
5150 mask = (~0) << (31 - i);
5152 if(low & mask)
5153 return 0; /* Fail. */
5155 high <<= (31 - i);
5157 return (high|low);
5159 #endif /* !defined(HAVE_LONGLONG) */
5161 /****************************************************************************
5162 Get a lock offset, dealing with large offset requests.
5163 ****************************************************************************/
5165 SMB_BIG_UINT get_lock_offset( char *data, int data_offset, BOOL large_file_format, BOOL *err)
5167 SMB_BIG_UINT offset = 0;
5169 *err = False;
5171 if(!large_file_format) {
5172 offset = (SMB_BIG_UINT)IVAL(data,SMB_LKOFF_OFFSET(data_offset));
5173 } else {
5175 #if defined(HAVE_LONGLONG)
5176 offset = (((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset))) << 32) |
5177 ((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset)));
5178 #else /* HAVE_LONGLONG */
5181 * NT4.x seems to be broken in that it sends large file (64 bit)
5182 * lockingX calls even if the CAP_LARGE_FILES was *not*
5183 * negotiated. For boxes without large unsigned ints mangle the
5184 * lock offset by mapping the top 32 bits onto the lower 32.
5187 if(IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset)) != 0) {
5188 uint32 low = IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
5189 uint32 high = IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset));
5190 uint32 new_low = 0;
5192 if((new_low = map_lock_offset(high, low)) == 0) {
5193 *err = True;
5194 return (SMB_BIG_UINT)-1;
5197 DEBUG(3,("get_lock_offset: truncating lock offset (high)0x%x (low)0x%x to offset 0x%x.\n",
5198 (unsigned int)high, (unsigned int)low, (unsigned int)new_low ));
5199 SIVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset),0);
5200 SIVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset),new_low);
5203 offset = (SMB_BIG_UINT)IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
5204 #endif /* HAVE_LONGLONG */
5207 return offset;
5210 /****************************************************************************
5211 Reply to a lockingX request.
5212 ****************************************************************************/
5214 int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
5215 int length, int bufsize)
5217 files_struct *fsp = file_fsp(inbuf,smb_vwv2);
5218 unsigned char locktype = CVAL(inbuf,smb_vwv3);
5219 unsigned char oplocklevel = CVAL(inbuf,smb_vwv3+1);
5220 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
5221 uint16 num_locks = SVAL(inbuf,smb_vwv7);
5222 SMB_BIG_UINT count = 0, offset = 0;
5223 uint32 lock_pid;
5224 int32 lock_timeout = IVAL(inbuf,smb_vwv4);
5225 int i;
5226 char *data;
5227 BOOL large_file_format =
5228 (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
5229 BOOL err;
5230 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5232 START_PROFILE(SMBlockingX);
5234 CHECK_FSP(fsp,conn);
5236 data = smb_buf(inbuf);
5238 if (locktype & LOCKING_ANDX_CHANGE_LOCKTYPE) {
5239 /* we don't support these - and CANCEL_LOCK makes w2k
5240 and XP reboot so I don't really want to be
5241 compatible! (tridge) */
5242 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks));
5245 /* Check if this is an oplock break on a file
5246 we have granted an oplock on.
5248 if ((locktype & LOCKING_ANDX_OPLOCK_RELEASE)) {
5249 /* Client can insist on breaking to none. */
5250 BOOL break_to_none = (oplocklevel == 0);
5251 BOOL result;
5253 DEBUG(5,("reply_lockingX: oplock break reply (%u) from client "
5254 "for fnum = %d\n", (unsigned int)oplocklevel,
5255 fsp->fnum ));
5258 * Make sure we have granted an exclusive or batch oplock on
5259 * this file.
5262 if (fsp->oplock_type == 0) {
5264 /* The Samba4 nbench simulator doesn't understand
5265 the difference between break to level2 and break
5266 to none from level2 - it sends oplock break
5267 replies in both cases. Don't keep logging an error
5268 message here - just ignore it. JRA. */
5270 DEBUG(5,("reply_lockingX: Error : oplock break from "
5271 "client for fnum = %d (oplock=%d) and no "
5272 "oplock granted on this file (%s).\n",
5273 fsp->fnum, fsp->oplock_type, fsp->fsp_name));
5275 /* if this is a pure oplock break request then don't
5276 * send a reply */
5277 if (num_locks == 0 && num_ulocks == 0) {
5278 END_PROFILE(SMBlockingX);
5279 return -1;
5280 } else {
5281 END_PROFILE(SMBlockingX);
5282 return ERROR_DOS(ERRDOS,ERRlock);
5286 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
5287 (break_to_none)) {
5288 result = remove_oplock(fsp);
5289 } else {
5290 result = downgrade_oplock(fsp);
5293 if (!result) {
5294 DEBUG(0, ("reply_lockingX: error in removing "
5295 "oplock on file %s\n", fsp->fsp_name));
5296 /* Hmmm. Is this panic justified? */
5297 smb_panic("internal tdb error");
5300 reply_to_oplock_break_requests(fsp);
5302 /* if this is a pure oplock break request then don't send a
5303 * reply */
5304 if (num_locks == 0 && num_ulocks == 0) {
5305 /* Sanity check - ensure a pure oplock break is not a
5306 chained request. */
5307 if(CVAL(inbuf,smb_vwv0) != 0xff)
5308 DEBUG(0,("reply_lockingX: Error : pure oplock "
5309 "break is a chained %d request !\n",
5310 (unsigned int)CVAL(inbuf,smb_vwv0) ));
5311 END_PROFILE(SMBlockingX);
5312 return -1;
5317 * We do this check *after* we have checked this is not a oplock break
5318 * response message. JRA.
5321 release_level_2_oplocks_on_change(fsp);
5323 /* Data now points at the beginning of the list
5324 of smb_unlkrng structs */
5325 for(i = 0; i < (int)num_ulocks; i++) {
5326 lock_pid = get_lock_pid( data, i, large_file_format);
5327 count = get_lock_count( data, i, large_file_format);
5328 offset = get_lock_offset( data, i, large_file_format, &err);
5331 * There is no error code marked "stupid client bug".... :-).
5333 if(err) {
5334 END_PROFILE(SMBlockingX);
5335 return ERROR_DOS(ERRDOS,ERRnoaccess);
5338 DEBUG(10,("reply_lockingX: unlock start=%.0f, len=%.0f for "
5339 "pid %u, file %s\n", (double)offset, (double)count,
5340 (unsigned int)lock_pid, fsp->fsp_name ));
5342 status = do_unlock(fsp,
5343 lock_pid,
5344 count,
5345 offset,
5346 WINDOWS_LOCK);
5348 if (NT_STATUS_V(status)) {
5349 END_PROFILE(SMBlockingX);
5350 return ERROR_NT(status);
5354 /* Setup the timeout in seconds. */
5356 if (lp_blocking_locks(SNUM(conn))) {
5357 lock_timeout = ((lock_timeout == -1) ? -1 : (lock_timeout+999)/1000);
5358 } else {
5359 lock_timeout = 0;
5362 /* Now do any requested locks */
5363 data += ((large_file_format ? 20 : 10)*num_ulocks);
5365 /* Data now points at the beginning of the list
5366 of smb_lkrng structs */
5368 for(i = 0; i < (int)num_locks; i++) {
5369 enum brl_type lock_type = ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
5370 READ_LOCK:WRITE_LOCK);
5371 lock_pid = get_lock_pid( data, i, large_file_format);
5372 count = get_lock_count( data, i, large_file_format);
5373 offset = get_lock_offset( data, i, large_file_format, &err);
5376 * There is no error code marked "stupid client bug".... :-).
5378 if(err) {
5379 END_PROFILE(SMBlockingX);
5380 return ERROR_DOS(ERRDOS,ERRnoaccess);
5383 DEBUG(10,("reply_lockingX: lock start=%.0f, len=%.0f for pid "
5384 "%u, file %s timeout = %d\n", (double)offset,
5385 (double)count, (unsigned int)lock_pid,
5386 fsp->fsp_name, (int)lock_timeout ));
5388 if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
5389 if (lp_blocking_locks(SNUM(conn))) {
5391 /* Schedule a message to ourselves to
5392 remove the blocking lock record and
5393 return the right error. */
5395 if (!blocking_lock_cancel(fsp,
5396 lock_pid,
5397 offset,
5398 count,
5399 WINDOWS_LOCK,
5400 locktype,
5401 NT_STATUS_FILE_LOCK_CONFLICT)) {
5402 END_PROFILE(SMBlockingX);
5403 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRcancelviolation));
5406 /* Remove a matching pending lock. */
5407 status = do_lock_cancel(fsp,
5408 lock_pid,
5409 count,
5410 offset,
5411 WINDOWS_LOCK);
5412 } else {
5413 status = do_lock(fsp,
5414 lock_pid,
5415 count,
5416 offset,
5417 lock_type,
5418 WINDOWS_LOCK,
5419 lock_timeout);
5421 if (NT_STATUS_V(status)) {
5423 * Interesting fact found by IFSTEST /t
5424 * LockOverlappedTest... Even if it's our own lock
5425 * context, we need to wait here as there may be an
5426 * unlock on the way. JRA.
5428 if ((lock_timeout != 0) &&
5429 ERROR_WAS_LOCK_DENIED(status)) {
5431 * A blocking lock was requested. Package up
5432 * this smb into a queued request and push it
5433 * onto the blocking lock queue.
5435 if(push_blocking_lock_request(inbuf, length,
5436 fsp,
5437 lock_timeout,
5439 lock_pid,
5440 lock_type,
5441 WINDOWS_LOCK,
5442 offset,
5443 count)) {
5444 END_PROFILE(SMBlockingX);
5445 return -1;
5451 if (NT_STATUS_V(status)) {
5452 END_PROFILE(SMBlockingX);
5453 return ERROR_NT(status);
5457 /* If any of the above locks failed, then we must unlock
5458 all of the previous locks (X/Open spec). */
5460 if (!(locktype & LOCKING_ANDX_CANCEL_LOCK) &&
5461 (i != num_locks) &&
5462 (num_locks != 0)) {
5464 * Ensure we don't do a remove on the lock that just failed,
5465 * as under POSIX rules, if we have a lock already there, we
5466 * will delete it (and we shouldn't) .....
5468 for(i--; i >= 0; i--) {
5469 lock_pid = get_lock_pid( data, i, large_file_format);
5470 count = get_lock_count( data, i, large_file_format);
5471 offset = get_lock_offset( data, i, large_file_format,
5472 &err);
5475 * There is no error code marked "stupid client
5476 * bug".... :-).
5478 if(err) {
5479 END_PROFILE(SMBlockingX);
5480 return ERROR_DOS(ERRDOS,ERRnoaccess);
5483 do_unlock(fsp,
5484 lock_pid,
5485 count,
5486 offset,
5487 WINDOWS_LOCK);
5489 END_PROFILE(SMBlockingX);
5490 return ERROR_NT(status);
5493 set_message(outbuf,2,0,True);
5495 DEBUG(3, ("lockingX fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
5496 fsp->fnum, (unsigned int)locktype, num_locks, num_ulocks));
5498 END_PROFILE(SMBlockingX);
5499 return chain_reply(inbuf,outbuf,length,bufsize);
5502 #undef DBGC_CLASS
5503 #define DBGC_CLASS DBGC_ALL
5505 /****************************************************************************
5506 Reply to a SMBreadbmpx (read block multiplex) request.
5507 ****************************************************************************/
5509 int reply_readbmpx(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
5511 ssize_t nread = -1;
5512 ssize_t total_read;
5513 char *data;
5514 SMB_OFF_T startpos;
5515 int outsize;
5516 size_t maxcount;
5517 int max_per_packet;
5518 size_t tcount;
5519 int pad;
5520 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5521 START_PROFILE(SMBreadBmpx);
5523 /* this function doesn't seem to work - disable by default */
5524 if (!lp_readbmpx()) {
5525 END_PROFILE(SMBreadBmpx);
5526 return ERROR_DOS(ERRSRV,ERRuseSTD);
5529 outsize = set_message(outbuf,8,0,True);
5531 CHECK_FSP(fsp,conn);
5532 if (!CHECK_READ(fsp,inbuf)) {
5533 return(ERROR_DOS(ERRDOS,ERRbadaccess));
5536 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
5537 maxcount = SVAL(inbuf,smb_vwv3);
5539 data = smb_buf(outbuf);
5540 pad = ((long)data)%4;
5541 if (pad)
5542 pad = 4 - pad;
5543 data += pad;
5545 max_per_packet = bufsize-(outsize+pad);
5546 tcount = maxcount;
5547 total_read = 0;
5549 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
5550 END_PROFILE(SMBreadBmpx);
5551 return ERROR_DOS(ERRDOS,ERRlock);
5554 do {
5555 size_t N = MIN(max_per_packet,tcount-total_read);
5557 nread = read_file(fsp,data,startpos,N);
5559 if (nread <= 0)
5560 nread = 0;
5562 if (nread < (ssize_t)N)
5563 tcount = total_read + nread;
5565 set_message(outbuf,8,nread+pad,False);
5566 SIVAL(outbuf,smb_vwv0,startpos);
5567 SSVAL(outbuf,smb_vwv2,tcount);
5568 SSVAL(outbuf,smb_vwv6,nread);
5569 SSVAL(outbuf,smb_vwv7,smb_offset(data,outbuf));
5571 show_msg(outbuf);
5572 if (!send_smb(smbd_server_fd(),outbuf))
5573 exit_server("reply_readbmpx: send_smb failed.");
5575 total_read += nread;
5576 startpos += nread;
5577 } while (total_read < (ssize_t)tcount);
5579 END_PROFILE(SMBreadBmpx);
5580 return(-1);
5583 /****************************************************************************
5584 Reply to a SMBsetattrE.
5585 ****************************************************************************/
5587 int reply_setattrE(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
5589 struct utimbuf unix_times;
5590 int outsize = 0;
5591 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5592 START_PROFILE(SMBsetattrE);
5594 outsize = set_message(outbuf,0,0,False);
5596 if(!fsp || (fsp->conn != conn)) {
5597 END_PROFILE(SMBsetattrE);
5598 return ERROR_DOS(ERRDOS,ERRbadfid);
5602 * Convert the DOS times into unix times. Ignore create
5603 * time as UNIX can't set this.
5606 unix_times.actime = srv_make_unix_date2(inbuf+smb_vwv3);
5607 unix_times.modtime = srv_make_unix_date2(inbuf+smb_vwv5);
5610 * Patch from Ray Frush <frush@engr.colostate.edu>
5611 * Sometimes times are sent as zero - ignore them.
5614 if (null_mtime(unix_times.actime) && null_mtime(unix_times.modtime)) {
5615 /* Ignore request */
5616 if( DEBUGLVL( 3 ) ) {
5617 dbgtext( "reply_setattrE fnum=%d ", fsp->fnum);
5618 dbgtext( "ignoring zero request - not setting timestamps of 0\n" );
5620 END_PROFILE(SMBsetattrE);
5621 return(outsize);
5622 } else if (!null_mtime(unix_times.actime) && null_mtime(unix_times.modtime)) {
5623 /* set modify time = to access time if modify time was unset */
5624 unix_times.modtime = unix_times.actime;
5627 /* Set the date on this file */
5628 /* Should we set pending modtime here ? JRA */
5629 if(file_utime(conn, fsp->fsp_name, &unix_times)) {
5630 END_PROFILE(SMBsetattrE);
5631 return ERROR_DOS(ERRDOS,ERRnoaccess);
5634 DEBUG( 3, ( "reply_setattrE fnum=%d actime=%d modtime=%d\n",
5635 fsp->fnum, (int)unix_times.actime, (int)unix_times.modtime ) );
5637 END_PROFILE(SMBsetattrE);
5638 return(outsize);
5642 /* Back from the dead for OS/2..... JRA. */
5644 /****************************************************************************
5645 Reply to a SMBwritebmpx (write block multiplex primary) request.
5646 ****************************************************************************/
5648 int reply_writebmpx(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
5650 size_t numtowrite;
5651 ssize_t nwritten = -1;
5652 int outsize = 0;
5653 SMB_OFF_T startpos;
5654 size_t tcount;
5655 BOOL write_through;
5656 int smb_doff;
5657 char *data;
5658 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5659 START_PROFILE(SMBwriteBmpx);
5661 CHECK_FSP(fsp,conn);
5662 if (!CHECK_WRITE(fsp)) {
5663 return(ERROR_DOS(ERRDOS,ERRbadaccess));
5665 if (HAS_CACHED_ERROR(fsp)) {
5666 return(CACHED_ERROR(fsp));
5669 tcount = SVAL(inbuf,smb_vwv1);
5670 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
5671 write_through = BITSETW(inbuf+smb_vwv7,0);
5672 numtowrite = SVAL(inbuf,smb_vwv10);
5673 smb_doff = SVAL(inbuf,smb_vwv11);
5675 data = smb_base(inbuf) + smb_doff;
5677 /* If this fails we need to send an SMBwriteC response,
5678 not an SMBwritebmpx - set this up now so we don't forget */
5679 SCVAL(outbuf,smb_com,SMBwritec);
5681 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos,WRITE_LOCK)) {
5682 END_PROFILE(SMBwriteBmpx);
5683 return(ERROR_DOS(ERRDOS,ERRlock));
5686 nwritten = write_file(fsp,data,startpos,numtowrite);
5688 sync_file(conn, fsp, write_through);
5690 if(nwritten < (ssize_t)numtowrite) {
5691 END_PROFILE(SMBwriteBmpx);
5692 return(UNIXERROR(ERRHRD,ERRdiskfull));
5695 /* If the maximum to be written to this file
5696 is greater than what we just wrote then set
5697 up a secondary struct to be attached to this
5698 fd, we will use this to cache error messages etc. */
5700 if((ssize_t)tcount > nwritten) {
5701 write_bmpx_struct *wbms;
5702 if(fsp->wbmpx_ptr != NULL)
5703 wbms = fsp->wbmpx_ptr; /* Use an existing struct */
5704 else
5705 wbms = SMB_MALLOC_P(write_bmpx_struct);
5706 if(!wbms) {
5707 DEBUG(0,("Out of memory in reply_readmpx\n"));
5708 END_PROFILE(SMBwriteBmpx);
5709 return(ERROR_DOS(ERRSRV,ERRnoresource));
5711 wbms->wr_mode = write_through;
5712 wbms->wr_discard = False; /* No errors yet */
5713 wbms->wr_total_written = nwritten;
5714 wbms->wr_errclass = 0;
5715 wbms->wr_error = 0;
5716 fsp->wbmpx_ptr = wbms;
5719 /* We are returning successfully, set the message type back to
5720 SMBwritebmpx */
5721 SCVAL(outbuf,smb_com,SMBwriteBmpx);
5723 outsize = set_message(outbuf,1,0,True);
5725 SSVALS(outbuf,smb_vwv0,-1); /* We don't support smb_remaining */
5727 DEBUG( 3, ( "writebmpx fnum=%d num=%d wrote=%d\n",
5728 fsp->fnum, (int)numtowrite, (int)nwritten ) );
5730 if (write_through && tcount==nwritten) {
5731 /* We need to send both a primary and a secondary response */
5732 smb_setlen(outbuf,outsize - 4);
5733 show_msg(outbuf);
5734 if (!send_smb(smbd_server_fd(),outbuf))
5735 exit_server("reply_writebmpx: send_smb failed.");
5737 /* Now the secondary */
5738 outsize = set_message(outbuf,1,0,True);
5739 SCVAL(outbuf,smb_com,SMBwritec);
5740 SSVAL(outbuf,smb_vwv0,nwritten);
5743 END_PROFILE(SMBwriteBmpx);
5744 return(outsize);
5747 /****************************************************************************
5748 Reply to a SMBwritebs (write block multiplex secondary) request.
5749 ****************************************************************************/
5751 int reply_writebs(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
5753 size_t numtowrite;
5754 ssize_t nwritten = -1;
5755 int outsize = 0;
5756 SMB_OFF_T startpos;
5757 size_t tcount;
5758 BOOL write_through;
5759 int smb_doff;
5760 char *data;
5761 write_bmpx_struct *wbms;
5762 BOOL send_response = False;
5763 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5764 START_PROFILE(SMBwriteBs);
5766 CHECK_FSP(fsp,conn);
5767 if (!CHECK_WRITE(fsp)) {
5768 return(ERROR_DOS(ERRDOS,ERRbadaccess));
5771 tcount = SVAL(inbuf,smb_vwv1);
5772 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
5773 numtowrite = SVAL(inbuf,smb_vwv6);
5774 smb_doff = SVAL(inbuf,smb_vwv7);
5776 data = smb_base(inbuf) + smb_doff;
5778 /* We need to send an SMBwriteC response, not an SMBwritebs */
5779 SCVAL(outbuf,smb_com,SMBwritec);
5781 /* This fd should have an auxiliary struct attached,
5782 check that it does */
5783 wbms = fsp->wbmpx_ptr;
5784 if(!wbms) {
5785 END_PROFILE(SMBwriteBs);
5786 return(-1);
5789 /* If write through is set we can return errors, else we must cache them */
5790 write_through = wbms->wr_mode;
5792 /* Check for an earlier error */
5793 if(wbms->wr_discard) {
5794 END_PROFILE(SMBwriteBs);
5795 return -1; /* Just discard the packet */
5798 nwritten = write_file(fsp,data,startpos,numtowrite);
5800 sync_file(conn, fsp, write_through);
5802 if (nwritten < (ssize_t)numtowrite) {
5803 if(write_through) {
5804 /* We are returning an error - we can delete the aux struct */
5805 if (wbms)
5806 free((char *)wbms);
5807 fsp->wbmpx_ptr = NULL;
5808 END_PROFILE(SMBwriteBs);
5809 return(ERROR_DOS(ERRHRD,ERRdiskfull));
5811 wbms->wr_errclass = ERRHRD;
5812 wbms->wr_error = ERRdiskfull;
5813 wbms->wr_status = NT_STATUS_DISK_FULL;
5814 wbms->wr_discard = True;
5815 END_PROFILE(SMBwriteBs);
5816 return -1;
5819 /* Increment the total written, if this matches tcount
5820 we can discard the auxiliary struct (hurrah !) and return a writeC */
5821 wbms->wr_total_written += nwritten;
5822 if(wbms->wr_total_written >= tcount) {
5823 if (write_through) {
5824 outsize = set_message(outbuf,1,0,True);
5825 SSVAL(outbuf,smb_vwv0,wbms->wr_total_written);
5826 send_response = True;
5829 free((char *)wbms);
5830 fsp->wbmpx_ptr = NULL;
5833 if(send_response) {
5834 END_PROFILE(SMBwriteBs);
5835 return(outsize);
5838 END_PROFILE(SMBwriteBs);
5839 return(-1);
5842 /****************************************************************************
5843 Reply to a SMBgetattrE.
5844 ****************************************************************************/
5846 int reply_getattrE(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
5848 SMB_STRUCT_STAT sbuf;
5849 int outsize = 0;
5850 int mode;
5851 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5852 START_PROFILE(SMBgetattrE);
5854 outsize = set_message(outbuf,11,0,True);
5856 if(!fsp || (fsp->conn != conn)) {
5857 END_PROFILE(SMBgetattrE);
5858 return ERROR_DOS(ERRDOS,ERRbadfid);
5861 /* Do an fstat on this file */
5862 if(fsp_stat(fsp, &sbuf)) {
5863 END_PROFILE(SMBgetattrE);
5864 return(UNIXERROR(ERRDOS,ERRnoaccess));
5867 mode = dos_mode(conn,fsp->fsp_name,&sbuf);
5870 * Convert the times into dos times. Set create
5871 * date to be last modify date as UNIX doesn't save
5872 * this.
5875 srv_put_dos_date2(outbuf,smb_vwv0,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
5876 srv_put_dos_date2(outbuf,smb_vwv2,sbuf.st_atime);
5877 /* Should we check pending modtime here ? JRA */
5878 srv_put_dos_date2(outbuf,smb_vwv4,sbuf.st_mtime);
5880 if (mode & aDIR) {
5881 SIVAL(outbuf,smb_vwv6,0);
5882 SIVAL(outbuf,smb_vwv8,0);
5883 } else {
5884 uint32 allocation_size = get_allocation_size(conn,fsp, &sbuf);
5885 SIVAL(outbuf,smb_vwv6,(uint32)sbuf.st_size);
5886 SIVAL(outbuf,smb_vwv8,allocation_size);
5888 SSVAL(outbuf,smb_vwv10, mode);
5890 DEBUG( 3, ( "reply_getattrE fnum=%d\n", fsp->fnum));
5892 END_PROFILE(SMBgetattrE);
5893 return(outsize);