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