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