2 Unix SMB/CIFS implementation.
3 Main SMB reply routines
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Andrew Bartlett 2001
6 Copyright (C) Jeremy Allison 1992-2007.
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
29 /* look in server.c for some explanation of these variables */
30 extern enum protocol_types Protocol;
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 a findfirst/findnext
41 path or anything including wildcards.
42 We're assuming here that '/' is not the second byte in any multibyte char
43 set (a safe assumption). '\\' *may* be the second byte in a multibyte char
45 ****************************************************************************/
47 NTSTATUS check_path_syntax_internal(pstring destname,
48 const pstring srcname,
50 BOOL *p_last_component_contains_wcard)
53 const char *s = srcname;
54 NTSTATUS ret = NT_STATUS_OK;
55 BOOL start_of_name_component = True;
57 *p_last_component_contains_wcard = False;
60 if (IS_DIRECTORY_SEP(*s)) {
62 * Safe to assume is not the second part of a mb char
63 * as this is handled below.
65 /* Eat multiple '/' or '\\' */
66 while (IS_DIRECTORY_SEP(*s)) {
69 if ((d != destname) && (*s != '\0')) {
70 /* We only care about non-leading or trailing '/' or '\\' */
74 start_of_name_component = True;
76 *p_last_component_contains_wcard = False;
80 if (start_of_name_component) {
81 if ((s[0] == '.') && (s[1] == '.') && (IS_DIRECTORY_SEP(s[2]) || s[2] == '\0')) {
82 /* Uh oh - "/../" or "\\..\\" or "/..\0" or "\\..\0" ! */
85 * No mb char starts with '.' so we're safe checking the directory separator here.
88 /* If we just added a '/' - delete it */
89 if ((d > destname) && (*(d-1) == '/')) {
94 /* Are we at the start ? Can't go back further if so. */
96 ret = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
99 /* Go back one level... */
100 /* We know this is safe as '/' cannot be part of a mb sequence. */
101 /* NOTE - if this assumption is invalid we are not in good shape... */
102 /* Decrement d first as d points to the *next* char to write into. */
103 for (d--; d > destname; d--) {
107 s += 2; /* Else go past the .. */
108 /* We're still at the start of a name component, just the previous one. */
111 } else if ((s[0] == '.') && ((s[1] == '\0') || IS_DIRECTORY_SEP(s[1]))) {
124 return NT_STATUS_OBJECT_NAME_INVALID;
132 *p_last_component_contains_wcard = True;
141 /* Get the size of the next MB character. */
142 next_codepoint(s,&siz);
160 DEBUG(0,("check_path_syntax_internal: character length assumptions invalid !\n"));
162 return NT_STATUS_INVALID_PARAMETER;
165 start_of_name_component = False;
172 /****************************************************************************
173 Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
174 No wildcards allowed.
175 ****************************************************************************/
177 NTSTATUS check_path_syntax(pstring destname, const pstring srcname)
180 return check_path_syntax_internal(destname, srcname, True, &ignore);
183 /****************************************************************************
184 Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
185 Wildcards allowed - p_contains_wcard returns true if the last component contained
187 ****************************************************************************/
189 NTSTATUS check_path_syntax_wcard(pstring destname, const pstring srcname, BOOL *p_contains_wcard)
191 return check_path_syntax_internal(destname, srcname, True, p_contains_wcard);
194 /****************************************************************************
195 Check the path for a POSIX client.
196 We're assuming here that '/' is not the second byte in any multibyte char
197 set (a safe assumption).
198 ****************************************************************************/
200 static NTSTATUS check_path_syntax_posix(pstring destname, const pstring srcname)
203 return check_path_syntax_internal(destname, srcname, False, &ignore);
206 /****************************************************************************
207 Pull a string and check the path allowing a wilcard - provide for error return.
208 ****************************************************************************/
210 size_t srvstr_get_path_wcard(char *inbuf, char *dest, const char *src, size_t dest_len, size_t src_len, int flags,
211 NTSTATUS *err, BOOL *contains_wcard)
214 char *tmppath_ptr = tmppath;
217 SMB_ASSERT(dest_len == sizeof(pstring));
221 ret = srvstr_pull_buf( inbuf, tmppath_ptr, src, dest_len, flags);
223 ret = srvstr_pull( inbuf, tmppath_ptr, src, dest_len, src_len, flags);
226 *contains_wcard = False;
228 if (lp_posix_pathnames()) {
229 *err = check_path_syntax_posix(dest, tmppath);
231 *err = check_path_syntax_wcard(dest, tmppath, contains_wcard);
237 /****************************************************************************
238 Pull a string and check the path - provide for error return.
239 ****************************************************************************/
241 size_t srvstr_get_path(char *inbuf, char *dest, const char *src, size_t dest_len, size_t src_len, int flags, NTSTATUS *err)
244 char *tmppath_ptr = tmppath;
247 SMB_ASSERT(dest_len == sizeof(pstring));
251 ret = srvstr_pull_buf( inbuf, tmppath_ptr, src, dest_len, flags);
253 ret = srvstr_pull( inbuf, tmppath_ptr, src, dest_len, src_len, flags);
255 if (lp_posix_pathnames()) {
256 *err = check_path_syntax_posix(dest, tmppath);
258 *err = check_path_syntax(dest, tmppath);
264 /****************************************************************************
265 Reply to a special message.
266 ****************************************************************************/
268 int reply_special(char *inbuf,char *outbuf)
271 int msg_type = CVAL(inbuf,0);
272 int msg_flags = CVAL(inbuf,1);
276 static BOOL already_got_session = False;
280 memset(outbuf,'\0',smb_size);
282 smb_setlen(outbuf,0);
285 case 0x81: /* session request */
287 if (already_got_session) {
288 exit_server_cleanly("multiple session request not permitted");
291 SCVAL(outbuf,0,0x82);
293 if (name_len(inbuf+4) > 50 ||
294 name_len(inbuf+4 + name_len(inbuf + 4)) > 50) {
295 DEBUG(0,("Invalid name length in session request\n"));
298 name_extract(inbuf,4,name1);
299 name_type = name_extract(inbuf,4 + name_len(inbuf + 4),name2);
300 DEBUG(2,("netbios connect: name1=%s name2=%s\n",
303 set_local_machine_name(name1, True);
304 set_remote_machine_name(name2, True);
306 DEBUG(2,("netbios connect: local=%s remote=%s, name type = %x\n",
307 get_local_machine_name(), get_remote_machine_name(),
310 if (name_type == 'R') {
311 /* We are being asked for a pathworks session ---
313 SCVAL(outbuf, 0,0x83);
317 /* only add the client's machine name to the list
318 of possibly valid usernames if we are operating
319 in share mode security */
320 if (lp_security() == SEC_SHARE) {
321 add_session_user(get_remote_machine_name());
324 reload_services(True);
327 already_got_session = True;
330 case 0x89: /* session keepalive request
331 (some old clients produce this?) */
332 SCVAL(outbuf,0,SMBkeepalive);
336 case 0x82: /* positive session response */
337 case 0x83: /* negative session response */
338 case 0x84: /* retarget session response */
339 DEBUG(0,("Unexpected session response\n"));
342 case SMBkeepalive: /* session keepalive */
347 DEBUG(5,("init msg_type=0x%x msg_flags=0x%x\n",
348 msg_type, msg_flags));
353 /****************************************************************************
355 conn POINTER CAN BE NULL HERE !
356 ****************************************************************************/
358 int reply_tcon(connection_struct *conn,
359 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
366 uint16 vuid = SVAL(inbuf,smb_uid);
370 DATA_BLOB password_blob;
372 START_PROFILE(SMBtcon);
374 *service_buf = *password = *dev = 0;
376 p = smb_buf(inbuf)+1;
377 p += srvstr_pull_buf(inbuf, service_buf, p, sizeof(service_buf), STR_TERMINATE) + 1;
378 pwlen = srvstr_pull_buf(inbuf, password, p, sizeof(password), STR_TERMINATE) + 1;
380 p += srvstr_pull_buf(inbuf, dev, p, sizeof(dev), STR_TERMINATE) + 1;
382 p = strrchr_m(service_buf,'\\');
386 service = service_buf;
389 password_blob = data_blob(password, pwlen+1);
391 conn = make_connection(service,password_blob,dev,vuid,&nt_status);
393 data_blob_clear_free(&password_blob);
396 END_PROFILE(SMBtcon);
397 return ERROR_NT(nt_status);
400 outsize = set_message(outbuf,2,0,True);
401 SSVAL(outbuf,smb_vwv0,max_recv);
402 SSVAL(outbuf,smb_vwv1,conn->cnum);
403 SSVAL(outbuf,smb_tid,conn->cnum);
405 DEBUG(3,("tcon service=%s cnum=%d\n",
406 service, conn->cnum));
408 END_PROFILE(SMBtcon);
412 /****************************************************************************
413 Reply to a tcon and X.
414 conn POINTER CAN BE NULL HERE !
415 ****************************************************************************/
417 int reply_tcon_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
422 /* what the cleint thinks the device is */
423 fstring client_devicetype;
424 /* what the server tells the client the share represents */
425 const char *server_devicetype;
427 uint16 vuid = SVAL(inbuf,smb_uid);
428 int passlen = SVAL(inbuf,smb_vwv3);
432 START_PROFILE(SMBtconX);
434 *service = *client_devicetype = 0;
436 /* we might have to close an old one */
437 if ((SVAL(inbuf,smb_vwv2) & 0x1) && conn) {
438 close_cnum(conn,vuid);
441 if (passlen > MAX_PASS_LEN) {
442 return ERROR_DOS(ERRDOS,ERRbuftoosmall);
445 if (global_encrypted_passwords_negotiated) {
446 password = data_blob(smb_buf(inbuf),passlen);
448 password = data_blob(smb_buf(inbuf),passlen+1);
449 /* Ensure correct termination */
450 password.data[passlen]=0;
453 p = smb_buf(inbuf) + passlen;
454 p += srvstr_pull_buf(inbuf, path, p, sizeof(path), STR_TERMINATE);
457 * the service name can be either: \\server\share
458 * or share directly like on the DELL PowerVault 705
461 q = strchr_m(path+2,'\\');
463 END_PROFILE(SMBtconX);
464 return(ERROR_DOS(ERRDOS,ERRnosuchshare));
466 fstrcpy(service,q+1);
469 fstrcpy(service,path);
471 p += srvstr_pull(inbuf, client_devicetype, p, sizeof(client_devicetype), 6, STR_ASCII);
473 DEBUG(4,("Client requested device type [%s] for share [%s]\n", client_devicetype, service));
475 conn = make_connection(service,password,client_devicetype,vuid,&nt_status);
477 data_blob_clear_free(&password);
480 END_PROFILE(SMBtconX);
481 return ERROR_NT(nt_status);
485 server_devicetype = "IPC";
486 else if ( IS_PRINT(conn) )
487 server_devicetype = "LPT1:";
489 server_devicetype = "A:";
491 if (Protocol < PROTOCOL_NT1) {
492 set_message(outbuf,2,0,True);
494 p += srvstr_push(outbuf, p, server_devicetype, -1,
495 STR_TERMINATE|STR_ASCII);
496 set_message_end(outbuf,p);
498 /* NT sets the fstype of IPC$ to the null string */
499 const char *fstype = IS_IPC(conn) ? "" : lp_fstype(SNUM(conn));
501 set_message(outbuf,3,0,True);
504 p += srvstr_push(outbuf, p, server_devicetype, -1,
505 STR_TERMINATE|STR_ASCII);
506 p += srvstr_push(outbuf, p, fstype, -1,
509 set_message_end(outbuf,p);
511 /* what does setting this bit do? It is set by NT4 and
512 may affect the ability to autorun mounted cdroms */
513 SSVAL(outbuf, smb_vwv2, SMB_SUPPORT_SEARCH_BITS|
514 (lp_csc_policy(SNUM(conn)) << 2));
516 init_dfsroot(conn, inbuf, outbuf);
520 DEBUG(3,("tconX service=%s \n",
523 /* set the incoming and outgoing tid to the just created one */
524 SSVAL(inbuf,smb_tid,conn->cnum);
525 SSVAL(outbuf,smb_tid,conn->cnum);
527 END_PROFILE(SMBtconX);
528 return chain_reply(inbuf,outbuf,length,bufsize);
531 /****************************************************************************
532 Reply to an unknown type.
533 ****************************************************************************/
535 int reply_unknown(char *inbuf,char *outbuf)
538 type = CVAL(inbuf,smb_com);
540 DEBUG(0,("unknown command type (%s): type=%d (0x%X)\n",
541 smb_fn_name(type), type, type));
543 return(ERROR_DOS(ERRSRV,ERRunknownsmb));
546 /****************************************************************************
548 conn POINTER CAN BE NULL HERE !
549 ****************************************************************************/
551 int reply_ioctl(connection_struct *conn,
552 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
554 uint16 device = SVAL(inbuf,smb_vwv1);
555 uint16 function = SVAL(inbuf,smb_vwv2);
556 uint32 ioctl_code = (device << 16) + function;
557 int replysize, outsize;
559 START_PROFILE(SMBioctl);
561 DEBUG(4, ("Received IOCTL (code 0x%x)\n", ioctl_code));
563 switch (ioctl_code) {
564 case IOCTL_QUERY_JOB_INFO:
568 END_PROFILE(SMBioctl);
569 return(ERROR_DOS(ERRSRV,ERRnosupport));
572 outsize = set_message(outbuf,8,replysize+1,True);
573 SSVAL(outbuf,smb_vwv1,replysize); /* Total data bytes returned */
574 SSVAL(outbuf,smb_vwv5,replysize); /* Data bytes this buffer */
575 SSVAL(outbuf,smb_vwv6,52); /* Offset to data */
576 p = smb_buf(outbuf) + 1; /* Allow for alignment */
578 switch (ioctl_code) {
579 case IOCTL_QUERY_JOB_INFO:
581 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
583 END_PROFILE(SMBioctl);
584 return(UNIXERROR(ERRDOS,ERRbadfid));
586 SSVAL(p,0,fsp->rap_print_jobid); /* Job number */
587 srvstr_push(outbuf, p+2, global_myname(), 15, STR_TERMINATE|STR_ASCII);
589 srvstr_push(outbuf, p+18, lp_servicename(SNUM(conn)), 13, STR_TERMINATE|STR_ASCII);
595 END_PROFILE(SMBioctl);
599 /****************************************************************************
600 Strange checkpath NTSTATUS mapping.
601 ****************************************************************************/
603 static NTSTATUS map_checkpath_error(const char *inbuf, NTSTATUS status)
605 /* Strange DOS error code semantics only for checkpath... */
606 if (!(SVAL(inbuf,smb_flg2) & FLAGS2_32_BIT_ERROR_CODES)) {
607 if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_INVALID,status)) {
608 /* We need to map to ERRbadpath */
609 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
615 /****************************************************************************
616 Reply to a checkpath.
617 ****************************************************************************/
619 int reply_checkpath(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
623 SMB_STRUCT_STAT sbuf;
626 START_PROFILE(SMBcheckpath);
628 srvstr_get_path(inbuf, name, smb_buf(inbuf) + 1, sizeof(name), 0, STR_TERMINATE, &status);
629 if (!NT_STATUS_IS_OK(status)) {
630 END_PROFILE(SMBcheckpath);
631 status = map_checkpath_error(inbuf, status);
632 return ERROR_NT(status);
635 RESOLVE_DFSPATH(name, conn, inbuf, outbuf);
637 DEBUG(3,("reply_checkpath %s mode=%d\n", name, (int)SVAL(inbuf,smb_vwv0)));
639 status = unix_convert(conn, name, False, NULL, &sbuf);
640 if (!NT_STATUS_IS_OK(status)) {
641 END_PROFILE(SMBcheckpath);
642 status = map_checkpath_error(inbuf, status);
643 return ERROR_NT(status);
646 if (check_name(name,conn) && (VALID_STAT(sbuf) || SMB_VFS_STAT(conn,name,&sbuf) == 0)) {
647 if (!S_ISDIR(sbuf.st_mode)) {
648 END_PROFILE(SMBcheckpath);
649 return ERROR_BOTH(NT_STATUS_NOT_A_DIRECTORY,ERRDOS,ERRbadpath);
652 /* We special case this - as when a Windows machine
653 is parsing a path is steps through the components
654 one at a time - if a component fails it expects
655 ERRbadpath, not ERRbadfile.
657 if(errno == ENOENT) {
659 * Windows returns different error codes if
660 * the parent directory is valid but not the
661 * last component - it returns NT_STATUS_OBJECT_NAME_NOT_FOUND
662 * for that case and NT_STATUS_OBJECT_PATH_NOT_FOUND
663 * if the path is invalid.
665 END_PROFILE(SMBcheckpath);
666 return ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpath);
669 END_PROFILE(SMBcheckpath);
670 return(UNIXERROR(ERRDOS,ERRbadpath));
673 outsize = set_message(outbuf,0,0,False);
675 END_PROFILE(SMBcheckpath);
679 /****************************************************************************
681 ****************************************************************************/
683 int reply_getatr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
687 SMB_STRUCT_STAT sbuf;
694 START_PROFILE(SMBgetatr);
696 p = smb_buf(inbuf) + 1;
697 p += srvstr_get_path(inbuf, fname, p, sizeof(fname), 0, STR_TERMINATE, &status);
698 if (!NT_STATUS_IS_OK(status)) {
699 END_PROFILE(SMBgetatr);
700 return ERROR_NT(status);
703 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
705 /* dos smetimes asks for a stat of "" - it returns a "hidden directory"
706 under WfWg - weird! */
707 if (*fname == '\0') {
708 mode = aHIDDEN | aDIR;
709 if (!CAN_WRITE(conn)) {
715 status = unix_convert(conn, fname, False, NULL,&sbuf);
716 if (!NT_STATUS_IS_OK(status)) {
717 END_PROFILE(SMBgetatr);
718 return ERROR_NT(status);
720 if (check_name(fname,conn) &&
721 (VALID_STAT(sbuf) || SMB_VFS_STAT(conn,fname,&sbuf) == 0)) {
722 mode = dos_mode(conn,fname,&sbuf);
724 mtime = sbuf.st_mtime;
729 DEBUG(3,("reply_getatr: stat of %s failed (%s)\n",fname,strerror(errno)));
730 END_PROFILE(SMBgetatr);
731 return UNIXERROR(ERRDOS,ERRbadfile);
735 outsize = set_message(outbuf,10,0,True);
737 SSVAL(outbuf,smb_vwv0,mode);
738 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
739 srv_put_dos_date3(outbuf,smb_vwv1,mtime & ~1);
741 srv_put_dos_date3(outbuf,smb_vwv1,mtime);
743 SIVAL(outbuf,smb_vwv3,(uint32)size);
745 if (Protocol >= PROTOCOL_NT1) {
746 SSVAL(outbuf,smb_flg2,SVAL(outbuf, smb_flg2) | FLAGS2_IS_LONG_NAME);
749 DEBUG(3,("reply_getatr: name=%s mode=%d size=%u\n", fname, mode, (unsigned int)size ) );
751 END_PROFILE(SMBgetatr);
755 /****************************************************************************
757 ****************************************************************************/
759 int reply_setatr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
765 SMB_STRUCT_STAT sbuf;
769 START_PROFILE(SMBsetatr);
771 p = smb_buf(inbuf) + 1;
772 p += srvstr_get_path(inbuf, fname, p, sizeof(fname), 0, STR_TERMINATE, &status);
773 if (!NT_STATUS_IS_OK(status)) {
774 END_PROFILE(SMBsetatr);
775 return ERROR_NT(status);
778 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
780 status = unix_convert(conn, fname, False, NULL, &sbuf);
781 if (!NT_STATUS_IS_OK(status)) {
782 END_PROFILE(SMBsetatr);
783 return ERROR_NT(status);
786 if (fname[0] == '.' && fname[1] == '\0') {
788 * Not sure here is the right place to catch this
789 * condition. Might be moved to somewhere else later -- vl
791 END_PROFILE(SMBsetatr);
792 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
795 if (!check_name(fname,conn)) {
796 END_PROFILE(SMBsetatr);
797 return UNIXERROR(ERRDOS, ERRnoaccess);
800 mode = SVAL(inbuf,smb_vwv0);
801 mtime = srv_make_unix_date3(inbuf+smb_vwv1);
803 if (mode != FILE_ATTRIBUTE_NORMAL) {
804 if (VALID_STAT_OF_DIR(sbuf))
809 if (file_set_dosmode(conn,fname,mode,&sbuf,False) != 0) {
810 END_PROFILE(SMBsetatr);
811 return UNIXERROR(ERRDOS, ERRnoaccess);
815 if (!set_filetime(conn,fname,mtime)) {
816 END_PROFILE(SMBsetatr);
817 return UNIXERROR(ERRDOS, ERRnoaccess);
820 outsize = set_message(outbuf,0,0,False);
822 DEBUG( 3, ( "setatr name=%s mode=%d\n", fname, mode ) );
824 END_PROFILE(SMBsetatr);
828 /****************************************************************************
830 ****************************************************************************/
832 int reply_dskattr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
835 SMB_BIG_UINT dfree,dsize,bsize;
836 START_PROFILE(SMBdskattr);
838 if (get_dfree_info(conn,".",True,&bsize,&dfree,&dsize) == (SMB_BIG_UINT)-1) {
839 END_PROFILE(SMBdskattr);
840 return(UNIXERROR(ERRHRD,ERRgeneral));
843 outsize = set_message(outbuf,5,0,True);
845 if (Protocol <= PROTOCOL_LANMAN2) {
846 double total_space, free_space;
847 /* we need to scale this to a number that DOS6 can handle. We
848 use floating point so we can handle large drives on systems
849 that don't have 64 bit integers
851 we end up displaying a maximum of 2G to DOS systems
853 total_space = dsize * (double)bsize;
854 free_space = dfree * (double)bsize;
856 dsize = (total_space+63*512) / (64*512);
857 dfree = (free_space+63*512) / (64*512);
859 if (dsize > 0xFFFF) dsize = 0xFFFF;
860 if (dfree > 0xFFFF) dfree = 0xFFFF;
862 SSVAL(outbuf,smb_vwv0,dsize);
863 SSVAL(outbuf,smb_vwv1,64); /* this must be 64 for dos systems */
864 SSVAL(outbuf,smb_vwv2,512); /* and this must be 512 */
865 SSVAL(outbuf,smb_vwv3,dfree);
867 SSVAL(outbuf,smb_vwv0,dsize);
868 SSVAL(outbuf,smb_vwv1,bsize/512);
869 SSVAL(outbuf,smb_vwv2,512);
870 SSVAL(outbuf,smb_vwv3,dfree);
873 DEBUG(3,("dskattr dfree=%d\n", (unsigned int)dfree));
875 END_PROFILE(SMBdskattr);
879 /****************************************************************************
881 Can be called from SMBsearch, SMBffirst or SMBfunique.
882 ****************************************************************************/
884 int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
894 unsigned int numentries = 0;
895 unsigned int maxentries = 0;
896 BOOL finished = False;
902 BOOL check_descend = False;
903 BOOL expect_close = False;
905 BOOL mask_contains_wcard = False;
906 BOOL allow_long_path_components = (SVAL(inbuf,smb_flg2) & FLAGS2_LONG_PATH_COMPONENTS) ? True : False;
908 START_PROFILE(SMBsearch);
910 if (lp_posix_pathnames()) {
911 END_PROFILE(SMBsearch);
912 return reply_unknown(inbuf, outbuf);
915 *mask = *directory = *fname = 0;
917 /* If we were called as SMBffirst then we must expect close. */
918 if(CVAL(inbuf,smb_com) == SMBffirst) {
922 outsize = set_message(outbuf,1,3,True);
923 maxentries = SVAL(inbuf,smb_vwv0);
924 dirtype = SVAL(inbuf,smb_vwv1);
925 p = smb_buf(inbuf) + 1;
926 p += srvstr_get_path_wcard(inbuf, path, p, sizeof(path), 0, STR_TERMINATE, &nt_status, &mask_contains_wcard);
927 if (!NT_STATUS_IS_OK(nt_status)) {
928 END_PROFILE(SMBsearch);
929 return ERROR_NT(nt_status);
932 RESOLVE_DFSPATH_WCARD(path, conn, inbuf, outbuf);
935 status_len = SVAL(p, 0);
938 /* dirtype &= ~aDIR; */
940 if (status_len == 0) {
941 SMB_STRUCT_STAT sbuf;
944 pstrcpy(directory,path);
946 nt_status = unix_convert(conn, directory, True, NULL, &sbuf);
947 if (!NT_STATUS_IS_OK(nt_status)) {
948 END_PROFILE(SMBsearch);
949 return ERROR_NT(nt_status);
953 if (!check_name(directory,conn)) {
954 END_PROFILE(SMBsearch);
955 return UNIXERROR(ERRDOS, ERRnoaccess);
958 p = strrchr_m(dir2,'/');
967 p = strrchr_m(directory,'/');
974 if (strlen(directory) == 0) {
975 pstrcpy(directory,".");
977 memset((char *)status,'\0',21);
978 SCVAL(status,0,(dirtype & 0x1F));
983 status_dirtype = CVAL(status,0) & 0x1F;
984 if (status_dirtype != (dirtype & 0x1F)) {
985 dirtype = status_dirtype;
988 conn->dirptr = dptr_fetch(status+12,&dptr_num);
992 string_set(&conn->dirpath,dptr_path(dptr_num));
993 pstrcpy(mask, dptr_wcard(dptr_num));
996 p = smb_buf(outbuf) + 3;
998 if (status_len == 0) {
999 dptr_num = dptr_create(conn,directory,True,expect_close,SVAL(inbuf,smb_pid), mask, mask_contains_wcard, dirtype);
1001 if(dptr_num == -2) {
1002 END_PROFILE(SMBsearch);
1003 return UNIXERROR(ERRDOS, ERRnofids);
1005 END_PROFILE(SMBsearch);
1006 return ERROR_DOS(ERRDOS,ERRnofids);
1009 dirtype = dptr_attr(dptr_num);
1012 DEBUG(4,("dptr_num is %d\n",dptr_num));
1014 if ((dirtype&0x1F) == aVOLID) {
1015 memcpy(p,status,21);
1016 make_dir_struct(p,"???????????",volume_label(SNUM(conn)),
1017 0,aVOLID,0,!allow_long_path_components);
1018 dptr_fill(p+12,dptr_num);
1019 if (dptr_zero(p+12) && (status_len==0)) {
1024 p += DIR_STRUCT_SIZE;
1027 maxentries = MIN(maxentries, ((BUFFER_SIZE - (p - outbuf))/DIR_STRUCT_SIZE));
1029 DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
1030 conn->dirpath,lp_dontdescend(SNUM(conn))));
1031 if (in_list(conn->dirpath, lp_dontdescend(SNUM(conn)),True)) {
1032 check_descend = True;
1035 for (i=numentries;(i<maxentries) && !finished;i++) {
1036 finished = !get_dir_entry(conn,mask,dirtype,fname,&size,&mode,&date,check_descend);
1038 memcpy(p,status,21);
1039 make_dir_struct(p,mask,fname,size, mode,date,
1040 !allow_long_path_components);
1041 if (!dptr_fill(p+12,dptr_num)) {
1045 p += DIR_STRUCT_SIZE;
1052 /* If we were called as SMBffirst with smb_search_id == NULL
1053 and no entries were found then return error and close dirptr
1056 if (numentries == 0) {
1057 dptr_close(&dptr_num);
1058 } else if(expect_close && status_len == 0) {
1059 /* Close the dptr - we know it's gone */
1060 dptr_close(&dptr_num);
1063 /* If we were called as SMBfunique, then we can close the dirptr now ! */
1064 if(dptr_num >= 0 && CVAL(inbuf,smb_com) == SMBfunique) {
1065 dptr_close(&dptr_num);
1068 if ((numentries == 0) && !mask_contains_wcard) {
1069 return ERROR_BOTH(STATUS_NO_MORE_FILES,ERRDOS,ERRnofiles);
1072 SSVAL(outbuf,smb_vwv0,numentries);
1073 SSVAL(outbuf,smb_vwv1,3 + numentries * DIR_STRUCT_SIZE);
1074 SCVAL(smb_buf(outbuf),0,5);
1075 SSVAL(smb_buf(outbuf),1,numentries*DIR_STRUCT_SIZE);
1077 /* The replies here are never long name. */
1078 SSVAL(outbuf,smb_flg2,SVAL(outbuf, smb_flg2) & (~FLAGS2_IS_LONG_NAME));
1079 if (!allow_long_path_components) {
1080 SSVAL(outbuf,smb_flg2,SVAL(outbuf, smb_flg2) & (~FLAGS2_LONG_PATH_COMPONENTS));
1083 /* This SMB *always* returns ASCII names. Remove the unicode bit in flags2. */
1084 SSVAL(outbuf,smb_flg2, (SVAL(outbuf, smb_flg2) & (~FLAGS2_UNICODE_STRINGS)));
1086 outsize += DIR_STRUCT_SIZE*numentries;
1087 smb_setlen(outbuf,outsize - 4);
1089 if ((! *directory) && dptr_path(dptr_num))
1090 slprintf(directory, sizeof(directory)-1, "(%s)",dptr_path(dptr_num));
1092 DEBUG( 4, ( "%s mask=%s path=%s dtype=%d nument=%u of %u\n",
1093 smb_fn_name(CVAL(inbuf,smb_com)),
1094 mask, directory, dirtype, numentries, maxentries ) );
1096 END_PROFILE(SMBsearch);
1100 /****************************************************************************
1101 Reply to a fclose (stop directory search).
1102 ****************************************************************************/
1104 int reply_fclose(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1113 BOOL path_contains_wcard = False;
1115 START_PROFILE(SMBfclose);
1117 if (lp_posix_pathnames()) {
1118 END_PROFILE(SMBfclose);
1119 return reply_unknown(inbuf, outbuf);
1122 outsize = set_message(outbuf,1,0,True);
1123 p = smb_buf(inbuf) + 1;
1124 p += srvstr_get_path_wcard(inbuf, path, p, sizeof(path), 0, STR_TERMINATE, &err, &path_contains_wcard);
1125 if (!NT_STATUS_IS_OK(err)) {
1126 END_PROFILE(SMBfclose);
1127 return ERROR_NT(err);
1130 status_len = SVAL(p,0);
1133 if (status_len == 0) {
1134 END_PROFILE(SMBfclose);
1135 return ERROR_DOS(ERRSRV,ERRsrverror);
1138 memcpy(status,p,21);
1140 if(dptr_fetch(status+12,&dptr_num)) {
1141 /* Close the dptr - we know it's gone */
1142 dptr_close(&dptr_num);
1145 SSVAL(outbuf,smb_vwv0,0);
1147 DEBUG(3,("search close\n"));
1149 END_PROFILE(SMBfclose);
1153 /****************************************************************************
1155 ****************************************************************************/
1157 int reply_open(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1165 SMB_STRUCT_STAT sbuf;
1167 int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
1169 uint32 dos_attr = SVAL(inbuf,smb_vwv1);
1172 uint32 create_disposition;
1173 uint32 create_options = 0;
1175 START_PROFILE(SMBopen);
1177 deny_mode = SVAL(inbuf,smb_vwv0);
1179 srvstr_get_path(inbuf, fname, smb_buf(inbuf)+1, sizeof(fname), 0, STR_TERMINATE, &status);
1180 if (!NT_STATUS_IS_OK(status)) {
1181 END_PROFILE(SMBopen);
1182 return ERROR_NT(status);
1185 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1187 status = unix_convert(conn, fname, False, NULL, &sbuf);
1188 if (!NT_STATUS_IS_OK(status)) {
1189 END_PROFILE(SMBopen);
1190 return ERROR_NT(status);
1193 if (!map_open_params_to_ntcreate(fname, deny_mode, OPENX_FILE_EXISTS_OPEN,
1194 &access_mask, &share_mode, &create_disposition, &create_options)) {
1195 END_PROFILE(SMBopen);
1196 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRbadaccess));
1199 status = open_file_ntcreate(conn,fname,&sbuf,
1208 if (!NT_STATUS_IS_OK(status)) {
1209 END_PROFILE(SMBopen);
1210 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1211 /* We have re-scheduled this call. */
1214 return ERROR_NT(status);
1217 size = sbuf.st_size;
1218 fattr = dos_mode(conn,fname,&sbuf);
1219 mtime = sbuf.st_mtime;
1222 DEBUG(3,("attempt to open a directory %s\n",fname));
1223 close_file(fsp,ERROR_CLOSE);
1224 END_PROFILE(SMBopen);
1225 return ERROR_DOS(ERRDOS,ERRnoaccess);
1228 outsize = set_message(outbuf,7,0,True);
1229 SSVAL(outbuf,smb_vwv0,fsp->fnum);
1230 SSVAL(outbuf,smb_vwv1,fattr);
1231 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1232 srv_put_dos_date3(outbuf,smb_vwv2,mtime & ~1);
1234 srv_put_dos_date3(outbuf,smb_vwv2,mtime);
1236 SIVAL(outbuf,smb_vwv4,(uint32)size);
1237 SSVAL(outbuf,smb_vwv6,deny_mode);
1239 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1240 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1243 if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1244 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1246 END_PROFILE(SMBopen);
1250 /****************************************************************************
1251 Reply to an open and X.
1252 ****************************************************************************/
1254 int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
1257 uint16 open_flags = SVAL(inbuf,smb_vwv2);
1258 int deny_mode = SVAL(inbuf,smb_vwv3);
1259 uint32 smb_attr = SVAL(inbuf,smb_vwv5);
1260 /* Breakout the oplock request bits so we can set the
1261 reply bits separately. */
1262 int ex_oplock_request = EXTENDED_OPLOCK_REQUEST(inbuf);
1263 int core_oplock_request = CORE_OPLOCK_REQUEST(inbuf);
1264 int oplock_request = ex_oplock_request | core_oplock_request;
1266 int smb_sattr = SVAL(inbuf,smb_vwv4);
1267 uint32 smb_time = make_unix_date3(inbuf+smb_vwv6);
1269 int smb_ofun = SVAL(inbuf,smb_vwv8);
1272 SMB_STRUCT_STAT sbuf;
1276 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv9);
1277 ssize_t retval = -1;
1280 uint32 create_disposition;
1281 uint32 create_options = 0;
1283 START_PROFILE(SMBopenX);
1285 /* If it's an IPC, pass off the pipe handler. */
1287 if (lp_nt_pipe_support()) {
1288 END_PROFILE(SMBopenX);
1289 return reply_open_pipe_and_X(conn, inbuf,outbuf,length,bufsize);
1291 END_PROFILE(SMBopenX);
1292 return ERROR_DOS(ERRSRV,ERRaccess);
1296 /* XXXX we need to handle passed times, sattr and flags */
1297 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
1298 if (!NT_STATUS_IS_OK(status)) {
1299 END_PROFILE(SMBopenX);
1300 return ERROR_NT(status);
1303 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1305 status = unix_convert(conn, fname, False, NULL, &sbuf);
1306 if (!NT_STATUS_IS_OK(status)) {
1307 END_PROFILE(SMBopenX);
1308 return ERROR_NT(status);
1311 if (!map_open_params_to_ntcreate(fname, deny_mode, smb_ofun,
1314 &create_disposition,
1316 END_PROFILE(SMBopenX);
1317 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRbadaccess));
1320 status = open_file_ntcreate(conn,fname,&sbuf,
1329 if (!NT_STATUS_IS_OK(status)) {
1330 END_PROFILE(SMBopenX);
1331 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1332 /* We have re-scheduled this call. */
1335 return ERROR_NT(status);
1338 /* Setting the "size" field in vwv9 and vwv10 causes the file to be set to this size,
1339 if the file is truncated or created. */
1340 if (((smb_action == FILE_WAS_CREATED) || (smb_action == FILE_WAS_OVERWRITTEN)) && allocation_size) {
1341 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1342 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1343 close_file(fsp,ERROR_CLOSE);
1344 END_PROFILE(SMBopenX);
1345 return ERROR_NT(NT_STATUS_DISK_FULL);
1347 retval = vfs_set_filelen(fsp, (SMB_OFF_T)allocation_size);
1349 close_file(fsp,ERROR_CLOSE);
1350 END_PROFILE(SMBopenX);
1351 return ERROR_NT(NT_STATUS_DISK_FULL);
1353 sbuf.st_size = get_allocation_size(conn,fsp,&sbuf);
1356 fattr = dos_mode(conn,fname,&sbuf);
1357 mtime = sbuf.st_mtime;
1359 close_file(fsp,ERROR_CLOSE);
1360 END_PROFILE(SMBopenX);
1361 return ERROR_DOS(ERRDOS,ERRnoaccess);
1364 /* If the caller set the extended oplock request bit
1365 and we granted one (by whatever means) - set the
1366 correct bit for extended oplock reply.
1369 if (ex_oplock_request && lp_fake_oplocks(SNUM(conn))) {
1370 smb_action |= EXTENDED_OPLOCK_GRANTED;
1373 if(ex_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1374 smb_action |= EXTENDED_OPLOCK_GRANTED;
1377 /* If the caller set the core oplock request bit
1378 and we granted one (by whatever means) - set the
1379 correct bit for core oplock reply.
1382 if (core_oplock_request && lp_fake_oplocks(SNUM(conn))) {
1383 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1386 if(core_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1387 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1390 if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
1391 set_message(outbuf,19,0,True);
1393 set_message(outbuf,15,0,True);
1395 SSVAL(outbuf,smb_vwv2,fsp->fnum);
1396 SSVAL(outbuf,smb_vwv3,fattr);
1397 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1398 srv_put_dos_date3(outbuf,smb_vwv4,mtime & ~1);
1400 srv_put_dos_date3(outbuf,smb_vwv4,mtime);
1402 SIVAL(outbuf,smb_vwv6,(uint32)sbuf.st_size);
1403 SSVAL(outbuf,smb_vwv8,GET_OPENX_MODE(deny_mode));
1404 SSVAL(outbuf,smb_vwv11,smb_action);
1406 if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
1407 SIVAL(outbuf, smb_vwv15, STD_RIGHT_ALL_ACCESS);
1410 END_PROFILE(SMBopenX);
1411 return chain_reply(inbuf,outbuf,length,bufsize);
1414 /****************************************************************************
1415 Reply to a SMBulogoffX.
1416 conn POINTER CAN BE NULL HERE !
1417 ****************************************************************************/
1419 int reply_ulogoffX(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
1421 uint16 vuid = SVAL(inbuf,smb_uid);
1422 user_struct *vuser = get_valid_user_struct(vuid);
1423 START_PROFILE(SMBulogoffX);
1426 DEBUG(3,("ulogoff, vuser id %d does not map to user.\n", vuid));
1428 /* in user level security we are supposed to close any files
1429 open by this user */
1430 if ((vuser != 0) && (lp_security() != SEC_SHARE))
1431 file_close_user(vuid);
1433 invalidate_vuid(vuid);
1435 set_message(outbuf,2,0,True);
1437 DEBUG( 3, ( "ulogoffX vuid=%d\n", vuid ) );
1439 END_PROFILE(SMBulogoffX);
1440 return chain_reply(inbuf,outbuf,length,bufsize);
1443 /****************************************************************************
1444 Reply to a mknew or a create.
1445 ****************************************************************************/
1447 int reply_mknew(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1452 uint32 fattr = SVAL(inbuf,smb_vwv0);
1453 struct utimbuf times;
1455 int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
1456 SMB_STRUCT_STAT sbuf;
1458 uint32 access_mask = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
1459 uint32 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1460 uint32 create_disposition;
1461 uint32 create_options = 0;
1463 START_PROFILE(SMBcreate);
1465 com = SVAL(inbuf,smb_com);
1467 times.modtime = srv_make_unix_date3(inbuf + smb_vwv1);
1469 srvstr_get_path(inbuf, fname, smb_buf(inbuf) + 1, sizeof(fname), 0, STR_TERMINATE, &status);
1470 if (!NT_STATUS_IS_OK(status)) {
1471 END_PROFILE(SMBcreate);
1472 return ERROR_NT(status);
1475 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1477 status = unix_convert(conn, fname, False, NULL, &sbuf);
1478 if (!NT_STATUS_IS_OK(status)) {
1479 END_PROFILE(SMBcreate);
1480 return ERROR_NT(status);
1483 if (fattr & aVOLID) {
1484 DEBUG(0,("Attempt to create file (%s) with volid set - please report this\n",fname));
1487 if(com == SMBmknew) {
1488 /* We should fail if file exists. */
1489 create_disposition = FILE_CREATE;
1491 /* Create if file doesn't exist, truncate if it does. */
1492 create_disposition = FILE_OVERWRITE_IF;
1495 /* Open file using ntcreate. */
1496 status = open_file_ntcreate(conn,fname,&sbuf,
1505 if (!NT_STATUS_IS_OK(status)) {
1506 END_PROFILE(SMBcreate);
1507 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1508 /* We have re-scheduled this call. */
1511 return ERROR_NT(status);
1514 times.actime = sbuf.st_atime;
1515 file_utime(conn, fname, ×);
1517 outsize = set_message(outbuf,1,0,True);
1518 SSVAL(outbuf,smb_vwv0,fsp->fnum);
1520 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1521 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1524 if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1525 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1528 DEBUG( 2, ( "reply_mknew: file %s\n", fname ) );
1529 DEBUG( 3, ( "reply_mknew %s fd=%d dmode=0x%x\n", fname, fsp->fh->fd, (unsigned int)fattr ) );
1531 END_PROFILE(SMBcreate);
1535 /****************************************************************************
1536 Reply to a create temporary file.
1537 ****************************************************************************/
1539 int reply_ctemp(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1543 uint32 fattr = SVAL(inbuf,smb_vwv0);
1545 int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
1547 SMB_STRUCT_STAT sbuf;
1550 unsigned int namelen;
1552 START_PROFILE(SMBctemp);
1554 srvstr_get_path(inbuf, fname, smb_buf(inbuf)+1, sizeof(fname), 0, STR_TERMINATE, &status);
1555 if (!NT_STATUS_IS_OK(status)) {
1556 END_PROFILE(SMBctemp);
1557 return ERROR_NT(status);
1560 pstrcat(fname,"/TMXXXXXX");
1562 pstrcat(fname,"TMXXXXXX");
1565 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1567 status = unix_convert(conn, fname, False, NULL, &sbuf);
1568 if (!NT_STATUS_IS_OK(status)) {
1569 END_PROFILE(SMBctemp);
1570 return ERROR_NT(status);
1573 tmpfd = smb_mkstemp(fname);
1575 END_PROFILE(SMBctemp);
1576 return(UNIXERROR(ERRDOS,ERRnoaccess));
1579 SMB_VFS_STAT(conn,fname,&sbuf);
1581 /* We should fail if file does not exist. */
1582 status = open_file_ntcreate(conn,fname,&sbuf,
1583 FILE_GENERIC_READ | FILE_GENERIC_WRITE,
1584 FILE_SHARE_READ|FILE_SHARE_WRITE,
1591 /* close fd from smb_mkstemp() */
1594 if (!NT_STATUS_IS_OK(status)) {
1595 END_PROFILE(SMBctemp);
1596 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1597 /* We have re-scheduled this call. */
1600 return ERROR_NT(status);
1603 outsize = set_message(outbuf,1,0,True);
1604 SSVAL(outbuf,smb_vwv0,fsp->fnum);
1606 /* the returned filename is relative to the directory */
1607 s = strrchr_m(fname, '/');
1614 p = smb_buf(outbuf);
1616 /* Tested vs W2K3 - this doesn't seem to be here - null terminated filename is the only
1617 thing in the byte section. JRA */
1618 SSVALS(p, 0, -1); /* what is this? not in spec */
1620 namelen = srvstr_push(outbuf, p, s, -1, STR_ASCII|STR_TERMINATE);
1622 outsize = set_message_end(outbuf, p);
1624 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1625 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1628 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1629 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1632 DEBUG( 2, ( "reply_ctemp: created temp file %s\n", fname ) );
1633 DEBUG( 3, ( "reply_ctemp %s fd=%d umode=0%o\n", fname, fsp->fh->fd,
1634 (unsigned int)sbuf.st_mode ) );
1636 END_PROFILE(SMBctemp);
1640 /*******************************************************************
1641 Check if a user is allowed to rename a file.
1642 ********************************************************************/
1644 static NTSTATUS can_rename(connection_struct *conn, char *fname, uint16 dirtype, SMB_STRUCT_STAT *pst)
1650 if (!CAN_WRITE(conn)) {
1651 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1654 fmode = dos_mode(conn,fname,pst);
1655 if ((fmode & ~dirtype) & (aHIDDEN | aSYSTEM)) {
1656 return NT_STATUS_NO_SUCH_FILE;
1659 if (S_ISDIR(pst->st_mode)) {
1660 return NT_STATUS_OK;
1663 status = open_file_ntcreate(conn, fname, pst,
1665 FILE_SHARE_READ|FILE_SHARE_WRITE,
1668 FILE_ATTRIBUTE_NORMAL,
1672 if (!NT_STATUS_IS_OK(status)) {
1675 close_file(fsp,NORMAL_CLOSE);
1676 return NT_STATUS_OK;
1679 /*******************************************************************
1680 Check if a user is allowed to delete a file.
1681 ********************************************************************/
1683 static NTSTATUS can_delete(connection_struct *conn, char *fname,
1684 uint32 dirtype, BOOL can_defer)
1686 SMB_STRUCT_STAT sbuf;
1689 uint32 dirtype_orig = dirtype;
1692 DEBUG(10,("can_delete: %s, dirtype = %d\n", fname, dirtype ));
1694 if (!CAN_WRITE(conn)) {
1695 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1698 if (SMB_VFS_LSTAT(conn,fname,&sbuf) != 0) {
1699 return map_nt_error_from_unix(errno);
1702 fattr = dos_mode(conn,fname,&sbuf);
1704 if (dirtype & FILE_ATTRIBUTE_NORMAL) {
1705 dirtype = aDIR|aARCH|aRONLY;
1708 dirtype &= (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM);
1710 return NT_STATUS_NO_SUCH_FILE;
1713 if (!dir_check_ftype(conn, fattr, dirtype)) {
1715 return NT_STATUS_FILE_IS_A_DIRECTORY;
1717 return NT_STATUS_NO_SUCH_FILE;
1720 if (dirtype_orig & 0x8000) {
1721 /* These will never be set for POSIX. */
1722 return NT_STATUS_NO_SUCH_FILE;
1726 if ((fattr & dirtype) & FILE_ATTRIBUTE_DIRECTORY) {
1727 return NT_STATUS_FILE_IS_A_DIRECTORY;
1730 if ((fattr & ~dirtype) & (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)) {
1731 return NT_STATUS_NO_SUCH_FILE;
1734 if (dirtype & 0xFF00) {
1735 /* These will never be set for POSIX. */
1736 return NT_STATUS_NO_SUCH_FILE;
1741 return NT_STATUS_NO_SUCH_FILE;
1744 /* Can't delete a directory. */
1746 return NT_STATUS_FILE_IS_A_DIRECTORY;
1751 else if (dirtype & aDIR) /* Asked for a directory and it isn't. */
1752 return NT_STATUS_OBJECT_NAME_INVALID;
1753 #endif /* JRATEST */
1755 /* Fix for bug #3035 from SATOH Fumiyasu <fumiyas@miraclelinux.com>
1757 On a Windows share, a file with read-only dosmode can be opened with
1758 DELETE_ACCESS. But on a Samba share (delete readonly = no), it
1759 fails with NT_STATUS_CANNOT_DELETE error.
1761 This semantic causes a problem that a user can not
1762 rename a file with read-only dosmode on a Samba share
1763 from a Windows command prompt (i.e. cmd.exe, but can rename
1764 from Windows Explorer).
1767 if (!lp_delete_readonly(SNUM(conn))) {
1768 if (fattr & aRONLY) {
1769 return NT_STATUS_CANNOT_DELETE;
1773 /* On open checks the open itself will check the share mode, so
1774 don't do it here as we'll get it wrong. */
1776 status = open_file_ntcreate(conn, fname, &sbuf,
1781 FILE_ATTRIBUTE_NORMAL,
1782 can_defer ? 0 : INTERNAL_OPEN_ONLY,
1785 if (NT_STATUS_IS_OK(status)) {
1786 close_file(fsp,NORMAL_CLOSE);
1791 /****************************************************************************
1792 The guts of the unlink command, split out so it may be called by the NT SMB
1794 ****************************************************************************/
1796 NTSTATUS unlink_internals(connection_struct *conn, uint32 dirtype,
1797 char *name, BOOL has_wild, BOOL can_defer)
1803 NTSTATUS status = NT_STATUS_OK;
1804 SMB_STRUCT_STAT sbuf;
1806 *directory = *mask = 0;
1808 status = unix_convert(conn, name, has_wild, NULL, &sbuf);
1809 if (!NT_STATUS_IS_OK(status)) {
1813 p = strrchr_m(name,'/');
1815 pstrcpy(directory,".");
1819 pstrcpy(directory,name);
1824 * We should only check the mangled cache
1825 * here if unix_convert failed. This means
1826 * that the path in 'mask' doesn't exist
1827 * on the file system and so we need to look
1828 * for a possible mangle. This patch from
1829 * Tine Smukavec <valentin.smukavec@hermes.si>.
1832 if (!VALID_STAT(sbuf) && mangle_is_mangled(mask,conn->params))
1833 mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
1836 pstrcat(directory,"/");
1837 pstrcat(directory,mask);
1839 dirtype = FILE_ATTRIBUTE_NORMAL;
1841 status = can_delete(conn,directory,dirtype,can_defer);
1842 if (!NT_STATUS_IS_OK(status))
1845 if (SMB_VFS_UNLINK(conn,directory) == 0) {
1849 struct smb_Dir *dir_hnd = NULL;
1852 if ((dirtype & SAMBA_ATTRIBUTES_MASK) == aDIR) {
1853 return NT_STATUS_OBJECT_NAME_INVALID;
1856 if (strequal(mask,"????????.???"))
1859 if (check_name(directory,conn))
1860 dir_hnd = OpenDir(conn, directory, mask, dirtype);
1862 /* XXXX the CIFS spec says that if bit0 of the flags2 field is set then
1863 the pattern matches against the long name, otherwise the short name
1864 We don't implement this yet XXXX
1869 status = NT_STATUS_NO_SUCH_FILE;
1871 while ((dname = ReadDirName(dir_hnd, &offset))) {
1874 pstrcpy(fname,dname);
1876 if (!is_visible_file(conn, directory, dname, &st, True)) {
1880 /* Quick check for "." and ".." */
1881 if (fname[0] == '.') {
1882 if (!fname[1] || (fname[1] == '.' && !fname[2])) {
1887 if(!mask_match(fname, mask, conn->case_sensitive))
1890 slprintf(fname,sizeof(fname)-1, "%s/%s",directory,dname);
1891 status = can_delete(conn, fname, dirtype,
1893 if (!NT_STATUS_IS_OK(status)) {
1896 if (SMB_VFS_UNLINK(conn,fname) == 0)
1898 DEBUG(3,("unlink_internals: succesful unlink [%s]\n",fname));
1904 if (count == 0 && NT_STATUS_IS_OK(status)) {
1905 status = map_nt_error_from_unix(errno);
1911 /****************************************************************************
1913 ****************************************************************************/
1915 int reply_unlink(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
1922 BOOL path_contains_wcard = False;
1924 START_PROFILE(SMBunlink);
1926 dirtype = SVAL(inbuf,smb_vwv0);
1928 srvstr_get_path_wcard(inbuf, name, smb_buf(inbuf) + 1, sizeof(name), 0, STR_TERMINATE, &status, &path_contains_wcard);
1929 if (!NT_STATUS_IS_OK(status)) {
1930 END_PROFILE(SMBunlink);
1931 return ERROR_NT(status);
1934 RESOLVE_DFSPATH_WCARD(name, conn, inbuf, outbuf);
1936 DEBUG(3,("reply_unlink : %s\n",name));
1938 status = unlink_internals(conn, dirtype, name, path_contains_wcard,
1940 if (!NT_STATUS_IS_OK(status)) {
1941 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1942 /* We have re-scheduled this call. */
1945 return ERROR_NT(status);
1949 * Win2k needs a changenotify request response before it will
1950 * update after a rename..
1952 process_pending_change_notify_queue((time_t)0);
1954 outsize = set_message(outbuf,0,0,False);
1956 END_PROFILE(SMBunlink);
1960 /****************************************************************************
1962 ****************************************************************************/
1964 static void fail_readraw(void)
1967 slprintf(errstr, sizeof(errstr)-1, "FAIL ! reply_readbraw: socket write fail (%s)",
1969 exit_server_cleanly(errstr);
1972 #if defined(WITH_SENDFILE)
1973 /****************************************************************************
1974 Fake (read/write) sendfile. Returns -1 on read or write fail.
1975 ****************************************************************************/
1977 static ssize_t fake_sendfile(files_struct *fsp, SMB_OFF_T startpos, size_t nread, char *buf, int bufsize)
1981 /* Paranioa check... */
1982 if (nread > bufsize) {
1987 ret = read_file(fsp,buf,startpos,nread);
1993 /* If we had a short read, fill with zeros. */
1995 memset(buf, '\0', nread - ret);
1998 if (write_data(smbd_server_fd(),buf,nread) != nread) {
2002 return (ssize_t)nread;
2006 /****************************************************************************
2007 Use sendfile in readbraw.
2008 ****************************************************************************/
2010 void send_file_readbraw(connection_struct *conn, files_struct *fsp, SMB_OFF_T startpos, size_t nread,
2011 ssize_t mincount, char *outbuf, int out_buffsize)
2015 #if defined(WITH_SENDFILE)
2017 * We can only use sendfile on a non-chained packet
2018 * but we can use on a non-oplocked file. tridge proved this
2019 * on a train in Germany :-). JRA.
2020 * reply_readbraw has already checked the length.
2023 if ( (chain_size == 0) && (nread > 0) &&
2024 (fsp->wcp == NULL) && lp_use_sendfile(SNUM(conn)) ) {
2027 _smb_setlen(outbuf,nread);
2028 header.data = (uint8 *)outbuf;
2032 if ( SMB_VFS_SENDFILE( smbd_server_fd(), fsp, fsp->fh->fd, &header, startpos, nread) == -1) {
2033 /* Returning ENOSYS means no data at all was sent. Do this as a normal read. */
2034 if (errno == ENOSYS) {
2035 goto normal_readbraw;
2039 * Special hack for broken Linux with no working sendfile. If we
2040 * return EINTR we sent the header but not the rest of the data.
2041 * Fake this up by doing read/write calls.
2043 if (errno == EINTR) {
2044 /* Ensure we don't do this again. */
2045 set_use_sendfile(SNUM(conn), False);
2046 DEBUG(0,("send_file_readbraw: sendfile not available. Faking..\n"));
2048 if (fake_sendfile(fsp, startpos, nread, outbuf + 4, out_buffsize - 4) == -1) {
2049 DEBUG(0,("send_file_readbraw: fake_sendfile failed for file %s (%s).\n",
2050 fsp->fsp_name, strerror(errno) ));
2051 exit_server_cleanly("send_file_readbraw fake_sendfile failed");
2056 DEBUG(0,("send_file_readbraw: sendfile failed for file %s (%s). Terminating\n",
2057 fsp->fsp_name, strerror(errno) ));
2058 exit_server_cleanly("send_file_readbraw sendfile failed");
2068 ret = read_file(fsp,outbuf+4,startpos,nread);
2069 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2078 _smb_setlen(outbuf,ret);
2079 if (write_data(smbd_server_fd(),outbuf,4+ret) != 4+ret)
2083 /****************************************************************************
2084 Reply to a readbraw (core+ protocol).
2085 ****************************************************************************/
2087 int reply_readbraw(connection_struct *conn, char *inbuf, char *outbuf, int dum_size, int out_buffsize)
2089 ssize_t maxcount,mincount;
2092 char *header = outbuf;
2094 START_PROFILE(SMBreadbraw);
2096 if (srv_is_signing_active()) {
2097 exit_server_cleanly("reply_readbraw: SMB signing is active - raw reads/writes are disallowed.");
2101 * Special check if an oplock break has been issued
2102 * and the readraw request croses on the wire, we must
2103 * return a zero length response here.
2106 fsp = file_fsp(inbuf,smb_vwv0);
2108 if (!FNUM_OK(fsp,conn) || !fsp->can_read) {
2110 * fsp could be NULL here so use the value from the packet. JRA.
2112 DEBUG(3,("fnum %d not open in readbraw - cache prime?\n",(int)SVAL(inbuf,smb_vwv0)));
2113 _smb_setlen(header,0);
2114 if (write_data(smbd_server_fd(),header,4) != 4)
2116 END_PROFILE(SMBreadbraw);
2120 CHECK_FSP(fsp,conn);
2122 flush_write_cache(fsp, READRAW_FLUSH);
2124 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
2125 if(CVAL(inbuf,smb_wct) == 10) {
2127 * This is a large offset (64 bit) read.
2129 #ifdef LARGE_SMB_OFF_T
2131 startpos |= (((SMB_OFF_T)IVAL(inbuf,smb_vwv8)) << 32);
2133 #else /* !LARGE_SMB_OFF_T */
2136 * Ensure we haven't been sent a >32 bit offset.
2139 if(IVAL(inbuf,smb_vwv8) != 0) {
2140 DEBUG(0,("readbraw - large offset (%x << 32) used and we don't support \
2141 64 bit offsets.\n", (unsigned int)IVAL(inbuf,smb_vwv8) ));
2142 _smb_setlen(header,0);
2143 if (write_data(smbd_server_fd(),header,4) != 4)
2145 END_PROFILE(SMBreadbraw);
2149 #endif /* LARGE_SMB_OFF_T */
2152 DEBUG(0,("readbraw - negative 64 bit readraw offset (%.0f) !\n", (double)startpos ));
2153 _smb_setlen(header,0);
2154 if (write_data(smbd_server_fd(),header,4) != 4)
2156 END_PROFILE(SMBreadbraw);
2160 maxcount = (SVAL(inbuf,smb_vwv3) & 0xFFFF);
2161 mincount = (SVAL(inbuf,smb_vwv4) & 0xFFFF);
2163 /* ensure we don't overrun the packet size */
2164 maxcount = MIN(65535,maxcount);
2166 if (!is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
2170 if (SMB_VFS_FSTAT(fsp,fsp->fh->fd,&st) == 0) {
2174 if (startpos >= size) {
2177 nread = MIN(maxcount,(size - startpos));
2181 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2182 if (nread < mincount)
2186 DEBUG( 3, ( "readbraw fnum=%d start=%.0f max=%lu min=%lu nread=%lu\n", fsp->fnum, (double)startpos,
2187 (unsigned long)maxcount, (unsigned long)mincount, (unsigned long)nread ) );
2189 send_file_readbraw(conn, fsp, startpos, nread, mincount, outbuf, out_buffsize);
2191 DEBUG(5,("readbraw finished\n"));
2192 END_PROFILE(SMBreadbraw);
2197 #define DBGC_CLASS DBGC_LOCKING
2199 /****************************************************************************
2200 Reply to a lockread (core+ protocol).
2201 ****************************************************************************/
2203 int reply_lockread(connection_struct *conn, char *inbuf,char *outbuf, int length, int dum_buffsiz)
2211 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2212 struct byte_range_lock *br_lck = NULL;
2213 START_PROFILE(SMBlockread);
2215 CHECK_FSP(fsp,conn);
2216 if (!CHECK_READ(fsp,inbuf)) {
2217 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2220 release_level_2_oplocks_on_change(fsp);
2222 numtoread = SVAL(inbuf,smb_vwv1);
2223 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2225 outsize = set_message(outbuf,5,3,True);
2226 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
2227 data = smb_buf(outbuf) + 3;
2230 * NB. Discovered by Menny Hamburger at Mainsoft. This is a core+
2231 * protocol request that predates the read/write lock concept.
2232 * Thus instead of asking for a read lock here we need to ask
2233 * for a write lock. JRA.
2234 * Note that the requested lock size is unaffected by max_recv.
2237 br_lck = do_lock(fsp,
2238 (uint32)SVAL(inbuf,smb_pid),
2239 (SMB_BIG_UINT)numtoread,
2240 (SMB_BIG_UINT)startpos,
2243 False, /* Non-blocking lock. */
2245 TALLOC_FREE(br_lck);
2247 if (NT_STATUS_V(status)) {
2248 END_PROFILE(SMBlockread);
2249 return ERROR_NT(status);
2253 * However the requested READ size IS affected by max_recv. Insanity.... JRA.
2256 if (numtoread > max_recv) {
2257 DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u). \
2258 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
2259 (unsigned int)numtoread, (unsigned int)max_recv ));
2260 numtoread = MIN(numtoread,max_recv);
2262 nread = read_file(fsp,data,startpos,numtoread);
2265 END_PROFILE(SMBlockread);
2266 return(UNIXERROR(ERRDOS,ERRnoaccess));
2270 SSVAL(outbuf,smb_vwv0,nread);
2271 SSVAL(outbuf,smb_vwv5,nread+3);
2272 SSVAL(smb_buf(outbuf),1,nread);
2274 DEBUG(3,("lockread fnum=%d num=%d nread=%d\n",
2275 fsp->fnum, (int)numtoread, (int)nread));
2277 END_PROFILE(SMBlockread);
2282 #define DBGC_CLASS DBGC_ALL
2284 /****************************************************************************
2286 ****************************************************************************/
2288 int reply_read(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
2295 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2296 START_PROFILE(SMBread);
2298 CHECK_FSP(fsp,conn);
2299 if (!CHECK_READ(fsp,inbuf)) {
2300 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2303 numtoread = SVAL(inbuf,smb_vwv1);
2304 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2306 outsize = set_message(outbuf,5,3,True);
2307 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
2309 * The requested read size cannot be greater than max_recv. JRA.
2311 if (numtoread > max_recv) {
2312 DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u). \
2313 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
2314 (unsigned int)numtoread, (unsigned int)max_recv ));
2315 numtoread = MIN(numtoread,max_recv);
2318 data = smb_buf(outbuf) + 3;
2320 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtoread,(SMB_BIG_UINT)startpos, READ_LOCK)) {
2321 END_PROFILE(SMBread);
2322 return ERROR_DOS(ERRDOS,ERRlock);
2326 nread = read_file(fsp,data,startpos,numtoread);
2329 END_PROFILE(SMBread);
2330 return(UNIXERROR(ERRDOS,ERRnoaccess));
2334 SSVAL(outbuf,smb_vwv0,nread);
2335 SSVAL(outbuf,smb_vwv5,nread+3);
2336 SCVAL(smb_buf(outbuf),0,1);
2337 SSVAL(smb_buf(outbuf),1,nread);
2339 DEBUG( 3, ( "read fnum=%d num=%d nread=%d\n",
2340 fsp->fnum, (int)numtoread, (int)nread ) );
2342 END_PROFILE(SMBread);
2346 /****************************************************************************
2347 Reply to a read and X - possibly using sendfile.
2348 ****************************************************************************/
2350 int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length, int len_outbuf,
2351 files_struct *fsp, SMB_OFF_T startpos, size_t smb_maxcnt)
2355 char *data = smb_buf(outbuf);
2357 #if defined(WITH_SENDFILE)
2359 * We can only use sendfile on a non-chained packet
2360 * but we can use on a non-oplocked file. tridge proved this
2361 * on a train in Germany :-). JRA.
2364 if ((chain_size == 0) && (CVAL(inbuf,smb_vwv0) == 0xFF) &&
2365 lp_use_sendfile(SNUM(conn)) && (fsp->wcp == NULL) ) {
2366 SMB_STRUCT_STAT sbuf;
2369 if(SMB_VFS_FSTAT(fsp,fsp->fh->fd, &sbuf) == -1)
2370 return(UNIXERROR(ERRDOS,ERRnoaccess));
2372 if (startpos > sbuf.st_size)
2375 if (smb_maxcnt > (sbuf.st_size - startpos))
2376 smb_maxcnt = (sbuf.st_size - startpos);
2378 if (smb_maxcnt == 0)
2382 * Set up the packet header before send. We
2383 * assume here the sendfile will work (get the
2384 * correct amount of data).
2387 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be -1. */
2388 SSVAL(outbuf,smb_vwv5,smb_maxcnt);
2389 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
2390 SSVAL(outbuf,smb_vwv7,((smb_maxcnt >> 16) & 1));
2391 SSVAL(smb_buf(outbuf),-2,smb_maxcnt);
2392 SCVAL(outbuf,smb_vwv0,0xFF);
2393 set_message(outbuf,12,smb_maxcnt,False);
2394 header.data = (uint8 *)outbuf;
2395 header.length = data - outbuf;
2398 if ((nread = SMB_VFS_SENDFILE( smbd_server_fd(), fsp, fsp->fh->fd, &header, startpos, smb_maxcnt)) == -1) {
2399 /* Returning ENOSYS means no data at all was sent. Do this as a normal read. */
2400 if (errno == ENOSYS) {
2405 * Special hack for broken Linux with no working sendfile. If we
2406 * return EINTR we sent the header but not the rest of the data.
2407 * Fake this up by doing read/write calls.
2410 if (errno == EINTR) {
2411 /* Ensure we don't do this again. */
2412 set_use_sendfile(SNUM(conn), False);
2413 DEBUG(0,("send_file_readX: sendfile not available. Faking..\n"));
2415 if ((nread = fake_sendfile(fsp, startpos, smb_maxcnt, data,
2416 len_outbuf - (data-outbuf))) == -1) {
2417 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
2418 fsp->fsp_name, strerror(errno) ));
2419 exit_server_cleanly("send_file_readX: fake_sendfile failed");
2421 DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
2422 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
2423 /* Returning -1 here means successful sendfile. */
2427 DEBUG(0,("send_file_readX: sendfile failed for file %s (%s). Terminating\n",
2428 fsp->fsp_name, strerror(errno) ));
2429 exit_server_cleanly("send_file_readX sendfile failed");
2432 DEBUG( 3, ( "send_file_readX: sendfile fnum=%d max=%d nread=%d\n",
2433 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
2434 /* Returning -1 here means successful sendfile. */
2442 nread = read_file(fsp,data,startpos,smb_maxcnt);
2445 return(UNIXERROR(ERRDOS,ERRnoaccess));
2448 outsize = set_message(outbuf,12,nread,False);
2449 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be -1. */
2450 SSVAL(outbuf,smb_vwv5,nread);
2451 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
2452 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
2453 SSVAL(smb_buf(outbuf),-2,nread);
2455 DEBUG( 3, ( "send_file_readX fnum=%d max=%d nread=%d\n",
2456 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
2458 /* Returning the number of bytes we want to send back - including header. */
2462 /****************************************************************************
2463 Reply to a read and X.
2464 ****************************************************************************/
2466 int reply_read_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
2468 files_struct *fsp = file_fsp(inbuf,smb_vwv2);
2469 SMB_OFF_T startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
2471 size_t smb_maxcnt = SVAL(inbuf,smb_vwv5);
2473 size_t smb_mincnt = SVAL(inbuf,smb_vwv6);
2476 START_PROFILE(SMBreadX);
2478 /* If it's an IPC, pass off the pipe handler. */
2480 END_PROFILE(SMBreadX);
2481 return reply_pipe_read_and_X(inbuf,outbuf,length,bufsize);
2484 CHECK_FSP(fsp,conn);
2485 if (!CHECK_READ(fsp,inbuf)) {
2486 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2489 set_message(outbuf,12,0,True);
2491 if (global_client_caps & CAP_LARGE_READX) {
2492 if (SVAL(inbuf,smb_vwv7) == 1) {
2493 smb_maxcnt |= (1<<16);
2495 if (smb_maxcnt > BUFFER_SIZE) {
2496 DEBUG(0,("reply_read_and_X - read too large (%u) for reply buffer %u\n",
2497 (unsigned int)smb_maxcnt, (unsigned int)BUFFER_SIZE));
2498 END_PROFILE(SMBreadX);
2499 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2503 if(CVAL(inbuf,smb_wct) == 12) {
2504 #ifdef LARGE_SMB_OFF_T
2506 * This is a large offset (64 bit) read.
2508 startpos |= (((SMB_OFF_T)IVAL(inbuf,smb_vwv10)) << 32);
2510 #else /* !LARGE_SMB_OFF_T */
2513 * Ensure we haven't been sent a >32 bit offset.
2516 if(IVAL(inbuf,smb_vwv10) != 0) {
2517 DEBUG(0,("reply_read_and_X - large offset (%x << 32) used and we don't support \
2518 64 bit offsets.\n", (unsigned int)IVAL(inbuf,smb_vwv10) ));
2519 END_PROFILE(SMBreadX);
2520 return ERROR_DOS(ERRDOS,ERRbadaccess);
2523 #endif /* LARGE_SMB_OFF_T */
2527 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)smb_maxcnt,(SMB_BIG_UINT)startpos, READ_LOCK)) {
2528 END_PROFILE(SMBreadX);
2529 return ERROR_DOS(ERRDOS,ERRlock);
2532 if (schedule_aio_read_and_X(conn, inbuf, outbuf, length, bufsize, fsp, startpos, smb_maxcnt)) {
2533 END_PROFILE(SMBreadX);
2537 nread = send_file_readX(conn, inbuf, outbuf, length, bufsize, fsp, startpos, smb_maxcnt);
2539 nread = chain_reply(inbuf,outbuf,length,bufsize);
2541 END_PROFILE(SMBreadX);
2545 /****************************************************************************
2546 Reply to a writebraw (core+ or LANMAN1.0 protocol).
2547 ****************************************************************************/
2549 int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
2552 ssize_t total_written=0;
2553 size_t numtowrite=0;
2558 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2560 START_PROFILE(SMBwritebraw);
2562 if (srv_is_signing_active()) {
2563 exit_server_cleanly("reply_writebraw: SMB signing is active - raw reads/writes are disallowed.");
2566 CHECK_FSP(fsp,conn);
2567 if (!CHECK_WRITE(fsp)) {
2568 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2571 tcount = IVAL(inbuf,smb_vwv1);
2572 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
2573 write_through = BITSETW(inbuf+smb_vwv7,0);
2575 /* We have to deal with slightly different formats depending
2576 on whether we are using the core+ or lanman1.0 protocol */
2578 if(Protocol <= PROTOCOL_COREPLUS) {
2579 numtowrite = SVAL(smb_buf(inbuf),-2);
2580 data = smb_buf(inbuf);
2582 numtowrite = SVAL(inbuf,smb_vwv10);
2583 data = smb_base(inbuf) + SVAL(inbuf, smb_vwv11);
2586 /* force the error type */
2587 SCVAL(inbuf,smb_com,SMBwritec);
2588 SCVAL(outbuf,smb_com,SMBwritec);
2590 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
2591 END_PROFILE(SMBwritebraw);
2592 return(ERROR_DOS(ERRDOS,ERRlock));
2596 nwritten = write_file(fsp,data,startpos,numtowrite);
2598 DEBUG(3,("writebraw1 fnum=%d start=%.0f num=%d wrote=%d sync=%d\n",
2599 fsp->fnum, (double)startpos, (int)numtowrite, (int)nwritten, (int)write_through));
2601 if (nwritten < (ssize_t)numtowrite) {
2602 END_PROFILE(SMBwritebraw);
2603 return(UNIXERROR(ERRHRD,ERRdiskfull));
2606 total_written = nwritten;
2608 /* Return a message to the redirector to tell it to send more bytes */
2609 SCVAL(outbuf,smb_com,SMBwritebraw);
2610 SSVALS(outbuf,smb_vwv0,-1);
2611 outsize = set_message(outbuf,Protocol>PROTOCOL_COREPLUS?1:0,0,True);
2613 if (!send_smb(smbd_server_fd(),outbuf))
2614 exit_server_cleanly("reply_writebraw: send_smb failed.");
2616 /* Now read the raw data into the buffer and write it */
2617 if (read_smb_length(smbd_server_fd(),inbuf,SMB_SECONDARY_WAIT) == -1) {
2618 exit_server_cleanly("secondary writebraw failed");
2621 /* Even though this is not an smb message, smb_len returns the generic length of an smb message */
2622 numtowrite = smb_len(inbuf);
2624 /* Set up outbuf to return the correct return */
2625 outsize = set_message(outbuf,1,0,True);
2626 SCVAL(outbuf,smb_com,SMBwritec);
2628 if (numtowrite != 0) {
2630 if (numtowrite > BUFFER_SIZE) {
2631 DEBUG(0,("reply_writebraw: Oversize secondary write raw requested (%u). Terminating\n",
2632 (unsigned int)numtowrite ));
2633 exit_server_cleanly("secondary writebraw failed");
2636 if (tcount > nwritten+numtowrite) {
2637 DEBUG(3,("Client overestimated the write %d %d %d\n",
2638 (int)tcount,(int)nwritten,(int)numtowrite));
2641 if (read_data( smbd_server_fd(), inbuf+4, numtowrite) != numtowrite ) {
2642 DEBUG(0,("reply_writebraw: Oversize secondary write raw read failed (%s). Terminating\n",
2644 exit_server_cleanly("secondary writebraw failed");
2647 nwritten = write_file(fsp,inbuf+4,startpos+nwritten,numtowrite);
2648 if (nwritten == -1) {
2649 END_PROFILE(SMBwritebraw);
2650 return(UNIXERROR(ERRHRD,ERRdiskfull));
2653 if (nwritten < (ssize_t)numtowrite) {
2654 SCVAL(outbuf,smb_rcls,ERRHRD);
2655 SSVAL(outbuf,smb_err,ERRdiskfull);
2659 total_written += nwritten;
2662 SSVAL(outbuf,smb_vwv0,total_written);
2664 sync_file(conn, fsp, write_through);
2666 DEBUG(3,("writebraw2 fnum=%d start=%.0f num=%d wrote=%d\n",
2667 fsp->fnum, (double)startpos, (int)numtowrite,(int)total_written));
2669 /* we won't return a status if write through is not selected - this follows what WfWg does */
2670 END_PROFILE(SMBwritebraw);
2671 if (!write_through && total_written==tcount) {
2673 #if RABBIT_PELLET_FIX
2675 * Fix for "rabbit pellet" mode, trigger an early TCP ack by
2676 * sending a SMBkeepalive. Thanks to DaveCB at Sun for this. JRA.
2678 if (!send_keepalive(smbd_server_fd()))
2679 exit_server_cleanly("reply_writebraw: send of keepalive failed");
2688 #define DBGC_CLASS DBGC_LOCKING
2690 /****************************************************************************
2691 Reply to a writeunlock (core+).
2692 ****************************************************************************/
2694 int reply_writeunlock(connection_struct *conn, char *inbuf,char *outbuf,
2695 int size, int dum_buffsize)
2697 ssize_t nwritten = -1;
2701 NTSTATUS status = NT_STATUS_OK;
2702 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2704 START_PROFILE(SMBwriteunlock);
2706 CHECK_FSP(fsp,conn);
2707 if (!CHECK_WRITE(fsp)) {
2708 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2711 numtowrite = SVAL(inbuf,smb_vwv1);
2712 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2713 data = smb_buf(inbuf) + 3;
2715 if (numtowrite && is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
2716 END_PROFILE(SMBwriteunlock);
2717 return ERROR_DOS(ERRDOS,ERRlock);
2720 /* The special X/Open SMB protocol handling of
2721 zero length writes is *NOT* done for
2723 if(numtowrite == 0) {
2726 nwritten = write_file(fsp,data,startpos,numtowrite);
2729 sync_file(conn, fsp, False /* write through */);
2731 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
2732 END_PROFILE(SMBwriteunlock);
2733 return(UNIXERROR(ERRHRD,ERRdiskfull));
2737 status = do_unlock(fsp,
2738 (uint32)SVAL(inbuf,smb_pid),
2739 (SMB_BIG_UINT)numtowrite,
2740 (SMB_BIG_UINT)startpos,
2743 if (NT_STATUS_V(status)) {
2744 END_PROFILE(SMBwriteunlock);
2745 return ERROR_NT(status);
2749 outsize = set_message(outbuf,1,0,True);
2751 SSVAL(outbuf,smb_vwv0,nwritten);
2753 DEBUG(3,("writeunlock fnum=%d num=%d wrote=%d\n",
2754 fsp->fnum, (int)numtowrite, (int)nwritten));
2756 END_PROFILE(SMBwriteunlock);
2761 #define DBGC_CLASS DBGC_ALL
2763 /****************************************************************************
2765 ****************************************************************************/
2767 int reply_write(connection_struct *conn, char *inbuf,char *outbuf,int size,int dum_buffsize)
2770 ssize_t nwritten = -1;
2773 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2775 START_PROFILE(SMBwrite);
2777 /* If it's an IPC, pass off the pipe handler. */
2779 END_PROFILE(SMBwrite);
2780 return reply_pipe_write(inbuf,outbuf,size,dum_buffsize);
2783 CHECK_FSP(fsp,conn);
2784 if (!CHECK_WRITE(fsp)) {
2785 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2788 numtowrite = SVAL(inbuf,smb_vwv1);
2789 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2790 data = smb_buf(inbuf) + 3;
2792 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
2793 END_PROFILE(SMBwrite);
2794 return ERROR_DOS(ERRDOS,ERRlock);
2798 * X/Open SMB protocol says that if smb_vwv1 is
2799 * zero then the file size should be extended or
2800 * truncated to the size given in smb_vwv[2-3].
2803 if(numtowrite == 0) {
2805 * This is actually an allocate call, and set EOF. JRA.
2807 nwritten = vfs_allocate_file_space(fsp, (SMB_OFF_T)startpos);
2809 END_PROFILE(SMBwrite);
2810 return ERROR_NT(NT_STATUS_DISK_FULL);
2812 nwritten = vfs_set_filelen(fsp, (SMB_OFF_T)startpos);
2814 END_PROFILE(SMBwrite);
2815 return ERROR_NT(NT_STATUS_DISK_FULL);
2818 nwritten = write_file(fsp,data,startpos,numtowrite);
2820 sync_file(conn, fsp, False);
2822 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
2823 END_PROFILE(SMBwrite);
2824 return(UNIXERROR(ERRHRD,ERRdiskfull));
2827 outsize = set_message(outbuf,1,0,True);
2829 SSVAL(outbuf,smb_vwv0,nwritten);
2831 if (nwritten < (ssize_t)numtowrite) {
2832 SCVAL(outbuf,smb_rcls,ERRHRD);
2833 SSVAL(outbuf,smb_err,ERRdiskfull);
2836 DEBUG(3,("write fnum=%d num=%d wrote=%d\n", fsp->fnum, (int)numtowrite, (int)nwritten));
2838 END_PROFILE(SMBwrite);
2842 /****************************************************************************
2843 Reply to a write and X.
2844 ****************************************************************************/
2846 int reply_write_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
2848 files_struct *fsp = file_fsp(inbuf,smb_vwv2);
2849 SMB_OFF_T startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
2850 size_t numtowrite = SVAL(inbuf,smb_vwv10);
2851 BOOL write_through = BITSETW(inbuf+smb_vwv7,0);
2852 ssize_t nwritten = -1;
2853 unsigned int smb_doff = SVAL(inbuf,smb_vwv11);
2854 unsigned int smblen = smb_len(inbuf);
2856 BOOL large_writeX = ((CVAL(inbuf,smb_wct) == 14) && (smblen > 0xFFFF));
2857 START_PROFILE(SMBwriteX);
2859 /* If it's an IPC, pass off the pipe handler. */
2861 END_PROFILE(SMBwriteX);
2862 return reply_pipe_write_and_X(inbuf,outbuf,length,bufsize);
2865 CHECK_FSP(fsp,conn);
2866 if (!CHECK_WRITE(fsp)) {
2867 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2870 set_message(outbuf,6,0,True);
2872 /* Deal with possible LARGE_WRITEX */
2874 numtowrite |= ((((size_t)SVAL(inbuf,smb_vwv9)) & 1 )<<16);
2877 if(smb_doff > smblen || (smb_doff + numtowrite > smblen)) {
2878 END_PROFILE(SMBwriteX);
2879 return ERROR_DOS(ERRDOS,ERRbadmem);
2882 data = smb_base(inbuf) + smb_doff;
2884 if(CVAL(inbuf,smb_wct) == 14) {
2885 #ifdef LARGE_SMB_OFF_T
2887 * This is a large offset (64 bit) write.
2889 startpos |= (((SMB_OFF_T)IVAL(inbuf,smb_vwv12)) << 32);
2891 #else /* !LARGE_SMB_OFF_T */
2894 * Ensure we haven't been sent a >32 bit offset.
2897 if(IVAL(inbuf,smb_vwv12) != 0) {
2898 DEBUG(0,("reply_write_and_X - large offset (%x << 32) used and we don't support \
2899 64 bit offsets.\n", (unsigned int)IVAL(inbuf,smb_vwv12) ));
2900 END_PROFILE(SMBwriteX);
2901 return ERROR_DOS(ERRDOS,ERRbadaccess);
2904 #endif /* LARGE_SMB_OFF_T */
2907 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
2908 END_PROFILE(SMBwriteX);
2909 return ERROR_DOS(ERRDOS,ERRlock);
2912 /* X/Open SMB protocol says that, unlike SMBwrite
2913 if the length is zero then NO truncation is
2914 done, just a write of zero. To truncate a file,
2917 if(numtowrite == 0) {
2921 if (schedule_aio_write_and_X(conn, inbuf, outbuf, length, bufsize,
2922 fsp,data,startpos,numtowrite)) {
2923 END_PROFILE(SMBwriteX);
2927 nwritten = write_file(fsp,data,startpos,numtowrite);
2930 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
2931 END_PROFILE(SMBwriteX);
2932 return(UNIXERROR(ERRHRD,ERRdiskfull));
2935 SSVAL(outbuf,smb_vwv2,nwritten);
2937 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
2939 if (nwritten < (ssize_t)numtowrite) {
2940 SCVAL(outbuf,smb_rcls,ERRHRD);
2941 SSVAL(outbuf,smb_err,ERRdiskfull);
2944 DEBUG(3,("writeX fnum=%d num=%d wrote=%d\n",
2945 fsp->fnum, (int)numtowrite, (int)nwritten));
2947 sync_file(conn, fsp, write_through);
2949 END_PROFILE(SMBwriteX);
2950 return chain_reply(inbuf,outbuf,length,bufsize);
2953 /****************************************************************************
2955 ****************************************************************************/
2957 int reply_lseek(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
2963 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2964 START_PROFILE(SMBlseek);
2966 CHECK_FSP(fsp,conn);
2968 flush_write_cache(fsp, SEEK_FLUSH);
2970 mode = SVAL(inbuf,smb_vwv1) & 3;
2971 /* NB. This doesn't use IVAL_TO_SMB_OFF_T as startpos can be signed in this case. */
2972 startpos = (SMB_OFF_T)IVALS(inbuf,smb_vwv2);
2981 res = fsp->fh->pos + startpos;
2992 if (umode == SEEK_END) {
2993 if((res = SMB_VFS_LSEEK(fsp,fsp->fh->fd,startpos,umode)) == -1) {
2994 if(errno == EINVAL) {
2995 SMB_OFF_T current_pos = startpos;
2996 SMB_STRUCT_STAT sbuf;
2998 if(SMB_VFS_FSTAT(fsp,fsp->fh->fd, &sbuf) == -1) {
2999 END_PROFILE(SMBlseek);
3000 return(UNIXERROR(ERRDOS,ERRnoaccess));
3003 current_pos += sbuf.st_size;
3005 res = SMB_VFS_LSEEK(fsp,fsp->fh->fd,0,SEEK_SET);
3010 END_PROFILE(SMBlseek);
3011 return(UNIXERROR(ERRDOS,ERRnoaccess));
3017 outsize = set_message(outbuf,2,0,True);
3018 SIVAL(outbuf,smb_vwv0,res);
3020 DEBUG(3,("lseek fnum=%d ofs=%.0f newpos = %.0f mode=%d\n",
3021 fsp->fnum, (double)startpos, (double)res, mode));
3023 END_PROFILE(SMBlseek);
3027 /****************************************************************************
3029 ****************************************************************************/
3031 int reply_flush(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
3033 int outsize = set_message(outbuf,0,0,False);
3034 uint16 fnum = SVAL(inbuf,smb_vwv0);
3035 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3036 START_PROFILE(SMBflush);
3039 CHECK_FSP(fsp,conn);
3042 file_sync_all(conn);
3044 sync_file(conn,fsp, True);
3047 DEBUG(3,("flush\n"));
3048 END_PROFILE(SMBflush);
3052 /****************************************************************************
3054 conn POINTER CAN BE NULL HERE !
3055 ****************************************************************************/
3057 int reply_exit(connection_struct *conn,
3058 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3061 START_PROFILE(SMBexit);
3063 file_close_pid(SVAL(inbuf,smb_pid),SVAL(inbuf,smb_uid));
3065 outsize = set_message(outbuf,0,0,False);
3067 DEBUG(3,("exit\n"));
3069 END_PROFILE(SMBexit);
3073 /****************************************************************************
3074 Reply to a close - has to deal with closing a directory opened by NT SMB's.
3075 ****************************************************************************/
3077 int reply_close(connection_struct *conn, char *inbuf,char *outbuf, int size,
3082 int32 eclass = 0, err = 0;
3083 files_struct *fsp = NULL;
3084 START_PROFILE(SMBclose);
3086 outsize = set_message(outbuf,0,0,False);
3088 /* If it's an IPC, pass off to the pipe handler. */
3090 END_PROFILE(SMBclose);
3091 return reply_pipe_close(conn, inbuf,outbuf);
3094 fsp = file_fsp(inbuf,smb_vwv0);
3097 * We can only use CHECK_FSP if we know it's not a directory.
3100 if(!fsp || (fsp->conn != conn) || (fsp->vuid != current_user.vuid)) {
3101 END_PROFILE(SMBclose);
3102 return ERROR_DOS(ERRDOS,ERRbadfid);
3105 if(fsp->is_directory) {
3107 * Special case - close NT SMB directory handle.
3109 DEBUG(3,("close directory fnum=%d\n", fsp->fnum));
3110 close_file(fsp,NORMAL_CLOSE);
3113 * Close ordinary file.
3117 DEBUG(3,("close fd=%d fnum=%d (numopen=%d)\n",
3118 fsp->fh->fd, fsp->fnum,
3119 conn->num_files_open));
3122 * Take care of any time sent in the close.
3125 mtime = srv_make_unix_date3(inbuf+smb_vwv1);
3126 fsp_set_pending_modtime(fsp, mtime);
3129 * close_file() returns the unix errno if an error
3130 * was detected on close - normally this is due to
3131 * a disk full error. If not then it was probably an I/O error.
3134 if((close_err = close_file(fsp,NORMAL_CLOSE)) != 0) {
3136 END_PROFILE(SMBclose);
3137 return (UNIXERROR(ERRHRD,ERRgeneral));
3141 /* We have a cached error */
3143 END_PROFILE(SMBclose);
3144 return ERROR_DOS(eclass,err);
3147 END_PROFILE(SMBclose);
3151 /****************************************************************************
3152 Reply to a writeclose (Core+ protocol).
3153 ****************************************************************************/
3155 int reply_writeclose(connection_struct *conn,
3156 char *inbuf,char *outbuf, int size, int dum_buffsize)
3159 ssize_t nwritten = -1;
3165 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3166 START_PROFILE(SMBwriteclose);
3168 CHECK_FSP(fsp,conn);
3169 if (!CHECK_WRITE(fsp)) {
3170 return(ERROR_DOS(ERRDOS,ERRbadaccess));
3173 numtowrite = SVAL(inbuf,smb_vwv1);
3174 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
3175 mtime = srv_make_unix_date3(inbuf+smb_vwv4);
3176 data = smb_buf(inbuf) + 1;
3178 if (numtowrite && is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
3179 END_PROFILE(SMBwriteclose);
3180 return ERROR_DOS(ERRDOS,ERRlock);
3183 nwritten = write_file(fsp,data,startpos,numtowrite);
3185 set_filetime(conn, fsp->fsp_name,mtime);
3188 * More insanity. W2K only closes the file if writelen > 0.
3193 DEBUG(3,("reply_writeclose: zero length write doesn't close file %s\n",
3195 close_err = close_file(fsp,NORMAL_CLOSE);
3198 DEBUG(3,("writeclose fnum=%d num=%d wrote=%d (numopen=%d)\n",
3199 fsp->fnum, (int)numtowrite, (int)nwritten,
3200 conn->num_files_open));
3202 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
3203 END_PROFILE(SMBwriteclose);
3204 return(UNIXERROR(ERRHRD,ERRdiskfull));
3207 if(close_err != 0) {
3209 END_PROFILE(SMBwriteclose);
3210 return(UNIXERROR(ERRHRD,ERRgeneral));
3213 outsize = set_message(outbuf,1,0,True);
3215 SSVAL(outbuf,smb_vwv0,nwritten);
3216 END_PROFILE(SMBwriteclose);
3221 #define DBGC_CLASS DBGC_LOCKING
3223 /****************************************************************************
3225 ****************************************************************************/
3227 int reply_lock(connection_struct *conn,
3228 char *inbuf,char *outbuf, int length, int dum_buffsize)
3230 int outsize = set_message(outbuf,0,0,False);
3231 SMB_BIG_UINT count,offset;
3233 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3234 struct byte_range_lock *br_lck = NULL;
3236 START_PROFILE(SMBlock);
3238 CHECK_FSP(fsp,conn);
3240 release_level_2_oplocks_on_change(fsp);
3242 count = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv1);
3243 offset = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv3);
3245 DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
3246 fsp->fh->fd, fsp->fnum, (double)offset, (double)count));
3248 br_lck = do_lock(fsp,
3249 (uint32)SVAL(inbuf,smb_pid),
3254 False, /* Non-blocking lock. */
3257 TALLOC_FREE(br_lck);
3259 if (NT_STATUS_V(status)) {
3260 END_PROFILE(SMBlock);
3261 return ERROR_NT(status);
3264 END_PROFILE(SMBlock);
3268 /****************************************************************************
3270 ****************************************************************************/
3272 int reply_unlock(connection_struct *conn, char *inbuf,char *outbuf, int size,
3275 int outsize = set_message(outbuf,0,0,False);
3276 SMB_BIG_UINT count,offset;
3278 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3279 START_PROFILE(SMBunlock);
3281 CHECK_FSP(fsp,conn);
3283 count = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv1);
3284 offset = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv3);
3286 status = do_unlock(fsp,
3287 (uint32)SVAL(inbuf,smb_pid),
3292 if (NT_STATUS_V(status)) {
3293 END_PROFILE(SMBunlock);
3294 return ERROR_NT(status);
3297 DEBUG( 3, ( "unlock fd=%d fnum=%d offset=%.0f count=%.0f\n",
3298 fsp->fh->fd, fsp->fnum, (double)offset, (double)count ) );
3300 END_PROFILE(SMBunlock);
3305 #define DBGC_CLASS DBGC_ALL
3307 /****************************************************************************
3309 conn POINTER CAN BE NULL HERE !
3310 ****************************************************************************/
3312 int reply_tdis(connection_struct *conn,
3313 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3315 int outsize = set_message(outbuf,0,0,False);
3317 START_PROFILE(SMBtdis);
3319 vuid = SVAL(inbuf,smb_uid);
3322 DEBUG(4,("Invalid connection in tdis\n"));
3323 END_PROFILE(SMBtdis);
3324 return ERROR_DOS(ERRSRV,ERRinvnid);
3329 close_cnum(conn,vuid);
3331 END_PROFILE(SMBtdis);
3335 /****************************************************************************
3337 conn POINTER CAN BE NULL HERE !
3338 ****************************************************************************/
3340 int reply_echo(connection_struct *conn,
3341 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3343 int smb_reverb = SVAL(inbuf,smb_vwv0);
3345 unsigned int data_len = smb_buflen(inbuf);
3346 int outsize = set_message(outbuf,1,data_len,True);
3347 START_PROFILE(SMBecho);
3349 if (data_len > BUFFER_SIZE) {
3350 DEBUG(0,("reply_echo: data_len too large.\n"));
3351 END_PROFILE(SMBecho);
3355 /* copy any incoming data back out */
3357 memcpy(smb_buf(outbuf),smb_buf(inbuf),data_len);
3359 if (smb_reverb > 100) {
3360 DEBUG(0,("large reverb (%d)?? Setting to 100\n",smb_reverb));
3364 for (seq_num =1 ; seq_num <= smb_reverb ; seq_num++) {
3365 SSVAL(outbuf,smb_vwv0,seq_num);
3367 smb_setlen(outbuf,outsize - 4);
3370 if (!send_smb(smbd_server_fd(),outbuf))
3371 exit_server_cleanly("reply_echo: send_smb failed.");
3374 DEBUG(3,("echo %d times\n", smb_reverb));
3378 END_PROFILE(SMBecho);
3382 /****************************************************************************
3383 Reply to a printopen.
3384 ****************************************************************************/
3386 int reply_printopen(connection_struct *conn,
3387 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3393 START_PROFILE(SMBsplopen);
3395 if (!CAN_PRINT(conn)) {
3396 END_PROFILE(SMBsplopen);
3397 return ERROR_DOS(ERRDOS,ERRnoaccess);
3400 /* Open for exclusive use, write only. */
3401 status = print_fsp_open(conn, NULL, &fsp);
3403 if (!NT_STATUS_IS_OK(status)) {
3404 END_PROFILE(SMBsplopen);
3405 return(ERROR_NT(status));
3408 outsize = set_message(outbuf,1,0,True);
3409 SSVAL(outbuf,smb_vwv0,fsp->fnum);
3411 DEBUG(3,("openprint fd=%d fnum=%d\n",
3412 fsp->fh->fd, fsp->fnum));
3414 END_PROFILE(SMBsplopen);
3418 /****************************************************************************
3419 Reply to a printclose.
3420 ****************************************************************************/
3422 int reply_printclose(connection_struct *conn,
3423 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3425 int outsize = set_message(outbuf,0,0,False);
3426 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3428 START_PROFILE(SMBsplclose);
3430 CHECK_FSP(fsp,conn);
3432 if (!CAN_PRINT(conn)) {
3433 END_PROFILE(SMBsplclose);
3434 return ERROR_NT(NT_STATUS_DOS(ERRSRV, ERRerror));
3437 DEBUG(3,("printclose fd=%d fnum=%d\n",
3438 fsp->fh->fd,fsp->fnum));
3440 close_err = close_file(fsp,NORMAL_CLOSE);
3442 if(close_err != 0) {
3444 END_PROFILE(SMBsplclose);
3445 return(UNIXERROR(ERRHRD,ERRgeneral));
3448 END_PROFILE(SMBsplclose);
3452 /****************************************************************************
3453 Reply to a printqueue.
3454 ****************************************************************************/
3456 int reply_printqueue(connection_struct *conn,
3457 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3459 int outsize = set_message(outbuf,2,3,True);
3460 int max_count = SVAL(inbuf,smb_vwv0);
3461 int start_index = SVAL(inbuf,smb_vwv1);
3462 START_PROFILE(SMBsplretq);
3464 /* we used to allow the client to get the cnum wrong, but that
3465 is really quite gross and only worked when there was only
3466 one printer - I think we should now only accept it if they
3467 get it right (tridge) */
3468 if (!CAN_PRINT(conn)) {
3469 END_PROFILE(SMBsplretq);
3470 return ERROR_DOS(ERRDOS,ERRnoaccess);
3473 SSVAL(outbuf,smb_vwv0,0);
3474 SSVAL(outbuf,smb_vwv1,0);
3475 SCVAL(smb_buf(outbuf),0,1);
3476 SSVAL(smb_buf(outbuf),1,0);
3478 DEBUG(3,("printqueue start_index=%d max_count=%d\n",
3479 start_index, max_count));
3482 print_queue_struct *queue = NULL;
3483 print_status_struct status;
3484 char *p = smb_buf(outbuf) + 3;
3485 int count = print_queue_status(SNUM(conn), &queue, &status);
3486 int num_to_get = ABS(max_count);
3487 int first = (max_count>0?start_index:start_index+max_count+1);
3493 num_to_get = MIN(num_to_get,count-first);
3496 for (i=first;i<first+num_to_get;i++) {
3497 srv_put_dos_date2(p,0,queue[i].time);
3498 SCVAL(p,4,(queue[i].status==LPQ_PRINTING?2:3));
3499 SSVAL(p,5, queue[i].job);
3500 SIVAL(p,7,queue[i].size);
3502 srvstr_push(outbuf, p+12, queue[i].fs_user, 16, STR_ASCII);
3507 outsize = set_message(outbuf,2,28*count+3,False);
3508 SSVAL(outbuf,smb_vwv0,count);
3509 SSVAL(outbuf,smb_vwv1,(max_count>0?first+count:first-1));
3510 SCVAL(smb_buf(outbuf),0,1);
3511 SSVAL(smb_buf(outbuf),1,28*count);
3516 DEBUG(3,("%d entries returned in queue\n",count));
3519 END_PROFILE(SMBsplretq);
3523 /****************************************************************************
3524 Reply to a printwrite.
3525 ****************************************************************************/
3527 int reply_printwrite(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3530 int outsize = set_message(outbuf,0,0,False);
3532 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3534 START_PROFILE(SMBsplwr);
3536 if (!CAN_PRINT(conn)) {
3537 END_PROFILE(SMBsplwr);
3538 return ERROR_DOS(ERRDOS,ERRnoaccess);
3541 CHECK_FSP(fsp,conn);
3542 if (!CHECK_WRITE(fsp)) {
3543 return(ERROR_DOS(ERRDOS,ERRbadaccess));
3546 numtowrite = SVAL(smb_buf(inbuf),1);
3547 data = smb_buf(inbuf) + 3;
3549 if (write_file(fsp,data,-1,numtowrite) != numtowrite) {
3550 END_PROFILE(SMBsplwr);
3551 return(UNIXERROR(ERRHRD,ERRdiskfull));
3554 DEBUG( 3, ( "printwrite fnum=%d num=%d\n", fsp->fnum, numtowrite ) );
3556 END_PROFILE(SMBsplwr);
3560 /****************************************************************************
3562 ****************************************************************************/
3564 int reply_mkdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3569 SMB_STRUCT_STAT sbuf;
3571 START_PROFILE(SMBmkdir);
3573 srvstr_get_path(inbuf, directory, smb_buf(inbuf) + 1, sizeof(directory), 0, STR_TERMINATE, &status);
3574 if (!NT_STATUS_IS_OK(status)) {
3575 END_PROFILE(SMBmkdir);
3576 return ERROR_NT(status);
3579 RESOLVE_DFSPATH(directory, conn, inbuf, outbuf);
3581 status = unix_convert(conn, directory, False, NULL, &sbuf);
3582 if (!NT_STATUS_IS_OK(status)) {
3583 END_PROFILE(SMBmkdir);
3584 return ERROR_NT(status);
3587 status = create_directory(conn, directory);
3589 DEBUG(5, ("create_directory returned %s\n", nt_errstr(status)));
3591 if (!NT_STATUS_IS_OK(status)) {
3593 if (!use_nt_status()
3594 && NT_STATUS_EQUAL(status,
3595 NT_STATUS_OBJECT_NAME_COLLISION)) {
3597 * Yes, in the DOS error code case we get a
3598 * ERRDOS:ERRnoaccess here. See BASE-SAMBA3ERROR
3599 * samba4 torture test.
3601 status = NT_STATUS_DOS(ERRDOS, ERRnoaccess);
3604 END_PROFILE(SMBmkdir);
3605 return ERROR_NT(status);
3608 outsize = set_message(outbuf,0,0,False);
3610 DEBUG( 3, ( "mkdir %s ret=%d\n", directory, outsize ) );
3612 END_PROFILE(SMBmkdir);
3616 /****************************************************************************
3617 Static function used by reply_rmdir to delete an entire directory
3618 tree recursively. Return True on ok, False on fail.
3619 ****************************************************************************/
3621 static BOOL recursive_rmdir(connection_struct *conn, char *directory)
3623 const char *dname = NULL;
3626 struct smb_Dir *dir_hnd = OpenDir(conn, directory, NULL, 0);
3631 while((dname = ReadDirName(dir_hnd, &offset))) {
3635 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
3638 if (!is_visible_file(conn, directory, dname, &st, False))
3641 /* Construct the full name. */
3642 if(strlen(directory) + strlen(dname) + 1 >= sizeof(fullname)) {
3648 pstrcpy(fullname, directory);
3649 pstrcat(fullname, "/");
3650 pstrcat(fullname, dname);
3652 if(SMB_VFS_LSTAT(conn,fullname, &st) != 0) {
3657 if(st.st_mode & S_IFDIR) {
3658 if(!recursive_rmdir(conn, fullname)) {
3662 if(SMB_VFS_RMDIR(conn,fullname) != 0) {
3666 } else if(SMB_VFS_UNLINK(conn,fullname) != 0) {
3675 /****************************************************************************
3676 The internals of the rmdir code - called elsewhere.
3677 ****************************************************************************/
3679 BOOL rmdir_internals(connection_struct *conn, const char *directory)
3684 ret = SMB_VFS_RMDIR(conn,directory);
3689 if(((errno == ENOTEMPTY)||(errno == EEXIST)) && lp_veto_files(SNUM(conn))) {
3691 * Check to see if the only thing in this directory are
3692 * vetoed files/directories. If so then delete them and
3693 * retry. If we fail to delete any of them (and we *don't*
3694 * do a recursive delete) then fail the rmdir.
3698 struct smb_Dir *dir_hnd = OpenDir(conn, directory, NULL, 0);
3700 if(dir_hnd == NULL) {
3705 while ((dname = ReadDirName(dir_hnd,&dirpos))) {
3706 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
3708 if (!is_visible_file(conn, directory, dname, &st, False))
3710 if(!IS_VETO_PATH(conn, dname)) {
3717 /* We only have veto files/directories. Recursive delete. */
3719 RewindDir(dir_hnd,&dirpos);
3720 while ((dname = ReadDirName(dir_hnd,&dirpos))) {
3723 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
3725 if (!is_visible_file(conn, directory, dname, &st, False))
3728 /* Construct the full name. */
3729 if(strlen(directory) + strlen(dname) + 1 >= sizeof(fullname)) {
3734 pstrcpy(fullname, directory);
3735 pstrcat(fullname, "/");
3736 pstrcat(fullname, dname);
3738 if(SMB_VFS_LSTAT(conn,fullname, &st) != 0)
3740 if(st.st_mode & S_IFDIR) {
3741 if(lp_recursive_veto_delete(SNUM(conn))) {
3742 if(!recursive_rmdir(conn, fullname))
3745 if(SMB_VFS_RMDIR(conn,fullname) != 0)
3747 } else if(SMB_VFS_UNLINK(conn,fullname) != 0)
3751 /* Retry the rmdir */
3752 ret = SMB_VFS_RMDIR(conn,directory);
3758 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
3759 "%s\n", directory,strerror(errno)));
3766 /****************************************************************************
3768 ****************************************************************************/
3770 int reply_rmdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3774 SMB_STRUCT_STAT sbuf;
3776 START_PROFILE(SMBrmdir);
3778 srvstr_get_path(inbuf, directory, smb_buf(inbuf) + 1, sizeof(directory), 0, STR_TERMINATE, &status);
3779 if (!NT_STATUS_IS_OK(status)) {
3780 END_PROFILE(SMBrmdir);
3781 return ERROR_NT(status);
3784 RESOLVE_DFSPATH(directory, conn, inbuf, outbuf)
3786 status = unix_convert(conn, directory, False, NULL, &sbuf);
3787 if (!NT_STATUS_IS_OK(status)) {
3788 END_PROFILE(SMBrmdir);
3789 return ERROR_NT(status);
3792 if (!check_name(directory,conn)) {
3793 END_PROFILE(SMBrmdir);
3794 return UNIXERROR(ERRDOS, ERRbadpath);
3797 dptr_closepath(directory,SVAL(inbuf,smb_pid));
3798 if (!rmdir_internals(conn, directory)) {
3799 END_PROFILE(SMBrmdir);
3800 return UNIXERROR(ERRDOS, ERRbadpath);
3803 outsize = set_message(outbuf,0,0,False);
3805 DEBUG( 3, ( "rmdir %s\n", directory ) );
3807 END_PROFILE(SMBrmdir);
3811 /*******************************************************************
3812 Resolve wildcards in a filename rename.
3813 Note that name is in UNIX charset and thus potentially can be more
3814 than fstring buffer (255 bytes) especially in default UTF-8 case.
3815 Therefore, we use pstring inside and all calls should ensure that
3816 name2 is at least pstring-long (they do already)
3817 ********************************************************************/
3819 static BOOL resolve_wildcards(const char *name1, char *name2)
3821 pstring root1,root2;
3823 char *p,*p2, *pname1, *pname2;
3824 int available_space, actual_space;
3827 pname1 = strrchr_m(name1,'/');
3828 pname2 = strrchr_m(name2,'/');
3830 if (!pname1 || !pname2)
3833 pstrcpy(root1,pname1);
3834 pstrcpy(root2,pname2);
3835 p = strrchr_m(root1,'.');
3842 p = strrchr_m(root2,'.');
3856 } else if (*p2 == '*') {
3872 } else if (*p2 == '*') {
3882 available_space = sizeof(pstring) - PTR_DIFF(pname2, name2);
3885 actual_space = snprintf(pname2, available_space - 1, "%s.%s", root2, ext2);
3886 if (actual_space >= available_space - 1) {
3887 DEBUG(1,("resolve_wildcards: can't fit resolved name into specified buffer (overrun by %d bytes)\n",
3888 actual_space - available_space));
3891 pstrcpy_base(pname2, root2, name2);
3897 /****************************************************************************
3898 Ensure open files have their names updated. Updated to notify other smbd's
3900 ****************************************************************************/
3902 static void rename_open_files(connection_struct *conn, struct share_mode_lock *lck,
3903 SMB_DEV_T dev, SMB_INO_T inode, const char *newname)
3906 BOOL did_rename = False;
3908 for(fsp = file_find_di_first(dev, inode); fsp; fsp = file_find_di_next(fsp)) {
3909 /* fsp_name is a relative path under the fsp. To change this for other
3910 sharepaths we need to manipulate relative paths. */
3911 /* TODO - create the absolute path and manipulate the newname
3912 relative to the sharepath. */
3913 if (fsp->conn != conn) {
3916 DEBUG(10,("rename_open_files: renaming file fnum %d (dev = %x, inode = %.0f) from %s -> %s\n",
3917 fsp->fnum, (unsigned int)fsp->dev, (double)fsp->inode,
3918 fsp->fsp_name, newname ));
3919 string_set(&fsp->fsp_name, newname);
3924 DEBUG(10,("rename_open_files: no open files on dev %x, inode %.0f for %s\n",
3925 (unsigned int)dev, (double)inode, newname ));
3928 /* Send messages to all smbd's (not ourself) that the name has changed. */
3929 rename_share_filename(lck, conn->connectpath, newname);
3932 /****************************************************************************
3933 We need to check if the source path is a parent directory of the destination
3934 (ie. a rename of /foo/bar/baz -> /foo/bar/baz/bibble/bobble. If so we must
3935 refuse the rename with a sharing violation. Under UNIX the above call can
3936 *succeed* if /foo/bar/baz is a symlink to another area in the share. We
3937 probably need to check that the client is a Windows one before disallowing
3938 this as a UNIX client (one with UNIX extensions) can know the source is a
3939 symlink and make this decision intelligently. Found by an excellent bug
3940 report from <AndyLiebman@aol.com>.
3941 ****************************************************************************/
3943 static BOOL rename_path_prefix_equal(const char *src, const char *dest)
3945 const char *psrc = src;
3946 const char *pdst = dest;
3949 if (psrc[0] == '.' && psrc[1] == '/') {
3952 if (pdst[0] == '.' && pdst[1] == '/') {
3955 if ((slen = strlen(psrc)) > strlen(pdst)) {
3958 return ((memcmp(psrc, pdst, slen) == 0) && pdst[slen] == '/');
3961 /****************************************************************************
3962 Rename an open file - given an fsp.
3963 ****************************************************************************/
3965 NTSTATUS rename_internals_fsp(connection_struct *conn, files_struct *fsp, char *newname, uint32 attrs, BOOL replace_if_exists)
3967 SMB_STRUCT_STAT sbuf;
3968 pstring newname_last_component;
3969 NTSTATUS status = NT_STATUS_OK;
3971 struct share_mode_lock *lck = NULL;
3975 status = unix_convert(conn, newname, False, newname_last_component, &sbuf);
3976 if (!NT_STATUS_IS_OK(status)) {
3980 /* Ensure newname contains a '/' */
3981 if(strrchr_m(newname,'/') == 0) {
3984 pstrcpy(tmpstr, "./");
3985 pstrcat(tmpstr, newname);
3986 pstrcpy(newname, tmpstr);
3990 * Check for special case with case preserving and not
3991 * case sensitive. If the old last component differs from the original
3992 * last component only by case, then we should allow
3993 * the rename (user is trying to change the case of the
3997 if((conn->case_sensitive == False) && (conn->case_preserve == True) &&
3998 strequal(newname, fsp->fsp_name)) {
4000 pstring newname_modified_last_component;
4003 * Get the last component of the modified name.
4004 * Note that we guarantee that newname contains a '/'
4007 p = strrchr_m(newname,'/');
4008 pstrcpy(newname_modified_last_component,p+1);
4010 if(strcsequal(newname_modified_last_component,
4011 newname_last_component) == False) {
4013 * Replace the modified last component with
4016 pstrcpy(p+1, newname_last_component);
4021 * If the src and dest names are identical - including case,
4022 * don't do the rename, just return success.
4025 if (strcsequal(fsp->fsp_name, newname)) {
4026 DEBUG(3,("rename_internals_fsp: identical names in rename %s - returning success\n",
4028 return NT_STATUS_OK;
4031 dest_exists = vfs_object_exist(conn,newname,NULL);
4033 if(!replace_if_exists && dest_exists) {
4034 DEBUG(3,("rename_internals_fsp: dest exists doing rename %s -> %s\n",
4035 fsp->fsp_name,newname));
4036 return NT_STATUS_OBJECT_NAME_COLLISION;
4039 status = can_rename(conn,newname,attrs,&sbuf);
4041 if (dest_exists && !NT_STATUS_IS_OK(status)) {
4042 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4043 nt_errstr(status), fsp->fsp_name,newname));
4044 if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION))
4045 status = NT_STATUS_ACCESS_DENIED;
4049 if (rename_path_prefix_equal(fsp->fsp_name, newname)) {
4050 return NT_STATUS_ACCESS_DENIED;
4053 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
4055 if(SMB_VFS_RENAME(conn,fsp->fsp_name, newname) == 0) {
4056 DEBUG(3,("rename_internals_fsp: succeeded doing rename on %s -> %s\n",
4057 fsp->fsp_name,newname));
4058 rename_open_files(conn, lck, fsp->dev, fsp->inode, newname);
4060 return NT_STATUS_OK;
4065 if (errno == ENOTDIR || errno == EISDIR) {
4066 status = NT_STATUS_OBJECT_NAME_COLLISION;
4068 status = map_nt_error_from_unix(errno);
4071 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
4072 nt_errstr(status), fsp->fsp_name,newname));
4077 /****************************************************************************
4078 The guts of the rename command, split out so it may be called by the NT SMB
4080 ****************************************************************************/
4082 NTSTATUS rename_internals(connection_struct *conn, char *name, char *newname, uint32 attrs, BOOL replace_if_exists, BOOL has_wild)
4086 pstring last_component_src;
4087 pstring last_component_dest;
4090 NTSTATUS status = NT_STATUS_OK;
4091 SMB_STRUCT_STAT sbuf1, sbuf2;
4092 struct share_mode_lock *lck = NULL;
4094 *directory = *mask = 0;
4099 status = unix_convert(conn, name, has_wild, last_component_src, &sbuf1);
4100 if (!NT_STATUS_IS_OK(status)) {
4104 status = unix_convert(conn, newname, True, last_component_dest, &sbuf2);
4105 if (!NT_STATUS_IS_OK(status)) {
4110 * Split the old name into directory and last component
4111 * strings. Note that unix_convert may have stripped off a
4112 * leading ./ from both name and newname if the rename is
4113 * at the root of the share. We need to make sure either both
4114 * name and newname contain a / character or neither of them do
4115 * as this is checked in resolve_wildcards().
4118 p = strrchr_m(name,'/');
4120 pstrcpy(directory,".");
4124 pstrcpy(directory,name);
4126 *p = '/'; /* Replace needed for exceptional test below. */
4130 * We should only check the mangled cache
4131 * here if unix_convert failed. This means
4132 * that the path in 'mask' doesn't exist
4133 * on the file system and so we need to look
4134 * for a possible mangle. This patch from
4135 * Tine Smukavec <valentin.smukavec@hermes.si>.
4138 if (!VALID_STAT(sbuf1) && mangle_is_mangled(mask, conn->params))
4139 mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
4143 * No wildcards - just process the one file.
4145 BOOL is_short_name = mangle_is_8_3(name, True, conn->params);
4147 /* Add a terminating '/' to the directory name. */
4148 pstrcat(directory,"/");
4149 pstrcat(directory,mask);
4151 /* Ensure newname contains a '/' also */
4152 if(strrchr_m(newname,'/') == 0) {
4155 pstrcpy(tmpstr, "./");
4156 pstrcat(tmpstr, newname);
4157 pstrcpy(newname, tmpstr);
4160 DEBUG(3,("rename_internals: case_sensitive = %d, case_preserve = %d, short case preserve = %d, \
4161 directory = %s, newname = %s, last_component_dest = %s, is_8_3 = %d\n",
4162 conn->case_sensitive, conn->case_preserve, conn->short_case_preserve, directory,
4163 newname, last_component_dest, is_short_name));
4166 * Check for special case with case preserving and not
4167 * case sensitive, if directory and newname are identical,
4168 * and the old last component differs from the original
4169 * last component only by case, then we should allow
4170 * the rename (user is trying to change the case of the
4173 if((conn->case_sensitive == False) &&
4174 (((conn->case_preserve == True) &&
4175 (is_short_name == False)) ||
4176 ((conn->short_case_preserve == True) &&
4177 (is_short_name == True))) &&
4178 strcsequal(directory, newname)) {
4179 pstring modified_last_component;
4182 * Get the last component of the modified name.
4183 * Note that we guarantee that newname contains a '/'
4186 p = strrchr_m(newname,'/');
4187 pstrcpy(modified_last_component,p+1);
4189 if(strcsequal(modified_last_component,
4190 last_component_dest) == False) {
4192 * Replace the modified last component with
4195 pstrcpy(p+1, last_component_dest);
4199 resolve_wildcards(directory,newname);
4202 * The source object must exist.
4205 if (!vfs_object_exist(conn, directory, &sbuf1)) {
4206 DEBUG(3,("rename_internals: source doesn't exist doing rename %s -> %s\n",
4207 directory,newname));
4209 if (errno == ENOTDIR || errno == EISDIR || errno == ENOENT) {
4211 * Must return different errors depending on whether the parent
4212 * directory existed or not.
4215 p = strrchr_m(directory, '/');
4217 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4219 if (vfs_object_exist(conn, directory, NULL))
4220 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4221 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
4223 status = map_nt_error_from_unix(errno);
4224 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4225 nt_errstr(status), directory,newname));
4230 status = can_rename(conn,directory,attrs,&sbuf1);
4232 if (!NT_STATUS_IS_OK(status)) {
4233 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4234 nt_errstr(status), directory,newname));
4239 * If the src and dest names are identical - including case,
4240 * don't do the rename, just return success.
4243 if (strcsequal(directory, newname)) {
4244 rename_open_files(conn, NULL, sbuf1.st_dev, sbuf1.st_ino, newname);
4245 DEBUG(3,("rename_internals: identical names in rename %s - returning success\n", directory));
4246 return NT_STATUS_OK;
4249 if(!replace_if_exists && vfs_object_exist(conn,newname,NULL)) {
4250 DEBUG(3,("rename_internals: dest exists doing rename %s -> %s\n",
4251 directory,newname));
4252 return NT_STATUS_OBJECT_NAME_COLLISION;
4255 if (rename_path_prefix_equal(directory, newname)) {
4256 return NT_STATUS_SHARING_VIOLATION;
4259 lck = get_share_mode_lock(NULL, sbuf1.st_dev, sbuf1.st_ino, NULL, NULL);
4261 if(SMB_VFS_RENAME(conn,directory, newname) == 0) {
4262 DEBUG(3,("rename_internals: succeeded doing rename on %s -> %s\n",
4263 directory,newname));
4264 rename_open_files(conn, lck, sbuf1.st_dev, sbuf1.st_ino, newname);
4266 return NT_STATUS_OK;
4270 if (errno == ENOTDIR || errno == EISDIR)
4271 status = NT_STATUS_OBJECT_NAME_COLLISION;
4273 status = map_nt_error_from_unix(errno);
4275 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4276 nt_errstr(status), directory,newname));
4281 * Wildcards - process each file that matches.
4283 struct smb_Dir *dir_hnd = NULL;
4287 if (strequal(mask,"????????.???"))
4290 if (check_name(directory,conn))
4291 dir_hnd = OpenDir(conn, directory, mask, attrs);
4295 status = NT_STATUS_NO_SUCH_FILE;
4296 /* Was status = NT_STATUS_OBJECT_NAME_NOT_FOUND; - gentest fix. JRA */
4298 while ((dname = ReadDirName(dir_hnd, &offset))) {
4300 BOOL sysdir_entry = False;
4302 pstrcpy(fname,dname);
4304 /* Quick check for "." and ".." */
4305 if (fname[0] == '.') {
4306 if (!fname[1] || (fname[1] == '.' && !fname[2])) {
4308 sysdir_entry = True;
4315 if (!is_visible_file(conn, directory, dname, &sbuf1, False))
4318 if(!mask_match(fname, mask, conn->case_sensitive))
4322 status = NT_STATUS_OBJECT_NAME_INVALID;
4326 status = NT_STATUS_ACCESS_DENIED;
4327 slprintf(fname,sizeof(fname)-1,"%s/%s",directory,dname);
4328 if (!vfs_object_exist(conn, fname, &sbuf1)) {
4329 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4330 DEBUG(6,("rename %s failed. Error %s\n", fname, nt_errstr(status)));
4333 status = can_rename(conn,fname,attrs,&sbuf1);
4334 if (!NT_STATUS_IS_OK(status)) {
4335 DEBUG(6,("rename %s refused\n", fname));
4338 pstrcpy(destname,newname);
4340 if (!resolve_wildcards(fname,destname)) {
4341 DEBUG(6,("resolve_wildcards %s %s failed\n",
4346 if (strcsequal(fname,destname)) {
4347 rename_open_files(conn, NULL, sbuf1.st_dev, sbuf1.st_ino, newname);
4348 DEBUG(3,("rename_internals: identical names in wildcard rename %s - success\n", fname));
4350 status = NT_STATUS_OK;
4354 if (!replace_if_exists &&
4355 vfs_file_exist(conn,destname, NULL)) {
4356 DEBUG(6,("file_exist %s\n", destname));
4357 status = NT_STATUS_OBJECT_NAME_COLLISION;
4361 if (rename_path_prefix_equal(fname, destname)) {
4362 return NT_STATUS_SHARING_VIOLATION;
4365 lck = get_share_mode_lock(NULL, sbuf1.st_dev, sbuf1.st_ino, NULL, NULL);
4367 if (!SMB_VFS_RENAME(conn,fname,destname)) {
4368 rename_open_files(conn, lck, sbuf1.st_dev, sbuf1.st_ino, newname);
4370 status = NT_STATUS_OK;
4373 DEBUG(3,("rename_internals: doing rename on %s -> %s\n",fname,destname));
4379 /* Don't think needed any more - JRA. */
4380 if (!NT_STATUS_EQUAL(error,NT_STATUS_NO_SUCH_FILE)) {
4381 if (!rcdest && bad_path_dest) {
4382 if (ms_has_wild(last_component_dest))
4383 return NT_STATUS_OBJECT_NAME_INVALID;
4384 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
4391 if (count == 0 && NT_STATUS_IS_OK(status)) {
4392 status = map_nt_error_from_unix(errno);
4398 /****************************************************************************
4400 ****************************************************************************/
4402 int reply_mv(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
4409 uint32 attrs = SVAL(inbuf,smb_vwv0);
4411 BOOL path1_contains_wcard = False;
4412 BOOL path2_contains_wcard = False;
4414 START_PROFILE(SMBmv);
4416 p = smb_buf(inbuf) + 1;
4417 p += srvstr_get_path_wcard(inbuf, name, p, sizeof(name), 0, STR_TERMINATE, &status, &path1_contains_wcard);
4418 if (!NT_STATUS_IS_OK(status)) {
4420 return ERROR_NT(status);
4423 p += srvstr_get_path_wcard(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status, &path2_contains_wcard);
4424 if (!NT_STATUS_IS_OK(status)) {
4426 return ERROR_NT(status);
4429 RESOLVE_DFSPATH_WCARD(name, conn, inbuf, outbuf);
4430 RESOLVE_DFSPATH_WCARD(newname, conn, inbuf, outbuf);
4432 DEBUG(3,("reply_mv : %s -> %s\n",name,newname));
4434 status = rename_internals(conn, name, newname, attrs, False, path1_contains_wcard);
4435 if (!NT_STATUS_IS_OK(status)) {
4437 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
4438 /* We have re-scheduled this call. */
4441 return ERROR_NT(status);
4445 * Win2k needs a changenotify request response before it will
4446 * update after a rename..
4448 process_pending_change_notify_queue((time_t)0);
4449 outsize = set_message(outbuf,0,0,False);
4455 /*******************************************************************
4456 Copy a file as part of a reply_copy.
4457 ******************************************************************/
4460 * TODO: check error codes on all callers
4463 NTSTATUS copy_file(char *src, char *dest1,connection_struct *conn, int ofun,
4464 int count, BOOL target_is_directory)
4466 SMB_STRUCT_STAT src_sbuf, sbuf2;
4468 files_struct *fsp1,*fsp2;
4471 uint32 new_create_disposition;
4475 pstrcpy(dest,dest1);
4476 if (target_is_directory) {
4477 char *p = strrchr_m(src,'/');
4487 if (!vfs_file_exist(conn,src,&src_sbuf)) {
4488 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4491 if (!target_is_directory && count) {
4492 new_create_disposition = FILE_OPEN;
4494 if (!map_open_params_to_ntcreate(dest1,0,ofun,
4495 NULL, NULL, &new_create_disposition, NULL)) {
4496 return NT_STATUS_INVALID_PARAMETER;
4500 status = open_file_ntcreate(conn,src,&src_sbuf,
4502 FILE_SHARE_READ|FILE_SHARE_WRITE,
4505 FILE_ATTRIBUTE_NORMAL,
4509 if (!NT_STATUS_IS_OK(status)) {
4513 dosattrs = dos_mode(conn, src, &src_sbuf);
4514 if (SMB_VFS_STAT(conn,dest,&sbuf2) == -1) {
4515 ZERO_STRUCTP(&sbuf2);
4518 status = open_file_ntcreate(conn,dest,&sbuf2,
4520 FILE_SHARE_READ|FILE_SHARE_WRITE,
4521 new_create_disposition,
4527 if (!NT_STATUS_IS_OK(status)) {
4528 close_file(fsp1,ERROR_CLOSE);
4532 if ((ofun&3) == 1) {
4533 if(SMB_VFS_LSEEK(fsp2,fsp2->fh->fd,0,SEEK_END) == -1) {
4534 DEBUG(0,("copy_file: error - vfs lseek returned error %s\n", strerror(errno) ));
4536 * Stop the copy from occurring.
4539 src_sbuf.st_size = 0;
4543 if (src_sbuf.st_size) {
4544 ret = vfs_transfer_file(fsp1, fsp2, src_sbuf.st_size);
4547 close_file(fsp1,NORMAL_CLOSE);
4549 /* Ensure the modtime is set correctly on the destination file. */
4550 fsp_set_pending_modtime( fsp2, src_sbuf.st_mtime);
4553 * As we are opening fsp1 read-only we only expect
4554 * an error on close on fsp2 if we are out of space.
4555 * Thus we don't look at the error return from the
4558 close_err = close_file(fsp2,NORMAL_CLOSE);
4560 if (close_err != 0) {
4561 return map_nt_error_from_unix(close_err);
4564 if (ret != (SMB_OFF_T)src_sbuf.st_size) {
4565 return NT_STATUS_DISK_FULL;
4568 return NT_STATUS_OK;
4571 /****************************************************************************
4572 Reply to a file copy.
4573 ****************************************************************************/
4575 int reply_copy(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
4580 pstring mask,newname;
4583 int error = ERRnoaccess;
4587 int tid2 = SVAL(inbuf,smb_vwv0);
4588 int ofun = SVAL(inbuf,smb_vwv1);
4589 int flags = SVAL(inbuf,smb_vwv2);
4590 BOOL target_is_directory=False;
4591 BOOL path_contains_wcard1 = False;
4592 BOOL path_contains_wcard2 = False;
4593 SMB_STRUCT_STAT sbuf1, sbuf2;
4595 START_PROFILE(SMBcopy);
4597 *directory = *mask = 0;
4600 p += srvstr_get_path_wcard(inbuf, name, p, sizeof(name), 0, STR_TERMINATE, &status, &path_contains_wcard1);
4601 if (!NT_STATUS_IS_OK(status)) {
4602 END_PROFILE(SMBcopy);
4603 return ERROR_NT(status);
4605 p += srvstr_get_path_wcard(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status, &path_contains_wcard2);
4606 if (!NT_STATUS_IS_OK(status)) {
4607 END_PROFILE(SMBcopy);
4608 return ERROR_NT(status);
4611 DEBUG(3,("reply_copy : %s -> %s\n",name,newname));
4613 if (tid2 != conn->cnum) {
4614 /* can't currently handle inter share copies XXXX */
4615 DEBUG(3,("Rejecting inter-share copy\n"));
4616 END_PROFILE(SMBcopy);
4617 return ERROR_DOS(ERRSRV,ERRinvdevice);
4620 RESOLVE_DFSPATH_WCARD(name, conn, inbuf, outbuf);
4621 RESOLVE_DFSPATH_WCARD(newname, conn, inbuf, outbuf);
4623 status = unix_convert(conn, name, path_contains_wcard1, NULL, &sbuf1);
4624 if (!NT_STATUS_IS_OK(status)) {
4625 END_PROFILE(SMBcopy);
4626 return ERROR_NT(status);
4629 status = unix_convert(conn, newname, path_contains_wcard2, NULL, &sbuf2);
4630 if (!NT_STATUS_IS_OK(status)) {
4631 END_PROFILE(SMBcopy);
4632 return ERROR_NT(status);
4635 target_is_directory = VALID_STAT_OF_DIR(sbuf2);
4637 if ((flags&1) && target_is_directory) {
4638 END_PROFILE(SMBcopy);
4639 return ERROR_DOS(ERRDOS,ERRbadfile);
4642 if ((flags&2) && !target_is_directory) {
4643 END_PROFILE(SMBcopy);
4644 return ERROR_DOS(ERRDOS,ERRbadpath);
4647 if ((flags&(1<<5)) && VALID_STAT_OF_DIR(sbuf1)) {
4648 /* wants a tree copy! XXXX */
4649 DEBUG(3,("Rejecting tree copy\n"));
4650 END_PROFILE(SMBcopy);
4651 return ERROR_DOS(ERRSRV,ERRerror);
4654 p = strrchr_m(name,'/');
4656 pstrcpy(directory,"./");
4660 pstrcpy(directory,name);
4665 * We should only check the mangled cache
4666 * here if unix_convert failed. This means
4667 * that the path in 'mask' doesn't exist
4668 * on the file system and so we need to look
4669 * for a possible mangle. This patch from
4670 * Tine Smukavec <valentin.smukavec@hermes.si>.
4673 if (!VALID_STAT(sbuf1) && mangle_is_mangled(mask, conn->params))
4674 mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
4676 has_wild = path_contains_wcard1;
4679 pstrcat(directory,"/");
4680 pstrcat(directory,mask);
4681 if (resolve_wildcards(directory,newname)
4682 && NT_STATUS_IS_OK(status = copy_file(
4683 directory,newname,conn,ofun,
4684 count,target_is_directory)))
4686 if(!count && !NT_STATUS_IS_OK(status)) {
4687 END_PROFILE(SMBcopy);
4688 return ERROR_NT(status);
4691 exists = vfs_file_exist(conn,directory,NULL);
4694 struct smb_Dir *dir_hnd = NULL;
4698 if (strequal(mask,"????????.???"))
4701 if (check_name(directory,conn))
4702 dir_hnd = OpenDir(conn, directory, mask, 0);
4708 while ((dname = ReadDirName(dir_hnd, &offset))) {
4710 pstrcpy(fname,dname);
4712 if (!is_visible_file(conn, directory, dname, &sbuf1, False))
4715 if(!mask_match(fname, mask, conn->case_sensitive))
4718 error = ERRnoaccess;
4719 slprintf(fname,sizeof(fname)-1, "%s/%s",directory,dname);
4720 pstrcpy(destname,newname);
4721 if (resolve_wildcards(fname,destname)
4722 && NT_STATUS_IS_OK(status = copy_file(
4723 fname,destname,conn,ofun,
4724 count,target_is_directory)))
4726 DEBUG(3,("reply_copy : doing copy on %s -> %s\n",fname,destname));
4734 /* Error on close... */
4736 END_PROFILE(SMBcopy);
4737 return(UNIXERROR(ERRHRD,ERRgeneral));
4740 END_PROFILE(SMBcopy);
4741 return ERROR_DOS(ERRDOS,error);
4744 outsize = set_message(outbuf,1,0,True);
4745 SSVAL(outbuf,smb_vwv0,count);
4747 END_PROFILE(SMBcopy);
4751 /****************************************************************************
4753 ****************************************************************************/
4755 int reply_setdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
4762 START_PROFILE(pathworks_setdir);
4765 if (!CAN_SETDIR(snum)) {
4766 END_PROFILE(pathworks_setdir);
4767 return ERROR_DOS(ERRDOS,ERRnoaccess);
4770 srvstr_get_path(inbuf, newdir, smb_buf(inbuf) + 1, sizeof(newdir), 0, STR_TERMINATE, &status);
4771 if (!NT_STATUS_IS_OK(status)) {
4772 END_PROFILE(pathworks_setdir);
4773 return ERROR_NT(status);
4776 RESOLVE_DFSPATH(newdir, conn, inbuf, outbuf);
4778 if (strlen(newdir) != 0) {
4779 if (!vfs_directory_exist(conn,newdir,NULL)) {
4780 END_PROFILE(pathworks_setdir);
4781 return ERROR_DOS(ERRDOS,ERRbadpath);
4783 set_conn_connectpath(conn,newdir);
4786 outsize = set_message(outbuf,0,0,False);
4787 SCVAL(outbuf,smb_reh,CVAL(inbuf,smb_reh));
4789 DEBUG(3,("setdir %s\n", newdir));
4791 END_PROFILE(pathworks_setdir);
4796 #define DBGC_CLASS DBGC_LOCKING
4798 /****************************************************************************
4799 Get a lock pid, dealing with large count requests.
4800 ****************************************************************************/
4802 uint32 get_lock_pid( char *data, int data_offset, BOOL large_file_format)
4804 if(!large_file_format)
4805 return (uint32)SVAL(data,SMB_LPID_OFFSET(data_offset));
4807 return (uint32)SVAL(data,SMB_LARGE_LPID_OFFSET(data_offset));
4810 /****************************************************************************
4811 Get a lock count, dealing with large count requests.
4812 ****************************************************************************/
4814 SMB_BIG_UINT get_lock_count( char *data, int data_offset, BOOL large_file_format)
4816 SMB_BIG_UINT count = 0;
4818 if(!large_file_format) {
4819 count = (SMB_BIG_UINT)IVAL(data,SMB_LKLEN_OFFSET(data_offset));
4822 #if defined(HAVE_LONGLONG)
4823 count = (((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset))) << 32) |
4824 ((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)));
4825 #else /* HAVE_LONGLONG */
4828 * NT4.x seems to be broken in that it sends large file (64 bit)
4829 * lockingX calls even if the CAP_LARGE_FILES was *not*
4830 * negotiated. For boxes without large unsigned ints truncate the
4831 * lock count by dropping the top 32 bits.
4834 if(IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)) != 0) {
4835 DEBUG(3,("get_lock_count: truncating lock count (high)0x%x (low)0x%x to just low count.\n",
4836 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)),
4837 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)) ));
4838 SIVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset),0);
4841 count = (SMB_BIG_UINT)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset));
4842 #endif /* HAVE_LONGLONG */
4848 #if !defined(HAVE_LONGLONG)
4849 /****************************************************************************
4850 Pathetically try and map a 64 bit lock offset into 31 bits. I hate Windows :-).
4851 ****************************************************************************/
4853 static uint32 map_lock_offset(uint32 high, uint32 low)
4857 uint32 highcopy = high;
4860 * Try and find out how many significant bits there are in high.
4863 for(i = 0; highcopy; i++)
4867 * We use 31 bits not 32 here as POSIX
4868 * lock offsets may not be negative.
4871 mask = (~0) << (31 - i);
4874 return 0; /* Fail. */
4880 #endif /* !defined(HAVE_LONGLONG) */
4882 /****************************************************************************
4883 Get a lock offset, dealing with large offset requests.
4884 ****************************************************************************/
4886 SMB_BIG_UINT get_lock_offset( char *data, int data_offset, BOOL large_file_format, BOOL *err)
4888 SMB_BIG_UINT offset = 0;
4892 if(!large_file_format) {
4893 offset = (SMB_BIG_UINT)IVAL(data,SMB_LKOFF_OFFSET(data_offset));
4896 #if defined(HAVE_LONGLONG)
4897 offset = (((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset))) << 32) |
4898 ((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset)));
4899 #else /* HAVE_LONGLONG */
4902 * NT4.x seems to be broken in that it sends large file (64 bit)
4903 * lockingX calls even if the CAP_LARGE_FILES was *not*
4904 * negotiated. For boxes without large unsigned ints mangle the
4905 * lock offset by mapping the top 32 bits onto the lower 32.
4908 if(IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset)) != 0) {
4909 uint32 low = IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
4910 uint32 high = IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset));
4913 if((new_low = map_lock_offset(high, low)) == 0) {
4915 return (SMB_BIG_UINT)-1;
4918 DEBUG(3,("get_lock_offset: truncating lock offset (high)0x%x (low)0x%x to offset 0x%x.\n",
4919 (unsigned int)high, (unsigned int)low, (unsigned int)new_low ));
4920 SIVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset),0);
4921 SIVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset),new_low);
4924 offset = (SMB_BIG_UINT)IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
4925 #endif /* HAVE_LONGLONG */
4931 /****************************************************************************
4932 Reply to a lockingX request.
4933 ****************************************************************************/
4935 int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
4936 int length, int bufsize)
4938 files_struct *fsp = file_fsp(inbuf,smb_vwv2);
4939 unsigned char locktype = CVAL(inbuf,smb_vwv3);
4940 unsigned char oplocklevel = CVAL(inbuf,smb_vwv3+1);
4941 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
4942 uint16 num_locks = SVAL(inbuf,smb_vwv7);
4943 SMB_BIG_UINT count = 0, offset = 0;
4945 int32 lock_timeout = IVAL(inbuf,smb_vwv4);
4948 BOOL large_file_format =
4949 (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
4951 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
4953 START_PROFILE(SMBlockingX);
4955 CHECK_FSP(fsp,conn);
4957 data = smb_buf(inbuf);
4959 if (locktype & LOCKING_ANDX_CHANGE_LOCKTYPE) {
4960 /* we don't support these - and CANCEL_LOCK makes w2k
4961 and XP reboot so I don't really want to be
4962 compatible! (tridge) */
4963 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks));
4966 /* Check if this is an oplock break on a file
4967 we have granted an oplock on.
4969 if ((locktype & LOCKING_ANDX_OPLOCK_RELEASE)) {
4970 /* Client can insist on breaking to none. */
4971 BOOL break_to_none = (oplocklevel == 0);
4974 DEBUG(5,("reply_lockingX: oplock break reply (%u) from client "
4975 "for fnum = %d\n", (unsigned int)oplocklevel,
4979 * Make sure we have granted an exclusive or batch oplock on
4983 if (fsp->oplock_type == 0) {
4985 /* The Samba4 nbench simulator doesn't understand
4986 the difference between break to level2 and break
4987 to none from level2 - it sends oplock break
4988 replies in both cases. Don't keep logging an error
4989 message here - just ignore it. JRA. */
4991 DEBUG(5,("reply_lockingX: Error : oplock break from "
4992 "client for fnum = %d (oplock=%d) and no "
4993 "oplock granted on this file (%s).\n",
4994 fsp->fnum, fsp->oplock_type, fsp->fsp_name));
4996 /* if this is a pure oplock break request then don't
4998 if (num_locks == 0 && num_ulocks == 0) {
4999 END_PROFILE(SMBlockingX);
5002 END_PROFILE(SMBlockingX);
5003 return ERROR_DOS(ERRDOS,ERRlock);
5007 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
5009 result = remove_oplock(fsp);
5011 result = downgrade_oplock(fsp);
5015 DEBUG(0, ("reply_lockingX: error in removing "
5016 "oplock on file %s\n", fsp->fsp_name));
5017 /* Hmmm. Is this panic justified? */
5018 smb_panic("internal tdb error");
5021 reply_to_oplock_break_requests(fsp);
5023 /* if this is a pure oplock break request then don't send a
5025 if (num_locks == 0 && num_ulocks == 0) {
5026 /* Sanity check - ensure a pure oplock break is not a
5028 if(CVAL(inbuf,smb_vwv0) != 0xff)
5029 DEBUG(0,("reply_lockingX: Error : pure oplock "
5030 "break is a chained %d request !\n",
5031 (unsigned int)CVAL(inbuf,smb_vwv0) ));
5032 END_PROFILE(SMBlockingX);
5038 * We do this check *after* we have checked this is not a oplock break
5039 * response message. JRA.
5042 release_level_2_oplocks_on_change(fsp);
5044 /* Data now points at the beginning of the list
5045 of smb_unlkrng structs */
5046 for(i = 0; i < (int)num_ulocks; i++) {
5047 lock_pid = get_lock_pid( data, i, large_file_format);
5048 count = get_lock_count( data, i, large_file_format);
5049 offset = get_lock_offset( data, i, large_file_format, &err);
5052 * There is no error code marked "stupid client bug".... :-).
5055 END_PROFILE(SMBlockingX);
5056 return ERROR_DOS(ERRDOS,ERRnoaccess);
5059 DEBUG(10,("reply_lockingX: unlock start=%.0f, len=%.0f for "
5060 "pid %u, file %s\n", (double)offset, (double)count,
5061 (unsigned int)lock_pid, fsp->fsp_name ));
5063 status = do_unlock(fsp,
5069 if (NT_STATUS_V(status)) {
5070 END_PROFILE(SMBlockingX);
5071 return ERROR_NT(status);
5075 /* Setup the timeout in seconds. */
5077 if (!lp_blocking_locks(SNUM(conn))) {
5081 /* Now do any requested locks */
5082 data += ((large_file_format ? 20 : 10)*num_ulocks);
5084 /* Data now points at the beginning of the list
5085 of smb_lkrng structs */
5087 for(i = 0; i < (int)num_locks; i++) {
5088 enum brl_type lock_type = ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
5089 READ_LOCK:WRITE_LOCK);
5090 lock_pid = get_lock_pid( data, i, large_file_format);
5091 count = get_lock_count( data, i, large_file_format);
5092 offset = get_lock_offset( data, i, large_file_format, &err);
5095 * There is no error code marked "stupid client bug".... :-).
5098 END_PROFILE(SMBlockingX);
5099 return ERROR_DOS(ERRDOS,ERRnoaccess);
5102 DEBUG(10,("reply_lockingX: lock start=%.0f, len=%.0f for pid "
5103 "%u, file %s timeout = %d\n", (double)offset,
5104 (double)count, (unsigned int)lock_pid,
5105 fsp->fsp_name, (int)lock_timeout ));
5107 if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
5108 if (lp_blocking_locks(SNUM(conn))) {
5110 /* Schedule a message to ourselves to
5111 remove the blocking lock record and
5112 return the right error. */
5114 if (!blocking_lock_cancel(fsp,
5120 NT_STATUS_FILE_LOCK_CONFLICT)) {
5121 END_PROFILE(SMBlockingX);
5122 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRcancelviolation));
5125 /* Remove a matching pending lock. */
5126 status = do_lock_cancel(fsp,
5132 BOOL blocking_lock = lock_timeout ? True : False;
5133 BOOL defer_lock = False;
5134 struct byte_range_lock *br_lck;
5136 br_lck = do_lock(fsp,
5145 if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
5146 /* Windows internal resolution for blocking locks seems
5147 to be about 200ms... Don't wait for less than that. JRA. */
5148 if (lock_timeout != -1 && lock_timeout < lp_lock_spin_time()) {
5149 lock_timeout = lp_lock_spin_time();
5154 /* This heuristic seems to match W2K3 very well. If a
5155 lock sent with timeout of zero would fail with NT_STATUS_FILE_LOCK_CONFLICT
5156 it pretends we asked for a timeout of between 150 - 300 milliseconds as
5157 far as I can tell. Replacement for do_lock_spin(). JRA. */
5159 if (br_lck && lp_blocking_locks(SNUM(conn)) && !blocking_lock &&
5160 NT_STATUS_EQUAL((status), NT_STATUS_FILE_LOCK_CONFLICT)) {
5162 lock_timeout = lp_lock_spin_time();
5165 if (br_lck && defer_lock) {
5167 * A blocking lock was requested. Package up
5168 * this smb into a queued request and push it
5169 * onto the blocking lock queue.
5171 if(push_blocking_lock_request(br_lck,
5181 TALLOC_FREE(br_lck);
5182 END_PROFILE(SMBlockingX);
5187 TALLOC_FREE(br_lck);
5190 if (NT_STATUS_V(status)) {
5191 END_PROFILE(SMBlockingX);
5192 return ERROR_NT(status);
5196 /* If any of the above locks failed, then we must unlock
5197 all of the previous locks (X/Open spec). */
5199 if (!(locktype & LOCKING_ANDX_CANCEL_LOCK) &&
5203 * Ensure we don't do a remove on the lock that just failed,
5204 * as under POSIX rules, if we have a lock already there, we
5205 * will delete it (and we shouldn't) .....
5207 for(i--; i >= 0; i--) {
5208 lock_pid = get_lock_pid( data, i, large_file_format);
5209 count = get_lock_count( data, i, large_file_format);
5210 offset = get_lock_offset( data, i, large_file_format,
5214 * There is no error code marked "stupid client
5218 END_PROFILE(SMBlockingX);
5219 return ERROR_DOS(ERRDOS,ERRnoaccess);
5228 END_PROFILE(SMBlockingX);
5229 return ERROR_NT(status);
5232 set_message(outbuf,2,0,True);
5234 DEBUG(3, ("lockingX fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
5235 fsp->fnum, (unsigned int)locktype, num_locks, num_ulocks));
5237 END_PROFILE(SMBlockingX);
5238 return chain_reply(inbuf,outbuf,length,bufsize);
5242 #define DBGC_CLASS DBGC_ALL
5244 /****************************************************************************
5245 Reply to a SMBreadbmpx (read block multiplex) request.
5246 ****************************************************************************/
5248 int reply_readbmpx(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
5259 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5260 START_PROFILE(SMBreadBmpx);
5262 /* this function doesn't seem to work - disable by default */
5263 if (!lp_readbmpx()) {
5264 END_PROFILE(SMBreadBmpx);
5265 return ERROR_DOS(ERRSRV,ERRuseSTD);
5268 outsize = set_message(outbuf,8,0,True);
5270 CHECK_FSP(fsp,conn);
5271 if (!CHECK_READ(fsp,inbuf)) {
5272 return(ERROR_DOS(ERRDOS,ERRbadaccess));
5275 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
5276 maxcount = SVAL(inbuf,smb_vwv3);
5278 data = smb_buf(outbuf);
5279 pad = ((long)data)%4;
5284 max_per_packet = bufsize-(outsize+pad);
5288 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
5289 END_PROFILE(SMBreadBmpx);
5290 return ERROR_DOS(ERRDOS,ERRlock);
5294 size_t N = MIN(max_per_packet,tcount-total_read);
5296 nread = read_file(fsp,data,startpos,N);
5301 if (nread < (ssize_t)N)
5302 tcount = total_read + nread;
5304 set_message(outbuf,8,nread+pad,False);
5305 SIVAL(outbuf,smb_vwv0,startpos);
5306 SSVAL(outbuf,smb_vwv2,tcount);
5307 SSVAL(outbuf,smb_vwv6,nread);
5308 SSVAL(outbuf,smb_vwv7,smb_offset(data,outbuf));
5311 if (!send_smb(smbd_server_fd(),outbuf))
5312 exit_server_cleanly("reply_readbmpx: send_smb failed.");
5314 total_read += nread;
5316 } while (total_read < (ssize_t)tcount);
5318 END_PROFILE(SMBreadBmpx);
5322 /****************************************************************************
5323 Reply to a SMBsetattrE.
5324 ****************************************************************************/
5326 int reply_setattrE(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
5328 struct utimbuf unix_times;
5330 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5331 START_PROFILE(SMBsetattrE);
5333 outsize = set_message(outbuf,0,0,False);
5335 if(!fsp || (fsp->conn != conn)) {
5336 END_PROFILE(SMBsetattrE);
5337 return ERROR_DOS(ERRDOS,ERRbadfid);
5341 * Convert the DOS times into unix times. Ignore create
5342 * time as UNIX can't set this.
5345 unix_times.actime = srv_make_unix_date2(inbuf+smb_vwv3);
5346 unix_times.modtime = srv_make_unix_date2(inbuf+smb_vwv5);
5349 * Patch from Ray Frush <frush@engr.colostate.edu>
5350 * Sometimes times are sent as zero - ignore them.
5353 if (null_mtime(unix_times.actime) && null_mtime(unix_times.modtime)) {
5354 /* Ignore request */
5355 if( DEBUGLVL( 3 ) ) {
5356 dbgtext( "reply_setattrE fnum=%d ", fsp->fnum);
5357 dbgtext( "ignoring zero request - not setting timestamps of 0\n" );
5359 END_PROFILE(SMBsetattrE);
5361 } else if (!null_mtime(unix_times.actime) && null_mtime(unix_times.modtime)) {
5362 /* set modify time = to access time if modify time was unset */
5363 unix_times.modtime = unix_times.actime;
5366 /* Set the date on this file */
5367 /* Should we set pending modtime here ? JRA */
5368 if(file_utime(conn, fsp->fsp_name, &unix_times)) {
5369 END_PROFILE(SMBsetattrE);
5370 return ERROR_DOS(ERRDOS,ERRnoaccess);
5373 DEBUG( 3, ( "reply_setattrE fnum=%d actime=%d modtime=%d\n",
5374 fsp->fnum, (int)unix_times.actime, (int)unix_times.modtime ) );
5376 END_PROFILE(SMBsetattrE);
5381 /* Back from the dead for OS/2..... JRA. */
5383 /****************************************************************************
5384 Reply to a SMBwritebmpx (write block multiplex primary) request.
5385 ****************************************************************************/
5387 int reply_writebmpx(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
5390 ssize_t nwritten = -1;
5397 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5398 START_PROFILE(SMBwriteBmpx);
5400 CHECK_FSP(fsp,conn);
5401 if (!CHECK_WRITE(fsp)) {
5402 return(ERROR_DOS(ERRDOS,ERRbadaccess));
5404 if (HAS_CACHED_ERROR(fsp)) {
5405 return(CACHED_ERROR(fsp));
5408 tcount = SVAL(inbuf,smb_vwv1);
5409 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
5410 write_through = BITSETW(inbuf+smb_vwv7,0);
5411 numtowrite = SVAL(inbuf,smb_vwv10);
5412 smb_doff = SVAL(inbuf,smb_vwv11);
5414 data = smb_base(inbuf) + smb_doff;
5416 /* If this fails we need to send an SMBwriteC response,
5417 not an SMBwritebmpx - set this up now so we don't forget */
5418 SCVAL(outbuf,smb_com,SMBwritec);
5420 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos,WRITE_LOCK)) {
5421 END_PROFILE(SMBwriteBmpx);
5422 return(ERROR_DOS(ERRDOS,ERRlock));
5425 nwritten = write_file(fsp,data,startpos,numtowrite);
5427 sync_file(conn, fsp, write_through);
5429 if(nwritten < (ssize_t)numtowrite) {
5430 END_PROFILE(SMBwriteBmpx);
5431 return(UNIXERROR(ERRHRD,ERRdiskfull));
5434 /* If the maximum to be written to this file
5435 is greater than what we just wrote then set
5436 up a secondary struct to be attached to this
5437 fd, we will use this to cache error messages etc. */
5439 if((ssize_t)tcount > nwritten) {
5440 write_bmpx_struct *wbms;
5441 if(fsp->wbmpx_ptr != NULL)
5442 wbms = fsp->wbmpx_ptr; /* Use an existing struct */
5444 wbms = SMB_MALLOC_P(write_bmpx_struct);
5446 DEBUG(0,("Out of memory in reply_readmpx\n"));
5447 END_PROFILE(SMBwriteBmpx);
5448 return(ERROR_DOS(ERRSRV,ERRnoresource));
5450 wbms->wr_mode = write_through;
5451 wbms->wr_discard = False; /* No errors yet */
5452 wbms->wr_total_written = nwritten;
5453 wbms->wr_errclass = 0;
5455 fsp->wbmpx_ptr = wbms;
5458 /* We are returning successfully, set the message type back to
5460 SCVAL(outbuf,smb_com,SMBwriteBmpx);
5462 outsize = set_message(outbuf,1,0,True);
5464 SSVALS(outbuf,smb_vwv0,-1); /* We don't support smb_remaining */
5466 DEBUG( 3, ( "writebmpx fnum=%d num=%d wrote=%d\n",
5467 fsp->fnum, (int)numtowrite, (int)nwritten ) );
5469 if (write_through && tcount==nwritten) {
5470 /* We need to send both a primary and a secondary response */
5471 smb_setlen(outbuf,outsize - 4);
5473 if (!send_smb(smbd_server_fd(),outbuf))
5474 exit_server_cleanly("reply_writebmpx: send_smb failed.");
5476 /* Now the secondary */
5477 outsize = set_message(outbuf,1,0,True);
5478 SCVAL(outbuf,smb_com,SMBwritec);
5479 SSVAL(outbuf,smb_vwv0,nwritten);
5482 END_PROFILE(SMBwriteBmpx);
5486 /****************************************************************************
5487 Reply to a SMBwritebs (write block multiplex secondary) request.
5488 ****************************************************************************/
5490 int reply_writebs(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
5493 ssize_t nwritten = -1;
5500 write_bmpx_struct *wbms;
5501 BOOL send_response = False;
5502 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5503 START_PROFILE(SMBwriteBs);
5505 CHECK_FSP(fsp,conn);
5506 if (!CHECK_WRITE(fsp)) {
5507 return(ERROR_DOS(ERRDOS,ERRbadaccess));
5510 tcount = SVAL(inbuf,smb_vwv1);
5511 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
5512 numtowrite = SVAL(inbuf,smb_vwv6);
5513 smb_doff = SVAL(inbuf,smb_vwv7);
5515 data = smb_base(inbuf) + smb_doff;
5517 /* We need to send an SMBwriteC response, not an SMBwritebs */
5518 SCVAL(outbuf,smb_com,SMBwritec);
5520 /* This fd should have an auxiliary struct attached,
5521 check that it does */
5522 wbms = fsp->wbmpx_ptr;
5524 END_PROFILE(SMBwriteBs);
5528 /* If write through is set we can return errors, else we must cache them */
5529 write_through = wbms->wr_mode;
5531 /* Check for an earlier error */
5532 if(wbms->wr_discard) {
5533 END_PROFILE(SMBwriteBs);
5534 return -1; /* Just discard the packet */
5537 nwritten = write_file(fsp,data,startpos,numtowrite);
5539 sync_file(conn, fsp, write_through);
5541 if (nwritten < (ssize_t)numtowrite) {
5543 /* We are returning an error - we can delete the aux struct */
5546 fsp->wbmpx_ptr = NULL;
5547 END_PROFILE(SMBwriteBs);
5548 return(ERROR_DOS(ERRHRD,ERRdiskfull));
5550 wbms->wr_errclass = ERRHRD;
5551 wbms->wr_error = ERRdiskfull;
5552 wbms->wr_status = NT_STATUS_DISK_FULL;
5553 wbms->wr_discard = True;
5554 END_PROFILE(SMBwriteBs);
5558 /* Increment the total written, if this matches tcount
5559 we can discard the auxiliary struct (hurrah !) and return a writeC */
5560 wbms->wr_total_written += nwritten;
5561 if(wbms->wr_total_written >= tcount) {
5562 if (write_through) {
5563 outsize = set_message(outbuf,1,0,True);
5564 SSVAL(outbuf,smb_vwv0,wbms->wr_total_written);
5565 send_response = True;
5569 fsp->wbmpx_ptr = NULL;
5573 END_PROFILE(SMBwriteBs);
5577 END_PROFILE(SMBwriteBs);
5581 /****************************************************************************
5582 Reply to a SMBgetattrE.
5583 ****************************************************************************/
5585 int reply_getattrE(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
5587 SMB_STRUCT_STAT sbuf;
5590 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5591 START_PROFILE(SMBgetattrE);
5593 outsize = set_message(outbuf,11,0,True);
5595 if(!fsp || (fsp->conn != conn)) {
5596 END_PROFILE(SMBgetattrE);
5597 return ERROR_DOS(ERRDOS,ERRbadfid);
5600 /* Do an fstat on this file */
5601 if(fsp_stat(fsp, &sbuf)) {
5602 END_PROFILE(SMBgetattrE);
5603 return(UNIXERROR(ERRDOS,ERRnoaccess));
5606 mode = dos_mode(conn,fsp->fsp_name,&sbuf);
5609 * Convert the times into dos times. Set create
5610 * date to be last modify date as UNIX doesn't save
5614 srv_put_dos_date2(outbuf,smb_vwv0,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
5615 srv_put_dos_date2(outbuf,smb_vwv2,sbuf.st_atime);
5616 /* Should we check pending modtime here ? JRA */
5617 srv_put_dos_date2(outbuf,smb_vwv4,sbuf.st_mtime);
5620 SIVAL(outbuf,smb_vwv6,0);
5621 SIVAL(outbuf,smb_vwv8,0);
5623 uint32 allocation_size = get_allocation_size(conn,fsp, &sbuf);
5624 SIVAL(outbuf,smb_vwv6,(uint32)sbuf.st_size);
5625 SIVAL(outbuf,smb_vwv8,allocation_size);
5627 SSVAL(outbuf,smb_vwv10, mode);
5629 DEBUG( 3, ( "reply_getattrE fnum=%d\n", fsp->fnum));
5631 END_PROFILE(SMBgetattrE);