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)) {
644 status = check_name(conn, name);
645 if (!NT_STATUS_IS_OK(status)) {
646 DEBUG(3,("reply_checkpath: check_name of %s failed (%s)\n",name,nt_errstr(status)));
650 if (!VALID_STAT(sbuf) && (SMB_VFS_STAT(conn,name,&sbuf) != 0)) {
651 DEBUG(3,("reply_checkpath: stat of %s failed (%s)\n",name,strerror(errno)));
652 status = map_nt_error_from_unix(errno);
656 if (!S_ISDIR(sbuf.st_mode)) {
657 END_PROFILE(SMBcheckpath);
658 return ERROR_BOTH(NT_STATUS_NOT_A_DIRECTORY,ERRDOS,ERRbadpath);
661 outsize = set_message(outbuf,0,0,False);
663 END_PROFILE(SMBcheckpath);
668 END_PROFILE(SMBcheckpath);
670 /* We special case this - as when a Windows machine
671 is parsing a path is steps through the components
672 one at a time - if a component fails it expects
673 ERRbadpath, not ERRbadfile.
675 status = map_checkpath_error(inbuf, status);
676 if(NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
678 * Windows returns different error codes if
679 * the parent directory is valid but not the
680 * last component - it returns NT_STATUS_OBJECT_NAME_NOT_FOUND
681 * for that case and NT_STATUS_OBJECT_PATH_NOT_FOUND
682 * if the path is invalid.
684 return ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpath);
687 return ERROR_NT(status);
690 /****************************************************************************
692 ****************************************************************************/
694 int reply_getatr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
698 SMB_STRUCT_STAT sbuf;
705 START_PROFILE(SMBgetatr);
707 p = smb_buf(inbuf) + 1;
708 p += srvstr_get_path(inbuf, fname, p, sizeof(fname), 0, STR_TERMINATE, &status);
709 if (!NT_STATUS_IS_OK(status)) {
710 END_PROFILE(SMBgetatr);
711 return ERROR_NT(status);
714 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
716 /* dos smetimes asks for a stat of "" - it returns a "hidden directory"
717 under WfWg - weird! */
718 if (*fname == '\0') {
719 mode = aHIDDEN | aDIR;
720 if (!CAN_WRITE(conn)) {
726 status = unix_convert(conn, fname, False, NULL,&sbuf);
727 if (!NT_STATUS_IS_OK(status)) {
728 END_PROFILE(SMBgetatr);
729 return ERROR_NT(status);
731 status = check_name(conn, fname);
732 if (!NT_STATUS_IS_OK(status)) {
733 DEBUG(3,("reply_getatr: check_name of %s failed (%s)\n",fname,nt_errstr(status)));
734 END_PROFILE(SMBgetatr);
735 return ERROR_NT(status);
737 if (!VALID_STAT(sbuf) && (SMB_VFS_STAT(conn,fname,&sbuf) != 0)) {
738 DEBUG(3,("reply_getatr: stat of %s failed (%s)\n",fname,strerror(errno)));
739 return UNIXERROR(ERRDOS,ERRbadfile);
742 mode = dos_mode(conn,fname,&sbuf);
744 mtime = sbuf.st_mtime;
750 outsize = set_message(outbuf,10,0,True);
752 SSVAL(outbuf,smb_vwv0,mode);
753 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
754 srv_put_dos_date3(outbuf,smb_vwv1,mtime & ~1);
756 srv_put_dos_date3(outbuf,smb_vwv1,mtime);
758 SIVAL(outbuf,smb_vwv3,(uint32)size);
760 if (Protocol >= PROTOCOL_NT1) {
761 SSVAL(outbuf,smb_flg2,SVAL(outbuf, smb_flg2) | FLAGS2_IS_LONG_NAME);
764 DEBUG(3,("reply_getatr: name=%s mode=%d size=%u\n", fname, mode, (unsigned int)size ) );
766 END_PROFILE(SMBgetatr);
770 /****************************************************************************
772 ****************************************************************************/
774 int reply_setatr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
780 SMB_STRUCT_STAT sbuf;
784 START_PROFILE(SMBsetatr);
786 p = smb_buf(inbuf) + 1;
787 p += srvstr_get_path(inbuf, fname, p, sizeof(fname), 0, STR_TERMINATE, &status);
788 if (!NT_STATUS_IS_OK(status)) {
789 END_PROFILE(SMBsetatr);
790 return ERROR_NT(status);
793 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
795 status = unix_convert(conn, fname, False, NULL, &sbuf);
796 if (!NT_STATUS_IS_OK(status)) {
797 END_PROFILE(SMBsetatr);
798 return ERROR_NT(status);
801 if (fname[0] == '.' && fname[1] == '\0') {
803 * Not sure here is the right place to catch this
804 * condition. Might be moved to somewhere else later -- vl
806 END_PROFILE(SMBsetatr);
807 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
810 status = check_name(conn, fname);
811 if (!NT_STATUS_IS_OK(status)) {
812 END_PROFILE(SMBsetatr);
813 return ERROR_NT(status);
816 mode = SVAL(inbuf,smb_vwv0);
817 mtime = srv_make_unix_date3(inbuf+smb_vwv1);
819 if (mode != FILE_ATTRIBUTE_NORMAL) {
820 if (VALID_STAT_OF_DIR(sbuf))
825 if (file_set_dosmode(conn,fname,mode,&sbuf,False) != 0) {
826 END_PROFILE(SMBsetatr);
827 return UNIXERROR(ERRDOS, ERRnoaccess);
831 if (!set_filetime(conn,fname,mtime)) {
832 END_PROFILE(SMBsetatr);
833 return UNIXERROR(ERRDOS, ERRnoaccess);
836 outsize = set_message(outbuf,0,0,False);
838 DEBUG( 3, ( "setatr name=%s mode=%d\n", fname, mode ) );
840 END_PROFILE(SMBsetatr);
844 /****************************************************************************
846 ****************************************************************************/
848 int reply_dskattr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
851 SMB_BIG_UINT dfree,dsize,bsize;
852 START_PROFILE(SMBdskattr);
854 if (get_dfree_info(conn,".",True,&bsize,&dfree,&dsize) == (SMB_BIG_UINT)-1) {
855 END_PROFILE(SMBdskattr);
856 return(UNIXERROR(ERRHRD,ERRgeneral));
859 outsize = set_message(outbuf,5,0,True);
861 if (Protocol <= PROTOCOL_LANMAN2) {
862 double total_space, free_space;
863 /* we need to scale this to a number that DOS6 can handle. We
864 use floating point so we can handle large drives on systems
865 that don't have 64 bit integers
867 we end up displaying a maximum of 2G to DOS systems
869 total_space = dsize * (double)bsize;
870 free_space = dfree * (double)bsize;
872 dsize = (total_space+63*512) / (64*512);
873 dfree = (free_space+63*512) / (64*512);
875 if (dsize > 0xFFFF) dsize = 0xFFFF;
876 if (dfree > 0xFFFF) dfree = 0xFFFF;
878 SSVAL(outbuf,smb_vwv0,dsize);
879 SSVAL(outbuf,smb_vwv1,64); /* this must be 64 for dos systems */
880 SSVAL(outbuf,smb_vwv2,512); /* and this must be 512 */
881 SSVAL(outbuf,smb_vwv3,dfree);
883 SSVAL(outbuf,smb_vwv0,dsize);
884 SSVAL(outbuf,smb_vwv1,bsize/512);
885 SSVAL(outbuf,smb_vwv2,512);
886 SSVAL(outbuf,smb_vwv3,dfree);
889 DEBUG(3,("dskattr dfree=%d\n", (unsigned int)dfree));
891 END_PROFILE(SMBdskattr);
895 /****************************************************************************
897 Can be called from SMBsearch, SMBffirst or SMBfunique.
898 ****************************************************************************/
900 int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
910 unsigned int numentries = 0;
911 unsigned int maxentries = 0;
912 BOOL finished = False;
918 BOOL check_descend = False;
919 BOOL expect_close = False;
921 BOOL mask_contains_wcard = False;
922 BOOL allow_long_path_components = (SVAL(inbuf,smb_flg2) & FLAGS2_LONG_PATH_COMPONENTS) ? True : False;
924 START_PROFILE(SMBsearch);
926 if (lp_posix_pathnames()) {
927 END_PROFILE(SMBsearch);
928 return reply_unknown(inbuf, outbuf);
931 *mask = *directory = *fname = 0;
933 /* If we were called as SMBffirst then we must expect close. */
934 if(CVAL(inbuf,smb_com) == SMBffirst) {
938 outsize = set_message(outbuf,1,3,True);
939 maxentries = SVAL(inbuf,smb_vwv0);
940 dirtype = SVAL(inbuf,smb_vwv1);
941 p = smb_buf(inbuf) + 1;
942 p += srvstr_get_path_wcard(inbuf, path, p, sizeof(path), 0, STR_TERMINATE, &nt_status, &mask_contains_wcard);
943 if (!NT_STATUS_IS_OK(nt_status)) {
944 END_PROFILE(SMBsearch);
945 return ERROR_NT(nt_status);
948 RESOLVE_DFSPATH_WCARD(path, conn, inbuf, outbuf);
951 status_len = SVAL(p, 0);
954 /* dirtype &= ~aDIR; */
956 if (status_len == 0) {
957 SMB_STRUCT_STAT sbuf;
960 pstrcpy(directory,path);
962 nt_status = unix_convert(conn, directory, True, NULL, &sbuf);
963 if (!NT_STATUS_IS_OK(nt_status)) {
964 END_PROFILE(SMBsearch);
965 return ERROR_NT(nt_status);
969 nt_status = check_name(conn, directory);
970 if (!NT_STATUS_IS_OK(nt_status)) {
971 END_PROFILE(SMBsearch);
972 return ERROR_NT(nt_status);
975 p = strrchr_m(dir2,'/');
984 p = strrchr_m(directory,'/');
991 if (strlen(directory) == 0) {
992 pstrcpy(directory,".");
994 memset((char *)status,'\0',21);
995 SCVAL(status,0,(dirtype & 0x1F));
1000 status_dirtype = CVAL(status,0) & 0x1F;
1001 if (status_dirtype != (dirtype & 0x1F)) {
1002 dirtype = status_dirtype;
1005 conn->dirptr = dptr_fetch(status+12,&dptr_num);
1006 if (!conn->dirptr) {
1009 string_set(&conn->dirpath,dptr_path(dptr_num));
1010 pstrcpy(mask, dptr_wcard(dptr_num));
1013 p = smb_buf(outbuf) + 3;
1015 if (status_len == 0) {
1016 nt_status = dptr_create(conn,
1020 SVAL(inbuf,smb_pid),
1022 mask_contains_wcard,
1025 if (!NT_STATUS_IS_OK(nt_status)) {
1026 return ERROR_NT(nt_status);
1028 dptr_num = dptr_dnum(conn->dirptr);
1030 dirtype = dptr_attr(dptr_num);
1033 DEBUG(4,("dptr_num is %d\n",dptr_num));
1035 if ((dirtype&0x1F) == aVOLID) {
1036 memcpy(p,status,21);
1037 make_dir_struct(p,"???????????",volume_label(SNUM(conn)),
1038 0,aVOLID,0,!allow_long_path_components);
1039 dptr_fill(p+12,dptr_num);
1040 if (dptr_zero(p+12) && (status_len==0)) {
1045 p += DIR_STRUCT_SIZE;
1048 maxentries = MIN(maxentries, ((BUFFER_SIZE - (p - outbuf))/DIR_STRUCT_SIZE));
1050 DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
1051 conn->dirpath,lp_dontdescend(SNUM(conn))));
1052 if (in_list(conn->dirpath, lp_dontdescend(SNUM(conn)),True)) {
1053 check_descend = True;
1056 for (i=numentries;(i<maxentries) && !finished;i++) {
1057 finished = !get_dir_entry(conn,mask,dirtype,fname,&size,&mode,&date,check_descend);
1059 memcpy(p,status,21);
1060 make_dir_struct(p,mask,fname,size, mode,date,
1061 !allow_long_path_components);
1062 if (!dptr_fill(p+12,dptr_num)) {
1066 p += DIR_STRUCT_SIZE;
1073 /* If we were called as SMBffirst with smb_search_id == NULL
1074 and no entries were found then return error and close dirptr
1077 if (numentries == 0) {
1078 dptr_close(&dptr_num);
1079 } else if(expect_close && status_len == 0) {
1080 /* Close the dptr - we know it's gone */
1081 dptr_close(&dptr_num);
1084 /* If we were called as SMBfunique, then we can close the dirptr now ! */
1085 if(dptr_num >= 0 && CVAL(inbuf,smb_com) == SMBfunique) {
1086 dptr_close(&dptr_num);
1089 if ((numentries == 0) && !mask_contains_wcard) {
1090 return ERROR_BOTH(STATUS_NO_MORE_FILES,ERRDOS,ERRnofiles);
1093 SSVAL(outbuf,smb_vwv0,numentries);
1094 SSVAL(outbuf,smb_vwv1,3 + numentries * DIR_STRUCT_SIZE);
1095 SCVAL(smb_buf(outbuf),0,5);
1096 SSVAL(smb_buf(outbuf),1,numentries*DIR_STRUCT_SIZE);
1098 /* The replies here are never long name. */
1099 SSVAL(outbuf,smb_flg2,SVAL(outbuf, smb_flg2) & (~FLAGS2_IS_LONG_NAME));
1100 if (!allow_long_path_components) {
1101 SSVAL(outbuf,smb_flg2,SVAL(outbuf, smb_flg2) & (~FLAGS2_LONG_PATH_COMPONENTS));
1104 /* This SMB *always* returns ASCII names. Remove the unicode bit in flags2. */
1105 SSVAL(outbuf,smb_flg2, (SVAL(outbuf, smb_flg2) & (~FLAGS2_UNICODE_STRINGS)));
1107 outsize += DIR_STRUCT_SIZE*numentries;
1108 smb_setlen(outbuf,outsize - 4);
1110 if ((! *directory) && dptr_path(dptr_num))
1111 slprintf(directory, sizeof(directory)-1, "(%s)",dptr_path(dptr_num));
1113 DEBUG( 4, ( "%s mask=%s path=%s dtype=%d nument=%u of %u\n",
1114 smb_fn_name(CVAL(inbuf,smb_com)),
1115 mask, directory, dirtype, numentries, maxentries ) );
1117 END_PROFILE(SMBsearch);
1121 /****************************************************************************
1122 Reply to a fclose (stop directory search).
1123 ****************************************************************************/
1125 int reply_fclose(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1134 BOOL path_contains_wcard = False;
1136 START_PROFILE(SMBfclose);
1138 if (lp_posix_pathnames()) {
1139 END_PROFILE(SMBfclose);
1140 return reply_unknown(inbuf, outbuf);
1143 outsize = set_message(outbuf,1,0,True);
1144 p = smb_buf(inbuf) + 1;
1145 p += srvstr_get_path_wcard(inbuf, path, p, sizeof(path), 0, STR_TERMINATE, &err, &path_contains_wcard);
1146 if (!NT_STATUS_IS_OK(err)) {
1147 END_PROFILE(SMBfclose);
1148 return ERROR_NT(err);
1151 status_len = SVAL(p,0);
1154 if (status_len == 0) {
1155 END_PROFILE(SMBfclose);
1156 return ERROR_DOS(ERRSRV,ERRsrverror);
1159 memcpy(status,p,21);
1161 if(dptr_fetch(status+12,&dptr_num)) {
1162 /* Close the dptr - we know it's gone */
1163 dptr_close(&dptr_num);
1166 SSVAL(outbuf,smb_vwv0,0);
1168 DEBUG(3,("search close\n"));
1170 END_PROFILE(SMBfclose);
1174 /****************************************************************************
1176 ****************************************************************************/
1178 int reply_open(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1186 SMB_STRUCT_STAT sbuf;
1188 int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
1190 uint32 dos_attr = SVAL(inbuf,smb_vwv1);
1193 uint32 create_disposition;
1194 uint32 create_options = 0;
1196 START_PROFILE(SMBopen);
1198 deny_mode = SVAL(inbuf,smb_vwv0);
1200 srvstr_get_path(inbuf, fname, smb_buf(inbuf)+1, sizeof(fname), 0, STR_TERMINATE, &status);
1201 if (!NT_STATUS_IS_OK(status)) {
1202 END_PROFILE(SMBopen);
1203 return ERROR_NT(status);
1206 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1208 status = unix_convert(conn, fname, False, NULL, &sbuf);
1209 if (!NT_STATUS_IS_OK(status)) {
1210 END_PROFILE(SMBopen);
1211 return ERROR_NT(status);
1214 if (!map_open_params_to_ntcreate(fname, deny_mode, OPENX_FILE_EXISTS_OPEN,
1215 &access_mask, &share_mode, &create_disposition, &create_options)) {
1216 END_PROFILE(SMBopen);
1217 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRbadaccess));
1220 status = open_file_ntcreate(conn,fname,&sbuf,
1229 if (!NT_STATUS_IS_OK(status)) {
1230 END_PROFILE(SMBopen);
1231 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1232 /* We have re-scheduled this call. */
1235 return ERROR_NT(status);
1238 size = sbuf.st_size;
1239 fattr = dos_mode(conn,fname,&sbuf);
1240 mtime = sbuf.st_mtime;
1243 DEBUG(3,("attempt to open a directory %s\n",fname));
1244 close_file(fsp,ERROR_CLOSE);
1245 END_PROFILE(SMBopen);
1246 return ERROR_DOS(ERRDOS,ERRnoaccess);
1249 outsize = set_message(outbuf,7,0,True);
1250 SSVAL(outbuf,smb_vwv0,fsp->fnum);
1251 SSVAL(outbuf,smb_vwv1,fattr);
1252 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1253 srv_put_dos_date3(outbuf,smb_vwv2,mtime & ~1);
1255 srv_put_dos_date3(outbuf,smb_vwv2,mtime);
1257 SIVAL(outbuf,smb_vwv4,(uint32)size);
1258 SSVAL(outbuf,smb_vwv6,deny_mode);
1260 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1261 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1264 if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1265 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1267 END_PROFILE(SMBopen);
1271 /****************************************************************************
1272 Reply to an open and X.
1273 ****************************************************************************/
1275 int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
1278 uint16 open_flags = SVAL(inbuf,smb_vwv2);
1279 int deny_mode = SVAL(inbuf,smb_vwv3);
1280 uint32 smb_attr = SVAL(inbuf,smb_vwv5);
1281 /* Breakout the oplock request bits so we can set the
1282 reply bits separately. */
1283 int ex_oplock_request = EXTENDED_OPLOCK_REQUEST(inbuf);
1284 int core_oplock_request = CORE_OPLOCK_REQUEST(inbuf);
1285 int oplock_request = ex_oplock_request | core_oplock_request;
1287 int smb_sattr = SVAL(inbuf,smb_vwv4);
1288 uint32 smb_time = make_unix_date3(inbuf+smb_vwv6);
1290 int smb_ofun = SVAL(inbuf,smb_vwv8);
1293 SMB_STRUCT_STAT sbuf;
1297 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv9);
1298 ssize_t retval = -1;
1301 uint32 create_disposition;
1302 uint32 create_options = 0;
1304 START_PROFILE(SMBopenX);
1306 /* If it's an IPC, pass off the pipe handler. */
1308 if (lp_nt_pipe_support()) {
1309 END_PROFILE(SMBopenX);
1310 return reply_open_pipe_and_X(conn, inbuf,outbuf,length,bufsize);
1312 END_PROFILE(SMBopenX);
1313 return ERROR_DOS(ERRSRV,ERRaccess);
1317 /* XXXX we need to handle passed times, sattr and flags */
1318 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
1319 if (!NT_STATUS_IS_OK(status)) {
1320 END_PROFILE(SMBopenX);
1321 return ERROR_NT(status);
1324 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1326 status = unix_convert(conn, fname, False, NULL, &sbuf);
1327 if (!NT_STATUS_IS_OK(status)) {
1328 END_PROFILE(SMBopenX);
1329 return ERROR_NT(status);
1332 if (!map_open_params_to_ntcreate(fname, deny_mode, smb_ofun,
1335 &create_disposition,
1337 END_PROFILE(SMBopenX);
1338 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRbadaccess));
1341 status = open_file_ntcreate(conn,fname,&sbuf,
1350 if (!NT_STATUS_IS_OK(status)) {
1351 END_PROFILE(SMBopenX);
1352 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1353 /* We have re-scheduled this call. */
1356 return ERROR_NT(status);
1359 /* Setting the "size" field in vwv9 and vwv10 causes the file to be set to this size,
1360 if the file is truncated or created. */
1361 if (((smb_action == FILE_WAS_CREATED) || (smb_action == FILE_WAS_OVERWRITTEN)) && allocation_size) {
1362 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1363 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1364 close_file(fsp,ERROR_CLOSE);
1365 END_PROFILE(SMBopenX);
1366 return ERROR_NT(NT_STATUS_DISK_FULL);
1368 retval = vfs_set_filelen(fsp, (SMB_OFF_T)allocation_size);
1370 close_file(fsp,ERROR_CLOSE);
1371 END_PROFILE(SMBopenX);
1372 return ERROR_NT(NT_STATUS_DISK_FULL);
1374 sbuf.st_size = get_allocation_size(conn,fsp,&sbuf);
1377 fattr = dos_mode(conn,fname,&sbuf);
1378 mtime = sbuf.st_mtime;
1380 close_file(fsp,ERROR_CLOSE);
1381 END_PROFILE(SMBopenX);
1382 return ERROR_DOS(ERRDOS,ERRnoaccess);
1385 /* If the caller set the extended oplock request bit
1386 and we granted one (by whatever means) - set the
1387 correct bit for extended oplock reply.
1390 if (ex_oplock_request && lp_fake_oplocks(SNUM(conn))) {
1391 smb_action |= EXTENDED_OPLOCK_GRANTED;
1394 if(ex_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1395 smb_action |= EXTENDED_OPLOCK_GRANTED;
1398 /* If the caller set the core oplock request bit
1399 and we granted one (by whatever means) - set the
1400 correct bit for core oplock reply.
1403 if (core_oplock_request && lp_fake_oplocks(SNUM(conn))) {
1404 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1407 if(core_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1408 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1411 if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
1412 set_message(outbuf,19,0,True);
1414 set_message(outbuf,15,0,True);
1416 SSVAL(outbuf,smb_vwv2,fsp->fnum);
1417 SSVAL(outbuf,smb_vwv3,fattr);
1418 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1419 srv_put_dos_date3(outbuf,smb_vwv4,mtime & ~1);
1421 srv_put_dos_date3(outbuf,smb_vwv4,mtime);
1423 SIVAL(outbuf,smb_vwv6,(uint32)sbuf.st_size);
1424 SSVAL(outbuf,smb_vwv8,GET_OPENX_MODE(deny_mode));
1425 SSVAL(outbuf,smb_vwv11,smb_action);
1427 if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
1428 SIVAL(outbuf, smb_vwv15, STD_RIGHT_ALL_ACCESS);
1431 END_PROFILE(SMBopenX);
1432 return chain_reply(inbuf,outbuf,length,bufsize);
1435 /****************************************************************************
1436 Reply to a SMBulogoffX.
1437 conn POINTER CAN BE NULL HERE !
1438 ****************************************************************************/
1440 int reply_ulogoffX(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
1442 uint16 vuid = SVAL(inbuf,smb_uid);
1443 user_struct *vuser = get_valid_user_struct(vuid);
1444 START_PROFILE(SMBulogoffX);
1447 DEBUG(3,("ulogoff, vuser id %d does not map to user.\n", vuid));
1449 /* in user level security we are supposed to close any files
1450 open by this user */
1451 if ((vuser != 0) && (lp_security() != SEC_SHARE))
1452 file_close_user(vuid);
1454 invalidate_vuid(vuid);
1456 set_message(outbuf,2,0,True);
1458 DEBUG( 3, ( "ulogoffX vuid=%d\n", vuid ) );
1460 END_PROFILE(SMBulogoffX);
1461 return chain_reply(inbuf,outbuf,length,bufsize);
1464 /****************************************************************************
1465 Reply to a mknew or a create.
1466 ****************************************************************************/
1468 int reply_mknew(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1473 uint32 fattr = SVAL(inbuf,smb_vwv0);
1474 struct utimbuf times;
1476 int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
1477 SMB_STRUCT_STAT sbuf;
1479 uint32 access_mask = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
1480 uint32 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1481 uint32 create_disposition;
1482 uint32 create_options = 0;
1484 START_PROFILE(SMBcreate);
1486 com = SVAL(inbuf,smb_com);
1488 times.modtime = srv_make_unix_date3(inbuf + smb_vwv1);
1490 srvstr_get_path(inbuf, fname, smb_buf(inbuf) + 1, sizeof(fname), 0, STR_TERMINATE, &status);
1491 if (!NT_STATUS_IS_OK(status)) {
1492 END_PROFILE(SMBcreate);
1493 return ERROR_NT(status);
1496 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1498 status = unix_convert(conn, fname, False, NULL, &sbuf);
1499 if (!NT_STATUS_IS_OK(status)) {
1500 END_PROFILE(SMBcreate);
1501 return ERROR_NT(status);
1504 if (fattr & aVOLID) {
1505 DEBUG(0,("Attempt to create file (%s) with volid set - please report this\n",fname));
1508 if(com == SMBmknew) {
1509 /* We should fail if file exists. */
1510 create_disposition = FILE_CREATE;
1512 /* Create if file doesn't exist, truncate if it does. */
1513 create_disposition = FILE_OVERWRITE_IF;
1516 /* Open file using ntcreate. */
1517 status = open_file_ntcreate(conn,fname,&sbuf,
1526 if (!NT_STATUS_IS_OK(status)) {
1527 END_PROFILE(SMBcreate);
1528 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1529 /* We have re-scheduled this call. */
1532 return ERROR_NT(status);
1535 times.actime = sbuf.st_atime;
1536 file_utime(conn, fname, ×);
1538 outsize = set_message(outbuf,1,0,True);
1539 SSVAL(outbuf,smb_vwv0,fsp->fnum);
1541 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1542 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1545 if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1546 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1549 DEBUG( 2, ( "reply_mknew: file %s\n", fname ) );
1550 DEBUG( 3, ( "reply_mknew %s fd=%d dmode=0x%x\n", fname, fsp->fh->fd, (unsigned int)fattr ) );
1552 END_PROFILE(SMBcreate);
1556 /****************************************************************************
1557 Reply to a create temporary file.
1558 ****************************************************************************/
1560 int reply_ctemp(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1564 uint32 fattr = SVAL(inbuf,smb_vwv0);
1566 int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
1568 SMB_STRUCT_STAT sbuf;
1571 unsigned int namelen;
1573 START_PROFILE(SMBctemp);
1575 srvstr_get_path(inbuf, fname, smb_buf(inbuf)+1, sizeof(fname), 0, STR_TERMINATE, &status);
1576 if (!NT_STATUS_IS_OK(status)) {
1577 END_PROFILE(SMBctemp);
1578 return ERROR_NT(status);
1581 pstrcat(fname,"/TMXXXXXX");
1583 pstrcat(fname,"TMXXXXXX");
1586 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1588 status = unix_convert(conn, fname, False, NULL, &sbuf);
1589 if (!NT_STATUS_IS_OK(status)) {
1590 END_PROFILE(SMBctemp);
1591 return ERROR_NT(status);
1594 tmpfd = smb_mkstemp(fname);
1596 END_PROFILE(SMBctemp);
1597 return(UNIXERROR(ERRDOS,ERRnoaccess));
1600 SMB_VFS_STAT(conn,fname,&sbuf);
1602 /* We should fail if file does not exist. */
1603 status = open_file_ntcreate(conn,fname,&sbuf,
1604 FILE_GENERIC_READ | FILE_GENERIC_WRITE,
1605 FILE_SHARE_READ|FILE_SHARE_WRITE,
1612 /* close fd from smb_mkstemp() */
1615 if (!NT_STATUS_IS_OK(status)) {
1616 END_PROFILE(SMBctemp);
1617 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1618 /* We have re-scheduled this call. */
1621 return ERROR_NT(status);
1624 outsize = set_message(outbuf,1,0,True);
1625 SSVAL(outbuf,smb_vwv0,fsp->fnum);
1627 /* the returned filename is relative to the directory */
1628 s = strrchr_m(fname, '/');
1635 p = smb_buf(outbuf);
1637 /* Tested vs W2K3 - this doesn't seem to be here - null terminated filename is the only
1638 thing in the byte section. JRA */
1639 SSVALS(p, 0, -1); /* what is this? not in spec */
1641 namelen = srvstr_push(outbuf, p, s, -1, STR_ASCII|STR_TERMINATE);
1643 outsize = set_message_end(outbuf, p);
1645 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1646 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1649 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1650 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1653 DEBUG( 2, ( "reply_ctemp: created temp file %s\n", fname ) );
1654 DEBUG( 3, ( "reply_ctemp %s fd=%d umode=0%o\n", fname, fsp->fh->fd,
1655 (unsigned int)sbuf.st_mode ) );
1657 END_PROFILE(SMBctemp);
1661 /*******************************************************************
1662 Check if a user is allowed to rename a file.
1663 ********************************************************************/
1665 static NTSTATUS can_rename(connection_struct *conn, char *fname, uint16 dirtype, SMB_STRUCT_STAT *pst)
1671 if (!CAN_WRITE(conn)) {
1672 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1675 fmode = dos_mode(conn,fname,pst);
1676 if ((fmode & ~dirtype) & (aHIDDEN | aSYSTEM)) {
1677 return NT_STATUS_NO_SUCH_FILE;
1680 if (S_ISDIR(pst->st_mode)) {
1681 return NT_STATUS_OK;
1684 status = open_file_ntcreate(conn, fname, pst,
1686 FILE_SHARE_READ|FILE_SHARE_WRITE,
1689 FILE_ATTRIBUTE_NORMAL,
1693 if (!NT_STATUS_IS_OK(status)) {
1696 close_file(fsp,NORMAL_CLOSE);
1697 return NT_STATUS_OK;
1700 /*******************************************************************
1701 Check if a user is allowed to delete a file.
1702 ********************************************************************/
1704 static NTSTATUS can_delete(connection_struct *conn, char *fname,
1705 uint32 dirtype, BOOL can_defer)
1707 SMB_STRUCT_STAT sbuf;
1710 uint32 dirtype_orig = dirtype;
1713 DEBUG(10,("can_delete: %s, dirtype = %d\n", fname, dirtype ));
1715 if (!CAN_WRITE(conn)) {
1716 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1719 if (SMB_VFS_LSTAT(conn,fname,&sbuf) != 0) {
1720 return map_nt_error_from_unix(errno);
1723 fattr = dos_mode(conn,fname,&sbuf);
1725 if (dirtype & FILE_ATTRIBUTE_NORMAL) {
1726 dirtype = aDIR|aARCH|aRONLY;
1729 dirtype &= (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM);
1731 return NT_STATUS_NO_SUCH_FILE;
1734 if (!dir_check_ftype(conn, fattr, dirtype)) {
1736 return NT_STATUS_FILE_IS_A_DIRECTORY;
1738 return NT_STATUS_NO_SUCH_FILE;
1741 if (dirtype_orig & 0x8000) {
1742 /* These will never be set for POSIX. */
1743 return NT_STATUS_NO_SUCH_FILE;
1747 if ((fattr & dirtype) & FILE_ATTRIBUTE_DIRECTORY) {
1748 return NT_STATUS_FILE_IS_A_DIRECTORY;
1751 if ((fattr & ~dirtype) & (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)) {
1752 return NT_STATUS_NO_SUCH_FILE;
1755 if (dirtype & 0xFF00) {
1756 /* These will never be set for POSIX. */
1757 return NT_STATUS_NO_SUCH_FILE;
1762 return NT_STATUS_NO_SUCH_FILE;
1765 /* Can't delete a directory. */
1767 return NT_STATUS_FILE_IS_A_DIRECTORY;
1772 else if (dirtype & aDIR) /* Asked for a directory and it isn't. */
1773 return NT_STATUS_OBJECT_NAME_INVALID;
1774 #endif /* JRATEST */
1776 /* Fix for bug #3035 from SATOH Fumiyasu <fumiyas@miraclelinux.com>
1778 On a Windows share, a file with read-only dosmode can be opened with
1779 DELETE_ACCESS. But on a Samba share (delete readonly = no), it
1780 fails with NT_STATUS_CANNOT_DELETE error.
1782 This semantic causes a problem that a user can not
1783 rename a file with read-only dosmode on a Samba share
1784 from a Windows command prompt (i.e. cmd.exe, but can rename
1785 from Windows Explorer).
1788 if (!lp_delete_readonly(SNUM(conn))) {
1789 if (fattr & aRONLY) {
1790 return NT_STATUS_CANNOT_DELETE;
1794 /* On open checks the open itself will check the share mode, so
1795 don't do it here as we'll get it wrong. */
1797 status = open_file_ntcreate(conn, fname, &sbuf,
1802 FILE_ATTRIBUTE_NORMAL,
1803 can_defer ? 0 : INTERNAL_OPEN_ONLY,
1806 if (NT_STATUS_IS_OK(status)) {
1807 close_file(fsp,NORMAL_CLOSE);
1812 /****************************************************************************
1813 The guts of the unlink command, split out so it may be called by the NT SMB
1815 ****************************************************************************/
1817 NTSTATUS unlink_internals(connection_struct *conn, uint32 dirtype,
1818 char *name, BOOL has_wild, BOOL can_defer)
1824 NTSTATUS status = NT_STATUS_OK;
1825 SMB_STRUCT_STAT sbuf;
1827 *directory = *mask = 0;
1829 status = unix_convert(conn, name, has_wild, NULL, &sbuf);
1830 if (!NT_STATUS_IS_OK(status)) {
1834 p = strrchr_m(name,'/');
1836 pstrcpy(directory,".");
1840 pstrcpy(directory,name);
1845 * We should only check the mangled cache
1846 * here if unix_convert failed. This means
1847 * that the path in 'mask' doesn't exist
1848 * on the file system and so we need to look
1849 * for a possible mangle. This patch from
1850 * Tine Smukavec <valentin.smukavec@hermes.si>.
1853 if (!VALID_STAT(sbuf) && mangle_is_mangled(mask,conn->params))
1854 mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
1857 pstrcat(directory,"/");
1858 pstrcat(directory,mask);
1860 dirtype = FILE_ATTRIBUTE_NORMAL;
1862 status = can_delete(conn,directory,dirtype,can_defer);
1863 if (!NT_STATUS_IS_OK(status)) {
1867 if (SMB_VFS_UNLINK(conn,directory) == 0) {
1869 notify_fname(conn, NOTIFY_ACTION_REMOVED,
1870 FILE_NOTIFY_CHANGE_FILE_NAME,
1874 struct smb_Dir *dir_hnd = NULL;
1878 if ((dirtype & SAMBA_ATTRIBUTES_MASK) == aDIR) {
1879 return NT_STATUS_OBJECT_NAME_INVALID;
1882 if (strequal(mask,"????????.???")) {
1886 status = check_name(conn, directory);
1887 if (!NT_STATUS_IS_OK(status)) {
1891 dir_hnd = OpenDir(conn, directory, mask, dirtype);
1892 if (dir_hnd == NULL) {
1893 return map_nt_error_from_unix(errno);
1896 /* XXXX the CIFS spec says that if bit0 of the flags2 field is set then
1897 the pattern matches against the long name, otherwise the short name
1898 We don't implement this yet XXXX
1901 status = NT_STATUS_NO_SUCH_FILE;
1903 while ((dname = ReadDirName(dir_hnd, &offset))) {
1906 pstrcpy(fname,dname);
1908 if (!is_visible_file(conn, directory, dname, &st, True)) {
1912 /* Quick check for "." and ".." */
1913 if (fname[0] == '.') {
1914 if (!fname[1] || (fname[1] == '.' && !fname[2])) {
1919 if(!mask_match(fname, mask, conn->case_sensitive)) {
1923 slprintf(fname,sizeof(fname)-1, "%s/%s",directory,dname);
1924 status = can_delete(conn, fname, dirtype, can_defer);
1925 if (!NT_STATUS_IS_OK(status)) {
1928 if (SMB_VFS_UNLINK(conn,fname) == 0) {
1930 DEBUG(3,("unlink_internals: succesful unlink "
1932 notify_fname(conn, NOTIFY_ACTION_REMOVED,
1933 FILE_NOTIFY_CHANGE_FILE_NAME,
1941 if (count == 0 && NT_STATUS_IS_OK(status)) {
1942 status = map_nt_error_from_unix(errno);
1948 /****************************************************************************
1950 ****************************************************************************/
1952 int reply_unlink(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
1959 BOOL path_contains_wcard = False;
1961 START_PROFILE(SMBunlink);
1963 dirtype = SVAL(inbuf,smb_vwv0);
1965 srvstr_get_path_wcard(inbuf, name, smb_buf(inbuf) + 1, sizeof(name), 0, STR_TERMINATE, &status, &path_contains_wcard);
1966 if (!NT_STATUS_IS_OK(status)) {
1967 END_PROFILE(SMBunlink);
1968 return ERROR_NT(status);
1971 RESOLVE_DFSPATH_WCARD(name, conn, inbuf, outbuf);
1973 DEBUG(3,("reply_unlink : %s\n",name));
1975 status = unlink_internals(conn, dirtype, name, path_contains_wcard,
1977 if (!NT_STATUS_IS_OK(status)) {
1978 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1979 /* We have re-scheduled this call. */
1982 return ERROR_NT(status);
1985 outsize = set_message(outbuf,0,0,False);
1987 END_PROFILE(SMBunlink);
1991 /****************************************************************************
1993 ****************************************************************************/
1995 static void fail_readraw(void)
1998 slprintf(errstr, sizeof(errstr)-1, "FAIL ! reply_readbraw: socket write fail (%s)",
2000 exit_server_cleanly(errstr);
2003 #if defined(WITH_SENDFILE)
2004 /****************************************************************************
2005 Fake (read/write) sendfile. Returns -1 on read or write fail.
2006 ****************************************************************************/
2008 static ssize_t fake_sendfile(files_struct *fsp, SMB_OFF_T startpos, size_t nread, char *buf, int bufsize)
2012 /* Paranioa check... */
2013 if (nread > bufsize) {
2018 ret = read_file(fsp,buf,startpos,nread);
2024 /* If we had a short read, fill with zeros. */
2026 memset(buf, '\0', nread - ret);
2029 if (write_data(smbd_server_fd(),buf,nread) != nread) {
2033 return (ssize_t)nread;
2037 /****************************************************************************
2038 Use sendfile in readbraw.
2039 ****************************************************************************/
2041 void send_file_readbraw(connection_struct *conn, files_struct *fsp, SMB_OFF_T startpos, size_t nread,
2042 ssize_t mincount, char *outbuf, int out_buffsize)
2046 #if defined(WITH_SENDFILE)
2048 * We can only use sendfile on a non-chained packet
2049 * but we can use on a non-oplocked file. tridge proved this
2050 * on a train in Germany :-). JRA.
2051 * reply_readbraw has already checked the length.
2054 if ( (chain_size == 0) && (nread > 0) &&
2055 (fsp->wcp == NULL) && lp_use_sendfile(SNUM(conn)) ) {
2058 _smb_setlen(outbuf,nread);
2059 header.data = (uint8 *)outbuf;
2063 if ( SMB_VFS_SENDFILE( smbd_server_fd(), fsp, fsp->fh->fd, &header, startpos, nread) == -1) {
2064 /* Returning ENOSYS means no data at all was sent. Do this as a normal read. */
2065 if (errno == ENOSYS) {
2066 goto normal_readbraw;
2070 * Special hack for broken Linux with no working sendfile. If we
2071 * return EINTR we sent the header but not the rest of the data.
2072 * Fake this up by doing read/write calls.
2074 if (errno == EINTR) {
2075 /* Ensure we don't do this again. */
2076 set_use_sendfile(SNUM(conn), False);
2077 DEBUG(0,("send_file_readbraw: sendfile not available. Faking..\n"));
2079 if (fake_sendfile(fsp, startpos, nread, outbuf + 4, out_buffsize - 4) == -1) {
2080 DEBUG(0,("send_file_readbraw: fake_sendfile failed for file %s (%s).\n",
2081 fsp->fsp_name, strerror(errno) ));
2082 exit_server_cleanly("send_file_readbraw fake_sendfile failed");
2087 DEBUG(0,("send_file_readbraw: sendfile failed for file %s (%s). Terminating\n",
2088 fsp->fsp_name, strerror(errno) ));
2089 exit_server_cleanly("send_file_readbraw sendfile failed");
2099 ret = read_file(fsp,outbuf+4,startpos,nread);
2100 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2109 _smb_setlen(outbuf,ret);
2110 if (write_data(smbd_server_fd(),outbuf,4+ret) != 4+ret)
2114 /****************************************************************************
2115 Reply to a readbraw (core+ protocol).
2116 ****************************************************************************/
2118 int reply_readbraw(connection_struct *conn, char *inbuf, char *outbuf, int dum_size, int out_buffsize)
2120 ssize_t maxcount,mincount;
2123 char *header = outbuf;
2125 START_PROFILE(SMBreadbraw);
2127 if (srv_is_signing_active()) {
2128 exit_server_cleanly("reply_readbraw: SMB signing is active - raw reads/writes are disallowed.");
2132 * Special check if an oplock break has been issued
2133 * and the readraw request croses on the wire, we must
2134 * return a zero length response here.
2137 fsp = file_fsp(inbuf,smb_vwv0);
2139 if (!FNUM_OK(fsp,conn) || !fsp->can_read) {
2141 * fsp could be NULL here so use the value from the packet. JRA.
2143 DEBUG(3,("fnum %d not open in readbraw - cache prime?\n",(int)SVAL(inbuf,smb_vwv0)));
2144 _smb_setlen(header,0);
2145 if (write_data(smbd_server_fd(),header,4) != 4)
2147 END_PROFILE(SMBreadbraw);
2151 CHECK_FSP(fsp,conn);
2153 flush_write_cache(fsp, READRAW_FLUSH);
2155 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
2156 if(CVAL(inbuf,smb_wct) == 10) {
2158 * This is a large offset (64 bit) read.
2160 #ifdef LARGE_SMB_OFF_T
2162 startpos |= (((SMB_OFF_T)IVAL(inbuf,smb_vwv8)) << 32);
2164 #else /* !LARGE_SMB_OFF_T */
2167 * Ensure we haven't been sent a >32 bit offset.
2170 if(IVAL(inbuf,smb_vwv8) != 0) {
2171 DEBUG(0,("readbraw - large offset (%x << 32) used and we don't support \
2172 64 bit offsets.\n", (unsigned int)IVAL(inbuf,smb_vwv8) ));
2173 _smb_setlen(header,0);
2174 if (write_data(smbd_server_fd(),header,4) != 4)
2176 END_PROFILE(SMBreadbraw);
2180 #endif /* LARGE_SMB_OFF_T */
2183 DEBUG(0,("readbraw - negative 64 bit readraw offset (%.0f) !\n", (double)startpos ));
2184 _smb_setlen(header,0);
2185 if (write_data(smbd_server_fd(),header,4) != 4)
2187 END_PROFILE(SMBreadbraw);
2191 maxcount = (SVAL(inbuf,smb_vwv3) & 0xFFFF);
2192 mincount = (SVAL(inbuf,smb_vwv4) & 0xFFFF);
2194 /* ensure we don't overrun the packet size */
2195 maxcount = MIN(65535,maxcount);
2197 if (!is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
2201 if (SMB_VFS_FSTAT(fsp,fsp->fh->fd,&st) == 0) {
2205 if (startpos >= size) {
2208 nread = MIN(maxcount,(size - startpos));
2212 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2213 if (nread < mincount)
2217 DEBUG( 3, ( "readbraw fnum=%d start=%.0f max=%lu min=%lu nread=%lu\n", fsp->fnum, (double)startpos,
2218 (unsigned long)maxcount, (unsigned long)mincount, (unsigned long)nread ) );
2220 send_file_readbraw(conn, fsp, startpos, nread, mincount, outbuf, out_buffsize);
2222 DEBUG(5,("readbraw finished\n"));
2223 END_PROFILE(SMBreadbraw);
2228 #define DBGC_CLASS DBGC_LOCKING
2230 /****************************************************************************
2231 Reply to a lockread (core+ protocol).
2232 ****************************************************************************/
2234 int reply_lockread(connection_struct *conn, char *inbuf,char *outbuf, int length, int dum_buffsiz)
2242 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2243 struct byte_range_lock *br_lck = NULL;
2244 START_PROFILE(SMBlockread);
2246 CHECK_FSP(fsp,conn);
2247 if (!CHECK_READ(fsp,inbuf)) {
2248 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2251 release_level_2_oplocks_on_change(fsp);
2253 numtoread = SVAL(inbuf,smb_vwv1);
2254 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2256 outsize = set_message(outbuf,5,3,True);
2257 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
2258 data = smb_buf(outbuf) + 3;
2261 * NB. Discovered by Menny Hamburger at Mainsoft. This is a core+
2262 * protocol request that predates the read/write lock concept.
2263 * Thus instead of asking for a read lock here we need to ask
2264 * for a write lock. JRA.
2265 * Note that the requested lock size is unaffected by max_recv.
2268 br_lck = do_lock(fsp,
2269 (uint32)SVAL(inbuf,smb_pid),
2270 (SMB_BIG_UINT)numtoread,
2271 (SMB_BIG_UINT)startpos,
2274 False, /* Non-blocking lock. */
2276 TALLOC_FREE(br_lck);
2278 if (NT_STATUS_V(status)) {
2279 END_PROFILE(SMBlockread);
2280 return ERROR_NT(status);
2284 * However the requested READ size IS affected by max_recv. Insanity.... JRA.
2287 if (numtoread > max_recv) {
2288 DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u). \
2289 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
2290 (unsigned int)numtoread, (unsigned int)max_recv ));
2291 numtoread = MIN(numtoread,max_recv);
2293 nread = read_file(fsp,data,startpos,numtoread);
2296 END_PROFILE(SMBlockread);
2297 return(UNIXERROR(ERRDOS,ERRnoaccess));
2301 SSVAL(outbuf,smb_vwv0,nread);
2302 SSVAL(outbuf,smb_vwv5,nread+3);
2303 SSVAL(smb_buf(outbuf),1,nread);
2305 DEBUG(3,("lockread fnum=%d num=%d nread=%d\n",
2306 fsp->fnum, (int)numtoread, (int)nread));
2308 END_PROFILE(SMBlockread);
2313 #define DBGC_CLASS DBGC_ALL
2315 /****************************************************************************
2317 ****************************************************************************/
2319 int reply_read(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
2326 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2327 START_PROFILE(SMBread);
2329 CHECK_FSP(fsp,conn);
2330 if (!CHECK_READ(fsp,inbuf)) {
2331 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2334 numtoread = SVAL(inbuf,smb_vwv1);
2335 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2337 outsize = set_message(outbuf,5,3,True);
2338 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
2340 * The requested read size cannot be greater than max_recv. JRA.
2342 if (numtoread > max_recv) {
2343 DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u). \
2344 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
2345 (unsigned int)numtoread, (unsigned int)max_recv ));
2346 numtoread = MIN(numtoread,max_recv);
2349 data = smb_buf(outbuf) + 3;
2351 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtoread,(SMB_BIG_UINT)startpos, READ_LOCK)) {
2352 END_PROFILE(SMBread);
2353 return ERROR_DOS(ERRDOS,ERRlock);
2357 nread = read_file(fsp,data,startpos,numtoread);
2360 END_PROFILE(SMBread);
2361 return(UNIXERROR(ERRDOS,ERRnoaccess));
2365 SSVAL(outbuf,smb_vwv0,nread);
2366 SSVAL(outbuf,smb_vwv5,nread+3);
2367 SCVAL(smb_buf(outbuf),0,1);
2368 SSVAL(smb_buf(outbuf),1,nread);
2370 DEBUG( 3, ( "read fnum=%d num=%d nread=%d\n",
2371 fsp->fnum, (int)numtoread, (int)nread ) );
2373 END_PROFILE(SMBread);
2377 /****************************************************************************
2378 Reply to a read and X - possibly using sendfile.
2379 ****************************************************************************/
2381 int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length, int len_outbuf,
2382 files_struct *fsp, SMB_OFF_T startpos, size_t smb_maxcnt)
2386 char *data = smb_buf(outbuf);
2388 #if defined(WITH_SENDFILE)
2390 * We can only use sendfile on a non-chained packet
2391 * but we can use on a non-oplocked file. tridge proved this
2392 * on a train in Germany :-). JRA.
2395 if ((chain_size == 0) && (CVAL(inbuf,smb_vwv0) == 0xFF) &&
2396 lp_use_sendfile(SNUM(conn)) && (fsp->wcp == NULL) ) {
2397 SMB_STRUCT_STAT sbuf;
2400 if(SMB_VFS_FSTAT(fsp,fsp->fh->fd, &sbuf) == -1)
2401 return(UNIXERROR(ERRDOS,ERRnoaccess));
2403 if (startpos > sbuf.st_size)
2406 if (smb_maxcnt > (sbuf.st_size - startpos))
2407 smb_maxcnt = (sbuf.st_size - startpos);
2409 if (smb_maxcnt == 0)
2413 * Set up the packet header before send. We
2414 * assume here the sendfile will work (get the
2415 * correct amount of data).
2418 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be -1. */
2419 SSVAL(outbuf,smb_vwv5,smb_maxcnt);
2420 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
2421 SSVAL(outbuf,smb_vwv7,((smb_maxcnt >> 16) & 1));
2422 SSVAL(smb_buf(outbuf),-2,smb_maxcnt);
2423 SCVAL(outbuf,smb_vwv0,0xFF);
2424 set_message(outbuf,12,smb_maxcnt,False);
2425 header.data = (uint8 *)outbuf;
2426 header.length = data - outbuf;
2429 if ((nread = SMB_VFS_SENDFILE( smbd_server_fd(), fsp, fsp->fh->fd, &header, startpos, smb_maxcnt)) == -1) {
2430 /* Returning ENOSYS means no data at all was sent. Do this as a normal read. */
2431 if (errno == ENOSYS) {
2436 * Special hack for broken Linux with no working sendfile. If we
2437 * return EINTR we sent the header but not the rest of the data.
2438 * Fake this up by doing read/write calls.
2441 if (errno == EINTR) {
2442 /* Ensure we don't do this again. */
2443 set_use_sendfile(SNUM(conn), False);
2444 DEBUG(0,("send_file_readX: sendfile not available. Faking..\n"));
2446 if ((nread = fake_sendfile(fsp, startpos, smb_maxcnt, data,
2447 len_outbuf - (data-outbuf))) == -1) {
2448 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
2449 fsp->fsp_name, strerror(errno) ));
2450 exit_server_cleanly("send_file_readX: fake_sendfile failed");
2452 DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
2453 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
2454 /* Returning -1 here means successful sendfile. */
2458 DEBUG(0,("send_file_readX: sendfile failed for file %s (%s). Terminating\n",
2459 fsp->fsp_name, strerror(errno) ));
2460 exit_server_cleanly("send_file_readX sendfile failed");
2463 DEBUG( 3, ( "send_file_readX: sendfile fnum=%d max=%d nread=%d\n",
2464 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
2465 /* Returning -1 here means successful sendfile. */
2473 nread = read_file(fsp,data,startpos,smb_maxcnt);
2476 return(UNIXERROR(ERRDOS,ERRnoaccess));
2479 outsize = set_message(outbuf,12,nread,False);
2480 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be -1. */
2481 SSVAL(outbuf,smb_vwv5,nread);
2482 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
2483 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
2484 SSVAL(smb_buf(outbuf),-2,nread);
2486 DEBUG( 3, ( "send_file_readX fnum=%d max=%d nread=%d\n",
2487 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
2489 /* Returning the number of bytes we want to send back - including header. */
2493 /****************************************************************************
2494 Reply to a read and X.
2495 ****************************************************************************/
2497 int reply_read_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
2499 files_struct *fsp = file_fsp(inbuf,smb_vwv2);
2500 SMB_OFF_T startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
2502 size_t smb_maxcnt = SVAL(inbuf,smb_vwv5);
2504 size_t smb_mincnt = SVAL(inbuf,smb_vwv6);
2507 START_PROFILE(SMBreadX);
2509 /* If it's an IPC, pass off the pipe handler. */
2511 END_PROFILE(SMBreadX);
2512 return reply_pipe_read_and_X(inbuf,outbuf,length,bufsize);
2515 CHECK_FSP(fsp,conn);
2516 if (!CHECK_READ(fsp,inbuf)) {
2517 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2520 set_message(outbuf,12,0,True);
2522 if (global_client_caps & CAP_LARGE_READX) {
2523 if (SVAL(inbuf,smb_vwv7) == 1) {
2524 smb_maxcnt |= (1<<16);
2526 if (smb_maxcnt > BUFFER_SIZE) {
2527 DEBUG(0,("reply_read_and_X - read too large (%u) for reply buffer %u\n",
2528 (unsigned int)smb_maxcnt, (unsigned int)BUFFER_SIZE));
2529 END_PROFILE(SMBreadX);
2530 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2534 if(CVAL(inbuf,smb_wct) == 12) {
2535 #ifdef LARGE_SMB_OFF_T
2537 * This is a large offset (64 bit) read.
2539 startpos |= (((SMB_OFF_T)IVAL(inbuf,smb_vwv10)) << 32);
2541 #else /* !LARGE_SMB_OFF_T */
2544 * Ensure we haven't been sent a >32 bit offset.
2547 if(IVAL(inbuf,smb_vwv10) != 0) {
2548 DEBUG(0,("reply_read_and_X - large offset (%x << 32) used and we don't support \
2549 64 bit offsets.\n", (unsigned int)IVAL(inbuf,smb_vwv10) ));
2550 END_PROFILE(SMBreadX);
2551 return ERROR_DOS(ERRDOS,ERRbadaccess);
2554 #endif /* LARGE_SMB_OFF_T */
2558 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)smb_maxcnt,(SMB_BIG_UINT)startpos, READ_LOCK)) {
2559 END_PROFILE(SMBreadX);
2560 return ERROR_DOS(ERRDOS,ERRlock);
2563 if (schedule_aio_read_and_X(conn, inbuf, outbuf, length, bufsize, fsp, startpos, smb_maxcnt)) {
2564 END_PROFILE(SMBreadX);
2568 nread = send_file_readX(conn, inbuf, outbuf, length, bufsize, fsp, startpos, smb_maxcnt);
2570 nread = chain_reply(inbuf,outbuf,length,bufsize);
2572 END_PROFILE(SMBreadX);
2576 /****************************************************************************
2577 Reply to a writebraw (core+ or LANMAN1.0 protocol).
2578 ****************************************************************************/
2580 int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
2583 ssize_t total_written=0;
2584 size_t numtowrite=0;
2589 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2591 START_PROFILE(SMBwritebraw);
2593 if (srv_is_signing_active()) {
2594 exit_server_cleanly("reply_writebraw: SMB signing is active - raw reads/writes are disallowed.");
2597 CHECK_FSP(fsp,conn);
2598 if (!CHECK_WRITE(fsp)) {
2599 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2602 tcount = IVAL(inbuf,smb_vwv1);
2603 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
2604 write_through = BITSETW(inbuf+smb_vwv7,0);
2606 /* We have to deal with slightly different formats depending
2607 on whether we are using the core+ or lanman1.0 protocol */
2609 if(Protocol <= PROTOCOL_COREPLUS) {
2610 numtowrite = SVAL(smb_buf(inbuf),-2);
2611 data = smb_buf(inbuf);
2613 numtowrite = SVAL(inbuf,smb_vwv10);
2614 data = smb_base(inbuf) + SVAL(inbuf, smb_vwv11);
2617 /* force the error type */
2618 SCVAL(inbuf,smb_com,SMBwritec);
2619 SCVAL(outbuf,smb_com,SMBwritec);
2621 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
2622 END_PROFILE(SMBwritebraw);
2623 return(ERROR_DOS(ERRDOS,ERRlock));
2627 nwritten = write_file(fsp,data,startpos,numtowrite);
2629 DEBUG(3,("writebraw1 fnum=%d start=%.0f num=%d wrote=%d sync=%d\n",
2630 fsp->fnum, (double)startpos, (int)numtowrite, (int)nwritten, (int)write_through));
2632 if (nwritten < (ssize_t)numtowrite) {
2633 END_PROFILE(SMBwritebraw);
2634 return(UNIXERROR(ERRHRD,ERRdiskfull));
2637 total_written = nwritten;
2639 /* Return a message to the redirector to tell it to send more bytes */
2640 SCVAL(outbuf,smb_com,SMBwritebraw);
2641 SSVALS(outbuf,smb_vwv0,-1);
2642 outsize = set_message(outbuf,Protocol>PROTOCOL_COREPLUS?1:0,0,True);
2644 if (!send_smb(smbd_server_fd(),outbuf))
2645 exit_server_cleanly("reply_writebraw: send_smb failed.");
2647 /* Now read the raw data into the buffer and write it */
2648 if (read_smb_length(smbd_server_fd(),inbuf,SMB_SECONDARY_WAIT) == -1) {
2649 exit_server_cleanly("secondary writebraw failed");
2652 /* Even though this is not an smb message, smb_len returns the generic length of an smb message */
2653 numtowrite = smb_len(inbuf);
2655 /* Set up outbuf to return the correct return */
2656 outsize = set_message(outbuf,1,0,True);
2657 SCVAL(outbuf,smb_com,SMBwritec);
2659 if (numtowrite != 0) {
2661 if (numtowrite > BUFFER_SIZE) {
2662 DEBUG(0,("reply_writebraw: Oversize secondary write raw requested (%u). Terminating\n",
2663 (unsigned int)numtowrite ));
2664 exit_server_cleanly("secondary writebraw failed");
2667 if (tcount > nwritten+numtowrite) {
2668 DEBUG(3,("Client overestimated the write %d %d %d\n",
2669 (int)tcount,(int)nwritten,(int)numtowrite));
2672 if (read_data( smbd_server_fd(), inbuf+4, numtowrite) != numtowrite ) {
2673 DEBUG(0,("reply_writebraw: Oversize secondary write raw read failed (%s). Terminating\n",
2675 exit_server_cleanly("secondary writebraw failed");
2678 nwritten = write_file(fsp,inbuf+4,startpos+nwritten,numtowrite);
2679 if (nwritten == -1) {
2680 END_PROFILE(SMBwritebraw);
2681 return(UNIXERROR(ERRHRD,ERRdiskfull));
2684 if (nwritten < (ssize_t)numtowrite) {
2685 SCVAL(outbuf,smb_rcls,ERRHRD);
2686 SSVAL(outbuf,smb_err,ERRdiskfull);
2690 total_written += nwritten;
2693 SSVAL(outbuf,smb_vwv0,total_written);
2695 sync_file(conn, fsp, write_through);
2697 DEBUG(3,("writebraw2 fnum=%d start=%.0f num=%d wrote=%d\n",
2698 fsp->fnum, (double)startpos, (int)numtowrite,(int)total_written));
2700 /* we won't return a status if write through is not selected - this follows what WfWg does */
2701 END_PROFILE(SMBwritebraw);
2702 if (!write_through && total_written==tcount) {
2704 #if RABBIT_PELLET_FIX
2706 * Fix for "rabbit pellet" mode, trigger an early TCP ack by
2707 * sending a SMBkeepalive. Thanks to DaveCB at Sun for this. JRA.
2709 if (!send_keepalive(smbd_server_fd()))
2710 exit_server_cleanly("reply_writebraw: send of keepalive failed");
2719 #define DBGC_CLASS DBGC_LOCKING
2721 /****************************************************************************
2722 Reply to a writeunlock (core+).
2723 ****************************************************************************/
2725 int reply_writeunlock(connection_struct *conn, char *inbuf,char *outbuf,
2726 int size, int dum_buffsize)
2728 ssize_t nwritten = -1;
2732 NTSTATUS status = NT_STATUS_OK;
2733 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2735 START_PROFILE(SMBwriteunlock);
2737 CHECK_FSP(fsp,conn);
2738 if (!CHECK_WRITE(fsp)) {
2739 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2742 numtowrite = SVAL(inbuf,smb_vwv1);
2743 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2744 data = smb_buf(inbuf) + 3;
2746 if (numtowrite && is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
2747 END_PROFILE(SMBwriteunlock);
2748 return ERROR_DOS(ERRDOS,ERRlock);
2751 /* The special X/Open SMB protocol handling of
2752 zero length writes is *NOT* done for
2754 if(numtowrite == 0) {
2757 nwritten = write_file(fsp,data,startpos,numtowrite);
2760 sync_file(conn, fsp, False /* write through */);
2762 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
2763 END_PROFILE(SMBwriteunlock);
2764 return(UNIXERROR(ERRHRD,ERRdiskfull));
2768 status = do_unlock(fsp,
2769 (uint32)SVAL(inbuf,smb_pid),
2770 (SMB_BIG_UINT)numtowrite,
2771 (SMB_BIG_UINT)startpos,
2774 if (NT_STATUS_V(status)) {
2775 END_PROFILE(SMBwriteunlock);
2776 return ERROR_NT(status);
2780 outsize = set_message(outbuf,1,0,True);
2782 SSVAL(outbuf,smb_vwv0,nwritten);
2784 DEBUG(3,("writeunlock fnum=%d num=%d wrote=%d\n",
2785 fsp->fnum, (int)numtowrite, (int)nwritten));
2787 END_PROFILE(SMBwriteunlock);
2792 #define DBGC_CLASS DBGC_ALL
2794 /****************************************************************************
2796 ****************************************************************************/
2798 int reply_write(connection_struct *conn, char *inbuf,char *outbuf,int size,int dum_buffsize)
2801 ssize_t nwritten = -1;
2804 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2806 START_PROFILE(SMBwrite);
2808 /* If it's an IPC, pass off the pipe handler. */
2810 END_PROFILE(SMBwrite);
2811 return reply_pipe_write(inbuf,outbuf,size,dum_buffsize);
2814 CHECK_FSP(fsp,conn);
2815 if (!CHECK_WRITE(fsp)) {
2816 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2819 numtowrite = SVAL(inbuf,smb_vwv1);
2820 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2821 data = smb_buf(inbuf) + 3;
2823 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
2824 END_PROFILE(SMBwrite);
2825 return ERROR_DOS(ERRDOS,ERRlock);
2829 * X/Open SMB protocol says that if smb_vwv1 is
2830 * zero then the file size should be extended or
2831 * truncated to the size given in smb_vwv[2-3].
2834 if(numtowrite == 0) {
2836 * This is actually an allocate call, and set EOF. JRA.
2838 nwritten = vfs_allocate_file_space(fsp, (SMB_OFF_T)startpos);
2840 END_PROFILE(SMBwrite);
2841 return ERROR_NT(NT_STATUS_DISK_FULL);
2843 nwritten = vfs_set_filelen(fsp, (SMB_OFF_T)startpos);
2845 END_PROFILE(SMBwrite);
2846 return ERROR_NT(NT_STATUS_DISK_FULL);
2849 nwritten = write_file(fsp,data,startpos,numtowrite);
2851 sync_file(conn, fsp, False);
2853 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
2854 END_PROFILE(SMBwrite);
2855 return(UNIXERROR(ERRHRD,ERRdiskfull));
2858 outsize = set_message(outbuf,1,0,True);
2860 SSVAL(outbuf,smb_vwv0,nwritten);
2862 if (nwritten < (ssize_t)numtowrite) {
2863 SCVAL(outbuf,smb_rcls,ERRHRD);
2864 SSVAL(outbuf,smb_err,ERRdiskfull);
2867 DEBUG(3,("write fnum=%d num=%d wrote=%d\n", fsp->fnum, (int)numtowrite, (int)nwritten));
2869 END_PROFILE(SMBwrite);
2873 /****************************************************************************
2874 Reply to a write and X.
2875 ****************************************************************************/
2877 int reply_write_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
2879 files_struct *fsp = file_fsp(inbuf,smb_vwv2);
2880 SMB_OFF_T startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
2881 size_t numtowrite = SVAL(inbuf,smb_vwv10);
2882 BOOL write_through = BITSETW(inbuf+smb_vwv7,0);
2883 ssize_t nwritten = -1;
2884 unsigned int smb_doff = SVAL(inbuf,smb_vwv11);
2885 unsigned int smblen = smb_len(inbuf);
2887 BOOL large_writeX = ((CVAL(inbuf,smb_wct) == 14) && (smblen > 0xFFFF));
2888 START_PROFILE(SMBwriteX);
2890 /* If it's an IPC, pass off the pipe handler. */
2892 END_PROFILE(SMBwriteX);
2893 return reply_pipe_write_and_X(inbuf,outbuf,length,bufsize);
2896 CHECK_FSP(fsp,conn);
2897 if (!CHECK_WRITE(fsp)) {
2898 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2901 set_message(outbuf,6,0,True);
2903 /* Deal with possible LARGE_WRITEX */
2905 numtowrite |= ((((size_t)SVAL(inbuf,smb_vwv9)) & 1 )<<16);
2908 if(smb_doff > smblen || (smb_doff + numtowrite > smblen)) {
2909 END_PROFILE(SMBwriteX);
2910 return ERROR_DOS(ERRDOS,ERRbadmem);
2913 data = smb_base(inbuf) + smb_doff;
2915 if(CVAL(inbuf,smb_wct) == 14) {
2916 #ifdef LARGE_SMB_OFF_T
2918 * This is a large offset (64 bit) write.
2920 startpos |= (((SMB_OFF_T)IVAL(inbuf,smb_vwv12)) << 32);
2922 #else /* !LARGE_SMB_OFF_T */
2925 * Ensure we haven't been sent a >32 bit offset.
2928 if(IVAL(inbuf,smb_vwv12) != 0) {
2929 DEBUG(0,("reply_write_and_X - large offset (%x << 32) used and we don't support \
2930 64 bit offsets.\n", (unsigned int)IVAL(inbuf,smb_vwv12) ));
2931 END_PROFILE(SMBwriteX);
2932 return ERROR_DOS(ERRDOS,ERRbadaccess);
2935 #endif /* LARGE_SMB_OFF_T */
2938 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
2939 END_PROFILE(SMBwriteX);
2940 return ERROR_DOS(ERRDOS,ERRlock);
2943 /* X/Open SMB protocol says that, unlike SMBwrite
2944 if the length is zero then NO truncation is
2945 done, just a write of zero. To truncate a file,
2948 if(numtowrite == 0) {
2952 if (schedule_aio_write_and_X(conn, inbuf, outbuf, length, bufsize,
2953 fsp,data,startpos,numtowrite)) {
2954 END_PROFILE(SMBwriteX);
2958 nwritten = write_file(fsp,data,startpos,numtowrite);
2961 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
2962 END_PROFILE(SMBwriteX);
2963 return(UNIXERROR(ERRHRD,ERRdiskfull));
2966 SSVAL(outbuf,smb_vwv2,nwritten);
2968 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
2970 if (nwritten < (ssize_t)numtowrite) {
2971 SCVAL(outbuf,smb_rcls,ERRHRD);
2972 SSVAL(outbuf,smb_err,ERRdiskfull);
2975 DEBUG(3,("writeX fnum=%d num=%d wrote=%d\n",
2976 fsp->fnum, (int)numtowrite, (int)nwritten));
2978 sync_file(conn, fsp, write_through);
2980 END_PROFILE(SMBwriteX);
2981 return chain_reply(inbuf,outbuf,length,bufsize);
2984 /****************************************************************************
2986 ****************************************************************************/
2988 int reply_lseek(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
2994 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2995 START_PROFILE(SMBlseek);
2997 CHECK_FSP(fsp,conn);
2999 flush_write_cache(fsp, SEEK_FLUSH);
3001 mode = SVAL(inbuf,smb_vwv1) & 3;
3002 /* NB. This doesn't use IVAL_TO_SMB_OFF_T as startpos can be signed in this case. */
3003 startpos = (SMB_OFF_T)IVALS(inbuf,smb_vwv2);
3012 res = fsp->fh->pos + startpos;
3023 if (umode == SEEK_END) {
3024 if((res = SMB_VFS_LSEEK(fsp,fsp->fh->fd,startpos,umode)) == -1) {
3025 if(errno == EINVAL) {
3026 SMB_OFF_T current_pos = startpos;
3027 SMB_STRUCT_STAT sbuf;
3029 if(SMB_VFS_FSTAT(fsp,fsp->fh->fd, &sbuf) == -1) {
3030 END_PROFILE(SMBlseek);
3031 return(UNIXERROR(ERRDOS,ERRnoaccess));
3034 current_pos += sbuf.st_size;
3036 res = SMB_VFS_LSEEK(fsp,fsp->fh->fd,0,SEEK_SET);
3041 END_PROFILE(SMBlseek);
3042 return(UNIXERROR(ERRDOS,ERRnoaccess));
3048 outsize = set_message(outbuf,2,0,True);
3049 SIVAL(outbuf,smb_vwv0,res);
3051 DEBUG(3,("lseek fnum=%d ofs=%.0f newpos = %.0f mode=%d\n",
3052 fsp->fnum, (double)startpos, (double)res, mode));
3054 END_PROFILE(SMBlseek);
3058 /****************************************************************************
3060 ****************************************************************************/
3062 int reply_flush(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
3064 int outsize = set_message(outbuf,0,0,False);
3065 uint16 fnum = SVAL(inbuf,smb_vwv0);
3066 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3067 START_PROFILE(SMBflush);
3070 CHECK_FSP(fsp,conn);
3073 file_sync_all(conn);
3075 sync_file(conn,fsp, True);
3078 DEBUG(3,("flush\n"));
3079 END_PROFILE(SMBflush);
3083 /****************************************************************************
3085 conn POINTER CAN BE NULL HERE !
3086 ****************************************************************************/
3088 int reply_exit(connection_struct *conn,
3089 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3092 START_PROFILE(SMBexit);
3094 file_close_pid(SVAL(inbuf,smb_pid),SVAL(inbuf,smb_uid));
3096 outsize = set_message(outbuf,0,0,False);
3098 DEBUG(3,("exit\n"));
3100 END_PROFILE(SMBexit);
3104 /****************************************************************************
3105 Reply to a close - has to deal with closing a directory opened by NT SMB's.
3106 ****************************************************************************/
3108 int reply_close(connection_struct *conn, char *inbuf,char *outbuf, int size,
3113 int32 eclass = 0, err = 0;
3114 files_struct *fsp = NULL;
3115 START_PROFILE(SMBclose);
3117 outsize = set_message(outbuf,0,0,False);
3119 /* If it's an IPC, pass off to the pipe handler. */
3121 END_PROFILE(SMBclose);
3122 return reply_pipe_close(conn, inbuf,outbuf);
3125 fsp = file_fsp(inbuf,smb_vwv0);
3128 * We can only use CHECK_FSP if we know it's not a directory.
3131 if(!fsp || (fsp->conn != conn) || (fsp->vuid != current_user.vuid)) {
3132 END_PROFILE(SMBclose);
3133 return ERROR_DOS(ERRDOS,ERRbadfid);
3136 if(fsp->is_directory) {
3138 * Special case - close NT SMB directory handle.
3140 DEBUG(3,("close directory fnum=%d\n", fsp->fnum));
3141 close_file(fsp,NORMAL_CLOSE);
3144 * Close ordinary file.
3148 DEBUG(3,("close fd=%d fnum=%d (numopen=%d)\n",
3149 fsp->fh->fd, fsp->fnum,
3150 conn->num_files_open));
3153 * Take care of any time sent in the close.
3156 mtime = srv_make_unix_date3(inbuf+smb_vwv1);
3157 fsp_set_pending_modtime(fsp, mtime);
3160 * close_file() returns the unix errno if an error
3161 * was detected on close - normally this is due to
3162 * a disk full error. If not then it was probably an I/O error.
3165 if((close_err = close_file(fsp,NORMAL_CLOSE)) != 0) {
3167 END_PROFILE(SMBclose);
3168 return (UNIXERROR(ERRHRD,ERRgeneral));
3172 /* We have a cached error */
3174 END_PROFILE(SMBclose);
3175 return ERROR_DOS(eclass,err);
3178 END_PROFILE(SMBclose);
3182 /****************************************************************************
3183 Reply to a writeclose (Core+ protocol).
3184 ****************************************************************************/
3186 int reply_writeclose(connection_struct *conn,
3187 char *inbuf,char *outbuf, int size, int dum_buffsize)
3190 ssize_t nwritten = -1;
3196 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3197 START_PROFILE(SMBwriteclose);
3199 CHECK_FSP(fsp,conn);
3200 if (!CHECK_WRITE(fsp)) {
3201 return(ERROR_DOS(ERRDOS,ERRbadaccess));
3204 numtowrite = SVAL(inbuf,smb_vwv1);
3205 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
3206 mtime = srv_make_unix_date3(inbuf+smb_vwv4);
3207 data = smb_buf(inbuf) + 1;
3209 if (numtowrite && is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
3210 END_PROFILE(SMBwriteclose);
3211 return ERROR_DOS(ERRDOS,ERRlock);
3214 nwritten = write_file(fsp,data,startpos,numtowrite);
3216 set_filetime(conn, fsp->fsp_name,mtime);
3219 * More insanity. W2K only closes the file if writelen > 0.
3224 DEBUG(3,("reply_writeclose: zero length write doesn't close file %s\n",
3226 close_err = close_file(fsp,NORMAL_CLOSE);
3229 DEBUG(3,("writeclose fnum=%d num=%d wrote=%d (numopen=%d)\n",
3230 fsp->fnum, (int)numtowrite, (int)nwritten,
3231 conn->num_files_open));
3233 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
3234 END_PROFILE(SMBwriteclose);
3235 return(UNIXERROR(ERRHRD,ERRdiskfull));
3238 if(close_err != 0) {
3240 END_PROFILE(SMBwriteclose);
3241 return(UNIXERROR(ERRHRD,ERRgeneral));
3244 outsize = set_message(outbuf,1,0,True);
3246 SSVAL(outbuf,smb_vwv0,nwritten);
3247 END_PROFILE(SMBwriteclose);
3252 #define DBGC_CLASS DBGC_LOCKING
3254 /****************************************************************************
3256 ****************************************************************************/
3258 int reply_lock(connection_struct *conn,
3259 char *inbuf,char *outbuf, int length, int dum_buffsize)
3261 int outsize = set_message(outbuf,0,0,False);
3262 SMB_BIG_UINT count,offset;
3264 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3265 struct byte_range_lock *br_lck = NULL;
3267 START_PROFILE(SMBlock);
3269 CHECK_FSP(fsp,conn);
3271 release_level_2_oplocks_on_change(fsp);
3273 count = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv1);
3274 offset = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv3);
3276 DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
3277 fsp->fh->fd, fsp->fnum, (double)offset, (double)count));
3279 br_lck = do_lock(fsp,
3280 (uint32)SVAL(inbuf,smb_pid),
3285 False, /* Non-blocking lock. */
3288 TALLOC_FREE(br_lck);
3290 if (NT_STATUS_V(status)) {
3291 END_PROFILE(SMBlock);
3292 return ERROR_NT(status);
3295 END_PROFILE(SMBlock);
3299 /****************************************************************************
3301 ****************************************************************************/
3303 int reply_unlock(connection_struct *conn, char *inbuf,char *outbuf, int size,
3306 int outsize = set_message(outbuf,0,0,False);
3307 SMB_BIG_UINT count,offset;
3309 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3310 START_PROFILE(SMBunlock);
3312 CHECK_FSP(fsp,conn);
3314 count = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv1);
3315 offset = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv3);
3317 status = do_unlock(fsp,
3318 (uint32)SVAL(inbuf,smb_pid),
3323 if (NT_STATUS_V(status)) {
3324 END_PROFILE(SMBunlock);
3325 return ERROR_NT(status);
3328 DEBUG( 3, ( "unlock fd=%d fnum=%d offset=%.0f count=%.0f\n",
3329 fsp->fh->fd, fsp->fnum, (double)offset, (double)count ) );
3331 END_PROFILE(SMBunlock);
3336 #define DBGC_CLASS DBGC_ALL
3338 /****************************************************************************
3340 conn POINTER CAN BE NULL HERE !
3341 ****************************************************************************/
3343 int reply_tdis(connection_struct *conn,
3344 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3346 int outsize = set_message(outbuf,0,0,False);
3348 START_PROFILE(SMBtdis);
3350 vuid = SVAL(inbuf,smb_uid);
3353 DEBUG(4,("Invalid connection in tdis\n"));
3354 END_PROFILE(SMBtdis);
3355 return ERROR_DOS(ERRSRV,ERRinvnid);
3360 close_cnum(conn,vuid);
3362 END_PROFILE(SMBtdis);
3366 /****************************************************************************
3368 conn POINTER CAN BE NULL HERE !
3369 ****************************************************************************/
3371 int reply_echo(connection_struct *conn,
3372 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3374 int smb_reverb = SVAL(inbuf,smb_vwv0);
3376 unsigned int data_len = smb_buflen(inbuf);
3377 int outsize = set_message(outbuf,1,data_len,True);
3378 START_PROFILE(SMBecho);
3380 if (data_len > BUFFER_SIZE) {
3381 DEBUG(0,("reply_echo: data_len too large.\n"));
3382 END_PROFILE(SMBecho);
3386 /* copy any incoming data back out */
3388 memcpy(smb_buf(outbuf),smb_buf(inbuf),data_len);
3390 if (smb_reverb > 100) {
3391 DEBUG(0,("large reverb (%d)?? Setting to 100\n",smb_reverb));
3395 for (seq_num =1 ; seq_num <= smb_reverb ; seq_num++) {
3396 SSVAL(outbuf,smb_vwv0,seq_num);
3398 smb_setlen(outbuf,outsize - 4);
3401 if (!send_smb(smbd_server_fd(),outbuf))
3402 exit_server_cleanly("reply_echo: send_smb failed.");
3405 DEBUG(3,("echo %d times\n", smb_reverb));
3409 END_PROFILE(SMBecho);
3413 /****************************************************************************
3414 Reply to a printopen.
3415 ****************************************************************************/
3417 int reply_printopen(connection_struct *conn,
3418 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3424 START_PROFILE(SMBsplopen);
3426 if (!CAN_PRINT(conn)) {
3427 END_PROFILE(SMBsplopen);
3428 return ERROR_DOS(ERRDOS,ERRnoaccess);
3431 /* Open for exclusive use, write only. */
3432 status = print_fsp_open(conn, NULL, &fsp);
3434 if (!NT_STATUS_IS_OK(status)) {
3435 END_PROFILE(SMBsplopen);
3436 return(ERROR_NT(status));
3439 outsize = set_message(outbuf,1,0,True);
3440 SSVAL(outbuf,smb_vwv0,fsp->fnum);
3442 DEBUG(3,("openprint fd=%d fnum=%d\n",
3443 fsp->fh->fd, fsp->fnum));
3445 END_PROFILE(SMBsplopen);
3449 /****************************************************************************
3450 Reply to a printclose.
3451 ****************************************************************************/
3453 int reply_printclose(connection_struct *conn,
3454 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3456 int outsize = set_message(outbuf,0,0,False);
3457 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3459 START_PROFILE(SMBsplclose);
3461 CHECK_FSP(fsp,conn);
3463 if (!CAN_PRINT(conn)) {
3464 END_PROFILE(SMBsplclose);
3465 return ERROR_NT(NT_STATUS_DOS(ERRSRV, ERRerror));
3468 DEBUG(3,("printclose fd=%d fnum=%d\n",
3469 fsp->fh->fd,fsp->fnum));
3471 close_err = close_file(fsp,NORMAL_CLOSE);
3473 if(close_err != 0) {
3475 END_PROFILE(SMBsplclose);
3476 return(UNIXERROR(ERRHRD,ERRgeneral));
3479 END_PROFILE(SMBsplclose);
3483 /****************************************************************************
3484 Reply to a printqueue.
3485 ****************************************************************************/
3487 int reply_printqueue(connection_struct *conn,
3488 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3490 int outsize = set_message(outbuf,2,3,True);
3491 int max_count = SVAL(inbuf,smb_vwv0);
3492 int start_index = SVAL(inbuf,smb_vwv1);
3493 START_PROFILE(SMBsplretq);
3495 /* we used to allow the client to get the cnum wrong, but that
3496 is really quite gross and only worked when there was only
3497 one printer - I think we should now only accept it if they
3498 get it right (tridge) */
3499 if (!CAN_PRINT(conn)) {
3500 END_PROFILE(SMBsplretq);
3501 return ERROR_DOS(ERRDOS,ERRnoaccess);
3504 SSVAL(outbuf,smb_vwv0,0);
3505 SSVAL(outbuf,smb_vwv1,0);
3506 SCVAL(smb_buf(outbuf),0,1);
3507 SSVAL(smb_buf(outbuf),1,0);
3509 DEBUG(3,("printqueue start_index=%d max_count=%d\n",
3510 start_index, max_count));
3513 print_queue_struct *queue = NULL;
3514 print_status_struct status;
3515 char *p = smb_buf(outbuf) + 3;
3516 int count = print_queue_status(SNUM(conn), &queue, &status);
3517 int num_to_get = ABS(max_count);
3518 int first = (max_count>0?start_index:start_index+max_count+1);
3524 num_to_get = MIN(num_to_get,count-first);
3527 for (i=first;i<first+num_to_get;i++) {
3528 srv_put_dos_date2(p,0,queue[i].time);
3529 SCVAL(p,4,(queue[i].status==LPQ_PRINTING?2:3));
3530 SSVAL(p,5, queue[i].job);
3531 SIVAL(p,7,queue[i].size);
3533 srvstr_push(outbuf, p+12, queue[i].fs_user, 16, STR_ASCII);
3538 outsize = set_message(outbuf,2,28*count+3,False);
3539 SSVAL(outbuf,smb_vwv0,count);
3540 SSVAL(outbuf,smb_vwv1,(max_count>0?first+count:first-1));
3541 SCVAL(smb_buf(outbuf),0,1);
3542 SSVAL(smb_buf(outbuf),1,28*count);
3547 DEBUG(3,("%d entries returned in queue\n",count));
3550 END_PROFILE(SMBsplretq);
3554 /****************************************************************************
3555 Reply to a printwrite.
3556 ****************************************************************************/
3558 int reply_printwrite(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3561 int outsize = set_message(outbuf,0,0,False);
3563 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3565 START_PROFILE(SMBsplwr);
3567 if (!CAN_PRINT(conn)) {
3568 END_PROFILE(SMBsplwr);
3569 return ERROR_DOS(ERRDOS,ERRnoaccess);
3572 CHECK_FSP(fsp,conn);
3573 if (!CHECK_WRITE(fsp)) {
3574 return(ERROR_DOS(ERRDOS,ERRbadaccess));
3577 numtowrite = SVAL(smb_buf(inbuf),1);
3578 data = smb_buf(inbuf) + 3;
3580 if (write_file(fsp,data,-1,numtowrite) != numtowrite) {
3581 END_PROFILE(SMBsplwr);
3582 return(UNIXERROR(ERRHRD,ERRdiskfull));
3585 DEBUG( 3, ( "printwrite fnum=%d num=%d\n", fsp->fnum, numtowrite ) );
3587 END_PROFILE(SMBsplwr);
3591 /****************************************************************************
3593 ****************************************************************************/
3595 int reply_mkdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3600 SMB_STRUCT_STAT sbuf;
3602 START_PROFILE(SMBmkdir);
3604 srvstr_get_path(inbuf, directory, smb_buf(inbuf) + 1, sizeof(directory), 0, STR_TERMINATE, &status);
3605 if (!NT_STATUS_IS_OK(status)) {
3606 END_PROFILE(SMBmkdir);
3607 return ERROR_NT(status);
3610 RESOLVE_DFSPATH(directory, conn, inbuf, outbuf);
3612 status = unix_convert(conn, directory, False, NULL, &sbuf);
3613 if (!NT_STATUS_IS_OK(status)) {
3614 END_PROFILE(SMBmkdir);
3615 return ERROR_NT(status);
3618 status = create_directory(conn, directory);
3620 DEBUG(5, ("create_directory returned %s\n", nt_errstr(status)));
3622 if (!NT_STATUS_IS_OK(status)) {
3624 if (!use_nt_status()
3625 && NT_STATUS_EQUAL(status,
3626 NT_STATUS_OBJECT_NAME_COLLISION)) {
3628 * Yes, in the DOS error code case we get a
3629 * ERRDOS:ERRnoaccess here. See BASE-SAMBA3ERROR
3630 * samba4 torture test.
3632 status = NT_STATUS_DOS(ERRDOS, ERRnoaccess);
3635 END_PROFILE(SMBmkdir);
3636 return ERROR_NT(status);
3639 outsize = set_message(outbuf,0,0,False);
3641 DEBUG( 3, ( "mkdir %s ret=%d\n", directory, outsize ) );
3643 END_PROFILE(SMBmkdir);
3647 /****************************************************************************
3648 Static function used by reply_rmdir to delete an entire directory
3649 tree recursively. Return True on ok, False on fail.
3650 ****************************************************************************/
3652 static BOOL recursive_rmdir(connection_struct *conn, char *directory)
3654 const char *dname = NULL;
3657 struct smb_Dir *dir_hnd = OpenDir(conn, directory, NULL, 0);
3662 while((dname = ReadDirName(dir_hnd, &offset))) {
3666 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
3669 if (!is_visible_file(conn, directory, dname, &st, False))
3672 /* Construct the full name. */
3673 if(strlen(directory) + strlen(dname) + 1 >= sizeof(fullname)) {
3679 pstrcpy(fullname, directory);
3680 pstrcat(fullname, "/");
3681 pstrcat(fullname, dname);
3683 if(SMB_VFS_LSTAT(conn,fullname, &st) != 0) {
3688 if(st.st_mode & S_IFDIR) {
3689 if(!recursive_rmdir(conn, fullname)) {
3693 if(SMB_VFS_RMDIR(conn,fullname) != 0) {
3697 } else if(SMB_VFS_UNLINK(conn,fullname) != 0) {
3706 /****************************************************************************
3707 The internals of the rmdir code - called elsewhere.
3708 ****************************************************************************/
3710 BOOL rmdir_internals(connection_struct *conn, const char *directory)
3715 ret = SMB_VFS_RMDIR(conn,directory);
3717 notify_fname(conn, NOTIFY_ACTION_REMOVED,
3718 FILE_NOTIFY_CHANGE_DIR_NAME,
3723 if(((errno == ENOTEMPTY)||(errno == EEXIST)) && lp_veto_files(SNUM(conn))) {
3725 * Check to see if the only thing in this directory are
3726 * vetoed files/directories. If so then delete them and
3727 * retry. If we fail to delete any of them (and we *don't*
3728 * do a recursive delete) then fail the rmdir.
3732 struct smb_Dir *dir_hnd = OpenDir(conn, directory, NULL, 0);
3734 if(dir_hnd == NULL) {
3739 while ((dname = ReadDirName(dir_hnd,&dirpos))) {
3740 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
3742 if (!is_visible_file(conn, directory, dname, &st, False))
3744 if(!IS_VETO_PATH(conn, dname)) {
3751 /* We only have veto files/directories. Recursive delete. */
3753 RewindDir(dir_hnd,&dirpos);
3754 while ((dname = ReadDirName(dir_hnd,&dirpos))) {
3757 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
3759 if (!is_visible_file(conn, directory, dname, &st, False))
3762 /* Construct the full name. */
3763 if(strlen(directory) + strlen(dname) + 1 >= sizeof(fullname)) {
3768 pstrcpy(fullname, directory);
3769 pstrcat(fullname, "/");
3770 pstrcat(fullname, dname);
3772 if(SMB_VFS_LSTAT(conn,fullname, &st) != 0)
3774 if(st.st_mode & S_IFDIR) {
3775 if(lp_recursive_veto_delete(SNUM(conn))) {
3776 if(!recursive_rmdir(conn, fullname))
3779 if(SMB_VFS_RMDIR(conn,fullname) != 0)
3781 } else if(SMB_VFS_UNLINK(conn,fullname) != 0)
3785 /* Retry the rmdir */
3786 ret = SMB_VFS_RMDIR(conn,directory);
3792 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
3793 "%s\n", directory,strerror(errno)));
3797 notify_fname(conn, NOTIFY_ACTION_REMOVED,
3798 FILE_NOTIFY_CHANGE_DIR_NAME,
3804 /****************************************************************************
3806 ****************************************************************************/
3808 int reply_rmdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3812 SMB_STRUCT_STAT sbuf;
3814 START_PROFILE(SMBrmdir);
3816 srvstr_get_path(inbuf, directory, smb_buf(inbuf) + 1, sizeof(directory), 0, STR_TERMINATE, &status);
3817 if (!NT_STATUS_IS_OK(status)) {
3818 END_PROFILE(SMBrmdir);
3819 return ERROR_NT(status);
3822 RESOLVE_DFSPATH(directory, conn, inbuf, outbuf)
3824 status = unix_convert(conn, directory, False, NULL, &sbuf);
3825 if (!NT_STATUS_IS_OK(status)) {
3826 END_PROFILE(SMBrmdir);
3827 return ERROR_NT(status);
3830 status = check_name(conn, directory);
3831 if (!NT_STATUS_IS_OK(status)) {
3832 END_PROFILE(SMBrmdir);
3833 return ERROR_NT(status);
3836 dptr_closepath(directory,SVAL(inbuf,smb_pid));
3837 if (!rmdir_internals(conn, directory)) {
3838 END_PROFILE(SMBrmdir);
3839 return UNIXERROR(ERRDOS, ERRbadpath);
3842 outsize = set_message(outbuf,0,0,False);
3844 DEBUG( 3, ( "rmdir %s\n", directory ) );
3846 END_PROFILE(SMBrmdir);
3850 /*******************************************************************
3851 Resolve wildcards in a filename rename.
3852 Note that name is in UNIX charset and thus potentially can be more
3853 than fstring buffer (255 bytes) especially in default UTF-8 case.
3854 Therefore, we use pstring inside and all calls should ensure that
3855 name2 is at least pstring-long (they do already)
3856 ********************************************************************/
3858 static BOOL resolve_wildcards(const char *name1, char *name2)
3860 pstring root1,root2;
3862 char *p,*p2, *pname1, *pname2;
3863 int available_space, actual_space;
3866 pname1 = strrchr_m(name1,'/');
3867 pname2 = strrchr_m(name2,'/');
3869 if (!pname1 || !pname2)
3872 pstrcpy(root1,pname1);
3873 pstrcpy(root2,pname2);
3874 p = strrchr_m(root1,'.');
3881 p = strrchr_m(root2,'.');
3895 } else if (*p2 == '*') {
3911 } else if (*p2 == '*') {
3921 available_space = sizeof(pstring) - PTR_DIFF(pname2, name2);
3924 actual_space = snprintf(pname2, available_space - 1, "%s.%s", root2, ext2);
3925 if (actual_space >= available_space - 1) {
3926 DEBUG(1,("resolve_wildcards: can't fit resolved name into specified buffer (overrun by %d bytes)\n",
3927 actual_space - available_space));
3930 pstrcpy_base(pname2, root2, name2);
3936 /****************************************************************************
3937 Ensure open files have their names updated. Updated to notify other smbd's
3939 ****************************************************************************/
3941 static void rename_open_files(connection_struct *conn, struct share_mode_lock *lck,
3942 SMB_DEV_T dev, SMB_INO_T inode, const char *newname)
3945 BOOL did_rename = False;
3947 for(fsp = file_find_di_first(dev, inode); fsp; fsp = file_find_di_next(fsp)) {
3948 /* fsp_name is a relative path under the fsp. To change this for other
3949 sharepaths we need to manipulate relative paths. */
3950 /* TODO - create the absolute path and manipulate the newname
3951 relative to the sharepath. */
3952 if (fsp->conn != conn) {
3955 DEBUG(10,("rename_open_files: renaming file fnum %d (dev = %x, inode = %.0f) from %s -> %s\n",
3956 fsp->fnum, (unsigned int)fsp->dev, (double)fsp->inode,
3957 fsp->fsp_name, newname ));
3958 string_set(&fsp->fsp_name, newname);
3963 DEBUG(10,("rename_open_files: no open files on dev %x, inode %.0f for %s\n",
3964 (unsigned int)dev, (double)inode, newname ));
3967 /* Send messages to all smbd's (not ourself) that the name has changed. */
3968 rename_share_filename(lck, conn->connectpath, newname);
3971 /****************************************************************************
3972 We need to check if the source path is a parent directory of the destination
3973 (ie. a rename of /foo/bar/baz -> /foo/bar/baz/bibble/bobble. If so we must
3974 refuse the rename with a sharing violation. Under UNIX the above call can
3975 *succeed* if /foo/bar/baz is a symlink to another area in the share. We
3976 probably need to check that the client is a Windows one before disallowing
3977 this as a UNIX client (one with UNIX extensions) can know the source is a
3978 symlink and make this decision intelligently. Found by an excellent bug
3979 report from <AndyLiebman@aol.com>.
3980 ****************************************************************************/
3982 static BOOL rename_path_prefix_equal(const char *src, const char *dest)
3984 const char *psrc = src;
3985 const char *pdst = dest;
3988 if (psrc[0] == '.' && psrc[1] == '/') {
3991 if (pdst[0] == '.' && pdst[1] == '/') {
3994 if ((slen = strlen(psrc)) > strlen(pdst)) {
3997 return ((memcmp(psrc, pdst, slen) == 0) && pdst[slen] == '/');
4000 /****************************************************************************
4001 Rename an open file - given an fsp.
4002 ****************************************************************************/
4004 NTSTATUS rename_internals_fsp(connection_struct *conn, files_struct *fsp, pstring newname, uint32 attrs, BOOL replace_if_exists)
4006 SMB_STRUCT_STAT sbuf;
4007 pstring newname_last_component;
4008 NTSTATUS status = NT_STATUS_OK;
4010 struct share_mode_lock *lck = NULL;
4014 status = unix_convert(conn, newname, False, newname_last_component, &sbuf);
4015 if (!NT_STATUS_IS_OK(status)) {
4019 /* Ensure newname contains a '/' */
4020 if(strrchr_m(newname,'/') == 0) {
4023 pstrcpy(tmpstr, "./");
4024 pstrcat(tmpstr, newname);
4025 pstrcpy(newname, tmpstr);
4029 * Check for special case with case preserving and not
4030 * case sensitive. If the old last component differs from the original
4031 * last component only by case, then we should allow
4032 * the rename (user is trying to change the case of the
4036 if((conn->case_sensitive == False) && (conn->case_preserve == True) &&
4037 strequal(newname, fsp->fsp_name)) {
4039 pstring newname_modified_last_component;
4042 * Get the last component of the modified name.
4043 * Note that we guarantee that newname contains a '/'
4046 p = strrchr_m(newname,'/');
4047 pstrcpy(newname_modified_last_component,p+1);
4049 if(strcsequal(newname_modified_last_component,
4050 newname_last_component) == False) {
4052 * Replace the modified last component with
4055 pstrcpy(p+1, newname_last_component);
4060 * If the src and dest names are identical - including case,
4061 * don't do the rename, just return success.
4064 if (strcsequal(fsp->fsp_name, newname)) {
4065 DEBUG(3,("rename_internals_fsp: identical names in rename %s - returning success\n",
4067 return NT_STATUS_OK;
4070 dest_exists = vfs_object_exist(conn,newname,NULL);
4072 if(!replace_if_exists && dest_exists) {
4073 DEBUG(3,("rename_internals_fsp: dest exists doing rename %s -> %s\n",
4074 fsp->fsp_name,newname));
4075 return NT_STATUS_OBJECT_NAME_COLLISION;
4078 status = can_rename(conn,newname,attrs,&sbuf);
4080 if (dest_exists && !NT_STATUS_IS_OK(status)) {
4081 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4082 nt_errstr(status), fsp->fsp_name,newname));
4083 if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION))
4084 status = NT_STATUS_ACCESS_DENIED;
4088 if (rename_path_prefix_equal(fsp->fsp_name, newname)) {
4089 return NT_STATUS_ACCESS_DENIED;
4092 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
4094 if(SMB_VFS_RENAME(conn,fsp->fsp_name, newname) == 0) {
4095 DEBUG(3,("rename_internals_fsp: succeeded doing rename on %s -> %s\n",
4096 fsp->fsp_name,newname));
4097 rename_open_files(conn, lck, fsp->dev, fsp->inode, newname);
4099 return NT_STATUS_OK;
4104 if (errno == ENOTDIR || errno == EISDIR) {
4105 status = NT_STATUS_OBJECT_NAME_COLLISION;
4107 status = map_nt_error_from_unix(errno);
4110 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
4111 nt_errstr(status), fsp->fsp_name,newname));
4117 * Do the notify calls from a rename
4120 static void notify_rename(connection_struct *conn, BOOL is_dir,
4121 const char *oldpath, const char *newpath)
4123 char *olddir, *newdir;
4124 const char *oldname, *newname;
4127 mask = is_dir ? FILE_NOTIFY_CHANGE_DIR_NAME
4128 : FILE_NOTIFY_CHANGE_FILE_NAME;
4130 if (!parent_dirname_talloc(NULL, oldpath, &olddir, &oldname)
4131 || !parent_dirname_talloc(NULL, newpath, &newdir, &newname)) {
4132 TALLOC_FREE(olddir);
4136 if (strcmp(olddir, newdir) == 0) {
4137 notify_fname(conn, NOTIFY_ACTION_OLD_NAME, mask, oldpath);
4138 notify_fname(conn, NOTIFY_ACTION_NEW_NAME, mask, newpath);
4141 notify_fname(conn, NOTIFY_ACTION_REMOVED, mask, oldpath);
4142 notify_fname(conn, NOTIFY_ACTION_ADDED, mask, newpath);
4144 TALLOC_FREE(olddir);
4145 TALLOC_FREE(newdir);
4147 /* this is a strange one. w2k3 gives an additional event for
4148 CHANGE_ATTRIBUTES and CHANGE_CREATION on the new file when renaming
4149 files, but not directories */
4151 notify_fname(conn, NOTIFY_ACTION_MODIFIED,
4152 FILE_NOTIFY_CHANGE_ATTRIBUTES
4153 |FILE_NOTIFY_CHANGE_CREATION,
4158 /****************************************************************************
4159 The guts of the rename command, split out so it may be called by the NT SMB
4161 ****************************************************************************/
4163 NTSTATUS rename_internals(connection_struct *conn, pstring name,
4164 pstring newname, uint32 attrs,
4165 BOOL replace_if_exists, BOOL has_wild)
4169 pstring last_component_src;
4170 pstring last_component_dest;
4173 NTSTATUS status = NT_STATUS_OK;
4174 SMB_STRUCT_STAT sbuf1, sbuf2;
4175 struct share_mode_lock *lck = NULL;
4177 *directory = *mask = 0;
4182 status = unix_convert(conn, name, has_wild, last_component_src,
4184 if (!NT_STATUS_IS_OK(status)) {
4188 status = unix_convert(conn, newname, True, last_component_dest,
4190 if (!NT_STATUS_IS_OK(status)) {
4195 * Split the old name into directory and last component
4196 * strings. Note that unix_convert may have stripped off a
4197 * leading ./ from both name and newname if the rename is
4198 * at the root of the share. We need to make sure either both
4199 * name and newname contain a / character or neither of them do
4200 * as this is checked in resolve_wildcards().
4203 p = strrchr_m(name,'/');
4205 pstrcpy(directory,".");
4209 pstrcpy(directory,name);
4211 *p = '/'; /* Replace needed for exceptional test below. */
4215 * We should only check the mangled cache
4216 * here if unix_convert failed. This means
4217 * that the path in 'mask' doesn't exist
4218 * on the file system and so we need to look
4219 * for a possible mangle. This patch from
4220 * Tine Smukavec <valentin.smukavec@hermes.si>.
4223 if (!VALID_STAT(sbuf1) && mangle_is_mangled(mask, conn->params))
4224 mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
4228 * No wildcards - just process the one file.
4230 BOOL is_short_name = mangle_is_8_3(name, True, conn->params);
4232 /* Add a terminating '/' to the directory name. */
4233 pstrcat(directory,"/");
4234 pstrcat(directory,mask);
4236 /* Ensure newname contains a '/' also */
4237 if(strrchr_m(newname,'/') == 0) {
4240 pstrcpy(tmpstr, "./");
4241 pstrcat(tmpstr, newname);
4242 pstrcpy(newname, tmpstr);
4245 DEBUG(3, ("rename_internals: case_sensitive = %d, "
4246 "case_preserve = %d, short case preserve = %d, "
4247 "directory = %s, newname = %s, "
4248 "last_component_dest = %s, is_8_3 = %d\n",
4249 conn->case_sensitive, conn->case_preserve,
4250 conn->short_case_preserve, directory,
4251 newname, last_component_dest, is_short_name));
4254 * Check for special case with case preserving and not
4255 * case sensitive, if directory and newname are identical,
4256 * and the old last component differs from the original
4257 * last component only by case, then we should allow
4258 * the rename (user is trying to change the case of the
4261 if((conn->case_sensitive == False) &&
4262 (((conn->case_preserve == True) &&
4263 (is_short_name == False)) ||
4264 ((conn->short_case_preserve == True) &&
4265 (is_short_name == True))) &&
4266 strcsequal(directory, newname)) {
4267 pstring modified_last_component;
4270 * Get the last component of the modified name.
4271 * Note that we guarantee that newname contains a '/'
4274 p = strrchr_m(newname,'/');
4275 pstrcpy(modified_last_component,p+1);
4277 if(strcsequal(modified_last_component,
4278 last_component_dest) == False) {
4280 * Replace the modified last component with
4283 pstrcpy(p+1, last_component_dest);
4287 resolve_wildcards(directory,newname);
4290 * The source object must exist.
4293 if (!vfs_object_exist(conn, directory, &sbuf1)) {
4294 DEBUG(3, ("rename_internals: source doesn't exist "
4295 "doing rename %s -> %s\n",
4296 directory,newname));
4298 if (errno == ENOTDIR || errno == EISDIR
4299 || errno == ENOENT) {
4301 * Must return different errors depending on
4302 * whether the parent directory existed or
4306 p = strrchr_m(directory, '/');
4308 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4310 if (vfs_object_exist(conn, directory, NULL))
4311 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4312 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
4314 status = map_nt_error_from_unix(errno);
4315 DEBUG(3, ("rename_internals: Error %s rename %s -> "
4316 "%s\n", nt_errstr(status), directory,
4322 status = can_rename(conn,directory,attrs,&sbuf1);
4324 if (!NT_STATUS_IS_OK(status)) {
4325 DEBUG(3,("rename_internals: Error %s rename %s -> "
4326 "%s\n", nt_errstr(status), directory,
4332 * If the src and dest names are identical - including case,
4333 * don't do the rename, just return success.
4336 if (strcsequal(directory, newname)) {
4337 rename_open_files(conn, NULL, sbuf1.st_dev,
4338 sbuf1.st_ino, newname);
4339 DEBUG(3, ("rename_internals: identical names in "
4340 "rename %s - returning success\n",
4342 return NT_STATUS_OK;
4345 if(!replace_if_exists && vfs_object_exist(conn,newname,NULL)) {
4346 DEBUG(3,("rename_internals: dest exists doing "
4347 "rename %s -> %s\n", directory, newname));
4348 return NT_STATUS_OBJECT_NAME_COLLISION;
4351 if (rename_path_prefix_equal(directory, newname)) {
4352 return NT_STATUS_SHARING_VIOLATION;
4355 lck = get_share_mode_lock(NULL, sbuf1.st_dev, sbuf1.st_ino,
4358 if(SMB_VFS_RENAME(conn,directory, newname) == 0) {
4359 DEBUG(3,("rename_internals: succeeded doing rename "
4360 "on %s -> %s\n", directory, newname));
4361 rename_open_files(conn, lck, sbuf1.st_dev,
4362 sbuf1.st_ino, newname);
4364 notify_rename(conn, S_ISDIR(sbuf1.st_mode),
4365 directory, newname);
4366 return NT_STATUS_OK;
4370 if (errno == ENOTDIR || errno == EISDIR) {
4371 status = NT_STATUS_OBJECT_NAME_COLLISION;
4373 status = map_nt_error_from_unix(errno);
4376 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4377 nt_errstr(status), directory,newname));
4382 * Wildcards - process each file that matches.
4384 struct smb_Dir *dir_hnd = NULL;
4389 if (strequal(mask,"????????.???"))
4392 status = check_name(conn, directory);
4393 if (!NT_STATUS_IS_OK(status)) {
4397 dir_hnd = OpenDir(conn, directory, mask, attrs);
4398 if (dir_hnd == NULL) {
4399 return map_nt_error_from_unix(errno);
4402 status = NT_STATUS_NO_SUCH_FILE;
4404 * Was status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4405 * - gentest fix. JRA
4408 while ((dname = ReadDirName(dir_hnd, &offset))) {
4410 BOOL sysdir_entry = False;
4412 pstrcpy(fname,dname);
4414 /* Quick check for "." and ".." */
4415 if (fname[0] == '.') {
4417 || (fname[1] == '.' && !fname[2])) {
4419 sysdir_entry = True;
4426 if (!is_visible_file(conn, directory, dname, &sbuf1,
4431 if(!mask_match(fname, mask, conn->case_sensitive)) {
4436 status = NT_STATUS_OBJECT_NAME_INVALID;
4440 status = NT_STATUS_ACCESS_DENIED;
4441 slprintf(fname, sizeof(fname)-1, "%s/%s", directory,
4443 if (!vfs_object_exist(conn, fname, &sbuf1)) {
4444 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4445 DEBUG(6, ("rename %s failed. Error %s\n",
4446 fname, nt_errstr(status)));
4449 status = can_rename(conn,fname,attrs,&sbuf1);
4450 if (!NT_STATUS_IS_OK(status)) {
4451 DEBUG(6, ("rename %s refused\n", fname));
4454 pstrcpy(destname,newname);
4456 if (!resolve_wildcards(fname,destname)) {
4457 DEBUG(6, ("resolve_wildcards %s %s failed\n",
4462 if (strcsequal(fname,destname)) {
4463 rename_open_files(conn, NULL, sbuf1.st_dev,
4464 sbuf1.st_ino, newname);
4465 DEBUG(3,("rename_internals: identical names "
4466 "in wildcard rename %s - success\n",
4469 status = NT_STATUS_OK;
4473 if (!replace_if_exists
4474 && vfs_file_exist(conn,destname, NULL)) {
4475 DEBUG(6,("file_exist %s\n", destname));
4476 status = NT_STATUS_OBJECT_NAME_COLLISION;
4480 if (rename_path_prefix_equal(fname, destname)) {
4481 return NT_STATUS_SHARING_VIOLATION;
4484 lck = get_share_mode_lock(NULL, sbuf1.st_dev,
4485 sbuf1.st_ino, NULL, NULL);
4487 if (!SMB_VFS_RENAME(conn,fname,destname)) {
4488 rename_open_files(conn, lck, sbuf1.st_dev,
4489 sbuf1.st_ino, newname);
4491 status = NT_STATUS_OK;
4494 DEBUG(3,("rename_internals: doing rename on %s -> "
4495 "%s\n",fname,destname));
4501 /* Don't think needed any more - JRA. */
4502 if (!NT_STATUS_EQUAL(error,NT_STATUS_NO_SUCH_FILE)) {
4503 if (!rcdest && bad_path_dest) {
4504 if (ms_has_wild(last_component_dest))
4505 return NT_STATUS_OBJECT_NAME_INVALID;
4506 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
4511 if (count == 0 && NT_STATUS_IS_OK(status)) {
4512 status = map_nt_error_from_unix(errno);
4518 /****************************************************************************
4520 ****************************************************************************/
4522 int reply_mv(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
4529 uint32 attrs = SVAL(inbuf,smb_vwv0);
4531 BOOL path1_contains_wcard = False;
4532 BOOL path2_contains_wcard = False;
4534 START_PROFILE(SMBmv);
4536 p = smb_buf(inbuf) + 1;
4537 p += srvstr_get_path_wcard(inbuf, name, p, sizeof(name), 0, STR_TERMINATE, &status, &path1_contains_wcard);
4538 if (!NT_STATUS_IS_OK(status)) {
4540 return ERROR_NT(status);
4543 p += srvstr_get_path_wcard(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status, &path2_contains_wcard);
4544 if (!NT_STATUS_IS_OK(status)) {
4546 return ERROR_NT(status);
4549 RESOLVE_DFSPATH_WCARD(name, conn, inbuf, outbuf);
4550 RESOLVE_DFSPATH_WCARD(newname, conn, inbuf, outbuf);
4552 DEBUG(3,("reply_mv : %s -> %s\n",name,newname));
4554 status = rename_internals(conn, name, newname, attrs, False, path1_contains_wcard);
4555 if (!NT_STATUS_IS_OK(status)) {
4557 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
4558 /* We have re-scheduled this call. */
4561 return ERROR_NT(status);
4564 outsize = set_message(outbuf,0,0,False);
4570 /*******************************************************************
4571 Copy a file as part of a reply_copy.
4572 ******************************************************************/
4575 * TODO: check error codes on all callers
4578 NTSTATUS copy_file(char *src, char *dest1,connection_struct *conn, int ofun,
4579 int count, BOOL target_is_directory)
4581 SMB_STRUCT_STAT src_sbuf, sbuf2;
4583 files_struct *fsp1,*fsp2;
4586 uint32 new_create_disposition;
4590 pstrcpy(dest,dest1);
4591 if (target_is_directory) {
4592 char *p = strrchr_m(src,'/');
4602 if (!vfs_file_exist(conn,src,&src_sbuf)) {
4603 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4606 if (!target_is_directory && count) {
4607 new_create_disposition = FILE_OPEN;
4609 if (!map_open_params_to_ntcreate(dest1,0,ofun,
4610 NULL, NULL, &new_create_disposition, NULL)) {
4611 return NT_STATUS_INVALID_PARAMETER;
4615 status = open_file_ntcreate(conn,src,&src_sbuf,
4617 FILE_SHARE_READ|FILE_SHARE_WRITE,
4620 FILE_ATTRIBUTE_NORMAL,
4624 if (!NT_STATUS_IS_OK(status)) {
4628 dosattrs = dos_mode(conn, src, &src_sbuf);
4629 if (SMB_VFS_STAT(conn,dest,&sbuf2) == -1) {
4630 ZERO_STRUCTP(&sbuf2);
4633 status = open_file_ntcreate(conn,dest,&sbuf2,
4635 FILE_SHARE_READ|FILE_SHARE_WRITE,
4636 new_create_disposition,
4642 if (!NT_STATUS_IS_OK(status)) {
4643 close_file(fsp1,ERROR_CLOSE);
4647 if ((ofun&3) == 1) {
4648 if(SMB_VFS_LSEEK(fsp2,fsp2->fh->fd,0,SEEK_END) == -1) {
4649 DEBUG(0,("copy_file: error - vfs lseek returned error %s\n", strerror(errno) ));
4651 * Stop the copy from occurring.
4654 src_sbuf.st_size = 0;
4658 if (src_sbuf.st_size) {
4659 ret = vfs_transfer_file(fsp1, fsp2, src_sbuf.st_size);
4662 close_file(fsp1,NORMAL_CLOSE);
4664 /* Ensure the modtime is set correctly on the destination file. */
4665 fsp_set_pending_modtime( fsp2, src_sbuf.st_mtime);
4668 * As we are opening fsp1 read-only we only expect
4669 * an error on close on fsp2 if we are out of space.
4670 * Thus we don't look at the error return from the
4673 close_err = close_file(fsp2,NORMAL_CLOSE);
4675 if (close_err != 0) {
4676 return map_nt_error_from_unix(close_err);
4679 if (ret != (SMB_OFF_T)src_sbuf.st_size) {
4680 return NT_STATUS_DISK_FULL;
4683 return NT_STATUS_OK;
4686 /****************************************************************************
4687 Reply to a file copy.
4688 ****************************************************************************/
4690 int reply_copy(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
4695 pstring mask,newname;
4698 int error = ERRnoaccess;
4702 int tid2 = SVAL(inbuf,smb_vwv0);
4703 int ofun = SVAL(inbuf,smb_vwv1);
4704 int flags = SVAL(inbuf,smb_vwv2);
4705 BOOL target_is_directory=False;
4706 BOOL path_contains_wcard1 = False;
4707 BOOL path_contains_wcard2 = False;
4708 SMB_STRUCT_STAT sbuf1, sbuf2;
4710 START_PROFILE(SMBcopy);
4712 *directory = *mask = 0;
4715 p += srvstr_get_path_wcard(inbuf, name, p, sizeof(name), 0, STR_TERMINATE, &status, &path_contains_wcard1);
4716 if (!NT_STATUS_IS_OK(status)) {
4717 END_PROFILE(SMBcopy);
4718 return ERROR_NT(status);
4720 p += srvstr_get_path_wcard(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status, &path_contains_wcard2);
4721 if (!NT_STATUS_IS_OK(status)) {
4722 END_PROFILE(SMBcopy);
4723 return ERROR_NT(status);
4726 DEBUG(3,("reply_copy : %s -> %s\n",name,newname));
4728 if (tid2 != conn->cnum) {
4729 /* can't currently handle inter share copies XXXX */
4730 DEBUG(3,("Rejecting inter-share copy\n"));
4731 END_PROFILE(SMBcopy);
4732 return ERROR_DOS(ERRSRV,ERRinvdevice);
4735 RESOLVE_DFSPATH_WCARD(name, conn, inbuf, outbuf);
4736 RESOLVE_DFSPATH_WCARD(newname, conn, inbuf, outbuf);
4738 status = unix_convert(conn, name, path_contains_wcard1, NULL, &sbuf1);
4739 if (!NT_STATUS_IS_OK(status)) {
4740 END_PROFILE(SMBcopy);
4741 return ERROR_NT(status);
4744 status = unix_convert(conn, newname, path_contains_wcard2, NULL, &sbuf2);
4745 if (!NT_STATUS_IS_OK(status)) {
4746 END_PROFILE(SMBcopy);
4747 return ERROR_NT(status);
4750 target_is_directory = VALID_STAT_OF_DIR(sbuf2);
4752 if ((flags&1) && target_is_directory) {
4753 END_PROFILE(SMBcopy);
4754 return ERROR_DOS(ERRDOS,ERRbadfile);
4757 if ((flags&2) && !target_is_directory) {
4758 END_PROFILE(SMBcopy);
4759 return ERROR_DOS(ERRDOS,ERRbadpath);
4762 if ((flags&(1<<5)) && VALID_STAT_OF_DIR(sbuf1)) {
4763 /* wants a tree copy! XXXX */
4764 DEBUG(3,("Rejecting tree copy\n"));
4765 END_PROFILE(SMBcopy);
4766 return ERROR_DOS(ERRSRV,ERRerror);
4769 p = strrchr_m(name,'/');
4771 pstrcpy(directory,"./");
4775 pstrcpy(directory,name);
4780 * We should only check the mangled cache
4781 * here if unix_convert failed. This means
4782 * that the path in 'mask' doesn't exist
4783 * on the file system and so we need to look
4784 * for a possible mangle. This patch from
4785 * Tine Smukavec <valentin.smukavec@hermes.si>.
4788 if (!VALID_STAT(sbuf1) && mangle_is_mangled(mask, conn->params))
4789 mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
4791 has_wild = path_contains_wcard1;
4794 pstrcat(directory,"/");
4795 pstrcat(directory,mask);
4796 if (resolve_wildcards(directory,newname)
4797 && NT_STATUS_IS_OK(status = copy_file(
4798 directory,newname,conn,ofun,
4799 count,target_is_directory)))
4801 if(!count && !NT_STATUS_IS_OK(status)) {
4802 END_PROFILE(SMBcopy);
4803 return ERROR_NT(status);
4806 exists = vfs_file_exist(conn,directory,NULL);
4809 struct smb_Dir *dir_hnd = NULL;
4814 if (strequal(mask,"????????.???"))
4817 status = check_name(conn, directory);
4818 if (!NT_STATUS_IS_OK(status)) {
4819 return ERROR_NT(status);
4822 dir_hnd = OpenDir(conn, directory, mask, 0);
4823 if (dir_hnd == NULL) {
4824 status = map_nt_error_from_unix(errno);
4825 return ERROR_NT(status);
4830 while ((dname = ReadDirName(dir_hnd, &offset))) {
4832 pstrcpy(fname,dname);
4834 if (!is_visible_file(conn, directory, dname, &sbuf1, False)) {
4838 if(!mask_match(fname, mask, conn->case_sensitive)) {
4842 error = ERRnoaccess;
4843 slprintf(fname,sizeof(fname)-1, "%s/%s",directory,dname);
4844 pstrcpy(destname,newname);
4845 if (resolve_wildcards(fname,destname) &&
4846 NT_STATUS_IS_OK(status = copy_file(
4847 fname,destname,conn,ofun,
4848 count,target_is_directory))) {
4851 DEBUG(3,("reply_copy : doing copy on %s -> %s\n",fname,destname));
4858 /* Error on close... */
4860 END_PROFILE(SMBcopy);
4861 return(UNIXERROR(ERRHRD,ERRgeneral));
4864 END_PROFILE(SMBcopy);
4865 return ERROR_DOS(ERRDOS,error);
4868 outsize = set_message(outbuf,1,0,True);
4869 SSVAL(outbuf,smb_vwv0,count);
4871 END_PROFILE(SMBcopy);
4875 /****************************************************************************
4877 ****************************************************************************/
4879 int reply_setdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
4886 START_PROFILE(pathworks_setdir);
4889 if (!CAN_SETDIR(snum)) {
4890 END_PROFILE(pathworks_setdir);
4891 return ERROR_DOS(ERRDOS,ERRnoaccess);
4894 srvstr_get_path(inbuf, newdir, smb_buf(inbuf) + 1, sizeof(newdir), 0, STR_TERMINATE, &status);
4895 if (!NT_STATUS_IS_OK(status)) {
4896 END_PROFILE(pathworks_setdir);
4897 return ERROR_NT(status);
4900 RESOLVE_DFSPATH(newdir, conn, inbuf, outbuf);
4902 if (strlen(newdir) != 0) {
4903 if (!vfs_directory_exist(conn,newdir,NULL)) {
4904 END_PROFILE(pathworks_setdir);
4905 return ERROR_DOS(ERRDOS,ERRbadpath);
4907 set_conn_connectpath(conn,newdir);
4910 outsize = set_message(outbuf,0,0,False);
4911 SCVAL(outbuf,smb_reh,CVAL(inbuf,smb_reh));
4913 DEBUG(3,("setdir %s\n", newdir));
4915 END_PROFILE(pathworks_setdir);
4920 #define DBGC_CLASS DBGC_LOCKING
4922 /****************************************************************************
4923 Get a lock pid, dealing with large count requests.
4924 ****************************************************************************/
4926 uint32 get_lock_pid( char *data, int data_offset, BOOL large_file_format)
4928 if(!large_file_format)
4929 return (uint32)SVAL(data,SMB_LPID_OFFSET(data_offset));
4931 return (uint32)SVAL(data,SMB_LARGE_LPID_OFFSET(data_offset));
4934 /****************************************************************************
4935 Get a lock count, dealing with large count requests.
4936 ****************************************************************************/
4938 SMB_BIG_UINT get_lock_count( char *data, int data_offset, BOOL large_file_format)
4940 SMB_BIG_UINT count = 0;
4942 if(!large_file_format) {
4943 count = (SMB_BIG_UINT)IVAL(data,SMB_LKLEN_OFFSET(data_offset));
4946 #if defined(HAVE_LONGLONG)
4947 count = (((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset))) << 32) |
4948 ((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)));
4949 #else /* HAVE_LONGLONG */
4952 * NT4.x seems to be broken in that it sends large file (64 bit)
4953 * lockingX calls even if the CAP_LARGE_FILES was *not*
4954 * negotiated. For boxes without large unsigned ints truncate the
4955 * lock count by dropping the top 32 bits.
4958 if(IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)) != 0) {
4959 DEBUG(3,("get_lock_count: truncating lock count (high)0x%x (low)0x%x to just low count.\n",
4960 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)),
4961 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)) ));
4962 SIVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset),0);
4965 count = (SMB_BIG_UINT)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset));
4966 #endif /* HAVE_LONGLONG */
4972 #if !defined(HAVE_LONGLONG)
4973 /****************************************************************************
4974 Pathetically try and map a 64 bit lock offset into 31 bits. I hate Windows :-).
4975 ****************************************************************************/
4977 static uint32 map_lock_offset(uint32 high, uint32 low)
4981 uint32 highcopy = high;
4984 * Try and find out how many significant bits there are in high.
4987 for(i = 0; highcopy; i++)
4991 * We use 31 bits not 32 here as POSIX
4992 * lock offsets may not be negative.
4995 mask = (~0) << (31 - i);
4998 return 0; /* Fail. */
5004 #endif /* !defined(HAVE_LONGLONG) */
5006 /****************************************************************************
5007 Get a lock offset, dealing with large offset requests.
5008 ****************************************************************************/
5010 SMB_BIG_UINT get_lock_offset( char *data, int data_offset, BOOL large_file_format, BOOL *err)
5012 SMB_BIG_UINT offset = 0;
5016 if(!large_file_format) {
5017 offset = (SMB_BIG_UINT)IVAL(data,SMB_LKOFF_OFFSET(data_offset));
5020 #if defined(HAVE_LONGLONG)
5021 offset = (((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset))) << 32) |
5022 ((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset)));
5023 #else /* HAVE_LONGLONG */
5026 * NT4.x seems to be broken in that it sends large file (64 bit)
5027 * lockingX calls even if the CAP_LARGE_FILES was *not*
5028 * negotiated. For boxes without large unsigned ints mangle the
5029 * lock offset by mapping the top 32 bits onto the lower 32.
5032 if(IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset)) != 0) {
5033 uint32 low = IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
5034 uint32 high = IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset));
5037 if((new_low = map_lock_offset(high, low)) == 0) {
5039 return (SMB_BIG_UINT)-1;
5042 DEBUG(3,("get_lock_offset: truncating lock offset (high)0x%x (low)0x%x to offset 0x%x.\n",
5043 (unsigned int)high, (unsigned int)low, (unsigned int)new_low ));
5044 SIVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset),0);
5045 SIVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset),new_low);
5048 offset = (SMB_BIG_UINT)IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
5049 #endif /* HAVE_LONGLONG */
5055 /****************************************************************************
5056 Reply to a lockingX request.
5057 ****************************************************************************/
5059 int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
5060 int length, int bufsize)
5062 files_struct *fsp = file_fsp(inbuf,smb_vwv2);
5063 unsigned char locktype = CVAL(inbuf,smb_vwv3);
5064 unsigned char oplocklevel = CVAL(inbuf,smb_vwv3+1);
5065 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
5066 uint16 num_locks = SVAL(inbuf,smb_vwv7);
5067 SMB_BIG_UINT count = 0, offset = 0;
5069 int32 lock_timeout = IVAL(inbuf,smb_vwv4);
5072 BOOL large_file_format =
5073 (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
5075 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5077 START_PROFILE(SMBlockingX);
5079 CHECK_FSP(fsp,conn);
5081 data = smb_buf(inbuf);
5083 if (locktype & LOCKING_ANDX_CHANGE_LOCKTYPE) {
5084 /* we don't support these - and CANCEL_LOCK makes w2k
5085 and XP reboot so I don't really want to be
5086 compatible! (tridge) */
5087 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks));
5090 /* Check if this is an oplock break on a file
5091 we have granted an oplock on.
5093 if ((locktype & LOCKING_ANDX_OPLOCK_RELEASE)) {
5094 /* Client can insist on breaking to none. */
5095 BOOL break_to_none = (oplocklevel == 0);
5098 DEBUG(5,("reply_lockingX: oplock break reply (%u) from client "
5099 "for fnum = %d\n", (unsigned int)oplocklevel,
5103 * Make sure we have granted an exclusive or batch oplock on
5107 if (fsp->oplock_type == 0) {
5109 /* The Samba4 nbench simulator doesn't understand
5110 the difference between break to level2 and break
5111 to none from level2 - it sends oplock break
5112 replies in both cases. Don't keep logging an error
5113 message here - just ignore it. JRA. */
5115 DEBUG(5,("reply_lockingX: Error : oplock break from "
5116 "client for fnum = %d (oplock=%d) and no "
5117 "oplock granted on this file (%s).\n",
5118 fsp->fnum, fsp->oplock_type, fsp->fsp_name));
5120 /* if this is a pure oplock break request then don't
5122 if (num_locks == 0 && num_ulocks == 0) {
5123 END_PROFILE(SMBlockingX);
5126 END_PROFILE(SMBlockingX);
5127 return ERROR_DOS(ERRDOS,ERRlock);
5131 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
5133 result = remove_oplock(fsp);
5135 result = downgrade_oplock(fsp);
5139 DEBUG(0, ("reply_lockingX: error in removing "
5140 "oplock on file %s\n", fsp->fsp_name));
5141 /* Hmmm. Is this panic justified? */
5142 smb_panic("internal tdb error");
5145 reply_to_oplock_break_requests(fsp);
5147 /* if this is a pure oplock break request then don't send a
5149 if (num_locks == 0 && num_ulocks == 0) {
5150 /* Sanity check - ensure a pure oplock break is not a
5152 if(CVAL(inbuf,smb_vwv0) != 0xff)
5153 DEBUG(0,("reply_lockingX: Error : pure oplock "
5154 "break is a chained %d request !\n",
5155 (unsigned int)CVAL(inbuf,smb_vwv0) ));
5156 END_PROFILE(SMBlockingX);
5162 * We do this check *after* we have checked this is not a oplock break
5163 * response message. JRA.
5166 release_level_2_oplocks_on_change(fsp);
5168 /* Data now points at the beginning of the list
5169 of smb_unlkrng structs */
5170 for(i = 0; i < (int)num_ulocks; i++) {
5171 lock_pid = get_lock_pid( data, i, large_file_format);
5172 count = get_lock_count( data, i, large_file_format);
5173 offset = get_lock_offset( data, i, large_file_format, &err);
5176 * There is no error code marked "stupid client bug".... :-).
5179 END_PROFILE(SMBlockingX);
5180 return ERROR_DOS(ERRDOS,ERRnoaccess);
5183 DEBUG(10,("reply_lockingX: unlock start=%.0f, len=%.0f for "
5184 "pid %u, file %s\n", (double)offset, (double)count,
5185 (unsigned int)lock_pid, fsp->fsp_name ));
5187 status = do_unlock(fsp,
5193 if (NT_STATUS_V(status)) {
5194 END_PROFILE(SMBlockingX);
5195 return ERROR_NT(status);
5199 /* Setup the timeout in seconds. */
5201 if (!lp_blocking_locks(SNUM(conn))) {
5205 /* Now do any requested locks */
5206 data += ((large_file_format ? 20 : 10)*num_ulocks);
5208 /* Data now points at the beginning of the list
5209 of smb_lkrng structs */
5211 for(i = 0; i < (int)num_locks; i++) {
5212 enum brl_type lock_type = ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
5213 READ_LOCK:WRITE_LOCK);
5214 lock_pid = get_lock_pid( data, i, large_file_format);
5215 count = get_lock_count( data, i, large_file_format);
5216 offset = get_lock_offset( data, i, large_file_format, &err);
5219 * There is no error code marked "stupid client bug".... :-).
5222 END_PROFILE(SMBlockingX);
5223 return ERROR_DOS(ERRDOS,ERRnoaccess);
5226 DEBUG(10,("reply_lockingX: lock start=%.0f, len=%.0f for pid "
5227 "%u, file %s timeout = %d\n", (double)offset,
5228 (double)count, (unsigned int)lock_pid,
5229 fsp->fsp_name, (int)lock_timeout ));
5231 if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
5232 if (lp_blocking_locks(SNUM(conn))) {
5234 /* Schedule a message to ourselves to
5235 remove the blocking lock record and
5236 return the right error. */
5238 if (!blocking_lock_cancel(fsp,
5244 NT_STATUS_FILE_LOCK_CONFLICT)) {
5245 END_PROFILE(SMBlockingX);
5246 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRcancelviolation));
5249 /* Remove a matching pending lock. */
5250 status = do_lock_cancel(fsp,
5256 BOOL blocking_lock = lock_timeout ? True : False;
5257 BOOL defer_lock = False;
5258 struct byte_range_lock *br_lck;
5260 br_lck = do_lock(fsp,
5269 if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
5270 /* Windows internal resolution for blocking locks seems
5271 to be about 200ms... Don't wait for less than that. JRA. */
5272 if (lock_timeout != -1 && lock_timeout < lp_lock_spin_time()) {
5273 lock_timeout = lp_lock_spin_time();
5278 /* This heuristic seems to match W2K3 very well. If a
5279 lock sent with timeout of zero would fail with NT_STATUS_FILE_LOCK_CONFLICT
5280 it pretends we asked for a timeout of between 150 - 300 milliseconds as
5281 far as I can tell. Replacement for do_lock_spin(). JRA. */
5283 if (br_lck && lp_blocking_locks(SNUM(conn)) && !blocking_lock &&
5284 NT_STATUS_EQUAL((status), NT_STATUS_FILE_LOCK_CONFLICT)) {
5286 lock_timeout = lp_lock_spin_time();
5289 if (br_lck && defer_lock) {
5291 * A blocking lock was requested. Package up
5292 * this smb into a queued request and push it
5293 * onto the blocking lock queue.
5295 if(push_blocking_lock_request(br_lck,
5305 TALLOC_FREE(br_lck);
5306 END_PROFILE(SMBlockingX);
5311 TALLOC_FREE(br_lck);
5314 if (NT_STATUS_V(status)) {
5315 END_PROFILE(SMBlockingX);
5316 return ERROR_NT(status);
5320 /* If any of the above locks failed, then we must unlock
5321 all of the previous locks (X/Open spec). */
5323 if (!(locktype & LOCKING_ANDX_CANCEL_LOCK) &&
5327 * Ensure we don't do a remove on the lock that just failed,
5328 * as under POSIX rules, if we have a lock already there, we
5329 * will delete it (and we shouldn't) .....
5331 for(i--; i >= 0; i--) {
5332 lock_pid = get_lock_pid( data, i, large_file_format);
5333 count = get_lock_count( data, i, large_file_format);
5334 offset = get_lock_offset( data, i, large_file_format,
5338 * There is no error code marked "stupid client
5342 END_PROFILE(SMBlockingX);
5343 return ERROR_DOS(ERRDOS,ERRnoaccess);
5352 END_PROFILE(SMBlockingX);
5353 return ERROR_NT(status);
5356 set_message(outbuf,2,0,True);
5358 DEBUG(3, ("lockingX fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
5359 fsp->fnum, (unsigned int)locktype, num_locks, num_ulocks));
5361 END_PROFILE(SMBlockingX);
5362 return chain_reply(inbuf,outbuf,length,bufsize);
5366 #define DBGC_CLASS DBGC_ALL
5368 /****************************************************************************
5369 Reply to a SMBreadbmpx (read block multiplex) request.
5370 ****************************************************************************/
5372 int reply_readbmpx(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
5383 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5384 START_PROFILE(SMBreadBmpx);
5386 /* this function doesn't seem to work - disable by default */
5387 if (!lp_readbmpx()) {
5388 END_PROFILE(SMBreadBmpx);
5389 return ERROR_DOS(ERRSRV,ERRuseSTD);
5392 outsize = set_message(outbuf,8,0,True);
5394 CHECK_FSP(fsp,conn);
5395 if (!CHECK_READ(fsp,inbuf)) {
5396 return(ERROR_DOS(ERRDOS,ERRbadaccess));
5399 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
5400 maxcount = SVAL(inbuf,smb_vwv3);
5402 data = smb_buf(outbuf);
5403 pad = ((long)data)%4;
5408 max_per_packet = bufsize-(outsize+pad);
5412 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
5413 END_PROFILE(SMBreadBmpx);
5414 return ERROR_DOS(ERRDOS,ERRlock);
5418 size_t N = MIN(max_per_packet,tcount-total_read);
5420 nread = read_file(fsp,data,startpos,N);
5425 if (nread < (ssize_t)N)
5426 tcount = total_read + nread;
5428 set_message(outbuf,8,nread+pad,False);
5429 SIVAL(outbuf,smb_vwv0,startpos);
5430 SSVAL(outbuf,smb_vwv2,tcount);
5431 SSVAL(outbuf,smb_vwv6,nread);
5432 SSVAL(outbuf,smb_vwv7,smb_offset(data,outbuf));
5435 if (!send_smb(smbd_server_fd(),outbuf))
5436 exit_server_cleanly("reply_readbmpx: send_smb failed.");
5438 total_read += nread;
5440 } while (total_read < (ssize_t)tcount);
5442 END_PROFILE(SMBreadBmpx);
5446 /****************************************************************************
5447 Reply to a SMBsetattrE.
5448 ****************************************************************************/
5450 int reply_setattrE(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
5452 struct utimbuf unix_times;
5454 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5455 START_PROFILE(SMBsetattrE);
5457 outsize = set_message(outbuf,0,0,False);
5459 if(!fsp || (fsp->conn != conn)) {
5460 END_PROFILE(SMBsetattrE);
5461 return ERROR_DOS(ERRDOS,ERRbadfid);
5465 * Convert the DOS times into unix times. Ignore create
5466 * time as UNIX can't set this.
5469 unix_times.actime = srv_make_unix_date2(inbuf+smb_vwv3);
5470 unix_times.modtime = srv_make_unix_date2(inbuf+smb_vwv5);
5473 * Patch from Ray Frush <frush@engr.colostate.edu>
5474 * Sometimes times are sent as zero - ignore them.
5477 if (null_mtime(unix_times.actime) && null_mtime(unix_times.modtime)) {
5478 /* Ignore request */
5479 if( DEBUGLVL( 3 ) ) {
5480 dbgtext( "reply_setattrE fnum=%d ", fsp->fnum);
5481 dbgtext( "ignoring zero request - not setting timestamps of 0\n" );
5483 END_PROFILE(SMBsetattrE);
5485 } else if (!null_mtime(unix_times.actime) && null_mtime(unix_times.modtime)) {
5486 /* set modify time = to access time if modify time was unset */
5487 unix_times.modtime = unix_times.actime;
5490 /* Set the date on this file */
5491 /* Should we set pending modtime here ? JRA */
5492 if(file_utime(conn, fsp->fsp_name, &unix_times)) {
5493 END_PROFILE(SMBsetattrE);
5494 return ERROR_DOS(ERRDOS,ERRnoaccess);
5497 DEBUG( 3, ( "reply_setattrE fnum=%d actime=%d modtime=%d\n",
5498 fsp->fnum, (int)unix_times.actime, (int)unix_times.modtime ) );
5500 END_PROFILE(SMBsetattrE);
5505 /* Back from the dead for OS/2..... JRA. */
5507 /****************************************************************************
5508 Reply to a SMBwritebmpx (write block multiplex primary) request.
5509 ****************************************************************************/
5511 int reply_writebmpx(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
5514 ssize_t nwritten = -1;
5521 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5522 START_PROFILE(SMBwriteBmpx);
5524 CHECK_FSP(fsp,conn);
5525 if (!CHECK_WRITE(fsp)) {
5526 return(ERROR_DOS(ERRDOS,ERRbadaccess));
5528 if (HAS_CACHED_ERROR(fsp)) {
5529 return(CACHED_ERROR(fsp));
5532 tcount = SVAL(inbuf,smb_vwv1);
5533 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
5534 write_through = BITSETW(inbuf+smb_vwv7,0);
5535 numtowrite = SVAL(inbuf,smb_vwv10);
5536 smb_doff = SVAL(inbuf,smb_vwv11);
5538 data = smb_base(inbuf) + smb_doff;
5540 /* If this fails we need to send an SMBwriteC response,
5541 not an SMBwritebmpx - set this up now so we don't forget */
5542 SCVAL(outbuf,smb_com,SMBwritec);
5544 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos,WRITE_LOCK)) {
5545 END_PROFILE(SMBwriteBmpx);
5546 return(ERROR_DOS(ERRDOS,ERRlock));
5549 nwritten = write_file(fsp,data,startpos,numtowrite);
5551 sync_file(conn, fsp, write_through);
5553 if(nwritten < (ssize_t)numtowrite) {
5554 END_PROFILE(SMBwriteBmpx);
5555 return(UNIXERROR(ERRHRD,ERRdiskfull));
5558 /* If the maximum to be written to this file
5559 is greater than what we just wrote then set
5560 up a secondary struct to be attached to this
5561 fd, we will use this to cache error messages etc. */
5563 if((ssize_t)tcount > nwritten) {
5564 write_bmpx_struct *wbms;
5565 if(fsp->wbmpx_ptr != NULL)
5566 wbms = fsp->wbmpx_ptr; /* Use an existing struct */
5568 wbms = SMB_MALLOC_P(write_bmpx_struct);
5570 DEBUG(0,("Out of memory in reply_readmpx\n"));
5571 END_PROFILE(SMBwriteBmpx);
5572 return(ERROR_DOS(ERRSRV,ERRnoresource));
5574 wbms->wr_mode = write_through;
5575 wbms->wr_discard = False; /* No errors yet */
5576 wbms->wr_total_written = nwritten;
5577 wbms->wr_errclass = 0;
5579 fsp->wbmpx_ptr = wbms;
5582 /* We are returning successfully, set the message type back to
5584 SCVAL(outbuf,smb_com,SMBwriteBmpx);
5586 outsize = set_message(outbuf,1,0,True);
5588 SSVALS(outbuf,smb_vwv0,-1); /* We don't support smb_remaining */
5590 DEBUG( 3, ( "writebmpx fnum=%d num=%d wrote=%d\n",
5591 fsp->fnum, (int)numtowrite, (int)nwritten ) );
5593 if (write_through && tcount==nwritten) {
5594 /* We need to send both a primary and a secondary response */
5595 smb_setlen(outbuf,outsize - 4);
5597 if (!send_smb(smbd_server_fd(),outbuf))
5598 exit_server_cleanly("reply_writebmpx: send_smb failed.");
5600 /* Now the secondary */
5601 outsize = set_message(outbuf,1,0,True);
5602 SCVAL(outbuf,smb_com,SMBwritec);
5603 SSVAL(outbuf,smb_vwv0,nwritten);
5606 END_PROFILE(SMBwriteBmpx);
5610 /****************************************************************************
5611 Reply to a SMBwritebs (write block multiplex secondary) request.
5612 ****************************************************************************/
5614 int reply_writebs(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
5617 ssize_t nwritten = -1;
5624 write_bmpx_struct *wbms;
5625 BOOL send_response = False;
5626 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5627 START_PROFILE(SMBwriteBs);
5629 CHECK_FSP(fsp,conn);
5630 if (!CHECK_WRITE(fsp)) {
5631 return(ERROR_DOS(ERRDOS,ERRbadaccess));
5634 tcount = SVAL(inbuf,smb_vwv1);
5635 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
5636 numtowrite = SVAL(inbuf,smb_vwv6);
5637 smb_doff = SVAL(inbuf,smb_vwv7);
5639 data = smb_base(inbuf) + smb_doff;
5641 /* We need to send an SMBwriteC response, not an SMBwritebs */
5642 SCVAL(outbuf,smb_com,SMBwritec);
5644 /* This fd should have an auxiliary struct attached,
5645 check that it does */
5646 wbms = fsp->wbmpx_ptr;
5648 END_PROFILE(SMBwriteBs);
5652 /* If write through is set we can return errors, else we must cache them */
5653 write_through = wbms->wr_mode;
5655 /* Check for an earlier error */
5656 if(wbms->wr_discard) {
5657 END_PROFILE(SMBwriteBs);
5658 return -1; /* Just discard the packet */
5661 nwritten = write_file(fsp,data,startpos,numtowrite);
5663 sync_file(conn, fsp, write_through);
5665 if (nwritten < (ssize_t)numtowrite) {
5667 /* We are returning an error - we can delete the aux struct */
5670 fsp->wbmpx_ptr = NULL;
5671 END_PROFILE(SMBwriteBs);
5672 return(ERROR_DOS(ERRHRD,ERRdiskfull));
5674 wbms->wr_errclass = ERRHRD;
5675 wbms->wr_error = ERRdiskfull;
5676 wbms->wr_status = NT_STATUS_DISK_FULL;
5677 wbms->wr_discard = True;
5678 END_PROFILE(SMBwriteBs);
5682 /* Increment the total written, if this matches tcount
5683 we can discard the auxiliary struct (hurrah !) and return a writeC */
5684 wbms->wr_total_written += nwritten;
5685 if(wbms->wr_total_written >= tcount) {
5686 if (write_through) {
5687 outsize = set_message(outbuf,1,0,True);
5688 SSVAL(outbuf,smb_vwv0,wbms->wr_total_written);
5689 send_response = True;
5693 fsp->wbmpx_ptr = NULL;
5697 END_PROFILE(SMBwriteBs);
5701 END_PROFILE(SMBwriteBs);
5705 /****************************************************************************
5706 Reply to a SMBgetattrE.
5707 ****************************************************************************/
5709 int reply_getattrE(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
5711 SMB_STRUCT_STAT sbuf;
5714 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5715 START_PROFILE(SMBgetattrE);
5717 outsize = set_message(outbuf,11,0,True);
5719 if(!fsp || (fsp->conn != conn)) {
5720 END_PROFILE(SMBgetattrE);
5721 return ERROR_DOS(ERRDOS,ERRbadfid);
5724 /* Do an fstat on this file */
5725 if(fsp_stat(fsp, &sbuf)) {
5726 END_PROFILE(SMBgetattrE);
5727 return(UNIXERROR(ERRDOS,ERRnoaccess));
5730 mode = dos_mode(conn,fsp->fsp_name,&sbuf);
5733 * Convert the times into dos times. Set create
5734 * date to be last modify date as UNIX doesn't save
5738 srv_put_dos_date2(outbuf,smb_vwv0,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
5739 srv_put_dos_date2(outbuf,smb_vwv2,sbuf.st_atime);
5740 /* Should we check pending modtime here ? JRA */
5741 srv_put_dos_date2(outbuf,smb_vwv4,sbuf.st_mtime);
5744 SIVAL(outbuf,smb_vwv6,0);
5745 SIVAL(outbuf,smb_vwv8,0);
5747 uint32 allocation_size = get_allocation_size(conn,fsp, &sbuf);
5748 SIVAL(outbuf,smb_vwv6,(uint32)sbuf.st_size);
5749 SIVAL(outbuf,smb_vwv8,allocation_size);
5751 SSVAL(outbuf,smb_vwv10, mode);
5753 DEBUG( 3, ( "reply_getattrE fnum=%d\n", fsp->fnum));
5755 END_PROFILE(SMBgetattrE);