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