2 Unix SMB/Netbios implementation.
4 SMB NT transaction handling
5 Copyright (C) Jeremy Allison 1994-1998
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.
24 extern int DEBUGLEVEL;
26 extern int smb_read_error;
27 extern int global_oplock_break;
28 extern BOOL case_sensitive;
29 extern BOOL case_preserve;
30 extern BOOL short_case_preserve;
32 static char *known_nt_pipes[] = {
50 /****************************************************************************
51 Send the required number of replies back.
52 We assume all fields other than the data fields are
53 set correctly for the type of call.
54 HACK ! Always assumes smb_setup field is zero.
55 ****************************************************************************/
57 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, uint32 nt_error, char *params,
58 int paramsize, char *pdata, int datasize)
61 int data_to_send = datasize;
62 int params_to_send = paramsize;
66 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
67 int alignment_offset = 3;
68 int data_alignment_offset = 0;
71 * Initially set the wcnt area to be 18 - this is true for all
75 set_message(outbuf,18,0,True);
79 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
85 * If there genuinely are no parameters or data to send just send
89 if(params_to_send == 0 && data_to_send == 0) {
90 send_smb(smbd_server_fd(),outbuf);
95 * When sending params and data ensure that both are nicely aligned.
96 * Only do this alignment when there is also data to send - else
97 * can cause NT redirector problems.
100 if (((params_to_send % 4) != 0) && (data_to_send != 0))
101 data_alignment_offset = 4 - (params_to_send % 4);
104 * Space is bufsize minus Netbios over TCP header minus SMB header.
105 * The alignment_offset is to align the param bytes on a four byte
106 * boundary (2 bytes for data len, one byte pad).
107 * NT needs this to work correctly.
110 useable_space = bufsize - ((smb_buf(outbuf)+
111 alignment_offset+data_alignment_offset) -
115 * useable_space can never be more than max_send minus the
119 useable_space = MIN(useable_space,
120 max_send - (alignment_offset+data_alignment_offset));
123 while (params_to_send || data_to_send) {
126 * Calculate whether we will totally or partially fill this packet.
129 total_sent_thistime = params_to_send + data_to_send +
130 alignment_offset + data_alignment_offset;
133 * We can never send more than useable_space.
136 total_sent_thistime = MIN(total_sent_thistime, useable_space);
138 set_message(outbuf, 18, total_sent_thistime, True);
141 * Set total params and data to be sent.
144 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
145 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
148 * Calculate how many parameters and data we can fit into
149 * this packet. Parameters get precedence.
152 params_sent_thistime = MIN(params_to_send,useable_space);
153 data_sent_thistime = useable_space - params_sent_thistime;
154 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
156 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
158 if(params_sent_thistime == 0) {
159 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
160 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
163 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
164 * parameter bytes, however the first 4 bytes of outbuf are
165 * the Netbios over TCP header. Thus use smb_base() to subtract
166 * them from the calculation.
169 SIVAL(outbuf,smb_ntr_ParameterOffset,
170 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
172 * Absolute displacement of param bytes sent in this packet.
175 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
179 * Deal with the data portion.
182 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
184 if(data_sent_thistime == 0) {
185 SIVAL(outbuf,smb_ntr_DataOffset,0);
186 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
189 * The offset of the data bytes is the offset of the
190 * parameter bytes plus the number of parameters being sent this time.
193 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
194 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
195 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
199 * Copy the param bytes into the packet.
202 if(params_sent_thistime)
203 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
206 * Copy in the data bytes
209 if(data_sent_thistime)
210 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
211 data_alignment_offset,pd,data_sent_thistime);
213 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
214 params_sent_thistime, data_sent_thistime, useable_space));
215 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
216 params_to_send, data_to_send, paramsize, datasize));
218 /* Send the packet */
219 send_smb(smbd_server_fd(),outbuf);
221 pp += params_sent_thistime;
222 pd += data_sent_thistime;
224 params_to_send -= params_sent_thistime;
225 data_to_send -= data_sent_thistime;
231 if(params_to_send < 0 || data_to_send < 0) {
232 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
233 params_to_send, data_to_send));
241 /****************************************************************************
242 (Hopefully) temporary call to fix bugs in NT5.0beta2. This OS sends unicode
243 strings in NT calls AND DOESN'T SET THE UNICODE BIT !!!!!!!
244 ****************************************************************************/
246 static void get_filename( char *fname, char *inbuf, int data_offset, int data_len, int fname_len)
249 * We need various heuristics here to detect a unicode string... JRA.
252 DEBUG(10,("get_filename: data_offset = %d, data_len = %d, fname_len = %d\n",
253 data_offset, data_len, fname_len ));
255 if(data_len - fname_len > 1) {
257 * NT 5.0 Beta 2 has kindly sent us a UNICODE string
258 * without bothering to set the unicode bit. How kind.
260 * Firstly - ensure that the data offset is aligned
261 * on a 2 byte boundary - add one if not.
263 fname_len = fname_len/2;
266 pstrcpy(fname, dos_unistrn2((uint16 *)(inbuf+data_offset), fname_len));
268 StrnCpy(fname,inbuf+data_offset,fname_len);
269 fname[fname_len] = '\0';
273 /****************************************************************************
274 Fix bugs in Win2000 final release. In trans calls this OS sends unicode
275 strings AND DOESN'T SET THE UNICODE BIT !!!!!!!
276 ****************************************************************************/
278 static void get_filename_transact( char *fname, char *inbuf, int data_offset, int data_len, int fname_len)
281 * We need various heuristics here to detect a unicode string... JRA.
284 DEBUG(10,("get_filename_transact: data_offset = %d, data_len = %d, fname_len = %d\n",
285 data_offset, data_len, fname_len ));
288 * Win2K sends a unicode filename plus one extra alingment byte.
289 * WinNT4.x send an ascii string with multiple garbage bytes on
293 if((data_len == 1) || (inbuf[data_offset] == '\0')) {
295 * Ensure that the data offset is aligned
296 * on a 2 byte boundary - add one if not.
298 fname_len = fname_len/2;
301 pstrcpy(fname, dos_unistrn2((uint16 *)(inbuf+data_offset), fname_len));
303 StrnCpy(fname,inbuf+data_offset,fname_len);
304 fname[fname_len] = '\0';
308 /****************************************************************************
310 ****************************************************************************/
312 static BOOL saved_case_sensitive;
313 static BOOL saved_case_preserve;
314 static BOOL saved_short_case_preserve;
316 /****************************************************************************
318 ****************************************************************************/
320 static void set_posix_case_semantics(uint32 file_attributes)
322 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
325 saved_case_sensitive = case_sensitive;
326 saved_case_preserve = case_preserve;
327 saved_short_case_preserve = short_case_preserve;
330 case_sensitive = True;
331 case_preserve = True;
332 short_case_preserve = True;
335 /****************************************************************************
336 Restore case semantics.
337 ****************************************************************************/
339 static void restore_case_semantics(uint32 file_attributes)
341 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
344 case_sensitive = saved_case_sensitive;
345 case_preserve = saved_case_preserve;
346 short_case_preserve = saved_short_case_preserve;
349 /****************************************************************************
350 Utility function to map create disposition.
351 ****************************************************************************/
353 static int map_create_disposition( uint32 create_disposition)
357 switch( create_disposition ) {
359 /* create if not exist, fail if exist */
360 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL);
363 case FILE_OVERWRITE_IF:
364 /* create if not exist, trunc if exist */
365 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
368 /* fail if not exist, open if exists */
369 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN);
372 /* create if not exist, open if exists */
373 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_OPEN);
376 /* fail if not exist, truncate if exists */
377 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
380 DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
381 create_disposition ));
385 DEBUG(10,("map_create_disposition: Mapped create_disposition %lx to %x\n",
386 (unsigned long)create_disposition, ret ));
391 /****************************************************************************
392 Utility function to map share modes.
393 ****************************************************************************/
395 static int map_share_mode( BOOL *pstat_open_only, char *fname,
396 uint32 desired_access, uint32 share_access, uint32 file_attributes)
398 int smb_open_mode = -1;
400 *pstat_open_only = False;
402 switch( desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
404 smb_open_mode = DOS_OPEN_RDONLY;
406 case FILE_WRITE_DATA:
407 case FILE_APPEND_DATA:
408 case FILE_WRITE_DATA|FILE_APPEND_DATA:
409 smb_open_mode = DOS_OPEN_WRONLY;
411 case FILE_READ_DATA|FILE_WRITE_DATA:
412 case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
413 case FILE_READ_DATA|FILE_APPEND_DATA:
414 smb_open_mode = DOS_OPEN_RDWR;
419 * NB. For DELETE_ACCESS we should really check the
420 * directory permissions, as that is what controls
421 * delete, and for WRITE_DAC_ACCESS we should really
422 * check the ownership, as that is what controls the
423 * chmod. Note that this is *NOT* a security hole (this
424 * note is for you, Andrew) as we are not *allowing*
425 * the access at this point, the actual unlink or
426 * chown or chmod call would do this. We are just helping
427 * clients out by telling them if they have a hope
428 * of any of this succeeding. POSIX acls may still
429 * deny the real call. JRA.
432 if (smb_open_mode == -1) {
434 if(desired_access == WRITE_DAC_ACCESS || desired_access == READ_CONTROL_ACCESS)
435 *pstat_open_only = True;
437 if(desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|
438 FILE_EXECUTE|FILE_READ_ATTRIBUTES|
439 FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
440 FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS)) {
441 smb_open_mode = DOS_OPEN_RDONLY;
442 } else if(desired_access == 0) {
445 * JRA - NT seems to sometimes send desired_access as zero. play it safe
446 * and map to a stat open.
449 *pstat_open_only = True;
450 smb_open_mode = DOS_OPEN_RDONLY;
453 DEBUG(0,("map_share_mode: Incorrect value %lx for desired_access to file %s\n",
454 (unsigned long)desired_access, fname));
460 * Set the special bit that means allow share delete.
461 * This is held outside the normal share mode bits at 1<<15.
465 if(share_access & FILE_SHARE_DELETE)
466 smb_open_mode |= ALLOW_SHARE_DELETE;
468 /* Add in the requested share mode. */
469 switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
470 case FILE_SHARE_READ:
471 smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
473 case FILE_SHARE_WRITE:
474 smb_open_mode |= SET_DENY_MODE(DENY_READ);
476 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
477 smb_open_mode |= SET_DENY_MODE(DENY_NONE);
479 case FILE_SHARE_NONE:
480 smb_open_mode |= SET_DENY_MODE(DENY_ALL);
485 * Handle an O_SYNC request.
488 if(file_attributes & FILE_FLAG_WRITE_THROUGH)
489 smb_open_mode |= FILE_SYNC_OPENMODE;
491 DEBUG(10,("map_share_mode: Mapped desired access %lx, share access %lx, file attributes %lx \
492 to open_mode %x\n", (unsigned long)desired_access, (unsigned long)share_access,
493 (unsigned long)file_attributes, smb_open_mode ));
495 return smb_open_mode;
500 * This is a *disgusting* hack.
501 * This is *so* bad that even I'm embarrassed (and I
502 * have no shame). Here's the deal :
503 * Until we get the correct SPOOLSS code into smbd
504 * then when we're running with NT SMB support then
505 * NT makes this call with a level of zero, and then
506 * immediately follows it with an open request to
507 * the \\SRVSVC pipe. If we allow that open to
508 * succeed then NT barfs when it cannot open the
509 * \\SPOOLSS pipe immediately after and continually
510 * whines saying "Printer name is invalid" forever
511 * after. If we cause *JUST THIS NEXT OPEN* of \\SRVSVC
512 * to fail, then NT downgrades to using the downlevel code
513 * and everything works as well as before. I hate
514 * myself for adding this code.... JRA.
516 * The HACK_FAIL_TIME define allows only a 2
517 * second window for this to occur, just in
521 static BOOL fail_next_srvsvc = False;
522 static time_t fail_time;
523 #define HACK_FAIL_TIME 2 /* In seconds. */
525 void fail_next_srvsvc_open(void)
527 /* Check client is WinNT proper; Win2K doesn't like Jeremy's hack - matty */
528 if (get_remote_arch() != RA_WINNT)
531 fail_next_srvsvc = True;
532 fail_time = time(NULL);
533 DEBUG(10,("fail_next_srvsvc_open: setting up timeout close of \\srvsvc pipe for print fix.\n"));
537 * HACK alert.... see above - JRA.
540 BOOL should_fail_next_srvsvc_open(const char *pipename)
543 DEBUG(10,("should_fail_next_srvsvc_open: fail = %d, pipe = %s\n",
544 (int)fail_next_srvsvc, pipename));
546 if(fail_next_srvsvc && (time(NULL) > fail_time + HACK_FAIL_TIME)) {
547 fail_next_srvsvc = False;
548 fail_time = (time_t)0;
549 DEBUG(10,("should_fail_next_srvsvc_open: End of timeout close of \\srvsvc pipe for print fix.\n"));
552 if(fail_next_srvsvc && strequal(pipename, "srvsvc")) {
553 fail_next_srvsvc = False;
554 DEBUG(10,("should_fail_next_srvsvc_open: Deliberately failing open of \\srvsvc pipe for print fix.\n"));
561 /****************************************************************************
562 Reply to an NT create and X call on a pipe.
563 ****************************************************************************/
564 static int nt_open_pipe(char *fname, connection_struct *conn,
565 char *inbuf, char *outbuf, int *ppnum)
567 pipes_struct *p = NULL;
569 uint16 vuid = SVAL(inbuf, smb_uid);
572 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
574 /* See if it is one we want to handle. */
575 for( i = 0; known_nt_pipes[i]; i++ )
576 if( strequal(fname,known_nt_pipes[i]))
579 if ( known_nt_pipes[i] == NULL )
580 return(ERROR(ERRSRV,ERRaccess));
582 /* Strip \\ off the name. */
586 if(should_fail_next_srvsvc_open(fname))
587 return (ERROR(ERRSRV,ERRaccess));
590 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
592 p = open_rpc_pipe_p(fname, conn, vuid);
594 return(ERROR(ERRSRV,ERRnofids));
601 /****************************************************************************
602 Reply to an NT create and X call for pipes.
603 ****************************************************************************/
605 static int do_ntcreate_pipe_open(connection_struct *conn,
606 char *inbuf,char *outbuf,int length,int bufsize)
612 uint32 fname_len = MIN(((uint32)SVAL(inbuf,smb_ntcreate_NameLength)),
613 ((uint32)sizeof(fname)-1));
615 get_filename(fname, inbuf, smb_buf(inbuf)-inbuf,
616 smb_buflen(inbuf),fname_len);
617 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
621 * Deal with pipe return.
624 set_message(outbuf,34,0,True);
626 p = outbuf + smb_vwv2;
630 SIVAL(p,0,FILE_WAS_OPENED);
633 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
636 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
638 SSVAL(p,2, 0x5FF); /* ? */
640 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
642 return chain_reply(inbuf,outbuf,length,bufsize);
645 /****************************************************************************
646 Reply to an NT create and X call.
647 ****************************************************************************/
649 int reply_ntcreate_and_X(connection_struct *conn,
650 char *inbuf,char *outbuf,int length,int bufsize)
654 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
655 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
656 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
657 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
658 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
659 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
660 uint32 fname_len = MIN(((uint32)SVAL(inbuf,smb_ntcreate_NameLength)),
661 ((uint32)sizeof(fname)-1));
662 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
665 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
666 /* Breakout the oplock request bits so we can set the
667 reply bits separately. */
668 int oplock_request = 0;
671 SMB_OFF_T file_len = 0;
672 SMB_STRUCT_STAT sbuf;
674 BOOL bad_path = False;
675 files_struct *fsp=NULL;
677 BOOL stat_open_only = False;
678 START_PROFILE(SMBntcreateX);
680 /* If it's an IPC, use the pipe handler. */
683 if (lp_nt_pipe_support()) {
684 END_PROFILE(SMBntcreateX);
685 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
687 END_PROFILE(SMBntcreateX);
688 return(ERROR(ERRDOS,ERRbadaccess));
694 * We need to construct the open_and_X ofun value from the
695 * NT values, as that's what our code is structured to accept.
698 if((smb_ofun = map_create_disposition( create_disposition )) == -1) {
699 END_PROFILE(SMBntcreateX);
700 return(ERROR(ERRDOS,ERRbadaccess));
707 if(root_dir_fid != 0) {
709 * This filename is relative to a directory fid.
711 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
715 END_PROFILE(SMBntcreateX);
716 return(ERROR(ERRDOS,ERRbadfid));
719 if(!dir_fsp->is_directory) {
721 * Check to see if this is a mac fork of some kind.
724 get_filename(&fname[0], inbuf, smb_buf(inbuf)-inbuf,
725 smb_buflen(inbuf),fname_len);
727 if( strchr(fname, ':')) {
728 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
729 END_PROFILE(SMBntcreateX);
730 return(ERROR(0, NT_STATUS_OBJECT_PATH_NOT_FOUND));
732 END_PROFILE(SMBntcreateX);
733 return(ERROR(ERRDOS,ERRbadfid));
737 * Copy in the base directory name.
740 pstrcpy( fname, dir_fsp->fsp_name );
741 dir_name_len = strlen(fname);
744 * Ensure it ends in a '\'.
747 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
748 pstrcat(fname, "\\");
753 * This next calculation can refuse a correct filename if we're dealing
754 * with the Win2k unicode bug, but that would be rare. JRA.
757 if(fname_len + dir_name_len >= sizeof(pstring)) {
758 END_PROFILE(SMBntcreateX);
759 return(ERROR(ERRSRV,ERRfilespecs));
762 get_filename(&fname[dir_name_len], inbuf, smb_buf(inbuf)-inbuf,
763 smb_buflen(inbuf),fname_len);
767 get_filename(fname, inbuf, smb_buf(inbuf)-inbuf,
768 smb_buflen(inbuf),fname_len);
772 * Now contruct the smb_open_mode value from the filename,
773 * desired access and the share access.
775 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
777 if((smb_open_mode = map_share_mode(&stat_open_only, fname, desired_access,
779 file_attributes)) == -1) {
780 END_PROFILE(SMBntcreateX);
781 return(ERROR(ERRDOS,ERRbadaccess));
784 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
785 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
788 * Ordinary file or directory.
792 * Check if POSIX semantics are wanted.
795 set_posix_case_semantics(file_attributes);
797 unix_convert(fname,conn,0,&bad_path,&sbuf);
799 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
802 * If it's a request for a directory open, deal with it separately.
805 if(create_options & FILE_DIRECTORY_FILE) {
808 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
810 restore_case_semantics(file_attributes);
813 if((errno == ENOENT) && bad_path) {
814 unix_ERR_class = ERRDOS;
815 unix_ERR_code = ERRbadpath;
817 END_PROFILE(SMBntcreateX);
818 return(UNIXERROR(ERRDOS,ERRnoaccess));
822 * Ordinary file case.
825 /* NB. We have a potential bug here. If we
826 * cause an oplock break to ourselves, then we
827 * could end up processing filename related
828 * SMB requests whilst we await the oplock
829 * break response. As we may have changed the
830 * filename case semantics to be POSIX-like,
831 * this could mean a filename request could
832 * fail when it should succeed. This is a rare
833 * condition, but eventually we must arrange
834 * to restore the correct case semantics
835 * before issuing an oplock break request to
836 * our client. JRA. */
838 fsp = open_file_shared(conn,fname,&sbuf,smb_open_mode,
839 smb_ofun,unixmode, oplock_request,&rmode,&smb_action);
842 /* We cheat here. There are two cases we
843 * care about. One is a directory rename,
844 * where the NT client will attempt to
845 * open the source directory for
846 * DELETE access. Note that when the
847 * NT client does this it does *not*
848 * set the directory bit in the
849 * request packet. This is translated
850 * into a read/write open
851 * request. POSIX states that any open
852 * for write request on a directory
853 * will generate an EISDIR error, so
854 * we can catch this here and open a
855 * pseudo handle that is flagged as a
856 * directory. The second is an open
857 * for a permissions read only, which
858 * we handle in the open_file_stat case. JRA.
861 if(errno == EISDIR) {
864 * Fail the open if it was explicitly a non-directory file.
867 if (create_options & FILE_NON_DIRECTORY_FILE) {
868 restore_case_semantics(file_attributes);
869 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
870 END_PROFILE(SMBntcreateX);
871 return(ERROR(0, NT_STATUS_FILE_IS_A_DIRECTORY));
875 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
878 restore_case_semantics(file_attributes);
879 if((errno == ENOENT) && bad_path) {
880 unix_ERR_class = ERRDOS;
881 unix_ERR_code = ERRbadpath;
883 END_PROFILE(SMBntcreateX);
884 return(UNIXERROR(ERRDOS,ERRnoaccess));
887 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
889 } else if (errno == EACCES && stat_open_only) {
892 * We couldn't open normally and all we want
893 * are the permissions. Try and do a stat open.
898 fsp = open_file_stat(conn,fname,&sbuf,smb_open_mode,&smb_action);
901 restore_case_semantics(file_attributes);
902 END_PROFILE(SMBntcreateX);
903 return(UNIXERROR(ERRDOS,ERRnoaccess));
908 if((errno == ENOENT) && bad_path) {
909 unix_ERR_class = ERRDOS;
910 unix_ERR_code = ERRbadpath;
913 restore_case_semantics(file_attributes);
915 END_PROFILE(SMBntcreateX);
916 return(UNIXERROR(ERRDOS,ERRnoaccess));
921 restore_case_semantics(file_attributes);
923 file_len = sbuf.st_size;
924 fmode = dos_mode(conn,fname,&sbuf);
926 fmode = FILE_ATTRIBUTE_NORMAL;
927 if (!fsp->is_directory && (fmode & aDIR)) {
928 close_file(fsp,False);
929 END_PROFILE(SMBntcreateX);
930 return(ERROR(ERRDOS,ERRnoaccess));
934 * If the caller set the extended oplock request bit
935 * and we granted one (by whatever means) - set the
936 * correct bit for extended oplock reply.
939 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
940 smb_action |= EXTENDED_OPLOCK_GRANTED;
942 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
943 smb_action |= EXTENDED_OPLOCK_GRANTED;
945 set_message(outbuf,34,0,True);
947 p = outbuf + smb_vwv2;
950 * Currently as we don't support level II oplocks we just report
951 * exclusive & batch here.
954 if (smb_action & EXTENDED_OPLOCK_GRANTED)
955 SCVAL(p,0, BATCH_OPLOCK_RETURN);
956 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
957 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
959 SCVAL(p,0,NO_OPLOCK_RETURN);
962 SSVAL(p,0,fsp->fnum);
964 SIVAL(p,0,smb_action);
968 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
970 put_long_date(p,sbuf.st_atime); /* access time */
972 put_long_date(p,sbuf.st_mtime); /* write time */
974 put_long_date(p,sbuf.st_mtime); /* change time */
976 SIVAL(p,0,fmode); /* File Attributes. */
978 SOFF_T(p, 0, file_len);
980 SOFF_T(p,0,file_len);
982 SCVAL(p,0,fsp->is_directory ? 1 : 0);
984 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
986 result = chain_reply(inbuf,outbuf,length,bufsize);
987 END_PROFILE(SMBntcreateX);
991 /****************************************************************************
992 Reply to a NT_TRANSACT_CREATE call to open a pipe.
993 ****************************************************************************/
995 static int do_nt_transact_create_pipe( connection_struct *conn,
996 char *inbuf, char *outbuf, int length,
997 int bufsize, char **ppsetup, char **ppparams,
1002 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
1003 char *params = *ppparams;
1009 * Ensure minimum number of parameters sent.
1012 if(total_parameter_count < 54) {
1013 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1014 return(ERROR(ERRDOS,ERRbadaccess));
1017 fname_len = MIN(((uint32)IVAL(params,44)),((uint32)sizeof(fname)-1));
1019 get_filename_transact(&fname[0], params, 53,
1020 total_parameter_count - 53 - fname_len, fname_len);
1022 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
1025 /* Realloc the size of parameters and data we will return */
1026 params = Realloc(*ppparams, 69);
1028 return(ERROR(ERRDOS,ERRnomem));
1032 memset((char *)params,'\0',69);
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, 0, params, 69, *ppdata, 0);
1059 /****************************************************************************
1060 Internal fn to set security descriptors.
1061 ****************************************************************************/
1063 static BOOL set_sd(files_struct *fsp, char *data, uint32 sd_len, uint security_info_sent, int *pdef_class,uint32 *pdef_code)
1066 SEC_DESC *psd = NULL;
1067 TALLOC_CTX *mem_ctx;
1071 *pdef_class = ERRDOS;
1072 *pdef_code = ERRbadaccess;
1077 * Init the parse struct we will unmarshall from.
1080 if ((mem_ctx = talloc_init()) == NULL) {
1081 DEBUG(0,("set_sd: talloc_init failed.\n"));
1082 *pdef_class = ERRDOS;
1083 *pdef_code = ERRnomem;
1087 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1090 * Setup the prs_struct to point at the memory we just
1094 prs_give_memory( &pd, data, sd_len, False);
1097 * Finally, unmarshall from the data buffer.
1100 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1101 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1103 * Return access denied for want of a better error message..
1105 talloc_destroy(mem_ctx);
1106 *pdef_class = ERRDOS;
1107 *pdef_code = ERRnomem;
1111 if (psd->off_owner_sid==0)
1112 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1113 if (psd->off_grp_sid==0)
1114 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1115 if (psd->off_sacl==0)
1116 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1117 if (psd->off_dacl==0)
1118 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1120 ret = set_nt_acl( fsp, security_info_sent, psd);
1123 talloc_destroy(mem_ctx);
1124 *pdef_class = ERRDOS;
1125 *pdef_code = ERRnoaccess;
1129 talloc_destroy(mem_ctx);
1136 /****************************************************************************
1137 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1138 ****************************************************************************/
1140 static int call_nt_transact_create(connection_struct *conn,
1141 char *inbuf, char *outbuf, int length,
1142 int bufsize, char **ppsetup, char **ppparams,
1146 char *params = *ppparams;
1147 char *data = *ppdata;
1148 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
1149 /* Breakout the oplock request bits so we can set the
1150 reply bits separately. */
1151 int oplock_request = 0;
1153 int fmode=0,rmode=0;
1154 SMB_OFF_T file_len = 0;
1155 SMB_STRUCT_STAT sbuf;
1157 BOOL bad_path = False;
1158 files_struct *fsp = NULL;
1160 BOOL stat_open_only = False;
1162 uint32 desired_access;
1163 uint32 file_attributes;
1164 uint32 share_access;
1165 uint32 create_disposition;
1166 uint32 create_options;
1169 uint16 root_dir_fid;
1176 DEBUG(5,("call_nt_transact_create\n"));
1179 * If it's an IPC, use the pipe handler.
1183 if (lp_nt_pipe_support())
1184 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1185 bufsize, ppsetup, ppparams, ppdata);
1187 return(ERROR(ERRDOS,ERRbadaccess));
1191 * Ensure minimum number of parameters sent.
1194 if(total_parameter_count < 54) {
1195 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1196 return(ERROR(ERRDOS,ERRbadaccess));
1199 flags = IVAL(params,0);
1200 desired_access = IVAL(params,8);
1201 file_attributes = IVAL(params,20);
1202 share_access = IVAL(params,24);
1203 create_disposition = IVAL(params,28);
1204 create_options = IVAL(params,32);
1205 sd_len = IVAL(params,36);
1206 fname_len = MIN(((uint32)IVAL(params,44)),((uint32)sizeof(fname)-1));
1207 root_dir_fid = (uint16)IVAL(params,4);
1208 smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
1211 * We need to construct the open_and_X ofun value from the
1212 * NT values, as that's what our code is structured to accept.
1215 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
1216 return(ERROR(ERRDOS,ERRbadmem));
1219 * Get the file name.
1222 if(root_dir_fid != 0) {
1224 * This filename is relative to a directory fid.
1227 files_struct *dir_fsp = file_fsp(params,4);
1228 size_t dir_name_len;
1231 return(ERROR(ERRDOS,ERRbadfid));
1233 if(!dir_fsp->is_directory) {
1235 * Check to see if this is a mac fork of some kind.
1238 get_filename_transact(&fname[0], params, 53,
1239 total_parameter_count - 53 - fname_len, fname_len);
1241 if( strchr(fname, ':')) {
1242 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
1243 return(ERROR(0, NT_STATUS_OBJECT_PATH_NOT_FOUND));
1246 return(ERROR(ERRDOS,ERRbadfid));
1250 * Copy in the base directory name.
1253 pstrcpy( fname, dir_fsp->fsp_name );
1254 dir_name_len = strlen(fname);
1257 * Ensure it ends in a '\'.
1260 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1261 pstrcat(fname, "\\");
1266 * This next calculation can refuse a correct filename if we're dealing
1267 * with the Win2k unicode bug, but that would be rare. JRA.
1270 if(fname_len + dir_name_len >= sizeof(pstring))
1271 return(ERROR(ERRSRV,ERRfilespecs));
1273 get_filename_transact(&fname[dir_name_len], params, 53,
1274 total_parameter_count - 53 - fname_len, fname_len);
1277 get_filename_transact(&fname[0], params, 53,
1278 total_parameter_count - 53 - fname_len, fname_len);
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( &stat_open_only, fname, desired_access,
1287 share_access, file_attributes)) == -1)
1288 return(ERROR(ERRDOS,ERRbadaccess));
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) {
1314 * We will get a create directory here if the Win32
1315 * app specified a security descriptor in the
1316 * CreateDirectory() call.
1319 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
1322 restore_case_semantics(file_attributes);
1323 if((errno == ENOENT) && bad_path) {
1324 unix_ERR_class = ERRDOS;
1325 unix_ERR_code = ERRbadpath;
1327 return(UNIXERROR(ERRDOS,ERRnoaccess));
1333 * Ordinary file case.
1336 fsp = open_file_shared(conn,fname,&sbuf,smb_open_mode,smb_ofun,unixmode,
1337 oplock_request,&rmode,&smb_action);
1341 if(errno == EISDIR) {
1344 * Fail the open if it was explicitly a non-directory file.
1347 if (create_options & FILE_NON_DIRECTORY_FILE) {
1348 restore_case_semantics(file_attributes);
1349 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
1350 return(ERROR(0, NT_STATUS_FILE_IS_A_DIRECTORY));
1354 fsp = open_directory(conn, fname, &sbuf, smb_ofun, unixmode, &smb_action);
1357 restore_case_semantics(file_attributes);
1358 if((errno == ENOENT) && bad_path) {
1359 unix_ERR_class = ERRDOS;
1360 unix_ERR_code = ERRbadpath;
1362 return(UNIXERROR(ERRDOS,ERRnoaccess));
1365 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
1367 } else if (errno == EACCES && stat_open_only) {
1371 * We couldn't open normally and all we want
1372 * are the permissions. Try and do a stat open.
1377 fsp = open_file_stat(conn,fname,&sbuf,smb_open_mode,&smb_action);
1380 restore_case_semantics(file_attributes);
1381 return(UNIXERROR(ERRDOS,ERRnoaccess));
1385 if((errno == ENOENT) && bad_path) {
1386 unix_ERR_class = ERRDOS;
1387 unix_ERR_code = ERRbadpath;
1390 restore_case_semantics(file_attributes);
1392 return(UNIXERROR(ERRDOS,ERRnoaccess));
1396 file_len = sbuf.st_size;
1397 fmode = dos_mode(conn,fname,&sbuf);
1399 fmode = FILE_ATTRIBUTE_NORMAL;
1402 close_file(fsp,False);
1403 restore_case_semantics(file_attributes);
1404 return(ERROR(ERRDOS,ERRnoaccess));
1408 * If the caller set the extended oplock request bit
1409 * and we granted one (by whatever means) - set the
1410 * correct bit for extended oplock reply.
1413 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1414 smb_action |= EXTENDED_OPLOCK_GRANTED;
1416 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1417 smb_action |= EXTENDED_OPLOCK_GRANTED;
1421 * Now try and apply the desired SD.
1424 if (!set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION, &error_class, &error_code)) {
1425 close_file(fsp,False);
1426 restore_case_semantics(file_attributes);
1427 return(ERROR(error_class, error_code));
1430 restore_case_semantics(file_attributes);
1432 /* Realloc the size of parameters and data we will return */
1433 params = Realloc(*ppparams, 69);
1435 return(ERROR(ERRDOS,ERRnomem));
1439 memset((char *)params,'\0',69);
1442 if (smb_action & EXTENDED_OPLOCK_GRANTED)
1443 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1444 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1445 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1447 SCVAL(p,0,NO_OPLOCK_RETURN);
1450 SSVAL(p,0,fsp->fnum);
1452 SIVAL(p,0,smb_action);
1456 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
1458 put_long_date(p,sbuf.st_atime); /* access time */
1460 put_long_date(p,sbuf.st_mtime); /* write time */
1462 put_long_date(p,sbuf.st_mtime); /* change time */
1464 SIVAL(p,0,fmode); /* File Attributes. */
1466 SOFF_T(p,0,file_len);
1468 SOFF_T(p,0,file_len);
1470 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1472 /* Send the required number of replies */
1473 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 69, *ppdata, 0);
1478 /****************************************************************************
1479 Reply to a NT CANCEL request.
1480 ****************************************************************************/
1481 int reply_ntcancel(connection_struct *conn,
1482 char *inbuf,char *outbuf,int length,int bufsize)
1485 * Go through and cancel any pending change notifies.
1488 int mid = SVAL(inbuf,smb_mid);
1489 START_PROFILE(SMBntcancel);
1490 remove_pending_change_notify_requests_by_mid(mid);
1491 remove_pending_lock_requests_by_mid(mid);
1493 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1495 END_PROFILE(SMBntcancel);
1499 /****************************************************************************
1500 Reply to an unsolicited SMBNTtranss - just ignore it!
1501 ****************************************************************************/
1502 int reply_nttranss(connection_struct *conn,
1503 char *inbuf,char *outbuf,int length,int bufsize)
1505 START_PROFILE(SMBnttranss);
1506 DEBUG(4,("Ignoring nttranss of length %d\n",length));
1507 END_PROFILE(SMBnttranss);
1511 /****************************************************************************
1512 Reply to a notify change - queue the request and
1513 don't allow a directory to be opened.
1514 ****************************************************************************/
1515 static int call_nt_transact_notify_change(connection_struct *conn,
1516 char *inbuf, char *outbuf, int length,
1519 char **ppparams, char **ppdata)
1521 char *setup = *ppsetup;
1525 fsp = file_fsp(setup,4);
1526 flags = IVAL(setup, 0);
1528 DEBUG(3,("call_nt_transact_notify_change\n"));
1531 return(ERROR(ERRDOS,ERRbadfid));
1533 if((!fsp->is_directory) || (conn != fsp->conn))
1534 return(ERROR(ERRDOS,ERRbadfid));
1536 if (!change_notify_set(inbuf, fsp, conn, flags)) {
1537 return(UNIXERROR(ERRDOS,ERRbadfid));
1540 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1541 name = %s\n", fsp->fsp_name ));
1546 /****************************************************************************
1547 Reply to an NT transact rename command.
1548 ****************************************************************************/
1550 static int call_nt_transact_rename(connection_struct *conn,
1551 char *inbuf, char *outbuf, int length,
1553 char **ppsetup, char **ppparams, char **ppdata)
1555 char *params = *ppparams;
1557 files_struct *fsp = file_fsp(params, 0);
1558 BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1559 uint32 fname_len = MIN((((uint32)IVAL(inbuf,smb_nt_TotalParameterCount)-4)),
1560 ((uint32)sizeof(new_name)-1));
1563 CHECK_FSP(fsp, conn);
1564 StrnCpy(new_name,params+4,fname_len);
1565 new_name[fname_len] = '\0';
1567 outsize = rename_internals(conn, inbuf, outbuf, fsp->fsp_name,
1568 new_name, replace_if_exists);
1571 * Rename was successful.
1573 send_nt_replies(inbuf, outbuf, bufsize, 0, NULL, 0, NULL, 0);
1575 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1576 fsp->fsp_name, new_name));
1581 * Win2k needs a changenotify request response before it will
1582 * update after a rename..
1585 process_pending_change_notify_queue((time_t)0);
1592 /****************************************************************************
1593 Reply to query a security descriptor - currently this is not implemented (it
1594 is planned to be though). Right now it just returns the same thing NT would
1595 when queried on a FAT filesystem. JRA.
1596 ****************************************************************************/
1598 static int call_nt_transact_query_security_desc(connection_struct *conn,
1599 char *inbuf, char *outbuf,
1600 int length, int bufsize,
1601 char **ppsetup, char **ppparams, char **ppdata)
1603 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1604 char *params = *ppparams;
1605 char *data = *ppdata;
1607 SEC_DESC *psd = NULL;
1609 TALLOC_CTX *mem_ctx;
1611 files_struct *fsp = file_fsp(params,0);
1614 return(ERROR(ERRDOS,ERRbadfid));
1616 DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1618 params = Realloc(*ppparams, 4);
1620 return(ERROR(ERRDOS,ERRnomem));
1625 * Get the permissions to return.
1628 if((sd_size = get_nt_acl(fsp, &psd)) == 0)
1629 return(UNIXERROR(ERRDOS,ERRnoaccess));
1631 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %d.\n",(int)sd_size));
1633 SIVAL(params,0,(uint32)sd_size);
1635 if(max_data_count < sd_size) {
1637 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1638 params, 4, *ppdata, 0);
1643 * Allocate the data we will point this at.
1646 data = Realloc(*ppdata, sd_size);
1648 return(ERROR(ERRDOS,ERRnomem));
1653 memset(data, '\0', sd_size);
1656 * Init the parse struct we will marshall into.
1659 if ((mem_ctx = talloc_init()) == NULL) {
1660 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1661 return(ERROR(ERRDOS,ERRnomem));
1664 prs_init(&pd, 0, mem_ctx, MARSHALL);
1667 * Setup the prs_struct to point at the memory we just
1671 prs_give_memory( &pd, data, (uint32)sd_size, False);
1674 * Finally, linearize into the outgoing buffer.
1677 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
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 talloc_destroy(mem_ctx);
1693 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 4, data, (int)sd_size);
1697 /****************************************************************************
1698 Reply to set a security descriptor. Map to UNIX perms.
1699 ****************************************************************************/
1701 static int call_nt_transact_set_security_desc(connection_struct *conn,
1702 char *inbuf, char *outbuf, int length,
1703 int bufsize, char **ppsetup,
1704 char **ppparams, char **ppdata)
1706 uint32 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1707 char *params= *ppparams;
1708 char *data = *ppdata;
1709 uint32 total_data_count = (uint32)IVAL(inbuf, smb_nts_TotalDataCount);
1710 files_struct *fsp = NULL;
1711 uint32 security_info_sent = 0;
1715 if(!lp_nt_acl_support())
1716 return(UNIXERROR(ERRDOS,ERRnoaccess));
1718 if(total_parameter_count < 8)
1719 return(ERROR(ERRDOS,ERRbadfunc));
1721 if((fsp = file_fsp(params,0)) == NULL)
1722 return(ERROR(ERRDOS,ERRbadfid));
1724 security_info_sent = IVAL(params,4);
1726 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1727 (unsigned int)security_info_sent ));
1729 if (!set_sd( fsp, data, total_data_count, security_info_sent, &error_class, &error_code))
1730 return (ERROR(error_class, error_code));
1732 send_nt_replies(inbuf, outbuf, bufsize, 0, NULL, 0, NULL, 0);
1736 /****************************************************************************
1737 Reply to IOCTL - not implemented - no plans.
1738 ****************************************************************************/
1739 static int call_nt_transact_ioctl(connection_struct *conn,
1740 char *inbuf, char *outbuf, int length,
1742 char **ppsetup, char **ppparams, char **ppdata)
1744 static BOOL logged_message = False;
1746 if(!logged_message) {
1747 DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
1748 logged_message = True; /* Only print this once... */
1750 return(ERROR(ERRSRV,ERRnosupport));
1753 /****************************************************************************
1754 Reply to a SMBNTtrans.
1755 ****************************************************************************/
1756 int reply_nttrans(connection_struct *conn,
1757 char *inbuf,char *outbuf,int length,int bufsize)
1760 #if 0 /* Not used. */
1761 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1762 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1763 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1764 #endif /* Not used. */
1765 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1766 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1767 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1768 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1769 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1770 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1771 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1772 uint16 function_code = SVAL( inbuf, smb_nt_Function);
1773 char *params = NULL, *data = NULL, *setup = NULL;
1774 uint32 num_params_sofar, num_data_sofar;
1775 START_PROFILE(SMBnttrans);
1777 if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
1779 * Queue this open message as we are the process of an oplock break.
1782 DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
1783 due to being in oplock break state.\n" ));
1785 push_oplock_pending_smb_message( inbuf, length);
1786 END_PROFILE(SMBnttrans);
1790 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
1791 END_PROFILE(SMBnttrans);
1792 return (ERROR(ERRSRV,ERRaccess));
1795 outsize = set_message(outbuf,0,0,True);
1798 * All nttrans messages we handle have smb_wct == 19 + setup_count.
1799 * Ensure this is so as a sanity check.
1802 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1803 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1804 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1805 END_PROFILE(SMBnttrans);
1806 return(ERROR(ERRSRV,ERRerror));
1809 /* Allocate the space for the setup, the maximum needed parameters and data */
1812 setup = (char *)malloc(setup_count);
1813 if (total_parameter_count > 0)
1814 params = (char *)malloc(total_parameter_count);
1815 if (total_data_count > 0)
1816 data = (char *)malloc(total_data_count);
1818 if ((total_parameter_count && !params) || (total_data_count && !data) ||
1819 (setup_count && !setup)) {
1823 DEBUG(0,("reply_nttrans : Out of memory\n"));
1824 END_PROFILE(SMBnttrans);
1825 return(ERROR(ERRDOS,ERRnomem));
1828 /* Copy the param and data bytes sent with this request into
1829 the params buffer */
1830 num_params_sofar = parameter_count;
1831 num_data_sofar = data_count;
1833 if (parameter_count > total_parameter_count || data_count > total_data_count)
1834 exit_server("reply_nttrans: invalid sizes in packet.\n");
1837 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1838 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1839 dump_data(10, setup, setup_count);
1842 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1843 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1844 dump_data(10, params, parameter_count);
1847 memcpy( data, smb_base(inbuf) + data_offset, data_count);
1848 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1849 dump_data(10, data, data_count);
1852 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1853 /* We need to send an interim response then receive the rest
1854 of the parameter/data bytes */
1855 outsize = set_message(outbuf,0,0,True);
1856 send_smb(smbd_server_fd(),outbuf);
1858 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1861 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
1863 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1864 outsize = set_message(outbuf,0,0,True);
1866 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1868 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1869 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1877 END_PROFILE(SMBnttrans);
1878 return(ERROR(ERRSRV,ERRerror));
1881 /* Revise total_params and total_data in case they have changed downwards */
1882 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1883 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1884 num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1885 num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1886 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1887 exit_server("reply_nttrans2: data overflow in secondary nttrans packet\n");
1889 memcpy( ¶ms[ IVAL(inbuf, smb_nts_ParameterDisplacement)],
1890 smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1891 memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1892 smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1896 if (Protocol >= PROTOCOL_NT1) {
1897 uint16 flg2 = SVAL(outbuf,smb_flg2);
1898 SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
1901 /* Now we must call the relevant NT_TRANS function */
1902 switch(function_code) {
1903 case NT_TRANSACT_CREATE:
1904 START_PROFILE_NESTED(NT_transact_create);
1905 outsize = call_nt_transact_create(conn, inbuf, outbuf, length, bufsize,
1906 &setup, ¶ms, &data);
1907 END_PROFILE_NESTED(NT_transact_create);
1909 case NT_TRANSACT_IOCTL:
1910 START_PROFILE_NESTED(NT_transact_ioctl);
1911 outsize = call_nt_transact_ioctl(conn,
1912 inbuf, outbuf, length, bufsize,
1913 &setup, ¶ms, &data);
1914 END_PROFILE_NESTED(NT_transact_ioctl);
1916 case NT_TRANSACT_SET_SECURITY_DESC:
1917 START_PROFILE_NESTED(NT_transact_set_security_desc);
1918 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
1920 &setup, ¶ms, &data);
1921 END_PROFILE_NESTED(NT_transact_set_security_desc);
1923 case NT_TRANSACT_NOTIFY_CHANGE:
1924 START_PROFILE_NESTED(NT_transact_notify_change);
1925 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
1927 &setup, ¶ms, &data);
1928 END_PROFILE_NESTED(NT_transact_notify_change);
1930 case NT_TRANSACT_RENAME:
1931 START_PROFILE_NESTED(NT_transact_rename);
1932 outsize = call_nt_transact_rename(conn, inbuf, outbuf, length,
1934 &setup, ¶ms, &data);
1935 END_PROFILE_NESTED(NT_transact_rename);
1938 case NT_TRANSACT_QUERY_SECURITY_DESC:
1939 START_PROFILE_NESTED(NT_transact_query_security_desc);
1940 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
1942 &setup, ¶ms, &data);
1943 END_PROFILE_NESTED(NT_transact_query_security_desc);
1946 /* Error in request */
1947 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1954 END_PROFILE(SMBnttrans);
1955 return (ERROR(ERRSRV,ERRerror));
1958 /* As we do not know how many data packets will need to be
1959 returned here the various call_nt_transact_xxxx calls
1960 must send their own. Thus a call_nt_transact_xxxx routine only
1961 returns a value other than -1 when it wants to send
1971 END_PROFILE(SMBnttrans);
1972 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
1973 calls have already sent it. If outsize != -1 then it is
1974 returning an error packet. */