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