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)
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;
679 /* If it's an IPC, use the pipe handler. */
682 if (lp_nt_pipe_support())
683 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
685 return(ERROR(ERRDOS,ERRbadaccess));
690 * We need to construct the open_and_X ofun value from the
691 * NT values, as that's what our code is structured to accept.
694 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
695 return(ERROR(ERRDOS,ERRbadaccess));
701 if(root_dir_fid != 0) {
703 * This filename is relative to a directory fid.
705 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
709 return(ERROR(ERRDOS,ERRbadfid));
711 if(!dir_fsp->is_directory) {
713 * Check to see if this is a mac fork of some kind.
716 get_filename(&fname[0], inbuf, smb_buf(inbuf)-inbuf,
717 smb_buflen(inbuf),fname_len);
719 if( fname[0] == ':') {
720 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
721 return(ERROR(0, NT_STATUS_OBJECT_PATH_NOT_FOUND));
723 return(ERROR(ERRDOS,ERRbadfid));
727 * Copy in the base directory name.
730 pstrcpy( fname, dir_fsp->fsp_name );
731 dir_name_len = strlen(fname);
734 * Ensure it ends in a '\'.
737 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
738 pstrcat(fname, "\\");
743 * This next calculation can refuse a correct filename if we're dealing
744 * with the Win2k unicode bug, but that would be rare. JRA.
747 if(fname_len + dir_name_len >= sizeof(pstring))
748 return(ERROR(ERRSRV,ERRfilespecs));
750 get_filename(&fname[dir_name_len], inbuf, smb_buf(inbuf)-inbuf,
751 smb_buflen(inbuf),fname_len);
755 get_filename(fname, inbuf, smb_buf(inbuf)-inbuf,
756 smb_buflen(inbuf),fname_len);
760 * Now contruct the smb_open_mode value from the filename,
761 * desired access and the share access.
763 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
765 if((smb_open_mode = map_share_mode(&stat_open_only, fname, desired_access,
767 file_attributes)) == -1)
768 return(ERROR(ERRDOS,ERRbadaccess));
770 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
771 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
774 * Ordinary file or directory.
778 * Check if POSIX semantics are wanted.
781 set_posix_case_semantics(file_attributes);
783 unix_convert(fname,conn,0,&bad_path,NULL);
785 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
788 * If it's a request for a directory open, deal with it separately.
791 if(create_options & FILE_DIRECTORY_FILE) {
794 fsp = open_directory(conn, fname, smb_ofun, unixmode, &smb_action);
796 restore_case_semantics(file_attributes);
799 if((errno == ENOENT) && bad_path) {
800 unix_ERR_class = ERRDOS;
801 unix_ERR_code = ERRbadpath;
803 return(UNIXERROR(ERRDOS,ERRnoaccess));
807 * Ordinary file case.
810 /* NB. We have a potential bug here. If we
811 * cause an oplock break to ourselves, then we
812 * could end up processing filename related
813 * SMB requests whilst we await the oplock
814 * break response. As we may have changed the
815 * filename case semantics to be POSIX-like,
816 * this could mean a filename request could
817 * fail when it should succeed. This is a rare
818 * condition, but eventually we must arrange
819 * to restore the correct case semantics
820 * before issuing an oplock break request to
821 * our client. JRA. */
823 fsp = open_file_shared(conn,fname,smb_open_mode,
824 smb_ofun,unixmode, oplock_request,&rmode,&smb_action);
827 /* We cheat here. There are two cases we
828 * care about. One is a directory rename,
829 * where the NT client will attempt to
830 * open the source directory for
831 * DELETE access. Note that when the
832 * NT client does this it does *not*
833 * set the directory bit in the
834 * request packet. This is translated
835 * into a read/write open
836 * request. POSIX states that any open
837 * for write request on a directory
838 * will generate an EISDIR error, so
839 * we can catch this here and open a
840 * pseudo handle that is flagged as a
841 * directory. The second is an open
842 * for a permissions read only, which
843 * we handle in the open_file_stat case. JRA.
846 if(errno == EISDIR) {
849 * Fail the open if it was explicitly a non-directory file.
852 if (create_options & FILE_NON_DIRECTORY_FILE) {
853 restore_case_semantics(file_attributes);
854 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
855 return(ERROR(0, NT_STATUS_FILE_IS_A_DIRECTORY));
859 fsp = open_directory(conn, fname, smb_ofun, unixmode, &smb_action);
862 restore_case_semantics(file_attributes);
863 if((errno == ENOENT) && bad_path) {
864 unix_ERR_class = ERRDOS;
865 unix_ERR_code = ERRbadpath;
867 return(UNIXERROR(ERRDOS,ERRnoaccess));
870 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
872 } else if (errno == EACCES && stat_open_only) {
875 * We couldn't open normally and all we want
876 * are the permissions. Try and do a stat open.
881 fsp = open_file_stat(conn,fname,smb_open_mode,&sbuf,&smb_action);
884 restore_case_semantics(file_attributes);
885 return(UNIXERROR(ERRDOS,ERRnoaccess));
890 if((errno == ENOENT) && bad_path) {
891 unix_ERR_class = ERRDOS;
892 unix_ERR_code = ERRbadpath;
895 restore_case_semantics(file_attributes);
897 return(UNIXERROR(ERRDOS,ERRnoaccess));
902 if(fsp->is_directory) {
903 if(conn->vfs_ops.stat(conn,dos_to_unix(fsp->fsp_name, False), &sbuf) != 0) {
904 close_file(fsp,True);
905 restore_case_semantics(file_attributes);
906 return(ERROR(ERRDOS,ERRnoaccess));
909 if (conn->vfs_ops.fstat(fsp,fsp->fd,&sbuf) != 0) {
910 close_file(fsp,False);
911 restore_case_semantics(file_attributes);
912 return(ERROR(ERRDOS,ERRnoaccess));
916 restore_case_semantics(file_attributes);
918 file_len = sbuf.st_size;
919 fmode = dos_mode(conn,fname,&sbuf);
921 fmode = FILE_ATTRIBUTE_NORMAL;
922 if (!fsp->is_directory && (fmode & aDIR)) {
923 close_file(fsp,False);
924 return(ERROR(ERRDOS,ERRnoaccess));
928 * If the caller set the extended oplock request bit
929 * and we granted one (by whatever means) - set the
930 * correct bit for extended oplock reply.
933 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
934 smb_action |= EXTENDED_OPLOCK_GRANTED;
936 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
937 smb_action |= EXTENDED_OPLOCK_GRANTED;
939 set_message(outbuf,34,0,True);
941 p = outbuf + smb_vwv2;
944 * Currently as we don't support level II oplocks we just report
945 * exclusive & batch here.
948 if (smb_action & EXTENDED_OPLOCK_GRANTED)
949 SCVAL(p,0, BATCH_OPLOCK_RETURN);
950 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
951 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
953 SCVAL(p,0,NO_OPLOCK_RETURN);
956 SSVAL(p,0,fsp->fnum);
958 SIVAL(p,0,smb_action);
962 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
964 put_long_date(p,sbuf.st_atime); /* access time */
966 put_long_date(p,sbuf.st_mtime); /* write time */
968 put_long_date(p,sbuf.st_mtime); /* change time */
970 SIVAL(p,0,fmode); /* File Attributes. */
972 SOFF_T(p, 0, file_len);
974 SOFF_T(p,0,file_len);
976 SCVAL(p,0,fsp->is_directory ? 1 : 0);
978 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
980 return chain_reply(inbuf,outbuf,length,bufsize);
983 /****************************************************************************
984 Reply to a NT_TRANSACT_CREATE call to open a pipe.
985 ****************************************************************************/
987 static int do_nt_transact_create_pipe( connection_struct *conn,
988 char *inbuf, char *outbuf, int length,
989 int bufsize, char **ppsetup, char **ppparams,
994 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
995 char *params = *ppparams;
1001 * Ensure minimum number of parameters sent.
1004 if(total_parameter_count < 54) {
1005 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1006 return(ERROR(ERRDOS,ERRbadaccess));
1009 fname_len = MIN(((uint32)IVAL(params,44)),((uint32)sizeof(fname)-1));
1011 get_filename_transact(&fname[0], params, 53,
1012 total_parameter_count - 53 - fname_len, fname_len);
1014 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
1017 /* Realloc the size of parameters and data we will return */
1018 params = *ppparams = Realloc(*ppparams, 69);
1020 return(ERROR(ERRDOS,ERRnomem));
1022 memset((char *)params,'\0',69);
1025 SCVAL(p,0,NO_OPLOCK_RETURN);
1030 SIVAL(p,0,FILE_WAS_OPENED);
1034 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1037 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1039 SSVAL(p,2, 0x5FF); /* ? */
1041 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1043 /* Send the required number of replies */
1044 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 69, *ppdata, 0);
1049 /****************************************************************************
1050 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1051 ****************************************************************************/
1053 static int call_nt_transact_create(connection_struct *conn,
1054 char *inbuf, char *outbuf, int length,
1055 int bufsize, char **ppsetup, char **ppparams,
1059 char *params = *ppparams;
1060 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
1061 /* Breakout the oplock request bits so we can set the
1062 reply bits separately. */
1063 int oplock_request = 0;
1065 int fmode=0,rmode=0;
1066 SMB_OFF_T file_len = 0;
1067 SMB_STRUCT_STAT sbuf;
1069 BOOL bad_path = False;
1070 files_struct *fsp = NULL;
1072 BOOL stat_open_only = False;
1074 uint32 desired_access;
1075 uint32 file_attributes;
1076 uint32 share_access;
1077 uint32 create_disposition;
1078 uint32 create_options;
1080 uint16 root_dir_fid;
1085 DEBUG(5,("call_nt_transact_create\n"));
1088 * If it's an IPC, use the pipe handler.
1092 if (lp_nt_pipe_support())
1093 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1094 bufsize, ppsetup, ppparams, ppdata);
1096 return(ERROR(ERRDOS,ERRbadaccess));
1100 * Ensure minimum number of parameters sent.
1103 if(total_parameter_count < 54) {
1104 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1105 return(ERROR(ERRDOS,ERRbadaccess));
1108 flags = IVAL(params,0);
1109 desired_access = IVAL(params,8);
1110 file_attributes = IVAL(params,20);
1111 share_access = IVAL(params,24);
1112 create_disposition = IVAL(params,28);
1113 create_options = IVAL(params,32);
1114 fname_len = MIN(((uint32)IVAL(params,44)),((uint32)sizeof(fname)-1));
1115 root_dir_fid = (uint16)IVAL(params,4);
1116 smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
1119 * We need to construct the open_and_X ofun value from the
1120 * NT values, as that's what our code is structured to accept.
1123 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
1124 return(ERROR(ERRDOS,ERRbadmem));
1127 * Get the file name.
1130 if(root_dir_fid != 0) {
1132 * This filename is relative to a directory fid.
1135 files_struct *dir_fsp = file_fsp(params,4);
1136 size_t dir_name_len;
1139 return(ERROR(ERRDOS,ERRbadfid));
1141 if(!dir_fsp->is_directory) {
1143 * Check to see if this is a mac fork of some kind.
1146 get_filename_transact(&fname[0], params, 53,
1147 total_parameter_count - 53 - fname_len, fname_len);
1149 if( fname[0] == ':') {
1150 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
1151 return(ERROR(0, NT_STATUS_OBJECT_PATH_NOT_FOUND));
1154 return(ERROR(ERRDOS,ERRbadfid));
1158 * Copy in the base directory name.
1161 pstrcpy( fname, dir_fsp->fsp_name );
1162 dir_name_len = strlen(fname);
1165 * Ensure it ends in a '\'.
1168 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1169 pstrcat(fname, "\\");
1174 * This next calculation can refuse a correct filename if we're dealing
1175 * with the Win2k unicode bug, but that would be rare. JRA.
1178 if(fname_len + dir_name_len >= sizeof(pstring))
1179 return(ERROR(ERRSRV,ERRfilespecs));
1181 get_filename_transact(&fname[dir_name_len], params, 53,
1182 total_parameter_count - 53 - fname_len, fname_len);
1185 get_filename_transact(&fname[0], params, 53,
1186 total_parameter_count - 53 - fname_len, fname_len);
1190 * Now contruct the smb_open_mode value from the desired access
1191 * and the share access.
1194 if((smb_open_mode = map_share_mode( &stat_open_only, fname, desired_access,
1195 share_access, file_attributes)) == -1)
1196 return(ERROR(ERRDOS,ERRbadaccess));
1198 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1199 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1202 * Check if POSIX semantics are wanted.
1205 set_posix_case_semantics(file_attributes);
1207 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1209 unix_convert(fname,conn,0,&bad_path,NULL);
1211 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
1214 * If it's a request for a directory open, deal with it separately.
1217 if(create_options & FILE_DIRECTORY_FILE) {
1222 * We will get a create directory here if the Win32
1223 * app specified a security descriptor in the
1224 * CreateDirectory() call.
1227 fsp = open_directory(conn, fname, smb_ofun, unixmode, &smb_action);
1230 restore_case_semantics(file_attributes);
1231 if((errno == ENOENT) && bad_path) {
1232 unix_ERR_class = ERRDOS;
1233 unix_ERR_code = ERRbadpath;
1235 return(UNIXERROR(ERRDOS,ERRnoaccess));
1238 if(conn->vfs_ops.stat(conn,dos_to_unix(fsp->fsp_name, False),
1240 close_file(fsp,True);
1241 restore_case_semantics(file_attributes);
1242 return(ERROR(ERRDOS,ERRnoaccess));
1248 * Ordinary file case.
1251 fsp = open_file_shared(conn,fname,smb_open_mode,smb_ofun,unixmode,
1252 oplock_request,&rmode,&smb_action);
1256 if(errno == EISDIR) {
1259 * Fail the open if it was explicitly a non-directory file.
1262 if (create_options & FILE_NON_DIRECTORY_FILE) {
1263 restore_case_semantics(file_attributes);
1264 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
1265 return(ERROR(0, NT_STATUS_FILE_IS_A_DIRECTORY));
1269 fsp = open_directory(conn, fname, smb_ofun, unixmode, &smb_action);
1272 restore_case_semantics(file_attributes);
1273 if((errno == ENOENT) && bad_path) {
1274 unix_ERR_class = ERRDOS;
1275 unix_ERR_code = ERRbadpath;
1277 return(UNIXERROR(ERRDOS,ERRnoaccess));
1280 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
1282 } else if (errno == EACCES && stat_open_only) {
1286 * We couldn't open normally and all we want
1287 * are the permissions. Try and do a stat open.
1292 fsp = open_file_stat(conn,fname,smb_open_mode,&sbuf,&smb_action);
1295 restore_case_semantics(file_attributes);
1296 return(UNIXERROR(ERRDOS,ERRnoaccess));
1300 if((errno == ENOENT) && bad_path) {
1301 unix_ERR_class = ERRDOS;
1302 unix_ERR_code = ERRbadpath;
1305 restore_case_semantics(file_attributes);
1307 return(UNIXERROR(ERRDOS,ERRnoaccess));
1311 if(fsp->is_directory) {
1312 if(conn->vfs_ops.stat(conn,dos_to_unix(fsp->fsp_name,False), &sbuf) != 0) {
1313 close_file(fsp,True);
1314 restore_case_semantics(file_attributes);
1315 return(ERROR(ERRDOS,ERRnoaccess));
1318 if (!fsp->stat_open && conn->vfs_ops.fstat(fsp,fsp->fd,&sbuf) != 0) {
1319 close_file(fsp,False);
1320 restore_case_semantics(file_attributes);
1321 return(ERROR(ERRDOS,ERRnoaccess));
1325 file_len = sbuf.st_size;
1326 fmode = dos_mode(conn,fname,&sbuf);
1328 fmode = FILE_ATTRIBUTE_NORMAL;
1331 close_file(fsp,False);
1332 restore_case_semantics(file_attributes);
1333 return(ERROR(ERRDOS,ERRnoaccess));
1337 * If the caller set the extended oplock request bit
1338 * and we granted one (by whatever means) - set the
1339 * correct bit for extended oplock reply.
1342 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1343 smb_action |= EXTENDED_OPLOCK_GRANTED;
1345 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1346 smb_action |= EXTENDED_OPLOCK_GRANTED;
1349 restore_case_semantics(file_attributes);
1351 /* Realloc the size of parameters and data we will return */
1352 params = *ppparams = Realloc(*ppparams, 69);
1354 return(ERROR(ERRDOS,ERRnomem));
1356 memset((char *)params,'\0',69);
1359 if (smb_action & EXTENDED_OPLOCK_GRANTED)
1360 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1361 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1362 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1364 SCVAL(p,0,NO_OPLOCK_RETURN);
1367 SSVAL(p,0,fsp->fnum);
1369 SIVAL(p,0,smb_action);
1373 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
1375 put_long_date(p,sbuf.st_atime); /* access time */
1377 put_long_date(p,sbuf.st_mtime); /* write time */
1379 put_long_date(p,sbuf.st_mtime); /* change time */
1381 SIVAL(p,0,fmode); /* File Attributes. */
1383 SOFF_T(p,0,file_len);
1385 SOFF_T(p,0,file_len);
1387 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1389 /* Send the required number of replies */
1390 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 69, *ppdata, 0);
1395 /****************************************************************************
1396 Reply to a NT CANCEL request.
1397 ****************************************************************************/
1398 int reply_ntcancel(connection_struct *conn,
1399 char *inbuf,char *outbuf,int length,int bufsize)
1402 * Go through and cancel any pending change notifies.
1405 int mid = SVAL(inbuf,smb_mid);
1406 remove_pending_change_notify_requests_by_mid(mid);
1407 remove_pending_lock_requests_by_mid(mid);
1409 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1414 /****************************************************************************
1415 Reply to an unsolicited SMBNTtranss - just ignore it!
1416 ****************************************************************************/
1417 int reply_nttranss(connection_struct *conn,
1418 char *inbuf,char *outbuf,int length,int bufsize)
1420 DEBUG(4,("Ignoring nttranss of length %d\n",length));
1424 /****************************************************************************
1425 Reply to a notify change - queue the request and
1426 don't allow a directory to be opened.
1427 ****************************************************************************/
1428 static int call_nt_transact_notify_change(connection_struct *conn,
1429 char *inbuf, char *outbuf, int length,
1432 char **ppparams, char **ppdata)
1434 char *setup = *ppsetup;
1438 fsp = file_fsp(setup,4);
1439 flags = IVAL(setup, 0);
1441 DEBUG(3,("call_nt_transact_notify_change\n"));
1444 return(ERROR(ERRDOS,ERRbadfid));
1446 if((!fsp->is_directory) || (conn != fsp->conn))
1447 return(ERROR(ERRDOS,ERRbadfid));
1449 if (!change_notify_set(inbuf, fsp, conn, flags)) {
1450 return(UNIXERROR(ERRDOS,ERRbadfid));
1453 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1454 name = %s\n", fsp->fsp_name ));
1459 /****************************************************************************
1460 Reply to an NT transact rename command.
1461 ****************************************************************************/
1463 static int call_nt_transact_rename(connection_struct *conn,
1464 char *inbuf, char *outbuf, int length,
1466 char **ppsetup, char **ppparams, char **ppdata)
1468 char *params = *ppparams;
1470 files_struct *fsp = file_fsp(params, 0);
1471 BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1472 uint32 fname_len = MIN((((uint32)IVAL(inbuf,smb_nt_TotalParameterCount)-4)),
1473 ((uint32)sizeof(new_name)-1));
1476 CHECK_FSP(fsp, conn);
1477 StrnCpy(new_name,params+4,fname_len);
1478 new_name[fname_len] = '\0';
1480 outsize = rename_internals(conn, inbuf, outbuf, fsp->fsp_name,
1481 new_name, replace_if_exists);
1484 * Rename was successful.
1486 send_nt_replies(inbuf, outbuf, bufsize, 0, NULL, 0, NULL, 0);
1488 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1489 fsp->fsp_name, new_name));
1494 * Win2k needs a changenotify request response before it will
1495 * update after a rename..
1498 process_pending_change_notify_queue((time_t)0);
1505 /****************************************************************************
1506 Reply to query a security descriptor - currently this is not implemented (it
1507 is planned to be though). Right now it just returns the same thing NT would
1508 when queried on a FAT filesystem. JRA.
1509 ****************************************************************************/
1511 static int call_nt_transact_query_security_desc(connection_struct *conn,
1512 char *inbuf, char *outbuf,
1513 int length, int bufsize,
1514 char **ppsetup, char **ppparams, char **ppdata)
1516 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1517 char *params = *ppparams;
1518 char *data = *ppdata;
1520 SEC_DESC *psd = NULL;
1522 TALLOC_CTX *mem_ctx;
1524 files_struct *fsp = file_fsp(params,0);
1527 return(ERROR(ERRDOS,ERRbadfid));
1529 DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1531 params = *ppparams = Realloc(*ppparams, 4);
1533 return(ERROR(ERRDOS,ERRnomem));
1536 * Get the permissions to return.
1539 if((sd_size = get_nt_acl(fsp, &psd)) == 0)
1540 return(UNIXERROR(ERRDOS,ERRnoaccess));
1542 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %d.\n",(int)sd_size));
1544 SIVAL(params,0,(uint32)sd_size);
1546 if(max_data_count < sd_size) {
1548 free_sec_desc(&psd);
1550 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1551 params, 4, *ppdata, 0);
1556 * Allocate the data we will point this at.
1559 data = *ppdata = Realloc(*ppdata, sd_size);
1561 free_sec_desc(&psd);
1562 return(ERROR(ERRDOS,ERRnomem));
1565 memset(data, '\0', sd_size);
1568 * Init the parse struct we will marshall into.
1571 if ((mem_ctx = talloc_init()) == NULL) {
1572 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1573 free_sec_desc(&psd);
1574 return(ERROR(ERRDOS,ERRnomem));
1577 prs_init(&pd, 0, 4, mem_ctx, MARSHALL);
1580 * Setup the prs_struct to point at the memory we just
1584 prs_give_memory( &pd, data, (uint32)sd_size, False);
1587 * Finally, linearize into the outgoing buffer.
1590 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1591 free_sec_desc(&psd);
1592 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1593 security descriptor.\n"));
1595 * Return access denied for want of a better error message..
1597 talloc_destroy(mem_ctx);
1598 return(UNIXERROR(ERRDOS,ERRnoaccess));
1602 * Now we can delete the security descriptor.
1605 free_sec_desc(&psd);
1606 talloc_destroy(mem_ctx);
1608 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 4, data, (int)sd_size);
1612 /****************************************************************************
1613 Reply to set a security descriptor. Map to UNIX perms.
1614 ****************************************************************************/
1616 static int call_nt_transact_set_security_desc(connection_struct *conn,
1617 char *inbuf, char *outbuf, int length,
1618 int bufsize, char **ppsetup,
1619 char **ppparams, char **ppdata)
1621 uint32 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1622 char *params= *ppparams;
1623 char *data = *ppdata;
1625 SEC_DESC *psd = NULL;
1626 uint32 total_data_count = (uint32)IVAL(inbuf, smb_nts_TotalDataCount);
1627 files_struct *fsp = NULL;
1628 uint32 security_info_sent = 0;
1629 TALLOC_CTX *mem_ctx;
1632 if(!lp_nt_acl_support())
1633 return(UNIXERROR(ERRDOS,ERRnoaccess));
1635 if(total_parameter_count < 8)
1636 return(ERROR(ERRDOS,ERRbadfunc));
1638 if((fsp = file_fsp(params,0)) == NULL)
1639 return(ERROR(ERRDOS,ERRbadfid));
1641 security_info_sent = IVAL(params,4);
1643 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1644 (unsigned int)security_info_sent ));
1647 * Init the parse struct we will unmarshall from.
1650 if ((mem_ctx = talloc_init()) == NULL) {
1651 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1652 return(ERROR(ERRDOS,ERRnomem));
1655 prs_init(&pd, 0, 4, mem_ctx, UNMARSHALL);
1658 * Setup the prs_struct to point at the memory we just
1662 prs_give_memory( &pd, data, total_data_count, False);
1665 * Finally, unmarshall from the data buffer.
1668 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1669 free_sec_desc(&psd);
1670 DEBUG(0,("call_nt_transact_set_security_desc: Error in unmarshalling \
1671 security descriptor.\n"));
1673 * Return access denied for want of a better error message..
1675 talloc_destroy(mem_ctx);
1676 return(UNIXERROR(ERRDOS,ERRnoaccess));
1679 ret = set_nt_acl( fsp, security_info_sent, psd);
1682 free_sec_desc(&psd);
1683 talloc_destroy(mem_ctx);
1684 return(UNIXERROR(ERRDOS,ERRnoaccess));
1687 free_sec_desc(&psd);
1688 talloc_destroy(mem_ctx);
1689 send_nt_replies(inbuf, outbuf, bufsize, 0, NULL, 0, NULL, 0);
1693 /****************************************************************************
1694 Reply to IOCTL - not implemented - no plans.
1695 ****************************************************************************/
1696 static int call_nt_transact_ioctl(connection_struct *conn,
1697 char *inbuf, char *outbuf, int length,
1699 char **ppsetup, char **ppparams, char **ppdata)
1701 static BOOL logged_message = False;
1703 if(!logged_message) {
1704 DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
1705 logged_message = True; /* Only print this once... */
1707 return(ERROR(ERRSRV,ERRnosupport));
1710 /****************************************************************************
1711 Reply to a SMBNTtrans.
1712 ****************************************************************************/
1713 int reply_nttrans(connection_struct *conn,
1714 char *inbuf,char *outbuf,int length,int bufsize)
1717 #if 0 /* Not used. */
1718 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1719 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1720 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1721 #endif /* Not used. */
1722 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1723 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1724 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1725 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1726 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1727 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1728 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1729 uint16 function_code = SVAL( inbuf, smb_nt_Function);
1730 char *params = NULL, *data = NULL, *setup = NULL;
1731 uint32 num_params_sofar, num_data_sofar;
1733 if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
1735 * Queue this open message as we are the process of an oplock break.
1738 DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
1739 due to being in oplock break state.\n" ));
1741 push_oplock_pending_smb_message( inbuf, length);
1745 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE))
1746 return (ERROR(ERRSRV,ERRaccess));
1748 outsize = set_message(outbuf,0,0,True);
1751 * All nttrans messages we handle have smb_wct == 19 + setup_count.
1752 * Ensure this is so as a sanity check.
1755 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1756 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1757 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1758 return(ERROR(ERRSRV,ERRerror));
1761 /* Allocate the space for the setup, the maximum needed parameters and data */
1764 setup = (char *)malloc(setup_count);
1765 if (total_parameter_count > 0)
1766 params = (char *)malloc(total_parameter_count);
1767 if (total_data_count > 0)
1768 data = (char *)malloc(total_data_count);
1770 if ((total_parameter_count && !params) || (total_data_count && !data) ||
1771 (setup_count && !setup)) {
1772 DEBUG(0,("reply_nttrans : Out of memory\n"));
1773 return(ERROR(ERRDOS,ERRnomem));
1776 /* Copy the param and data bytes sent with this request into
1777 the params buffer */
1778 num_params_sofar = parameter_count;
1779 num_data_sofar = data_count;
1781 if (parameter_count > total_parameter_count || data_count > total_data_count)
1782 exit_server("reply_nttrans: invalid sizes in packet.\n");
1785 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1786 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1787 dump_data(10, setup, setup_count);
1790 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1791 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1792 dump_data(10, params, parameter_count);
1795 memcpy( data, smb_base(inbuf) + data_offset, data_count);
1796 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1797 dump_data(10, data, data_count);
1800 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1801 /* We need to send an interim response then receive the rest
1802 of the parameter/data bytes */
1803 outsize = set_message(outbuf,0,0,True);
1804 send_smb(smbd_server_fd(),outbuf);
1806 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1809 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
1811 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1812 outsize = set_message(outbuf,0,0,True);
1814 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1816 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1817 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1825 return(ERROR(ERRSRV,ERRerror));
1828 /* Revise total_params and total_data in case they have changed downwards */
1829 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1830 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1831 num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1832 num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1833 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1834 exit_server("reply_nttrans2: data overflow in secondary nttrans packet\n");
1836 memcpy( ¶ms[ IVAL(inbuf, smb_nts_ParameterDisplacement)],
1837 smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1838 memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1839 smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1843 if (Protocol >= PROTOCOL_NT1) {
1844 uint16 flg2 = SVAL(outbuf,smb_flg2);
1845 SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
1848 /* Now we must call the relevant NT_TRANS function */
1849 switch(function_code) {
1850 case NT_TRANSACT_CREATE:
1851 outsize = call_nt_transact_create(conn, inbuf, outbuf, length, bufsize,
1852 &setup, ¶ms, &data);
1854 case NT_TRANSACT_IOCTL:
1855 outsize = call_nt_transact_ioctl(conn,
1856 inbuf, outbuf, length, bufsize,
1857 &setup, ¶ms, &data);
1859 case NT_TRANSACT_SET_SECURITY_DESC:
1860 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
1862 &setup, ¶ms, &data);
1864 case NT_TRANSACT_NOTIFY_CHANGE:
1865 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
1867 &setup, ¶ms, &data);
1869 case NT_TRANSACT_RENAME:
1870 outsize = call_nt_transact_rename(conn, inbuf, outbuf, length,
1872 &setup, ¶ms, &data);
1875 case NT_TRANSACT_QUERY_SECURITY_DESC:
1876 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
1878 &setup, ¶ms, &data);
1881 /* Error in request */
1882 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1889 return (ERROR(ERRSRV,ERRerror));
1892 /* As we do not know how many data packets will need to be
1893 returned here the various call_nt_transact_xxxx calls
1894 must send their own. Thus a call_nt_transact_xxxx routine only
1895 returns a value other than -1 when it wants to send
1905 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
1906 calls have already sent it. If outsize != -1 then it is
1907 returning an error packet. */