r23041: Remainder of fix for 4630: fix special case of unix_to_nt_time() for
[Samba/nascimento.git] / source3 / smbd / reply.c
blob364f571f4bfe9fc71cec38308317981a6c7828b5
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 /****************************************************************************
2153 Fake (read/write) sendfile. Returns -1 on read or write fail.
2154 ****************************************************************************/
2156 static ssize_t fake_sendfile(files_struct *fsp, SMB_OFF_T startpos, size_t nread, char *buf, size_t bufsize)
2158 size_t tosend = nread;
2160 while (tosend > 0) {
2161 ssize_t ret;
2162 size_t cur_read;
2164 if (tosend > bufsize) {
2165 cur_read = bufsize;
2166 } else {
2167 cur_read = tosend;
2169 ret = read_file(fsp,buf,startpos,cur_read);
2170 if (ret == -1) {
2171 return -1;
2174 /* If we had a short read, fill with zeros. */
2175 if (ret < cur_read) {
2176 memset(buf, '\0', cur_read - ret);
2179 if (write_data(smbd_server_fd(),buf,cur_read) != cur_read) {
2180 return -1;
2182 tosend -= cur_read;
2183 startpos += cur_read;
2186 return (ssize_t)nread;
2189 /****************************************************************************
2190 Use sendfile in readbraw.
2191 ****************************************************************************/
2193 void send_file_readbraw(connection_struct *conn, files_struct *fsp, SMB_OFF_T startpos, size_t nread,
2194 ssize_t mincount, char *outbuf, int out_buffsize)
2196 ssize_t ret=0;
2198 #if defined(WITH_SENDFILE)
2200 * We can only use sendfile on a non-chained packet
2201 * but we can use on a non-oplocked file. tridge proved this
2202 * on a train in Germany :-). JRA.
2203 * reply_readbraw has already checked the length.
2206 if ( (chain_size == 0) && (nread > 0) &&
2207 (fsp->wcp == NULL) && lp_use_sendfile(SNUM(conn)) ) {
2208 DATA_BLOB header;
2210 _smb_setlen(outbuf,nread);
2211 header.data = (uint8 *)outbuf;
2212 header.length = 4;
2213 header.free = NULL;
2215 if ( SMB_VFS_SENDFILE( smbd_server_fd(), fsp, fsp->fh->fd, &header, startpos, nread) == -1) {
2216 /* Returning ENOSYS means no data at all was sent. Do this as a normal read. */
2217 if (errno == ENOSYS) {
2218 goto normal_readbraw;
2222 * Special hack for broken Linux with no working sendfile. If we
2223 * return EINTR we sent the header but not the rest of the data.
2224 * Fake this up by doing read/write calls.
2226 if (errno == EINTR) {
2227 /* Ensure we don't do this again. */
2228 set_use_sendfile(SNUM(conn), False);
2229 DEBUG(0,("send_file_readbraw: sendfile not available. Faking..\n"));
2231 if (fake_sendfile(fsp, startpos, nread, outbuf + 4, out_buffsize - 4) == -1) {
2232 DEBUG(0,("send_file_readbraw: fake_sendfile failed for file %s (%s).\n",
2233 fsp->fsp_name, strerror(errno) ));
2234 exit_server_cleanly("send_file_readbraw fake_sendfile failed");
2236 return;
2239 DEBUG(0,("send_file_readbraw: sendfile failed for file %s (%s). Terminating\n",
2240 fsp->fsp_name, strerror(errno) ));
2241 exit_server_cleanly("send_file_readbraw sendfile failed");
2244 return;
2247 normal_readbraw:
2249 #endif
2251 if (nread > 0) {
2252 ret = read_file(fsp,outbuf+4,startpos,nread);
2253 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2254 if (ret < mincount)
2255 ret = 0;
2256 #else
2257 if (ret < nread)
2258 ret = 0;
2259 #endif
2262 _smb_setlen(outbuf,ret);
2263 if (write_data(smbd_server_fd(),outbuf,4+ret) != 4+ret)
2264 fail_readraw();
2267 /****************************************************************************
2268 Reply to a readbraw (core+ protocol).
2269 ****************************************************************************/
2271 int reply_readbraw(connection_struct *conn, char *inbuf, char *outbuf, int dum_size, int out_buffsize)
2273 ssize_t maxcount,mincount;
2274 size_t nread = 0;
2275 SMB_OFF_T startpos;
2276 char *header = outbuf;
2277 files_struct *fsp;
2278 START_PROFILE(SMBreadbraw);
2280 if (srv_is_signing_active()) {
2281 exit_server_cleanly("reply_readbraw: SMB signing is active - raw reads/writes are disallowed.");
2285 * Special check if an oplock break has been issued
2286 * and the readraw request croses on the wire, we must
2287 * return a zero length response here.
2290 fsp = file_fsp(inbuf,smb_vwv0);
2292 if (!FNUM_OK(fsp,conn) || !fsp->can_read) {
2294 * fsp could be NULL here so use the value from the packet. JRA.
2296 DEBUG(3,("fnum %d not open in readbraw - cache prime?\n",(int)SVAL(inbuf,smb_vwv0)));
2297 _smb_setlen(header,0);
2298 if (write_data(smbd_server_fd(),header,4) != 4)
2299 fail_readraw();
2300 END_PROFILE(SMBreadbraw);
2301 return(-1);
2304 CHECK_FSP(fsp,conn);
2306 flush_write_cache(fsp, READRAW_FLUSH);
2308 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
2309 if(CVAL(inbuf,smb_wct) == 10) {
2311 * This is a large offset (64 bit) read.
2313 #ifdef LARGE_SMB_OFF_T
2315 startpos |= (((SMB_OFF_T)IVAL(inbuf,smb_vwv8)) << 32);
2317 #else /* !LARGE_SMB_OFF_T */
2320 * Ensure we haven't been sent a >32 bit offset.
2323 if(IVAL(inbuf,smb_vwv8) != 0) {
2324 DEBUG(0,("readbraw - large offset (%x << 32) used and we don't support \
2325 64 bit offsets.\n", (unsigned int)IVAL(inbuf,smb_vwv8) ));
2326 _smb_setlen(header,0);
2327 if (write_data(smbd_server_fd(),header,4) != 4)
2328 fail_readraw();
2329 END_PROFILE(SMBreadbraw);
2330 return(-1);
2333 #endif /* LARGE_SMB_OFF_T */
2335 if(startpos < 0) {
2336 DEBUG(0,("readbraw - negative 64 bit readraw offset (%.0f) !\n", (double)startpos ));
2337 _smb_setlen(header,0);
2338 if (write_data(smbd_server_fd(),header,4) != 4)
2339 fail_readraw();
2340 END_PROFILE(SMBreadbraw);
2341 return(-1);
2344 maxcount = (SVAL(inbuf,smb_vwv3) & 0xFFFF);
2345 mincount = (SVAL(inbuf,smb_vwv4) & 0xFFFF);
2347 /* ensure we don't overrun the packet size */
2348 maxcount = MIN(65535,maxcount);
2350 if (!is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
2351 SMB_STRUCT_STAT st;
2352 SMB_OFF_T size = 0;
2354 if (SMB_VFS_FSTAT(fsp,fsp->fh->fd,&st) == 0) {
2355 size = st.st_size;
2358 if (startpos >= size) {
2359 nread = 0;
2360 } else {
2361 nread = MIN(maxcount,(size - startpos));
2365 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2366 if (nread < mincount)
2367 nread = 0;
2368 #endif
2370 DEBUG( 3, ( "readbraw fnum=%d start=%.0f max=%lu min=%lu nread=%lu\n", fsp->fnum, (double)startpos,
2371 (unsigned long)maxcount, (unsigned long)mincount, (unsigned long)nread ) );
2373 send_file_readbraw(conn, fsp, startpos, nread, mincount, outbuf, out_buffsize);
2375 DEBUG(5,("readbraw finished\n"));
2376 END_PROFILE(SMBreadbraw);
2377 return -1;
2380 #undef DBGC_CLASS
2381 #define DBGC_CLASS DBGC_LOCKING
2383 /****************************************************************************
2384 Reply to a lockread (core+ protocol).
2385 ****************************************************************************/
2387 int reply_lockread(connection_struct *conn, char *inbuf,char *outbuf, int length, int dum_buffsiz)
2389 ssize_t nread = -1;
2390 char *data;
2391 int outsize = 0;
2392 SMB_OFF_T startpos;
2393 size_t numtoread;
2394 NTSTATUS status;
2395 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2396 struct byte_range_lock *br_lck = NULL;
2397 START_PROFILE(SMBlockread);
2399 CHECK_FSP(fsp,conn);
2400 if (!CHECK_READ(fsp,inbuf)) {
2401 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2404 release_level_2_oplocks_on_change(fsp);
2406 numtoread = SVAL(inbuf,smb_vwv1);
2407 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2409 outsize = set_message(inbuf,outbuf,5,3,True);
2410 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
2411 data = smb_buf(outbuf) + 3;
2414 * NB. Discovered by Menny Hamburger at Mainsoft. This is a core+
2415 * protocol request that predates the read/write lock concept.
2416 * Thus instead of asking for a read lock here we need to ask
2417 * for a write lock. JRA.
2418 * Note that the requested lock size is unaffected by max_recv.
2421 br_lck = do_lock(smbd_messaging_context(),
2422 fsp,
2423 (uint32)SVAL(inbuf,smb_pid),
2424 (SMB_BIG_UINT)numtoread,
2425 (SMB_BIG_UINT)startpos,
2426 WRITE_LOCK,
2427 WINDOWS_LOCK,
2428 False, /* Non-blocking lock. */
2429 &status,
2430 NULL);
2431 TALLOC_FREE(br_lck);
2433 if (NT_STATUS_V(status)) {
2434 END_PROFILE(SMBlockread);
2435 return ERROR_NT(status);
2439 * However the requested READ size IS affected by max_recv. Insanity.... JRA.
2442 if (numtoread > max_recv) {
2443 DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u). \
2444 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
2445 (unsigned int)numtoread, (unsigned int)max_recv ));
2446 numtoread = MIN(numtoread,max_recv);
2448 nread = read_file(fsp,data,startpos,numtoread);
2450 if (nread < 0) {
2451 END_PROFILE(SMBlockread);
2452 return(UNIXERROR(ERRDOS,ERRnoaccess));
2455 outsize += nread;
2456 SSVAL(outbuf,smb_vwv0,nread);
2457 SSVAL(outbuf,smb_vwv5,nread+3);
2458 SSVAL(smb_buf(outbuf),1,nread);
2460 DEBUG(3,("lockread fnum=%d num=%d nread=%d\n",
2461 fsp->fnum, (int)numtoread, (int)nread));
2463 END_PROFILE(SMBlockread);
2464 return(outsize);
2467 #undef DBGC_CLASS
2468 #define DBGC_CLASS DBGC_ALL
2470 /****************************************************************************
2471 Reply to a read.
2472 ****************************************************************************/
2474 int reply_read(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
2476 size_t numtoread;
2477 ssize_t nread = 0;
2478 char *data;
2479 SMB_OFF_T startpos;
2480 int outsize = 0;
2481 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2482 START_PROFILE(SMBread);
2484 CHECK_FSP(fsp,conn);
2485 if (!CHECK_READ(fsp,inbuf)) {
2486 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2489 numtoread = SVAL(inbuf,smb_vwv1);
2490 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2492 outsize = set_message(inbuf,outbuf,5,3,True);
2493 numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
2495 * The requested read size cannot be greater than max_recv. JRA.
2497 if (numtoread > max_recv) {
2498 DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u). \
2499 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
2500 (unsigned int)numtoread, (unsigned int)max_recv ));
2501 numtoread = MIN(numtoread,max_recv);
2504 data = smb_buf(outbuf) + 3;
2506 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtoread,(SMB_BIG_UINT)startpos, READ_LOCK)) {
2507 END_PROFILE(SMBread);
2508 return ERROR_DOS(ERRDOS,ERRlock);
2511 if (numtoread > 0)
2512 nread = read_file(fsp,data,startpos,numtoread);
2514 if (nread < 0) {
2515 END_PROFILE(SMBread);
2516 return(UNIXERROR(ERRDOS,ERRnoaccess));
2519 outsize += nread;
2520 SSVAL(outbuf,smb_vwv0,nread);
2521 SSVAL(outbuf,smb_vwv5,nread+3);
2522 SCVAL(smb_buf(outbuf),0,1);
2523 SSVAL(smb_buf(outbuf),1,nread);
2525 DEBUG( 3, ( "read fnum=%d num=%d nread=%d\n",
2526 fsp->fnum, (int)numtoread, (int)nread ) );
2528 END_PROFILE(SMBread);
2529 return(outsize);
2532 /****************************************************************************
2533 Setup readX header.
2534 ****************************************************************************/
2536 static int setup_readX_header(char *inbuf, char *outbuf, size_t smb_maxcnt)
2538 int outsize;
2539 char *data = smb_buf(outbuf);
2541 SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be -1. */
2542 SSVAL(outbuf,smb_vwv5,smb_maxcnt);
2543 SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
2544 SSVAL(outbuf,smb_vwv7,(smb_maxcnt >> 16));
2545 SSVAL(smb_buf(outbuf),-2,smb_maxcnt);
2546 SCVAL(outbuf,smb_vwv0,0xFF);
2547 outsize = set_message(inbuf, outbuf,12,smb_maxcnt,False);
2548 /* Reset the outgoing length, set_message truncates at 0x1FFFF. */
2549 _smb_setlen_large(outbuf,(smb_size + 12*2 + smb_maxcnt - 4));
2550 return outsize;
2553 /****************************************************************************
2554 Reply to a read and X - possibly using sendfile.
2555 ****************************************************************************/
2557 int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length, int len_outbuf,
2558 files_struct *fsp, SMB_OFF_T startpos, size_t smb_maxcnt)
2560 SMB_STRUCT_STAT sbuf;
2561 int outsize = 0;
2562 ssize_t nread = -1;
2563 char *data = smb_buf(outbuf);
2565 if(SMB_VFS_FSTAT(fsp,fsp->fh->fd, &sbuf) == -1) {
2566 return(UNIXERROR(ERRDOS,ERRnoaccess));
2569 if (startpos > sbuf.st_size) {
2570 smb_maxcnt = 0;
2573 if (smb_maxcnt > (sbuf.st_size - startpos)) {
2574 smb_maxcnt = (sbuf.st_size - startpos);
2577 if (smb_maxcnt == 0) {
2578 goto normal_read;
2581 #if defined(WITH_SENDFILE)
2583 * We can only use sendfile on a non-chained packet
2584 * but we can use on a non-oplocked file. tridge proved this
2585 * on a train in Germany :-). JRA.
2588 if ((chain_size == 0) && (CVAL(inbuf,smb_vwv0) == 0xFF) &&
2589 lp_use_sendfile(SNUM(conn)) && (fsp->wcp == NULL) ) {
2590 DATA_BLOB header;
2593 * Set up the packet header before send. We
2594 * assume here the sendfile will work (get the
2595 * correct amount of data).
2598 setup_readX_header(inbuf,outbuf,smb_maxcnt);
2599 set_message(inbuf,outbuf,12,smb_maxcnt,False);
2600 header.data = (uint8 *)outbuf;
2601 header.length = data - outbuf;
2602 header.free = NULL;
2604 if ((nread = SMB_VFS_SENDFILE( smbd_server_fd(), fsp, fsp->fh->fd, &header, startpos, smb_maxcnt)) == -1) {
2605 /* Returning ENOSYS means no data at all was sent. Do this as a normal read. */
2606 if (errno == ENOSYS) {
2607 goto normal_read;
2611 * Special hack for broken Linux with no working sendfile. If we
2612 * return EINTR we sent the header but not the rest of the data.
2613 * Fake this up by doing read/write calls.
2616 if (errno == EINTR) {
2617 /* Ensure we don't do this again. */
2618 set_use_sendfile(SNUM(conn), False);
2619 DEBUG(0,("send_file_readX: sendfile not available. Faking..\n"));
2621 if ((nread = fake_sendfile(fsp, startpos, smb_maxcnt, data,
2622 len_outbuf - (data-outbuf))) == -1) {
2623 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
2624 fsp->fsp_name, strerror(errno) ));
2625 exit_server_cleanly("send_file_readX: fake_sendfile failed");
2627 DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
2628 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
2629 /* Returning -1 here means successful sendfile. */
2630 return -1;
2633 DEBUG(0,("send_file_readX: sendfile failed for file %s (%s). Terminating\n",
2634 fsp->fsp_name, strerror(errno) ));
2635 exit_server_cleanly("send_file_readX sendfile failed");
2638 DEBUG( 3, ( "send_file_readX: sendfile fnum=%d max=%d nread=%d\n",
2639 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
2640 /* Returning -1 here means successful sendfile. */
2641 return -1;
2644 #endif
2646 normal_read:
2648 if ((smb_maxcnt & 0xFF0000) > 0x10000) {
2649 int sendlen = setup_readX_header(inbuf,outbuf,smb_maxcnt) - smb_maxcnt;
2650 /* Send out the header. */
2651 if (write_data(smbd_server_fd(),outbuf,sendlen) != sendlen) {
2652 DEBUG(0,("send_file_readX: write_data failed for file %s (%s). Terminating\n",
2653 fsp->fsp_name, strerror(errno) ));
2654 exit_server_cleanly("send_file_readX sendfile failed");
2656 if ((nread = fake_sendfile(fsp, startpos, smb_maxcnt, data,
2657 len_outbuf - (data-outbuf))) == -1) {
2658 DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
2659 fsp->fsp_name, strerror(errno) ));
2660 exit_server_cleanly("send_file_readX: fake_sendfile failed");
2662 return -1;
2663 } else {
2664 nread = read_file(fsp,data,startpos,smb_maxcnt);
2666 if (nread < 0) {
2667 return(UNIXERROR(ERRDOS,ERRnoaccess));
2670 outsize = setup_readX_header(inbuf, outbuf,nread);
2672 DEBUG( 3, ( "send_file_readX fnum=%d max=%d nread=%d\n",
2673 fsp->fnum, (int)smb_maxcnt, (int)nread ) );
2675 /* Returning the number of bytes we want to send back - including header. */
2676 return outsize;
2680 /****************************************************************************
2681 Reply to a read and X.
2682 ****************************************************************************/
2684 int reply_read_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
2686 files_struct *fsp = file_fsp(inbuf,smb_vwv2);
2687 SMB_OFF_T startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
2688 ssize_t nread = -1;
2689 size_t smb_maxcnt = SVAL(inbuf,smb_vwv5);
2690 BOOL big_readX = False;
2691 #if 0
2692 size_t smb_mincnt = SVAL(inbuf,smb_vwv6);
2693 #endif
2695 START_PROFILE(SMBreadX);
2697 /* If it's an IPC, pass off the pipe handler. */
2698 if (IS_IPC(conn)) {
2699 END_PROFILE(SMBreadX);
2700 return reply_pipe_read_and_X(inbuf,outbuf,length,bufsize);
2703 CHECK_FSP(fsp,conn);
2704 if (!CHECK_READ(fsp,inbuf)) {
2705 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2708 set_message(inbuf,outbuf,12,0,True);
2710 if (global_client_caps & CAP_LARGE_READX) {
2711 size_t upper_size = SVAL(inbuf,smb_vwv7);
2712 smb_maxcnt |= (upper_size<<16);
2713 if (upper_size > 1) {
2714 /* Can't do this on a chained packet. */
2715 if ((CVAL(inbuf,smb_vwv0) != 0xFF)) {
2716 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2718 /* We currently don't do this on signed or sealed data. */
2719 if (srv_is_signing_active() || srv_encryption_on()) {
2720 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2722 /* Is there room in the reply for this data ? */
2723 if (smb_maxcnt > (0xFFFFFF - (smb_size -4 + 12*2))) {
2724 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2726 big_readX = True;
2730 if(CVAL(inbuf,smb_wct) == 12) {
2731 #ifdef LARGE_SMB_OFF_T
2733 * This is a large offset (64 bit) read.
2735 startpos |= (((SMB_OFF_T)IVAL(inbuf,smb_vwv10)) << 32);
2737 #else /* !LARGE_SMB_OFF_T */
2740 * Ensure we haven't been sent a >32 bit offset.
2743 if(IVAL(inbuf,smb_vwv10) != 0) {
2744 DEBUG(0,("reply_read_and_X - large offset (%x << 32) used and we don't support \
2745 64 bit offsets.\n", (unsigned int)IVAL(inbuf,smb_vwv10) ));
2746 END_PROFILE(SMBreadX);
2747 return ERROR_DOS(ERRDOS,ERRbadaccess);
2750 #endif /* LARGE_SMB_OFF_T */
2754 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)smb_maxcnt,(SMB_BIG_UINT)startpos, READ_LOCK)) {
2755 END_PROFILE(SMBreadX);
2756 return ERROR_DOS(ERRDOS,ERRlock);
2759 if (!big_readX && schedule_aio_read_and_X(conn, inbuf, outbuf, length, bufsize, fsp, startpos, smb_maxcnt)) {
2760 END_PROFILE(SMBreadX);
2761 return -1;
2764 nread = send_file_readX(conn, inbuf, outbuf, length, bufsize, fsp, startpos, smb_maxcnt);
2765 /* Only call chain_reply if not an error. */
2766 if (nread != -1 && SVAL(outbuf,smb_rcls) == 0) {
2767 nread = chain_reply(inbuf,outbuf,length,bufsize);
2770 END_PROFILE(SMBreadX);
2771 return nread;
2774 /****************************************************************************
2775 Reply to a writebraw (core+ or LANMAN1.0 protocol).
2776 ****************************************************************************/
2778 int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
2780 ssize_t nwritten=0;
2781 ssize_t total_written=0;
2782 size_t numtowrite=0;
2783 size_t tcount;
2784 SMB_OFF_T startpos;
2785 char *data=NULL;
2786 BOOL write_through;
2787 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2788 int outsize = 0;
2789 START_PROFILE(SMBwritebraw);
2791 if (srv_is_signing_active()) {
2792 exit_server_cleanly("reply_writebraw: SMB signing is active - raw reads/writes are disallowed.");
2795 CHECK_FSP(fsp,conn);
2796 if (!CHECK_WRITE(fsp)) {
2797 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2800 tcount = IVAL(inbuf,smb_vwv1);
2801 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
2802 write_through = BITSETW(inbuf+smb_vwv7,0);
2804 /* We have to deal with slightly different formats depending
2805 on whether we are using the core+ or lanman1.0 protocol */
2807 if(Protocol <= PROTOCOL_COREPLUS) {
2808 numtowrite = SVAL(smb_buf(inbuf),-2);
2809 data = smb_buf(inbuf);
2810 } else {
2811 numtowrite = SVAL(inbuf,smb_vwv10);
2812 data = smb_base(inbuf) + SVAL(inbuf, smb_vwv11);
2815 /* force the error type */
2816 SCVAL(inbuf,smb_com,SMBwritec);
2817 SCVAL(outbuf,smb_com,SMBwritec);
2819 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
2820 END_PROFILE(SMBwritebraw);
2821 return(ERROR_DOS(ERRDOS,ERRlock));
2824 if (numtowrite>0)
2825 nwritten = write_file(fsp,data,startpos,numtowrite);
2827 DEBUG(3,("writebraw1 fnum=%d start=%.0f num=%d wrote=%d sync=%d\n",
2828 fsp->fnum, (double)startpos, (int)numtowrite, (int)nwritten, (int)write_through));
2830 if (nwritten < (ssize_t)numtowrite) {
2831 END_PROFILE(SMBwritebraw);
2832 return(UNIXERROR(ERRHRD,ERRdiskfull));
2835 total_written = nwritten;
2837 /* Return a message to the redirector to tell it to send more bytes */
2838 SCVAL(outbuf,smb_com,SMBwritebraw);
2839 SSVALS(outbuf,smb_vwv0,-1);
2840 outsize = set_message(inbuf,outbuf,Protocol>PROTOCOL_COREPLUS?1:0,0,True);
2841 show_msg(outbuf);
2842 if (!send_smb(smbd_server_fd(),outbuf))
2843 exit_server_cleanly("reply_writebraw: send_smb failed.");
2845 /* Now read the raw data into the buffer and write it */
2846 if (read_smb_length(smbd_server_fd(),inbuf,SMB_SECONDARY_WAIT) == -1) {
2847 exit_server_cleanly("secondary writebraw failed");
2850 /* Even though this is not an smb message, smb_len returns the generic length of an smb message */
2851 numtowrite = smb_len(inbuf);
2853 /* Set up outbuf to return the correct return */
2854 outsize = set_message(inbuf,outbuf,1,0,True);
2855 SCVAL(outbuf,smb_com,SMBwritec);
2857 if (numtowrite != 0) {
2859 if (numtowrite > BUFFER_SIZE) {
2860 DEBUG(0,("reply_writebraw: Oversize secondary write raw requested (%u). Terminating\n",
2861 (unsigned int)numtowrite ));
2862 exit_server_cleanly("secondary writebraw failed");
2865 if (tcount > nwritten+numtowrite) {
2866 DEBUG(3,("Client overestimated the write %d %d %d\n",
2867 (int)tcount,(int)nwritten,(int)numtowrite));
2870 if (read_data( smbd_server_fd(), inbuf+4, numtowrite) != numtowrite ) {
2871 DEBUG(0,("reply_writebraw: Oversize secondary write raw read failed (%s). Terminating\n",
2872 strerror(errno) ));
2873 exit_server_cleanly("secondary writebraw failed");
2876 nwritten = write_file(fsp,inbuf+4,startpos+nwritten,numtowrite);
2877 if (nwritten == -1) {
2878 END_PROFILE(SMBwritebraw);
2879 return(UNIXERROR(ERRHRD,ERRdiskfull));
2882 if (nwritten < (ssize_t)numtowrite) {
2883 SCVAL(outbuf,smb_rcls,ERRHRD);
2884 SSVAL(outbuf,smb_err,ERRdiskfull);
2887 if (nwritten > 0)
2888 total_written += nwritten;
2891 SSVAL(outbuf,smb_vwv0,total_written);
2893 sync_file(conn, fsp, write_through);
2895 DEBUG(3,("writebraw2 fnum=%d start=%.0f num=%d wrote=%d\n",
2896 fsp->fnum, (double)startpos, (int)numtowrite,(int)total_written));
2898 /* we won't return a status if write through is not selected - this follows what WfWg does */
2899 END_PROFILE(SMBwritebraw);
2900 if (!write_through && total_written==tcount) {
2902 #if RABBIT_PELLET_FIX
2904 * Fix for "rabbit pellet" mode, trigger an early TCP ack by
2905 * sending a SMBkeepalive. Thanks to DaveCB at Sun for this. JRA.
2907 if (!send_keepalive(smbd_server_fd()))
2908 exit_server_cleanly("reply_writebraw: send of keepalive failed");
2909 #endif
2910 return(-1);
2913 return(outsize);
2916 #undef DBGC_CLASS
2917 #define DBGC_CLASS DBGC_LOCKING
2919 /****************************************************************************
2920 Reply to a writeunlock (core+).
2921 ****************************************************************************/
2923 int reply_writeunlock(connection_struct *conn, char *inbuf,char *outbuf,
2924 int size, int dum_buffsize)
2926 ssize_t nwritten = -1;
2927 size_t numtowrite;
2928 SMB_OFF_T startpos;
2929 char *data;
2930 NTSTATUS status = NT_STATUS_OK;
2931 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
2932 int outsize = 0;
2933 START_PROFILE(SMBwriteunlock);
2935 CHECK_FSP(fsp,conn);
2936 if (!CHECK_WRITE(fsp)) {
2937 return(ERROR_DOS(ERRDOS,ERRbadaccess));
2940 numtowrite = SVAL(inbuf,smb_vwv1);
2941 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
2942 data = smb_buf(inbuf) + 3;
2944 if (numtowrite && is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
2945 END_PROFILE(SMBwriteunlock);
2946 return ERROR_DOS(ERRDOS,ERRlock);
2949 /* The special X/Open SMB protocol handling of
2950 zero length writes is *NOT* done for
2951 this call */
2952 if(numtowrite == 0) {
2953 nwritten = 0;
2954 } else {
2955 nwritten = write_file(fsp,data,startpos,numtowrite);
2958 sync_file(conn, fsp, False /* write through */);
2960 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
2961 END_PROFILE(SMBwriteunlock);
2962 return(UNIXERROR(ERRHRD,ERRdiskfull));
2965 if (numtowrite) {
2966 status = do_unlock(smbd_messaging_context(),
2967 fsp,
2968 (uint32)SVAL(inbuf,smb_pid),
2969 (SMB_BIG_UINT)numtowrite,
2970 (SMB_BIG_UINT)startpos,
2971 WINDOWS_LOCK);
2973 if (NT_STATUS_V(status)) {
2974 END_PROFILE(SMBwriteunlock);
2975 return ERROR_NT(status);
2979 outsize = set_message(inbuf,outbuf,1,0,True);
2981 SSVAL(outbuf,smb_vwv0,nwritten);
2983 DEBUG(3,("writeunlock fnum=%d num=%d wrote=%d\n",
2984 fsp->fnum, (int)numtowrite, (int)nwritten));
2986 END_PROFILE(SMBwriteunlock);
2987 return outsize;
2990 #undef DBGC_CLASS
2991 #define DBGC_CLASS DBGC_ALL
2993 /****************************************************************************
2994 Reply to a write.
2995 ****************************************************************************/
2997 int reply_write(connection_struct *conn, char *inbuf,char *outbuf,int size,int dum_buffsize)
2999 size_t numtowrite;
3000 ssize_t nwritten = -1;
3001 SMB_OFF_T startpos;
3002 char *data;
3003 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3004 int outsize = 0;
3005 START_PROFILE(SMBwrite);
3007 /* If it's an IPC, pass off the pipe handler. */
3008 if (IS_IPC(conn)) {
3009 END_PROFILE(SMBwrite);
3010 return reply_pipe_write(inbuf,outbuf,size,dum_buffsize);
3013 CHECK_FSP(fsp,conn);
3014 if (!CHECK_WRITE(fsp)) {
3015 return(ERROR_DOS(ERRDOS,ERRbadaccess));
3018 numtowrite = SVAL(inbuf,smb_vwv1);
3019 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
3020 data = smb_buf(inbuf) + 3;
3022 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
3023 END_PROFILE(SMBwrite);
3024 return ERROR_DOS(ERRDOS,ERRlock);
3028 * X/Open SMB protocol says that if smb_vwv1 is
3029 * zero then the file size should be extended or
3030 * truncated to the size given in smb_vwv[2-3].
3033 if(numtowrite == 0) {
3035 * This is actually an allocate call, and set EOF. JRA.
3037 nwritten = vfs_allocate_file_space(fsp, (SMB_OFF_T)startpos);
3038 if (nwritten < 0) {
3039 END_PROFILE(SMBwrite);
3040 return ERROR_NT(NT_STATUS_DISK_FULL);
3042 nwritten = vfs_set_filelen(fsp, (SMB_OFF_T)startpos);
3043 if (nwritten < 0) {
3044 END_PROFILE(SMBwrite);
3045 return ERROR_NT(NT_STATUS_DISK_FULL);
3047 } else
3048 nwritten = write_file(fsp,data,startpos,numtowrite);
3050 sync_file(conn, fsp, False);
3052 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
3053 END_PROFILE(SMBwrite);
3054 return(UNIXERROR(ERRHRD,ERRdiskfull));
3057 outsize = set_message(inbuf,outbuf,1,0,True);
3059 SSVAL(outbuf,smb_vwv0,nwritten);
3061 if (nwritten < (ssize_t)numtowrite) {
3062 SCVAL(outbuf,smb_rcls,ERRHRD);
3063 SSVAL(outbuf,smb_err,ERRdiskfull);
3066 DEBUG(3,("write fnum=%d num=%d wrote=%d\n", fsp->fnum, (int)numtowrite, (int)nwritten));
3068 END_PROFILE(SMBwrite);
3069 return(outsize);
3072 /****************************************************************************
3073 Reply to a write and X.
3074 ****************************************************************************/
3076 int reply_write_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
3078 files_struct *fsp = file_fsp(inbuf,smb_vwv2);
3079 SMB_OFF_T startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
3080 size_t numtowrite = SVAL(inbuf,smb_vwv10);
3081 BOOL write_through = BITSETW(inbuf+smb_vwv7,0);
3082 ssize_t nwritten = -1;
3083 unsigned int smb_doff = SVAL(inbuf,smb_vwv11);
3084 unsigned int smblen = smb_len(inbuf);
3085 char *data;
3086 BOOL large_writeX = ((CVAL(inbuf,smb_wct) == 14) && (smblen > 0xFFFF));
3087 START_PROFILE(SMBwriteX);
3089 /* If it's an IPC, pass off the pipe handler. */
3090 if (IS_IPC(conn)) {
3091 END_PROFILE(SMBwriteX);
3092 return reply_pipe_write_and_X(inbuf,outbuf,length,bufsize);
3095 CHECK_FSP(fsp,conn);
3096 if (!CHECK_WRITE(fsp)) {
3097 return(ERROR_DOS(ERRDOS,ERRbadaccess));
3100 set_message(inbuf,outbuf,6,0,True);
3102 /* Deal with possible LARGE_WRITEX */
3103 if (large_writeX) {
3104 numtowrite |= ((((size_t)SVAL(inbuf,smb_vwv9)) & 1 )<<16);
3107 if(smb_doff > smblen || (smb_doff + numtowrite > smblen)) {
3108 END_PROFILE(SMBwriteX);
3109 return ERROR_DOS(ERRDOS,ERRbadmem);
3112 data = smb_base(inbuf) + smb_doff;
3114 if(CVAL(inbuf,smb_wct) == 14) {
3115 #ifdef LARGE_SMB_OFF_T
3117 * This is a large offset (64 bit) write.
3119 startpos |= (((SMB_OFF_T)IVAL(inbuf,smb_vwv12)) << 32);
3121 #else /* !LARGE_SMB_OFF_T */
3124 * Ensure we haven't been sent a >32 bit offset.
3127 if(IVAL(inbuf,smb_vwv12) != 0) {
3128 DEBUG(0,("reply_write_and_X - large offset (%x << 32) used and we don't support \
3129 64 bit offsets.\n", (unsigned int)IVAL(inbuf,smb_vwv12) ));
3130 END_PROFILE(SMBwriteX);
3131 return ERROR_DOS(ERRDOS,ERRbadaccess);
3134 #endif /* LARGE_SMB_OFF_T */
3137 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
3138 END_PROFILE(SMBwriteX);
3139 return ERROR_DOS(ERRDOS,ERRlock);
3142 /* X/Open SMB protocol says that, unlike SMBwrite
3143 if the length is zero then NO truncation is
3144 done, just a write of zero. To truncate a file,
3145 use SMBwrite. */
3147 if(numtowrite == 0) {
3148 nwritten = 0;
3149 } else {
3151 if (schedule_aio_write_and_X(conn, inbuf, outbuf, length, bufsize,
3152 fsp,data,startpos,numtowrite)) {
3153 END_PROFILE(SMBwriteX);
3154 return -1;
3157 nwritten = write_file(fsp,data,startpos,numtowrite);
3160 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
3161 END_PROFILE(SMBwriteX);
3162 return(UNIXERROR(ERRHRD,ERRdiskfull));
3165 SSVAL(outbuf,smb_vwv2,nwritten);
3166 if (large_writeX)
3167 SSVAL(outbuf,smb_vwv4,(nwritten>>16)&1);
3169 if (nwritten < (ssize_t)numtowrite) {
3170 SCVAL(outbuf,smb_rcls,ERRHRD);
3171 SSVAL(outbuf,smb_err,ERRdiskfull);
3174 DEBUG(3,("writeX fnum=%d num=%d wrote=%d\n",
3175 fsp->fnum, (int)numtowrite, (int)nwritten));
3177 sync_file(conn, fsp, write_through);
3179 END_PROFILE(SMBwriteX);
3180 return chain_reply(inbuf,outbuf,length,bufsize);
3183 /****************************************************************************
3184 Reply to a lseek.
3185 ****************************************************************************/
3187 int reply_lseek(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
3189 SMB_OFF_T startpos;
3190 SMB_OFF_T res= -1;
3191 int mode,umode;
3192 int outsize = 0;
3193 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3194 START_PROFILE(SMBlseek);
3196 CHECK_FSP(fsp,conn);
3198 flush_write_cache(fsp, SEEK_FLUSH);
3200 mode = SVAL(inbuf,smb_vwv1) & 3;
3201 /* NB. This doesn't use IVAL_TO_SMB_OFF_T as startpos can be signed in this case. */
3202 startpos = (SMB_OFF_T)IVALS(inbuf,smb_vwv2);
3204 switch (mode) {
3205 case 0:
3206 umode = SEEK_SET;
3207 res = startpos;
3208 break;
3209 case 1:
3210 umode = SEEK_CUR;
3211 res = fsp->fh->pos + startpos;
3212 break;
3213 case 2:
3214 umode = SEEK_END;
3215 break;
3216 default:
3217 umode = SEEK_SET;
3218 res = startpos;
3219 break;
3222 if (umode == SEEK_END) {
3223 if((res = SMB_VFS_LSEEK(fsp,fsp->fh->fd,startpos,umode)) == -1) {
3224 if(errno == EINVAL) {
3225 SMB_OFF_T current_pos = startpos;
3226 SMB_STRUCT_STAT sbuf;
3228 if(SMB_VFS_FSTAT(fsp,fsp->fh->fd, &sbuf) == -1) {
3229 END_PROFILE(SMBlseek);
3230 return(UNIXERROR(ERRDOS,ERRnoaccess));
3233 current_pos += sbuf.st_size;
3234 if(current_pos < 0)
3235 res = SMB_VFS_LSEEK(fsp,fsp->fh->fd,0,SEEK_SET);
3239 if(res == -1) {
3240 END_PROFILE(SMBlseek);
3241 return(UNIXERROR(ERRDOS,ERRnoaccess));
3245 fsp->fh->pos = res;
3247 outsize = set_message(inbuf,outbuf,2,0,True);
3248 SIVAL(outbuf,smb_vwv0,res);
3250 DEBUG(3,("lseek fnum=%d ofs=%.0f newpos = %.0f mode=%d\n",
3251 fsp->fnum, (double)startpos, (double)res, mode));
3253 END_PROFILE(SMBlseek);
3254 return(outsize);
3257 /****************************************************************************
3258 Reply to a flush.
3259 ****************************************************************************/
3261 int reply_flush(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
3263 int outsize = set_message(inbuf,outbuf,0,0,False);
3264 uint16 fnum = SVAL(inbuf,smb_vwv0);
3265 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3266 START_PROFILE(SMBflush);
3268 if (fnum != 0xFFFF)
3269 CHECK_FSP(fsp,conn);
3271 if (!fsp) {
3272 file_sync_all(conn);
3273 } else {
3274 sync_file(conn,fsp, True);
3277 DEBUG(3,("flush\n"));
3278 END_PROFILE(SMBflush);
3279 return(outsize);
3282 /****************************************************************************
3283 Reply to a exit.
3284 conn POINTER CAN BE NULL HERE !
3285 ****************************************************************************/
3287 int reply_exit(connection_struct *conn,
3288 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3290 int outsize;
3291 START_PROFILE(SMBexit);
3293 file_close_pid(SVAL(inbuf,smb_pid),SVAL(inbuf,smb_uid));
3295 outsize = set_message(inbuf,outbuf,0,0,False);
3297 DEBUG(3,("exit\n"));
3299 END_PROFILE(SMBexit);
3300 return(outsize);
3303 /****************************************************************************
3304 Reply to a close - has to deal with closing a directory opened by NT SMB's.
3305 ****************************************************************************/
3307 int reply_close(connection_struct *conn, char *inbuf,char *outbuf, int size,
3308 int dum_buffsize)
3310 NTSTATUS status = NT_STATUS_OK;
3311 int outsize = 0;
3312 files_struct *fsp = NULL;
3313 START_PROFILE(SMBclose);
3315 outsize = set_message(inbuf,outbuf,0,0,False);
3317 /* If it's an IPC, pass off to the pipe handler. */
3318 if (IS_IPC(conn)) {
3319 END_PROFILE(SMBclose);
3320 return reply_pipe_close(conn, inbuf,outbuf);
3323 fsp = file_fsp(inbuf,smb_vwv0);
3326 * We can only use CHECK_FSP if we know it's not a directory.
3329 if(!fsp || (fsp->conn != conn) || (fsp->vuid != current_user.vuid)) {
3330 END_PROFILE(SMBclose);
3331 return ERROR_DOS(ERRDOS,ERRbadfid);
3334 if(fsp->is_directory) {
3336 * Special case - close NT SMB directory handle.
3338 DEBUG(3,("close directory fnum=%d\n", fsp->fnum));
3339 status = close_file(fsp,NORMAL_CLOSE);
3340 } else {
3342 * Close ordinary file.
3345 DEBUG(3,("close fd=%d fnum=%d (numopen=%d)\n",
3346 fsp->fh->fd, fsp->fnum,
3347 conn->num_files_open));
3350 * Take care of any time sent in the close.
3353 fsp_set_pending_modtime(fsp,
3354 convert_time_t_to_timespec(srv_make_unix_date3(inbuf+smb_vwv1)));
3357 * close_file() returns the unix errno if an error
3358 * was detected on close - normally this is due to
3359 * a disk full error. If not then it was probably an I/O error.
3362 status = close_file(fsp,NORMAL_CLOSE);
3365 if(!NT_STATUS_IS_OK(status)) {
3366 END_PROFILE(SMBclose);
3367 return ERROR_NT(status);
3370 END_PROFILE(SMBclose);
3371 return(outsize);
3374 /****************************************************************************
3375 Reply to a writeclose (Core+ protocol).
3376 ****************************************************************************/
3378 int reply_writeclose(connection_struct *conn,
3379 char *inbuf,char *outbuf, int size, int dum_buffsize)
3381 size_t numtowrite;
3382 ssize_t nwritten = -1;
3383 int outsize = 0;
3384 NTSTATUS close_status = NT_STATUS_OK;
3385 SMB_OFF_T startpos;
3386 char *data;
3387 struct timespec mtime;
3388 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3389 START_PROFILE(SMBwriteclose);
3391 CHECK_FSP(fsp,conn);
3392 if (!CHECK_WRITE(fsp)) {
3393 return(ERROR_DOS(ERRDOS,ERRbadaccess));
3396 numtowrite = SVAL(inbuf,smb_vwv1);
3397 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
3398 mtime = convert_time_t_to_timespec(srv_make_unix_date3(inbuf+smb_vwv4));
3399 data = smb_buf(inbuf) + 1;
3401 if (numtowrite && is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
3402 END_PROFILE(SMBwriteclose);
3403 return ERROR_DOS(ERRDOS,ERRlock);
3406 nwritten = write_file(fsp,data,startpos,numtowrite);
3408 set_filetime(conn, fsp->fsp_name, mtime);
3411 * More insanity. W2K only closes the file if writelen > 0.
3412 * JRA.
3415 if (numtowrite) {
3416 DEBUG(3,("reply_writeclose: zero length write doesn't close file %s\n",
3417 fsp->fsp_name ));
3418 close_status = close_file(fsp,NORMAL_CLOSE);
3421 DEBUG(3,("writeclose fnum=%d num=%d wrote=%d (numopen=%d)\n",
3422 fsp->fnum, (int)numtowrite, (int)nwritten,
3423 conn->num_files_open));
3425 if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
3426 END_PROFILE(SMBwriteclose);
3427 return(UNIXERROR(ERRHRD,ERRdiskfull));
3430 if(!NT_STATUS_IS_OK(close_status)) {
3431 END_PROFILE(SMBwriteclose);
3432 return ERROR_NT(close_status);
3435 outsize = set_message(inbuf,outbuf,1,0,True);
3437 SSVAL(outbuf,smb_vwv0,nwritten);
3438 END_PROFILE(SMBwriteclose);
3439 return(outsize);
3442 #undef DBGC_CLASS
3443 #define DBGC_CLASS DBGC_LOCKING
3445 /****************************************************************************
3446 Reply to a lock.
3447 ****************************************************************************/
3449 int reply_lock(connection_struct *conn,
3450 char *inbuf,char *outbuf, int length, int dum_buffsize)
3452 int outsize = set_message(inbuf,outbuf,0,0,False);
3453 SMB_BIG_UINT count,offset;
3454 NTSTATUS status;
3455 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3456 struct byte_range_lock *br_lck = NULL;
3458 START_PROFILE(SMBlock);
3460 CHECK_FSP(fsp,conn);
3462 release_level_2_oplocks_on_change(fsp);
3464 count = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv1);
3465 offset = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv3);
3467 DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
3468 fsp->fh->fd, fsp->fnum, (double)offset, (double)count));
3470 br_lck = do_lock(smbd_messaging_context(),
3471 fsp,
3472 (uint32)SVAL(inbuf,smb_pid),
3473 count,
3474 offset,
3475 WRITE_LOCK,
3476 WINDOWS_LOCK,
3477 False, /* Non-blocking lock. */
3478 &status,
3479 NULL);
3481 TALLOC_FREE(br_lck);
3483 if (NT_STATUS_V(status)) {
3484 END_PROFILE(SMBlock);
3485 return ERROR_NT(status);
3488 END_PROFILE(SMBlock);
3489 return(outsize);
3492 /****************************************************************************
3493 Reply to a unlock.
3494 ****************************************************************************/
3496 int reply_unlock(connection_struct *conn, char *inbuf,char *outbuf, int size,
3497 int dum_buffsize)
3499 int outsize = set_message(inbuf,outbuf,0,0,False);
3500 SMB_BIG_UINT count,offset;
3501 NTSTATUS status;
3502 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3503 START_PROFILE(SMBunlock);
3505 CHECK_FSP(fsp,conn);
3507 count = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv1);
3508 offset = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv3);
3510 status = do_unlock(smbd_messaging_context(),
3511 fsp,
3512 (uint32)SVAL(inbuf,smb_pid),
3513 count,
3514 offset,
3515 WINDOWS_LOCK);
3517 if (NT_STATUS_V(status)) {
3518 END_PROFILE(SMBunlock);
3519 return ERROR_NT(status);
3522 DEBUG( 3, ( "unlock fd=%d fnum=%d offset=%.0f count=%.0f\n",
3523 fsp->fh->fd, fsp->fnum, (double)offset, (double)count ) );
3525 END_PROFILE(SMBunlock);
3526 return(outsize);
3529 #undef DBGC_CLASS
3530 #define DBGC_CLASS DBGC_ALL
3532 /****************************************************************************
3533 Reply to a tdis.
3534 conn POINTER CAN BE NULL HERE !
3535 ****************************************************************************/
3537 int reply_tdis(connection_struct *conn,
3538 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3540 int outsize = set_message(inbuf,outbuf,0,0,False);
3541 uint16 vuid;
3542 START_PROFILE(SMBtdis);
3544 vuid = SVAL(inbuf,smb_uid);
3546 if (!conn) {
3547 DEBUG(4,("Invalid connection in tdis\n"));
3548 END_PROFILE(SMBtdis);
3549 return ERROR_DOS(ERRSRV,ERRinvnid);
3552 conn->used = False;
3554 close_cnum(conn,vuid);
3556 END_PROFILE(SMBtdis);
3557 return outsize;
3560 /****************************************************************************
3561 Reply to a echo.
3562 conn POINTER CAN BE NULL HERE !
3563 ****************************************************************************/
3565 int reply_echo(connection_struct *conn,
3566 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3568 int smb_reverb = SVAL(inbuf,smb_vwv0);
3569 int seq_num;
3570 unsigned int data_len = smb_buflen(inbuf);
3571 int outsize = set_message(inbuf,outbuf,1,data_len,True);
3572 START_PROFILE(SMBecho);
3574 if (data_len > BUFFER_SIZE) {
3575 DEBUG(0,("reply_echo: data_len too large.\n"));
3576 END_PROFILE(SMBecho);
3577 return -1;
3580 /* copy any incoming data back out */
3581 if (data_len > 0)
3582 memcpy(smb_buf(outbuf),smb_buf(inbuf),data_len);
3584 if (smb_reverb > 100) {
3585 DEBUG(0,("large reverb (%d)?? Setting to 100\n",smb_reverb));
3586 smb_reverb = 100;
3589 for (seq_num =1 ; seq_num <= smb_reverb ; seq_num++) {
3590 SSVAL(outbuf,smb_vwv0,seq_num);
3592 smb_setlen(inbuf,outbuf,outsize - 4);
3594 show_msg(outbuf);
3595 if (!send_smb(smbd_server_fd(),outbuf))
3596 exit_server_cleanly("reply_echo: send_smb failed.");
3599 DEBUG(3,("echo %d times\n", smb_reverb));
3601 smb_echo_count++;
3603 END_PROFILE(SMBecho);
3604 return -1;
3607 /****************************************************************************
3608 Reply to a printopen.
3609 ****************************************************************************/
3611 int reply_printopen(connection_struct *conn,
3612 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3614 int outsize = 0;
3615 files_struct *fsp;
3616 NTSTATUS status;
3618 START_PROFILE(SMBsplopen);
3620 if (!CAN_PRINT(conn)) {
3621 END_PROFILE(SMBsplopen);
3622 return ERROR_DOS(ERRDOS,ERRnoaccess);
3625 /* Open for exclusive use, write only. */
3626 status = print_fsp_open(conn, NULL, &fsp);
3628 if (!NT_STATUS_IS_OK(status)) {
3629 END_PROFILE(SMBsplopen);
3630 return(ERROR_NT(status));
3633 outsize = set_message(inbuf,outbuf,1,0,True);
3634 SSVAL(outbuf,smb_vwv0,fsp->fnum);
3636 DEBUG(3,("openprint fd=%d fnum=%d\n",
3637 fsp->fh->fd, fsp->fnum));
3639 END_PROFILE(SMBsplopen);
3640 return(outsize);
3643 /****************************************************************************
3644 Reply to a printclose.
3645 ****************************************************************************/
3647 int reply_printclose(connection_struct *conn,
3648 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3650 int outsize = set_message(inbuf,outbuf,0,0,False);
3651 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3652 NTSTATUS status;
3653 START_PROFILE(SMBsplclose);
3655 CHECK_FSP(fsp,conn);
3657 if (!CAN_PRINT(conn)) {
3658 END_PROFILE(SMBsplclose);
3659 return ERROR_NT(NT_STATUS_DOS(ERRSRV, ERRerror));
3662 DEBUG(3,("printclose fd=%d fnum=%d\n",
3663 fsp->fh->fd,fsp->fnum));
3665 status = close_file(fsp,NORMAL_CLOSE);
3667 if(!NT_STATUS_IS_OK(status)) {
3668 END_PROFILE(SMBsplclose);
3669 return ERROR_NT(status);
3672 END_PROFILE(SMBsplclose);
3673 return(outsize);
3676 /****************************************************************************
3677 Reply to a printqueue.
3678 ****************************************************************************/
3680 int reply_printqueue(connection_struct *conn,
3681 char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3683 int outsize = set_message(inbuf,outbuf,2,3,True);
3684 int max_count = SVAL(inbuf,smb_vwv0);
3685 int start_index = SVAL(inbuf,smb_vwv1);
3686 START_PROFILE(SMBsplretq);
3688 /* we used to allow the client to get the cnum wrong, but that
3689 is really quite gross and only worked when there was only
3690 one printer - I think we should now only accept it if they
3691 get it right (tridge) */
3692 if (!CAN_PRINT(conn)) {
3693 END_PROFILE(SMBsplretq);
3694 return ERROR_DOS(ERRDOS,ERRnoaccess);
3697 SSVAL(outbuf,smb_vwv0,0);
3698 SSVAL(outbuf,smb_vwv1,0);
3699 SCVAL(smb_buf(outbuf),0,1);
3700 SSVAL(smb_buf(outbuf),1,0);
3702 DEBUG(3,("printqueue start_index=%d max_count=%d\n",
3703 start_index, max_count));
3706 print_queue_struct *queue = NULL;
3707 print_status_struct status;
3708 char *p = smb_buf(outbuf) + 3;
3709 int count = print_queue_status(SNUM(conn), &queue, &status);
3710 int num_to_get = ABS(max_count);
3711 int first = (max_count>0?start_index:start_index+max_count+1);
3712 int i;
3714 if (first >= count)
3715 num_to_get = 0;
3716 else
3717 num_to_get = MIN(num_to_get,count-first);
3720 for (i=first;i<first+num_to_get;i++) {
3721 srv_put_dos_date2(p,0,queue[i].time);
3722 SCVAL(p,4,(queue[i].status==LPQ_PRINTING?2:3));
3723 SSVAL(p,5, queue[i].job);
3724 SIVAL(p,7,queue[i].size);
3725 SCVAL(p,11,0);
3726 srvstr_push(outbuf, p+12, queue[i].fs_user, 16, STR_ASCII);
3727 p += 28;
3730 if (count > 0) {
3731 outsize = set_message(inbuf,outbuf,2,28*count+3,False);
3732 SSVAL(outbuf,smb_vwv0,count);
3733 SSVAL(outbuf,smb_vwv1,(max_count>0?first+count:first-1));
3734 SCVAL(smb_buf(outbuf),0,1);
3735 SSVAL(smb_buf(outbuf),1,28*count);
3738 SAFE_FREE(queue);
3740 DEBUG(3,("%d entries returned in queue\n",count));
3743 END_PROFILE(SMBsplretq);
3744 return(outsize);
3747 /****************************************************************************
3748 Reply to a printwrite.
3749 ****************************************************************************/
3751 int reply_printwrite(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3753 int numtowrite;
3754 int outsize = set_message(inbuf,outbuf,0,0,False);
3755 char *data;
3756 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
3758 START_PROFILE(SMBsplwr);
3760 if (!CAN_PRINT(conn)) {
3761 END_PROFILE(SMBsplwr);
3762 return ERROR_DOS(ERRDOS,ERRnoaccess);
3765 CHECK_FSP(fsp,conn);
3766 if (!CHECK_WRITE(fsp)) {
3767 return(ERROR_DOS(ERRDOS,ERRbadaccess));
3770 numtowrite = SVAL(smb_buf(inbuf),1);
3771 data = smb_buf(inbuf) + 3;
3773 if (write_file(fsp,data,-1,numtowrite) != numtowrite) {
3774 END_PROFILE(SMBsplwr);
3775 return(UNIXERROR(ERRHRD,ERRdiskfull));
3778 DEBUG( 3, ( "printwrite fnum=%d num=%d\n", fsp->fnum, numtowrite ) );
3780 END_PROFILE(SMBsplwr);
3781 return(outsize);
3784 /****************************************************************************
3785 Reply to a mkdir.
3786 ****************************************************************************/
3788 int reply_mkdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
3790 pstring directory;
3791 int outsize;
3792 NTSTATUS status;
3793 SMB_STRUCT_STAT sbuf;
3795 START_PROFILE(SMBmkdir);
3797 srvstr_get_path(inbuf, directory, smb_buf(inbuf) + 1, sizeof(directory), 0, STR_TERMINATE, &status);
3798 if (!NT_STATUS_IS_OK(status)) {
3799 END_PROFILE(SMBmkdir);
3800 return ERROR_NT(status);
3803 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, directory);
3804 if (!NT_STATUS_IS_OK(status)) {
3805 END_PROFILE(SMBmkdir);
3806 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
3807 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
3809 return ERROR_NT(status);
3812 status = unix_convert(conn, directory, False, NULL, &sbuf);
3813 if (!NT_STATUS_IS_OK(status)) {
3814 END_PROFILE(SMBmkdir);
3815 return ERROR_NT(status);
3818 status = check_name(conn, directory);
3819 if (!NT_STATUS_IS_OK(status)) {
3820 END_PROFILE(SMBmkdir);
3821 return ERROR_NT(status);
3824 status = create_directory(conn, directory);
3826 DEBUG(5, ("create_directory returned %s\n", nt_errstr(status)));
3828 if (!NT_STATUS_IS_OK(status)) {
3830 if (!use_nt_status()
3831 && NT_STATUS_EQUAL(status,
3832 NT_STATUS_OBJECT_NAME_COLLISION)) {
3834 * Yes, in the DOS error code case we get a
3835 * ERRDOS:ERRnoaccess here. See BASE-SAMBA3ERROR
3836 * samba4 torture test.
3838 status = NT_STATUS_DOS(ERRDOS, ERRnoaccess);
3841 END_PROFILE(SMBmkdir);
3842 return ERROR_NT(status);
3845 outsize = set_message(inbuf,outbuf,0,0,False);
3847 DEBUG( 3, ( "mkdir %s ret=%d\n", directory, outsize ) );
3849 END_PROFILE(SMBmkdir);
3850 return(outsize);
3853 /****************************************************************************
3854 Static function used by reply_rmdir to delete an entire directory
3855 tree recursively. Return True on ok, False on fail.
3856 ****************************************************************************/
3858 static BOOL recursive_rmdir(connection_struct *conn, char *directory)
3860 const char *dname = NULL;
3861 BOOL ret = True;
3862 long offset = 0;
3863 struct smb_Dir *dir_hnd = OpenDir(conn, directory, NULL, 0);
3865 if(dir_hnd == NULL)
3866 return False;
3868 while((dname = ReadDirName(dir_hnd, &offset))) {
3869 pstring fullname;
3870 SMB_STRUCT_STAT st;
3872 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
3873 continue;
3875 if (!is_visible_file(conn, directory, dname, &st, False))
3876 continue;
3878 /* Construct the full name. */
3879 if(strlen(directory) + strlen(dname) + 1 >= sizeof(fullname)) {
3880 errno = ENOMEM;
3881 ret = False;
3882 break;
3885 pstrcpy(fullname, directory);
3886 pstrcat(fullname, "/");
3887 pstrcat(fullname, dname);
3889 if(SMB_VFS_LSTAT(conn,fullname, &st) != 0) {
3890 ret = False;
3891 break;
3894 if(st.st_mode & S_IFDIR) {
3895 if(!recursive_rmdir(conn, fullname)) {
3896 ret = False;
3897 break;
3899 if(SMB_VFS_RMDIR(conn,fullname) != 0) {
3900 ret = False;
3901 break;
3903 } else if(SMB_VFS_UNLINK(conn,fullname) != 0) {
3904 ret = False;
3905 break;
3908 CloseDir(dir_hnd);
3909 return ret;
3912 /****************************************************************************
3913 The internals of the rmdir code - called elsewhere.
3914 ****************************************************************************/
3916 NTSTATUS rmdir_internals(connection_struct *conn, const char *directory)
3918 int ret;
3919 SMB_STRUCT_STAT st;
3921 /* Might be a symlink. */
3922 if(SMB_VFS_LSTAT(conn, directory, &st) != 0) {
3923 return map_nt_error_from_unix(errno);
3926 if (S_ISLNK(st.st_mode)) {
3927 /* Is what it points to a directory ? */
3928 if(SMB_VFS_STAT(conn, directory, &st) != 0) {
3929 return map_nt_error_from_unix(errno);
3931 if (!(S_ISDIR(st.st_mode))) {
3932 return NT_STATUS_NOT_A_DIRECTORY;
3934 ret = SMB_VFS_UNLINK(conn,directory);
3935 } else {
3936 ret = SMB_VFS_RMDIR(conn,directory);
3938 if (ret == 0) {
3939 notify_fname(conn, NOTIFY_ACTION_REMOVED,
3940 FILE_NOTIFY_CHANGE_DIR_NAME,
3941 directory);
3942 return NT_STATUS_OK;
3945 if(((errno == ENOTEMPTY)||(errno == EEXIST)) && lp_veto_files(SNUM(conn))) {
3947 * Check to see if the only thing in this directory are
3948 * vetoed files/directories. If so then delete them and
3949 * retry. If we fail to delete any of them (and we *don't*
3950 * do a recursive delete) then fail the rmdir.
3952 const char *dname;
3953 long dirpos = 0;
3954 struct smb_Dir *dir_hnd = OpenDir(conn, directory, NULL, 0);
3956 if(dir_hnd == NULL) {
3957 errno = ENOTEMPTY;
3958 goto err;
3961 while ((dname = ReadDirName(dir_hnd,&dirpos))) {
3962 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
3963 continue;
3964 if (!is_visible_file(conn, directory, dname, &st, False))
3965 continue;
3966 if(!IS_VETO_PATH(conn, dname)) {
3967 CloseDir(dir_hnd);
3968 errno = ENOTEMPTY;
3969 goto err;
3973 /* We only have veto files/directories. Recursive delete. */
3975 RewindDir(dir_hnd,&dirpos);
3976 while ((dname = ReadDirName(dir_hnd,&dirpos))) {
3977 pstring fullname;
3979 if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
3980 continue;
3981 if (!is_visible_file(conn, directory, dname, &st, False))
3982 continue;
3984 /* Construct the full name. */
3985 if(strlen(directory) + strlen(dname) + 1 >= sizeof(fullname)) {
3986 errno = ENOMEM;
3987 break;
3990 pstrcpy(fullname, directory);
3991 pstrcat(fullname, "/");
3992 pstrcat(fullname, dname);
3994 if(SMB_VFS_LSTAT(conn,fullname, &st) != 0)
3995 break;
3996 if(st.st_mode & S_IFDIR) {
3997 if(lp_recursive_veto_delete(SNUM(conn))) {
3998 if(!recursive_rmdir(conn, fullname))
3999 break;
4001 if(SMB_VFS_RMDIR(conn,fullname) != 0)
4002 break;
4003 } else if(SMB_VFS_UNLINK(conn,fullname) != 0)
4004 break;
4006 CloseDir(dir_hnd);
4007 /* Retry the rmdir */
4008 ret = SMB_VFS_RMDIR(conn,directory);
4011 err:
4013 if (ret != 0) {
4014 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
4015 "%s\n", directory,strerror(errno)));
4016 return map_nt_error_from_unix(errno);
4019 notify_fname(conn, NOTIFY_ACTION_REMOVED,
4020 FILE_NOTIFY_CHANGE_DIR_NAME,
4021 directory);
4023 return NT_STATUS_OK;
4026 /****************************************************************************
4027 Reply to a rmdir.
4028 ****************************************************************************/
4030 int reply_rmdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
4032 pstring directory;
4033 int outsize = 0;
4034 SMB_STRUCT_STAT sbuf;
4035 NTSTATUS status;
4036 START_PROFILE(SMBrmdir);
4038 srvstr_get_path(inbuf, directory, smb_buf(inbuf) + 1, sizeof(directory), 0, STR_TERMINATE, &status);
4039 if (!NT_STATUS_IS_OK(status)) {
4040 END_PROFILE(SMBrmdir);
4041 return ERROR_NT(status);
4044 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, directory);
4045 if (!NT_STATUS_IS_OK(status)) {
4046 END_PROFILE(SMBrmdir);
4047 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
4048 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
4050 return ERROR_NT(status);
4053 status = unix_convert(conn, directory, False, NULL, &sbuf);
4054 if (!NT_STATUS_IS_OK(status)) {
4055 END_PROFILE(SMBrmdir);
4056 return ERROR_NT(status);
4059 status = check_name(conn, directory);
4060 if (!NT_STATUS_IS_OK(status)) {
4061 END_PROFILE(SMBrmdir);
4062 return ERROR_NT(status);
4065 dptr_closepath(directory,SVAL(inbuf,smb_pid));
4066 status = rmdir_internals(conn, directory);
4067 if (!NT_STATUS_IS_OK(status)) {
4068 END_PROFILE(SMBrmdir);
4069 return ERROR_NT(status);
4072 outsize = set_message(inbuf,outbuf,0,0,False);
4074 DEBUG( 3, ( "rmdir %s\n", directory ) );
4076 END_PROFILE(SMBrmdir);
4077 return(outsize);
4080 /*******************************************************************
4081 Resolve wildcards in a filename rename.
4082 Note that name is in UNIX charset and thus potentially can be more
4083 than fstring buffer (255 bytes) especially in default UTF-8 case.
4084 Therefore, we use pstring inside and all calls should ensure that
4085 name2 is at least pstring-long (they do already)
4086 ********************************************************************/
4088 static BOOL resolve_wildcards(const char *name1, char *name2)
4090 pstring root1,root2;
4091 pstring ext1,ext2;
4092 char *p,*p2, *pname1, *pname2;
4093 int available_space, actual_space;
4095 pname1 = strrchr_m(name1,'/');
4096 pname2 = strrchr_m(name2,'/');
4098 if (!pname1 || !pname2)
4099 return(False);
4101 pstrcpy(root1,pname1);
4102 pstrcpy(root2,pname2);
4103 p = strrchr_m(root1,'.');
4104 if (p) {
4105 *p = 0;
4106 pstrcpy(ext1,p+1);
4107 } else {
4108 pstrcpy(ext1,"");
4110 p = strrchr_m(root2,'.');
4111 if (p) {
4112 *p = 0;
4113 pstrcpy(ext2,p+1);
4114 } else {
4115 pstrcpy(ext2,"");
4118 p = root1;
4119 p2 = root2;
4120 while (*p2) {
4121 if (*p2 == '?') {
4122 *p2 = *p;
4123 p2++;
4124 } else if (*p2 == '*') {
4125 pstrcpy(p2, p);
4126 break;
4127 } else {
4128 p2++;
4130 if (*p)
4131 p++;
4134 p = ext1;
4135 p2 = ext2;
4136 while (*p2) {
4137 if (*p2 == '?') {
4138 *p2 = *p;
4139 p2++;
4140 } else if (*p2 == '*') {
4141 pstrcpy(p2, p);
4142 break;
4143 } else {
4144 p2++;
4146 if (*p)
4147 p++;
4150 available_space = sizeof(pstring) - PTR_DIFF(pname2, name2);
4152 if (ext2[0]) {
4153 actual_space = snprintf(pname2, available_space - 1, "%s.%s", root2, ext2);
4154 if (actual_space >= available_space - 1) {
4155 DEBUG(1,("resolve_wildcards: can't fit resolved name into specified buffer (overrun by %d bytes)\n",
4156 actual_space - available_space));
4158 } else {
4159 pstrcpy_base(pname2, root2, name2);
4162 return(True);
4165 /****************************************************************************
4166 Ensure open files have their names updated. Updated to notify other smbd's
4167 asynchronously.
4168 ****************************************************************************/
4170 static void rename_open_files(connection_struct *conn, struct share_mode_lock *lck,
4171 SMB_DEV_T dev, SMB_INO_T inode, const char *newname)
4173 files_struct *fsp;
4174 BOOL did_rename = False;
4176 for(fsp = file_find_di_first(dev, inode); fsp; fsp = file_find_di_next(fsp)) {
4177 /* fsp_name is a relative path under the fsp. To change this for other
4178 sharepaths we need to manipulate relative paths. */
4179 /* TODO - create the absolute path and manipulate the newname
4180 relative to the sharepath. */
4181 if (fsp->conn != conn) {
4182 continue;
4184 DEBUG(10,("rename_open_files: renaming file fnum %d (dev = %x, inode = %.0f) from %s -> %s\n",
4185 fsp->fnum, (unsigned int)fsp->dev, (double)fsp->inode,
4186 fsp->fsp_name, newname ));
4187 string_set(&fsp->fsp_name, newname);
4188 did_rename = True;
4191 if (!did_rename) {
4192 DEBUG(10,("rename_open_files: no open files on dev %x, inode %.0f for %s\n",
4193 (unsigned int)dev, (double)inode, newname ));
4196 /* Send messages to all smbd's (not ourself) that the name has changed. */
4197 rename_share_filename(smbd_messaging_context(), lck, conn->connectpath,
4198 newname);
4201 /****************************************************************************
4202 We need to check if the source path is a parent directory of the destination
4203 (ie. a rename of /foo/bar/baz -> /foo/bar/baz/bibble/bobble. If so we must
4204 refuse the rename with a sharing violation. Under UNIX the above call can
4205 *succeed* if /foo/bar/baz is a symlink to another area in the share. We
4206 probably need to check that the client is a Windows one before disallowing
4207 this as a UNIX client (one with UNIX extensions) can know the source is a
4208 symlink and make this decision intelligently. Found by an excellent bug
4209 report from <AndyLiebman@aol.com>.
4210 ****************************************************************************/
4212 static BOOL rename_path_prefix_equal(const char *src, const char *dest)
4214 const char *psrc = src;
4215 const char *pdst = dest;
4216 size_t slen;
4218 if (psrc[0] == '.' && psrc[1] == '/') {
4219 psrc += 2;
4221 if (pdst[0] == '.' && pdst[1] == '/') {
4222 pdst += 2;
4224 if ((slen = strlen(psrc)) > strlen(pdst)) {
4225 return False;
4227 return ((memcmp(psrc, pdst, slen) == 0) && pdst[slen] == '/');
4230 /****************************************************************************
4231 Rename an open file - given an fsp.
4232 ****************************************************************************/
4234 NTSTATUS rename_internals_fsp(connection_struct *conn, files_struct *fsp, pstring newname, uint32 attrs, BOOL replace_if_exists)
4236 SMB_STRUCT_STAT sbuf;
4237 pstring newname_last_component;
4238 NTSTATUS status = NT_STATUS_OK;
4239 BOOL dest_exists;
4240 struct share_mode_lock *lck = NULL;
4242 ZERO_STRUCT(sbuf);
4244 status = unix_convert(conn, newname, False, newname_last_component, &sbuf);
4245 if (!NT_STATUS_IS_OK(status)) {
4246 return status;
4249 status = check_name(conn, newname);
4250 if (!NT_STATUS_IS_OK(status)) {
4251 return status;
4254 /* Ensure newname contains a '/' */
4255 if(strrchr_m(newname,'/') == 0) {
4256 pstring tmpstr;
4258 pstrcpy(tmpstr, "./");
4259 pstrcat(tmpstr, newname);
4260 pstrcpy(newname, tmpstr);
4264 * Check for special case with case preserving and not
4265 * case sensitive. If the old last component differs from the original
4266 * last component only by case, then we should allow
4267 * the rename (user is trying to change the case of the
4268 * filename).
4271 if((conn->case_sensitive == False) && (conn->case_preserve == True) &&
4272 strequal(newname, fsp->fsp_name)) {
4273 char *p;
4274 pstring newname_modified_last_component;
4277 * Get the last component of the modified name.
4278 * Note that we guarantee that newname contains a '/'
4279 * character above.
4281 p = strrchr_m(newname,'/');
4282 pstrcpy(newname_modified_last_component,p+1);
4284 if(strcsequal(newname_modified_last_component,
4285 newname_last_component) == False) {
4287 * Replace the modified last component with
4288 * the original.
4290 pstrcpy(p+1, newname_last_component);
4295 * If the src and dest names are identical - including case,
4296 * don't do the rename, just return success.
4299 if (strcsequal(fsp->fsp_name, newname)) {
4300 DEBUG(3,("rename_internals_fsp: identical names in rename %s - returning success\n",
4301 newname));
4302 return NT_STATUS_OK;
4305 dest_exists = vfs_object_exist(conn,newname,NULL);
4307 if(!replace_if_exists && dest_exists) {
4308 DEBUG(3,("rename_internals_fsp: dest exists doing rename %s -> %s\n",
4309 fsp->fsp_name,newname));
4310 return NT_STATUS_OBJECT_NAME_COLLISION;
4313 status = can_rename(conn,newname,attrs,&sbuf);
4315 if (dest_exists && !NT_STATUS_IS_OK(status)) {
4316 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4317 nt_errstr(status), fsp->fsp_name,newname));
4318 if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION))
4319 status = NT_STATUS_ACCESS_DENIED;
4320 return status;
4323 if (rename_path_prefix_equal(fsp->fsp_name, newname)) {
4324 return NT_STATUS_ACCESS_DENIED;
4327 lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
4329 if(SMB_VFS_RENAME(conn,fsp->fsp_name, newname) == 0) {
4330 DEBUG(3,("rename_internals_fsp: succeeded doing rename on %s -> %s\n",
4331 fsp->fsp_name,newname));
4332 rename_open_files(conn, lck, fsp->dev, fsp->inode, newname);
4333 TALLOC_FREE(lck);
4334 return NT_STATUS_OK;
4337 TALLOC_FREE(lck);
4339 if (errno == ENOTDIR || errno == EISDIR) {
4340 status = NT_STATUS_OBJECT_NAME_COLLISION;
4341 } else {
4342 status = map_nt_error_from_unix(errno);
4345 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
4346 nt_errstr(status), fsp->fsp_name,newname));
4348 return status;
4352 * Do the notify calls from a rename
4355 static void notify_rename(connection_struct *conn, BOOL is_dir,
4356 const char *oldpath, const char *newpath)
4358 char *olddir, *newdir;
4359 const char *oldname, *newname;
4360 uint32 mask;
4362 mask = is_dir ? FILE_NOTIFY_CHANGE_DIR_NAME
4363 : FILE_NOTIFY_CHANGE_FILE_NAME;
4365 if (!parent_dirname_talloc(NULL, oldpath, &olddir, &oldname)
4366 || !parent_dirname_talloc(NULL, newpath, &newdir, &newname)) {
4367 TALLOC_FREE(olddir);
4368 return;
4371 if (strcmp(olddir, newdir) == 0) {
4372 notify_fname(conn, NOTIFY_ACTION_OLD_NAME, mask, oldpath);
4373 notify_fname(conn, NOTIFY_ACTION_NEW_NAME, mask, newpath);
4375 else {
4376 notify_fname(conn, NOTIFY_ACTION_REMOVED, mask, oldpath);
4377 notify_fname(conn, NOTIFY_ACTION_ADDED, mask, newpath);
4379 TALLOC_FREE(olddir);
4380 TALLOC_FREE(newdir);
4382 /* this is a strange one. w2k3 gives an additional event for
4383 CHANGE_ATTRIBUTES and CHANGE_CREATION on the new file when renaming
4384 files, but not directories */
4385 if (!is_dir) {
4386 notify_fname(conn, NOTIFY_ACTION_MODIFIED,
4387 FILE_NOTIFY_CHANGE_ATTRIBUTES
4388 |FILE_NOTIFY_CHANGE_CREATION,
4389 newpath);
4393 /****************************************************************************
4394 The guts of the rename command, split out so it may be called by the NT SMB
4395 code.
4396 ****************************************************************************/
4398 NTSTATUS rename_internals(connection_struct *conn,
4399 pstring name,
4400 pstring newname,
4401 uint32 attrs,
4402 BOOL replace_if_exists,
4403 BOOL src_has_wild,
4404 BOOL dest_has_wild)
4406 pstring directory;
4407 pstring mask;
4408 pstring last_component_src;
4409 pstring last_component_dest;
4410 char *p;
4411 int count=0;
4412 NTSTATUS status = NT_STATUS_OK;
4413 SMB_STRUCT_STAT sbuf1, sbuf2;
4414 struct share_mode_lock *lck = NULL;
4415 struct smb_Dir *dir_hnd = NULL;
4416 const char *dname;
4417 long offset = 0;
4418 pstring destname;
4420 *directory = *mask = 0;
4422 ZERO_STRUCT(sbuf1);
4423 ZERO_STRUCT(sbuf2);
4425 status = unix_convert(conn, name, src_has_wild, last_component_src, &sbuf1);
4426 if (!NT_STATUS_IS_OK(status)) {
4427 return status;
4430 status = unix_convert(conn, newname, dest_has_wild, last_component_dest, &sbuf2);
4431 if (!NT_STATUS_IS_OK(status)) {
4432 return status;
4436 * Split the old name into directory and last component
4437 * strings. Note that unix_convert may have stripped off a
4438 * leading ./ from both name and newname if the rename is
4439 * at the root of the share. We need to make sure either both
4440 * name and newname contain a / character or neither of them do
4441 * as this is checked in resolve_wildcards().
4444 p = strrchr_m(name,'/');
4445 if (!p) {
4446 pstrcpy(directory,".");
4447 pstrcpy(mask,name);
4448 } else {
4449 *p = 0;
4450 pstrcpy(directory,name);
4451 pstrcpy(mask,p+1);
4452 *p = '/'; /* Replace needed for exceptional test below. */
4456 * We should only check the mangled cache
4457 * here if unix_convert failed. This means
4458 * that the path in 'mask' doesn't exist
4459 * on the file system and so we need to look
4460 * for a possible mangle. This patch from
4461 * Tine Smukavec <valentin.smukavec@hermes.si>.
4464 if (!VALID_STAT(sbuf1) && mangle_is_mangled(mask, conn->params)) {
4465 mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
4468 if (!src_has_wild) {
4470 * No wildcards - just process the one file.
4472 BOOL is_short_name = mangle_is_8_3(name, True, conn->params);
4474 /* Add a terminating '/' to the directory name. */
4475 pstrcat(directory,"/");
4476 pstrcat(directory,mask);
4478 /* Ensure newname contains a '/' also */
4479 if(strrchr_m(newname,'/') == 0) {
4480 pstring tmpstr;
4482 pstrcpy(tmpstr, "./");
4483 pstrcat(tmpstr, newname);
4484 pstrcpy(newname, tmpstr);
4487 DEBUG(3, ("rename_internals: case_sensitive = %d, "
4488 "case_preserve = %d, short case preserve = %d, "
4489 "directory = %s, newname = %s, "
4490 "last_component_dest = %s, is_8_3 = %d\n",
4491 conn->case_sensitive, conn->case_preserve,
4492 conn->short_case_preserve, directory,
4493 newname, last_component_dest, is_short_name));
4495 /* Ensure the source name is valid for us to access. */
4496 status = check_name(conn, directory);
4497 if (!NT_STATUS_IS_OK(status)) {
4498 return status;
4501 /* The dest name still may have wildcards. */
4502 if (dest_has_wild) {
4503 if (!resolve_wildcards(directory,newname)) {
4504 DEBUG(6, ("rename_internals: resolve_wildcards %s %s failed\n",
4505 directory,newname));
4506 return NT_STATUS_NO_MEMORY;
4511 * Check for special case with case preserving and not
4512 * case sensitive, if directory and newname are identical,
4513 * and the old last component differs from the original
4514 * last component only by case, then we should allow
4515 * the rename (user is trying to change the case of the
4516 * filename).
4518 if((conn->case_sensitive == False) &&
4519 (((conn->case_preserve == True) &&
4520 (is_short_name == False)) ||
4521 ((conn->short_case_preserve == True) &&
4522 (is_short_name == True))) &&
4523 strcsequal(directory, newname)) {
4524 pstring modified_last_component;
4527 * Get the last component of the modified name.
4528 * Note that we guarantee that newname contains a '/'
4529 * character above.
4531 p = strrchr_m(newname,'/');
4532 pstrcpy(modified_last_component,p+1);
4534 if(strcsequal(modified_last_component,
4535 last_component_dest) == False) {
4537 * Replace the modified last component with
4538 * the original.
4540 pstrcpy(p+1, last_component_dest);
4544 /* Ensure the dest name is valid for us to access. */
4545 status = check_name(conn, newname);
4546 if (!NT_STATUS_IS_OK(status)) {
4547 return status;
4551 * The source object must exist.
4554 if (!vfs_object_exist(conn, directory, &sbuf1)) {
4555 DEBUG(3, ("rename_internals: source doesn't exist "
4556 "doing rename %s -> %s\n",
4557 directory,newname));
4559 if (errno == ENOTDIR || errno == EISDIR
4560 || errno == ENOENT) {
4562 * Must return different errors depending on
4563 * whether the parent directory existed or
4564 * not.
4567 p = strrchr_m(directory, '/');
4568 if (!p)
4569 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4570 *p = '\0';
4571 if (vfs_object_exist(conn, directory, NULL))
4572 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4573 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
4575 status = map_nt_error_from_unix(errno);
4576 DEBUG(3, ("rename_internals: Error %s rename %s -> "
4577 "%s\n", nt_errstr(status), directory,
4578 newname));
4580 return status;
4583 status = can_rename(conn,directory,attrs,&sbuf1);
4585 if (!NT_STATUS_IS_OK(status)) {
4586 DEBUG(3,("rename_internals: Error %s rename %s -> "
4587 "%s\n", nt_errstr(status), directory,
4588 newname));
4589 return status;
4593 * If the src and dest names are identical - including case,
4594 * don't do the rename, just return success.
4597 if (strcsequal(directory, newname)) {
4598 rename_open_files(conn, NULL, sbuf1.st_dev,
4599 sbuf1.st_ino, newname);
4600 DEBUG(3, ("rename_internals: identical names in "
4601 "rename %s - returning success\n",
4602 directory));
4603 return NT_STATUS_OK;
4606 if(!replace_if_exists && vfs_object_exist(conn,newname,NULL)) {
4607 DEBUG(3,("rename_internals: dest exists doing "
4608 "rename %s -> %s\n", directory, newname));
4609 return NT_STATUS_OBJECT_NAME_COLLISION;
4612 if (rename_path_prefix_equal(directory, newname)) {
4613 return NT_STATUS_SHARING_VIOLATION;
4616 lck = get_share_mode_lock(NULL, sbuf1.st_dev, sbuf1.st_ino,
4617 NULL, NULL);
4619 if(SMB_VFS_RENAME(conn,directory, newname) == 0) {
4620 DEBUG(3,("rename_internals: succeeded doing rename "
4621 "on %s -> %s\n", directory, newname));
4622 rename_open_files(conn, lck, sbuf1.st_dev,
4623 sbuf1.st_ino, newname);
4624 TALLOC_FREE(lck);
4625 notify_rename(conn, S_ISDIR(sbuf1.st_mode),
4626 directory, newname);
4627 return NT_STATUS_OK;
4630 TALLOC_FREE(lck);
4631 if (errno == ENOTDIR || errno == EISDIR) {
4632 status = NT_STATUS_OBJECT_NAME_COLLISION;
4633 } else {
4634 status = map_nt_error_from_unix(errno);
4637 DEBUG(3,("rename_internals: Error %s rename %s -> %s\n",
4638 nt_errstr(status), directory,newname));
4640 return status;
4644 * Wildcards - process each file that matches.
4646 if (strequal(mask,"????????.???")) {
4647 pstrcpy(mask,"*");
4650 status = check_name(conn, directory);
4651 if (!NT_STATUS_IS_OK(status)) {
4652 return status;
4655 dir_hnd = OpenDir(conn, directory, mask, attrs);
4656 if (dir_hnd == NULL) {
4657 return map_nt_error_from_unix(errno);
4660 status = NT_STATUS_NO_SUCH_FILE;
4662 * Was status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4663 * - gentest fix. JRA
4666 while ((dname = ReadDirName(dir_hnd, &offset))) {
4667 pstring fname;
4668 BOOL sysdir_entry = False;
4670 pstrcpy(fname,dname);
4672 /* Quick check for "." and ".." */
4673 if (fname[0] == '.') {
4674 if (!fname[1] || (fname[1] == '.' && !fname[2])) {
4675 if (attrs & aDIR) {
4676 sysdir_entry = True;
4677 } else {
4678 continue;
4683 if (!is_visible_file(conn, directory, dname, &sbuf1, False)) {
4684 continue;
4687 if(!mask_match(fname, mask, conn->case_sensitive)) {
4688 continue;
4691 if (sysdir_entry) {
4692 status = NT_STATUS_OBJECT_NAME_INVALID;
4693 break;
4696 status = NT_STATUS_ACCESS_DENIED;
4697 slprintf(fname, sizeof(fname)-1, "%s/%s", directory, dname);
4699 /* Ensure the source name is valid for us to access. */
4700 status = check_name(conn, fname);
4701 if (!NT_STATUS_IS_OK(status)) {
4702 return status;
4705 if (!vfs_object_exist(conn, fname, &sbuf1)) {
4706 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
4707 DEBUG(6, ("rename %s failed. Error %s\n",
4708 fname, nt_errstr(status)));
4709 continue;
4711 status = can_rename(conn,fname,attrs,&sbuf1);
4712 if (!NT_STATUS_IS_OK(status)) {
4713 DEBUG(6, ("rename %s refused\n", fname));
4714 continue;
4716 pstrcpy(destname,newname);
4718 if (!resolve_wildcards(fname,destname)) {
4719 DEBUG(6, ("resolve_wildcards %s %s failed\n",
4720 fname, destname));
4721 continue;
4724 /* Ensure the dest name is valid for us to access. */
4725 status = check_name(conn, destname);
4726 if (!NT_STATUS_IS_OK(status)) {
4727 return status;
4730 if (strcsequal(fname,destname)) {
4731 rename_open_files(conn, NULL, sbuf1.st_dev,
4732 sbuf1.st_ino, newname);
4733 DEBUG(3,("rename_internals: identical names "
4734 "in wildcard rename %s - success\n",
4735 fname));
4736 count++;
4737 status = NT_STATUS_OK;
4738 continue;
4741 if (!replace_if_exists && vfs_file_exist(conn,destname, NULL)) {
4742 DEBUG(6,("file_exist %s\n", destname));
4743 status = NT_STATUS_OBJECT_NAME_COLLISION;
4744 continue;
4747 if (rename_path_prefix_equal(fname, destname)) {
4748 return NT_STATUS_SHARING_VIOLATION;
4751 lck = get_share_mode_lock(NULL, sbuf1.st_dev,
4752 sbuf1.st_ino, NULL, NULL);
4754 if (!SMB_VFS_RENAME(conn,fname,destname)) {
4755 rename_open_files(conn, lck, sbuf1.st_dev,
4756 sbuf1.st_ino, newname);
4757 count++;
4758 status = NT_STATUS_OK;
4760 TALLOC_FREE(lck);
4761 DEBUG(3,("rename_internals: doing rename on %s -> "
4762 "%s\n",fname,destname));
4764 CloseDir(dir_hnd);
4766 if (count == 0 && NT_STATUS_IS_OK(status)) {
4767 status = map_nt_error_from_unix(errno);
4770 return status;
4773 /****************************************************************************
4774 Reply to a mv.
4775 ****************************************************************************/
4777 int reply_mv(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
4778 int dum_buffsize)
4780 int outsize = 0;
4781 pstring name;
4782 pstring newname;
4783 char *p;
4784 uint32 attrs = SVAL(inbuf,smb_vwv0);
4785 NTSTATUS status;
4786 BOOL src_has_wcard = False;
4787 BOOL dest_has_wcard = False;
4789 START_PROFILE(SMBmv);
4791 p = smb_buf(inbuf) + 1;
4792 p += srvstr_get_path_wcard(inbuf, name, p, sizeof(name), 0, STR_TERMINATE, &status, &src_has_wcard);
4793 if (!NT_STATUS_IS_OK(status)) {
4794 END_PROFILE(SMBmv);
4795 return ERROR_NT(status);
4797 p++;
4798 p += srvstr_get_path_wcard(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status, &dest_has_wcard);
4799 if (!NT_STATUS_IS_OK(status)) {
4800 END_PROFILE(SMBmv);
4801 return ERROR_NT(status);
4804 status = resolve_dfspath_wcard(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, name, &src_has_wcard);
4805 if (!NT_STATUS_IS_OK(status)) {
4806 END_PROFILE(SMBmv);
4807 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
4808 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
4810 return ERROR_NT(status);
4813 status = resolve_dfspath_wcard(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, newname, &dest_has_wcard);
4814 if (!NT_STATUS_IS_OK(status)) {
4815 END_PROFILE(SMBmv);
4816 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
4817 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
4819 return ERROR_NT(status);
4822 DEBUG(3,("reply_mv : %s -> %s\n",name,newname));
4824 status = rename_internals(conn, name, newname, attrs, False, src_has_wcard, dest_has_wcard);
4825 if (!NT_STATUS_IS_OK(status)) {
4826 END_PROFILE(SMBmv);
4827 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
4828 /* We have re-scheduled this call. */
4829 return -1;
4831 return ERROR_NT(status);
4834 outsize = set_message(inbuf,outbuf,0,0,False);
4836 END_PROFILE(SMBmv);
4837 return(outsize);
4840 /*******************************************************************
4841 Copy a file as part of a reply_copy.
4842 ******************************************************************/
4845 * TODO: check error codes on all callers
4848 NTSTATUS copy_file(connection_struct *conn,
4849 char *src,
4850 char *dest1,
4851 int ofun,
4852 int count,
4853 BOOL target_is_directory)
4855 SMB_STRUCT_STAT src_sbuf, sbuf2;
4856 SMB_OFF_T ret=-1;
4857 files_struct *fsp1,*fsp2;
4858 pstring dest;
4859 uint32 dosattrs;
4860 uint32 new_create_disposition;
4861 NTSTATUS status;
4863 pstrcpy(dest,dest1);
4864 if (target_is_directory) {
4865 char *p = strrchr_m(src,'/');
4866 if (p) {
4867 p++;
4868 } else {
4869 p = src;
4871 pstrcat(dest,"/");
4872 pstrcat(dest,p);
4875 if (!vfs_file_exist(conn,src,&src_sbuf)) {
4876 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
4879 if (!target_is_directory && count) {
4880 new_create_disposition = FILE_OPEN;
4881 } else {
4882 if (!map_open_params_to_ntcreate(dest1,0,ofun,
4883 NULL, NULL, &new_create_disposition, NULL)) {
4884 return NT_STATUS_INVALID_PARAMETER;
4888 status = open_file_ntcreate(conn,src,&src_sbuf,
4889 FILE_GENERIC_READ,
4890 FILE_SHARE_READ|FILE_SHARE_WRITE,
4891 FILE_OPEN,
4893 FILE_ATTRIBUTE_NORMAL,
4894 INTERNAL_OPEN_ONLY,
4895 NULL, &fsp1);
4897 if (!NT_STATUS_IS_OK(status)) {
4898 return status;
4901 dosattrs = dos_mode(conn, src, &src_sbuf);
4902 if (SMB_VFS_STAT(conn,dest,&sbuf2) == -1) {
4903 ZERO_STRUCTP(&sbuf2);
4906 status = open_file_ntcreate(conn,dest,&sbuf2,
4907 FILE_GENERIC_WRITE,
4908 FILE_SHARE_READ|FILE_SHARE_WRITE,
4909 new_create_disposition,
4911 dosattrs,
4912 INTERNAL_OPEN_ONLY,
4913 NULL, &fsp2);
4915 if (!NT_STATUS_IS_OK(status)) {
4916 close_file(fsp1,ERROR_CLOSE);
4917 return status;
4920 if ((ofun&3) == 1) {
4921 if(SMB_VFS_LSEEK(fsp2,fsp2->fh->fd,0,SEEK_END) == -1) {
4922 DEBUG(0,("copy_file: error - vfs lseek returned error %s\n", strerror(errno) ));
4924 * Stop the copy from occurring.
4926 ret = -1;
4927 src_sbuf.st_size = 0;
4931 if (src_sbuf.st_size) {
4932 ret = vfs_transfer_file(fsp1, fsp2, src_sbuf.st_size);
4935 close_file(fsp1,NORMAL_CLOSE);
4937 /* Ensure the modtime is set correctly on the destination file. */
4938 fsp_set_pending_modtime( fsp2, get_mtimespec(&src_sbuf));
4941 * As we are opening fsp1 read-only we only expect
4942 * an error on close on fsp2 if we are out of space.
4943 * Thus we don't look at the error return from the
4944 * close of fsp1.
4946 status = close_file(fsp2,NORMAL_CLOSE);
4948 if (!NT_STATUS_IS_OK(status)) {
4949 return status;
4952 if (ret != (SMB_OFF_T)src_sbuf.st_size) {
4953 return NT_STATUS_DISK_FULL;
4956 return NT_STATUS_OK;
4959 /****************************************************************************
4960 Reply to a file copy.
4961 ****************************************************************************/
4963 int reply_copy(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
4965 int outsize = 0;
4966 pstring name;
4967 pstring directory;
4968 pstring mask,newname;
4969 char *p;
4970 int count=0;
4971 int error = ERRnoaccess;
4972 int err = 0;
4973 int tid2 = SVAL(inbuf,smb_vwv0);
4974 int ofun = SVAL(inbuf,smb_vwv1);
4975 int flags = SVAL(inbuf,smb_vwv2);
4976 BOOL target_is_directory=False;
4977 BOOL source_has_wild = False;
4978 BOOL dest_has_wild = False;
4979 SMB_STRUCT_STAT sbuf1, sbuf2;
4980 NTSTATUS status;
4981 START_PROFILE(SMBcopy);
4983 *directory = *mask = 0;
4985 p = smb_buf(inbuf);
4986 p += srvstr_get_path_wcard(inbuf, name, p, sizeof(name), 0, STR_TERMINATE, &status, &source_has_wild);
4987 if (!NT_STATUS_IS_OK(status)) {
4988 END_PROFILE(SMBcopy);
4989 return ERROR_NT(status);
4991 p += srvstr_get_path_wcard(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status, &dest_has_wild);
4992 if (!NT_STATUS_IS_OK(status)) {
4993 END_PROFILE(SMBcopy);
4994 return ERROR_NT(status);
4997 DEBUG(3,("reply_copy : %s -> %s\n",name,newname));
4999 if (tid2 != conn->cnum) {
5000 /* can't currently handle inter share copies XXXX */
5001 DEBUG(3,("Rejecting inter-share copy\n"));
5002 END_PROFILE(SMBcopy);
5003 return ERROR_DOS(ERRSRV,ERRinvdevice);
5006 status = resolve_dfspath_wcard(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, name, &source_has_wild);
5007 if (!NT_STATUS_IS_OK(status)) {
5008 END_PROFILE(SMBcopy);
5009 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
5010 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
5012 return ERROR_NT(status);
5015 status = resolve_dfspath_wcard(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, newname, &dest_has_wild);
5016 if (!NT_STATUS_IS_OK(status)) {
5017 END_PROFILE(SMBcopy);
5018 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
5019 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
5021 return ERROR_NT(status);
5024 status = unix_convert(conn, name, source_has_wild, NULL, &sbuf1);
5025 if (!NT_STATUS_IS_OK(status)) {
5026 END_PROFILE(SMBcopy);
5027 return ERROR_NT(status);
5030 status = unix_convert(conn, newname, dest_has_wild, NULL, &sbuf2);
5031 if (!NT_STATUS_IS_OK(status)) {
5032 END_PROFILE(SMBcopy);
5033 return ERROR_NT(status);
5036 target_is_directory = VALID_STAT_OF_DIR(sbuf2);
5038 if ((flags&1) && target_is_directory) {
5039 END_PROFILE(SMBcopy);
5040 return ERROR_DOS(ERRDOS,ERRbadfile);
5043 if ((flags&2) && !target_is_directory) {
5044 END_PROFILE(SMBcopy);
5045 return ERROR_DOS(ERRDOS,ERRbadpath);
5048 if ((flags&(1<<5)) && VALID_STAT_OF_DIR(sbuf1)) {
5049 /* wants a tree copy! XXXX */
5050 DEBUG(3,("Rejecting tree copy\n"));
5051 END_PROFILE(SMBcopy);
5052 return ERROR_DOS(ERRSRV,ERRerror);
5055 p = strrchr_m(name,'/');
5056 if (!p) {
5057 pstrcpy(directory,"./");
5058 pstrcpy(mask,name);
5059 } else {
5060 *p = 0;
5061 pstrcpy(directory,name);
5062 pstrcpy(mask,p+1);
5066 * We should only check the mangled cache
5067 * here if unix_convert failed. This means
5068 * that the path in 'mask' doesn't exist
5069 * on the file system and so we need to look
5070 * for a possible mangle. This patch from
5071 * Tine Smukavec <valentin.smukavec@hermes.si>.
5074 if (!VALID_STAT(sbuf1) && mangle_is_mangled(mask, conn->params)) {
5075 mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
5078 if (!source_has_wild) {
5079 pstrcat(directory,"/");
5080 pstrcat(directory,mask);
5081 if (dest_has_wild) {
5082 if (!resolve_wildcards(directory,newname)) {
5083 END_PROFILE(SMBcopy);
5084 return ERROR_NT(NT_STATUS_NO_MEMORY);
5088 status = check_name(conn, directory);
5089 if (!NT_STATUS_IS_OK(status)) {
5090 return ERROR_NT(status);
5093 status = check_name(conn, newname);
5094 if (!NT_STATUS_IS_OK(status)) {
5095 return ERROR_NT(status);
5098 status = copy_file(conn,directory,newname,ofun,
5099 count,target_is_directory);
5101 if(!NT_STATUS_IS_OK(status)) {
5102 END_PROFILE(SMBcopy);
5103 return ERROR_NT(status);
5104 } else {
5105 count++;
5107 } else {
5108 struct smb_Dir *dir_hnd = NULL;
5109 const char *dname;
5110 long offset = 0;
5111 pstring destname;
5113 if (strequal(mask,"????????.???"))
5114 pstrcpy(mask,"*");
5116 status = check_name(conn, directory);
5117 if (!NT_STATUS_IS_OK(status)) {
5118 return ERROR_NT(status);
5121 dir_hnd = OpenDir(conn, directory, mask, 0);
5122 if (dir_hnd == NULL) {
5123 status = map_nt_error_from_unix(errno);
5124 return ERROR_NT(status);
5127 error = ERRbadfile;
5129 while ((dname = ReadDirName(dir_hnd, &offset))) {
5130 pstring fname;
5131 pstrcpy(fname,dname);
5133 if (!is_visible_file(conn, directory, dname, &sbuf1, False)) {
5134 continue;
5137 if(!mask_match(fname, mask, conn->case_sensitive)) {
5138 continue;
5141 error = ERRnoaccess;
5142 slprintf(fname,sizeof(fname)-1, "%s/%s",directory,dname);
5143 pstrcpy(destname,newname);
5144 if (!resolve_wildcards(fname,destname)) {
5145 continue;
5148 status = check_name(conn, fname);
5149 if (!NT_STATUS_IS_OK(status)) {
5150 return ERROR_NT(status);
5153 status = check_name(conn, destname);
5154 if (!NT_STATUS_IS_OK(status)) {
5155 return ERROR_NT(status);
5158 DEBUG(3,("reply_copy : doing copy on %s -> %s\n",fname, destname));
5160 status = copy_file(conn,fname,destname,ofun,
5161 count,target_is_directory);
5162 if (NT_STATUS_IS_OK(status)) {
5163 count++;
5166 CloseDir(dir_hnd);
5169 if (count == 0) {
5170 if(err) {
5171 /* Error on close... */
5172 errno = err;
5173 END_PROFILE(SMBcopy);
5174 return(UNIXERROR(ERRHRD,ERRgeneral));
5177 END_PROFILE(SMBcopy);
5178 return ERROR_DOS(ERRDOS,error);
5181 outsize = set_message(inbuf,outbuf,1,0,True);
5182 SSVAL(outbuf,smb_vwv0,count);
5184 END_PROFILE(SMBcopy);
5185 return(outsize);
5188 /****************************************************************************
5189 Reply to a setdir.
5190 ****************************************************************************/
5192 int reply_setdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
5194 int snum;
5195 int outsize = 0;
5196 pstring newdir;
5197 NTSTATUS status;
5199 START_PROFILE(pathworks_setdir);
5201 snum = SNUM(conn);
5202 if (!CAN_SETDIR(snum)) {
5203 END_PROFILE(pathworks_setdir);
5204 return ERROR_DOS(ERRDOS,ERRnoaccess);
5207 srvstr_get_path(inbuf, newdir, smb_buf(inbuf) + 1, sizeof(newdir), 0, STR_TERMINATE, &status);
5208 if (!NT_STATUS_IS_OK(status)) {
5209 END_PROFILE(pathworks_setdir);
5210 return ERROR_NT(status);
5213 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, newdir);
5214 if (!NT_STATUS_IS_OK(status)) {
5215 END_PROFILE(pathworks_setdir);
5216 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
5217 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
5219 return ERROR_NT(status);
5222 if (strlen(newdir) != 0) {
5223 if (!vfs_directory_exist(conn,newdir,NULL)) {
5224 END_PROFILE(pathworks_setdir);
5225 return ERROR_DOS(ERRDOS,ERRbadpath);
5227 set_conn_connectpath(conn,newdir);
5230 outsize = set_message(inbuf,outbuf,0,0,False);
5231 SCVAL(outbuf,smb_reh,CVAL(inbuf,smb_reh));
5233 DEBUG(3,("setdir %s\n", newdir));
5235 END_PROFILE(pathworks_setdir);
5236 return(outsize);
5239 #undef DBGC_CLASS
5240 #define DBGC_CLASS DBGC_LOCKING
5242 /****************************************************************************
5243 Get a lock pid, dealing with large count requests.
5244 ****************************************************************************/
5246 uint32 get_lock_pid( char *data, int data_offset, BOOL large_file_format)
5248 if(!large_file_format)
5249 return (uint32)SVAL(data,SMB_LPID_OFFSET(data_offset));
5250 else
5251 return (uint32)SVAL(data,SMB_LARGE_LPID_OFFSET(data_offset));
5254 /****************************************************************************
5255 Get a lock count, dealing with large count requests.
5256 ****************************************************************************/
5258 SMB_BIG_UINT get_lock_count( char *data, int data_offset, BOOL large_file_format)
5260 SMB_BIG_UINT count = 0;
5262 if(!large_file_format) {
5263 count = (SMB_BIG_UINT)IVAL(data,SMB_LKLEN_OFFSET(data_offset));
5264 } else {
5266 #if defined(HAVE_LONGLONG)
5267 count = (((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset))) << 32) |
5268 ((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)));
5269 #else /* HAVE_LONGLONG */
5272 * NT4.x seems to be broken in that it sends large file (64 bit)
5273 * lockingX calls even if the CAP_LARGE_FILES was *not*
5274 * negotiated. For boxes without large unsigned ints truncate the
5275 * lock count by dropping the top 32 bits.
5278 if(IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)) != 0) {
5279 DEBUG(3,("get_lock_count: truncating lock count (high)0x%x (low)0x%x to just low count.\n",
5280 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)),
5281 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)) ));
5282 SIVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset),0);
5285 count = (SMB_BIG_UINT)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset));
5286 #endif /* HAVE_LONGLONG */
5289 return count;
5292 #if !defined(HAVE_LONGLONG)
5293 /****************************************************************************
5294 Pathetically try and map a 64 bit lock offset into 31 bits. I hate Windows :-).
5295 ****************************************************************************/
5297 static uint32 map_lock_offset(uint32 high, uint32 low)
5299 unsigned int i;
5300 uint32 mask = 0;
5301 uint32 highcopy = high;
5304 * Try and find out how many significant bits there are in high.
5307 for(i = 0; highcopy; i++)
5308 highcopy >>= 1;
5311 * We use 31 bits not 32 here as POSIX
5312 * lock offsets may not be negative.
5315 mask = (~0) << (31 - i);
5317 if(low & mask)
5318 return 0; /* Fail. */
5320 high <<= (31 - i);
5322 return (high|low);
5324 #endif /* !defined(HAVE_LONGLONG) */
5326 /****************************************************************************
5327 Get a lock offset, dealing with large offset requests.
5328 ****************************************************************************/
5330 SMB_BIG_UINT get_lock_offset( char *data, int data_offset, BOOL large_file_format, BOOL *err)
5332 SMB_BIG_UINT offset = 0;
5334 *err = False;
5336 if(!large_file_format) {
5337 offset = (SMB_BIG_UINT)IVAL(data,SMB_LKOFF_OFFSET(data_offset));
5338 } else {
5340 #if defined(HAVE_LONGLONG)
5341 offset = (((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset))) << 32) |
5342 ((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset)));
5343 #else /* HAVE_LONGLONG */
5346 * NT4.x seems to be broken in that it sends large file (64 bit)
5347 * lockingX calls even if the CAP_LARGE_FILES was *not*
5348 * negotiated. For boxes without large unsigned ints mangle the
5349 * lock offset by mapping the top 32 bits onto the lower 32.
5352 if(IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset)) != 0) {
5353 uint32 low = IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
5354 uint32 high = IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset));
5355 uint32 new_low = 0;
5357 if((new_low = map_lock_offset(high, low)) == 0) {
5358 *err = True;
5359 return (SMB_BIG_UINT)-1;
5362 DEBUG(3,("get_lock_offset: truncating lock offset (high)0x%x (low)0x%x to offset 0x%x.\n",
5363 (unsigned int)high, (unsigned int)low, (unsigned int)new_low ));
5364 SIVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset),0);
5365 SIVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset),new_low);
5368 offset = (SMB_BIG_UINT)IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
5369 #endif /* HAVE_LONGLONG */
5372 return offset;
5375 /****************************************************************************
5376 Reply to a lockingX request.
5377 ****************************************************************************/
5379 int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
5380 int length, int bufsize)
5382 files_struct *fsp = file_fsp(inbuf,smb_vwv2);
5383 unsigned char locktype = CVAL(inbuf,smb_vwv3);
5384 unsigned char oplocklevel = CVAL(inbuf,smb_vwv3+1);
5385 uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
5386 uint16 num_locks = SVAL(inbuf,smb_vwv7);
5387 SMB_BIG_UINT count = 0, offset = 0;
5388 uint32 lock_pid;
5389 int32 lock_timeout = IVAL(inbuf,smb_vwv4);
5390 int i;
5391 char *data;
5392 BOOL large_file_format =
5393 (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
5394 BOOL err;
5395 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
5397 START_PROFILE(SMBlockingX);
5399 CHECK_FSP(fsp,conn);
5401 data = smb_buf(inbuf);
5403 if (locktype & LOCKING_ANDX_CHANGE_LOCKTYPE) {
5404 /* we don't support these - and CANCEL_LOCK makes w2k
5405 and XP reboot so I don't really want to be
5406 compatible! (tridge) */
5407 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks));
5410 /* Check if this is an oplock break on a file
5411 we have granted an oplock on.
5413 if ((locktype & LOCKING_ANDX_OPLOCK_RELEASE)) {
5414 /* Client can insist on breaking to none. */
5415 BOOL break_to_none = (oplocklevel == 0);
5416 BOOL result;
5418 DEBUG(5,("reply_lockingX: oplock break reply (%u) from client "
5419 "for fnum = %d\n", (unsigned int)oplocklevel,
5420 fsp->fnum ));
5423 * Make sure we have granted an exclusive or batch oplock on
5424 * this file.
5427 if (fsp->oplock_type == 0) {
5429 /* The Samba4 nbench simulator doesn't understand
5430 the difference between break to level2 and break
5431 to none from level2 - it sends oplock break
5432 replies in both cases. Don't keep logging an error
5433 message here - just ignore it. JRA. */
5435 DEBUG(5,("reply_lockingX: Error : oplock break from "
5436 "client for fnum = %d (oplock=%d) and no "
5437 "oplock granted on this file (%s).\n",
5438 fsp->fnum, fsp->oplock_type, fsp->fsp_name));
5440 /* if this is a pure oplock break request then don't
5441 * send a reply */
5442 if (num_locks == 0 && num_ulocks == 0) {
5443 END_PROFILE(SMBlockingX);
5444 return -1;
5445 } else {
5446 END_PROFILE(SMBlockingX);
5447 return ERROR_DOS(ERRDOS,ERRlock);
5451 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
5452 (break_to_none)) {
5453 result = remove_oplock(fsp);
5454 } else {
5455 result = downgrade_oplock(fsp);
5458 if (!result) {
5459 DEBUG(0, ("reply_lockingX: error in removing "
5460 "oplock on file %s\n", fsp->fsp_name));
5461 /* Hmmm. Is this panic justified? */
5462 smb_panic("internal tdb error");
5465 reply_to_oplock_break_requests(fsp);
5467 /* if this is a pure oplock break request then don't send a
5468 * reply */
5469 if (num_locks == 0 && num_ulocks == 0) {
5470 /* Sanity check - ensure a pure oplock break is not a
5471 chained request. */
5472 if(CVAL(inbuf,smb_vwv0) != 0xff)
5473 DEBUG(0,("reply_lockingX: Error : pure oplock "
5474 "break is a chained %d request !\n",
5475 (unsigned int)CVAL(inbuf,smb_vwv0) ));
5476 END_PROFILE(SMBlockingX);
5477 return -1;
5482 * We do this check *after* we have checked this is not a oplock break
5483 * response message. JRA.
5486 release_level_2_oplocks_on_change(fsp);
5488 /* Data now points at the beginning of the list
5489 of smb_unlkrng structs */
5490 for(i = 0; i < (int)num_ulocks; i++) {
5491 lock_pid = get_lock_pid( data, i, large_file_format);
5492 count = get_lock_count( data, i, large_file_format);
5493 offset = get_lock_offset( data, i, large_file_format, &err);
5496 * There is no error code marked "stupid client bug".... :-).
5498 if(err) {
5499 END_PROFILE(SMBlockingX);
5500 return ERROR_DOS(ERRDOS,ERRnoaccess);
5503 DEBUG(10,("reply_lockingX: unlock start=%.0f, len=%.0f for "
5504 "pid %u, file %s\n", (double)offset, (double)count,
5505 (unsigned int)lock_pid, fsp->fsp_name ));
5507 status = do_unlock(smbd_messaging_context(),
5508 fsp,
5509 lock_pid,
5510 count,
5511 offset,
5512 WINDOWS_LOCK);
5514 if (NT_STATUS_V(status)) {
5515 END_PROFILE(SMBlockingX);
5516 return ERROR_NT(status);
5520 /* Setup the timeout in seconds. */
5522 if (!lp_blocking_locks(SNUM(conn))) {
5523 lock_timeout = 0;
5526 /* Now do any requested locks */
5527 data += ((large_file_format ? 20 : 10)*num_ulocks);
5529 /* Data now points at the beginning of the list
5530 of smb_lkrng structs */
5532 for(i = 0; i < (int)num_locks; i++) {
5533 enum brl_type lock_type = ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
5534 READ_LOCK:WRITE_LOCK);
5535 lock_pid = get_lock_pid( data, i, large_file_format);
5536 count = get_lock_count( data, i, large_file_format);
5537 offset = get_lock_offset( data, i, large_file_format, &err);
5540 * There is no error code marked "stupid client bug".... :-).
5542 if(err) {
5543 END_PROFILE(SMBlockingX);
5544 return ERROR_DOS(ERRDOS,ERRnoaccess);
5547 DEBUG(10,("reply_lockingX: lock start=%.0f, len=%.0f for pid "
5548 "%u, file %s timeout = %d\n", (double)offset,
5549 (double)count, (unsigned int)lock_pid,
5550 fsp->fsp_name, (int)lock_timeout ));
5552 if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
5553 if (lp_blocking_locks(SNUM(conn))) {
5555 /* Schedule a message to ourselves to
5556 remove the blocking lock record and
5557 return the right error. */
5559 if (!blocking_lock_cancel(fsp,
5560 lock_pid,
5561 offset,
5562 count,
5563 WINDOWS_LOCK,
5564 locktype,
5565 NT_STATUS_FILE_LOCK_CONFLICT)) {
5566 END_PROFILE(SMBlockingX);
5567 return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRcancelviolation));
5570 /* Remove a matching pending lock. */
5571 status = do_lock_cancel(fsp,
5572 lock_pid,
5573 count,
5574 offset,
5575 WINDOWS_LOCK);
5576 } else {
5577 BOOL blocking_lock = lock_timeout ? True : False;
5578 BOOL defer_lock = False;
5579 struct byte_range_lock *br_lck;
5580 uint32 block_smbpid;
5582 br_lck = do_lock(smbd_messaging_context(),
5583 fsp,
5584 lock_pid,
5585 count,
5586 offset,
5587 lock_type,
5588 WINDOWS_LOCK,
5589 blocking_lock,
5590 &status,
5591 &block_smbpid);
5593 if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
5594 /* Windows internal resolution for blocking locks seems
5595 to be about 200ms... Don't wait for less than that. JRA. */
5596 if (lock_timeout != -1 && lock_timeout < lp_lock_spin_time()) {
5597 lock_timeout = lp_lock_spin_time();
5599 defer_lock = True;
5602 /* This heuristic seems to match W2K3 very well. If a
5603 lock sent with timeout of zero would fail with NT_STATUS_FILE_LOCK_CONFLICT
5604 it pretends we asked for a timeout of between 150 - 300 milliseconds as
5605 far as I can tell. Replacement for do_lock_spin(). JRA. */
5607 if (br_lck && lp_blocking_locks(SNUM(conn)) && !blocking_lock &&
5608 NT_STATUS_EQUAL((status), NT_STATUS_FILE_LOCK_CONFLICT)) {
5609 defer_lock = True;
5610 lock_timeout = lp_lock_spin_time();
5613 if (br_lck && defer_lock) {
5615 * A blocking lock was requested. Package up
5616 * this smb into a queued request and push it
5617 * onto the blocking lock queue.
5619 if(push_blocking_lock_request(br_lck,
5620 inbuf, length,
5621 fsp,
5622 lock_timeout,
5624 lock_pid,
5625 lock_type,
5626 WINDOWS_LOCK,
5627 offset,
5628 count,
5629 block_smbpid)) {
5630 TALLOC_FREE(br_lck);
5631 END_PROFILE(SMBlockingX);
5632 return -1;
5636 TALLOC_FREE(br_lck);
5639 if (NT_STATUS_V(status)) {
5640 END_PROFILE(SMBlockingX);
5641 return ERROR_NT(status);
5645 /* If any of the above locks failed, then we must unlock
5646 all of the previous locks (X/Open spec). */
5648 if (!(locktype & LOCKING_ANDX_CANCEL_LOCK) &&
5649 (i != num_locks) &&
5650 (num_locks != 0)) {
5652 * Ensure we don't do a remove on the lock that just failed,
5653 * as under POSIX rules, if we have a lock already there, we
5654 * will delete it (and we shouldn't) .....
5656 for(i--; i >= 0; i--) {
5657 lock_pid = get_lock_pid( data, i, large_file_format);
5658 count = get_lock_count( data, i, large_file_format);
5659 offset = get_lock_offset( data, i, large_file_format,
5660 &err);
5663 * There is no error code marked "stupid client
5664 * bug".... :-).
5666 if(err) {
5667 END_PROFILE(SMBlockingX);
5668 return ERROR_DOS(ERRDOS,ERRnoaccess);
5671 do_unlock(smbd_messaging_context(),
5672 fsp,
5673 lock_pid,
5674 count,
5675 offset,
5676 WINDOWS_LOCK);
5678 END_PROFILE(SMBlockingX);
5679 return ERROR_NT(status);
5682 set_message(inbuf,outbuf,2,0,True);
5684 DEBUG(3, ("lockingX fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
5685 fsp->fnum, (unsigned int)locktype, num_locks, num_ulocks));
5687 END_PROFILE(SMBlockingX);
5688 return chain_reply(inbuf,outbuf,length,bufsize);
5691 #undef DBGC_CLASS
5692 #define DBGC_CLASS DBGC_ALL
5694 /****************************************************************************
5695 Reply to a SMBreadbmpx (read block multiplex) request.
5696 ****************************************************************************/
5698 int reply_readbmpx(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
5700 ssize_t nread = -1;
5701 ssize_t total_read;
5702 char *data;
5703 SMB_OFF_T startpos;
5704 int outsize;
5705 size_t maxcount;
5706 int max_per_packet;
5707 size_t tcount;
5708 int pad;
5709 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5710 START_PROFILE(SMBreadBmpx);
5712 /* this function doesn't seem to work - disable by default */
5713 if (!lp_readbmpx()) {
5714 END_PROFILE(SMBreadBmpx);
5715 return ERROR_DOS(ERRSRV,ERRuseSTD);
5718 outsize = set_message(inbuf,outbuf,8,0,True);
5720 CHECK_FSP(fsp,conn);
5721 if (!CHECK_READ(fsp,inbuf)) {
5722 return(ERROR_DOS(ERRDOS,ERRbadaccess));
5725 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv1);
5726 maxcount = SVAL(inbuf,smb_vwv3);
5728 data = smb_buf(outbuf);
5729 pad = ((long)data)%4;
5730 if (pad)
5731 pad = 4 - pad;
5732 data += pad;
5734 max_per_packet = bufsize-(outsize+pad);
5735 tcount = maxcount;
5736 total_read = 0;
5738 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
5739 END_PROFILE(SMBreadBmpx);
5740 return ERROR_DOS(ERRDOS,ERRlock);
5743 do {
5744 size_t N = MIN(max_per_packet,tcount-total_read);
5746 nread = read_file(fsp,data,startpos,N);
5748 if (nread <= 0)
5749 nread = 0;
5751 if (nread < (ssize_t)N)
5752 tcount = total_read + nread;
5754 set_message(inbuf,outbuf,8,nread+pad,False);
5755 SIVAL(outbuf,smb_vwv0,startpos);
5756 SSVAL(outbuf,smb_vwv2,tcount);
5757 SSVAL(outbuf,smb_vwv6,nread);
5758 SSVAL(outbuf,smb_vwv7,smb_offset(data,outbuf));
5760 show_msg(outbuf);
5761 if (!send_smb(smbd_server_fd(),outbuf))
5762 exit_server_cleanly("reply_readbmpx: send_smb failed.");
5764 total_read += nread;
5765 startpos += nread;
5766 } while (total_read < (ssize_t)tcount);
5768 END_PROFILE(SMBreadBmpx);
5769 return(-1);
5772 /****************************************************************************
5773 Reply to a SMBsetattrE.
5774 ****************************************************************************/
5776 int reply_setattrE(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
5778 struct timespec ts[2];
5779 int outsize = 0;
5780 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5781 START_PROFILE(SMBsetattrE);
5783 outsize = set_message(inbuf,outbuf,0,0,False);
5785 if(!fsp || (fsp->conn != conn)) {
5786 END_PROFILE(SMBsetattrE);
5787 return ERROR_DOS(ERRDOS,ERRbadfid);
5791 * Convert the DOS times into unix times. Ignore create
5792 * time as UNIX can't set this.
5795 ts[0] = convert_time_t_to_timespec(srv_make_unix_date2(inbuf+smb_vwv3)); /* atime. */
5796 ts[1] = convert_time_t_to_timespec(srv_make_unix_date2(inbuf+smb_vwv5)); /* mtime. */
5799 * Patch from Ray Frush <frush@engr.colostate.edu>
5800 * Sometimes times are sent as zero - ignore them.
5803 if (null_timespec(ts[0]) && null_timespec(ts[1])) {
5804 /* Ignore request */
5805 if( DEBUGLVL( 3 ) ) {
5806 dbgtext( "reply_setattrE fnum=%d ", fsp->fnum);
5807 dbgtext( "ignoring zero request - not setting timestamps of 0\n" );
5809 END_PROFILE(SMBsetattrE);
5810 return(outsize);
5811 } else if (!null_timespec(ts[0]) && null_timespec(ts[1])) {
5812 /* set modify time = to access time if modify time was unset */
5813 ts[1] = ts[0];
5816 /* Set the date on this file */
5817 /* Should we set pending modtime here ? JRA */
5818 if(file_ntimes(conn, fsp->fsp_name, ts)) {
5819 END_PROFILE(SMBsetattrE);
5820 return ERROR_DOS(ERRDOS,ERRnoaccess);
5823 DEBUG( 3, ( "reply_setattrE fnum=%d actime=%u modtime=%u\n",
5824 fsp->fnum,
5825 (unsigned int)ts[0].tv_sec,
5826 (unsigned int)ts[1].tv_sec));
5828 END_PROFILE(SMBsetattrE);
5829 return(outsize);
5833 /* Back from the dead for OS/2..... JRA. */
5835 /****************************************************************************
5836 Reply to a SMBwritebmpx (write block multiplex primary) request.
5837 ****************************************************************************/
5839 int reply_writebmpx(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
5841 size_t numtowrite;
5842 ssize_t nwritten = -1;
5843 int outsize = 0;
5844 SMB_OFF_T startpos;
5845 size_t tcount;
5846 BOOL write_through;
5847 int smb_doff;
5848 char *data;
5849 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5850 START_PROFILE(SMBwriteBmpx);
5852 CHECK_FSP(fsp,conn);
5853 if (!CHECK_WRITE(fsp)) {
5854 return(ERROR_DOS(ERRDOS,ERRbadaccess));
5856 if (HAS_CACHED_ERROR(fsp)) {
5857 return(CACHED_ERROR(fsp));
5860 tcount = SVAL(inbuf,smb_vwv1);
5861 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv3);
5862 write_through = BITSETW(inbuf+smb_vwv7,0);
5863 numtowrite = SVAL(inbuf,smb_vwv10);
5864 smb_doff = SVAL(inbuf,smb_vwv11);
5866 data = smb_base(inbuf) + smb_doff;
5868 /* If this fails we need to send an SMBwriteC response,
5869 not an SMBwritebmpx - set this up now so we don't forget */
5870 SCVAL(outbuf,smb_com,SMBwritec);
5872 if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos,WRITE_LOCK)) {
5873 END_PROFILE(SMBwriteBmpx);
5874 return(ERROR_DOS(ERRDOS,ERRlock));
5877 nwritten = write_file(fsp,data,startpos,numtowrite);
5879 sync_file(conn, fsp, write_through);
5881 if(nwritten < (ssize_t)numtowrite) {
5882 END_PROFILE(SMBwriteBmpx);
5883 return(UNIXERROR(ERRHRD,ERRdiskfull));
5886 /* If the maximum to be written to this file
5887 is greater than what we just wrote then set
5888 up a secondary struct to be attached to this
5889 fd, we will use this to cache error messages etc. */
5891 if((ssize_t)tcount > nwritten) {
5892 write_bmpx_struct *wbms;
5893 if(fsp->wbmpx_ptr != NULL)
5894 wbms = fsp->wbmpx_ptr; /* Use an existing struct */
5895 else
5896 wbms = SMB_MALLOC_P(write_bmpx_struct);
5897 if(!wbms) {
5898 DEBUG(0,("Out of memory in reply_readmpx\n"));
5899 END_PROFILE(SMBwriteBmpx);
5900 return(ERROR_DOS(ERRSRV,ERRnoresource));
5902 wbms->wr_mode = write_through;
5903 wbms->wr_discard = False; /* No errors yet */
5904 wbms->wr_total_written = nwritten;
5905 wbms->wr_errclass = 0;
5906 wbms->wr_error = 0;
5907 fsp->wbmpx_ptr = wbms;
5910 /* We are returning successfully, set the message type back to
5911 SMBwritebmpx */
5912 SCVAL(outbuf,smb_com,SMBwriteBmpx);
5914 outsize = set_message(inbuf,outbuf,1,0,True);
5916 SSVALS(outbuf,smb_vwv0,-1); /* We don't support smb_remaining */
5918 DEBUG( 3, ( "writebmpx fnum=%d num=%d wrote=%d\n",
5919 fsp->fnum, (int)numtowrite, (int)nwritten ) );
5921 if (write_through && tcount==nwritten) {
5922 /* We need to send both a primary and a secondary response */
5923 smb_setlen(inbuf,outbuf,outsize - 4);
5924 show_msg(outbuf);
5925 if (!send_smb(smbd_server_fd(),outbuf))
5926 exit_server_cleanly("reply_writebmpx: send_smb failed.");
5928 /* Now the secondary */
5929 outsize = set_message(inbuf,outbuf,1,0,True);
5930 SCVAL(outbuf,smb_com,SMBwritec);
5931 SSVAL(outbuf,smb_vwv0,nwritten);
5934 END_PROFILE(SMBwriteBmpx);
5935 return(outsize);
5938 /****************************************************************************
5939 Reply to a SMBwritebs (write block multiplex secondary) request.
5940 ****************************************************************************/
5942 int reply_writebs(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
5944 size_t numtowrite;
5945 ssize_t nwritten = -1;
5946 int outsize = 0;
5947 SMB_OFF_T startpos;
5948 size_t tcount;
5949 BOOL write_through;
5950 int smb_doff;
5951 char *data;
5952 write_bmpx_struct *wbms;
5953 BOOL send_response = False;
5954 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
5955 START_PROFILE(SMBwriteBs);
5957 CHECK_FSP(fsp,conn);
5958 if (!CHECK_WRITE(fsp)) {
5959 return(ERROR_DOS(ERRDOS,ERRbadaccess));
5962 tcount = SVAL(inbuf,smb_vwv1);
5963 startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
5964 numtowrite = SVAL(inbuf,smb_vwv6);
5965 smb_doff = SVAL(inbuf,smb_vwv7);
5967 data = smb_base(inbuf) + smb_doff;
5969 /* We need to send an SMBwriteC response, not an SMBwritebs */
5970 SCVAL(outbuf,smb_com,SMBwritec);
5972 /* This fd should have an auxiliary struct attached,
5973 check that it does */
5974 wbms = fsp->wbmpx_ptr;
5975 if(!wbms) {
5976 END_PROFILE(SMBwriteBs);
5977 return(-1);
5980 /* If write through is set we can return errors, else we must cache them */
5981 write_through = wbms->wr_mode;
5983 /* Check for an earlier error */
5984 if(wbms->wr_discard) {
5985 END_PROFILE(SMBwriteBs);
5986 return -1; /* Just discard the packet */
5989 nwritten = write_file(fsp,data,startpos,numtowrite);
5991 sync_file(conn, fsp, write_through);
5993 if (nwritten < (ssize_t)numtowrite) {
5994 if(write_through) {
5995 /* We are returning an error - we can delete the aux struct */
5996 if (wbms)
5997 free((char *)wbms);
5998 fsp->wbmpx_ptr = NULL;
5999 END_PROFILE(SMBwriteBs);
6000 return(ERROR_DOS(ERRHRD,ERRdiskfull));
6002 wbms->wr_errclass = ERRHRD;
6003 wbms->wr_error = ERRdiskfull;
6004 wbms->wr_status = NT_STATUS_DISK_FULL;
6005 wbms->wr_discard = True;
6006 END_PROFILE(SMBwriteBs);
6007 return -1;
6010 /* Increment the total written, if this matches tcount
6011 we can discard the auxiliary struct (hurrah !) and return a writeC */
6012 wbms->wr_total_written += nwritten;
6013 if(wbms->wr_total_written >= tcount) {
6014 if (write_through) {
6015 outsize = set_message(inbuf,outbuf,1,0,True);
6016 SSVAL(outbuf,smb_vwv0,wbms->wr_total_written);
6017 send_response = True;
6020 free((char *)wbms);
6021 fsp->wbmpx_ptr = NULL;
6024 if(send_response) {
6025 END_PROFILE(SMBwriteBs);
6026 return(outsize);
6029 END_PROFILE(SMBwriteBs);
6030 return(-1);
6033 /****************************************************************************
6034 Reply to a SMBgetattrE.
6035 ****************************************************************************/
6037 int reply_getattrE(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
6039 SMB_STRUCT_STAT sbuf;
6040 int outsize = 0;
6041 int mode;
6042 files_struct *fsp = file_fsp(inbuf,smb_vwv0);
6043 START_PROFILE(SMBgetattrE);
6045 outsize = set_message(inbuf,outbuf,11,0,True);
6047 if(!fsp || (fsp->conn != conn)) {
6048 END_PROFILE(SMBgetattrE);
6049 return ERROR_DOS(ERRDOS,ERRbadfid);
6052 /* Do an fstat on this file */
6053 if(fsp_stat(fsp, &sbuf)) {
6054 END_PROFILE(SMBgetattrE);
6055 return(UNIXERROR(ERRDOS,ERRnoaccess));
6058 mode = dos_mode(conn,fsp->fsp_name,&sbuf);
6061 * Convert the times into dos times. Set create
6062 * date to be last modify date as UNIX doesn't save
6063 * this.
6066 srv_put_dos_date2(outbuf,smb_vwv0,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
6067 srv_put_dos_date2(outbuf,smb_vwv2,sbuf.st_atime);
6068 /* Should we check pending modtime here ? JRA */
6069 srv_put_dos_date2(outbuf,smb_vwv4,sbuf.st_mtime);
6071 if (mode & aDIR) {
6072 SIVAL(outbuf,smb_vwv6,0);
6073 SIVAL(outbuf,smb_vwv8,0);
6074 } else {
6075 uint32 allocation_size = get_allocation_size(conn,fsp, &sbuf);
6076 SIVAL(outbuf,smb_vwv6,(uint32)sbuf.st_size);
6077 SIVAL(outbuf,smb_vwv8,allocation_size);
6079 SSVAL(outbuf,smb_vwv10, mode);
6081 DEBUG( 3, ( "reply_getattrE fnum=%d\n", fsp->fnum));
6083 END_PROFILE(SMBgetattrE);
6084 return(outsize);