Add streams support
[samba.git] / source3 / smbd / reply.c
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.
7    Copyright (C) Volker Lendecke 2007
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22 /*
23    This file handles most of the reply_ calls that the server
24    makes to handle specific protocols
25 */
26
27 #include "includes.h"
28
29 /* look in server.c for some explanation of these variables */
30 extern enum protocol_types Protocol;
31 extern int max_recv;
32 unsigned int smb_echo_count = 0;
33 extern uint32 global_client_caps;
34
35 extern struct current_user current_user;
36 extern bool global_encrypted_passwords_negotiated;
37
38 /****************************************************************************
39  Ensure we check the path in *exactly* the same way as W2K for a findfirst/findnext
40  path or anything including wildcards.
41  We're assuming here that '/' is not the second byte in any multibyte char
42  set (a safe assumption). '\\' *may* be the second byte in a multibyte char
43  set.
44 ****************************************************************************/
45
46 /* Custom version for processing POSIX paths. */
47 #define IS_PATH_SEP(c,posix_only) ((c) == '/' || (!(posix_only) && (c) == '\\'))
48
49 static NTSTATUS check_path_syntax_internal(char *path,
50                                            bool posix_path,
51                                            bool *p_last_component_contains_wcard)
52 {
53         char *d = path;
54         const char *s = path;
55         NTSTATUS ret = NT_STATUS_OK;
56         bool start_of_name_component = True;
57
58         *p_last_component_contains_wcard = False;
59
60         while (*s) {
61                 if (IS_PATH_SEP(*s,posix_path)) {
62                         /*
63                          * Safe to assume is not the second part of a mb char
64                          * as this is handled below.
65                          */
66                         /* Eat multiple '/' or '\\' */
67                         while (IS_PATH_SEP(*s,posix_path)) {
68                                 s++;
69                         }
70                         if ((d != path) && (*s != '\0')) {
71                                 /* We only care about non-leading or trailing '/' or '\\' */
72                                 *d++ = '/';
73                         }
74
75                         start_of_name_component = True;
76                         /* New component. */
77                         *p_last_component_contains_wcard = False;
78                         continue;
79                 }
80
81                 if (start_of_name_component) {
82                         if ((s[0] == '.') && (s[1] == '.') && (IS_PATH_SEP(s[2],posix_path) || s[2] == '\0')) {
83                                 /* Uh oh - "/../" or "\\..\\"  or "/..\0" or "\\..\0" ! */
84
85                                 /*
86                                  * No mb char starts with '.' so we're safe checking the directory separator here.
87                                  */
88
89                                 /* If  we just added a '/' - delete it */
90                                 if ((d > path) && (*(d-1) == '/')) {
91                                         *(d-1) = '\0';
92                                         d--;
93                                 }
94
95                                 /* Are we at the start ? Can't go back further if so. */
96                                 if (d <= path) {
97                                         ret = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
98                                         break;
99                                 }
100                                 /* Go back one level... */
101                                 /* We know this is safe as '/' cannot be part of a mb sequence. */
102                                 /* NOTE - if this assumption is invalid we are not in good shape... */
103                                 /* Decrement d first as d points to the *next* char to write into. */
104                                 for (d--; d > path; d--) {
105                                         if (*d == '/')
106                                                 break;
107                                 }
108                                 s += 2; /* Else go past the .. */
109                                 /* We're still at the start of a name component, just the previous one. */
110                                 continue;
111
112                         } else if ((s[0] == '.') && ((s[1] == '\0') || IS_PATH_SEP(s[1],posix_path))) {
113                                 if (posix_path) {
114                                         /* Eat the '.' */
115                                         s++;
116                                         continue;
117                                 }
118                         }
119
120                 }
121
122                 if (!(*s & 0x80)) {
123                         if (!posix_path) {
124                                 if (*s <= 0x1f) {
125                                         return NT_STATUS_OBJECT_NAME_INVALID;
126                                 }
127                                 switch (*s) {
128                                         case '*':
129                                         case '?':
130                                         case '<':
131                                         case '>':
132                                         case '"':
133                                                 *p_last_component_contains_wcard = True;
134                                                 break;
135                                         default:
136                                                 break;
137                                 }
138                         }
139                         *d++ = *s++;
140                 } else {
141                         size_t siz;
142                         /* Get the size of the next MB character. */
143                         next_codepoint(s,&siz);
144                         switch(siz) {
145                                 case 5:
146                                         *d++ = *s++;
147                                         /*fall through*/
148                                 case 4:
149                                         *d++ = *s++;
150                                         /*fall through*/
151                                 case 3:
152                                         *d++ = *s++;
153                                         /*fall through*/
154                                 case 2:
155                                         *d++ = *s++;
156                                         /*fall through*/
157                                 case 1:
158                                         *d++ = *s++;
159                                         break;
160                                 default:
161                                         DEBUG(0,("check_path_syntax_internal: character length assumptions invalid !\n"));
162                                         *d = '\0';
163                                         return NT_STATUS_INVALID_PARAMETER;
164                         }
165                 }
166                 start_of_name_component = False;
167         }
168
169         *d = '\0';
170
171         if (NT_STATUS_IS_OK(ret) && !posix_path) {
172                 ret = split_ntfs_stream_name(NULL, path, NULL, NULL);
173         }
174         return ret;
175 }
176
177 /****************************************************************************
178  Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
179  No wildcards allowed.
180 ****************************************************************************/
181
182 NTSTATUS check_path_syntax(char *path)
183 {
184         bool ignore;
185         return check_path_syntax_internal(path, False, &ignore);
186 }
187
188 /****************************************************************************
189  Ensure we check the path in *exactly* the same way as W2K for regular pathnames.
190  Wildcards allowed - p_contains_wcard returns true if the last component contained
191  a wildcard.
192 ****************************************************************************/
193
194 NTSTATUS check_path_syntax_wcard(char *path, bool *p_contains_wcard)
195 {
196         return check_path_syntax_internal(path, False, p_contains_wcard);
197 }
198
199 /****************************************************************************
200  Check the path for a POSIX client.
201  We're assuming here that '/' is not the second byte in any multibyte char
202  set (a safe assumption).
203 ****************************************************************************/
204
205 NTSTATUS check_path_syntax_posix(char *path)
206 {
207         bool ignore;
208         return check_path_syntax_internal(path, True, &ignore);
209 }
210
211 /****************************************************************************
212  Pull a string and check the path allowing a wilcard - provide for error return.
213 ****************************************************************************/
214
215 size_t srvstr_get_path_wcard(TALLOC_CTX *ctx,
216                         const char *inbuf,
217                         uint16 smb_flags2,
218                         char **pp_dest,
219                         const char *src,
220                         size_t src_len,
221                         int flags,
222                         NTSTATUS *err,
223                         bool *contains_wcard)
224 {
225         size_t ret;
226
227         *pp_dest = NULL;
228
229         if (src_len == 0) {
230                 ret = srvstr_pull_buf_talloc(ctx,
231                                 inbuf,
232                                 smb_flags2,
233                                 pp_dest,
234                                 src,
235                                 flags);
236         } else {
237                 ret = srvstr_pull_talloc(ctx,
238                                 inbuf,
239                                 smb_flags2,
240                                 pp_dest,
241                                 src,
242                                 src_len,
243                                 flags);
244         }
245
246         if (!*pp_dest) {
247                 *err = NT_STATUS_INVALID_PARAMETER;
248                 return ret;
249         }
250
251         *contains_wcard = False;
252
253         if (smb_flags2 & FLAGS2_DFS_PATHNAMES) {
254                 /*
255                  * For a DFS path the function parse_dfs_path()
256                  * will do the path processing, just make a copy.
257                  */
258                 *err = NT_STATUS_OK;
259                 return ret;
260         }
261
262         if (lp_posix_pathnames()) {
263                 *err = check_path_syntax_posix(*pp_dest);
264         } else {
265                 *err = check_path_syntax_wcard(*pp_dest, contains_wcard);
266         }
267
268         return ret;
269 }
270
271 /****************************************************************************
272  Pull a string and check the path - provide for error return.
273 ****************************************************************************/
274
275 size_t srvstr_get_path(TALLOC_CTX *ctx,
276                         const char *inbuf,
277                         uint16 smb_flags2,
278                         char **pp_dest,
279                         const char *src,
280                         size_t src_len,
281                         int flags,
282                         NTSTATUS *err)
283 {
284         size_t ret;
285
286         *pp_dest = NULL;
287
288         if (src_len == 0) {
289                 ret = srvstr_pull_buf_talloc(ctx,
290                                         inbuf,
291                                         smb_flags2,
292                                         pp_dest,
293                                         src,
294                                         flags);
295         } else {
296                 ret = srvstr_pull_talloc(ctx,
297                                 inbuf,
298                                 smb_flags2,
299                                 pp_dest,
300                                 src,
301                                 src_len,
302                                 flags);
303         }
304
305         if (!*pp_dest) {
306                 *err = NT_STATUS_INVALID_PARAMETER;
307                 return ret;
308         }
309
310         if (smb_flags2 & FLAGS2_DFS_PATHNAMES) {
311                 /*
312                  * For a DFS path the function parse_dfs_path()
313                  * will do the path processing, just make a copy.
314                  */
315                 *err = NT_STATUS_OK;
316                 return ret;
317         }
318
319         if (lp_posix_pathnames()) {
320                 *err = check_path_syntax_posix(*pp_dest);
321         } else {
322                 *err = check_path_syntax(*pp_dest);
323         }
324
325         return ret;
326 }
327
328 /****************************************************************************
329  Check if we have a correct fsp pointing to a file. Basic check for open fsp.
330 ****************************************************************************/
331
332 bool check_fsp_open(connection_struct *conn, struct smb_request *req,
333                files_struct *fsp, struct current_user *user)
334 {
335         if (!(fsp) || !(conn)) {
336                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
337                 return False;
338         }
339         if (((conn) != (fsp)->conn) || user->vuid != (fsp)->vuid) {
340                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
341                 return False;
342         }
343         return True;
344 }
345
346 /****************************************************************************
347  Check if we have a correct fsp pointing to a file. Replacement for the
348  CHECK_FSP macro.
349 ****************************************************************************/
350
351 bool check_fsp(connection_struct *conn, struct smb_request *req,
352                files_struct *fsp, struct current_user *user)
353 {
354         if (!check_fsp_open(conn, req, fsp, user)) {
355                 return False;
356         }
357         if ((fsp)->is_directory) {
358                 reply_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
359                 return False;
360         }
361         if ((fsp)->fh->fd == -1) {
362                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
363                 return False;
364         }
365         (fsp)->num_smb_operations++;
366         return True;
367 }
368
369 /****************************************************************************
370  Check if we have a correct fsp. Replacement for the FSP_BELONGS_CONN macro
371 ****************************************************************************/
372
373 bool fsp_belongs_conn(connection_struct *conn, struct smb_request *req,
374                       files_struct *fsp, struct current_user *user)
375 {
376         if ((fsp) && (conn) && ((conn)==(fsp)->conn)
377             && (current_user.vuid==(fsp)->vuid)) {
378                 return True;
379         }
380
381         reply_nterror(req, NT_STATUS_INVALID_HANDLE);
382         return False;
383 }
384
385 /****************************************************************************
386  Reply to a (netbios-level) special message.
387 ****************************************************************************/
388
389 void reply_special(char *inbuf)
390 {
391         int msg_type = CVAL(inbuf,0);
392         int msg_flags = CVAL(inbuf,1);
393         fstring name1,name2;
394         char name_type = 0;
395
396         /*
397          * We only really use 4 bytes of the outbuf, but for the smb_setlen
398          * calculation & friends (srv_send_smb uses that) we need the full smb
399          * header.
400          */
401         char outbuf[smb_size];
402         
403         static bool already_got_session = False;
404
405         *name1 = *name2 = 0;
406         
407         memset(outbuf, '\0', sizeof(outbuf));
408
409         smb_setlen(outbuf,0);
410         
411         switch (msg_type) {
412         case 0x81: /* session request */
413                 
414                 if (already_got_session) {
415                         exit_server_cleanly("multiple session request not permitted");
416                 }
417                 
418                 SCVAL(outbuf,0,0x82);
419                 SCVAL(outbuf,3,0);
420                 if (name_len(inbuf+4) > 50 || 
421                     name_len(inbuf+4 + name_len(inbuf + 4)) > 50) {
422                         DEBUG(0,("Invalid name length in session request\n"));
423                         return;
424                 }
425                 name_extract(inbuf,4,name1);
426                 name_type = name_extract(inbuf,4 + name_len(inbuf + 4),name2);
427                 DEBUG(2,("netbios connect: name1=%s name2=%s\n",
428                          name1,name2));      
429
430                 set_local_machine_name(name1, True);
431                 set_remote_machine_name(name2, True);
432
433                 DEBUG(2,("netbios connect: local=%s remote=%s, name type = %x\n",
434                          get_local_machine_name(), get_remote_machine_name(),
435                          name_type));
436
437                 if (name_type == 'R') {
438                         /* We are being asked for a pathworks session --- 
439                            no thanks! */
440                         SCVAL(outbuf, 0,0x83);
441                         break;
442                 }
443
444                 /* only add the client's machine name to the list
445                    of possibly valid usernames if we are operating
446                    in share mode security */
447                 if (lp_security() == SEC_SHARE) {
448                         add_session_user(get_remote_machine_name());
449                 }
450
451                 reload_services(True);
452                 reopen_logs();
453
454                 already_got_session = True;
455                 break;
456                 
457         case 0x89: /* session keepalive request 
458                       (some old clients produce this?) */
459                 SCVAL(outbuf,0,SMBkeepalive);
460                 SCVAL(outbuf,3,0);
461                 break;
462                 
463         case 0x82: /* positive session response */
464         case 0x83: /* negative session response */
465         case 0x84: /* retarget session response */
466                 DEBUG(0,("Unexpected session response\n"));
467                 break;
468                 
469         case SMBkeepalive: /* session keepalive */
470         default:
471                 return;
472         }
473         
474         DEBUG(5,("init msg_type=0x%x msg_flags=0x%x\n",
475                     msg_type, msg_flags));
476
477         srv_send_smb(smbd_server_fd(), outbuf, false);
478         return;
479 }
480
481 /****************************************************************************
482  Reply to a tcon.
483  conn POINTER CAN BE NULL HERE !
484 ****************************************************************************/
485
486 void reply_tcon(struct smb_request *req)
487 {
488         connection_struct *conn = req->conn;
489         const char *service;
490         char *service_buf = NULL;
491         char *password = NULL;
492         char *dev = NULL;
493         int pwlen=0;
494         NTSTATUS nt_status;
495         char *p;
496         DATA_BLOB password_blob;
497         TALLOC_CTX *ctx = talloc_tos();
498
499         START_PROFILE(SMBtcon);
500
501         if (smb_buflen(req->inbuf) < 4) {
502                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
503                 END_PROFILE(SMBtcon);
504                 return;
505         }
506
507         p = smb_buf(req->inbuf)+1;
508         p += srvstr_pull_buf_talloc(ctx, req->inbuf, req->flags2,
509                                     &service_buf, p, STR_TERMINATE) + 1;
510         pwlen = srvstr_pull_buf_talloc(ctx, req->inbuf, req->flags2,
511                                        &password, p, STR_TERMINATE) + 1;
512         p += pwlen;
513         p += srvstr_pull_buf_talloc(ctx, req->inbuf, req->flags2,
514                                     &dev, p, STR_TERMINATE) + 1;
515
516         if (service_buf == NULL || password == NULL || dev == NULL) {
517                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
518                 END_PROFILE(SMBtcon);
519                 return;
520         }
521         p = strrchr_m(service_buf,'\\');
522         if (p) {
523                 service = p+1;
524         } else {
525                 service = service_buf;
526         }
527
528         password_blob = data_blob(password, pwlen+1);
529
530         conn = make_connection(service,password_blob,dev,req->vuid,&nt_status);
531         req->conn = conn;
532
533         data_blob_clear_free(&password_blob);
534
535         if (!conn) {
536                 reply_nterror(req, nt_status);
537                 END_PROFILE(SMBtcon);
538                 return;
539         }
540
541         reply_outbuf(req, 2, 0);
542         SSVAL(req->outbuf,smb_vwv0,max_recv);
543         SSVAL(req->outbuf,smb_vwv1,conn->cnum);
544         SSVAL(req->outbuf,smb_tid,conn->cnum);
545
546         DEBUG(3,("tcon service=%s cnum=%d\n",
547                  service, conn->cnum));
548
549         END_PROFILE(SMBtcon);
550         return;
551 }
552
553 /****************************************************************************
554  Reply to a tcon and X.
555  conn POINTER CAN BE NULL HERE !
556 ****************************************************************************/
557
558 void reply_tcon_and_X(struct smb_request *req)
559 {
560         connection_struct *conn = req->conn;
561         char *service = NULL;
562         DATA_BLOB password;
563         TALLOC_CTX *ctx = talloc_tos();
564         /* what the cleint thinks the device is */
565         char *client_devicetype = NULL;
566         /* what the server tells the client the share represents */
567         const char *server_devicetype;
568         NTSTATUS nt_status;
569         int passlen;
570         char *path = NULL;
571         char *p, *q;
572         uint16 tcon_flags;
573
574         START_PROFILE(SMBtconX);
575
576         if (req->wct < 4) {
577                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
578                 END_PROFILE(SMBtconX);
579                 return;
580         }
581
582         passlen = SVAL(req->inbuf,smb_vwv3);
583         tcon_flags = SVAL(req->inbuf,smb_vwv2);
584
585         /* we might have to close an old one */
586         if ((tcon_flags & 0x1) && conn) {
587                 close_cnum(conn,req->vuid);
588                 req->conn = NULL;
589                 conn = NULL;
590         }
591
592         if ((passlen > MAX_PASS_LEN) || (passlen >= smb_buflen(req->inbuf))) {
593                 reply_doserror(req, ERRDOS, ERRbuftoosmall);
594                 END_PROFILE(SMBtconX);
595                 return;
596         }
597
598         if (global_encrypted_passwords_negotiated) {
599                 password = data_blob_talloc(talloc_tos(), smb_buf(req->inbuf),
600                                             passlen);
601                 if (lp_security() == SEC_SHARE) {
602                         /*
603                          * Security = share always has a pad byte
604                          * after the password.
605                          */
606                         p = smb_buf(req->inbuf) + passlen + 1;
607                 } else {
608                         p = smb_buf(req->inbuf) + passlen;
609                 }
610         } else {
611                 password = data_blob_talloc(talloc_tos(), smb_buf(req->inbuf),
612                                             passlen+1);
613                 /* Ensure correct termination */
614                 password.data[passlen]=0;
615                 p = smb_buf(req->inbuf) + passlen + 1;
616         }
617
618         p += srvstr_pull_buf_talloc(ctx, req->inbuf, req->flags2, &path, p,
619                              STR_TERMINATE);
620
621         if (path == NULL) {
622                 data_blob_clear_free(&password);
623                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
624                 END_PROFILE(SMBtconX);
625                 return;
626         }
627
628         /*
629          * the service name can be either: \\server\share
630          * or share directly like on the DELL PowerVault 705
631          */
632         if (*path=='\\') {
633                 q = strchr_m(path+2,'\\');
634                 if (!q) {
635                         data_blob_clear_free(&password);
636                         reply_doserror(req, ERRDOS, ERRnosuchshare);
637                         END_PROFILE(SMBtconX);
638                         return;
639                 }
640                 service = q+1;
641         } else {
642                 service = path;
643         }
644
645         p += srvstr_pull_talloc(ctx, req->inbuf, req->flags2,
646                                 &client_devicetype, p,
647                                 MIN(6,smb_bufrem(req->inbuf, p)), STR_ASCII);
648
649         if (client_devicetype == NULL) {
650                 data_blob_clear_free(&password);
651                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
652                 END_PROFILE(SMBtconX);
653                 return;
654         }
655
656         DEBUG(4,("Client requested device type [%s] for share [%s]\n", client_devicetype, service));
657
658         conn = make_connection(service, password, client_devicetype,
659                                req->vuid, &nt_status);
660         req->conn =conn;
661
662         data_blob_clear_free(&password);
663
664         if (!conn) {
665                 reply_nterror(req, nt_status);
666                 END_PROFILE(SMBtconX);
667                 return;
668         }
669
670         if ( IS_IPC(conn) )
671                 server_devicetype = "IPC";
672         else if ( IS_PRINT(conn) )
673                 server_devicetype = "LPT1:";
674         else
675                 server_devicetype = "A:";
676
677         if (Protocol < PROTOCOL_NT1) {
678                 reply_outbuf(req, 2, 0);
679                 if (message_push_string(&req->outbuf, server_devicetype,
680                                         STR_TERMINATE|STR_ASCII) == -1) {
681                         reply_nterror(req, NT_STATUS_NO_MEMORY);
682                         END_PROFILE(SMBtconX);
683                         return;
684                 }
685         } else {
686                 /* NT sets the fstype of IPC$ to the null string */
687                 const char *fstype = IS_IPC(conn) ? "" : lp_fstype(SNUM(conn));
688
689                 if (tcon_flags & TCONX_FLAG_EXTENDED_RESPONSE) {
690                         /* Return permissions. */
691                         uint32 perm1 = 0;
692                         uint32 perm2 = 0;
693
694                         reply_outbuf(req, 7, 0);
695
696                         if (IS_IPC(conn)) {
697                                 perm1 = FILE_ALL_ACCESS;
698                                 perm2 = FILE_ALL_ACCESS;
699                         } else {
700                                 perm1 = CAN_WRITE(conn) ?
701                                                 SHARE_ALL_ACCESS :
702                                                 SHARE_READ_ONLY;
703                         }
704
705                         SIVAL(req->outbuf, smb_vwv3, perm1);
706                         SIVAL(req->outbuf, smb_vwv5, perm2);
707                 } else {
708                         reply_outbuf(req, 3, 0);
709                 }
710
711                 if ((message_push_string(&req->outbuf, server_devicetype,
712                                          STR_TERMINATE|STR_ASCII) == -1)
713                     || (message_push_string(&req->outbuf, fstype,
714                                             STR_TERMINATE) == -1)) {
715                         reply_nterror(req, NT_STATUS_NO_MEMORY);
716                         END_PROFILE(SMBtconX);
717                         return;
718                 }
719
720                 /* what does setting this bit do? It is set by NT4 and
721                    may affect the ability to autorun mounted cdroms */
722                 SSVAL(req->outbuf, smb_vwv2, SMB_SUPPORT_SEARCH_BITS|
723                       (lp_csc_policy(SNUM(conn)) << 2));
724
725                 init_dfsroot(conn, req->inbuf, req->outbuf);
726         }
727
728
729         DEBUG(3,("tconX service=%s \n",
730                  service));
731
732         /* set the incoming and outgoing tid to the just created one */
733         SSVAL(req->inbuf,smb_tid,conn->cnum);
734         SSVAL(req->outbuf,smb_tid,conn->cnum);
735
736         END_PROFILE(SMBtconX);
737
738         chain_reply(req);
739         return;
740 }
741
742 /****************************************************************************
743  Reply to an unknown type.
744 ****************************************************************************/
745
746 void reply_unknown_new(struct smb_request *req, uint8 type)
747 {
748         DEBUG(0, ("unknown command type (%s): type=%d (0x%X)\n",
749                   smb_fn_name(type), type, type));
750         reply_doserror(req, ERRSRV, ERRunknownsmb);
751         return;
752 }
753
754 /****************************************************************************
755  Reply to an ioctl.
756  conn POINTER CAN BE NULL HERE !
757 ****************************************************************************/
758
759 void reply_ioctl(struct smb_request *req)
760 {
761         connection_struct *conn = req->conn;
762         uint16 device;
763         uint16 function;
764         uint32 ioctl_code;
765         int replysize;
766         char *p;
767
768         START_PROFILE(SMBioctl);
769
770         if (req->wct < 3) {
771                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
772                 END_PROFILE(SMBioctl);
773                 return;
774         }
775
776         device     = SVAL(req->inbuf,smb_vwv1);
777         function   = SVAL(req->inbuf,smb_vwv2);
778         ioctl_code = (device << 16) + function;
779
780         DEBUG(4, ("Received IOCTL (code 0x%x)\n", ioctl_code));
781
782         switch (ioctl_code) {
783             case IOCTL_QUERY_JOB_INFO:
784                     replysize = 32;
785                     break;
786             default:
787                     reply_doserror(req, ERRSRV, ERRnosupport);
788                     END_PROFILE(SMBioctl);
789                     return;
790         }
791
792         reply_outbuf(req, 8, replysize+1);
793         SSVAL(req->outbuf,smb_vwv1,replysize); /* Total data bytes returned */
794         SSVAL(req->outbuf,smb_vwv5,replysize); /* Data bytes this buffer */
795         SSVAL(req->outbuf,smb_vwv6,52);        /* Offset to data */
796         p = smb_buf(req->outbuf);
797         memset(p, '\0', replysize+1); /* valgrind-safe. */
798         p += 1;          /* Allow for alignment */
799
800         switch (ioctl_code) {
801                 case IOCTL_QUERY_JOB_INFO:                  
802                 {
803                         files_struct *fsp = file_fsp(SVAL(req->inbuf,
804                                                           smb_vwv0));
805                         if (!fsp) {
806                                 reply_doserror(req, ERRDOS, ERRbadfid);
807                                 END_PROFILE(SMBioctl);
808                                 return;
809                         }
810                         SSVAL(p,0,fsp->rap_print_jobid);             /* Job number */
811                         srvstr_push((char *)req->outbuf, req->flags2, p+2,
812                                     global_myname(), 15,
813                                     STR_TERMINATE|STR_ASCII);
814                         if (conn) {
815                                 srvstr_push((char *)req->outbuf, req->flags2,
816                                             p+18, lp_servicename(SNUM(conn)),
817                                             13, STR_TERMINATE|STR_ASCII);
818                         } else {
819                                 memset(p+18, 0, 13);
820                         }
821                         break;
822                 }
823         }
824
825         END_PROFILE(SMBioctl);
826         return;
827 }
828
829 /****************************************************************************
830  Strange checkpath NTSTATUS mapping.
831 ****************************************************************************/
832
833 static NTSTATUS map_checkpath_error(const char *inbuf, NTSTATUS status)
834 {
835         /* Strange DOS error code semantics only for checkpath... */
836         if (!(SVAL(inbuf,smb_flg2) & FLAGS2_32_BIT_ERROR_CODES)) {
837                 if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_INVALID,status)) {
838                         /* We need to map to ERRbadpath */
839                         return NT_STATUS_OBJECT_PATH_NOT_FOUND;
840                 }
841         }
842         return status;
843 }
844
845 /****************************************************************************
846  Reply to a checkpath.
847 ****************************************************************************/
848
849 void reply_checkpath(struct smb_request *req)
850 {
851         connection_struct *conn = req->conn;
852         char *name = NULL;
853         SMB_STRUCT_STAT sbuf;
854         NTSTATUS status;
855         TALLOC_CTX *ctx = talloc_tos();
856
857         START_PROFILE(SMBcheckpath);
858
859         srvstr_get_path(ctx,(char *)req->inbuf, req->flags2, &name,
860                         smb_buf(req->inbuf) + 1, 0,
861                         STR_TERMINATE, &status);
862         if (!NT_STATUS_IS_OK(status)) {
863                 status = map_checkpath_error((char *)req->inbuf, status);
864                 reply_nterror(req, status);
865                 END_PROFILE(SMBcheckpath);
866                 return;
867         }
868
869         status = resolve_dfspath(ctx, conn,
870                         req->flags2 & FLAGS2_DFS_PATHNAMES,
871                         name,
872                         &name);
873         if (!NT_STATUS_IS_OK(status)) {
874                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
875                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
876                                         ERRSRV, ERRbadpath);
877                         END_PROFILE(SMBcheckpath);
878                         return;
879                 }
880                 goto path_err;
881         }
882
883         DEBUG(3,("reply_checkpath %s mode=%d\n", name, (int)SVAL(req->inbuf,smb_vwv0)));
884
885         status = unix_convert(ctx, conn, name, False, &name, NULL, &sbuf);
886         if (!NT_STATUS_IS_OK(status)) {
887                 goto path_err;
888         }
889
890         status = check_name(conn, name);
891         if (!NT_STATUS_IS_OK(status)) {
892                 DEBUG(3,("reply_checkpath: check_name of %s failed (%s)\n",name,nt_errstr(status)));
893                 goto path_err;
894         }
895
896         if (!VALID_STAT(sbuf) && (SMB_VFS_STAT(conn,name,&sbuf) != 0)) {
897                 DEBUG(3,("reply_checkpath: stat of %s failed (%s)\n",name,strerror(errno)));
898                 status = map_nt_error_from_unix(errno);
899                 goto path_err;
900         }
901
902         if (!S_ISDIR(sbuf.st_mode)) {
903                 reply_botherror(req, NT_STATUS_NOT_A_DIRECTORY,
904                                 ERRDOS, ERRbadpath);
905                 END_PROFILE(SMBcheckpath);
906                 return;
907         }
908
909         reply_outbuf(req, 0, 0);
910
911         END_PROFILE(SMBcheckpath);
912         return;
913
914   path_err:
915
916         END_PROFILE(SMBcheckpath);
917
918         /* We special case this - as when a Windows machine
919                 is parsing a path is steps through the components
920                 one at a time - if a component fails it expects
921                 ERRbadpath, not ERRbadfile.
922         */
923         status = map_checkpath_error((char *)req->inbuf, status);
924         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
925                 /*
926                  * Windows returns different error codes if
927                  * the parent directory is valid but not the
928                  * last component - it returns NT_STATUS_OBJECT_NAME_NOT_FOUND
929                  * for that case and NT_STATUS_OBJECT_PATH_NOT_FOUND
930                  * if the path is invalid.
931                  */
932                 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
933                                 ERRDOS, ERRbadpath);
934                 return;
935         }
936
937         reply_nterror(req, status);
938 }
939
940 /****************************************************************************
941  Reply to a getatr.
942 ****************************************************************************/
943
944 void reply_getatr(struct smb_request *req)
945 {
946         connection_struct *conn = req->conn;
947         char *fname = NULL;
948         SMB_STRUCT_STAT sbuf;
949         int mode=0;
950         SMB_OFF_T size=0;
951         time_t mtime=0;
952         char *p;
953         NTSTATUS status;
954         TALLOC_CTX *ctx = talloc_tos();
955
956         START_PROFILE(SMBgetatr);
957
958         p = smb_buf(req->inbuf) + 1;
959         p += srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname, p,
960                              0, STR_TERMINATE, &status);
961         if (!NT_STATUS_IS_OK(status)) {
962                 reply_nterror(req, status);
963                 END_PROFILE(SMBgetatr);
964                 return;
965         }
966
967         status = resolve_dfspath(ctx, conn,
968                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
969                                 fname,
970                                 &fname);
971         if (!NT_STATUS_IS_OK(status)) {
972                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
973                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
974                                         ERRSRV, ERRbadpath);
975                         END_PROFILE(SMBgetatr);
976                         return;
977                 }
978                 reply_nterror(req, status);
979                 END_PROFILE(SMBgetatr);
980                 return;
981         }
982
983         /* dos smetimes asks for a stat of "" - it returns a "hidden directory"
984                 under WfWg - weird! */
985         if (*fname == '\0') {
986                 mode = aHIDDEN | aDIR;
987                 if (!CAN_WRITE(conn)) {
988                         mode |= aRONLY;
989                 }
990                 size = 0;
991                 mtime = 0;
992         } else {
993                 status = unix_convert(ctx, conn, fname, False, &fname, NULL,&sbuf);
994                 if (!NT_STATUS_IS_OK(status)) {
995                         reply_nterror(req, status);
996                         END_PROFILE(SMBgetatr);
997                         return;
998                 }
999                 status = check_name(conn, fname);
1000                 if (!NT_STATUS_IS_OK(status)) {
1001                         DEBUG(3,("reply_getatr: check_name of %s failed (%s)\n",fname,nt_errstr(status)));
1002                         reply_nterror(req, status);
1003                         END_PROFILE(SMBgetatr);
1004                         return;
1005                 }
1006                 if (!VALID_STAT(sbuf) && (SMB_VFS_STAT(conn,fname,&sbuf) != 0)) {
1007                         DEBUG(3,("reply_getatr: stat of %s failed (%s)\n",fname,strerror(errno)));
1008                         reply_unixerror(req, ERRDOS,ERRbadfile);
1009                         END_PROFILE(SMBgetatr);
1010                         return;
1011                 }
1012
1013                 mode = dos_mode(conn,fname,&sbuf);
1014                 size = sbuf.st_size;
1015                 mtime = sbuf.st_mtime;
1016                 if (mode & aDIR) {
1017                         size = 0;
1018                 }
1019         }
1020
1021         reply_outbuf(req, 10, 0);
1022
1023         SSVAL(req->outbuf,smb_vwv0,mode);
1024         if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1025                 srv_put_dos_date3((char *)req->outbuf,smb_vwv1,mtime & ~1);
1026         } else {
1027                 srv_put_dos_date3((char *)req->outbuf,smb_vwv1,mtime);
1028         }
1029         SIVAL(req->outbuf,smb_vwv3,(uint32)size);
1030
1031         if (Protocol >= PROTOCOL_NT1) {
1032                 SSVAL(req->outbuf, smb_flg2,
1033                       SVAL(req->outbuf, smb_flg2) | FLAGS2_IS_LONG_NAME);
1034         }
1035   
1036         DEBUG(3,("reply_getatr: name=%s mode=%d size=%u\n", fname, mode, (unsigned int)size ) );
1037
1038         END_PROFILE(SMBgetatr);
1039         return;
1040 }
1041
1042 /****************************************************************************
1043  Reply to a setatr.
1044 ****************************************************************************/
1045
1046 void reply_setatr(struct smb_request *req)
1047 {
1048         connection_struct *conn = req->conn;
1049         char *fname = NULL;
1050         int mode;
1051         time_t mtime;
1052         SMB_STRUCT_STAT sbuf;
1053         char *p;
1054         NTSTATUS status;
1055         TALLOC_CTX *ctx = talloc_tos();
1056
1057         START_PROFILE(SMBsetatr);
1058
1059         if (req->wct < 2) {
1060                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1061                 return;
1062         }
1063
1064         p = smb_buf(req->inbuf) + 1;
1065         p += srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname, p,
1066                                 0, STR_TERMINATE, &status);
1067         if (!NT_STATUS_IS_OK(status)) {
1068                 reply_nterror(req, status);
1069                 END_PROFILE(SMBsetatr);
1070                 return;
1071         }
1072
1073         status = resolve_dfspath(ctx, conn,
1074                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
1075                                 fname,
1076                                 &fname);
1077         if (!NT_STATUS_IS_OK(status)) {
1078                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1079                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1080                                         ERRSRV, ERRbadpath);
1081                         END_PROFILE(SMBsetatr);
1082                         return;
1083                 }
1084                 reply_nterror(req, status);
1085                 END_PROFILE(SMBsetatr);
1086                 return;
1087         }
1088
1089         status = unix_convert(ctx, conn, fname, False, &fname, NULL, &sbuf);
1090         if (!NT_STATUS_IS_OK(status)) {
1091                 reply_nterror(req, status);
1092                 END_PROFILE(SMBsetatr);
1093                 return;
1094         }
1095
1096         status = check_name(conn, fname);
1097         if (!NT_STATUS_IS_OK(status)) {
1098                 reply_nterror(req, status);
1099                 END_PROFILE(SMBsetatr);
1100                 return;
1101         }
1102
1103         if (fname[0] == '.' && fname[1] == '\0') {
1104                 /*
1105                  * Not sure here is the right place to catch this
1106                  * condition. Might be moved to somewhere else later -- vl
1107                  */
1108                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1109                 END_PROFILE(SMBsetatr);
1110                 return;
1111         }
1112
1113         mode = SVAL(req->inbuf,smb_vwv0);
1114         mtime = srv_make_unix_date3(req->inbuf+smb_vwv1);
1115
1116         if (mode != FILE_ATTRIBUTE_NORMAL) {
1117                 if (VALID_STAT_OF_DIR(sbuf))
1118                         mode |= aDIR;
1119                 else
1120                         mode &= ~aDIR;
1121
1122                 if (file_set_dosmode(conn,fname,mode,&sbuf,NULL,false) != 0) {
1123                         reply_unixerror(req, ERRDOS, ERRnoaccess);
1124                         END_PROFILE(SMBsetatr);
1125                         return;
1126                 }
1127         }
1128
1129         if (!set_filetime(conn,fname,convert_time_t_to_timespec(mtime))) {
1130                 reply_unixerror(req, ERRDOS, ERRnoaccess);
1131                 END_PROFILE(SMBsetatr);
1132                 return;
1133         }
1134
1135         reply_outbuf(req, 0, 0);
1136  
1137         DEBUG( 3, ( "setatr name=%s mode=%d\n", fname, mode ) );
1138   
1139         END_PROFILE(SMBsetatr);
1140         return;
1141 }
1142
1143 /****************************************************************************
1144  Reply to a dskattr.
1145 ****************************************************************************/
1146
1147 void reply_dskattr(struct smb_request *req)
1148 {
1149         connection_struct *conn = req->conn;
1150         SMB_BIG_UINT dfree,dsize,bsize;
1151         START_PROFILE(SMBdskattr);
1152
1153         if (get_dfree_info(conn,".",True,&bsize,&dfree,&dsize) == (SMB_BIG_UINT)-1) {
1154                 reply_unixerror(req, ERRHRD, ERRgeneral);
1155                 END_PROFILE(SMBdskattr);
1156                 return;
1157         }
1158
1159         reply_outbuf(req, 5, 0);
1160         
1161         if (Protocol <= PROTOCOL_LANMAN2) {
1162                 double total_space, free_space;
1163                 /* we need to scale this to a number that DOS6 can handle. We
1164                    use floating point so we can handle large drives on systems
1165                    that don't have 64 bit integers 
1166
1167                    we end up displaying a maximum of 2G to DOS systems
1168                 */
1169                 total_space = dsize * (double)bsize;
1170                 free_space = dfree * (double)bsize;
1171
1172                 dsize = (SMB_BIG_UINT)((total_space+63*512) / (64*512));
1173                 dfree = (SMB_BIG_UINT)((free_space+63*512) / (64*512));
1174                 
1175                 if (dsize > 0xFFFF) dsize = 0xFFFF;
1176                 if (dfree > 0xFFFF) dfree = 0xFFFF;
1177
1178                 SSVAL(req->outbuf,smb_vwv0,dsize);
1179                 SSVAL(req->outbuf,smb_vwv1,64); /* this must be 64 for dos systems */
1180                 SSVAL(req->outbuf,smb_vwv2,512); /* and this must be 512 */
1181                 SSVAL(req->outbuf,smb_vwv3,dfree);
1182         } else {
1183                 SSVAL(req->outbuf,smb_vwv0,dsize);
1184                 SSVAL(req->outbuf,smb_vwv1,bsize/512);
1185                 SSVAL(req->outbuf,smb_vwv2,512);
1186                 SSVAL(req->outbuf,smb_vwv3,dfree);
1187         }
1188
1189         DEBUG(3,("dskattr dfree=%d\n", (unsigned int)dfree));
1190
1191         END_PROFILE(SMBdskattr);
1192         return;
1193 }
1194
1195 /****************************************************************************
1196  Reply to a search.
1197  Can be called from SMBsearch, SMBffirst or SMBfunique.
1198 ****************************************************************************/
1199
1200 void reply_search(struct smb_request *req)
1201 {
1202         connection_struct *conn = req->conn;
1203         char *mask = NULL;
1204         char *directory = NULL;
1205         char *fname = NULL;
1206         SMB_OFF_T size;
1207         uint32 mode;
1208         time_t date;
1209         uint32 dirtype;
1210         unsigned int numentries = 0;
1211         unsigned int maxentries = 0;
1212         bool finished = False;
1213         char *p;
1214         int status_len;
1215         char *path = NULL;
1216         char status[21];
1217         int dptr_num= -1;
1218         bool check_descend = False;
1219         bool expect_close = False;
1220         NTSTATUS nt_status;
1221         bool mask_contains_wcard = False;
1222         bool allow_long_path_components = (req->flags2 & FLAGS2_LONG_PATH_COMPONENTS) ? True : False;
1223         TALLOC_CTX *ctx = talloc_tos();
1224
1225         START_PROFILE(SMBsearch);
1226
1227         if (req->wct < 2) {
1228                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1229                 END_PROFILE(SMBsearch);
1230                 return;
1231         }
1232
1233         if (lp_posix_pathnames()) {
1234                 reply_unknown_new(req, CVAL(req->inbuf, smb_com));
1235                 END_PROFILE(SMBsearch);
1236                 return;
1237         }
1238
1239         /* If we were called as SMBffirst then we must expect close. */
1240         if(CVAL(req->inbuf,smb_com) == SMBffirst) {
1241                 expect_close = True;
1242         }
1243
1244         reply_outbuf(req, 1, 3);
1245         maxentries = SVAL(req->inbuf,smb_vwv0);
1246         dirtype = SVAL(req->inbuf,smb_vwv1);
1247         p = smb_buf(req->inbuf) + 1;
1248         p += srvstr_get_path_wcard(ctx,
1249                                 (char *)req->inbuf,
1250                                 req->flags2,
1251                                 &path,
1252                                 p,
1253                                 0,
1254                                 STR_TERMINATE,
1255                                 &nt_status,
1256                                 &mask_contains_wcard);
1257         if (!NT_STATUS_IS_OK(nt_status)) {
1258                 reply_nterror(req, nt_status);
1259                 END_PROFILE(SMBsearch);
1260                 return;
1261         }
1262
1263         nt_status = resolve_dfspath_wcard(ctx, conn,
1264                                           req->flags2 & FLAGS2_DFS_PATHNAMES,
1265                                           path,
1266                                           &path,
1267                                           &mask_contains_wcard);
1268         if (!NT_STATUS_IS_OK(nt_status)) {
1269                 if (NT_STATUS_EQUAL(nt_status,NT_STATUS_PATH_NOT_COVERED)) {
1270                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1271                                         ERRSRV, ERRbadpath);
1272                         END_PROFILE(SMBsearch);
1273                         return;
1274                 }
1275                 reply_nterror(req, nt_status);
1276                 END_PROFILE(SMBsearch);
1277                 return;
1278         }
1279
1280         p++;
1281         status_len = SVAL(p, 0);
1282         p += 2;
1283
1284         /* dirtype &= ~aDIR; */
1285
1286         if (status_len == 0) {
1287                 SMB_STRUCT_STAT sbuf;
1288
1289                 nt_status = unix_convert(ctx, conn, path, True,
1290                                 &directory, NULL, &sbuf);
1291                 if (!NT_STATUS_IS_OK(nt_status)) {
1292                         reply_nterror(req, nt_status);
1293                         END_PROFILE(SMBsearch);
1294                         return;
1295                 }
1296
1297                 nt_status = check_name(conn, directory);
1298                 if (!NT_STATUS_IS_OK(nt_status)) {
1299                         reply_nterror(req, nt_status);
1300                         END_PROFILE(SMBsearch);
1301                         return;
1302                 }
1303
1304                 p = strrchr_m(directory,'/');
1305                 if (!p) {
1306                         mask = directory;
1307                         directory = talloc_strdup(ctx,".");
1308                         if (!directory) {
1309                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
1310                                 END_PROFILE(SMBsearch);
1311                                 return;
1312                         }
1313                 } else {
1314                         *p = 0;
1315                         mask = p+1;
1316                 }
1317
1318                 if (*directory == '\0') {
1319                         directory = talloc_strdup(ctx,".");
1320                         if (!directory) {
1321                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
1322                                 END_PROFILE(SMBsearch);
1323                                 return;
1324                         }
1325                 }
1326                 memset((char *)status,'\0',21);
1327                 SCVAL(status,0,(dirtype & 0x1F));
1328
1329                 nt_status = dptr_create(conn,
1330                                         directory,
1331                                         True,
1332                                         expect_close,
1333                                         req->smbpid,
1334                                         mask,
1335                                         mask_contains_wcard,
1336                                         dirtype,
1337                                         &conn->dirptr);
1338                 if (!NT_STATUS_IS_OK(nt_status)) {
1339                         reply_nterror(req, nt_status);
1340                         END_PROFILE(SMBsearch);
1341                         return;
1342                 }
1343                 dptr_num = dptr_dnum(conn->dirptr);
1344         } else {
1345                 int status_dirtype;
1346
1347                 memcpy(status,p,21);
1348                 status_dirtype = CVAL(status,0) & 0x1F;
1349                 if (status_dirtype != (dirtype & 0x1F)) {
1350                         dirtype = status_dirtype;
1351                 }
1352
1353                 conn->dirptr = dptr_fetch(status+12,&dptr_num);
1354                 if (!conn->dirptr) {
1355                         goto SearchEmpty;
1356                 }
1357                 string_set(&conn->dirpath,dptr_path(dptr_num));
1358                 mask = dptr_wcard(dptr_num);
1359                 if (!mask) {
1360                         goto SearchEmpty;
1361                 }
1362                 /*
1363                  * For a 'continue' search we have no string. So
1364                  * check from the initial saved string.
1365                  */
1366                 mask_contains_wcard = ms_has_wild(mask);
1367                 dirtype = dptr_attr(dptr_num);
1368         }
1369
1370         DEBUG(4,("dptr_num is %d\n",dptr_num));
1371
1372         if ((dirtype&0x1F) == aVOLID) {
1373                 char buf[DIR_STRUCT_SIZE];
1374                 memcpy(buf,status,21);
1375                 if (!make_dir_struct(ctx,buf,"???????????",volume_label(SNUM(conn)),
1376                                 0,aVOLID,0,!allow_long_path_components)) {
1377                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1378                         END_PROFILE(SMBsearch);
1379                         return;
1380                 }
1381                 dptr_fill(buf+12,dptr_num);
1382                 if (dptr_zero(buf+12) && (status_len==0)) {
1383                         numentries = 1;
1384                 } else {
1385                         numentries = 0;
1386                 }
1387                 if (message_push_blob(&req->outbuf,
1388                                       data_blob_const(buf, sizeof(buf)))
1389                     == -1) {
1390                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1391                         END_PROFILE(SMBsearch);
1392                         return;
1393                 }
1394         } else {
1395                 unsigned int i;
1396                 maxentries = MIN(
1397                         maxentries,
1398                         ((BUFFER_SIZE -
1399                           ((uint8 *)smb_buf(req->outbuf) + 3 - req->outbuf))
1400                          /DIR_STRUCT_SIZE));
1401
1402                 DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
1403                         conn->dirpath,lp_dontdescend(SNUM(conn))));
1404                 if (in_list(conn->dirpath, lp_dontdescend(SNUM(conn)),True)) {
1405                         check_descend = True;
1406                 }
1407
1408                 for (i=numentries;(i<maxentries) && !finished;i++) {
1409                         finished = !get_dir_entry(ctx,conn,mask,dirtype,&fname,
1410                                         &size,&mode,&date,check_descend);
1411                         if (!finished) {
1412                                 char buf[DIR_STRUCT_SIZE];
1413                                 memcpy(buf,status,21);
1414                                 if (!make_dir_struct(ctx,
1415                                                 buf,
1416                                                 mask,
1417                                                 fname,
1418                                                 size,
1419                                                 mode,
1420                                                 date,
1421                                                 !allow_long_path_components)) {
1422                                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1423                                         END_PROFILE(SMBsearch);
1424                                         return;
1425                                 }
1426                                 if (!dptr_fill(buf+12,dptr_num)) {
1427                                         break;
1428                                 }
1429                                 if (message_push_blob(&req->outbuf,
1430                                                       data_blob_const(buf, sizeof(buf)))
1431                                     == -1) {
1432                                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1433                                         END_PROFILE(SMBsearch);
1434                                         return;
1435                                 }
1436                                 numentries++;
1437                         }
1438                 }
1439         }
1440
1441   SearchEmpty:
1442
1443         /* If we were called as SMBffirst with smb_search_id == NULL
1444                 and no entries were found then return error and close dirptr 
1445                 (X/Open spec) */
1446
1447         if (numentries == 0) {
1448                 dptr_close(&dptr_num);
1449         } else if(expect_close && status_len == 0) {
1450                 /* Close the dptr - we know it's gone */
1451                 dptr_close(&dptr_num);
1452         }
1453
1454         /* If we were called as SMBfunique, then we can close the dirptr now ! */
1455         if(dptr_num >= 0 && CVAL(req->inbuf,smb_com) == SMBfunique) {
1456                 dptr_close(&dptr_num);
1457         }
1458
1459         if ((numentries == 0) && !mask_contains_wcard) {
1460                 reply_botherror(req, STATUS_NO_MORE_FILES, ERRDOS, ERRnofiles);
1461                 END_PROFILE(SMBsearch);
1462                 return;
1463         }
1464
1465         SSVAL(req->outbuf,smb_vwv0,numentries);
1466         SSVAL(req->outbuf,smb_vwv1,3 + numentries * DIR_STRUCT_SIZE);
1467         SCVAL(smb_buf(req->outbuf),0,5);
1468         SSVAL(smb_buf(req->outbuf),1,numentries*DIR_STRUCT_SIZE);
1469
1470         /* The replies here are never long name. */
1471         SSVAL(req->outbuf, smb_flg2,
1472               SVAL(req->outbuf, smb_flg2) & (~FLAGS2_IS_LONG_NAME));
1473         if (!allow_long_path_components) {
1474                 SSVAL(req->outbuf, smb_flg2,
1475                       SVAL(req->outbuf, smb_flg2)
1476                       & (~FLAGS2_LONG_PATH_COMPONENTS));
1477         }
1478
1479         /* This SMB *always* returns ASCII names. Remove the unicode bit in flags2. */
1480         SSVAL(req->outbuf, smb_flg2,
1481               (SVAL(req->outbuf, smb_flg2) & (~FLAGS2_UNICODE_STRINGS)));
1482
1483         if (!directory) {
1484                 directory = dptr_path(dptr_num);
1485         }
1486
1487         DEBUG(4,("%s mask=%s path=%s dtype=%d nument=%u of %u\n",
1488                 smb_fn_name(CVAL(req->inbuf,smb_com)),
1489                 mask,
1490                 directory ? directory : "./",
1491                 dirtype,
1492                 numentries,
1493                 maxentries ));
1494
1495         END_PROFILE(SMBsearch);
1496         return;
1497 }
1498
1499 /****************************************************************************
1500  Reply to a fclose (stop directory search).
1501 ****************************************************************************/
1502
1503 void reply_fclose(struct smb_request *req)
1504 {
1505         int status_len;
1506         char status[21];
1507         int dptr_num= -2;
1508         char *p;
1509         char *path = NULL;
1510         NTSTATUS err;
1511         bool path_contains_wcard = False;
1512         TALLOC_CTX *ctx = talloc_tos();
1513
1514         START_PROFILE(SMBfclose);
1515
1516         if (lp_posix_pathnames()) {
1517                 reply_unknown_new(req, CVAL(req->inbuf, smb_com));
1518                 END_PROFILE(SMBfclose);
1519                 return;
1520         }
1521
1522         p = smb_buf(req->inbuf) + 1;
1523         p += srvstr_get_path_wcard(ctx,
1524                                 (char *)req->inbuf,
1525                                 req->flags2,
1526                                 &path,
1527                                 p,
1528                                 0,
1529                                 STR_TERMINATE,
1530                                 &err,
1531                                 &path_contains_wcard);
1532         if (!NT_STATUS_IS_OK(err)) {
1533                 reply_nterror(req, err);
1534                 END_PROFILE(SMBfclose);
1535                 return;
1536         }
1537         p++;
1538         status_len = SVAL(p,0);
1539         p += 2;
1540
1541         if (status_len == 0) {
1542                 reply_doserror(req, ERRSRV, ERRsrverror);
1543                 END_PROFILE(SMBfclose);
1544                 return;
1545         }
1546
1547         memcpy(status,p,21);
1548
1549         if(dptr_fetch(status+12,&dptr_num)) {
1550                 /*  Close the dptr - we know it's gone */
1551                 dptr_close(&dptr_num);
1552         }
1553
1554         reply_outbuf(req, 1, 0);
1555         SSVAL(req->outbuf,smb_vwv0,0);
1556
1557         DEBUG(3,("search close\n"));
1558
1559         END_PROFILE(SMBfclose);
1560         return;
1561 }
1562
1563 /****************************************************************************
1564  Reply to an open.
1565 ****************************************************************************/
1566
1567 void reply_open(struct smb_request *req)
1568 {
1569         connection_struct *conn = req->conn;
1570         char *fname = NULL;
1571         uint32 fattr=0;
1572         SMB_OFF_T size = 0;
1573         time_t mtime=0;
1574         int info;
1575         SMB_STRUCT_STAT sbuf;
1576         files_struct *fsp;
1577         int oplock_request;
1578         int deny_mode;
1579         uint32 dos_attr;
1580         uint32 access_mask;
1581         uint32 share_mode;
1582         uint32 create_disposition;
1583         uint32 create_options = 0;
1584         NTSTATUS status;
1585         TALLOC_CTX *ctx = talloc_tos();
1586
1587         START_PROFILE(SMBopen);
1588
1589         if (req->wct < 2) {
1590                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1591                 END_PROFILE(SMBopen);
1592                 return;
1593         }
1594
1595         oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
1596         deny_mode = SVAL(req->inbuf,smb_vwv0);
1597         dos_attr = SVAL(req->inbuf,smb_vwv1);
1598
1599         srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
1600                         smb_buf(req->inbuf)+1, 0,
1601                         STR_TERMINATE, &status);
1602         if (!NT_STATUS_IS_OK(status)) {
1603                 reply_nterror(req, status);
1604                 END_PROFILE(SMBopen);
1605                 return;
1606         }
1607
1608         if (!map_open_params_to_ntcreate(
1609                     fname, deny_mode, OPENX_FILE_EXISTS_OPEN, &access_mask,
1610                     &share_mode, &create_disposition, &create_options)) {
1611                 reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRbadaccess));
1612                 END_PROFILE(SMBopen);
1613                 return;
1614         }
1615
1616         status = create_file(conn,                      /* conn */
1617                              req,                       /* req */
1618                              0,                         /* root_dir_fid */
1619                              fname,                     /* fname */
1620                              access_mask,               /* access_mask */
1621                              share_mode,                /* share_access */
1622                              create_disposition,        /* create_disposition*/
1623                              create_options,            /* create_options */
1624                              dos_attr,                  /* file_attributes */
1625                              oplock_request,            /* oplock_request */
1626                              0,                         /* allocation_size */
1627                              NULL,                      /* sd */
1628                              NULL,                      /* ea_list */
1629                              &fsp,                      /* result */
1630                              &info,                     /* pinfo */
1631                              &sbuf);                    /* psbuf */
1632
1633         if (!NT_STATUS_IS_OK(status)) {
1634                 if (open_was_deferred(req->mid)) {
1635                         /* We have re-scheduled this call. */
1636                         END_PROFILE(SMBopen);
1637                         return;
1638                 }
1639                 reply_openerror(req, status);
1640                 END_PROFILE(SMBopen);
1641                 return;
1642         }
1643
1644         size = sbuf.st_size;
1645         fattr = dos_mode(conn,fname,&sbuf);
1646         mtime = sbuf.st_mtime;
1647
1648         if (fattr & aDIR) {
1649                 DEBUG(3,("attempt to open a directory %s\n",fname));
1650                 close_file(fsp,ERROR_CLOSE);
1651                 reply_doserror(req, ERRDOS,ERRnoaccess);
1652                 END_PROFILE(SMBopen);
1653                 return;
1654         }
1655
1656         reply_outbuf(req, 7, 0);
1657         SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
1658         SSVAL(req->outbuf,smb_vwv1,fattr);
1659         if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1660                 srv_put_dos_date3((char *)req->outbuf,smb_vwv2,mtime & ~1);
1661         } else {
1662                 srv_put_dos_date3((char *)req->outbuf,smb_vwv2,mtime);
1663         }
1664         SIVAL(req->outbuf,smb_vwv4,(uint32)size);
1665         SSVAL(req->outbuf,smb_vwv6,deny_mode);
1666
1667         if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1668                 SCVAL(req->outbuf,smb_flg,
1669                       CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1670         }
1671     
1672         if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1673                 SCVAL(req->outbuf,smb_flg,
1674                       CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1675         }
1676         END_PROFILE(SMBopen);
1677         return;
1678 }
1679
1680 /****************************************************************************
1681  Reply to an open and X.
1682 ****************************************************************************/
1683
1684 void reply_open_and_X(struct smb_request *req)
1685 {
1686         connection_struct *conn = req->conn;
1687         char *fname = NULL;
1688         uint16 open_flags;
1689         int deny_mode;
1690         uint32 smb_attr;
1691         /* Breakout the oplock request bits so we can set the
1692                 reply bits separately. */
1693         int ex_oplock_request;
1694         int core_oplock_request;
1695         int oplock_request;
1696 #if 0
1697         int smb_sattr = SVAL(req->inbuf,smb_vwv4);
1698         uint32 smb_time = make_unix_date3(req->inbuf+smb_vwv6);
1699 #endif
1700         int smb_ofun;
1701         uint32 fattr=0;
1702         int mtime=0;
1703         SMB_STRUCT_STAT sbuf;
1704         int smb_action = 0;
1705         files_struct *fsp;
1706         NTSTATUS status;
1707         SMB_BIG_UINT allocation_size;
1708         ssize_t retval = -1;
1709         uint32 access_mask;
1710         uint32 share_mode;
1711         uint32 create_disposition;
1712         uint32 create_options = 0;
1713         TALLOC_CTX *ctx = talloc_tos();
1714
1715         START_PROFILE(SMBopenX);
1716
1717         if (req->wct < 15) {
1718                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1719                 END_PROFILE(SMBopenX);
1720                 return;
1721         }
1722
1723         open_flags = SVAL(req->inbuf,smb_vwv2);
1724         deny_mode = SVAL(req->inbuf,smb_vwv3);
1725         smb_attr = SVAL(req->inbuf,smb_vwv5);
1726         ex_oplock_request = EXTENDED_OPLOCK_REQUEST(req->inbuf);
1727         core_oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
1728         oplock_request = ex_oplock_request | core_oplock_request;
1729         smb_ofun = SVAL(req->inbuf,smb_vwv8);
1730         allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,smb_vwv9);
1731
1732         /* If it's an IPC, pass off the pipe handler. */
1733         if (IS_IPC(conn)) {
1734                 if (lp_nt_pipe_support()) {
1735                         reply_open_pipe_and_X(conn, req);
1736                 } else {
1737                         reply_doserror(req, ERRSRV, ERRaccess);
1738                 }
1739                 END_PROFILE(SMBopenX);
1740                 return;
1741         }
1742
1743         /* XXXX we need to handle passed times, sattr and flags */
1744         srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
1745                         smb_buf(req->inbuf), 0, STR_TERMINATE,
1746                         &status);
1747         if (!NT_STATUS_IS_OK(status)) {
1748                 reply_nterror(req, status);
1749                 END_PROFILE(SMBopenX);
1750                 return;
1751         }
1752
1753         if (!map_open_params_to_ntcreate(
1754                     fname, deny_mode, smb_ofun, &access_mask,
1755                     &share_mode, &create_disposition, &create_options)) {
1756                 reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRbadaccess));
1757                 END_PROFILE(SMBopenX);
1758                 return;
1759         }
1760
1761         status = create_file(conn,                      /* conn */
1762                              req,                       /* req */
1763                              0,                         /* root_dir_fid */
1764                              fname,                     /* fname */
1765                              access_mask,               /* access_mask */
1766                              share_mode,                /* share_access */
1767                              create_disposition,        /* create_disposition*/
1768                              create_options,            /* create_options */
1769                              smb_attr,                  /* file_attributes */
1770                              oplock_request,            /* oplock_request */
1771                              0,                         /* allocation_size */
1772                              NULL,                      /* sd */
1773                              NULL,                      /* ea_list */
1774                              &fsp,                      /* result */
1775                              &smb_action,               /* pinfo */
1776                              &sbuf);                    /* psbuf */
1777
1778         if (!NT_STATUS_IS_OK(status)) {
1779                 END_PROFILE(SMBopenX);
1780                 if (open_was_deferred(req->mid)) {
1781                         /* We have re-scheduled this call. */
1782                         return;
1783                 }
1784                 reply_openerror(req, status);
1785                 return;
1786         }
1787
1788         /* Setting the "size" field in vwv9 and vwv10 causes the file to be set to this size,
1789            if the file is truncated or created. */
1790         if (((smb_action == FILE_WAS_CREATED) || (smb_action == FILE_WAS_OVERWRITTEN)) && allocation_size) {
1791                 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1792                 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1793                         close_file(fsp,ERROR_CLOSE);
1794                         reply_nterror(req, NT_STATUS_DISK_FULL);
1795                         END_PROFILE(SMBopenX);
1796                         return;
1797                 }
1798                 retval = vfs_set_filelen(fsp, (SMB_OFF_T)allocation_size);
1799                 if (retval < 0) {
1800                         close_file(fsp,ERROR_CLOSE);
1801                         reply_nterror(req, NT_STATUS_DISK_FULL);
1802                         END_PROFILE(SMBopenX);
1803                         return;
1804                 }
1805                 sbuf.st_size = get_allocation_size(conn,fsp,&sbuf);
1806         }
1807
1808         fattr = dos_mode(conn,fname,&sbuf);
1809         mtime = sbuf.st_mtime;
1810         if (fattr & aDIR) {
1811                 close_file(fsp,ERROR_CLOSE);
1812                 reply_doserror(req, ERRDOS, ERRnoaccess);
1813                 END_PROFILE(SMBopenX);
1814                 return;
1815         }
1816
1817         /* If the caller set the extended oplock request bit
1818                 and we granted one (by whatever means) - set the
1819                 correct bit for extended oplock reply.
1820         */
1821
1822         if (ex_oplock_request && lp_fake_oplocks(SNUM(conn))) {
1823                 smb_action |= EXTENDED_OPLOCK_GRANTED;
1824         }
1825
1826         if(ex_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1827                 smb_action |= EXTENDED_OPLOCK_GRANTED;
1828         }
1829
1830         /* If the caller set the core oplock request bit
1831                 and we granted one (by whatever means) - set the
1832                 correct bit for core oplock reply.
1833         */
1834
1835         if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
1836                 reply_outbuf(req, 19, 0);
1837         } else {
1838                 reply_outbuf(req, 15, 0);
1839         }
1840
1841         if (core_oplock_request && lp_fake_oplocks(SNUM(conn))) {
1842                 SCVAL(req->outbuf, smb_flg,
1843                       CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1844         }
1845
1846         if(core_oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1847                 SCVAL(req->outbuf, smb_flg,
1848                       CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1849         }
1850
1851         SSVAL(req->outbuf,smb_vwv2,fsp->fnum);
1852         SSVAL(req->outbuf,smb_vwv3,fattr);
1853         if(lp_dos_filetime_resolution(SNUM(conn)) ) {
1854                 srv_put_dos_date3((char *)req->outbuf,smb_vwv4,mtime & ~1);
1855         } else {
1856                 srv_put_dos_date3((char *)req->outbuf,smb_vwv4,mtime);
1857         }
1858         SIVAL(req->outbuf,smb_vwv6,(uint32)sbuf.st_size);
1859         SSVAL(req->outbuf,smb_vwv8,GET_OPENX_MODE(deny_mode));
1860         SSVAL(req->outbuf,smb_vwv11,smb_action);
1861
1862         if (open_flags & EXTENDED_RESPONSE_REQUIRED) {
1863                 SIVAL(req->outbuf, smb_vwv15, STD_RIGHT_ALL_ACCESS);
1864         }
1865
1866         END_PROFILE(SMBopenX);
1867         chain_reply(req);
1868         return;
1869 }
1870
1871 /****************************************************************************
1872  Reply to a SMBulogoffX.
1873 ****************************************************************************/
1874
1875 void reply_ulogoffX(struct smb_request *req)
1876 {
1877         user_struct *vuser;
1878
1879         START_PROFILE(SMBulogoffX);
1880
1881         vuser = get_valid_user_struct(req->vuid);
1882
1883         if(vuser == NULL) {
1884                 DEBUG(3,("ulogoff, vuser id %d does not map to user.\n",
1885                          req->vuid));
1886         }
1887
1888         /* in user level security we are supposed to close any files
1889                 open by this user */
1890         if ((vuser != NULL) && (lp_security() != SEC_SHARE)) {
1891                 file_close_user(req->vuid);
1892         }
1893
1894         invalidate_vuid(req->vuid);
1895
1896         reply_outbuf(req, 2, 0);
1897
1898         DEBUG( 3, ( "ulogoffX vuid=%d\n", req->vuid ) );
1899
1900         END_PROFILE(SMBulogoffX);
1901         chain_reply(req);
1902 }
1903
1904 /****************************************************************************
1905  Reply to a mknew or a create.
1906 ****************************************************************************/
1907
1908 void reply_mknew(struct smb_request *req)
1909 {
1910         connection_struct *conn = req->conn;
1911         char *fname = NULL;
1912         int com;
1913         uint32 fattr = 0;
1914         struct timespec ts[2];
1915         files_struct *fsp;
1916         int oplock_request = 0;
1917         SMB_STRUCT_STAT sbuf;
1918         NTSTATUS status;
1919         uint32 access_mask = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
1920         uint32 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
1921         uint32 create_disposition;
1922         uint32 create_options = 0;
1923         TALLOC_CTX *ctx = talloc_tos();
1924
1925         START_PROFILE(SMBcreate);
1926
1927         if (req->wct < 3) {
1928                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1929                 END_PROFILE(SMBcreate);
1930                 return;
1931         }
1932
1933         fattr = SVAL(req->inbuf,smb_vwv0);
1934         oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
1935         com = SVAL(req->inbuf,smb_com);
1936
1937         ts[1] =convert_time_t_to_timespec(
1938                         srv_make_unix_date3(req->inbuf + smb_vwv1));
1939                         /* mtime. */
1940
1941         srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
1942                         smb_buf(req->inbuf) + 1, 0,
1943                         STR_TERMINATE, &status);
1944         if (!NT_STATUS_IS_OK(status)) {
1945                 reply_nterror(req, status);
1946                 END_PROFILE(SMBcreate);
1947                 return;
1948         }
1949
1950         if (fattr & aVOLID) {
1951                 DEBUG(0,("Attempt to create file (%s) with volid set - "
1952                         "please report this\n", fname));
1953         }
1954
1955         if(com == SMBmknew) {
1956                 /* We should fail if file exists. */
1957                 create_disposition = FILE_CREATE;
1958         } else {
1959                 /* Create if file doesn't exist, truncate if it does. */
1960                 create_disposition = FILE_OVERWRITE_IF;
1961         }
1962
1963         status = create_file(conn,                      /* conn */
1964                              req,                       /* req */
1965                              0,                         /* root_dir_fid */
1966                              fname,                     /* fname */
1967                              access_mask,               /* access_mask */
1968                              share_mode,                /* share_access */
1969                              create_disposition,        /* create_disposition*/
1970                              create_options,            /* create_options */
1971                              fattr,                     /* file_attributes */
1972                              oplock_request,            /* oplock_request */
1973                              0,                         /* allocation_size */
1974                              NULL,                      /* sd */
1975                              NULL,                      /* ea_list */
1976                              &fsp,                      /* result */
1977                              NULL,                      /* pinfo */
1978                              &sbuf);                    /* psbuf */
1979
1980         if (!NT_STATUS_IS_OK(status)) {
1981                 END_PROFILE(SMBcreate);
1982                 if (open_was_deferred(req->mid)) {
1983                         /* We have re-scheduled this call. */
1984                         return;
1985                 }
1986                 reply_openerror(req, status);
1987                 return;
1988         }
1989
1990         ts[0] = get_atimespec(&sbuf); /* atime. */
1991         file_ntimes(conn, fname, ts);
1992
1993         reply_outbuf(req, 1, 0);
1994         SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
1995
1996         if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1997                 SCVAL(req->outbuf,smb_flg,
1998                                 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
1999         }
2000
2001         if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
2002                 SCVAL(req->outbuf,smb_flg,
2003                                 CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2004         }
2005
2006         DEBUG( 2, ( "reply_mknew: file %s\n", fname ) );
2007         DEBUG( 3, ( "reply_mknew %s fd=%d dmode=0x%x\n",
2008                                 fname, fsp->fh->fd, (unsigned int)fattr ) );
2009
2010         END_PROFILE(SMBcreate);
2011         return;
2012 }
2013
2014 /****************************************************************************
2015  Reply to a create temporary file.
2016 ****************************************************************************/
2017
2018 void reply_ctemp(struct smb_request *req)
2019 {
2020         connection_struct *conn = req->conn;
2021         char *fname = NULL;
2022         uint32 fattr;
2023         files_struct *fsp;
2024         int oplock_request;
2025         int tmpfd;
2026         SMB_STRUCT_STAT sbuf;
2027         char *s;
2028         NTSTATUS status;
2029         TALLOC_CTX *ctx = talloc_tos();
2030
2031         START_PROFILE(SMBctemp);
2032
2033         if (req->wct < 3) {
2034                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2035                 END_PROFILE(SMBctemp);
2036                 return;
2037         }
2038
2039         fattr = SVAL(req->inbuf,smb_vwv0);
2040         oplock_request = CORE_OPLOCK_REQUEST(req->inbuf);
2041
2042         srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
2043                         smb_buf(req->inbuf)+1, 0, STR_TERMINATE,
2044                         &status);
2045         if (!NT_STATUS_IS_OK(status)) {
2046                 reply_nterror(req, status);
2047                 END_PROFILE(SMBctemp);
2048                 return;
2049         }
2050         if (*fname) {
2051                 fname = talloc_asprintf(ctx,
2052                                 "%s/TMXXXXXX",
2053                                 fname);
2054         } else {
2055                 fname = talloc_strdup(ctx, "TMXXXXXX");
2056         }
2057
2058         if (!fname) {
2059                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2060                 END_PROFILE(SMBctemp);
2061                 return;
2062         }
2063
2064         status = resolve_dfspath(ctx, conn,
2065                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
2066                                 fname,
2067                                 &fname);
2068         if (!NT_STATUS_IS_OK(status)) {
2069                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2070                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2071                                         ERRSRV, ERRbadpath);
2072                         END_PROFILE(SMBctemp);
2073                         return;
2074                 }
2075                 reply_nterror(req, status);
2076                 END_PROFILE(SMBctemp);
2077                 return;
2078         }
2079
2080         status = unix_convert(ctx, conn, fname, False, &fname, NULL, &sbuf);
2081         if (!NT_STATUS_IS_OK(status)) {
2082                 reply_nterror(req, status);
2083                 END_PROFILE(SMBctemp);
2084                 return;
2085         }
2086
2087         status = check_name(conn, CONST_DISCARD(char *,fname));
2088         if (!NT_STATUS_IS_OK(status)) {
2089                 reply_nterror(req, status);
2090                 END_PROFILE(SMBctemp);
2091                 return;
2092         }
2093
2094         tmpfd = smb_mkstemp(fname);
2095         if (tmpfd == -1) {
2096                 reply_unixerror(req, ERRDOS, ERRnoaccess);
2097                 END_PROFILE(SMBctemp);
2098                 return;
2099         }
2100
2101         SMB_VFS_STAT(conn,fname,&sbuf);
2102
2103         /* We should fail if file does not exist. */
2104         status = open_file_ntcreate(conn, req, fname, &sbuf,
2105                                 FILE_GENERIC_READ | FILE_GENERIC_WRITE,
2106                                 FILE_SHARE_READ|FILE_SHARE_WRITE,
2107                                 FILE_OPEN,
2108                                 0,
2109                                 fattr,
2110                                 oplock_request,
2111                                 NULL, &fsp);
2112
2113         /* close fd from smb_mkstemp() */
2114         close(tmpfd);
2115
2116         if (!NT_STATUS_IS_OK(status)) {
2117                 if (open_was_deferred(req->mid)) {
2118                         /* We have re-scheduled this call. */
2119                         END_PROFILE(SMBctemp);
2120                         return;
2121                 }
2122                 reply_openerror(req, status);
2123                 END_PROFILE(SMBctemp);
2124                 return;
2125         }
2126
2127         reply_outbuf(req, 1, 0);
2128         SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
2129
2130         /* the returned filename is relative to the directory */
2131         s = strrchr_m(fname, '/');
2132         if (!s) {
2133                 s = fname;
2134         } else {
2135                 s++;
2136         }
2137
2138 #if 0
2139         /* Tested vs W2K3 - this doesn't seem to be here - null terminated filename is the only
2140            thing in the byte section. JRA */
2141         SSVALS(p, 0, -1); /* what is this? not in spec */
2142 #endif
2143         if (message_push_string(&req->outbuf, s, STR_ASCII|STR_TERMINATE)
2144             == -1) {
2145                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2146                 END_PROFILE(SMBctemp);
2147                 return;
2148         }
2149
2150         if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
2151                 SCVAL(req->outbuf, smb_flg,
2152                       CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2153         }
2154   
2155         if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
2156                 SCVAL(req->outbuf, smb_flg,
2157                       CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
2158         }
2159
2160         DEBUG( 2, ( "reply_ctemp: created temp file %s\n", fname ) );
2161         DEBUG( 3, ( "reply_ctemp %s fd=%d umode=0%o\n", fname, fsp->fh->fd,
2162                         (unsigned int)sbuf.st_mode ) );
2163
2164         END_PROFILE(SMBctemp);
2165         return;
2166 }
2167
2168 /*******************************************************************
2169  Check if a user is allowed to rename a file.
2170 ********************************************************************/
2171
2172 static NTSTATUS can_rename(connection_struct *conn, files_struct *fsp,
2173                            uint16 dirtype, SMB_STRUCT_STAT *pst)
2174 {
2175         uint32 fmode;
2176
2177         if (!CAN_WRITE(conn)) {
2178                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
2179         }
2180
2181         fmode = dos_mode(conn, fsp->fsp_name, pst);
2182         if ((fmode & ~dirtype) & (aHIDDEN | aSYSTEM)) {
2183                 return NT_STATUS_NO_SUCH_FILE;
2184         }
2185
2186         if (S_ISDIR(pst->st_mode)) {
2187                 return NT_STATUS_OK;
2188         }
2189
2190         if (fsp->access_mask & DELETE_ACCESS) {
2191                 return NT_STATUS_OK;
2192         }
2193
2194         return NT_STATUS_ACCESS_DENIED;
2195 }
2196
2197 /*******************************************************************
2198  * unlink a file with all relevant access checks
2199  *******************************************************************/
2200
2201 static NTSTATUS do_unlink(connection_struct *conn,
2202                         struct smb_request *req,
2203                         const char *fname,
2204                         uint32 dirtype)
2205 {
2206         SMB_STRUCT_STAT sbuf;
2207         uint32 fattr;
2208         files_struct *fsp;
2209         uint32 dirtype_orig = dirtype;
2210         NTSTATUS status;
2211
2212         DEBUG(10,("do_unlink: %s, dirtype = %d\n", fname, dirtype ));
2213
2214         if (!CAN_WRITE(conn)) {
2215                 return NT_STATUS_MEDIA_WRITE_PROTECTED;
2216         }
2217
2218         if (SMB_VFS_LSTAT(conn,fname,&sbuf) != 0) {
2219                 return map_nt_error_from_unix(errno);
2220         }
2221
2222         fattr = dos_mode(conn,fname,&sbuf);
2223
2224         if (dirtype & FILE_ATTRIBUTE_NORMAL) {
2225                 dirtype = aDIR|aARCH|aRONLY;
2226         }
2227
2228         dirtype &= (aDIR|aARCH|aRONLY|aHIDDEN|aSYSTEM);
2229         if (!dirtype) {
2230                 return NT_STATUS_NO_SUCH_FILE;
2231         }
2232
2233         if (!dir_check_ftype(conn, fattr, dirtype)) {
2234                 if (fattr & aDIR) {
2235                         return NT_STATUS_FILE_IS_A_DIRECTORY;
2236                 }
2237                 return NT_STATUS_NO_SUCH_FILE;
2238         }
2239
2240         if (dirtype_orig & 0x8000) {
2241                 /* These will never be set for POSIX. */
2242                 return NT_STATUS_NO_SUCH_FILE;
2243         }
2244
2245 #if 0
2246         if ((fattr & dirtype) & FILE_ATTRIBUTE_DIRECTORY) {
2247                 return NT_STATUS_FILE_IS_A_DIRECTORY;
2248         }
2249
2250         if ((fattr & ~dirtype) & (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM)) {
2251                 return NT_STATUS_NO_SUCH_FILE;
2252         }
2253
2254         if (dirtype & 0xFF00) {
2255                 /* These will never be set for POSIX. */
2256                 return NT_STATUS_NO_SUCH_FILE;
2257         }
2258
2259         dirtype &= 0xFF;
2260         if (!dirtype) {
2261                 return NT_STATUS_NO_SUCH_FILE;
2262         }
2263
2264         /* Can't delete a directory. */
2265         if (fattr & aDIR) {
2266                 return NT_STATUS_FILE_IS_A_DIRECTORY;
2267         }
2268 #endif
2269
2270 #if 0 /* JRATEST */
2271         else if (dirtype & aDIR) /* Asked for a directory and it isn't. */
2272                 return NT_STATUS_OBJECT_NAME_INVALID;
2273 #endif /* JRATEST */
2274
2275         /* Fix for bug #3035 from SATOH Fumiyasu <fumiyas@miraclelinux.com>
2276
2277           On a Windows share, a file with read-only dosmode can be opened with
2278           DELETE_ACCESS. But on a Samba share (delete readonly = no), it
2279           fails with NT_STATUS_CANNOT_DELETE error.
2280
2281           This semantic causes a problem that a user can not
2282           rename a file with read-only dosmode on a Samba share
2283           from a Windows command prompt (i.e. cmd.exe, but can rename
2284           from Windows Explorer).
2285         */
2286
2287         if (!lp_delete_readonly(SNUM(conn))) {
2288                 if (fattr & aRONLY) {
2289                         return NT_STATUS_CANNOT_DELETE;
2290                 }
2291         }
2292
2293         /* On open checks the open itself will check the share mode, so
2294            don't do it here as we'll get it wrong. */
2295
2296         status = create_file_unixpath
2297                 (conn,                  /* conn */
2298                  req,                   /* req */
2299                  fname,                 /* fname */
2300                  DELETE_ACCESS,         /* access_mask */
2301                  FILE_SHARE_NONE,       /* share_access */
2302                  FILE_OPEN,             /* create_disposition*/
2303                  FILE_NON_DIRECTORY_FILE, /* create_options */
2304                  FILE_ATTRIBUTE_NORMAL, /* file_attributes */
2305                  0,                     /* oplock_request */
2306                  0,                     /* allocation_size */
2307                  NULL,                  /* sd */
2308                  NULL,                  /* ea_list */
2309                  &fsp,                  /* result */
2310                  NULL,                  /* pinfo */
2311                  &sbuf);                /* psbuf */
2312
2313         if (!NT_STATUS_IS_OK(status)) {
2314                 DEBUG(10, ("open_file_ntcreate failed: %s\n",
2315                            nt_errstr(status)));
2316                 return status;
2317         }
2318
2319         /* The set is across all open files on this dev/inode pair. */
2320         if (!set_delete_on_close(fsp, True, &current_user.ut)) {
2321                 close_file(fsp, NORMAL_CLOSE);
2322                 return NT_STATUS_ACCESS_DENIED;
2323         }
2324
2325         return close_file(fsp,NORMAL_CLOSE);
2326 }
2327
2328 /****************************************************************************
2329  The guts of the unlink command, split out so it may be called by the NT SMB
2330  code.
2331 ****************************************************************************/
2332
2333 NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
2334                           uint32 dirtype, const char *name_in, bool has_wild)
2335 {
2336         const char *directory = NULL;
2337         char *mask = NULL;
2338         char *name = NULL;
2339         char *p = NULL;
2340         int count=0;
2341         NTSTATUS status = NT_STATUS_OK;
2342         SMB_STRUCT_STAT sbuf;
2343         TALLOC_CTX *ctx = talloc_tos();
2344
2345         status = unix_convert(ctx, conn, name_in, has_wild, &name, NULL, &sbuf);
2346         if (!NT_STATUS_IS_OK(status)) {
2347                 return status;
2348         }
2349
2350         p = strrchr_m(name,'/');
2351         if (!p) {
2352                 directory = talloc_strdup(ctx, ".");
2353                 if (!directory) {
2354                         return NT_STATUS_NO_MEMORY;
2355                 }
2356                 mask = name;
2357         } else {
2358                 *p = 0;
2359                 directory = name;
2360                 mask = p+1;
2361         }
2362
2363         /*
2364          * We should only check the mangled cache
2365          * here if unix_convert failed. This means
2366          * that the path in 'mask' doesn't exist
2367          * on the file system and so we need to look
2368          * for a possible mangle. This patch from
2369          * Tine Smukavec <valentin.smukavec@hermes.si>.
2370          */
2371
2372         if (!VALID_STAT(sbuf) && mangle_is_mangled(mask,conn->params)) {
2373                 char *new_mask = NULL;
2374                 mangle_lookup_name_from_8_3(ctx,
2375                                 mask,
2376                                 &new_mask,
2377                                 conn->params );
2378                 if (new_mask) {
2379                         mask = new_mask;
2380                 }
2381         }
2382
2383         if (!has_wild) {
2384                 directory = talloc_asprintf(ctx,
2385                                 "%s/%s",
2386                                 directory,
2387                                 mask);
2388                 if (!directory) {
2389                         return NT_STATUS_NO_MEMORY;
2390                 }
2391                 if (dirtype == 0) {
2392                         dirtype = FILE_ATTRIBUTE_NORMAL;
2393                 }
2394
2395                 status = check_name(conn, directory);
2396                 if (!NT_STATUS_IS_OK(status)) {
2397                         return status;
2398                 }
2399
2400                 status = do_unlink(conn, req, directory, dirtype);
2401                 if (!NT_STATUS_IS_OK(status)) {
2402                         return status;
2403                 }
2404
2405                 count++;
2406         } else {
2407                 struct smb_Dir *dir_hnd = NULL;
2408                 long offset = 0;
2409                 const char *dname;
2410
2411                 if ((dirtype & SAMBA_ATTRIBUTES_MASK) == aDIR) {
2412                         return NT_STATUS_OBJECT_NAME_INVALID;
2413                 }
2414
2415                 if (strequal(mask,"????????.???")) {
2416                         mask[0] = '*';
2417                         mask[1] = '\0';
2418                 }
2419
2420                 status = check_name(conn, directory);
2421                 if (!NT_STATUS_IS_OK(status)) {
2422                         return status;
2423                 }
2424
2425                 dir_hnd = OpenDir(talloc_tos(), conn, directory, mask,
2426                                   dirtype);
2427                 if (dir_hnd == NULL) {
2428                         return map_nt_error_from_unix(errno);
2429                 }
2430
2431                 /* XXXX the CIFS spec says that if bit0 of the flags2 field is set then
2432                    the pattern matches against the long name, otherwise the short name 
2433                    We don't implement this yet XXXX
2434                 */
2435
2436                 status = NT_STATUS_NO_SUCH_FILE;
2437
2438                 while ((dname = ReadDirName(dir_hnd, &offset))) {
2439                         SMB_STRUCT_STAT st;
2440                         char *fname = NULL;
2441
2442                         if (!is_visible_file(conn, directory, dname, &st, True)) {
2443                                 continue;
2444                         }
2445
2446                         /* Quick check for "." and ".." */
2447                         if (ISDOT(dname) || ISDOTDOT(dname)) {
2448                                 continue;
2449                         }
2450
2451                         if(!mask_match(dname, mask, conn->case_sensitive)) {
2452                                 continue;
2453                         }
2454
2455                         fname = talloc_asprintf(ctx, "%s/%s",
2456                                         directory,
2457                                         dname);
2458                         if (!fname) {
2459                                 return NT_STATUS_NO_MEMORY;
2460                         }
2461
2462                         status = check_name(conn, fname);
2463                         if (!NT_STATUS_IS_OK(status)) {
2464                                 TALLOC_FREE(dir_hnd);
2465                                 return status;
2466                         }
2467
2468                         status = do_unlink(conn, req, fname, dirtype);
2469                         if (!NT_STATUS_IS_OK(status)) {
2470                                 TALLOC_FREE(fname);
2471                                 continue;
2472                         }
2473
2474                         count++;
2475                         DEBUG(3,("unlink_internals: succesful unlink [%s]\n",
2476                                  fname));
2477
2478                         TALLOC_FREE(fname);
2479                 }
2480                 TALLOC_FREE(dir_hnd);
2481         }
2482
2483         if (count == 0 && NT_STATUS_IS_OK(status)) {
2484                 status = map_nt_error_from_unix(errno);
2485         }
2486
2487         return status;
2488 }
2489
2490 /****************************************************************************
2491  Reply to a unlink
2492 ****************************************************************************/
2493
2494 void reply_unlink(struct smb_request *req)
2495 {
2496         connection_struct *conn = req->conn;
2497         char *name = NULL;
2498         uint32 dirtype;
2499         NTSTATUS status;
2500         bool path_contains_wcard = False;
2501         TALLOC_CTX *ctx = talloc_tos();
2502
2503         START_PROFILE(SMBunlink);
2504
2505         if (req->wct < 1) {
2506                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2507                 END_PROFILE(SMBunlink);
2508                 return;
2509         }
2510
2511         dirtype = SVAL(req->inbuf,smb_vwv0);
2512
2513         srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &name,
2514                               smb_buf(req->inbuf) + 1, 0,
2515                               STR_TERMINATE, &status, &path_contains_wcard);
2516         if (!NT_STATUS_IS_OK(status)) {
2517                 reply_nterror(req, status);
2518                 END_PROFILE(SMBunlink);
2519                 return;
2520         }
2521
2522         status = resolve_dfspath_wcard(ctx, conn,
2523                                        req->flags2 & FLAGS2_DFS_PATHNAMES,
2524                                        name,
2525                                        &name,
2526                                        &path_contains_wcard);
2527         if (!NT_STATUS_IS_OK(status)) {
2528                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
2529                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
2530                                         ERRSRV, ERRbadpath);
2531                         END_PROFILE(SMBunlink);
2532                         return;
2533                 }
2534                 reply_nterror(req, status);
2535                 END_PROFILE(SMBunlink);
2536                 return;
2537         }
2538
2539         DEBUG(3,("reply_unlink : %s\n",name));
2540
2541         status = unlink_internals(conn, req, dirtype, name,
2542                                   path_contains_wcard);
2543         if (!NT_STATUS_IS_OK(status)) {
2544                 if (open_was_deferred(req->mid)) {
2545                         /* We have re-scheduled this call. */
2546                         END_PROFILE(SMBunlink);
2547                         return;
2548                 }
2549                 reply_nterror(req, status);
2550                 END_PROFILE(SMBunlink);
2551                 return;
2552         }
2553
2554         reply_outbuf(req, 0, 0);
2555         END_PROFILE(SMBunlink);
2556
2557         return;
2558 }
2559
2560 /****************************************************************************
2561  Fail for readbraw.
2562 ****************************************************************************/
2563
2564 static void fail_readraw(void)
2565 {
2566         const char *errstr = talloc_asprintf(talloc_tos(),
2567                         "FAIL ! reply_readbraw: socket write fail (%s)",
2568                         strerror(errno));
2569         if (!errstr) {
2570                 errstr = "";
2571         }
2572         exit_server_cleanly(errstr);
2573 }
2574
2575 /****************************************************************************
2576  Fake (read/write) sendfile. Returns -1 on read or write fail.
2577 ****************************************************************************/
2578
2579 static ssize_t fake_sendfile(files_struct *fsp, SMB_OFF_T startpos,
2580                              size_t nread)
2581 {
2582         size_t bufsize;
2583         size_t tosend = nread;
2584         char *buf;
2585
2586         if (nread == 0) {
2587                 return 0;
2588         }
2589
2590         bufsize = MIN(nread, 65536);
2591
2592         if (!(buf = SMB_MALLOC_ARRAY(char, bufsize))) {
2593                 return -1;
2594         }
2595
2596         while (tosend > 0) {
2597                 ssize_t ret;
2598                 size_t cur_read;
2599
2600                 if (tosend > bufsize) {
2601                         cur_read = bufsize;
2602                 } else {
2603                         cur_read = tosend;
2604                 }
2605                 ret = read_file(fsp,buf,startpos,cur_read);
2606                 if (ret == -1) {
2607                         SAFE_FREE(buf);
2608                         return -1;
2609                 }
2610
2611                 /* If we had a short read, fill with zeros. */
2612                 if (ret < cur_read) {
2613                         memset(buf, '\0', cur_read - ret);
2614                 }
2615
2616                 if (write_data(smbd_server_fd(),buf,cur_read) != cur_read) {
2617                         SAFE_FREE(buf);
2618                         return -1;
2619                 }
2620                 tosend -= cur_read;
2621                 startpos += cur_read;
2622         }
2623
2624         SAFE_FREE(buf);
2625         return (ssize_t)nread;
2626 }
2627
2628 /****************************************************************************
2629  Return a readbraw error (4 bytes of zero).
2630 ****************************************************************************/
2631
2632 static void reply_readbraw_error(void)
2633 {
2634         char header[4];
2635         SIVAL(header,0,0);
2636         if (write_data(smbd_server_fd(),header,4) != 4) {
2637                 fail_readraw();
2638         }
2639 }
2640
2641 /****************************************************************************
2642  Use sendfile in readbraw.
2643 ****************************************************************************/
2644
2645 void send_file_readbraw(connection_struct *conn,
2646                         files_struct *fsp,
2647                         SMB_OFF_T startpos,
2648                         size_t nread,
2649                         ssize_t mincount)
2650 {
2651         char *outbuf = NULL;
2652         ssize_t ret=0;
2653
2654 #if defined(WITH_SENDFILE)
2655         /*
2656          * We can only use sendfile on a non-chained packet 
2657          * but we can use on a non-oplocked file. tridge proved this
2658          * on a train in Germany :-). JRA.
2659          * reply_readbraw has already checked the length.
2660          */
2661
2662         if ( (chain_size == 0) && (nread > 0) &&
2663             (fsp->wcp == NULL) && lp_use_sendfile(SNUM(conn)) ) {
2664                 char header[4];
2665                 DATA_BLOB header_blob;
2666
2667                 _smb_setlen(header,nread);
2668                 header_blob = data_blob_const(header, 4);
2669
2670                 if (SMB_VFS_SENDFILE(smbd_server_fd(), fsp,
2671                                 &header_blob, startpos, nread) == -1) {
2672                         /* Returning ENOSYS means no data at all was sent.
2673                          * Do this as a normal read. */
2674                         if (errno == ENOSYS) {
2675                                 goto normal_readbraw;
2676                         }
2677
2678                         /*
2679                          * Special hack for broken Linux with no working sendfile. If we
2680                          * return EINTR we sent the header but not the rest of the data.
2681                          * Fake this up by doing read/write calls.
2682                          */
2683                         if (errno == EINTR) {
2684                                 /* Ensure we don't do this again. */
2685                                 set_use_sendfile(SNUM(conn), False);
2686                                 DEBUG(0,("send_file_readbraw: sendfile not available. Faking..\n"));
2687
2688                                 if (fake_sendfile(fsp, startpos, nread) == -1) {
2689                                         DEBUG(0,("send_file_readbraw: fake_sendfile failed for file %s (%s).\n",
2690                                                 fsp->fsp_name, strerror(errno) ));
2691                                         exit_server_cleanly("send_file_readbraw fake_sendfile failed");
2692                                 }
2693                                 return;
2694                         }
2695
2696                         DEBUG(0,("send_file_readbraw: sendfile failed for file %s (%s). Terminating\n",
2697                                 fsp->fsp_name, strerror(errno) ));
2698                         exit_server_cleanly("send_file_readbraw sendfile failed");
2699                 }
2700
2701                 return;
2702         }
2703 #endif
2704
2705 normal_readbraw:
2706
2707         outbuf = TALLOC_ARRAY(NULL, char, nread+4);
2708         if (!outbuf) {
2709                 DEBUG(0,("send_file_readbraw: TALLOC_ARRAY failed for size %u.\n",
2710                         (unsigned)(nread+4)));
2711                 reply_readbraw_error();
2712                 return;
2713         }
2714
2715         if (nread > 0) {
2716                 ret = read_file(fsp,outbuf+4,startpos,nread);
2717 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2718                 if (ret < mincount)
2719                         ret = 0;
2720 #else
2721                 if (ret < nread)
2722                         ret = 0;
2723 #endif
2724         }
2725
2726         _smb_setlen(outbuf,ret);
2727         if (write_data(smbd_server_fd(),outbuf,4+ret) != 4+ret)
2728                 fail_readraw();
2729
2730         TALLOC_FREE(outbuf);
2731 }
2732
2733 /****************************************************************************
2734  Reply to a readbraw (core+ protocol).
2735 ****************************************************************************/
2736
2737 void reply_readbraw(struct smb_request *req)
2738 {
2739         connection_struct *conn = req->conn;
2740         ssize_t maxcount,mincount;
2741         size_t nread = 0;
2742         SMB_OFF_T startpos;
2743         files_struct *fsp;
2744         SMB_STRUCT_STAT st;
2745         SMB_OFF_T size = 0;
2746
2747         START_PROFILE(SMBreadbraw);
2748
2749         if (srv_is_signing_active() || is_encrypted_packet(req->inbuf)) {
2750                 exit_server_cleanly("reply_readbraw: SMB signing/sealing is active - "
2751                         "raw reads/writes are disallowed.");
2752         }
2753
2754         if (req->wct < 8) {
2755                 reply_readbraw_error();
2756                 END_PROFILE(SMBreadbraw);
2757                 return;
2758         }
2759
2760         /*
2761          * Special check if an oplock break has been issued
2762          * and the readraw request croses on the wire, we must
2763          * return a zero length response here.
2764          */
2765
2766         fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
2767
2768         /*
2769          * We have to do a check_fsp by hand here, as
2770          * we must always return 4 zero bytes on error,
2771          * not a NTSTATUS.
2772          */
2773
2774         if (!fsp || !conn || conn != fsp->conn ||
2775                         current_user.vuid != fsp->vuid ||
2776                         fsp->is_directory || fsp->fh->fd == -1) {
2777                 /*
2778                  * fsp could be NULL here so use the value from the packet. JRA.
2779                  */
2780                 DEBUG(3,("reply_readbraw: fnum %d not valid "
2781                         "- cache prime?\n",
2782                         (int)SVAL(req->inbuf,smb_vwv0)));
2783                 reply_readbraw_error();
2784                 END_PROFILE(SMBreadbraw);
2785                 return;
2786         }
2787
2788         /* Do a "by hand" version of CHECK_READ. */
2789         if (!(fsp->can_read ||
2790                         ((req->flags2 & FLAGS2_READ_PERMIT_EXECUTE) &&
2791                                 (fsp->access_mask & FILE_EXECUTE)))) {
2792                 DEBUG(3,("reply_readbraw: fnum %d not readable.\n",
2793                                 (int)SVAL(req->inbuf,smb_vwv0)));
2794                 reply_readbraw_error();
2795                 END_PROFILE(SMBreadbraw);
2796                 return;
2797         }
2798
2799         flush_write_cache(fsp, READRAW_FLUSH);
2800
2801         startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv1);
2802         if(req->wct == 10) {
2803                 /*
2804                  * This is a large offset (64 bit) read.
2805                  */
2806 #ifdef LARGE_SMB_OFF_T
2807
2808                 startpos |= (((SMB_OFF_T)IVAL(req->inbuf,smb_vwv8)) << 32);
2809
2810 #else /* !LARGE_SMB_OFF_T */
2811
2812                 /*
2813                  * Ensure we haven't been sent a >32 bit offset.
2814                  */
2815
2816                 if(IVAL(req->inbuf,smb_vwv8) != 0) {
2817                         DEBUG(0,("reply_readbraw: large offset "
2818                                 "(%x << 32) used and we don't support "
2819                                 "64 bit offsets.\n",
2820                         (unsigned int)IVAL(req->inbuf,smb_vwv8) ));
2821                         reply_readbraw_error();
2822                         END_PROFILE(SMBreadbraw);
2823                         return;
2824                 }
2825
2826 #endif /* LARGE_SMB_OFF_T */
2827
2828                 if(startpos < 0) {
2829                         DEBUG(0,("reply_readbraw: negative 64 bit "
2830                                 "readraw offset (%.0f) !\n",
2831                                 (double)startpos ));
2832                         reply_readbraw_error();
2833                         END_PROFILE(SMBreadbraw);
2834                         return;
2835                 }      
2836         }
2837
2838         maxcount = (SVAL(req->inbuf,smb_vwv3) & 0xFFFF);
2839         mincount = (SVAL(req->inbuf,smb_vwv4) & 0xFFFF);
2840
2841         /* ensure we don't overrun the packet size */
2842         maxcount = MIN(65535,maxcount);
2843
2844         if (is_locked(fsp,(uint32)req->smbpid,
2845                         (SMB_BIG_UINT)maxcount,
2846                         (SMB_BIG_UINT)startpos,
2847                         READ_LOCK)) {
2848                 reply_readbraw_error();
2849                 END_PROFILE(SMBreadbraw);
2850                 return;
2851         }
2852
2853         if (SMB_VFS_FSTAT(fsp, &st) == 0) {
2854                 size = st.st_size;
2855         }
2856
2857         if (startpos >= size) {
2858                 nread = 0;
2859         } else {
2860                 nread = MIN(maxcount,(size - startpos));
2861         }
2862
2863 #if 0 /* mincount appears to be ignored in a W2K server. JRA. */
2864         if (nread < mincount)
2865                 nread = 0;
2866 #endif
2867   
2868         DEBUG( 3, ( "reply_readbraw: fnum=%d start=%.0f max=%lu "
2869                 "min=%lu nread=%lu\n",
2870                 fsp->fnum, (double)startpos,
2871                 (unsigned long)maxcount,
2872                 (unsigned long)mincount,
2873                 (unsigned long)nread ) );
2874   
2875         send_file_readbraw(conn, fsp, startpos, nread, mincount);
2876
2877         DEBUG(5,("reply_readbraw finished\n"));
2878         END_PROFILE(SMBreadbraw);
2879 }
2880
2881 #undef DBGC_CLASS
2882 #define DBGC_CLASS DBGC_LOCKING
2883
2884 /****************************************************************************
2885  Reply to a lockread (core+ protocol).
2886 ****************************************************************************/
2887
2888 void reply_lockread(struct smb_request *req)
2889 {
2890         connection_struct *conn = req->conn;
2891         ssize_t nread = -1;
2892         char *data;
2893         SMB_OFF_T startpos;
2894         size_t numtoread;
2895         NTSTATUS status;
2896         files_struct *fsp;
2897         struct byte_range_lock *br_lck = NULL;
2898         char *p = NULL;
2899
2900         START_PROFILE(SMBlockread);
2901
2902         if (req->wct < 5) {
2903                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2904                 END_PROFILE(SMBlockread);
2905                 return;
2906         }
2907
2908         fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
2909
2910         if (!check_fsp(conn, req, fsp, &current_user)) {
2911                 END_PROFILE(SMBlockread);
2912                 return;
2913         }
2914
2915         if (!CHECK_READ(fsp,req->inbuf)) {
2916                 reply_doserror(req, ERRDOS, ERRbadaccess);
2917                 END_PROFILE(SMBlockread);
2918                 return;
2919         }
2920
2921         release_level_2_oplocks_on_change(fsp);
2922
2923         numtoread = SVAL(req->inbuf,smb_vwv1);
2924         startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv2);
2925
2926         numtoread = MIN(BUFFER_SIZE - (smb_size + 3*2 + 3), numtoread);
2927
2928         reply_outbuf(req, 5, numtoread + 3);
2929
2930         data = smb_buf(req->outbuf) + 3;
2931         
2932         /*
2933          * NB. Discovered by Menny Hamburger at Mainsoft. This is a core+
2934          * protocol request that predates the read/write lock concept. 
2935          * Thus instead of asking for a read lock here we need to ask
2936          * for a write lock. JRA.
2937          * Note that the requested lock size is unaffected by max_recv.
2938          */
2939         
2940         br_lck = do_lock(smbd_messaging_context(),
2941                         fsp,
2942                         req->smbpid,
2943                         (SMB_BIG_UINT)numtoread,
2944                         (SMB_BIG_UINT)startpos,
2945                         WRITE_LOCK,
2946                         WINDOWS_LOCK,
2947                         False, /* Non-blocking lock. */
2948                         &status,
2949                         NULL);
2950         TALLOC_FREE(br_lck);
2951
2952         if (NT_STATUS_V(status)) {
2953                 reply_nterror(req, status);
2954                 END_PROFILE(SMBlockread);
2955                 return;
2956         }
2957
2958         /*
2959          * However the requested READ size IS affected by max_recv. Insanity.... JRA.
2960          */
2961
2962         if (numtoread > max_recv) {
2963                 DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u). \
2964 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
2965                         (unsigned int)numtoread, (unsigned int)max_recv ));
2966                 numtoread = MIN(numtoread,max_recv);
2967         }
2968         nread = read_file(fsp,data,startpos,numtoread);
2969
2970         if (nread < 0) {
2971                 reply_unixerror(req, ERRDOS, ERRnoaccess);
2972                 END_PROFILE(SMBlockread);
2973                 return;
2974         }
2975         
2976         srv_set_message((char *)req->outbuf, 5, nread+3, False);
2977
2978         SSVAL(req->outbuf,smb_vwv0,nread);
2979         SSVAL(req->outbuf,smb_vwv5,nread+3);
2980         p = smb_buf(req->outbuf);
2981         SCVAL(p,0,0); /* pad byte. */
2982         SSVAL(p,1,nread);
2983         
2984         DEBUG(3,("lockread fnum=%d num=%d nread=%d\n",
2985                  fsp->fnum, (int)numtoread, (int)nread));
2986
2987         END_PROFILE(SMBlockread);
2988         return;
2989 }
2990
2991 #undef DBGC_CLASS
2992 #define DBGC_CLASS DBGC_ALL
2993
2994 /****************************************************************************
2995  Reply to a read.
2996 ****************************************************************************/
2997
2998 void reply_read(struct smb_request *req)
2999 {
3000         connection_struct *conn = req->conn;
3001         size_t numtoread;
3002         ssize_t nread = 0;
3003         char *data;
3004         SMB_OFF_T startpos;
3005         int outsize = 0;
3006         files_struct *fsp;
3007
3008         START_PROFILE(SMBread);
3009
3010         if (req->wct < 3) {
3011                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3012                 END_PROFILE(SMBread);
3013                 return;
3014         }
3015
3016         fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
3017
3018         if (!check_fsp(conn, req, fsp, &current_user)) {
3019                 END_PROFILE(SMBread);
3020                 return;
3021         }
3022
3023         if (!CHECK_READ(fsp,req->inbuf)) {
3024                 reply_doserror(req, ERRDOS, ERRbadaccess);
3025                 END_PROFILE(SMBread);
3026                 return;
3027         }
3028
3029         numtoread = SVAL(req->inbuf,smb_vwv1);
3030         startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv2);
3031
3032         numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
3033
3034         /*
3035          * The requested read size cannot be greater than max_recv. JRA.
3036          */
3037         if (numtoread > max_recv) {
3038                 DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u). \
3039 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
3040                         (unsigned int)numtoread, (unsigned int)max_recv ));
3041                 numtoread = MIN(numtoread,max_recv);
3042         }
3043
3044         reply_outbuf(req, 5, numtoread+3);
3045
3046         data = smb_buf(req->outbuf) + 3;
3047   
3048         if (is_locked(fsp, (uint32)req->smbpid, (SMB_BIG_UINT)numtoread,
3049                       (SMB_BIG_UINT)startpos, READ_LOCK)) {
3050                 reply_doserror(req, ERRDOS,ERRlock);
3051                 END_PROFILE(SMBread);
3052                 return;
3053         }
3054
3055         if (numtoread > 0)
3056                 nread = read_file(fsp,data,startpos,numtoread);
3057
3058         if (nread < 0) {
3059                 reply_unixerror(req, ERRDOS,ERRnoaccess);
3060                 END_PROFILE(SMBread);
3061                 return;
3062         }
3063
3064         srv_set_message((char *)req->outbuf, 5, nread+3, False);
3065
3066         SSVAL(req->outbuf,smb_vwv0,nread);
3067         SSVAL(req->outbuf,smb_vwv5,nread+3);
3068         SCVAL(smb_buf(req->outbuf),0,1);
3069         SSVAL(smb_buf(req->outbuf),1,nread);
3070   
3071         DEBUG( 3, ( "read fnum=%d num=%d nread=%d\n",
3072                 fsp->fnum, (int)numtoread, (int)nread ) );
3073
3074         END_PROFILE(SMBread);
3075         return;
3076 }
3077
3078 /****************************************************************************
3079  Setup readX header.
3080 ****************************************************************************/
3081
3082 static int setup_readX_header(char *outbuf, size_t smb_maxcnt)
3083 {
3084         int outsize;
3085         char *data;
3086
3087         outsize = srv_set_message(outbuf,12,smb_maxcnt,False);
3088         data = smb_buf(outbuf);
3089
3090         memset(outbuf+smb_vwv0,'\0',24); /* valgrind init. */
3091
3092         SCVAL(outbuf,smb_vwv0,0xFF);
3093         SSVAL(outbuf,smb_vwv2,0xFFFF); /* Remaining - must be -1. */
3094         SSVAL(outbuf,smb_vwv5,smb_maxcnt);
3095         SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
3096         SSVAL(outbuf,smb_vwv7,(smb_maxcnt >> 16));
3097         SSVAL(smb_buf(outbuf),-2,smb_maxcnt);
3098         /* Reset the outgoing length, set_message truncates at 0x1FFFF. */
3099         _smb_setlen_large(outbuf,(smb_size + 12*2 + smb_maxcnt - 4));
3100         return outsize;
3101 }
3102
3103 /****************************************************************************
3104  Reply to a read and X - possibly using sendfile.
3105 ****************************************************************************/
3106
3107 static void send_file_readX(connection_struct *conn, struct smb_request *req,
3108                             files_struct *fsp, SMB_OFF_T startpos,
3109                             size_t smb_maxcnt)
3110 {
3111         SMB_STRUCT_STAT sbuf;
3112         ssize_t nread = -1;
3113
3114         if(SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
3115                 reply_unixerror(req, ERRDOS, ERRnoaccess);
3116                 return;
3117         }
3118
3119         if (startpos > sbuf.st_size) {
3120                 smb_maxcnt = 0;
3121         } else if (smb_maxcnt > (sbuf.st_size - startpos)) {
3122                 smb_maxcnt = (sbuf.st_size - startpos);
3123         }
3124
3125         if (smb_maxcnt == 0) {
3126                 goto normal_read;
3127         }
3128
3129 #if defined(WITH_SENDFILE)
3130         /*
3131          * We can only use sendfile on a non-chained packet
3132          * but we can use on a non-oplocked file. tridge proved this
3133          * on a train in Germany :-). JRA.
3134          */
3135
3136         if ((chain_size == 0) && (CVAL(req->inbuf,smb_vwv0) == 0xFF) &&
3137             !is_encrypted_packet(req->inbuf) &&
3138             lp_use_sendfile(SNUM(conn)) && (fsp->wcp == NULL) ) {
3139                 uint8 headerbuf[smb_size + 12 * 2];
3140                 DATA_BLOB header;
3141
3142                 /*
3143                  * Set up the packet header before send. We
3144                  * assume here the sendfile will work (get the
3145                  * correct amount of data).
3146                  */
3147
3148                 header = data_blob_const(headerbuf, sizeof(headerbuf));
3149
3150                 construct_reply_common((char *)req->inbuf, (char *)headerbuf);
3151                 setup_readX_header((char *)headerbuf, smb_maxcnt);
3152
3153                 if ((nread = SMB_VFS_SENDFILE(smbd_server_fd(), fsp, &header, startpos, smb_maxcnt)) == -1) {
3154                         /* Returning ENOSYS means no data at all was sent. Do this as a normal read. */
3155                         if (errno == ENOSYS) {
3156                                 goto normal_read;
3157                         }
3158
3159                         /*
3160                          * Special hack for broken Linux with no working sendfile. If we
3161                          * return EINTR we sent the header but not the rest of the data.
3162                          * Fake this up by doing read/write calls.
3163                          */
3164
3165                         if (errno == EINTR) {
3166                                 /* Ensure we don't do this again. */
3167                                 set_use_sendfile(SNUM(conn), False);
3168                                 DEBUG(0,("send_file_readX: sendfile not available. Faking..\n"));
3169                                 nread = fake_sendfile(fsp, startpos,
3170                                                       smb_maxcnt);
3171                                 if (nread == -1) {
3172                                         DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
3173                                                 fsp->fsp_name, strerror(errno) ));
3174                                         exit_server_cleanly("send_file_readX: fake_sendfile failed");
3175                                 }
3176                                 DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
3177                                         fsp->fnum, (int)smb_maxcnt, (int)nread ) );
3178                                 /* No outbuf here means successful sendfile. */
3179                                 TALLOC_FREE(req->outbuf);
3180                                 return;
3181                         }
3182
3183                         DEBUG(0,("send_file_readX: sendfile failed for file %s (%s). Terminating\n",
3184                                 fsp->fsp_name, strerror(errno) ));
3185                         exit_server_cleanly("send_file_readX sendfile failed");
3186                 }
3187
3188                 DEBUG( 3, ( "send_file_readX: sendfile fnum=%d max=%d nread=%d\n",
3189                         fsp->fnum, (int)smb_maxcnt, (int)nread ) );
3190                 /* No outbuf here means successful sendfile. */
3191                 TALLOC_FREE(req->outbuf);
3192                 return;
3193         }
3194 #endif
3195
3196 normal_read:
3197
3198         if ((smb_maxcnt & 0xFF0000) > 0x10000) {
3199                 uint8 headerbuf[smb_size + 2*12];
3200
3201                 construct_reply_common((char *)req->inbuf, (char *)headerbuf);
3202                 setup_readX_header((char *)headerbuf, smb_maxcnt);
3203
3204                 /* Send out the header. */
3205                 if (write_data(smbd_server_fd(), (char *)headerbuf,
3206                                sizeof(headerbuf)) != sizeof(headerbuf)) {
3207                         DEBUG(0,("send_file_readX: write_data failed for file %s (%s). Terminating\n",
3208                                 fsp->fsp_name, strerror(errno) ));
3209                         exit_server_cleanly("send_file_readX sendfile failed");
3210                 }
3211                 nread = fake_sendfile(fsp, startpos, smb_maxcnt);
3212                 if (nread == -1) {
3213                         DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
3214                                 fsp->fsp_name, strerror(errno) ));
3215                         exit_server_cleanly("send_file_readX: fake_sendfile failed");
3216                 }
3217                 TALLOC_FREE(req->outbuf);
3218                 return;
3219         } else {
3220                 reply_outbuf(req, 12, smb_maxcnt);
3221
3222                 nread = read_file(fsp, smb_buf(req->outbuf), startpos,
3223                                   smb_maxcnt);
3224                 if (nread < 0) {
3225                         reply_unixerror(req, ERRDOS, ERRnoaccess);
3226                         return;
3227                 }
3228
3229                 setup_readX_header((char *)req->outbuf, nread);
3230
3231                 DEBUG( 3, ( "send_file_readX fnum=%d max=%d nread=%d\n",
3232                         fsp->fnum, (int)smb_maxcnt, (int)nread ) );
3233
3234                 chain_reply(req);
3235
3236                 return;
3237         }
3238 }
3239
3240 /****************************************************************************
3241  Reply to a read and X.
3242 ****************************************************************************/
3243
3244 void reply_read_and_X(struct smb_request *req)
3245 {
3246         connection_struct *conn = req->conn;
3247         files_struct *fsp;
3248         SMB_OFF_T startpos;
3249         size_t smb_maxcnt;
3250         bool big_readX = False;
3251 #if 0
3252         size_t smb_mincnt = SVAL(req->inbuf,smb_vwv6);
3253 #endif
3254
3255         START_PROFILE(SMBreadX);
3256
3257         if ((req->wct != 10) && (req->wct != 12)) {
3258                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3259                 return;
3260         }
3261
3262         fsp = file_fsp(SVAL(req->inbuf,smb_vwv2));
3263         startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv3);
3264         smb_maxcnt = SVAL(req->inbuf,smb_vwv5);
3265
3266         /* If it's an IPC, pass off the pipe handler. */
3267         if (IS_IPC(conn)) {
3268                 reply_pipe_read_and_X(req);
3269                 END_PROFILE(SMBreadX);
3270                 return;
3271         }
3272
3273         if (!check_fsp(conn, req, fsp, &current_user)) {
3274                 END_PROFILE(SMBreadX);
3275                 return;
3276         }
3277
3278         if (!CHECK_READ(fsp,req->inbuf)) {
3279                 reply_doserror(req, ERRDOS,ERRbadaccess);
3280                 END_PROFILE(SMBreadX);
3281                 return;
3282         }
3283
3284         if (global_client_caps & CAP_LARGE_READX) {
3285                 size_t upper_size = SVAL(req->inbuf,smb_vwv7);
3286                 smb_maxcnt |= (upper_size<<16);
3287                 if (upper_size > 1) {
3288                         /* Can't do this on a chained packet. */
3289                         if ((CVAL(req->inbuf,smb_vwv0) != 0xFF)) {
3290                                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
3291                                 END_PROFILE(SMBreadX);
3292                                 return;
3293                         }
3294                         /* We currently don't do this on signed or sealed data. */
3295                         if (srv_is_signing_active() || is_encrypted_packet(req->inbuf)) {
3296                                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
3297                                 END_PROFILE(SMBreadX);
3298                                 return;
3299                         }
3300                         /* Is there room in the reply for this data ? */
3301                         if (smb_maxcnt > (0xFFFFFF - (smb_size -4 + 12*2)))  {
3302                                 reply_nterror(req,
3303                                               NT_STATUS_INVALID_PARAMETER);
3304                                 END_PROFILE(SMBreadX);
3305                                 return;
3306                         }
3307                         big_readX = True;
3308                 }
3309         }
3310
3311         if (req->wct == 12) {
3312 #ifdef LARGE_SMB_OFF_T
3313                 /*
3314                  * This is a large offset (64 bit) read.
3315                  */
3316                 startpos |= (((SMB_OFF_T)IVAL(req->inbuf,smb_vwv10)) << 32);
3317
3318 #else /* !LARGE_SMB_OFF_T */
3319
3320                 /*
3321                  * Ensure we haven't been sent a >32 bit offset.
3322                  */
3323
3324                 if(IVAL(req->inbuf,smb_vwv10) != 0) {
3325                         DEBUG(0,("reply_read_and_X - large offset (%x << 32) "
3326                                  "used and we don't support 64 bit offsets.\n",
3327                                  (unsigned int)IVAL(req->inbuf,smb_vwv10) ));
3328                         END_PROFILE(SMBreadX);
3329                         reply_doserror(req, ERRDOS, ERRbadaccess);
3330                         return;
3331                 }
3332
3333 #endif /* LARGE_SMB_OFF_T */
3334
3335         }
3336
3337         if (is_locked(fsp, (uint32)req->smbpid, (SMB_BIG_UINT)smb_maxcnt,
3338                       (SMB_BIG_UINT)startpos, READ_LOCK)) {
3339                 END_PROFILE(SMBreadX);
3340                 reply_doserror(req, ERRDOS, ERRlock);
3341                 return;
3342         }
3343
3344         /* It is possible for VFS modules to selectively decide whether Async I/O should be used
3345            for the file or not.
3346         */
3347         if ((SMB_VFS_AIO_FORCE(fsp)) &&
3348             !big_readX &&
3349             schedule_aio_read_and_X(conn, req, fsp, startpos, smb_maxcnt)) {
3350                 END_PROFILE(SMBreadX);
3351                 return;
3352         }
3353
3354         send_file_readX(conn, req, fsp, startpos, smb_maxcnt);
3355
3356         END_PROFILE(SMBreadX);
3357         return;
3358 }
3359
3360 /****************************************************************************
3361  Error replies to writebraw must have smb_wct == 1. Fix this up.
3362 ****************************************************************************/
3363
3364 void error_to_writebrawerr(struct smb_request *req)
3365 {
3366         uint8 *old_outbuf = req->outbuf;
3367
3368         reply_outbuf(req, 1, 0);
3369
3370         memcpy(req->outbuf, old_outbuf, smb_size);
3371         TALLOC_FREE(old_outbuf);
3372 }
3373
3374 /****************************************************************************
3375  Reply to a writebraw (core+ or LANMAN1.0 protocol).
3376 ****************************************************************************/
3377
3378 void reply_writebraw(struct smb_request *req)
3379 {
3380         connection_struct *conn = req->conn;
3381         int outsize = 0;
3382         char *buf = NULL;
3383         ssize_t nwritten=0;
3384         ssize_t total_written=0;
3385         size_t numtowrite=0;
3386         size_t tcount;
3387         SMB_OFF_T startpos;
3388         char *data=NULL;
3389         bool write_through;
3390         files_struct *fsp;
3391         NTSTATUS status;
3392
3393         START_PROFILE(SMBwritebraw);
3394
3395         /*
3396          * If we ever reply with an error, it must have the SMB command
3397          * type of SMBwritec, not SMBwriteBraw, as this tells the client
3398          * we're finished.
3399          */
3400         SCVAL(req->inbuf,smb_com,SMBwritec);
3401
3402         if (srv_is_signing_active()) {
3403                 END_PROFILE(SMBwritebraw);
3404                 exit_server_cleanly("reply_writebraw: SMB signing is active - "
3405                                 "raw reads/writes are disallowed.");
3406         }
3407
3408         if (req->wct < 12) {
3409                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3410                 error_to_writebrawerr(req);
3411                 END_PROFILE(SMBwritebraw);
3412                 return;
3413         }
3414
3415         fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
3416         if (!check_fsp(conn, req, fsp, &current_user)) {
3417                 error_to_writebrawerr(req);
3418                 END_PROFILE(SMBwritebraw);
3419                 return;
3420         }
3421
3422         if (!CHECK_WRITE(fsp)) {
3423                 reply_doserror(req, ERRDOS, ERRbadaccess);
3424                 error_to_writebrawerr(req);
3425                 END_PROFILE(SMBwritebraw);
3426                 return;
3427         }
3428
3429         tcount = IVAL(req->inbuf,smb_vwv1);
3430         startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv3);
3431         write_through = BITSETW(req->inbuf+smb_vwv7,0);
3432
3433         /* We have to deal with slightly different formats depending
3434                 on whether we are using the core+ or lanman1.0 protocol */
3435
3436         if(Protocol <= PROTOCOL_COREPLUS) {
3437                 numtowrite = SVAL(smb_buf(req->inbuf),-2);
3438                 data = smb_buf(req->inbuf);
3439         } else {
3440                 numtowrite = SVAL(req->inbuf,smb_vwv10);
3441                 data = smb_base(req->inbuf) + SVAL(req->inbuf, smb_vwv11);
3442         }
3443
3444         /* Ensure we don't write bytes past the end of this packet. */
3445         if (data + numtowrite > smb_base(req->inbuf) + smb_len(req->inbuf)) {
3446                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3447                 error_to_writebrawerr(req);
3448                 END_PROFILE(SMBwritebraw);
3449                 return;
3450         }
3451
3452         if (is_locked(fsp,(uint32)req->smbpid,(SMB_BIG_UINT)tcount,
3453                                 (SMB_BIG_UINT)startpos, WRITE_LOCK)) {
3454                 reply_doserror(req, ERRDOS, ERRlock);
3455                 error_to_writebrawerr(req);
3456                 END_PROFILE(SMBwritebraw);
3457                 return;
3458         }
3459
3460         if (numtowrite>0) {
3461                 nwritten = write_file(req,fsp,data,startpos,numtowrite);
3462         }
3463
3464         DEBUG(3,("reply_writebraw: initial write fnum=%d start=%.0f num=%d "
3465                         "wrote=%d sync=%d\n",
3466                 fsp->fnum, (double)startpos, (int)numtowrite,
3467                 (int)nwritten, (int)write_through));
3468
3469         if (nwritten < (ssize_t)numtowrite)  {
3470                 reply_unixerror(req, ERRHRD, ERRdiskfull);
3471                 error_to_writebrawerr(req);
3472                 END_PROFILE(SMBwritebraw);
3473                 return;
3474         }
3475
3476         total_written = nwritten;
3477
3478         /* Allocate a buffer of 64k + length. */
3479         buf = TALLOC_ARRAY(NULL, char, 65540);
3480         if (!buf) {
3481                 reply_doserror(req, ERRDOS, ERRnomem);
3482                 error_to_writebrawerr(req);
3483                 END_PROFILE(SMBwritebraw);
3484                 return;
3485         }
3486
3487         /* Return a SMBwritebraw message to the redirector to tell
3488          * it to send more bytes */
3489
3490         memcpy(buf, req->inbuf, smb_size);
3491         outsize = srv_set_message(buf,
3492                         Protocol>PROTOCOL_COREPLUS?1:0,0,True);
3493         SCVAL(buf,smb_com,SMBwritebraw);
3494         SSVALS(buf,smb_vwv0,0xFFFF);
3495         show_msg(buf);
3496         if (!srv_send_smb(smbd_server_fd(),
3497                         buf,
3498                         IS_CONN_ENCRYPTED(conn))) {
3499                 exit_server_cleanly("reply_writebraw: srv_send_smb "
3500                         "failed.");
3501         }
3502
3503         /* Now read the raw data into the buffer and write it */
3504         if (read_smb_length(smbd_server_fd(),buf,
3505                         SMB_SECONDARY_WAIT, get_srv_read_error()) == -1) {
3506                 exit_server_cleanly("secondary writebraw failed");
3507         }
3508
3509         /*
3510          * Even though this is not an smb message,
3511          * smb_len returns the generic length of a packet.
3512          */
3513
3514         numtowrite = smb_len(buf);
3515
3516         /* Set up outbuf to return the correct size */
3517         reply_outbuf(req, 1, 0);
3518
3519         if (numtowrite != 0) {
3520
3521                 if (numtowrite > 0xFFFF) {
3522                         DEBUG(0,("reply_writebraw: Oversize secondary write "
3523                                 "raw requested (%u). Terminating\n",
3524                                 (unsigned int)numtowrite ));
3525                         exit_server_cleanly("secondary writebraw failed");
3526                 }
3527
3528                 if (tcount > nwritten+numtowrite) {
3529                         DEBUG(3,("reply_writebraw: Client overestimated the "
3530                                 "write %d %d %d\n",
3531                                 (int)tcount,(int)nwritten,(int)numtowrite));
3532                 }
3533
3534                 if (read_data(smbd_server_fd(), buf+4, numtowrite,get_srv_read_error())
3535                                         != numtowrite ) {
3536                         DEBUG(0,("reply_writebraw: Oversize secondary write "
3537                                 "raw read failed (%s). Terminating\n",
3538                                 strerror(errno) ));
3539                         exit_server_cleanly("secondary writebraw failed");
3540                 }
3541
3542                 nwritten = write_file(req,fsp,buf+4,startpos+nwritten,numtowrite);
3543                 if (nwritten == -1) {
3544                         TALLOC_FREE(buf);
3545                         reply_unixerror(req, ERRHRD, ERRdiskfull);
3546                         error_to_writebrawerr(req);
3547                         END_PROFILE(SMBwritebraw);
3548                         return;
3549                 }
3550
3551                 if (nwritten < (ssize_t)numtowrite) {
3552                         SCVAL(req->outbuf,smb_rcls,ERRHRD);
3553                         SSVAL(req->outbuf,smb_err,ERRdiskfull);
3554                 }
3555
3556                 if (nwritten > 0) {
3557                         total_written += nwritten;
3558                 }
3559         }
3560
3561         TALLOC_FREE(buf);
3562         SSVAL(req->outbuf,smb_vwv0,total_written);
3563
3564         status = sync_file(conn, fsp, write_through);
3565         if (!NT_STATUS_IS_OK(status)) {
3566                 DEBUG(5,("reply_writebraw: sync_file for %s returned %s\n",
3567                         fsp->fsp_name, nt_errstr(status) ));
3568                 reply_nterror(req, status);
3569                 error_to_writebrawerr(req);
3570                 END_PROFILE(SMBwritebraw);
3571                 return;
3572         }
3573
3574         DEBUG(3,("reply_writebraw: secondart write fnum=%d start=%.0f num=%d "
3575                 "wrote=%d\n",
3576                 fsp->fnum, (double)startpos, (int)numtowrite,
3577                 (int)total_written));
3578
3579         /* We won't return a status if write through is not selected - this
3580          * follows what WfWg does */
3581         END_PROFILE(SMBwritebraw);
3582
3583         if (!write_through && total_written==tcount) {
3584
3585 #if RABBIT_PELLET_FIX
3586                 /*
3587                  * Fix for "rabbit pellet" mode, trigger an early TCP ack by
3588                  * sending a SMBkeepalive. Thanks to DaveCB at Sun for this.
3589                  * JRA.
3590                  */
3591                 if (!send_keepalive(smbd_server_fd())) {
3592                         exit_server_cleanly("reply_writebraw: send of "
3593                                 "keepalive failed");
3594                 }
3595 #endif
3596                 TALLOC_FREE(req->outbuf);
3597         }
3598         return;
3599 }
3600
3601 #undef DBGC_CLASS
3602 #define DBGC_CLASS DBGC_LOCKING
3603
3604 /****************************************************************************
3605  Reply to a writeunlock (core+).
3606 ****************************************************************************/
3607
3608 void reply_writeunlock(struct smb_request *req)
3609 {
3610         connection_struct *conn = req->conn;
3611         ssize_t nwritten = -1;
3612         size_t numtowrite;
3613         SMB_OFF_T startpos;
3614         char *data;
3615         NTSTATUS status = NT_STATUS_OK;
3616         files_struct *fsp;
3617
3618         START_PROFILE(SMBwriteunlock);
3619
3620         if (req->wct < 5) {
3621                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3622                 END_PROFILE(SMBwriteunlock);
3623                 return;
3624         }
3625         
3626         fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
3627
3628         if (!check_fsp(conn, req, fsp, &current_user)) {
3629                 END_PROFILE(SMBwriteunlock);
3630                 return;
3631         }
3632
3633         if (!CHECK_WRITE(fsp)) {
3634                 reply_doserror(req, ERRDOS,ERRbadaccess);
3635                 END_PROFILE(SMBwriteunlock);
3636                 return;
3637         }
3638
3639         numtowrite = SVAL(req->inbuf,smb_vwv1);
3640         startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv2);
3641         data = smb_buf(req->inbuf) + 3;
3642   
3643         if (numtowrite
3644             && is_locked(fsp, (uint32)req->smbpid, (SMB_BIG_UINT)numtowrite,
3645                          (SMB_BIG_UINT)startpos, WRITE_LOCK)) {
3646                 reply_doserror(req, ERRDOS, ERRlock);
3647                 END_PROFILE(SMBwriteunlock);
3648                 return;
3649         }
3650
3651         /* The special X/Open SMB protocol handling of
3652            zero length writes is *NOT* done for
3653            this call */
3654         if(numtowrite == 0) {
3655                 nwritten = 0;
3656         } else {
3657                 nwritten = write_file(req,fsp,data,startpos,numtowrite);
3658         }
3659   
3660         status = sync_file(conn, fsp, False /* write through */);
3661         if (!NT_STATUS_IS_OK(status)) {
3662                 DEBUG(5,("reply_writeunlock: sync_file for %s returned %s\n",
3663                         fsp->fsp_name, nt_errstr(status) ));
3664                 reply_nterror(req, status);
3665                 END_PROFILE(SMBwriteunlock);
3666                 return;
3667         }
3668
3669         if(((nwritten < numtowrite) && (numtowrite != 0))||(nwritten < 0)) {
3670                 reply_unixerror(req, ERRHRD, ERRdiskfull);
3671                 END_PROFILE(SMBwriteunlock);
3672                 return;
3673         }
3674
3675         if (numtowrite) {
3676                 status = do_unlock(smbd_messaging_context(),
3677                                 fsp,
3678                                 req->smbpid,
3679                                 (SMB_BIG_UINT)numtowrite, 
3680                                 (SMB_BIG_UINT)startpos,
3681                                 WINDOWS_LOCK);
3682
3683                 if (NT_STATUS_V(status)) {
3684                         reply_nterror(req, status);
3685                         END_PROFILE(SMBwriteunlock);
3686                         return;
3687                 }
3688         }
3689
3690         reply_outbuf(req, 1, 0);
3691         
3692         SSVAL(req->outbuf,smb_vwv0,nwritten);
3693         
3694         DEBUG(3,("writeunlock fnum=%d num=%d wrote=%d\n",
3695                  fsp->fnum, (int)numtowrite, (int)nwritten));
3696         
3697         END_PROFILE(SMBwriteunlock);
3698         return;
3699 }
3700
3701 #undef DBGC_CLASS
3702 #define DBGC_CLASS DBGC_ALL
3703
3704 /****************************************************************************
3705  Reply to a write.
3706 ****************************************************************************/
3707
3708 void reply_write(struct smb_request *req)
3709 {
3710         connection_struct *conn = req->conn;
3711         size_t numtowrite;
3712         ssize_t nwritten = -1;
3713         SMB_OFF_T startpos;
3714         char *data;
3715         files_struct *fsp;
3716         NTSTATUS status;
3717
3718         START_PROFILE(SMBwrite);
3719
3720         if (req->wct < 5) {
3721                 END_PROFILE(SMBwrite);
3722                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3723                 return;
3724         }
3725
3726         /* If it's an IPC, pass off the pipe handler. */
3727         if (IS_IPC(conn)) {
3728                 reply_pipe_write(req);
3729                 END_PROFILE(SMBwrite);
3730                 return;
3731         }
3732
3733         fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
3734
3735         if (!check_fsp(conn, req, fsp, &current_user)) {
3736                 END_PROFILE(SMBwrite);
3737                 return;
3738         }
3739
3740         if (!CHECK_WRITE(fsp)) {
3741                 reply_doserror(req, ERRDOS, ERRbadaccess);
3742                 END_PROFILE(SMBwrite);
3743                 return;
3744         }
3745
3746         numtowrite = SVAL(req->inbuf,smb_vwv1);
3747         startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv2);
3748         data = smb_buf(req->inbuf) + 3;
3749   
3750         if (is_locked(fsp, (uint32)req->smbpid, (SMB_BIG_UINT)numtowrite,
3751                       (SMB_BIG_UINT)startpos, WRITE_LOCK)) {
3752                 reply_doserror(req, ERRDOS, ERRlock);
3753                 END_PROFILE(SMBwrite);
3754                 return;
3755         }
3756
3757         /*
3758          * X/Open SMB protocol says that if smb_vwv1 is
3759          * zero then the file size should be extended or
3760          * truncated to the size given in smb_vwv[2-3].
3761          */
3762
3763         if(numtowrite == 0) {
3764                 /*
3765                  * This is actually an allocate call, and set EOF. JRA.
3766                  */
3767                 nwritten = vfs_allocate_file_space(fsp, (SMB_OFF_T)startpos);
3768                 if (nwritten < 0) {
3769                         reply_nterror(req, NT_STATUS_DISK_FULL);
3770                         END_PROFILE(SMBwrite);
3771                         return;
3772                 }
3773                 nwritten = vfs_set_filelen(fsp, (SMB_OFF_T)startpos);
3774                 if (nwritten < 0) {
3775                         reply_nterror(req, NT_STATUS_DISK_FULL);
3776                         END_PROFILE(SMBwrite);
3777                         return;
3778                 }
3779         } else
3780                 nwritten = write_file(req,fsp,data,startpos,numtowrite);
3781   
3782         status = sync_file(conn, fsp, False);
3783         if (!NT_STATUS_IS_OK(status)) {
3784                 DEBUG(5,("reply_write: sync_file for %s returned %s\n",
3785                         fsp->fsp_name, nt_errstr(status) ));
3786                 reply_nterror(req, status);
3787                 END_PROFILE(SMBwrite);
3788                 return;
3789         }
3790
3791         if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
3792                 reply_unixerror(req, ERRHRD, ERRdiskfull);
3793                 END_PROFILE(SMBwrite);
3794                 return;
3795         }
3796
3797         reply_outbuf(req, 1, 0);
3798   
3799         SSVAL(req->outbuf,smb_vwv0,nwritten);
3800
3801         if (nwritten < (ssize_t)numtowrite) {
3802                 SCVAL(req->outbuf,smb_rcls,ERRHRD);
3803                 SSVAL(req->outbuf,smb_err,ERRdiskfull);
3804         }
3805   
3806         DEBUG(3,("write fnum=%d num=%d wrote=%d\n", fsp->fnum, (int)numtowrite, (int)nwritten));
3807
3808         END_PROFILE(SMBwrite);
3809         return;
3810 }
3811
3812 /****************************************************************************
3813  Ensure a buffer is a valid writeX for recvfile purposes.
3814 ****************************************************************************/
3815
3816 #define STANDARD_WRITE_AND_X_HEADER_SIZE (smb_size - 4 + /* basic header */ \
3817                                                 (2*14) + /* word count (including bcc) */ \
3818                                                 1 /* pad byte */)
3819
3820 bool is_valid_writeX_buffer(const uint8_t *inbuf)
3821 {
3822         size_t numtowrite;
3823         connection_struct *conn = NULL;
3824         unsigned int doff = 0;
3825         size_t len = smb_len_large(inbuf);
3826
3827         if (is_encrypted_packet(inbuf)) {
3828                 /* Can't do this on encrypted
3829                  * connections. */
3830                 return false;
3831         }
3832
3833         if (CVAL(inbuf,smb_com) != SMBwriteX) {
3834                 return false;
3835         }
3836
3837         if (CVAL(inbuf,smb_vwv0) != 0xFF ||
3838                         CVAL(inbuf,smb_wct) != 14) {
3839                 DEBUG(10,("is_valid_writeX_buffer: chained or "
3840                         "invalid word length.\n"));
3841                 return false;
3842         }
3843
3844         conn = conn_find(SVAL(inbuf, smb_tid));
3845         if (conn == NULL) {
3846                 DEBUG(10,("is_valid_writeX_buffer: bad tid\n"));
3847                 return false;
3848         }
3849         if (IS_IPC(conn)) {
3850                 DEBUG(10,("is_valid_writeX_buffer: IPC$ tid\n"));
3851                 return false;
3852         }
3853         doff = SVAL(inbuf,smb_vwv11);
3854
3855         numtowrite = SVAL(inbuf,smb_vwv10);
3856
3857         if (len > doff && len - doff > 0xFFFF) {
3858                 numtowrite |= (((size_t)SVAL(inbuf,smb_vwv9))<<16);
3859         }
3860
3861         if (numtowrite == 0) {
3862                 DEBUG(10,("is_valid_writeX_buffer: zero write\n"));
3863                 return false;
3864         }
3865
3866         /* Ensure the sizes match up. */
3867         if (doff < STANDARD_WRITE_AND_X_HEADER_SIZE) {
3868                 /* no pad byte...old smbclient :-( */
3869                 DEBUG(10,("is_valid_writeX_buffer: small doff %u (min %u)\n",
3870                         (unsigned int)doff,
3871                         (unsigned int)STANDARD_WRITE_AND_X_HEADER_SIZE));
3872                 return false;
3873         }
3874
3875         if (len - doff != numtowrite) {
3876                 DEBUG(10,("is_valid_writeX_buffer: doff mismatch "
3877                         "len = %u, doff = %u, numtowrite = %u\n",
3878                         (unsigned int)len,
3879                         (unsigned int)doff,
3880                         (unsigned int)numtowrite ));
3881                 return false;
3882         }
3883
3884         DEBUG(10,("is_valid_writeX_buffer: true "
3885                 "len = %u, doff = %u, numtowrite = %u\n",
3886                 (unsigned int)len,
3887                 (unsigned int)doff,
3888                 (unsigned int)numtowrite ));
3889
3890         return true;
3891 }
3892
3893 /****************************************************************************
3894  Reply to a write and X.
3895 ****************************************************************************/
3896
3897 void reply_write_and_X(struct smb_request *req)
3898 {
3899         connection_struct *conn = req->conn;
3900         files_struct *fsp;
3901         SMB_OFF_T startpos;
3902         size_t numtowrite;
3903         bool write_through;
3904         ssize_t nwritten;
3905         unsigned int smb_doff;
3906         unsigned int smblen;
3907         char *data;
3908         NTSTATUS status;
3909
3910         START_PROFILE(SMBwriteX);
3911
3912         if ((req->wct != 12) && (req->wct != 14)) {
3913                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3914                 END_PROFILE(SMBwriteX);
3915                 return;
3916         }
3917
3918         numtowrite = SVAL(req->inbuf,smb_vwv10);
3919         smb_doff = SVAL(req->inbuf,smb_vwv11);
3920         smblen = smb_len(req->inbuf);
3921
3922         if (req->unread_bytes > 0xFFFF ||
3923                         (smblen > smb_doff &&
3924                                 smblen - smb_doff > 0xFFFF)) {
3925                 numtowrite |= (((size_t)SVAL(req->inbuf,smb_vwv9))<<16);
3926         }
3927
3928         if (req->unread_bytes) {
3929                 /* Can't do a recvfile write on IPC$ */
3930                 if (IS_IPC(conn)) {
3931                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3932                         END_PROFILE(SMBwriteX);
3933                         return;
3934                 }
3935                 if (numtowrite != req->unread_bytes) {
3936                         reply_doserror(req, ERRDOS, ERRbadmem);
3937                         END_PROFILE(SMBwriteX);
3938                         return;
3939                 }
3940         } else {
3941                 if (smb_doff > smblen || smb_doff + numtowrite < numtowrite ||
3942                                 smb_doff + numtowrite > smblen) {
3943                         reply_doserror(req, ERRDOS, ERRbadmem);
3944                         END_PROFILE(SMBwriteX);
3945                         return;
3946                 }
3947         }
3948
3949         /* If it's an IPC, pass off the pipe handler. */
3950         if (IS_IPC(conn)) {
3951                 if (req->unread_bytes) {
3952                         reply_doserror(req, ERRDOS, ERRbadmem);
3953                         END_PROFILE(SMBwriteX);
3954                         return;
3955                 }
3956                 reply_pipe_write_and_X(req);
3957                 END_PROFILE(SMBwriteX);
3958                 return;
3959         }
3960
3961         fsp = file_fsp(SVAL(req->inbuf,smb_vwv2));
3962         startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv3);
3963         write_through = BITSETW(req->inbuf+smb_vwv7,0);
3964
3965         if (!check_fsp(conn, req, fsp, &current_user)) {
3966                 END_PROFILE(SMBwriteX);
3967                 return;
3968         }
3969
3970         if (!CHECK_WRITE(fsp)) {
3971                 reply_doserror(req, ERRDOS, ERRbadaccess);
3972                 END_PROFILE(SMBwriteX);
3973                 return;
3974         }
3975
3976         data = smb_base(req->inbuf) + smb_doff;
3977
3978         if(req->wct == 14) {
3979 #ifdef LARGE_SMB_OFF_T
3980                 /*
3981                  * This is a large offset (64 bit) write.
3982                  */
3983                 startpos |= (((SMB_OFF_T)IVAL(req->inbuf,smb_vwv12)) << 32);
3984
3985 #else /* !LARGE_SMB_OFF_T */
3986
3987                 /*
3988                  * Ensure we haven't been sent a >32 bit offset.
3989                  */
3990
3991                 if(IVAL(req->inbuf,smb_vwv12) != 0) {
3992                         DEBUG(0,("reply_write_and_X - large offset (%x << 32) "
3993                                  "used and we don't support 64 bit offsets.\n",
3994                                  (unsigned int)IVAL(req->inbuf,smb_vwv12) ));
3995                         reply_doserror(req, ERRDOS, ERRbadaccess);
3996                         END_PROFILE(SMBwriteX);
3997                         return;
3998                 }
3999
4000 #endif /* LARGE_SMB_OFF_T */
4001         }
4002
4003         if (is_locked(fsp,(uint32)req->smbpid,
4004                       (SMB_BIG_UINT)numtowrite,
4005                       (SMB_BIG_UINT)startpos, WRITE_LOCK)) {
4006                 reply_doserror(req, ERRDOS, ERRlock);
4007                 END_PROFILE(SMBwriteX);
4008                 return;
4009         }
4010
4011         /* X/Open SMB protocol says that, unlike SMBwrite
4012         if the length is zero then NO truncation is
4013         done, just a write of zero. To truncate a file,
4014         use SMBwrite. */
4015
4016         if(numtowrite == 0) {
4017                 nwritten = 0;
4018         } else {
4019
4020                 /* It is possible for VFS modules to selectively decide whether Async I/O
4021                    should be used for the file or not.
4022                 */
4023                 if ((SMB_VFS_AIO_FORCE(fsp)) && (req->unread_bytes == 0) &&
4024                     schedule_aio_write_and_X(conn, req, fsp, data, startpos,
4025                                              numtowrite)) {
4026                         END_PROFILE(SMBwriteX);
4027                         return;
4028                 }
4029                 
4030                 nwritten = write_file(req,fsp,data,startpos,numtowrite);
4031         }
4032
4033         if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
4034                 reply_unixerror(req, ERRHRD, ERRdiskfull);
4035                 END_PROFILE(SMBwriteX);
4036                 return;
4037         }
4038
4039         reply_outbuf(req, 6, 0);
4040         SSVAL(req->outbuf,smb_vwv2,nwritten);
4041         SSVAL(req->outbuf,smb_vwv4,nwritten>>16);
4042
4043         if (nwritten < (ssize_t)numtowrite) {
4044                 SCVAL(req->outbuf,smb_rcls,ERRHRD);
4045                 SSVAL(req->outbuf,smb_err,ERRdiskfull);
4046         }
4047
4048         DEBUG(3,("writeX fnum=%d num=%d wrote=%d\n",
4049                 fsp->fnum, (int)numtowrite, (int)nwritten));
4050
4051         status = sync_file(conn, fsp, write_through);
4052         if (!NT_STATUS_IS_OK(status)) {
4053                 DEBUG(5,("reply_write_and_X: sync_file for %s returned %s\n",
4054                         fsp->fsp_name, nt_errstr(status) ));
4055                 reply_nterror(req, status);
4056                 END_PROFILE(SMBwriteX);
4057                 return;
4058         }
4059
4060         END_PROFILE(SMBwriteX);
4061         chain_reply(req);
4062         return;
4063 }
4064
4065 /****************************************************************************
4066  Reply to a lseek.
4067 ****************************************************************************/
4068
4069 void reply_lseek(struct smb_request *req)
4070 {
4071         connection_struct *conn = req->conn;
4072         SMB_OFF_T startpos;
4073         SMB_OFF_T res= -1;
4074         int mode,umode;
4075         files_struct *fsp;
4076
4077         START_PROFILE(SMBlseek);
4078
4079         if (req->wct < 4) {
4080                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4081                 END_PROFILE(SMBlseek);
4082                 return;
4083         }
4084
4085         fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
4086
4087         if (!check_fsp(conn, req, fsp, &current_user)) {
4088                 return;
4089         }
4090
4091         flush_write_cache(fsp, SEEK_FLUSH);
4092
4093         mode = SVAL(req->inbuf,smb_vwv1) & 3;
4094         /* NB. This doesn't use IVAL_TO_SMB_OFF_T as startpos can be signed in this case. */
4095         startpos = (SMB_OFF_T)IVALS(req->inbuf,smb_vwv2);
4096
4097         switch (mode) {
4098                 case 0:
4099                         umode = SEEK_SET;
4100                         res = startpos;
4101                         break;
4102                 case 1:
4103                         umode = SEEK_CUR;
4104                         res = fsp->fh->pos + startpos;
4105                         break;
4106                 case 2:
4107                         umode = SEEK_END;
4108                         break;
4109                 default:
4110                         umode = SEEK_SET;
4111                         res = startpos;
4112                         break;
4113         }
4114
4115         if (umode == SEEK_END) {
4116                 if((res = SMB_VFS_LSEEK(fsp,startpos,umode)) == -1) {
4117                         if(errno == EINVAL) {
4118                                 SMB_OFF_T current_pos = startpos;
4119                                 SMB_STRUCT_STAT sbuf;
4120
4121                                 if(SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
4122                                         reply_unixerror(req, ERRDOS,
4123                                                         ERRnoaccess);
4124                                         END_PROFILE(SMBlseek);
4125                                         return;
4126                                 }
4127
4128                                 current_pos += sbuf.st_size;
4129                                 if(current_pos < 0)
4130                                         res = SMB_VFS_LSEEK(fsp,0,SEEK_SET);
4131                         }
4132                 }
4133
4134                 if(res == -1) {
4135                         reply_unixerror(req, ERRDOS, ERRnoaccess);
4136                         END_PROFILE(SMBlseek);
4137                         return;
4138                 }
4139         }
4140
4141         fsp->fh->pos = res;
4142
4143         reply_outbuf(req, 2, 0);
4144         SIVAL(req->outbuf,smb_vwv0,res);
4145   
4146         DEBUG(3,("lseek fnum=%d ofs=%.0f newpos = %.0f mode=%d\n",
4147                 fsp->fnum, (double)startpos, (double)res, mode));
4148
4149         END_PROFILE(SMBlseek);
4150         return;
4151 }
4152
4153 /****************************************************************************
4154  Reply to a flush.
4155 ****************************************************************************/
4156
4157 void reply_flush(struct smb_request *req)
4158 {
4159         connection_struct *conn = req->conn;
4160         uint16 fnum;
4161         files_struct *fsp;
4162
4163         START_PROFILE(SMBflush);
4164
4165         if (req->wct < 1) {
4166                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4167                 return;
4168         }
4169
4170         fnum = SVAL(req->inbuf,smb_vwv0);
4171         fsp = file_fsp(fnum);
4172
4173         if ((fnum != 0xFFFF) && !check_fsp(conn, req, fsp, &current_user)) {
4174                 return;
4175         }
4176         
4177         if (!fsp) {
4178                 file_sync_all(conn);
4179         } else {
4180                 NTSTATUS status = sync_file(conn, fsp, True);
4181                 if (!NT_STATUS_IS_OK(status)) {
4182                         DEBUG(5,("reply_flush: sync_file for %s returned %s\n",
4183                                 fsp->fsp_name, nt_errstr(status) ));
4184                         reply_nterror(req, status);
4185                         END_PROFILE(SMBflush);
4186                         return;
4187                 }
4188         }
4189         
4190         reply_outbuf(req, 0, 0);
4191
4192         DEBUG(3,("flush\n"));
4193         END_PROFILE(SMBflush);
4194         return;
4195 }
4196
4197 /****************************************************************************
4198  Reply to a exit.
4199  conn POINTER CAN BE NULL HERE !
4200 ****************************************************************************/
4201
4202 void reply_exit(struct smb_request *req)
4203 {
4204         START_PROFILE(SMBexit);
4205
4206         file_close_pid(req->smbpid, req->vuid);
4207
4208         reply_outbuf(req, 0, 0);
4209
4210         DEBUG(3,("exit\n"));
4211
4212         END_PROFILE(SMBexit);
4213         return;
4214 }
4215
4216 /****************************************************************************
4217  Reply to a close - has to deal with closing a directory opened by NT SMB's.
4218 ****************************************************************************/
4219
4220 void reply_close(struct smb_request *req)
4221 {
4222         connection_struct *conn = req->conn;
4223         NTSTATUS status = NT_STATUS_OK;
4224         files_struct *fsp = NULL;
4225         START_PROFILE(SMBclose);
4226
4227         if (req->wct < 3) {
4228                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4229                 END_PROFILE(SMBclose);
4230                 return;
4231         }
4232
4233         /* If it's an IPC, pass off to the pipe handler. */
4234         if (IS_IPC(conn)) {
4235                 reply_pipe_close(conn, req);
4236                 END_PROFILE(SMBclose);
4237                 return;
4238         }
4239
4240         fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
4241
4242         /*
4243          * We can only use CHECK_FSP if we know it's not a directory.
4244          */
4245
4246         if(!fsp || (fsp->conn != conn) || (fsp->vuid != current_user.vuid)) {
4247                 reply_doserror(req, ERRDOS, ERRbadfid);
4248                 END_PROFILE(SMBclose);
4249                 return;
4250         }
4251
4252         if(fsp->is_directory) {
4253                 /*
4254                  * Special case - close NT SMB directory handle.
4255                  */
4256                 DEBUG(3,("close directory fnum=%d\n", fsp->fnum));
4257                 status = close_file(fsp,NORMAL_CLOSE);
4258         } else {
4259                 /*
4260                  * Close ordinary file.
4261                  */
4262
4263                 DEBUG(3,("close fd=%d fnum=%d (numopen=%d)\n",
4264                          fsp->fh->fd, fsp->fnum,
4265                          conn->num_files_open));
4266  
4267                 /*
4268                  * Take care of any time sent in the close.
4269                  */
4270
4271                 fsp_set_pending_modtime(fsp, convert_time_t_to_timespec(
4272                                                 srv_make_unix_date3(
4273                                                         req->inbuf+smb_vwv1)));
4274
4275                 /*
4276                  * close_file() returns the unix errno if an error
4277                  * was detected on close - normally this is due to
4278                  * a disk full error. If not then it was probably an I/O error.
4279                  */
4280  
4281                 status = close_file(fsp,NORMAL_CLOSE);
4282         }  
4283
4284         if (!NT_STATUS_IS_OK(status)) {
4285                 reply_nterror(req, status);
4286                 END_PROFILE(SMBclose);
4287                 return;
4288         }
4289
4290         reply_outbuf(req, 0, 0);
4291         END_PROFILE(SMBclose);
4292         return;
4293 }
4294
4295 /****************************************************************************
4296  Reply to a writeclose (Core+ protocol).
4297 ****************************************************************************/
4298
4299 void reply_writeclose(struct smb_request *req)
4300 {
4301         connection_struct *conn = req->conn;
4302         size_t numtowrite;
4303         ssize_t nwritten = -1;
4304         NTSTATUS close_status = NT_STATUS_OK;
4305         SMB_OFF_T startpos;
4306         char *data;
4307         struct timespec mtime;
4308         files_struct *fsp;
4309
4310         START_PROFILE(SMBwriteclose);
4311
4312         if (req->wct < 6) {
4313                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4314                 END_PROFILE(SMBwriteclose);
4315                 return;
4316         }
4317
4318         fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
4319
4320         if (!check_fsp(conn, req, fsp, &current_user)) {
4321                 END_PROFILE(SMBwriteclose);
4322                 return;
4323         }
4324         if (!CHECK_WRITE(fsp)) {
4325                 reply_doserror(req, ERRDOS,ERRbadaccess);
4326                 END_PROFILE(SMBwriteclose);
4327                 return;
4328         }
4329
4330         numtowrite = SVAL(req->inbuf,smb_vwv1);
4331         startpos = IVAL_TO_SMB_OFF_T(req->inbuf,smb_vwv2);
4332         mtime = convert_time_t_to_timespec(srv_make_unix_date3(
4333                                                    req->inbuf+smb_vwv4));
4334         data = smb_buf(req->inbuf) + 1;
4335   
4336         if (numtowrite
4337             && is_locked(fsp, (uint32)req->smbpid, (SMB_BIG_UINT)numtowrite,
4338                          (SMB_BIG_UINT)startpos, WRITE_LOCK)) {
4339                 reply_doserror(req, ERRDOS,ERRlock);
4340                 END_PROFILE(SMBwriteclose);
4341                 return;
4342         }
4343   
4344         nwritten = write_file(req,fsp,data,startpos,numtowrite);
4345
4346         set_filetime(conn, fsp->fsp_name, mtime);
4347   
4348         /*
4349          * More insanity. W2K only closes the file if writelen > 0.
4350          * JRA.
4351          */
4352
4353         if (numtowrite) {
4354                 DEBUG(3,("reply_writeclose: zero length write doesn't close file %s\n",
4355                         fsp->fsp_name ));
4356                 close_status = close_file(fsp,NORMAL_CLOSE);
4357         }
4358
4359         DEBUG(3,("writeclose fnum=%d num=%d wrote=%d (numopen=%d)\n",
4360                  fsp->fnum, (int)numtowrite, (int)nwritten,
4361                  conn->num_files_open));
4362   
4363         if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
4364                 reply_doserror(req, ERRHRD, ERRdiskfull);
4365                 END_PROFILE(SMBwriteclose);
4366                 return;
4367         }
4368  
4369         if(!NT_STATUS_IS_OK(close_status)) {
4370                 reply_nterror(req, close_status);
4371                 END_PROFILE(SMBwriteclose);
4372                 return;
4373         }
4374
4375         reply_outbuf(req, 1, 0);
4376   
4377         SSVAL(req->outbuf,smb_vwv0,nwritten);
4378         END_PROFILE(SMBwriteclose);
4379         return;
4380 }
4381
4382 #undef DBGC_CLASS
4383 #define DBGC_CLASS DBGC_LOCKING
4384
4385 /****************************************************************************
4386  Reply to a lock.
4387 ****************************************************************************/
4388
4389 void reply_lock(struct smb_request *req)
4390 {
4391         connection_struct *conn = req->conn;
4392         SMB_BIG_UINT count,offset;
4393         NTSTATUS status;
4394         files_struct *fsp;
4395         struct byte_range_lock *br_lck = NULL;
4396
4397         START_PROFILE(SMBlock);
4398
4399         if (req->wct < 5) {
4400                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4401                 END_PROFILE(SMBlock);
4402                 return;
4403         }
4404
4405         fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
4406
4407         if (!check_fsp(conn, req, fsp, &current_user)) {
4408                 END_PROFILE(SMBlock);
4409                 return;
4410         }
4411
4412         release_level_2_oplocks_on_change(fsp);
4413
4414         count = (SMB_BIG_UINT)IVAL(req->inbuf,smb_vwv1);
4415         offset = (SMB_BIG_UINT)IVAL(req->inbuf,smb_vwv3);
4416
4417         DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
4418                  fsp->fh->fd, fsp->fnum, (double)offset, (double)count));
4419
4420         br_lck = do_lock(smbd_messaging_context(),
4421                         fsp,
4422                         req->smbpid,
4423                         count,
4424                         offset,
4425                         WRITE_LOCK,
4426                         WINDOWS_LOCK,
4427                         False, /* Non-blocking lock. */
4428                         &status,
4429                         NULL);
4430
4431         TALLOC_FREE(br_lck);
4432
4433         if (NT_STATUS_V(status)) {
4434                 reply_nterror(req, status);
4435                 END_PROFILE(SMBlock);
4436                 return;
4437         }
4438
4439         reply_outbuf(req, 0, 0);
4440
4441         END_PROFILE(SMBlock);
4442         return;
4443 }
4444
4445 /****************************************************************************
4446  Reply to a unlock.
4447 ****************************************************************************/
4448
4449 void reply_unlock(struct smb_request *req)
4450 {
4451         connection_struct *conn = req->conn;
4452         SMB_BIG_UINT count,offset;
4453         NTSTATUS status;
4454         files_struct *fsp;
4455
4456         START_PROFILE(SMBunlock);
4457
4458         if (req->wct < 5) {
4459                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4460                 END_PROFILE(SMBunlock);
4461                 return;
4462         }
4463
4464         fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
4465
4466         if (!check_fsp(conn, req, fsp, &current_user)) {
4467                 END_PROFILE(SMBunlock);
4468                 return;
4469         }
4470         
4471         count = (SMB_BIG_UINT)IVAL(req->inbuf,smb_vwv1);
4472         offset = (SMB_BIG_UINT)IVAL(req->inbuf,smb_vwv3);
4473         
4474         status = do_unlock(smbd_messaging_context(),
4475                         fsp,
4476                         req->smbpid,
4477                         count,
4478                         offset,
4479                         WINDOWS_LOCK);
4480
4481         if (NT_STATUS_V(status)) {
4482                 reply_nterror(req, status);
4483                 END_PROFILE(SMBunlock);
4484                 return;
4485         }
4486
4487         DEBUG( 3, ( "unlock fd=%d fnum=%d offset=%.0f count=%.0f\n",
4488                     fsp->fh->fd, fsp->fnum, (double)offset, (double)count ) );
4489
4490         reply_outbuf(req, 0, 0);
4491
4492         END_PROFILE(SMBunlock);
4493         return;
4494 }
4495
4496 #undef DBGC_CLASS
4497 #define DBGC_CLASS DBGC_ALL
4498
4499 /****************************************************************************
4500  Reply to a tdis.
4501  conn POINTER CAN BE NULL HERE !
4502 ****************************************************************************/
4503
4504 void reply_tdis(struct smb_request *req)
4505 {
4506         connection_struct *conn = req->conn;
4507         START_PROFILE(SMBtdis);
4508
4509         if (!conn) {
4510                 DEBUG(4,("Invalid connection in tdis\n"));
4511                 reply_doserror(req, ERRSRV, ERRinvnid);
4512                 END_PROFILE(SMBtdis);
4513                 return;
4514         }
4515
4516         conn->used = False;
4517
4518         close_cnum(conn,req->vuid);
4519         req->conn = NULL;
4520
4521         reply_outbuf(req, 0, 0);
4522         END_PROFILE(SMBtdis);
4523         return;
4524 }
4525
4526 /****************************************************************************
4527  Reply to a echo.
4528  conn POINTER CAN BE NULL HERE !
4529 ****************************************************************************/
4530
4531 void reply_echo(struct smb_request *req)
4532 {
4533         connection_struct *conn = req->conn;
4534         int smb_reverb;
4535         int seq_num;
4536         unsigned int data_len = smb_buflen(req->inbuf);
4537
4538         START_PROFILE(SMBecho);
4539
4540         if (req->wct < 1) {
4541                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4542                 END_PROFILE(SMBecho);
4543                 return;
4544         }
4545
4546         if (data_len > BUFFER_SIZE) {
4547                 DEBUG(0,("reply_echo: data_len too large.\n"));
4548                 reply_nterror(req, NT_STATUS_INSUFFICIENT_RESOURCES);
4549                 END_PROFILE(SMBecho);
4550                 return;
4551         }
4552
4553         smb_reverb = SVAL(req->inbuf,smb_vwv0);
4554
4555         reply_outbuf(req, 1, data_len);
4556
4557         /* copy any incoming data back out */
4558         if (data_len > 0) {
4559                 memcpy(smb_buf(req->outbuf),smb_buf(req->inbuf),data_len);
4560         }
4561
4562         if (smb_reverb > 100) {
4563                 DEBUG(0,("large reverb (%d)?? Setting to 100\n",smb_reverb));
4564                 smb_reverb = 100;
4565         }
4566
4567         for (seq_num =1 ; seq_num <= smb_reverb ; seq_num++) {
4568                 SSVAL(req->outbuf,smb_vwv0,seq_num);
4569
4570                 show_msg((char *)req->outbuf);
4571                 if (!srv_send_smb(smbd_server_fd(),
4572                                 (char *)req->outbuf,
4573                                 IS_CONN_ENCRYPTED(conn)||req->encrypted))
4574                         exit_server_cleanly("reply_echo: srv_send_smb failed.");
4575         }
4576
4577         DEBUG(3,("echo %d times\n", smb_reverb));
4578
4579         TALLOC_FREE(req->outbuf);
4580
4581         smb_echo_count++;
4582
4583         END_PROFILE(SMBecho);
4584         return;
4585 }
4586
4587 /****************************************************************************
4588  Reply to a printopen.
4589 ****************************************************************************/
4590
4591 void reply_printopen(struct smb_request *req)
4592 {
4593         connection_struct *conn = req->conn;
4594         files_struct *fsp;
4595         NTSTATUS status;
4596         
4597         START_PROFILE(SMBsplopen);
4598
4599         if (req->wct < 2) {
4600                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4601                 END_PROFILE(SMBsplopen);
4602                 return;
4603         }
4604
4605         if (!CAN_PRINT(conn)) {
4606                 reply_doserror(req, ERRDOS, ERRnoaccess);
4607                 END_PROFILE(SMBsplopen);
4608                 return;
4609         }
4610
4611         /* Open for exclusive use, write only. */
4612         status = print_fsp_open(conn, NULL, &fsp);
4613
4614         if (!NT_STATUS_IS_OK(status)) {
4615                 reply_nterror(req, status);
4616                 END_PROFILE(SMBsplopen);
4617                 return;
4618         }
4619
4620         reply_outbuf(req, 1, 0);
4621         SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
4622   
4623         DEBUG(3,("openprint fd=%d fnum=%d\n",
4624                  fsp->fh->fd, fsp->fnum));
4625
4626         END_PROFILE(SMBsplopen);
4627         return;
4628 }
4629
4630 /****************************************************************************
4631  Reply to a printclose.
4632 ****************************************************************************/
4633
4634 void reply_printclose(struct smb_request *req)
4635 {
4636         connection_struct *conn = req->conn;
4637         files_struct *fsp;
4638         NTSTATUS status;
4639
4640         START_PROFILE(SMBsplclose);
4641
4642         if (req->wct < 3) {
4643                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4644                 END_PROFILE(SMBsplclose);
4645                 return;
4646         }
4647
4648         fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
4649
4650         if (!check_fsp(conn, req, fsp, &current_user)) {
4651                 END_PROFILE(SMBsplclose);
4652                 return;
4653         }
4654
4655         if (!CAN_PRINT(conn)) {
4656                 reply_nterror(req, NT_STATUS_DOS(ERRSRV, ERRerror));
4657                 END_PROFILE(SMBsplclose);
4658                 return;
4659         }
4660   
4661         DEBUG(3,("printclose fd=%d fnum=%d\n",
4662                  fsp->fh->fd,fsp->fnum));
4663   
4664         status = close_file(fsp,NORMAL_CLOSE);
4665
4666         if(!NT_STATUS_IS_OK(status)) {
4667                 reply_nterror(req, status);
4668                 END_PROFILE(SMBsplclose);
4669                 return;
4670         }
4671
4672         END_PROFILE(SMBsplclose);
4673         return;
4674 }
4675
4676 /****************************************************************************
4677  Reply to a printqueue.
4678 ****************************************************************************/
4679
4680 void reply_printqueue(struct smb_request *req)
4681 {
4682         connection_struct *conn = req->conn;
4683         int max_count;
4684         int start_index;
4685
4686         START_PROFILE(SMBsplretq);
4687
4688         if (req->wct < 2) {
4689                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4690                 END_PROFILE(SMBsplretq);
4691                 return;
4692         }
4693
4694         max_count = SVAL(req->inbuf,smb_vwv0);
4695         start_index = SVAL(req->inbuf,smb_vwv1);
4696
4697         /* we used to allow the client to get the cnum wrong, but that
4698            is really quite gross and only worked when there was only
4699            one printer - I think we should now only accept it if they
4700            get it right (tridge) */
4701         if (!CAN_PRINT(conn)) {
4702                 reply_doserror(req, ERRDOS, ERRnoaccess);
4703                 END_PROFILE(SMBsplretq);
4704                 return;
4705         }
4706
4707         reply_outbuf(req, 2, 3);
4708         SSVAL(req->outbuf,smb_vwv0,0);
4709         SSVAL(req->outbuf,smb_vwv1,0);
4710         SCVAL(smb_buf(req->outbuf),0,1);
4711         SSVAL(smb_buf(req->outbuf),1,0);
4712   
4713         DEBUG(3,("printqueue start_index=%d max_count=%d\n",
4714                  start_index, max_count));
4715
4716         {
4717                 print_queue_struct *queue = NULL;
4718                 print_status_struct status;
4719                 int count = print_queue_status(SNUM(conn), &queue, &status);
4720                 int num_to_get = ABS(max_count);
4721                 int first = (max_count>0?start_index:start_index+max_count+1);
4722                 int i;
4723
4724                 if (first >= count)
4725                         num_to_get = 0;
4726                 else
4727                         num_to_get = MIN(num_to_get,count-first);
4728     
4729
4730                 for (i=first;i<first+num_to_get;i++) {
4731                         char blob[28];
4732                         char *p = blob;
4733
4734                         srv_put_dos_date2(p,0,queue[i].time);
4735                         SCVAL(p,4,(queue[i].status==LPQ_PRINTING?2:3));
4736                         SSVAL(p,5, queue[i].job);
4737                         SIVAL(p,7,queue[i].size);
4738                         SCVAL(p,11,0);
4739                         srvstr_push(blob, req->flags2, p+12,
4740                                     queue[i].fs_user, 16, STR_ASCII);
4741
4742                         if (message_push_blob(
4743                                     &req->outbuf,
4744                                     data_blob_const(
4745                                             blob, sizeof(blob))) == -1) {
4746                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
4747                                 END_PROFILE(SMBsplretq);
4748                                 return;
4749                         }
4750                 }
4751
4752                 if (count > 0) {
4753                         SSVAL(req->outbuf,smb_vwv0,count);
4754                         SSVAL(req->outbuf,smb_vwv1,
4755                               (max_count>0?first+count:first-1));
4756                         SCVAL(smb_buf(req->outbuf),0,1);
4757                         SSVAL(smb_buf(req->outbuf),1,28*count);
4758                 }
4759
4760                 SAFE_FREE(queue);
4761           
4762                 DEBUG(3,("%d entries returned in queue\n",count));
4763         }
4764   
4765         END_PROFILE(SMBsplretq);
4766         return;
4767 }
4768
4769 /****************************************************************************
4770  Reply to a printwrite.
4771 ****************************************************************************/
4772
4773 void reply_printwrite(struct smb_request *req)
4774 {
4775         connection_struct *conn = req->conn;
4776         int numtowrite;
4777         char *data;
4778         files_struct *fsp;
4779
4780         START_PROFILE(SMBsplwr);
4781
4782         if (req->wct < 1) {
4783                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4784                 END_PROFILE(SMBsplwr);
4785                 return;
4786         }
4787   
4788         fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
4789
4790         if (!check_fsp(conn, req, fsp, &current_user)) {
4791                 END_PROFILE(SMBsplwr);
4792                 return;
4793         }
4794
4795         if (!CAN_PRINT(conn)) {
4796                 reply_doserror(req, ERRDOS, ERRnoaccess);
4797                 END_PROFILE(SMBsplwr);
4798                 return;
4799         }
4800
4801         if (!CHECK_WRITE(fsp)) {
4802                 reply_doserror(req, ERRDOS, ERRbadaccess);
4803                 END_PROFILE(SMBsplwr);
4804                 return;
4805         }
4806
4807         numtowrite = SVAL(smb_buf(req->inbuf),1);
4808
4809         if (smb_buflen(req->inbuf) < numtowrite + 3) {
4810                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
4811                 END_PROFILE(SMBsplwr);
4812                 return;
4813         }
4814
4815         data = smb_buf(req->inbuf) + 3;
4816
4817         if (write_file(req,fsp,data,-1,numtowrite) != numtowrite) {
4818                 reply_unixerror(req, ERRHRD, ERRdiskfull);
4819                 END_PROFILE(SMBsplwr);
4820                 return;
4821         }
4822
4823         DEBUG( 3, ( "printwrite fnum=%d num=%d\n", fsp->fnum, numtowrite ) );
4824
4825         END_PROFILE(SMBsplwr);
4826         return;
4827 }
4828
4829 /****************************************************************************
4830  Reply to a mkdir.
4831 ****************************************************************************/
4832
4833 void reply_mkdir(struct smb_request *req)
4834 {
4835         connection_struct *conn = req->conn;
4836         char *directory = NULL;
4837         NTSTATUS status;
4838         SMB_STRUCT_STAT sbuf;
4839         TALLOC_CTX *ctx = talloc_tos();
4840
4841         START_PROFILE(SMBmkdir);
4842
4843         srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &directory,
4844                         smb_buf(req->inbuf) + 1, 0,
4845                         STR_TERMINATE, &status);
4846         if (!NT_STATUS_IS_OK(status)) {
4847                 reply_nterror(req, status);
4848                 END_PROFILE(SMBmkdir);
4849                 return;
4850         }
4851
4852         status = resolve_dfspath(ctx, conn,
4853                                  req->flags2 & FLAGS2_DFS_PATHNAMES,
4854                                  directory,
4855                                  &directory);
4856         if (!NT_STATUS_IS_OK(status)) {
4857                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
4858                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
4859                                         ERRSRV, ERRbadpath);
4860                         END_PROFILE(SMBmkdir);
4861                         return;
4862                 }
4863                 reply_nterror(req, status);
4864                 END_PROFILE(SMBmkdir);
4865                 return;
4866         }
4867
4868         status = unix_convert(ctx, conn, directory, False, &directory, NULL, &sbuf);
4869         if (!NT_STATUS_IS_OK(status)) {
4870                 reply_nterror(req, status);
4871                 END_PROFILE(SMBmkdir);
4872                 return;
4873         }
4874
4875         status = check_name(conn, directory);
4876         if (!NT_STATUS_IS_OK(status)) {
4877                 reply_nterror(req, status);
4878                 END_PROFILE(SMBmkdir);
4879                 return;
4880         }
4881
4882         status = create_directory(conn, req, directory);
4883
4884         DEBUG(5, ("create_directory returned %s\n", nt_errstr(status)));
4885
4886         if (!NT_STATUS_IS_OK(status)) {
4887
4888                 if (!use_nt_status()
4889                     && NT_STATUS_EQUAL(status,
4890                                        NT_STATUS_OBJECT_NAME_COLLISION)) {
4891                         /*
4892                          * Yes, in the DOS error code case we get a
4893                          * ERRDOS:ERRnoaccess here. See BASE-SAMBA3ERROR
4894                          * samba4 torture test.
4895                          */
4896                         status = NT_STATUS_DOS(ERRDOS, ERRnoaccess);
4897                 }
4898
4899                 reply_nterror(req, status);
4900                 END_PROFILE(SMBmkdir);
4901                 return;
4902         }
4903
4904         reply_outbuf(req, 0, 0);
4905
4906         DEBUG( 3, ( "mkdir %s\n", directory ) );
4907
4908         END_PROFILE(SMBmkdir);
4909         return;
4910 }
4911
4912 /****************************************************************************
4913  Static function used by reply_rmdir to delete an entire directory
4914  tree recursively. Return True on ok, False on fail.
4915 ****************************************************************************/
4916
4917 static bool recursive_rmdir(TALLOC_CTX *ctx,
4918                         connection_struct *conn,
4919                         char *directory)
4920 {
4921         const char *dname = NULL;
4922         bool ret = True;
4923         long offset = 0;
4924         struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn, directory,
4925                                           NULL, 0);
4926
4927         if(dir_hnd == NULL)
4928                 return False;
4929
4930         while((dname = ReadDirName(dir_hnd, &offset))) {
4931                 char *fullname = NULL;
4932                 SMB_STRUCT_STAT st;
4933
4934                 if (ISDOT(dname) || ISDOTDOT(dname)) {
4935                         continue;
4936                 }
4937
4938                 if (!is_visible_file(conn, directory, dname, &st, False)) {
4939                         continue;
4940                 }
4941
4942                 /* Construct the full name. */
4943                 fullname = talloc_asprintf(ctx,
4944                                 "%s/%s",
4945                                 directory,
4946                                 dname);
4947                 if (!fullname) {
4948                         errno = ENOMEM;
4949                         ret = False;
4950                         break;
4951                 }
4952
4953                 if(SMB_VFS_LSTAT(conn,fullname, &st) != 0) {
4954                         ret = False;
4955                         break;
4956                 }
4957
4958                 if(st.st_mode & S_IFDIR) {
4959                         if(!recursive_rmdir(ctx, conn, fullname)) {
4960                                 ret = False;
4961                                 break;
4962                         }
4963                         if(SMB_VFS_RMDIR(conn,fullname) != 0) {
4964                                 ret = False;
4965                                 break;
4966                         }
4967                 } else if(SMB_VFS_UNLINK(conn,fullname) != 0) {
4968                         ret = False;
4969                         break;
4970                 }
4971                 TALLOC_FREE(fullname);
4972         }
4973         TALLOC_FREE(dir_hnd);
4974         return ret;
4975 }
4976
4977 /****************************************************************************
4978  The internals of the rmdir code - called elsewhere.
4979 ****************************************************************************/
4980
4981 NTSTATUS rmdir_internals(TALLOC_CTX *ctx,
4982                         connection_struct *conn,
4983                         const char *directory)
4984 {
4985         int ret;
4986         SMB_STRUCT_STAT st;
4987
4988         /* Might be a symlink. */
4989         if(SMB_VFS_LSTAT(conn, directory, &st) != 0) {
4990                 return map_nt_error_from_unix(errno);
4991         }
4992
4993         if (S_ISLNK(st.st_mode)) {
4994                 /* Is what it points to a directory ? */
4995                 if(SMB_VFS_STAT(conn, directory, &st) != 0) {
4996                         return map_nt_error_from_unix(errno);
4997                 }
4998                 if (!(S_ISDIR(st.st_mode))) {
4999                         return NT_STATUS_NOT_A_DIRECTORY;
5000                 }
5001                 ret = SMB_VFS_UNLINK(conn,directory);
5002         } else {
5003                 ret = SMB_VFS_RMDIR(conn,directory);
5004         }
5005         if (ret == 0) {
5006                 notify_fname(conn, NOTIFY_ACTION_REMOVED,
5007                              FILE_NOTIFY_CHANGE_DIR_NAME,
5008                              directory);
5009                 return NT_STATUS_OK;
5010         }
5011
5012         if(((errno == ENOTEMPTY)||(errno == EEXIST)) && lp_veto_files(SNUM(conn))) {
5013                 /*
5014                  * Check to see if the only thing in this directory are
5015                  * vetoed files/directories. If so then delete them and
5016                  * retry. If we fail to delete any of them (and we *don't*
5017                  * do a recursive delete) then fail the rmdir.
5018                  */
5019                 const char *dname;
5020                 long dirpos = 0;
5021                 struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
5022                                                   directory, NULL, 0);
5023
5024                 if(dir_hnd == NULL) {
5025                         errno = ENOTEMPTY;
5026                         goto err;
5027                 }
5028
5029                 while ((dname = ReadDirName(dir_hnd,&dirpos))) {
5030                         if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
5031                                 continue;
5032                         if (!is_visible_file(conn, directory, dname, &st, False))
5033                                 continue;
5034                         if(!IS_VETO_PATH(conn, dname)) {
5035                                 TALLOC_FREE(dir_hnd);
5036                                 errno = ENOTEMPTY;
5037                                 goto err;
5038                         }
5039                 }
5040
5041                 /* We only have veto files/directories. Recursive delete. */
5042
5043                 RewindDir(dir_hnd,&dirpos);
5044                 while ((dname = ReadDirName(dir_hnd,&dirpos))) {
5045                         char *fullname = NULL;
5046
5047                         if (ISDOT(dname) || ISDOTDOT(dname)) {
5048                                 continue;
5049                         }
5050                         if (!is_visible_file(conn, directory, dname, &st, False)) {
5051                                 continue;
5052                         }
5053
5054                         fullname = talloc_asprintf(ctx,
5055                                         "%s/%s",
5056                                         directory,
5057                                         dname);
5058
5059                         if(!fullname) {
5060                                 errno = ENOMEM;
5061                                 break;
5062                         }
5063
5064                         if(SMB_VFS_LSTAT(conn,fullname, &st) != 0) {
5065                                 break;
5066                         }
5067                         if(st.st_mode & S_IFDIR) {
5068                                 if(lp_recursive_veto_delete(SNUM(conn))) {
5069                                         if(!recursive_rmdir(ctx, conn, fullname))
5070                                                 break;
5071                                 }
5072                                 if(SMB_VFS_RMDIR(conn,fullname) != 0) {
5073                                         break;
5074                                 }
5075                         } else if(SMB_VFS_UNLINK(conn,fullname) != 0) {
5076                                 break;
5077                         }
5078                         TALLOC_FREE(fullname);
5079                 }
5080                 TALLOC_FREE(dir_hnd);
5081                 /* Retry the rmdir */
5082                 ret = SMB_VFS_RMDIR(conn,directory);
5083         }
5084
5085   err:
5086
5087         if (ret != 0) {
5088                 DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
5089                          "%s\n", directory,strerror(errno)));
5090                 return map_nt_error_from_unix(errno);
5091         }
5092
5093         notify_fname(conn, NOTIFY_ACTION_REMOVED,
5094                      FILE_NOTIFY_CHANGE_DIR_NAME,
5095                      directory);
5096
5097         return NT_STATUS_OK;
5098 }
5099
5100 /****************************************************************************
5101  Reply to a rmdir.
5102 ****************************************************************************/
5103
5104 void reply_rmdir(struct smb_request *req)
5105 {
5106         connection_struct *conn = req->conn;
5107         char *directory = NULL;
5108         SMB_STRUCT_STAT sbuf;
5109         NTSTATUS status;
5110         TALLOC_CTX *ctx = talloc_tos();
5111
5112         START_PROFILE(SMBrmdir);
5113
5114         srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &directory,
5115                         smb_buf(req->inbuf) + 1, 0,
5116                         STR_TERMINATE, &status);
5117         if (!NT_STATUS_IS_OK(status)) {
5118                 reply_nterror(req, status);
5119                 END_PROFILE(SMBrmdir);
5120                 return;
5121         }
5122
5123         status = resolve_dfspath(ctx, conn,
5124                                  req->flags2 & FLAGS2_DFS_PATHNAMES,
5125                                  directory,
5126                                  &directory);
5127         if (!NT_STATUS_IS_OK(status)) {
5128                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
5129                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
5130                                         ERRSRV, ERRbadpath);
5131                         END_PROFILE(SMBrmdir);
5132                         return;
5133                 }
5134                 reply_nterror(req, status);
5135                 END_PROFILE(SMBrmdir);
5136                 return;
5137         }
5138
5139         status = unix_convert(ctx, conn, directory, False, &directory,
5140                         NULL, &sbuf);
5141         if (!NT_STATUS_IS_OK(status)) {
5142                 reply_nterror(req, status);
5143                 END_PROFILE(SMBrmdir);
5144                 return;
5145         }
5146
5147         status = check_name(conn, directory);
5148         if (!NT_STATUS_IS_OK(status)) {
5149                 reply_nterror(req, status);
5150                 END_PROFILE(SMBrmdir);
5151                 return;
5152         }
5153
5154         dptr_closepath(directory, req->smbpid);
5155         status = rmdir_internals(ctx, conn, directory);
5156         if (!NT_STATUS_IS_OK(status)) {
5157                 reply_nterror(req, status);
5158                 END_PROFILE(SMBrmdir);
5159                 return;
5160         }
5161
5162         reply_outbuf(req, 0, 0);
5163
5164         DEBUG( 3, ( "rmdir %s\n", directory ) );
5165
5166         END_PROFILE(SMBrmdir);
5167         return;
5168 }
5169
5170 /*******************************************************************
5171  Resolve wildcards in a filename rename.
5172 ********************************************************************/
5173
5174 static bool resolve_wildcards(TALLOC_CTX *ctx,
5175                                 const char *name1,
5176                                 const char *name2,
5177                                 char **pp_newname)
5178 {
5179         char *name2_copy = NULL;
5180         char *root1 = NULL;
5181         char *root2 = NULL;
5182         char *ext1 = NULL;
5183         char *ext2 = NULL;
5184         char *p,*p2, *pname1, *pname2;
5185         
5186         name2_copy = talloc_strdup(ctx, name2);
5187         if (!name2_copy) {
5188                 return False;
5189         }
5190
5191         pname1 = strrchr_m(name1,'/');
5192         pname2 = strrchr_m(name2_copy,'/');
5193
5194         if (!pname1 || !pname2) {
5195                 return False;
5196         }
5197   
5198         /* Truncate the copy of name2 at the last '/' */
5199         *pname2 = '\0';
5200
5201         /* Now go past the '/' */
5202         pname1++;
5203         pname2++;
5204
5205         root1 = talloc_strdup(ctx, pname1);
5206         root2 = talloc_strdup(ctx, pname2);
5207
5208         if (!root1 || !root2) {
5209                 return False;
5210         }
5211
5212         p = strrchr_m(root1,'.');
5213         if (p) {
5214                 *p = 0;
5215                 ext1 = talloc_strdup(ctx, p+1);
5216         } else {
5217                 ext1 = talloc_strdup(ctx, "");
5218         }
5219         p = strrchr_m(root2,'.');
5220         if (p) {
5221                 *p = 0;
5222                 ext2 = talloc_strdup(ctx, p+1);
5223         } else {
5224                 ext2 = talloc_strdup(ctx, "");
5225         }
5226
5227         if (!ext1 || !ext2) {
5228                 return False;
5229         }
5230
5231         p = root1;
5232         p2 = root2;
5233         while (*p2) {
5234                 if (*p2 == '?') {
5235                         /* Hmmm. Should this be mb-aware ? */
5236                         *p2 = *p;
5237                         p2++;
5238                 } else if (*p2 == '*') {
5239                         *p2 = '\0';
5240                         root2 = talloc_asprintf(ctx, "%s%s",
5241                                                 root2,
5242                                                 p);
5243                         if (!root2) {
5244                                 return False;
5245                         }
5246                         break;
5247                 } else {
5248                         p2++;
5249                 }
5250                 if (*p) {
5251                         p++;
5252                 }
5253         }
5254
5255         p = ext1;
5256         p2 = ext2;
5257         while (*p2) {
5258                 if (*p2 == '?') {
5259                         /* Hmmm. Should this be mb-aware ? */
5260                         *p2 = *p;
5261                         p2++;
5262                 } else if (*p2 == '*') {
5263                         *p2 = '\0';
5264                         ext2 = talloc_asprintf(ctx, "%s%s",
5265                                                 ext2,
5266                                                 p);
5267                         if (!ext2) {
5268                                 return False;
5269                         }
5270                         break;
5271                 } else {
5272                         p2++;
5273                 }
5274                 if (*p) {
5275                         p++;
5276                 }
5277         }
5278
5279         if (*ext2) {
5280                 *pp_newname = talloc_asprintf(ctx, "%s/%s.%s",
5281                                 name2_copy,
5282                                 root2,
5283                                 ext2);
5284         } else {
5285                 *pp_newname = talloc_asprintf(ctx, "%s/%s",
5286                                 name2_copy,
5287                                 root2);
5288         }
5289
5290         if (!*pp_newname) {
5291                 return False;
5292         }
5293
5294         return True;
5295 }
5296
5297 /****************************************************************************
5298  Ensure open files have their names updated. Updated to notify other smbd's
5299  asynchronously.
5300 ****************************************************************************/
5301
5302 static void rename_open_files(connection_struct *conn,
5303                               struct share_mode_lock *lck,
5304                               const char *newname)
5305 {
5306         files_struct *fsp;
5307         bool did_rename = False;
5308
5309         for(fsp = file_find_di_first(lck->id); fsp;
5310             fsp = file_find_di_next(fsp)) {
5311                 /* fsp_name is a relative path under the fsp. To change this for other
5312                    sharepaths we need to manipulate relative paths. */
5313                 /* TODO - create the absolute path and manipulate the newname
5314                    relative to the sharepath. */
5315                 if (fsp->conn != conn) {
5316                         continue;
5317                 }
5318                 DEBUG(10,("rename_open_files: renaming file fnum %d (file_id %s) from %s -> %s\n",
5319                           fsp->fnum, file_id_string_tos(&fsp->file_id),
5320                         fsp->fsp_name, newname ));
5321                 string_set(&fsp->fsp_name, newname);
5322                 did_rename = True;
5323         }
5324
5325         if (!did_rename) {
5326                 DEBUG(10,("rename_open_files: no open files on file_id %s for %s\n",
5327                           file_id_string_tos(&lck->id), newname ));
5328         }
5329
5330         /* Send messages to all smbd's (not ourself) that the name has changed. */
5331         rename_share_filename(smbd_messaging_context(), lck, conn->connectpath,
5332                               newname);
5333 }
5334
5335 /****************************************************************************
5336  We need to check if the source path is a parent directory of the destination
5337  (ie. a rename of /foo/bar/baz -> /foo/bar/baz/bibble/bobble. If so we must
5338  refuse the rename with a sharing violation. Under UNIX the above call can
5339  *succeed* if /foo/bar/baz is a symlink to another area in the share. We
5340  probably need to check that the client is a Windows one before disallowing
5341  this as a UNIX client (one with UNIX extensions) can know the source is a
5342  symlink and make this decision intelligently. Found by an excellent bug
5343  report from <AndyLiebman@aol.com>.
5344 ****************************************************************************/
5345
5346 static bool rename_path_prefix_equal(const char *src, const char *dest)
5347 {
5348         const char *psrc = src;
5349         const char *pdst = dest;
5350         size_t slen;
5351
5352         if (psrc[0] == '.' && psrc[1] == '/') {
5353                 psrc += 2;
5354         }
5355         if (pdst[0] == '.' && pdst[1] == '/') {
5356                 pdst += 2;
5357         }
5358         if ((slen = strlen(psrc)) > strlen(pdst)) {
5359                 return False;
5360         }
5361         return ((memcmp(psrc, pdst, slen) == 0) && pdst[slen] == '/');
5362 }
5363
5364 /*
5365  * Do the notify calls from a rename
5366  */
5367
5368 static void notify_rename(connection_struct *conn, bool is_dir,
5369                           const char *oldpath, const char *newpath)
5370 {
5371         char *olddir, *newdir;
5372         const char *oldname, *newname;
5373         uint32 mask;
5374
5375         mask = is_dir ? FILE_NOTIFY_CHANGE_DIR_NAME
5376                 : FILE_NOTIFY_CHANGE_FILE_NAME;
5377
5378         if (!parent_dirname_talloc(NULL, oldpath, &olddir, &oldname)
5379             || !parent_dirname_talloc(NULL, newpath, &newdir, &newname)) {
5380                 TALLOC_FREE(olddir);
5381                 return;
5382         }
5383
5384         if (strcmp(olddir, newdir) == 0) {
5385                 notify_fname(conn, NOTIFY_ACTION_OLD_NAME, mask, oldpath);
5386                 notify_fname(conn, NOTIFY_ACTION_NEW_NAME, mask, newpath);
5387         }
5388         else {
5389                 notify_fname(conn, NOTIFY_ACTION_REMOVED, mask, oldpath);
5390                 notify_fname(conn, NOTIFY_ACTION_ADDED, mask, newpath);
5391         }
5392         TALLOC_FREE(olddir);
5393         TALLOC_FREE(newdir);
5394
5395         /* this is a strange one. w2k3 gives an additional event for
5396            CHANGE_ATTRIBUTES and CHANGE_CREATION on the new file when renaming
5397            files, but not directories */
5398         if (!is_dir) {
5399                 notify_fname(conn, NOTIFY_ACTION_MODIFIED,
5400                              FILE_NOTIFY_CHANGE_ATTRIBUTES
5401                              |FILE_NOTIFY_CHANGE_CREATION,
5402                              newpath);
5403         }
5404 }
5405
5406 /****************************************************************************
5407  Rename an open file - given an fsp.
5408 ****************************************************************************/
5409
5410 NTSTATUS rename_internals_fsp(connection_struct *conn,
5411                         files_struct *fsp,
5412                         char *newname,
5413                         const char *newname_last_component,
5414                         uint32 attrs,
5415                         bool replace_if_exists)
5416 {
5417         TALLOC_CTX *ctx = talloc_tos();
5418         SMB_STRUCT_STAT sbuf, sbuf1;
5419         NTSTATUS status = NT_STATUS_OK;
5420         struct share_mode_lock *lck = NULL;
5421         bool dst_exists;
5422
5423         ZERO_STRUCT(sbuf);
5424
5425         status = check_name(conn, newname);
5426         if (!NT_STATUS_IS_OK(status)) {
5427                 return status;
5428         }
5429
5430         /* Ensure newname contains a '/' */
5431         if(strrchr_m(newname,'/') == 0) {
5432                 newname = talloc_asprintf(ctx,
5433                                         "./%s",
5434                                         newname);
5435                 if (!newname) {
5436                         return NT_STATUS_NO_MEMORY;
5437                 }
5438         }
5439
5440         /*
5441          * Check for special case with case preserving and not
5442          * case sensitive. If the old last component differs from the original
5443          * last component only by case, then we should allow
5444          * the rename (user is trying to change the case of the
5445          * filename).
5446          */
5447
5448         if((conn->case_sensitive == False) && (conn->case_preserve == True) &&
5449                         strequal(newname, fsp->fsp_name)) {
5450                 char *p;
5451                 char *newname_modified_last_component = NULL;
5452
5453                 /*
5454                  * Get the last component of the modified name.
5455                  * Note that we guarantee that newname contains a '/'
5456                  * character above.
5457                  */
5458                 p = strrchr_m(newname,'/');
5459                 newname_modified_last_component = talloc_strdup(ctx,
5460                                                 p+1);
5461                 if (!newname_modified_last_component) {
5462                         return NT_STATUS_NO_MEMORY;
5463                 }
5464
5465                 if(strcsequal(newname_modified_last_component,
5466                               newname_last_component) == False) {
5467                         /*
5468                          * Replace the modified last component with
5469                          * the original.
5470                          */
5471                         *p = '\0'; /* Truncate at the '/' */
5472                         newname = talloc_asprintf(ctx,
5473                                         "%s/%s",
5474                                         newname,
5475                                         newname_last_component);
5476                 }
5477         }
5478
5479         /*
5480          * If the src and dest names are identical - including case,
5481          * don't do the rename, just return success.
5482          */
5483
5484         if (strcsequal(fsp->fsp_name, newname)) {
5485                 DEBUG(3,("rename_internals_fsp: identical names in rename %s - returning success\n",
5486                         newname));
5487                 return NT_STATUS_OK;
5488         }
5489
5490         /*
5491          * Have vfs_object_exist also fill sbuf1
5492          */
5493         dst_exists = vfs_object_exist(conn, newname, &sbuf1);
5494
5495         if(!replace_if_exists && dst_exists) {
5496                 DEBUG(3,("rename_internals_fsp: dest exists doing rename %s -> %s\n",
5497                         fsp->fsp_name,newname));
5498                 return NT_STATUS_OBJECT_NAME_COLLISION;
5499         }
5500
5501         if (dst_exists) {
5502                 struct file_id fileid = vfs_file_id_from_sbuf(conn, &sbuf1);
5503                 files_struct *dst_fsp = file_find_di_first(fileid);
5504                 if (dst_fsp) {
5505                         DEBUG(3, ("rename_internals_fsp: Target file open\n"));
5506                         return NT_STATUS_ACCESS_DENIED;
5507                 }
5508         }
5509
5510         /* Ensure we have a valid stat struct for the source. */
5511         if (fsp->fh->fd != -1) {
5512                 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
5513                         return map_nt_error_from_unix(errno);
5514                 }
5515         } else {
5516                 if (SMB_VFS_STAT(conn,fsp->fsp_name,&sbuf) == -1) {
5517                         return map_nt_error_from_unix(errno);
5518                 }
5519         }
5520
5521         status = can_rename(conn, fsp, attrs, &sbuf);
5522
5523         if (!NT_STATUS_IS_OK(status)) {
5524                 DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
5525                         nt_errstr(status), fsp->fsp_name,newname));
5526                 if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION))
5527                         status = NT_STATUS_ACCESS_DENIED;
5528                 return status;
5529         }
5530
5531         if (rename_path_prefix_equal(fsp->fsp_name, newname)) {
5532                 return NT_STATUS_ACCESS_DENIED;
5533         }
5534
5535         lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL);
5536
5537         /*
5538          * We have the file open ourselves, so not being able to get the
5539          * corresponding share mode lock is a fatal error.
5540          */
5541
5542         SMB_ASSERT(lck != NULL);
5543
5544         if(SMB_VFS_RENAME(conn,fsp->fsp_name, newname) == 0) {
5545                 uint32 create_options = fsp->fh->private_options;
5546
5547                 DEBUG(3,("rename_internals_fsp: succeeded doing rename on %s -> %s\n",
5548                         fsp->fsp_name,newname));
5549
5550                 rename_open_files(conn, lck, newname);
5551
5552                 notify_rename(conn, fsp->is_directory, fsp->fsp_name, newname);
5553
5554                 /*
5555                  * A rename acts as a new file create w.r.t. allowing an initial delete
5556                  * on close, probably because in Windows there is a new handle to the
5557                  * new file. If initial delete on close was requested but not
5558                  * originally set, we need to set it here. This is probably not 100% correct,
5559                  * but will work for the CIFSFS client which in non-posix mode
5560                  * depends on these semantics. JRA.
5561                  */
5562
5563                 set_allow_initial_delete_on_close(lck, fsp, True);
5564
5565                 if (create_options & FILE_DELETE_ON_CLOSE) {
5566                         status = can_set_delete_on_close(fsp, True, 0);
5567
5568                         if (NT_STATUS_IS_OK(status)) {
5569                                 /* Note that here we set the *inital* delete on close flag,
5570                                  * not the regular one. The magic gets handled in close. */
5571                                 fsp->initial_delete_on_close = True;
5572                         }
5573                 }
5574                 TALLOC_FREE(lck);
5575                 return NT_STATUS_OK;
5576         }
5577
5578         TALLOC_FREE(lck);
5579
5580         if (errno == ENOTDIR || errno == EISDIR) {
5581                 status = NT_STATUS_OBJECT_NAME_COLLISION;
5582         } else {
5583                 status = map_nt_error_from_unix(errno);
5584         }
5585
5586         DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
5587                 nt_errstr(status), fsp->fsp_name,newname));
5588
5589         return status;
5590 }
5591
5592 /****************************************************************************
5593  The guts of the rename command, split out so it may be called by the NT SMB
5594  code.
5595 ****************************************************************************/
5596
5597 NTSTATUS rename_internals(TALLOC_CTX *ctx,
5598                         connection_struct *conn,
5599                         struct smb_request *req,
5600                         const char *name_in,
5601                         const char *newname_in,
5602                         uint32 attrs,
5603                         bool replace_if_exists,
5604                         bool src_has_wild,
5605                         bool dest_has_wild)
5606 {
5607         char *directory = NULL;
5608         char *mask = NULL;
5609         char *last_component_src = NULL;
5610         char *last_component_dest = NULL;
5611         char *name = NULL;
5612         char *newname = NULL;
5613         char *p;
5614         int count=0;
5615         NTSTATUS status = NT_STATUS_OK;
5616         SMB_STRUCT_STAT sbuf1, sbuf2;
5617         struct smb_Dir *dir_hnd = NULL;
5618         const char *dname;
5619         long offset = 0;
5620
5621         ZERO_STRUCT(sbuf1);
5622         ZERO_STRUCT(sbuf2);
5623
5624         status = unix_convert(ctx, conn, name_in, src_has_wild, &name,
5625                         &last_component_src, &sbuf1);
5626         if (!NT_STATUS_IS_OK(status)) {
5627                 return status;
5628         }
5629
5630         status = unix_convert(ctx, conn, newname_in, dest_has_wild, &newname,
5631                         &last_component_dest, &sbuf2);
5632         if (!NT_STATUS_IS_OK(status)) {
5633                 return status;
5634         }
5635
5636         /*
5637          * Split the old name into directory and last component
5638          * strings. Note that unix_convert may have stripped off a
5639          * leading ./ from both name and newname if the rename is
5640          * at the root of the share. We need to make sure either both
5641          * name and newname contain a / character or neither of them do
5642          * as this is checked in resolve_wildcards().
5643          */
5644
5645         p = strrchr_m(name,'/');
5646         if (!p) {
5647                 directory = talloc_strdup(ctx, ".");
5648                 if (!directory) {
5649                         return NT_STATUS_NO_MEMORY;
5650                 }
5651                 mask = name;
5652         } else {
5653                 *p = 0;
5654                 directory = talloc_strdup(ctx, name);
5655                 if (!directory) {
5656                         return NT_STATUS_NO_MEMORY;
5657                 }
5658                 mask = p+1;
5659                 *p = '/'; /* Replace needed for exceptional test below. */
5660         }
5661
5662         /*
5663          * We should only check the mangled cache
5664          * here if unix_convert failed. This means
5665          * that the path in 'mask' doesn't exist
5666          * on the file system and so we need to look
5667          * for a possible mangle. This patch from
5668          * Tine Smukavec <valentin.smukavec@hermes.si>.
5669          */
5670
5671         if (!VALID_STAT(sbuf1) && mangle_is_mangled(mask, conn->params)) {
5672                 char *new_mask = NULL;
5673                 mangle_lookup_name_from_8_3(ctx,
5674                                         mask,
5675                                         &new_mask,
5676                                         conn->params );
5677                 if (new_mask) {
5678                         mask = new_mask;
5679                 }
5680         }
5681
5682         if (!src_has_wild) {
5683                 files_struct *fsp;
5684
5685                 /*
5686                  * No wildcards - just process the one file.
5687                  */
5688                 bool is_short_name = mangle_is_8_3(name, True, conn->params);
5689
5690                 /* Add a terminating '/' to the directory name. */
5691                 directory = talloc_asprintf_append(directory,
5692                                 "/%s",
5693                                 mask);
5694                 if (!directory) {
5695                         return NT_STATUS_NO_MEMORY;
5696                 }
5697
5698                 /* Ensure newname contains a '/' also */
5699                 if(strrchr_m(newname,'/') == 0) {
5700                         newname = talloc_asprintf(ctx,
5701                                                 "./%s",
5702                                                 newname);
5703                         if (!newname) {
5704                                 return NT_STATUS_NO_MEMORY;
5705                         }
5706                 }
5707
5708                 DEBUG(3, ("rename_internals: case_sensitive = %d, "
5709                           "case_preserve = %d, short case preserve = %d, "
5710                           "directory = %s, newname = %s, "
5711                           "last_component_dest = %s, is_8_3 = %d\n",
5712                           conn->case_sensitive, conn->case_preserve,
5713                           conn->short_case_preserve, directory,
5714                           newname, last_component_dest, is_short_name));
5715
5716                 /* The dest name still may have wildcards. */
5717                 if (dest_has_wild) {
5718                         char *mod_newname = NULL;
5719                         if (!resolve_wildcards(ctx,
5720                                         directory,newname,&mod_newname)) {
5721                                 DEBUG(6, ("rename_internals: resolve_wildcards "
5722                                         "%s %s failed\n",
5723                                         directory,
5724                                         newname));
5725                                 return NT_STATUS_NO_MEMORY;
5726                         }
5727                         newname = mod_newname;
5728                 }
5729
5730                 ZERO_STRUCT(sbuf1);
5731                 SMB_VFS_STAT(conn, directory, &sbuf1);
5732
5733                 status = S_ISDIR(sbuf1.st_mode) ?
5734                         open_directory(conn, req, directory, &sbuf1,
5735                                        DELETE_ACCESS,
5736                                        FILE_SHARE_READ|FILE_SHARE_WRITE,
5737                                        FILE_OPEN, 0, 0, NULL,
5738                                        &fsp)
5739                         : open_file_ntcreate(conn, req, directory, &sbuf1,
5740                                              DELETE_ACCESS,
5741                                              FILE_SHARE_READ|FILE_SHARE_WRITE,
5742                                              FILE_OPEN, 0, 0, 0, NULL,
5743                                              &fsp);
5744
5745                 if (!NT_STATUS_IS_OK(status)) {
5746                         DEBUG(3, ("Could not open rename source %s: %s\n",
5747                                   directory, nt_errstr(status)));
5748                         return status;
5749                 }
5750
5751                 status = rename_internals_fsp(conn, fsp, newname,
5752                                               last_component_dest,
5753                                               attrs, replace_if_exists);
5754
5755                 close_file(fsp, NORMAL_CLOSE);
5756
5757                 DEBUG(3, ("rename_internals: Error %s rename %s -> %s\n",
5758                           nt_errstr(status), directory,newname));
5759
5760                 return status;
5761         }
5762
5763         /*
5764          * Wildcards - process each file that matches.
5765          */
5766         if (strequal(mask,"????????.???")) {
5767                 mask[0] = '*';
5768                 mask[1] = '\0';
5769         }
5770
5771         status = check_name(conn, directory);
5772         if (!NT_STATUS_IS_OK(status)) {
5773                 return status;
5774         }
5775
5776         dir_hnd = OpenDir(talloc_tos(), conn, directory, mask, attrs);
5777         if (dir_hnd == NULL) {
5778                 return map_nt_error_from_unix(errno);
5779         }
5780
5781         status = NT_STATUS_NO_SUCH_FILE;
5782         /*
5783          * Was status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5784          * - gentest fix. JRA
5785          */
5786
5787         while ((dname = ReadDirName(dir_hnd, &offset))) {
5788                 files_struct *fsp = NULL;
5789                 char *fname = NULL;
5790                 char *destname = NULL;
5791                 bool sysdir_entry = False;
5792
5793                 /* Quick check for "." and ".." */
5794                 if (ISDOT(dname) || ISDOTDOT(dname)) {
5795                         if (attrs & aDIR) {
5796                                 sysdir_entry = True;
5797                         } else {
5798                                 continue;
5799                         }
5800                 }
5801
5802                 if (!is_visible_file(conn, directory, dname, &sbuf1, False)) {
5803                         continue;
5804                 }
5805
5806                 if(!mask_match(dname, mask, conn->case_sensitive)) {
5807                         continue;
5808                 }
5809
5810                 if (sysdir_entry) {
5811                         status = NT_STATUS_OBJECT_NAME_INVALID;
5812                         break;
5813                 }
5814
5815                 fname = talloc_asprintf(ctx,
5816                                 "%s/%s",
5817                                 directory,
5818                                 dname);
5819                 if (!fname) {
5820                         return NT_STATUS_NO_MEMORY;
5821                 }
5822
5823                 if (!resolve_wildcards(ctx,
5824                                 fname,newname,&destname)) {
5825                         DEBUG(6, ("resolve_wildcards %s %s failed\n",
5826                                   fname, destname));
5827                         TALLOC_FREE(fname);
5828                         continue;
5829                 }
5830                 if (!destname) {
5831                         return NT_STATUS_NO_MEMORY;
5832                 }
5833
5834                 ZERO_STRUCT(sbuf1);
5835                 SMB_VFS_STAT(conn, fname, &sbuf1);
5836
5837                 status = S_ISDIR(sbuf1.st_mode) ?
5838                         open_directory(conn, req, fname, &sbuf1,
5839                                        DELETE_ACCESS,
5840                                        FILE_SHARE_READ|FILE_SHARE_WRITE,
5841                                        FILE_OPEN, 0, 0, NULL,
5842                                        &fsp)
5843                         : open_file_ntcreate(conn, req, fname, &sbuf1,
5844                                              DELETE_ACCESS,
5845                                              FILE_SHARE_READ|FILE_SHARE_WRITE,
5846                                              FILE_OPEN, 0, 0, 0, NULL,
5847                                              &fsp);
5848
5849                 if (!NT_STATUS_IS_OK(status)) {
5850                         DEBUG(3,("rename_internals: open_file_ntcreate "
5851                                  "returned %s rename %s -> %s\n",
5852                                  nt_errstr(status), directory, newname));
5853                         break;
5854                 }
5855
5856                 status = rename_internals_fsp(conn, fsp, destname, dname,
5857                                               attrs, replace_if_exists);
5858
5859                 close_file(fsp, NORMAL_CLOSE);
5860
5861                 if (!NT_STATUS_IS_OK(status)) {
5862                         DEBUG(3, ("rename_internals_fsp returned %s for "
5863                                   "rename %s -> %s\n", nt_errstr(status),
5864                                   directory, newname));
5865                         break;
5866                 }
5867
5868                 count++;
5869
5870                 DEBUG(3,("rename_internals: doing rename on %s -> "
5871                          "%s\n",fname,destname));
5872
5873                 TALLOC_FREE(fname);
5874                 TALLOC_FREE(destname);
5875         }
5876         TALLOC_FREE(dir_hnd);
5877
5878         if (count == 0 && NT_STATUS_IS_OK(status)) {
5879                 status = map_nt_error_from_unix(errno);
5880         }
5881
5882         return status;
5883 }
5884
5885 /****************************************************************************
5886  Reply to a mv.
5887 ****************************************************************************/
5888
5889 void reply_mv(struct smb_request *req)
5890 {
5891         connection_struct *conn = req->conn;
5892         char *name = NULL;
5893         char *newname = NULL;
5894         char *p;
5895         uint32 attrs;
5896         NTSTATUS status;
5897         bool src_has_wcard = False;
5898         bool dest_has_wcard = False;
5899         TALLOC_CTX *ctx = talloc_tos();
5900
5901         START_PROFILE(SMBmv);
5902
5903         if (req->wct < 1) {
5904                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
5905                 END_PROFILE(SMBmv);
5906                 return;
5907         }
5908
5909         attrs = SVAL(req->inbuf,smb_vwv0);
5910
5911         p = smb_buf(req->inbuf) + 1;
5912         p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &name, p,
5913                                    0, STR_TERMINATE, &status,
5914                                    &src_has_wcard);
5915         if (!NT_STATUS_IS_OK(status)) {
5916                 reply_nterror(req, status);
5917                 END_PROFILE(SMBmv);
5918                 return;
5919         }
5920         p++;
5921         p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
5922                                    0, STR_TERMINATE, &status,
5923                                    &dest_has_wcard);
5924         if (!NT_STATUS_IS_OK(status)) {
5925                 reply_nterror(req, status);
5926                 END_PROFILE(SMBmv);
5927                 return;
5928         }
5929
5930         status = resolve_dfspath_wcard(ctx, conn,
5931                                        req->flags2 & FLAGS2_DFS_PATHNAMES,
5932                                        name,
5933                                        &name,
5934                                        &src_has_wcard);
5935         if (!NT_STATUS_IS_OK(status)) {
5936                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
5937                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
5938                                         ERRSRV, ERRbadpath);
5939                         END_PROFILE(SMBmv);
5940                         return;
5941                 }
5942                 reply_nterror(req, status);
5943                 END_PROFILE(SMBmv);
5944                 return;
5945         }
5946
5947         status = resolve_dfspath_wcard(ctx, conn,
5948                                        req->flags2 & FLAGS2_DFS_PATHNAMES,
5949                                        newname,
5950                                        &newname,
5951                                        &dest_has_wcard);
5952         if (!NT_STATUS_IS_OK(status)) {
5953                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
5954                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
5955                                         ERRSRV, ERRbadpath);
5956                         END_PROFILE(SMBmv);
5957                         return;
5958                 }
5959                 reply_nterror(req, status);
5960                 END_PROFILE(SMBmv);
5961                 return;
5962         }
5963
5964         DEBUG(3,("reply_mv : %s -> %s\n",name,newname));
5965
5966         status = rename_internals(ctx, conn, req, name, newname, attrs, False,
5967                                   src_has_wcard, dest_has_wcard);
5968         if (!NT_STATUS_IS_OK(status)) {
5969                 if (open_was_deferred(req->mid)) {
5970                         /* We have re-scheduled this call. */
5971                         END_PROFILE(SMBmv);
5972                         return;
5973                 }
5974                 reply_nterror(req, status);
5975                 END_PROFILE(SMBmv);
5976                 return;
5977         }
5978
5979         reply_outbuf(req, 0, 0);
5980
5981         END_PROFILE(SMBmv);
5982         return;
5983 }
5984
5985 /*******************************************************************
5986  Copy a file as part of a reply_copy.
5987 ******************************************************************/
5988
5989 /*
5990  * TODO: check error codes on all callers
5991  */
5992
5993 NTSTATUS copy_file(TALLOC_CTX *ctx,
5994                         connection_struct *conn,
5995                         const char *src,
5996                         const char *dest1,
5997                         int ofun,
5998                         int count,
5999                         bool target_is_directory)
6000 {
6001         SMB_STRUCT_STAT src_sbuf, sbuf2;
6002         SMB_OFF_T ret=-1;
6003         files_struct *fsp1,*fsp2;
6004         char *dest = NULL;
6005         uint32 dosattrs;
6006         uint32 new_create_disposition;
6007         NTSTATUS status;
6008
6009         dest = talloc_strdup(ctx, dest1);
6010         if (!dest) {
6011                 return NT_STATUS_NO_MEMORY;
6012         }
6013         if (target_is_directory) {
6014                 const char *p = strrchr_m(src,'/');
6015                 if (p) {
6016                         p++;
6017                 } else {
6018                         p = src;
6019                 }
6020                 dest = talloc_asprintf_append(dest,
6021                                 "/%s",
6022                                 p);
6023                 if (!dest) {
6024                         return NT_STATUS_NO_MEMORY;
6025                 }
6026         }
6027
6028         if (!vfs_file_exist(conn,src,&src_sbuf)) {
6029                 TALLOC_FREE(dest);
6030                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
6031         }
6032
6033         if (!target_is_directory && count) {
6034                 new_create_disposition = FILE_OPEN;
6035         } else {
6036                 if (!map_open_params_to_ntcreate(dest1,0,ofun,
6037                                 NULL, NULL, &new_create_disposition, NULL)) {
6038                         TALLOC_FREE(dest);
6039                         return NT_STATUS_INVALID_PARAMETER;
6040                 }
6041         }
6042
6043         status = open_file_ntcreate(conn, NULL, src, &src_sbuf,
6044                         FILE_GENERIC_READ,
6045                         FILE_SHARE_READ|FILE_SHARE_WRITE,
6046                         FILE_OPEN,
6047                         0,
6048                         FILE_ATTRIBUTE_NORMAL,
6049                         INTERNAL_OPEN_ONLY,
6050                         NULL, &fsp1);
6051
6052         if (!NT_STATUS_IS_OK(status)) {
6053                 TALLOC_FREE(dest);
6054                 return status;
6055         }
6056
6057         dosattrs = dos_mode(conn, src, &src_sbuf);
6058         if (SMB_VFS_STAT(conn,dest,&sbuf2) == -1) {
6059                 ZERO_STRUCTP(&sbuf2);
6060         }
6061
6062         status = open_file_ntcreate(conn, NULL, dest, &sbuf2,
6063                         FILE_GENERIC_WRITE,
6064                         FILE_SHARE_READ|FILE_SHARE_WRITE,
6065                         new_create_disposition,
6066                         0,
6067                         dosattrs,
6068                         INTERNAL_OPEN_ONLY,
6069                         NULL, &fsp2);
6070
6071         TALLOC_FREE(dest);
6072
6073         if (!NT_STATUS_IS_OK(status)) {
6074                 close_file(fsp1,ERROR_CLOSE);
6075                 return status;
6076         }
6077
6078         if ((ofun&3) == 1) {
6079                 if(SMB_VFS_LSEEK(fsp2,0,SEEK_END) == -1) {
6080                         DEBUG(0,("copy_file: error - vfs lseek returned error %s\n", strerror(errno) ));
6081                         /*
6082                          * Stop the copy from occurring.
6083                          */
6084                         ret = -1;
6085                         src_sbuf.st_size = 0;
6086                 }
6087         }
6088
6089         if (src_sbuf.st_size) {
6090                 ret = vfs_transfer_file(fsp1, fsp2, src_sbuf.st_size);
6091         }
6092
6093         close_file(fsp1,NORMAL_CLOSE);
6094
6095         /* Ensure the modtime is set correctly on the destination file. */
6096         fsp_set_pending_modtime( fsp2, get_mtimespec(&src_sbuf));
6097
6098         /*
6099          * As we are opening fsp1 read-only we only expect
6100          * an error on close on fsp2 if we are out of space.
6101          * Thus we don't look at the error return from the
6102          * close of fsp1.
6103          */
6104         status = close_file(fsp2,NORMAL_CLOSE);
6105
6106         if (!NT_STATUS_IS_OK(status)) {
6107                 return status;
6108         }
6109
6110         if (ret != (SMB_OFF_T)src_sbuf.st_size) {
6111                 return NT_STATUS_DISK_FULL;
6112         }
6113
6114         return NT_STATUS_OK;
6115 }
6116
6117 /****************************************************************************
6118  Reply to a file copy.
6119 ****************************************************************************/
6120
6121 void reply_copy(struct smb_request *req)
6122 {
6123         connection_struct *conn = req->conn;
6124         char *name = NULL;
6125         char *newname = NULL;
6126         char *directory = NULL;
6127         char *mask = NULL;
6128         char *p;
6129         int count=0;
6130         int error = ERRnoaccess;
6131         int err = 0;
6132         int tid2;
6133         int ofun;
6134         int flags;
6135         bool target_is_directory=False;
6136         bool source_has_wild = False;
6137         bool dest_has_wild = False;
6138         SMB_STRUCT_STAT sbuf1, sbuf2;
6139         NTSTATUS status;
6140         TALLOC_CTX *ctx = talloc_tos();
6141
6142         START_PROFILE(SMBcopy);
6143
6144         if (req->wct < 3) {
6145                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
6146                 END_PROFILE(SMBcopy);
6147                 return;
6148         }
6149
6150         tid2 = SVAL(req->inbuf,smb_vwv0);
6151         ofun = SVAL(req->inbuf,smb_vwv1);
6152         flags = SVAL(req->inbuf,smb_vwv2);
6153
6154         p = smb_buf(req->inbuf);
6155         p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &name, p,
6156                                    0, STR_TERMINATE, &status,
6157                                    &source_has_wild);
6158         if (!NT_STATUS_IS_OK(status)) {
6159                 reply_nterror(req, status);
6160                 END_PROFILE(SMBcopy);
6161                 return;
6162         }
6163         p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
6164                                    0, STR_TERMINATE, &status,
6165                                    &dest_has_wild);
6166         if (!NT_STATUS_IS_OK(status)) {
6167                 reply_nterror(req, status);
6168                 END_PROFILE(SMBcopy);
6169                 return;
6170         }
6171
6172         DEBUG(3,("reply_copy : %s -> %s\n",name,newname));
6173
6174         if (tid2 != conn->cnum) {
6175                 /* can't currently handle inter share copies XXXX */
6176                 DEBUG(3,("Rejecting inter-share copy\n"));
6177                 reply_doserror(req, ERRSRV, ERRinvdevice);
6178                 END_PROFILE(SMBcopy);
6179                 return;
6180         }
6181
6182         status = resolve_dfspath_wcard(ctx, conn,
6183                                        req->flags2 & FLAGS2_DFS_PATHNAMES,
6184                                        name,
6185                                        &name,
6186                                        &source_has_wild);
6187         if (!NT_STATUS_IS_OK(status)) {
6188                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
6189                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
6190                                         ERRSRV, ERRbadpath);
6191                         END_PROFILE(SMBcopy);
6192                         return;
6193                 }
6194                 reply_nterror(req, status);
6195                 END_PROFILE(SMBcopy);
6196                 return;
6197         }
6198
6199         status = resolve_dfspath_wcard(ctx, conn,
6200                                        req->flags2 & FLAGS2_DFS_PATHNAMES,
6201                                        newname,
6202                                        &newname,
6203                                        &dest_has_wild);
6204         if (!NT_STATUS_IS_OK(status)) {
6205                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
6206                         reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
6207                                         ERRSRV, ERRbadpath);
6208                         END_PROFILE(SMBcopy);
6209                         return;
6210                 }
6211                 reply_nterror(req, status);
6212                 END_PROFILE(SMBcopy);
6213                 return;
6214         }
6215
6216         status = unix_convert(ctx, conn, name, source_has_wild,
6217                         &name, NULL, &sbuf1);
6218         if (!NT_STATUS_IS_OK(status)) {
6219                 reply_nterror(req, status);
6220                 END_PROFILE(SMBcopy);
6221                 return;
6222         }
6223
6224         status = unix_convert(ctx, conn, newname, dest_has_wild,
6225                         &newname, NULL, &sbuf2);
6226         if (!NT_STATUS_IS_OK(status)) {
6227                 reply_nterror(req, status);
6228                 END_PROFILE(SMBcopy);
6229                 return;
6230         }
6231
6232         target_is_directory = VALID_STAT_OF_DIR(sbuf2);
6233
6234         if ((flags&1) && target_is_directory) {
6235                 reply_doserror(req, ERRDOS, ERRbadfile);
6236                 END_PROFILE(SMBcopy);
6237                 return;
6238         }
6239
6240         if ((flags&2) && !target_is_directory) {
6241                 reply_doserror(req, ERRDOS, ERRbadpath);
6242                 END_PROFILE(SMBcopy);
6243                 return;
6244         }
6245
6246         if ((flags&(1<<5)) && VALID_STAT_OF_DIR(sbuf1)) {
6247                 /* wants a tree copy! XXXX */
6248                 DEBUG(3,("Rejecting tree copy\n"));
6249                 reply_doserror(req, ERRSRV, ERRerror);
6250                 END_PROFILE(SMBcopy);
6251                 return;
6252         }
6253
6254         p = strrchr_m(name,'/');
6255         if (!p) {
6256                 directory = talloc_strdup(ctx, "./");
6257                 if (!directory) {
6258                         reply_nterror(req, NT_STATUS_NO_MEMORY);
6259                         END_PROFILE(SMBcopy);
6260                         return;
6261                 }
6262                 mask = name;
6263         } else {
6264                 *p = 0;
6265                 directory = talloc_strdup(ctx, name);
6266                 if (!directory) {
6267                         reply_nterror(req, NT_STATUS_NO_MEMORY);
6268                         END_PROFILE(SMBcopy);
6269                         return;
6270                 }
6271                 mask = p+1;
6272         }
6273
6274         /*
6275          * We should only check the mangled cache
6276          * here if unix_convert failed. This means
6277          * that the path in 'mask' doesn't exist
6278          * on the file system and so we need to look
6279          * for a possible mangle. This patch from
6280          * Tine Smukavec <valentin.smukavec@hermes.si>.
6281          */
6282
6283         if (!VALID_STAT(sbuf1) && mangle_is_mangled(mask, conn->params)) {
6284                 char *new_mask = NULL;
6285                 mangle_lookup_name_from_8_3(ctx,
6286                                         mask,
6287                                         &new_mask,
6288                                         conn->params );
6289                 if (new_mask) {
6290                         mask = new_mask;
6291                 }
6292         }
6293
6294         if (!source_has_wild) {
6295                 directory = talloc_asprintf_append(directory,
6296                                 "/%s",
6297                                 mask);
6298                 if (dest_has_wild) {
6299                         char *mod_newname = NULL;
6300                         if (!resolve_wildcards(ctx,
6301                                         directory,newname,&mod_newname)) {
6302                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
6303                                 END_PROFILE(SMBcopy);
6304                                 return;
6305                         }
6306                         newname = mod_newname;
6307                 }
6308
6309                 status = check_name(conn, directory);
6310                 if (!NT_STATUS_IS_OK(status)) {
6311                         reply_nterror(req, status);
6312                         END_PROFILE(SMBcopy);
6313                         return;
6314                 }
6315
6316                 status = check_name(conn, newname);
6317                 if (!NT_STATUS_IS_OK(status)) {
6318                         reply_nterror(req, status);
6319                         END_PROFILE(SMBcopy);
6320                         return;
6321                 }
6322
6323                 status = copy_file(ctx,conn,directory,newname,ofun,
6324                                 count,target_is_directory);
6325
6326                 if(!NT_STATUS_IS_OK(status)) {
6327                         reply_nterror(req, status);
6328                         END_PROFILE(SMBcopy);
6329                         return;
6330                 } else {
6331                         count++;
6332                 }
6333         } else {
6334                 struct smb_Dir *dir_hnd = NULL;
6335                 const char *dname = NULL;
6336                 long offset = 0;
6337
6338                 if (strequal(mask,"????????.???")) {
6339                         mask[0] = '*';
6340                         mask[1] = '\0';
6341                 }
6342
6343                 status = check_name(conn, directory);
6344                 if (!NT_STATUS_IS_OK(status)) {
6345                         reply_nterror(req, status);
6346                         END_PROFILE(SMBcopy);
6347                         return;
6348                 }
6349
6350                 dir_hnd = OpenDir(talloc_tos(), conn, directory, mask, 0);
6351                 if (dir_hnd == NULL) {
6352                         status = map_nt_error_from_unix(errno);
6353                         reply_nterror(req, status);
6354                         END_PROFILE(SMBcopy);
6355                         return;
6356                 }
6357
6358                 error = ERRbadfile;
6359
6360                 while ((dname = ReadDirName(dir_hnd, &offset))) {
6361                         char *destname = NULL;
6362                         char *fname = NULL;
6363
6364                         if (ISDOT(dname) || ISDOTDOT(dname)) {
6365                                 continue;
6366                         }
6367
6368                         if (!is_visible_file(conn, directory, dname, &sbuf1, False)) {
6369                                 continue;
6370                         }
6371
6372                         if(!mask_match(dname, mask, conn->case_sensitive)) {
6373                                 continue;
6374                         }
6375
6376                         error = ERRnoaccess;
6377                         fname = talloc_asprintf(ctx,
6378                                         "%s/%s",
6379                                         directory,
6380                                         dname);
6381                         if (!fname) {
6382                                 TALLOC_FREE(dir_hnd);
6383                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
6384                                 END_PROFILE(SMBcopy);
6385                                 return;
6386                         }
6387
6388                         if (!resolve_wildcards(ctx,
6389                                         fname,newname,&destname)) {
6390                                 continue;
6391                         }
6392                         if (!destname) {
6393                                 TALLOC_FREE(dir_hnd);
6394                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
6395                                 END_PROFILE(SMBcopy);
6396                                 return;
6397                         }
6398
6399                         status = check_name(conn, fname);
6400                         if (!NT_STATUS_IS_OK(status)) {
6401                                 TALLOC_FREE(dir_hnd);
6402                                 reply_nterror(req, status);
6403                                 END_PROFILE(SMBcopy);
6404                                 return;
6405                         }
6406
6407                         status = check_name(conn, destname);
6408                         if (!NT_STATUS_IS_OK(status)) {
6409                                 TALLOC_FREE(dir_hnd);
6410                                 reply_nterror(req, status);
6411                                 END_PROFILE(SMBcopy);
6412                                 return;
6413                         }
6414
6415                         DEBUG(3,("reply_copy : doing copy on %s -> %s\n",fname, destname));
6416
6417                         status = copy_file(ctx,conn,fname,destname,ofun,
6418                                         count,target_is_directory);
6419                         if (NT_STATUS_IS_OK(status)) {
6420                                 count++;
6421                         }
6422                         TALLOC_FREE(fname);
6423                         TALLOC_FREE(destname);
6424                 }
6425                 TALLOC_FREE(dir_hnd);
6426         }
6427
6428         if (count == 0) {
6429                 if(err) {
6430                         /* Error on close... */
6431                         errno = err;
6432                         reply_unixerror(req, ERRHRD, ERRgeneral);
6433                         END_PROFILE(SMBcopy);
6434                         return;
6435                 }
6436
6437                 reply_doserror(req, ERRDOS, error);
6438                 END_PROFILE(SMBcopy);
6439                 return;
6440         }
6441
6442         reply_outbuf(req, 1, 0);
6443         SSVAL(req->outbuf,smb_vwv0,count);
6444
6445         END_PROFILE(SMBcopy);
6446         return;
6447 }
6448
6449 #undef DBGC_CLASS
6450 #define DBGC_CLASS DBGC_LOCKING
6451
6452 /****************************************************************************
6453  Get a lock pid, dealing with large count requests.
6454 ****************************************************************************/
6455
6456 uint32 get_lock_pid( char *data, int data_offset, bool large_file_format)
6457 {
6458         if(!large_file_format)
6459                 return (uint32)SVAL(data,SMB_LPID_OFFSET(data_offset));
6460         else
6461                 return (uint32)SVAL(data,SMB_LARGE_LPID_OFFSET(data_offset));
6462 }
6463
6464 /****************************************************************************
6465  Get a lock count, dealing with large count requests.
6466 ****************************************************************************/
6467
6468 SMB_BIG_UINT get_lock_count( char *data, int data_offset, bool large_file_format)
6469 {
6470         SMB_BIG_UINT count = 0;
6471
6472         if(!large_file_format) {
6473                 count = (SMB_BIG_UINT)IVAL(data,SMB_LKLEN_OFFSET(data_offset));
6474         } else {
6475
6476 #if defined(HAVE_LONGLONG)
6477                 count = (((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset))) << 32) |
6478                         ((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)));
6479 #else /* HAVE_LONGLONG */
6480
6481                 /*
6482                  * NT4.x seems to be broken in that it sends large file (64 bit)
6483                  * lockingX calls even if the CAP_LARGE_FILES was *not*
6484                  * negotiated. For boxes without large unsigned ints truncate the
6485                  * lock count by dropping the top 32 bits.
6486                  */
6487
6488                 if(IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)) != 0) {
6489                         DEBUG(3,("get_lock_count: truncating lock count (high)0x%x (low)0x%x to just low count.\n",
6490                                 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)),
6491                                 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)) ));
6492                                 SIVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset),0);
6493                 }
6494
6495                 count = (SMB_BIG_UINT)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset));
6496 #endif /* HAVE_LONGLONG */
6497         }
6498
6499         return count;
6500 }
6501
6502 #if !defined(HAVE_LONGLONG)
6503 /****************************************************************************
6504  Pathetically try and map a 64 bit lock offset into 31 bits. I hate Windows :-).
6505 ****************************************************************************/
6506
6507 static uint32 map_lock_offset(uint32 high, uint32 low)
6508 {
6509         unsigned int i;
6510         uint32 mask = 0;
6511         uint32 highcopy = high;
6512  
6513         /*
6514          * Try and find out how many significant bits there are in high.
6515          */
6516  
6517         for(i = 0; highcopy; i++)
6518                 highcopy >>= 1;
6519  
6520         /*
6521          * We use 31 bits not 32 here as POSIX
6522          * lock offsets may not be negative.
6523          */
6524  
6525         mask = (~0) << (31 - i);
6526  
6527         if(low & mask)
6528                 return 0; /* Fail. */
6529  
6530         high <<= (31 - i);
6531  
6532         return (high|low);
6533 }
6534 #endif /* !defined(HAVE_LONGLONG) */
6535
6536 /****************************************************************************
6537  Get a lock offset, dealing with large offset requests.
6538 ****************************************************************************/
6539
6540 SMB_BIG_UINT get_lock_offset( char *data, int data_offset, bool large_file_format, bool *err)
6541 {
6542         SMB_BIG_UINT offset = 0;
6543
6544         *err = False;
6545
6546         if(!large_file_format) {
6547                 offset = (SMB_BIG_UINT)IVAL(data,SMB_LKOFF_OFFSET(data_offset));
6548         } else {
6549
6550 #if defined(HAVE_LONGLONG)
6551                 offset = (((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset))) << 32) |
6552                                 ((SMB_BIG_UINT) IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset)));
6553 #else /* HAVE_LONGLONG */
6554
6555                 /*
6556                  * NT4.x seems to be broken in that it sends large file (64 bit)
6557                  * lockingX calls even if the CAP_LARGE_FILES was *not*
6558                  * negotiated. For boxes without large unsigned ints mangle the
6559                  * lock offset by mapping the top 32 bits onto the lower 32.
6560                  */
6561       
6562                 if(IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset)) != 0) {
6563                         uint32 low = IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
6564                         uint32 high = IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset));
6565                         uint32 new_low = 0;
6566
6567                         if((new_low = map_lock_offset(high, low)) == 0) {
6568                                 *err = True;
6569                                 return (SMB_BIG_UINT)-1;
6570                         }
6571
6572                         DEBUG(3,("get_lock_offset: truncating lock offset (high)0x%x (low)0x%x to offset 0x%x.\n",
6573                                 (unsigned int)high, (unsigned int)low, (unsigned int)new_low ));
6574                         SIVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset),0);
6575                         SIVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset),new_low);
6576                 }
6577
6578                 offset = (SMB_BIG_UINT)IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
6579 #endif /* HAVE_LONGLONG */
6580         }
6581
6582         return offset;
6583 }
6584
6585 /****************************************************************************
6586  Reply to a lockingX request.
6587 ****************************************************************************/
6588
6589 void reply_lockingX(struct smb_request *req)
6590 {
6591         connection_struct *conn = req->conn;
6592         files_struct *fsp;
6593         unsigned char locktype;
6594         unsigned char oplocklevel;
6595         uint16 num_ulocks;
6596         uint16 num_locks;
6597         SMB_BIG_UINT count = 0, offset = 0;
6598         uint32 lock_pid;
6599         int32 lock_timeout;
6600         int i;
6601         char *data;
6602         bool large_file_format;
6603         bool err;
6604         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
6605
6606         START_PROFILE(SMBlockingX);
6607
6608         if (req->wct < 8) {
6609                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
6610                 END_PROFILE(SMBlockingX);
6611                 return;
6612         }
6613         
6614         fsp = file_fsp(SVAL(req->inbuf,smb_vwv2));
6615         locktype = CVAL(req->inbuf,smb_vwv3);
6616         oplocklevel = CVAL(req->inbuf,smb_vwv3+1);
6617         num_ulocks = SVAL(req->inbuf,smb_vwv6);
6618         num_locks = SVAL(req->inbuf,smb_vwv7);
6619         lock_timeout = IVAL(req->inbuf,smb_vwv4);
6620         large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
6621
6622         if (!check_fsp(conn, req, fsp, &current_user)) {
6623                 END_PROFILE(SMBlockingX);
6624                 return;
6625         }
6626         
6627         data = smb_buf(req->inbuf);
6628
6629         if (locktype & LOCKING_ANDX_CHANGE_LOCKTYPE) {
6630                 /* we don't support these - and CANCEL_LOCK makes w2k
6631                    and XP reboot so I don't really want to be
6632                    compatible! (tridge) */
6633                 reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks));
6634                 END_PROFILE(SMBlockingX);
6635                 return;
6636         }
6637         
6638         /* Check if this is an oplock break on a file
6639            we have granted an oplock on.
6640         */
6641         if ((locktype & LOCKING_ANDX_OPLOCK_RELEASE)) {
6642                 /* Client can insist on breaking to none. */
6643                 bool break_to_none = (oplocklevel == 0);
6644                 bool result;
6645
6646                 DEBUG(5,("reply_lockingX: oplock break reply (%u) from client "
6647                          "for fnum = %d\n", (unsigned int)oplocklevel,
6648                          fsp->fnum ));
6649
6650                 /*
6651                  * Make sure we have granted an exclusive or batch oplock on
6652                  * this file.
6653                  */
6654                 
6655                 if (fsp->oplock_type == 0) {
6656
6657                         /* The Samba4 nbench simulator doesn't understand
6658                            the difference between break to level2 and break
6659                            to none from level2 - it sends oplock break
6660                            replies in both cases. Don't keep logging an error
6661                            message here - just ignore it. JRA. */
6662
6663                         DEBUG(5,("reply_lockingX: Error : oplock break from "
6664                                  "client for fnum = %d (oplock=%d) and no "
6665                                  "oplock granted on this file (%s).\n",
6666                                  fsp->fnum, fsp->oplock_type, fsp->fsp_name));
6667
6668                         /* if this is a pure oplock break request then don't
6669                          * send a reply */
6670                         if (num_locks == 0 && num_ulocks == 0) {
6671                                 END_PROFILE(SMBlockingX);
6672                                 return;
6673                         } else {
6674                                 END_PROFILE(SMBlockingX);
6675                                 reply_doserror(req, ERRDOS, ERRlock);
6676                                 return;
6677                         }
6678                 }
6679
6680                 if ((fsp->sent_oplock_break == BREAK_TO_NONE_SENT) ||
6681                     (break_to_none)) {
6682                         result = remove_oplock(fsp);
6683                 } else {
6684                         result = downgrade_oplock(fsp);
6685                 }
6686                 
6687                 if (!result) {
6688                         DEBUG(0, ("reply_lockingX: error in removing "
6689                                   "oplock on file %s\n", fsp->fsp_name));
6690                         /* Hmmm. Is this panic justified? */
6691                         smb_panic("internal tdb error");
6692                 }
6693
6694                 reply_to_oplock_break_requests(fsp);
6695
6696                 /* if this is a pure oplock break request then don't send a
6697                  * reply */
6698                 if (num_locks == 0 && num_ulocks == 0) {
6699                         /* Sanity check - ensure a pure oplock break is not a
6700                            chained request. */
6701                         if(CVAL(req->inbuf,smb_vwv0) != 0xff)
6702                                 DEBUG(0,("reply_lockingX: Error : pure oplock "
6703                                          "break is a chained %d request !\n",
6704                                          (unsigned int)CVAL(req->inbuf,
6705                                                             smb_vwv0) ));
6706                         END_PROFILE(SMBlockingX);
6707                         return;
6708                 }
6709         }
6710
6711         /*
6712          * We do this check *after* we have checked this is not a oplock break
6713          * response message. JRA.
6714          */
6715         
6716         release_level_2_oplocks_on_change(fsp);
6717
6718         if (smb_buflen(req->inbuf) <
6719             (num_ulocks + num_locks) * (large_file_format ? 20 : 10)) {
6720                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
6721                 END_PROFILE(SMBlockingX);
6722                 return;
6723         }
6724         
6725         /* Data now points at the beginning of the list
6726            of smb_unlkrng structs */
6727         for(i = 0; i < (int)num_ulocks; i++) {
6728                 lock_pid = get_lock_pid( data, i, large_file_format);
6729                 count = get_lock_count( data, i, large_file_format);
6730                 offset = get_lock_offset( data, i, large_file_format, &err);
6731                 
6732                 /*
6733                  * There is no error code marked "stupid client bug".... :-).
6734                  */
6735                 if(err) {
6736                         END_PROFILE(SMBlockingX);
6737                         reply_doserror(req, ERRDOS, ERRnoaccess);
6738                         return;
6739                 }
6740
6741                 DEBUG(10,("reply_lockingX: unlock start=%.0f, len=%.0f for "
6742                           "pid %u, file %s\n", (double)offset, (double)count,
6743                           (unsigned int)lock_pid, fsp->fsp_name ));
6744                 
6745                 status = do_unlock(smbd_messaging_context(),
6746                                 fsp,
6747                                 lock_pid,
6748                                 count,
6749                                 offset,
6750                                 WINDOWS_LOCK);
6751
6752                 if (NT_STATUS_V(status)) {
6753                         END_PROFILE(SMBlockingX);
6754                         reply_nterror(req, status);
6755                         return;
6756                 }
6757         }
6758
6759         /* Setup the timeout in seconds. */
6760
6761         if (!lp_blocking_locks(SNUM(conn))) {
6762                 lock_timeout = 0;
6763         }
6764         
6765         /* Now do any requested locks */
6766         data += ((large_file_format ? 20 : 10)*num_ulocks);
6767         
6768         /* Data now points at the beginning of the list
6769            of smb_lkrng structs */
6770         
6771         for(i = 0; i < (int)num_locks; i++) {
6772                 enum brl_type lock_type = ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
6773                                 READ_LOCK:WRITE_LOCK);
6774                 lock_pid = get_lock_pid( data, i, large_file_format);
6775                 count = get_lock_count( data, i, large_file_format);
6776                 offset = get_lock_offset( data, i, large_file_format, &err);
6777                 
6778                 /*
6779                  * There is no error code marked "stupid client bug".... :-).
6780                  */
6781                 if(err) {
6782                         END_PROFILE(SMBlockingX);
6783                         reply_doserror(req, ERRDOS, ERRnoaccess);
6784                         return;
6785                 }
6786                 
6787                 DEBUG(10,("reply_lockingX: lock start=%.0f, len=%.0f for pid "
6788                           "%u, file %s timeout = %d\n", (double)offset,
6789                           (double)count, (unsigned int)lock_pid,
6790                           fsp->fsp_name, (int)lock_timeout ));
6791                 
6792                 if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
6793                         if (lp_blocking_locks(SNUM(conn))) {
6794
6795                                 /* Schedule a message to ourselves to
6796                                    remove the blocking lock record and
6797                                    return the right error. */
6798
6799                                 if (!blocking_lock_cancel(fsp,
6800                                                 lock_pid,
6801                                                 offset,
6802                                                 count,
6803                                                 WINDOWS_LOCK,
6804                                                 locktype,
6805                                                 NT_STATUS_FILE_LOCK_CONFLICT)) {
6806                                         END_PROFILE(SMBlockingX);
6807                                         reply_nterror(
6808                                                 req,
6809                                                 NT_STATUS_DOS(
6810                                                         ERRDOS,
6811                                                         ERRcancelviolation));
6812                                         return;
6813                                 }
6814                         }
6815                         /* Remove a matching pending lock. */
6816                         status = do_lock_cancel(fsp,
6817                                                 lock_pid,
6818                                                 count,
6819                                                 offset,
6820                                                 WINDOWS_LOCK);
6821                 } else {
6822                         bool blocking_lock = lock_timeout ? True : False;
6823                         bool defer_lock = False;
6824                         struct byte_range_lock *br_lck;
6825                         uint32 block_smbpid;
6826
6827                         br_lck = do_lock(smbd_messaging_context(),
6828                                         fsp,
6829                                         lock_pid,
6830                                         count,
6831                                         offset, 
6832                                         lock_type,
6833                                         WINDOWS_LOCK,
6834                                         blocking_lock,
6835                                         &status,
6836                                         &block_smbpid);
6837
6838                         if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
6839                                 /* Windows internal resolution for blocking locks seems
6840                                    to be about 200ms... Don't wait for less than that. JRA. */
6841                                 if (lock_timeout != -1 && lock_timeout < lp_lock_spin_time()) {
6842                                         lock_timeout = lp_lock_spin_time();
6843                                 }
6844                                 defer_lock = True;
6845                         }
6846
6847                         /* This heuristic seems to match W2K3 very well. If a
6848                            lock sent with timeout of zero would fail with NT_STATUS_FILE_LOCK_CONFLICT
6849                            it pretends we asked for a timeout of between 150 - 300 milliseconds as
6850                            far as I can tell. Replacement for do_lock_spin(). JRA. */
6851
6852                         if (br_lck && lp_blocking_locks(SNUM(conn)) && !blocking_lock &&
6853                                         NT_STATUS_EQUAL((status), NT_STATUS_FILE_LOCK_CONFLICT)) {
6854                                 defer_lock = True;
6855                                 lock_timeout = lp_lock_spin_time();
6856                         }
6857
6858                         if (br_lck && defer_lock) {
6859                                 /*
6860                                  * A blocking lock was requested. Package up
6861                                  * this smb into a queued request and push it
6862                                  * onto the blocking lock queue.
6863                                  */
6864                                 if(push_blocking_lock_request(br_lck,
6865                                                         req,
6866                                                         fsp,
6867                                                         lock_timeout,
6868                                                         i,
6869                                                         lock_pid,
6870                                                         lock_type,
6871                                                         WINDOWS_LOCK,
6872                                                         offset,
6873                                                         count,
6874                                                         block_smbpid)) {
6875                                         TALLOC_FREE(br_lck);
6876                                         END_PROFILE(SMBlockingX);
6877                                         return;
6878                                 }
6879                         }
6880
6881                         TALLOC_FREE(br_lck);
6882                 }
6883
6884                 if (NT_STATUS_V(status)) {
6885                         END_PROFILE(SMBlockingX);
6886                         reply_nterror(req, status);
6887                         return;
6888                 }
6889         }
6890         
6891         /* If any of the above locks failed, then we must unlock
6892            all of the previous locks (X/Open spec). */
6893
6894         if (!(locktype & LOCKING_ANDX_CANCEL_LOCK) &&
6895                         (i != num_locks) &&
6896                         (num_locks != 0)) {
6897                 /*
6898                  * Ensure we don't do a remove on the lock that just failed,
6899                  * as under POSIX rules, if we have a lock already there, we
6900                  * will delete it (and we shouldn't) .....
6901                  */
6902                 for(i--; i >= 0; i--) {
6903                         lock_pid = get_lock_pid( data, i, large_file_format);
6904                         count = get_lock_count( data, i, large_file_format);
6905                         offset = get_lock_offset( data, i, large_file_format,
6906                                                   &err);
6907                         
6908                         /*
6909                          * There is no error code marked "stupid client
6910                          * bug".... :-).
6911                          */
6912                         if(err) {
6913                                 END_PROFILE(SMBlockingX);
6914                                 reply_doserror(req, ERRDOS, ERRnoaccess);
6915                                 return;
6916                         }
6917                         
6918                         do_unlock(smbd_messaging_context(),
6919                                 fsp,
6920                                 lock_pid,
6921                                 count,
6922                                 offset,
6923                                 WINDOWS_LOCK);
6924                 }
6925                 END_PROFILE(SMBlockingX);
6926                 reply_nterror(req, status);
6927                 return;
6928         }
6929
6930         reply_outbuf(req, 2, 0);
6931         
6932         DEBUG(3, ("lockingX fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
6933                   fsp->fnum, (unsigned int)locktype, num_locks, num_ulocks));
6934         
6935         END_PROFILE(SMBlockingX);
6936         chain_reply(req);
6937 }
6938
6939 #undef DBGC_CLASS
6940 #define DBGC_CLASS DBGC_ALL
6941
6942 /****************************************************************************
6943  Reply to a SMBreadbmpx (read block multiplex) request.
6944  Always reply with an error, if someone has a platform really needs this,
6945  please contact vl@samba.org
6946 ****************************************************************************/
6947
6948 void reply_readbmpx(struct smb_request *req)
6949 {
6950         START_PROFILE(SMBreadBmpx);
6951         reply_doserror(req, ERRSRV, ERRuseSTD);
6952         END_PROFILE(SMBreadBmpx);
6953         return;
6954 }
6955
6956 /****************************************************************************
6957  Reply to a SMBreadbs (read block multiplex secondary) request.
6958  Always reply with an error, if someone has a platform really needs this,
6959  please contact vl@samba.org
6960 ****************************************************************************/
6961
6962 void reply_readbs(struct smb_request *req)
6963 {
6964         START_PROFILE(SMBreadBs);
6965         reply_doserror(req, ERRSRV, ERRuseSTD);
6966         END_PROFILE(SMBreadBs);
6967         return;
6968 }
6969
6970 /****************************************************************************
6971  Reply to a SMBsetattrE.
6972 ****************************************************************************/
6973
6974 void reply_setattrE(struct smb_request *req)
6975 {
6976         connection_struct *conn = req->conn;
6977         struct timespec ts[2];
6978         files_struct *fsp;
6979
6980         START_PROFILE(SMBsetattrE);
6981
6982         if (req->wct < 7) {
6983                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
6984                 END_PROFILE(SMBsetattrE);
6985                 return;
6986         }
6987
6988         fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
6989
6990         if(!fsp || (fsp->conn != conn)) {
6991                 reply_doserror(req, ERRDOS, ERRbadfid);
6992                 END_PROFILE(SMBsetattrE);
6993                 return;
6994         }
6995
6996
6997         /*
6998          * Convert the DOS times into unix times. Ignore create
6999          * time as UNIX can't set this.
7000          */
7001
7002         ts[0] = convert_time_t_to_timespec(
7003                 srv_make_unix_date2(req->inbuf+smb_vwv3)); /* atime. */
7004         ts[1] = convert_time_t_to_timespec(
7005                 srv_make_unix_date2(req->inbuf+smb_vwv5)); /* mtime. */
7006   
7007         reply_outbuf(req, 0, 0);
7008
7009         /* 
7010          * Patch from Ray Frush <frush@engr.colostate.edu>
7011          * Sometimes times are sent as zero - ignore them.
7012          */
7013
7014         if (null_timespec(ts[0]) && null_timespec(ts[1])) {
7015                 /* Ignore request */
7016                 if( DEBUGLVL( 3 ) ) {
7017                         dbgtext( "reply_setattrE fnum=%d ", fsp->fnum);
7018                         dbgtext( "ignoring zero request - not setting timestamps of 0\n" );
7019                 }
7020                 END_PROFILE(SMBsetattrE);
7021                 return;
7022         } else if (!null_timespec(ts[0]) && null_timespec(ts[1])) {
7023                 /* set modify time = to access time if modify time was unset */
7024                 ts[1] = ts[0];
7025         }
7026
7027         /* Set the date on this file */
7028         /* Should we set pending modtime here ? JRA */
7029         if(file_ntimes(conn, fsp->fsp_name, ts)) {
7030                 reply_doserror(req, ERRDOS, ERRnoaccess);
7031                 END_PROFILE(SMBsetattrE);
7032                 return;
7033         }
7034   
7035         DEBUG( 3, ( "reply_setattrE fnum=%d actime=%u modtime=%u\n",
7036                 fsp->fnum,
7037                 (unsigned int)ts[0].tv_sec,
7038                 (unsigned int)ts[1].tv_sec));
7039
7040         END_PROFILE(SMBsetattrE);
7041         return;
7042 }
7043
7044
7045 /* Back from the dead for OS/2..... JRA. */
7046
7047 /****************************************************************************
7048  Reply to a SMBwritebmpx (write block multiplex primary) request.
7049  Always reply with an error, if someone has a platform really needs this,
7050  please contact vl@samba.org
7051 ****************************************************************************/
7052
7053 void reply_writebmpx(struct smb_request *req)
7054 {
7055         START_PROFILE(SMBwriteBmpx);
7056         reply_doserror(req, ERRSRV, ERRuseSTD);
7057         END_PROFILE(SMBwriteBmpx);
7058         return;
7059 }
7060
7061 /****************************************************************************
7062  Reply to a SMBwritebs (write block multiplex secondary) request.
7063  Always reply with an error, if someone has a platform really needs this,
7064  please contact vl@samba.org
7065 ****************************************************************************/
7066
7067 void reply_writebs(struct smb_request *req)
7068 {
7069         START_PROFILE(SMBwriteBs);
7070         reply_doserror(req, ERRSRV, ERRuseSTD);
7071         END_PROFILE(SMBwriteBs);
7072         return;
7073 }
7074
7075 /****************************************************************************
7076  Reply to a SMBgetattrE.
7077 ****************************************************************************/
7078
7079 void reply_getattrE(struct smb_request *req)
7080 {
7081         connection_struct *conn = req->conn;
7082         SMB_STRUCT_STAT sbuf;
7083         int mode;
7084         files_struct *fsp;
7085
7086         START_PROFILE(SMBgetattrE);
7087
7088         if (req->wct < 1) {
7089                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
7090                 END_PROFILE(SMBgetattrE);
7091                 return;
7092         }
7093
7094         fsp = file_fsp(SVAL(req->inbuf,smb_vwv0));
7095
7096         if(!fsp || (fsp->conn != conn)) {
7097                 reply_doserror(req, ERRDOS, ERRbadfid);
7098                 END_PROFILE(SMBgetattrE);
7099                 return;
7100         }
7101
7102         /* Do an fstat on this file */
7103         if(fsp_stat(fsp, &sbuf)) {
7104                 reply_unixerror(req, ERRDOS, ERRnoaccess);
7105                 END_PROFILE(SMBgetattrE);
7106                 return;
7107         }
7108   
7109         mode = dos_mode(conn,fsp->fsp_name,&sbuf);
7110   
7111         /*
7112          * Convert the times into dos times. Set create
7113          * date to be last modify date as UNIX doesn't save
7114          * this.
7115          */
7116
7117         reply_outbuf(req, 11, 0);
7118
7119         srv_put_dos_date2((char *)req->outbuf, smb_vwv0,
7120                           get_create_time(&sbuf,
7121                                           lp_fake_dir_create_times(SNUM(conn))));
7122         srv_put_dos_date2((char *)req->outbuf, smb_vwv2, sbuf.st_atime);
7123         /* Should we check pending modtime here ? JRA */
7124         srv_put_dos_date2((char *)req->outbuf, smb_vwv4, sbuf.st_mtime);
7125
7126         if (mode & aDIR) {
7127                 SIVAL(req->outbuf, smb_vwv6, 0);
7128                 SIVAL(req->outbuf, smb_vwv8, 0);
7129         } else {
7130                 uint32 allocation_size = get_allocation_size(conn,fsp, &sbuf);
7131                 SIVAL(req->outbuf, smb_vwv6, (uint32)sbuf.st_size);
7132                 SIVAL(req->outbuf, smb_vwv8, allocation_size);
7133         }
7134         SSVAL(req->outbuf,smb_vwv10, mode);
7135   
7136         DEBUG( 3, ( "reply_getattrE fnum=%d\n", fsp->fnum));
7137   
7138         END_PROFILE(SMBgetattrE);
7139         return;
7140 }