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