3 Unix SMB/Netbios implementation.
5 SMB NT transaction handling
6 Copyright (C) Jeremy Allison 1994-1998
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern int DEBUGLEVEL;
27 extern int smb_read_error;
28 extern int global_oplock_break;
29 extern BOOL case_sensitive;
30 extern BOOL case_preserve;
31 extern BOOL short_case_preserve;
33 static char *known_nt_pipes[] = {
51 /****************************************************************************
52 Send the required number of replies back.
53 We assume all fields other than the data fields are
54 set correctly for the type of call.
55 HACK ! Always assumes smb_setup field is zero.
56 ****************************************************************************/
58 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, uint32 nt_error, char *params,
59 int paramsize, char *pdata, int datasize)
62 int data_to_send = datasize;
63 int params_to_send = paramsize;
67 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
68 int alignment_offset = 3;
69 int data_alignment_offset = 0;
72 * Initially set the wcnt area to be 18 - this is true for all
76 set_message(outbuf,18,0,True);
80 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
86 * If there genuinely are no parameters or data to send just send
90 if(params_to_send == 0 && data_to_send == 0) {
91 send_smb(smbd_server_fd(),outbuf);
96 * When sending params and data ensure that both are nicely aligned.
97 * Only do this alignment when there is also data to send - else
98 * can cause NT redirector problems.
101 if (((params_to_send % 4) != 0) && (data_to_send != 0))
102 data_alignment_offset = 4 - (params_to_send % 4);
105 * Space is bufsize minus Netbios over TCP header minus SMB header.
106 * The alignment_offset is to align the param bytes on a four byte
107 * boundary (2 bytes for data len, one byte pad).
108 * NT needs this to work correctly.
111 useable_space = bufsize - ((smb_buf(outbuf)+
112 alignment_offset+data_alignment_offset) -
116 * useable_space can never be more than max_send minus the
120 useable_space = MIN(useable_space,
121 max_send - (alignment_offset+data_alignment_offset));
124 while (params_to_send || data_to_send) {
127 * Calculate whether we will totally or partially fill this packet.
130 total_sent_thistime = params_to_send + data_to_send +
131 alignment_offset + data_alignment_offset;
134 * We can never send more than useable_space.
137 total_sent_thistime = MIN(total_sent_thistime, useable_space);
139 set_message(outbuf, 18, total_sent_thistime, True);
142 * Set total params and data to be sent.
145 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
146 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
149 * Calculate how many parameters and data we can fit into
150 * this packet. Parameters get precedence.
153 params_sent_thistime = MIN(params_to_send,useable_space);
154 data_sent_thistime = useable_space - params_sent_thistime;
155 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
157 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
159 if(params_sent_thistime == 0) {
160 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
161 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
164 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
165 * parameter bytes, however the first 4 bytes of outbuf are
166 * the Netbios over TCP header. Thus use smb_base() to subtract
167 * them from the calculation.
170 SIVAL(outbuf,smb_ntr_ParameterOffset,
171 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
173 * Absolute displacement of param bytes sent in this packet.
176 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
180 * Deal with the data portion.
183 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
185 if(data_sent_thistime == 0) {
186 SIVAL(outbuf,smb_ntr_DataOffset,0);
187 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
190 * The offset of the data bytes is the offset of the
191 * parameter bytes plus the number of parameters being sent this time.
194 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
195 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
196 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
200 * Copy the param bytes into the packet.
203 if(params_sent_thistime)
204 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
207 * Copy in the data bytes
210 if(data_sent_thistime)
211 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
212 data_alignment_offset,pd,data_sent_thistime);
214 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
215 params_sent_thistime, data_sent_thistime, useable_space));
216 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
217 params_to_send, data_to_send, paramsize, datasize));
219 /* Send the packet */
220 send_smb(smbd_server_fd(),outbuf);
222 pp += params_sent_thistime;
223 pd += data_sent_thistime;
225 params_to_send -= params_sent_thistime;
226 data_to_send -= data_sent_thistime;
232 if(params_to_send < 0 || data_to_send < 0) {
233 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
234 params_to_send, data_to_send));
242 /****************************************************************************
243 (Hopefully) temporary call to fix bugs in NT5.0beta2. This OS sends unicode
244 strings in NT calls AND DOESN'T SET THE UNICODE BIT !!!!!!!
245 ****************************************************************************/
247 static void get_filename( char *fname, char *inbuf, int data_offset, int data_len, int fname_len)
250 * We need various heuristics here to detect a unicode string... JRA.
253 DEBUG(10,("get_filename: data_offset = %d, data_len = %d, fname_len = %d\n",
254 data_offset, data_len, fname_len ));
256 if(data_len - fname_len > 1) {
258 * NT 5.0 Beta 2 has kindly sent us a UNICODE string
259 * without bothering to set the unicode bit. How kind.
261 * Firstly - ensure that the data offset is aligned
262 * on a 2 byte boundary - add one if not.
264 fname_len = fname_len/2;
267 pstrcpy(fname, dos_unistrn2((uint16 *)(inbuf+data_offset), fname_len));
269 StrnCpy(fname,inbuf+data_offset,fname_len);
270 fname[fname_len] = '\0';
274 /****************************************************************************
275 Fix bugs in Win2000 final release. In trans calls this OS sends unicode
276 strings AND DOESN'T SET THE UNICODE BIT !!!!!!!
277 ****************************************************************************/
279 static void get_filename_transact( char *fname, char *inbuf, int data_offset, int data_len, int fname_len)
282 * We need various heuristics here to detect a unicode string... JRA.
285 DEBUG(10,("get_filename_transact: data_offset = %d, data_len = %d, fname_len = %d\n",
286 data_offset, data_len, fname_len ));
289 * Win2K sends a unicode filename plus one extra alingment byte.
290 * WinNT4.x send an ascii string with multiple garbage bytes on
294 if((data_len - fname_len == 1) || (inbuf[data_offset] == '\0')) {
296 * Ensure that the data offset is aligned
297 * on a 2 byte boundary - add one if not.
299 fname_len = fname_len/2;
302 pstrcpy(fname, dos_unistrn2((uint16 *)(inbuf+data_offset), fname_len));
304 StrnCpy(fname,inbuf+data_offset,fname_len);
305 fname[fname_len] = '\0';
309 /****************************************************************************
311 ****************************************************************************/
313 static BOOL saved_case_sensitive;
314 static BOOL saved_case_preserve;
315 static BOOL saved_short_case_preserve;
317 /****************************************************************************
319 ****************************************************************************/
321 static void set_posix_case_semantics(uint32 file_attributes)
323 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
326 saved_case_sensitive = case_sensitive;
327 saved_case_preserve = case_preserve;
328 saved_short_case_preserve = short_case_preserve;
331 case_sensitive = True;
332 case_preserve = True;
333 short_case_preserve = True;
336 /****************************************************************************
337 Restore case semantics.
338 ****************************************************************************/
340 static void restore_case_semantics(uint32 file_attributes)
342 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
345 case_sensitive = saved_case_sensitive;
346 case_preserve = saved_case_preserve;
347 short_case_preserve = saved_short_case_preserve;
350 /****************************************************************************
351 Utility function to map create disposition.
352 ****************************************************************************/
354 static int map_create_disposition( uint32 create_disposition)
358 switch( create_disposition ) {
360 /* create if not exist, fail if exist */
361 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL);
364 case FILE_OVERWRITE_IF:
365 /* create if not exist, trunc if exist */
366 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
369 /* fail if not exist, open if exists */
370 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN);
373 /* create if not exist, open if exists */
374 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_OPEN);
377 /* fail if not exist, truncate if exists */
378 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
381 DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
382 create_disposition ));
386 DEBUG(10,("map_create_disposition: Mapped create_disposition %lx to %x\n",
387 (unsigned long)create_disposition, ret ));
392 /****************************************************************************
393 Utility function to map share modes.
394 ****************************************************************************/
396 static int map_share_mode( BOOL *pstat_open_only, char *fname,
397 uint32 desired_access, uint32 share_access, uint32 file_attributes)
399 int smb_open_mode = -1;
401 *pstat_open_only = False;
403 switch( desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
405 smb_open_mode = DOS_OPEN_RDONLY;
407 case FILE_WRITE_DATA:
408 case FILE_APPEND_DATA:
409 case FILE_WRITE_DATA|FILE_APPEND_DATA:
410 smb_open_mode = DOS_OPEN_WRONLY;
412 case FILE_READ_DATA|FILE_WRITE_DATA:
413 case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
414 case FILE_READ_DATA|FILE_APPEND_DATA:
415 smb_open_mode = DOS_OPEN_RDWR;
420 * NB. For DELETE_ACCESS we should really check the
421 * directory permissions, as that is what controls
422 * delete, and for WRITE_DAC_ACCESS we should really
423 * check the ownership, as that is what controls the
424 * chmod. Note that this is *NOT* a security hole (this
425 * note is for you, Andrew) as we are not *allowing*
426 * the access at this point, the actual unlink or
427 * chown or chmod call would do this. We are just helping
428 * clients out by telling them if they have a hope
429 * of any of this succeeding. POSIX acls may still
430 * deny the real call. JRA.
433 if (smb_open_mode == -1) {
435 if(desired_access == WRITE_DAC_ACCESS || desired_access == READ_CONTROL_ACCESS)
436 *pstat_open_only = True;
438 if(desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|
439 FILE_EXECUTE|FILE_READ_ATTRIBUTES|
440 FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
441 FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS)) {
442 smb_open_mode = DOS_OPEN_RDONLY;
443 } else if(desired_access == 0) {
446 * JRA - NT seems to sometimes send desired_access as zero. play it safe
447 * and map to a stat open.
450 *pstat_open_only = True;
451 smb_open_mode = DOS_OPEN_RDONLY;
454 DEBUG(0,("map_share_mode: Incorrect value %lx for desired_access to file %s\n",
455 (unsigned long)desired_access, fname));
461 * Set the special bit that means allow share delete.
462 * This is held outside the normal share mode bits at 1<<15.
466 if(share_access & FILE_SHARE_DELETE)
467 smb_open_mode |= ALLOW_SHARE_DELETE;
469 /* Add in the requested share mode. */
470 switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
471 case FILE_SHARE_READ:
472 smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
474 case FILE_SHARE_WRITE:
475 smb_open_mode |= SET_DENY_MODE(DENY_READ);
477 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
478 smb_open_mode |= SET_DENY_MODE(DENY_NONE);
480 case FILE_SHARE_NONE:
481 smb_open_mode |= SET_DENY_MODE(DENY_ALL);
486 * Handle an O_SYNC request.
489 if(file_attributes & FILE_FLAG_WRITE_THROUGH)
490 smb_open_mode |= FILE_SYNC_OPENMODE;
492 DEBUG(10,("map_share_mode: Mapped desired access %lx, share access %lx, file attributes %lx \
493 to open_mode %x\n", (unsigned long)desired_access, (unsigned long)share_access,
494 (unsigned long)file_attributes, smb_open_mode ));
496 return smb_open_mode;
501 * This is a *disgusting* hack.
502 * This is *so* bad that even I'm embarrassed (and I
503 * have no shame). Here's the deal :
504 * Until we get the correct SPOOLSS code into smbd
505 * then when we're running with NT SMB support then
506 * NT makes this call with a level of zero, and then
507 * immediately follows it with an open request to
508 * the \\SRVSVC pipe. If we allow that open to
509 * succeed then NT barfs when it cannot open the
510 * \\SPOOLSS pipe immediately after and continually
511 * whines saying "Printer name is invalid" forever
512 * after. If we cause *JUST THIS NEXT OPEN* of \\SRVSVC
513 * to fail, then NT downgrades to using the downlevel code
514 * and everything works as well as before. I hate
515 * myself for adding this code.... JRA.
517 * The HACK_FAIL_TIME define allows only a 2
518 * second window for this to occur, just in
522 static BOOL fail_next_srvsvc = False;
523 static time_t fail_time;
524 #define HACK_FAIL_TIME 2 /* In seconds. */
526 void fail_next_srvsvc_open(void)
528 /* Check client is WinNT proper; Win2K doesn't like Jeremy's hack - matty */
529 if (get_remote_arch() != RA_WINNT)
532 fail_next_srvsvc = True;
533 fail_time = time(NULL);
534 DEBUG(10,("fail_next_srvsvc_open: setting up timeout close of \\srvsvc pipe for print fix.\n"));
538 * HACK alert.... see above - JRA.
541 BOOL should_fail_next_srvsvc_open(const char *pipename)
544 DEBUG(10,("should_fail_next_srvsvc_open: fail = %d, pipe = %s\n",
545 (int)fail_next_srvsvc, pipename));
547 if(fail_next_srvsvc && (time(NULL) > fail_time + HACK_FAIL_TIME)) {
548 fail_next_srvsvc = False;
549 fail_time = (time_t)0;
550 DEBUG(10,("should_fail_next_srvsvc_open: End of timeout close of \\srvsvc pipe for print fix.\n"));
553 if(fail_next_srvsvc && strequal(pipename, "srvsvc")) {
554 fail_next_srvsvc = False;
555 DEBUG(10,("should_fail_next_srvsvc_open: Deliberately failing open of \\srvsvc pipe for print fix.\n"));
562 /****************************************************************************
563 Reply to an NT create and X call on a pipe.
564 ****************************************************************************/
565 static int nt_open_pipe(char *fname, connection_struct *conn,
566 char *inbuf, char *outbuf, int *ppnum)
568 pipes_struct *p = NULL;
570 uint16 vuid = SVAL(inbuf, smb_uid);
573 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
575 /* See if it is one we want to handle. */
576 for( i = 0; known_nt_pipes[i]; i++ )
577 if( strequal(fname,known_nt_pipes[i]))
580 if ( known_nt_pipes[i] == NULL )
581 return(ERROR(ERRSRV,ERRaccess));
583 /* Strip \\ off the name. */
587 if(should_fail_next_srvsvc_open(fname))
588 return (ERROR(ERRSRV,ERRaccess));
591 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
593 p = open_rpc_pipe_p(fname, conn, vuid);
595 return(ERROR(ERRSRV,ERRnofids));
602 /****************************************************************************
603 Reply to an NT create and X call for pipes.
604 ****************************************************************************/
606 static int do_ntcreate_pipe_open(connection_struct *conn,
607 char *inbuf,char *outbuf,int length,int bufsize)
613 uint32 fname_len = MIN(((uint32)SVAL(inbuf,smb_ntcreate_NameLength)),
614 ((uint32)sizeof(fname)-1));
616 get_filename(fname, inbuf, smb_buf(inbuf)-inbuf,
617 smb_buflen(inbuf),fname_len);
618 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
622 * Deal with pipe return.
625 set_message(outbuf,34,0,True);
627 p = outbuf + smb_vwv2;
631 SIVAL(p,0,FILE_WAS_OPENED);
634 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
637 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
639 SSVAL(p,2, 0x5FF); /* ? */
641 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
643 return chain_reply(inbuf,outbuf,length,bufsize);
646 /****************************************************************************
647 Reply to an NT create and X call.
648 ****************************************************************************/
650 int reply_ntcreate_and_X(connection_struct *conn,
651 char *inbuf,char *outbuf,int length,int bufsize)
655 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
656 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
657 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
658 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
659 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
660 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
661 uint32 fname_len = MIN(((uint32)SVAL(inbuf,smb_ntcreate_NameLength)),
662 ((uint32)sizeof(fname)-1));
663 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
666 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
667 /* Breakout the oplock request bits so we can set the
668 reply bits separately. */
669 int oplock_request = 0;
672 SMB_OFF_T file_len = 0;
673 SMB_STRUCT_STAT sbuf;
675 BOOL bad_path = False;
676 files_struct *fsp=NULL;
678 BOOL stat_open_only = False;
679 START_PROFILE(SMBntcreateX);
681 /* If it's an IPC, use the pipe handler. */
684 if (lp_nt_pipe_support()) {
685 END_PROFILE(SMBntcreateX);
686 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
688 END_PROFILE(SMBntcreateX);
689 return(ERROR(ERRDOS,ERRbadaccess));
695 * We need to construct the open_and_X ofun value from the
696 * NT values, as that's what our code is structured to accept.
699 if((smb_ofun = map_create_disposition( create_disposition )) == -1) {
700 END_PROFILE(SMBntcreateX);
701 return(ERROR(ERRDOS,ERRbadaccess));
708 if(root_dir_fid != 0) {
710 * This filename is relative to a directory fid.
712 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
716 END_PROFILE(SMBntcreateX);
717 return(ERROR(ERRDOS,ERRbadfid));
720 if(!dir_fsp->is_directory) {
722 * Check to see if this is a mac fork of some kind.
725 get_filename(&fname[0], inbuf, smb_buf(inbuf)-inbuf,
726 smb_buflen(inbuf),fname_len);
728 if( strchr(fname, ':')) {
729 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
730 END_PROFILE(SMBntcreateX);
731 return(ERROR(0, NT_STATUS_OBJECT_PATH_NOT_FOUND));
733 END_PROFILE(SMBntcreateX);
734 return(ERROR(ERRDOS,ERRbadfid));
738 * Copy in the base directory name.
741 pstrcpy( fname, dir_fsp->fsp_name );
742 dir_name_len = strlen(fname);
745 * Ensure it ends in a '\'.
748 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
749 pstrcat(fname, "\\");
754 * This next calculation can refuse a correct filename if we're dealing
755 * with the Win2k unicode bug, but that would be rare. JRA.
758 if(fname_len + dir_name_len >= sizeof(pstring)) {
759 END_PROFILE(SMBntcreateX);
760 return(ERROR(ERRSRV,ERRfilespecs));
763 get_filename(&fname[dir_name_len], inbuf, smb_buf(inbuf)-inbuf,
764 smb_buflen(inbuf),fname_len);
768 get_filename(fname, inbuf, smb_buf(inbuf)-inbuf,
769 smb_buflen(inbuf),fname_len);
773 * Now contruct the smb_open_mode value from the filename,
774 * desired access and the share access.
776 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
778 if((smb_open_mode = map_share_mode(&stat_open_only, fname, desired_access,
780 file_attributes)) == -1) {
781 END_PROFILE(SMBntcreateX);
782 return(ERROR(ERRDOS,ERRbadaccess));
785 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
786 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
789 * Ordinary file or directory.
793 * Check if POSIX semantics are wanted.
796 set_posix_case_semantics(file_attributes);
798 unix_convert(fname,conn,0,&bad_path,&sbuf);
800 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
803 * If it's a request for a directory open, deal with it separately.
806 if(create_options & FILE_DIRECTORY_FILE) {
809 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
811 restore_case_semantics(file_attributes);
814 if((errno == ENOENT) && bad_path) {
815 unix_ERR_class = ERRDOS;
816 unix_ERR_code = ERRbadpath;
818 END_PROFILE(SMBntcreateX);
819 return(UNIXERROR(ERRDOS,ERRnoaccess));
823 * Ordinary file case.
826 /* NB. We have a potential bug here. If we
827 * cause an oplock break to ourselves, then we
828 * could end up processing filename related
829 * SMB requests whilst we await the oplock
830 * break response. As we may have changed the
831 * filename case semantics to be POSIX-like,
832 * this could mean a filename request could
833 * fail when it should succeed. This is a rare
834 * condition, but eventually we must arrange
835 * to restore the correct case semantics
836 * before issuing an oplock break request to
837 * our client. JRA. */
839 fsp = open_file_shared(conn,fname,&sbuf,smb_open_mode,
840 smb_ofun,unixmode, oplock_request,&rmode,&smb_action);
843 /* We cheat here. There are two cases we
844 * care about. One is a directory rename,
845 * where the NT client will attempt to
846 * open the source directory for
847 * DELETE access. Note that when the
848 * NT client does this it does *not*
849 * set the directory bit in the
850 * request packet. This is translated
851 * into a read/write open
852 * request. POSIX states that any open
853 * for write request on a directory
854 * will generate an EISDIR error, so
855 * we can catch this here and open a
856 * pseudo handle that is flagged as a
857 * directory. The second is an open
858 * for a permissions read only, which
859 * we handle in the open_file_stat case. JRA.
862 if(errno == EISDIR) {
865 * Fail the open if it was explicitly a non-directory file.
868 if (create_options & FILE_NON_DIRECTORY_FILE) {
869 restore_case_semantics(file_attributes);
870 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
871 END_PROFILE(SMBntcreateX);
872 return(ERROR(0, NT_STATUS_FILE_IS_A_DIRECTORY));
876 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
879 restore_case_semantics(file_attributes);
880 if((errno == ENOENT) && bad_path) {
881 unix_ERR_class = ERRDOS;
882 unix_ERR_code = ERRbadpath;
884 END_PROFILE(SMBntcreateX);
885 return(UNIXERROR(ERRDOS,ERRnoaccess));
888 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
890 } else if (errno == EACCES && stat_open_only) {
893 * We couldn't open normally and all we want
894 * are the permissions. Try and do a stat open.
899 fsp = open_file_stat(conn,fname,&sbuf,smb_open_mode,&smb_action);
902 restore_case_semantics(file_attributes);
903 END_PROFILE(SMBntcreateX);
904 return(UNIXERROR(ERRDOS,ERRnoaccess));
909 if((errno == ENOENT) && bad_path) {
910 unix_ERR_class = ERRDOS;
911 unix_ERR_code = ERRbadpath;
914 restore_case_semantics(file_attributes);
916 END_PROFILE(SMBntcreateX);
917 return(UNIXERROR(ERRDOS,ERRnoaccess));
922 restore_case_semantics(file_attributes);
924 file_len = sbuf.st_size;
925 fmode = dos_mode(conn,fname,&sbuf);
927 fmode = FILE_ATTRIBUTE_NORMAL;
928 if (!fsp->is_directory && (fmode & aDIR)) {
929 close_file(fsp,False);
930 END_PROFILE(SMBntcreateX);
931 return(ERROR(ERRDOS,ERRnoaccess));
935 * If the caller set the extended oplock request bit
936 * and we granted one (by whatever means) - set the
937 * correct bit for extended oplock reply.
940 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
941 smb_action |= EXTENDED_OPLOCK_GRANTED;
943 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
944 smb_action |= EXTENDED_OPLOCK_GRANTED;
946 set_message(outbuf,34,0,True);
948 p = outbuf + smb_vwv2;
951 * Currently as we don't support level II oplocks we just report
952 * exclusive & batch here.
955 if (smb_action & EXTENDED_OPLOCK_GRANTED)
956 SCVAL(p,0, BATCH_OPLOCK_RETURN);
957 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
958 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
960 SCVAL(p,0,NO_OPLOCK_RETURN);
963 SSVAL(p,0,fsp->fnum);
965 SIVAL(p,0,smb_action);
969 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
971 put_long_date(p,sbuf.st_atime); /* access time */
973 put_long_date(p,sbuf.st_mtime); /* write time */
975 put_long_date(p,sbuf.st_mtime); /* change time */
977 SIVAL(p,0,fmode); /* File Attributes. */
979 SOFF_T(p, 0, file_len);
981 SOFF_T(p,0,file_len);
983 SCVAL(p,0,fsp->is_directory ? 1 : 0);
985 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
987 result = chain_reply(inbuf,outbuf,length,bufsize);
988 END_PROFILE(SMBntcreateX);
992 /****************************************************************************
993 Reply to a NT_TRANSACT_CREATE call to open a pipe.
994 ****************************************************************************/
996 static int do_nt_transact_create_pipe( connection_struct *conn,
997 char *inbuf, char *outbuf, int length,
998 int bufsize, char **ppsetup, char **ppparams,
1003 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
1004 char *params = *ppparams;
1010 * Ensure minimum number of parameters sent.
1013 if(total_parameter_count < 54) {
1014 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1015 return(ERROR(ERRDOS,ERRbadaccess));
1018 fname_len = MIN(((uint32)IVAL(params,44)),((uint32)sizeof(fname)-1));
1020 get_filename_transact(&fname[0], params, 53,
1021 total_parameter_count - 53 - fname_len, fname_len);
1023 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
1026 /* Realloc the size of parameters and data we will return */
1027 params = Realloc(*ppparams, 69);
1029 return(ERROR(ERRDOS,ERRnomem));
1033 memset((char *)params,'\0',69);
1036 SCVAL(p,0,NO_OPLOCK_RETURN);
1041 SIVAL(p,0,FILE_WAS_OPENED);
1045 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1048 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1050 SSVAL(p,2, 0x5FF); /* ? */
1052 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1054 /* Send the required number of replies */
1055 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 69, *ppdata, 0);
1060 /****************************************************************************
1061 Internal fn to set security descriptors.
1062 ****************************************************************************/
1064 static BOOL set_sd(files_struct *fsp, char *data, uint32 sd_len, uint security_info_sent, int *pdef_class,uint32 *pdef_code)
1067 SEC_DESC *psd = NULL;
1068 TALLOC_CTX *mem_ctx;
1072 *pdef_class = ERRDOS;
1073 *pdef_code = ERRbadaccess;
1078 * Init the parse struct we will unmarshall from.
1081 if ((mem_ctx = talloc_init()) == NULL) {
1082 DEBUG(0,("set_sd: talloc_init failed.\n"));
1083 *pdef_class = ERRDOS;
1084 *pdef_code = ERRnomem;
1088 prs_init(&pd, 0, 4, mem_ctx, UNMARSHALL);
1091 * Setup the prs_struct to point at the memory we just
1095 prs_give_memory( &pd, data, sd_len, False);
1098 * Finally, unmarshall from the data buffer.
1101 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1102 free_sec_desc(&psd);
1103 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1105 * Return access denied for want of a better error message..
1107 talloc_destroy(mem_ctx);
1108 *pdef_class = ERRDOS;
1109 *pdef_code = ERRnomem;
1113 ret = set_nt_acl( fsp, security_info_sent, psd);
1116 free_sec_desc(&psd);
1117 talloc_destroy(mem_ctx);
1118 *pdef_class = ERRDOS;
1119 *pdef_code = ERRnoaccess;
1123 free_sec_desc(&psd);
1124 talloc_destroy(mem_ctx);
1131 /****************************************************************************
1132 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1133 ****************************************************************************/
1135 static int call_nt_transact_create(connection_struct *conn,
1136 char *inbuf, char *outbuf, int length,
1137 int bufsize, char **ppsetup, char **ppparams,
1141 char *params = *ppparams;
1142 char *data = *ppdata;
1143 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
1144 /* Breakout the oplock request bits so we can set the
1145 reply bits separately. */
1146 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 stat_open_only = False;
1157 uint32 desired_access;
1158 uint32 file_attributes;
1159 uint32 share_access;
1160 uint32 create_disposition;
1161 uint32 create_options;
1164 uint16 root_dir_fid;
1171 DEBUG(5,("call_nt_transact_create\n"));
1174 * If it's an IPC, use the pipe handler.
1178 if (lp_nt_pipe_support())
1179 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1180 bufsize, ppsetup, ppparams, ppdata);
1182 return(ERROR(ERRDOS,ERRbadaccess));
1186 * Ensure minimum number of parameters sent.
1189 if(total_parameter_count < 54) {
1190 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1191 return(ERROR(ERRDOS,ERRbadaccess));
1194 flags = IVAL(params,0);
1195 desired_access = IVAL(params,8);
1196 file_attributes = IVAL(params,20);
1197 share_access = IVAL(params,24);
1198 create_disposition = IVAL(params,28);
1199 create_options = IVAL(params,32);
1200 sd_len = IVAL(params,36);
1201 fname_len = MIN(((uint32)IVAL(params,44)),((uint32)sizeof(fname)-1));
1202 root_dir_fid = (uint16)IVAL(params,4);
1203 smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
1206 * We need to construct the open_and_X ofun value from the
1207 * NT values, as that's what our code is structured to accept.
1210 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
1211 return(ERROR(ERRDOS,ERRbadmem));
1214 * Get the file name.
1217 if(root_dir_fid != 0) {
1219 * This filename is relative to a directory fid.
1222 files_struct *dir_fsp = file_fsp(params,4);
1223 size_t dir_name_len;
1226 return(ERROR(ERRDOS,ERRbadfid));
1228 if(!dir_fsp->is_directory) {
1230 * Check to see if this is a mac fork of some kind.
1233 get_filename_transact(&fname[0], params, 53,
1234 total_parameter_count - 53 - fname_len, fname_len);
1236 if( strchr(fname, ':')) {
1237 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
1238 return(ERROR(0, NT_STATUS_OBJECT_PATH_NOT_FOUND));
1241 return(ERROR(ERRDOS,ERRbadfid));
1245 * Copy in the base directory name.
1248 pstrcpy( fname, dir_fsp->fsp_name );
1249 dir_name_len = strlen(fname);
1252 * Ensure it ends in a '\'.
1255 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1256 pstrcat(fname, "\\");
1261 * This next calculation can refuse a correct filename if we're dealing
1262 * with the Win2k unicode bug, but that would be rare. JRA.
1265 if(fname_len + dir_name_len >= sizeof(pstring))
1266 return(ERROR(ERRSRV,ERRfilespecs));
1268 get_filename_transact(&fname[dir_name_len], params, 53,
1269 total_parameter_count - 53 - fname_len, fname_len);
1272 get_filename_transact(&fname[0], params, 53,
1273 total_parameter_count - 53 - fname_len, fname_len);
1277 * Now contruct the smb_open_mode value from the desired access
1278 * and the share access.
1281 if((smb_open_mode = map_share_mode( &stat_open_only, fname, desired_access,
1282 share_access, file_attributes)) == -1)
1283 return(ERROR(ERRDOS,ERRbadaccess));
1285 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1286 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1289 * Check if POSIX semantics are wanted.
1292 set_posix_case_semantics(file_attributes);
1294 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1296 unix_convert(fname,conn,0,&bad_path,&sbuf);
1298 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
1301 * If it's a request for a directory open, deal with it separately.
1304 if(create_options & FILE_DIRECTORY_FILE) {
1309 * We will get a create directory here if the Win32
1310 * app specified a security descriptor in the
1311 * CreateDirectory() call.
1314 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
1317 restore_case_semantics(file_attributes);
1318 if((errno == ENOENT) && bad_path) {
1319 unix_ERR_class = ERRDOS;
1320 unix_ERR_code = ERRbadpath;
1322 return(UNIXERROR(ERRDOS,ERRnoaccess));
1328 * Ordinary file case.
1331 fsp = open_file_shared(conn,fname,&sbuf,smb_open_mode,smb_ofun,unixmode,
1332 oplock_request,&rmode,&smb_action);
1336 if(errno == EISDIR) {
1339 * Fail the open if it was explicitly a non-directory file.
1342 if (create_options & FILE_NON_DIRECTORY_FILE) {
1343 restore_case_semantics(file_attributes);
1344 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
1345 return(ERROR(0, NT_STATUS_FILE_IS_A_DIRECTORY));
1349 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
1352 restore_case_semantics(file_attributes);
1353 if((errno == ENOENT) && bad_path) {
1354 unix_ERR_class = ERRDOS;
1355 unix_ERR_code = ERRbadpath;
1357 return(UNIXERROR(ERRDOS,ERRnoaccess));
1360 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
1362 } else if (errno == EACCES && stat_open_only) {
1366 * We couldn't open normally and all we want
1367 * are the permissions. Try and do a stat open.
1372 fsp = open_file_stat(conn,fname,&sbuf,smb_open_mode,&smb_action);
1375 restore_case_semantics(file_attributes);
1376 return(UNIXERROR(ERRDOS,ERRnoaccess));
1380 if((errno == ENOENT) && bad_path) {
1381 unix_ERR_class = ERRDOS;
1382 unix_ERR_code = ERRbadpath;
1385 restore_case_semantics(file_attributes);
1387 return(UNIXERROR(ERRDOS,ERRnoaccess));
1391 file_len = sbuf.st_size;
1392 fmode = dos_mode(conn,fname,&sbuf);
1394 fmode = FILE_ATTRIBUTE_NORMAL;
1397 close_file(fsp,False);
1398 restore_case_semantics(file_attributes);
1399 return(ERROR(ERRDOS,ERRnoaccess));
1403 * If the caller set the extended oplock request bit
1404 * and we granted one (by whatever means) - set the
1405 * correct bit for extended oplock reply.
1408 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1409 smb_action |= EXTENDED_OPLOCK_GRANTED;
1411 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1412 smb_action |= EXTENDED_OPLOCK_GRANTED;
1416 * Now try and apply the desired SD.
1419 if (!set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION, &error_class, &error_code)) {
1420 close_file(fsp,False);
1421 restore_case_semantics(file_attributes);
1422 return(ERROR(error_class, error_code));
1425 restore_case_semantics(file_attributes);
1427 /* Realloc the size of parameters and data we will return */
1428 params = Realloc(*ppparams, 69);
1430 return(ERROR(ERRDOS,ERRnomem));
1434 memset((char *)params,'\0',69);
1437 if (smb_action & EXTENDED_OPLOCK_GRANTED)
1438 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1439 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1440 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1442 SCVAL(p,0,NO_OPLOCK_RETURN);
1445 SSVAL(p,0,fsp->fnum);
1447 SIVAL(p,0,smb_action);
1451 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
1453 put_long_date(p,sbuf.st_atime); /* access time */
1455 put_long_date(p,sbuf.st_mtime); /* write time */
1457 put_long_date(p,sbuf.st_mtime); /* change time */
1459 SIVAL(p,0,fmode); /* File Attributes. */
1461 SOFF_T(p,0,file_len);
1463 SOFF_T(p,0,file_len);
1465 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1467 /* Send the required number of replies */
1468 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 69, *ppdata, 0);
1473 /****************************************************************************
1474 Reply to a NT CANCEL request.
1475 ****************************************************************************/
1476 int reply_ntcancel(connection_struct *conn,
1477 char *inbuf,char *outbuf,int length,int bufsize)
1480 * Go through and cancel any pending change notifies.
1483 int mid = SVAL(inbuf,smb_mid);
1484 START_PROFILE(SMBntcancel);
1485 remove_pending_change_notify_requests_by_mid(mid);
1486 remove_pending_lock_requests_by_mid(mid);
1488 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1490 END_PROFILE(SMBntcancel);
1494 /****************************************************************************
1495 Reply to an unsolicited SMBNTtranss - just ignore it!
1496 ****************************************************************************/
1497 int reply_nttranss(connection_struct *conn,
1498 char *inbuf,char *outbuf,int length,int bufsize)
1500 START_PROFILE(SMBnttranss);
1501 DEBUG(4,("Ignoring nttranss of length %d\n",length));
1502 END_PROFILE(SMBnttranss);
1506 /****************************************************************************
1507 Reply to a notify change - queue the request and
1508 don't allow a directory to be opened.
1509 ****************************************************************************/
1510 static int call_nt_transact_notify_change(connection_struct *conn,
1511 char *inbuf, char *outbuf, int length,
1514 char **ppparams, char **ppdata)
1516 char *setup = *ppsetup;
1520 fsp = file_fsp(setup,4);
1521 flags = IVAL(setup, 0);
1523 DEBUG(3,("call_nt_transact_notify_change\n"));
1526 return(ERROR(ERRDOS,ERRbadfid));
1528 if((!fsp->is_directory) || (conn != fsp->conn))
1529 return(ERROR(ERRDOS,ERRbadfid));
1531 if (!change_notify_set(inbuf, fsp, conn, flags)) {
1532 return(UNIXERROR(ERRDOS,ERRbadfid));
1535 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1536 name = %s\n", fsp->fsp_name ));
1541 /****************************************************************************
1542 Reply to an NT transact rename command.
1543 ****************************************************************************/
1545 static int call_nt_transact_rename(connection_struct *conn,
1546 char *inbuf, char *outbuf, int length,
1548 char **ppsetup, char **ppparams, char **ppdata)
1550 char *params = *ppparams;
1552 files_struct *fsp = file_fsp(params, 0);
1553 BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1554 uint32 fname_len = MIN((((uint32)IVAL(inbuf,smb_nt_TotalParameterCount)-4)),
1555 ((uint32)sizeof(new_name)-1));
1558 CHECK_FSP(fsp, conn);
1559 StrnCpy(new_name,params+4,fname_len);
1560 new_name[fname_len] = '\0';
1562 outsize = rename_internals(conn, inbuf, outbuf, fsp->fsp_name,
1563 new_name, replace_if_exists);
1566 * Rename was successful.
1568 send_nt_replies(inbuf, outbuf, bufsize, 0, NULL, 0, NULL, 0);
1570 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1571 fsp->fsp_name, new_name));
1576 * Win2k needs a changenotify request response before it will
1577 * update after a rename..
1580 process_pending_change_notify_queue((time_t)0);
1587 /****************************************************************************
1588 Reply to query a security descriptor - currently this is not implemented (it
1589 is planned to be though). Right now it just returns the same thing NT would
1590 when queried on a FAT filesystem. JRA.
1591 ****************************************************************************/
1593 static int call_nt_transact_query_security_desc(connection_struct *conn,
1594 char *inbuf, char *outbuf,
1595 int length, int bufsize,
1596 char **ppsetup, char **ppparams, char **ppdata)
1598 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1599 char *params = *ppparams;
1600 char *data = *ppdata;
1602 SEC_DESC *psd = NULL;
1604 TALLOC_CTX *mem_ctx;
1606 files_struct *fsp = file_fsp(params,0);
1609 return(ERROR(ERRDOS,ERRbadfid));
1611 DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1613 params = Realloc(*ppparams, 4);
1615 return(ERROR(ERRDOS,ERRnomem));
1620 * Get the permissions to return.
1623 if((sd_size = get_nt_acl(fsp, &psd)) == 0)
1624 return(UNIXERROR(ERRDOS,ERRnoaccess));
1626 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %d.\n",(int)sd_size));
1628 SIVAL(params,0,(uint32)sd_size);
1630 if(max_data_count < sd_size) {
1632 free_sec_desc(&psd);
1634 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1635 params, 4, *ppdata, 0);
1640 * Allocate the data we will point this at.
1643 data = Realloc(*ppdata, sd_size);
1645 free_sec_desc(&psd);
1646 return(ERROR(ERRDOS,ERRnomem));
1651 memset(data, '\0', sd_size);
1654 * Init the parse struct we will marshall into.
1657 if ((mem_ctx = talloc_init()) == NULL) {
1658 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1659 free_sec_desc(&psd);
1660 return(ERROR(ERRDOS,ERRnomem));
1663 prs_init(&pd, 0, 4, mem_ctx, MARSHALL);
1666 * Setup the prs_struct to point at the memory we just
1670 prs_give_memory( &pd, data, (uint32)sd_size, False);
1673 * Finally, linearize into the outgoing buffer.
1676 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1677 free_sec_desc(&psd);
1678 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1679 security descriptor.\n"));
1681 * Return access denied for want of a better error message..
1683 talloc_destroy(mem_ctx);
1684 return(UNIXERROR(ERRDOS,ERRnoaccess));
1688 * Now we can delete the security descriptor.
1691 free_sec_desc(&psd);
1692 talloc_destroy(mem_ctx);
1694 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 4, data, (int)sd_size);
1698 /****************************************************************************
1699 Reply to set a security descriptor. Map to UNIX perms.
1700 ****************************************************************************/
1702 static int call_nt_transact_set_security_desc(connection_struct *conn,
1703 char *inbuf, char *outbuf, int length,
1704 int bufsize, char **ppsetup,
1705 char **ppparams, char **ppdata)
1707 uint32 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1708 char *params= *ppparams;
1709 char *data = *ppdata;
1710 uint32 total_data_count = (uint32)IVAL(inbuf, smb_nts_TotalDataCount);
1711 files_struct *fsp = NULL;
1712 uint32 security_info_sent = 0;
1716 if(!lp_nt_acl_support())
1717 return(UNIXERROR(ERRDOS,ERRnoaccess));
1719 if(total_parameter_count < 8)
1720 return(ERROR(ERRDOS,ERRbadfunc));
1722 if((fsp = file_fsp(params,0)) == NULL)
1723 return(ERROR(ERRDOS,ERRbadfid));
1725 security_info_sent = IVAL(params,4);
1727 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1728 (unsigned int)security_info_sent ));
1730 if (!set_sd( fsp, data, total_data_count, security_info_sent, &error_class, &error_code))
1731 return (ERROR(error_class, error_code));
1733 send_nt_replies(inbuf, outbuf, bufsize, 0, NULL, 0, NULL, 0);
1737 /****************************************************************************
1738 Reply to IOCTL - not implemented - no plans.
1739 ****************************************************************************/
1740 static int call_nt_transact_ioctl(connection_struct *conn,
1741 char *inbuf, char *outbuf, int length,
1743 char **ppsetup, char **ppparams, char **ppdata)
1745 static BOOL logged_message = False;
1747 if(!logged_message) {
1748 DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
1749 logged_message = True; /* Only print this once... */
1751 return(ERROR(ERRSRV,ERRnosupport));
1754 /****************************************************************************
1755 Reply to a SMBNTtrans.
1756 ****************************************************************************/
1757 int reply_nttrans(connection_struct *conn,
1758 char *inbuf,char *outbuf,int length,int bufsize)
1761 #if 0 /* Not used. */
1762 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1763 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1764 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1765 #endif /* Not used. */
1766 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1767 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1768 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1769 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1770 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1771 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1772 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1773 uint16 function_code = SVAL( inbuf, smb_nt_Function);
1774 char *params = NULL, *data = NULL, *setup = NULL;
1775 uint32 num_params_sofar, num_data_sofar;
1776 START_PROFILE(SMBnttrans);
1778 if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
1780 * Queue this open message as we are the process of an oplock break.
1783 DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
1784 due to being in oplock break state.\n" ));
1786 push_oplock_pending_smb_message( inbuf, length);
1787 END_PROFILE(SMBnttrans);
1791 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
1792 END_PROFILE(SMBnttrans);
1793 return (ERROR(ERRSRV,ERRaccess));
1796 outsize = set_message(outbuf,0,0,True);
1799 * All nttrans messages we handle have smb_wct == 19 + setup_count.
1800 * Ensure this is so as a sanity check.
1803 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1804 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1805 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1806 END_PROFILE(SMBnttrans);
1807 return(ERROR(ERRSRV,ERRerror));
1810 /* Allocate the space for the setup, the maximum needed parameters and data */
1813 setup = (char *)malloc(setup_count);
1814 if (total_parameter_count > 0)
1815 params = (char *)malloc(total_parameter_count);
1816 if (total_data_count > 0)
1817 data = (char *)malloc(total_data_count);
1819 if ((total_parameter_count && !params) || (total_data_count && !data) ||
1820 (setup_count && !setup)) {
1824 DEBUG(0,("reply_nttrans : Out of memory\n"));
1825 END_PROFILE(SMBnttrans);
1826 return(ERROR(ERRDOS,ERRnomem));
1829 /* Copy the param and data bytes sent with this request into
1830 the params buffer */
1831 num_params_sofar = parameter_count;
1832 num_data_sofar = data_count;
1834 if (parameter_count > total_parameter_count || data_count > total_data_count)
1835 exit_server("reply_nttrans: invalid sizes in packet.\n");
1838 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1839 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1840 dump_data(10, setup, setup_count);
1843 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1844 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1845 dump_data(10, params, parameter_count);
1848 memcpy( data, smb_base(inbuf) + data_offset, data_count);
1849 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1850 dump_data(10, data, data_count);
1853 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1854 /* We need to send an interim response then receive the rest
1855 of the parameter/data bytes */
1856 outsize = set_message(outbuf,0,0,True);
1857 send_smb(smbd_server_fd(),outbuf);
1859 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1862 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
1864 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1865 outsize = set_message(outbuf,0,0,True);
1867 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1869 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1870 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1878 END_PROFILE(SMBnttrans);
1879 return(ERROR(ERRSRV,ERRerror));
1882 /* Revise total_params and total_data in case they have changed downwards */
1883 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1884 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1885 num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1886 num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1887 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1888 exit_server("reply_nttrans2: data overflow in secondary nttrans packet\n");
1890 memcpy( ¶ms[ IVAL(inbuf, smb_nts_ParameterDisplacement)],
1891 smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1892 memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1893 smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1897 if (Protocol >= PROTOCOL_NT1) {
1898 uint16 flg2 = SVAL(outbuf,smb_flg2);
1899 SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
1902 /* Now we must call the relevant NT_TRANS function */
1903 switch(function_code) {
1904 case NT_TRANSACT_CREATE:
1905 START_PROFILE_NESTED(NT_transact_create);
1906 outsize = call_nt_transact_create(conn, inbuf, outbuf, length, bufsize,
1907 &setup, ¶ms, &data);
1908 END_PROFILE_NESTED(NT_transact_create);
1910 case NT_TRANSACT_IOCTL:
1911 START_PROFILE_NESTED(NT_transact_ioctl);
1912 outsize = call_nt_transact_ioctl(conn,
1913 inbuf, outbuf, length, bufsize,
1914 &setup, ¶ms, &data);
1915 END_PROFILE_NESTED(NT_transact_ioctl);
1917 case NT_TRANSACT_SET_SECURITY_DESC:
1918 START_PROFILE_NESTED(NT_transact_set_security_desc);
1919 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
1921 &setup, ¶ms, &data);
1922 END_PROFILE_NESTED(NT_transact_set_security_desc);
1924 case NT_TRANSACT_NOTIFY_CHANGE:
1925 START_PROFILE_NESTED(NT_transact_notify_change);
1926 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
1928 &setup, ¶ms, &data);
1929 END_PROFILE_NESTED(NT_transact_notify_change);
1931 case NT_TRANSACT_RENAME:
1932 START_PROFILE_NESTED(NT_transact_rename);
1933 outsize = call_nt_transact_rename(conn, inbuf, outbuf, length,
1935 &setup, ¶ms, &data);
1936 END_PROFILE_NESTED(NT_transact_rename);
1939 case NT_TRANSACT_QUERY_SECURITY_DESC:
1940 START_PROFILE_NESTED(NT_transact_query_security_desc);
1941 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
1943 &setup, ¶ms, &data);
1944 END_PROFILE_NESTED(NT_transact_query_security_desc);
1947 /* Error in request */
1948 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1955 END_PROFILE(SMBnttrans);
1956 return (ERROR(ERRSRV,ERRerror));
1959 /* As we do not know how many data packets will need to be
1960 returned here the various call_nt_transact_xxxx calls
1961 must send their own. Thus a call_nt_transact_xxxx routine only
1962 returns a value other than -1 when it wants to send
1972 END_PROFILE(SMBnttrans);
1973 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
1974 calls have already sent it. If outsize != -1 then it is
1975 returning an error packet. */