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