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.
25 extern int smb_read_error;
26 extern int global_oplock_break;
27 extern struct current_user current_user;
29 static const char *known_nt_pipes[] = {
46 /* Map generic permissions to file object specific permissions */
48 struct generic_mapping file_generic_mapping = {
55 static char *nttrans_realloc(char **ptr, size_t size)
59 smb_panic("nttrans_realloc() called with NULL ptr\n");
61 tptr = Realloc_zero(*ptr, size);
73 /****************************************************************************
74 Send the required number of replies back.
75 We assume all fields other than the data fields are
76 set correctly for the type of call.
77 HACK ! Always assumes smb_setup field is zero.
78 ****************************************************************************/
80 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, NTSTATUS nt_error, char *params,
81 int paramsize, char *pdata, int datasize)
84 int data_to_send = datasize;
85 int params_to_send = paramsize;
89 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
90 int alignment_offset = 3;
91 int data_alignment_offset = 0;
94 * Initially set the wcnt area to be 18 - this is true for all
98 set_message(outbuf,18,0,True);
100 if (NT_STATUS_V(nt_error))
104 * If there genuinely are no parameters or data to send just send
108 if(params_to_send == 0 && data_to_send == 0) {
109 if (!send_smb(smbd_server_fd(),outbuf))
110 exit_server("send_nt_replies: send_smb failed.");
115 * When sending params and data ensure that both are nicely aligned.
116 * Only do this alignment when there is also data to send - else
117 * can cause NT redirector problems.
120 if (((params_to_send % 4) != 0) && (data_to_send != 0))
121 data_alignment_offset = 4 - (params_to_send % 4);
124 * Space is bufsize minus Netbios over TCP header minus SMB header.
125 * The alignment_offset is to align the param bytes on a four byte
126 * boundary (2 bytes for data len, one byte pad).
127 * NT needs this to work correctly.
130 useable_space = bufsize - ((smb_buf(outbuf)+
131 alignment_offset+data_alignment_offset) -
135 * useable_space can never be more than max_send minus the
139 useable_space = MIN(useable_space,
140 max_send - (alignment_offset+data_alignment_offset));
143 while (params_to_send || data_to_send) {
146 * Calculate whether we will totally or partially fill this packet.
149 total_sent_thistime = params_to_send + data_to_send +
150 alignment_offset + data_alignment_offset;
153 * We can never send more than useable_space.
156 total_sent_thistime = MIN(total_sent_thistime, useable_space);
158 set_message(outbuf, 18, total_sent_thistime, True);
161 * Set total params and data to be sent.
164 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
165 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
168 * Calculate how many parameters and data we can fit into
169 * this packet. Parameters get precedence.
172 params_sent_thistime = MIN(params_to_send,useable_space);
173 data_sent_thistime = useable_space - params_sent_thistime;
174 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
176 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
178 if(params_sent_thistime == 0) {
179 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
180 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
183 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
184 * parameter bytes, however the first 4 bytes of outbuf are
185 * the Netbios over TCP header. Thus use smb_base() to subtract
186 * them from the calculation.
189 SIVAL(outbuf,smb_ntr_ParameterOffset,
190 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
192 * Absolute displacement of param bytes sent in this packet.
195 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
199 * Deal with the data portion.
202 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
204 if(data_sent_thistime == 0) {
205 SIVAL(outbuf,smb_ntr_DataOffset,0);
206 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
209 * The offset of the data bytes is the offset of the
210 * parameter bytes plus the number of parameters being sent this time.
213 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
214 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
215 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
219 * Copy the param bytes into the packet.
222 if(params_sent_thistime)
223 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
226 * Copy in the data bytes
229 if(data_sent_thistime)
230 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
231 data_alignment_offset,pd,data_sent_thistime);
233 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
234 params_sent_thistime, data_sent_thistime, useable_space));
235 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
236 params_to_send, data_to_send, paramsize, datasize));
238 /* Send the packet */
239 if (!send_smb(smbd_server_fd(),outbuf))
240 exit_server("send_nt_replies: send_smb failed.");
242 pp += params_sent_thistime;
243 pd += data_sent_thistime;
245 params_to_send -= params_sent_thistime;
246 data_to_send -= data_sent_thistime;
252 if(params_to_send < 0 || data_to_send < 0) {
253 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
254 params_to_send, data_to_send));
262 /****************************************************************************
264 ****************************************************************************/
266 static BOOL saved_case_sensitive;
267 static BOOL saved_case_preserve;
268 static BOOL saved_short_case_preserve;
270 /****************************************************************************
272 ****************************************************************************/
274 static void set_posix_case_semantics(connection_struct *conn, uint32 file_attributes)
276 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
279 saved_case_sensitive = conn->case_sensitive;
280 saved_case_preserve = conn->case_preserve;
281 saved_short_case_preserve = conn->short_case_preserve;
284 conn->case_sensitive = True;
285 conn->case_preserve = True;
286 conn->short_case_preserve = True;
289 /****************************************************************************
290 Restore case semantics.
291 ****************************************************************************/
293 static void restore_case_semantics(connection_struct *conn, uint32 file_attributes)
295 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
298 conn->case_sensitive = saved_case_sensitive;
299 conn->case_preserve = saved_case_preserve;
300 conn->short_case_preserve = saved_short_case_preserve;
303 /****************************************************************************
304 Utility function to map create disposition.
305 ****************************************************************************/
307 static int map_create_disposition( uint32 create_disposition)
311 switch( create_disposition ) {
313 /* create if not exist, fail if exist */
314 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL);
317 case FILE_OVERWRITE_IF:
318 /* create if not exist, trunc if exist */
319 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
322 /* fail if not exist, open if exists */
323 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN);
326 /* create if not exist, open if exists */
327 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_OPEN);
330 /* fail if not exist, truncate if exists */
331 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
334 DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
335 create_disposition ));
339 DEBUG(10,("map_create_disposition: Mapped create_disposition 0x%lx to 0x%x\n",
340 (unsigned long)create_disposition, ret ));
345 /****************************************************************************
346 Utility function to map share modes.
347 ****************************************************************************/
349 static int map_share_mode( char *fname, uint32 create_options,
350 uint32 *desired_access, uint32 share_access, uint32 file_attributes)
352 int smb_open_mode = -1;
353 uint32 original_desired_access = *desired_access;
356 * Convert GENERIC bits to specific bits.
359 se_map_generic(desired_access, &file_generic_mapping);
361 switch( *desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
363 smb_open_mode = DOS_OPEN_RDONLY;
365 case FILE_WRITE_DATA:
366 case FILE_APPEND_DATA:
367 case FILE_WRITE_DATA|FILE_APPEND_DATA:
368 smb_open_mode = DOS_OPEN_WRONLY;
370 case FILE_READ_DATA|FILE_WRITE_DATA:
371 case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
372 case FILE_READ_DATA|FILE_APPEND_DATA:
373 smb_open_mode = DOS_OPEN_RDWR;
378 * NB. For DELETE_ACCESS we should really check the
379 * directory permissions, as that is what controls
380 * delete, and for WRITE_DAC_ACCESS we should really
381 * check the ownership, as that is what controls the
382 * chmod. Note that this is *NOT* a security hole (this
383 * note is for you, Andrew) as we are not *allowing*
384 * the access at this point, the actual unlink or
385 * chown or chmod call would do this. We are just helping
386 * clients out by telling them if they have a hope
387 * of any of this succeeding. POSIX acls may still
388 * deny the real call. JRA.
391 if (smb_open_mode == -1) {
393 if(*desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS|
394 FILE_EXECUTE|FILE_READ_ATTRIBUTES|
395 FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
396 FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS)) {
397 smb_open_mode = DOS_OPEN_RDONLY;
398 } else if(*desired_access == 0) {
401 * JRA - NT seems to sometimes send desired_access as zero. play it safe
402 * and map to a stat open.
405 smb_open_mode = DOS_OPEN_RDONLY;
408 DEBUG(0,("map_share_mode: Incorrect value 0x%lx for desired_access to file %s\n",
409 (unsigned long)*desired_access, fname));
415 * Set the special bit that means allow share delete.
416 * This is held outside the normal share mode bits at 1<<15.
420 if(share_access & FILE_SHARE_DELETE) {
421 smb_open_mode |= ALLOW_SHARE_DELETE;
422 DEBUG(10,("map_share_mode: FILE_SHARE_DELETE requested. open_mode = 0x%x\n", smb_open_mode));
425 if(*desired_access & DELETE_ACCESS) {
426 DEBUG(10,("map_share_mode: DELETE_ACCESS requested. open_mode = 0x%x\n", smb_open_mode));
430 * We need to store the intent to open for Delete. This
431 * is what determines if a delete on close flag can be set.
432 * This is the wrong way (and place) to store this, but for 2.2 this
433 * is the only practical way. JRA.
436 if (create_options & FILE_DELETE_ON_CLOSE) {
438 * W2K3 bug compatibility mode... To set delete on close
439 * the redirector must have *specifically* set DELETE_ACCESS
440 * in the desired_access field. Just asking for GENERIC_ALL won't do. JRA.
443 if (!(original_desired_access & DELETE_ACCESS)) {
444 DEBUG(5,("map_share_mode: FILE_DELETE_ON_CLOSE requested without \
445 DELETE_ACCESS for file %s. (desired_access = 0x%lx)\n",
446 fname, (unsigned long)*desired_access));
449 /* Implicit delete access is *NOT* requested... */
450 smb_open_mode |= DELETE_ON_CLOSE_FLAG;
451 DEBUG(10,("map_share_mode: FILE_DELETE_ON_CLOSE requested. open_mode = 0x%x\n", smb_open_mode));
454 /* Add in the requested share mode. */
455 switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
456 case FILE_SHARE_READ:
457 smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
459 case FILE_SHARE_WRITE:
460 smb_open_mode |= SET_DENY_MODE(DENY_READ);
462 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
463 smb_open_mode |= SET_DENY_MODE(DENY_NONE);
465 case FILE_SHARE_NONE:
466 smb_open_mode |= SET_DENY_MODE(DENY_ALL);
471 * Handle an O_SYNC request.
474 if(file_attributes & FILE_FLAG_WRITE_THROUGH)
475 smb_open_mode |= FILE_SYNC_OPENMODE;
477 DEBUG(10,("map_share_mode: Mapped desired access 0x%lx, share access 0x%lx, file attributes 0x%lx \
478 to open_mode 0x%x\n", (unsigned long)*desired_access, (unsigned long)share_access,
479 (unsigned long)file_attributes, smb_open_mode ));
481 return smb_open_mode;
484 /****************************************************************************
485 Reply to an NT create and X call on a pipe.
486 ****************************************************************************/
488 static int nt_open_pipe(char *fname, connection_struct *conn,
489 char *inbuf, char *outbuf, int *ppnum)
491 smb_np_struct *p = NULL;
493 uint16 vuid = SVAL(inbuf, smb_uid);
496 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
498 /* See if it is one we want to handle. */
500 if (lp_disable_spoolss() && strequal(fname, "\\spoolss"))
501 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
503 for( i = 0; known_nt_pipes[i]; i++ )
504 if( strequal(fname,known_nt_pipes[i]))
507 if ( known_nt_pipes[i] == NULL )
508 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
510 /* Strip \\ off the name. */
513 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
515 p = open_rpc_pipe_p(fname, conn, vuid);
517 return(ERROR_DOS(ERRSRV,ERRnofids));
524 /****************************************************************************
525 Reply to an NT create and X call for pipes.
526 ****************************************************************************/
528 static int do_ntcreate_pipe_open(connection_struct *conn,
529 char *inbuf,char *outbuf,int length,int bufsize)
536 srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
538 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
542 * Deal with pipe return.
545 set_message(outbuf,34,0,True);
547 p = outbuf + smb_vwv2;
551 SIVAL(p,0,FILE_WAS_OPENED);
554 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
557 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
559 SSVAL(p,2, 0x5FF); /* ? */
561 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
563 return chain_reply(inbuf,outbuf,length,bufsize);
566 /****************************************************************************
567 Reply to an NT create and X call.
568 ****************************************************************************/
570 int reply_ntcreate_and_X(connection_struct *conn,
571 char *inbuf,char *outbuf,int length,int bufsize)
575 enum FAKE_FILE_TYPE fake_file_type = FAKE_FILE_TYPE_NONE;
576 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
577 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
578 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
579 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
580 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
581 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
582 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
583 SMB_BIG_UINT allocation_size = 0;
586 /* Breakout the oplock request bits so we can set the
587 reply bits separately. */
588 int oplock_request = 0;
590 SMB_OFF_T file_len = 0;
591 SMB_STRUCT_STAT sbuf;
593 BOOL bad_path = False;
594 files_struct *fsp=NULL;
597 BOOL extended_oplock_granted = False;
600 START_PROFILE(SMBntcreateX);
602 DEBUG(10,("reply_ntcreateX: flags = 0x%x, desired_access = 0x%x \
603 file_attributes = 0x%x, share_access = 0x%x, create_disposition = 0x%x \
604 create_options = 0x%x root_dir_fid = 0x%x\n", flags, desired_access, file_attributes,
605 share_access, create_disposition,
606 create_options, root_dir_fid ));
608 /* If it's an IPC, use the pipe handler. */
611 if (lp_nt_pipe_support()) {
612 END_PROFILE(SMBntcreateX);
613 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
615 END_PROFILE(SMBntcreateX);
616 return(ERROR_DOS(ERRDOS,ERRnoaccess));
620 if (create_options & FILE_OPEN_BY_FILE_ID) {
621 END_PROFILE(SMBntcreateX);
622 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
626 * We need to construct the open_and_X ofun value from the
627 * NT values, as that's what our code is structured to accept.
630 if((smb_ofun = map_create_disposition( create_disposition )) == -1) {
631 END_PROFILE(SMBntcreateX);
632 return(ERROR_DOS(ERRDOS,ERRnoaccess));
639 if(root_dir_fid != 0) {
641 * This filename is relative to a directory fid.
644 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
648 END_PROFILE(SMBntcreateX);
649 return(ERROR_DOS(ERRDOS,ERRbadfid));
652 if(!dir_fsp->is_directory) {
654 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status,False);
655 if (!NT_STATUS_IS_OK(status)) {
656 END_PROFILE(SMBntcreateX);
657 return ERROR_NT(status);
661 * Check to see if this is a mac fork of some kind.
664 if( strchr_m(fname, ':')) {
665 END_PROFILE(SMBntcreateX);
666 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
670 we need to handle the case when we get a
671 relative open relative to a file and the
672 pathname is blank - this is a reopen!
673 (hint from demyn plantenberg)
676 END_PROFILE(SMBntcreateX);
677 return(ERROR_DOS(ERRDOS,ERRbadfid));
681 * Copy in the base directory name.
684 pstrcpy( fname, dir_fsp->fsp_name );
685 dir_name_len = strlen(fname);
688 * Ensure it ends in a '\'.
691 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
696 srvstr_get_path(inbuf, rel_fname, smb_buf(inbuf), sizeof(rel_fname), 0, STR_TERMINATE, &status,False);
697 if (!NT_STATUS_IS_OK(status)) {
698 END_PROFILE(SMBntcreateX);
699 return ERROR_NT(status);
701 pstrcat(fname, rel_fname);
703 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status,False);
704 if (!NT_STATUS_IS_OK(status)) {
705 END_PROFILE(SMBntcreateX);
706 return ERROR_NT(status);
710 * Check to see if this is a mac fork of some kind.
713 if( strchr_m(fname, ':')) {
715 #ifdef HAVE_SYS_QUOTAS
716 if ((fake_file_type=is_fake_file(fname))!=FAKE_FILE_TYPE_NONE) {
718 * here we go! support for changing the disk quotas --metze
720 * we need to fake up to open this MAGIC QUOTA file
721 * and return a valid FID
723 * w2k close this file directly after openening
724 * xp also tries a QUERY_FILE_INFO on the file and then close it
728 END_PROFILE(SMBntcreateX);
729 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
730 #ifdef HAVE_SYS_QUOTAS
737 * Now contruct the smb_open_mode value from the filename,
738 * desired access and the share access.
740 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
742 if((smb_open_mode = map_share_mode(fname, create_options, &desired_access,
744 file_attributes)) == -1) {
745 END_PROFILE(SMBntcreateX);
746 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
749 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
750 if (oplock_request) {
751 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
755 * Ordinary file or directory.
759 * Check if POSIX semantics are wanted.
762 set_posix_case_semantics(conn, file_attributes);
764 unix_convert(fname,conn,0,&bad_path,&sbuf);
766 restore_case_semantics(conn, file_attributes);
767 END_PROFILE(SMBntcreateX);
768 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
770 /* All file access must go through check_name() */
771 if (!check_name(fname,conn)) {
772 restore_case_semantics(conn, file_attributes);
773 END_PROFILE(SMBntcreateX);
774 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRbadpath);
778 * If it's a request for a directory open, deal with it separately.
781 if(create_options & FILE_DIRECTORY_FILE) {
784 /* Can't open a temp directory. IFS kit test. */
785 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
786 END_PROFILE(SMBntcreateX);
787 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
790 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, &smb_action);
792 restore_case_semantics(conn, file_attributes);
795 END_PROFILE(SMBntcreateX);
796 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
800 * Ordinary file case.
803 /* NB. We have a potential bug here. If we
804 * cause an oplock break to ourselves, then we
805 * could end up processing filename related
806 * SMB requests whilst we await the oplock
807 * break response. As we may have changed the
808 * filename case semantics to be POSIX-like,
809 * this could mean a filename request could
810 * fail when it should succeed. This is a rare
811 * condition, but eventually we must arrange
812 * to restore the correct case semantics
813 * before issuing an oplock break request to
814 * our client. JRA. */
816 if (fake_file_type==FAKE_FILE_TYPE_NONE) {
817 fsp = open_file_shared1(conn,fname,&sbuf,
820 smb_ofun,file_attributes,oplock_request,
823 /* to open a fake_file --metze */
824 fsp = open_fake_file_shared1(fake_file_type,conn,fname,&sbuf,
827 smb_ofun,file_attributes, oplock_request,
832 /* We cheat here. There are two cases we
833 * care about. One is a directory rename,
834 * where the NT client will attempt to
835 * open the source directory for
836 * DELETE access. Note that when the
837 * NT client does this it does *not*
838 * set the directory bit in the
839 * request packet. This is translated
840 * into a read/write open
841 * request. POSIX states that any open
842 * for write request on a directory
843 * will generate an EISDIR error, so
844 * we can catch this here and open a
845 * pseudo handle that is flagged as a
846 * directory. The second is an open
847 * for a permissions read only, which
848 * we handle in the open_file_stat case. JRA.
851 if(errno == EISDIR) {
854 * Fail the open if it was explicitly a non-directory file.
857 if (create_options & FILE_NON_DIRECTORY_FILE) {
858 restore_case_semantics(conn, file_attributes);
859 SSVAL(outbuf, smb_flg2,
860 SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
861 END_PROFILE(SMBntcreateX);
862 return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
866 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, &smb_action);
869 restore_case_semantics(conn, file_attributes);
870 END_PROFILE(SMBntcreateX);
871 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
875 restore_case_semantics(conn, file_attributes);
876 END_PROFILE(SMBntcreateX);
877 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
878 /* We have re-scheduled this call. */
879 clear_cached_errors();
882 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
887 restore_case_semantics(conn, file_attributes);
889 file_len = sbuf.st_size;
890 fmode = dos_mode(conn,fname,&sbuf);
892 fmode = FILE_ATTRIBUTE_NORMAL;
893 if (!fsp->is_directory && (fmode & aDIR)) {
894 close_file(fsp,False);
895 END_PROFILE(SMBntcreateX);
896 return ERROR_DOS(ERRDOS,ERRnoaccess);
899 /* Save the requested allocation size. */
900 allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize);
901 #ifdef LARGE_SMB_OFF_T
902 allocation_size |= (((SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
904 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
905 fsp->initial_allocation_size = SMB_ROUNDUP(allocation_size,SMB_ROUNDUP_ALLOCATION_SIZE);
906 if (fsp->is_directory) {
907 close_file(fsp,False);
908 END_PROFILE(SMBntcreateX);
909 /* Can't set allocation size on a directory. */
910 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
912 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
913 close_file(fsp,False);
914 END_PROFILE(SMBntcreateX);
915 return ERROR_NT(NT_STATUS_DISK_FULL);
918 fsp->initial_allocation_size = SMB_ROUNDUP(((SMB_BIG_UINT)file_len),SMB_ROUNDUP_ALLOCATION_SIZE);
922 * If the caller set the extended oplock request bit
923 * and we granted one (by whatever means) - set the
924 * correct bit for extended oplock reply.
927 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
928 extended_oplock_granted = True;
930 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
931 extended_oplock_granted = True;
934 /* W2K sends back 42 words here ! If we do the same it breaks offline sync. Go figure... ? JRA. */
935 set_message(outbuf,42,0,True);
937 set_message(outbuf,34,0,True);
940 p = outbuf + smb_vwv2;
943 * Currently as we don't support level II oplocks we just report
944 * exclusive & batch here.
947 if (extended_oplock_granted) {
948 if (flags & REQUEST_BATCH_OPLOCK) {
949 SCVAL(p,0, BATCH_OPLOCK_RETURN);
951 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
953 } else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
954 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
956 SCVAL(p,0,NO_OPLOCK_RETURN);
960 SSVAL(p,0,fsp->fnum);
962 if ((create_disposition == FILE_SUPERSEDE) && (smb_action == FILE_WAS_OVERWRITTEN))
963 SIVAL(p,0,FILE_WAS_SUPERSEDED);
965 SIVAL(p,0,smb_action);
969 c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
971 if (lp_dos_filetime_resolution(SNUM(conn))) {
978 put_long_date(p,c_time);
980 put_long_date(p,sbuf.st_atime); /* access time */
982 put_long_date(p,sbuf.st_mtime); /* write time */
984 put_long_date(p,sbuf.st_mtime); /* change time */
986 SIVAL(p,0,fmode); /* File Attributes. */
988 SOFF_T(p, 0, get_allocation_size(fsp,&sbuf));
990 SOFF_T(p,0,file_len);
992 if (flags & EXTENDED_RESPONSE_REQUIRED)
995 SCVAL(p,0,fsp->is_directory ? 1 : 0);
997 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
999 result = chain_reply(inbuf,outbuf,length,bufsize);
1000 END_PROFILE(SMBntcreateX);
1004 /****************************************************************************
1005 Reply to a NT_TRANSACT_CREATE call to open a pipe.
1006 ****************************************************************************/
1008 static int do_nt_transact_create_pipe( connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1009 char **ppsetup, uint32 setup_count,
1010 char **ppparams, uint32 parameter_count,
1011 char **ppdata, uint32 data_count)
1014 char *params = *ppparams;
1021 * Ensure minimum number of parameters sent.
1024 if(parameter_count < 54) {
1025 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1026 return ERROR_DOS(ERRDOS,ERRnoaccess);
1029 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status, False);
1030 if (!NT_STATUS_IS_OK(status)) {
1031 return ERROR_NT(status);
1034 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
1037 /* Realloc the size of parameters and data we will return */
1038 params = nttrans_realloc(ppparams, 69);
1040 return ERROR_DOS(ERRDOS,ERRnomem);
1043 SCVAL(p,0,NO_OPLOCK_RETURN);
1048 SIVAL(p,0,FILE_WAS_OPENED);
1052 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1055 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1057 SSVAL(p,2, 0x5FF); /* ? */
1059 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1061 /* Send the required number of replies */
1062 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1067 /****************************************************************************
1068 Internal fn to set security descriptors.
1069 ****************************************************************************/
1071 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1074 SEC_DESC *psd = NULL;
1075 TALLOC_CTX *mem_ctx;
1079 return NT_STATUS_OK;
1083 * Init the parse struct we will unmarshall from.
1086 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1087 DEBUG(0,("set_sd: talloc_init failed.\n"));
1088 return NT_STATUS_NO_MEMORY;
1091 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1094 * Setup the prs_struct to point at the memory we just
1098 prs_give_memory( &pd, data, sd_len, False);
1101 * Finally, unmarshall from the data buffer.
1104 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1105 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1107 * Return access denied for want of a better error message..
1109 talloc_destroy(mem_ctx);
1110 return NT_STATUS_NO_MEMORY;
1113 if (psd->off_owner_sid==0)
1114 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1115 if (psd->off_grp_sid==0)
1116 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1117 if (psd->off_sacl==0)
1118 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1119 if (psd->off_dacl==0)
1120 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1122 ret = SMB_VFS_FSET_NT_ACL( fsp, fsp->fd, security_info_sent, psd);
1125 talloc_destroy(mem_ctx);
1126 return NT_STATUS_ACCESS_DENIED;
1129 talloc_destroy(mem_ctx);
1131 return NT_STATUS_OK;
1134 /****************************************************************************
1135 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1136 ****************************************************************************/
1138 static int call_nt_transact_create(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1139 char **ppsetup, uint32 setup_count,
1140 char **ppparams, uint32 parameter_count,
1141 char **ppdata, uint32 data_count)
1144 char *params = *ppparams;
1145 char *data = *ppdata;
1146 /* Breakout the oplock request bits so we can set the reply bits separately. */
1147 int oplock_request = 0;
1148 int fmode=0,rmode=0;
1149 SMB_OFF_T file_len = 0;
1150 SMB_STRUCT_STAT sbuf;
1152 BOOL bad_path = False;
1153 files_struct *fsp = NULL;
1155 BOOL extended_oplock_granted = False;
1157 uint32 desired_access;
1158 uint32 file_attributes;
1159 uint32 share_access;
1160 uint32 create_disposition;
1161 uint32 create_options;
1163 uint16 root_dir_fid;
1164 SMB_BIG_UINT allocation_size = 0;
1170 DEBUG(5,("call_nt_transact_create\n"));
1173 * If it's an IPC, use the pipe handler.
1177 if (lp_nt_pipe_support())
1178 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1180 ppsetup, setup_count,
1181 ppparams, parameter_count,
1182 ppdata, data_count);
1184 return ERROR_DOS(ERRDOS,ERRnoaccess);
1188 * Ensure minimum number of parameters sent.
1191 if(parameter_count < 54) {
1192 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1193 return ERROR_DOS(ERRDOS,ERRnoaccess);
1196 flags = IVAL(params,0);
1197 desired_access = IVAL(params,8);
1198 file_attributes = IVAL(params,20);
1199 share_access = IVAL(params,24);
1200 create_disposition = IVAL(params,28);
1201 create_options = IVAL(params,32);
1202 sd_len = IVAL(params,36);
1203 root_dir_fid = (uint16)IVAL(params,4);
1205 if (create_options & FILE_OPEN_BY_FILE_ID) {
1206 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1210 * We need to construct the open_and_X ofun value from the
1211 * NT values, as that's what our code is structured to accept.
1214 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
1215 return ERROR_DOS(ERRDOS,ERRbadmem);
1218 * Get the file name.
1221 if(root_dir_fid != 0) {
1223 * This filename is relative to a directory fid.
1225 files_struct *dir_fsp = file_fsp(params,4);
1226 size_t dir_name_len;
1229 return ERROR_DOS(ERRDOS,ERRbadfid);
1231 if(!dir_fsp->is_directory) {
1232 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status, False);
1233 if (!NT_STATUS_IS_OK(status)) {
1234 return ERROR_NT(status);
1238 * Check to see if this is a mac fork of some kind.
1241 if( strchr_m(fname, ':'))
1242 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1244 return ERROR_DOS(ERRDOS,ERRbadfid);
1248 * Copy in the base directory name.
1251 pstrcpy( fname, dir_fsp->fsp_name );
1252 dir_name_len = strlen(fname);
1255 * Ensure it ends in a '\'.
1258 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1259 pstrcat(fname, "/");
1265 srvstr_get_path(inbuf, tmpname, params+53, sizeof(tmpname), parameter_count-53, STR_TERMINATE, &status, False);
1266 if (!NT_STATUS_IS_OK(status)) {
1267 return ERROR_NT(status);
1269 pstrcat(fname, tmpname);
1272 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status, False);
1273 if (!NT_STATUS_IS_OK(status)) {
1274 return ERROR_NT(status);
1278 * Check to see if this is a mac fork of some kind.
1281 if( strchr_m(fname, ':'))
1282 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1286 * Now contruct the smb_open_mode value from the desired access
1287 * and the share access.
1290 if((smb_open_mode = map_share_mode( fname, create_options, &desired_access,
1291 share_access, file_attributes)) == -1)
1292 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1294 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1295 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1298 * Check if POSIX semantics are wanted.
1301 set_posix_case_semantics(conn, file_attributes);
1303 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1305 unix_convert(fname,conn,0,&bad_path,&sbuf);
1307 restore_case_semantics(conn, file_attributes);
1308 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1310 /* All file access must go through check_name() */
1311 if (!check_name(fname,conn)) {
1312 restore_case_semantics(conn, file_attributes);
1313 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRbadpath);
1317 * If it's a request for a directory open, deal with it separately.
1320 if(create_options & FILE_DIRECTORY_FILE) {
1322 /* Can't open a temp directory. IFS kit test. */
1323 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1324 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1330 * We will get a create directory here if the Win32
1331 * app specified a security descriptor in the
1332 * CreateDirectory() call.
1335 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, &smb_action);
1338 restore_case_semantics(conn, file_attributes);
1339 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1345 * Ordinary file case.
1348 fsp = open_file_shared1(conn,fname,&sbuf,desired_access,
1349 smb_open_mode,smb_ofun,file_attributes,
1350 oplock_request,&rmode,&smb_action);
1354 if(errno == EISDIR) {
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 SSVAL(outbuf, smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1363 return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1367 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, &smb_action);
1370 restore_case_semantics(conn, file_attributes);
1371 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1374 restore_case_semantics(conn, file_attributes);
1375 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1376 /* We have re-scheduled this call. */
1377 clear_cached_errors();
1380 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1384 file_len = sbuf.st_size;
1385 fmode = dos_mode(conn,fname,&sbuf);
1387 fmode = FILE_ATTRIBUTE_NORMAL;
1390 close_file(fsp,False);
1391 restore_case_semantics(conn, file_attributes);
1392 return ERROR_DOS(ERRDOS,ERRnoaccess);
1396 * If the caller set the extended oplock request bit
1397 * and we granted one (by whatever means) - set the
1398 * correct bit for extended oplock reply.
1401 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1402 extended_oplock_granted = True;
1404 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1405 extended_oplock_granted = True;
1409 * Now try and apply the desired SD.
1412 if (sd_len && !NT_STATUS_IS_OK(status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION))) {
1413 close_file(fsp,False);
1414 restore_case_semantics(conn, file_attributes);
1415 return ERROR_NT(status);
1418 restore_case_semantics(conn, file_attributes);
1420 /* Save the requested allocation size. */
1421 allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1422 #ifdef LARGE_SMB_OFF_T
1423 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1425 if (allocation_size && (allocation_size > file_len)) {
1426 fsp->initial_allocation_size = SMB_ROUNDUP(allocation_size,SMB_ROUNDUP_ALLOCATION_SIZE);
1427 if (fsp->is_directory) {
1428 close_file(fsp,False);
1429 END_PROFILE(SMBntcreateX);
1430 /* Can't set allocation size on a directory. */
1431 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1433 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1434 close_file(fsp,False);
1435 return ERROR_NT(NT_STATUS_DISK_FULL);
1438 fsp->initial_allocation_size = SMB_ROUNDUP(((SMB_BIG_UINT)file_len),SMB_ROUNDUP_ALLOCATION_SIZE);
1441 /* Realloc the size of parameters and data we will return */
1442 params = nttrans_realloc(ppparams, 69);
1444 return ERROR_DOS(ERRDOS,ERRnomem);
1447 if (extended_oplock_granted)
1448 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1449 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1450 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1452 SCVAL(p,0,NO_OPLOCK_RETURN);
1455 SSVAL(p,0,fsp->fnum);
1457 if ((create_disposition == FILE_SUPERSEDE) && (smb_action == FILE_WAS_OVERWRITTEN))
1458 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1460 SIVAL(p,0,smb_action);
1464 c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1466 if (lp_dos_filetime_resolution(SNUM(conn))) {
1468 sbuf.st_atime &= ~1;
1469 sbuf.st_mtime &= ~1;
1470 sbuf.st_mtime &= ~1;
1473 put_long_date(p,c_time);
1475 put_long_date(p,sbuf.st_atime); /* access time */
1477 put_long_date(p,sbuf.st_mtime); /* write time */
1479 put_long_date(p,sbuf.st_mtime); /* change time */
1481 SIVAL(p,0,fmode); /* File Attributes. */
1483 SOFF_T(p, 0, get_allocation_size(fsp,&sbuf));
1485 SOFF_T(p,0,file_len);
1487 if (flags & EXTENDED_RESPONSE_REQUIRED)
1490 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1492 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1494 /* Send the required number of replies */
1495 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1500 /****************************************************************************
1501 Reply to a NT CANCEL request.
1502 ****************************************************************************/
1504 int reply_ntcancel(connection_struct *conn,
1505 char *inbuf,char *outbuf,int length,int bufsize)
1508 * Go through and cancel any pending change notifies.
1511 int mid = SVAL(inbuf,smb_mid);
1512 START_PROFILE(SMBntcancel);
1513 remove_pending_change_notify_requests_by_mid(mid);
1514 remove_pending_lock_requests_by_mid(mid);
1515 srv_cancel_sign_response(mid);
1517 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1519 END_PROFILE(SMBntcancel);
1523 /****************************************************************************
1524 Reply to a NT rename request.
1525 ****************************************************************************/
1527 int reply_ntrename(connection_struct *conn,
1528 char *inbuf,char *outbuf,int length,int bufsize)
1535 uint16 attrs = SVAL(inbuf,smb_vwv0);
1536 uint16 rename_type = SVAL(inbuf,smb_vwv1);
1538 START_PROFILE(SMBntrename);
1540 if ((rename_type != RENAME_FLAG_RENAME) && (rename_type != RENAME_FLAG_HARD_LINK)) {
1541 END_PROFILE(SMBntrename);
1542 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1545 p = smb_buf(inbuf) + 1;
1546 p += srvstr_get_path(inbuf, oldname, p, sizeof(oldname), 0, STR_TERMINATE, &status, True);
1547 if (!NT_STATUS_IS_OK(status)) {
1548 END_PROFILE(SMBntrename);
1549 return ERROR_NT(status);
1552 if( strchr_m(oldname, ':')) {
1553 /* Can't rename a stream. */
1554 END_PROFILE(SMBntrename);
1555 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1559 p += srvstr_get_path(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status, True);
1560 if (!NT_STATUS_IS_OK(status)) {
1561 END_PROFILE(SMBntrename);
1562 return ERROR_NT(status);
1565 RESOLVE_DFSPATH(oldname, conn, inbuf, outbuf);
1566 RESOLVE_DFSPATH(newname, conn, inbuf, outbuf);
1568 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1570 if (rename_type == RENAME_FLAG_RENAME) {
1571 status = rename_internals(conn, oldname, newname, attrs, False);
1573 status = hardlink_internals(conn, oldname, newname);
1576 if (!NT_STATUS_IS_OK(status)) {
1577 END_PROFILE(SMBntrename);
1578 return ERROR_NT(status);
1582 * Win2k needs a changenotify request response before it will
1583 * update after a rename..
1585 process_pending_change_notify_queue((time_t)0);
1586 outsize = set_message(outbuf,0,0,True);
1588 END_PROFILE(SMBntrename);
1592 /****************************************************************************
1593 Reply to an unsolicited SMBNTtranss - just ignore it!
1594 ****************************************************************************/
1596 int reply_nttranss(connection_struct *conn,
1597 char *inbuf,char *outbuf,int length,int bufsize)
1599 START_PROFILE(SMBnttranss);
1600 DEBUG(4,("Ignoring nttranss of length %d\n",length));
1601 END_PROFILE(SMBnttranss);
1605 /****************************************************************************
1606 Reply to a notify change - queue the request and
1607 don't allow a directory to be opened.
1608 ****************************************************************************/
1610 static int call_nt_transact_notify_change(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1611 char **ppsetup, uint32 setup_count,
1612 char **ppparams, uint32 parameter_count,
1613 char **ppdata, uint32 data_count)
1615 char *setup = *ppsetup;
1620 return ERROR_DOS(ERRDOS,ERRbadfunc);
1622 fsp = file_fsp(setup,4);
1623 flags = IVAL(setup, 0);
1625 DEBUG(3,("call_nt_transact_notify_change\n"));
1628 return ERROR_DOS(ERRDOS,ERRbadfid);
1630 if((!fsp->is_directory) || (conn != fsp->conn))
1631 return ERROR_DOS(ERRDOS,ERRbadfid);
1633 if (!change_notify_set(inbuf, fsp, conn, flags))
1634 return(UNIXERROR(ERRDOS,ERRbadfid));
1636 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1637 name = %s\n", fsp->fsp_name ));
1642 /****************************************************************************
1643 Reply to an NT transact rename command.
1644 ****************************************************************************/
1646 static int call_nt_transact_rename(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1647 char **ppsetup, uint32 setup_count,
1648 char **ppparams, uint32 parameter_count,
1649 char **ppdata, uint32 data_count)
1651 char *params = *ppparams;
1653 files_struct *fsp = NULL;
1654 BOOL replace_if_exists = False;
1657 if(parameter_count < 4)
1658 return ERROR_DOS(ERRDOS,ERRbadfunc);
1660 fsp = file_fsp(params, 0);
1661 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1662 CHECK_FSP(fsp, conn);
1663 srvstr_get_path(inbuf, new_name, params+4, sizeof(new_name), -1, STR_TERMINATE, &status, True);
1664 if (!NT_STATUS_IS_OK(status)) {
1665 return ERROR_NT(status);
1668 status = rename_internals(conn, fsp->fsp_name,
1669 new_name, 0, replace_if_exists);
1670 if (!NT_STATUS_IS_OK(status))
1671 return ERROR_NT(status);
1674 * Rename was successful.
1676 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1678 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1679 fsp->fsp_name, new_name));
1682 * Win2k needs a changenotify request response before it will
1683 * update after a rename..
1686 process_pending_change_notify_queue((time_t)0);
1691 /******************************************************************************
1692 Fake up a completely empty SD.
1693 *******************************************************************************/
1695 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1697 extern DOM_SID global_sid_World;
1700 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1702 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1709 /****************************************************************************
1710 Reply to query a security descriptor.
1711 ****************************************************************************/
1713 static int call_nt_transact_query_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1714 char **ppsetup, uint32 setup_count,
1715 char **ppparams, uint32 parameter_count,
1716 char **ppdata, uint32 data_count)
1718 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1719 char *params = *ppparams;
1720 char *data = *ppdata;
1722 SEC_DESC *psd = NULL;
1724 uint32 security_info_wanted;
1725 TALLOC_CTX *mem_ctx;
1726 files_struct *fsp = NULL;
1728 if(parameter_count < 8)
1729 return ERROR_DOS(ERRDOS,ERRbadfunc);
1731 fsp = file_fsp(params,0);
1733 return ERROR_DOS(ERRDOS,ERRbadfid);
1735 security_info_wanted = IVAL(params,4);
1737 DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1739 params = nttrans_realloc(ppparams, 4);
1741 return ERROR_DOS(ERRDOS,ERRnomem);
1743 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
1744 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1745 return ERROR_DOS(ERRDOS,ERRnomem);
1749 * Get the permissions to return.
1752 if (!lp_nt_acl_support(SNUM(conn)))
1753 sd_size = get_null_nt_acl(mem_ctx, &psd);
1755 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fd, security_info_wanted, &psd);
1758 talloc_destroy(mem_ctx);
1759 return(UNIXERROR(ERRDOS,ERRnoaccess));
1762 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %d.\n",(int)sd_size));
1764 SIVAL(params,0,(uint32)sd_size);
1766 if(max_data_count < sd_size) {
1768 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1769 params, 4, *ppdata, 0);
1770 talloc_destroy(mem_ctx);
1775 * Allocate the data we will point this at.
1778 data = nttrans_realloc(ppdata, sd_size);
1780 talloc_destroy(mem_ctx);
1781 return ERROR_DOS(ERRDOS,ERRnomem);
1785 * Init the parse struct we will marshall into.
1788 prs_init(&pd, 0, mem_ctx, MARSHALL);
1791 * Setup the prs_struct to point at the memory we just
1795 prs_give_memory( &pd, data, (uint32)sd_size, False);
1798 * Finally, linearize into the outgoing buffer.
1801 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1802 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1803 security descriptor.\n"));
1805 * Return access denied for want of a better error message..
1807 talloc_destroy(mem_ctx);
1808 return(UNIXERROR(ERRDOS,ERRnoaccess));
1812 * Now we can delete the security descriptor.
1815 talloc_destroy(mem_ctx);
1817 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data, (int)sd_size);
1821 /****************************************************************************
1822 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1823 ****************************************************************************/
1825 static int call_nt_transact_set_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1826 char **ppsetup, uint32 setup_count,
1827 char **ppparams, uint32 parameter_count,
1828 char **ppdata, uint32 data_count)
1830 char *params= *ppparams;
1831 char *data = *ppdata;
1832 files_struct *fsp = NULL;
1833 uint32 security_info_sent = 0;
1836 if(parameter_count < 8)
1837 return ERROR_DOS(ERRDOS,ERRbadfunc);
1839 if((fsp = file_fsp(params,0)) == NULL)
1840 return ERROR_DOS(ERRDOS,ERRbadfid);
1842 if(!lp_nt_acl_support(SNUM(conn)))
1845 security_info_sent = IVAL(params,4);
1847 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1848 (unsigned int)security_info_sent ));
1850 if (data_count == 0)
1851 return ERROR_DOS(ERRDOS, ERRnoaccess);
1853 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent)))
1854 return ERROR_NT(nt_status);
1858 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1862 /****************************************************************************
1864 ****************************************************************************/
1866 static int call_nt_transact_ioctl(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1867 char **ppsetup, uint32 setup_count,
1868 char **ppparams, uint32 parameter_count,
1869 char **ppdata, uint32 data_count)
1876 static BOOL logged_message;
1877 char *pdata = *ppdata;
1879 if (setup_count != 8) {
1880 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1881 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1884 function = IVAL(*ppsetup, 0);
1885 fidnum = SVAL(*ppsetup, 4);
1886 isFSctl = CVAL(*ppsetup, 6);
1887 compfilter = CVAL(*ppsetup, 7);
1889 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1890 function, fidnum, isFSctl, compfilter));
1892 fsp=file_fsp(*ppsetup, 4);
1893 /* this check is done in each implemented function case for now
1894 because I don't want to break anything... --metze
1895 FSP_BELONGS_CONN(fsp,conn);*/
1898 case FSCTL_SET_SPARSE:
1899 /* pretend this succeeded - tho strictly we should
1900 mark the file sparse (if the local fs supports it)
1901 so we can know if we need to pre-allocate or not */
1903 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1904 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1907 case FSCTL_0x000900C0:
1908 /* pretend this succeeded - don't know what this really is
1909 but works ok like this --metze
1912 DEBUG(10,("FSCTL_0x000900C0: called on FID[0x%04X](but not implemented)\n",fidnum));
1913 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1916 case FSCTL_GET_REPARSE_POINT:
1917 /* pretend this fail - my winXP does it like this
1921 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1922 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
1925 case FSCTL_SET_REPARSE_POINT:
1926 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1930 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1931 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
1934 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1937 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1938 * and return their volume names. If max_data_count is 16, then it is just
1939 * asking for the number of volumes and length of the combined names.
1941 * pdata is the data allocated by our caller, but that uses
1942 * total_data_count (which is 0 in our case) rather than max_data_count.
1943 * Allocate the correct amount and return the pointer to let
1944 * it be deallocated when we return.
1946 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1947 SHADOW_COPY_DATA *shadow_data = NULL;
1948 TALLOC_CTX *shadow_mem_ctx = NULL;
1949 BOOL labels = False;
1950 uint32 labels_data_count = 0;
1954 FSP_BELONGS_CONN(fsp,conn);
1956 if (max_data_count < 16) {
1957 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1959 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1962 if (max_data_count > 16) {
1966 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1967 if (shadow_mem_ctx == NULL) {
1968 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1969 return ERROR_NT(NT_STATUS_NO_MEMORY);
1972 shadow_data = (SHADOW_COPY_DATA *)talloc_zero(shadow_mem_ctx,sizeof(SHADOW_COPY_DATA));
1973 if (shadow_data == NULL) {
1974 DEBUG(0,("talloc_zero() failed!\n"));
1975 return ERROR_NT(NT_STATUS_NO_MEMORY);
1978 shadow_data->mem_ctx = shadow_mem_ctx;
1981 * Call the VFS routine to actually do the work.
1983 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1984 talloc_destroy(shadow_data->mem_ctx);
1985 if (errno == ENOSYS) {
1986 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1987 conn->connectpath));
1988 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1990 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1991 conn->connectpath));
1992 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
1996 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2001 data_count = 12+labels_data_count+4;
2004 if (max_data_count<data_count) {
2005 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2006 max_data_count,data_count));
2007 talloc_destroy(shadow_data->mem_ctx);
2008 return ERROR_NT(NT_STATUS_BUFFER_TOO_SMALL);
2011 pdata = nttrans_realloc(ppdata, data_count);
2012 if (pdata == NULL) {
2013 talloc_destroy(shadow_data->mem_ctx);
2014 return ERROR_NT(NT_STATUS_NO_MEMORY);
2019 /* num_volumes 4 bytes */
2020 SIVAL(pdata,0,shadow_data->num_volumes);
2023 /* num_labels 4 bytes */
2024 SIVAL(pdata,4,shadow_data->num_volumes);
2027 /* needed_data_count 4 bytes */
2028 SIVAL(pdata,8,labels_data_count);
2032 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2033 shadow_data->num_volumes,fsp->fsp_name));
2034 if (labels && shadow_data->labels) {
2035 for (i=0;i<shadow_data->num_volumes;i++) {
2036 srvstr_push(outbuf, cur_pdata, shadow_data->labels[i], 2*sizeof(SHADOW_COPY_LABEL), STR_UNICODE|STR_TERMINATE);
2037 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2038 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2042 talloc_destroy(shadow_data->mem_ctx);
2044 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, pdata, data_count);
2049 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2051 /* pretend this succeeded -
2053 * we have to send back a list with all files owned by this SID
2055 * but I have to check that --metze
2059 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2061 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2063 FSP_BELONGS_CONN(fsp,conn);
2065 /* unknown 4 bytes: this is not the length of the sid :-( */
2066 /*unknown = IVAL(pdata,0);*/
2068 sid_parse(pdata+4,sid_len,&sid);
2069 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2071 if (!NT_STATUS_IS_OK(sid_to_uid(&sid, &uid))) {
2072 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2073 sid_string_static(&sid),(unsigned long)sid_len));
2077 /* we can take a look at the find source :-)
2079 * find ./ -uid $uid -name '*' is what we need here
2082 * and send 4bytes len and then NULL terminated unicode strings
2085 * but I don't know how to deal with the paged results
2086 * (maybe we can hang the result anywhere in the fsp struct)
2088 * we don't send all files at once
2089 * and at the next we should *not* start from the beginning,
2090 * so we have to cache the result
2095 /* this works for now... */
2096 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2100 if (!logged_message) {
2101 logged_message = True; /* Only print this once... */
2102 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2107 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2111 #ifdef HAVE_SYS_QUOTAS
2112 /****************************************************************************
2113 Reply to get user quota
2114 ****************************************************************************/
2116 static int call_nt_transact_get_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2117 char **ppsetup, uint32 setup_count,
2118 char **ppparams, uint32 parameter_count,
2119 char **ppdata, uint32 data_count)
2121 NTSTATUS nt_status = NT_STATUS_OK;
2122 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
2123 char *params = *ppparams;
2124 char *pdata = *ppdata;
2126 int data_len=0,param_len=0;
2129 files_struct *fsp = NULL;
2133 BOOL start_enum = True;
2134 SMB_NTQUOTA_STRUCT qt;
2135 SMB_NTQUOTA_LIST *tmp_list;
2136 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2137 extern struct current_user current_user;
2142 if (current_user.uid != 0) {
2143 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2144 lp_servicename(SNUM(conn)),conn->user));
2145 return ERROR_DOS(ERRDOS,ERRnoaccess);
2149 * Ensure minimum number of parameters sent.
2152 if (parameter_count < 4) {
2153 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2154 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2157 /* maybe we can check the quota_fnum */
2158 fsp = file_fsp(params,0);
2159 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2160 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2161 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2164 /* the NULL pointer cheking for fsp->fake_file_handle->pd
2165 * is done by CHECK_NTQUOTA_HANDLE_OK()
2167 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2169 level = SVAL(params,2);
2171 /* unknown 12 bytes leading in params */
2174 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2175 /* seems that we should continue with the enum here --metze */
2177 if (qt_handle->quota_list!=NULL &&
2178 qt_handle->tmp_list==NULL) {
2181 free_ntquota_list(&(qt_handle->quota_list));
2183 /* Realloc the size of parameters and data we will return */
2185 params = nttrans_realloc(ppparams, param_len);
2187 return ERROR_DOS(ERRDOS,ERRnomem);
2190 SIVAL(params,0,data_len);
2197 case TRANSACT_GET_USER_QUOTA_LIST_START:
2199 if (qt_handle->quota_list==NULL &&
2200 qt_handle->tmp_list==NULL) {
2204 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0)
2205 return ERROR_DOS(ERRSRV,ERRerror);
2207 /* Realloc the size of parameters and data we will return */
2209 params = nttrans_realloc(ppparams, param_len);
2211 return ERROR_DOS(ERRDOS,ERRnomem);
2213 /* we should not trust the value in max_data_count*/
2214 max_data_count = MIN(max_data_count,2048);
2216 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2218 return ERROR_DOS(ERRDOS,ERRnomem);
2223 /* set params Size of returned Quota Data 4 bytes*/
2224 /* but set it later when we know it */
2226 /* for each entry push the data */
2229 qt_handle->tmp_list = qt_handle->quota_list;
2232 tmp_list = qt_handle->tmp_list;
2234 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2235 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2237 sid_len = sid_size(&tmp_list->quotas->sid);
2238 entry_len = 40 + sid_len;
2240 /* nextoffset entry 4 bytes */
2241 SIVAL(entry,0,entry_len);
2243 /* then the len of the SID 4 bytes */
2244 SIVAL(entry,4,sid_len);
2246 /* unknown data 8 bytes SMB_BIG_UINT */
2247 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2249 /* the used disk space 8 bytes SMB_BIG_UINT */
2250 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2252 /* the soft quotas 8 bytes SMB_BIG_UINT */
2253 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2255 /* the hard quotas 8 bytes SMB_BIG_UINT */
2256 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2258 /* and now the SID */
2259 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2262 qt_handle->tmp_list = tmp_list;
2264 /* overwrite the offset of the last entry */
2265 SIVAL(entry-entry_len,0,0);
2267 data_len = 4+qt_len;
2268 /* overwrite the params quota_data_len */
2269 SIVAL(params,0,data_len);
2273 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2275 /* unknown 4 bytes IVAL(pdata,0) */
2277 if (data_count < 8) {
2278 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2279 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2282 sid_len = IVAL(pdata,4);
2284 if (data_count < 8+sid_len) {
2285 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2286 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2289 data_len = 4+40+sid_len;
2291 if (max_data_count < data_len) {
2292 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2293 max_data_count, data_len));
2295 SIVAL(params,0,data_len);
2297 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2301 sid_parse(pdata+8,sid_len,&sid);
2304 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2307 * we have to return zero's in all fields
2308 * instead of returning an error here
2313 /* Realloc the size of parameters and data we will return */
2315 params = nttrans_realloc(ppparams, param_len);
2317 return ERROR_DOS(ERRDOS,ERRnomem);
2319 pdata = nttrans_realloc(ppdata, data_len);
2321 return ERROR_DOS(ERRDOS,ERRnomem);
2325 /* set params Size of returned Quota Data 4 bytes*/
2326 SIVAL(params,0,data_len);
2328 /* nextoffset entry 4 bytes */
2331 /* then the len of the SID 4 bytes */
2332 SIVAL(entry,4,sid_len);
2334 /* unknown data 8 bytes SMB_BIG_UINT */
2335 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2337 /* the used disk space 8 bytes SMB_BIG_UINT */
2338 SBIG_UINT(entry,16,qt.usedspace);
2340 /* the soft quotas 8 bytes SMB_BIG_UINT */
2341 SBIG_UINT(entry,24,qt.softlim);
2343 /* the hard quotas 8 bytes SMB_BIG_UINT */
2344 SBIG_UINT(entry,32,qt.hardlim);
2346 /* and now the SID */
2347 sid_linearize(entry+40, sid_len, &sid);
2352 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2353 return ERROR_DOS(ERRSRV,ERRerror);
2357 send_nt_replies(inbuf, outbuf, bufsize, nt_status, params, param_len, pdata, data_len);
2362 /****************************************************************************
2363 Reply to set user quota
2364 ****************************************************************************/
2366 static int call_nt_transact_set_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2367 char **ppsetup, uint32 setup_count,
2368 char **ppparams, uint32 parameter_count,
2369 char **ppdata, uint32 data_count)
2371 char *params = *ppparams;
2372 char *pdata = *ppdata;
2373 int data_len=0,param_len=0;
2374 SMB_NTQUOTA_STRUCT qt;
2377 files_struct *fsp = NULL;
2382 if (current_user.uid != 0) {
2383 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2384 lp_servicename(SNUM(conn)),conn->user));
2385 return ERROR_DOS(ERRDOS,ERRnoaccess);
2389 * Ensure minimum number of parameters sent.
2392 if (parameter_count < 2) {
2393 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2394 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2397 /* maybe we can check the quota_fnum */
2398 fsp = file_fsp(params,0);
2399 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2400 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2401 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2404 if (data_count < 40) {
2405 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2406 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2409 /* offset to next quota record.
2410 * 4 bytes IVAL(pdata,0)
2415 sid_len = IVAL(pdata,4);
2417 if (data_count < 40+sid_len) {
2418 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2419 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2422 /* unknown 8 bytes in pdata
2423 * maybe its the change time in NTTIME
2426 /* the used space 8 bytes (SMB_BIG_UINT)*/
2427 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2428 #ifdef LARGE_SMB_OFF_T
2429 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2430 #else /* LARGE_SMB_OFF_T */
2431 if ((IVAL(pdata,20) != 0)&&
2432 ((qt.usedspace != 0xFFFFFFFF)||
2433 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2434 /* more than 32 bits? */
2435 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2437 #endif /* LARGE_SMB_OFF_T */
2439 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2440 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2441 #ifdef LARGE_SMB_OFF_T
2442 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2443 #else /* LARGE_SMB_OFF_T */
2444 if ((IVAL(pdata,28) != 0)&&
2445 ((qt.softlim != 0xFFFFFFFF)||
2446 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2447 /* more than 32 bits? */
2448 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2450 #endif /* LARGE_SMB_OFF_T */
2452 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2453 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2454 #ifdef LARGE_SMB_OFF_T
2455 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2456 #else /* LARGE_SMB_OFF_T */
2457 if ((IVAL(pdata,36) != 0)&&
2458 ((qt.hardlim != 0xFFFFFFFF)||
2459 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2460 /* more than 32 bits? */
2461 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2463 #endif /* LARGE_SMB_OFF_T */
2465 sid_parse(pdata+40,sid_len,&sid);
2466 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2468 /* 44 unknown bytes left... */
2470 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2471 return ERROR_DOS(ERRSRV,ERRerror);
2474 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, pdata, data_len);
2478 #endif /* HAVE_SYS_QUOTAS */
2480 /****************************************************************************
2481 Reply to a SMBNTtrans.
2482 ****************************************************************************/
2484 int reply_nttrans(connection_struct *conn,
2485 char *inbuf,char *outbuf,int length,int bufsize)
2488 #if 0 /* Not used. */
2489 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
2490 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
2491 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
2492 #endif /* Not used. */
2493 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
2494 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
2495 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
2496 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
2497 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
2498 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
2499 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
2500 uint16 function_code = SVAL( inbuf, smb_nt_Function);
2501 char *params = NULL, *data = NULL, *setup = NULL;
2502 uint32 num_params_sofar, num_data_sofar;
2503 START_PROFILE(SMBnttrans);
2505 if(global_oplock_break &&
2506 ((function_code == NT_TRANSACT_CREATE) ||
2507 (function_code == NT_TRANSACT_RENAME))) {
2509 * Queue this open message as we are the process of an oplock break.
2512 DEBUG(2,("reply_nttrans: queueing message code 0x%x \
2513 due to being in oplock break state.\n", (unsigned int)function_code ));
2515 push_oplock_pending_smb_message( inbuf, length);
2516 END_PROFILE(SMBnttrans);
2520 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2521 END_PROFILE(SMBnttrans);
2522 return ERROR_DOS(ERRSRV,ERRaccess);
2525 outsize = set_message(outbuf,0,0,True);
2528 * All nttrans messages we handle have smb_wct == 19 + setup_count.
2529 * Ensure this is so as a sanity check.
2532 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
2533 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2534 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
2538 /* Allocate the space for the setup, the maximum needed parameters and data */
2541 setup = (char *)malloc(setup_count);
2542 if (total_parameter_count > 0)
2543 params = (char *)malloc(total_parameter_count);
2544 if (total_data_count > 0)
2545 data = (char *)malloc(total_data_count);
2547 if ((total_parameter_count && !params) || (total_data_count && !data) ||
2548 (setup_count && !setup)) {
2552 DEBUG(0,("reply_nttrans : Out of memory\n"));
2553 END_PROFILE(SMBnttrans);
2554 return ERROR_DOS(ERRDOS,ERRnomem);
2557 /* Copy the param and data bytes sent with this request into the params buffer */
2558 num_params_sofar = parameter_count;
2559 num_data_sofar = data_count;
2561 if (parameter_count > total_parameter_count || data_count > total_data_count)
2565 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
2566 if ((smb_nt_SetupStart + setup_count < smb_nt_SetupStart) ||
2567 (smb_nt_SetupStart + setup_count < setup_count))
2569 if (smb_nt_SetupStart + setup_count > length)
2572 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
2573 dump_data(10, setup, setup_count);
2576 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
2577 if ((parameter_offset + parameter_count < parameter_offset) ||
2578 (parameter_offset + parameter_count < parameter_count))
2580 if ((smb_base(inbuf) + parameter_offset + parameter_count > inbuf + length)||
2581 (smb_base(inbuf) + parameter_offset + parameter_count < smb_base(inbuf)))
2584 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
2585 dump_data(10, params, parameter_count);
2588 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
2589 if ((data_offset + data_count < data_offset) || (data_offset + data_count < data_count))
2591 if ((smb_base(inbuf) + data_offset + data_count > inbuf + length) ||
2592 (smb_base(inbuf) + data_offset + data_count < smb_base(inbuf)))
2595 memcpy( data, smb_base(inbuf) + data_offset, data_count);
2596 dump_data(10, data, data_count);
2599 srv_signing_trans_start(SVAL(inbuf,smb_mid));
2601 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2602 /* We need to send an interim response then receive the rest
2603 of the parameter/data bytes */
2604 outsize = set_message(outbuf,0,0,True);
2605 srv_signing_trans_stop();
2606 if (!send_smb(smbd_server_fd(),outbuf))
2607 exit_server("reply_nttrans: send_smb failed.");
2609 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2611 uint32 parameter_displacement;
2612 uint32 data_displacement;
2614 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
2617 * The sequence number for the trans reply is always
2618 * based on the last secondary received.
2621 srv_signing_trans_start(SVAL(inbuf,smb_mid));
2623 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
2624 outsize = set_message(outbuf,0,0,True);
2626 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
2628 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
2629 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
2634 /* Revise total_params and total_data in case they have changed downwards */
2635 if (IVAL(inbuf, smb_nts_TotalParameterCount) < total_parameter_count)
2636 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
2637 if (IVAL(inbuf, smb_nts_TotalDataCount) < total_data_count)
2638 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
2640 parameter_count = IVAL(inbuf,smb_nts_ParameterCount);
2641 parameter_offset = IVAL(inbuf, smb_nts_ParameterOffset);
2642 parameter_displacement = IVAL(inbuf, smb_nts_ParameterDisplacement);
2643 num_params_sofar += parameter_count;
2645 data_count = IVAL(inbuf, smb_nts_DataCount);
2646 data_displacement = IVAL(inbuf, smb_nts_DataDisplacement);
2647 data_offset = IVAL(inbuf, smb_nts_DataOffset);
2648 num_data_sofar += data_count;
2650 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count) {
2651 DEBUG(0,("reply_nttrans2: data overflow in secondary nttrans packet"));
2655 if (parameter_count) {
2656 if (parameter_displacement + parameter_count >= total_parameter_count)
2658 if ((parameter_displacement + parameter_count < parameter_displacement) ||
2659 (parameter_displacement + parameter_count < parameter_count))
2661 if (parameter_displacement > total_parameter_count)
2663 if ((smb_base(inbuf) + parameter_offset + parameter_count >= inbuf + bufsize) ||
2664 (smb_base(inbuf) + parameter_offset + parameter_count < smb_base(inbuf)))
2666 if (parameter_displacement + params < params)
2669 memcpy( ¶ms[parameter_displacement], smb_base(inbuf) + parameter_offset, parameter_count);
2673 if (data_displacement + data_count >= total_data_count)
2675 if ((data_displacement + data_count < data_displacement) ||
2676 (data_displacement + data_count < data_count))
2678 if (data_displacement > total_data_count)
2680 if ((smb_base(inbuf) + data_offset + data_count >= inbuf + bufsize) ||
2681 (smb_base(inbuf) + data_offset + data_count < smb_base(inbuf)))
2683 if (data_displacement + data < data)
2686 memcpy( &data[data_displacement], smb_base(inbuf)+ data_offset, data_count);
2691 if (Protocol >= PROTOCOL_NT1)
2692 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | FLAGS2_IS_LONG_NAME);
2694 /* Now we must call the relevant NT_TRANS function */
2695 switch(function_code) {
2696 case NT_TRANSACT_CREATE:
2697 START_PROFILE_NESTED(NT_transact_create);
2698 outsize = call_nt_transact_create(conn, inbuf, outbuf,
2700 &setup, setup_count,
2701 ¶ms, total_parameter_count,
2702 &data, total_data_count);
2703 END_PROFILE_NESTED(NT_transact_create);
2705 case NT_TRANSACT_IOCTL:
2706 START_PROFILE_NESTED(NT_transact_ioctl);
2707 outsize = call_nt_transact_ioctl(conn, inbuf, outbuf,
2709 &setup, setup_count,
2710 ¶ms, total_parameter_count,
2711 &data, total_data_count);
2712 END_PROFILE_NESTED(NT_transact_ioctl);
2714 case NT_TRANSACT_SET_SECURITY_DESC:
2715 START_PROFILE_NESTED(NT_transact_set_security_desc);
2716 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
2718 &setup, setup_count,
2719 ¶ms, total_parameter_count,
2720 &data, total_data_count);
2721 END_PROFILE_NESTED(NT_transact_set_security_desc);
2723 case NT_TRANSACT_NOTIFY_CHANGE:
2724 START_PROFILE_NESTED(NT_transact_notify_change);
2725 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
2727 &setup, setup_count,
2728 ¶ms, total_parameter_count,
2729 &data, total_data_count);
2730 END_PROFILE_NESTED(NT_transact_notify_change);
2732 case NT_TRANSACT_RENAME:
2733 START_PROFILE_NESTED(NT_transact_rename);
2734 outsize = call_nt_transact_rename(conn, inbuf, outbuf,
2736 &setup, setup_count,
2737 ¶ms, total_parameter_count,
2738 &data, total_data_count);
2739 END_PROFILE_NESTED(NT_transact_rename);
2742 case NT_TRANSACT_QUERY_SECURITY_DESC:
2743 START_PROFILE_NESTED(NT_transact_query_security_desc);
2744 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
2746 &setup, setup_count,
2747 ¶ms, total_parameter_count,
2748 &data, total_data_count);
2749 END_PROFILE_NESTED(NT_transact_query_security_desc);
2751 #ifdef HAVE_SYS_QUOTAS
2752 case NT_TRANSACT_GET_USER_QUOTA:
2753 START_PROFILE_NESTED(NT_transact_get_user_quota);
2754 outsize = call_nt_transact_get_user_quota(conn, inbuf, outbuf,
2756 &setup, setup_count,
2757 ¶ms, total_parameter_count,
2758 &data, total_data_count);
2759 END_PROFILE_NESTED(NT_transact_get_user_quota);
2761 case NT_TRANSACT_SET_USER_QUOTA:
2762 START_PROFILE_NESTED(NT_transact_set_user_quota);
2763 outsize = call_nt_transact_set_user_quota(conn, inbuf, outbuf,
2765 &setup, setup_count,
2766 ¶ms, total_parameter_count,
2767 &data, total_data_count);
2768 END_PROFILE_NESTED(NT_transact_set_user_quota);
2770 #endif /* HAVE_SYS_QUOTAS */
2772 /* Error in request */
2773 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
2777 END_PROFILE(SMBnttrans);
2778 srv_signing_trans_stop();
2779 return ERROR_DOS(ERRSRV,ERRerror);
2782 /* As we do not know how many data packets will need to be
2783 returned here the various call_nt_transact_xxxx calls
2784 must send their own. Thus a call_nt_transact_xxxx routine only
2785 returns a value other than -1 when it wants to send
2789 srv_signing_trans_stop();
2794 END_PROFILE(SMBnttrans);
2795 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
2796 calls have already sent it. If outsize != -1 then it is
2797 returning an error packet. */
2801 srv_signing_trans_stop();
2805 END_PROFILE(SMBnttrans);
2806 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);