2ca14f477d208c7bea6851a8ee555627cd18667b
[samba.git] / source3 / smbd / nttrans.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB NT transaction handling
4    Copyright (C) Jeremy Allison                 1994-2007
5    Copyright (C) Stefan (metze) Metzmacher      2003
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/globals.h"
23
24 extern const struct generic_mapping file_generic_mapping;
25
26 static char *nttrans_realloc(char **ptr, size_t size)
27 {
28         if (ptr==NULL) {
29                 smb_panic("nttrans_realloc() called with NULL ptr");
30         }
31
32         *ptr = (char *)SMB_REALLOC(*ptr, size);
33         if(*ptr == NULL) {
34                 return NULL;
35         }
36         memset(*ptr,'\0',size);
37         return *ptr;
38 }
39
40 /****************************************************************************
41  Send the required number of replies back.
42  We assume all fields other than the data fields are
43  set correctly for the type of call.
44  HACK ! Always assumes smb_setup field is zero.
45 ****************************************************************************/
46
47 void send_nt_replies(connection_struct *conn,
48                         struct smb_request *req, NTSTATUS nt_error,
49                      char *params, int paramsize,
50                      char *pdata, int datasize)
51 {
52         int data_to_send = datasize;
53         int params_to_send = paramsize;
54         int useable_space;
55         char *pp = params;
56         char *pd = pdata;
57         int params_sent_thistime, data_sent_thistime, total_sent_thistime;
58         int alignment_offset = 3;
59         int data_alignment_offset = 0;
60         struct smbd_server_connection *sconn = smbd_server_conn;
61         int max_send = sconn->smb1.sessions.max_send;
62
63         /*
64          * If there genuinely are no parameters or data to send just send
65          * the empty packet.
66          */
67
68         if(params_to_send == 0 && data_to_send == 0) {
69                 reply_outbuf(req, 18, 0);
70                 if (NT_STATUS_V(nt_error)) {
71                         error_packet_set((char *)req->outbuf,
72                                          0, 0, nt_error,
73                                          __LINE__,__FILE__);
74                 }
75                 show_msg((char *)req->outbuf);
76                 if (!srv_send_smb(smbd_server_fd(),
77                                 (char *)req->outbuf,
78                                 true, req->seqnum+1,
79                                 IS_CONN_ENCRYPTED(conn),
80                                 &req->pcd)) {
81                         exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
82                 }
83                 TALLOC_FREE(req->outbuf);
84                 return;
85         }
86
87         /*
88          * When sending params and data ensure that both are nicely aligned.
89          * Only do this alignment when there is also data to send - else
90          * can cause NT redirector problems.
91          */
92
93         if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
94                 data_alignment_offset = 4 - (params_to_send % 4);
95         }
96
97         /*
98          * Space is bufsize minus Netbios over TCP header minus SMB header.
99          * The alignment_offset is to align the param bytes on a four byte
100          * boundary (2 bytes for data len, one byte pad).
101          * NT needs this to work correctly.
102          */
103
104         useable_space = max_send - (smb_size
105                                     + 2 * 18 /* wct */
106                                     + alignment_offset
107                                     + data_alignment_offset);
108
109         if (useable_space < 0) {
110                 char *msg = talloc_asprintf(
111                         talloc_tos(),
112                         "send_nt_replies failed sanity useable_space = %d!!!",
113                         useable_space);
114                 DEBUG(0, ("%s\n", msg));
115                 exit_server_cleanly(msg);
116         }
117
118         while (params_to_send || data_to_send) {
119
120                 /*
121                  * Calculate whether we will totally or partially fill this packet.
122                  */
123
124                 total_sent_thistime = params_to_send + data_to_send;
125
126                 /*
127                  * We can never send more than useable_space.
128                  */
129
130                 total_sent_thistime = MIN(total_sent_thistime, useable_space);
131
132                 reply_outbuf(req, 18,
133                              total_sent_thistime + alignment_offset
134                              + data_alignment_offset);
135
136                 /*
137                  * Set total params and data to be sent.
138                  */
139
140                 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
141                 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
142
143                 /*
144                  * Calculate how many parameters and data we can fit into
145                  * this packet. Parameters get precedence.
146                  */
147
148                 params_sent_thistime = MIN(params_to_send,useable_space);
149                 data_sent_thistime = useable_space - params_sent_thistime;
150                 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
151
152                 SIVAL(req->outbuf, smb_ntr_ParameterCount,
153                       params_sent_thistime);
154
155                 if(params_sent_thistime == 0) {
156                         SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
157                         SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
158                 } else {
159                         /*
160                          * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
161                          * parameter bytes, however the first 4 bytes of outbuf are
162                          * the Netbios over TCP header. Thus use smb_base() to subtract
163                          * them from the calculation.
164                          */
165
166                         SIVAL(req->outbuf,smb_ntr_ParameterOffset,
167                               ((smb_buf(req->outbuf)+alignment_offset)
168                                - smb_base(req->outbuf)));
169                         /*
170                          * Absolute displacement of param bytes sent in this packet.
171                          */
172
173                         SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
174                               pp - params);
175                 }
176
177                 /*
178                  * Deal with the data portion.
179                  */
180
181                 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
182
183                 if(data_sent_thistime == 0) {
184                         SIVAL(req->outbuf,smb_ntr_DataOffset,0);
185                         SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
186                 } else {
187                         /*
188                          * The offset of the data bytes is the offset of the
189                          * parameter bytes plus the number of parameters being sent this time.
190                          */
191
192                         SIVAL(req->outbuf, smb_ntr_DataOffset,
193                               ((smb_buf(req->outbuf)+alignment_offset) -
194                                smb_base(req->outbuf))
195                               + params_sent_thistime + data_alignment_offset);
196                         SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
197                 }
198
199                 /*
200                  * Copy the param bytes into the packet.
201                  */
202
203                 if(params_sent_thistime) {
204                         if (alignment_offset != 0) {
205                                 memset(smb_buf(req->outbuf), 0,
206                                        alignment_offset);
207                         }
208                         memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
209                                params_sent_thistime);
210                 }
211
212                 /*
213                  * Copy in the data bytes
214                  */
215
216                 if(data_sent_thistime) {
217                         if (data_alignment_offset != 0) {
218                                 memset((smb_buf(req->outbuf)+alignment_offset+
219                                         params_sent_thistime), 0,
220                                        data_alignment_offset);
221                         }
222                         memcpy(smb_buf(req->outbuf)+alignment_offset
223                                +params_sent_thistime+data_alignment_offset,
224                                pd,data_sent_thistime);
225                 }
226
227                 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
228                         params_sent_thistime, data_sent_thistime, useable_space));
229                 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
230                         params_to_send, data_to_send, paramsize, datasize));
231
232                 if (NT_STATUS_V(nt_error)) {
233                         error_packet_set((char *)req->outbuf,
234                                          0, 0, nt_error,
235                                          __LINE__,__FILE__);
236                 }
237
238                 /* Send the packet */
239                 show_msg((char *)req->outbuf);
240                 if (!srv_send_smb(smbd_server_fd(),
241                                 (char *)req->outbuf,
242                                 true, req->seqnum+1,
243                                 IS_CONN_ENCRYPTED(conn),
244                                 &req->pcd)) {
245                         exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
246                 }
247
248                 TALLOC_FREE(req->outbuf);
249
250                 pp += params_sent_thistime;
251                 pd += data_sent_thistime;
252
253                 params_to_send -= params_sent_thistime;
254                 data_to_send -= data_sent_thistime;
255
256                 /*
257                  * Sanity check
258                  */
259
260                 if(params_to_send < 0 || data_to_send < 0) {
261                         DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
262                                 params_to_send, data_to_send));
263                         exit_server_cleanly("send_nt_replies: internal error");
264                 }
265         }
266 }
267
268 /****************************************************************************
269  Reply to an NT create and X call on a pipe
270 ****************************************************************************/
271
272 static void nt_open_pipe(char *fname, connection_struct *conn,
273                          struct smb_request *req, int *ppnum)
274 {
275         files_struct *fsp;
276         NTSTATUS status;
277
278         DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
279
280         /* Strip \\ off the name. */
281         fname++;
282
283         status = open_np_file(req, fname, &fsp);
284         if (!NT_STATUS_IS_OK(status)) {
285                 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
286                         reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
287                                         ERRDOS, ERRbadpipe);
288                         return;
289                 }
290                 reply_nterror(req, status);
291                 return;
292         }
293
294         *ppnum = fsp->fnum;
295         return;
296 }
297
298 /****************************************************************************
299  Reply to an NT create and X call for pipes.
300 ****************************************************************************/
301
302 static void do_ntcreate_pipe_open(connection_struct *conn,
303                                   struct smb_request *req)
304 {
305         char *fname = NULL;
306         int pnum = -1;
307         char *p = NULL;
308         uint32 flags = IVAL(req->vwv+3, 1);
309         TALLOC_CTX *ctx = talloc_tos();
310
311         srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
312
313         if (!fname) {
314                 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
315                                 ERRDOS, ERRbadpipe);
316                 return;
317         }
318         nt_open_pipe(fname, conn, req, &pnum);
319
320         if (req->outbuf) {
321                 /* error reply */
322                 return;
323         }
324
325         /*
326          * Deal with pipe return.
327          */
328
329         if (flags & EXTENDED_RESPONSE_REQUIRED) {
330                 /* This is very strange. We
331                  * return 50 words, but only set
332                  * the wcnt to 42 ? It's definately
333                  * what happens on the wire....
334                  */
335                 reply_outbuf(req, 50, 0);
336                 SCVAL(req->outbuf,smb_wct,42);
337         } else {
338                 reply_outbuf(req, 34, 0);
339         }
340
341         p = (char *)req->outbuf + smb_vwv2;
342         p++;
343         SSVAL(p,0,pnum);
344         p += 2;
345         SIVAL(p,0,FILE_WAS_OPENED);
346         p += 4;
347         p += 32;
348         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
349         p += 20;
350         /* File type. */
351         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
352         /* Device state. */
353         SSVAL(p,2, 0x5FF); /* ? */
354         p += 4;
355
356         if (flags & EXTENDED_RESPONSE_REQUIRED) {
357                 p += 25;
358                 SIVAL(p,0,FILE_GENERIC_ALL);
359                 /*
360                  * For pipes W2K3 seems to return
361                  * 0x12019B next.
362                  * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
363                  */
364                 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
365         }
366
367         DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
368
369         chain_reply(req);
370 }
371
372 struct case_semantics_state {
373         connection_struct *conn;
374         bool case_sensitive;
375         bool case_preserve;
376         bool short_case_preserve;
377 };
378
379 /****************************************************************************
380  Restore case semantics.
381 ****************************************************************************/
382
383 static int restore_case_semantics(struct case_semantics_state *state)
384 {
385         state->conn->case_sensitive = state->case_sensitive;
386         state->conn->case_preserve = state->case_preserve;
387         state->conn->short_case_preserve = state->short_case_preserve;
388         return 0;
389 }
390
391 /****************************************************************************
392  Save case semantics.
393 ****************************************************************************/
394
395 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
396                                                 connection_struct *conn)
397 {
398         struct case_semantics_state *result;
399
400         if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
401                 return NULL;
402         }
403
404         result->conn = conn;
405         result->case_sensitive = conn->case_sensitive;
406         result->case_preserve = conn->case_preserve;
407         result->short_case_preserve = conn->short_case_preserve;
408
409         /* Set to POSIX. */
410         conn->case_sensitive = True;
411         conn->case_preserve = True;
412         conn->short_case_preserve = True;
413
414         talloc_set_destructor(result, restore_case_semantics);
415
416         return result;
417 }
418
419 /****************************************************************************
420  Reply to an NT create and X call.
421 ****************************************************************************/
422
423 void reply_ntcreate_and_X(struct smb_request *req)
424 {
425         connection_struct *conn = req->conn;
426         struct smb_filename *smb_fname = NULL;
427         char *fname = NULL;
428         uint32 flags;
429         uint32 access_mask;
430         uint32 file_attributes;
431         uint32 share_access;
432         uint32 create_disposition;
433         uint32 create_options;
434         uint16 root_dir_fid;
435         uint64_t allocation_size;
436         /* Breakout the oplock request bits so we can set the
437            reply bits separately. */
438         uint32 fattr=0;
439         SMB_OFF_T file_len = 0;
440         int info = 0;
441         files_struct *fsp = NULL;
442         char *p = NULL;
443         struct timespec create_timespec;
444         struct timespec c_timespec;
445         struct timespec a_timespec;
446         struct timespec m_timespec;
447         struct timespec write_time_ts;
448         NTSTATUS status;
449         int oplock_request;
450         uint8_t oplock_granted = NO_OPLOCK_RETURN;
451         struct case_semantics_state *case_state = NULL;
452         TALLOC_CTX *ctx = talloc_tos();
453
454         START_PROFILE(SMBntcreateX);
455
456         if (req->wct < 24) {
457                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
458                 goto out;
459         }
460
461         flags = IVAL(req->vwv+3, 1);
462         access_mask = IVAL(req->vwv+7, 1);
463         file_attributes = IVAL(req->vwv+13, 1);
464         share_access = IVAL(req->vwv+15, 1);
465         create_disposition = IVAL(req->vwv+17, 1);
466         create_options = IVAL(req->vwv+19, 1);
467         root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
468
469         allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
470 #ifdef LARGE_SMB_OFF_T
471         allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
472 #endif
473
474         srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
475                             STR_TERMINATE, &status);
476
477         if (!NT_STATUS_IS_OK(status)) {
478                 reply_nterror(req, status);
479                 goto out;
480         }
481
482         DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
483                   "file_attributes = 0x%x, share_access = 0x%x, "
484                   "create_disposition = 0x%x create_options = 0x%x "
485                   "root_dir_fid = 0x%x, fname = %s\n",
486                         (unsigned int)flags,
487                         (unsigned int)access_mask,
488                         (unsigned int)file_attributes,
489                         (unsigned int)share_access,
490                         (unsigned int)create_disposition,
491                         (unsigned int)create_options,
492                         (unsigned int)root_dir_fid,
493                         fname));
494
495         /*
496          * we need to remove ignored bits when they come directly from the client
497          * because we reuse some of them for internal stuff
498          */
499         create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
500
501         /*
502          * If it's an IPC, use the pipe handler.
503          */
504
505         if (IS_IPC(conn)) {
506                 if (lp_nt_pipe_support()) {
507                         do_ntcreate_pipe_open(conn, req);
508                         goto out;
509                 }
510                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
511                 goto out;
512         }
513
514         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
515         if (oplock_request) {
516                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
517                         ? BATCH_OPLOCK : 0;
518         }
519
520         if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
521                 case_state = set_posix_case_semantics(ctx, conn);
522                 if (!case_state) {
523                         reply_nterror(req, NT_STATUS_NO_MEMORY);
524                         goto out;
525                 }
526         }
527
528         status = filename_convert(ctx,
529                                 conn,
530                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
531                                 fname,
532                                 0,
533                                 NULL,
534                                 &smb_fname);
535
536         TALLOC_FREE(case_state);
537
538         if (!NT_STATUS_IS_OK(status)) {
539                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
540                         reply_botherror(req,
541                                 NT_STATUS_PATH_NOT_COVERED,
542                                 ERRSRV, ERRbadpath);
543                         goto out;
544                 }
545                 reply_nterror(req, status);
546                 goto out;
547         }
548
549         /*
550          * Bug #6898 - clients using Windows opens should
551          * never be able to set this attribute into the
552          * VFS.
553          */
554         file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
555
556         status = SMB_VFS_CREATE_FILE(
557                 conn,                                   /* conn */
558                 req,                                    /* req */
559                 root_dir_fid,                           /* root_dir_fid */
560                 smb_fname,                              /* fname */
561                 access_mask,                            /* access_mask */
562                 share_access,                           /* share_access */
563                 create_disposition,                     /* create_disposition*/
564                 create_options,                         /* create_options */
565                 file_attributes,                        /* file_attributes */
566                 oplock_request,                         /* oplock_request */
567                 allocation_size,                        /* allocation_size */
568                 NULL,                                   /* sd */
569                 NULL,                                   /* ea_list */
570                 &fsp,                                   /* result */
571                 &info);                                 /* pinfo */
572
573         if (!NT_STATUS_IS_OK(status)) {
574                 if (open_was_deferred(req->mid)) {
575                         /* We have re-scheduled this call, no error. */
576                         goto out;
577                 }
578                 reply_openerror(req, status);
579                 goto out;
580         }
581
582         /* Ensure we're pointing at the correct stat struct. */
583         TALLOC_FREE(smb_fname);
584         smb_fname = fsp->fsp_name;
585
586         /*
587          * If the caller set the extended oplock request bit
588          * and we granted one (by whatever means) - set the
589          * correct bit for extended oplock reply.
590          */
591
592         if (oplock_request &&
593             (lp_fake_oplocks(SNUM(conn))
594              || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
595
596                 /*
597                  * Exclusive oplock granted
598                  */
599
600                 if (flags & REQUEST_BATCH_OPLOCK) {
601                         oplock_granted = BATCH_OPLOCK_RETURN;
602                 } else {
603                         oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
604                 }
605         } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
606                 oplock_granted = LEVEL_II_OPLOCK_RETURN;
607         } else {
608                 oplock_granted = NO_OPLOCK_RETURN;
609         }
610
611         file_len = smb_fname->st.st_ex_size;
612
613         if (flags & EXTENDED_RESPONSE_REQUIRED) {
614                 /* This is very strange. We
615                  * return 50 words, but only set
616                  * the wcnt to 42 ? It's definately
617                  * what happens on the wire....
618                  */
619                 reply_outbuf(req, 50, 0);
620                 SCVAL(req->outbuf,smb_wct,42);
621         } else {
622                 reply_outbuf(req, 34, 0);
623         }
624
625         p = (char *)req->outbuf + smb_vwv2;
626
627         SCVAL(p, 0, oplock_granted);
628
629         p++;
630         SSVAL(p,0,fsp->fnum);
631         p += 2;
632         if ((create_disposition == FILE_SUPERSEDE)
633             && (info == FILE_WAS_OVERWRITTEN)) {
634                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
635         } else {
636                 SIVAL(p,0,info);
637         }
638         p += 4;
639
640         fattr = dos_mode(conn, smb_fname);
641         if (fattr == 0) {
642                 fattr = FILE_ATTRIBUTE_NORMAL;
643         }
644
645         /* Deal with other possible opens having a modified
646            write time. JRA. */
647         ZERO_STRUCT(write_time_ts);
648         get_file_infos(fsp->file_id, NULL, &write_time_ts);
649         if (!null_timespec(write_time_ts)) {
650                 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
651         }
652
653         /* Create time. */
654         create_timespec = get_create_timespec(conn, fsp, smb_fname);
655         a_timespec = smb_fname->st.st_ex_atime;
656         m_timespec = smb_fname->st.st_ex_mtime;
657         c_timespec = get_change_timespec(conn, fsp, smb_fname);
658
659         if (lp_dos_filetime_resolution(SNUM(conn))) {
660                 dos_filetime_timespec(&create_timespec);
661                 dos_filetime_timespec(&a_timespec);
662                 dos_filetime_timespec(&m_timespec);
663                 dos_filetime_timespec(&c_timespec);
664         }
665
666         put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
667         p += 8;
668         put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
669         p += 8;
670         put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
671         p += 8;
672         put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
673         p += 8;
674         SIVAL(p,0,fattr); /* File Attributes. */
675         p += 4;
676         SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
677         p += 8;
678         SOFF_T(p,0,file_len);
679         p += 8;
680         if (flags & EXTENDED_RESPONSE_REQUIRED) {
681                 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
682                 size_t num_names = 0;
683                 unsigned int num_streams;
684                 struct stream_struct *streams = NULL;
685
686                 /* Do we have any EA's ? */
687                 status = get_ea_names_from_file(ctx, conn, fsp,
688                                 smb_fname->base_name, NULL, &num_names);
689                 if (NT_STATUS_IS_OK(status) && num_names) {
690                         file_status &= ~NO_EAS;
691                 }
692                 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
693                         &num_streams, &streams);
694                 /* There is always one stream, ::$DATA. */
695                 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
696                         file_status &= ~NO_SUBSTREAMS;
697                 }
698                 TALLOC_FREE(streams);
699                 SSVAL(p,2,file_status);
700         }
701         p += 4;
702         SCVAL(p,0,fsp->is_directory ? 1 : 0);
703
704         if (flags & EXTENDED_RESPONSE_REQUIRED) {
705                 uint32 perms = 0;
706                 p += 25;
707                 if (fsp->is_directory ||
708                     can_write_to_file(conn, smb_fname)) {
709                         perms = FILE_GENERIC_ALL;
710                 } else {
711                         perms = FILE_GENERIC_READ|FILE_EXECUTE;
712                 }
713                 SIVAL(p,0,perms);
714         }
715
716         DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
717                 fsp->fnum, smb_fname_str_dbg(smb_fname)));
718
719         chain_reply(req);
720  out:
721         END_PROFILE(SMBntcreateX);
722         return;
723 }
724
725 /****************************************************************************
726  Reply to a NT_TRANSACT_CREATE call to open a pipe.
727 ****************************************************************************/
728
729 static void do_nt_transact_create_pipe(connection_struct *conn,
730                                        struct smb_request *req,
731                                        uint16 **ppsetup, uint32 setup_count,
732                                        char **ppparams, uint32 parameter_count,
733                                        char **ppdata, uint32 data_count)
734 {
735         char *fname = NULL;
736         char *params = *ppparams;
737         int pnum = -1;
738         char *p = NULL;
739         NTSTATUS status;
740         size_t param_len;
741         uint32 flags;
742         TALLOC_CTX *ctx = talloc_tos();
743
744         /*
745          * Ensure minimum number of parameters sent.
746          */
747
748         if(parameter_count < 54) {
749                 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
750                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
751                 return;
752         }
753
754         flags = IVAL(params,0);
755
756         srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
757                         parameter_count-53, STR_TERMINATE,
758                         &status);
759         if (!NT_STATUS_IS_OK(status)) {
760                 reply_nterror(req, status);
761                 return;
762         }
763
764         nt_open_pipe(fname, conn, req, &pnum);
765
766         if (req->outbuf) {
767                 /* Error return */
768                 return;
769         }
770
771         /* Realloc the size of parameters and data we will return */
772         if (flags & EXTENDED_RESPONSE_REQUIRED) {
773                 /* Extended response is 32 more byyes. */
774                 param_len = 101;
775         } else {
776                 param_len = 69;
777         }
778         params = nttrans_realloc(ppparams, param_len);
779         if(params == NULL) {
780                 reply_nterror(req, NT_STATUS_NO_MEMORY);
781                 return;
782         }
783
784         p = params;
785         SCVAL(p,0,NO_OPLOCK_RETURN);
786
787         p += 2;
788         SSVAL(p,0,pnum);
789         p += 2;
790         SIVAL(p,0,FILE_WAS_OPENED);
791         p += 8;
792
793         p += 32;
794         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
795         p += 20;
796         /* File type. */
797         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
798         /* Device state. */
799         SSVAL(p,2, 0x5FF); /* ? */
800         p += 4;
801
802         if (flags & EXTENDED_RESPONSE_REQUIRED) {
803                 p += 25;
804                 SIVAL(p,0,FILE_GENERIC_ALL);
805                 /*
806                  * For pipes W2K3 seems to return
807                  * 0x12019B next.
808                  * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
809                  */
810                 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
811         }
812
813         DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
814
815         /* Send the required number of replies */
816         send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
817
818         return;
819 }
820
821 /****************************************************************************
822  Internal fn to set security descriptors.
823 ****************************************************************************/
824
825 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
826                        uint32 security_info_sent)
827 {
828         SEC_DESC *psd = NULL;
829         NTSTATUS status;
830
831         if (sd_len == 0) {
832                 return NT_STATUS_INVALID_PARAMETER;
833         }
834
835         if (!CAN_WRITE(fsp->conn)) {
836                 return NT_STATUS_ACCESS_DENIED;
837         }
838
839         if (!lp_nt_acl_support(SNUM(fsp->conn))) {
840                 return NT_STATUS_OK;
841         }
842
843         status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
844
845         if (!NT_STATUS_IS_OK(status)) {
846                 return status;
847         }
848
849         if (psd->owner_sid == NULL) {
850                 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
851         }
852         if (psd->group_sid == NULL) {
853                 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
854         }
855
856         /* Ensure we have at least one thing set. */
857         if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
858                 if (security_info_sent & SECINFO_LABEL) {
859                         /* Only consider SECINFO_LABEL if no other
860                            bits are set. Just like W2K3 we don't
861                            store this. */
862                         return NT_STATUS_OK;
863                 }
864                 return NT_STATUS_INVALID_PARAMETER;
865         }
866
867         /* Ensure we have the rights to do this. */
868         if (security_info_sent & SECINFO_OWNER) {
869                 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
870                         return NT_STATUS_ACCESS_DENIED;
871                 }
872         }
873
874         if (security_info_sent & SECINFO_GROUP) {
875                 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
876                         return NT_STATUS_ACCESS_DENIED;
877                 }
878         }
879
880         if (security_info_sent & SECINFO_DACL) {
881                 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
882                         return NT_STATUS_ACCESS_DENIED;
883                 }
884                 /* Convert all the generic bits. */
885                 if (psd->dacl) {
886                         security_acl_map_generic(psd->dacl, &file_generic_mapping);
887                 }
888         }
889
890         if (security_info_sent & SECINFO_SACL) {
891                 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
892                         return NT_STATUS_ACCESS_DENIED;
893                 }
894                 /* Convert all the generic bits. */
895                 if (psd->sacl) {
896                         security_acl_map_generic(psd->sacl, &file_generic_mapping);
897                 }
898         }
899
900         if (DEBUGLEVEL >= 10) {
901                 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
902                 NDR_PRINT_DEBUG(security_descriptor, psd);
903         }
904
905         status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
906
907         TALLOC_FREE(psd);
908
909         return status;
910 }
911
912 /****************************************************************************
913  Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
914 ****************************************************************************/
915
916 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
917 {
918         struct ea_list *ea_list_head = NULL;
919         size_t offset = 0;
920
921         if (data_size < 4) {
922                 return NULL;
923         }
924
925         while (offset + 4 <= data_size) {
926                 size_t next_offset = IVAL(pdata,offset);
927                 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
928
929                 if (!eal) {
930                         return NULL;
931                 }
932
933                 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
934                 if (next_offset == 0) {
935                         break;
936                 }
937                 offset += next_offset;
938         }
939
940         return ea_list_head;
941 }
942
943 /****************************************************************************
944  Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
945 ****************************************************************************/
946
947 static void call_nt_transact_create(connection_struct *conn,
948                                     struct smb_request *req,
949                                     uint16 **ppsetup, uint32 setup_count,
950                                     char **ppparams, uint32 parameter_count,
951                                     char **ppdata, uint32 data_count,
952                                     uint32 max_data_count)
953 {
954         struct smb_filename *smb_fname = NULL;
955         char *fname = NULL;
956         char *params = *ppparams;
957         char *data = *ppdata;
958         /* Breakout the oplock request bits so we can set the reply bits separately. */
959         uint32 fattr=0;
960         SMB_OFF_T file_len = 0;
961         int info = 0;
962         files_struct *fsp = NULL;
963         char *p = NULL;
964         uint32 flags;
965         uint32 access_mask;
966         uint32 file_attributes;
967         uint32 share_access;
968         uint32 create_disposition;
969         uint32 create_options;
970         uint32 sd_len;
971         struct security_descriptor *sd = NULL;
972         uint32 ea_len;
973         uint16 root_dir_fid;
974         struct timespec create_timespec;
975         struct timespec c_timespec;
976         struct timespec a_timespec;
977         struct timespec m_timespec;
978         struct timespec write_time_ts;
979         struct ea_list *ea_list = NULL;
980         NTSTATUS status;
981         size_t param_len;
982         uint64_t allocation_size;
983         int oplock_request;
984         uint8_t oplock_granted;
985         struct case_semantics_state *case_state = NULL;
986         TALLOC_CTX *ctx = talloc_tos();
987
988         DEBUG(5,("call_nt_transact_create\n"));
989
990         /*
991          * If it's an IPC, use the pipe handler.
992          */
993
994         if (IS_IPC(conn)) {
995                 if (lp_nt_pipe_support()) {
996                         do_nt_transact_create_pipe(
997                                 conn, req,
998                                 ppsetup, setup_count,
999                                 ppparams, parameter_count,
1000                                 ppdata, data_count);
1001                         goto out;
1002                 }
1003                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1004                 goto out;
1005         }
1006
1007         /*
1008          * Ensure minimum number of parameters sent.
1009          */
1010
1011         if(parameter_count < 54) {
1012                 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1013                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1014                 goto out;
1015         }
1016
1017         flags = IVAL(params,0);
1018         access_mask = IVAL(params,8);
1019         file_attributes = IVAL(params,20);
1020         share_access = IVAL(params,24);
1021         create_disposition = IVAL(params,28);
1022         create_options = IVAL(params,32);
1023         sd_len = IVAL(params,36);
1024         ea_len = IVAL(params,40);
1025         root_dir_fid = (uint16)IVAL(params,4);
1026         allocation_size = (uint64_t)IVAL(params,12);
1027 #ifdef LARGE_SMB_OFF_T
1028         allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
1029 #endif
1030
1031         /*
1032          * we need to remove ignored bits when they come directly from the client
1033          * because we reuse some of them for internal stuff
1034          */
1035         create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1036
1037         /* Ensure the data_len is correct for the sd and ea values given. */
1038         if ((ea_len + sd_len > data_count)
1039             || (ea_len > data_count) || (sd_len > data_count)
1040             || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1041                 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1042                            "%u, data_count = %u\n", (unsigned int)ea_len,
1043                            (unsigned int)sd_len, (unsigned int)data_count));
1044                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1045                 goto out;
1046         }
1047
1048         if (sd_len) {
1049                 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1050                            sd_len));
1051
1052                 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1053                                              &sd);
1054                 if (!NT_STATUS_IS_OK(status)) {
1055                         DEBUG(10, ("call_nt_transact_create: "
1056                                    "unmarshall_sec_desc failed: %s\n",
1057                                    nt_errstr(status)));
1058                         reply_nterror(req, status);
1059                         goto out;
1060                 }
1061         }
1062
1063         if (ea_len) {
1064                 if (!lp_ea_support(SNUM(conn))) {
1065                         DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1066                                    "EA's not supported.\n",
1067                                    (unsigned int)ea_len));
1068                         reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1069                         goto out;
1070                 }
1071
1072                 if (ea_len < 10) {
1073                         DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1074                                   "too small (should be more than 10)\n",
1075                                   (unsigned int)ea_len ));
1076                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1077                         goto out;
1078                 }
1079
1080                 /* We have already checked that ea_len <= data_count here. */
1081                 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1082                                                ea_len);
1083                 if (ea_list == NULL) {
1084                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1085                         goto out;
1086                 }
1087         }
1088
1089         srvstr_get_path(ctx, params, req->flags2, &fname,
1090                         params+53, parameter_count-53,
1091                         STR_TERMINATE, &status);
1092         if (!NT_STATUS_IS_OK(status)) {
1093                 reply_nterror(req, status);
1094                 goto out;
1095         }
1096
1097         if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1098                 case_state = set_posix_case_semantics(ctx, conn);
1099                 if (!case_state) {
1100                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1101                         goto out;
1102                 }
1103         }
1104
1105         status = filename_convert(ctx,
1106                                 conn,
1107                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
1108                                 fname,
1109                                 0,
1110                                 NULL,
1111                                 &smb_fname);
1112
1113         TALLOC_FREE(case_state);
1114
1115         if (!NT_STATUS_IS_OK(status)) {
1116                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1117                         reply_botherror(req,
1118                                 NT_STATUS_PATH_NOT_COVERED,
1119                                 ERRSRV, ERRbadpath);
1120                         goto out;
1121                 }
1122                 reply_nterror(req, status);
1123                 goto out;
1124         }
1125
1126         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1127         if (oplock_request) {
1128                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1129                         ? BATCH_OPLOCK : 0;
1130         }
1131
1132         /*
1133          * Bug #6898 - clients using Windows opens should
1134          * never be able to set this attribute into the
1135          * VFS.
1136          */
1137         file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1138
1139         status = SMB_VFS_CREATE_FILE(
1140                 conn,                                   /* conn */
1141                 req,                                    /* req */
1142                 root_dir_fid,                           /* root_dir_fid */
1143                 smb_fname,                              /* fname */
1144                 access_mask,                            /* access_mask */
1145                 share_access,                           /* share_access */
1146                 create_disposition,                     /* create_disposition*/
1147                 create_options,                         /* create_options */
1148                 file_attributes,                        /* file_attributes */
1149                 oplock_request,                         /* oplock_request */
1150                 allocation_size,                        /* allocation_size */
1151                 sd,                                     /* sd */
1152                 ea_list,                                /* ea_list */
1153                 &fsp,                                   /* result */
1154                 &info);                                 /* pinfo */
1155
1156         if(!NT_STATUS_IS_OK(status)) {
1157                 if (open_was_deferred(req->mid)) {
1158                         /* We have re-scheduled this call, no error. */
1159                         return;
1160                 }
1161                 reply_openerror(req, status);
1162                 goto out;
1163         }
1164
1165         /* Ensure we're pointing at the correct stat struct. */
1166         TALLOC_FREE(smb_fname);
1167         smb_fname = fsp->fsp_name;
1168
1169         /*
1170          * If the caller set the extended oplock request bit
1171          * and we granted one (by whatever means) - set the
1172          * correct bit for extended oplock reply.
1173          */
1174
1175         if (oplock_request &&
1176             (lp_fake_oplocks(SNUM(conn))
1177              || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1178
1179                 /*
1180                  * Exclusive oplock granted
1181                  */
1182
1183                 if (flags & REQUEST_BATCH_OPLOCK) {
1184                         oplock_granted = BATCH_OPLOCK_RETURN;
1185                 } else {
1186                         oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1187                 }
1188         } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1189                 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1190         } else {
1191                 oplock_granted = NO_OPLOCK_RETURN;
1192         }
1193
1194         file_len = smb_fname->st.st_ex_size;
1195
1196         /* Realloc the size of parameters and data we will return */
1197         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1198                 /* Extended response is 32 more byyes. */
1199                 param_len = 101;
1200         } else {
1201                 param_len = 69;
1202         }
1203         params = nttrans_realloc(ppparams, param_len);
1204         if(params == NULL) {
1205                 reply_nterror(req, NT_STATUS_NO_MEMORY);
1206                 goto out;
1207         }
1208
1209         p = params;
1210         SCVAL(p, 0, oplock_granted);
1211
1212         p += 2;
1213         SSVAL(p,0,fsp->fnum);
1214         p += 2;
1215         if ((create_disposition == FILE_SUPERSEDE)
1216             && (info == FILE_WAS_OVERWRITTEN)) {
1217                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1218         } else {
1219                 SIVAL(p,0,info);
1220         }
1221         p += 8;
1222
1223         fattr = dos_mode(conn, smb_fname);
1224         if (fattr == 0) {
1225                 fattr = FILE_ATTRIBUTE_NORMAL;
1226         }
1227
1228         /* Deal with other possible opens having a modified
1229            write time. JRA. */
1230         ZERO_STRUCT(write_time_ts);
1231         get_file_infos(fsp->file_id, NULL, &write_time_ts);
1232         if (!null_timespec(write_time_ts)) {
1233                 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1234         }
1235
1236         /* Create time. */
1237         create_timespec = get_create_timespec(conn, fsp, smb_fname);
1238         a_timespec = smb_fname->st.st_ex_atime;
1239         m_timespec = smb_fname->st.st_ex_mtime;
1240         c_timespec = get_change_timespec(conn, fsp, smb_fname);
1241
1242         if (lp_dos_filetime_resolution(SNUM(conn))) {
1243                 dos_filetime_timespec(&create_timespec);
1244                 dos_filetime_timespec(&a_timespec);
1245                 dos_filetime_timespec(&m_timespec);
1246                 dos_filetime_timespec(&c_timespec);
1247         }
1248
1249         put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1250         p += 8;
1251         put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1252         p += 8;
1253         put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1254         p += 8;
1255         put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1256         p += 8;
1257         SIVAL(p,0,fattr); /* File Attributes. */
1258         p += 4;
1259         SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1260         p += 8;
1261         SOFF_T(p,0,file_len);
1262         p += 8;
1263         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1264                 SSVAL(p,2,0x7);
1265         }
1266         p += 4;
1267         SCVAL(p,0,fsp->is_directory ? 1 : 0);
1268
1269         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1270                 uint32 perms = 0;
1271                 p += 25;
1272                 if (fsp->is_directory ||
1273                     can_write_to_file(conn, smb_fname)) {
1274                         perms = FILE_GENERIC_ALL;
1275                 } else {
1276                         perms = FILE_GENERIC_READ|FILE_EXECUTE;
1277                 }
1278                 SIVAL(p,0,perms);
1279         }
1280
1281         DEBUG(5,("call_nt_transact_create: open name = %s\n",
1282                  smb_fname_str_dbg(smb_fname)));
1283
1284         /* Send the required number of replies */
1285         send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1286  out:
1287         return;
1288 }
1289
1290 /****************************************************************************
1291  Reply to a NT CANCEL request.
1292  conn POINTER CAN BE NULL HERE !
1293 ****************************************************************************/
1294
1295 void reply_ntcancel(struct smb_request *req)
1296 {
1297         /*
1298          * Go through and cancel any pending change notifies.
1299          */
1300
1301         START_PROFILE(SMBntcancel);
1302         srv_cancel_sign_response(smbd_server_conn);
1303         remove_pending_change_notify_requests_by_mid(req->mid);
1304         remove_pending_lock_requests_by_mid(req->mid);
1305
1306         DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1307
1308         END_PROFILE(SMBntcancel);
1309         return;
1310 }
1311
1312 /****************************************************************************
1313  Copy a file.
1314 ****************************************************************************/
1315
1316 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1317                                 connection_struct *conn,
1318                                 struct smb_request *req,
1319                                 struct smb_filename *smb_fname_src,
1320                                 struct smb_filename *smb_fname_dst,
1321                                 uint32 attrs)
1322 {
1323         files_struct *fsp1,*fsp2;
1324         uint32 fattr;
1325         int info;
1326         SMB_OFF_T ret=-1;
1327         NTSTATUS status = NT_STATUS_OK;
1328         char *parent;
1329
1330         if (!CAN_WRITE(conn)) {
1331                 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1332                 goto out;
1333         }
1334
1335         /* Source must already exist. */
1336         if (!VALID_STAT(smb_fname_src->st)) {
1337                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1338                 goto out;
1339         }
1340
1341         /* Ensure attributes match. */
1342         fattr = dos_mode(conn, smb_fname_src);
1343         if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1344                 status = NT_STATUS_NO_SUCH_FILE;
1345                 goto out;
1346         }
1347
1348         /* Disallow if dst file already exists. */
1349         if (VALID_STAT(smb_fname_dst->st)) {
1350                 status = NT_STATUS_OBJECT_NAME_COLLISION;
1351                 goto out;
1352         }
1353
1354         /* No links from a directory. */
1355         if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1356                 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1357                 goto out;
1358         }
1359
1360         DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1361                   smb_fname_str_dbg(smb_fname_src),
1362                   smb_fname_str_dbg(smb_fname_dst)));
1363
1364         status = SMB_VFS_CREATE_FILE(
1365                 conn,                                   /* conn */
1366                 req,                                    /* req */
1367                 0,                                      /* root_dir_fid */
1368                 smb_fname_src,                          /* fname */
1369                 FILE_READ_DATA,                         /* access_mask */
1370                 (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
1371                     FILE_SHARE_DELETE),
1372                 FILE_OPEN,                              /* create_disposition*/
1373                 0,                                      /* create_options */
1374                 FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
1375                 NO_OPLOCK,                              /* oplock_request */
1376                 0,                                      /* allocation_size */
1377                 NULL,                                   /* sd */
1378                 NULL,                                   /* ea_list */
1379                 &fsp1,                                  /* result */
1380                 &info);                                 /* pinfo */
1381
1382         if (!NT_STATUS_IS_OK(status)) {
1383                 goto out;
1384         }
1385
1386         status = SMB_VFS_CREATE_FILE(
1387                 conn,                                   /* conn */
1388                 req,                                    /* req */
1389                 0,                                      /* root_dir_fid */
1390                 smb_fname_dst,                          /* fname */
1391                 FILE_WRITE_DATA,                        /* access_mask */
1392                 (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
1393                     FILE_SHARE_DELETE),
1394                 FILE_CREATE,                            /* create_disposition*/
1395                 0,                                      /* create_options */
1396                 fattr,                                  /* file_attributes */
1397                 NO_OPLOCK,                              /* oplock_request */
1398                 0,                                      /* allocation_size */
1399                 NULL,                                   /* sd */
1400                 NULL,                                   /* ea_list */
1401                 &fsp2,                                  /* result */
1402                 &info);                                 /* pinfo */
1403
1404         if (!NT_STATUS_IS_OK(status)) {
1405                 close_file(NULL, fsp1, ERROR_CLOSE);
1406                 goto out;
1407         }
1408
1409         if (smb_fname_src->st.st_ex_size) {
1410                 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1411         }
1412
1413         /*
1414          * As we are opening fsp1 read-only we only expect
1415          * an error on close on fsp2 if we are out of space.
1416          * Thus we don't look at the error return from the
1417          * close of fsp1.
1418          */
1419         close_file(NULL, fsp1, NORMAL_CLOSE);
1420
1421         /* Ensure the modtime is set correctly on the destination file. */
1422         set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1423
1424         status = close_file(NULL, fsp2, NORMAL_CLOSE);
1425
1426         /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1427            creates the file. This isn't the correct thing to do in the copy
1428            case. JRA */
1429         if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1430                             NULL)) {
1431                 status = NT_STATUS_NO_MEMORY;
1432                 goto out;
1433         }
1434         file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1435         TALLOC_FREE(parent);
1436
1437         if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1438                 status = NT_STATUS_DISK_FULL;
1439                 goto out;
1440         }
1441  out:
1442         if (!NT_STATUS_IS_OK(status)) {
1443                 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1444                         nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1445                         smb_fname_str_dbg(smb_fname_dst)));
1446         }
1447
1448         return status;
1449 }
1450
1451 /****************************************************************************
1452  Reply to a NT rename request.
1453 ****************************************************************************/
1454
1455 void reply_ntrename(struct smb_request *req)
1456 {
1457         connection_struct *conn = req->conn;
1458         struct smb_filename *smb_fname_old = NULL;
1459         struct smb_filename *smb_fname_new = NULL;
1460         char *oldname = NULL;
1461         char *newname = NULL;
1462         const char *p;
1463         NTSTATUS status;
1464         bool src_has_wcard = False;
1465         bool dest_has_wcard = False;
1466         uint32 attrs;
1467         uint32_t ucf_flags_src = 0;
1468         uint32_t ucf_flags_dst = 0;
1469         uint16 rename_type;
1470         TALLOC_CTX *ctx = talloc_tos();
1471
1472         START_PROFILE(SMBntrename);
1473
1474         if (req->wct < 4) {
1475                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1476                 goto out;
1477         }
1478
1479         attrs = SVAL(req->vwv+0, 0);
1480         rename_type = SVAL(req->vwv+1, 0);
1481
1482         p = (const char *)req->buf + 1;
1483         p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1484                                        &status, &src_has_wcard);
1485         if (!NT_STATUS_IS_OK(status)) {
1486                 reply_nterror(req, status);
1487                 goto out;
1488         }
1489
1490         if (ms_has_wild(oldname)) {
1491                 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1492                 goto out;
1493         }
1494
1495         p++;
1496         p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1497                                        &status, &dest_has_wcard);
1498         if (!NT_STATUS_IS_OK(status)) {
1499                 reply_nterror(req, status);
1500                 goto out;
1501         }
1502
1503         /* The newname must begin with a ':' if the oldname contains a ':'. */
1504         if (strchr_m(oldname, ':') && (newname[0] != ':')) {
1505                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1506                 goto out;
1507         }
1508
1509         /*
1510          * If this is a rename operation, allow wildcards and save the
1511          * destination's last component.
1512          */
1513         if (rename_type == RENAME_FLAG_RENAME) {
1514                 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1515                 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1516         }
1517
1518         /* rename_internals() calls unix_convert(), so don't call it here. */
1519         status = filename_convert(ctx, conn,
1520                                   req->flags2 & FLAGS2_DFS_PATHNAMES,
1521                                   oldname,
1522                                   ucf_flags_src,
1523                                   NULL,
1524                                   &smb_fname_old);
1525         if (!NT_STATUS_IS_OK(status)) {
1526                 if (NT_STATUS_EQUAL(status,
1527                                     NT_STATUS_PATH_NOT_COVERED)) {
1528                         reply_botherror(req,
1529                                         NT_STATUS_PATH_NOT_COVERED,
1530                                         ERRSRV, ERRbadpath);
1531                         goto out;
1532                 }
1533                 reply_nterror(req, status);
1534                 goto out;
1535         }
1536
1537         status = filename_convert(ctx, conn,
1538                                   req->flags2 & FLAGS2_DFS_PATHNAMES,
1539                                   newname,
1540                                   ucf_flags_dst,
1541                                   &dest_has_wcard,
1542                                   &smb_fname_new);
1543         if (!NT_STATUS_IS_OK(status)) {
1544                 if (NT_STATUS_EQUAL(status,
1545                                     NT_STATUS_PATH_NOT_COVERED)) {
1546                         reply_botherror(req,
1547                                         NT_STATUS_PATH_NOT_COVERED,
1548                                         ERRSRV, ERRbadpath);
1549                         goto out;
1550                 }
1551                 reply_nterror(req, status);
1552                 goto out;
1553         }
1554
1555         DEBUG(3,("reply_ntrename: %s -> %s\n",
1556                  smb_fname_str_dbg(smb_fname_old),
1557                  smb_fname_str_dbg(smb_fname_new)));
1558
1559         switch(rename_type) {
1560                 case RENAME_FLAG_RENAME:
1561                         status = rename_internals(ctx, conn, req,
1562                                                   smb_fname_old, smb_fname_new,
1563                                                   attrs, False, src_has_wcard,
1564                                                   dest_has_wcard,
1565                                                   DELETE_ACCESS);
1566                         break;
1567                 case RENAME_FLAG_HARD_LINK:
1568                         if (src_has_wcard || dest_has_wcard) {
1569                                 /* No wildcards. */
1570                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1571                         } else {
1572                                 status = hardlink_internals(ctx, conn,
1573                                                             smb_fname_old,
1574                                                             smb_fname_new);
1575                         }
1576                         break;
1577                 case RENAME_FLAG_COPY:
1578                         if (src_has_wcard || dest_has_wcard) {
1579                                 /* No wildcards. */
1580                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1581                         } else {
1582                                 status = copy_internals(ctx, conn, req,
1583                                                         smb_fname_old,
1584                                                         smb_fname_new,
1585                                                         attrs);
1586                         }
1587                         break;
1588                 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1589                         status = NT_STATUS_INVALID_PARAMETER;
1590                         break;
1591                 default:
1592                         status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1593                         break;
1594         }
1595
1596         if (!NT_STATUS_IS_OK(status)) {
1597                 if (open_was_deferred(req->mid)) {
1598                         /* We have re-scheduled this call. */
1599                         goto out;
1600                 }
1601
1602                 reply_nterror(req, status);
1603                 goto out;
1604         }
1605
1606         reply_outbuf(req, 0, 0);
1607  out:
1608         END_PROFILE(SMBntrename);
1609         return;
1610 }
1611
1612 /****************************************************************************
1613  Reply to a notify change - queue the request and
1614  don't allow a directory to be opened.
1615 ****************************************************************************/
1616
1617 static void smbd_smb1_notify_reply(struct smb_request *req,
1618                                    NTSTATUS error_code,
1619                                    uint8_t *buf, size_t len)
1620 {
1621         send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1622 }
1623
1624 static void call_nt_transact_notify_change(connection_struct *conn,
1625                                            struct smb_request *req,
1626                                            uint16 **ppsetup,
1627                                            uint32 setup_count,
1628                                            char **ppparams,
1629                                            uint32 parameter_count,
1630                                            char **ppdata, uint32 data_count,
1631                                            uint32 max_data_count,
1632                                            uint32 max_param_count)
1633 {
1634         uint16 *setup = *ppsetup;
1635         files_struct *fsp;
1636         uint32 filter;
1637         NTSTATUS status;
1638         bool recursive;
1639
1640         if(setup_count < 6) {
1641                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1642                 return;
1643         }
1644
1645         fsp = file_fsp(req, SVAL(setup,4));
1646         filter = IVAL(setup, 0);
1647         recursive = (SVAL(setup, 6) != 0) ? True : False;
1648
1649         DEBUG(3,("call_nt_transact_notify_change\n"));
1650
1651         if(!fsp) {
1652                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1653                 return;
1654         }
1655
1656         {
1657                 char *filter_string;
1658
1659                 if (!(filter_string = notify_filter_string(NULL, filter))) {
1660                         reply_nterror(req,NT_STATUS_NO_MEMORY);
1661                         return;
1662                 }
1663
1664                 DEBUG(3,("call_nt_transact_notify_change: notify change "
1665                          "called on %s, filter = %s, recursive = %d\n",
1666                          fsp_str_dbg(fsp), filter_string, recursive));
1667
1668                 TALLOC_FREE(filter_string);
1669         }
1670
1671         if((!fsp->is_directory) || (conn != fsp->conn)) {
1672                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1673                 return;
1674         }
1675
1676         if (fsp->notify == NULL) {
1677
1678                 status = change_notify_create(fsp, filter, recursive);
1679
1680                 if (!NT_STATUS_IS_OK(status)) {
1681                         DEBUG(10, ("change_notify_create returned %s\n",
1682                                    nt_errstr(status)));
1683                         reply_nterror(req, status);
1684                         return;
1685                 }
1686         }
1687
1688         if (fsp->notify->num_changes != 0) {
1689
1690                 /*
1691                  * We've got changes pending, respond immediately
1692                  */
1693
1694                 /*
1695                  * TODO: write a torture test to check the filtering behaviour
1696                  * here.
1697                  */
1698
1699                 change_notify_reply(fsp->conn, req,
1700                                     NT_STATUS_OK,
1701                                     max_param_count,
1702                                     fsp->notify,
1703                                     smbd_smb1_notify_reply);
1704
1705                 /*
1706                  * change_notify_reply() above has independently sent its
1707                  * results
1708                  */
1709                 return;
1710         }
1711
1712         /*
1713          * No changes pending, queue the request
1714          */
1715
1716         status = change_notify_add_request(req,
1717                         max_param_count,
1718                         filter,
1719                         recursive, fsp,
1720                         smbd_smb1_notify_reply);
1721         if (!NT_STATUS_IS_OK(status)) {
1722                 reply_nterror(req, status);
1723         }
1724         return;
1725 }
1726
1727 /****************************************************************************
1728  Reply to an NT transact rename command.
1729 ****************************************************************************/
1730
1731 static void call_nt_transact_rename(connection_struct *conn,
1732                                     struct smb_request *req,
1733                                     uint16 **ppsetup, uint32 setup_count,
1734                                     char **ppparams, uint32 parameter_count,
1735                                     char **ppdata, uint32 data_count,
1736                                     uint32 max_data_count)
1737 {
1738         char *params = *ppparams;
1739         char *new_name = NULL;
1740         files_struct *fsp = NULL;
1741         bool dest_has_wcard = False;
1742         NTSTATUS status;
1743         TALLOC_CTX *ctx = talloc_tos();
1744
1745         if(parameter_count < 5) {
1746                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1747                 return;
1748         }
1749
1750         fsp = file_fsp(req, SVAL(params, 0));
1751         if (!check_fsp(conn, req, fsp)) {
1752                 return;
1753         }
1754         srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1755                               parameter_count - 4,
1756                               STR_TERMINATE, &status, &dest_has_wcard);
1757         if (!NT_STATUS_IS_OK(status)) {
1758                 reply_nterror(req, status);
1759                 return;
1760         }
1761
1762         /*
1763          * W2K3 ignores this request as the RAW-RENAME test
1764          * demonstrates, so we do.
1765          */
1766         send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1767
1768         DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1769                  fsp_str_dbg(fsp), new_name));
1770
1771         return;
1772 }
1773
1774 /******************************************************************************
1775  Fake up a completely empty SD.
1776 *******************************************************************************/
1777
1778 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1779 {
1780         size_t sd_size;
1781
1782         *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1783         if(!*ppsd) {
1784                 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1785                 return NT_STATUS_NO_MEMORY;
1786         }
1787
1788         return NT_STATUS_OK;
1789 }
1790
1791 /****************************************************************************
1792  Reply to query a security descriptor.
1793 ****************************************************************************/
1794
1795 static void call_nt_transact_query_security_desc(connection_struct *conn,
1796                                                  struct smb_request *req,
1797                                                  uint16 **ppsetup,
1798                                                  uint32 setup_count,
1799                                                  char **ppparams,
1800                                                  uint32 parameter_count,
1801                                                  char **ppdata,
1802                                                  uint32 data_count,
1803                                                  uint32 max_data_count)
1804 {
1805         char *params = *ppparams;
1806         char *data = *ppdata;
1807         SEC_DESC *psd = NULL;
1808         size_t sd_size;
1809         uint32 security_info_wanted;
1810         files_struct *fsp = NULL;
1811         NTSTATUS status;
1812         DATA_BLOB blob;
1813
1814         if(parameter_count < 8) {
1815                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1816                 return;
1817         }
1818
1819         fsp = file_fsp(req, SVAL(params,0));
1820         if(!fsp) {
1821                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1822                 return;
1823         }
1824
1825         security_info_wanted = IVAL(params,4);
1826
1827         DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1828                  "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1829                  (unsigned int)security_info_wanted));
1830
1831         params = nttrans_realloc(ppparams, 4);
1832         if(params == NULL) {
1833                 reply_nterror(req, NT_STATUS_NO_MEMORY);
1834                 return;
1835         }
1836
1837         /*
1838          * Get the permissions to return.
1839          */
1840
1841         if ((security_info_wanted & SECINFO_SACL) &&
1842                         !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1843                 reply_nterror(req,  NT_STATUS_ACCESS_DENIED);
1844                 return;
1845         }
1846
1847         if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1848                         !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1849                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1850                 return;
1851         }
1852
1853         if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
1854                         SECINFO_GROUP|SECINFO_SACL)) {
1855                 /* Don't return SECINFO_LABEL if anything else was
1856                    requested. See bug #8458. */
1857                 security_info_wanted &= ~SECINFO_LABEL;
1858         }
1859
1860         if (!lp_nt_acl_support(SNUM(conn))) {
1861                 status = get_null_nt_acl(talloc_tos(), &psd);
1862         } else if (security_info_wanted & SECINFO_LABEL) {
1863                 /* Like W2K3 return a null object. */
1864                 status = get_null_nt_acl(talloc_tos(), &psd);
1865         } else {
1866                 status = SMB_VFS_FGET_NT_ACL(
1867                         fsp, security_info_wanted, &psd);
1868         }
1869         if (!NT_STATUS_IS_OK(status)) {
1870                 reply_nterror(req, status);
1871                 return;
1872         }
1873
1874         if (!(security_info_wanted & SECINFO_OWNER)) {
1875                 psd->owner_sid = NULL;
1876         }
1877         if (!(security_info_wanted & SECINFO_GROUP)) {
1878                 psd->group_sid = NULL;
1879         }
1880         if (!(security_info_wanted & SECINFO_DACL)) {
1881                 psd->type &= ~SEC_DESC_DACL_PRESENT;
1882                 psd->dacl = NULL;
1883         }
1884         if (!(security_info_wanted & SECINFO_SACL)) {
1885                 psd->type &= ~SEC_DESC_SACL_PRESENT;
1886                 psd->sacl = NULL;
1887         }
1888
1889         /* If the SACL/DACL is NULL, but was requested, we mark that it is
1890          * present in the reply to match Windows behavior */
1891         if (psd->sacl == NULL &&
1892             security_info_wanted & SACL_SECURITY_INFORMATION)
1893                 psd->type |= SEC_DESC_SACL_PRESENT;
1894         if (psd->dacl == NULL &&
1895             security_info_wanted & DACL_SECURITY_INFORMATION)
1896                 psd->type |= SEC_DESC_DACL_PRESENT;
1897
1898         if (security_info_wanted & SECINFO_LABEL) {
1899                 /* Like W2K3 return a null object. */
1900                 psd->owner_sid = NULL;
1901                 psd->group_sid = NULL;
1902                 psd->dacl = NULL;
1903                 psd->sacl = NULL;
1904                 psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
1905         }
1906
1907         sd_size = ndr_size_security_descriptor(psd, NULL, 0);
1908
1909         DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1910
1911         if (DEBUGLEVEL >= 10) {
1912                 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n",
1913                           fsp_str_dbg(fsp)));
1914                 NDR_PRINT_DEBUG(security_descriptor, psd);
1915         }
1916
1917         SIVAL(params,0,(uint32)sd_size);
1918
1919         if (max_data_count < sd_size) {
1920                 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1921                                 params, 4, *ppdata, 0);
1922                 return;
1923         }
1924
1925         /*
1926          * Allocate the data we will point this at.
1927          */
1928
1929         data = nttrans_realloc(ppdata, sd_size);
1930         if(data == NULL) {
1931                 reply_nterror(req, NT_STATUS_NO_MEMORY);
1932                 return;
1933         }
1934
1935         status = marshall_sec_desc(talloc_tos(), psd,
1936                                    &blob.data, &blob.length);
1937
1938         if (!NT_STATUS_IS_OK(status)) {
1939                 reply_nterror(req, status);
1940                 return;
1941         }
1942
1943         SMB_ASSERT(sd_size == blob.length);
1944         memcpy(data, blob.data, sd_size);
1945
1946         send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1947
1948         return;
1949 }
1950
1951 /****************************************************************************
1952  Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1953 ****************************************************************************/
1954
1955 static void call_nt_transact_set_security_desc(connection_struct *conn,
1956                                                struct smb_request *req,
1957                                                uint16 **ppsetup,
1958                                                uint32 setup_count,
1959                                                char **ppparams,
1960                                                uint32 parameter_count,
1961                                                char **ppdata,
1962                                                uint32 data_count,
1963                                                uint32 max_data_count)
1964 {
1965         char *params= *ppparams;
1966         char *data = *ppdata;
1967         files_struct *fsp = NULL;
1968         uint32 security_info_sent = 0;
1969         NTSTATUS status;
1970
1971         if(parameter_count < 8) {
1972                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1973                 return;
1974         }
1975
1976         if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1977                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1978                 return;
1979         }
1980
1981         if(!lp_nt_acl_support(SNUM(conn))) {
1982                 goto done;
1983         }
1984
1985         security_info_sent = IVAL(params,4);
1986
1987         DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
1988                  fsp_str_dbg(fsp), (unsigned int)security_info_sent));
1989
1990         if (data_count == 0) {
1991                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1992                 return;
1993         }
1994
1995         status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1996
1997         if (!NT_STATUS_IS_OK(status)) {
1998                 reply_nterror(req, status);
1999                 return;
2000         }
2001
2002   done:
2003         send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2004         return;
2005 }
2006
2007 /****************************************************************************
2008  Reply to NT IOCTL
2009 ****************************************************************************/
2010
2011 static void call_nt_transact_ioctl(connection_struct *conn,
2012                                    struct smb_request *req,
2013                                    uint16 **ppsetup, uint32 setup_count,
2014                                    char **ppparams, uint32 parameter_count,
2015                                    char **ppdata, uint32 data_count,
2016                                    uint32 max_data_count)
2017 {
2018         uint32 function;
2019         uint16 fidnum;
2020         files_struct *fsp;
2021         uint8 isFSctl;
2022         uint8 compfilter;
2023         char *pdata = *ppdata;
2024
2025         if (setup_count != 8) {
2026                 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2027                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2028                 return;
2029         }
2030
2031         function = IVAL(*ppsetup, 0);
2032         fidnum = SVAL(*ppsetup, 4);
2033         isFSctl = CVAL(*ppsetup, 6);
2034         compfilter = CVAL(*ppsetup, 7);
2035
2036         DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n", 
2037                  function, fidnum, isFSctl, compfilter));
2038
2039         fsp=file_fsp(req, fidnum);
2040         /* this check is done in each implemented function case for now
2041            because I don't want to break anything... --metze
2042         FSP_BELONGS_CONN(fsp,conn);*/
2043
2044         SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2045
2046         switch (function) {
2047         case FSCTL_SET_SPARSE:
2048                 /* pretend this succeeded - tho strictly we should
2049                    mark the file sparse (if the local fs supports it)
2050                    so we can know if we need to pre-allocate or not */
2051
2052                 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2053                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2054                 return;
2055
2056         case FSCTL_CREATE_OR_GET_OBJECT_ID:
2057         {
2058                 unsigned char objid[16];
2059
2060                 /* This should return the object-id on this file.
2061                  * I think I'll make this be the inode+dev. JRA.
2062                  */
2063
2064                 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2065
2066                 if (!fsp_belongs_conn(conn, req, fsp)) {
2067                         return;
2068                 }
2069
2070                 data_count = 64;
2071                 pdata = nttrans_realloc(ppdata, data_count);
2072                 if (pdata == NULL) {
2073                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2074                         return;
2075                 }
2076
2077                 /* For backwards compatibility only store the dev/inode. */
2078                 push_file_id_16(pdata, &fsp->file_id);
2079                 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2080                 push_file_id_16(pdata+32, &fsp->file_id);
2081                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2082                                 pdata, data_count);
2083                 return;
2084         }
2085
2086         case FSCTL_GET_REPARSE_POINT:
2087                 /* pretend this fail - my winXP does it like this
2088                  * --metze
2089                  */
2090
2091                 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2092                 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2093                 return;
2094
2095         case FSCTL_SET_REPARSE_POINT:
2096                 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2097                  * --metze
2098                  */
2099
2100                 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2101                 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2102                 return;
2103
2104         case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2105         {
2106                 /*
2107                  * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2108                  * and return their volume names.  If max_data_count is 16, then it is just
2109                  * asking for the number of volumes and length of the combined names.
2110                  *
2111                  * pdata is the data allocated by our caller, but that uses
2112                  * total_data_count (which is 0 in our case) rather than max_data_count.
2113                  * Allocate the correct amount and return the pointer to let
2114                  * it be deallocated when we return.
2115                  */
2116                 SHADOW_COPY_DATA *shadow_data = NULL;
2117                 TALLOC_CTX *shadow_mem_ctx = NULL;
2118                 bool labels = False;
2119                 uint32 labels_data_count = 0;
2120                 uint32 i;
2121                 char *cur_pdata;
2122
2123                 if (!fsp_belongs_conn(conn, req, fsp)) {
2124                         return;
2125                 }
2126
2127                 if (max_data_count < 16) {
2128                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2129                                 max_data_count));
2130                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2131                         return;
2132                 }
2133
2134                 if (max_data_count > 16) {
2135                         labels = True;
2136                 }
2137
2138                 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2139                 if (shadow_mem_ctx == NULL) {
2140                         DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2141                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2142                         return;
2143                 }
2144
2145                 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2146                 if (shadow_data == NULL) {
2147                         DEBUG(0,("TALLOC_ZERO() failed!\n"));
2148                         talloc_destroy(shadow_mem_ctx);
2149                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2150                         return;
2151                 }
2152
2153                 shadow_data->mem_ctx = shadow_mem_ctx;
2154
2155                 /*
2156                  * Call the VFS routine to actually do the work.
2157                  */
2158                 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2159                         talloc_destroy(shadow_data->mem_ctx);
2160                         if (errno == ENOSYS) {
2161                                 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n", 
2162                                         conn->connectpath));
2163                                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2164                                 return;
2165                         } else {
2166                                 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n", 
2167                                         conn->connectpath));
2168                                 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2169                                 return;
2170                         }
2171                 }
2172
2173                 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2174
2175                 if (!labels) {
2176                         data_count = 16;
2177                 } else {
2178                         data_count = 12+labels_data_count+4;
2179                 }
2180
2181                 if (max_data_count<data_count) {
2182                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2183                                 max_data_count,data_count));
2184                         talloc_destroy(shadow_data->mem_ctx);
2185                         reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2186                         return;
2187                 }
2188
2189                 pdata = nttrans_realloc(ppdata, data_count);
2190                 if (pdata == NULL) {
2191                         talloc_destroy(shadow_data->mem_ctx);
2192                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2193                         return;
2194                 }
2195
2196                 cur_pdata = pdata;
2197
2198                 /* num_volumes 4 bytes */
2199                 SIVAL(pdata,0,shadow_data->num_volumes);
2200
2201                 if (labels) {
2202                         /* num_labels 4 bytes */
2203                         SIVAL(pdata,4,shadow_data->num_volumes);
2204                 }
2205
2206                 /* needed_data_count 4 bytes */
2207                 SIVAL(pdata, 8, labels_data_count+4);
2208
2209                 cur_pdata+=12;
2210
2211                 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2212                           shadow_data->num_volumes, fsp_str_dbg(fsp)));
2213                 if (labels && shadow_data->labels) {
2214                         for (i=0;i<shadow_data->num_volumes;i++) {
2215                                 srvstr_push(pdata, req->flags2,
2216                                             cur_pdata, shadow_data->labels[i],
2217                                             2*sizeof(SHADOW_COPY_LABEL),
2218                                             STR_UNICODE|STR_TERMINATE);
2219                                 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2220                                 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2221                         }
2222                 }
2223
2224                 talloc_destroy(shadow_data->mem_ctx);
2225
2226                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2227                                 pdata, data_count);
2228
2229                 return;
2230         }
2231
2232         case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2233         {
2234                 /* pretend this succeeded -
2235                  *
2236                  * we have to send back a list with all files owned by this SID
2237                  *
2238                  * but I have to check that --metze
2239                  */
2240                 DOM_SID sid;
2241                 uid_t uid;
2242                 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2243
2244                 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2245
2246                 if (!fsp_belongs_conn(conn, req, fsp)) {
2247                         return;
2248                 }
2249
2250                 /* unknown 4 bytes: this is not the length of the sid :-(  */
2251                 /*unknown = IVAL(pdata,0);*/
2252
2253                 if (!sid_parse(pdata+4,sid_len,&sid)) {
2254                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2255                         return;
2256                 }
2257
2258                 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2259
2260                 if (!sid_to_uid(&sid, &uid)) {
2261                         DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2262                                  sid_string_dbg(&sid),
2263                                  (unsigned long)sid_len));
2264                         uid = (-1);
2265                 }
2266
2267                 /* we can take a look at the find source :-)
2268                  *
2269                  * find ./ -uid $uid  -name '*'   is what we need here
2270                  *
2271                  *
2272                  * and send 4bytes len and then NULL terminated unicode strings
2273                  * for each file
2274                  *
2275                  * but I don't know how to deal with the paged results
2276                  * (maybe we can hang the result anywhere in the fsp struct)
2277                  *
2278                  * we don't send all files at once
2279                  * and at the next we should *not* start from the beginning,
2280                  * so we have to cache the result
2281                  *
2282                  * --metze
2283                  */
2284
2285                 /* this works for now... */
2286                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2287                 return;
2288         }
2289         default:
2290                 if (!logged_ioctl_message) {
2291                         logged_ioctl_message = true; /* Only print this once... */
2292                         DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2293                                  function));
2294                 }
2295         }
2296
2297         reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2298 }
2299
2300
2301 #ifdef HAVE_SYS_QUOTAS
2302 /****************************************************************************
2303  Reply to get user quota
2304 ****************************************************************************/
2305
2306 static void call_nt_transact_get_user_quota(connection_struct *conn,
2307                                             struct smb_request *req,
2308                                             uint16 **ppsetup,
2309                                             uint32 setup_count,
2310                                             char **ppparams,
2311                                             uint32 parameter_count,
2312                                             char **ppdata,
2313                                             uint32 data_count,
2314                                             uint32 max_data_count)
2315 {
2316         NTSTATUS nt_status = NT_STATUS_OK;
2317         char *params = *ppparams;
2318         char *pdata = *ppdata;
2319         char *entry;
2320         int data_len=0,param_len=0;
2321         int qt_len=0;
2322         int entry_len = 0;
2323         files_struct *fsp = NULL;
2324         uint16 level = 0;
2325         size_t sid_len;
2326         DOM_SID sid;
2327         bool start_enum = True;
2328         SMB_NTQUOTA_STRUCT qt;
2329         SMB_NTQUOTA_LIST *tmp_list;
2330         SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2331
2332         ZERO_STRUCT(qt);
2333
2334         /* access check */
2335         if (conn->server_info->utok.uid != 0 && !conn->admin_user) {
2336                 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2337                          "[%s]\n", lp_servicename(SNUM(conn)),
2338                          conn->server_info->unix_name));
2339                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2340                 return;
2341         }
2342
2343         /*
2344          * Ensure minimum number of parameters sent.
2345          */
2346
2347         if (parameter_count < 4) {
2348                 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2349                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2350                 return;
2351         }
2352
2353         /* maybe we can check the quota_fnum */
2354         fsp = file_fsp(req, SVAL(params,0));
2355         if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2356                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2357                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2358                 return;
2359         }
2360
2361         /* the NULL pointer checking for fsp->fake_file_handle->pd
2362          * is done by CHECK_NTQUOTA_HANDLE_OK()
2363          */
2364         qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2365
2366         level = SVAL(params,2);
2367
2368         /* unknown 12 bytes leading in params */
2369
2370         switch (level) {
2371                 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2372                         /* seems that we should continue with the enum here --metze */
2373
2374                         if (qt_handle->quota_list!=NULL &&
2375                             qt_handle->tmp_list==NULL) {
2376
2377                                 /* free the list */
2378                                 free_ntquota_list(&(qt_handle->quota_list));
2379
2380                                 /* Realloc the size of parameters and data we will return */
2381                                 param_len = 4;
2382                                 params = nttrans_realloc(ppparams, param_len);
2383                                 if(params == NULL) {
2384                                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2385                                         return;
2386                                 }
2387
2388                                 data_len = 0;
2389                                 SIVAL(params,0,data_len);
2390
2391                                 break;
2392                         }
2393
2394                         start_enum = False;
2395
2396                 case TRANSACT_GET_USER_QUOTA_LIST_START:
2397
2398                         if (qt_handle->quota_list==NULL &&
2399                                 qt_handle->tmp_list==NULL) {
2400                                 start_enum = True;
2401                         }
2402
2403                         if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2404                                 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2405                                 return;
2406                         }
2407
2408                         /* Realloc the size of parameters and data we will return */
2409                         param_len = 4;
2410                         params = nttrans_realloc(ppparams, param_len);
2411                         if(params == NULL) {
2412                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2413                                 return;
2414                         }
2415
2416                         /* we should not trust the value in max_data_count*/
2417                         max_data_count = MIN(max_data_count,2048);
2418
2419                         pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2420                         if(pdata == NULL) {
2421                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2422                                 return;
2423                         }
2424
2425                         entry = pdata;
2426
2427                         /* set params Size of returned Quota Data 4 bytes*/
2428                         /* but set it later when we know it */
2429
2430                         /* for each entry push the data */
2431
2432                         if (start_enum) {
2433                                 qt_handle->tmp_list = qt_handle->quota_list;
2434                         }
2435
2436                         tmp_list = qt_handle->tmp_list;
2437
2438                         for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2439                                 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2440
2441                                 sid_len = ndr_size_dom_sid(
2442                                         &tmp_list->quotas->sid, NULL, 0);
2443                                 entry_len = 40 + sid_len;
2444
2445                                 /* nextoffset entry 4 bytes */
2446                                 SIVAL(entry,0,entry_len);
2447
2448                                 /* then the len of the SID 4 bytes */
2449                                 SIVAL(entry,4,sid_len);
2450
2451                                 /* unknown data 8 bytes uint64_t */
2452                                 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2453
2454                                 /* the used disk space 8 bytes uint64_t */
2455                                 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2456
2457                                 /* the soft quotas 8 bytes uint64_t */
2458                                 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2459
2460                                 /* the hard quotas 8 bytes uint64_t */
2461                                 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2462
2463                                 /* and now the SID */
2464                                 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2465                         }
2466
2467                         qt_handle->tmp_list = tmp_list;
2468
2469                         /* overwrite the offset of the last entry */
2470                         SIVAL(entry-entry_len,0,0);
2471
2472                         data_len = 4+qt_len;
2473                         /* overwrite the params quota_data_len */
2474                         SIVAL(params,0,data_len);
2475
2476                         break;
2477
2478                 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2479
2480                         /* unknown 4 bytes IVAL(pdata,0) */
2481
2482                         if (data_count < 8) {
2483                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2484                                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2485                                 return;
2486                         }
2487
2488                         sid_len = IVAL(pdata,4);
2489                         /* Ensure this is less than 1mb. */
2490                         if (sid_len > (1024*1024)) {
2491                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2492                                 return;
2493                         }
2494
2495                         if (data_count < 8+sid_len) {
2496                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2497                                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2498                                 return;
2499                         }
2500
2501                         data_len = 4+40+sid_len;
2502
2503                         if (max_data_count < data_len) {
2504                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2505                                         max_data_count, data_len));
2506                                 param_len = 4;
2507                                 SIVAL(params,0,data_len);
2508                                 data_len = 0;
2509                                 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2510                                 break;
2511                         }
2512
2513                         if (!sid_parse(pdata+8,sid_len,&sid)) {
2514                                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2515                                 return;
2516                         }
2517
2518                         if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2519                                 ZERO_STRUCT(qt);
2520                                 /*
2521                                  * we have to return zero's in all fields
2522                                  * instead of returning an error here
2523                                  * --metze
2524                                  */
2525                         }
2526
2527                         /* Realloc the size of parameters and data we will return */
2528                         param_len = 4;
2529                         params = nttrans_realloc(ppparams, param_len);
2530                         if(params == NULL) {
2531                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2532                                 return;
2533                         }
2534
2535                         pdata = nttrans_realloc(ppdata, data_len);
2536                         if(pdata == NULL) {
2537                                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2538                                 return;
2539                         }
2540
2541                         entry = pdata;
2542
2543                         /* set params Size of returned Quota Data 4 bytes*/
2544                         SIVAL(params,0,data_len);
2545
2546                         /* nextoffset entry 4 bytes */
2547                         SIVAL(entry,0,0);
2548
2549                         /* then the len of the SID 4 bytes */
2550                         SIVAL(entry,4,sid_len);
2551
2552                         /* unknown data 8 bytes uint64_t */
2553                         SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2554
2555                         /* the used disk space 8 bytes uint64_t */
2556                         SBIG_UINT(entry,16,qt.usedspace);
2557
2558                         /* the soft quotas 8 bytes uint64_t */
2559                         SBIG_UINT(entry,24,qt.softlim);
2560
2561                         /* the hard quotas 8 bytes uint64_t */
2562                         SBIG_UINT(entry,32,qt.hardlim);
2563
2564                         /* and now the SID */
2565                         sid_linearize(entry+40, sid_len, &sid);
2566
2567                         break;
2568
2569                 default:
2570                         DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2571                         reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2572                         return;
2573                         break;
2574         }
2575
2576         send_nt_replies(conn, req, nt_status, params, param_len,
2577                         pdata, data_len);
2578 }
2579
2580 /****************************************************************************
2581  Reply to set user quota
2582 ****************************************************************************/
2583
2584 static void call_nt_transact_set_user_quota(connection_struct *conn,
2585                                             struct smb_request *req,
2586                                             uint16 **ppsetup,
2587                                             uint32 setup_count,
2588                                             char **ppparams,
2589                                             uint32 parameter_count,
2590                                             char **ppdata,
2591                                             uint32 data_count,
2592                                             uint32 max_data_count)
2593 {
2594         char *params = *ppparams;
2595         char *pdata = *ppdata;
2596         int data_len=0,param_len=0;
2597         SMB_NTQUOTA_STRUCT qt;
2598         size_t sid_len;
2599         DOM_SID sid;
2600         files_struct *fsp = NULL;
2601
2602         ZERO_STRUCT(qt);
2603
2604         /* access check */
2605         if (conn->server_info->utok.uid != 0 && !conn->admin_user) {
2606                 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2607                          "[%s]\n", lp_servicename(SNUM(conn)),
2608                          conn->server_info->unix_name));
2609                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2610                 return;
2611         }
2612
2613         /*
2614          * Ensure minimum number of parameters sent.
2615          */
2616
2617         if (parameter_count < 2) {
2618                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2619                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2620                 return;
2621         }
2622
2623         /* maybe we can check the quota_fnum */
2624         fsp = file_fsp(req, SVAL(params,0));
2625         if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2626                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2627                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2628                 return;
2629         }
2630
2631         if (data_count < 40) {
2632                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2633                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2634                 return;
2635         }
2636
2637         /* offset to next quota record.
2638          * 4 bytes IVAL(pdata,0)
2639          * unused here...
2640          */
2641
2642         /* sid len */
2643         sid_len = IVAL(pdata,4);
2644
2645         if (data_count < 40+sid_len) {
2646                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2647                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2648                 return;
2649         }
2650
2651         /* unknown 8 bytes in pdata
2652          * maybe its the change time in NTTIME
2653          */
2654
2655         /* the used space 8 bytes (uint64_t)*/
2656         qt.usedspace = (uint64_t)IVAL(pdata,16);
2657 #ifdef LARGE_SMB_OFF_T
2658         qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2659 #else /* LARGE_SMB_OFF_T */
2660         if ((IVAL(pdata,20) != 0)&&
2661                 ((qt.usedspace != 0xFFFFFFFF)||
2662                 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2663                 /* more than 32 bits? */
2664                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2665                 return;
2666         }
2667 #endif /* LARGE_SMB_OFF_T */
2668
2669         /* the soft quotas 8 bytes (uint64_t)*/
2670         qt.softlim = (uint64_t)IVAL(pdata,24);
2671 #ifdef LARGE_SMB_OFF_T
2672         qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2673 #else /* LARGE_SMB_OFF_T */
2674         if ((IVAL(pdata,28) != 0)&&
2675                 ((qt.softlim != 0xFFFFFFFF)||
2676                 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2677                 /* more than 32 bits? */
2678                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2679                 return;
2680         }
2681 #endif /* LARGE_SMB_OFF_T */
2682
2683         /* the hard quotas 8 bytes (uint64_t)*/
2684         qt.hardlim = (uint64_t)IVAL(pdata,32);
2685 #ifdef LARGE_SMB_OFF_T
2686         qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2687 #else /* LARGE_SMB_OFF_T */
2688         if ((IVAL(pdata,36) != 0)&&
2689                 ((qt.hardlim != 0xFFFFFFFF)||
2690                 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2691                 /* more than 32 bits? */
2692                 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2693                 return;
2694         }
2695 #endif /* LARGE_SMB_OFF_T */
2696
2697         if (!sid_parse(pdata+40,sid_len,&sid)) {
2698                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2699                 return;
2700         }
2701
2702         DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2703
2704         /* 44 unknown bytes left... */
2705
2706         if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2707                 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2708                 return;
2709         }
2710
2711         send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2712                         pdata, data_len);
2713 }
2714 #endif /* HAVE_SYS_QUOTAS */
2715
2716 static void handle_nttrans(connection_struct *conn,
2717                            struct trans_state *state,
2718                            struct smb_request *req)
2719 {
2720         if (get_Protocol() >= PROTOCOL_NT1) {
2721                 req->flags2 |= 0x40; /* IS_LONG_NAME */
2722                 SSVAL(req->inbuf,smb_flg2,req->flags2);
2723         }
2724
2725
2726         SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2727
2728         /* Now we must call the relevant NT_TRANS function */
2729         switch(state->call) {
2730                 case NT_TRANSACT_CREATE:
2731                 {
2732                         START_PROFILE(NT_transact_create);
2733                         call_nt_transact_create(
2734                                 conn, req,
2735                                 &state->setup, state->setup_count,
2736                                 &state->param, state->total_param,
2737                                 &state->data, state->total_data,
2738                                 state->max_data_return);
2739                         END_PROFILE(NT_transact_create);
2740                         break;
2741                 }
2742
2743                 case NT_TRANSACT_IOCTL:
2744                 {
2745                         START_PROFILE(NT_transact_ioctl);
2746                         call_nt_transact_ioctl(
2747                                 conn, req,
2748                                 &state->setup, state->setup_count,
2749                                 &state->param, state->total_param,
2750                                 &state->data, state->total_data,
2751                                 state->max_data_return);
2752                         END_PROFILE(NT_transact_ioctl);
2753                         break;
2754                 }
2755
2756                 case NT_TRANSACT_SET_SECURITY_DESC:
2757                 {
2758                         START_PROFILE(NT_transact_set_security_desc);
2759                         call_nt_transact_set_security_desc(
2760                                 conn, req,
2761                                 &state->setup, state->setup_count,
2762                                 &state->param, state->total_param,
2763                                 &state->data, state->total_data,
2764                                 state->max_data_return);
2765                         END_PROFILE(NT_transact_set_security_desc);
2766                         break;
2767                 }
2768
2769                 case NT_TRANSACT_NOTIFY_CHANGE:
2770                 {
2771                         START_PROFILE(NT_transact_notify_change);
2772                         call_nt_transact_notify_change(
2773                                 conn, req,
2774                                 &state->setup, state->setup_count,
2775                                 &state->param, state->total_param,
2776                                 &state->data, state->total_data,
2777                                 state->max_data_return,
2778                                 state->max_param_return);
2779                         END_PROFILE(NT_transact_notify_change);
2780                         break;
2781                 }
2782
2783                 case NT_TRANSACT_RENAME:
2784                 {
2785                         START_PROFILE(NT_transact_rename);
2786                         call_nt_transact_rename(
2787                                 conn, req,
2788                                 &state->setup, state->setup_count,
2789                                 &state->param, state->total_param,
2790                                 &state->data, state->total_data,
2791                                 state->max_data_return);
2792                         END_PROFILE(NT_transact_rename);
2793                         break;
2794                 }
2795
2796                 case NT_TRANSACT_QUERY_SECURITY_DESC:
2797                 {
2798                         START_PROFILE(NT_transact_query_security_desc);
2799                         call_nt_transact_query_security_desc(
2800                                 conn, req,
2801                                 &state->setup, state->setup_count,
2802                                 &state->param, state->total_param,
2803                                 &state->data, state->total_data,
2804                                 state->max_data_return);
2805                         END_PROFILE(NT_transact_query_security_desc);
2806                         break;
2807                 }
2808
2809 #ifdef HAVE_SYS_QUOTAS
2810                 case NT_TRANSACT_GET_USER_QUOTA:
2811                 {
2812                         START_PROFILE(NT_transact_get_user_quota);
2813                         call_nt_transact_get_user_quota(
2814                                 conn, req,
2815                                 &state->setup, state->setup_count,
2816                                 &state->param, state->total_param,
2817                                 &state->data, state->total_data,
2818                                 state->max_data_return);
2819                         END_PROFILE(NT_transact_get_user_quota);
2820                         break;
2821                 }
2822
2823                 case NT_TRANSACT_SET_USER_QUOTA:
2824                 {
2825                         START_PROFILE(NT_transact_set_user_quota);
2826                         call_nt_transact_set_user_quota(
2827                                 conn, req,
2828                                 &state->setup, state->setup_count,
2829                                 &state->param, state->total_param,
2830                                 &state->data, state->total_data,
2831                                 state->max_data_return);
2832                         END_PROFILE(NT_transact_set_user_quota);
2833                         break;
2834                 }
2835 #endif /* HAVE_SYS_QUOTAS */
2836
2837                 default:
2838                         /* Error in request */
2839                         DEBUG(0,("handle_nttrans: Unknown request %d in "
2840                                  "nttrans call\n", state->call));
2841                         reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2842                         return;
2843         }
2844         return;
2845 }
2846
2847 /****************************************************************************
2848  Reply to a SMBNTtrans.
2849 ****************************************************************************/
2850
2851 void reply_nttrans(struct smb_request *req)
2852 {
2853         connection_struct *conn = req->conn;
2854         uint32_t pscnt;
2855         uint32_t psoff;
2856         uint32_t dscnt;
2857         uint32_t dsoff;
2858         uint16 function_code;
2859         NTSTATUS result;
2860         struct trans_state *state;
2861
2862         START_PROFILE(SMBnttrans);
2863
2864         if (req->wct < 19) {
2865                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2866                 END_PROFILE(SMBnttrans);
2867                 return;
2868         }
2869
2870         pscnt = IVAL(req->vwv+9, 1);
2871         psoff = IVAL(req->vwv+11, 1);
2872         dscnt = IVAL(req->vwv+13, 1);
2873         dsoff = IVAL(req->vwv+15, 1);
2874         function_code = SVAL(req->vwv+18, 0);
2875
2876         if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2877                 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2878                 END_PROFILE(SMBnttrans);
2879                 return;
2880         }
2881
2882         result = allow_new_trans(conn->pending_trans, req->mid);
2883         if (!NT_STATUS_IS_OK(result)) {
2884                 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2885                 reply_nterror(req, result);
2886                 END_PROFILE(SMBnttrans);
2887                 return;
2888         }
2889
2890         if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2891                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2892                 END_PROFILE(SMBnttrans);
2893                 return;
2894         }
2895
2896         state->cmd = SMBnttrans;
2897
2898         state->mid = req->mid;
2899         state->vuid = req->vuid;
2900         state->total_data = IVAL(req->vwv+3, 1);
2901         state->data = NULL;
2902         state->total_param = IVAL(req->vwv+1, 1);
2903         state->param = NULL;
2904         state->max_data_return = IVAL(req->vwv+7, 1);
2905         state->max_param_return = IVAL(req->vwv+5, 1);
2906
2907         /* setup count is in *words* */
2908         state->setup_count = 2*CVAL(req->vwv+17, 1);
2909         state->setup = NULL;
2910         state->call = function_code;
2911
2912         DEBUG(10, ("num_setup=%u, "
2913                    "param_total=%u, this_param=%u, max_param=%u, "
2914                    "data_total=%u, this_data=%u, max_data=%u, "
2915                    "param_offset=%u, data_offset=%u\n",
2916                    (unsigned)state->setup_count,
2917                    (unsigned)state->total_param, (unsigned)pscnt,
2918                    (unsigned)state->max_param_return,
2919                    (unsigned)state->total_data, (unsigned)dscnt,
2920                    (unsigned)state->max_data_return,
2921                    (unsigned)psoff, (unsigned)dsoff));
2922
2923         /*
2924          * All nttrans messages we handle have smb_wct == 19 +
2925          * state->setup_count.  Ensure this is so as a sanity check.
2926          */
2927
2928         if(req->wct != 19 + (state->setup_count/2)) {
2929                 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2930                          req->wct, 19 + (state->setup_count/2)));
2931                 goto bad_param;
2932         }
2933
2934         /* Don't allow more than 128mb for each value. */
2935         if ((state->total_data > (1024*1024*128)) ||
2936             (state->total_param > (1024*1024*128))) {
2937                 reply_nterror(req, NT_STATUS_NO_MEMORY);
2938                 END_PROFILE(SMBnttrans);
2939                 return;
2940         }
2941
2942         if ((dscnt > state->total_data) || (pscnt > state->total_param))
2943                 goto bad_param;
2944
2945         if (state->total_data)  {
2946
2947                 if (trans_oob(state->total_data, 0, dscnt)
2948                     || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2949                         goto bad_param;
2950                 }
2951
2952                 /* Can't use talloc here, the core routines do realloc on the
2953                  * params and data. */
2954                 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2955                         DEBUG(0,("reply_nttrans: data malloc fail for %u "
2956                                  "bytes !\n", (unsigned int)state->total_data));
2957                         TALLOC_FREE(state);
2958                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2959                         END_PROFILE(SMBnttrans);
2960                         return;
2961                 }
2962
2963                 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2964         }
2965
2966         if (state->total_param) {
2967
2968                 if (trans_oob(state->total_param, 0, pscnt)
2969                     || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2970                         goto bad_param;
2971                 }
2972
2973                 /* Can't use talloc here, the core routines do realloc on the
2974                  * params and data. */
2975                 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2976                         DEBUG(0,("reply_nttrans: param malloc fail for %u "
2977                                  "bytes !\n", (unsigned int)state->total_param));
2978                         SAFE_FREE(state->data);
2979                         TALLOC_FREE(state);
2980                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2981                         END_PROFILE(SMBnttrans);
2982                         return;
2983                 }
2984
2985                 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2986         }
2987
2988         state->received_data  = dscnt;
2989         state->received_param = pscnt;
2990
2991         if(state->setup_count > 0) {
2992                 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2993                           state->setup_count));
2994
2995                 /*
2996                  * No overflow possible here, state->setup_count is an
2997                  * unsigned int, being filled by a single byte from
2998                  * CVAL(req->vwv+13, 0) above. The cast in the comparison
2999                  * below is not necessary, it's here to clarify things. The
3000                  * validity of req->vwv and req->wct has been checked in
3001                  * init_smb_request already.
3002                  */
3003                 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
3004                         goto bad_param;
3005                 }
3006
3007                 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3008                 if (state->setup == NULL) {
3009                         DEBUG(0,("reply_nttrans : Out of memory\n"));
3010                         SAFE_FREE(state->data);
3011                         SAFE_FREE(state->param);
3012                         TALLOC_FREE(state);
3013                         reply_nterror(req, NT_STATUS_NO_MEMORY);
3014                         END_PROFILE(SMBnttrans);
3015                         return;
3016                 }
3017
3018                 memcpy(state->setup, req->vwv+19, state->setup_count);
3019                 dump_data(10, (uint8 *)state->setup, state->setup_count);
3020         }
3021
3022         if ((state->received_data == state->total_data) &&
3023             (state->received_param == state->total_param)) {
3024                 handle_nttrans(conn, state, req);
3025                 SAFE_FREE(state->param);
3026                 SAFE_FREE(state->data);
3027                 TALLOC_FREE(state);
3028                 END_PROFILE(SMBnttrans);
3029                 return;
3030         }
3031
3032         DLIST_ADD(conn->pending_trans, state);
3033
3034         /* We need to send an interim response then receive the rest
3035            of the parameter/data bytes */
3036         reply_outbuf(req, 0, 0);
3037         show_msg((char *)req->outbuf);
3038         END_PROFILE(SMBnttrans);
3039         return;
3040
3041   bad_param:
3042
3043         DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3044         SAFE_FREE(state->data);
3045         SAFE_FREE(state->param);
3046         TALLOC_FREE(state);
3047         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3048         END_PROFILE(SMBnttrans);
3049         return;
3050 }
3051
3052 /****************************************************************************
3053  Reply to a SMBnttranss
3054  ****************************************************************************/
3055
3056 void reply_nttranss(struct smb_request *req)
3057 {
3058         connection_struct *conn = req->conn;
3059         uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
3060         struct trans_state *state;
3061
3062         START_PROFILE(SMBnttranss);
3063
3064         show_msg((char *)req->inbuf);
3065
3066         /* Windows clients expect all replies to
3067            an NT transact secondary (SMBnttranss 0xA1)
3068            to have a command code of NT transact
3069            (SMBnttrans 0xA0). See bug #8989 for details. */
3070         req->cmd = SMBnttrans;
3071
3072         if (req->wct < 18) {
3073                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3074                 END_PROFILE(SMBnttranss);
3075                 return;
3076         }
3077
3078         for (state = conn->pending_trans; state != NULL;
3079              state = state->next) {
3080                 if (state->mid == req->mid) {
3081                         break;
3082                 }
3083         }
3084
3085         if ((state == NULL) || (state->cmd != SMBnttrans)) {
3086                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3087                 END_PROFILE(SMBnttranss);
3088                 return;
3089         }
3090
3091         /* Revise state->total_param and state->total_data in case they have
3092            changed downwards */
3093         if (IVAL(req->vwv+1, 1) < state->total_param) {
3094                 state->total_param = IVAL(req->vwv+1, 1);
3095         }
3096         if (IVAL(req->vwv+3, 1) < state->total_data) {
3097                 state->total_data = IVAL(req->vwv+3, 1);
3098         }
3099
3100         pcnt = IVAL(req->vwv+5, 1);
3101         poff = IVAL(req->vwv+7, 1);
3102         pdisp = IVAL(req->vwv+9, 1);
3103
3104         dcnt = IVAL(req->vwv+11, 1);
3105         doff = IVAL(req->vwv+13, 1);
3106         ddisp = IVAL(req->vwv+15, 1);
3107
3108         state->received_param += pcnt;
3109         state->received_data += dcnt;
3110
3111         if ((state->received_data > state->total_data) ||
3112             (state->received_param > state->total_param))
3113                 goto bad_param;
3114
3115         if (pcnt) {
3116                 if (trans_oob(state->total_param, pdisp, pcnt)
3117                     || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3118                         goto bad_param;
3119                 }
3120                 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3121         }
3122
3123         if (dcnt) {
3124                 if (trans_oob(state->total_data, ddisp, dcnt)
3125                     || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3126                         goto bad_param;
3127                 }
3128                 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3129         }
3130
3131         if ((state->received_param < state->total_param) ||
3132             (state->received_data < state->total_data)) {
3133                 END_PROFILE(SMBnttranss);
3134                 return;
3135         }
3136
3137         handle_nttrans(conn, state, req);
3138
3139         DLIST_REMOVE(conn->pending_trans, state);
3140         SAFE_FREE(state->data);
3141         SAFE_FREE(state->param);
3142         TALLOC_FREE(state);
3143         END_PROFILE(SMBnttranss);
3144         return;
3145
3146   bad_param:
3147
3148         DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3149         DLIST_REMOVE(conn->pending_trans, state);
3150         SAFE_FREE(state->data);
3151         SAFE_FREE(state->param);
3152         TALLOC_FREE(state);
3153         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3154         END_PROFILE(SMBnttranss);
3155         return;
3156 }