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