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