s3:smbd: make change_notify_reply() more generic and hide send_nt_replies() behind...
[ira/wip.git] / source3 / smbd / nttrans.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB NT transaction handling
4    Copyright (C) Jeremy Allison                 1994-2007
5    Copyright (C) Stefan (metze) Metzmacher      2003
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/globals.h"
23
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 smbd_smb1_notify_reply(struct smb_request *req,
1521                                    NTSTATUS error_code,
1522                                    uint8_t *buf, size_t len)
1523 {
1524         send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1525 }
1526
1527 static void call_nt_transact_notify_change(connection_struct *conn,
1528                                            struct smb_request *req,
1529                                            uint16 **ppsetup,
1530                                            uint32 setup_count,
1531                                            char **ppparams,
1532                                            uint32 parameter_count,
1533                                            char **ppdata, uint32 data_count,
1534                                            uint32 max_data_count,
1535                                            uint32 max_param_count)
1536 {
1537         uint16 *setup = *ppsetup;
1538         files_struct *fsp;
1539         uint32 filter;
1540         NTSTATUS status;
1541         bool recursive;
1542
1543         if(setup_count < 6) {
1544                 reply_doserror(req, ERRDOS, ERRbadfunc);
1545                 return;
1546         }
1547
1548         fsp = file_fsp(req, SVAL(setup,4));
1549         filter = IVAL(setup, 0);
1550         recursive = (SVAL(setup, 6) != 0) ? True : False;
1551
1552         DEBUG(3,("call_nt_transact_notify_change\n"));
1553
1554         if(!fsp) {
1555                 reply_doserror(req, ERRDOS, ERRbadfid);
1556                 return;
1557         }
1558
1559         {
1560                 char *filter_string;
1561
1562                 if (!(filter_string = notify_filter_string(NULL, filter))) {
1563                         reply_nterror(req,NT_STATUS_NO_MEMORY);
1564                         return;
1565                 }
1566
1567                 DEBUG(3,("call_nt_transact_notify_change: notify change "
1568                          "called on %s, filter = %s, recursive = %d\n",
1569                          fsp->fsp_name, filter_string, recursive));
1570
1571                 TALLOC_FREE(filter_string);
1572         }
1573
1574         if((!fsp->is_directory) || (conn != fsp->conn)) {
1575                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1576                 return;
1577         }
1578
1579         if (fsp->notify == NULL) {
1580
1581                 status = change_notify_create(fsp, filter, recursive);
1582
1583                 if (!NT_STATUS_IS_OK(status)) {
1584                         DEBUG(10, ("change_notify_create returned %s\n",
1585                                    nt_errstr(status)));
1586                         reply_nterror(req, status);
1587                         return;
1588                 }
1589         }
1590
1591         if (fsp->notify->num_changes != 0) {
1592
1593                 /*
1594                  * We've got changes pending, respond immediately
1595                  */
1596
1597                 /*
1598                  * TODO: write a torture test to check the filtering behaviour
1599                  * here.
1600                  */
1601
1602                 change_notify_reply(fsp->conn, req,
1603                                     NT_STATUS_OK,
1604                                     max_param_count,
1605                                     fsp->notify,
1606                                     smbd_smb1_notify_reply);
1607
1608                 /*
1609                  * change_notify_reply() above has independently sent its
1610                  * results
1611                  */
1612                 return;
1613         }
1614
1615         /*
1616          * No changes pending, queue the request
1617          */
1618
1619         status = change_notify_add_request(req,
1620                         max_param_count,
1621                         filter,
1622                         recursive, fsp,
1623                         smbd_smb1_notify_reply);
1624         if (!NT_STATUS_IS_OK(status)) {
1625                 reply_nterror(req, status);
1626         }
1627         return;
1628 }
1629
1630 /****************************************************************************
1631  Reply to an NT transact rename command.
1632 ****************************************************************************/
1633
1634 static void call_nt_transact_rename(connection_struct *conn,
1635                                     struct smb_request *req,
1636                                     uint16 **ppsetup, uint32 setup_count,
1637                                     char **ppparams, uint32 parameter_count,
1638                                     char **ppdata, uint32 data_count,
1639                                     uint32 max_data_count)
1640 {
1641         char *params = *ppparams;
1642         char *new_name = NULL;
1643         files_struct *fsp = NULL;
1644         bool dest_has_wcard = False;
1645         NTSTATUS status;
1646         TALLOC_CTX *ctx = talloc_tos();
1647
1648         if(parameter_count < 5) {
1649                 reply_doserror(req, ERRDOS, ERRbadfunc);
1650                 return;
1651         }
1652
1653         fsp = file_fsp(req, SVAL(params, 0));
1654         if (!check_fsp(conn, req, fsp)) {
1655                 return;
1656         }
1657         srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1658                               parameter_count - 4,
1659                               STR_TERMINATE, &status, &dest_has_wcard);
1660         if (!NT_STATUS_IS_OK(status)) {
1661                 reply_nterror(req, status);
1662                 return;
1663         }
1664
1665         /*
1666          * W2K3 ignores this request as the RAW-RENAME test
1667          * demonstrates, so we do.
1668          */
1669         send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1670
1671         DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1672                  fsp->fsp_name, new_name));
1673
1674         return;
1675 }
1676
1677 /******************************************************************************
1678  Fake up a completely empty SD.
1679 *******************************************************************************/
1680
1681 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1682 {
1683         size_t sd_size;
1684
1685         *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1686         if(!*ppsd) {
1687                 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1688                 return NT_STATUS_NO_MEMORY;
1689         }
1690
1691         return NT_STATUS_OK;
1692 }
1693
1694 /****************************************************************************
1695  Reply to query a security descriptor.
1696 ****************************************************************************/
1697
1698 static void call_nt_transact_query_security_desc(connection_struct *conn,
1699                                                  struct smb_request *req,
1700                                                  uint16 **ppsetup,
1701                                                  uint32 setup_count,
1702                                                  char **ppparams,
1703                                                  uint32 parameter_count,
1704                                                  char **ppdata,
1705                                                  uint32 data_count,
1706                                                  uint32 max_data_count)
1707 {
1708         char *params = *ppparams;
1709         char *data = *ppdata;
1710         SEC_DESC *psd = NULL;
1711         size_t sd_size;
1712         uint32 security_info_wanted;
1713         files_struct *fsp = NULL;
1714         NTSTATUS status;
1715         DATA_BLOB blob;
1716
1717         if(parameter_count < 8) {
1718                 reply_doserror(req, ERRDOS, ERRbadfunc);
1719                 return;
1720         }
1721
1722         fsp = file_fsp(req, SVAL(params,0));
1723         if(!fsp) {
1724                 reply_doserror(req, ERRDOS, ERRbadfid);
1725                 return;
1726         }
1727
1728         security_info_wanted = IVAL(params,4);
1729
1730         DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1731                         (unsigned int)security_info_wanted ));
1732
1733         params = nttrans_realloc(ppparams, 4);
1734         if(params == NULL) {
1735                 reply_doserror(req, ERRDOS, ERRnomem);
1736                 return;
1737         }
1738
1739         /*
1740          * Get the permissions to return.
1741          */
1742
1743         if (!lp_nt_acl_support(SNUM(conn))) {
1744                 status = get_null_nt_acl(talloc_tos(), &psd);
1745         } else {
1746                 status = SMB_VFS_FGET_NT_ACL(
1747                         fsp, security_info_wanted, &psd);
1748         }
1749         if (!NT_STATUS_IS_OK(status)) {
1750                 reply_nterror(req, status);
1751                 return;
1752         }
1753
1754         /* If the SACL/DACL is NULL, but was requested, we mark that it is
1755          * present in the reply to match Windows behavior */
1756         if (psd->sacl == NULL &&
1757             security_info_wanted & SACL_SECURITY_INFORMATION)
1758                 psd->type |= SEC_DESC_SACL_PRESENT;
1759         if (psd->dacl == NULL &&
1760             security_info_wanted & DACL_SECURITY_INFORMATION)
1761                 psd->type |= SEC_DESC_DACL_PRESENT;
1762
1763         sd_size = ndr_size_security_descriptor(psd, NULL, 0);
1764
1765         DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1766
1767         if (DEBUGLEVEL >= 10) {
1768                 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n", fsp->fsp_name));
1769                 NDR_PRINT_DEBUG(security_descriptor, psd);
1770         }
1771
1772         SIVAL(params,0,(uint32)sd_size);
1773
1774         if (max_data_count < sd_size) {
1775                 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1776                                 params, 4, *ppdata, 0);
1777                 return;
1778         }
1779
1780         /*
1781          * Allocate the data we will point this at.
1782          */
1783
1784         data = nttrans_realloc(ppdata, sd_size);
1785         if(data == NULL) {
1786                 reply_doserror(req, ERRDOS, ERRnomem);
1787                 return;
1788         }
1789
1790         status = marshall_sec_desc(talloc_tos(), psd,
1791                                    &blob.data, &blob.length);
1792
1793         if (!NT_STATUS_IS_OK(status)) {
1794                 reply_nterror(req, status);
1795                 return;
1796         }
1797
1798         SMB_ASSERT(sd_size == blob.length);
1799         memcpy(data, blob.data, sd_size);
1800
1801         send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1802
1803         return;
1804 }
1805
1806 /****************************************************************************
1807  Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1808 ****************************************************************************/
1809
1810 static void call_nt_transact_set_security_desc(connection_struct *conn,
1811                                                struct smb_request *req,
1812                                                uint16 **ppsetup,
1813                                                uint32 setup_count,
1814                                                char **ppparams,
1815                                                uint32 parameter_count,
1816                                                char **ppdata,
1817                                                uint32 data_count,
1818                                                uint32 max_data_count)
1819 {
1820         char *params= *ppparams;
1821         char *data = *ppdata;
1822         files_struct *fsp = NULL;
1823         uint32 security_info_sent = 0;
1824         NTSTATUS status;
1825
1826         if(parameter_count < 8) {
1827                 reply_doserror(req, ERRDOS, ERRbadfunc);
1828                 return;
1829         }
1830
1831         if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1832                 reply_doserror(req, ERRDOS, ERRbadfid);
1833                 return;
1834         }
1835
1836         if(!lp_nt_acl_support(SNUM(conn))) {
1837                 goto done;
1838         }
1839
1840         security_info_sent = IVAL(params,4);
1841
1842         DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1843                 (unsigned int)security_info_sent ));
1844
1845         if (data_count == 0) {
1846                 reply_doserror(req, ERRDOS, ERRnoaccess);
1847                 return;
1848         }
1849
1850         status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1851
1852         if (!NT_STATUS_IS_OK(status)) {
1853                 reply_nterror(req, status);
1854                 return;
1855         }
1856
1857   done:
1858         send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1859         return;
1860 }
1861
1862 /****************************************************************************
1863  Reply to NT IOCTL
1864 ****************************************************************************/
1865
1866 static void call_nt_transact_ioctl(connection_struct *conn,
1867                                    struct smb_request *req,
1868                                    uint16 **ppsetup, uint32 setup_count,
1869                                    char **ppparams, uint32 parameter_count,
1870                                    char **ppdata, uint32 data_count,
1871                                    uint32 max_data_count)
1872 {
1873         uint32 function;
1874         uint16 fidnum;
1875         files_struct *fsp;
1876         uint8 isFSctl;
1877         uint8 compfilter;
1878         char *pdata = *ppdata;
1879
1880         if (setup_count != 8) {
1881                 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1882                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1883                 return;
1884         }
1885
1886         function = IVAL(*ppsetup, 0);
1887         fidnum = SVAL(*ppsetup, 4);
1888         isFSctl = CVAL(*ppsetup, 6);
1889         compfilter = CVAL(*ppsetup, 7);
1890
1891         DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n", 
1892                  function, fidnum, isFSctl, compfilter));
1893
1894         fsp=file_fsp(req, fidnum);
1895         /* this check is done in each implemented function case for now
1896            because I don't want to break anything... --metze
1897         FSP_BELONGS_CONN(fsp,conn);*/
1898
1899         SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
1900
1901         switch (function) {
1902         case FSCTL_SET_SPARSE:
1903                 /* pretend this succeeded - tho strictly we should
1904                    mark the file sparse (if the local fs supports it)
1905                    so we can know if we need to pre-allocate or not */
1906
1907                 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1908                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1909                 return;
1910
1911         case FSCTL_CREATE_OR_GET_OBJECT_ID:
1912         {
1913                 unsigned char objid[16];
1914
1915                 /* This should return the object-id on this file.
1916                  * I think I'll make this be the inode+dev. JRA.
1917                  */
1918
1919                 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1920
1921                 if (!fsp_belongs_conn(conn, req, fsp)) {
1922                         return;
1923                 }
1924
1925                 data_count = 64;
1926                 pdata = nttrans_realloc(ppdata, data_count);
1927                 if (pdata == NULL) {
1928                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1929                         return;
1930                 }
1931
1932                 /* For backwards compatibility only store the dev/inode. */
1933                 push_file_id_16(pdata, &fsp->file_id);
1934                 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1935                 push_file_id_16(pdata+32, &fsp->file_id);
1936                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1937                                 pdata, data_count);
1938                 return;
1939         }
1940
1941         case FSCTL_GET_REPARSE_POINT:
1942                 /* pretend this fail - my winXP does it like this
1943                  * --metze
1944                  */
1945
1946                 DEBUG(10,("FSCTL_GET_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_SET_REPARSE_POINT:
1951                 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1952                  * --metze
1953                  */
1954
1955                 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1956                 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1957                 return;
1958
1959         case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1960         {
1961                 /*
1962                  * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1963                  * and return their volume names.  If max_data_count is 16, then it is just
1964                  * asking for the number of volumes and length of the combined names.
1965                  *
1966                  * pdata is the data allocated by our caller, but that uses
1967                  * total_data_count (which is 0 in our case) rather than max_data_count.
1968                  * Allocate the correct amount and return the pointer to let
1969                  * it be deallocated when we return.
1970                  */
1971                 SHADOW_COPY_DATA *shadow_data = NULL;
1972                 TALLOC_CTX *shadow_mem_ctx = NULL;
1973                 bool labels = False;
1974                 uint32 labels_data_count = 0;
1975                 uint32 i;
1976                 char *cur_pdata;
1977
1978                 if (!fsp_belongs_conn(conn, req, fsp)) {
1979                         return;
1980                 }
1981
1982                 if (max_data_count < 16) {
1983                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1984                                 max_data_count));
1985                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1986                         return;
1987                 }
1988
1989                 if (max_data_count > 16) {
1990                         labels = True;
1991                 }
1992
1993                 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1994                 if (shadow_mem_ctx == NULL) {
1995                         DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1996                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1997                         return;
1998                 }
1999
2000                 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2001                 if (shadow_data == NULL) {
2002                         DEBUG(0,("TALLOC_ZERO() failed!\n"));
2003                         talloc_destroy(shadow_mem_ctx);
2004                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2005                         return;
2006                 }
2007
2008                 shadow_data->mem_ctx = shadow_mem_ctx;
2009
2010                 /*
2011                  * Call the VFS routine to actually do the work.
2012                  */
2013                 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2014                         talloc_destroy(shadow_data->mem_ctx);
2015                         if (errno == ENOSYS) {
2016                                 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n", 
2017                                         conn->connectpath));
2018                                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2019                                 return;
2020                         } else {
2021                                 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n", 
2022                                         conn->connectpath));
2023                                 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2024                                 return;
2025                         }
2026                 }
2027
2028                 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2029
2030                 if (!labels) {
2031                         data_count = 16;
2032                 } else {
2033                         data_count = 12+labels_data_count+4;
2034                 }
2035
2036                 if (max_data_count<data_count) {
2037                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2038                                 max_data_count,data_count));
2039                         talloc_destroy(shadow_data->mem_ctx);
2040                         reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2041                         return;
2042                 }
2043
2044                 pdata = nttrans_realloc(ppdata, data_count);
2045                 if (pdata == NULL) {
2046                         talloc_destroy(shadow_data->mem_ctx);
2047                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2048                         return;
2049                 }
2050
2051                 cur_pdata = pdata;
2052
2053                 /* num_volumes 4 bytes */
2054                 SIVAL(pdata,0,shadow_data->num_volumes);
2055
2056                 if (labels) {
2057                         /* num_labels 4 bytes */
2058                         SIVAL(pdata,4,shadow_data->num_volumes);
2059                 }
2060
2061                 /* needed_data_count 4 bytes */
2062                 SIVAL(pdata,8,labels_data_count);
2063
2064                 cur_pdata+=12;
2065
2066                 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2067                         shadow_data->num_volumes,fsp->fsp_name));
2068                 if (labels && shadow_data->labels) {
2069                         for (i=0;i<shadow_data->num_volumes;i++) {
2070                                 srvstr_push(pdata, req->flags2,
2071                                             cur_pdata, shadow_data->labels[i],
2072                                             2*sizeof(SHADOW_COPY_LABEL),
2073                                             STR_UNICODE|STR_TERMINATE);
2074                                 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2075                                 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2076                         }
2077                 }
2078
2079                 talloc_destroy(shadow_data->mem_ctx);
2080
2081                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2082                                 pdata, data_count);
2083
2084                 return;
2085         }
2086
2087         case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2088         {
2089                 /* pretend this succeeded -
2090                  *
2091                  * we have to send back a list with all files owned by this SID
2092                  *
2093                  * but I have to check that --metze
2094                  */
2095                 DOM_SID sid;
2096                 uid_t uid;
2097                 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2098
2099                 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2100
2101                 if (!fsp_belongs_conn(conn, req, fsp)) {
2102                         return;
2103                 }
2104
2105                 /* unknown 4 bytes: this is not the length of the sid :-(  */
2106                 /*unknown = IVAL(pdata,0);*/
2107
2108                 sid_parse(pdata+4,sid_len,&sid);
2109                 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2110
2111                 if (!sid_to_uid(&sid, &uid)) {
2112                         DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2113                                  sid_string_dbg(&sid),
2114                                  (unsigned long)sid_len));
2115                         uid = (-1);
2116                 }
2117
2118                 /* we can take a look at the find source :-)
2119                  *
2120                  * find ./ -uid $uid  -name '*'   is what we need here
2121                  *
2122                  *
2123                  * and send 4bytes len and then NULL terminated unicode strings
2124                  * for each file
2125                  *
2126                  * but I don't know how to deal with the paged results
2127                  * (maybe we can hang the result anywhere in the fsp struct)
2128                  *
2129                  * we don't send all files at once
2130                  * and at the next we should *not* start from the beginning,
2131                  * so we have to cache the result
2132                  *
2133                  * --metze
2134                  */
2135
2136                 /* this works for now... */
2137                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2138                 return;
2139         }
2140         default:
2141                 if (!logged_ioctl_message) {
2142                         logged_ioctl_message = true; /* Only print this once... */
2143                         DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2144                                  function));
2145                 }
2146         }
2147
2148         reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2149 }
2150
2151
2152 #ifdef HAVE_SYS_QUOTAS
2153 /****************************************************************************
2154  Reply to get user quota
2155 ****************************************************************************/
2156
2157 static void call_nt_transact_get_user_quota(connection_struct *conn,
2158                                             struct smb_request *req,
2159                                             uint16 **ppsetup,
2160                                             uint32 setup_count,
2161                                             char **ppparams,
2162                                             uint32 parameter_count,
2163                                             char **ppdata,
2164                                             uint32 data_count,
2165                                             uint32 max_data_count)
2166 {
2167         NTSTATUS nt_status = NT_STATUS_OK;
2168         char *params = *ppparams;
2169         char *pdata = *ppdata;
2170         char *entry;
2171         int data_len=0,param_len=0;
2172         int qt_len=0;
2173         int entry_len = 0;
2174         files_struct *fsp = NULL;
2175         uint16 level = 0;
2176         size_t sid_len;
2177         DOM_SID sid;
2178         bool start_enum = True;
2179         SMB_NTQUOTA_STRUCT qt;
2180         SMB_NTQUOTA_LIST *tmp_list;
2181         SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2182
2183         ZERO_STRUCT(qt);
2184
2185         /* access check */
2186         if (conn->server_info->utok.uid != 0) {
2187                 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2188                          "[%s]\n", lp_servicename(SNUM(conn)),
2189                          conn->server_info->unix_name));
2190                 reply_doserror(req, ERRDOS, ERRnoaccess);
2191                 return;
2192         }
2193
2194         /*
2195          * Ensure minimum number of parameters sent.
2196          */
2197
2198         if (parameter_count < 4) {
2199                 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2200                 reply_doserror(req, ERRDOS, ERRinvalidparam);
2201                 return;
2202         }
2203
2204         /* maybe we can check the quota_fnum */
2205         fsp = file_fsp(req, SVAL(params,0));
2206         if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2207                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2208                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2209                 return;
2210         }
2211
2212         /* the NULL pointer checking for fsp->fake_file_handle->pd
2213          * is done by CHECK_NTQUOTA_HANDLE_OK()
2214          */
2215         qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2216
2217         level = SVAL(params,2);
2218
2219         /* unknown 12 bytes leading in params */
2220
2221         switch (level) {
2222                 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2223                         /* seems that we should continue with the enum here --metze */
2224
2225                         if (qt_handle->quota_list!=NULL &&
2226                             qt_handle->tmp_list==NULL) {
2227
2228                                 /* free the list */
2229                                 free_ntquota_list(&(qt_handle->quota_list));
2230
2231                                 /* Realloc the size of parameters and data we will return */
2232                                 param_len = 4;
2233                                 params = nttrans_realloc(ppparams, param_len);
2234                                 if(params == NULL) {
2235                                         reply_doserror(req, ERRDOS, ERRnomem);
2236                                         return;
2237                                 }
2238
2239                                 data_len = 0;
2240                                 SIVAL(params,0,data_len);
2241
2242                                 break;
2243                         }
2244
2245                         start_enum = False;
2246
2247                 case TRANSACT_GET_USER_QUOTA_LIST_START:
2248
2249                         if (qt_handle->quota_list==NULL &&
2250                                 qt_handle->tmp_list==NULL) {
2251                                 start_enum = True;
2252                         }
2253
2254                         if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2255                                 reply_doserror(req, ERRSRV, ERRerror);
2256                                 return;
2257                         }
2258
2259                         /* Realloc the size of parameters and data we will return */
2260                         param_len = 4;
2261                         params = nttrans_realloc(ppparams, param_len);
2262                         if(params == NULL) {
2263                                 reply_doserror(req, ERRDOS, ERRnomem);
2264                                 return;
2265                         }
2266
2267                         /* we should not trust the value in max_data_count*/
2268                         max_data_count = MIN(max_data_count,2048);
2269
2270                         pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2271                         if(pdata == NULL) {
2272                                 reply_doserror(req, ERRDOS, ERRnomem);
2273                                 return;
2274                         }
2275
2276                         entry = pdata;
2277
2278                         /* set params Size of returned Quota Data 4 bytes*/
2279                         /* but set it later when we know it */
2280
2281                         /* for each entry push the data */
2282
2283                         if (start_enum) {
2284                                 qt_handle->tmp_list = qt_handle->quota_list;
2285                         }
2286
2287                         tmp_list = qt_handle->tmp_list;
2288
2289                         for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2290                                 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2291
2292                                 sid_len = ndr_size_dom_sid(
2293                                         &tmp_list->quotas->sid, NULL, 0);
2294                                 entry_len = 40 + sid_len;
2295
2296                                 /* nextoffset entry 4 bytes */
2297                                 SIVAL(entry,0,entry_len);
2298
2299                                 /* then the len of the SID 4 bytes */
2300                                 SIVAL(entry,4,sid_len);
2301
2302                                 /* unknown data 8 bytes uint64_t */
2303                                 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2304
2305                                 /* the used disk space 8 bytes uint64_t */
2306                                 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2307
2308                                 /* the soft quotas 8 bytes uint64_t */
2309                                 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2310
2311                                 /* the hard quotas 8 bytes uint64_t */
2312                                 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2313
2314                                 /* and now the SID */
2315                                 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2316                         }
2317
2318                         qt_handle->tmp_list = tmp_list;
2319
2320                         /* overwrite the offset of the last entry */
2321                         SIVAL(entry-entry_len,0,0);
2322
2323                         data_len = 4+qt_len;
2324                         /* overwrite the params quota_data_len */
2325                         SIVAL(params,0,data_len);
2326
2327                         break;
2328
2329                 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2330
2331                         /* unknown 4 bytes IVAL(pdata,0) */
2332
2333                         if (data_count < 8) {
2334                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2335                                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2336                                 return;
2337                         }
2338
2339                         sid_len = IVAL(pdata,4);
2340                         /* Ensure this is less than 1mb. */
2341                         if (sid_len > (1024*1024)) {
2342                                 reply_doserror(req, ERRDOS, ERRnomem);
2343                                 return;
2344                         }
2345
2346                         if (data_count < 8+sid_len) {
2347                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2348                                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2349                                 return;
2350                         }
2351
2352                         data_len = 4+40+sid_len;
2353
2354                         if (max_data_count < data_len) {
2355                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2356                                         max_data_count, data_len));
2357                                 param_len = 4;
2358                                 SIVAL(params,0,data_len);
2359                                 data_len = 0;
2360                                 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2361                                 break;
2362                         }
2363
2364                         sid_parse(pdata+8,sid_len,&sid);
2365
2366                         if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2367                                 ZERO_STRUCT(qt);
2368                                 /*
2369                                  * we have to return zero's in all fields
2370                                  * instead of returning an error here
2371                                  * --metze
2372                                  */
2373                         }
2374
2375                         /* Realloc the size of parameters and data we will return */
2376                         param_len = 4;
2377                         params = nttrans_realloc(ppparams, param_len);
2378                         if(params == NULL) {
2379                                 reply_doserror(req, ERRDOS, ERRnomem);
2380                                 return;
2381                         }
2382
2383                         pdata = nttrans_realloc(ppdata, data_len);
2384                         if(pdata == NULL) {
2385                                 reply_doserror(req, ERRDOS, ERRnomem);
2386                                 return;
2387                         }
2388
2389                         entry = pdata;
2390
2391                         /* set params Size of returned Quota Data 4 bytes*/
2392                         SIVAL(params,0,data_len);
2393
2394                         /* nextoffset entry 4 bytes */
2395                         SIVAL(entry,0,0);
2396
2397                         /* then the len of the SID 4 bytes */
2398                         SIVAL(entry,4,sid_len);
2399
2400                         /* unknown data 8 bytes uint64_t */
2401                         SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2402
2403                         /* the used disk space 8 bytes uint64_t */
2404                         SBIG_UINT(entry,16,qt.usedspace);
2405
2406                         /* the soft quotas 8 bytes uint64_t */
2407                         SBIG_UINT(entry,24,qt.softlim);
2408
2409                         /* the hard quotas 8 bytes uint64_t */
2410                         SBIG_UINT(entry,32,qt.hardlim);
2411
2412                         /* and now the SID */
2413                         sid_linearize(entry+40, sid_len, &sid);
2414
2415                         break;
2416
2417                 default:
2418                         DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2419                         reply_doserror(req, ERRSRV, ERRerror);
2420                         return;
2421                         break;
2422         }
2423
2424         send_nt_replies(conn, req, nt_status, params, param_len,
2425                         pdata, data_len);
2426 }
2427
2428 /****************************************************************************
2429  Reply to set user quota
2430 ****************************************************************************/
2431
2432 static void call_nt_transact_set_user_quota(connection_struct *conn,
2433                                             struct smb_request *req,
2434                                             uint16 **ppsetup,
2435                                             uint32 setup_count,
2436                                             char **ppparams,
2437                                             uint32 parameter_count,
2438                                             char **ppdata,
2439                                             uint32 data_count,
2440                                             uint32 max_data_count)
2441 {
2442         char *params = *ppparams;
2443         char *pdata = *ppdata;
2444         int data_len=0,param_len=0;
2445         SMB_NTQUOTA_STRUCT qt;
2446         size_t sid_len;
2447         DOM_SID sid;
2448         files_struct *fsp = NULL;
2449
2450         ZERO_STRUCT(qt);
2451
2452         /* access check */
2453         if (conn->server_info->utok.uid != 0) {
2454                 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2455                          "[%s]\n", lp_servicename(SNUM(conn)),
2456                          conn->server_info->unix_name));
2457                 reply_doserror(req, ERRDOS, ERRnoaccess);
2458                 return;
2459         }
2460
2461         /*
2462          * Ensure minimum number of parameters sent.
2463          */
2464
2465         if (parameter_count < 2) {
2466                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2467                 reply_doserror(req, ERRDOS, ERRinvalidparam);
2468                 return;
2469         }
2470
2471         /* maybe we can check the quota_fnum */
2472         fsp = file_fsp(req, SVAL(params,0));
2473         if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2474                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2475                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2476                 return;
2477         }
2478
2479         if (data_count < 40) {
2480                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2481                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2482                 return;
2483         }
2484
2485         /* offset to next quota record.
2486          * 4 bytes IVAL(pdata,0)
2487          * unused here...
2488          */
2489
2490         /* sid len */
2491         sid_len = IVAL(pdata,4);
2492
2493         if (data_count < 40+sid_len) {
2494                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2495                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2496                 return;
2497         }
2498
2499         /* unknown 8 bytes in pdata
2500          * maybe its the change time in NTTIME
2501          */
2502
2503         /* the used space 8 bytes (uint64_t)*/
2504         qt.usedspace = (uint64_t)IVAL(pdata,16);
2505 #ifdef LARGE_SMB_OFF_T
2506         qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2507 #else /* LARGE_SMB_OFF_T */
2508         if ((IVAL(pdata,20) != 0)&&
2509                 ((qt.usedspace != 0xFFFFFFFF)||
2510                 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2511                 /* more than 32 bits? */
2512                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2513                 return;
2514         }
2515 #endif /* LARGE_SMB_OFF_T */
2516
2517         /* the soft quotas 8 bytes (uint64_t)*/
2518         qt.softlim = (uint64_t)IVAL(pdata,24);
2519 #ifdef LARGE_SMB_OFF_T
2520         qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2521 #else /* LARGE_SMB_OFF_T */
2522         if ((IVAL(pdata,28) != 0)&&
2523                 ((qt.softlim != 0xFFFFFFFF)||
2524                 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2525                 /* more than 32 bits? */
2526                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2527                 return;
2528         }
2529 #endif /* LARGE_SMB_OFF_T */
2530
2531         /* the hard quotas 8 bytes (uint64_t)*/
2532         qt.hardlim = (uint64_t)IVAL(pdata,32);
2533 #ifdef LARGE_SMB_OFF_T
2534         qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2535 #else /* LARGE_SMB_OFF_T */
2536         if ((IVAL(pdata,36) != 0)&&
2537                 ((qt.hardlim != 0xFFFFFFFF)||
2538                 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2539                 /* more than 32 bits? */
2540                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2541                 return;
2542         }
2543 #endif /* LARGE_SMB_OFF_T */
2544
2545         sid_parse(pdata+40,sid_len,&sid);
2546         DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2547
2548         /* 44 unknown bytes left... */
2549
2550         if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2551                 reply_doserror(req, ERRSRV, ERRerror);
2552                 return;
2553         }
2554
2555         send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2556                         pdata, data_len);
2557 }
2558 #endif /* HAVE_SYS_QUOTAS */
2559
2560 static void handle_nttrans(connection_struct *conn,
2561                            struct trans_state *state,
2562                            struct smb_request *req)
2563 {
2564         if (Protocol >= PROTOCOL_NT1) {
2565                 req->flags2 |= 0x40; /* IS_LONG_NAME */
2566                 SSVAL(req->inbuf,smb_flg2,req->flags2);
2567         }
2568
2569
2570         SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2571
2572         /* Now we must call the relevant NT_TRANS function */
2573         switch(state->call) {
2574                 case NT_TRANSACT_CREATE:
2575                 {
2576                         START_PROFILE(NT_transact_create);
2577                         call_nt_transact_create(
2578                                 conn, req,
2579                                 &state->setup, state->setup_count,
2580                                 &state->param, state->total_param,
2581                                 &state->data, state->total_data,
2582                                 state->max_data_return);
2583                         END_PROFILE(NT_transact_create);
2584                         break;
2585                 }
2586
2587                 case NT_TRANSACT_IOCTL:
2588                 {
2589                         START_PROFILE(NT_transact_ioctl);
2590                         call_nt_transact_ioctl(
2591                                 conn, req,
2592                                 &state->setup, state->setup_count,
2593                                 &state->param, state->total_param,
2594                                 &state->data, state->total_data,
2595                                 state->max_data_return);
2596                         END_PROFILE(NT_transact_ioctl);
2597                         break;
2598                 }
2599
2600                 case NT_TRANSACT_SET_SECURITY_DESC:
2601                 {
2602                         START_PROFILE(NT_transact_set_security_desc);
2603                         call_nt_transact_set_security_desc(
2604                                 conn, req,
2605                                 &state->setup, state->setup_count,
2606                                 &state->param, state->total_param,
2607                                 &state->data, state->total_data,
2608                                 state->max_data_return);
2609                         END_PROFILE(NT_transact_set_security_desc);
2610                         break;
2611                 }
2612
2613                 case NT_TRANSACT_NOTIFY_CHANGE:
2614                 {
2615                         START_PROFILE(NT_transact_notify_change);
2616                         call_nt_transact_notify_change(
2617                                 conn, req,
2618                                 &state->setup, state->setup_count,
2619                                 &state->param, state->total_param,
2620                                 &state->data, state->total_data,
2621                                 state->max_data_return,
2622                                 state->max_param_return);
2623                         END_PROFILE(NT_transact_notify_change);
2624                         break;
2625                 }
2626
2627                 case NT_TRANSACT_RENAME:
2628                 {
2629                         START_PROFILE(NT_transact_rename);
2630                         call_nt_transact_rename(
2631                                 conn, req,
2632                                 &state->setup, state->setup_count,
2633                                 &state->param, state->total_param,
2634                                 &state->data, state->total_data,
2635                                 state->max_data_return);
2636                         END_PROFILE(NT_transact_rename);
2637                         break;
2638                 }
2639
2640                 case NT_TRANSACT_QUERY_SECURITY_DESC:
2641                 {
2642                         START_PROFILE(NT_transact_query_security_desc);
2643                         call_nt_transact_query_security_desc(
2644                                 conn, req,
2645                                 &state->setup, state->setup_count,
2646                                 &state->param, state->total_param,
2647                                 &state->data, state->total_data,
2648                                 state->max_data_return);
2649                         END_PROFILE(NT_transact_query_security_desc);
2650                         break;
2651                 }
2652
2653 #ifdef HAVE_SYS_QUOTAS
2654                 case NT_TRANSACT_GET_USER_QUOTA:
2655                 {
2656                         START_PROFILE(NT_transact_get_user_quota);
2657                         call_nt_transact_get_user_quota(
2658                                 conn, req,
2659                                 &state->setup, state->setup_count,
2660                                 &state->param, state->total_param,
2661                                 &state->data, state->total_data,
2662                                 state->max_data_return);
2663                         END_PROFILE(NT_transact_get_user_quota);
2664                         break;
2665                 }
2666
2667                 case NT_TRANSACT_SET_USER_QUOTA:
2668                 {
2669                         START_PROFILE(NT_transact_set_user_quota);
2670                         call_nt_transact_set_user_quota(
2671                                 conn, req,
2672                                 &state->setup, state->setup_count,
2673                                 &state->param, state->total_param,
2674                                 &state->data, state->total_data,
2675                                 state->max_data_return);
2676                         END_PROFILE(NT_transact_set_user_quota);
2677                         break;
2678                 }
2679 #endif /* HAVE_SYS_QUOTAS */
2680
2681                 default:
2682                         /* Error in request */
2683                         DEBUG(0,("handle_nttrans: Unknown request %d in "
2684                                  "nttrans call\n", state->call));
2685                         reply_doserror(req, ERRSRV, ERRerror);
2686                         return;
2687         }
2688         return;
2689 }
2690
2691 /****************************************************************************
2692  Reply to a SMBNTtrans.
2693 ****************************************************************************/
2694
2695 void reply_nttrans(struct smb_request *req)
2696 {
2697         connection_struct *conn = req->conn;
2698         uint32_t pscnt;
2699         uint32_t psoff;
2700         uint32_t dscnt;
2701         uint32_t dsoff;
2702         uint16 function_code;
2703         NTSTATUS result;
2704         struct trans_state *state;
2705
2706         START_PROFILE(SMBnttrans);
2707
2708         if (req->wct < 19) {
2709                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2710                 END_PROFILE(SMBnttrans);
2711                 return;
2712         }
2713
2714         pscnt = IVAL(req->vwv+9, 1);
2715         psoff = IVAL(req->vwv+11, 1);
2716         dscnt = IVAL(req->vwv+13, 1);
2717         dsoff = IVAL(req->vwv+15, 1);
2718         function_code = SVAL(req->vwv+18, 0);
2719
2720         if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2721                 reply_doserror(req, ERRSRV, ERRaccess);
2722                 END_PROFILE(SMBnttrans);
2723                 return;
2724         }
2725
2726         result = allow_new_trans(conn->pending_trans, req->mid);
2727         if (!NT_STATUS_IS_OK(result)) {
2728                 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2729                 reply_nterror(req, result);
2730                 END_PROFILE(SMBnttrans);
2731                 return;
2732         }
2733
2734         if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2735                 reply_doserror(req, ERRSRV, ERRaccess);
2736                 END_PROFILE(SMBnttrans);
2737                 return;
2738         }
2739
2740         state->cmd = SMBnttrans;
2741
2742         state->mid = req->mid;
2743         state->vuid = req->vuid;
2744         state->total_data = IVAL(req->vwv+3, 1);
2745         state->data = NULL;
2746         state->total_param = IVAL(req->vwv+1, 1);
2747         state->param = NULL;
2748         state->max_data_return = IVAL(req->vwv+7, 1);
2749         state->max_param_return = IVAL(req->vwv+5, 1);
2750
2751         /* setup count is in *words* */
2752         state->setup_count = 2*CVAL(req->vwv+17, 1);
2753         state->setup = NULL;
2754         state->call = function_code;
2755
2756         DEBUG(10, ("num_setup=%u, "
2757                    "param_total=%u, this_param=%u, max_param=%u, "
2758                    "data_total=%u, this_data=%u, max_data=%u, "
2759                    "param_offset=%u, data_offset=%u\n",
2760                    (unsigned)state->setup_count,
2761                    (unsigned)state->total_param, (unsigned)pscnt,
2762                    (unsigned)state->max_param_return,
2763                    (unsigned)state->total_data, (unsigned)dscnt,
2764                    (unsigned)state->max_data_return,
2765                    (unsigned)psoff, (unsigned)dsoff));
2766
2767         /*
2768          * All nttrans messages we handle have smb_wct == 19 +
2769          * state->setup_count.  Ensure this is so as a sanity check.
2770          */
2771
2772         if(req->wct != 19 + (state->setup_count/2)) {
2773                 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2774                          req->wct, 19 + (state->setup_count/2)));
2775                 goto bad_param;
2776         }
2777
2778         /* Don't allow more than 128mb for each value. */
2779         if ((state->total_data > (1024*1024*128)) ||
2780             (state->total_param > (1024*1024*128))) {
2781                 reply_doserror(req, ERRDOS, ERRnomem);
2782                 END_PROFILE(SMBnttrans);
2783                 return;
2784         }
2785
2786         if ((dscnt > state->total_data) || (pscnt > state->total_param))
2787                 goto bad_param;
2788
2789         if (state->total_data)  {
2790
2791                 if (trans_oob(state->total_data, 0, dscnt)
2792                     || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2793                         goto bad_param;
2794                 }
2795
2796                 /* Can't use talloc here, the core routines do realloc on the
2797                  * params and data. */
2798                 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2799                         DEBUG(0,("reply_nttrans: data malloc fail for %u "
2800                                  "bytes !\n", (unsigned int)state->total_data));
2801                         TALLOC_FREE(state);
2802                         reply_doserror(req, ERRDOS, ERRnomem);
2803                         END_PROFILE(SMBnttrans);
2804                         return;
2805                 }
2806
2807                 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2808         }
2809
2810         if (state->total_param) {
2811
2812                 if (trans_oob(state->total_param, 0, pscnt)
2813                     || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2814                         goto bad_param;
2815                 }
2816
2817                 /* Can't use talloc here, the core routines do realloc on the
2818                  * params and data. */
2819                 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2820                         DEBUG(0,("reply_nttrans: param malloc fail for %u "
2821                                  "bytes !\n", (unsigned int)state->total_param));
2822                         SAFE_FREE(state->data);
2823                         TALLOC_FREE(state);
2824                         reply_doserror(req, ERRDOS, ERRnomem);
2825                         END_PROFILE(SMBnttrans);
2826                         return;
2827                 }
2828
2829                 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2830         }
2831
2832         state->received_data  = dscnt;
2833         state->received_param = pscnt;
2834
2835         if(state->setup_count > 0) {
2836                 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2837                           state->setup_count));
2838
2839                 /*
2840                  * No overflow possible here, state->setup_count is an
2841                  * unsigned int, being filled by a single byte from
2842                  * CVAL(req->vwv+13, 0) above. The cast in the comparison
2843                  * below is not necessary, it's here to clarify things. The
2844                  * validity of req->vwv and req->wct has been checked in
2845                  * init_smb_request already.
2846                  */
2847                 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2848                         goto bad_param;
2849                 }
2850
2851                 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2852                 if (state->setup == NULL) {
2853                         DEBUG(0,("reply_nttrans : Out of memory\n"));
2854                         SAFE_FREE(state->data);
2855                         SAFE_FREE(state->param);
2856                         TALLOC_FREE(state);
2857                         reply_doserror(req, ERRDOS, ERRnomem);
2858                         END_PROFILE(SMBnttrans);
2859                         return;
2860                 }
2861
2862                 memcpy(state->setup, req->vwv+19, state->setup_count);
2863                 dump_data(10, (uint8 *)state->setup, state->setup_count);
2864         }
2865
2866         if ((state->received_data == state->total_data) &&
2867             (state->received_param == state->total_param)) {
2868                 handle_nttrans(conn, state, req);
2869                 SAFE_FREE(state->param);
2870                 SAFE_FREE(state->data);
2871                 TALLOC_FREE(state);
2872                 END_PROFILE(SMBnttrans);
2873                 return;
2874         }
2875
2876         DLIST_ADD(conn->pending_trans, state);
2877
2878         /* We need to send an interim response then receive the rest
2879            of the parameter/data bytes */
2880         reply_outbuf(req, 0, 0);
2881         show_msg((char *)req->outbuf);
2882         END_PROFILE(SMBnttrans);
2883         return;
2884
2885   bad_param:
2886
2887         DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2888         SAFE_FREE(state->data);
2889         SAFE_FREE(state->param);
2890         TALLOC_FREE(state);
2891         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2892         END_PROFILE(SMBnttrans);
2893         return;
2894 }
2895
2896 /****************************************************************************
2897  Reply to a SMBnttranss
2898  ****************************************************************************/
2899
2900 void reply_nttranss(struct smb_request *req)
2901 {
2902         connection_struct *conn = req->conn;
2903         uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2904         struct trans_state *state;
2905
2906         START_PROFILE(SMBnttranss);
2907
2908         show_msg((char *)req->inbuf);
2909
2910         if (req->wct < 18) {
2911                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2912                 END_PROFILE(SMBnttranss);
2913                 return;
2914         }
2915
2916         for (state = conn->pending_trans; state != NULL;
2917              state = state->next) {
2918                 if (state->mid == req->mid) {
2919                         break;
2920                 }
2921         }
2922
2923         if ((state == NULL) || (state->cmd != SMBnttrans)) {
2924                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2925                 END_PROFILE(SMBnttranss);
2926                 return;
2927         }
2928
2929         /* Revise state->total_param and state->total_data in case they have
2930            changed downwards */
2931         if (IVAL(req->vwv+1, 1) < state->total_param) {
2932                 state->total_param = IVAL(req->vwv+1, 1);
2933         }
2934         if (IVAL(req->vwv+3, 1) < state->total_data) {
2935                 state->total_data = IVAL(req->vwv+3, 1);
2936         }
2937
2938         pcnt = IVAL(req->vwv+5, 1);
2939         poff = IVAL(req->vwv+7, 1);
2940         pdisp = IVAL(req->vwv+9, 1);
2941
2942         dcnt = IVAL(req->vwv+11, 1);
2943         doff = IVAL(req->vwv+13, 1);
2944         ddisp = IVAL(req->vwv+15, 1);
2945
2946         state->received_param += pcnt;
2947         state->received_data += dcnt;
2948
2949         if ((state->received_data > state->total_data) ||
2950             (state->received_param > state->total_param))
2951                 goto bad_param;
2952
2953         if (pcnt) {
2954                 if (trans_oob(state->total_param, pdisp, pcnt)
2955                     || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
2956                         goto bad_param;
2957                 }
2958                 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
2959         }
2960
2961         if (dcnt) {
2962                 if (trans_oob(state->total_data, ddisp, dcnt)
2963                     || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
2964                         goto bad_param;
2965                 }
2966                 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
2967         }
2968
2969         if ((state->received_param < state->total_param) ||
2970             (state->received_data < state->total_data)) {
2971                 END_PROFILE(SMBnttranss);
2972                 return;
2973         }
2974
2975         handle_nttrans(conn, state, req);
2976
2977         DLIST_REMOVE(conn->pending_trans, state);
2978         SAFE_FREE(state->data);
2979         SAFE_FREE(state->param);
2980         TALLOC_FREE(state);
2981         END_PROFILE(SMBnttranss);
2982         return;
2983
2984   bad_param:
2985
2986         DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2987         DLIST_REMOVE(conn->pending_trans, state);
2988         SAFE_FREE(state->data);
2989         SAFE_FREE(state->param);
2990         TALLOC_FREE(state);
2991         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2992         END_PROFILE(SMBnttranss);
2993         return;
2994 }