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