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 void remove_pending_change_notify_requests_by_mid(int mid);
35 static char *known_nt_pipes[] = {
53 /****************************************************************************
54 Send the required number of replies back.
55 We assume all fields other than the data fields are
56 set correctly for the type of call.
57 HACK ! Always assumes smb_setup field is zero.
58 ****************************************************************************/
60 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, uint32 nt_error, char *params,
61 int paramsize, char *pdata, int datasize)
64 int data_to_send = datasize;
65 int params_to_send = paramsize;
69 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
70 int alignment_offset = 3;
71 int data_alignment_offset = 0;
74 * Initially set the wcnt area to be 18 - this is true for all
78 set_message(outbuf,18,0,True);
82 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
88 * If there genuinely are no parameters or data to send just send
92 if(params_to_send == 0 && data_to_send == 0) {
93 send_smb(smbd_server_fd(),outbuf);
98 * When sending params and data ensure that both are nicely aligned.
99 * Only do this alignment when there is also data to send - else
100 * can cause NT redirector problems.
103 if (((params_to_send % 4) != 0) && (data_to_send != 0))
104 data_alignment_offset = 4 - (params_to_send % 4);
107 * Space is bufsize minus Netbios over TCP header minus SMB header.
108 * The alignment_offset is to align the param bytes on a four byte
109 * boundary (2 bytes for data len, one byte pad).
110 * NT needs this to work correctly.
113 useable_space = bufsize - ((smb_buf(outbuf)+
114 alignment_offset+data_alignment_offset) -
118 * useable_space can never be more than max_send minus the
122 useable_space = MIN(useable_space,
123 max_send - (alignment_offset+data_alignment_offset));
126 while (params_to_send || data_to_send) {
129 * Calculate whether we will totally or partially fill this packet.
132 total_sent_thistime = params_to_send + data_to_send +
133 alignment_offset + data_alignment_offset;
136 * We can never send more than useable_space.
139 total_sent_thistime = MIN(total_sent_thistime, useable_space);
141 set_message(outbuf, 18, total_sent_thistime, True);
144 * Set total params and data to be sent.
147 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
148 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
151 * Calculate how many parameters and data we can fit into
152 * this packet. Parameters get precedence.
155 params_sent_thistime = MIN(params_to_send,useable_space);
156 data_sent_thistime = useable_space - params_sent_thistime;
157 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
159 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
161 if(params_sent_thistime == 0) {
162 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
163 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
166 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
167 * parameter bytes, however the first 4 bytes of outbuf are
168 * the Netbios over TCP header. Thus use smb_base() to subtract
169 * them from the calculation.
172 SIVAL(outbuf,smb_ntr_ParameterOffset,
173 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
175 * Absolute displacement of param bytes sent in this packet.
178 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
182 * Deal with the data portion.
185 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
187 if(data_sent_thistime == 0) {
188 SIVAL(outbuf,smb_ntr_DataOffset,0);
189 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
192 * The offset of the data bytes is the offset of the
193 * parameter bytes plus the number of parameters being sent this time.
196 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
197 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
198 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
202 * Copy the param bytes into the packet.
205 if(params_sent_thistime)
206 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
209 * Copy in the data bytes
212 if(data_sent_thistime)
213 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
214 data_alignment_offset,pd,data_sent_thistime);
216 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
217 params_sent_thistime, data_sent_thistime, useable_space));
218 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
219 params_to_send, data_to_send, paramsize, datasize));
221 /* Send the packet */
222 send_smb(smbd_server_fd(),outbuf);
224 pp += params_sent_thistime;
225 pd += data_sent_thistime;
227 params_to_send -= params_sent_thistime;
228 data_to_send -= data_sent_thistime;
234 if(params_to_send < 0 || data_to_send < 0) {
235 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
236 params_to_send, data_to_send));
244 /****************************************************************************
245 (Hopefully) temporary call to fix bugs in NT5.0beta2. This OS sends unicode
246 strings in NT calls AND DOESN'T SET THE UNICODE BIT !!!!!!!
247 ****************************************************************************/
249 static void get_filename( char *fname, char *inbuf, int data_offset, int data_len, int fname_len)
252 * We need various heuristics here to detect a unicode string... JRA.
255 DEBUG(10,("get_filename: data_offset = %d, data_len = %d, fname_len = %d\n",
256 data_offset, data_len, fname_len ));
258 if(data_len - fname_len > 1) {
260 * NT 5.0 Beta 2 has kindly sent us a UNICODE string
261 * without bothering to set the unicode bit. How kind.
263 * Firstly - ensure that the data offset is aligned
264 * on a 2 byte boundary - add one if not.
266 fname_len = fname_len/2;
269 pstrcpy(fname, dos_unistrn2((uint16 *)(inbuf+data_offset), fname_len));
271 StrnCpy(fname,inbuf+data_offset,fname_len);
272 fname[fname_len] = '\0';
276 /****************************************************************************
277 Fix bugs in Win2000 final release. In trans calls this OS sends unicode
278 strings AND DOESN'T SET THE UNICODE BIT !!!!!!!
279 ****************************************************************************/
281 static void get_filename_transact( char *fname, char *inbuf, int data_offset, int data_len, int fname_len)
284 * We need various heuristics here to detect a unicode string... JRA.
287 DEBUG(10,("get_filename_transact: data_offset = %d, data_len = %d, fname_len = %d\n",
288 data_offset, data_len, fname_len ));
291 * Win2K sends a unicode filename plus one extra alingment byte.
292 * WinNT4.x send an ascii string with multiple garbage bytes on
296 if((data_len - fname_len == 1) || (inbuf[data_offset] == '\0')) {
298 * Ensure that the data offset is aligned
299 * on a 2 byte boundary - add one if not.
301 fname_len = fname_len/2;
304 pstrcpy(fname, dos_unistrn2((uint16 *)(inbuf+data_offset), fname_len));
306 StrnCpy(fname,inbuf+data_offset,fname_len);
307 fname[fname_len] = '\0';
311 /****************************************************************************
313 ****************************************************************************/
315 static BOOL saved_case_sensitive;
316 static BOOL saved_case_preserve;
317 static BOOL saved_short_case_preserve;
319 /****************************************************************************
321 ****************************************************************************/
323 static void set_posix_case_semantics(uint32 file_attributes)
325 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
328 saved_case_sensitive = case_sensitive;
329 saved_case_preserve = case_preserve;
330 saved_short_case_preserve = short_case_preserve;
333 case_sensitive = True;
334 case_preserve = True;
335 short_case_preserve = True;
338 /****************************************************************************
339 Restore case semantics.
340 ****************************************************************************/
342 static void restore_case_semantics(uint32 file_attributes)
344 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
347 case_sensitive = saved_case_sensitive;
348 case_preserve = saved_case_preserve;
349 short_case_preserve = saved_short_case_preserve;
352 /****************************************************************************
353 Utility function to map create disposition.
354 ****************************************************************************/
356 static int map_create_disposition( uint32 create_disposition)
360 switch( create_disposition ) {
362 /* create if not exist, fail if exist */
363 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL);
366 case FILE_OVERWRITE_IF:
367 /* create if not exist, trunc if exist */
368 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
371 /* fail if not exist, open if exists */
372 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN);
375 /* create if not exist, open if exists */
376 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_OPEN);
379 /* fail if not exist, truncate if exists */
380 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
383 DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
384 create_disposition ));
388 DEBUG(10,("map_create_disposition: Mapped create_disposition %lx to %x\n",
389 (unsigned long)create_disposition, ret ));
394 /****************************************************************************
395 Utility function to map share modes.
396 ****************************************************************************/
398 static int map_share_mode( BOOL *pstat_open_only, char *fname,
399 uint32 desired_access, uint32 share_access, uint32 file_attributes)
401 int smb_open_mode = -1;
403 *pstat_open_only = False;
405 switch( desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
407 smb_open_mode = DOS_OPEN_RDONLY;
409 case FILE_WRITE_DATA:
410 case FILE_APPEND_DATA:
411 case FILE_WRITE_DATA|FILE_APPEND_DATA:
412 smb_open_mode = DOS_OPEN_WRONLY;
414 case FILE_READ_DATA|FILE_WRITE_DATA:
415 case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
416 case FILE_READ_DATA|FILE_APPEND_DATA:
417 smb_open_mode = DOS_OPEN_RDWR;
422 * NB. For DELETE_ACCESS we should really check the
423 * directory permissions, as that is what controls
424 * delete, and for WRITE_DAC_ACCESS we should really
425 * check the ownership, as that is what controls the
426 * chmod. Note that this is *NOT* a security hole (this
427 * note is for you, Andrew) as we are not *allowing*
428 * the access at this point, the actual unlink or
429 * chown or chmod call would do this. We are just helping
430 * clients out by telling them if they have a hope
431 * of any of this succeeding. POSIX acls may still
432 * deny the real call. JRA.
435 if (smb_open_mode == -1) {
436 if(desired_access == WRITE_DAC_ACCESS || desired_access == READ_CONTROL_ACCESS)
437 *pstat_open_only = True;
439 if(desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|
440 FILE_EXECUTE|FILE_READ_ATTRIBUTES|
441 FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
442 FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS))
443 smb_open_mode = DOS_OPEN_RDONLY;
445 DEBUG(0,("map_share_mode: Incorrect value %lx for desired_access to file %s\n",
446 (unsigned long)desired_access, fname));
452 * Set the special bit that means allow share delete.
453 * This is held outside the normal share mode bits at 1<<15.
457 if(share_access & FILE_SHARE_DELETE)
458 smb_open_mode |= ALLOW_SHARE_DELETE;
460 /* Add in the requested share mode. */
461 switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
462 case FILE_SHARE_READ:
463 smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
465 case FILE_SHARE_WRITE:
466 smb_open_mode |= SET_DENY_MODE(DENY_READ);
468 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
469 smb_open_mode |= SET_DENY_MODE(DENY_NONE);
471 case FILE_SHARE_NONE:
472 smb_open_mode |= SET_DENY_MODE(DENY_ALL);
477 * Handle an O_SYNC request.
480 if(file_attributes & FILE_FLAG_WRITE_THROUGH)
481 smb_open_mode |= FILE_SYNC_OPENMODE;
483 DEBUG(10,("map_share_mode: Mapped desired access %lx, share access %lx, file attributes %lx \
484 to open_mode %x\n", (unsigned long)desired_access, (unsigned long)share_access,
485 (unsigned long)file_attributes, smb_open_mode ));
487 return smb_open_mode;
491 * This is a *disgusting* hack.
492 * This is *so* bad that even I'm embarrassed (and I
493 * have no shame). Here's the deal :
494 * Until we get the correct SPOOLSS code into smbd
495 * then when we're running with NT SMB support then
496 * NT makes this call with a level of zero, and then
497 * immediately follows it with an open request to
498 * the \\SRVSVC pipe. If we allow that open to
499 * succeed then NT barfs when it cannot open the
500 * \\SPOOLSS pipe immediately after and continually
501 * whines saying "Printer name is invalid" forever
502 * after. If we cause *JUST THIS NEXT OPEN* of \\SRVSVC
503 * to fail, then NT downgrades to using the downlevel code
504 * and everything works as well as before. I hate
505 * myself for adding this code.... JRA.
507 * The HACK_FAIL_TIME define allows only a 2
508 * second window for this to occur, just in
512 static BOOL fail_next_srvsvc = False;
513 static time_t fail_time;
514 #define HACK_FAIL_TIME 2 /* In seconds. */
516 void fail_next_srvsvc_open(void)
518 /* Check client is WinNT proper; Win2K doesn't like Jeremy's hack - matty */
519 if (get_remote_arch() != RA_WINNT)
522 fail_next_srvsvc = True;
523 fail_time = time(NULL);
524 DEBUG(10,("fail_next_srvsvc_open: setting up timeout close of \\srvsvc pipe for print fix.\n"));
528 * HACK alert.... see above - JRA.
531 BOOL should_fail_next_srvsvc_open(const char *pipename)
534 DEBUG(10,("should_fail_next_srvsvc_open: fail = %d, pipe = %s\n",
535 (int)fail_next_srvsvc, pipename));
537 if(fail_next_srvsvc && (time(NULL) > fail_time + HACK_FAIL_TIME)) {
538 fail_next_srvsvc = False;
539 fail_time = (time_t)0;
540 DEBUG(10,("should_fail_next_srvsvc_open: End of timeout close of \\srvsvc pipe for print fix.\n"));
543 if(fail_next_srvsvc && strequal(pipename, "srvsvc")) {
544 fail_next_srvsvc = False;
545 DEBUG(10,("should_fail_next_srvsvc_open: Deliberately failing open of \\srvsvc pipe for print fix.\n"));
552 /****************************************************************************
553 Reply to an NT create and X call on a pipe.
554 ****************************************************************************/
555 static int nt_open_pipe(char *fname, connection_struct *conn,
556 char *inbuf, char *outbuf, int *ppnum)
558 pipes_struct *p = NULL;
560 uint16 vuid = SVAL(inbuf, smb_uid);
563 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
565 /* See if it is one we want to handle. */
566 for( i = 0; known_nt_pipes[i]; i++ )
567 if( strequal(fname,known_nt_pipes[i]))
570 if ( known_nt_pipes[i] == NULL )
571 return(ERROR(ERRSRV,ERRaccess));
573 /* Strip \\ off the name. */
576 if(should_fail_next_srvsvc_open(fname))
577 return (ERROR(ERRSRV,ERRaccess));
579 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
581 p = open_rpc_pipe_p(fname, conn, vuid);
583 return(ERROR(ERRSRV,ERRnofids));
590 /****************************************************************************
591 Reply to an NT create and X call for pipes.
592 ****************************************************************************/
594 static int do_ntcreate_pipe_open(connection_struct *conn,
595 char *inbuf,char *outbuf,int length,int bufsize)
601 uint32 fname_len = MIN(((uint32)SVAL(inbuf,smb_ntcreate_NameLength)),
602 ((uint32)sizeof(fname)-1));
604 get_filename(fname, inbuf, smb_buf(inbuf)-inbuf,
605 smb_buflen(inbuf),fname_len);
606 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
610 * Deal with pipe return.
613 set_message(outbuf,34,0,True);
615 p = outbuf + smb_vwv2;
619 SIVAL(p,0,FILE_WAS_OPENED);
622 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
625 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
627 SSVAL(p,2, 0x5FF); /* ? */
629 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
631 return chain_reply(inbuf,outbuf,length,bufsize);
634 /****************************************************************************
635 Reply to an NT create and X call.
636 ****************************************************************************/
638 int reply_ntcreate_and_X(connection_struct *conn,
639 char *inbuf,char *outbuf,int length,int bufsize)
642 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
643 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
644 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
645 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
646 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
647 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
648 uint32 fname_len = MIN(((uint32)SVAL(inbuf,smb_ntcreate_NameLength)),
649 ((uint32)sizeof(fname)-1));
650 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
653 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
654 /* Breakout the oplock request bits so we can set the
655 reply bits separately. */
656 int oplock_request = 0;
659 SMB_OFF_T file_len = 0;
660 SMB_STRUCT_STAT sbuf;
662 BOOL bad_path = False;
663 files_struct *fsp=NULL;
665 BOOL stat_open_only = False;
667 /* If it's an IPC, use the pipe handler. */
670 if (lp_nt_pipe_support())
671 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
673 return(ERROR(ERRDOS,ERRbadaccess));
678 * We need to construct the open_and_X ofun value from the
679 * NT values, as that's what our code is structured to accept.
682 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
683 return(ERROR(ERRDOS,ERRbadaccess));
689 if(root_dir_fid != 0) {
691 * This filename is relative to a directory fid.
693 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
697 return(ERROR(ERRDOS,ERRbadfid));
699 if(!dir_fsp->is_directory) {
701 * Check to see if this is a mac fork of some kind.
704 get_filename(&fname[0], inbuf, smb_buf(inbuf)-inbuf,
705 smb_buflen(inbuf),fname_len);
707 if( fname[0] == ':') {
708 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
709 return(ERROR(0, 0xc0000000|NT_STATUS_OBJECT_PATH_NOT_FOUND));
711 return(ERROR(ERRDOS,ERRbadfid));
715 * Copy in the base directory name.
718 pstrcpy( fname, dir_fsp->fsp_name );
719 dir_name_len = strlen(fname);
722 * Ensure it ends in a '\'.
725 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
726 pstrcat(fname, "\\");
731 * This next calculation can refuse a correct filename if we're dealing
732 * with the Win2k unicode bug, but that would be rare. JRA.
735 if(fname_len + dir_name_len >= sizeof(pstring))
736 return(ERROR(ERRSRV,ERRfilespecs));
738 get_filename(&fname[dir_name_len], inbuf, smb_buf(inbuf)-inbuf,
739 smb_buflen(inbuf),fname_len);
743 get_filename(fname, inbuf, smb_buf(inbuf)-inbuf,
744 smb_buflen(inbuf),fname_len);
748 * Now contruct the smb_open_mode value from the filename,
749 * desired access and the share access.
751 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
753 if((smb_open_mode = map_share_mode(&stat_open_only, fname, desired_access,
755 file_attributes)) == -1)
756 return(ERROR(ERRDOS,ERRbadaccess));
758 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
759 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
762 * Ordinary file or directory.
766 * Check if POSIX semantics are wanted.
769 set_posix_case_semantics(file_attributes);
771 unix_convert(fname,conn,0,&bad_path,NULL);
773 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
776 * If it's a request for a directory open, deal with it separately.
779 if(create_options & FILE_DIRECTORY_FILE) {
782 fsp = open_directory(conn, fname, smb_ofun, unixmode, &smb_action);
784 restore_case_semantics(file_attributes);
787 return(UNIXERROR(ERRDOS,ERRnoaccess));
791 * Ordinary file case.
794 /* NB. We have a potential bug here. If we
795 * cause an oplock break to ourselves, then we
796 * could end up processing filename related
797 * SMB requests whilst we await the oplock
798 * break response. As we may have changed the
799 * filename case semantics to be POSIX-like,
800 * this could mean a filename request could
801 * fail when it should succeed. This is a rare
802 * condition, but eventually we must arrange
803 * to restore the correct case semantics
804 * before issuing an oplock break request to
805 * our client. JRA. */
807 fsp = open_file_shared(conn,fname,smb_open_mode,
808 smb_ofun,unixmode, oplock_request,&rmode,&smb_action);
811 /* We cheat here. There are two cases we
812 * care about. One is a directory rename,
813 * where the NT client will attempt to
814 * open the source directory for
815 * DELETE access. Note that when the
816 * NT client does this it does *not*
817 * set the directory bit in the
818 * request packet. This is translated
819 * into a read/write open
820 * request. POSIX states that any open
821 * for write request on a directory
822 * will generate an EISDIR error, so
823 * we can catch this here and open a
824 * pseudo handle that is flagged as a
825 * directory. The second is an open
826 * for a permissions read only, which
827 * we handle in the open_file_stat case. JRA.
830 if(errno == EISDIR) {
833 * Fail the open if it was explicitly a non-directory file.
836 if (create_options & FILE_NON_DIRECTORY_FILE) {
837 restore_case_semantics(file_attributes);
838 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
839 return(ERROR(0, 0xc0000000|NT_STATUS_FILE_IS_A_DIRECTORY));
843 fsp = open_directory(conn, fname, smb_ofun, unixmode, &smb_action);
846 restore_case_semantics(file_attributes);
847 return(UNIXERROR(ERRDOS,ERRnoaccess));
850 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
852 } else if (errno == EACCES && stat_open_only) {
855 * We couldn't open normally and all we want
856 * are the permissions. Try and do a stat open.
861 fsp = open_file_stat(conn,fname,smb_open_mode,&sbuf,&smb_action);
864 restore_case_semantics(file_attributes);
865 return(UNIXERROR(ERRDOS,ERRnoaccess));
870 if((errno == ENOENT) && bad_path) {
871 unix_ERR_class = ERRDOS;
872 unix_ERR_code = ERRbadpath;
875 restore_case_semantics(file_attributes);
877 return(UNIXERROR(ERRDOS,ERRnoaccess));
882 if(fsp->is_directory) {
883 if(conn->vfs_ops.stat(dos_to_unix(fsp->fsp_name, False), &sbuf) != 0) {
884 close_file(fsp,True);
885 restore_case_semantics(file_attributes);
886 return(ERROR(ERRDOS,ERRnoaccess));
889 if (conn->vfs_ops.fstat(fsp->fd,&sbuf) != 0) {
890 close_file(fsp,False);
891 restore_case_semantics(file_attributes);
892 return(ERROR(ERRDOS,ERRnoaccess));
896 restore_case_semantics(file_attributes);
898 file_len = sbuf.st_size;
899 fmode = dos_mode(conn,fname,&sbuf);
901 fmode = FILE_ATTRIBUTE_NORMAL;
902 if (!fsp->is_directory && (fmode & aDIR)) {
903 close_file(fsp,False);
904 return(ERROR(ERRDOS,ERRnoaccess));
908 * If the caller set the extended oplock request bit
909 * and we granted one (by whatever means) - set the
910 * correct bit for extended oplock reply.
913 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
914 smb_action |= EXTENDED_OPLOCK_GRANTED;
916 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
917 smb_action |= EXTENDED_OPLOCK_GRANTED;
919 set_message(outbuf,34,0,True);
921 p = outbuf + smb_vwv2;
924 * Currently as we don't support level II oplocks we just report
925 * exclusive & batch here.
928 if (smb_action & EXTENDED_OPLOCK_GRANTED)
929 SCVAL(p,0, BATCH_OPLOCK_RETURN);
930 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
931 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
933 SCVAL(p,0,NO_OPLOCK_RETURN);
936 SSVAL(p,0,fsp->fnum);
938 SIVAL(p,0,smb_action);
942 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
944 put_long_date(p,sbuf.st_atime); /* access time */
946 put_long_date(p,sbuf.st_mtime); /* write time */
948 put_long_date(p,sbuf.st_mtime); /* change time */
950 SIVAL(p,0,fmode); /* File Attributes. */
952 SOFF_T(p, 0, file_len);
954 SOFF_T(p,0,file_len);
956 SCVAL(p,0,fsp->is_directory ? 1 : 0);
958 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
960 return chain_reply(inbuf,outbuf,length,bufsize);
963 /****************************************************************************
964 Reply to a NT_TRANSACT_CREATE call to open a pipe.
965 ****************************************************************************/
967 static int do_nt_transact_create_pipe( connection_struct *conn,
968 char *inbuf, char *outbuf, int length,
969 int bufsize, char **ppsetup, char **ppparams,
974 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
975 char *params = *ppparams;
981 * Ensure minimum number of parameters sent.
984 if(total_parameter_count < 54) {
985 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
986 return(ERROR(ERRDOS,ERRbadaccess));
989 fname_len = MIN(((uint32)IVAL(params,44)),((uint32)sizeof(fname)-1));
991 get_filename_transact(&fname[0], params, 53,
992 total_parameter_count - 53 - fname_len, fname_len);
994 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
997 /* Realloc the size of parameters and data we will return */
998 params = *ppparams = Realloc(*ppparams, 69);
1000 return(ERROR(ERRDOS,ERRnomem));
1002 memset((char *)params,'\0',69);
1005 SCVAL(p,0,NO_OPLOCK_RETURN);
1010 SIVAL(p,0,FILE_WAS_OPENED);
1014 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1017 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1019 SSVAL(p,2, 0x5FF); /* ? */
1021 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1023 /* Send the required number of replies */
1024 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 69, *ppdata, 0);
1029 /****************************************************************************
1030 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1031 ****************************************************************************/
1033 static int call_nt_transact_create(connection_struct *conn,
1034 char *inbuf, char *outbuf, int length,
1035 int bufsize, char **ppsetup, char **ppparams,
1039 char *params = *ppparams;
1040 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
1041 /* Breakout the oplock request bits so we can set the
1042 reply bits separately. */
1043 int oplock_request = 0;
1045 int fmode=0,rmode=0;
1046 SMB_OFF_T file_len = 0;
1047 SMB_STRUCT_STAT sbuf;
1049 BOOL bad_path = False;
1050 files_struct *fsp = NULL;
1052 BOOL stat_open_only = False;
1054 uint32 desired_access;
1055 uint32 file_attributes;
1056 uint32 share_access;
1057 uint32 create_disposition;
1058 uint32 create_options;
1060 uint16 root_dir_fid;
1065 DEBUG(5,("call_nt_transact_create\n"));
1068 * If it's an IPC, use the pipe handler.
1072 if (lp_nt_pipe_support())
1073 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1074 bufsize, ppsetup, ppparams, ppdata);
1076 return(ERROR(ERRDOS,ERRbadaccess));
1080 * Ensure minimum number of parameters sent.
1083 if(total_parameter_count < 54) {
1084 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1085 return(ERROR(ERRDOS,ERRbadaccess));
1088 flags = IVAL(params,0);
1089 desired_access = IVAL(params,8);
1090 file_attributes = IVAL(params,20);
1091 share_access = IVAL(params,24);
1092 create_disposition = IVAL(params,28);
1093 create_options = IVAL(params,32);
1094 fname_len = MIN(((uint32)IVAL(params,44)),((uint32)sizeof(fname)-1));
1095 root_dir_fid = (uint16)IVAL(params,4);
1096 smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
1099 * We need to construct the open_and_X ofun value from the
1100 * NT values, as that's what our code is structured to accept.
1103 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
1104 return(ERROR(ERRDOS,ERRbadmem));
1107 * Get the file name.
1110 if(root_dir_fid != 0) {
1112 * This filename is relative to a directory fid.
1115 files_struct *dir_fsp = file_fsp(params,4);
1116 size_t dir_name_len;
1119 return(ERROR(ERRDOS,ERRbadfid));
1121 if(!dir_fsp->is_directory) {
1123 * Check to see if this is a mac fork of some kind.
1126 get_filename_transact(&fname[0], params, 53,
1127 total_parameter_count - 53 - fname_len, fname_len);
1129 if( fname[0] == ':') {
1130 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
1131 return(ERROR(0, 0xc0000000|NT_STATUS_OBJECT_PATH_NOT_FOUND));
1134 return(ERROR(ERRDOS,ERRbadfid));
1138 * Copy in the base directory name.
1141 pstrcpy( fname, dir_fsp->fsp_name );
1142 dir_name_len = strlen(fname);
1145 * Ensure it ends in a '\'.
1148 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1149 pstrcat(fname, "\\");
1154 * This next calculation can refuse a correct filename if we're dealing
1155 * with the Win2k unicode bug, but that would be rare. JRA.
1158 if(fname_len + dir_name_len >= sizeof(pstring))
1159 return(ERROR(ERRSRV,ERRfilespecs));
1161 get_filename_transact(&fname[dir_name_len], params, 53,
1162 total_parameter_count - 53 - fname_len, fname_len);
1165 get_filename_transact(&fname[0], params, 53,
1166 total_parameter_count - 53 - fname_len, fname_len);
1170 * Now contruct the smb_open_mode value from the desired access
1171 * and the share access.
1174 if((smb_open_mode = map_share_mode( &stat_open_only, fname, desired_access,
1175 share_access, file_attributes)) == -1)
1176 return(ERROR(ERRDOS,ERRbadaccess));
1178 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1179 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1182 * Check if POSIX semantics are wanted.
1185 set_posix_case_semantics(file_attributes);
1187 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1189 unix_convert(fname,conn,0,&bad_path,NULL);
1191 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
1194 * If it's a request for a directory open, deal with it separately.
1197 if(create_options & FILE_DIRECTORY_FILE) {
1202 * We will get a create directory here if the Win32
1203 * app specified a security descriptor in the
1204 * CreateDirectory() call.
1207 fsp = open_directory(conn, fname, smb_ofun, unixmode, &smb_action);
1210 restore_case_semantics(file_attributes);
1211 return(UNIXERROR(ERRDOS,ERRnoaccess));
1214 if(conn->vfs_ops.stat(dos_to_unix(fsp->fsp_name, False),
1216 close_file(fsp,True);
1217 restore_case_semantics(file_attributes);
1218 return(ERROR(ERRDOS,ERRnoaccess));
1224 * Ordinary file case.
1227 fsp = open_file_shared(conn,fname,smb_open_mode,smb_ofun,unixmode,
1228 oplock_request,&rmode,&smb_action);
1232 if(errno == EISDIR) {
1235 * Fail the open if it was explicitly a non-directory file.
1238 if (create_options & FILE_NON_DIRECTORY_FILE) {
1239 restore_case_semantics(file_attributes);
1240 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
1241 return(ERROR(0, 0xc0000000|NT_STATUS_FILE_IS_A_DIRECTORY));
1245 fsp = open_directory(conn, fname, smb_ofun, unixmode, &smb_action);
1248 restore_case_semantics(file_attributes);
1249 return(UNIXERROR(ERRDOS,ERRnoaccess));
1252 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
1254 } else if (errno == EACCES && stat_open_only) {
1258 * We couldn't open normally and all we want
1259 * are the permissions. Try and do a stat open.
1264 fsp = open_file_stat(conn,fname,smb_open_mode,&sbuf,&smb_action);
1267 restore_case_semantics(file_attributes);
1268 return(UNIXERROR(ERRDOS,ERRnoaccess));
1272 if((errno == ENOENT) && bad_path) {
1273 unix_ERR_class = ERRDOS;
1274 unix_ERR_code = ERRbadpath;
1277 restore_case_semantics(file_attributes);
1279 return(UNIXERROR(ERRDOS,ERRnoaccess));
1283 if(fsp->is_directory) {
1284 if(conn->vfs_ops.stat(dos_to_unix(fsp->fsp_name,False), &sbuf) != 0) {
1285 close_file(fsp,True);
1286 restore_case_semantics(file_attributes);
1287 return(ERROR(ERRDOS,ERRnoaccess));
1290 if (!fsp->stat_open && conn->vfs_ops.fstat(fsp->fd,&sbuf) != 0) {
1291 close_file(fsp,False);
1292 restore_case_semantics(file_attributes);
1293 return(ERROR(ERRDOS,ERRnoaccess));
1297 file_len = sbuf.st_size;
1298 fmode = dos_mode(conn,fname,&sbuf);
1300 fmode = FILE_ATTRIBUTE_NORMAL;
1303 close_file(fsp,False);
1304 restore_case_semantics(file_attributes);
1305 return(ERROR(ERRDOS,ERRnoaccess));
1309 * If the caller set the extended oplock request bit
1310 * and we granted one (by whatever means) - set the
1311 * correct bit for extended oplock reply.
1314 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1315 smb_action |= EXTENDED_OPLOCK_GRANTED;
1317 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1318 smb_action |= EXTENDED_OPLOCK_GRANTED;
1321 restore_case_semantics(file_attributes);
1323 /* Realloc the size of parameters and data we will return */
1324 params = *ppparams = Realloc(*ppparams, 69);
1326 return(ERROR(ERRDOS,ERRnomem));
1328 memset((char *)params,'\0',69);
1331 if (smb_action & EXTENDED_OPLOCK_GRANTED)
1332 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1333 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1334 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1336 SCVAL(p,0,NO_OPLOCK_RETURN);
1339 SSVAL(p,0,fsp->fnum);
1341 SIVAL(p,0,smb_action);
1345 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
1347 put_long_date(p,sbuf.st_atime); /* access time */
1349 put_long_date(p,sbuf.st_mtime); /* write time */
1351 put_long_date(p,sbuf.st_mtime); /* change time */
1353 SIVAL(p,0,fmode); /* File Attributes. */
1355 SOFF_T(p,0,file_len);
1357 SOFF_T(p,0,file_len);
1359 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1361 /* Send the required number of replies */
1362 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 69, *ppdata, 0);
1367 /****************************************************************************
1368 Reply to a NT CANCEL request.
1369 ****************************************************************************/
1370 int reply_ntcancel(connection_struct *conn,
1371 char *inbuf,char *outbuf,int length,int bufsize)
1374 * Go through and cancel any pending change notifies.
1377 int mid = SVAL(inbuf,smb_mid);
1378 remove_pending_change_notify_requests_by_mid(mid);
1379 remove_pending_lock_requests_by_mid(mid);
1381 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1386 /****************************************************************************
1387 Reply to an unsolicited SMBNTtranss - just ignore it!
1388 ****************************************************************************/
1389 int reply_nttranss(connection_struct *conn,
1390 char *inbuf,char *outbuf,int length,int bufsize)
1392 DEBUG(4,("Ignoring nttranss of length %d\n",length));
1396 /****************************************************************************
1397 Reply to an NT transact rename command.
1398 ****************************************************************************/
1400 static int call_nt_transact_rename(connection_struct *conn,
1401 char *inbuf, char *outbuf, int length,
1403 char **ppsetup, char **ppparams, char **ppdata)
1405 char *params = *ppparams;
1407 files_struct *fsp = file_fsp(params, 0);
1408 BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1409 uint32 fname_len = MIN((((uint32)IVAL(inbuf,smb_nt_TotalParameterCount)-4)),
1410 ((uint32)sizeof(new_name)-1));
1413 CHECK_FSP(fsp, conn);
1414 StrnCpy(new_name,params+4,fname_len);
1415 new_name[fname_len] = '\0';
1417 outsize = rename_internals(conn, inbuf, outbuf, fsp->fsp_name,
1418 new_name, replace_if_exists);
1421 * Rename was successful.
1423 send_nt_replies(inbuf, outbuf, bufsize, 0, NULL, 0, NULL, 0);
1425 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1426 fsp->fsp_name, new_name));
1434 /****************************************************************************
1435 This is the structure to keep the information needed to
1436 determine if a directory has changed.
1437 *****************************************************************************/
1440 time_t modify_time; /* Info from the directory we're monitoring. */
1441 time_t status_time; /* Info from the directory we're monitoring. */
1442 time_t total_time; /* Total time of all directory entries - don't care if it wraps. */
1443 unsigned int num_entries; /* Zero or the number of files in the directory. */
1446 /****************************************************************************
1447 This is the structure to queue to implement NT change
1448 notify. It consists of smb_size bytes stored from the
1449 transact command (to keep the mid, tid etc around).
1450 Plus the fid to examine and the time to check next.
1451 *****************************************************************************/
1454 ubi_slNode msg_next;
1456 connection_struct *conn;
1458 time_t next_check_time;
1459 change_hash_data change_data;
1460 char request_buf[smb_size];
1461 } change_notify_buf;
1463 static ubi_slList change_notify_queue = { NULL, (ubi_slNodePtr)&change_notify_queue, 0};
1465 /****************************************************************************
1466 Setup the common parts of the return packet and send it.
1467 *****************************************************************************/
1469 static void change_notify_reply_packet(char *inbuf, int error_class, uint32 error_code)
1471 char outbuf[smb_size+38];
1473 memset(outbuf, '\0', sizeof(outbuf));
1474 construct_reply_common(inbuf, outbuf);
1477 * If we're returning a 'too much in the directory changed' we need to
1478 * set this is an NT error status flags. If we don't then the (probably
1479 * untested) code in the NT redirector has a bug in that it doesn't re-issue
1480 * the change notify.... Ah - I *love* it when I get so deeply into this I
1481 * can even determine how MS failed to test stuff and why.... :-). JRA.
1484 if(error_class == 0) /* NT Error. */
1485 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1487 ERROR(error_class,error_code);
1490 * Seems NT needs a transact command with an error code
1491 * in it. This is a longer packet than a simple error.
1493 set_message(outbuf,18,0,False);
1495 send_smb(smbd_server_fd(),outbuf);
1498 /****************************************************************************
1499 Create the hash we will use to determine if the contents changed.
1500 *****************************************************************************/
1502 static BOOL create_directory_notify_hash( change_notify_buf *cnbp, change_hash_data *change_data)
1505 files_struct *fsp = cnbp->fsp;
1507 memset((char *)change_data, '\0', sizeof(change_data));
1510 * Store the current timestamp on the directory we are monitoring.
1513 if(dos_stat(fsp->fsp_name, &st) < 0) {
1514 DEBUG(0,("create_directory_notify_hash: Unable to stat name = %s. \
1515 Error was %s\n", fsp->fsp_name, strerror(errno) ));
1519 change_data->modify_time = st.st_mtime;
1520 change_data->status_time = st.st_ctime;
1523 * If we are to watch for changes that are only stored
1524 * in inodes of files, not in the directory inode, we must
1525 * scan the directory and produce a unique identifier with
1526 * which we can determine if anything changed. We use the
1527 * modify and change times from all the files in the
1528 * directory, added together (ignoring wrapping if it's
1529 * larger than the max time_t value).
1532 if(cnbp->flags & (FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_LAST_WRITE)) {
1536 size_t remaining_len;
1537 size_t fullname_len;
1538 void *dp = OpenDir(cnbp->conn, fsp->fsp_name, True);
1541 DEBUG(0,("create_directory_notify_hash: Unable to open directory = %s. \
1542 Error was %s\n", fsp->fsp_name, strerror(errno) ));
1546 change_data->num_entries = 0;
1548 pstrcpy(full_name, fsp->fsp_name);
1549 pstrcat(full_name, "/");
1551 fullname_len = strlen(full_name);
1552 remaining_len = sizeof(full_name) - fullname_len - 1;
1553 p = &full_name[fullname_len];
1555 while ((fname = ReadDirName(dp))) {
1556 if(strequal(fname, ".") || strequal(fname, ".."))
1559 change_data->num_entries++;
1560 safe_strcpy( p, fname, remaining_len);
1562 memset(&st, '\0', sizeof(st));
1565 * Do the stat - but ignore errors.
1568 if(dos_stat(full_name, &st) < 0) {
1569 DEBUG(5,("create_directory_notify_hash: Unable to stat content file = %s. \
1570 Error was %s\n", fsp->fsp_name, strerror(errno) ));
1572 change_data->total_time += (st.st_mtime + st.st_ctime);
1581 /****************************************************************************
1582 Delete entries by fnum from the change notify pending queue.
1583 *****************************************************************************/
1585 void remove_pending_change_notify_requests_by_fid(files_struct *fsp)
1587 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1588 change_notify_buf *prev = NULL;
1590 while(cnbp != NULL) {
1591 if(cnbp->fsp->fnum == fsp->fnum) {
1592 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1593 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1598 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1602 /****************************************************************************
1603 Delete entries by mid from the change notify pending queue. Always send reply.
1604 *****************************************************************************/
1606 static void remove_pending_change_notify_requests_by_mid(int mid)
1608 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1609 change_notify_buf *prev = NULL;
1611 while(cnbp != NULL) {
1612 if(SVAL(cnbp->request_buf,smb_mid) == mid) {
1613 change_notify_reply_packet(cnbp->request_buf,0,0xC0000000 |NT_STATUS_CANCELLED);
1614 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1615 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1620 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1624 /****************************************************************************
1625 Delete entries by filename and cnum from the change notify pending queue.
1627 *****************************************************************************/
1629 void remove_pending_change_notify_requests_by_filename(files_struct *fsp)
1631 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1632 change_notify_buf *prev = NULL;
1634 while(cnbp != NULL) {
1636 * We know it refers to the same directory if the connection number and
1637 * the filename are identical.
1639 if((cnbp->fsp->conn == fsp->conn) && strequal(cnbp->fsp->fsp_name,fsp->fsp_name)) {
1640 change_notify_reply_packet(cnbp->request_buf,0,0xC0000000 |NT_STATUS_CANCELLED);
1641 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1642 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1647 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1651 /****************************************************************************
1652 Process the change notify queue. Note that this is only called as root.
1653 Returns True if there are still outstanding change notify requests on the
1655 *****************************************************************************/
1657 BOOL process_pending_change_notify_queue(time_t t)
1659 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1660 change_notify_buf *prev = NULL;
1665 if(cnbp->next_check_time >= t)
1669 * It's time to check. Go through the queue and see if
1670 * the timestamps changed.
1673 while((cnbp != NULL) && (cnbp->next_check_time <= t)) {
1674 change_hash_data change_data;
1675 connection_struct *conn = cnbp->conn;
1676 uint16 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
1677 SVAL(cnbp->request_buf,smb_uid);
1679 ZERO_STRUCT(change_data);
1682 * Ensure we don't have any old chain_fsp values
1683 * sitting around....
1688 if(!become_user(conn,vuid)) {
1689 DEBUG(0,("process_pending_change_notify_queue: Unable to become user vuid=%d.\n",
1692 * Remove the entry and return an error to the client.
1694 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1695 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1696 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1700 if(!become_service(conn,True)) {
1701 DEBUG(0,("process_pending_change_notify_queue: Unable to become service Error was %s.\n", strerror(errno) ));
1703 * Remove the entry and return an error to the client.
1705 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1706 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1707 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1712 if(!create_directory_notify_hash( cnbp, &change_data)) {
1713 DEBUG(0,("process_pending_change_notify_queue: Unable to create change data for \
1714 directory %s\n", cnbp->fsp->fsp_name ));
1716 * Remove the entry and return an error to the client.
1718 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1719 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1720 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1725 if(memcmp( (char *)&cnbp->change_data, (char *)&change_data, sizeof(change_data))) {
1727 * Remove the entry and return a change notify to the client.
1729 DEBUG(5,("process_pending_change_notify_queue: directory name = %s changed.\n",
1730 cnbp->fsp->fsp_name ));
1731 change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_NOTIFY_ENUM_DIR);
1732 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1733 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1741 * Move to the next in the list.
1744 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1747 return (cnbp != NULL);
1750 /****************************************************************************
1751 Return true if there are pending change notifies.
1752 ****************************************************************************/
1754 BOOL change_notifies_pending(void)
1756 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1757 return (cnbp != NULL);
1760 /****************************************************************************
1761 Reply to a notify change - queue the request and
1762 don't allow a directory to be opened.
1763 ****************************************************************************/
1765 static int call_nt_transact_notify_change(connection_struct *conn,
1766 char *inbuf, char *outbuf, int length,
1769 char **ppparams, char **ppdata)
1771 char *setup = *ppsetup;
1773 change_notify_buf *cnbp;
1775 fsp = file_fsp(setup,4);
1777 DEBUG(3,("call_nt_transact_notify_change\n"));
1780 return(ERROR(ERRDOS,ERRbadfid));
1782 if((!fsp->is_directory) || (conn != fsp->conn))
1783 return(ERROR(ERRDOS,ERRbadfid));
1786 * Now queue an entry on the notify change stack. We timestamp
1787 * the entry we are adding so that we know when to scan next.
1788 * We only need to save smb_size bytes from this incoming packet
1789 * as we will always by returning a 'read the directory yourself'
1793 if((cnbp = (change_notify_buf *)malloc(sizeof(change_notify_buf))) == NULL) {
1794 DEBUG(0,("call_nt_transact_notify_change: malloc fail !\n" ));
1798 memset((char *)cnbp, '\0', sizeof(change_notify_buf));
1800 memcpy(cnbp->request_buf, inbuf, smb_size);
1803 cnbp->next_check_time = time(NULL) + lp_change_notify_timeout();
1804 cnbp->flags = IVAL(setup, 0);
1806 if(!create_directory_notify_hash( cnbp, &cnbp->change_data )) {
1808 return(UNIXERROR(ERRDOS,ERRbadfid));
1812 * Adding to the tail enables us to check only
1813 * the head when scanning for change, as this entry
1814 * is forced to have the first timeout expiration.
1817 ubi_slAddTail(&change_notify_queue, cnbp);
1819 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1820 name = %s\n", fsp->fsp_name ));
1825 /****************************************************************************
1826 Reply to query a security descriptor from an fsp. If it succeeds it allocates
1827 the space for the return elements and returns True.
1828 ****************************************************************************/
1830 static size_t get_nt_acl(files_struct *fsp, SEC_DESC **ppdesc)
1832 SMB_STRUCT_STAT sbuf;
1835 if(fsp->is_directory || fsp->fd == -1) {
1836 if(dos_stat(fsp->fsp_name, &sbuf) != 0) {
1840 if(fsp->conn->vfs_ops.fstat(fsp->fd,&sbuf) != 0) {
1845 if(fsp->is_directory) {
1847 * For directory ACLs we also add in the inherited permissions
1848 * ACE entries. These are the permissions a file would get when
1849 * being created in the directory.
1851 mode = unix_mode( fsp->conn, FILE_ATTRIBUTE_ARCHIVE, fsp->fsp_name);
1855 mode = sbuf.st_mode;
1857 return convertperms_unix_to_sd(&sbuf, fsp->is_directory, mode, ppdesc);
1860 /****************************************************************************
1861 Reply to query a security descriptor - currently this is not implemented (it
1862 is planned to be though). Right now it just returns the same thing NT would
1863 when queried on a FAT filesystem. JRA.
1864 ****************************************************************************/
1866 static int call_nt_transact_query_security_desc(connection_struct *conn,
1867 char *inbuf, char *outbuf,
1868 int length, int bufsize,
1869 char **ppsetup, char **ppparams, char **ppdata)
1871 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1872 char *params = *ppparams;
1873 char *data = *ppdata;
1875 SEC_DESC *psd = NULL;
1876 size_t sec_desc_size;
1878 files_struct *fsp = file_fsp(params,0);
1881 return(ERROR(ERRDOS,ERRbadfid));
1883 DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1885 params = *ppparams = Realloc(*ppparams, 4);
1887 return(ERROR(ERRDOS,ERRnomem));
1890 * Get the permissions to return.
1893 if((sec_desc_size = get_nt_acl(fsp, &psd)) == 0)
1894 return(UNIXERROR(ERRDOS,ERRnoaccess));
1896 DEBUG(3,("call_nt_transact_query_security_desc: sec_desc_size = %d.\n",(int)sec_desc_size));
1898 SIVAL(params,0,(uint32)sec_desc_size);
1900 if(max_data_count < sec_desc_size) {
1905 send_nt_replies(inbuf, outbuf, bufsize, 0xC0000000|NT_STATUS_BUFFER_TOO_SMALL,
1906 params, 4, *ppdata, 0);
1911 * Allocate the data we will point this at.
1914 data = *ppdata = Realloc(*ppdata, sec_desc_size);
1918 return(ERROR(ERRDOS,ERRnomem));
1921 memset(data, '\0', sec_desc_size);
1924 * Init the parse struct we will marshall into.
1927 prs_init(&pd, sec_desc_size, 4, MARSHALL);
1930 * Finally, linearize into the outgoing buffer.
1933 if(!sec_io_desc( "sd data", psd, &pd, 1))
1938 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1939 security descriptor.\n"));
1941 * Return access denied for want of a better error message..
1943 return(UNIXERROR(ERRDOS,ERRnoaccess));
1947 * copy the data out of the marshalled structure
1950 prs_give_memory( &pd, data, (uint32)sec_desc_size, False);
1953 * Now we can delete the security descriptor.
1960 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 4, data, (int)sec_desc_size);
1964 /****************************************************************************
1965 Reply to set a security descriptor. Map to UNIX perms.
1966 ****************************************************************************/
1968 static int call_nt_transact_set_security_desc(connection_struct *conn,
1969 char *inbuf, char *outbuf, int length,
1970 int bufsize, char **ppsetup,
1971 char **ppparams, char **ppdata)
1973 uint32 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1974 char *params= *ppparams;
1975 char *data = *ppdata;
1978 uint32 total_data_count = (uint32)IVAL(inbuf, smb_nts_TotalDataCount);
1979 uid_t user = (uid_t)-1;
1980 gid_t grp = (gid_t)-1;
1982 SMB_STRUCT_STAT sbuf;
1983 files_struct *fsp = NULL;
1984 uint32 security_info_sent = 0;
1985 BOOL got_dacl = False;
1987 if(!lp_nt_acl_support())
1988 return(UNIXERROR(ERRDOS,ERRnoaccess));
1990 if(total_parameter_count < 8)
1991 return(ERROR(ERRDOS,ERRbadfunc));
1993 if((fsp = file_fsp(params,0)) == NULL)
1994 return(ERROR(ERRDOS,ERRbadfid));
1996 security_info_sent = IVAL(params,4);
1998 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1999 (unsigned int)security_info_sent ));
2002 * Init the parse struct we will unmarshall from.
2005 prs_init(&pd, 0, 4, UNMARSHALL);
2008 * Setup the prs_struct to point at the memory we just
2012 prs_give_memory( &pd, data, total_data_count, False);
2015 * Finally, unmarshall from the data buffer.
2018 if(!sec_io_desc( "sd data", &psd, &pd, 1))
2020 free_sec_desc(&psd);
2022 DEBUG(0,("call_nt_transact_set_security_desc: Error in unmarshalling \
2023 security descriptor.\n"));
2025 * Return access denied for want of a better error message..
2027 return(UNIXERROR(ERRDOS,ERRnoaccess));
2031 * finished with the marshalling structure, already
2037 * Get the current state of the file.
2040 if(fsp->is_directory) {
2041 if(dos_stat(fsp->fsp_name, &sbuf) != 0) {
2042 free_sec_desc(&psd);
2043 return(UNIXERROR(ERRDOS,ERRnoaccess));
2050 ret = conn->vfs_ops.stat(dos_to_unix(fsp->fsp_name,False), &sbuf);
2052 ret = conn->vfs_ops.fstat(fsp->fd,&sbuf);
2055 free_sec_desc(&psd);
2056 return(UNIXERROR(ERRDOS,ERRnoaccess));
2061 * Unpack the user/group/world id's and permissions.
2064 if(!convertperms_sd_to_unix( &sbuf, &user, &grp, &perms, security_info_sent, &psd, fsp->is_directory)) {
2065 free_sec_desc(&psd);
2066 return(UNIXERROR(ERRDOS,ERRnoaccess));
2069 if (psd.dacl != NULL)
2072 free_sec_desc(&psd);
2075 * Do we need to chown ?
2078 if((user != (uid_t)-1 || grp != (uid_t)-1) && (sbuf.st_uid != user || sbuf.st_gid != grp)) {
2080 DEBUG(3,("call_nt_transact_set_security_desc: chown %s. uid = %u, gid = %u.\n",
2081 fsp->fsp_name, (unsigned int)user, (unsigned int)grp ));
2083 if(dos_chown( fsp->fsp_name, user, grp) == -1) {
2084 DEBUG(3,("call_nt_transact_set_security_desc: chown %s, %u, %u failed. Error = %s.\n",
2085 fsp->fsp_name, (unsigned int)user, (unsigned int)grp, strerror(errno) ));
2086 return(UNIXERROR(ERRDOS,ERRnoaccess));
2090 * Recheck the current state of the file, which may have changed.
2091 * (suid/sgid bits, for instance)
2094 if(fsp->is_directory) {
2095 if(dos_stat(fsp->fsp_name, &sbuf) != 0) {
2096 return(UNIXERROR(ERRDOS,ERRnoaccess));
2103 ret = conn->vfs_ops.stat(dos_to_unix(fsp->fsp_name,False), &sbuf);
2105 ret = conn->vfs_ops.fstat(fsp->fd,&sbuf);
2108 return(UNIXERROR(ERRDOS,ERRnoaccess));
2113 * Only change security if we got a DACL.
2116 if((security_info_sent & DACL_SECURITY_INFORMATION) && got_dacl) {
2119 * Check to see if we need to change anything.
2120 * Enforce limits on modified bits *only*. Don't enforce masks
2121 * on bits not changed by the user.
2124 if(fsp->is_directory) {
2126 perms &= (lp_dir_security_mask(SNUM(conn)) | sbuf.st_mode);
2127 perms |= (lp_force_dir_security_mode(SNUM(conn)) & ( perms ^ sbuf.st_mode ));
2131 perms &= (lp_security_mask(SNUM(conn)) | sbuf.st_mode);
2132 perms |= (lp_force_security_mode(SNUM(conn)) & ( perms ^ sbuf.st_mode ));
2137 * Preserve special bits.
2140 perms |= (sbuf.st_mode & ~0777);
2143 * Do we need to chmod ?
2146 if(sbuf.st_mode != perms) {
2148 DEBUG(3,("call_nt_transact_set_security_desc: chmod %s. perms = 0%o.\n",
2149 fsp->fsp_name, (unsigned int)perms ));
2151 if(conn->vfs_ops.chmod(dos_to_unix(fsp->fsp_name, False), perms) == -1) {
2152 DEBUG(3,("call_nt_transact_set_security_desc: chmod %s, 0%o failed. Error = %s.\n",
2153 fsp->fsp_name, (unsigned int)perms, strerror(errno) ));
2154 return(UNIXERROR(ERRDOS,ERRnoaccess));
2159 send_nt_replies(inbuf, outbuf, bufsize, 0, NULL, 0, NULL, 0);
2163 /****************************************************************************
2164 Reply to IOCTL - not implemented - no plans.
2165 ****************************************************************************/
2166 static int call_nt_transact_ioctl(connection_struct *conn,
2167 char *inbuf, char *outbuf, int length,
2169 char **ppsetup, char **ppparams, char **ppdata)
2171 static BOOL logged_message = False;
2173 if(!logged_message) {
2174 DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
2175 logged_message = True; /* Only print this once... */
2177 return(ERROR(ERRSRV,ERRnosupport));
2180 /****************************************************************************
2181 Reply to a SMBNTtrans.
2182 ****************************************************************************/
2183 int reply_nttrans(connection_struct *conn,
2184 char *inbuf,char *outbuf,int length,int bufsize)
2187 #if 0 /* Not used. */
2188 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
2189 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
2190 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
2191 #endif /* Not used. */
2192 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
2193 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
2194 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
2195 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
2196 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
2197 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
2198 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
2199 uint16 function_code = SVAL( inbuf, smb_nt_Function);
2200 char *params = NULL, *data = NULL, *setup = NULL;
2201 uint32 num_params_sofar, num_data_sofar;
2203 if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
2205 * Queue this open message as we are the process of an oplock break.
2208 DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
2209 due to being in oplock break state.\n" ));
2211 push_oplock_pending_smb_message( inbuf, length);
2215 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE))
2216 return (ERROR(ERRSRV,ERRaccess));
2218 outsize = set_message(outbuf,0,0,True);
2221 * All nttrans messages we handle have smb_wct == 19 + setup_count.
2222 * Ensure this is so as a sanity check.
2225 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
2226 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2227 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
2228 return(ERROR(ERRSRV,ERRerror));
2231 /* Allocate the space for the setup, the maximum needed parameters and data */
2234 setup = (char *)malloc(setup_count);
2235 if (total_parameter_count > 0)
2236 params = (char *)malloc(total_parameter_count);
2237 if (total_data_count > 0)
2238 data = (char *)malloc(total_data_count);
2240 if ((total_parameter_count && !params) || (total_data_count && !data) ||
2241 (setup_count && !setup)) {
2242 DEBUG(0,("reply_nttrans : Out of memory\n"));
2243 return(ERROR(ERRDOS,ERRnomem));
2246 /* Copy the param and data bytes sent with this request into
2247 the params buffer */
2248 num_params_sofar = parameter_count;
2249 num_data_sofar = data_count;
2251 if (parameter_count > total_parameter_count || data_count > total_data_count)
2252 exit_server("reply_nttrans: invalid sizes in packet.\n");
2255 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
2256 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
2257 dump_data(10, setup, setup_count);
2260 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
2261 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
2262 dump_data(10, params, parameter_count);
2265 memcpy( data, smb_base(inbuf) + data_offset, data_count);
2266 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
2267 dump_data(10, data, data_count);
2270 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2271 /* We need to send an interim response then receive the rest
2272 of the parameter/data bytes */
2273 outsize = set_message(outbuf,0,0,True);
2274 send_smb(smbd_server_fd(),outbuf);
2276 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2279 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
2281 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
2282 outsize = set_message(outbuf,0,0,True);
2284 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
2286 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
2287 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
2295 return(ERROR(ERRSRV,ERRerror));
2298 /* Revise total_params and total_data in case they have changed downwards */
2299 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
2300 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
2301 num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
2302 num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
2303 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
2304 exit_server("reply_nttrans2: data overflow in secondary nttrans packet\n");
2306 memcpy( ¶ms[ IVAL(inbuf, smb_nts_ParameterDisplacement)],
2307 smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
2308 memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
2309 smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
2313 if (Protocol >= PROTOCOL_NT1) {
2314 uint16 flg2 = SVAL(outbuf,smb_flg2);
2315 SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
2318 /* Now we must call the relevant NT_TRANS function */
2319 switch(function_code) {
2320 case NT_TRANSACT_CREATE:
2321 outsize = call_nt_transact_create(conn, inbuf, outbuf, length, bufsize,
2322 &setup, ¶ms, &data);
2324 case NT_TRANSACT_IOCTL:
2325 outsize = call_nt_transact_ioctl(conn,
2326 inbuf, outbuf, length, bufsize,
2327 &setup, ¶ms, &data);
2329 case NT_TRANSACT_SET_SECURITY_DESC:
2330 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
2332 &setup, ¶ms, &data);
2334 case NT_TRANSACT_NOTIFY_CHANGE:
2335 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
2337 &setup, ¶ms, &data);
2339 case NT_TRANSACT_RENAME:
2340 outsize = call_nt_transact_rename(conn, inbuf, outbuf, length,
2342 &setup, ¶ms, &data);
2345 case NT_TRANSACT_QUERY_SECURITY_DESC:
2346 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
2348 &setup, ¶ms, &data);
2351 /* Error in request */
2352 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
2359 return (ERROR(ERRSRV,ERRerror));
2362 /* As we do not know how many data packets will need to be
2363 returned here the various call_nt_transact_xxxx calls
2364 must send their own. Thus a call_nt_transact_xxxx routine only
2365 returns a value other than -1 when it wants to send
2375 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
2376 calls have already sent it. If outsize != -1 then it is
2377 returning an error packet. */