r20124: clean up nested extern declaration warnings
[Samba.git] / source3 / smbd / nttrans.c
blob3ade5b01c6b47f1423f70c0518792796ebc017be
1 /*
2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-1998
5 Copyright (C) Stefan (metze) Metzmacher 2003
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
24 extern int max_send;
25 extern enum protocol_types Protocol;
26 extern int smb_read_error;
27 extern struct current_user current_user;
29 static const char *known_nt_pipes[] = {
30 "\\LANMAN",
31 "\\srvsvc",
32 "\\samr",
33 "\\wkssvc",
34 "\\NETLOGON",
35 "\\ntlsa",
36 "\\ntsvcs",
37 "\\lsass",
38 "\\lsarpc",
39 "\\winreg",
40 "\\initshutdown",
41 "\\spoolss",
42 "\\netdfs",
43 "\\rpcecho",
44 "\\svcctl",
45 "\\eventlog",
46 "\\unixinfo",
47 NULL
50 static char *nttrans_realloc(char **ptr, size_t size)
52 if (ptr==NULL) {
53 smb_panic("nttrans_realloc() called with NULL ptr\n");
56 *ptr = (char *)SMB_REALLOC(*ptr, size);
57 if(*ptr == NULL) {
58 return NULL;
60 memset(*ptr,'\0',size);
61 return *ptr;
64 /****************************************************************************
65 Send the required number of replies back.
66 We assume all fields other than the data fields are
67 set correctly for the type of call.
68 HACK ! Always assumes smb_setup field is zero.
69 ****************************************************************************/
71 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, NTSTATUS nt_error, char *params,
72 int paramsize, char *pdata, int datasize)
74 int data_to_send = datasize;
75 int params_to_send = paramsize;
76 int useable_space;
77 char *pp = params;
78 char *pd = pdata;
79 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
80 int alignment_offset = 3;
81 int data_alignment_offset = 0;
84 * Initially set the wcnt area to be 18 - this is true for all
85 * transNT replies.
88 set_message(outbuf,18,0,True);
90 if (NT_STATUS_V(nt_error)) {
91 ERROR_NT(nt_error);
94 /*
95 * If there genuinely are no parameters or data to send just send
96 * the empty packet.
99 if(params_to_send == 0 && data_to_send == 0) {
100 show_msg(outbuf);
101 if (!send_smb(smbd_server_fd(),outbuf)) {
102 exit_server("send_nt_replies: send_smb failed.");
104 return 0;
108 * When sending params and data ensure that both are nicely aligned.
109 * Only do this alignment when there is also data to send - else
110 * can cause NT redirector problems.
113 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
114 data_alignment_offset = 4 - (params_to_send % 4);
118 * Space is bufsize minus Netbios over TCP header minus SMB header.
119 * The alignment_offset is to align the param bytes on a four byte
120 * boundary (2 bytes for data len, one byte pad).
121 * NT needs this to work correctly.
124 useable_space = bufsize - ((smb_buf(outbuf)+
125 alignment_offset+data_alignment_offset) -
126 outbuf);
129 * useable_space can never be more than max_send minus the
130 * alignment offset.
133 useable_space = MIN(useable_space,
134 max_send - (alignment_offset+data_alignment_offset));
137 while (params_to_send || data_to_send) {
140 * Calculate whether we will totally or partially fill this packet.
143 total_sent_thistime = params_to_send + data_to_send +
144 alignment_offset + data_alignment_offset;
147 * We can never send more than useable_space.
150 total_sent_thistime = MIN(total_sent_thistime, useable_space);
152 set_message(outbuf, 18, total_sent_thistime, True);
155 * Set total params and data to be sent.
158 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
159 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
162 * Calculate how many parameters and data we can fit into
163 * this packet. Parameters get precedence.
166 params_sent_thistime = MIN(params_to_send,useable_space);
167 data_sent_thistime = useable_space - params_sent_thistime;
168 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
170 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
172 if(params_sent_thistime == 0) {
173 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
174 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
175 } else {
177 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
178 * parameter bytes, however the first 4 bytes of outbuf are
179 * the Netbios over TCP header. Thus use smb_base() to subtract
180 * them from the calculation.
183 SIVAL(outbuf,smb_ntr_ParameterOffset,
184 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
186 * Absolute displacement of param bytes sent in this packet.
189 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
193 * Deal with the data portion.
196 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
198 if(data_sent_thistime == 0) {
199 SIVAL(outbuf,smb_ntr_DataOffset,0);
200 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
201 } else {
203 * The offset of the data bytes is the offset of the
204 * parameter bytes plus the number of parameters being sent this time.
207 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
208 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
209 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
213 * Copy the param bytes into the packet.
216 if(params_sent_thistime) {
217 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
221 * Copy in the data bytes
224 if(data_sent_thistime) {
225 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
226 data_alignment_offset,pd,data_sent_thistime);
229 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
230 params_sent_thistime, data_sent_thistime, useable_space));
231 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
232 params_to_send, data_to_send, paramsize, datasize));
234 /* Send the packet */
235 show_msg(outbuf);
236 if (!send_smb(smbd_server_fd(),outbuf)) {
237 exit_server("send_nt_replies: send_smb failed.");
240 pp += params_sent_thistime;
241 pd += data_sent_thistime;
243 params_to_send -= params_sent_thistime;
244 data_to_send -= data_sent_thistime;
247 * Sanity check
250 if(params_to_send < 0 || data_to_send < 0) {
251 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
252 params_to_send, data_to_send));
253 return -1;
257 return 0;
260 /****************************************************************************
261 Is it an NTFS stream name ?
262 ****************************************************************************/
264 BOOL is_ntfs_stream_name(const char *fname)
266 if (lp_posix_pathnames()) {
267 return False;
269 return (strchr_m(fname, ':') != NULL) ? True : False;
272 /****************************************************************************
273 Save case statics.
274 ****************************************************************************/
276 static BOOL saved_case_sensitive;
277 static BOOL saved_case_preserve;
278 static BOOL saved_short_case_preserve;
280 /****************************************************************************
281 Save case semantics.
282 ****************************************************************************/
284 static void set_posix_case_semantics(connection_struct *conn, uint32 file_attributes)
286 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
287 return;
290 saved_case_sensitive = conn->case_sensitive;
291 saved_case_preserve = conn->case_preserve;
292 saved_short_case_preserve = conn->short_case_preserve;
294 /* Set to POSIX. */
295 conn->case_sensitive = True;
296 conn->case_preserve = True;
297 conn->short_case_preserve = True;
300 /****************************************************************************
301 Restore case semantics.
302 ****************************************************************************/
304 static void restore_case_semantics(connection_struct *conn, uint32 file_attributes)
306 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
307 return;
310 conn->case_sensitive = saved_case_sensitive;
311 conn->case_preserve = saved_case_preserve;
312 conn->short_case_preserve = saved_short_case_preserve;
315 /****************************************************************************
316 Reply to an NT create and X call on a pipe.
317 ****************************************************************************/
319 static int nt_open_pipe(char *fname, connection_struct *conn,
320 char *inbuf, char *outbuf, int *ppnum)
322 smb_np_struct *p = NULL;
323 uint16 vuid = SVAL(inbuf, smb_uid);
324 int i;
326 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
328 /* See if it is one we want to handle. */
330 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
331 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
334 for( i = 0; known_nt_pipes[i]; i++ ) {
335 if( strequal(fname,known_nt_pipes[i])) {
336 break;
340 if ( known_nt_pipes[i] == NULL ) {
341 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
344 /* Strip \\ off the name. */
345 fname++;
347 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
349 p = open_rpc_pipe_p(fname, conn, vuid);
350 if (!p) {
351 return(ERROR_DOS(ERRSRV,ERRnofids));
354 /* TODO: Add pipe to db */
356 if ( !store_pipe_opendb( p ) ) {
357 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
360 *ppnum = p->pnum;
361 return 0;
364 /****************************************************************************
365 Reply to an NT create and X call for pipes.
366 ****************************************************************************/
368 static int do_ntcreate_pipe_open(connection_struct *conn,
369 char *inbuf,char *outbuf,int length,int bufsize)
371 pstring fname;
372 int ret;
373 int pnum = -1;
374 char *p = NULL;
376 srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
378 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0) {
379 return ret;
383 * Deal with pipe return.
386 set_message(outbuf,34,0,True);
388 p = outbuf + smb_vwv2;
389 p++;
390 SSVAL(p,0,pnum);
391 p += 2;
392 SIVAL(p,0,FILE_WAS_OPENED);
393 p += 4;
394 p += 32;
395 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
396 p += 20;
397 /* File type. */
398 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
399 /* Device state. */
400 SSVAL(p,2, 0x5FF); /* ? */
402 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
404 return chain_reply(inbuf,outbuf,length,bufsize);
407 /****************************************************************************
408 Reply to an NT create and X call for a quota file.
409 ****************************************************************************/
411 int reply_ntcreate_and_X_quota(connection_struct *conn,
412 char *inbuf,
413 char *outbuf,
414 int length,
415 int bufsize,
416 enum FAKE_FILE_TYPE fake_file_type,
417 const char *fname)
419 int result;
420 char *p;
421 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
422 files_struct *fsp;
423 NTSTATUS status;
425 status = open_fake_file(conn, fake_file_type, fname, desired_access,
426 &fsp);
428 if (!NT_STATUS_IS_OK(status)) {
429 return ERROR_NT(status);
432 set_message(outbuf,34,0,True);
434 p = outbuf + smb_vwv2;
436 /* SCVAL(p,0,NO_OPLOCK_RETURN); */
437 p++;
438 SSVAL(p,0,fsp->fnum);
440 DEBUG(5,("reply_ntcreate_and_X_quota: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
442 result = chain_reply(inbuf,outbuf,length,bufsize);
443 return result;
446 /****************************************************************************
447 Reply to an NT create and X call.
448 ****************************************************************************/
450 int reply_ntcreate_and_X(connection_struct *conn,
451 char *inbuf,char *outbuf,int length,int bufsize)
453 int result;
454 pstring fname;
455 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
456 uint32 access_mask = IVAL(inbuf,smb_ntcreate_DesiredAccess);
457 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
458 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
459 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
460 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
461 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
462 /* Breakout the oplock request bits so we can set the
463 reply bits separately. */
464 int oplock_request = 0;
465 uint32 fattr=0;
466 SMB_OFF_T file_len = 0;
467 SMB_STRUCT_STAT sbuf;
468 int info = 0;
469 BOOL bad_path = False;
470 files_struct *fsp=NULL;
471 char *p = NULL;
472 struct timespec c_timespec;
473 struct timespec a_timespec;
474 struct timespec m_timespec;
475 BOOL extended_oplock_granted = False;
476 NTSTATUS status;
478 START_PROFILE(SMBntcreateX);
480 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x \
481 file_attributes = 0x%x, share_access = 0x%x, create_disposition = 0x%x \
482 create_options = 0x%x root_dir_fid = 0x%x\n",
483 (unsigned int)flags,
484 (unsigned int)access_mask,
485 (unsigned int)file_attributes,
486 (unsigned int)share_access,
487 (unsigned int)create_disposition,
488 (unsigned int)create_options,
489 (unsigned int)root_dir_fid ));
491 /* If it's an IPC, use the pipe handler. */
493 if (IS_IPC(conn)) {
494 if (lp_nt_pipe_support()) {
495 END_PROFILE(SMBntcreateX);
496 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
497 } else {
498 END_PROFILE(SMBntcreateX);
499 return(ERROR_DOS(ERRDOS,ERRnoaccess));
503 if (create_options & FILE_OPEN_BY_FILE_ID) {
504 END_PROFILE(SMBntcreateX);
505 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
509 * Get the file name.
512 if(root_dir_fid != 0) {
514 * This filename is relative to a directory fid.
516 pstring rel_fname;
517 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
518 size_t dir_name_len;
520 if(!dir_fsp) {
521 END_PROFILE(SMBntcreateX);
522 return(ERROR_DOS(ERRDOS,ERRbadfid));
525 if(!dir_fsp->is_directory) {
527 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
528 if (!NT_STATUS_IS_OK(status)) {
529 END_PROFILE(SMBntcreateX);
530 return ERROR_NT(status);
534 * Check to see if this is a mac fork of some kind.
537 if( is_ntfs_stream_name(fname)) {
538 END_PROFILE(SMBntcreateX);
539 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
543 we need to handle the case when we get a
544 relative open relative to a file and the
545 pathname is blank - this is a reopen!
546 (hint from demyn plantenberg)
549 END_PROFILE(SMBntcreateX);
550 return(ERROR_DOS(ERRDOS,ERRbadfid));
554 * Copy in the base directory name.
557 pstrcpy( fname, dir_fsp->fsp_name );
558 dir_name_len = strlen(fname);
561 * Ensure it ends in a '\'.
564 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
565 pstrcat(fname, "/");
566 dir_name_len++;
569 srvstr_get_path(inbuf, rel_fname, smb_buf(inbuf), sizeof(rel_fname), 0, STR_TERMINATE, &status);
570 if (!NT_STATUS_IS_OK(status)) {
571 END_PROFILE(SMBntcreateX);
572 return ERROR_NT(status);
574 pstrcat(fname, rel_fname);
575 } else {
576 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
577 if (!NT_STATUS_IS_OK(status)) {
578 END_PROFILE(SMBntcreateX);
579 return ERROR_NT(status);
583 * Check to see if this is a mac fork of some kind.
586 if( is_ntfs_stream_name(fname)) {
587 enum FAKE_FILE_TYPE fake_file_type = is_fake_file(fname);
588 if (fake_file_type!=FAKE_FILE_TYPE_NONE) {
590 * Here we go! support for changing the disk quotas --metze
592 * We need to fake up to open this MAGIC QUOTA file
593 * and return a valid FID.
595 * w2k close this file directly after openening
596 * xp also tries a QUERY_FILE_INFO on the file and then close it
598 result = reply_ntcreate_and_X_quota(conn, inbuf, outbuf, length, bufsize,
599 fake_file_type, fname);
600 END_PROFILE(SMBntcreateX);
601 return result;
602 } else {
603 END_PROFILE(SMBntcreateX);
604 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
610 * Now contruct the smb_open_mode value from the filename,
611 * desired access and the share access.
613 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
615 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
616 if (oplock_request) {
617 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
621 * Ordinary file or directory.
625 * Check if POSIX semantics are wanted.
628 set_posix_case_semantics(conn, file_attributes);
630 unix_convert(fname,conn,0,&bad_path,&sbuf);
632 if (bad_path) {
633 restore_case_semantics(conn, file_attributes);
634 END_PROFILE(SMBntcreateX);
635 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
637 /* All file access must go through check_name() */
638 if (!check_name(fname,conn)) {
639 restore_case_semantics(conn, file_attributes);
640 END_PROFILE(SMBntcreateX);
641 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRbadpath);
644 #if 0
645 /* This is the correct thing to do (check every time) but can_delete is
646 expensive (it may have to read the parent directory permissions). So
647 for now we're not doing it unless we have a strong hint the client
648 is really going to delete this file. */
649 if (desired_access & DELETE_ACCESS) {
650 #else
651 /* Setting FILE_SHARE_DELETE is the hint. */
652 if (lp_acl_check_permissions(SNUM(conn)) && (share_access & FILE_SHARE_DELETE)
653 && (access_mask & DELETE_ACCESS)) {
654 #endif
655 status = can_delete(conn, fname, file_attributes, bad_path, True);
656 /* We're only going to fail here if it's access denied, as that's the
657 only error we care about for "can we delete this ?" questions. */
658 if (!NT_STATUS_IS_OK(status) && (NT_STATUS_EQUAL(status,NT_STATUS_ACCESS_DENIED) ||
659 NT_STATUS_EQUAL(status,NT_STATUS_CANNOT_DELETE))) {
660 restore_case_semantics(conn, file_attributes);
661 END_PROFILE(SMBntcreateX);
662 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
667 * If it's a request for a directory open, deal with it separately.
670 if(create_options & FILE_DIRECTORY_FILE) {
671 oplock_request = 0;
673 /* Can't open a temp directory. IFS kit test. */
674 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
675 END_PROFILE(SMBntcreateX);
676 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
679 status = open_directory(conn, fname, &sbuf,
680 access_mask,
681 share_access,
682 create_disposition,
683 create_options,
684 &info, &fsp);
686 restore_case_semantics(conn, file_attributes);
688 if(!NT_STATUS_IS_OK(status)) {
689 END_PROFILE(SMBntcreateX);
690 return ERROR_NT(status);
692 } else {
694 * Ordinary file case.
697 /* NB. We have a potential bug here. If we
698 * cause an oplock break to ourselves, then we
699 * could end up processing filename related
700 * SMB requests whilst we await the oplock
701 * break response. As we may have changed the
702 * filename case semantics to be POSIX-like,
703 * this could mean a filename request could
704 * fail when it should succeed. This is a rare
705 * condition, but eventually we must arrange
706 * to restore the correct case semantics
707 * before issuing an oplock break request to
708 * our client. JRA. */
710 status = open_file_ntcreate(conn,fname,&sbuf,
711 access_mask,
712 share_access,
713 create_disposition,
714 create_options,
715 file_attributes,
716 oplock_request,
717 &info, &fsp);
718 if (!NT_STATUS_IS_OK(status)) {
719 /* We cheat here. There are two cases we
720 * care about. One is a directory rename,
721 * where the NT client will attempt to
722 * open the source directory for
723 * DELETE access. Note that when the
724 * NT client does this it does *not*
725 * set the directory bit in the
726 * request packet. This is translated
727 * into a read/write open
728 * request. POSIX states that any open
729 * for write request on a directory
730 * will generate an EISDIR error, so
731 * we can catch this here and open a
732 * pseudo handle that is flagged as a
733 * directory. The second is an open
734 * for a permissions read only, which
735 * we handle in the open_file_stat case. JRA.
738 if (NT_STATUS_EQUAL(status,
739 NT_STATUS_FILE_IS_A_DIRECTORY)) {
742 * Fail the open if it was explicitly a non-directory file.
745 if (create_options & FILE_NON_DIRECTORY_FILE) {
746 restore_case_semantics(conn, file_attributes);
747 END_PROFILE(SMBntcreateX);
748 return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
751 oplock_request = 0;
752 status = open_directory(conn, fname, &sbuf,
753 access_mask,
754 share_access,
755 create_disposition,
756 create_options,
757 &info, &fsp);
759 if(!NT_STATUS_IS_OK(status)) {
760 restore_case_semantics(conn, file_attributes);
761 END_PROFILE(SMBntcreateX);
762 return ERROR_NT(status);
764 } else {
766 restore_case_semantics(conn, file_attributes);
767 END_PROFILE(SMBntcreateX);
768 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
769 /* We have re-scheduled this call. */
770 return -1;
772 return ERROR_NT(status);
777 restore_case_semantics(conn, file_attributes);
779 file_len = sbuf.st_size;
780 fattr = dos_mode(conn,fname,&sbuf);
781 if(fattr == 0) {
782 fattr = FILE_ATTRIBUTE_NORMAL;
784 if (!fsp->is_directory && (fattr & aDIR)) {
785 close_file(fsp,ERROR_CLOSE);
786 END_PROFILE(SMBntcreateX);
787 return ERROR_DOS(ERRDOS,ERRnoaccess);
790 /* Save the requested allocation size. */
791 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
792 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize);
793 #ifdef LARGE_SMB_OFF_T
794 allocation_size |= (((SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
795 #endif
796 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
797 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
798 if (fsp->is_directory) {
799 close_file(fsp,ERROR_CLOSE);
800 END_PROFILE(SMBntcreateX);
801 /* Can't set allocation size on a directory. */
802 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
804 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
805 close_file(fsp,ERROR_CLOSE);
806 END_PROFILE(SMBntcreateX);
807 return ERROR_NT(NT_STATUS_DISK_FULL);
809 } else {
810 fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)file_len);
815 * If the caller set the extended oplock request bit
816 * and we granted one (by whatever means) - set the
817 * correct bit for extended oplock reply.
820 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
821 extended_oplock_granted = True;
824 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
825 extended_oplock_granted = True;
828 #if 0
829 /* W2K sends back 42 words here ! If we do the same it breaks offline sync. Go figure... ? JRA. */
830 set_message(outbuf,42,0,True);
831 #else
832 set_message(outbuf,34,0,True);
833 #endif
835 p = outbuf + smb_vwv2;
838 * Currently as we don't support level II oplocks we just report
839 * exclusive & batch here.
842 if (extended_oplock_granted) {
843 if (flags & REQUEST_BATCH_OPLOCK) {
844 SCVAL(p,0, BATCH_OPLOCK_RETURN);
845 } else {
846 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
848 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
849 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
850 } else {
851 SCVAL(p,0,NO_OPLOCK_RETURN);
854 p++;
855 SSVAL(p,0,fsp->fnum);
856 p += 2;
857 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
858 SIVAL(p,0,FILE_WAS_SUPERSEDED);
859 } else {
860 SIVAL(p,0,info);
862 p += 4;
864 /* Create time. */
865 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
866 a_timespec = get_atimespec(&sbuf);
867 m_timespec = get_mtimespec(&sbuf);
869 if (lp_dos_filetime_resolution(SNUM(conn))) {
870 dos_filetime_timespec(&c_timespec);
871 dos_filetime_timespec(&a_timespec);
872 dos_filetime_timespec(&m_timespec);
875 put_long_date_timespec(p, c_timespec);
876 p += 8;
877 put_long_date_timespec(p, a_timespec); /* access time */
878 p += 8;
879 put_long_date_timespec(p, m_timespec); /* write time */
880 p += 8;
881 put_long_date_timespec(p, m_timespec); /* change time */
882 p += 8;
883 SIVAL(p,0,fattr); /* File Attributes. */
884 p += 4;
885 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
886 p += 8;
887 SOFF_T(p,0,file_len);
888 p += 8;
889 if (flags & EXTENDED_RESPONSE_REQUIRED) {
890 SSVAL(p,2,0x7);
892 p += 4;
893 SCVAL(p,0,fsp->is_directory ? 1 : 0);
895 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
897 result = chain_reply(inbuf,outbuf,length,bufsize);
898 END_PROFILE(SMBntcreateX);
899 return result;
902 /****************************************************************************
903 Reply to a NT_TRANSACT_CREATE call to open a pipe.
904 ****************************************************************************/
906 static int do_nt_transact_create_pipe( connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
907 uint16 **ppsetup, uint32 setup_count,
908 char **ppparams, uint32 parameter_count,
909 char **ppdata, uint32 data_count)
911 pstring fname;
912 char *params = *ppparams;
913 int ret;
914 int pnum = -1;
915 char *p = NULL;
916 NTSTATUS status;
919 * Ensure minimum number of parameters sent.
922 if(parameter_count < 54) {
923 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
924 return ERROR_DOS(ERRDOS,ERRnoaccess);
927 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
928 if (!NT_STATUS_IS_OK(status)) {
929 return ERROR_NT(status);
932 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0) {
933 return ret;
936 /* Realloc the size of parameters and data we will return */
937 params = nttrans_realloc(ppparams, 69);
938 if(params == NULL) {
939 return ERROR_DOS(ERRDOS,ERRnomem);
942 p = params;
943 SCVAL(p,0,NO_OPLOCK_RETURN);
945 p += 2;
946 SSVAL(p,0,pnum);
947 p += 2;
948 SIVAL(p,0,FILE_WAS_OPENED);
949 p += 8;
951 p += 32;
952 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
953 p += 20;
954 /* File type. */
955 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
956 /* Device state. */
957 SSVAL(p,2, 0x5FF); /* ? */
959 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
961 /* Send the required number of replies */
962 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
964 return -1;
967 /****************************************************************************
968 Internal fn to set security descriptors.
969 ****************************************************************************/
971 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
973 prs_struct pd;
974 SEC_DESC *psd = NULL;
975 TALLOC_CTX *mem_ctx;
976 BOOL ret;
978 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
979 return NT_STATUS_OK;
983 * Init the parse struct we will unmarshall from.
986 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
987 DEBUG(0,("set_sd: talloc_init failed.\n"));
988 return NT_STATUS_NO_MEMORY;
991 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
994 * Setup the prs_struct to point at the memory we just
995 * allocated.
998 prs_give_memory( &pd, data, sd_len, False);
1001 * Finally, unmarshall from the data buffer.
1004 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1005 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1007 * Return access denied for want of a better error message..
1009 talloc_destroy(mem_ctx);
1010 return NT_STATUS_NO_MEMORY;
1013 if (psd->owner_sid==0) {
1014 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1016 if (psd->group_sid==0) {
1017 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1019 if (psd->sacl==0) {
1020 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1022 if (psd->dacl==0) {
1023 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1026 ret = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
1028 if (!ret) {
1029 talloc_destroy(mem_ctx);
1030 return NT_STATUS_ACCESS_DENIED;
1033 talloc_destroy(mem_ctx);
1035 return NT_STATUS_OK;
1038 /****************************************************************************
1039 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1040 ****************************************************************************/
1042 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
1044 struct ea_list *ea_list_head = NULL;
1045 size_t offset = 0;
1047 if (data_size < 4) {
1048 return NULL;
1051 while (offset + 4 <= data_size) {
1052 size_t next_offset = IVAL(pdata,offset);
1053 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
1055 if (!eal) {
1056 return NULL;
1059 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
1060 if (next_offset == 0) {
1061 break;
1063 offset += next_offset;
1066 return ea_list_head;
1069 /****************************************************************************
1070 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1071 ****************************************************************************/
1073 static int call_nt_transact_create(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1074 uint16 **ppsetup, uint32 setup_count,
1075 char **ppparams, uint32 parameter_count,
1076 char **ppdata, uint32 data_count, uint32 max_data_count)
1078 pstring fname;
1079 char *params = *ppparams;
1080 char *data = *ppdata;
1081 /* Breakout the oplock request bits so we can set the reply bits separately. */
1082 int oplock_request = 0;
1083 uint32 fattr=0;
1084 SMB_OFF_T file_len = 0;
1085 SMB_STRUCT_STAT sbuf;
1086 int info = 0;
1087 BOOL bad_path = False;
1088 files_struct *fsp = NULL;
1089 char *p = NULL;
1090 BOOL extended_oplock_granted = False;
1091 uint32 flags;
1092 uint32 access_mask;
1093 uint32 file_attributes;
1094 uint32 share_access;
1095 uint32 create_disposition;
1096 uint32 create_options;
1097 uint32 sd_len;
1098 uint32 ea_len;
1099 uint16 root_dir_fid;
1100 struct timespec c_timespec;
1101 struct timespec a_timespec;
1102 struct timespec m_timespec;
1103 struct ea_list *ea_list = NULL;
1104 TALLOC_CTX *ctx = NULL;
1105 char *pdata = NULL;
1106 NTSTATUS status;
1108 DEBUG(5,("call_nt_transact_create\n"));
1111 * If it's an IPC, use the pipe handler.
1114 if (IS_IPC(conn)) {
1115 if (lp_nt_pipe_support()) {
1116 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1117 bufsize,
1118 ppsetup, setup_count,
1119 ppparams, parameter_count,
1120 ppdata, data_count);
1121 } else {
1122 return ERROR_DOS(ERRDOS,ERRnoaccess);
1127 * Ensure minimum number of parameters sent.
1130 if(parameter_count < 54) {
1131 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1132 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1135 flags = IVAL(params,0);
1136 access_mask = IVAL(params,8);
1137 file_attributes = IVAL(params,20);
1138 share_access = IVAL(params,24);
1139 create_disposition = IVAL(params,28);
1140 create_options = IVAL(params,32);
1141 sd_len = IVAL(params,36);
1142 ea_len = IVAL(params,40);
1143 root_dir_fid = (uint16)IVAL(params,4);
1145 /* Ensure the data_len is correct for the sd and ea values given. */
1146 if ((ea_len + sd_len > data_count) ||
1147 (ea_len > data_count) || (sd_len > data_count) ||
1148 (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1149 DEBUG(10,("call_nt_transact_create - ea_len = %u, sd_len = %u, data_count = %u\n",
1150 (unsigned int)ea_len, (unsigned int)sd_len, (unsigned int)data_count ));
1151 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1154 if (ea_len) {
1155 if (!lp_ea_support(SNUM(conn))) {
1156 DEBUG(10,("call_nt_transact_create - ea_len = %u but EA's not supported.\n",
1157 (unsigned int)ea_len ));
1158 return ERROR_NT(NT_STATUS_EAS_NOT_SUPPORTED);
1161 if (ea_len < 10) {
1162 DEBUG(10,("call_nt_transact_create - ea_len = %u - too small (should be more than 10)\n",
1163 (unsigned int)ea_len ));
1164 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1168 if (create_options & FILE_OPEN_BY_FILE_ID) {
1169 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1173 * Get the file name.
1176 if(root_dir_fid != 0) {
1178 * This filename is relative to a directory fid.
1180 files_struct *dir_fsp = file_fsp(params,4);
1181 size_t dir_name_len;
1183 if(!dir_fsp) {
1184 return ERROR_DOS(ERRDOS,ERRbadfid);
1187 if(!dir_fsp->is_directory) {
1188 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
1189 if (!NT_STATUS_IS_OK(status)) {
1190 return ERROR_NT(status);
1194 * Check to see if this is a mac fork of some kind.
1197 if( is_ntfs_stream_name(fname)) {
1198 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1201 return ERROR_DOS(ERRDOS,ERRbadfid);
1205 * Copy in the base directory name.
1208 pstrcpy( fname, dir_fsp->fsp_name );
1209 dir_name_len = strlen(fname);
1212 * Ensure it ends in a '\'.
1215 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1216 pstrcat(fname, "/");
1217 dir_name_len++;
1221 pstring tmpname;
1222 srvstr_get_path(inbuf, tmpname, params+53, sizeof(tmpname), parameter_count-53, STR_TERMINATE, &status);
1223 if (!NT_STATUS_IS_OK(status)) {
1224 return ERROR_NT(status);
1226 pstrcat(fname, tmpname);
1228 } else {
1229 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
1230 if (!NT_STATUS_IS_OK(status)) {
1231 return ERROR_NT(status);
1235 * Check to see if this is a mac fork of some kind.
1238 if( is_ntfs_stream_name(fname)) {
1239 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1243 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1244 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1247 * Check if POSIX semantics are wanted.
1250 set_posix_case_semantics(conn, file_attributes);
1252 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1254 unix_convert(fname,conn,0,&bad_path,&sbuf);
1255 if (bad_path) {
1256 restore_case_semantics(conn, file_attributes);
1257 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1259 /* All file access must go through check_name() */
1260 if (!check_name(fname,conn)) {
1261 restore_case_semantics(conn, file_attributes);
1262 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRbadpath);
1265 #if 0
1266 /* This is the correct thing to do (check every time) but can_delete is
1267 expensive (it may have to read the parent directory permissions). So
1268 for now we're not doing it unless we have a strong hint the client
1269 is really going to delete this file. */
1270 if (desired_access & DELETE_ACCESS) {
1271 #else
1272 /* Setting FILE_SHARE_DELETE is the hint. */
1273 if (lp_acl_check_permissions(SNUM(conn)) && (share_access & FILE_SHARE_DELETE) && (access_mask & DELETE_ACCESS)) {
1274 #endif
1275 status = can_delete(conn, fname, file_attributes, bad_path, True);
1276 /* We're only going to fail here if it's access denied, as that's the
1277 only error we care about for "can we delete this ?" questions. */
1278 if (!NT_STATUS_IS_OK(status) && (NT_STATUS_EQUAL(status,NT_STATUS_ACCESS_DENIED) ||
1279 NT_STATUS_EQUAL(status,NT_STATUS_CANNOT_DELETE))) {
1280 restore_case_semantics(conn, file_attributes);
1281 return ERROR_NT(status);
1285 if (ea_len) {
1286 ctx = talloc_init("NTTRANS_CREATE_EA");
1287 if (!ctx) {
1288 talloc_destroy(ctx);
1289 restore_case_semantics(conn, file_attributes);
1290 return ERROR_NT(NT_STATUS_NO_MEMORY);
1293 pdata = data + sd_len;
1295 /* We have already checked that ea_len <= data_count here. */
1296 ea_list = read_nttrans_ea_list(ctx, pdata, ea_len);
1297 if (!ea_list ) {
1298 talloc_destroy(ctx);
1299 restore_case_semantics(conn, file_attributes);
1300 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1305 * If it's a request for a directory open, deal with it separately.
1308 if(create_options & FILE_DIRECTORY_FILE) {
1310 /* Can't open a temp directory. IFS kit test. */
1311 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1312 talloc_destroy(ctx);
1313 restore_case_semantics(conn, file_attributes);
1314 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1317 oplock_request = 0;
1320 * We will get a create directory here if the Win32
1321 * app specified a security descriptor in the
1322 * CreateDirectory() call.
1325 status = open_directory(conn, fname, &sbuf,
1326 access_mask,
1327 share_access,
1328 create_disposition,
1329 create_options,
1330 &info, &fsp);
1331 if(!NT_STATUS_IS_OK(status)) {
1332 talloc_destroy(ctx);
1333 restore_case_semantics(conn, file_attributes);
1334 return ERROR_NT(status);
1337 } else {
1340 * Ordinary file case.
1343 status = open_file_ntcreate(conn,fname,&sbuf,
1344 access_mask,
1345 share_access,
1346 create_disposition,
1347 create_options,
1348 file_attributes,
1349 oplock_request,
1350 &info, &fsp);
1352 if (!NT_STATUS_IS_OK(status)) {
1353 if (NT_STATUS_EQUAL(status,
1354 NT_STATUS_FILE_IS_A_DIRECTORY)) {
1357 * Fail the open if it was explicitly a non-directory file.
1360 if (create_options & FILE_NON_DIRECTORY_FILE) {
1361 restore_case_semantics(conn, file_attributes);
1362 return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1365 oplock_request = 0;
1366 status = open_directory(conn, fname, &sbuf,
1367 access_mask,
1368 share_access,
1369 create_disposition,
1370 create_options,
1371 &info, &fsp);
1372 if(!NT_STATUS_IS_OK(status)) {
1373 talloc_destroy(ctx);
1374 restore_case_semantics(conn, file_attributes);
1375 return ERROR_NT(status);
1377 } else {
1378 talloc_destroy(ctx);
1379 restore_case_semantics(conn, file_attributes);
1380 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1381 /* We have re-scheduled this call. */
1382 return -1;
1384 return ERROR_NT(status);
1390 * According to the MS documentation, the only time the security
1391 * descriptor is applied to the opened file is iff we *created* the
1392 * file; an existing file stays the same.
1394 * Also, it seems (from observation) that you can open the file with
1395 * any access mask but you can still write the sd. We need to override
1396 * the granted access before we call set_sd
1397 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1400 if (lp_nt_acl_support(SNUM(conn)) && sd_len && info == FILE_WAS_CREATED) {
1401 uint32 saved_access_mask = fsp->access_mask;
1403 /* We have already checked that sd_len <= data_count here. */
1405 fsp->access_mask = FILE_GENERIC_ALL;
1407 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1408 if (!NT_STATUS_IS_OK(status)) {
1409 talloc_destroy(ctx);
1410 close_file(fsp,ERROR_CLOSE);
1411 restore_case_semantics(conn, file_attributes);
1412 return ERROR_NT(status);
1414 fsp->access_mask = saved_access_mask;
1417 if (ea_len && (info == FILE_WAS_CREATED)) {
1418 status = set_ea(conn, fsp, fname, ea_list);
1419 talloc_destroy(ctx);
1420 if (!NT_STATUS_IS_OK(status)) {
1421 close_file(fsp,ERROR_CLOSE);
1422 restore_case_semantics(conn, file_attributes);
1423 return ERROR_NT(status);
1427 restore_case_semantics(conn, file_attributes);
1429 file_len = sbuf.st_size;
1430 fattr = dos_mode(conn,fname,&sbuf);
1431 if(fattr == 0) {
1432 fattr = FILE_ATTRIBUTE_NORMAL;
1434 if (!fsp->is_directory && (fattr & aDIR)) {
1435 close_file(fsp,ERROR_CLOSE);
1436 return ERROR_DOS(ERRDOS,ERRnoaccess);
1439 /* Save the requested allocation size. */
1440 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1441 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1442 #ifdef LARGE_SMB_OFF_T
1443 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1444 #endif
1445 if (allocation_size && (allocation_size > file_len)) {
1446 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1447 if (fsp->is_directory) {
1448 close_file(fsp,ERROR_CLOSE);
1449 /* Can't set allocation size on a directory. */
1450 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1452 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1453 close_file(fsp,ERROR_CLOSE);
1454 return ERROR_NT(NT_STATUS_DISK_FULL);
1456 } else {
1457 fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1462 * If the caller set the extended oplock request bit
1463 * and we granted one (by whatever means) - set the
1464 * correct bit for extended oplock reply.
1467 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1468 extended_oplock_granted = True;
1471 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1472 extended_oplock_granted = True;
1475 /* Realloc the size of parameters and data we will return */
1476 params = nttrans_realloc(ppparams, 69);
1477 if(params == NULL) {
1478 return ERROR_DOS(ERRDOS,ERRnomem);
1481 p = params;
1482 if (extended_oplock_granted) {
1483 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1484 } else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
1485 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1486 } else {
1487 SCVAL(p,0,NO_OPLOCK_RETURN);
1490 p += 2;
1491 SSVAL(p,0,fsp->fnum);
1492 p += 2;
1493 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1494 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1495 } else {
1496 SIVAL(p,0,info);
1498 p += 8;
1500 /* Create time. */
1501 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1502 a_timespec = get_atimespec(&sbuf);
1503 m_timespec = get_mtimespec(&sbuf);
1505 if (lp_dos_filetime_resolution(SNUM(conn))) {
1506 dos_filetime_timespec(&c_timespec);
1507 dos_filetime_timespec(&a_timespec);
1508 dos_filetime_timespec(&m_timespec);
1511 put_long_date_timespec(p, c_timespec); /* create time. */
1512 p += 8;
1513 put_long_date_timespec(p, a_timespec); /* access time */
1514 p += 8;
1515 put_long_date_timespec(p, m_timespec); /* write time */
1516 p += 8;
1517 put_long_date_timespec(p, m_timespec); /* change time */
1518 p += 8;
1519 SIVAL(p,0,fattr); /* File Attributes. */
1520 p += 4;
1521 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1522 p += 8;
1523 SOFF_T(p,0,file_len);
1524 p += 8;
1525 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1526 SSVAL(p,2,0x7);
1528 p += 4;
1529 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1531 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1533 /* Send the required number of replies */
1534 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1536 return -1;
1539 /****************************************************************************
1540 Reply to a NT CANCEL request.
1541 conn POINTER CAN BE NULL HERE !
1542 ****************************************************************************/
1544 int reply_ntcancel(connection_struct *conn,
1545 char *inbuf,char *outbuf,int length,int bufsize)
1548 * Go through and cancel any pending change notifies.
1551 int mid = SVAL(inbuf,smb_mid);
1552 START_PROFILE(SMBntcancel);
1553 remove_pending_change_notify_requests_by_mid(mid);
1554 remove_pending_lock_requests_by_mid(mid);
1555 srv_cancel_sign_response(mid);
1557 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1559 END_PROFILE(SMBntcancel);
1560 return(-1);
1563 /****************************************************************************
1564 Copy a file.
1565 ****************************************************************************/
1567 static NTSTATUS copy_internals(connection_struct *conn, char *oldname, char *newname, uint32 attrs)
1569 BOOL bad_path_oldname = False;
1570 BOOL bad_path_newname = False;
1571 SMB_STRUCT_STAT sbuf1, sbuf2;
1572 pstring last_component_oldname;
1573 pstring last_component_newname;
1574 files_struct *fsp1,*fsp2;
1575 uint32 fattr;
1576 int info;
1577 SMB_OFF_T ret=-1;
1578 int close_ret;
1579 NTSTATUS status = NT_STATUS_OK;
1581 ZERO_STRUCT(sbuf1);
1582 ZERO_STRUCT(sbuf2);
1584 /* No wildcards. */
1585 if (ms_has_wild(newname) || ms_has_wild(oldname)) {
1586 return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1589 if (!CAN_WRITE(conn))
1590 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1592 unix_convert(oldname,conn,last_component_oldname,&bad_path_oldname,&sbuf1);
1593 if (bad_path_oldname) {
1594 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1597 /* Quick check for "." and ".." */
1598 if (last_component_oldname[0] == '.') {
1599 if (!last_component_oldname[1] || (last_component_oldname[1] == '.' && !last_component_oldname[2])) {
1600 return NT_STATUS_OBJECT_NAME_INVALID;
1604 /* Source must already exist. */
1605 if (!VALID_STAT(sbuf1)) {
1606 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1608 if (!check_name(oldname,conn)) {
1609 return NT_STATUS_ACCESS_DENIED;
1612 /* Ensure attributes match. */
1613 fattr = dos_mode(conn,oldname,&sbuf1);
1614 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1615 return NT_STATUS_NO_SUCH_FILE;
1618 unix_convert(newname,conn,last_component_newname,&bad_path_newname,&sbuf2);
1619 if (bad_path_newname) {
1620 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1623 /* Quick check for "." and ".." */
1624 if (last_component_newname[0] == '.') {
1625 if (!last_component_newname[1] || (last_component_newname[1] == '.' && !last_component_newname[2])) {
1626 return NT_STATUS_OBJECT_NAME_INVALID;
1630 /* Disallow if newname already exists. */
1631 if (VALID_STAT(sbuf2)) {
1632 return NT_STATUS_OBJECT_NAME_COLLISION;
1635 if (!check_name(newname,conn)) {
1636 return NT_STATUS_ACCESS_DENIED;
1639 /* No links from a directory. */
1640 if (S_ISDIR(sbuf1.st_mode)) {
1641 return NT_STATUS_FILE_IS_A_DIRECTORY;
1644 /* Ensure this is within the share. */
1645 if (!reduce_name(conn, oldname) != 0) {
1646 return NT_STATUS_ACCESS_DENIED;
1649 DEBUG(10,("copy_internals: doing file copy %s to %s\n", oldname, newname));
1651 status = open_file_ntcreate(conn,oldname,&sbuf1,
1652 FILE_READ_DATA, /* Read-only. */
1653 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1654 FILE_OPEN,
1655 0, /* No create options. */
1656 FILE_ATTRIBUTE_NORMAL,
1657 NO_OPLOCK,
1658 &info, &fsp1);
1660 if (!NT_STATUS_IS_OK(status)) {
1661 return status;
1664 status = open_file_ntcreate(conn,newname,&sbuf2,
1665 FILE_WRITE_DATA, /* Read-only. */
1666 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1667 FILE_CREATE,
1668 0, /* No create options. */
1669 fattr,
1670 NO_OPLOCK,
1671 &info, &fsp2);
1673 if (!NT_STATUS_IS_OK(status)) {
1674 close_file(fsp1,ERROR_CLOSE);
1675 return status;
1678 if (sbuf1.st_size) {
1679 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1683 * As we are opening fsp1 read-only we only expect
1684 * an error on close on fsp2 if we are out of space.
1685 * Thus we don't look at the error return from the
1686 * close of fsp1.
1688 close_file(fsp1,NORMAL_CLOSE);
1690 /* Ensure the modtime is set correctly on the destination file. */
1691 fsp_set_pending_modtime(fsp2, sbuf1.st_mtime);
1693 close_ret = close_file(fsp2,NORMAL_CLOSE);
1695 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1696 creates the file. This isn't the correct thing to do in the copy
1697 case. JRA */
1698 file_set_dosmode(conn, newname, fattr, &sbuf2, True);
1700 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1701 return NT_STATUS_DISK_FULL;
1704 if (close_ret != 0) {
1705 status = map_nt_error_from_unix(close_ret);
1706 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1707 nt_errstr(status), oldname, newname));
1709 return status;
1712 /****************************************************************************
1713 Reply to a NT rename request.
1714 ****************************************************************************/
1716 int reply_ntrename(connection_struct *conn,
1717 char *inbuf,char *outbuf,int length,int bufsize)
1719 int outsize = 0;
1720 pstring oldname;
1721 pstring newname;
1722 char *p;
1723 NTSTATUS status;
1724 BOOL path_contains_wcard = False;
1725 uint32 attrs = SVAL(inbuf,smb_vwv0);
1726 uint16 rename_type = SVAL(inbuf,smb_vwv1);
1728 START_PROFILE(SMBntrename);
1730 p = smb_buf(inbuf) + 1;
1731 p += srvstr_get_path_wcard(inbuf, oldname, p, sizeof(oldname), 0, STR_TERMINATE, &status, &path_contains_wcard);
1732 if (!NT_STATUS_IS_OK(status)) {
1733 END_PROFILE(SMBntrename);
1734 return ERROR_NT(status);
1737 if( is_ntfs_stream_name(oldname)) {
1738 /* Can't rename a stream. */
1739 END_PROFILE(SMBntrename);
1740 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1743 if (ms_has_wild(oldname)) {
1744 END_PROFILE(SMBntrename);
1745 return ERROR_NT(NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1748 p++;
1749 p += srvstr_get_path(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status);
1750 if (!NT_STATUS_IS_OK(status)) {
1751 END_PROFILE(SMBntrename);
1752 return ERROR_NT(status);
1755 RESOLVE_DFSPATH(oldname, conn, inbuf, outbuf);
1756 RESOLVE_DFSPATH(newname, conn, inbuf, outbuf);
1758 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1760 switch(rename_type) {
1761 case RENAME_FLAG_RENAME:
1762 status = rename_internals(conn, oldname, newname, attrs, False, path_contains_wcard);
1763 break;
1764 case RENAME_FLAG_HARD_LINK:
1765 status = hardlink_internals(conn, oldname, newname);
1766 break;
1767 case RENAME_FLAG_COPY:
1768 if (path_contains_wcard) {
1769 /* No wildcards. */
1770 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1771 } else {
1772 status = copy_internals(conn, oldname, newname, attrs);
1774 break;
1775 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1776 status = NT_STATUS_INVALID_PARAMETER;
1777 break;
1778 default:
1779 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1780 break;
1783 if (!NT_STATUS_IS_OK(status)) {
1784 END_PROFILE(SMBntrename);
1785 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1786 /* We have re-scheduled this call. */
1787 return -1;
1789 return ERROR_NT(status);
1793 * Win2k needs a changenotify request response before it will
1794 * update after a rename..
1796 process_pending_change_notify_queue((time_t)0);
1797 outsize = set_message(outbuf,0,0,False);
1799 END_PROFILE(SMBntrename);
1800 return(outsize);
1803 /****************************************************************************
1804 Reply to a notify change - queue the request and
1805 don't allow a directory to be opened.
1806 ****************************************************************************/
1808 static int call_nt_transact_notify_change(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1809 uint16 **ppsetup, uint32 setup_count,
1810 char **ppparams, uint32 parameter_count,
1811 char **ppdata, uint32 data_count, uint32 max_data_count)
1813 uint16 *setup = *ppsetup;
1814 files_struct *fsp;
1815 uint32 flags;
1817 if(setup_count < 6) {
1818 return ERROR_DOS(ERRDOS,ERRbadfunc);
1821 fsp = file_fsp((char *)setup,4);
1822 flags = IVAL(setup, 0);
1824 DEBUG(3,("call_nt_transact_notify_change\n"));
1826 if(!fsp) {
1827 return ERROR_DOS(ERRDOS,ERRbadfid);
1830 if((!fsp->is_directory) || (conn != fsp->conn)) {
1831 return ERROR_DOS(ERRDOS,ERRbadfid);
1834 if (!change_notify_set(inbuf, fsp, conn, flags)) {
1835 return(UNIXERROR(ERRDOS,ERRbadfid));
1838 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1839 name = %s\n", fsp->fsp_name ));
1841 return -1;
1844 /****************************************************************************
1845 Reply to an NT transact rename command.
1846 ****************************************************************************/
1848 static int call_nt_transact_rename(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1849 uint16 **ppsetup, uint32 setup_count,
1850 char **ppparams, uint32 parameter_count,
1851 char **ppdata, uint32 data_count, uint32 max_data_count)
1853 char *params = *ppparams;
1854 pstring new_name;
1855 files_struct *fsp = NULL;
1856 BOOL replace_if_exists = False;
1857 BOOL path_contains_wcard = False;
1858 NTSTATUS status;
1860 if(parameter_count < 4) {
1861 return ERROR_DOS(ERRDOS,ERRbadfunc);
1864 fsp = file_fsp(params, 0);
1865 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1866 CHECK_FSP(fsp, conn);
1867 srvstr_get_path_wcard(inbuf, new_name, params+4, sizeof(new_name), -1, STR_TERMINATE, &status, &path_contains_wcard);
1868 if (!NT_STATUS_IS_OK(status)) {
1869 return ERROR_NT(status);
1872 status = rename_internals(conn, fsp->fsp_name,
1873 new_name, 0, replace_if_exists, path_contains_wcard);
1874 if (!NT_STATUS_IS_OK(status))
1875 return ERROR_NT(status);
1878 * Rename was successful.
1880 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1882 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1883 fsp->fsp_name, new_name));
1886 * Win2k needs a changenotify request response before it will
1887 * update after a rename..
1890 process_pending_change_notify_queue((time_t)0);
1892 return -1;
1895 /******************************************************************************
1896 Fake up a completely empty SD.
1897 *******************************************************************************/
1899 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1901 size_t sd_size;
1903 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1904 if(!*ppsd) {
1905 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1906 sd_size = 0;
1909 return sd_size;
1912 /****************************************************************************
1913 Reply to query a security descriptor.
1914 ****************************************************************************/
1916 static int call_nt_transact_query_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1917 uint16 **ppsetup, uint32 setup_count,
1918 char **ppparams, uint32 parameter_count,
1919 char **ppdata, uint32 data_count, uint32 max_data_count)
1921 char *params = *ppparams;
1922 char *data = *ppdata;
1923 prs_struct pd;
1924 SEC_DESC *psd = NULL;
1925 size_t sd_size;
1926 uint32 security_info_wanted;
1927 TALLOC_CTX *mem_ctx;
1928 files_struct *fsp = NULL;
1930 if(parameter_count < 8) {
1931 return ERROR_DOS(ERRDOS,ERRbadfunc);
1934 fsp = file_fsp(params,0);
1935 if(!fsp) {
1936 return ERROR_DOS(ERRDOS,ERRbadfid);
1939 security_info_wanted = IVAL(params,4);
1941 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1942 (unsigned int)security_info_wanted ));
1944 params = nttrans_realloc(ppparams, 4);
1945 if(params == NULL) {
1946 return ERROR_DOS(ERRDOS,ERRnomem);
1949 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
1950 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1951 return ERROR_DOS(ERRDOS,ERRnomem);
1955 * Get the permissions to return.
1958 if (!lp_nt_acl_support(SNUM(conn))) {
1959 sd_size = get_null_nt_acl(mem_ctx, &psd);
1960 } else {
1961 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd, security_info_wanted, &psd);
1964 if (sd_size == 0) {
1965 talloc_destroy(mem_ctx);
1966 return(UNIXERROR(ERRDOS,ERRnoaccess));
1969 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1971 SIVAL(params,0,(uint32)sd_size);
1973 if(max_data_count < sd_size) {
1975 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1976 params, 4, *ppdata, 0);
1977 talloc_destroy(mem_ctx);
1978 return -1;
1982 * Allocate the data we will point this at.
1985 data = nttrans_realloc(ppdata, sd_size);
1986 if(data == NULL) {
1987 talloc_destroy(mem_ctx);
1988 return ERROR_DOS(ERRDOS,ERRnomem);
1992 * Init the parse struct we will marshall into.
1995 prs_init(&pd, 0, mem_ctx, MARSHALL);
1998 * Setup the prs_struct to point at the memory we just
1999 * allocated.
2002 prs_give_memory( &pd, data, (uint32)sd_size, False);
2005 * Finally, linearize into the outgoing buffer.
2008 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2009 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2010 security descriptor.\n"));
2012 * Return access denied for want of a better error message..
2014 talloc_destroy(mem_ctx);
2015 return(UNIXERROR(ERRDOS,ERRnoaccess));
2019 * Now we can delete the security descriptor.
2022 talloc_destroy(mem_ctx);
2024 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data, (int)sd_size);
2025 return -1;
2028 /****************************************************************************
2029 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2030 ****************************************************************************/
2032 static int call_nt_transact_set_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2033 uint16 **ppsetup, uint32 setup_count,
2034 char **ppparams, uint32 parameter_count,
2035 char **ppdata, uint32 data_count, uint32 max_data_count)
2037 char *params= *ppparams;
2038 char *data = *ppdata;
2039 files_struct *fsp = NULL;
2040 uint32 security_info_sent = 0;
2041 NTSTATUS nt_status;
2043 if(parameter_count < 8) {
2044 return ERROR_DOS(ERRDOS,ERRbadfunc);
2047 if((fsp = file_fsp(params,0)) == NULL) {
2048 return ERROR_DOS(ERRDOS,ERRbadfid);
2051 if(!lp_nt_acl_support(SNUM(conn))) {
2052 goto done;
2055 security_info_sent = IVAL(params,4);
2057 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2058 (unsigned int)security_info_sent ));
2060 if (data_count == 0) {
2061 return ERROR_DOS(ERRDOS, ERRnoaccess);
2064 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2065 return ERROR_NT(nt_status);
2068 done:
2070 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2071 return -1;
2074 /****************************************************************************
2075 Reply to NT IOCTL
2076 ****************************************************************************/
2078 static int call_nt_transact_ioctl(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2079 uint16 **ppsetup, uint32 setup_count,
2080 char **ppparams, uint32 parameter_count,
2081 char **ppdata, uint32 data_count, uint32 max_data_count)
2083 uint32 function;
2084 uint16 fidnum;
2085 files_struct *fsp;
2086 uint8 isFSctl;
2087 uint8 compfilter;
2088 static BOOL logged_message;
2089 char *pdata = *ppdata;
2091 if (setup_count != 8) {
2092 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2093 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2096 function = IVAL(*ppsetup, 0);
2097 fidnum = SVAL(*ppsetup, 4);
2098 isFSctl = CVAL(*ppsetup, 6);
2099 compfilter = CVAL(*ppsetup, 7);
2101 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2102 function, fidnum, isFSctl, compfilter));
2104 fsp=file_fsp((char *)*ppsetup, 4);
2105 /* this check is done in each implemented function case for now
2106 because I don't want to break anything... --metze
2107 FSP_BELONGS_CONN(fsp,conn);*/
2109 switch (function) {
2110 case FSCTL_SET_SPARSE:
2111 /* pretend this succeeded - tho strictly we should
2112 mark the file sparse (if the local fs supports it)
2113 so we can know if we need to pre-allocate or not */
2115 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2116 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2117 return -1;
2119 case FSCTL_0x000900C0:
2120 /* pretend this succeeded - don't know what this really is
2121 but works ok like this --metze
2124 DEBUG(10,("FSCTL_0x000900C0: called on FID[0x%04X](but not implemented)\n",fidnum));
2125 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2126 return -1;
2128 case FSCTL_GET_REPARSE_POINT:
2129 /* pretend this fail - my winXP does it like this
2130 * --metze
2133 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2134 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
2135 return -1;
2137 case FSCTL_SET_REPARSE_POINT:
2138 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2139 * --metze
2142 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2143 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
2144 return -1;
2146 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2149 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2150 * and return their volume names. If max_data_count is 16, then it is just
2151 * asking for the number of volumes and length of the combined names.
2153 * pdata is the data allocated by our caller, but that uses
2154 * total_data_count (which is 0 in our case) rather than max_data_count.
2155 * Allocate the correct amount and return the pointer to let
2156 * it be deallocated when we return.
2158 SHADOW_COPY_DATA *shadow_data = NULL;
2159 TALLOC_CTX *shadow_mem_ctx = NULL;
2160 BOOL labels = False;
2161 uint32 labels_data_count = 0;
2162 uint32 i;
2163 char *cur_pdata;
2165 FSP_BELONGS_CONN(fsp,conn);
2167 if (max_data_count < 16) {
2168 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2169 max_data_count));
2170 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2173 if (max_data_count > 16) {
2174 labels = True;
2177 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2178 if (shadow_mem_ctx == NULL) {
2179 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2180 return ERROR_NT(NT_STATUS_NO_MEMORY);
2183 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2184 if (shadow_data == NULL) {
2185 DEBUG(0,("talloc_zero() failed!\n"));
2186 talloc_destroy(shadow_mem_ctx);
2187 return ERROR_NT(NT_STATUS_NO_MEMORY);
2190 shadow_data->mem_ctx = shadow_mem_ctx;
2193 * Call the VFS routine to actually do the work.
2195 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2196 talloc_destroy(shadow_data->mem_ctx);
2197 if (errno == ENOSYS) {
2198 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2199 conn->connectpath));
2200 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2201 } else {
2202 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2203 conn->connectpath));
2204 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
2208 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2210 if (!labels) {
2211 data_count = 16;
2212 } else {
2213 data_count = 12+labels_data_count+4;
2216 if (max_data_count<data_count) {
2217 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2218 max_data_count,data_count));
2219 talloc_destroy(shadow_data->mem_ctx);
2220 return ERROR_NT(NT_STATUS_BUFFER_TOO_SMALL);
2223 pdata = nttrans_realloc(ppdata, data_count);
2224 if (pdata == NULL) {
2225 talloc_destroy(shadow_data->mem_ctx);
2226 return ERROR_NT(NT_STATUS_NO_MEMORY);
2229 cur_pdata = pdata;
2231 /* num_volumes 4 bytes */
2232 SIVAL(pdata,0,shadow_data->num_volumes);
2234 if (labels) {
2235 /* num_labels 4 bytes */
2236 SIVAL(pdata,4,shadow_data->num_volumes);
2239 /* needed_data_count 4 bytes */
2240 SIVAL(pdata,8,labels_data_count);
2242 cur_pdata+=12;
2244 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2245 shadow_data->num_volumes,fsp->fsp_name));
2246 if (labels && shadow_data->labels) {
2247 for (i=0;i<shadow_data->num_volumes;i++) {
2248 srvstr_push(outbuf, cur_pdata, shadow_data->labels[i], 2*sizeof(SHADOW_COPY_LABEL), STR_UNICODE|STR_TERMINATE);
2249 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2250 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2254 talloc_destroy(shadow_data->mem_ctx);
2256 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, pdata, data_count);
2258 return -1;
2261 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2263 /* pretend this succeeded -
2265 * we have to send back a list with all files owned by this SID
2267 * but I have to check that --metze
2269 DOM_SID sid;
2270 uid_t uid;
2271 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2273 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2275 FSP_BELONGS_CONN(fsp,conn);
2277 /* unknown 4 bytes: this is not the length of the sid :-( */
2278 /*unknown = IVAL(pdata,0);*/
2280 sid_parse(pdata+4,sid_len,&sid);
2281 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2283 if (!sid_to_uid(&sid, &uid)) {
2284 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2285 sid_string_static(&sid),(unsigned long)sid_len));
2286 uid = (-1);
2289 /* we can take a look at the find source :-)
2291 * find ./ -uid $uid -name '*' is what we need here
2294 * and send 4bytes len and then NULL terminated unicode strings
2295 * for each file
2297 * but I don't know how to deal with the paged results
2298 * (maybe we can hang the result anywhere in the fsp struct)
2300 * we don't send all files at once
2301 * and at the next we should *not* start from the beginning,
2302 * so we have to cache the result
2304 * --metze
2307 /* this works for now... */
2308 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2309 return -1;
2311 default:
2312 if (!logged_message) {
2313 logged_message = True; /* Only print this once... */
2314 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2315 function));
2319 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2323 #ifdef HAVE_SYS_QUOTAS
2324 /****************************************************************************
2325 Reply to get user quota
2326 ****************************************************************************/
2328 static int call_nt_transact_get_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2329 uint16 **ppsetup, uint32 setup_count,
2330 char **ppparams, uint32 parameter_count,
2331 char **ppdata, uint32 data_count, uint32 max_data_count)
2333 NTSTATUS nt_status = NT_STATUS_OK;
2334 char *params = *ppparams;
2335 char *pdata = *ppdata;
2336 char *entry;
2337 int data_len=0,param_len=0;
2338 int qt_len=0;
2339 int entry_len = 0;
2340 files_struct *fsp = NULL;
2341 uint16 level = 0;
2342 size_t sid_len;
2343 DOM_SID sid;
2344 BOOL start_enum = True;
2345 SMB_NTQUOTA_STRUCT qt;
2346 SMB_NTQUOTA_LIST *tmp_list;
2347 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2349 ZERO_STRUCT(qt);
2351 /* access check */
2352 if (current_user.ut.uid != 0) {
2353 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2354 lp_servicename(SNUM(conn)),conn->user));
2355 return ERROR_DOS(ERRDOS,ERRnoaccess);
2359 * Ensure minimum number of parameters sent.
2362 if (parameter_count < 4) {
2363 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2364 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2367 /* maybe we can check the quota_fnum */
2368 fsp = file_fsp(params,0);
2369 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2370 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2371 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2374 /* the NULL pointer checking for fsp->fake_file_handle->pd
2375 * is done by CHECK_NTQUOTA_HANDLE_OK()
2377 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2379 level = SVAL(params,2);
2381 /* unknown 12 bytes leading in params */
2383 switch (level) {
2384 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2385 /* seems that we should continue with the enum here --metze */
2387 if (qt_handle->quota_list!=NULL &&
2388 qt_handle->tmp_list==NULL) {
2390 /* free the list */
2391 free_ntquota_list(&(qt_handle->quota_list));
2393 /* Realloc the size of parameters and data we will return */
2394 param_len = 4;
2395 params = nttrans_realloc(ppparams, param_len);
2396 if(params == NULL) {
2397 return ERROR_DOS(ERRDOS,ERRnomem);
2400 data_len = 0;
2401 SIVAL(params,0,data_len);
2403 break;
2406 start_enum = False;
2408 case TRANSACT_GET_USER_QUOTA_LIST_START:
2410 if (qt_handle->quota_list==NULL &&
2411 qt_handle->tmp_list==NULL) {
2412 start_enum = True;
2415 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0)
2416 return ERROR_DOS(ERRSRV,ERRerror);
2418 /* Realloc the size of parameters and data we will return */
2419 param_len = 4;
2420 params = nttrans_realloc(ppparams, param_len);
2421 if(params == NULL) {
2422 return ERROR_DOS(ERRDOS,ERRnomem);
2425 /* we should not trust the value in max_data_count*/
2426 max_data_count = MIN(max_data_count,2048);
2428 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2429 if(pdata == NULL) {
2430 return ERROR_DOS(ERRDOS,ERRnomem);
2433 entry = pdata;
2435 /* set params Size of returned Quota Data 4 bytes*/
2436 /* but set it later when we know it */
2438 /* for each entry push the data */
2440 if (start_enum) {
2441 qt_handle->tmp_list = qt_handle->quota_list;
2444 tmp_list = qt_handle->tmp_list;
2446 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2447 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2449 sid_len = sid_size(&tmp_list->quotas->sid);
2450 entry_len = 40 + sid_len;
2452 /* nextoffset entry 4 bytes */
2453 SIVAL(entry,0,entry_len);
2455 /* then the len of the SID 4 bytes */
2456 SIVAL(entry,4,sid_len);
2458 /* unknown data 8 bytes SMB_BIG_UINT */
2459 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2461 /* the used disk space 8 bytes SMB_BIG_UINT */
2462 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2464 /* the soft quotas 8 bytes SMB_BIG_UINT */
2465 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2467 /* the hard quotas 8 bytes SMB_BIG_UINT */
2468 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2470 /* and now the SID */
2471 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2474 qt_handle->tmp_list = tmp_list;
2476 /* overwrite the offset of the last entry */
2477 SIVAL(entry-entry_len,0,0);
2479 data_len = 4+qt_len;
2480 /* overwrite the params quota_data_len */
2481 SIVAL(params,0,data_len);
2483 break;
2485 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2487 /* unknown 4 bytes IVAL(pdata,0) */
2489 if (data_count < 8) {
2490 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2491 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2494 sid_len = IVAL(pdata,4);
2495 /* Ensure this is less than 1mb. */
2496 if (sid_len > (1024*1024)) {
2497 return ERROR_DOS(ERRDOS,ERRnomem);
2500 if (data_count < 8+sid_len) {
2501 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2502 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2505 data_len = 4+40+sid_len;
2507 if (max_data_count < data_len) {
2508 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2509 max_data_count, data_len));
2510 param_len = 4;
2511 SIVAL(params,0,data_len);
2512 data_len = 0;
2513 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2514 break;
2517 sid_parse(pdata+8,sid_len,&sid);
2519 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2520 ZERO_STRUCT(qt);
2522 * we have to return zero's in all fields
2523 * instead of returning an error here
2524 * --metze
2528 /* Realloc the size of parameters and data we will return */
2529 param_len = 4;
2530 params = nttrans_realloc(ppparams, param_len);
2531 if(params == NULL) {
2532 return ERROR_DOS(ERRDOS,ERRnomem);
2535 pdata = nttrans_realloc(ppdata, data_len);
2536 if(pdata == NULL) {
2537 return ERROR_DOS(ERRDOS,ERRnomem);
2540 entry = pdata;
2542 /* set params Size of returned Quota Data 4 bytes*/
2543 SIVAL(params,0,data_len);
2545 /* nextoffset entry 4 bytes */
2546 SIVAL(entry,0,0);
2548 /* then the len of the SID 4 bytes */
2549 SIVAL(entry,4,sid_len);
2551 /* unknown data 8 bytes SMB_BIG_UINT */
2552 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2554 /* the used disk space 8 bytes SMB_BIG_UINT */
2555 SBIG_UINT(entry,16,qt.usedspace);
2557 /* the soft quotas 8 bytes SMB_BIG_UINT */
2558 SBIG_UINT(entry,24,qt.softlim);
2560 /* the hard quotas 8 bytes SMB_BIG_UINT */
2561 SBIG_UINT(entry,32,qt.hardlim);
2563 /* and now the SID */
2564 sid_linearize(entry+40, sid_len, &sid);
2566 break;
2568 default:
2569 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2570 return ERROR_DOS(ERRSRV,ERRerror);
2571 break;
2574 send_nt_replies(inbuf, outbuf, bufsize, nt_status, params, param_len, pdata, data_len);
2576 return -1;
2579 /****************************************************************************
2580 Reply to set user quota
2581 ****************************************************************************/
2583 static int call_nt_transact_set_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2584 uint16 **ppsetup, uint32 setup_count,
2585 char **ppparams, uint32 parameter_count,
2586 char **ppdata, uint32 data_count, uint32 max_data_count)
2588 char *params = *ppparams;
2589 char *pdata = *ppdata;
2590 int data_len=0,param_len=0;
2591 SMB_NTQUOTA_STRUCT qt;
2592 size_t sid_len;
2593 DOM_SID sid;
2594 files_struct *fsp = NULL;
2596 ZERO_STRUCT(qt);
2598 /* access check */
2599 if (current_user.ut.uid != 0) {
2600 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2601 lp_servicename(SNUM(conn)),conn->user));
2602 return ERROR_DOS(ERRDOS,ERRnoaccess);
2606 * Ensure minimum number of parameters sent.
2609 if (parameter_count < 2) {
2610 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2611 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2614 /* maybe we can check the quota_fnum */
2615 fsp = file_fsp(params,0);
2616 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2617 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2618 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2621 if (data_count < 40) {
2622 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2623 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2626 /* offset to next quota record.
2627 * 4 bytes IVAL(pdata,0)
2628 * unused here...
2631 /* sid len */
2632 sid_len = IVAL(pdata,4);
2634 if (data_count < 40+sid_len) {
2635 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2636 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2639 /* unknown 8 bytes in pdata
2640 * maybe its the change time in NTTIME
2643 /* the used space 8 bytes (SMB_BIG_UINT)*/
2644 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2645 #ifdef LARGE_SMB_OFF_T
2646 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2647 #else /* LARGE_SMB_OFF_T */
2648 if ((IVAL(pdata,20) != 0)&&
2649 ((qt.usedspace != 0xFFFFFFFF)||
2650 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2651 /* more than 32 bits? */
2652 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2654 #endif /* LARGE_SMB_OFF_T */
2656 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2657 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2658 #ifdef LARGE_SMB_OFF_T
2659 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2660 #else /* LARGE_SMB_OFF_T */
2661 if ((IVAL(pdata,28) != 0)&&
2662 ((qt.softlim != 0xFFFFFFFF)||
2663 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2664 /* more than 32 bits? */
2665 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2667 #endif /* LARGE_SMB_OFF_T */
2669 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2670 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2671 #ifdef LARGE_SMB_OFF_T
2672 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2673 #else /* LARGE_SMB_OFF_T */
2674 if ((IVAL(pdata,36) != 0)&&
2675 ((qt.hardlim != 0xFFFFFFFF)||
2676 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2677 /* more than 32 bits? */
2678 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2680 #endif /* LARGE_SMB_OFF_T */
2682 sid_parse(pdata+40,sid_len,&sid);
2683 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2685 /* 44 unknown bytes left... */
2687 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2688 return ERROR_DOS(ERRSRV,ERRerror);
2691 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, pdata, data_len);
2693 return -1;
2695 #endif /* HAVE_SYS_QUOTAS */
2697 static int handle_nttrans(connection_struct *conn,
2698 struct trans_state *state,
2699 char *inbuf, char *outbuf, int size, int bufsize)
2701 int outsize;
2703 if (Protocol >= PROTOCOL_NT1) {
2704 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | 0x40); /* IS_LONG_NAME */
2707 /* Now we must call the relevant NT_TRANS function */
2708 switch(state->call) {
2709 case NT_TRANSACT_CREATE:
2711 START_PROFILE_NESTED(NT_transact_create);
2712 outsize = call_nt_transact_create(conn, inbuf, outbuf,
2713 size, bufsize,
2714 &state->setup, state->setup_count,
2715 &state->param, state->total_param,
2716 &state->data, state->total_data,
2717 state->max_data_return);
2718 END_PROFILE_NESTED(NT_transact_create);
2719 break;
2722 case NT_TRANSACT_IOCTL:
2724 START_PROFILE_NESTED(NT_transact_ioctl);
2725 outsize = call_nt_transact_ioctl(conn, inbuf, outbuf,
2726 size, bufsize,
2727 &state->setup, state->setup_count,
2728 &state->param, state->total_param,
2729 &state->data, state->total_data, state->max_data_return);
2730 END_PROFILE_NESTED(NT_transact_ioctl);
2731 break;
2734 case NT_TRANSACT_SET_SECURITY_DESC:
2736 START_PROFILE_NESTED(NT_transact_set_security_desc);
2737 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
2738 size, bufsize,
2739 &state->setup, state->setup_count,
2740 &state->param, state->total_param,
2741 &state->data, state->total_data, state->max_data_return);
2742 END_PROFILE_NESTED(NT_transact_set_security_desc);
2743 break;
2746 case NT_TRANSACT_NOTIFY_CHANGE:
2748 START_PROFILE_NESTED(NT_transact_notify_change);
2749 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
2750 size, bufsize,
2751 &state->setup, state->setup_count,
2752 &state->param, state->total_param,
2753 &state->data, state->total_data, state->max_data_return);
2754 END_PROFILE_NESTED(NT_transact_notify_change);
2755 break;
2758 case NT_TRANSACT_RENAME:
2760 START_PROFILE_NESTED(NT_transact_rename);
2761 outsize = call_nt_transact_rename(conn, inbuf, outbuf,
2762 size, bufsize,
2763 &state->setup, state->setup_count,
2764 &state->param, state->total_param,
2765 &state->data, state->total_data, state->max_data_return);
2766 END_PROFILE_NESTED(NT_transact_rename);
2767 break;
2770 case NT_TRANSACT_QUERY_SECURITY_DESC:
2772 START_PROFILE_NESTED(NT_transact_query_security_desc);
2773 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
2774 size, bufsize,
2775 &state->setup, state->setup_count,
2776 &state->param, state->total_param,
2777 &state->data, state->total_data, state->max_data_return);
2778 END_PROFILE_NESTED(NT_transact_query_security_desc);
2779 break;
2782 #ifdef HAVE_SYS_QUOTAS
2783 case NT_TRANSACT_GET_USER_QUOTA:
2785 START_PROFILE_NESTED(NT_transact_get_user_quota);
2786 outsize = call_nt_transact_get_user_quota(conn, inbuf, outbuf,
2787 size, bufsize,
2788 &state->setup, state->setup_count,
2789 &state->param, state->total_param,
2790 &state->data, state->total_data, state->max_data_return);
2791 END_PROFILE_NESTED(NT_transact_get_user_quota);
2792 break;
2795 case NT_TRANSACT_SET_USER_QUOTA:
2797 START_PROFILE_NESTED(NT_transact_set_user_quota);
2798 outsize = call_nt_transact_set_user_quota(conn, inbuf, outbuf,
2799 size, bufsize,
2800 &state->setup, state->setup_count,
2801 &state->param, state->total_param,
2802 &state->data, state->total_data, state->max_data_return);
2803 END_PROFILE_NESTED(NT_transact_set_user_quota);
2804 break;
2806 #endif /* HAVE_SYS_QUOTAS */
2808 default:
2809 /* Error in request */
2810 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n",
2811 state->call));
2812 return ERROR_DOS(ERRSRV,ERRerror);
2814 return outsize;
2817 /****************************************************************************
2818 Reply to a SMBNTtrans.
2819 ****************************************************************************/
2821 int reply_nttrans(connection_struct *conn,
2822 char *inbuf,char *outbuf,int size,int bufsize)
2824 int outsize = 0;
2825 uint32 pscnt = IVAL(inbuf,smb_nt_ParameterCount);
2826 uint32 psoff = IVAL(inbuf,smb_nt_ParameterOffset);
2827 uint32 dscnt = IVAL(inbuf,smb_nt_DataCount);
2828 uint32 dsoff = IVAL(inbuf,smb_nt_DataOffset);
2830 uint16 function_code = SVAL( inbuf, smb_nt_Function);
2831 NTSTATUS result;
2832 struct trans_state *state;
2834 START_PROFILE(SMBnttrans);
2836 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2837 END_PROFILE(SMBnttrans);
2838 return ERROR_DOS(ERRSRV,ERRaccess);
2841 result = allow_new_trans(conn->pending_trans, SVAL(inbuf, smb_mid));
2842 if (!NT_STATUS_IS_OK(result)) {
2843 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2844 END_PROFILE(SMBnttrans);
2845 return ERROR_NT(result);
2848 if ((state = TALLOC_P(NULL, struct trans_state)) == NULL) {
2849 END_PROFILE(SMBnttrans);
2850 return ERROR_DOS(ERRSRV,ERRaccess);
2853 state->cmd = SMBnttrans;
2855 state->mid = SVAL(inbuf,smb_mid);
2856 state->vuid = SVAL(inbuf,smb_uid);
2857 state->total_data = IVAL(inbuf, smb_nt_TotalDataCount);
2858 state->data = NULL;
2859 state->total_param = IVAL(inbuf, smb_nt_TotalParameterCount);
2860 state->param = NULL;
2861 state->max_data_return = IVAL(inbuf,smb_nt_MaxDataCount);
2863 /* setup count is in *words* */
2864 state->setup_count = 2*CVAL(inbuf,smb_nt_SetupCount);
2865 state->call = function_code;
2868 * All nttrans messages we handle have smb_wct == 19 +
2869 * state->setup_count. Ensure this is so as a sanity check.
2872 if(CVAL(inbuf, smb_wct) != 19 + (state->setup_count/2)) {
2873 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2874 CVAL(inbuf, smb_wct), 19 + (state->setup_count/2)));
2875 goto bad_param;
2878 /* Don't allow more than 128mb for each value. */
2879 if ((state->total_data > (1024*1024*128)) ||
2880 (state->total_param > (1024*1024*128))) {
2881 END_PROFILE(SMBnttrans);
2882 return ERROR_DOS(ERRDOS,ERRnomem);
2885 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2886 goto bad_param;
2888 if (state->total_data) {
2889 /* Can't use talloc here, the core routines do realloc on the
2890 * params and data. */
2891 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2892 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2893 "bytes !\n", (unsigned int)state->total_data));
2894 TALLOC_FREE(state);
2895 END_PROFILE(SMBnttrans);
2896 return(ERROR_DOS(ERRDOS,ERRnomem));
2898 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
2899 goto bad_param;
2900 if ((smb_base(inbuf)+dsoff+dscnt > inbuf + size) ||
2901 (smb_base(inbuf)+dsoff+dscnt < smb_base(inbuf)))
2902 goto bad_param;
2904 memcpy(state->data,smb_base(inbuf)+dsoff,dscnt);
2907 if (state->total_param) {
2908 /* Can't use talloc here, the core routines do realloc on the
2909 * params and data. */
2910 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2911 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2912 "bytes !\n", (unsigned int)state->total_param));
2913 SAFE_FREE(state->data);
2914 TALLOC_FREE(state);
2915 END_PROFILE(SMBnttrans);
2916 return(ERROR_DOS(ERRDOS,ERRnomem));
2918 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
2919 goto bad_param;
2920 if ((smb_base(inbuf)+psoff+pscnt > inbuf + size) ||
2921 (smb_base(inbuf)+psoff+pscnt < smb_base(inbuf)))
2922 goto bad_param;
2924 memcpy(state->param,smb_base(inbuf)+psoff,pscnt);
2927 state->received_data = dscnt;
2928 state->received_param = pscnt;
2930 if(state->setup_count > 0) {
2931 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2932 state->setup_count));
2933 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2934 if (state->setup == NULL) {
2935 DEBUG(0,("reply_nttrans : Out of memory\n"));
2936 SAFE_FREE(state->data);
2937 SAFE_FREE(state->param);
2938 TALLOC_FREE(state);
2939 END_PROFILE(SMBnttrans);
2940 return ERROR_DOS(ERRDOS,ERRnomem);
2943 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
2944 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
2945 goto bad_param;
2947 if (smb_nt_SetupStart + state->setup_count > size) {
2948 goto bad_param;
2951 memcpy( state->setup, &inbuf[smb_nt_SetupStart], state->setup_count);
2952 dump_data(10, (char *)state->setup, state->setup_count);
2955 if ((state->received_data == state->total_data) &&
2956 (state->received_param == state->total_param)) {
2957 outsize = handle_nttrans(conn, state, inbuf, outbuf,
2958 size, bufsize);
2959 SAFE_FREE(state->param);
2960 SAFE_FREE(state->data);
2961 TALLOC_FREE(state);
2962 END_PROFILE(SMBnttrans);
2963 return outsize;
2966 DLIST_ADD(conn->pending_trans, state);
2968 /* We need to send an interim response then receive the rest
2969 of the parameter/data bytes */
2970 outsize = set_message(outbuf,0,0,False);
2971 show_msg(outbuf);
2972 END_PROFILE(SMBnttrans);
2973 return outsize;
2975 bad_param:
2977 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2978 SAFE_FREE(state->data);
2979 SAFE_FREE(state->param);
2980 TALLOC_FREE(state);
2981 END_PROFILE(SMBnttrans);
2982 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2985 /****************************************************************************
2986 Reply to a SMBnttranss
2987 ****************************************************************************/
2989 int reply_nttranss(connection_struct *conn, char *inbuf,char *outbuf,
2990 int size,int bufsize)
2992 int outsize = 0;
2993 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
2994 struct trans_state *state;
2996 START_PROFILE(SMBnttranss);
2998 show_msg(inbuf);
3000 for (state = conn->pending_trans; state != NULL;
3001 state = state->next) {
3002 if (state->mid == SVAL(inbuf,smb_mid)) {
3003 break;
3007 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3008 END_PROFILE(SMBnttranss);
3009 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
3012 /* Revise state->total_param and state->total_data in case they have
3013 changed downwards */
3014 if (IVAL(inbuf, smb_nts_TotalParameterCount) < state->total_param) {
3015 state->total_param = IVAL(inbuf, smb_nts_TotalParameterCount);
3017 if (IVAL(inbuf, smb_nts_TotalDataCount) < state->total_data) {
3018 state->total_data = IVAL(inbuf, smb_nts_TotalDataCount);
3021 pcnt = IVAL(inbuf,smb_nts_ParameterCount);
3022 poff = IVAL(inbuf, smb_nts_ParameterOffset);
3023 pdisp = IVAL(inbuf, smb_nts_ParameterDisplacement);
3025 dcnt = IVAL(inbuf, smb_nts_DataCount);
3026 ddisp = IVAL(inbuf, smb_nts_DataDisplacement);
3027 doff = IVAL(inbuf, smb_nts_DataOffset);
3029 state->received_param += pcnt;
3030 state->received_data += dcnt;
3032 if ((state->received_data > state->total_data) ||
3033 (state->received_param > state->total_param))
3034 goto bad_param;
3036 if (pcnt) {
3037 if (pdisp+pcnt > state->total_param)
3038 goto bad_param;
3039 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3040 goto bad_param;
3041 if (pdisp > state->total_param)
3042 goto bad_param;
3043 if ((smb_base(inbuf) + poff + pcnt > inbuf + size) ||
3044 (smb_base(inbuf) + poff + pcnt < smb_base(inbuf)))
3045 goto bad_param;
3046 if (state->param + pdisp < state->param)
3047 goto bad_param;
3049 memcpy(state->param+pdisp,smb_base(inbuf)+poff,
3050 pcnt);
3053 if (dcnt) {
3054 if (ddisp+dcnt > state->total_data)
3055 goto bad_param;
3056 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
3057 goto bad_param;
3058 if (ddisp > state->total_data)
3059 goto bad_param;
3060 if ((smb_base(inbuf) + doff + dcnt > inbuf + size) ||
3061 (smb_base(inbuf) + doff + dcnt < smb_base(inbuf)))
3062 goto bad_param;
3063 if (state->data + ddisp < state->data)
3064 goto bad_param;
3066 memcpy(state->data+ddisp, smb_base(inbuf)+doff,
3067 dcnt);
3070 if ((state->received_param < state->total_param) ||
3071 (state->received_data < state->total_data)) {
3072 END_PROFILE(SMBnttranss);
3073 return -1;
3076 /* construct_reply_common has done us the favor to pre-fill the
3077 * command field with SMBnttranss which is wrong :-)
3079 SCVAL(outbuf,smb_com,SMBnttrans);
3081 outsize = handle_nttrans(conn, state, inbuf, outbuf,
3082 size, bufsize);
3084 DLIST_REMOVE(conn->pending_trans, state);
3085 SAFE_FREE(state->data);
3086 SAFE_FREE(state->param);
3087 TALLOC_FREE(state);
3089 if (outsize == 0) {
3090 END_PROFILE(SMBnttranss);
3091 return(ERROR_DOS(ERRSRV,ERRnosupport));
3094 END_PROFILE(SMBnttranss);
3095 return(outsize);
3097 bad_param:
3099 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3100 DLIST_REMOVE(conn->pending_trans, state);
3101 SAFE_FREE(state->data);
3102 SAFE_FREE(state->param);
3103 TALLOC_FREE(state);
3104 END_PROFILE(SMBnttranss);
3105 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);