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