r22502: Fix bug #4536 - delete symlinks to a directory correctly.
[Samba.git] / source / smbd / reply.c
blob1acd78a106925d89587021c39e146fda5ea079b4
1 /*
2 Unix SMB/CIFS implementation.
3 Main SMB reply routines
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Andrew Bartlett 2001
6 Copyright (C) Jeremy Allison 1992-2007.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 This file handles most of the reply_ calls that the server
24 makes to handle specific protocols
27 #include "includes.h"
29 /* look in server.c for some explanation of these variables */
30 extern enum protocol_types Protocol;
31 extern int max_send;
32 extern int max_recv;
33 unsigned int smb_echo_count = 0;
34 extern uint32 global_client_caps;
36 extern struct current_user current_user;
37 extern BOOL global_encrypted_passwords_negotiated;
39 /****************************************************************************
40 Ensure we check the path in *exactly* the same way as W2K for 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
44 set.
45 ****************************************************************************/
47 /* Custom version for processing POSIX paths. */
48 #define IS_PATH_SEP(c,posix_only) ((c) == '/' || (!(posix_only) && (c) == '\\'))
50 NTSTATUS check_path_syntax_internal(pstring destname,
51 const pstring srcname,
52 BOOL posix_path,
53 BOOL *p_last_component_contains_wcard)
55 char *d = destname;
56 const char *s = srcname;
57 NTSTATUS ret = NT_STATUS_OK;
58 BOOL start_of_name_component = True;
60 *p_last_component_contains_wcard = False;
62 while (*s) {
63 if (IS_PATH_SEP(*s,posix_path)) {
65 * Safe to assume is not the second part of a mb char
66 * as this is handled below.
68 /* Eat multiple '/' or '\\' */
69 while (IS_PATH_SEP(*s,posix_path)) {
70 s++;
72 if ((d != destname) && (*s != '\0')) {
73 /* We only care about non-leading or trailing '/' or '\\' */
74 *d++ = '/';
77 start_of_name_component = True;
78 /* New component. */
79 *p_last_component_contains_wcard = False;
80 continue;
83 if (start_of_name_component) {
84 if ((s[0] == '.') && (s[1] == '.') && (IS_PATH_SEP(s[2],posix_path) || s[2] == '\0')) {
85 /* Uh oh - "/../" or "\\..\\" or "/..\0" or "\\..\0" ! */
88 * No mb char starts with '.' so we're safe checking the directory separator here.
91 /* If we just added a '/' - delete it */
92 if ((d > destname) && (*(d-1) == '/')) {
93 *(d-1) = '\0';
94 d--;
97 /* Are we at the start ? Can't go back further if so. */
98 if (d <= destname) {
99 ret = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
100 break;
102 /* Go back one level... */
103 /* We know this is safe as '/' cannot be part of a mb sequence. */
104 /* NOTE - if this assumption is invalid we are not in good shape... */
105 /* Decrement d first as d points to the *next* char to write into. */
106 for (d--; d > destname; d--) {
107 if (*d == '/')
108 break;
110 s += 2; /* Else go past the .. */
111 /* We're still at the start of a name component, just the previous one. */
112 continue;
114 } else if ((s[0] == '.') && ((s[1] == '\0') || IS_PATH_SEP(s[1],posix_path))) {
115 if (posix_path) {
116 /* Eat the '.' */
117 s++;
118 continue;
124 if (!(*s & 0x80)) {
125 if (!posix_path) {
126 if (*s <= 0x1f) {
127 return NT_STATUS_OBJECT_NAME_INVALID;
129 switch (*s) {
130 case '*':
131 case '?':
132 case '<':
133 case '>':
134 case '"':
135 *p_last_component_contains_wcard = True;
136 break;
137 default:
138 break;
141 *d++ = *s++;
142 } else {
143 size_t siz;
144 /* Get the size of the next MB character. */
145 next_codepoint(s,&siz);
146 switch(siz) {
147 case 5:
148 *d++ = *s++;
149 /*fall through*/
150 case 4:
151 *d++ = *s++;
152 /*fall through*/
153 case 3:
154 *d++ = *s++;
155 /*fall through*/
156 case 2:
157 *d++ = *s++;
158 /*fall through*/
159 case 1:
160 *d++ = *s++;
161 break;
162 default:
163 DEBUG(0,("check_path_syntax_internal: character length assumptions invalid !\n"));
164 *d = '\0';
165 return NT_STATUS_INVALID_PARAMETER;
168 start_of_name_component = False;
171 *d = '\0';
172 return ret;
175 /****************************************************************************
176 Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
177 No wildcards allowed.
178 ****************************************************************************/
180 NTSTATUS check_path_syntax(pstring destname, const pstring srcname)
182 BOOL ignore;
183 return check_path_syntax_internal(destname, srcname, False, &ignore);
186 /****************************************************************************
187 Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
188 Wildcards allowed - p_contains_wcard returns true if the last component contained
189 a wildcard.
190 ****************************************************************************/
192 NTSTATUS check_path_syntax_wcard(pstring destname, const pstring srcname, BOOL *p_contains_wcard)
194 return check_path_syntax_internal(destname, srcname, False, p_contains_wcard);
197 /****************************************************************************
198 Check the path for a POSIX client.
199 We're assuming here that '/' is not the second byte in any multibyte char
200 set (a safe assumption).
201 ****************************************************************************/
203 NTSTATUS check_path_syntax_posix(pstring destname, const pstring srcname)
205 BOOL ignore;
206 return check_path_syntax_internal(destname, srcname, True, &ignore);
209 /****************************************************************************
210 Pull a string and check the path allowing a wilcard - provide for error return.
211 ****************************************************************************/
213 size_t srvstr_get_path_wcard(char *inbuf, char *dest, const char *src, size_t dest_len, size_t src_len, int flags,
214 NTSTATUS *err, BOOL *contains_wcard)
216 pstring tmppath;
217 char *tmppath_ptr = tmppath;
218 size_t ret;
219 #ifdef DEVELOPER
220 SMB_ASSERT(dest_len == sizeof(pstring));
221 #endif
223 if (src_len == 0) {
224 ret = srvstr_pull_buf( inbuf, tmppath_ptr, src, dest_len, flags);
225 } else {
226 ret = srvstr_pull( inbuf, tmppath_ptr, src, dest_len, src_len, flags);
229 *contains_wcard = False;
231 if (SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES) {
233 * For a DFS path the function parse_dfs_path()
234 * will do the path processing, just make a copy.
236 pstrcpy(dest, tmppath);
237 *err = NT_STATUS_OK;
238 return ret;
241 if (lp_posix_pathnames()) {
242 *err = check_path_syntax_posix(dest, tmppath);
243 } else {
244 *err = check_path_syntax_wcard(dest, tmppath, contains_wcard);
247 return ret;
250 /****************************************************************************
251 Pull a string and check the path - provide for error return.
252 ****************************************************************************/
254 size_t srvstr_get_path(char *inbuf, char *dest, const char *src, size_t dest_len, size_t src_len, int flags, NTSTATUS *err)
256 pstring tmppath;
257 char *tmppath_ptr = tmppath;
258 size_t ret;
259 #ifdef DEVELOPER
260 SMB_ASSERT(dest_len == sizeof(pstring));
261 #endif
263 if (src_len == 0) {
264 ret = srvstr_pull_buf( inbuf, tmppath_ptr, src, dest_len, flags);
265 } else {
266 ret = srvstr_pull( inbuf, tmppath_ptr, src, dest_len, src_len, flags);
269 if (SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES) {
271 * For a DFS path the function parse_dfs_path()
272 * will do the path processing, just make a copy.
274 pstrcpy(dest, tmppath);
275 *err = NT_STATUS_OK;
276 return ret;
279 if (lp_posix_pathnames()) {
280 *err = check_path_syntax_posix(dest, tmppath);
281 } else {
282 *err = check_path_syntax(dest, tmppath);
285 return ret;
288 /****************************************************************************
289 Reply to a special message.
290 ****************************************************************************/
292 int reply_special(char *inbuf,char *outbuf)
294 int outsize = 4;
295 int msg_type = CVAL(inbuf,0);
296 int msg_flags = CVAL(inbuf,1);
297 fstring name1,name2;
298 char name_type = 0;
300 static BOOL already_got_session = False;
302 *name1 = *name2 = 0;
304 memset(outbuf,'\0',smb_size);
306 smb_setlen(inbuf,outbuf,0);
308 switch (msg_type) {
309 case 0x81: /* session request */
311 if (already_got_session) {
312 exit_server_cleanly("multiple session request not permitted");
315 SCVAL(outbuf,0,0x82);
316 SCVAL(outbuf,3,0);
317 if (name_len(inbuf+4) > 50 ||
318 name_len(inbuf+4 + name_len(inbuf + 4)) > 50) {
319 DEBUG(0,("Invalid name length in session request\n"));
320 return(0);
322 name_extract(inbuf,4,name1);
323 name_type = name_extract(inbuf,4 + name_len(inbuf + 4),name2);
324 DEBUG(2,("netbios connect: name1=%s name2=%s\n",
325 name1,name2));
327 set_local_machine_name(name1, True);
328 set_remote_machine_name(name2, True);
330 DEBUG(2,("netbios connect: local=%s remote=%s, name type = %x\n",
331 get_local_machine_name(), get_remote_machine_name(),
332 name_type));
334 if (name_type == 'R') {
335 /* We are being asked for a pathworks session ---
336 no thanks! */
337 SCVAL(outbuf, 0,0x83);
338 break;
341 /* only add the client's machine name to the list
342 of possibly valid usernames if we are operating
343 in share mode security */
344 if (lp_security() == SEC_SHARE) {
345 add_session_user(get_remote_machine_name());
348 reload_services(True);
349 reopen_logs();
351 already_got_session = True;
352 break;
354 case 0x89: /* session keepalive request
355 (some old clients produce this?) */
356 SCVAL(outbuf,0,SMBkeepalive);
357 SCVAL(outbuf,3,0);
358 break;
360 case 0x82: /* positive session response */
361 case 0x83: /* negative session response */
362 case 0x84: /* retarget session response */
363 DEBUG(0,("Unexpected session response\n"));
364 break;
366 case SMBkeepalive: /* session keepalive */
367 default:
368 return(0);
371 DEBUG(5,("init msg_type=0x%x msg_flags=0x%x\n",
372 msg_type, msg_flags));
374 return(outsize);
377 /****************************************************************************
378 Reply to a tcon.
379 conn POINTER CAN BE NULL HERE !
380 ****************************************************************************/
382 int reply_tcon(connection_struct *conn,
383 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
385 const char *service;
386 pstring service_buf;
387 pstring password;
388 pstring dev;
389 int outsize = 0;
390 uint16 vuid = SVAL(inbuf,smb_uid);
391 int pwlen=0;
392 NTSTATUS nt_status;
393 char *p;
394 DATA_BLOB password_blob;
396 START_PROFILE(SMBtcon);
398 *service_buf = *password = *dev = 0;
400 p = smb_buf(inbuf)+1;
401 p += srvstr_pull_buf(inbuf, service_buf, p, sizeof(service_buf), STR_TERMINATE) + 1;
402 pwlen = srvstr_pull_buf(inbuf, password, p, sizeof(password), STR_TERMINATE) + 1;
403 p += pwlen;
404 p += srvstr_pull_buf(inbuf, dev, p, sizeof(dev), STR_TERMINATE) + 1;
406 p = strrchr_m(service_buf,'\\');
407 if (p) {
408 service = p+1;
409 } else {
410 service = service_buf;
413 password_blob = data_blob(password, pwlen+1);
415 conn = make_connection(service,password_blob,dev,vuid,&nt_status);
417 data_blob_clear_free(&password_blob);
419 if (!conn) {
420 END_PROFILE(SMBtcon);
421 return ERROR_NT(nt_status);
424 outsize = set_message(inbuf,outbuf,2,0,True);
425 SSVAL(outbuf,smb_vwv0,max_recv);
426 SSVAL(outbuf,smb_vwv1,conn->cnum);
427 SSVAL(outbuf,smb_tid,conn->cnum);
429 DEBUG(3,("tcon service=%s cnum=%d\n",
430 service, conn->cnum));
432 END_PROFILE(SMBtcon);
433 return(outsize);
436 /****************************************************************************
437 Reply to a tcon and X.
438 conn POINTER CAN BE NULL HERE !
439 ****************************************************************************/
441 int reply_tcon_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
443 fstring service;
444 DATA_BLOB password;
446 /* what the cleint thinks the device is */
447 fstring client_devicetype;
448 /* what the server tells the client the share represents */
449 const char *server_devicetype;
450 NTSTATUS nt_status;
451 uint16 vuid = SVAL(inbuf,smb_uid);
452 int passlen = SVAL(inbuf,smb_vwv3);
453 pstring path;
454 char *p, *q;
455 uint16 tcon_flags = SVAL(inbuf,smb_vwv2);
457 START_PROFILE(SMBtconX);
459 *service = *client_devicetype = 0;
461 /* we might have to close an old one */
462 if ((SVAL(inbuf,smb_vwv2) & 0x1) && conn) {
463 close_cnum(conn,vuid);
466 if (passlen > MAX_PASS_LEN) {
467 return ERROR_DOS(ERRDOS,ERRbuftoosmall);
470 if (global_encrypted_passwords_negotiated) {
471 password = data_blob(smb_buf(inbuf),passlen);
472 if (lp_security() == SEC_SHARE) {
474 * Security = share always has a pad byte
475 * after the password.
477 p = smb_buf(inbuf) + passlen + 1;
478 } else {
479 p = smb_buf(inbuf) + passlen;
481 } else {
482 password = data_blob(smb_buf(inbuf),passlen+1);
483 /* Ensure correct termination */
484 password.data[passlen]=0;
485 p = smb_buf(inbuf) + passlen + 1;
488 p += srvstr_pull_buf(inbuf, path, p, sizeof(path), STR_TERMINATE);
491 * the service name can be either: \\server\share
492 * or share directly like on the DELL PowerVault 705
494 if (*path=='\\') {
495 q = strchr_m(path+2,'\\');
496 if (!q) {
497 END_PROFILE(SMBtconX);
498 return(ERROR_DOS(ERRDOS,ERRnosuchshare));
500 fstrcpy(service,q+1);
502 else
503 fstrcpy(service,path);
505 p += srvstr_pull(inbuf, client_devicetype, p, sizeof(client_devicetype), 6, STR_ASCII);
507 DEBUG(4,("Client requested device type [%s] for share [%s]\n", client_devicetype, service));
509 conn = make_connection(service,password,client_devicetype,vuid,&nt_status);
511 data_blob_clear_free(&password);
513 if (!conn) {
514 END_PROFILE(SMBtconX);
515 return ERROR_NT(nt_status);
518 if ( IS_IPC(conn) )
519 server_devicetype = "IPC";
520 else if ( IS_PRINT(conn) )
521 server_devicetype = "LPT1:";
522 else
523 server_devicetype = "A:";
525 if (Protocol < PROTOCOL_NT1) {
526 set_message(inbuf,outbuf,2,0,True);
527 p = smb_buf(outbuf);
528 p += srvstr_push(outbuf, p, server_devicetype, -1,
529 STR_TERMINATE|STR_ASCII);
530 set_message_end(inbuf,outbuf,p);
531 } else {
532 /* NT sets the fstype of IPC$ to the null string */
533 const char *fstype = IS_IPC(conn) ? "" : lp_fstype(SNUM(conn));
535 if (tcon_flags & TCONX_FLAG_EXTENDED_RESPONSE) {
536 /* Return permissions. */
537 uint32 perm1 = 0;
538 uint32 perm2 = 0;
540 set_message(inbuf,outbuf,7,0,True);
542 if (IS_IPC(conn)) {
543 perm1 = FILE_ALL_ACCESS;
544 perm2 = FILE_ALL_ACCESS;
545 } else {
546 perm1 = CAN_WRITE(conn) ?
547 SHARE_ALL_ACCESS :
548 SHARE_READ_ONLY;
551 SIVAL(outbuf, smb_vwv3, perm1);
552 SIVAL(outbuf, smb_vwv5, perm2);
553 } else {
554 set_message(inbuf,outbuf,3,0,True);
557 p = smb_buf(outbuf);
558 p += srvstr_push(outbuf, p, server_devicetype, -1,
559 STR_TERMINATE|STR_ASCII);
560 p += srvstr_push(outbuf, p, fstype, -1,
561 STR_TERMINATE);
563 set_message_end(inbuf,outbuf,p);
565 /* what does setting this bit do? It is set by NT4 and
566 may affect the ability to autorun mounted cdroms */
567 SSVAL(outbuf, smb_vwv2, SMB_SUPPORT_SEARCH_BITS|
568 (lp_csc_policy(SNUM(conn)) << 2));
570 init_dfsroot(conn, inbuf, outbuf);
574 DEBUG(3,("tconX service=%s \n",
575 service));
577 /* set the incoming and outgoing tid to the just created one */
578 SSVAL(inbuf,smb_tid,conn->cnum);
579 SSVAL(outbuf,smb_tid,conn->cnum);
581 END_PROFILE(SMBtconX);
582 return chain_reply(inbuf,outbuf,length,bufsize);
585 /****************************************************************************
586 Reply to an unknown type.
587 ****************************************************************************/
589 int reply_unknown(char *inbuf,char *outbuf)
591 int type;
592 type = CVAL(inbuf,smb_com);
594 DEBUG(0,("unknown command type (%s): type=%d (0x%X)\n",
595 smb_fn_name(type), type, type));
597 return(ERROR_DOS(ERRSRV,ERRunknownsmb));
600 /****************************************************************************
601 Reply to an ioctl.
602 conn POINTER CAN BE NULL HERE !
603 ****************************************************************************/
605 int reply_ioctl(connection_struct *conn,
606 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
608 uint16 device = SVAL(inbuf,smb_vwv1);
609 uint16 function = SVAL(inbuf,smb_vwv2);
610 uint32 ioctl_code = (device << 16) + function;
611 int replysize, outsize;
612 char *p;
613 START_PROFILE(SMBioctl);
615 DEBUG(4, ("Received IOCTL (code 0x%x)\n", ioctl_code));
617 switch (ioctl_code) {
618 case IOCTL_QUERY_JOB_INFO:
619 replysize = 32;
620 break;
621 default:
622 END_PROFILE(SMBioctl);
623 return(ERROR_DOS(ERRSRV,ERRnosupport));
626 outsize = set_message(inbuf,outbuf,8,replysize+1,True);
627 SSVAL(outbuf,smb_vwv1,replysize); /* Total data bytes returned */
628 SSVAL(outbuf,smb_vwv5,replysize); /* Data bytes this buffer */
629 SSVAL(outbuf,smb_vwv6,52); /* Offset to data */
630 p = smb_buf(outbuf) + 1; /* Allow for alignment */
632 switch (ioctl_code) {
633 case IOCTL_QUERY_JOB_INFO:
635 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
636 if (!fsp) {
637 END_PROFILE(SMBioctl);
638 return(UNIXERROR(ERRDOS,ERRbadfid));
640 SSVAL(p,0,fsp->rap_print_jobid); /* Job number */
641 srvstr_push(outbuf, p+2, global_myname(), 15, STR_TERMINATE|STR_ASCII);
642 if (conn) {
643 srvstr_push(outbuf, p+18, lp_servicename(SNUM(conn)), 13, STR_TERMINATE|STR_ASCII);
645 break;
649 END_PROFILE(SMBioctl);
650 return outsize;
653 /****************************************************************************
654 Strange checkpath NTSTATUS mapping.
655 ****************************************************************************/
657 static NTSTATUS map_checkpath_error(const char *inbuf, NTSTATUS status)
659 /* Strange DOS error code semantics only for checkpath... */
660 if (!(SVAL(inbuf,smb_flg2) & FLAGS2_32_BIT_ERROR_CODES)) {
661 if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_INVALID,status)) {
662 /* We need to map to ERRbadpath */
663 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
666 return status;
669 /****************************************************************************
670 Reply to a checkpath.
671 ****************************************************************************/
673 int reply_checkpath(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
675 int outsize = 0;
676 pstring name;
677 SMB_STRUCT_STAT sbuf;
678 NTSTATUS status;
680 START_PROFILE(SMBcheckpath);
682 srvstr_get_path(inbuf, name, smb_buf(inbuf) + 1, sizeof(name), 0, STR_TERMINATE, &status);
683 if (!NT_STATUS_IS_OK(status)) {
684 END_PROFILE(SMBcheckpath);
685 status = map_checkpath_error(inbuf, status);
686 return ERROR_NT(status);
689 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, name);
690 if (!NT_STATUS_IS_OK(status)) {
691 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
692 END_PROFILE(SMBcheckpath);
693 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
695 goto path_err;
698 DEBUG(3,("reply_checkpath %s mode=%d\n", name, (int)SVAL(inbuf,smb_vwv0)));
700 status = unix_convert(conn, name, False, NULL, &sbuf);
701 if (!NT_STATUS_IS_OK(status)) {
702 goto path_err;
705 status = check_name(conn, name);
706 if (!NT_STATUS_IS_OK(status)) {
707 DEBUG(3,("reply_checkpath: check_name of %s failed (%s)\n",name,nt_errstr(status)));
708 goto path_err;
711 if (!VALID_STAT(sbuf) && (SMB_VFS_STAT(conn,name,&sbuf) != 0)) {
712 DEBUG(3,("reply_checkpath: stat of %s failed (%s)\n",name,strerror(errno)));
713 status = map_nt_error_from_unix(errno);
714 goto path_err;
717 if (!S_ISDIR(sbuf.st_mode)) {
718 END_PROFILE(SMBcheckpath);
719 return ERROR_BOTH(NT_STATUS_NOT_A_DIRECTORY,ERRDOS,ERRbadpath);
722 outsize = set_message(inbuf,outbuf,0,0,False);
724 END_PROFILE(SMBcheckpath);
725 return outsize;
727 path_err:
729 END_PROFILE(SMBcheckpath);
731 /* We special case this - as when a Windows machine
732 is parsing a path is steps through the components
733 one at a time - if a component fails it expects
734 ERRbadpath, not ERRbadfile.
736 status = map_checkpath_error(inbuf, status);
737 if(NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
739 * Windows returns different error codes if
740 * the parent directory is valid but not the
741 * last component - it returns NT_STATUS_OBJECT_NAME_NOT_FOUND
742 * for that case and NT_STATUS_OBJECT_PATH_NOT_FOUND
743 * if the path is invalid.
745 return ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpath);
748 return ERROR_NT(status);
751 /****************************************************************************
752 Reply to a getatr.
753 ****************************************************************************/
755 int reply_getatr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
757 pstring fname;
758 int outsize = 0;
759 SMB_STRUCT_STAT sbuf;
760 int mode=0;
761 SMB_OFF_T size=0;
762 time_t mtime=0;
763 char *p;
764 NTSTATUS status;
766 START_PROFILE(SMBgetatr);
768 p = smb_buf(inbuf) + 1;
769 p += srvstr_get_path(inbuf, fname, p, sizeof(fname), 0, STR_TERMINATE, &status);
770 if (!NT_STATUS_IS_OK(status)) {
771 END_PROFILE(SMBgetatr);
772 return ERROR_NT(status);
775 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, fname);
776 if (!NT_STATUS_IS_OK(status)) {
777 END_PROFILE(SMBgetatr);
778 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
779 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
781 return ERROR_NT(status);
784 /* dos smetimes asks for a stat of "" - it returns a "hidden directory"
785 under WfWg - weird! */
786 if (*fname == '\0') {
787 mode = aHIDDEN | aDIR;
788 if (!CAN_WRITE(conn)) {
789 mode |= aRONLY;
791 size = 0;
792 mtime = 0;
793 } else {
794 status = unix_convert(conn, fname, False, NULL,&sbuf);
795 if (!NT_STATUS_IS_OK(status)) {
796 END_PROFILE(SMBgetatr);
797 return ERROR_NT(status);
799 status = check_name(conn, fname);
800 if (!NT_STATUS_IS_OK(status)) {
801 DEBUG(3,("reply_getatr: check_name of %s failed (%s)\n",fname,nt_errstr(status)));
802 END_PROFILE(SMBgetatr);
803 return ERROR_NT(status);
805 if (!VALID_STAT(sbuf) && (SMB_VFS_STAT(conn,fname,&sbuf) != 0)) {
806 DEBUG(3,("reply_getatr: stat of %s failed (%s)\n",fname,strerror(errno)));
807 return UNIXERROR(ERRDOS,ERRbadfile);
810 mode = dos_mode(conn,fname,&sbuf);
811 size = sbuf.st_size;
812 mtime = sbuf.st_mtime;
813 if (mode & aDIR) {
814 size = 0;
818 outsize = set_message(inbuf,outbuf,10,0,True);
820 SSVAL(outbuf,smb_vwv0,mode);
821 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
822 srv_put_dos_date3(outbuf,smb_vwv1,mtime & ~1);
823 } else {
824 srv_put_dos_date3(outbuf,smb_vwv1,mtime);
826 SIVAL(outbuf,smb_vwv3,(uint32)size);
828 if (Protocol >= PROTOCOL_NT1) {
829 SSVAL(outbuf,smb_flg2,SVAL(outbuf, smb_flg2) | FLAGS2_IS_LONG_NAME);
832 DEBUG(3,("reply_getatr: name=%s mode=%d size=%u\n", fname, mode, (unsigned int)size ) );
834 END_PROFILE(SMBgetatr);
835 return(outsize);
838 /****************************************************************************
839 Reply to a setatr.
840 ****************************************************************************/
842 int reply_setatr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
844 pstring fname;
845 int outsize = 0;
846 int mode;
847 time_t mtime;
848 SMB_STRUCT_STAT sbuf;
849 char *p;
850 NTSTATUS status;
852 START_PROFILE(SMBsetatr);
854 p = smb_buf(inbuf) + 1;
855 p += srvstr_get_path(inbuf, fname, p, sizeof(fname), 0, STR_TERMINATE, &status);
856 if (!NT_STATUS_IS_OK(status)) {
857 END_PROFILE(SMBsetatr);
858 return ERROR_NT(status);
861 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, fname);
862 if (!NT_STATUS_IS_OK(status)) {
863 END_PROFILE(SMBsetatr);
864 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
865 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
867 return ERROR_NT(status);
870 status = unix_convert(conn, fname, False, NULL, &sbuf);
871 if (!NT_STATUS_IS_OK(status)) {
872 END_PROFILE(SMBsetatr);
873 return ERROR_NT(status);
876 status = check_name(conn, fname);
877 if (!NT_STATUS_IS_OK(status)) {
878 END_PROFILE(SMBsetatr);
879 return ERROR_NT(status);
882 if (fname[0] == '.' && fname[1] == '\0') {
884 * Not sure here is the right place to catch this
885 * condition. Might be moved to somewhere else later -- vl
887 END_PROFILE(SMBsetatr);
888 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
891 mode = SVAL(inbuf,smb_vwv0);
892 mtime = srv_make_unix_date3(inbuf+smb_vwv1);
894 if (mode != FILE_ATTRIBUTE_NORMAL) {
895 if (VALID_STAT_OF_DIR(sbuf))
896 mode |= aDIR;
897 else
898 mode &= ~aDIR;
900 if (file_set_dosmode(conn,fname,mode,&sbuf,False) != 0) {
901 END_PROFILE(SMBsetatr);
902 return UNIXERROR(ERRDOS, ERRnoaccess);
906 if (!set_filetime(conn,fname,convert_time_t_to_timespec(mtime))) {
907 END_PROFILE(SMBsetatr);
908 return UNIXERROR(ERRDOS, ERRnoaccess);
911 outsize = set_message(inbuf,outbuf,0,0,False);
913 DEBUG( 3, ( "setatr name=%s mode=%d\n", fname, mode ) );
915 END_PROFILE(SMBsetatr);
916 return(outsize);
919 /****************************************************************************
920 Reply to a dskattr.
921 ****************************************************************************/
923 int reply_dskattr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
925 int outsize = 0;
926 SMB_BIG_UINT dfree,dsize,bsize;
927 START_PROFILE(SMBdskattr);
929 if (get_dfree_info(conn,".",True,&bsize,&dfree,&dsize) == (SMB_BIG_UINT)-1) {
930 END_PROFILE(SMBdskattr);
931 return(UNIXERROR(ERRHRD,ERRgeneral));
934 outsize = set_message(inbuf,outbuf,5,0,True);
936 if (Protocol <= PROTOCOL_LANMAN2) {
937 double total_space, free_space;
938 /* we need to scale this to a number that DOS6 can handle. We
939 use floating point so we can handle large drives on systems
940 that don't have 64 bit integers
942 we end up displaying a maximum of 2G to DOS systems
944 total_space = dsize * (double)bsize;
945 free_space = dfree * (double)bsize;
947 dsize = (total_space+63*512) / (64*512);
948 dfree = (free_space+63*512) / (64*512);
950 if (dsize > 0xFFFF) dsize = 0xFFFF;
951 if (dfree > 0xFFFF) dfree = 0xFFFF;
953 SSVAL(outbuf,smb_vwv0,dsize);
954 SSVAL(outbuf,smb_vwv1,64); /* this must be 64 for dos systems */
955 SSVAL(outbuf,smb_vwv2,512); /* and this must be 512 */
956 SSVAL(outbuf,smb_vwv3,dfree);
957 } else {
958 SSVAL(outbuf,smb_vwv0,dsize);
959 SSVAL(outbuf,smb_vwv1,bsize/512);
960 SSVAL(outbuf,smb_vwv2,512);
961 SSVAL(outbuf,smb_vwv3,dfree);
964 DEBUG(3,("dskattr dfree=%d\n", (unsigned int)dfree));
966 END_PROFILE(SMBdskattr);
967 return(outsize);
970 /****************************************************************************
971 Reply to a search.
972 Can be called from SMBsearch, SMBffirst or SMBfunique.
973 ****************************************************************************/
975 int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
977 pstring mask;
978 pstring directory;
979 pstring fname;
980 SMB_OFF_T size;
981 uint32 mode;
982 time_t date;
983 uint32 dirtype;
984 int outsize = 0;
985 unsigned int numentries = 0;
986 unsigned int maxentries = 0;
987 BOOL finished = False;
988 char *p;
989 int status_len;
990 pstring path;
991 char status[21];
992 int dptr_num= -1;
993 BOOL check_descend = False;
994 BOOL expect_close = False;
995 NTSTATUS nt_status;
996 BOOL mask_contains_wcard = False;
997 BOOL allow_long_path_components = (SVAL(inbuf,smb_flg2) & FLAGS2_LONG_PATH_COMPONENTS) ? True : False;
999 START_PROFILE(SMBsearch);
1001 if (lp_posix_pathnames()) {
1002 END_PROFILE(SMBsearch);
1003 return reply_unknown(inbuf, outbuf);
1006 *mask = *directory = *fname = 0;
1008 /* If we were called as SMBffirst then we must expect close. */
1009 if(CVAL(inbuf,smb_com) == SMBffirst) {
1010 expect_close = True;
1013 outsize = set_message(inbuf,outbuf,1,3,True);
1014 maxentries = SVAL(inbuf,smb_vwv0);
1015 dirtype = SVAL(inbuf,smb_vwv1);
1016 p = smb_buf(inbuf) + 1;
1017 p += srvstr_get_path_wcard(inbuf, path, p, sizeof(path), 0, STR_TERMINATE, &nt_status, &mask_contains_wcard);
1018 if (!NT_STATUS_IS_OK(nt_status)) {
1019 END_PROFILE(SMBsearch);
1020 return ERROR_NT(nt_status);
1023 nt_status = resolve_dfspath_wcard(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, path, &mask_contains_wcard);
1024 if (!NT_STATUS_IS_OK(nt_status)) {
1025 END_PROFILE(SMBsearch);
1026 if (NT_STATUS_EQUAL(nt_status,NT_STATUS_PATH_NOT_COVERED)) {
1027 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1029 return ERROR_NT(nt_status);
1032 p++;
1033 status_len = SVAL(p, 0);
1034 p += 2;
1036 /* dirtype &= ~aDIR; */
1038 if (status_len == 0) {
1039 SMB_STRUCT_STAT sbuf;
1041 pstrcpy(directory,path);
1042 nt_status = unix_convert(conn, directory, True, NULL, &sbuf);
1043 if (!NT_STATUS_IS_OK(nt_status)) {
1044 END_PROFILE(SMBsearch);
1045 return ERROR_NT(nt_status);
1048 nt_status = check_name(conn, directory);
1049 if (!NT_STATUS_IS_OK(nt_status)) {
1050 END_PROFILE(SMBsearch);
1051 return ERROR_NT(nt_status);
1054 p = strrchr_m(directory,'/');
1055 if (!p) {
1056 pstrcpy(mask,directory);
1057 pstrcpy(directory,".");
1058 } else {
1059 *p = 0;
1060 pstrcpy(mask,p+1);
1063 if (*directory == '\0') {
1064 pstrcpy(directory,".");
1066 memset((char *)status,'\0',21);
1067 SCVAL(status,0,(dirtype & 0x1F));
1068 } else {
1069 int status_dirtype;
1071 memcpy(status,p,21);
1072 status_dirtype = CVAL(status,0) & 0x1F;
1073 if (status_dirtype != (dirtype & 0x1F)) {
1074 dirtype = status_dirtype;
1077 conn->dirptr = dptr_fetch(status+12,&dptr_num);
1078 if (!conn->dirptr) {
1079 goto SearchEmpty;
1081 string_set(&conn->dirpath,dptr_path(dptr_num));
1082 pstrcpy(mask, dptr_wcard(dptr_num));
1084 * For a 'continue' search we have no string. So
1085 * check from the initial saved string.
1087 mask_contains_wcard = ms_has_wild(mask);
1090 p = smb_buf(outbuf) + 3;
1092 if (status_len == 0) {
1093 nt_status = dptr_create(conn,
1094 directory,
1095 True,
1096 expect_close,
1097 SVAL(inbuf,smb_pid),
1098 mask,
1099 mask_contains_wcard,
1100 dirtype,
1101 &conn->dirptr);
1102 if (!NT_STATUS_IS_OK(nt_status)) {
1103 return ERROR_NT(nt_status);
1105 dptr_num = dptr_dnum(conn->dirptr);
1106 } else {
1107 dirtype = dptr_attr(dptr_num);
1110 DEBUG(4,("dptr_num is %d\n",dptr_num));
1112 if ((dirtype&0x1F) == aVOLID) {
1113 memcpy(p,status,21);
1114 make_dir_struct(p,"???????????",volume_label(SNUM(conn)),
1115 0,aVOLID,0,!allow_long_path_components);
1116 dptr_fill(p+12,dptr_num);
1117 if (dptr_zero(p+12) && (status_len==0)) {
1118 numentries = 1;
1119 } else {
1120 numentries = 0;
1122 p += DIR_STRUCT_SIZE;
1123 } else {
1124 unsigned int i;
1125 maxentries = MIN(maxentries, ((BUFFER_SIZE - (p - outbuf))/DIR_STRUCT_SIZE));
1127 DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
1128 conn->dirpath,lp_dontdescend(SNUM(conn))));
1129 if (in_list(conn->dirpath, lp_dontdescend(SNUM(conn)),True)) {
1130 check_descend = True;
1133 for (i=numentries;(i<maxentries) && !finished;i++) {
1134 finished = !get_dir_entry(conn,mask,dirtype,fname,&size,&mode,&date,check_descend);
1135 if (!finished) {
1136 memcpy(p,status,21);
1137 make_dir_struct(p,mask,fname,size, mode,date,
1138 !allow_long_path_components);
1139 if (!dptr_fill(p+12,dptr_num)) {
1140 break;
1142 numentries++;
1143 p += DIR_STRUCT_SIZE;
1148 SearchEmpty:
1150 /* If we were called as SMBffirst with smb_search_id == NULL
1151 and no entries were found then return error and close dirptr
1152 (X/Open spec) */
1154 if (numentries == 0) {
1155 dptr_close(&dptr_num);
1156 } else if(expect_close && status_len == 0) {
1157 /* Close the dptr - we know it's gone */
1158 dptr_close(&dptr_num);
1161 /* If we were called as SMBfunique, then we can close the dirptr now ! */
1162 if(dptr_num >= 0 && CVAL(inbuf,smb_com) == SMBfunique) {
1163 dptr_close(&dptr_num);
1166 if ((numentries == 0) && !mask_contains_wcard) {
1167 return ERROR_BOTH(STATUS_NO_MORE_FILES,ERRDOS,ERRnofiles);
1170 SSVAL(outbuf,smb_vwv0,numentries);
1171 SSVAL(outbuf,smb_vwv1,3 + numentries * DIR_STRUCT_SIZE);
1172 SCVAL(smb_buf(outbuf),0,5);
1173 SSVAL(smb_buf(outbuf),1,numentries*DIR_STRUCT_SIZE);
1175 /* The replies here are never long name. */
1176 SSVAL(outbuf,smb_flg2,SVAL(outbuf, smb_flg2) & (~FLAGS2_IS_LONG_NAME));
1177 if (!allow_long_path_components) {
1178 SSVAL(outbuf,smb_flg2,SVAL(outbuf, smb_flg2) & (~FLAGS2_LONG_PATH_COMPONENTS));
1181 /* This SMB *always* returns ASCII names. Remove the unicode bit in flags2. */
1182 SSVAL(outbuf,smb_flg2, (SVAL(outbuf, smb_flg2) & (~FLAGS2_UNICODE_STRINGS)));
1184 outsize += DIR_STRUCT_SIZE*numentries;
1185 smb_setlen(inbuf,outbuf,outsize - 4);
1187 if ((! *directory) && dptr_path(dptr_num))
1188 slprintf(directory, sizeof(directory)-1, "(%s)",dptr_path(dptr_num));
1190 DEBUG( 4, ( "%s mask=%s path=%s dtype=%d nument=%u of %u\n",
1191 smb_fn_name(CVAL(inbuf,smb_com)),
1192 mask, directory, dirtype, numentries, maxentries ) );
1194 END_PROFILE(SMBsearch);
1195 return(outsize);
1198 /****************************************************************************
1199 Reply to a fclose (stop directory search).
1200 ****************************************************************************/
1202 int reply_fclose(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1204 int outsize = 0;
1205 int status_len;
1206 pstring path;
1207 char status[21];
1208 int dptr_num= -2;
1209 char *p;
1210 NTSTATUS err;
1211 BOOL path_contains_wcard = False;
1213 START_PROFILE(SMBfclose);
1215 if (lp_posix_pathnames()) {
1216 END_PROFILE(SMBfclose);
1217 return reply_unknown(inbuf, outbuf);
1220 outsize = set_message(inbuf,outbuf,1,0,True);
1221 p = smb_buf(inbuf) + 1;
1222 p += srvstr_get_path_wcard(inbuf, path, p, sizeof(path), 0, STR_TERMINATE, &err, &path_contains_wcard);
1223 if (!NT_STATUS_IS_OK(err)) {
1224 END_PROFILE(SMBfclose);
1225 return ERROR_NT(err);
1227 p++;
1228 status_len = SVAL(p,0);
1229 p += 2;
1231 if (status_len == 0) {
1232 END_PROFILE(SMBfclose);
1233 return ERROR_DOS(ERRSRV,ERRsrverror);
1236 memcpy(status,p,21);
1238 if(dptr_fetch(status+12,&dptr_num)) {
1239 /* Close the dptr - we know it's gone */
1240 dptr_close(&dptr_num);
1243 SSVAL(outbuf,smb_vwv0,0);
1245 DEBUG(3,("search close\n"));
1247 END_PROFILE(SMBfclose);
1248 return(outsize);
1251 /****************************************************************************
1252 Reply to an open.
1253 ****************************************************************************/
1255 int reply_open(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1257 pstring fname;
1258 int outsize = 0;
1259 uint32 fattr=0;
1260 SMB_OFF_T size = 0;
1261 time_t mtime=0;
1262 int info;
1263 SMB_STRUCT_STAT sbuf;
1264 files_struct *fsp;
1265 int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
1266 int deny_mode;
1267 uint32 dos_attr = SVAL(inbuf,smb_vwv1);
1268 uint32 access_mask;
1269 uint32 share_mode;
1270 uint32 create_disposition;
1271 uint32 create_options = 0;
1272 NTSTATUS status;
1273 START_PROFILE(SMBopen);
1275 deny_mode = SVAL(inbuf,smb_vwv0);
1277 srvstr_get_path(inbuf, fname, smb_buf(inbuf)+1, sizeof(fname), 0, STR_TERMINATE, &status);
1278 if (!NT_STATUS_IS_OK(status)) {
1279 END_PROFILE(SMBopen);
1280 return ERROR_NT(status);
1283 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, fname);
1284 if (!NT_STATUS_IS_OK(status)) {
1285 END_PROFILE(SMBopen);
1286 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1287 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1289 return ERROR_NT(status);
1292 status = unix_convert(conn, fname, False, NULL, &sbuf);
1293 if (!NT_STATUS_IS_OK(status)) {
1294 END_PROFILE(SMBopen);
1295 return ERROR_NT(status);
1298 status = check_name(conn, fname);
1299 if (!NT_STATUS_IS_OK(status)) {
1300 END_PROFILE(SMBopen);
1301 return ERROR_NT(status);
1304 if (!map_open_params_to_ntcreate(fname, deny_mode, OPENX_FILE_EXISTS_OPEN,
1305 &access_mask, &share_mode, &create_disposition, &create_options)) {
1306 END_PROFILE(SMBopen);
1307 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRbadaccess));
1310 status = open_file_ntcreate(conn,fname,&sbuf,
1311 access_mask,
1312 share_mode,
1313 create_disposition,
1314 create_options,
1315 dos_attr,
1316 oplock_request,
1317 &info, &fsp);
1319 if (!NT_STATUS_IS_OK(status)) {
1320 END_PROFILE(SMBopen);
1321 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1322 /* We have re-scheduled this call. */
1323 return -1;
1325 return ERROR_NT(status);
1328 size = sbuf.st_size;
1329 fattr = dos_mode(conn,fname,&sbuf);
1330 mtime = sbuf.st_mtime;
1332 if (fattr & aDIR) {
1333 DEBUG(3,("attempt to open a directory %s\n",fname));
1334 close_file(fsp,ERROR_CLOSE);
1335 END_PROFILE(SMBopen);
1336 return ERROR_DOS(ERRDOS,ERRnoaccess);
1339 outsize = set_message(inbuf,outbuf,7,0,True);
1340 SSVAL(outbuf,smb_vwv0,fsp->fnum);
1341 SSVAL(outbuf,smb_vwv1,fattr);
1342 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1343 srv_put_dos_date3(outbuf,smb_vwv2,mtime & ~1);
1344 } else {
1345 srv_put_dos_date3(outbuf,smb_vwv2,mtime);
1347 SIVAL(outbuf,smb_vwv4,(uint32)size);
1348 SSVAL(outbuf,smb_vwv6,deny_mode);
1350 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1351 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1354 if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1355 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1357 END_PROFILE(SMBopen);
1358 return(outsize);
1361 /****************************************************************************
1362 Reply to an open and X.
1363 ****************************************************************************/
1365 int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
1367 pstring fname;
1368 uint16 open_flags = SVAL(inbuf,smb_vwv2);
1369 int deny_mode = SVAL(inbuf,smb_vwv3);
1370 uint32 smb_attr = SVAL(inbuf,smb_vwv5);
1371 /* Breakout the oplock request bits so we can set the
1372 reply bits separately. */
1373 int ex_oplock_request = EXTENDED_OPLOCK_REQUEST(inbuf);
1374 int core_oplock_request = CORE_OPLOCK_REQUEST(inbuf);
1375 int oplock_request = ex_oplock_request | core_oplock_request;
1376 #if 0
1377 int smb_sattr = SVAL(inbuf,smb_vwv4);
1378 uint32 smb_time = make_unix_date3(inbuf+smb_vwv6);
1379 #endif
1380 int smb_ofun = SVAL(inbuf,smb_vwv8);
1381 uint32 fattr=0;
1382 int mtime=0;
1383 SMB_STRUCT_STAT sbuf;
1384 int smb_action = 0;
1385 files_struct *fsp;
1386 NTSTATUS status;
1387 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv9);
1388 ssize_t retval = -1;
1389 uint32 access_mask;
1390 uint32 share_mode;
1391 uint32 create_disposition;
1392 uint32 create_options = 0;
1394 START_PROFILE(SMBopenX);
1396 /* If it's an IPC, pass off the pipe handler. */
1397 if (IS_IPC(conn)) {
1398 if (lp_nt_pipe_support()) {
1399 END_PROFILE(SMBopenX);
1400 return reply_open_pipe_and_X(conn, inbuf,outbuf,length,bufsize);
1401 } else {
1402 END_PROFILE(SMBopenX);
1403 return ERROR_DOS(ERRSRV,ERRaccess);
1407 /* XXXX we need to handle passed times, sattr and flags */
1408 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
1409 if (!NT_STATUS_IS_OK(status)) {
1410 END_PROFILE(SMBopenX);
1411 return ERROR_NT(status);
1414 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, fname);
1415 if (!NT_STATUS_IS_OK(status)) {
1416 END_PROFILE(SMBopenX);
1417 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1418 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1420 return ERROR_NT(status);
1423 status = unix_convert(conn, fname, False, NULL, &sbuf);
1424 if (!NT_STATUS_IS_OK(status)) {
1425 END_PROFILE(SMBopenX);
1426 return ERROR_NT(status);
1429 status = check_name(conn, fname);
1430 if (!NT_STATUS_IS_OK(status)) {
1431 END_PROFILE(SMBopenX);
1432 return ERROR_NT(status);
1435 if (!map_open_params_to_ntcreate(fname, deny_mode, smb_ofun,
1436 &access_mask,
1437 &share_mode,
1438 &create_disposition,
1439 &create_options)) {
1440 END_PROFILE(SMBopenX);
1441 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRbadaccess));
1444 status = open_file_ntcreate(conn,fname,&sbuf,
1445 access_mask,
1446 share_mode,
1447 create_disposition,
1448 create_options,
1449 smb_attr,
1450 oplock_request,
1451 &smb_action, &fsp);
1453 if (!NT_STATUS_IS_OK(status)) {
1454 END_PROFILE(SMBopenX);
1455 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1456 /* We have re-scheduled this call. */
1457 return -1;
1459 return ERROR_NT(status);
1462 /* Setting the "size" field in vwv9 and vwv10 causes the file to be set to this size,
1463 if the file is truncated or created. */
1464 if (((smb_action == FILE_WAS_CREATED) || (smb_action == FILE_WAS_OVERWRITTEN)) && allocation_size) {
1465 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1466 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1467 close_file(fsp,ERROR_CLOSE);
1468 END_PROFILE(SMBopenX);
1469 return ERROR_NT(NT_STATUS_DISK_FULL);
1471 retval = vfs_set_filelen(fsp, (SMB_OFF_T)allocation_size);
1472 if (retval < 0) {
1473 close_file(fsp,ERROR_CLOSE);
1474 END_PROFILE(SMBopenX);
1475 return ERROR_NT(NT_STATUS_DISK_FULL);
1477 sbuf.st_size = get_allocation_size(conn,fsp,&sbuf);
1480 fattr = dos_mode(conn,fname,&sbuf);
1481 mtime = sbuf.st_mtime;
1482 if (fattr & aDIR) {
1483 close_file(fsp,ERROR_CLOSE);
1484 END_PROFILE(SMBopenX);
1485 return ERROR_DOS(ERRDOS,ERRnoaccess);
1488 /* If the caller set the extended oplock request bit
1489 and we granted one (by whatever means) - set the
1490 correct bit for extended oplock reply.
1493 if (ex_oplock_request && lp_fake_oplocks(SNUM(conn))) {
1494 smb_action |= EXTENDED_OPLOCK_GRANTED;
1497 if(ex_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1498 smb_action |= EXTENDED_OPLOCK_GRANTED;
1501 /* If the caller set the core oplock request bit
1502 and we granted one (by whatever means) - set the
1503 correct bit for core oplock reply.
1506 if (core_oplock_request && lp_fake_oplocks(SNUM(conn))) {
1507 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1510 if(core_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1511 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1514 if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
1515 set_message(inbuf,outbuf,19,0,True);
1516 } else {
1517 set_message(inbuf,outbuf,15,0,True);
1519 SSVAL(outbuf,smb_vwv2,fsp->fnum);
1520 SSVAL(outbuf,smb_vwv3,fattr);
1521 if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1522 srv_put_dos_date3(outbuf,smb_vwv4,mtime & ~1);
1523 } else {
1524 srv_put_dos_date3(outbuf,smb_vwv4,mtime);
1526 SIVAL(outbuf,smb_vwv6,(uint32)sbuf.st_size);
1527 SSVAL(outbuf,smb_vwv8,GET_OPENX_MODE(deny_mode));
1528 SSVAL(outbuf,smb_vwv11,smb_action);
1530 if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
1531 SIVAL(outbuf, smb_vwv15, STD_RIGHT_ALL_ACCESS);
1534 END_PROFILE(SMBopenX);
1535 return chain_reply(inbuf,outbuf,length,bufsize);
1538 /****************************************************************************
1539 Reply to a SMBulogoffX.
1540 conn POINTER CAN BE NULL HERE !
1541 ****************************************************************************/
1543 int reply_ulogoffX(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
1545 uint16 vuid = SVAL(inbuf,smb_uid);
1546 user_struct *vuser = get_valid_user_struct(vuid);
1547 START_PROFILE(SMBulogoffX);
1549 if(vuser == 0)
1550 DEBUG(3,("ulogoff, vuser id %d does not map to user.\n", vuid));
1552 /* in user level security we are supposed to close any files
1553 open by this user */
1554 if ((vuser != 0) && (lp_security() != SEC_SHARE))
1555 file_close_user(vuid);
1557 invalidate_vuid(vuid);
1559 set_message(inbuf,outbuf,2,0,True);
1561 DEBUG( 3, ( "ulogoffX vuid=%d\n", vuid ) );
1563 END_PROFILE(SMBulogoffX);
1564 return chain_reply(inbuf,outbuf,length,bufsize);
1567 /****************************************************************************
1568 Reply to a mknew or a create.
1569 ****************************************************************************/
1571 int reply_mknew(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1573 pstring fname;
1574 int com;
1575 int outsize = 0;
1576 uint32 fattr = SVAL(inbuf,smb_vwv0);
1577 struct timespec ts[2];
1578 files_struct *fsp;
1579 int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
1580 SMB_STRUCT_STAT sbuf;
1581 NTSTATUS status;
1582 uint32 access_mask = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
1583 uint32 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1584 uint32 create_disposition;
1585 uint32 create_options = 0;
1587 START_PROFILE(SMBcreate);
1589 com = SVAL(inbuf,smb_com);
1591 ts[1] = convert_time_t_to_timespec(srv_make_unix_date3(inbuf + smb_vwv1)); /* mtime. */
1593 srvstr_get_path(inbuf, fname, smb_buf(inbuf) + 1, sizeof(fname), 0, STR_TERMINATE, &status);
1594 if (!NT_STATUS_IS_OK(status)) {
1595 END_PROFILE(SMBcreate);
1596 return ERROR_NT(status);
1599 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, fname);
1600 if (!NT_STATUS_IS_OK(status)) {
1601 END_PROFILE(SMBcreate);
1602 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1603 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1605 return ERROR_NT(status);
1608 status = unix_convert(conn, fname, False, NULL, &sbuf);
1609 if (!NT_STATUS_IS_OK(status)) {
1610 END_PROFILE(SMBcreate);
1611 return ERROR_NT(status);
1614 status = check_name(conn, fname);
1615 if (!NT_STATUS_IS_OK(status)) {
1616 END_PROFILE(SMBcreate);
1617 return ERROR_NT(status);
1620 if (fattr & aVOLID) {
1621 DEBUG(0,("Attempt to create file (%s) with volid set - please report this\n",fname));
1624 if(com == SMBmknew) {
1625 /* We should fail if file exists. */
1626 create_disposition = FILE_CREATE;
1627 } else {
1628 /* Create if file doesn't exist, truncate if it does. */
1629 create_disposition = FILE_OVERWRITE_IF;
1632 /* Open file using ntcreate. */
1633 status = open_file_ntcreate(conn,fname,&sbuf,
1634 access_mask,
1635 share_mode,
1636 create_disposition,
1637 create_options,
1638 fattr,
1639 oplock_request,
1640 NULL, &fsp);
1642 if (!NT_STATUS_IS_OK(status)) {
1643 END_PROFILE(SMBcreate);
1644 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1645 /* We have re-scheduled this call. */
1646 return -1;
1648 return ERROR_NT(status);
1651 ts[0] = get_atimespec(&sbuf); /* atime. */
1652 file_ntimes(conn, fname, ts);
1654 outsize = set_message(inbuf,outbuf,1,0,True);
1655 SSVAL(outbuf,smb_vwv0,fsp->fnum);
1657 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1658 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1661 if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1662 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1665 DEBUG( 2, ( "reply_mknew: file %s\n", fname ) );
1666 DEBUG( 3, ( "reply_mknew %s fd=%d dmode=0x%x\n", fname, fsp->fh->fd, (unsigned int)fattr ) );
1668 END_PROFILE(SMBcreate);
1669 return(outsize);
1672 /****************************************************************************
1673 Reply to a create temporary file.
1674 ****************************************************************************/
1676 int reply_ctemp(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
1678 pstring fname;
1679 int outsize = 0;
1680 uint32 fattr = SVAL(inbuf,smb_vwv0);
1681 files_struct *fsp;
1682 int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
1683 int tmpfd;
1684 SMB_STRUCT_STAT sbuf;
1685 char *p, *s;
1686 NTSTATUS status;
1687 unsigned int namelen;
1689 START_PROFILE(SMBctemp);
1691 srvstr_get_path(inbuf, fname, smb_buf(inbuf)+1, sizeof(fname), 0, STR_TERMINATE, &status);
1692 if (!NT_STATUS_IS_OK(status)) {
1693 END_PROFILE(SMBctemp);
1694 return ERROR_NT(status);
1696 if (*fname) {
1697 pstrcat(fname,"/TMXXXXXX");
1698 } else {
1699 pstrcat(fname,"TMXXXXXX");
1702 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, fname);
1703 if (!NT_STATUS_IS_OK(status)) {
1704 END_PROFILE(SMBctemp);
1705 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1706 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1708 return ERROR_NT(status);
1711 status = unix_convert(conn, fname, False, NULL, &sbuf);
1712 if (!NT_STATUS_IS_OK(status)) {
1713 END_PROFILE(SMBctemp);
1714 return ERROR_NT(status);
1717 status = check_name(conn, fname);
1718 if (!NT_STATUS_IS_OK(status)) {
1719 END_PROFILE(SMBctemp);
1720 return ERROR_NT(status);
1723 tmpfd = smb_mkstemp(fname);
1724 if (tmpfd == -1) {
1725 END_PROFILE(SMBctemp);
1726 return(UNIXERROR(ERRDOS,ERRnoaccess));
1729 SMB_VFS_STAT(conn,fname,&sbuf);
1731 /* We should fail if file does not exist. */
1732 status = open_file_ntcreate(conn,fname,&sbuf,
1733 FILE_GENERIC_READ | FILE_GENERIC_WRITE,
1734 FILE_SHARE_READ|FILE_SHARE_WRITE,
1735 FILE_OPEN,
1737 fattr,
1738 oplock_request,
1739 NULL, &fsp);
1741 /* close fd from smb_mkstemp() */
1742 close(tmpfd);
1744 if (!NT_STATUS_IS_OK(status)) {
1745 END_PROFILE(SMBctemp);
1746 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1747 /* We have re-scheduled this call. */
1748 return -1;
1750 return ERROR_NT(status);
1753 outsize = set_message(inbuf,outbuf,1,0,True);
1754 SSVAL(outbuf,smb_vwv0,fsp->fnum);
1756 /* the returned filename is relative to the directory */
1757 s = strrchr_m(fname, '/');
1758 if (!s) {
1759 s = fname;
1760 } else {
1761 s++;
1764 p = smb_buf(outbuf);
1765 #if 0
1766 /* Tested vs W2K3 - this doesn't seem to be here - null terminated filename is the only
1767 thing in the byte section. JRA */
1768 SSVALS(p, 0, -1); /* what is this? not in spec */
1769 #endif
1770 namelen = srvstr_push(outbuf, p, s, -1, STR_ASCII|STR_TERMINATE);
1771 p += namelen;
1772 outsize = set_message_end(inbuf,outbuf, p);
1774 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1775 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1778 if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1779 SCVAL(outbuf,smb_flg,CVAL(outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1782 DEBUG( 2, ( "reply_ctemp: created temp file %s\n", fname ) );
1783 DEBUG( 3, ( "reply_ctemp %s fd=%d umode=0%o\n", fname, fsp->fh->fd,
1784 (unsigned int)sbuf.st_mode ) );
1786 END_PROFILE(SMBctemp);
1787 return(outsize);
1790 /*******************************************************************
1791 Check if a user is allowed to rename a file.
1792 ********************************************************************/
1794 static NTSTATUS can_rename(connection_struct *conn, char *fname, uint16 dirtype, SMB_STRUCT_STAT *pst)
1796 files_struct *fsp;
1797 uint32 fmode;
1798 NTSTATUS status;
1800 if (!CAN_WRITE(conn)) {
1801 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1804 fmode = dos_mode(conn,fname,pst);
1805 if ((fmode & ~dirtype) & (aHIDDEN | aSYSTEM)) {
1806 return NT_STATUS_NO_SUCH_FILE;
1809 if (S_ISDIR(pst->st_mode)) {
1810 return NT_STATUS_OK;
1813 status = open_file_ntcreate(conn, fname, pst,
1814 DELETE_ACCESS,
1815 FILE_SHARE_READ|FILE_SHARE_WRITE,
1816 FILE_OPEN,
1818 FILE_ATTRIBUTE_NORMAL,
1820 NULL, &fsp);
1822 if (!NT_STATUS_IS_OK(status)) {
1823 return status;
1825 close_file(fsp,NORMAL_CLOSE);
1826 return NT_STATUS_OK;
1829 /*******************************************************************
1830 Check if a user is allowed to delete a file.
1831 ********************************************************************/
1833 static NTSTATUS can_delete(connection_struct *conn, char *fname,
1834 uint32 dirtype, BOOL can_defer)
1836 SMB_STRUCT_STAT sbuf;
1837 uint32 fattr;
1838 files_struct *fsp;
1839 uint32 dirtype_orig = dirtype;
1840 NTSTATUS status;
1842 DEBUG(10,("can_delete: %s, dirtype = %d\n", fname, dirtype ));
1844 if (!CAN_WRITE(conn)) {
1845 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1848 if (SMB_VFS_LSTAT(conn,fname,&sbuf) != 0) {
1849 return map_nt_error_from_unix(errno);
1852 fattr = dos_mode(conn,fname,&sbuf);
1854 if (dirtype & FILE_ATTRIBUTE_NORMAL) {
1855 dirtype = aDIR|aARCH|aRONLY;
1858 dirtype &= (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM);
1859 if (!dirtype) {
1860 return NT_STATUS_NO_SUCH_FILE;
1863 if (!dir_check_ftype(conn, fattr, dirtype)) {
1864 if (fattr & aDIR) {
1865 return NT_STATUS_FILE_IS_A_DIRECTORY;
1867 return NT_STATUS_NO_SUCH_FILE;
1870 if (dirtype_orig & 0x8000) {
1871 /* These will never be set for POSIX. */
1872 return NT_STATUS_NO_SUCH_FILE;
1875 #if 0
1876 if ((fattr & dirtype) & FILE_ATTRIBUTE_DIRECTORY) {
1877 return NT_STATUS_FILE_IS_A_DIRECTORY;
1880 if ((fattr & ~dirtype) & (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)) {
1881 return NT_STATUS_NO_SUCH_FILE;
1884 if (dirtype & 0xFF00) {
1885 /* These will never be set for POSIX. */
1886 return NT_STATUS_NO_SUCH_FILE;
1889 dirtype &= 0xFF;
1890 if (!dirtype) {
1891 return NT_STATUS_NO_SUCH_FILE;
1894 /* Can't delete a directory. */
1895 if (fattr & aDIR) {
1896 return NT_STATUS_FILE_IS_A_DIRECTORY;
1898 #endif
1900 #if 0 /* JRATEST */
1901 else if (dirtype & aDIR) /* Asked for a directory and it isn't. */
1902 return NT_STATUS_OBJECT_NAME_INVALID;
1903 #endif /* JRATEST */
1905 /* Fix for bug #3035 from SATOH Fumiyasu <fumiyas@miraclelinux.com>
1907 On a Windows share, a file with read-only dosmode can be opened with
1908 DELETE_ACCESS. But on a Samba share (delete readonly = no), it
1909 fails with NT_STATUS_CANNOT_DELETE error.
1911 This semantic causes a problem that a user can not
1912 rename a file with read-only dosmode on a Samba share
1913 from a Windows command prompt (i.e. cmd.exe, but can rename
1914 from Windows Explorer).
1917 if (!lp_delete_readonly(SNUM(conn))) {
1918 if (fattr & aRONLY) {
1919 return NT_STATUS_CANNOT_DELETE;
1923 /* On open checks the open itself will check the share mode, so
1924 don't do it here as we'll get it wrong. */
1926 status = open_file_ntcreate(conn, fname, &sbuf,
1927 DELETE_ACCESS,
1928 FILE_SHARE_NONE,
1929 FILE_OPEN,
1931 FILE_ATTRIBUTE_NORMAL,
1932 can_defer ? 0 : INTERNAL_OPEN_ONLY,
1933 NULL, &fsp);
1935 if (NT_STATUS_IS_OK(status)) {
1936 close_file(fsp,NORMAL_CLOSE);
1938 return status;
1941 /****************************************************************************
1942 The guts of the unlink command, split out so it may be called by the NT SMB
1943 code.
1944 ****************************************************************************/
1946 NTSTATUS unlink_internals(connection_struct *conn, uint32 dirtype,
1947 char *name, BOOL has_wild, BOOL can_defer)
1949 pstring directory;
1950 pstring mask;
1951 char *p;
1952 int count=0;
1953 NTSTATUS status = NT_STATUS_OK;
1954 SMB_STRUCT_STAT sbuf;
1956 *directory = *mask = 0;
1958 status = unix_convert(conn, name, has_wild, NULL, &sbuf);
1959 if (!NT_STATUS_IS_OK(status)) {
1960 return status;
1963 p = strrchr_m(name,'/');
1964 if (!p) {
1965 pstrcpy(directory,".");
1966 pstrcpy(mask,name);
1967 } else {
1968 *p = 0;
1969 pstrcpy(directory,name);
1970 pstrcpy(mask,p+1);
1974 * We should only check the mangled cache
1975 * here if unix_convert failed. This means
1976 * that the path in 'mask' doesn't exist
1977 * on the file system and so we need to look
1978 * for a possible mangle. This patch from
1979 * Tine Smukavec <valentin.smukavec@hermes.si>.
1982 if (!VALID_STAT(sbuf) && mangle_is_mangled(mask,conn->params))
1983 mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
1985 if (!has_wild) {
1986 pstrcat(directory,"/");
1987 pstrcat(directory,mask);
1988 if (dirtype == 0) {
1989 dirtype = FILE_ATTRIBUTE_NORMAL;
1992 status = check_name(conn, directory);
1993 if (!NT_STATUS_IS_OK(status)) {
1994 return status;
1997 status = can_delete(conn,directory,dirtype,can_defer);
1998 if (!NT_STATUS_IS_OK(status)) {
1999 return status;
2002 if (SMB_VFS_UNLINK(conn,directory) == 0) {
2003 count++;
2004 notify_fname(conn, NOTIFY_ACTION_REMOVED,
2005 FILE_NOTIFY_CHANGE_FILE_NAME,
2006 directory);
2008 } else {
2009 struct smb_Dir *dir_hnd = NULL;
2010 long offset = 0;
2011 const char *dname;
2013 if ((dirtype & SAMBA_ATTRIBUTES_MASK) == aDIR) {
2014 return NT_STATUS_OBJECT_NAME_INVALID;
2017 if (strequal(mask,"????????.???")) {
2018 pstrcpy(mask,"*");
2021 status = check_name(conn, directory);
2022 if (!NT_STATUS_IS_OK(status)) {
2023 return status;
2026 dir_hnd = OpenDir(conn, directory, mask, dirtype);
2027 if (dir_hnd == NULL) {
2028 return map_nt_error_from_unix(errno);
2031 /* XXXX the CIFS spec says that if bit0 of the flags2 field is set then
2032 the pattern matches against the long name, otherwise the short name
2033 We don't implement this yet XXXX
2036 status = NT_STATUS_NO_SUCH_FILE;
2038 while ((dname = ReadDirName(dir_hnd, &offset))) {
2039 SMB_STRUCT_STAT st;
2040 pstring fname;
2041 pstrcpy(fname,dname);
2043 if (!is_visible_file(conn, directory, dname, &st, True)) {
2044 continue;
2047 /* Quick check for "." and ".." */
2048 if (fname[0] == '.') {
2049 if (!fname[1] || (fname[1] == '.' && !fname[2])) {
2050 continue;
2054 if(!mask_match(fname, mask, conn->case_sensitive)) {
2055 continue;
2058 slprintf(fname,sizeof(fname)-1, "%s/%s",directory,dname);
2060 status = check_name(conn, fname);
2061 if (!NT_STATUS_IS_OK(status)) {
2062 CloseDir(dir_hnd);
2063 return status;
2066 status = can_delete(conn, fname, dirtype, can_defer);
2067 if (!NT_STATUS_IS_OK(status)) {
2068 continue;
2070 if (SMB_VFS_UNLINK(conn,fname) == 0) {
2071 count++;
2072 DEBUG(3,("unlink_internals: succesful unlink "
2073 "[%s]\n",fname));
2074 notify_fname(conn, NOTIFY_ACTION_REMOVED,
2075 FILE_NOTIFY_CHANGE_FILE_NAME,
2076 fname);
2080 CloseDir(dir_hnd);
2083 if (count == 0 && NT_STATUS_IS_OK(status)) {
2084 status = map_nt_error_from_unix(errno);
2087 return status;
2090 /****************************************************************************
2091 Reply to a unlink
2092 ****************************************************************************/
2094 int reply_unlink(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
2095 int dum_buffsize)
2097 int outsize = 0;
2098 pstring name;
2099 uint32 dirtype;
2100 NTSTATUS status;
2101 BOOL path_contains_wcard = False;
2103 START_PROFILE(SMBunlink);
2105 dirtype = SVAL(inbuf,smb_vwv0);
2107 srvstr_get_path_wcard(inbuf, name, smb_buf(inbuf) + 1, sizeof(name), 0, STR_TERMINATE, &status, &path_contains_wcard);
2108 if (!NT_STATUS_IS_OK(status)) {
2109 END_PROFILE(SMBunlink);
2110 return ERROR_NT(status);
2113 status = resolve_dfspath_wcard(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, name, &path_contains_wcard);
2114 if (!NT_STATUS_IS_OK(status)) {
2115 END_PROFILE(SMBunlink);
2116 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2117 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
2119 return ERROR_NT(status);
2122 DEBUG(3,("reply_unlink : %s\n",name));
2124 status = unlink_internals(conn, dirtype, name, path_contains_wcard,
2125 True);
2126 if (!NT_STATUS_IS_OK(status)) {
2127 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
2128 /* We have re-scheduled this call. */
2129 return -1;
2131 return ERROR_NT(status);
2134 outsize = set_message(inbuf,outbuf,0,0,False);
2136 END_PROFILE(SMBunlink);
2137 return outsize;
2140 /****************************************************************************
2141 Fail for readbraw.
2142 ****************************************************************************/
2144 static void fail_readraw(void)
2146 pstring errstr;
2147 slprintf(errstr, sizeof(errstr)-1, "FAIL ! reply_readbraw: socket write fail (%s)",
2148 strerror(errno) );
2149 exit_server_cleanly(errstr);
2152 #if defined(WITH_SENDFILE)
2153 /****************************************************************************
2154 Fake (read/write) sendfile. Returns -1 on read or write fail.
2155 ****************************************************************************/
2157 static ssize_t fake_sendfile(files_struct *fsp, SMB_OFF_T startpos, size_t nread, char *buf, int bufsize)
2159 ssize_t ret=0;
2161 /* Paranioa check... */
2162 if (nread > bufsize) {
2163 fail_readraw();
2166 if (nread > 0) {
2167 ret = read_file(fsp,buf,startpos,nread);
2168 if (ret == -1) {
2169 return -1;
2173 /* If we had a short read, fill with zeros. */
2174 if (ret < nread) {
2175 memset(buf, '\0', nread - ret);
2178 if (write_data(smbd_server_fd(),buf,nread) != nread) {
2179 return -1;
2182 return (ssize_t)nread;
2184 #endif
2186 /****************************************************************************
2187 Use sendfile in readbraw.
2188 ****************************************************************************/
2190 void send_file_readbraw(connection_struct *conn, files_struct *fsp, SMB_OFF_T startpos, size_t nread,
2191 ssize_t mincount, char *outbuf, int out_buffsize)
2193 ssize_t ret=0;
2195 #if defined(WITH_SENDFILE)
2197 * We can only use sendfile on a non-chained packet
2198 * but we can use on a non-oplocked file. tridge proved this
2199 * on a train in Germany :-). JRA.
2200 * reply_readbraw has already checked the length.
2203 if ( (chain_size == 0) && (nread > 0) &&
2204 (fsp->wcp == NULL) && lp_use_sendfile(SNUM(conn)) ) {
2205 DATA_BLOB header;
2207 _smb_setlen(outbuf,nread);
2208 header.data = (uint8 *)outbuf;
2209 header.length = 4;
2210 header.free = NULL;
2212 if ( SMB_VFS_SENDFILE( smbd_server_fd(), fsp, fsp->fh->fd, &header, startpos, nread) == -1) {
2213 /* Returning ENOSYS means no data at all was sent. Do this as a normal read. */
2214 if (errno == ENOSYS) {
2215 goto normal_readbraw;
2219 * Special hack for broken Linux with no working sendfile. If we
2220 * return EINTR we sent the header but not the rest of the data.
2221 * Fake this up by doing read/write calls.
2223 if (errno == EINTR) {
2224 /* Ensure we don't do this again. */
2225 set_use_sendfile(SNUM(conn), False);
2226 DEBUG(0,("send_file_readbraw: sendfile not available. Faking..\n"));
2228 if (fake_sendfile(fsp, startpos, nread, outbuf + 4, out_buffsize - 4) == -1) {
2229 DEBUG(0,("send_file_readbraw: fake_sendfile failed for file %s (%s).\n",
2230 fsp->fsp_name, strerror(errno) ));
2231 exit_server_cleanly("send_file_readbraw fake_sendfile failed");
2233 return;
2236 DEBUG(0,("send_file_readbraw: sendfile failed for file %s (%s). Terminating\n",
2237 fsp->fsp_name, strerror(errno) ));
2238 exit_server_cleanly("send_file_readbraw sendfile failed");
2243 normal_readbraw:
2245 #endif
2247 if (nread > 0) {
2248 ret = read_file(fsp,outbuf+4,startpos,nread);
2249 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2250 if (ret < mincount)
2251 ret = 0;
2252 #else
2253 if (ret < nread)
2254 ret = 0;
2255 #endif
2258 _smb_setlen(outbuf,ret);
2259 if (write_data(smbd_server_fd(),outbuf,4+ret) != 4+ret)
2260 fail_readraw();
2263 /****************************************************************************
2264 Reply to a readbraw (core+ protocol).
2265 ****************************************************************************/
2267 int reply_readbraw(connection_struct *conn, char *inbuf, char *outbuf, int dum_size, int out_buffsize)
2269 ssize_t maxcount,mincount;
2270 size_t nread = 0;
2271 SMB_OFF_T startpos;
2272 char *header = outbuf;
2273 files_struct *fsp;
2274 START_PROFILE(SMBreadbraw);
2276 if (srv_is_signing_active()) {
2277 exit_server_cleanly("reply_readbraw: SMB signing is active - raw reads/writes are disallowed.");
2281 * Special check if an oplock break has been issued
2282 * and the readraw request croses on the wire, we must
2283 * return a zero length response here.
2286 fsp = file_fsp(inbuf,smb_vwv0);
2288 if (!FNUM_OK(fsp,conn) || !fsp->can_read) {
2290 * fsp could be NULL here so use the value from the packet. JRA.
2292 DEBUG(3,("fnum %d not open in readbraw - cache prime?\n",(int)SVAL(inbuf,smb_vwv0)));
2293 _smb_setlen(header,0);
2294 if (write_data(smbd_server_fd(),header,4) != 4)
2295 fail_readraw();
2296 END_PROFILE(SMBreadbraw);
2297 return(-1);
2300 CHECK_FSP(fsp,conn);
2302 flush_write_cache(fsp, READRAW_FLUSH);
2304 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
2305 if(CVAL(inbuf,smb_wct) == 10) {
2307 * This is a large offset (64 bit) read.
2309 #ifdef LARGE_SMB_OFF_T
2311 startpos |= (((SMB_OFF_T)IVAL(inbuf,smb_vwv8)) << 32);
2313 #else /* !LARGE_SMB_OFF_T */
2316 * Ensure we haven't been sent a >32 bit offset.
2319 if(IVAL(inbuf,smb_vwv8) != 0) {
2320 DEBUG(0,("readbraw - large offset (%x << 32) used and we don't support \
2321 64 bit offsets.\n", (unsigned int)IVAL(inbuf,smb_vwv8) ));
2322 _smb_setlen(header,0);
2323 if (write_data(smbd_server_fd(),header,4) != 4)
2324 fail_readraw();
2325 END_PROFILE(SMBreadbraw);
2326 return(-1);
2329 #endif /* LARGE_SMB_OFF_T */
2331 if(startpos < 0) {
2332 DEBUG(0,("readbraw - negative 64 bit readraw offset (%.0f) !\n", (double)startpos ));
2333 _smb_setlen(header,0);
2334 if (write_data(smbd_server_fd(),header,4) != 4)
2335 fail_readraw();
2336 END_PROFILE(SMBreadbraw);
2337 return(-1);
2340 maxcount = (SVAL(inbuf,smb_vwv3) & 0xFFFF);
2341 mincount = (SVAL(inbuf,smb_vwv4) & 0xFFFF);
2343 /* ensure we don't overrun the packet size */
2344 maxcount = MIN(65535,maxcount);
2346 if (!is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
2347 SMB_STRUCT_STAT st;
2348 SMB_OFF_T size = 0;
2350 if (SMB_VFS_FSTAT(fsp,fsp->fh->fd,&st) == 0) {
2351 size = st.st_size;
2354 if (startpos >= size) {
2355 nread = 0;
2356 } else {
2357 nread = MIN(maxcount,(size - startpos));
2361 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2362 if (nread < mincount)
2363 nread = 0;
2364 #endif
2366 DEBUG( 3, ( "readbraw fnum=%d start=%.0f max=%lu min=%lu nread=%lu\n", fsp->fnum, (double)startpos,
2367 (unsigned long)maxcount, (unsigned long)mincount, (unsigned long)nread ) );
2369 send_file_readbraw(conn, fsp, startpos, nread, mincount, outbuf, out_buffsize);
2371 DEBUG(5,("readbraw finished\n"));
2372 END_PROFILE(SMBreadbraw);
2373 return -1;
2376 #undef DBGC_CLASS
2377 #define DBGC_CLASS DBGC_LOCKING
2379 /****************************************************************************
2380 Reply to a lockread (core+ protocol).
2381 ****************************************************************************/
2383 int reply_lockread(connection_struct *conn, char *inbuf,char *outbuf, int length, int dum_buffsiz)
2385 ssize_t nread = -1;
2386 char *data;
2387 int outsize = 0;
2388 SMB_OFF_T startpos;
2389 size_t numtoread;
2390 NTSTATUS status;
2391 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2392 struct byte_range_lock *br_lck = NULL;
2393 START_PROFILE(SMBlockread);
2395 CHECK_FSP(fsp,conn);
2396 if (!CHECK_READ(fsp,inbuf)) {
2397 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2400 release_level_2_oplocks_on_change(fsp);
2402 numtoread = SVAL(inbuf,smb_vwv1);
2403 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2405 outsize = set_message(inbuf,outbuf,5,3,True);
2406 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
2407 data = smb_buf(outbuf) + 3;
2410 * NB. Discovered by Menny Hamburger at Mainsoft. This is a core+
2411 * protocol request that predates the read/write lock concept.
2412 * Thus instead of asking for a read lock here we need to ask
2413 * for a write lock. JRA.
2414 * Note that the requested lock size is unaffected by max_recv.
2417 br_lck = do_lock(fsp,
2418 (uint32)SVAL(inbuf,smb_pid),
2419 (SMB_BIG_UINT)numtoread,
2420 (SMB_BIG_UINT)startpos,
2421 WRITE_LOCK,
2422 WINDOWS_LOCK,
2423 False, /* Non-blocking lock. */
2424 &status);
2425 TALLOC_FREE(br_lck);
2427 if (NT_STATUS_V(status)) {
2428 END_PROFILE(SMBlockread);
2429 return ERROR_NT(status);
2433 * However the requested READ size IS affected by max_recv. Insanity.... JRA.
2436 if (numtoread > max_recv) {
2437 DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u). \
2438 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
2439 (unsigned int)numtoread, (unsigned int)max_recv ));
2440 numtoread = MIN(numtoread,max_recv);
2442 nread = read_file(fsp,data,startpos,numtoread);
2444 if (nread < 0) {
2445 END_PROFILE(SMBlockread);
2446 return(UNIXERROR(ERRDOS,ERRnoaccess));
2449 outsize += nread;
2450 SSVAL(outbuf,smb_vwv0,nread);
2451 SSVAL(outbuf,smb_vwv5,nread+3);
2452 SSVAL(smb_buf(outbuf),1,nread);
2454 DEBUG(3,("lockread fnum=%d num=%d nread=%d\n",
2455 fsp->fnum, (int)numtoread, (int)nread));
2457 END_PROFILE(SMBlockread);
2458 return(outsize);
2461 #undef DBGC_CLASS
2462 #define DBGC_CLASS DBGC_ALL
2464 /****************************************************************************
2465 Reply to a read.
2466 ****************************************************************************/
2468 int reply_read(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
2470 size_t numtoread;
2471 ssize_t nread = 0;
2472 char *data;
2473 SMB_OFF_T startpos;
2474 int outsize = 0;
2475 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2476 START_PROFILE(SMBread);
2478 CHECK_FSP(fsp,conn);
2479 if (!CHECK_READ(fsp,inbuf)) {
2480 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2483 numtoread = SVAL(inbuf,smb_vwv1);
2484 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2486 outsize = set_message(inbuf,outbuf,5,3,True);
2487 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
2489 * The requested read size cannot be greater than max_recv. JRA.
2491 if (numtoread > max_recv) {
2492 DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u). \
2493 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
2494 (unsigned int)numtoread, (unsigned int)max_recv ));
2495 numtoread = MIN(numtoread,max_recv);
2498 data = smb_buf(outbuf) + 3;
2500 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtoread,(SMB_BIG_UINT)startpos, READ_LOCK)) {
2501 END_PROFILE(SMBread);
2502 return ERROR_DOS(ERRDOS,ERRlock);
2505 if (numtoread > 0)
2506 nread = read_file(fsp,data,startpos,numtoread);
2508 if (nread < 0) {
2509 END_PROFILE(SMBread);
2510 return(UNIXERROR(ERRDOS,ERRnoaccess));
2513 outsize += nread;
2514 SSVAL(outbuf,smb_vwv0,nread);
2515 SSVAL(outbuf,smb_vwv5,nread+3);
2516 SCVAL(smb_buf(outbuf),0,1);
2517 SSVAL(smb_buf(outbuf),1,nread);
2519 DEBUG( 3, ( "read fnum=%d num=%d nread=%d\n",
2520 fsp->fnum, (int)numtoread, (int)nread ) );
2522 END_PROFILE(SMBread);
2523 return(outsize);
2526 /****************************************************************************
2527 Reply to a read and X - possibly using sendfile.
2528 ****************************************************************************/
2530 int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length, int len_outbuf,
2531 files_struct *fsp, SMB_OFF_T startpos, size_t smb_maxcnt)
2533 int outsize = 0;
2534 ssize_t nread = -1;
2535 char *data = smb_buf(outbuf);
2537 #if defined(WITH_SENDFILE)
2539 * We can only use sendfile on a non-chained packet
2540 * but we can use on a non-oplocked file. tridge proved this
2541 * on a train in Germany :-). JRA.
2544 if ((chain_size == 0) && (CVAL(inbuf,smb_vwv0) == 0xFF) &&
2545 lp_use_sendfile(SNUM(conn)) && (fsp->wcp == NULL) ) {
2546 SMB_STRUCT_STAT sbuf;
2547 DATA_BLOB header;
2549 if(SMB_VFS_FSTAT(fsp,fsp->fh->fd, &sbuf) == -1)
2550 return(UNIXERROR(ERRDOS,ERRnoaccess));
2552 if (startpos > sbuf.st_size)
2553 goto normal_read;
2555 if (smb_maxcnt > (sbuf.st_size - startpos))
2556 smb_maxcnt = (sbuf.st_size - startpos);
2558 if (smb_maxcnt == 0)
2559 goto normal_read;
2562 * Set up the packet header before send. We
2563 * assume here the sendfile will work (get the
2564 * correct amount of data).
2567 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be -1. */
2568 SSVAL(outbuf,smb_vwv5,smb_maxcnt);
2569 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
2570 SSVAL(outbuf,smb_vwv7,((smb_maxcnt >> 16) & 1));
2571 SSVAL(smb_buf(outbuf),-2,smb_maxcnt);
2572 SCVAL(outbuf,smb_vwv0,0xFF);
2573 set_message(inbuf,outbuf,12,smb_maxcnt,False);
2574 header.data = (uint8 *)outbuf;
2575 header.length = data - outbuf;
2576 header.free = NULL;
2578 if ((nread = SMB_VFS_SENDFILE( smbd_server_fd(), fsp, fsp->fh->fd, &header, startpos, smb_maxcnt)) == -1) {
2579 /* Returning ENOSYS means no data at all was sent. Do this as a normal read. */
2580 if (errno == ENOSYS) {
2581 goto normal_read;
2585 * Special hack for broken Linux with no working sendfile. If we
2586 * return EINTR we sent the header but not the rest of the data.
2587 * Fake this up by doing read/write calls.
2590 if (errno == EINTR) {
2591 /* Ensure we don't do this again. */
2592 set_use_sendfile(SNUM(conn), False);
2593 DEBUG(0,("send_file_readX: sendfile not available. Faking..\n"));
2595 if ((nread = fake_sendfile(fsp, startpos, smb_maxcnt, data,
2596 len_outbuf - (data-outbuf))) == -1) {
2597 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
2598 fsp->fsp_name, strerror(errno) ));
2599 exit_server_cleanly("send_file_readX: fake_sendfile failed");
2601 DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
2602 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
2603 /* Returning -1 here means successful sendfile. */
2604 return -1;
2607 DEBUG(0,("send_file_readX: sendfile failed for file %s (%s). Terminating\n",
2608 fsp->fsp_name, strerror(errno) ));
2609 exit_server_cleanly("send_file_readX sendfile failed");
2612 DEBUG( 3, ( "send_file_readX: sendfile fnum=%d max=%d nread=%d\n",
2613 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
2614 /* Returning -1 here means successful sendfile. */
2615 return -1;
2618 normal_read:
2620 #endif
2622 nread = read_file(fsp,data,startpos,smb_maxcnt);
2624 if (nread < 0) {
2625 return(UNIXERROR(ERRDOS,ERRnoaccess));
2628 outsize = set_message(inbuf,outbuf,12,nread,False);
2629 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be -1. */
2630 SSVAL(outbuf,smb_vwv5,nread);
2631 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
2632 SSVAL(outbuf,smb_vwv7,((nread >> 16) & 1));
2633 SSVAL(smb_buf(outbuf),-2,nread);
2635 DEBUG( 3, ( "send_file_readX fnum=%d max=%d nread=%d\n",
2636 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
2638 /* Returning the number of bytes we want to send back - including header. */
2639 return outsize;
2642 /****************************************************************************
2643 Reply to a read and X.
2644 ****************************************************************************/
2646 int reply_read_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
2648 files_struct *fsp = file_fsp(inbuf,smb_vwv2);
2649 SMB_OFF_T startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
2650 ssize_t nread = -1;
2651 size_t smb_maxcnt = SVAL(inbuf,smb_vwv5);
2652 #if 0
2653 size_t smb_mincnt = SVAL(inbuf,smb_vwv6);
2654 #endif
2656 START_PROFILE(SMBreadX);
2658 /* If it's an IPC, pass off the pipe handler. */
2659 if (IS_IPC(conn)) {
2660 END_PROFILE(SMBreadX);
2661 return reply_pipe_read_and_X(inbuf,outbuf,length,bufsize);
2664 CHECK_FSP(fsp,conn);
2665 if (!CHECK_READ(fsp,inbuf)) {
2666 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2669 set_message(inbuf,outbuf,12,0,True);
2671 if (global_client_caps & CAP_LARGE_READX) {
2672 if (SVAL(inbuf,smb_vwv7) == 1) {
2673 smb_maxcnt |= (1<<16);
2675 if (smb_maxcnt > BUFFER_SIZE) {
2676 DEBUG(0,("reply_read_and_X - read too large (%u) for reply buffer %u\n",
2677 (unsigned int)smb_maxcnt, (unsigned int)BUFFER_SIZE));
2678 END_PROFILE(SMBreadX);
2679 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2683 if(CVAL(inbuf,smb_wct) == 12) {
2684 #ifdef LARGE_SMB_OFF_T
2686 * This is a large offset (64 bit) read.
2688 startpos |= (((SMB_OFF_T)IVAL(inbuf,smb_vwv10)) << 32);
2690 #else /* !LARGE_SMB_OFF_T */
2693 * Ensure we haven't been sent a >32 bit offset.
2696 if(IVAL(inbuf,smb_vwv10) != 0) {
2697 DEBUG(0,("reply_read_and_X - large offset (%x << 32) used and we don't support \
2698 64 bit offsets.\n", (unsigned int)IVAL(inbuf,smb_vwv10) ));
2699 END_PROFILE(SMBreadX);
2700 return ERROR_DOS(ERRDOS,ERRbadaccess);
2703 #endif /* LARGE_SMB_OFF_T */
2707 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)smb_maxcnt,(SMB_BIG_UINT)startpos, READ_LOCK)) {
2708 END_PROFILE(SMBreadX);
2709 return ERROR_DOS(ERRDOS,ERRlock);
2712 if (schedule_aio_read_and_X(conn, inbuf, outbuf, length, bufsize, fsp, startpos, smb_maxcnt)) {
2713 END_PROFILE(SMBreadX);
2714 return -1;
2717 nread = send_file_readX(conn, inbuf, outbuf, length, bufsize, fsp, startpos, smb_maxcnt);
2718 /* Only call chain_reply if not an error. */
2719 if (nread != -1 && SVAL(outbuf,smb_rcls) == 0) {
2720 nread = chain_reply(inbuf,outbuf,length,bufsize);
2723 END_PROFILE(SMBreadX);
2724 return nread;
2727 /****************************************************************************
2728 Reply to a writebraw (core+ or LANMAN1.0 protocol).
2729 ****************************************************************************/
2731 int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
2733 ssize_t nwritten=0;
2734 ssize_t total_written=0;
2735 size_t numtowrite=0;
2736 size_t tcount;
2737 SMB_OFF_T startpos;
2738 char *data=NULL;
2739 BOOL write_through;
2740 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2741 int outsize = 0;
2742 START_PROFILE(SMBwritebraw);
2744 if (srv_is_signing_active()) {
2745 exit_server_cleanly("reply_writebraw: SMB signing is active - raw reads/writes are disallowed.");
2748 CHECK_FSP(fsp,conn);
2749 if (!CHECK_WRITE(fsp)) {
2750 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2753 tcount = IVAL(inbuf,smb_vwv1);
2754 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
2755 write_through = BITSETW(inbuf+smb_vwv7,0);
2757 /* We have to deal with slightly different formats depending
2758 on whether we are using the core+ or lanman1.0 protocol */
2760 if(Protocol <= PROTOCOL_COREPLUS) {
2761 numtowrite = SVAL(smb_buf(inbuf),-2);
2762 data = smb_buf(inbuf);
2763 } else {
2764 numtowrite = SVAL(inbuf,smb_vwv10);
2765 data = smb_base(inbuf) + SVAL(inbuf, smb_vwv11);
2768 /* force the error type */
2769 SCVAL(inbuf,smb_com,SMBwritec);
2770 SCVAL(outbuf,smb_com,SMBwritec);
2772 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
2773 END_PROFILE(SMBwritebraw);
2774 return(ERROR_DOS(ERRDOS,ERRlock));
2777 if (numtowrite>0)
2778 nwritten = write_file(fsp,data,startpos,numtowrite);
2780 DEBUG(3,("writebraw1 fnum=%d start=%.0f num=%d wrote=%d sync=%d\n",
2781 fsp->fnum, (double)startpos, (int)numtowrite, (int)nwritten, (int)write_through));
2783 if (nwritten < (ssize_t)numtowrite) {
2784 END_PROFILE(SMBwritebraw);
2785 return(UNIXERROR(ERRHRD,ERRdiskfull));
2788 total_written = nwritten;
2790 /* Return a message to the redirector to tell it to send more bytes */
2791 SCVAL(outbuf,smb_com,SMBwritebraw);
2792 SSVALS(outbuf,smb_vwv0,-1);
2793 outsize = set_message(inbuf,outbuf,Protocol>PROTOCOL_COREPLUS?1:0,0,True);
2794 show_msg(outbuf);
2795 if (!send_smb(smbd_server_fd(),outbuf))
2796 exit_server_cleanly("reply_writebraw: send_smb failed.");
2798 /* Now read the raw data into the buffer and write it */
2799 if (read_smb_length(smbd_server_fd(),inbuf,SMB_SECONDARY_WAIT) == -1) {
2800 exit_server_cleanly("secondary writebraw failed");
2803 /* Even though this is not an smb message, smb_len returns the generic length of an smb message */
2804 numtowrite = smb_len(inbuf);
2806 /* Set up outbuf to return the correct return */
2807 outsize = set_message(inbuf,outbuf,1,0,True);
2808 SCVAL(outbuf,smb_com,SMBwritec);
2810 if (numtowrite != 0) {
2812 if (numtowrite > BUFFER_SIZE) {
2813 DEBUG(0,("reply_writebraw: Oversize secondary write raw requested (%u). Terminating\n",
2814 (unsigned int)numtowrite ));
2815 exit_server_cleanly("secondary writebraw failed");
2818 if (tcount > nwritten+numtowrite) {
2819 DEBUG(3,("Client overestimated the write %d %d %d\n",
2820 (int)tcount,(int)nwritten,(int)numtowrite));
2823 if (read_data( smbd_server_fd(), inbuf+4, numtowrite) != numtowrite ) {
2824 DEBUG(0,("reply_writebraw: Oversize secondary write raw read failed (%s). Terminating\n",
2825 strerror(errno) ));
2826 exit_server_cleanly("secondary writebraw failed");
2829 nwritten = write_file(fsp,inbuf+4,startpos+nwritten,numtowrite);
2830 if (nwritten == -1) {
2831 END_PROFILE(SMBwritebraw);
2832 return(UNIXERROR(ERRHRD,ERRdiskfull));
2835 if (nwritten < (ssize_t)numtowrite) {
2836 SCVAL(outbuf,smb_rcls,ERRHRD);
2837 SSVAL(outbuf,smb_err,ERRdiskfull);
2840 if (nwritten > 0)
2841 total_written += nwritten;
2844 SSVAL(outbuf,smb_vwv0,total_written);
2846 sync_file(conn, fsp, write_through);
2848 DEBUG(3,("writebraw2 fnum=%d start=%.0f num=%d wrote=%d\n",
2849 fsp->fnum, (double)startpos, (int)numtowrite,(int)total_written));
2851 /* we won't return a status if write through is not selected - this follows what WfWg does */
2852 END_PROFILE(SMBwritebraw);
2853 if (!write_through && total_written==tcount) {
2855 #if RABBIT_PELLET_FIX
2857 * Fix for "rabbit pellet" mode, trigger an early TCP ack by
2858 * sending a SMBkeepalive. Thanks to DaveCB at Sun for this. JRA.
2860 if (!send_keepalive(smbd_server_fd()))
2861 exit_server_cleanly("reply_writebraw: send of keepalive failed");
2862 #endif
2863 return(-1);
2866 return(outsize);
2869 #undef DBGC_CLASS
2870 #define DBGC_CLASS DBGC_LOCKING
2872 /****************************************************************************
2873 Reply to a writeunlock (core+).
2874 ****************************************************************************/
2876 int reply_writeunlock(connection_struct *conn, char *inbuf,char *outbuf,
2877 int size, int dum_buffsize)
2879 ssize_t nwritten = -1;
2880 size_t numtowrite;
2881 SMB_OFF_T startpos;
2882 char *data;
2883 NTSTATUS status = NT_STATUS_OK;
2884 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2885 int outsize = 0;
2886 START_PROFILE(SMBwriteunlock);
2888 CHECK_FSP(fsp,conn);
2889 if (!CHECK_WRITE(fsp)) {
2890 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2893 numtowrite = SVAL(inbuf,smb_vwv1);
2894 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2895 data = smb_buf(inbuf) + 3;
2897 if (numtowrite && is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
2898 END_PROFILE(SMBwriteunlock);
2899 return ERROR_DOS(ERRDOS,ERRlock);
2902 /* The special X/Open SMB protocol handling of
2903 zero length writes is *NOT* done for
2904 this call */
2905 if(numtowrite == 0) {
2906 nwritten = 0;
2907 } else {
2908 nwritten = write_file(fsp,data,startpos,numtowrite);
2911 sync_file(conn, fsp, False /* write through */);
2913 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
2914 END_PROFILE(SMBwriteunlock);
2915 return(UNIXERROR(ERRHRD,ERRdiskfull));
2918 if (numtowrite) {
2919 status = do_unlock(fsp,
2920 (uint32)SVAL(inbuf,smb_pid),
2921 (SMB_BIG_UINT)numtowrite,
2922 (SMB_BIG_UINT)startpos,
2923 WINDOWS_LOCK);
2925 if (NT_STATUS_V(status)) {
2926 END_PROFILE(SMBwriteunlock);
2927 return ERROR_NT(status);
2931 outsize = set_message(inbuf,outbuf,1,0,True);
2933 SSVAL(outbuf,smb_vwv0,nwritten);
2935 DEBUG(3,("writeunlock fnum=%d num=%d wrote=%d\n",
2936 fsp->fnum, (int)numtowrite, (int)nwritten));
2938 END_PROFILE(SMBwriteunlock);
2939 return outsize;
2942 #undef DBGC_CLASS
2943 #define DBGC_CLASS DBGC_ALL
2945 /****************************************************************************
2946 Reply to a write.
2947 ****************************************************************************/
2949 int reply_write(connection_struct *conn, char *inbuf,char *outbuf,int size,int dum_buffsize)
2951 size_t numtowrite;
2952 ssize_t nwritten = -1;
2953 SMB_OFF_T startpos;
2954 char *data;
2955 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2956 int outsize = 0;
2957 START_PROFILE(SMBwrite);
2959 /* If it's an IPC, pass off the pipe handler. */
2960 if (IS_IPC(conn)) {
2961 END_PROFILE(SMBwrite);
2962 return reply_pipe_write(inbuf,outbuf,size,dum_buffsize);
2965 CHECK_FSP(fsp,conn);
2966 if (!CHECK_WRITE(fsp)) {
2967 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2970 numtowrite = SVAL(inbuf,smb_vwv1);
2971 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2972 data = smb_buf(inbuf) + 3;
2974 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
2975 END_PROFILE(SMBwrite);
2976 return ERROR_DOS(ERRDOS,ERRlock);
2980 * X/Open SMB protocol says that if smb_vwv1 is
2981 * zero then the file size should be extended or
2982 * truncated to the size given in smb_vwv[2-3].
2985 if(numtowrite == 0) {
2987 * This is actually an allocate call, and set EOF. JRA.
2989 nwritten = vfs_allocate_file_space(fsp, (SMB_OFF_T)startpos);
2990 if (nwritten < 0) {
2991 END_PROFILE(SMBwrite);
2992 return ERROR_NT(NT_STATUS_DISK_FULL);
2994 nwritten = vfs_set_filelen(fsp, (SMB_OFF_T)startpos);
2995 if (nwritten < 0) {
2996 END_PROFILE(SMBwrite);
2997 return ERROR_NT(NT_STATUS_DISK_FULL);
2999 } else
3000 nwritten = write_file(fsp,data,startpos,numtowrite);
3002 sync_file(conn, fsp, False);
3004 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
3005 END_PROFILE(SMBwrite);
3006 return(UNIXERROR(ERRHRD,ERRdiskfull));
3009 outsize = set_message(inbuf,outbuf,1,0,True);
3011 SSVAL(outbuf,smb_vwv0,nwritten);
3013 if (nwritten < (ssize_t)numtowrite) {
3014 SCVAL(outbuf,smb_rcls,ERRHRD);
3015 SSVAL(outbuf,smb_err,ERRdiskfull);
3018 DEBUG(3,("write fnum=%d num=%d wrote=%d\n", fsp->fnum, (int)numtowrite, (int)nwritten));
3020 END_PROFILE(SMBwrite);
3021 return(outsize);
3024 /****************************************************************************
3025 Reply to a write and X.
3026 ****************************************************************************/
3028 int reply_write_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
3030 files_struct *fsp = file_fsp(inbuf,smb_vwv2);
3031 SMB_OFF_T startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
3032 size_t numtowrite = SVAL(inbuf,smb_vwv10);
3033 BOOL write_through = BITSETW(inbuf+smb_vwv7,0);
3034 ssize_t nwritten = -1;
3035 unsigned int smb_doff = SVAL(inbuf,smb_vwv11);
3036 unsigned int smblen = smb_len(inbuf);
3037 char *data;
3038 BOOL large_writeX = ((CVAL(inbuf,smb_wct) == 14) && (smblen > 0xFFFF));
3039 START_PROFILE(SMBwriteX);
3041 /* If it's an IPC, pass off the pipe handler. */
3042 if (IS_IPC(conn)) {
3043 END_PROFILE(SMBwriteX);
3044 return reply_pipe_write_and_X(inbuf,outbuf,length,bufsize);
3047 CHECK_FSP(fsp,conn);
3048 if (!CHECK_WRITE(fsp)) {
3049 return(ERROR_DOS(ERRDOS,ERRbadaccess));
3052 set_message(inbuf,outbuf,6,0,True);
3054 /* Deal with possible LARGE_WRITEX */
3055 if (large_writeX) {
3056 numtowrite |= ((((size_t)SVAL(inbuf,smb_vwv9)) & 1 )<<16);
3059 if(smb_doff > smblen || (smb_doff + numtowrite > smblen)) {
3060 END_PROFILE(SMBwriteX);
3061 return ERROR_DOS(ERRDOS,ERRbadmem);
3064 data = smb_base(inbuf) + smb_doff;
3066 if(CVAL(inbuf,smb_wct) == 14) {
3067 #ifdef LARGE_SMB_OFF_T
3069 * This is a large offset (64 bit) write.
3071 startpos |= (((SMB_OFF_T)IVAL(inbuf,smb_vwv12)) << 32);
3073 #else /* !LARGE_SMB_OFF_T */
3076 * Ensure we haven't been sent a >32 bit offset.
3079 if(IVAL(inbuf,smb_vwv12) != 0) {
3080 DEBUG(0,("reply_write_and_X - large offset (%x << 32) used and we don't support \
3081 64 bit offsets.\n", (unsigned int)IVAL(inbuf,smb_vwv12) ));
3082 END_PROFILE(SMBwriteX);
3083 return ERROR_DOS(ERRDOS,ERRbadaccess);
3086 #endif /* LARGE_SMB_OFF_T */
3089 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
3090 END_PROFILE(SMBwriteX);
3091 return ERROR_DOS(ERRDOS,ERRlock);
3094 /* X/Open SMB protocol says that, unlike SMBwrite
3095 if the length is zero then NO truncation is
3096 done, just a write of zero. To truncate a file,
3097 use SMBwrite. */
3099 if(numtowrite == 0) {
3100 nwritten = 0;
3101 } else {
3103 if (schedule_aio_write_and_X(conn, inbuf, outbuf, length, bufsize,
3104 fsp,data,startpos,numtowrite)) {
3105 END_PROFILE(SMBwriteX);
3106 return -1;
3109 nwritten = write_file(fsp,data,startpos,numtowrite);
3112 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
3113 END_PROFILE(SMBwriteX);
3114 return(UNIXERROR(ERRHRD,ERRdiskfull));
3117 SSVAL(outbuf,smb_vwv2,nwritten);
3118 if (large_writeX)
3119 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
3121 if (nwritten < (ssize_t)numtowrite) {
3122 SCVAL(outbuf,smb_rcls,ERRHRD);
3123 SSVAL(outbuf,smb_err,ERRdiskfull);
3126 DEBUG(3,("writeX fnum=%d num=%d wrote=%d\n",
3127 fsp->fnum, (int)numtowrite, (int)nwritten));
3129 sync_file(conn, fsp, write_through);
3131 END_PROFILE(SMBwriteX);
3132 return chain_reply(inbuf,outbuf,length,bufsize);
3135 /****************************************************************************
3136 Reply to a lseek.
3137 ****************************************************************************/
3139 int reply_lseek(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
3141 SMB_OFF_T startpos;
3142 SMB_OFF_T res= -1;
3143 int mode,umode;
3144 int outsize = 0;
3145 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3146 START_PROFILE(SMBlseek);
3148 CHECK_FSP(fsp,conn);
3150 flush_write_cache(fsp, SEEK_FLUSH);
3152 mode = SVAL(inbuf,smb_vwv1) & 3;
3153 /* NB. This doesn't use IVAL_TO_SMB_OFF_T as startpos can be signed in this case. */
3154 startpos = (SMB_OFF_T)IVALS(inbuf,smb_vwv2);
3156 switch (mode) {
3157 case 0:
3158 umode = SEEK_SET;
3159 res = startpos;
3160 break;
3161 case 1:
3162 umode = SEEK_CUR;
3163 res = fsp->fh->pos + startpos;
3164 break;
3165 case 2:
3166 umode = SEEK_END;
3167 break;
3168 default:
3169 umode = SEEK_SET;
3170 res = startpos;
3171 break;
3174 if (umode == SEEK_END) {
3175 if((res = SMB_VFS_LSEEK(fsp,fsp->fh->fd,startpos,umode)) == -1) {
3176 if(errno == EINVAL) {
3177 SMB_OFF_T current_pos = startpos;
3178 SMB_STRUCT_STAT sbuf;
3180 if(SMB_VFS_FSTAT(fsp,fsp->fh->fd, &sbuf) == -1) {
3181 END_PROFILE(SMBlseek);
3182 return(UNIXERROR(ERRDOS,ERRnoaccess));
3185 current_pos += sbuf.st_size;
3186 if(current_pos < 0)
3187 res = SMB_VFS_LSEEK(fsp,fsp->fh->fd,0,SEEK_SET);
3191 if(res == -1) {
3192 END_PROFILE(SMBlseek);
3193 return(UNIXERROR(ERRDOS,ERRnoaccess));
3197 fsp->fh->pos = res;
3199 outsize = set_message(inbuf,outbuf,2,0,True);
3200 SIVAL(outbuf,smb_vwv0,res);
3202 DEBUG(3,("lseek fnum=%d ofs=%.0f newpos = %.0f mode=%d\n",
3203 fsp->fnum, (double)startpos, (double)res, mode));
3205 END_PROFILE(SMBlseek);
3206 return(outsize);
3209 /****************************************************************************
3210 Reply to a flush.
3211 ****************************************************************************/
3213 int reply_flush(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
3215 int outsize = set_message(inbuf,outbuf,0,0,False);
3216 uint16 fnum = SVAL(inbuf,smb_vwv0);
3217 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3218 START_PROFILE(SMBflush);
3220 if (fnum != 0xFFFF)
3221 CHECK_FSP(fsp,conn);
3223 if (!fsp) {
3224 file_sync_all(conn);
3225 } else {
3226 sync_file(conn,fsp, True);
3229 DEBUG(3,("flush\n"));
3230 END_PROFILE(SMBflush);
3231 return(outsize);
3234 /****************************************************************************
3235 Reply to a exit.
3236 conn POINTER CAN BE NULL HERE !
3237 ****************************************************************************/
3239 int reply_exit(connection_struct *conn,
3240 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3242 int outsize;
3243 START_PROFILE(SMBexit);
3245 file_close_pid(SVAL(inbuf,smb_pid),SVAL(inbuf,smb_uid));
3247 outsize = set_message(inbuf,outbuf,0,0,False);
3249 DEBUG(3,("exit\n"));
3251 END_PROFILE(SMBexit);
3252 return(outsize);
3255 /****************************************************************************
3256 Reply to a close - has to deal with closing a directory opened by NT SMB's.
3257 ****************************************************************************/
3259 int reply_close(connection_struct *conn, char *inbuf,char *outbuf, int size,
3260 int dum_buffsize)
3262 NTSTATUS status = NT_STATUS_OK;
3263 int outsize = 0;
3264 files_struct *fsp = NULL;
3265 START_PROFILE(SMBclose);
3267 outsize = set_message(inbuf,outbuf,0,0,False);
3269 /* If it's an IPC, pass off to the pipe handler. */
3270 if (IS_IPC(conn)) {
3271 END_PROFILE(SMBclose);
3272 return reply_pipe_close(conn, inbuf,outbuf);
3275 fsp = file_fsp(inbuf,smb_vwv0);
3278 * We can only use CHECK_FSP if we know it's not a directory.
3281 if(!fsp || (fsp->conn != conn) || (fsp->vuid != current_user.vuid)) {
3282 END_PROFILE(SMBclose);
3283 return ERROR_DOS(ERRDOS,ERRbadfid);
3286 if(fsp->is_directory) {
3288 * Special case - close NT SMB directory handle.
3290 DEBUG(3,("close directory fnum=%d\n", fsp->fnum));
3291 status = close_file(fsp,NORMAL_CLOSE);
3292 } else {
3294 * Close ordinary file.
3297 DEBUG(3,("close fd=%d fnum=%d (numopen=%d)\n",
3298 fsp->fh->fd, fsp->fnum,
3299 conn->num_files_open));
3302 * Take care of any time sent in the close.
3305 fsp_set_pending_modtime(fsp,
3306 convert_time_t_to_timespec(srv_make_unix_date3(inbuf+smb_vwv1)));
3309 * close_file() returns the unix errno if an error
3310 * was detected on close - normally this is due to
3311 * a disk full error. If not then it was probably an I/O error.
3314 status = close_file(fsp,NORMAL_CLOSE);
3317 if(!NT_STATUS_IS_OK(status)) {
3318 END_PROFILE(SMBclose);
3319 return ERROR_NT(status);
3322 END_PROFILE(SMBclose);
3323 return(outsize);
3326 /****************************************************************************
3327 Reply to a writeclose (Core+ protocol).
3328 ****************************************************************************/
3330 int reply_writeclose(connection_struct *conn,
3331 char *inbuf,char *outbuf, int size, int dum_buffsize)
3333 size_t numtowrite;
3334 ssize_t nwritten = -1;
3335 int outsize = 0;
3336 NTSTATUS close_status = NT_STATUS_OK;
3337 SMB_OFF_T startpos;
3338 char *data;
3339 struct timespec mtime;
3340 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3341 START_PROFILE(SMBwriteclose);
3343 CHECK_FSP(fsp,conn);
3344 if (!CHECK_WRITE(fsp)) {
3345 return(ERROR_DOS(ERRDOS,ERRbadaccess));
3348 numtowrite = SVAL(inbuf,smb_vwv1);
3349 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
3350 mtime = convert_time_t_to_timespec(srv_make_unix_date3(inbuf+smb_vwv4));
3351 data = smb_buf(inbuf) + 1;
3353 if (numtowrite && is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
3354 END_PROFILE(SMBwriteclose);
3355 return ERROR_DOS(ERRDOS,ERRlock);
3358 nwritten = write_file(fsp,data,startpos,numtowrite);
3360 set_filetime(conn, fsp->fsp_name, mtime);
3363 * More insanity. W2K only closes the file if writelen > 0.
3364 * JRA.
3367 if (numtowrite) {
3368 DEBUG(3,("reply_writeclose: zero length write doesn't close file %s\n",
3369 fsp->fsp_name ));
3370 close_status = close_file(fsp,NORMAL_CLOSE);
3373 DEBUG(3,("writeclose fnum=%d num=%d wrote=%d (numopen=%d)\n",
3374 fsp->fnum, (int)numtowrite, (int)nwritten,
3375 conn->num_files_open));
3377 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
3378 END_PROFILE(SMBwriteclose);
3379 return(UNIXERROR(ERRHRD,ERRdiskfull));
3382 if(!NT_STATUS_IS_OK(close_status)) {
3383 END_PROFILE(SMBwriteclose);
3384 return ERROR_NT(close_status);
3387 outsize = set_message(inbuf,outbuf,1,0,True);
3389 SSVAL(outbuf,smb_vwv0,nwritten);
3390 END_PROFILE(SMBwriteclose);
3391 return(outsize);
3394 #undef DBGC_CLASS
3395 #define DBGC_CLASS DBGC_LOCKING
3397 /****************************************************************************
3398 Reply to a lock.
3399 ****************************************************************************/
3401 int reply_lock(connection_struct *conn,
3402 char *inbuf,char *outbuf, int length, int dum_buffsize)
3404 int outsize = set_message(inbuf,outbuf,0,0,False);
3405 SMB_BIG_UINT count,offset;
3406 NTSTATUS status;
3407 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3408 struct byte_range_lock *br_lck = NULL;
3410 START_PROFILE(SMBlock);
3412 CHECK_FSP(fsp,conn);
3414 release_level_2_oplocks_on_change(fsp);
3416 count = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv1);
3417 offset = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv3);
3419 DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
3420 fsp->fh->fd, fsp->fnum, (double)offset, (double)count));
3422 br_lck = do_lock(fsp,
3423 (uint32)SVAL(inbuf,smb_pid),
3424 count,
3425 offset,
3426 WRITE_LOCK,
3427 WINDOWS_LOCK,
3428 False, /* Non-blocking lock. */
3429 &status);
3431 TALLOC_FREE(br_lck);
3433 if (NT_STATUS_V(status)) {
3434 END_PROFILE(SMBlock);
3435 return ERROR_NT(status);
3438 END_PROFILE(SMBlock);
3439 return(outsize);
3442 /****************************************************************************
3443 Reply to a unlock.
3444 ****************************************************************************/
3446 int reply_unlock(connection_struct *conn, char *inbuf,char *outbuf, int size,
3447 int dum_buffsize)
3449 int outsize = set_message(inbuf,outbuf,0,0,False);
3450 SMB_BIG_UINT count,offset;
3451 NTSTATUS status;
3452 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3453 START_PROFILE(SMBunlock);
3455 CHECK_FSP(fsp,conn);
3457 count = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv1);
3458 offset = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv3);
3460 status = do_unlock(fsp,
3461 (uint32)SVAL(inbuf,smb_pid),
3462 count,
3463 offset,
3464 WINDOWS_LOCK);
3466 if (NT_STATUS_V(status)) {
3467 END_PROFILE(SMBunlock);
3468 return ERROR_NT(status);
3471 DEBUG( 3, ( "unlock fd=%d fnum=%d offset=%.0f count=%.0f\n",
3472 fsp->fh->fd, fsp->fnum, (double)offset, (double)count ) );
3474 END_PROFILE(SMBunlock);
3475 return(outsize);
3478 #undef DBGC_CLASS
3479 #define DBGC_CLASS DBGC_ALL
3481 /****************************************************************************
3482 Reply to a tdis.
3483 conn POINTER CAN BE NULL HERE !
3484 ****************************************************************************/
3486 int reply_tdis(connection_struct *conn,
3487 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3489 int outsize = set_message(inbuf,outbuf,0,0,False);
3490 uint16 vuid;
3491 START_PROFILE(SMBtdis);
3493 vuid = SVAL(inbuf,smb_uid);
3495 if (!conn) {
3496 DEBUG(4,("Invalid connection in tdis\n"));
3497 END_PROFILE(SMBtdis);
3498 return ERROR_DOS(ERRSRV,ERRinvnid);
3501 conn->used = False;
3503 close_cnum(conn,vuid);
3505 END_PROFILE(SMBtdis);
3506 return outsize;
3509 /****************************************************************************
3510 Reply to a echo.
3511 conn POINTER CAN BE NULL HERE !
3512 ****************************************************************************/
3514 int reply_echo(connection_struct *conn,
3515 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3517 int smb_reverb = SVAL(inbuf,smb_vwv0);
3518 int seq_num;
3519 unsigned int data_len = smb_buflen(inbuf);
3520 int outsize = set_message(inbuf,outbuf,1,data_len,True);
3521 START_PROFILE(SMBecho);
3523 if (data_len > BUFFER_SIZE) {
3524 DEBUG(0,("reply_echo: data_len too large.\n"));
3525 END_PROFILE(SMBecho);
3526 return -1;
3529 /* copy any incoming data back out */
3530 if (data_len > 0)
3531 memcpy(smb_buf(outbuf),smb_buf(inbuf),data_len);
3533 if (smb_reverb > 100) {
3534 DEBUG(0,("large reverb (%d)?? Setting to 100\n",smb_reverb));
3535 smb_reverb = 100;
3538 for (seq_num =1 ; seq_num <= smb_reverb ; seq_num++) {
3539 SSVAL(outbuf,smb_vwv0,seq_num);
3541 smb_setlen(inbuf,outbuf,outsize - 4);
3543 show_msg(outbuf);
3544 if (!send_smb(smbd_server_fd(),outbuf))
3545 exit_server_cleanly("reply_echo: send_smb failed.");
3548 DEBUG(3,("echo %d times\n", smb_reverb));
3550 smb_echo_count++;
3552 END_PROFILE(SMBecho);
3553 return -1;
3556 /****************************************************************************
3557 Reply to a printopen.
3558 ****************************************************************************/
3560 int reply_printopen(connection_struct *conn,
3561 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3563 int outsize = 0;
3564 files_struct *fsp;
3565 NTSTATUS status;
3567 START_PROFILE(SMBsplopen);
3569 if (!CAN_PRINT(conn)) {
3570 END_PROFILE(SMBsplopen);
3571 return ERROR_DOS(ERRDOS,ERRnoaccess);
3574 /* Open for exclusive use, write only. */
3575 status = print_fsp_open(conn, NULL, &fsp);
3577 if (!NT_STATUS_IS_OK(status)) {
3578 END_PROFILE(SMBsplopen);
3579 return(ERROR_NT(status));
3582 outsize = set_message(inbuf,outbuf,1,0,True);
3583 SSVAL(outbuf,smb_vwv0,fsp->fnum);
3585 DEBUG(3,("openprint fd=%d fnum=%d\n",
3586 fsp->fh->fd, fsp->fnum));
3588 END_PROFILE(SMBsplopen);
3589 return(outsize);
3592 /****************************************************************************
3593 Reply to a printclose.
3594 ****************************************************************************/
3596 int reply_printclose(connection_struct *conn,
3597 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3599 int outsize = set_message(inbuf,outbuf,0,0,False);
3600 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3601 NTSTATUS status;
3602 START_PROFILE(SMBsplclose);
3604 CHECK_FSP(fsp,conn);
3606 if (!CAN_PRINT(conn)) {
3607 END_PROFILE(SMBsplclose);
3608 return ERROR_NT(NT_STATUS_DOS(ERRSRV, ERRerror));
3611 DEBUG(3,("printclose fd=%d fnum=%d\n",
3612 fsp->fh->fd,fsp->fnum));
3614 status = close_file(fsp,NORMAL_CLOSE);
3616 if(!NT_STATUS_IS_OK(status)) {
3617 END_PROFILE(SMBsplclose);
3618 return ERROR_NT(status);
3621 END_PROFILE(SMBsplclose);
3622 return(outsize);
3625 /****************************************************************************
3626 Reply to a printqueue.
3627 ****************************************************************************/
3629 int reply_printqueue(connection_struct *conn,
3630 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3632 int outsize = set_message(inbuf,outbuf,2,3,True);
3633 int max_count = SVAL(inbuf,smb_vwv0);
3634 int start_index = SVAL(inbuf,smb_vwv1);
3635 START_PROFILE(SMBsplretq);
3637 /* we used to allow the client to get the cnum wrong, but that
3638 is really quite gross and only worked when there was only
3639 one printer - I think we should now only accept it if they
3640 get it right (tridge) */
3641 if (!CAN_PRINT(conn)) {
3642 END_PROFILE(SMBsplretq);
3643 return ERROR_DOS(ERRDOS,ERRnoaccess);
3646 SSVAL(outbuf,smb_vwv0,0);
3647 SSVAL(outbuf,smb_vwv1,0);
3648 SCVAL(smb_buf(outbuf),0,1);
3649 SSVAL(smb_buf(outbuf),1,0);
3651 DEBUG(3,("printqueue start_index=%d max_count=%d\n",
3652 start_index, max_count));
3655 print_queue_struct *queue = NULL;
3656 print_status_struct status;
3657 char *p = smb_buf(outbuf) + 3;
3658 int count = print_queue_status(SNUM(conn), &queue, &status);
3659 int num_to_get = ABS(max_count);
3660 int first = (max_count>0?start_index:start_index+max_count+1);
3661 int i;
3663 if (first >= count)
3664 num_to_get = 0;
3665 else
3666 num_to_get = MIN(num_to_get,count-first);
3669 for (i=first;i<first+num_to_get;i++) {
3670 srv_put_dos_date2(p,0,queue[i].time);
3671 SCVAL(p,4,(queue[i].status==LPQ_PRINTING?2:3));
3672 SSVAL(p,5, queue[i].job);
3673 SIVAL(p,7,queue[i].size);
3674 SCVAL(p,11,0);
3675 srvstr_push(outbuf, p+12, queue[i].fs_user, 16, STR_ASCII);
3676 p += 28;
3679 if (count > 0) {
3680 outsize = set_message(inbuf,outbuf,2,28*count+3,False);
3681 SSVAL(outbuf,smb_vwv0,count);
3682 SSVAL(outbuf,smb_vwv1,(max_count>0?first+count:first-1));
3683 SCVAL(smb_buf(outbuf),0,1);
3684 SSVAL(smb_buf(outbuf),1,28*count);
3687 SAFE_FREE(queue);
3689 DEBUG(3,("%d entries returned in queue\n",count));
3692 END_PROFILE(SMBsplretq);
3693 return(outsize);
3696 /****************************************************************************
3697 Reply to a printwrite.
3698 ****************************************************************************/
3700 int reply_printwrite(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3702 int numtowrite;
3703 int outsize = set_message(inbuf,outbuf,0,0,False);
3704 char *data;
3705 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3707 START_PROFILE(SMBsplwr);
3709 if (!CAN_PRINT(conn)) {
3710 END_PROFILE(SMBsplwr);
3711 return ERROR_DOS(ERRDOS,ERRnoaccess);
3714 CHECK_FSP(fsp,conn);
3715 if (!CHECK_WRITE(fsp)) {
3716 return(ERROR_DOS(ERRDOS,ERRbadaccess));
3719 numtowrite = SVAL(smb_buf(inbuf),1);
3720 data = smb_buf(inbuf) + 3;
3722 if (write_file(fsp,data,-1,numtowrite) != numtowrite) {
3723 END_PROFILE(SMBsplwr);
3724 return(UNIXERROR(ERRHRD,ERRdiskfull));
3727 DEBUG( 3, ( "printwrite fnum=%d num=%d\n", fsp->fnum, numtowrite ) );
3729 END_PROFILE(SMBsplwr);
3730 return(outsize);
3733 /****************************************************************************
3734 Reply to a mkdir.
3735 ****************************************************************************/
3737 int reply_mkdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3739 pstring directory;
3740 int outsize;
3741 NTSTATUS status;
3742 SMB_STRUCT_STAT sbuf;
3744 START_PROFILE(SMBmkdir);
3746 srvstr_get_path(inbuf, directory, smb_buf(inbuf) + 1, sizeof(directory), 0, STR_TERMINATE, &status);
3747 if (!NT_STATUS_IS_OK(status)) {
3748 END_PROFILE(SMBmkdir);
3749 return ERROR_NT(status);
3752 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, directory);
3753 if (!NT_STATUS_IS_OK(status)) {
3754 END_PROFILE(SMBmkdir);
3755 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
3756 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
3758 return ERROR_NT(status);
3761 status = unix_convert(conn, directory, False, NULL, &sbuf);
3762 if (!NT_STATUS_IS_OK(status)) {
3763 END_PROFILE(SMBmkdir);
3764 return ERROR_NT(status);
3767 status = check_name(conn, directory);
3768 if (!NT_STATUS_IS_OK(status)) {
3769 END_PROFILE(SMBmkdir);
3770 return ERROR_NT(status);
3773 status = create_directory(conn, directory);
3775 DEBUG(5, ("create_directory returned %s\n", nt_errstr(status)));
3777 if (!NT_STATUS_IS_OK(status)) {
3779 if (!use_nt_status()
3780 && NT_STATUS_EQUAL(status,
3781 NT_STATUS_OBJECT_NAME_COLLISION)) {
3783 * Yes, in the DOS error code case we get a
3784 * ERRDOS:ERRnoaccess here. See BASE-SAMBA3ERROR
3785 * samba4 torture test.
3787 status = NT_STATUS_DOS(ERRDOS, ERRnoaccess);
3790 END_PROFILE(SMBmkdir);
3791 return ERROR_NT(status);
3794 outsize = set_message(inbuf,outbuf,0,0,False);
3796 DEBUG( 3, ( "mkdir %s ret=%d\n", directory, outsize ) );
3798 END_PROFILE(SMBmkdir);
3799 return(outsize);
3802 /****************************************************************************
3803 Static function used by reply_rmdir to delete an entire directory
3804 tree recursively. Return True on ok, False on fail.
3805 ****************************************************************************/
3807 static BOOL recursive_rmdir(connection_struct *conn, char *directory)
3809 const char *dname = NULL;
3810 BOOL ret = True;
3811 long offset = 0;
3812 struct smb_Dir *dir_hnd = OpenDir(conn, directory, NULL, 0);
3814 if(dir_hnd == NULL)
3815 return False;
3817 while((dname = ReadDirName(dir_hnd, &offset))) {
3818 pstring fullname;
3819 SMB_STRUCT_STAT st;
3821 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
3822 continue;
3824 if (!is_visible_file(conn, directory, dname, &st, False))
3825 continue;
3827 /* Construct the full name. */
3828 if(strlen(directory) + strlen(dname) + 1 >= sizeof(fullname)) {
3829 errno = ENOMEM;
3830 ret = False;
3831 break;
3834 pstrcpy(fullname, directory);
3835 pstrcat(fullname, "/");
3836 pstrcat(fullname, dname);
3838 if(SMB_VFS_LSTAT(conn,fullname, &st) != 0) {
3839 ret = False;
3840 break;
3843 if(st.st_mode & S_IFDIR) {
3844 if(!recursive_rmdir(conn, fullname)) {
3845 ret = False;
3846 break;
3848 if(SMB_VFS_RMDIR(conn,fullname) != 0) {
3849 ret = False;
3850 break;
3852 } else if(SMB_VFS_UNLINK(conn,fullname) != 0) {
3853 ret = False;
3854 break;
3857 CloseDir(dir_hnd);
3858 return ret;
3861 /****************************************************************************
3862 The internals of the rmdir code - called elsewhere.
3863 ****************************************************************************/
3865 NTSTATUS rmdir_internals(connection_struct *conn, const char *directory)
3867 int ret;
3868 SMB_STRUCT_STAT st;
3870 /* Might be a symlink. */
3871 if(SMB_VFS_LSTAT(conn, directory, &st) != 0) {
3872 return map_nt_error_from_unix(errno);
3875 if (S_ISLNK(st.st_mode)) {
3876 /* Is what it points to a directory ? */
3877 if(SMB_VFS_STAT(conn, directory, &st) != 0) {
3878 return map_nt_error_from_unix(errno);
3880 if (!(S_ISDIR(st.st_mode))) {
3881 return NT_STATUS_NOT_A_DIRECTORY;
3883 ret = SMB_VFS_UNLINK(conn,directory);
3884 } else {
3885 ret = SMB_VFS_RMDIR(conn,directory);
3887 if (ret == 0) {
3888 notify_fname(conn, NOTIFY_ACTION_REMOVED,
3889 FILE_NOTIFY_CHANGE_DIR_NAME,
3890 directory);
3891 return NT_STATUS_OK;
3894 if(((errno == ENOTEMPTY)||(errno == EEXIST)) && lp_veto_files(SNUM(conn))) {
3896 * Check to see if the only thing in this directory are
3897 * vetoed files/directories. If so then delete them and
3898 * retry. If we fail to delete any of them (and we *don't*
3899 * do a recursive delete) then fail the rmdir.
3901 const char *dname;
3902 long dirpos = 0;
3903 struct smb_Dir *dir_hnd = OpenDir(conn, directory, NULL, 0);
3905 if(dir_hnd == NULL) {
3906 errno = ENOTEMPTY;
3907 goto err;
3910 while ((dname = ReadDirName(dir_hnd,&dirpos))) {
3911 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
3912 continue;
3913 if (!is_visible_file(conn, directory, dname, &st, False))
3914 continue;
3915 if(!IS_VETO_PATH(conn, dname)) {
3916 CloseDir(dir_hnd);
3917 errno = ENOTEMPTY;
3918 goto err;
3922 /* We only have veto files/directories. Recursive delete. */
3924 RewindDir(dir_hnd,&dirpos);
3925 while ((dname = ReadDirName(dir_hnd,&dirpos))) {
3926 pstring fullname;
3928 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
3929 continue;
3930 if (!is_visible_file(conn, directory, dname, &st, False))
3931 continue;
3933 /* Construct the full name. */
3934 if(strlen(directory) + strlen(dname) + 1 >= sizeof(fullname)) {
3935 errno = ENOMEM;
3936 break;
3939 pstrcpy(fullname, directory);
3940 pstrcat(fullname, "/");
3941 pstrcat(fullname, dname);
3943 if(SMB_VFS_LSTAT(conn,fullname, &st) != 0)
3944 break;
3945 if(st.st_mode & S_IFDIR) {
3946 if(lp_recursive_veto_delete(SNUM(conn))) {
3947 if(!recursive_rmdir(conn, fullname))
3948 break;
3950 if(SMB_VFS_RMDIR(conn,fullname) != 0)
3951 break;
3952 } else if(SMB_VFS_UNLINK(conn,fullname) != 0)
3953 break;
3955 CloseDir(dir_hnd);
3956 /* Retry the rmdir */
3957 ret = SMB_VFS_RMDIR(conn,directory);
3960 err:
3962 if (ret != 0) {
3963 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
3964 "%s\n", directory,strerror(errno)));
3965 return map_nt_error_from_unix(errno);
3968 notify_fname(conn, NOTIFY_ACTION_REMOVED,
3969 FILE_NOTIFY_CHANGE_DIR_NAME,
3970 directory);
3972 return NT_STATUS_OK;
3975 /****************************************************************************
3976 Reply to a rmdir.
3977 ****************************************************************************/
3979 int reply_rmdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3981 pstring directory;
3982 int outsize = 0;
3983 SMB_STRUCT_STAT sbuf;
3984 NTSTATUS status;
3985 START_PROFILE(SMBrmdir);
3987 srvstr_get_path(inbuf, directory, smb_buf(inbuf) + 1, sizeof(directory), 0, STR_TERMINATE, &status);
3988 if (!NT_STATUS_IS_OK(status)) {
3989 END_PROFILE(SMBrmdir);
3990 return ERROR_NT(status);
3993 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, directory);
3994 if (!NT_STATUS_IS_OK(status)) {
3995 END_PROFILE(SMBrmdir);
3996 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
3997 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
3999 return ERROR_NT(status);
4002 status = unix_convert(conn, directory, False, NULL, &sbuf);
4003 if (!NT_STATUS_IS_OK(status)) {
4004 END_PROFILE(SMBrmdir);
4005 return ERROR_NT(status);
4008 status = check_name(conn, directory);
4009 if (!NT_STATUS_IS_OK(status)) {
4010 END_PROFILE(SMBrmdir);
4011 return ERROR_NT(status);
4014 dptr_closepath(directory,SVAL(inbuf,smb_pid));
4015 status = rmdir_internals(conn, directory);
4016 if (!NT_STATUS_IS_OK(status)) {
4017 END_PROFILE(SMBrmdir);
4018 return ERROR_NT(status);
4021 outsize = set_message(inbuf,outbuf,0,0,False);
4023 DEBUG( 3, ( "rmdir %s\n", directory ) );
4025 END_PROFILE(SMBrmdir);
4026 return(outsize);
4029 /*******************************************************************
4030 Resolve wildcards in a filename rename.
4031 Note that name is in UNIX charset and thus potentially can be more
4032 than fstring buffer (255 bytes) especially in default UTF-8 case.
4033 Therefore, we use pstring inside and all calls should ensure that
4034 name2 is at least pstring-long (they do already)
4035 ********************************************************************/
4037 static BOOL resolve_wildcards(const char *name1, char *name2)
4039 pstring root1,root2;
4040 pstring ext1,ext2;
4041 char *p,*p2, *pname1, *pname2;
4042 int available_space, actual_space;
4044 pname1 = strrchr_m(name1,'/');
4045 pname2 = strrchr_m(name2,'/');
4047 if (!pname1 || !pname2)
4048 return(False);
4050 pstrcpy(root1,pname1);
4051 pstrcpy(root2,pname2);
4052 p = strrchr_m(root1,'.');
4053 if (p) {
4054 *p = 0;
4055 pstrcpy(ext1,p+1);
4056 } else {
4057 pstrcpy(ext1,"");
4059 p = strrchr_m(root2,'.');
4060 if (p) {
4061 *p = 0;
4062 pstrcpy(ext2,p+1);
4063 } else {
4064 pstrcpy(ext2,"");
4067 p = root1;
4068 p2 = root2;
4069 while (*p2) {
4070 if (*p2 == '?') {
4071 *p2 = *p;
4072 p2++;
4073 } else if (*p2 == '*') {
4074 pstrcpy(p2, p);
4075 break;
4076 } else {
4077 p2++;
4079 if (*p)
4080 p++;
4083 p = ext1;
4084 p2 = ext2;
4085 while (*p2) {
4086 if (*p2 == '?') {
4087 *p2 = *p;
4088 p2++;
4089 } else if (*p2 == '*') {
4090 pstrcpy(p2, p);
4091 break;
4092 } else {
4093 p2++;
4095 if (*p)
4096 p++;
4099 available_space = sizeof(pstring) - PTR_DIFF(pname2, name2);
4101 if (ext2[0]) {
4102 actual_space = snprintf(pname2, available_space - 1, "%s.%s", root2, ext2);
4103 if (actual_space >= available_space - 1) {
4104 DEBUG(1,("resolve_wildcards: can't fit resolved name into specified buffer (overrun by %d bytes)\n",
4105 actual_space - available_space));
4107 } else {
4108 pstrcpy_base(pname2, root2, name2);
4111 return(True);
4114 /****************************************************************************
4115 Ensure open files have their names updated. Updated to notify other smbd's
4116 asynchronously.
4117 ****************************************************************************/
4119 static void rename_open_files(connection_struct *conn, struct share_mode_lock *lck,
4120 SMB_DEV_T dev, SMB_INO_T inode, const char *newname)
4122 files_struct *fsp;
4123 BOOL did_rename = False;
4125 for(fsp = file_find_di_first(dev, inode); fsp; fsp = file_find_di_next(fsp)) {
4126 /* fsp_name is a relative path under the fsp. To change this for other
4127 sharepaths we need to manipulate relative paths. */
4128 /* TODO - create the absolute path and manipulate the newname
4129 relative to the sharepath. */
4130 if (fsp->conn != conn) {
4131 continue;
4133 DEBUG(10,("rename_open_files: renaming file fnum %d (dev = %x, inode = %.0f) from %s -> %s\n",
4134 fsp->fnum, (unsigned int)fsp->dev, (double)fsp->inode,
4135 fsp->fsp_name, newname ));
4136 string_set(&fsp->fsp_name, newname);
4137 did_rename = True;
4140 if (!did_rename) {
4141 DEBUG(10,("rename_open_files: no open files on dev %x, inode %.0f for %s\n",
4142 (unsigned int)dev, (double)inode, newname ));
4145 /* Send messages to all smbd's (not ourself) that the name has changed. */
4146 rename_share_filename(lck, conn->connectpath, newname);
4149 /****************************************************************************
4150 We need to check if the source path is a parent directory of the destination
4151 (ie. a rename of /foo/bar/baz -> /foo/bar/baz/bibble/bobble. If so we must
4152 refuse the rename with a sharing violation. Under UNIX the above call can
4153 *succeed* if /foo/bar/baz is a symlink to another area in the share. We
4154 probably need to check that the client is a Windows one before disallowing
4155 this as a UNIX client (one with UNIX extensions) can know the source is a
4156 symlink and make this decision intelligently. Found by an excellent bug
4157 report from <AndyLiebman@aol.com>.
4158 ****************************************************************************/
4160 static BOOL rename_path_prefix_equal(const char *src, const char *dest)
4162 const char *psrc = src;
4163 const char *pdst = dest;
4164 size_t slen;
4166 if (psrc[0] == '.' && psrc[1] == '/') {
4167 psrc += 2;
4169 if (pdst[0] == '.' && pdst[1] == '/') {
4170 pdst += 2;
4172 if ((slen = strlen(psrc)) > strlen(pdst)) {
4173 return False;
4175 return ((memcmp(psrc, pdst, slen) == 0) && pdst[slen] == '/');
4178 /****************************************************************************
4179 Rename an open file - given an fsp.
4180 ****************************************************************************/
4182 NTSTATUS rename_internals_fsp(connection_struct *conn, files_struct *fsp, pstring newname, uint32 attrs, BOOL replace_if_exists)
4184 SMB_STRUCT_STAT sbuf;
4185 pstring newname_last_component;
4186 NTSTATUS status = NT_STATUS_OK;
4187 BOOL dest_exists;
4188 struct share_mode_lock *lck = NULL;
4190 ZERO_STRUCT(sbuf);
4192 status = unix_convert(conn, newname, False, newname_last_component, &sbuf);
4193 if (!NT_STATUS_IS_OK(status)) {
4194 return status;
4197 status = check_name(conn, newname);
4198 if (!NT_STATUS_IS_OK(status)) {
4199 return status;
4202 /* Ensure newname contains a '/' */
4203 if(strrchr_m(newname,'/') == 0) {
4204 pstring tmpstr;
4206 pstrcpy(tmpstr, "./");
4207 pstrcat(tmpstr, newname);
4208 pstrcpy(newname, tmpstr);
4212 * Check for special case with case preserving and not
4213 * case sensitive. If the old last component differs from the original
4214 * last component only by case, then we should allow
4215 * the rename (user is trying to change the case of the
4216 * filename).
4219 if((conn->case_sensitive == False) && (conn->case_preserve == True) &&
4220 strequal(newname, fsp->fsp_name)) {
4221 char *p;
4222 pstring newname_modified_last_component;
4225 * Get the last component of the modified name.
4226 * Note that we guarantee that newname contains a '/'
4227 * character above.
4229 p = strrchr_m(newname,'/');
4230 pstrcpy(newname_modified_last_component,p+1);
4232 if(strcsequal(newname_modified_last_component,
4233 newname_last_component) == False) {
4235 * Replace the modified last component with
4236 * the original.
4238 pstrcpy(p+1, newname_last_component);
4243 * If the src and dest names are identical - including case,
4244 * don't do the rename, just return success.
4247 if (strcsequal(fsp->fsp_name, newname)) {
4248 DEBUG(3,("rename_internals_fsp: identical names in rename %s - returning success\n",
4249 newname));
4250 return NT_STATUS_OK;
4253 dest_exists = vfs_object_exist(conn,newname,NULL);
4255 if(!replace_if_exists && dest_exists) {
4256 DEBUG(3,("rename_internals_fsp: dest exists doing rename %s -> %s\n",
4257 fsp->fsp_name,newname));
4258 return NT_STATUS_OBJECT_NAME_COLLISION;
4261 status = can_rename(conn,newname,attrs,&sbuf);
4263 if (dest_exists && !NT_STATUS_IS_OK(status)) {
4264 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4265 nt_errstr(status), fsp->fsp_name,newname));
4266 if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION))
4267 status = NT_STATUS_ACCESS_DENIED;
4268 return status;
4271 if (rename_path_prefix_equal(fsp->fsp_name, newname)) {
4272 return NT_STATUS_ACCESS_DENIED;
4275 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
4277 if(SMB_VFS_RENAME(conn,fsp->fsp_name, newname) == 0) {
4278 DEBUG(3,("rename_internals_fsp: succeeded doing rename on %s -> %s\n",
4279 fsp->fsp_name,newname));
4280 rename_open_files(conn, lck, fsp->dev, fsp->inode, newname);
4281 TALLOC_FREE(lck);
4282 return NT_STATUS_OK;
4285 TALLOC_FREE(lck);
4287 if (errno == ENOTDIR || errno == EISDIR) {
4288 status = NT_STATUS_OBJECT_NAME_COLLISION;
4289 } else {
4290 status = map_nt_error_from_unix(errno);
4293 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
4294 nt_errstr(status), fsp->fsp_name,newname));
4296 return status;
4300 * Do the notify calls from a rename
4303 static void notify_rename(connection_struct *conn, BOOL is_dir,
4304 const char *oldpath, const char *newpath)
4306 char *olddir, *newdir;
4307 const char *oldname, *newname;
4308 uint32 mask;
4310 mask = is_dir ? FILE_NOTIFY_CHANGE_DIR_NAME
4311 : FILE_NOTIFY_CHANGE_FILE_NAME;
4313 if (!parent_dirname_talloc(NULL, oldpath, &olddir, &oldname)
4314 || !parent_dirname_talloc(NULL, newpath, &newdir, &newname)) {
4315 TALLOC_FREE(olddir);
4316 return;
4319 if (strcmp(olddir, newdir) == 0) {
4320 notify_fname(conn, NOTIFY_ACTION_OLD_NAME, mask, oldpath);
4321 notify_fname(conn, NOTIFY_ACTION_NEW_NAME, mask, newpath);
4323 else {
4324 notify_fname(conn, NOTIFY_ACTION_REMOVED, mask, oldpath);
4325 notify_fname(conn, NOTIFY_ACTION_ADDED, mask, newpath);
4327 TALLOC_FREE(olddir);
4328 TALLOC_FREE(newdir);
4330 /* this is a strange one. w2k3 gives an additional event for
4331 CHANGE_ATTRIBUTES and CHANGE_CREATION on the new file when renaming
4332 files, but not directories */
4333 if (!is_dir) {
4334 notify_fname(conn, NOTIFY_ACTION_MODIFIED,
4335 FILE_NOTIFY_CHANGE_ATTRIBUTES
4336 |FILE_NOTIFY_CHANGE_CREATION,
4337 newpath);
4341 /****************************************************************************
4342 The guts of the rename command, split out so it may be called by the NT SMB
4343 code.
4344 ****************************************************************************/
4346 NTSTATUS rename_internals(connection_struct *conn,
4347 pstring name,
4348 pstring newname,
4349 uint32 attrs,
4350 BOOL replace_if_exists,
4351 BOOL src_has_wild,
4352 BOOL dest_has_wild)
4354 pstring directory;
4355 pstring mask;
4356 pstring last_component_src;
4357 pstring last_component_dest;
4358 char *p;
4359 int count=0;
4360 NTSTATUS status = NT_STATUS_OK;
4361 SMB_STRUCT_STAT sbuf1, sbuf2;
4362 struct share_mode_lock *lck = NULL;
4363 struct smb_Dir *dir_hnd = NULL;
4364 const char *dname;
4365 long offset = 0;
4366 pstring destname;
4368 *directory = *mask = 0;
4370 ZERO_STRUCT(sbuf1);
4371 ZERO_STRUCT(sbuf2);
4373 status = unix_convert(conn, name, src_has_wild, last_component_src, &sbuf1);
4374 if (!NT_STATUS_IS_OK(status)) {
4375 return status;
4378 status = unix_convert(conn, newname, dest_has_wild, last_component_dest, &sbuf2);
4379 if (!NT_STATUS_IS_OK(status)) {
4380 return status;
4384 * Split the old name into directory and last component
4385 * strings. Note that unix_convert may have stripped off a
4386 * leading ./ from both name and newname if the rename is
4387 * at the root of the share. We need to make sure either both
4388 * name and newname contain a / character or neither of them do
4389 * as this is checked in resolve_wildcards().
4392 p = strrchr_m(name,'/');
4393 if (!p) {
4394 pstrcpy(directory,".");
4395 pstrcpy(mask,name);
4396 } else {
4397 *p = 0;
4398 pstrcpy(directory,name);
4399 pstrcpy(mask,p+1);
4400 *p = '/'; /* Replace needed for exceptional test below. */
4404 * We should only check the mangled cache
4405 * here if unix_convert failed. This means
4406 * that the path in 'mask' doesn't exist
4407 * on the file system and so we need to look
4408 * for a possible mangle. This patch from
4409 * Tine Smukavec <valentin.smukavec@hermes.si>.
4412 if (!VALID_STAT(sbuf1) && mangle_is_mangled(mask, conn->params)) {
4413 mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
4416 if (!src_has_wild) {
4418 * No wildcards - just process the one file.
4420 BOOL is_short_name = mangle_is_8_3(name, True, conn->params);
4422 /* Add a terminating '/' to the directory name. */
4423 pstrcat(directory,"/");
4424 pstrcat(directory,mask);
4426 /* Ensure newname contains a '/' also */
4427 if(strrchr_m(newname,'/') == 0) {
4428 pstring tmpstr;
4430 pstrcpy(tmpstr, "./");
4431 pstrcat(tmpstr, newname);
4432 pstrcpy(newname, tmpstr);
4435 DEBUG(3, ("rename_internals: case_sensitive = %d, "
4436 "case_preserve = %d, short case preserve = %d, "
4437 "directory = %s, newname = %s, "
4438 "last_component_dest = %s, is_8_3 = %d\n",
4439 conn->case_sensitive, conn->case_preserve,
4440 conn->short_case_preserve, directory,
4441 newname, last_component_dest, is_short_name));
4443 /* Ensure the source name is valid for us to access. */
4444 status = check_name(conn, directory);
4445 if (!NT_STATUS_IS_OK(status)) {
4446 return status;
4449 /* The dest name still may have wildcards. */
4450 if (dest_has_wild) {
4451 if (!resolve_wildcards(directory,newname)) {
4452 DEBUG(6, ("rename_internals: resolve_wildcards %s %s failed\n",
4453 directory,newname));
4454 return NT_STATUS_NO_MEMORY;
4459 * Check for special case with case preserving and not
4460 * case sensitive, if directory and newname are identical,
4461 * and the old last component differs from the original
4462 * last component only by case, then we should allow
4463 * the rename (user is trying to change the case of the
4464 * filename).
4466 if((conn->case_sensitive == False) &&
4467 (((conn->case_preserve == True) &&
4468 (is_short_name == False)) ||
4469 ((conn->short_case_preserve == True) &&
4470 (is_short_name == True))) &&
4471 strcsequal(directory, newname)) {
4472 pstring modified_last_component;
4475 * Get the last component of the modified name.
4476 * Note that we guarantee that newname contains a '/'
4477 * character above.
4479 p = strrchr_m(newname,'/');
4480 pstrcpy(modified_last_component,p+1);
4482 if(strcsequal(modified_last_component,
4483 last_component_dest) == False) {
4485 * Replace the modified last component with
4486 * the original.
4488 pstrcpy(p+1, last_component_dest);
4492 /* Ensure the dest name is valid for us to access. */
4493 status = check_name(conn, newname);
4494 if (!NT_STATUS_IS_OK(status)) {
4495 return status;
4499 * The source object must exist.
4502 if (!vfs_object_exist(conn, directory, &sbuf1)) {
4503 DEBUG(3, ("rename_internals: source doesn't exist "
4504 "doing rename %s -> %s\n",
4505 directory,newname));
4507 if (errno == ENOTDIR || errno == EISDIR
4508 || errno == ENOENT) {
4510 * Must return different errors depending on
4511 * whether the parent directory existed or
4512 * not.
4515 p = strrchr_m(directory, '/');
4516 if (!p)
4517 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4518 *p = '\0';
4519 if (vfs_object_exist(conn, directory, NULL))
4520 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4521 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
4523 status = map_nt_error_from_unix(errno);
4524 DEBUG(3, ("rename_internals: Error %s rename %s -> "
4525 "%s\n", nt_errstr(status), directory,
4526 newname));
4528 return status;
4531 status = can_rename(conn,directory,attrs,&sbuf1);
4533 if (!NT_STATUS_IS_OK(status)) {
4534 DEBUG(3,("rename_internals: Error %s rename %s -> "
4535 "%s\n", nt_errstr(status), directory,
4536 newname));
4537 return status;
4541 * If the src and dest names are identical - including case,
4542 * don't do the rename, just return success.
4545 if (strcsequal(directory, newname)) {
4546 rename_open_files(conn, NULL, sbuf1.st_dev,
4547 sbuf1.st_ino, newname);
4548 DEBUG(3, ("rename_internals: identical names in "
4549 "rename %s - returning success\n",
4550 directory));
4551 return NT_STATUS_OK;
4554 if(!replace_if_exists && vfs_object_exist(conn,newname,NULL)) {
4555 DEBUG(3,("rename_internals: dest exists doing "
4556 "rename %s -> %s\n", directory, newname));
4557 return NT_STATUS_OBJECT_NAME_COLLISION;
4560 if (rename_path_prefix_equal(directory, newname)) {
4561 return NT_STATUS_SHARING_VIOLATION;
4564 lck = get_share_mode_lock(NULL, sbuf1.st_dev, sbuf1.st_ino,
4565 NULL, NULL);
4567 if(SMB_VFS_RENAME(conn,directory, newname) == 0) {
4568 DEBUG(3,("rename_internals: succeeded doing rename "
4569 "on %s -> %s\n", directory, newname));
4570 rename_open_files(conn, lck, sbuf1.st_dev,
4571 sbuf1.st_ino, newname);
4572 TALLOC_FREE(lck);
4573 notify_rename(conn, S_ISDIR(sbuf1.st_mode),
4574 directory, newname);
4575 return NT_STATUS_OK;
4578 TALLOC_FREE(lck);
4579 if (errno == ENOTDIR || errno == EISDIR) {
4580 status = NT_STATUS_OBJECT_NAME_COLLISION;
4581 } else {
4582 status = map_nt_error_from_unix(errno);
4585 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4586 nt_errstr(status), directory,newname));
4588 return status;
4592 * Wildcards - process each file that matches.
4594 if (strequal(mask,"????????.???")) {
4595 pstrcpy(mask,"*");
4598 status = check_name(conn, directory);
4599 if (!NT_STATUS_IS_OK(status)) {
4600 return status;
4603 dir_hnd = OpenDir(conn, directory, mask, attrs);
4604 if (dir_hnd == NULL) {
4605 return map_nt_error_from_unix(errno);
4608 status = NT_STATUS_NO_SUCH_FILE;
4610 * Was status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4611 * - gentest fix. JRA
4614 while ((dname = ReadDirName(dir_hnd, &offset))) {
4615 pstring fname;
4616 BOOL sysdir_entry = False;
4618 pstrcpy(fname,dname);
4620 /* Quick check for "." and ".." */
4621 if (fname[0] == '.') {
4622 if (!fname[1] || (fname[1] == '.' && !fname[2])) {
4623 if (attrs & aDIR) {
4624 sysdir_entry = True;
4625 } else {
4626 continue;
4631 if (!is_visible_file(conn, directory, dname, &sbuf1, False)) {
4632 continue;
4635 if(!mask_match(fname, mask, conn->case_sensitive)) {
4636 continue;
4639 if (sysdir_entry) {
4640 status = NT_STATUS_OBJECT_NAME_INVALID;
4641 break;
4644 status = NT_STATUS_ACCESS_DENIED;
4645 slprintf(fname, sizeof(fname)-1, "%s/%s", directory, dname);
4647 /* Ensure the source name is valid for us to access. */
4648 status = check_name(conn, fname);
4649 if (!NT_STATUS_IS_OK(status)) {
4650 return status;
4653 if (!vfs_object_exist(conn, fname, &sbuf1)) {
4654 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4655 DEBUG(6, ("rename %s failed. Error %s\n",
4656 fname, nt_errstr(status)));
4657 continue;
4659 status = can_rename(conn,fname,attrs,&sbuf1);
4660 if (!NT_STATUS_IS_OK(status)) {
4661 DEBUG(6, ("rename %s refused\n", fname));
4662 continue;
4664 pstrcpy(destname,newname);
4666 if (!resolve_wildcards(fname,destname)) {
4667 DEBUG(6, ("resolve_wildcards %s %s failed\n",
4668 fname, destname));
4669 continue;
4672 /* Ensure the dest name is valid for us to access. */
4673 status = check_name(conn, destname);
4674 if (!NT_STATUS_IS_OK(status)) {
4675 return status;
4678 if (strcsequal(fname,destname)) {
4679 rename_open_files(conn, NULL, sbuf1.st_dev,
4680 sbuf1.st_ino, newname);
4681 DEBUG(3,("rename_internals: identical names "
4682 "in wildcard rename %s - success\n",
4683 fname));
4684 count++;
4685 status = NT_STATUS_OK;
4686 continue;
4689 if (!replace_if_exists && vfs_file_exist(conn,destname, NULL)) {
4690 DEBUG(6,("file_exist %s\n", destname));
4691 status = NT_STATUS_OBJECT_NAME_COLLISION;
4692 continue;
4695 if (rename_path_prefix_equal(fname, destname)) {
4696 return NT_STATUS_SHARING_VIOLATION;
4699 lck = get_share_mode_lock(NULL, sbuf1.st_dev,
4700 sbuf1.st_ino, NULL, NULL);
4702 if (!SMB_VFS_RENAME(conn,fname,destname)) {
4703 rename_open_files(conn, lck, sbuf1.st_dev,
4704 sbuf1.st_ino, newname);
4705 count++;
4706 status = NT_STATUS_OK;
4708 TALLOC_FREE(lck);
4709 DEBUG(3,("rename_internals: doing rename on %s -> "
4710 "%s\n",fname,destname));
4712 CloseDir(dir_hnd);
4714 if (count == 0 && NT_STATUS_IS_OK(status)) {
4715 status = map_nt_error_from_unix(errno);
4718 return status;
4721 /****************************************************************************
4722 Reply to a mv.
4723 ****************************************************************************/
4725 int reply_mv(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
4726 int dum_buffsize)
4728 int outsize = 0;
4729 pstring name;
4730 pstring newname;
4731 char *p;
4732 uint32 attrs = SVAL(inbuf,smb_vwv0);
4733 NTSTATUS status;
4734 BOOL src_has_wcard = False;
4735 BOOL dest_has_wcard = False;
4737 START_PROFILE(SMBmv);
4739 p = smb_buf(inbuf) + 1;
4740 p += srvstr_get_path_wcard(inbuf, name, p, sizeof(name), 0, STR_TERMINATE, &status, &src_has_wcard);
4741 if (!NT_STATUS_IS_OK(status)) {
4742 END_PROFILE(SMBmv);
4743 return ERROR_NT(status);
4745 p++;
4746 p += srvstr_get_path_wcard(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status, &dest_has_wcard);
4747 if (!NT_STATUS_IS_OK(status)) {
4748 END_PROFILE(SMBmv);
4749 return ERROR_NT(status);
4752 status = resolve_dfspath_wcard(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, name, &src_has_wcard);
4753 if (!NT_STATUS_IS_OK(status)) {
4754 END_PROFILE(SMBmv);
4755 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
4756 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
4758 return ERROR_NT(status);
4761 status = resolve_dfspath_wcard(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, newname, &dest_has_wcard);
4762 if (!NT_STATUS_IS_OK(status)) {
4763 END_PROFILE(SMBmv);
4764 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
4765 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
4767 return ERROR_NT(status);
4770 DEBUG(3,("reply_mv : %s -> %s\n",name,newname));
4772 status = rename_internals(conn, name, newname, attrs, False, src_has_wcard, dest_has_wcard);
4773 if (!NT_STATUS_IS_OK(status)) {
4774 END_PROFILE(SMBmv);
4775 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
4776 /* We have re-scheduled this call. */
4777 return -1;
4779 return ERROR_NT(status);
4782 outsize = set_message(inbuf,outbuf,0,0,False);
4784 END_PROFILE(SMBmv);
4785 return(outsize);
4788 /*******************************************************************
4789 Copy a file as part of a reply_copy.
4790 ******************************************************************/
4793 * TODO: check error codes on all callers
4796 NTSTATUS copy_file(connection_struct *conn,
4797 char *src,
4798 char *dest1,
4799 int ofun,
4800 int count,
4801 BOOL target_is_directory)
4803 SMB_STRUCT_STAT src_sbuf, sbuf2;
4804 SMB_OFF_T ret=-1;
4805 files_struct *fsp1,*fsp2;
4806 pstring dest;
4807 uint32 dosattrs;
4808 uint32 new_create_disposition;
4809 NTSTATUS status;
4811 pstrcpy(dest,dest1);
4812 if (target_is_directory) {
4813 char *p = strrchr_m(src,'/');
4814 if (p) {
4815 p++;
4816 } else {
4817 p = src;
4819 pstrcat(dest,"/");
4820 pstrcat(dest,p);
4823 if (!vfs_file_exist(conn,src,&src_sbuf)) {
4824 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4827 if (!target_is_directory && count) {
4828 new_create_disposition = FILE_OPEN;
4829 } else {
4830 if (!map_open_params_to_ntcreate(dest1,0,ofun,
4831 NULL, NULL, &new_create_disposition, NULL)) {
4832 return NT_STATUS_INVALID_PARAMETER;
4836 status = open_file_ntcreate(conn,src,&src_sbuf,
4837 FILE_GENERIC_READ,
4838 FILE_SHARE_READ|FILE_SHARE_WRITE,
4839 FILE_OPEN,
4841 FILE_ATTRIBUTE_NORMAL,
4842 INTERNAL_OPEN_ONLY,
4843 NULL, &fsp1);
4845 if (!NT_STATUS_IS_OK(status)) {
4846 return status;
4849 dosattrs = dos_mode(conn, src, &src_sbuf);
4850 if (SMB_VFS_STAT(conn,dest,&sbuf2) == -1) {
4851 ZERO_STRUCTP(&sbuf2);
4854 status = open_file_ntcreate(conn,dest,&sbuf2,
4855 FILE_GENERIC_WRITE,
4856 FILE_SHARE_READ|FILE_SHARE_WRITE,
4857 new_create_disposition,
4859 dosattrs,
4860 INTERNAL_OPEN_ONLY,
4861 NULL, &fsp2);
4863 if (!NT_STATUS_IS_OK(status)) {
4864 close_file(fsp1,ERROR_CLOSE);
4865 return status;
4868 if ((ofun&3) == 1) {
4869 if(SMB_VFS_LSEEK(fsp2,fsp2->fh->fd,0,SEEK_END) == -1) {
4870 DEBUG(0,("copy_file: error - vfs lseek returned error %s\n", strerror(errno) ));
4872 * Stop the copy from occurring.
4874 ret = -1;
4875 src_sbuf.st_size = 0;
4879 if (src_sbuf.st_size) {
4880 ret = vfs_transfer_file(fsp1, fsp2, src_sbuf.st_size);
4883 close_file(fsp1,NORMAL_CLOSE);
4885 /* Ensure the modtime is set correctly on the destination file. */
4886 fsp_set_pending_modtime( fsp2, get_mtimespec(&src_sbuf));
4889 * As we are opening fsp1 read-only we only expect
4890 * an error on close on fsp2 if we are out of space.
4891 * Thus we don't look at the error return from the
4892 * close of fsp1.
4894 status = close_file(fsp2,NORMAL_CLOSE);
4896 if (!NT_STATUS_IS_OK(status)) {
4897 return status;
4900 if (ret != (SMB_OFF_T)src_sbuf.st_size) {
4901 return NT_STATUS_DISK_FULL;
4904 return NT_STATUS_OK;
4907 /****************************************************************************
4908 Reply to a file copy.
4909 ****************************************************************************/
4911 int reply_copy(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
4913 int outsize = 0;
4914 pstring name;
4915 pstring directory;
4916 pstring mask,newname;
4917 char *p;
4918 int count=0;
4919 int error = ERRnoaccess;
4920 int err = 0;
4921 int tid2 = SVAL(inbuf,smb_vwv0);
4922 int ofun = SVAL(inbuf,smb_vwv1);
4923 int flags = SVAL(inbuf,smb_vwv2);
4924 BOOL target_is_directory=False;
4925 BOOL source_has_wild = False;
4926 BOOL dest_has_wild = False;
4927 SMB_STRUCT_STAT sbuf1, sbuf2;
4928 NTSTATUS status;
4929 START_PROFILE(SMBcopy);
4931 *directory = *mask = 0;
4933 p = smb_buf(inbuf);
4934 p += srvstr_get_path_wcard(inbuf, name, p, sizeof(name), 0, STR_TERMINATE, &status, &source_has_wild);
4935 if (!NT_STATUS_IS_OK(status)) {
4936 END_PROFILE(SMBcopy);
4937 return ERROR_NT(status);
4939 p += srvstr_get_path_wcard(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status, &dest_has_wild);
4940 if (!NT_STATUS_IS_OK(status)) {
4941 END_PROFILE(SMBcopy);
4942 return ERROR_NT(status);
4945 DEBUG(3,("reply_copy : %s -> %s\n",name,newname));
4947 if (tid2 != conn->cnum) {
4948 /* can't currently handle inter share copies XXXX */
4949 DEBUG(3,("Rejecting inter-share copy\n"));
4950 END_PROFILE(SMBcopy);
4951 return ERROR_DOS(ERRSRV,ERRinvdevice);
4954 status = resolve_dfspath_wcard(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, name, &source_has_wild);
4955 if (!NT_STATUS_IS_OK(status)) {
4956 END_PROFILE(SMBcopy);
4957 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
4958 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
4960 return ERROR_NT(status);
4963 status = resolve_dfspath_wcard(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, newname, &dest_has_wild);
4964 if (!NT_STATUS_IS_OK(status)) {
4965 END_PROFILE(SMBcopy);
4966 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
4967 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
4969 return ERROR_NT(status);
4972 status = unix_convert(conn, name, source_has_wild, NULL, &sbuf1);
4973 if (!NT_STATUS_IS_OK(status)) {
4974 END_PROFILE(SMBcopy);
4975 return ERROR_NT(status);
4978 status = unix_convert(conn, newname, dest_has_wild, NULL, &sbuf2);
4979 if (!NT_STATUS_IS_OK(status)) {
4980 END_PROFILE(SMBcopy);
4981 return ERROR_NT(status);
4984 target_is_directory = VALID_STAT_OF_DIR(sbuf2);
4986 if ((flags&1) && target_is_directory) {
4987 END_PROFILE(SMBcopy);
4988 return ERROR_DOS(ERRDOS,ERRbadfile);
4991 if ((flags&2) && !target_is_directory) {
4992 END_PROFILE(SMBcopy);
4993 return ERROR_DOS(ERRDOS,ERRbadpath);
4996 if ((flags&(1<<5)) && VALID_STAT_OF_DIR(sbuf1)) {
4997 /* wants a tree copy! XXXX */
4998 DEBUG(3,("Rejecting tree copy\n"));
4999 END_PROFILE(SMBcopy);
5000 return ERROR_DOS(ERRSRV,ERRerror);
5003 p = strrchr_m(name,'/');
5004 if (!p) {
5005 pstrcpy(directory,"./");
5006 pstrcpy(mask,name);
5007 } else {
5008 *p = 0;
5009 pstrcpy(directory,name);
5010 pstrcpy(mask,p+1);
5014 * We should only check the mangled cache
5015 * here if unix_convert failed. This means
5016 * that the path in 'mask' doesn't exist
5017 * on the file system and so we need to look
5018 * for a possible mangle. This patch from
5019 * Tine Smukavec <valentin.smukavec@hermes.si>.
5022 if (!VALID_STAT(sbuf1) && mangle_is_mangled(mask, conn->params)) {
5023 mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
5026 if (!source_has_wild) {
5027 pstrcat(directory,"/");
5028 pstrcat(directory,mask);
5029 if (dest_has_wild) {
5030 if (!resolve_wildcards(directory,newname)) {
5031 END_PROFILE(SMBcopy);
5032 return ERROR_NT(NT_STATUS_NO_MEMORY);
5036 status = check_name(conn, directory);
5037 if (!NT_STATUS_IS_OK(status)) {
5038 return ERROR_NT(status);
5041 status = check_name(conn, newname);
5042 if (!NT_STATUS_IS_OK(status)) {
5043 return ERROR_NT(status);
5046 status = copy_file(conn,directory,newname,ofun,
5047 count,target_is_directory);
5049 if(!NT_STATUS_IS_OK(status)) {
5050 END_PROFILE(SMBcopy);
5051 return ERROR_NT(status);
5052 } else {
5053 count++;
5055 } else {
5056 struct smb_Dir *dir_hnd = NULL;
5057 const char *dname;
5058 long offset = 0;
5059 pstring destname;
5061 if (strequal(mask,"????????.???"))
5062 pstrcpy(mask,"*");
5064 status = check_name(conn, directory);
5065 if (!NT_STATUS_IS_OK(status)) {
5066 return ERROR_NT(status);
5069 dir_hnd = OpenDir(conn, directory, mask, 0);
5070 if (dir_hnd == NULL) {
5071 status = map_nt_error_from_unix(errno);
5072 return ERROR_NT(status);
5075 error = ERRbadfile;
5077 while ((dname = ReadDirName(dir_hnd, &offset))) {
5078 pstring fname;
5079 pstrcpy(fname,dname);
5081 if (!is_visible_file(conn, directory, dname, &sbuf1, False)) {
5082 continue;
5085 if(!mask_match(fname, mask, conn->case_sensitive)) {
5086 continue;
5089 error = ERRnoaccess;
5090 slprintf(fname,sizeof(fname)-1, "%s/%s",directory,dname);
5091 pstrcpy(destname,newname);
5092 if (!resolve_wildcards(fname,destname)) {
5093 continue;
5096 status = check_name(conn, fname);
5097 if (!NT_STATUS_IS_OK(status)) {
5098 return ERROR_NT(status);
5101 status = check_name(conn, destname);
5102 if (!NT_STATUS_IS_OK(status)) {
5103 return ERROR_NT(status);
5106 DEBUG(3,("reply_copy : doing copy on %s -> %s\n",fname, destname));
5108 status = copy_file(conn,fname,destname,ofun,
5109 count,target_is_directory);
5110 if (NT_STATUS_IS_OK(status)) {
5111 count++;
5114 CloseDir(dir_hnd);
5117 if (count == 0) {
5118 if(err) {
5119 /* Error on close... */
5120 errno = err;
5121 END_PROFILE(SMBcopy);
5122 return(UNIXERROR(ERRHRD,ERRgeneral));
5125 END_PROFILE(SMBcopy);
5126 return ERROR_DOS(ERRDOS,error);
5129 outsize = set_message(inbuf,outbuf,1,0,True);
5130 SSVAL(outbuf,smb_vwv0,count);
5132 END_PROFILE(SMBcopy);
5133 return(outsize);
5136 /****************************************************************************
5137 Reply to a setdir.
5138 ****************************************************************************/
5140 int reply_setdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
5142 int snum;
5143 int outsize = 0;
5144 pstring newdir;
5145 NTSTATUS status;
5147 START_PROFILE(pathworks_setdir);
5149 snum = SNUM(conn);
5150 if (!CAN_SETDIR(snum)) {
5151 END_PROFILE(pathworks_setdir);
5152 return ERROR_DOS(ERRDOS,ERRnoaccess);
5155 srvstr_get_path(inbuf, newdir, smb_buf(inbuf) + 1, sizeof(newdir), 0, STR_TERMINATE, &status);
5156 if (!NT_STATUS_IS_OK(status)) {
5157 END_PROFILE(pathworks_setdir);
5158 return ERROR_NT(status);
5161 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, newdir);
5162 if (!NT_STATUS_IS_OK(status)) {
5163 END_PROFILE(pathworks_setdir);
5164 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
5165 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
5167 return ERROR_NT(status);
5170 if (strlen(newdir) != 0) {
5171 if (!vfs_directory_exist(conn,newdir,NULL)) {
5172 END_PROFILE(pathworks_setdir);
5173 return ERROR_DOS(ERRDOS,ERRbadpath);
5175 set_conn_connectpath(conn,newdir);
5178 outsize = set_message(inbuf,outbuf,0,0,False);
5179 SCVAL(outbuf,smb_reh,CVAL(inbuf,smb_reh));
5181 DEBUG(3,("setdir %s\n", newdir));
5183 END_PROFILE(pathworks_setdir);
5184 return(outsize);
5187 #undef DBGC_CLASS
5188 #define DBGC_CLASS DBGC_LOCKING
5190 /****************************************************************************
5191 Get a lock pid, dealing with large count requests.
5192 ****************************************************************************/
5194 uint32 get_lock_pid( char *data, int data_offset, BOOL large_file_format)
5196 if(!large_file_format)
5197 return (uint32)SVAL(data,SMB_LPID_OFFSET(data_offset));
5198 else
5199 return (uint32)SVAL(data,SMB_LARGE_LPID_OFFSET(data_offset));
5202 /****************************************************************************
5203 Get a lock count, dealing with large count requests.
5204 ****************************************************************************/
5206 SMB_BIG_UINT get_lock_count( char *data, int data_offset, BOOL large_file_format)
5208 SMB_BIG_UINT count = 0;
5210 if(!large_file_format) {
5211 count = (SMB_BIG_UINT)IVAL(data,SMB_LKLEN_OFFSET(data_offset));
5212 } else {
5214 #if defined(HAVE_LONGLONG)
5215 count = (((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset))) << 32) |
5216 ((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)));
5217 #else /* HAVE_LONGLONG */
5220 * NT4.x seems to be broken in that it sends large file (64 bit)
5221 * lockingX calls even if the CAP_LARGE_FILES was *not*
5222 * negotiated. For boxes without large unsigned ints truncate the
5223 * lock count by dropping the top 32 bits.
5226 if(IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)) != 0) {
5227 DEBUG(3,("get_lock_count: truncating lock count (high)0x%x (low)0x%x to just low count.\n",
5228 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)),
5229 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)) ));
5230 SIVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset),0);
5233 count = (SMB_BIG_UINT)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset));
5234 #endif /* HAVE_LONGLONG */
5237 return count;
5240 #if !defined(HAVE_LONGLONG)
5241 /****************************************************************************
5242 Pathetically try and map a 64 bit lock offset into 31 bits. I hate Windows :-).
5243 ****************************************************************************/
5245 static uint32 map_lock_offset(uint32 high, uint32 low)
5247 unsigned int i;
5248 uint32 mask = 0;
5249 uint32 highcopy = high;
5252 * Try and find out how many significant bits there are in high.
5255 for(i = 0; highcopy; i++)
5256 highcopy >>= 1;
5259 * We use 31 bits not 32 here as POSIX
5260 * lock offsets may not be negative.
5263 mask = (~0) << (31 - i);
5265 if(low & mask)
5266 return 0; /* Fail. */
5268 high <<= (31 - i);
5270 return (high|low);
5272 #endif /* !defined(HAVE_LONGLONG) */
5274 /****************************************************************************
5275 Get a lock offset, dealing with large offset requests.
5276 ****************************************************************************/
5278 SMB_BIG_UINT get_lock_offset( char *data, int data_offset, BOOL large_file_format, BOOL *err)
5280 SMB_BIG_UINT offset = 0;
5282 *err = False;
5284 if(!large_file_format) {
5285 offset = (SMB_BIG_UINT)IVAL(data,SMB_LKOFF_OFFSET(data_offset));
5286 } else {
5288 #if defined(HAVE_LONGLONG)
5289 offset = (((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset))) << 32) |
5290 ((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset)));
5291 #else /* HAVE_LONGLONG */
5294 * NT4.x seems to be broken in that it sends large file (64 bit)
5295 * lockingX calls even if the CAP_LARGE_FILES was *not*
5296 * negotiated. For boxes without large unsigned ints mangle the
5297 * lock offset by mapping the top 32 bits onto the lower 32.
5300 if(IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset)) != 0) {
5301 uint32 low = IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
5302 uint32 high = IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset));
5303 uint32 new_low = 0;
5305 if((new_low = map_lock_offset(high, low)) == 0) {
5306 *err = True;
5307 return (SMB_BIG_UINT)-1;
5310 DEBUG(3,("get_lock_offset: truncating lock offset (high)0x%x (low)0x%x to offset 0x%x.\n",
5311 (unsigned int)high, (unsigned int)low, (unsigned int)new_low ));
5312 SIVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset),0);
5313 SIVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset),new_low);
5316 offset = (SMB_BIG_UINT)IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
5317 #endif /* HAVE_LONGLONG */
5320 return offset;
5323 /****************************************************************************
5324 Reply to a lockingX request.
5325 ****************************************************************************/
5327 int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
5328 int length, int bufsize)
5330 files_struct *fsp = file_fsp(inbuf,smb_vwv2);
5331 unsigned char locktype = CVAL(inbuf,smb_vwv3);
5332 unsigned char oplocklevel = CVAL(inbuf,smb_vwv3+1);
5333 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
5334 uint16 num_locks = SVAL(inbuf,smb_vwv7);
5335 SMB_BIG_UINT count = 0, offset = 0;
5336 uint32 lock_pid;
5337 int32 lock_timeout = IVAL(inbuf,smb_vwv4);
5338 int i;
5339 char *data;
5340 BOOL large_file_format =
5341 (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
5342 BOOL err;
5343 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5345 START_PROFILE(SMBlockingX);
5347 CHECK_FSP(fsp,conn);
5349 data = smb_buf(inbuf);
5351 if (locktype & LOCKING_ANDX_CHANGE_LOCKTYPE) {
5352 /* we don't support these - and CANCEL_LOCK makes w2k
5353 and XP reboot so I don't really want to be
5354 compatible! (tridge) */
5355 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks));
5358 /* Check if this is an oplock break on a file
5359 we have granted an oplock on.
5361 if ((locktype & LOCKING_ANDX_OPLOCK_RELEASE)) {
5362 /* Client can insist on breaking to none. */
5363 BOOL break_to_none = (oplocklevel == 0);
5364 BOOL result;
5366 DEBUG(5,("reply_lockingX: oplock break reply (%u) from client "
5367 "for fnum = %d\n", (unsigned int)oplocklevel,
5368 fsp->fnum ));
5371 * Make sure we have granted an exclusive or batch oplock on
5372 * this file.
5375 if (fsp->oplock_type == 0) {
5377 /* The Samba4 nbench simulator doesn't understand
5378 the difference between break to level2 and break
5379 to none from level2 - it sends oplock break
5380 replies in both cases. Don't keep logging an error
5381 message here - just ignore it. JRA. */
5383 DEBUG(5,("reply_lockingX: Error : oplock break from "
5384 "client for fnum = %d (oplock=%d) and no "
5385 "oplock granted on this file (%s).\n",
5386 fsp->fnum, fsp->oplock_type, fsp->fsp_name));
5388 /* if this is a pure oplock break request then don't
5389 * send a reply */
5390 if (num_locks == 0 && num_ulocks == 0) {
5391 END_PROFILE(SMBlockingX);
5392 return -1;
5393 } else {
5394 END_PROFILE(SMBlockingX);
5395 return ERROR_DOS(ERRDOS,ERRlock);
5399 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
5400 (break_to_none)) {
5401 result = remove_oplock(fsp);
5402 } else {
5403 result = downgrade_oplock(fsp);
5406 if (!result) {
5407 DEBUG(0, ("reply_lockingX: error in removing "
5408 "oplock on file %s\n", fsp->fsp_name));
5409 /* Hmmm. Is this panic justified? */
5410 smb_panic("internal tdb error");
5413 reply_to_oplock_break_requests(fsp);
5415 /* if this is a pure oplock break request then don't send a
5416 * reply */
5417 if (num_locks == 0 && num_ulocks == 0) {
5418 /* Sanity check - ensure a pure oplock break is not a
5419 chained request. */
5420 if(CVAL(inbuf,smb_vwv0) != 0xff)
5421 DEBUG(0,("reply_lockingX: Error : pure oplock "
5422 "break is a chained %d request !\n",
5423 (unsigned int)CVAL(inbuf,smb_vwv0) ));
5424 END_PROFILE(SMBlockingX);
5425 return -1;
5430 * We do this check *after* we have checked this is not a oplock break
5431 * response message. JRA.
5434 release_level_2_oplocks_on_change(fsp);
5436 /* Data now points at the beginning of the list
5437 of smb_unlkrng structs */
5438 for(i = 0; i < (int)num_ulocks; i++) {
5439 lock_pid = get_lock_pid( data, i, large_file_format);
5440 count = get_lock_count( data, i, large_file_format);
5441 offset = get_lock_offset( data, i, large_file_format, &err);
5444 * There is no error code marked "stupid client bug".... :-).
5446 if(err) {
5447 END_PROFILE(SMBlockingX);
5448 return ERROR_DOS(ERRDOS,ERRnoaccess);
5451 DEBUG(10,("reply_lockingX: unlock start=%.0f, len=%.0f for "
5452 "pid %u, file %s\n", (double)offset, (double)count,
5453 (unsigned int)lock_pid, fsp->fsp_name ));
5455 status = do_unlock(fsp,
5456 lock_pid,
5457 count,
5458 offset,
5459 WINDOWS_LOCK);
5461 if (NT_STATUS_V(status)) {
5462 END_PROFILE(SMBlockingX);
5463 return ERROR_NT(status);
5467 /* Setup the timeout in seconds. */
5469 if (!lp_blocking_locks(SNUM(conn))) {
5470 lock_timeout = 0;
5473 /* Now do any requested locks */
5474 data += ((large_file_format ? 20 : 10)*num_ulocks);
5476 /* Data now points at the beginning of the list
5477 of smb_lkrng structs */
5479 for(i = 0; i < (int)num_locks; i++) {
5480 enum brl_type lock_type = ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
5481 READ_LOCK:WRITE_LOCK);
5482 lock_pid = get_lock_pid( data, i, large_file_format);
5483 count = get_lock_count( data, i, large_file_format);
5484 offset = get_lock_offset( data, i, large_file_format, &err);
5487 * There is no error code marked "stupid client bug".... :-).
5489 if(err) {
5490 END_PROFILE(SMBlockingX);
5491 return ERROR_DOS(ERRDOS,ERRnoaccess);
5494 DEBUG(10,("reply_lockingX: lock start=%.0f, len=%.0f for pid "
5495 "%u, file %s timeout = %d\n", (double)offset,
5496 (double)count, (unsigned int)lock_pid,
5497 fsp->fsp_name, (int)lock_timeout ));
5499 if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
5500 if (lp_blocking_locks(SNUM(conn))) {
5502 /* Schedule a message to ourselves to
5503 remove the blocking lock record and
5504 return the right error. */
5506 if (!blocking_lock_cancel(fsp,
5507 lock_pid,
5508 offset,
5509 count,
5510 WINDOWS_LOCK,
5511 locktype,
5512 NT_STATUS_FILE_LOCK_CONFLICT)) {
5513 END_PROFILE(SMBlockingX);
5514 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRcancelviolation));
5517 /* Remove a matching pending lock. */
5518 status = do_lock_cancel(fsp,
5519 lock_pid,
5520 count,
5521 offset,
5522 WINDOWS_LOCK);
5523 } else {
5524 BOOL blocking_lock = lock_timeout ? True : False;
5525 BOOL defer_lock = False;
5526 struct byte_range_lock *br_lck;
5528 br_lck = do_lock(fsp,
5529 lock_pid,
5530 count,
5531 offset,
5532 lock_type,
5533 WINDOWS_LOCK,
5534 blocking_lock,
5535 &status);
5537 if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
5538 /* Windows internal resolution for blocking locks seems
5539 to be about 200ms... Don't wait for less than that. JRA. */
5540 if (lock_timeout != -1 && lock_timeout < lp_lock_spin_time()) {
5541 lock_timeout = lp_lock_spin_time();
5543 defer_lock = True;
5546 /* This heuristic seems to match W2K3 very well. If a
5547 lock sent with timeout of zero would fail with NT_STATUS_FILE_LOCK_CONFLICT
5548 it pretends we asked for a timeout of between 150 - 300 milliseconds as
5549 far as I can tell. Replacement for do_lock_spin(). JRA. */
5551 if (br_lck && lp_blocking_locks(SNUM(conn)) && !blocking_lock &&
5552 NT_STATUS_EQUAL((status), NT_STATUS_FILE_LOCK_CONFLICT)) {
5553 defer_lock = True;
5554 lock_timeout = lp_lock_spin_time();
5557 if (br_lck && defer_lock) {
5559 * A blocking lock was requested. Package up
5560 * this smb into a queued request and push it
5561 * onto the blocking lock queue.
5563 if(push_blocking_lock_request(br_lck,
5564 inbuf, length,
5565 fsp,
5566 lock_timeout,
5568 lock_pid,
5569 lock_type,
5570 WINDOWS_LOCK,
5571 offset,
5572 count)) {
5573 TALLOC_FREE(br_lck);
5574 END_PROFILE(SMBlockingX);
5575 return -1;
5579 TALLOC_FREE(br_lck);
5582 if (NT_STATUS_V(status)) {
5583 END_PROFILE(SMBlockingX);
5584 return ERROR_NT(status);
5588 /* If any of the above locks failed, then we must unlock
5589 all of the previous locks (X/Open spec). */
5591 if (!(locktype & LOCKING_ANDX_CANCEL_LOCK) &&
5592 (i != num_locks) &&
5593 (num_locks != 0)) {
5595 * Ensure we don't do a remove on the lock that just failed,
5596 * as under POSIX rules, if we have a lock already there, we
5597 * will delete it (and we shouldn't) .....
5599 for(i--; i >= 0; i--) {
5600 lock_pid = get_lock_pid( data, i, large_file_format);
5601 count = get_lock_count( data, i, large_file_format);
5602 offset = get_lock_offset( data, i, large_file_format,
5603 &err);
5606 * There is no error code marked "stupid client
5607 * bug".... :-).
5609 if(err) {
5610 END_PROFILE(SMBlockingX);
5611 return ERROR_DOS(ERRDOS,ERRnoaccess);
5614 do_unlock(fsp,
5615 lock_pid,
5616 count,
5617 offset,
5618 WINDOWS_LOCK);
5620 END_PROFILE(SMBlockingX);
5621 return ERROR_NT(status);
5624 set_message(inbuf,outbuf,2,0,True);
5626 DEBUG(3, ("lockingX fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
5627 fsp->fnum, (unsigned int)locktype, num_locks, num_ulocks));
5629 END_PROFILE(SMBlockingX);
5630 return chain_reply(inbuf,outbuf,length,bufsize);
5633 #undef DBGC_CLASS
5634 #define DBGC_CLASS DBGC_ALL
5636 /****************************************************************************
5637 Reply to a SMBreadbmpx (read block multiplex) request.
5638 ****************************************************************************/
5640 int reply_readbmpx(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
5642 ssize_t nread = -1;
5643 ssize_t total_read;
5644 char *data;
5645 SMB_OFF_T startpos;
5646 int outsize;
5647 size_t maxcount;
5648 int max_per_packet;
5649 size_t tcount;
5650 int pad;
5651 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5652 START_PROFILE(SMBreadBmpx);
5654 /* this function doesn't seem to work - disable by default */
5655 if (!lp_readbmpx()) {
5656 END_PROFILE(SMBreadBmpx);
5657 return ERROR_DOS(ERRSRV,ERRuseSTD);
5660 outsize = set_message(inbuf,outbuf,8,0,True);
5662 CHECK_FSP(fsp,conn);
5663 if (!CHECK_READ(fsp,inbuf)) {
5664 return(ERROR_DOS(ERRDOS,ERRbadaccess));
5667 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
5668 maxcount = SVAL(inbuf,smb_vwv3);
5670 data = smb_buf(outbuf);
5671 pad = ((long)data)%4;
5672 if (pad)
5673 pad = 4 - pad;
5674 data += pad;
5676 max_per_packet = bufsize-(outsize+pad);
5677 tcount = maxcount;
5678 total_read = 0;
5680 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
5681 END_PROFILE(SMBreadBmpx);
5682 return ERROR_DOS(ERRDOS,ERRlock);
5685 do {
5686 size_t N = MIN(max_per_packet,tcount-total_read);
5688 nread = read_file(fsp,data,startpos,N);
5690 if (nread <= 0)
5691 nread = 0;
5693 if (nread < (ssize_t)N)
5694 tcount = total_read + nread;
5696 set_message(inbuf,outbuf,8,nread+pad,False);
5697 SIVAL(outbuf,smb_vwv0,startpos);
5698 SSVAL(outbuf,smb_vwv2,tcount);
5699 SSVAL(outbuf,smb_vwv6,nread);
5700 SSVAL(outbuf,smb_vwv7,smb_offset(data,outbuf));
5702 show_msg(outbuf);
5703 if (!send_smb(smbd_server_fd(),outbuf))
5704 exit_server_cleanly("reply_readbmpx: send_smb failed.");
5706 total_read += nread;
5707 startpos += nread;
5708 } while (total_read < (ssize_t)tcount);
5710 END_PROFILE(SMBreadBmpx);
5711 return(-1);
5714 /****************************************************************************
5715 Reply to a SMBsetattrE.
5716 ****************************************************************************/
5718 int reply_setattrE(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
5720 struct timespec ts[2];
5721 int outsize = 0;
5722 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5723 START_PROFILE(SMBsetattrE);
5725 outsize = set_message(inbuf,outbuf,0,0,False);
5727 if(!fsp || (fsp->conn != conn)) {
5728 END_PROFILE(SMBsetattrE);
5729 return ERROR_DOS(ERRDOS,ERRbadfid);
5733 * Convert the DOS times into unix times. Ignore create
5734 * time as UNIX can't set this.
5737 ts[0] = convert_time_t_to_timespec(srv_make_unix_date2(inbuf+smb_vwv3)); /* atime. */
5738 ts[1] = convert_time_t_to_timespec(srv_make_unix_date2(inbuf+smb_vwv5)); /* mtime. */
5741 * Patch from Ray Frush <frush@engr.colostate.edu>
5742 * Sometimes times are sent as zero - ignore them.
5745 if (null_timespec(ts[0]) && null_timespec(ts[1])) {
5746 /* Ignore request */
5747 if( DEBUGLVL( 3 ) ) {
5748 dbgtext( "reply_setattrE fnum=%d ", fsp->fnum);
5749 dbgtext( "ignoring zero request - not setting timestamps of 0\n" );
5751 END_PROFILE(SMBsetattrE);
5752 return(outsize);
5753 } else if (!null_timespec(ts[0]) && null_timespec(ts[1])) {
5754 /* set modify time = to access time if modify time was unset */
5755 ts[1] = ts[0];
5758 /* Set the date on this file */
5759 /* Should we set pending modtime here ? JRA */
5760 if(file_ntimes(conn, fsp->fsp_name, ts)) {
5761 END_PROFILE(SMBsetattrE);
5762 return ERROR_DOS(ERRDOS,ERRnoaccess);
5765 DEBUG( 3, ( "reply_setattrE fnum=%d actime=%u modtime=%u\n",
5766 fsp->fnum,
5767 (unsigned int)ts[0].tv_sec,
5768 (unsigned int)ts[1].tv_sec));
5770 END_PROFILE(SMBsetattrE);
5771 return(outsize);
5775 /* Back from the dead for OS/2..... JRA. */
5777 /****************************************************************************
5778 Reply to a SMBwritebmpx (write block multiplex primary) request.
5779 ****************************************************************************/
5781 int reply_writebmpx(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
5783 size_t numtowrite;
5784 ssize_t nwritten = -1;
5785 int outsize = 0;
5786 SMB_OFF_T startpos;
5787 size_t tcount;
5788 BOOL write_through;
5789 int smb_doff;
5790 char *data;
5791 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5792 START_PROFILE(SMBwriteBmpx);
5794 CHECK_FSP(fsp,conn);
5795 if (!CHECK_WRITE(fsp)) {
5796 return(ERROR_DOS(ERRDOS,ERRbadaccess));
5798 if (HAS_CACHED_ERROR(fsp)) {
5799 return(CACHED_ERROR(fsp));
5802 tcount = SVAL(inbuf,smb_vwv1);
5803 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
5804 write_through = BITSETW(inbuf+smb_vwv7,0);
5805 numtowrite = SVAL(inbuf,smb_vwv10);
5806 smb_doff = SVAL(inbuf,smb_vwv11);
5808 data = smb_base(inbuf) + smb_doff;
5810 /* If this fails we need to send an SMBwriteC response,
5811 not an SMBwritebmpx - set this up now so we don't forget */
5812 SCVAL(outbuf,smb_com,SMBwritec);
5814 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos,WRITE_LOCK)) {
5815 END_PROFILE(SMBwriteBmpx);
5816 return(ERROR_DOS(ERRDOS,ERRlock));
5819 nwritten = write_file(fsp,data,startpos,numtowrite);
5821 sync_file(conn, fsp, write_through);
5823 if(nwritten < (ssize_t)numtowrite) {
5824 END_PROFILE(SMBwriteBmpx);
5825 return(UNIXERROR(ERRHRD,ERRdiskfull));
5828 /* If the maximum to be written to this file
5829 is greater than what we just wrote then set
5830 up a secondary struct to be attached to this
5831 fd, we will use this to cache error messages etc. */
5833 if((ssize_t)tcount > nwritten) {
5834 write_bmpx_struct *wbms;
5835 if(fsp->wbmpx_ptr != NULL)
5836 wbms = fsp->wbmpx_ptr; /* Use an existing struct */
5837 else
5838 wbms = SMB_MALLOC_P(write_bmpx_struct);
5839 if(!wbms) {
5840 DEBUG(0,("Out of memory in reply_readmpx\n"));
5841 END_PROFILE(SMBwriteBmpx);
5842 return(ERROR_DOS(ERRSRV,ERRnoresource));
5844 wbms->wr_mode = write_through;
5845 wbms->wr_discard = False; /* No errors yet */
5846 wbms->wr_total_written = nwritten;
5847 wbms->wr_errclass = 0;
5848 wbms->wr_error = 0;
5849 fsp->wbmpx_ptr = wbms;
5852 /* We are returning successfully, set the message type back to
5853 SMBwritebmpx */
5854 SCVAL(outbuf,smb_com,SMBwriteBmpx);
5856 outsize = set_message(inbuf,outbuf,1,0,True);
5858 SSVALS(outbuf,smb_vwv0,-1); /* We don't support smb_remaining */
5860 DEBUG( 3, ( "writebmpx fnum=%d num=%d wrote=%d\n",
5861 fsp->fnum, (int)numtowrite, (int)nwritten ) );
5863 if (write_through && tcount==nwritten) {
5864 /* We need to send both a primary and a secondary response */
5865 smb_setlen(inbuf,outbuf,outsize - 4);
5866 show_msg(outbuf);
5867 if (!send_smb(smbd_server_fd(),outbuf))
5868 exit_server_cleanly("reply_writebmpx: send_smb failed.");
5870 /* Now the secondary */
5871 outsize = set_message(inbuf,outbuf,1,0,True);
5872 SCVAL(outbuf,smb_com,SMBwritec);
5873 SSVAL(outbuf,smb_vwv0,nwritten);
5876 END_PROFILE(SMBwriteBmpx);
5877 return(outsize);
5880 /****************************************************************************
5881 Reply to a SMBwritebs (write block multiplex secondary) request.
5882 ****************************************************************************/
5884 int reply_writebs(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
5886 size_t numtowrite;
5887 ssize_t nwritten = -1;
5888 int outsize = 0;
5889 SMB_OFF_T startpos;
5890 size_t tcount;
5891 BOOL write_through;
5892 int smb_doff;
5893 char *data;
5894 write_bmpx_struct *wbms;
5895 BOOL send_response = False;
5896 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5897 START_PROFILE(SMBwriteBs);
5899 CHECK_FSP(fsp,conn);
5900 if (!CHECK_WRITE(fsp)) {
5901 return(ERROR_DOS(ERRDOS,ERRbadaccess));
5904 tcount = SVAL(inbuf,smb_vwv1);
5905 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
5906 numtowrite = SVAL(inbuf,smb_vwv6);
5907 smb_doff = SVAL(inbuf,smb_vwv7);
5909 data = smb_base(inbuf) + smb_doff;
5911 /* We need to send an SMBwriteC response, not an SMBwritebs */
5912 SCVAL(outbuf,smb_com,SMBwritec);
5914 /* This fd should have an auxiliary struct attached,
5915 check that it does */
5916 wbms = fsp->wbmpx_ptr;
5917 if(!wbms) {
5918 END_PROFILE(SMBwriteBs);
5919 return(-1);
5922 /* If write through is set we can return errors, else we must cache them */
5923 write_through = wbms->wr_mode;
5925 /* Check for an earlier error */
5926 if(wbms->wr_discard) {
5927 END_PROFILE(SMBwriteBs);
5928 return -1; /* Just discard the packet */
5931 nwritten = write_file(fsp,data,startpos,numtowrite);
5933 sync_file(conn, fsp, write_through);
5935 if (nwritten < (ssize_t)numtowrite) {
5936 if(write_through) {
5937 /* We are returning an error - we can delete the aux struct */
5938 if (wbms)
5939 free((char *)wbms);
5940 fsp->wbmpx_ptr = NULL;
5941 END_PROFILE(SMBwriteBs);
5942 return(ERROR_DOS(ERRHRD,ERRdiskfull));
5944 wbms->wr_errclass = ERRHRD;
5945 wbms->wr_error = ERRdiskfull;
5946 wbms->wr_status = NT_STATUS_DISK_FULL;
5947 wbms->wr_discard = True;
5948 END_PROFILE(SMBwriteBs);
5949 return -1;
5952 /* Increment the total written, if this matches tcount
5953 we can discard the auxiliary struct (hurrah !) and return a writeC */
5954 wbms->wr_total_written += nwritten;
5955 if(wbms->wr_total_written >= tcount) {
5956 if (write_through) {
5957 outsize = set_message(inbuf,outbuf,1,0,True);
5958 SSVAL(outbuf,smb_vwv0,wbms->wr_total_written);
5959 send_response = True;
5962 free((char *)wbms);
5963 fsp->wbmpx_ptr = NULL;
5966 if(send_response) {
5967 END_PROFILE(SMBwriteBs);
5968 return(outsize);
5971 END_PROFILE(SMBwriteBs);
5972 return(-1);
5975 /****************************************************************************
5976 Reply to a SMBgetattrE.
5977 ****************************************************************************/
5979 int reply_getattrE(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
5981 SMB_STRUCT_STAT sbuf;
5982 int outsize = 0;
5983 int mode;
5984 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5985 START_PROFILE(SMBgetattrE);
5987 outsize = set_message(inbuf,outbuf,11,0,True);
5989 if(!fsp || (fsp->conn != conn)) {
5990 END_PROFILE(SMBgetattrE);
5991 return ERROR_DOS(ERRDOS,ERRbadfid);
5994 /* Do an fstat on this file */
5995 if(fsp_stat(fsp, &sbuf)) {
5996 END_PROFILE(SMBgetattrE);
5997 return(UNIXERROR(ERRDOS,ERRnoaccess));
6000 mode = dos_mode(conn,fsp->fsp_name,&sbuf);
6003 * Convert the times into dos times. Set create
6004 * date to be last modify date as UNIX doesn't save
6005 * this.
6008 srv_put_dos_date2(outbuf,smb_vwv0,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
6009 srv_put_dos_date2(outbuf,smb_vwv2,sbuf.st_atime);
6010 /* Should we check pending modtime here ? JRA */
6011 srv_put_dos_date2(outbuf,smb_vwv4,sbuf.st_mtime);
6013 if (mode & aDIR) {
6014 SIVAL(outbuf,smb_vwv6,0);
6015 SIVAL(outbuf,smb_vwv8,0);
6016 } else {
6017 uint32 allocation_size = get_allocation_size(conn,fsp, &sbuf);
6018 SIVAL(outbuf,smb_vwv6,(uint32)sbuf.st_size);
6019 SIVAL(outbuf,smb_vwv8,allocation_size);
6021 SSVAL(outbuf,smb_vwv10, mode);
6023 DEBUG( 3, ( "reply_getattrE fnum=%d\n", fsp->fnum));
6025 END_PROFILE(SMBgetattrE);
6026 return(outsize);