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