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 BOOL case_sensitive;
28 extern BOOL case_preserve;
29 extern BOOL short_case_preserve;
30 extern struct current_user current_user;
32 static const char *known_nt_pipes[] = {
49 /* Map generic permissions to file object specific permissions */
51 struct generic_mapping file_generic_mapping = {
58 static char *nttrans_realloc(char **ptr, size_t size)
62 smb_panic("nttrans_realloc() called with NULL ptr\n");
64 tptr = Realloc_zero(*ptr, size);
76 /****************************************************************************
77 Send the required number of replies back.
78 We assume all fields other than the data fields are
79 set correctly for the type of call.
80 HACK ! Always assumes smb_setup field is zero.
81 ****************************************************************************/
83 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, NTSTATUS nt_error, char *params,
84 int paramsize, char *pdata, int datasize)
87 int data_to_send = datasize;
88 int params_to_send = paramsize;
92 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
93 int alignment_offset = 3;
94 int data_alignment_offset = 0;
97 * Initially set the wcnt area to be 18 - this is true for all
101 set_message(outbuf,18,0,True);
103 if (NT_STATUS_V(nt_error))
107 * If there genuinely are no parameters or data to send just send
111 if(params_to_send == 0 && data_to_send == 0) {
112 if (!send_smb(smbd_server_fd(),outbuf))
113 exit_server("send_nt_replies: send_smb failed.");
118 * When sending params and data ensure that both are nicely aligned.
119 * Only do this alignment when there is also data to send - else
120 * can cause NT redirector problems.
123 if (((params_to_send % 4) != 0) && (data_to_send != 0))
124 data_alignment_offset = 4 - (params_to_send % 4);
127 * Space is bufsize minus Netbios over TCP header minus SMB header.
128 * The alignment_offset is to align the param bytes on a four byte
129 * boundary (2 bytes for data len, one byte pad).
130 * NT needs this to work correctly.
133 useable_space = bufsize - ((smb_buf(outbuf)+
134 alignment_offset+data_alignment_offset) -
138 * useable_space can never be more than max_send minus the
142 useable_space = MIN(useable_space,
143 max_send - (alignment_offset+data_alignment_offset));
146 while (params_to_send || data_to_send) {
149 * Calculate whether we will totally or partially fill this packet.
152 total_sent_thistime = params_to_send + data_to_send +
153 alignment_offset + data_alignment_offset;
156 * We can never send more than useable_space.
159 total_sent_thistime = MIN(total_sent_thistime, useable_space);
161 set_message(outbuf, 18, total_sent_thistime, True);
164 * Set total params and data to be sent.
167 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
168 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
171 * Calculate how many parameters and data we can fit into
172 * this packet. Parameters get precedence.
175 params_sent_thistime = MIN(params_to_send,useable_space);
176 data_sent_thistime = useable_space - params_sent_thistime;
177 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
179 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
181 if(params_sent_thistime == 0) {
182 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
183 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
186 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
187 * parameter bytes, however the first 4 bytes of outbuf are
188 * the Netbios over TCP header. Thus use smb_base() to subtract
189 * them from the calculation.
192 SIVAL(outbuf,smb_ntr_ParameterOffset,
193 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
195 * Absolute displacement of param bytes sent in this packet.
198 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
202 * Deal with the data portion.
205 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
207 if(data_sent_thistime == 0) {
208 SIVAL(outbuf,smb_ntr_DataOffset,0);
209 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
212 * The offset of the data bytes is the offset of the
213 * parameter bytes plus the number of parameters being sent this time.
216 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
217 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
218 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
222 * Copy the param bytes into the packet.
225 if(params_sent_thistime)
226 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
229 * Copy in the data bytes
232 if(data_sent_thistime)
233 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
234 data_alignment_offset,pd,data_sent_thistime);
236 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
237 params_sent_thistime, data_sent_thistime, useable_space));
238 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
239 params_to_send, data_to_send, paramsize, datasize));
241 /* Send the packet */
242 if (!send_smb(smbd_server_fd(),outbuf))
243 exit_server("send_nt_replies: send_smb failed.");
245 pp += params_sent_thistime;
246 pd += data_sent_thistime;
248 params_to_send -= params_sent_thistime;
249 data_to_send -= data_sent_thistime;
255 if(params_to_send < 0 || data_to_send < 0) {
256 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
257 params_to_send, data_to_send));
265 /****************************************************************************
267 ****************************************************************************/
269 static BOOL saved_case_sensitive;
270 static BOOL saved_case_preserve;
271 static BOOL saved_short_case_preserve;
273 /****************************************************************************
275 ****************************************************************************/
277 static void set_posix_case_semantics(uint32 file_attributes)
279 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
282 saved_case_sensitive = case_sensitive;
283 saved_case_preserve = case_preserve;
284 saved_short_case_preserve = short_case_preserve;
287 case_sensitive = True;
288 case_preserve = True;
289 short_case_preserve = True;
292 /****************************************************************************
293 Restore case semantics.
294 ****************************************************************************/
296 static void restore_case_semantics(uint32 file_attributes)
298 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
301 case_sensitive = saved_case_sensitive;
302 case_preserve = saved_case_preserve;
303 short_case_preserve = saved_short_case_preserve;
306 /****************************************************************************
307 Utility function to map create disposition.
308 ****************************************************************************/
310 static int map_create_disposition( uint32 create_disposition)
314 switch( create_disposition ) {
316 /* create if not exist, fail if exist */
317 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL);
320 case FILE_OVERWRITE_IF:
321 /* create if not exist, trunc if exist */
322 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
325 /* fail if not exist, open if exists */
326 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN);
329 /* create if not exist, open if exists */
330 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_OPEN);
333 /* fail if not exist, truncate if exists */
334 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
337 DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
338 create_disposition ));
342 DEBUG(10,("map_create_disposition: Mapped create_disposition 0x%lx to 0x%x\n",
343 (unsigned long)create_disposition, ret ));
348 /****************************************************************************
349 Utility function to map share modes.
350 ****************************************************************************/
352 static int map_share_mode( char *fname, uint32 create_options,
353 uint32 *desired_access, uint32 share_access, uint32 file_attributes)
355 int smb_open_mode = -1;
356 uint32 original_desired_access = *desired_access;
359 * Convert GENERIC bits to specific bits.
362 se_map_generic(desired_access, &file_generic_mapping);
364 switch( *desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
366 smb_open_mode = DOS_OPEN_RDONLY;
368 case FILE_WRITE_DATA:
369 case FILE_APPEND_DATA:
370 case FILE_WRITE_DATA|FILE_APPEND_DATA:
371 smb_open_mode = DOS_OPEN_WRONLY;
373 case FILE_READ_DATA|FILE_WRITE_DATA:
374 case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
375 case FILE_READ_DATA|FILE_APPEND_DATA:
376 smb_open_mode = DOS_OPEN_RDWR;
381 * NB. For DELETE_ACCESS we should really check the
382 * directory permissions, as that is what controls
383 * delete, and for WRITE_DAC_ACCESS we should really
384 * check the ownership, as that is what controls the
385 * chmod. Note that this is *NOT* a security hole (this
386 * note is for you, Andrew) as we are not *allowing*
387 * the access at this point, the actual unlink or
388 * chown or chmod call would do this. We are just helping
389 * clients out by telling them if they have a hope
390 * of any of this succeeding. POSIX acls may still
391 * deny the real call. JRA.
394 if (smb_open_mode == -1) {
396 if(*desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS|
397 FILE_EXECUTE|FILE_READ_ATTRIBUTES|
398 FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
399 FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS)) {
400 smb_open_mode = DOS_OPEN_RDONLY;
401 } else if(*desired_access == 0) {
404 * JRA - NT seems to sometimes send desired_access as zero. play it safe
405 * and map to a stat open.
408 smb_open_mode = DOS_OPEN_RDONLY;
411 DEBUG(0,("map_share_mode: Incorrect value 0x%lx for desired_access to file %s\n",
412 (unsigned long)*desired_access, fname));
418 * Set the special bit that means allow share delete.
419 * This is held outside the normal share mode bits at 1<<15.
423 if(share_access & FILE_SHARE_DELETE) {
424 smb_open_mode |= ALLOW_SHARE_DELETE;
425 DEBUG(10,("map_share_mode: FILE_SHARE_DELETE requested. open_mode = 0x%x\n", smb_open_mode));
428 if(*desired_access & DELETE_ACCESS) {
429 DEBUG(10,("map_share_mode: DELETE_ACCESS requested. open_mode = 0x%x\n", smb_open_mode));
433 * We need to store the intent to open for Delete. This
434 * is what determines if a delete on close flag can be set.
435 * This is the wrong way (and place) to store this, but for 2.2 this
436 * is the only practical way. JRA.
439 if (create_options & FILE_DELETE_ON_CLOSE) {
441 * W2K3 bug compatibility mode... To set delete on close
442 * the redirector must have *specifically* set DELETE_ACCESS
443 * in the desired_access field. Just asking for GENERIC_ALL won't do. JRA.
446 if (!(original_desired_access & DELETE_ACCESS)) {
447 DEBUG(5,("map_share_mode: FILE_DELETE_ON_CLOSE requested without \
448 DELETE_ACCESS for file %s. (desired_access = 0x%lx)\n",
449 fname, (unsigned long)*desired_access));
452 /* Implicit delete access is *NOT* requested... */
453 smb_open_mode |= DELETE_ON_CLOSE_FLAG;
454 DEBUG(10,("map_share_mode: FILE_DELETE_ON_CLOSE requested. open_mode = 0x%x\n", smb_open_mode));
457 /* Add in the requested share mode. */
458 switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
459 case FILE_SHARE_READ:
460 smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
462 case FILE_SHARE_WRITE:
463 smb_open_mode |= SET_DENY_MODE(DENY_READ);
465 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
466 smb_open_mode |= SET_DENY_MODE(DENY_NONE);
468 case FILE_SHARE_NONE:
469 smb_open_mode |= SET_DENY_MODE(DENY_ALL);
474 * Handle an O_SYNC request.
477 if(file_attributes & FILE_FLAG_WRITE_THROUGH)
478 smb_open_mode |= FILE_SYNC_OPENMODE;
480 DEBUG(10,("map_share_mode: Mapped desired access 0x%lx, share access 0x%lx, file attributes 0x%lx \
481 to open_mode 0x%x\n", (unsigned long)*desired_access, (unsigned long)share_access,
482 (unsigned long)file_attributes, smb_open_mode ));
484 return smb_open_mode;
487 /****************************************************************************
488 Reply to an NT create and X call on a pipe.
489 ****************************************************************************/
491 static int nt_open_pipe(char *fname, connection_struct *conn,
492 char *inbuf, char *outbuf, int *ppnum)
494 smb_np_struct *p = NULL;
496 uint16 vuid = SVAL(inbuf, smb_uid);
499 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
501 /* See if it is one we want to handle. */
503 if (lp_disable_spoolss() && strequal(fname, "\\spoolss"))
504 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
506 for( i = 0; known_nt_pipes[i]; i++ )
507 if( strequal(fname,known_nt_pipes[i]))
510 if ( known_nt_pipes[i] == NULL )
511 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
513 /* Strip \\ off the name. */
516 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
518 p = open_rpc_pipe_p(fname, conn, vuid);
520 return(ERROR_DOS(ERRSRV,ERRnofids));
527 /****************************************************************************
528 Reply to an NT create and X call for pipes.
529 ****************************************************************************/
531 static int do_ntcreate_pipe_open(connection_struct *conn,
532 char *inbuf,char *outbuf,int length,int bufsize)
540 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
541 if (!NT_STATUS_IS_OK(status))
542 return ERROR_NT(status);
544 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
548 * Deal with pipe return.
551 set_message(outbuf,34,0,True);
553 p = outbuf + smb_vwv2;
557 SIVAL(p,0,FILE_WAS_OPENED);
560 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
563 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
565 SSVAL(p,2, 0x5FF); /* ? */
567 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
569 return chain_reply(inbuf,outbuf,length,bufsize);
572 /****************************************************************************
573 Reply to an NT create and X call.
574 ****************************************************************************/
576 int reply_ntcreate_and_X(connection_struct *conn,
577 char *inbuf,char *outbuf,int length,int bufsize)
581 enum FAKE_FILE_TYPE fake_file_type = FAKE_FILE_TYPE_NONE;
582 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
583 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
584 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
585 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
586 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
587 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
588 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
589 SMB_BIG_UINT allocation_size = 0;
592 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
593 /* Breakout the oplock request bits so we can set the
594 reply bits separately. */
595 int oplock_request = 0;
598 SMB_OFF_T file_len = 0;
599 SMB_STRUCT_STAT sbuf;
601 BOOL bad_path = False;
602 files_struct *fsp=NULL;
605 BOOL extended_oplock_granted = False;
608 START_PROFILE(SMBntcreateX);
610 DEBUG(10,("reply_ntcreateX: flags = 0x%x, desired_access = 0x%x \
611 file_attributes = 0x%x, share_access = 0x%x, create_disposition = 0x%x \
612 create_options = 0x%x root_dir_fid = 0x%x\n", flags, desired_access, file_attributes,
613 share_access, create_disposition,
614 create_options, root_dir_fid ));
616 /* If it's an IPC, use the pipe handler. */
619 if (lp_nt_pipe_support()) {
620 END_PROFILE(SMBntcreateX);
621 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
623 END_PROFILE(SMBntcreateX);
624 return(ERROR_DOS(ERRDOS,ERRnoaccess));
628 if (create_options & FILE_OPEN_BY_FILE_ID) {
629 END_PROFILE(SMBntcreateX);
630 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
634 * We need to construct the open_and_X ofun value from the
635 * NT values, as that's what our code is structured to accept.
638 if((smb_ofun = map_create_disposition( create_disposition )) == -1) {
639 END_PROFILE(SMBntcreateX);
640 return(ERROR_DOS(ERRDOS,ERRnoaccess));
647 if(root_dir_fid != 0) {
649 * This filename is relative to a directory fid.
651 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
655 END_PROFILE(SMBntcreateX);
656 return(ERROR_DOS(ERRDOS,ERRbadfid));
659 if(!dir_fsp->is_directory) {
661 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
662 if (!NT_STATUS_IS_OK(status)) {
663 END_PROFILE(SMBntcreateX);
664 return ERROR_NT(status);
668 * Check to see if this is a mac fork of some kind.
671 if( strchr_m(fname, ':')) {
672 END_PROFILE(SMBntcreateX);
673 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
677 we need to handle the case when we get a
678 relative open relative to a file and the
679 pathname is blank - this is a reopen!
680 (hint from demyn plantenberg)
683 END_PROFILE(SMBntcreateX);
684 return(ERROR_DOS(ERRDOS,ERRbadfid));
688 * Copy in the base directory name.
691 pstrcpy( fname, dir_fsp->fsp_name );
692 dir_name_len = strlen(fname);
695 * Ensure it ends in a '\'.
698 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
699 pstrcat(fname, "\\");
703 srvstr_get_path(inbuf, &fname[dir_name_len], smb_buf(inbuf), sizeof(fname)-dir_name_len, 0, STR_TERMINATE, &status);
704 if (!NT_STATUS_IS_OK(status)) {
705 END_PROFILE(SMBntcreateX);
706 return ERROR_NT(status);
709 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
710 if (!NT_STATUS_IS_OK(status)) {
711 END_PROFILE(SMBntcreateX);
712 return ERROR_NT(status);
716 * Check to see if this is a mac fork of some kind.
719 if( strchr_m(fname, ':')) {
721 #ifdef HAVE_SYS_QUOTAS
722 if ((fake_file_type=is_fake_file(fname))!=FAKE_FILE_TYPE_NONE) {
724 * here we go! support for changing the disk quotas --metze
726 * we need to fake up to open this MAGIC QUOTA file
727 * and return a valid FID
729 * w2k close this file directly after openening
730 * xp also tries a QUERY_FILE_INFO on the file and then close it
734 END_PROFILE(SMBntcreateX);
735 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
736 #ifdef HAVE_SYS_QUOTAS
743 * Now contruct the smb_open_mode value from the filename,
744 * desired access and the share access.
746 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
748 if((smb_open_mode = map_share_mode(fname, create_options, &desired_access,
750 file_attributes)) == -1) {
751 END_PROFILE(SMBntcreateX);
752 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
755 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
756 if (oplock_request) {
757 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
761 * Ordinary file or directory.
765 * Check if POSIX semantics are wanted.
768 set_posix_case_semantics(file_attributes);
770 unix_convert(fname,conn,0,&bad_path,&sbuf);
772 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
775 * If it's a request for a directory open, deal with it separately.
778 if(create_options & FILE_DIRECTORY_FILE) {
781 /* Can't open a temp directory. IFS kit test. */
782 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
783 END_PROFILE(SMBntcreateX);
784 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
787 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
789 restore_case_semantics(file_attributes);
792 END_PROFILE(SMBntcreateX);
793 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
797 * Ordinary file case.
800 /* NB. We have a potential bug here. If we
801 * cause an oplock break to ourselves, then we
802 * could end up processing filename related
803 * SMB requests whilst we await the oplock
804 * break response. As we may have changed the
805 * filename case semantics to be POSIX-like,
806 * this could mean a filename request could
807 * fail when it should succeed. This is a rare
808 * condition, but eventually we must arrange
809 * to restore the correct case semantics
810 * before issuing an oplock break request to
811 * our client. JRA. */
813 if (fake_file_type==FAKE_FILE_TYPE_NONE) {
814 fsp = open_file_shared1(conn,fname,&sbuf,
817 smb_ofun,unixmode, oplock_request,
820 /* to open a fake_file --metze */
821 fsp = open_fake_file_shared1(fake_file_type,conn,fname,&sbuf,
824 smb_ofun,unixmode, oplock_request,
829 /* We cheat here. There are two cases we
830 * care about. One is a directory rename,
831 * where the NT client will attempt to
832 * open the source directory for
833 * DELETE access. Note that when the
834 * NT client does this it does *not*
835 * set the directory bit in the
836 * request packet. This is translated
837 * into a read/write open
838 * request. POSIX states that any open
839 * for write request on a directory
840 * will generate an EISDIR error, so
841 * we can catch this here and open a
842 * pseudo handle that is flagged as a
843 * directory. The second is an open
844 * for a permissions read only, which
845 * we handle in the open_file_stat case. JRA.
848 if(errno == EISDIR) {
851 * Fail the open if it was explicitly a non-directory file.
854 if (create_options & FILE_NON_DIRECTORY_FILE) {
855 restore_case_semantics(file_attributes);
856 SSVAL(outbuf, smb_flg2,
857 SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
858 END_PROFILE(SMBntcreateX);
859 return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
863 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
866 restore_case_semantics(file_attributes);
867 END_PROFILE(SMBntcreateX);
868 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
872 restore_case_semantics(file_attributes);
873 END_PROFILE(SMBntcreateX);
874 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
879 restore_case_semantics(file_attributes);
881 file_len = sbuf.st_size;
882 fmode = dos_mode(conn,fname,&sbuf);
884 fmode = FILE_ATTRIBUTE_NORMAL;
885 if (!fsp->is_directory && (fmode & aDIR)) {
886 close_file(fsp,False);
887 END_PROFILE(SMBntcreateX);
888 return ERROR_DOS(ERRDOS,ERRnoaccess);
891 /* Save the requested allocation size. */
892 allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize);
893 #ifdef LARGE_SMB_OFF_T
894 allocation_size |= (((SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
896 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
897 fsp->initial_allocation_size = SMB_ROUNDUP(allocation_size,SMB_ROUNDUP_ALLOCATION_SIZE);
898 if (fsp->is_directory) {
899 close_file(fsp,False);
900 END_PROFILE(SMBntcreateX);
901 /* Can't set allocation size on a directory. */
902 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
904 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
905 close_file(fsp,False);
906 END_PROFILE(SMBntcreateX);
907 return ERROR_NT(NT_STATUS_DISK_FULL);
910 fsp->initial_allocation_size = SMB_ROUNDUP(((SMB_BIG_UINT)file_len),SMB_ROUNDUP_ALLOCATION_SIZE);
914 * If the caller set the extended oplock request bit
915 * and we granted one (by whatever means) - set the
916 * correct bit for extended oplock reply.
919 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
920 extended_oplock_granted = True;
922 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
923 extended_oplock_granted = True;
926 /* W2K sends back 42 words here ! If we do the same it breaks offline sync. Go figure... ? JRA. */
927 set_message(outbuf,42,0,True);
929 set_message(outbuf,34,0,True);
932 p = outbuf + smb_vwv2;
935 * Currently as we don't support level II oplocks we just report
936 * exclusive & batch here.
939 if (extended_oplock_granted) {
940 if (flags & REQUEST_BATCH_OPLOCK) {
941 SCVAL(p,0, BATCH_OPLOCK_RETURN);
943 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
945 } else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
946 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
948 SCVAL(p,0,NO_OPLOCK_RETURN);
952 SSVAL(p,0,fsp->fnum);
954 if ((create_disposition == FILE_SUPERSEDE) && (smb_action == FILE_WAS_OVERWRITTEN))
955 SIVAL(p,0,FILE_WAS_SUPERSEDED);
957 SIVAL(p,0,smb_action);
961 c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
963 if (lp_dos_filetime_resolution(SNUM(conn))) {
970 put_long_date(p,c_time);
972 put_long_date(p,sbuf.st_atime); /* access time */
974 put_long_date(p,sbuf.st_mtime); /* write time */
976 put_long_date(p,sbuf.st_mtime); /* change time */
978 SIVAL(p,0,fmode); /* File Attributes. */
980 SOFF_T(p, 0, get_allocation_size(fsp,&sbuf));
982 SOFF_T(p,0,file_len);
984 if (flags & EXTENDED_RESPONSE_REQUIRED)
987 SCVAL(p,0,fsp->is_directory ? 1 : 0);
989 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
991 result = chain_reply(inbuf,outbuf,length,bufsize);
992 END_PROFILE(SMBntcreateX);
996 /****************************************************************************
997 Reply to a NT_TRANSACT_CREATE call to open a pipe.
998 ****************************************************************************/
1000 static int do_nt_transact_create_pipe( connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1001 char **ppsetup, uint32 setup_count,
1002 char **ppparams, uint32 parameter_count,
1003 char **ppdata, uint32 data_count)
1006 char *params = *ppparams;
1013 * Ensure minimum number of parameters sent.
1016 if(parameter_count < 54) {
1017 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1018 return ERROR_DOS(ERRDOS,ERRnoaccess);
1021 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
1022 if (!NT_STATUS_IS_OK(status)) {
1023 return ERROR_NT(status);
1026 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
1029 /* Realloc the size of parameters and data we will return */
1030 params = nttrans_realloc(ppparams, 69);
1032 return ERROR_DOS(ERRDOS,ERRnomem);
1035 SCVAL(p,0,NO_OPLOCK_RETURN);
1040 SIVAL(p,0,FILE_WAS_OPENED);
1044 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1047 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1049 SSVAL(p,2, 0x5FF); /* ? */
1051 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1053 /* Send the required number of replies */
1054 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1059 /****************************************************************************
1060 Internal fn to set security descriptors.
1061 ****************************************************************************/
1063 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1066 SEC_DESC *psd = NULL;
1067 TALLOC_CTX *mem_ctx;
1071 return NT_STATUS_OK;
1075 * Init the parse struct we will unmarshall from.
1078 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1079 DEBUG(0,("set_sd: talloc_init failed.\n"));
1080 return NT_STATUS_NO_MEMORY;
1083 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1086 * Setup the prs_struct to point at the memory we just
1090 prs_give_memory( &pd, data, sd_len, False);
1093 * Finally, unmarshall from the data buffer.
1096 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1097 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1099 * Return access denied for want of a better error message..
1101 talloc_destroy(mem_ctx);
1102 return NT_STATUS_NO_MEMORY;
1105 if (psd->off_owner_sid==0)
1106 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1107 if (psd->off_grp_sid==0)
1108 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1109 if (psd->off_sacl==0)
1110 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1111 if (psd->off_dacl==0)
1112 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1114 ret = SMB_VFS_FSET_NT_ACL( fsp, fsp->fd, security_info_sent, psd);
1117 talloc_destroy(mem_ctx);
1118 return NT_STATUS_ACCESS_DENIED;
1121 talloc_destroy(mem_ctx);
1123 return NT_STATUS_OK;
1126 /****************************************************************************
1127 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1128 ****************************************************************************/
1130 static int call_nt_transact_create(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1131 char **ppsetup, uint32 setup_count,
1132 char **ppparams, uint32 parameter_count,
1133 char **ppdata, uint32 data_count)
1136 char *params = *ppparams;
1137 char *data = *ppdata;
1138 /* Breakout the oplock request bits so we can set the reply bits separately. */
1139 int oplock_request = 0;
1141 int fmode=0,rmode=0;
1142 SMB_OFF_T file_len = 0;
1143 SMB_STRUCT_STAT sbuf;
1145 BOOL bad_path = False;
1146 files_struct *fsp = NULL;
1148 BOOL extended_oplock_granted = False;
1150 uint32 desired_access;
1151 uint32 file_attributes;
1152 uint32 share_access;
1153 uint32 create_disposition;
1154 uint32 create_options;
1156 uint16 root_dir_fid;
1157 SMB_BIG_UINT allocation_size = 0;
1164 DEBUG(5,("call_nt_transact_create\n"));
1167 * If it's an IPC, use the pipe handler.
1171 if (lp_nt_pipe_support())
1172 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1174 ppsetup, setup_count,
1175 ppparams, parameter_count,
1176 ppdata, data_count);
1178 return ERROR_DOS(ERRDOS,ERRnoaccess);
1182 * Ensure minimum number of parameters sent.
1185 if(parameter_count < 54) {
1186 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1187 return ERROR_DOS(ERRDOS,ERRnoaccess);
1190 flags = IVAL(params,0);
1191 desired_access = IVAL(params,8);
1192 file_attributes = IVAL(params,20);
1193 share_access = IVAL(params,24);
1194 create_disposition = IVAL(params,28);
1195 create_options = IVAL(params,32);
1196 sd_len = IVAL(params,36);
1197 root_dir_fid = (uint16)IVAL(params,4);
1198 smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
1200 if (create_options & FILE_OPEN_BY_FILE_ID) {
1201 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1205 * We need to construct the open_and_X ofun value from the
1206 * NT values, as that's what our code is structured to accept.
1209 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
1210 return ERROR_DOS(ERRDOS,ERRbadmem);
1213 * Get the file name.
1216 if(root_dir_fid != 0) {
1218 * This filename is relative to a directory fid.
1221 files_struct *dir_fsp = file_fsp(params,4);
1222 size_t dir_name_len;
1225 return ERROR_DOS(ERRDOS,ERRbadfid);
1227 if(!dir_fsp->is_directory) {
1228 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
1229 if (!NT_STATUS_IS_OK(status)) {
1230 return ERROR_NT(status);
1234 * Check to see if this is a mac fork of some kind.
1237 if( strchr_m(fname, ':'))
1238 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1240 return ERROR_DOS(ERRDOS,ERRbadfid);
1244 * Copy in the base directory name.
1247 pstrcpy( fname, dir_fsp->fsp_name );
1248 dir_name_len = strlen(fname);
1251 * Ensure it ends in a '\'.
1254 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1255 pstrcat(fname, "\\");
1261 srvstr_get_path(inbuf, tmpname, params+53, sizeof(tmpname), parameter_count-53, STR_TERMINATE, &status);
1262 if (!NT_STATUS_IS_OK(status)) {
1263 return ERROR_NT(status);
1265 pstrcat(fname, tmpname);
1268 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
1269 if (!NT_STATUS_IS_OK(status)) {
1270 return ERROR_NT(status);
1274 * Check to see if this is a mac fork of some kind.
1277 if( strchr_m(fname, ':'))
1278 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1282 * Now contruct the smb_open_mode value from the desired access
1283 * and the share access.
1286 if((smb_open_mode = map_share_mode( fname, create_options, &desired_access,
1287 share_access, file_attributes)) == -1)
1288 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1290 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1291 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1294 * Check if POSIX semantics are wanted.
1297 set_posix_case_semantics(file_attributes);
1299 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1301 unix_convert(fname,conn,0,&bad_path,&sbuf);
1303 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
1306 * If it's a request for a directory open, deal with it separately.
1309 if(create_options & FILE_DIRECTORY_FILE) {
1311 /* Can't open a temp directory. IFS kit test. */
1312 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1313 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1319 * We will get a create directory here if the Win32
1320 * app specified a security descriptor in the
1321 * CreateDirectory() call.
1324 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
1327 restore_case_semantics(file_attributes);
1328 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1334 * Ordinary file case.
1337 fsp = open_file_shared1(conn,fname,&sbuf,desired_access,
1338 smb_open_mode,smb_ofun,unixmode,
1339 oplock_request,&rmode,&smb_action);
1343 if(errno == EISDIR) {
1346 * Fail the open if it was explicitly a non-directory file.
1349 if (create_options & FILE_NON_DIRECTORY_FILE) {
1350 restore_case_semantics(file_attributes);
1351 SSVAL(outbuf, smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1352 return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1356 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
1359 restore_case_semantics(file_attributes);
1360 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1363 restore_case_semantics(file_attributes);
1364 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1368 file_len = sbuf.st_size;
1369 fmode = dos_mode(conn,fname,&sbuf);
1371 fmode = FILE_ATTRIBUTE_NORMAL;
1374 close_file(fsp,False);
1375 restore_case_semantics(file_attributes);
1376 return ERROR_DOS(ERRDOS,ERRnoaccess);
1380 * If the caller set the extended oplock request bit
1381 * and we granted one (by whatever means) - set the
1382 * correct bit for extended oplock reply.
1385 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1386 extended_oplock_granted = True;
1388 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1389 extended_oplock_granted = True;
1393 * Now try and apply the desired SD.
1396 if (sd_len && !NT_STATUS_IS_OK(status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION))) {
1397 close_file(fsp,False);
1398 restore_case_semantics(file_attributes);
1399 return ERROR_NT(status);
1402 restore_case_semantics(file_attributes);
1404 /* Save the requested allocation size. */
1405 allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1406 #ifdef LARGE_SMB_OFF_T
1407 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1409 if (allocation_size && (allocation_size > file_len)) {
1410 fsp->initial_allocation_size = SMB_ROUNDUP(allocation_size,SMB_ROUNDUP_ALLOCATION_SIZE);
1411 if (fsp->is_directory) {
1412 close_file(fsp,False);
1413 END_PROFILE(SMBntcreateX);
1414 /* Can't set allocation size on a directory. */
1415 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1417 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1418 close_file(fsp,False);
1419 return ERROR_NT(NT_STATUS_DISK_FULL);
1422 fsp->initial_allocation_size = SMB_ROUNDUP(((SMB_BIG_UINT)file_len),SMB_ROUNDUP_ALLOCATION_SIZE);
1425 /* Realloc the size of parameters and data we will return */
1426 params = nttrans_realloc(ppparams, 69);
1428 return ERROR_DOS(ERRDOS,ERRnomem);
1431 if (extended_oplock_granted)
1432 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1433 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1434 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1436 SCVAL(p,0,NO_OPLOCK_RETURN);
1439 SSVAL(p,0,fsp->fnum);
1441 if ((create_disposition == FILE_SUPERSEDE) && (smb_action == FILE_WAS_OVERWRITTEN))
1442 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1444 SIVAL(p,0,smb_action);
1448 c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1450 if (lp_dos_filetime_resolution(SNUM(conn))) {
1452 sbuf.st_atime &= ~1;
1453 sbuf.st_mtime &= ~1;
1454 sbuf.st_mtime &= ~1;
1457 put_long_date(p,c_time);
1459 put_long_date(p,sbuf.st_atime); /* access time */
1461 put_long_date(p,sbuf.st_mtime); /* write time */
1463 put_long_date(p,sbuf.st_mtime); /* change time */
1465 SIVAL(p,0,fmode); /* File Attributes. */
1467 SOFF_T(p, 0, get_allocation_size(fsp,&sbuf));
1469 SOFF_T(p,0,file_len);
1471 if (flags & EXTENDED_RESPONSE_REQUIRED)
1474 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1476 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1478 /* Send the required number of replies */
1479 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1484 /****************************************************************************
1485 Reply to a NT CANCEL request.
1486 ****************************************************************************/
1488 int reply_ntcancel(connection_struct *conn,
1489 char *inbuf,char *outbuf,int length,int bufsize)
1492 * Go through and cancel any pending change notifies.
1495 int mid = SVAL(inbuf,smb_mid);
1496 START_PROFILE(SMBntcancel);
1497 remove_pending_change_notify_requests_by_mid(mid);
1498 remove_pending_lock_requests_by_mid(mid);
1499 srv_cancel_sign_response(mid);
1501 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1503 END_PROFILE(SMBntcancel);
1507 /****************************************************************************
1508 Reply to a NT rename request.
1509 ****************************************************************************/
1511 int reply_ntrename(connection_struct *conn,
1512 char *inbuf,char *outbuf,int length,int bufsize)
1519 uint16 attrs = SVAL(inbuf,smb_vwv0);
1520 uint16 rename_type = SVAL(inbuf,smb_vwv1);
1522 START_PROFILE(SMBntrename);
1524 if (rename_type != RENAME_FLAG_RENAME) {
1525 END_PROFILE(SMBntrename);
1526 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1529 p = smb_buf(inbuf) + 1;
1530 p += srvstr_get_path(inbuf, name, p, sizeof(name), 0, STR_TERMINATE, &status);
1531 if (!NT_STATUS_IS_OK(status)) {
1532 END_PROFILE(SMBntrename);
1533 return ERROR_NT(status);
1536 if( strchr_m(name, ':')) {
1537 /* Can't rename a stream. */
1538 END_PROFILE(SMBntrename);
1539 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1543 p += srvstr_get_path(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status);
1544 if (!NT_STATUS_IS_OK(status)) {
1545 END_PROFILE(SMBntrename);
1546 return ERROR_NT(status);
1549 RESOLVE_DFSPATH(name, conn, inbuf, outbuf);
1550 RESOLVE_DFSPATH(newname, conn, inbuf, outbuf);
1552 DEBUG(3,("reply_ntrename : %s -> %s\n",name,newname));
1554 status = rename_internals(conn, name, newname, attrs, False);
1555 if (!NT_STATUS_IS_OK(status)) {
1556 END_PROFILE(SMBntrename);
1557 return ERROR_NT(status);
1561 * Win2k needs a changenotify request response before it will
1562 * update after a rename..
1564 process_pending_change_notify_queue((time_t)0);
1565 outsize = set_message(outbuf,0,0,True);
1567 END_PROFILE(SMBntrename);
1571 /****************************************************************************
1572 Reply to an unsolicited SMBNTtranss - just ignore it!
1573 ****************************************************************************/
1575 int reply_nttranss(connection_struct *conn,
1576 char *inbuf,char *outbuf,int length,int bufsize)
1578 START_PROFILE(SMBnttranss);
1579 DEBUG(4,("Ignoring nttranss of length %d\n",length));
1580 END_PROFILE(SMBnttranss);
1584 /****************************************************************************
1585 Reply to a notify change - queue the request and
1586 don't allow a directory to be opened.
1587 ****************************************************************************/
1589 static int call_nt_transact_notify_change(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1590 char **ppsetup, uint32 setup_count,
1591 char **ppparams, uint32 parameter_count,
1592 char **ppdata, uint32 data_count)
1594 char *setup = *ppsetup;
1599 return ERROR_DOS(ERRDOS,ERRbadfunc);
1601 fsp = file_fsp(setup,4);
1602 flags = IVAL(setup, 0);
1604 DEBUG(3,("call_nt_transact_notify_change\n"));
1607 return ERROR_DOS(ERRDOS,ERRbadfid);
1609 if((!fsp->is_directory) || (conn != fsp->conn))
1610 return ERROR_DOS(ERRDOS,ERRbadfid);
1612 if (!change_notify_set(inbuf, fsp, conn, flags))
1613 return(UNIXERROR(ERRDOS,ERRbadfid));
1615 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1616 name = %s\n", fsp->fsp_name ));
1621 /****************************************************************************
1622 Reply to an NT transact rename command.
1623 ****************************************************************************/
1625 static int call_nt_transact_rename(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1626 char **ppsetup, uint32 setup_count,
1627 char **ppparams, uint32 parameter_count,
1628 char **ppdata, uint32 data_count)
1630 char *params = *ppparams;
1632 files_struct *fsp = NULL;
1633 BOOL replace_if_exists = False;
1636 if(parameter_count < 4)
1637 return ERROR_DOS(ERRDOS,ERRbadfunc);
1639 fsp = file_fsp(params, 0);
1640 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1641 CHECK_FSP(fsp, conn);
1642 srvstr_get_path(inbuf, new_name, params+4, sizeof(new_name), -1, STR_TERMINATE, &status);
1643 if (!NT_STATUS_IS_OK(status)) {
1644 return ERROR_NT(status);
1647 status = rename_internals(conn, fsp->fsp_name,
1648 new_name, 0, replace_if_exists);
1649 if (!NT_STATUS_IS_OK(status))
1650 return ERROR_NT(status);
1653 * Rename was successful.
1655 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1657 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1658 fsp->fsp_name, new_name));
1661 * Win2k needs a changenotify request response before it will
1662 * update after a rename..
1665 process_pending_change_notify_queue((time_t)0);
1670 /******************************************************************************
1671 Fake up a completely empty SD.
1672 *******************************************************************************/
1674 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1676 extern DOM_SID global_sid_World;
1679 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1681 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1688 /****************************************************************************
1689 Reply to query a security descriptor.
1690 ****************************************************************************/
1692 static int call_nt_transact_query_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1693 char **ppsetup, uint32 setup_count,
1694 char **ppparams, uint32 parameter_count,
1695 char **ppdata, uint32 data_count)
1697 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1698 char *params = *ppparams;
1699 char *data = *ppdata;
1701 SEC_DESC *psd = NULL;
1703 uint32 security_info_wanted;
1704 TALLOC_CTX *mem_ctx;
1705 files_struct *fsp = NULL;
1707 if(parameter_count < 8)
1708 return ERROR_DOS(ERRDOS,ERRbadfunc);
1710 fsp = file_fsp(params,0);
1712 return ERROR_DOS(ERRDOS,ERRbadfid);
1714 security_info_wanted = IVAL(params,4);
1716 DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1718 params = nttrans_realloc(ppparams, 4);
1720 return ERROR_DOS(ERRDOS,ERRnomem);
1722 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
1723 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1724 return ERROR_DOS(ERRDOS,ERRnomem);
1728 * Get the permissions to return.
1731 if (!lp_nt_acl_support(SNUM(conn)))
1732 sd_size = get_null_nt_acl(mem_ctx, &psd);
1734 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fd, security_info_wanted, &psd);
1737 talloc_destroy(mem_ctx);
1738 return(UNIXERROR(ERRDOS,ERRnoaccess));
1741 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %d.\n",(int)sd_size));
1743 SIVAL(params,0,(uint32)sd_size);
1745 if(max_data_count < sd_size) {
1747 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1748 params, 4, *ppdata, 0);
1749 talloc_destroy(mem_ctx);
1754 * Allocate the data we will point this at.
1757 data = nttrans_realloc(ppdata, sd_size);
1759 talloc_destroy(mem_ctx);
1760 return ERROR_DOS(ERRDOS,ERRnomem);
1764 * Init the parse struct we will marshall into.
1767 prs_init(&pd, 0, mem_ctx, MARSHALL);
1770 * Setup the prs_struct to point at the memory we just
1774 prs_give_memory( &pd, data, (uint32)sd_size, False);
1777 * Finally, linearize into the outgoing buffer.
1780 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1781 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1782 security descriptor.\n"));
1784 * Return access denied for want of a better error message..
1786 talloc_destroy(mem_ctx);
1787 return(UNIXERROR(ERRDOS,ERRnoaccess));
1791 * Now we can delete the security descriptor.
1794 talloc_destroy(mem_ctx);
1796 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data, (int)sd_size);
1800 /****************************************************************************
1801 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1802 ****************************************************************************/
1804 static int call_nt_transact_set_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1805 char **ppsetup, uint32 setup_count,
1806 char **ppparams, uint32 parameter_count,
1807 char **ppdata, uint32 data_count)
1809 char *params= *ppparams;
1810 char *data = *ppdata;
1811 files_struct *fsp = NULL;
1812 uint32 security_info_sent = 0;
1815 if(parameter_count < 8)
1816 return ERROR_DOS(ERRDOS,ERRbadfunc);
1818 if((fsp = file_fsp(params,0)) == NULL)
1819 return ERROR_DOS(ERRDOS,ERRbadfid);
1821 if(!lp_nt_acl_support(SNUM(conn)))
1824 security_info_sent = IVAL(params,4);
1826 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1827 (unsigned int)security_info_sent ));
1829 if (data_count == 0)
1830 return ERROR_DOS(ERRDOS, ERRnoaccess);
1832 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent)))
1833 return ERROR_NT(nt_status);
1837 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1841 /****************************************************************************
1843 ****************************************************************************/
1845 static int call_nt_transact_ioctl(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1846 char **ppsetup, uint32 setup_count,
1847 char **ppparams, uint32 parameter_count,
1848 char **ppdata, uint32 data_count)
1855 static BOOL logged_message;
1856 char *pdata = *ppdata;
1858 if (setup_count != 8) {
1859 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1860 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1863 function = IVAL(*ppsetup, 0);
1864 fidnum = SVAL(*ppsetup, 4);
1865 isFSctl = CVAL(*ppsetup, 6);
1866 compfilter = CVAL(*ppsetup, 7);
1868 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1869 function, fidnum, isFSctl, compfilter));
1871 fsp=file_fsp(*ppsetup, 4);
1872 /* this check is done in each implemented function case for now
1873 because I don't want to break anything... --metze
1874 FSP_BELONGS_CONN(fsp,conn);*/
1877 case FSCTL_SET_SPARSE:
1878 /* pretend this succeeded - tho strictly we should
1879 mark the file sparse (if the local fs supports it)
1880 so we can know if we need to pre-allocate or not */
1882 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1883 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1886 case FSCTL_0x000900C0:
1887 /* pretend this succeeded - don't know what this really is
1888 but works ok like this --metze
1891 DEBUG(10,("FSCTL_0x000900C0: called on FID[0x%04X](but not implemented)\n",fidnum));
1892 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1895 case FSCTL_GET_REPARSE_POINT:
1896 /* pretend this fail - my winXP does it like this
1900 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1901 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
1904 case FSCTL_SET_REPARSE_POINT:
1905 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1909 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1910 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
1913 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1916 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1917 * and return their volume names. If max_data_count is 16, then it is just
1918 * asking for the number of volumes and length of the combined names.
1920 * pdata is the data allocated by our caller, but that uses
1921 * total_data_count (which is 0 in our case) rather than max_data_count.
1922 * Allocate the correct amount and return the pointer to let
1923 * it be deallocated when we return.
1925 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1926 SHADOW_COPY_DATA *shadow_data = NULL;
1927 TALLOC_CTX *shadow_mem_ctx = NULL;
1928 BOOL labels = False;
1929 uint32 labels_data_count = 0;
1933 FSP_BELONGS_CONN(fsp,conn);
1935 if (max_data_count < 16) {
1936 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1938 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1941 if (max_data_count > 16) {
1945 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1946 if (shadow_mem_ctx == NULL) {
1947 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1948 return ERROR_NT(NT_STATUS_NO_MEMORY);
1951 shadow_data = (SHADOW_COPY_DATA *)talloc_zero(shadow_mem_ctx,sizeof(SHADOW_COPY_DATA));
1952 if (shadow_data == NULL) {
1953 DEBUG(0,("talloc_zero() failed!\n"));
1954 return ERROR_NT(NT_STATUS_NO_MEMORY);
1957 shadow_data->mem_ctx = shadow_mem_ctx;
1960 * Call the VFS routine to actually do the work.
1962 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
1963 talloc_destroy(shadow_data->mem_ctx);
1964 if (errno == ENOSYS) {
1965 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
1966 conn->connectpath));
1967 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1969 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
1970 conn->connectpath));
1971 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
1975 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
1980 data_count = 12+labels_data_count+4;
1983 if (max_data_count<data_count) {
1984 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
1985 max_data_count,data_count));
1986 talloc_destroy(shadow_data->mem_ctx);
1987 return ERROR_NT(NT_STATUS_BUFFER_TOO_SMALL);
1990 pdata = nttrans_realloc(ppdata, data_count);
1991 if (pdata == NULL) {
1992 talloc_destroy(shadow_data->mem_ctx);
1993 return ERROR_NT(NT_STATUS_NO_MEMORY);
1998 /* num_volumes 4 bytes */
1999 SIVAL(pdata,0,shadow_data->num_volumes);
2002 /* num_labels 4 bytes */
2003 SIVAL(pdata,4,shadow_data->num_volumes);
2006 /* needed_data_count 4 bytes */
2007 SIVAL(pdata,8,labels_data_count);
2011 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2012 shadow_data->num_volumes,fsp->fsp_name));
2013 if (labels && shadow_data->labels) {
2014 for (i=0;i<shadow_data->num_volumes;i++) {
2015 srvstr_push(outbuf, cur_pdata, shadow_data->labels[i], 2*sizeof(SHADOW_COPY_LABEL), STR_UNICODE|STR_TERMINATE);
2016 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2017 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2021 talloc_destroy(shadow_data->mem_ctx);
2023 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, pdata, data_count);
2028 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2030 /* pretend this succeeded -
2032 * we have to send back a list with all files owned by this SID
2034 * but I have to check that --metze
2038 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2040 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2042 FSP_BELONGS_CONN(fsp,conn);
2044 /* unknown 4 bytes: this is not the length of the sid :-( */
2045 /*unknown = IVAL(pdata,0);*/
2047 sid_parse(pdata+4,sid_len,&sid);
2048 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2050 if (!NT_STATUS_IS_OK(sid_to_uid(&sid, &uid))) {
2051 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2052 sid_string_static(&sid),(unsigned long)sid_len));
2056 /* we can take a look at the find source :-)
2058 * find ./ -uid $uid -name '*' is what we need here
2061 * and send 4bytes len and then NULL terminated unicode strings
2064 * but I don't know how to deal with the paged results
2065 * (maybe we can hang the result anywhere in the fsp struct)
2067 * we don't send all files at once
2068 * and at the next we should *not* start from the beginning,
2069 * so we have to cache the result
2074 /* this works for now... */
2075 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2079 if (!logged_message) {
2080 logged_message = True; /* Only print this once... */
2081 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2086 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2090 #ifdef HAVE_SYS_QUOTAS
2091 /****************************************************************************
2092 Reply to get user quota
2093 ****************************************************************************/
2095 static int call_nt_transact_get_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2096 char **ppsetup, uint32 setup_count,
2097 char **ppparams, uint32 parameter_count,
2098 char **ppdata, uint32 data_count)
2100 NTSTATUS nt_status = NT_STATUS_OK;
2101 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
2102 char *params = *ppparams;
2103 char *pdata = *ppdata;
2105 int data_len=0,param_len=0;
2108 files_struct *fsp = NULL;
2112 BOOL start_enum = True;
2113 SMB_NTQUOTA_STRUCT qt;
2114 SMB_NTQUOTA_LIST *tmp_list;
2115 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2116 extern struct current_user current_user;
2121 if (current_user.uid != 0) {
2122 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2123 lp_servicename(SNUM(conn)),conn->user));
2124 return ERROR_DOS(ERRDOS,ERRnoaccess);
2128 * Ensure minimum number of parameters sent.
2131 if (parameter_count < 4) {
2132 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2133 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2136 /* maybe we can check the quota_fnum */
2137 fsp = file_fsp(params,0);
2138 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2139 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2140 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2143 /* the NULL pointer cheking for fsp->fake_file_handle->pd
2144 * is done by CHECK_NTQUOTA_HANDLE_OK()
2146 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2148 level = SVAL(params,2);
2150 /* unknown 12 bytes leading in params */
2153 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2154 /* seems that we should continue with the enum here --metze */
2156 if (qt_handle->quota_list!=NULL &&
2157 qt_handle->tmp_list==NULL) {
2160 free_ntquota_list(&(qt_handle->quota_list));
2162 /* Realloc the size of parameters and data we will return */
2164 params = nttrans_realloc(ppparams, param_len);
2166 return ERROR_DOS(ERRDOS,ERRnomem);
2169 SIVAL(params,0,data_len);
2176 case TRANSACT_GET_USER_QUOTA_LIST_START:
2178 if (qt_handle->quota_list==NULL &&
2179 qt_handle->tmp_list==NULL) {
2183 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0)
2184 return ERROR_DOS(ERRSRV,ERRerror);
2186 /* Realloc the size of parameters and data we will return */
2188 params = nttrans_realloc(ppparams, param_len);
2190 return ERROR_DOS(ERRDOS,ERRnomem);
2192 /* we should not trust the value in max_data_count*/
2193 max_data_count = MIN(max_data_count,2048);
2195 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2197 return ERROR_DOS(ERRDOS,ERRnomem);
2202 /* set params Size of returned Quota Data 4 bytes*/
2203 /* but set it later when we know it */
2205 /* for each entry push the data */
2208 qt_handle->tmp_list = qt_handle->quota_list;
2211 tmp_list = qt_handle->tmp_list;
2213 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2214 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2216 sid_len = sid_size(&tmp_list->quotas->sid);
2217 entry_len = 40 + sid_len;
2219 /* nextoffset entry 4 bytes */
2220 SIVAL(entry,0,entry_len);
2222 /* then the len of the SID 4 bytes */
2223 SIVAL(entry,4,sid_len);
2225 /* unknown data 8 bytes SMB_BIG_UINT */
2226 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2228 /* the used disk space 8 bytes SMB_BIG_UINT */
2229 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2231 /* the soft quotas 8 bytes SMB_BIG_UINT */
2232 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2234 /* the hard quotas 8 bytes SMB_BIG_UINT */
2235 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2237 /* and now the SID */
2238 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2241 qt_handle->tmp_list = tmp_list;
2243 /* overwrite the offset of the last entry */
2244 SIVAL(entry-entry_len,0,0);
2246 data_len = 4+qt_len;
2247 /* overwrite the params quota_data_len */
2248 SIVAL(params,0,data_len);
2252 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2254 /* unknown 4 bytes IVAL(pdata,0) */
2256 if (data_count < 8) {
2257 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2258 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2261 sid_len = IVAL(pdata,4);
2263 if (data_count < 8+sid_len) {
2264 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2265 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2268 data_len = 4+40+sid_len;
2270 if (max_data_count < data_len) {
2271 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2272 max_data_count, data_len));
2274 SIVAL(params,0,data_len);
2276 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2280 sid_parse(pdata+8,sid_len,&sid);
2283 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2286 * we have to return zero's in all fields
2287 * instead of returning an error here
2292 /* Realloc the size of parameters and data we will return */
2294 params = nttrans_realloc(ppparams, param_len);
2296 return ERROR_DOS(ERRDOS,ERRnomem);
2298 pdata = nttrans_realloc(ppdata, data_len);
2300 return ERROR_DOS(ERRDOS,ERRnomem);
2304 /* set params Size of returned Quota Data 4 bytes*/
2305 SIVAL(params,0,data_len);
2307 /* nextoffset entry 4 bytes */
2310 /* then the len of the SID 4 bytes */
2311 SIVAL(entry,4,sid_len);
2313 /* unknown data 8 bytes SMB_BIG_UINT */
2314 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2316 /* the used disk space 8 bytes SMB_BIG_UINT */
2317 SBIG_UINT(entry,16,qt.usedspace);
2319 /* the soft quotas 8 bytes SMB_BIG_UINT */
2320 SBIG_UINT(entry,24,qt.softlim);
2322 /* the hard quotas 8 bytes SMB_BIG_UINT */
2323 SBIG_UINT(entry,32,qt.hardlim);
2325 /* and now the SID */
2326 sid_linearize(entry+40, sid_len, &sid);
2331 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2332 return ERROR_DOS(ERRSRV,ERRerror);
2336 send_nt_replies(inbuf, outbuf, bufsize, nt_status, params, param_len, pdata, data_len);
2341 /****************************************************************************
2342 Reply to set user quota
2343 ****************************************************************************/
2345 static int call_nt_transact_set_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2346 char **ppsetup, uint32 setup_count,
2347 char **ppparams, uint32 parameter_count,
2348 char **ppdata, uint32 data_count)
2350 char *params = *ppparams;
2351 char *pdata = *ppdata;
2352 int data_len=0,param_len=0;
2353 SMB_NTQUOTA_STRUCT qt;
2356 files_struct *fsp = NULL;
2361 if (conn->admin_user != True) {
2362 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2363 lp_servicename(SNUM(conn)),conn->user));
2364 return ERROR_DOS(ERRDOS,ERRnoaccess);
2368 * Ensure minimum number of parameters sent.
2371 if (parameter_count < 2) {
2372 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2373 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2376 /* maybe we can check the quota_fnum */
2377 fsp = file_fsp(params,0);
2378 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2379 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2380 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2383 if (data_count < 40) {
2384 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2385 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2388 /* offset to next quota record.
2389 * 4 bytes IVAL(pdata,0)
2394 sid_len = IVAL(pdata,4);
2396 if (data_count < 40+sid_len) {
2397 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2398 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2401 /* unknown 8 bytes in pdata
2402 * maybe its the change time in NTTIME
2405 /* the used space 8 bytes (SMB_BIG_UINT)*/
2406 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2407 #ifdef LARGE_SMB_OFF_T
2408 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2409 #else /* LARGE_SMB_OFF_T */
2410 if ((IVAL(pdata,20) != 0)&&
2411 ((qt.usedspace != 0xFFFFFFFF)||
2412 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2413 /* more than 32 bits? */
2414 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2416 #endif /* LARGE_SMB_OFF_T */
2418 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2419 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2420 #ifdef LARGE_SMB_OFF_T
2421 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2422 #else /* LARGE_SMB_OFF_T */
2423 if ((IVAL(pdata,28) != 0)&&
2424 ((qt.softlim != 0xFFFFFFFF)||
2425 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2426 /* more than 32 bits? */
2427 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2429 #endif /* LARGE_SMB_OFF_T */
2431 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2432 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2433 #ifdef LARGE_SMB_OFF_T
2434 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2435 #else /* LARGE_SMB_OFF_T */
2436 if ((IVAL(pdata,36) != 0)&&
2437 ((qt.hardlim != 0xFFFFFFFF)||
2438 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2439 /* more than 32 bits? */
2440 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2442 #endif /* LARGE_SMB_OFF_T */
2444 sid_parse(pdata+40,sid_len,&sid);
2445 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2447 /* 44 unknown bytes left... */
2449 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2450 return ERROR_DOS(ERRSRV,ERRerror);
2453 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, pdata, data_len);
2457 #endif /* HAVE_SYS_QUOTAS */
2459 /****************************************************************************
2460 Reply to a SMBNTtrans.
2461 ****************************************************************************/
2463 int reply_nttrans(connection_struct *conn,
2464 char *inbuf,char *outbuf,int length,int bufsize)
2467 #if 0 /* Not used. */
2468 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
2469 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
2470 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
2471 #endif /* Not used. */
2472 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
2473 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
2474 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
2475 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
2476 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
2477 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
2478 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
2479 uint16 function_code = SVAL( inbuf, smb_nt_Function);
2480 char *params = NULL, *data = NULL, *setup = NULL;
2481 uint32 num_params_sofar, num_data_sofar;
2482 START_PROFILE(SMBnttrans);
2484 if(global_oplock_break &&
2485 ((function_code == NT_TRANSACT_CREATE) ||
2486 (function_code == NT_TRANSACT_RENAME))) {
2488 * Queue this open message as we are the process of an oplock break.
2491 DEBUG(2,("reply_nttrans: queueing message code 0x%x \
2492 due to being in oplock break state.\n", (unsigned int)function_code ));
2494 push_oplock_pending_smb_message( inbuf, length);
2495 END_PROFILE(SMBnttrans);
2499 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2500 END_PROFILE(SMBnttrans);
2501 return ERROR_DOS(ERRSRV,ERRaccess);
2504 outsize = set_message(outbuf,0,0,True);
2507 * All nttrans messages we handle have smb_wct == 19 + setup_count.
2508 * Ensure this is so as a sanity check.
2511 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
2512 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2513 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
2517 /* Allocate the space for the setup, the maximum needed parameters and data */
2520 setup = (char *)malloc(setup_count);
2521 if (total_parameter_count > 0)
2522 params = (char *)malloc(total_parameter_count);
2523 if (total_data_count > 0)
2524 data = (char *)malloc(total_data_count);
2526 if ((total_parameter_count && !params) || (total_data_count && !data) ||
2527 (setup_count && !setup)) {
2531 DEBUG(0,("reply_nttrans : Out of memory\n"));
2532 END_PROFILE(SMBnttrans);
2533 return ERROR_DOS(ERRDOS,ERRnomem);
2536 /* Copy the param and data bytes sent with this request into the params buffer */
2537 num_params_sofar = parameter_count;
2538 num_data_sofar = data_count;
2540 if (parameter_count > total_parameter_count || data_count > total_data_count)
2544 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
2545 if ((smb_nt_SetupStart + setup_count < smb_nt_SetupStart) ||
2546 (smb_nt_SetupStart + setup_count < setup_count))
2548 if (smb_nt_SetupStart + setup_count > length)
2551 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
2552 dump_data(10, setup, setup_count);
2555 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
2556 if ((parameter_offset + parameter_count < parameter_offset) ||
2557 (parameter_offset + parameter_count < parameter_count))
2559 if ((smb_base(inbuf) + parameter_offset + parameter_count > inbuf + length)||
2560 (smb_base(inbuf) + parameter_offset + parameter_count < smb_base(inbuf)))
2563 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
2564 dump_data(10, params, parameter_count);
2567 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
2568 if ((data_offset + data_count < data_offset) || (data_offset + data_count < data_count))
2570 if ((smb_base(inbuf) + data_offset + data_count > inbuf + length) ||
2571 (smb_base(inbuf) + data_offset + data_count < smb_base(inbuf)))
2574 memcpy( data, smb_base(inbuf) + data_offset, data_count);
2575 dump_data(10, data, data_count);
2578 srv_signing_trans_start(SVAL(inbuf,smb_mid));
2580 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2581 /* We need to send an interim response then receive the rest
2582 of the parameter/data bytes */
2583 outsize = set_message(outbuf,0,0,True);
2584 srv_signing_trans_stop();
2585 if (!send_smb(smbd_server_fd(),outbuf))
2586 exit_server("reply_nttrans: send_smb failed.");
2588 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2590 uint32 parameter_displacement;
2591 uint32 data_displacement;
2593 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
2596 * The sequence number for the trans reply is always
2597 * based on the last secondary received.
2600 srv_signing_trans_start(SVAL(inbuf,smb_mid));
2602 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
2603 outsize = set_message(outbuf,0,0,True);
2605 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
2607 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
2608 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
2613 /* Revise total_params and total_data in case they have changed downwards */
2614 if (IVAL(inbuf, smb_nts_TotalParameterCount) < total_parameter_count)
2615 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
2616 if (IVAL(inbuf, smb_nts_TotalDataCount) < total_data_count)
2617 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
2619 parameter_count = IVAL(inbuf,smb_nts_ParameterCount);
2620 parameter_offset = IVAL(inbuf, smb_nts_ParameterOffset);
2621 parameter_displacement = IVAL(inbuf, smb_nts_ParameterDisplacement);
2622 num_params_sofar += parameter_count;
2624 data_count = IVAL(inbuf, smb_nts_DataCount);
2625 data_displacement = IVAL(inbuf, smb_nts_DataDisplacement);
2626 data_offset = IVAL(inbuf, smb_nts_DataOffset);
2627 num_data_sofar += data_count;
2629 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count) {
2630 DEBUG(0,("reply_nttrans2: data overflow in secondary nttrans packet"));
2634 if (parameter_count) {
2635 if (parameter_displacement + parameter_count >= total_parameter_count)
2637 if ((parameter_displacement + parameter_count < parameter_displacement) ||
2638 (parameter_displacement + parameter_count < parameter_count))
2640 if (parameter_displacement > total_parameter_count)
2642 if ((smb_base(inbuf) + parameter_offset + parameter_count >= inbuf + bufsize) ||
2643 (smb_base(inbuf) + parameter_offset + parameter_count < smb_base(inbuf)))
2645 if (parameter_displacement + params < params)
2648 memcpy( ¶ms[parameter_displacement], smb_base(inbuf) + parameter_offset, parameter_count);
2652 if (data_displacement + data_count >= total_data_count)
2654 if ((data_displacement + data_count < data_displacement) ||
2655 (data_displacement + data_count < data_count))
2657 if (data_displacement > total_data_count)
2659 if ((smb_base(inbuf) + data_offset + data_count >= inbuf + bufsize) ||
2660 (smb_base(inbuf) + data_offset + data_count < smb_base(inbuf)))
2662 if (data_displacement + data < data)
2665 memcpy( &data[data_displacement], smb_base(inbuf)+ data_offset, data_count);
2670 if (Protocol >= PROTOCOL_NT1)
2671 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | FLAGS2_IS_LONG_NAME);
2673 /* Now we must call the relevant NT_TRANS function */
2674 switch(function_code) {
2675 case NT_TRANSACT_CREATE:
2676 START_PROFILE_NESTED(NT_transact_create);
2677 outsize = call_nt_transact_create(conn, inbuf, outbuf,
2679 &setup, setup_count,
2680 ¶ms, total_parameter_count,
2681 &data, total_data_count);
2682 END_PROFILE_NESTED(NT_transact_create);
2684 case NT_TRANSACT_IOCTL:
2685 START_PROFILE_NESTED(NT_transact_ioctl);
2686 outsize = call_nt_transact_ioctl(conn, inbuf, outbuf,
2688 &setup, setup_count,
2689 ¶ms, total_parameter_count,
2690 &data, total_data_count);
2691 END_PROFILE_NESTED(NT_transact_ioctl);
2693 case NT_TRANSACT_SET_SECURITY_DESC:
2694 START_PROFILE_NESTED(NT_transact_set_security_desc);
2695 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
2697 &setup, setup_count,
2698 ¶ms, total_parameter_count,
2699 &data, total_data_count);
2700 END_PROFILE_NESTED(NT_transact_set_security_desc);
2702 case NT_TRANSACT_NOTIFY_CHANGE:
2703 START_PROFILE_NESTED(NT_transact_notify_change);
2704 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
2706 &setup, setup_count,
2707 ¶ms, total_parameter_count,
2708 &data, total_data_count);
2709 END_PROFILE_NESTED(NT_transact_notify_change);
2711 case NT_TRANSACT_RENAME:
2712 START_PROFILE_NESTED(NT_transact_rename);
2713 outsize = call_nt_transact_rename(conn, inbuf, outbuf,
2715 &setup, setup_count,
2716 ¶ms, total_parameter_count,
2717 &data, total_data_count);
2718 END_PROFILE_NESTED(NT_transact_rename);
2721 case NT_TRANSACT_QUERY_SECURITY_DESC:
2722 START_PROFILE_NESTED(NT_transact_query_security_desc);
2723 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
2725 &setup, setup_count,
2726 ¶ms, total_parameter_count,
2727 &data, total_data_count);
2728 END_PROFILE_NESTED(NT_transact_query_security_desc);
2730 #ifdef HAVE_SYS_QUOTAS
2731 case NT_TRANSACT_GET_USER_QUOTA:
2732 START_PROFILE_NESTED(NT_transact_get_user_quota);
2733 outsize = call_nt_transact_get_user_quota(conn, inbuf, outbuf,
2735 &setup, setup_count,
2736 ¶ms, total_parameter_count,
2737 &data, total_data_count);
2738 END_PROFILE_NESTED(NT_transact_get_user_quota);
2740 case NT_TRANSACT_SET_USER_QUOTA:
2741 START_PROFILE_NESTED(NT_transact_set_user_quota);
2742 outsize = call_nt_transact_set_user_quota(conn, inbuf, outbuf,
2744 &setup, setup_count,
2745 ¶ms, total_parameter_count,
2746 &data, total_data_count);
2747 END_PROFILE_NESTED(NT_transact_set_user_quota);
2749 #endif /* HAVE_SYS_QUOTAS */
2751 /* Error in request */
2752 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
2756 END_PROFILE(SMBnttrans);
2757 srv_signing_trans_stop();
2758 return ERROR_DOS(ERRSRV,ERRerror);
2761 /* As we do not know how many data packets will need to be
2762 returned here the various call_nt_transact_xxxx calls
2763 must send their own. Thus a call_nt_transact_xxxx routine only
2764 returns a value other than -1 when it wants to send
2768 srv_signing_trans_stop();
2773 END_PROFILE(SMBnttrans);
2774 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
2775 calls have already sent it. If outsize != -1 then it is
2776 returning an error packet. */
2780 srv_signing_trans_stop();
2784 END_PROFILE(SMBnttrans);
2785 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);