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