2 Unix SMB/Netbios implementation.
4 SMB NT transaction handling
5 Copyright (C) Jeremy Allison 1994-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 extern int DEBUGLEVEL;
28 extern int smb_read_error;
29 extern int global_oplock_break;
30 extern BOOL case_sensitive;
31 extern BOOL case_preserve;
32 extern BOOL short_case_preserve;
34 static void remove_pending_change_notify_requests_by_mid(int mid);
36 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(Client,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(Client,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 my_wcstombs(char *dst, uint16 *src, size_t len)
251 for(i = 0; i < len; i++)
252 dst[i] = (char)SVAL(src,i*2);
255 static void get_filename( char *fname, char *inbuf, int data_offset, int data_len, int fname_len)
257 if(data_len - fname_len > 1) {
259 * NT 5.0 Beta 2 has kindly sent us a UNICODE string
260 * without bothering to set the unicode bit. How kind.
262 * Firstly - ensure that the data offset is aligned
263 * on a 2 byte boundary - add one if not.
265 fname_len = fname_len/2;
268 my_wcstombs( fname, (uint16 *)(inbuf+data_offset), fname_len);
270 StrnCpy(fname,inbuf+data_offset,fname_len);
272 fname[fname_len] = '\0';
275 /****************************************************************************
277 ****************************************************************************/
279 static BOOL saved_case_sensitive;
280 static BOOL saved_case_preserve;
281 static BOOL saved_short_case_preserve;
283 /****************************************************************************
285 ****************************************************************************/
287 static void set_posix_case_semantics(uint32 file_attributes)
289 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
292 saved_case_sensitive = case_sensitive;
293 saved_case_preserve = case_preserve;
294 saved_short_case_preserve = short_case_preserve;
297 case_sensitive = True;
298 case_preserve = True;
299 short_case_preserve = True;
302 /****************************************************************************
303 Restore case semantics.
304 ****************************************************************************/
306 static void restore_case_semantics(uint32 file_attributes)
308 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
311 case_sensitive = saved_case_sensitive;
312 case_preserve = saved_case_preserve;
313 short_case_preserve = saved_short_case_preserve;
316 /****************************************************************************
317 Utility function to map create disposition.
318 ****************************************************************************/
320 static int map_create_disposition( uint32 create_disposition)
324 switch( create_disposition ) {
326 /* create if not exist, fail if exist */
327 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL);
330 case FILE_OVERWRITE_IF:
331 /* create if not exist, trunc if exist */
332 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
335 /* fail if not exist, open if exists */
336 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN);
339 /* create if not exist, open if exists */
340 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_OPEN);
343 /* fail if not exist, truncate if exists */
344 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
347 DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
348 create_disposition ));
352 DEBUG(10,("map_create_disposition: Mapped create_disposition %lx to %x\n",
353 (unsigned long)create_disposition, ret ));
358 /****************************************************************************
359 Utility function to map share modes.
360 ****************************************************************************/
362 static int map_share_mode( BOOL *pstat_open_only, char *fname,
363 uint32 desired_access, uint32 share_access, uint32 file_attributes)
365 int smb_open_mode = -1;
367 *pstat_open_only = False;
369 switch( desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
371 smb_open_mode = DOS_OPEN_RDONLY;
373 case FILE_WRITE_DATA:
374 case FILE_APPEND_DATA:
375 case FILE_WRITE_DATA|FILE_APPEND_DATA:
376 smb_open_mode = DOS_OPEN_WRONLY;
378 case FILE_READ_DATA|FILE_WRITE_DATA:
379 case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
380 case FILE_READ_DATA|FILE_APPEND_DATA:
381 smb_open_mode = DOS_OPEN_RDWR;
386 * NB. For DELETE_ACCESS we should really check the
387 * directory permissions, as that is what controls
388 * delete, and for WRITE_DAC_ACCESS we should really
389 * check the ownership, as that is what controls the
390 * chmod. Note that this is *NOT* a security hole (this
391 * note is for you, Andrew) as we are not *allowing*
392 * the access at this point, the actual unlink or
393 * chown or chmod call would do this. We are just helping
394 * clients out by telling them if they have a hope
395 * of any of this succeeding. POSIX acls may still
396 * deny the real call. JRA.
399 if (smb_open_mode == -1) {
400 if(desired_access == WRITE_DAC_ACCESS || desired_access == READ_CONTROL_ACCESS)
401 *pstat_open_only = True;
403 if(desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|
404 FILE_EXECUTE|FILE_READ_ATTRIBUTES|
405 FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
406 FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS))
407 smb_open_mode = DOS_OPEN_RDONLY;
409 DEBUG(0,("map_share_mode: Incorrect value %lx for desired_access to file %s\n",
410 (unsigned long)desired_access, fname));
416 * Set the special bit that means allow share delete.
417 * This is held outside the normal share mode bits at 1<<15.
421 if(share_access & FILE_SHARE_DELETE)
422 smb_open_mode |= ALLOW_SHARE_DELETE;
424 /* Add in the requested share mode. */
425 switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
426 case FILE_SHARE_READ:
427 smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
429 case FILE_SHARE_WRITE:
430 smb_open_mode |= SET_DENY_MODE(DENY_READ);
432 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
433 smb_open_mode |= SET_DENY_MODE(DENY_NONE);
435 case FILE_SHARE_NONE:
436 smb_open_mode |= SET_DENY_MODE(DENY_ALL);
441 * Handle an O_SYNC request.
444 if(file_attributes & FILE_FLAG_WRITE_THROUGH)
445 smb_open_mode |= FILE_SYNC_OPENMODE;
447 DEBUG(10,("map_share_mode: Mapped desired access %lx, share access %lx, file attributes %lx \
448 to open_mode %x\n", (unsigned long)desired_access, (unsigned long)share_access,
449 (unsigned long)file_attributes, smb_open_mode ));
451 return smb_open_mode;
455 * This is a *disgusting* hack.
456 * This is *so* bad that even I'm embarrassed (and I
457 * have no shame). Here's the deal :
458 * Until we get the correct SPOOLSS code into smbd
459 * then when we're running with NT SMB support then
460 * NT makes this call with a level of zero, and then
461 * immediately follows it with an open request to
462 * the \\SRVSVC pipe. If we allow that open to
463 * succeed then NT barfs when it cannot open the
464 * \\SPOOLSS pipe immediately after and continually
465 * whines saying "Printer name is invalid" forever
466 * after. If we cause *JUST THIS NEXT OPEN* of \\SRVSVC
467 * to fail, then NT downgrades to using the downlevel code
468 * and everything works as well as before. I hate
469 * myself for adding this code.... JRA.
471 * The HACK_FAIL_TIME define allows only a 2
472 * second window for this to occur, just in
476 static BOOL fail_next_srvsvc = False;
477 static time_t fail_time;
478 #define HACK_FAIL_TIME 2 /* In seconds. */
480 void fail_next_srvsvc_open(void)
482 /* Check client is WinNT proper; Win2K doesn't like Jeremy's hack - matty */
483 if (get_remote_arch() != RA_WINNT)
486 fail_next_srvsvc = True;
487 fail_time = time(NULL);
488 DEBUG(10,("fail_next_srvsvc_open: setting up timeout close of \\srvsvc pipe for print fix.\n"));
492 * HACK alert.... see above - JRA.
495 BOOL should_fail_next_srvsvc_open(const char *pipename)
498 DEBUG(10,("should_fail_next_srvsvc_open: fail = %d, pipe = %s\n",
499 (int)fail_next_srvsvc, pipename));
501 if(fail_next_srvsvc && (time(NULL) > fail_time + HACK_FAIL_TIME)) {
502 fail_next_srvsvc = False;
503 fail_time = (time_t)0;
504 DEBUG(10,("should_fail_next_srvsvc_open: End of timeout close of \\srvsvc pipe for print fix.\n"));
507 if(fail_next_srvsvc && strequal(pipename, "srvsvc")) {
508 fail_next_srvsvc = False;
509 DEBUG(10,("should_fail_next_srvsvc_open: Deliberately failing open of \\srvsvc pipe for print fix.\n"));
516 /****************************************************************************
517 Reply to an NT create and X call on a pipe.
518 ****************************************************************************/
519 static int nt_open_pipe(char *fname, connection_struct *conn,
520 char *inbuf, char *outbuf, int *ppnum)
522 pipes_struct *p = NULL;
524 uint16 vuid = SVAL(inbuf, smb_uid);
527 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
529 /* See if it is one we want to handle. */
530 for( i = 0; known_nt_pipes[i]; i++ )
531 if( strequal(fname,known_nt_pipes[i]))
534 if ( known_nt_pipes[i] == NULL )
535 return(ERROR(ERRSRV,ERRaccess));
537 /* Strip \\ off the name. */
540 if(should_fail_next_srvsvc_open(fname))
541 return (ERROR(ERRSRV,ERRaccess));
543 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
545 p = open_rpc_pipe_p(fname, conn, vuid);
547 return(ERROR(ERRSRV,ERRnofids));
554 /****************************************************************************
555 Reply to an NT create and X call.
556 ****************************************************************************/
558 int reply_ntcreate_and_X(connection_struct *conn,
559 char *inbuf,char *outbuf,int length,int bufsize)
562 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
563 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
564 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
565 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
566 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
567 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
568 uint32 fname_len = MIN(((uint32)SVAL(inbuf,smb_ntcreate_NameLength)),
569 ((uint32)sizeof(fname)-1));
570 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
573 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
574 /* Breakout the oplock request bits so we can set the
575 reply bits separately. */
576 int oplock_request = 0;
580 SMB_OFF_T file_len = 0;
581 SMB_STRUCT_STAT sbuf;
583 BOOL bad_path = False;
584 files_struct *fsp=NULL;
586 BOOL stat_open_only = False;
589 * We need to construct the open_and_X ofun value from the
590 * NT values, as that's what our code is structured to accept.
593 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
594 return(ERROR(ERRDOS,ERRbadaccess));
600 if(root_dir_fid != 0) {
602 * This filename is relative to a directory fid.
604 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
608 return(ERROR(ERRDOS,ERRbadfid));
610 if(!dir_fsp->is_directory) {
612 * Check to see if this is a mac fork of some kind.
615 get_filename(&fname[0], inbuf, smb_buf(inbuf)-inbuf,
616 smb_buflen(inbuf),fname_len);
618 if( fname[0] == ':') {
619 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
620 return(ERROR(0, 0xc0000000|NT_STATUS_OBJECT_PATH_NOT_FOUND));
622 return(ERROR(ERRDOS,ERRbadfid));
626 * Copy in the base directory name.
629 pstrcpy( fname, dir_fsp->fsp_name );
630 dir_name_len = strlen(fname);
633 * Ensure it ends in a '\'.
636 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
637 pstrcat(fname, "\\");
641 if(fname_len + dir_name_len >= sizeof(pstring))
642 return(ERROR(ERRSRV,ERRfilespecs));
644 get_filename(&fname[dir_name_len], inbuf, smb_buf(inbuf)-inbuf,
645 smb_buflen(inbuf),fname_len);
649 get_filename(fname, inbuf, smb_buf(inbuf)-inbuf,
650 smb_buflen(inbuf),fname_len);
653 /* If it's an IPC, use the pipe handler. */
655 if (IS_IPC(conn) && lp_nt_pipe_support()) {
657 int ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum);
662 * Deal with pipe return.
665 set_message(outbuf,34,0,True);
667 p = outbuf + smb_vwv2;
671 SIVAL(p,0,FILE_WAS_OPENED);
674 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
677 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
679 SSVAL(p,2, 0x5FF); /* ? */
681 DEBUG(5,("reply_ntcreate_and_X: open pipe = %s\n", fname));
683 return chain_reply(inbuf,outbuf,length,bufsize);
687 * Now contruct the smb_open_mode value from the filename,
688 * desired access and the share access.
691 if((smb_open_mode = map_share_mode(&stat_open_only, fname, desired_access,
693 file_attributes)) == -1)
694 return(ERROR(ERRDOS,ERRbadaccess));
696 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
697 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
700 * Ordinary file or directory.
704 * Check if POSIX semantics are wanted.
707 set_posix_case_semantics(file_attributes);
709 unix_convert(fname,conn,0,&bad_path,NULL);
713 restore_case_semantics(file_attributes);
714 return(ERROR(ERRSRV,ERRnofids));
717 if (!check_name(fname,conn)) {
718 if((errno == ENOENT) && bad_path) {
719 unix_ERR_class = ERRDOS;
720 unix_ERR_code = ERRbadpath;
724 restore_case_semantics(file_attributes);
726 return(UNIXERROR(ERRDOS,ERRnoaccess));
729 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
732 * If it's a request for a directory open, deal with it separately.
735 if(create_options & FILE_DIRECTORY_FILE) {
738 open_directory(fsp, conn, fname, smb_ofun,
739 unixmode, &smb_action);
741 restore_case_semantics(file_attributes);
745 return(UNIXERROR(ERRDOS,ERRnoaccess));
749 * Ordinary file case.
752 /* NB. We have a potential bug here. If we
753 * cause an oplock break to ourselves, then we
754 * could end up processing filename related
755 * SMB requests whilst we await the oplock
756 * break response. As we may have changed the
757 * filename case semantics to be POSIX-like,
758 * this could mean a filename request could
759 * fail when it should succeed. This is a rare
760 * condition, but eventually we must arrange
761 * to restore the correct case semantics
762 * before issuing an oplock break request to
763 * our client. JRA. */
765 open_file_shared(fsp,conn,fname,smb_open_mode,
766 smb_ofun,unixmode, oplock_request,&rmode,&smb_action);
769 /* We cheat here. There are two cases we
770 * care about. One is a directory rename,
771 * where the NT client will attempt to
772 * open the source directory for
773 * DELETE access. Note that when the
774 * NT client does this it does *not*
775 * set the directory bit in the
776 * request packet. This is translated
777 * into a read/write open
778 * request. POSIX states that any open
779 * for write request on a directory
780 * will generate an EISDIR error, so
781 * we can catch this here and open a
782 * pseudo handle that is flagged as a
783 * directory. The second is an open
784 * for a permissions read only, which
785 * we handle in the open_file_stat case. JRA.
788 if(errno == EISDIR) {
791 * Fail the open if it was explicitly a non-directory file.
794 if (create_options & FILE_NON_DIRECTORY_FILE) {
796 restore_case_semantics(file_attributes);
797 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
798 return(ERROR(0, 0xc0000000|NT_STATUS_FILE_IS_A_DIRECTORY));
802 open_directory(fsp, conn, fname, smb_ofun, unixmode, &smb_action);
806 restore_case_semantics(file_attributes);
807 return(UNIXERROR(ERRDOS,ERRnoaccess));
810 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
812 } else if (errno == EACCES && stat_open_only) {
815 * We couldn't open normally and all we want
816 * are the permissions. Try and do a stat open.
821 open_file_stat(fsp,conn,fname,smb_open_mode,&sbuf,&smb_action);
825 restore_case_semantics(file_attributes);
826 return(UNIXERROR(ERRDOS,ERRnoaccess));
831 if((errno == ENOENT) && bad_path) {
832 unix_ERR_class = ERRDOS;
833 unix_ERR_code = ERRbadpath;
838 restore_case_semantics(file_attributes);
840 return(UNIXERROR(ERRDOS,ERRnoaccess));
845 if(fsp->is_directory) {
846 if(fsp->conn->vfs_ops.stat(dos_to_unix(fsp->fsp_name, False),
848 close_file(fsp,True);
849 restore_case_semantics(file_attributes);
850 return(ERROR(ERRDOS,ERRnoaccess));
853 if (fsp->conn->vfs_ops.fstat(fsp->fd_ptr->fd,&sbuf)
855 close_file(fsp,False);
856 restore_case_semantics(file_attributes);
857 return(ERROR(ERRDOS,ERRnoaccess));
861 restore_case_semantics(file_attributes);
863 file_len = sbuf.st_size;
864 fmode = dos_mode(conn,fname,&sbuf);
866 fmode = FILE_ATTRIBUTE_NORMAL;
867 if (!fsp->is_directory && (fmode & aDIR)) {
868 close_file(fsp,False);
869 return(ERROR(ERRDOS,ERRnoaccess));
873 * If the caller set the extended oplock request bit
874 * and we granted one (by whatever means) - set the
875 * correct bit for extended oplock reply.
878 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
879 smb_action |= EXTENDED_OPLOCK_GRANTED;
881 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
882 smb_action |= EXTENDED_OPLOCK_GRANTED;
884 set_message(outbuf,34,0,True);
886 p = outbuf + smb_vwv2;
889 * Currently as we don't support level II oplocks we just report
890 * exclusive & batch here.
893 if (smb_action & EXTENDED_OPLOCK_GRANTED)
894 SCVAL(p,0, BATCH_OPLOCK_RETURN);
895 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
896 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
898 SCVAL(p,0,NO_OPLOCK_RETURN);
901 SSVAL(p,0,fsp->fnum);
903 SIVAL(p,0,smb_action);
907 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
909 put_long_date(p,sbuf.st_atime); /* access time */
911 put_long_date(p,sbuf.st_mtime); /* write time */
913 put_long_date(p,sbuf.st_mtime); /* change time */
915 SIVAL(p,0,fmode); /* File Attributes. */
917 SOFF_T(p, 0, file_len);
919 SOFF_T(p,0,file_len);
921 SCVAL(p,0,fsp->is_directory ? 1 : 0);
923 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
925 return chain_reply(inbuf,outbuf,length,bufsize);
928 /****************************************************************************
929 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
930 ****************************************************************************/
932 static int call_nt_transact_create(connection_struct *conn,
933 char *inbuf, char *outbuf, int length,
935 char **ppsetup, char **ppparams,
939 char *params = *ppparams;
940 uint32 flags = IVAL(params,0);
941 uint32 desired_access = IVAL(params,8);
942 uint32 file_attributes = IVAL(params,20);
943 uint32 share_access = IVAL(params,24);
944 uint32 create_disposition = IVAL(params,28);
945 uint32 create_options = IVAL(params,32);
946 uint32 fname_len = MIN(((uint32)IVAL(params,44)),
947 ((uint32)sizeof(fname)-1));
948 uint16 root_dir_fid = (uint16)IVAL(params,4);
951 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
952 /* Breakout the oplock request bits so we can set the
953 reply bits separately. */
954 int oplock_request = 0;
958 SMB_OFF_T file_len = 0;
959 SMB_STRUCT_STAT sbuf;
961 BOOL bad_path = False;
962 files_struct *fsp = NULL;
964 BOOL stat_open_only = False;
967 * We need to construct the open_and_X ofun value from the
968 * NT values, as that's what our code is structured to accept.
971 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
972 return(ERROR(ERRDOS,ERRbadaccess));
978 if(root_dir_fid != 0) {
980 * This filename is relative to a directory fid.
983 files_struct *dir_fsp = file_fsp(params,4);
987 return(ERROR(ERRDOS,ERRbadfid));
989 if(!dir_fsp->is_directory) {
991 * Check to see if this is a mac fork of some kind.
994 StrnCpy(fname,params+53,fname_len);
995 fname[fname_len] = '\0';
997 if( fname[0] == ':') {
998 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
999 return(ERROR(0, 0xc0000000|NT_STATUS_OBJECT_PATH_NOT_FOUND));
1002 return(ERROR(ERRDOS,ERRbadfid));
1006 * Copy in the base directory name.
1009 pstrcpy( fname, dir_fsp->fsp_name );
1010 dir_name_len = strlen(fname);
1013 * Ensure it ends in a '\'.
1016 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1017 pstrcat(fname, "\\");
1021 if(fname_len + dir_name_len >= sizeof(pstring))
1022 return(ERROR(ERRSRV,ERRfilespecs));
1024 StrnCpy(&fname[dir_name_len], params+53, fname_len);
1025 fname[dir_name_len+fname_len] = '\0';
1028 StrnCpy(fname,params+53,fname_len);
1029 fname[fname_len] = '\0';
1032 /* If it's an IPC, use the pipe handler. */
1034 int ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum);
1037 smb_action = FILE_WAS_OPENED;
1041 * Now contruct the smb_open_mode value from the desired access
1042 * and the share access.
1045 if((smb_open_mode = map_share_mode( &stat_open_only, fname, desired_access,
1046 share_access, file_attributes)) == -1)
1047 return(ERROR(ERRDOS,ERRbadaccess));
1049 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1050 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1053 * Check if POSIX semantics are wanted.
1056 set_posix_case_semantics(file_attributes);
1058 unix_convert(fname,conn,0,&bad_path,NULL);
1062 restore_case_semantics(file_attributes);
1063 return(ERROR(ERRSRV,ERRnofids));
1066 if (!check_name(fname,conn)) {
1067 if((errno == ENOENT) && bad_path) {
1068 unix_ERR_class = ERRDOS;
1069 unix_ERR_code = ERRbadpath;
1073 restore_case_semantics(file_attributes);
1075 return(UNIXERROR(ERRDOS,ERRnoaccess));
1078 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
1081 * If it's a request for a directory open, deal with it separately.
1084 if(create_options & FILE_DIRECTORY_FILE) {
1089 * We will get a create directory here if the Win32
1090 * app specified a security descriptor in the
1091 * CreateDirectory() call.
1094 open_directory(fsp, conn, fname, smb_ofun, unixmode, &smb_action);
1098 restore_case_semantics(file_attributes);
1099 return(UNIXERROR(ERRDOS,ERRnoaccess));
1102 if(conn->vfs_ops.stat(dos_to_unix(fsp->fsp_name, False),
1104 close_file(fsp,True);
1105 restore_case_semantics(file_attributes);
1106 return(ERROR(ERRDOS,ERRnoaccess));
1112 * Ordinary file case.
1115 open_file_shared(fsp,conn,fname,smb_open_mode,smb_ofun,unixmode,
1116 oplock_request,&rmode,&smb_action);
1120 if(errno == EISDIR) {
1123 * Fail the open if it was explicitly a non-directory file.
1126 if (create_options & FILE_NON_DIRECTORY_FILE) {
1128 restore_case_semantics(file_attributes);
1129 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
1130 return(ERROR(0, 0xc0000000|NT_STATUS_FILE_IS_A_DIRECTORY));
1134 open_directory(fsp, conn, fname, smb_ofun, unixmode, &smb_action);
1138 restore_case_semantics(file_attributes);
1139 return(UNIXERROR(ERRDOS,ERRnoaccess));
1142 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
1144 } else if (errno == EACCES && stat_open_only) {
1148 * We couldn't open normally and all we want
1149 * are the permissions. Try and do a stat open.
1154 open_file_stat(fsp,conn,fname,smb_open_mode,&sbuf,&smb_action);
1158 restore_case_semantics(file_attributes);
1159 return(UNIXERROR(ERRDOS,ERRnoaccess));
1163 if((errno == ENOENT) && bad_path) {
1164 unix_ERR_class = ERRDOS;
1165 unix_ERR_code = ERRbadpath;
1169 restore_case_semantics(file_attributes);
1171 return(UNIXERROR(ERRDOS,ERRnoaccess));
1175 if(fsp->is_directory) {
1176 if(conn->vfs_ops.stat(dos_to_unix(fsp->fsp_name,False), &sbuf) != 0) {
1177 close_file(fsp,True);
1178 restore_case_semantics(file_attributes);
1179 return(ERROR(ERRDOS,ERRnoaccess));
1182 if (!fsp->stat_open && conn->vfs_ops.fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
1183 close_file(fsp,False);
1184 restore_case_semantics(file_attributes);
1185 return(ERROR(ERRDOS,ERRnoaccess));
1189 file_len = sbuf.st_size;
1190 fmode = dos_mode(conn,fname,&sbuf);
1192 fmode = FILE_ATTRIBUTE_NORMAL;
1195 close_file(fsp,False);
1196 restore_case_semantics(file_attributes);
1197 return(ERROR(ERRDOS,ERRnoaccess));
1201 * If the caller set the extended oplock request bit
1202 * and we granted one (by whatever means) - set the
1203 * correct bit for extended oplock reply.
1206 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1207 smb_action |= EXTENDED_OPLOCK_GRANTED;
1209 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1210 smb_action |= EXTENDED_OPLOCK_GRANTED;
1214 restore_case_semantics(file_attributes);
1216 /* Realloc the size of parameters and data we will return */
1217 params = *ppparams = Realloc(*ppparams, 69);
1219 return(ERROR(ERRDOS,ERRnomem));
1221 memset((char *)params,'\0',69);
1224 if (smb_action & EXTENDED_OPLOCK_GRANTED)
1225 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1226 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1227 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1229 SCVAL(p,0,NO_OPLOCK_RETURN);
1235 SSVAL(p,0,fsp->fnum);
1238 SIVAL(p,0,smb_action);
1243 * Deal with pipe return.
1246 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1249 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1251 SSVAL(p,2, 0x5FF); /* ? */
1254 * Deal with file return.
1257 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
1259 put_long_date(p,sbuf.st_atime); /* access time */
1261 put_long_date(p,sbuf.st_mtime); /* write time */
1263 put_long_date(p,sbuf.st_mtime); /* change time */
1265 SIVAL(p,0,fmode); /* File Attributes. */
1267 SOFF_T(p,0,file_len);
1269 SOFF_T(p,0,file_len);
1272 /* Send the required number of replies */
1273 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 69, *ppdata, 0);
1278 /****************************************************************************
1279 Reply to a NT CANCEL request.
1280 ****************************************************************************/
1281 int reply_ntcancel(connection_struct *conn,
1282 char *inbuf,char *outbuf,int length,int bufsize)
1285 * Go through and cancel any pending change notifies.
1288 int mid = SVAL(inbuf,smb_mid);
1289 remove_pending_change_notify_requests_by_mid(mid);
1290 remove_pending_lock_requests_by_mid(mid);
1292 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1297 /****************************************************************************
1298 Reply to an unsolicited SMBNTtranss - just ignore it!
1299 ****************************************************************************/
1300 int reply_nttranss(connection_struct *conn,
1301 char *inbuf,char *outbuf,int length,int bufsize)
1303 DEBUG(4,("Ignoring nttranss of length %d\n",length));
1307 /****************************************************************************
1308 Reply to an NT transact rename command.
1309 ****************************************************************************/
1311 static int call_nt_transact_rename(connection_struct *conn,
1312 char *inbuf, char *outbuf, int length,
1314 char **ppsetup, char **ppparams, char **ppdata)
1316 char *params = *ppparams;
1318 files_struct *fsp = file_fsp(params, 0);
1319 BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1320 uint32 fname_len = MIN((((uint32)IVAL(inbuf,smb_nt_TotalParameterCount)-4)),
1321 ((uint32)sizeof(new_name)-1));
1324 CHECK_FSP(fsp, conn);
1325 StrnCpy(new_name,params+4,fname_len);
1326 new_name[fname_len] = '\0';
1328 outsize = rename_internals(conn, inbuf, outbuf, fsp->fsp_name,
1329 new_name, replace_if_exists);
1332 * Rename was successful.
1334 send_nt_replies(inbuf, outbuf, bufsize, 0, NULL, 0, NULL, 0);
1336 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1337 fsp->fsp_name, new_name));
1345 /****************************************************************************
1346 This is the structure to keep the information needed to
1347 determine if a directory has changed.
1348 *****************************************************************************/
1351 time_t modify_time; /* Info from the directory we're monitoring. */
1352 time_t status_time; /* Info from the directory we're monitoring. */
1353 time_t total_time; /* Total time of all directory entries - don't care if it wraps. */
1354 unsigned int num_entries; /* Zero or the number of files in the directory. */
1357 /****************************************************************************
1358 This is the structure to queue to implement NT change
1359 notify. It consists of smb_size bytes stored from the
1360 transact command (to keep the mid, tid etc around).
1361 Plus the fid to examine and the time to check next.
1362 *****************************************************************************/
1365 ubi_slNode msg_next;
1367 connection_struct *conn;
1369 time_t next_check_time;
1370 change_hash_data change_data;
1371 char request_buf[smb_size];
1372 } change_notify_buf;
1374 static ubi_slList change_notify_queue = { NULL, (ubi_slNodePtr)&change_notify_queue, 0};
1376 /****************************************************************************
1377 Setup the common parts of the return packet and send it.
1378 *****************************************************************************/
1380 static void change_notify_reply_packet(char *inbuf, int error_class, uint32 error_code)
1383 char outbuf[smb_size+38];
1385 memset(outbuf, '\0', sizeof(outbuf));
1386 construct_reply_common(inbuf, outbuf);
1389 * If we're returning a 'too much in the directory changed' we need to
1390 * set this is an NT error status flags. If we don't then the (probably
1391 * untested) code in the NT redirector has a bug in that it doesn't re-issue
1392 * the change notify.... Ah - I *love* it when I get so deeply into this I
1393 * can even determine how MS failed to test stuff and why.... :-). JRA.
1396 if(error_class == 0) /* NT Error. */
1397 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1399 ERROR(error_class,error_code);
1402 * Seems NT needs a transact command with an error code
1403 * in it. This is a longer packet than a simple error.
1405 set_message(outbuf,18,0,False);
1407 send_smb(Client,outbuf);
1410 /****************************************************************************
1411 Create the hash we will use to determine if the contents changed.
1412 *****************************************************************************/
1414 static BOOL create_directory_notify_hash( change_notify_buf *cnbp, change_hash_data *change_data)
1417 files_struct *fsp = cnbp->fsp;
1419 memset((char *)change_data, '\0', sizeof(change_data));
1422 * Store the current timestamp on the directory we are monitoring.
1425 if(dos_stat(fsp->fsp_name, &st) < 0) {
1426 DEBUG(0,("create_directory_notify_hash: Unable to stat name = %s. \
1427 Error was %s\n", fsp->fsp_name, strerror(errno) ));
1431 change_data->modify_time = st.st_mtime;
1432 change_data->status_time = st.st_ctime;
1435 * If we are to watch for changes that are only stored
1436 * in inodes of files, not in the directory inode, we must
1437 * scan the directory and produce a unique identifier with
1438 * which we can determine if anything changed. We use the
1439 * modify and change times from all the files in the
1440 * directory, added together (ignoring wrapping if it's
1441 * larger than the max time_t value).
1444 if(cnbp->flags & (FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_LAST_WRITE)) {
1448 size_t remaining_len;
1449 size_t fullname_len;
1450 void *dp = OpenDir(cnbp->conn, fsp->fsp_name, True);
1453 DEBUG(0,("create_directory_notify_hash: Unable to open directory = %s. \
1454 Error was %s\n", fsp->fsp_name, strerror(errno) ));
1458 change_data->num_entries = 0;
1460 pstrcpy(full_name, fsp->fsp_name);
1461 pstrcat(full_name, "/");
1463 fullname_len = strlen(full_name);
1464 remaining_len = sizeof(full_name) - fullname_len - 1;
1465 p = &full_name[fullname_len];
1467 while ((fname = ReadDirName(dp))) {
1468 if(strequal(fname, ".") || strequal(fname, ".."))
1471 change_data->num_entries++;
1472 safe_strcpy( p, fname, remaining_len);
1474 memset(&st, '\0', sizeof(st));
1477 * Do the stat - but ignore errors.
1480 if(dos_stat(full_name, &st) < 0) {
1481 DEBUG(5,("create_directory_notify_hash: Unable to stat content file = %s. \
1482 Error was %s\n", fsp->fsp_name, strerror(errno) ));
1484 change_data->total_time += (st.st_mtime + st.st_ctime);
1493 /****************************************************************************
1494 Delete entries by fnum from the change notify pending queue.
1495 *****************************************************************************/
1497 void remove_pending_change_notify_requests_by_fid(files_struct *fsp)
1499 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1500 change_notify_buf *prev = NULL;
1502 while(cnbp != NULL) {
1503 if(cnbp->fsp->fnum == fsp->fnum) {
1504 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1505 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1510 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1514 /****************************************************************************
1515 Delete entries by mid from the change notify pending queue. Always send reply.
1516 *****************************************************************************/
1518 static void remove_pending_change_notify_requests_by_mid(int mid)
1520 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1521 change_notify_buf *prev = NULL;
1523 while(cnbp != NULL) {
1524 if(SVAL(cnbp->request_buf,smb_mid) == mid) {
1525 change_notify_reply_packet(cnbp->request_buf,0,0xC0000000 |NT_STATUS_CANCELLED);
1526 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1527 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1532 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1536 /****************************************************************************
1537 Delete entries by filename and cnum from the change notify pending queue.
1539 *****************************************************************************/
1541 void remove_pending_change_notify_requests_by_filename(files_struct *fsp)
1543 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1544 change_notify_buf *prev = NULL;
1546 while(cnbp != NULL) {
1548 * We know it refers to the same directory if the connection number and
1549 * the filename are identical.
1551 if((cnbp->fsp->conn == fsp->conn) && strequal(cnbp->fsp->fsp_name,fsp->fsp_name)) {
1552 change_notify_reply_packet(cnbp->request_buf,0,0xC0000000 |NT_STATUS_CANCELLED);
1553 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1554 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1559 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1563 /****************************************************************************
1564 Process the change notify queue. Note that this is only called as root.
1565 Returns True if there are still outstanding change notify requests on the
1567 *****************************************************************************/
1569 BOOL process_pending_change_notify_queue(time_t t)
1571 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1572 change_notify_buf *prev = NULL;
1577 if(cnbp->next_check_time >= t)
1581 * It's time to check. Go through the queue and see if
1582 * the timestamps changed.
1585 while((cnbp != NULL) && (cnbp->next_check_time <= t)) {
1586 change_hash_data change_data;
1587 connection_struct *conn = cnbp->conn;
1588 uint16 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
1589 SVAL(cnbp->request_buf,smb_uid);
1592 * Ensure we don't have any old chain_fsp values
1593 * sitting around....
1598 if(!become_user(conn,vuid)) {
1599 DEBUG(0,("process_pending_change_notify_queue: Unable to become user vuid=%d.\n",
1602 * Remove the entry and return an error to the client.
1604 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1605 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1606 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1610 if(!become_service(conn,True)) {
1611 DEBUG(0,("process_pending_change_notify_queue: Unable to become service Error was %s.\n", strerror(errno) ));
1613 * Remove the entry and return an error to the client.
1615 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1616 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1617 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1622 if(!create_directory_notify_hash( cnbp, &change_data)) {
1623 DEBUG(0,("process_pending_change_notify_queue: Unable to create change data for \
1624 directory %s\n", cnbp->fsp->fsp_name ));
1626 * Remove the entry and return an error to the client.
1628 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1629 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1630 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1635 if(memcmp( (char *)&cnbp->change_data, (char *)&change_data, sizeof(change_data))) {
1637 * Remove the entry and return a change notify to the client.
1639 DEBUG(5,("process_pending_change_notify_queue: directory name = %s changed.\n",
1640 cnbp->fsp->fsp_name ));
1641 change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_NOTIFY_ENUM_DIR);
1642 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1643 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1651 * Move to the next in the list.
1654 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1657 return (cnbp != NULL);
1660 /****************************************************************************
1661 Return true if there are pending change notifies.
1662 ****************************************************************************/
1664 BOOL change_notifies_pending(void)
1666 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1667 return (cnbp != NULL);
1670 /****************************************************************************
1671 Reply to a notify change - queue the request and
1672 don't allow a directory to be opened.
1673 ****************************************************************************/
1675 static int call_nt_transact_notify_change(connection_struct *conn,
1676 char *inbuf, char *outbuf, int length,
1679 char **ppparams, char **ppdata)
1681 char *setup = *ppsetup;
1683 change_notify_buf *cnbp;
1685 fsp = file_fsp(setup,4);
1687 DEBUG(3,("call_nt_transact_notify_change\n"));
1690 return(ERROR(ERRDOS,ERRbadfid));
1692 if((!fsp->open) || (!fsp->is_directory) || (conn != fsp->conn))
1693 return(ERROR(ERRDOS,ERRbadfid));
1696 * Now queue an entry on the notify change stack. We timestamp
1697 * the entry we are adding so that we know when to scan next.
1698 * We only need to save smb_size bytes from this incoming packet
1699 * as we will always by returning a 'read the directory yourself'
1703 if((cnbp = (change_notify_buf *)malloc(sizeof(change_notify_buf))) == NULL) {
1704 DEBUG(0,("call_nt_transact_notify_change: malloc fail !\n" ));
1708 memset((char *)cnbp, '\0', sizeof(change_notify_buf));
1710 memcpy(cnbp->request_buf, inbuf, smb_size);
1713 cnbp->next_check_time = time(NULL) + lp_change_notify_timeout();
1714 cnbp->flags = IVAL(setup, 0);
1716 if(!create_directory_notify_hash( cnbp, &cnbp->change_data )) {
1718 return(UNIXERROR(ERRDOS,ERRbadfid));
1722 * Adding to the tail enables us to check only
1723 * the head when scanning for change, as this entry
1724 * is forced to have the first timeout expiration.
1727 ubi_slAddTail(&change_notify_queue, cnbp);
1729 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1730 name = %s\n", fsp->fsp_name ));
1735 /****************************************************************************
1736 Map unix perms to NT.
1737 ****************************************************************************/
1739 static SEC_ACCESS map_unix_perms( int *pacl_type, mode_t perm, int r_mask, int w_mask, int x_mask, BOOL is_directory)
1744 *pacl_type = SEC_ACE_TYPE_ACCESS_ALLOWED;
1746 if((perm & (r_mask|w_mask|x_mask)) == (r_mask|w_mask|x_mask)) {
1747 nt_mask = UNIX_ACCESS_RWX;
1748 } else if((perm & (r_mask|w_mask|x_mask)) == 0) {
1749 nt_mask = UNIX_ACCESS_NONE;
1751 nt_mask |= (perm & r_mask) ? UNIX_ACCESS_R : 0;
1753 nt_mask |= (perm & w_mask) ? UNIX_ACCESS_W : 0;
1755 nt_mask |= (perm & w_mask) ? UNIX_ACCESS_W : 0;
1756 nt_mask |= (perm & x_mask) ? UNIX_ACCESS_X : 0;
1758 init_sec_access(&sa,nt_mask);
1762 /****************************************************************************
1763 Reply to query a security descriptor from an fsp. If it succeeds it allocates
1764 the space for the return elements and returns True.
1765 ****************************************************************************/
1767 static size_t get_nt_acl(files_struct *fsp, SEC_DESC **ppdesc)
1769 extern DOM_SID global_sam_sid;
1770 extern DOM_SID global_sid_World;
1771 SMB_STRUCT_STAT sbuf;
1772 SEC_ACE ace_list[6];
1775 size_t sec_desc_size;
1776 SEC_ACL *psa = NULL;
1777 SEC_ACCESS owner_access;
1779 SEC_ACCESS group_access;
1781 SEC_ACCESS other_access;
1787 if(!lp_nt_acl_support()) {
1788 sid_copy( &owner_sid, &global_sid_World);
1789 sid_copy( &group_sid, &global_sid_World);
1792 if(fsp->is_directory || fsp->fd_ptr == NULL) {
1793 if(dos_stat(fsp->fsp_name, &sbuf) != 0) {
1797 if(fsp->conn->vfs_ops.fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
1803 * Get the owner, group and world SIDs.
1806 sid_copy(&owner_sid, &global_sam_sid);
1807 sid_copy(&group_sid, &global_sam_sid);
1808 sid_append_rid(&owner_sid, pdb_uid_to_user_rid(sbuf.st_uid));
1809 sid_append_rid(&group_sid, pdb_gid_to_group_rid(sbuf.st_gid));
1812 * Create the generic 3 element UNIX acl.
1815 owner_access = map_unix_perms(&owner_acl_type, sbuf.st_mode,
1816 S_IRUSR, S_IWUSR, S_IXUSR, fsp->is_directory);
1817 group_access = map_unix_perms(&grp_acl_type, sbuf.st_mode,
1818 S_IRGRP, S_IWGRP, S_IXGRP, fsp->is_directory);
1819 other_access = map_unix_perms(&other_acl_type, sbuf.st_mode,
1820 S_IROTH, S_IWOTH, S_IXOTH, fsp->is_directory);
1822 if(owner_access.mask)
1823 init_sec_ace(&ace_list[num_acls++], &owner_sid, owner_acl_type,
1826 if(group_access.mask)
1827 init_sec_ace(&ace_list[num_acls++], &group_sid, grp_acl_type,
1830 if(other_access.mask)
1831 init_sec_ace(&ace_list[num_acls++], &global_sid_World, other_acl_type,
1834 if(fsp->is_directory) {
1836 * For directory ACLs we also add in the inherited permissions
1837 * ACE entries. These are the permissions a file would get when
1838 * being created in the directory.
1840 mode_t mode = unix_mode( fsp->conn, FILE_ATTRIBUTE_ARCHIVE, fsp->fsp_name);
1842 owner_access = map_unix_perms(&owner_acl_type, mode,
1843 S_IRUSR, S_IWUSR, S_IXUSR, fsp->is_directory);
1844 group_access = map_unix_perms(&grp_acl_type, mode,
1845 S_IRGRP, S_IWGRP, S_IXGRP, fsp->is_directory);
1846 other_access = map_unix_perms(&other_acl_type, mode,
1847 S_IROTH, S_IWOTH, S_IXOTH, fsp->is_directory);
1849 if(owner_access.mask)
1850 init_sec_ace(&ace_list[num_acls++], &owner_sid, owner_acl_type,
1851 owner_access, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_INHERIT_ONLY);
1853 if(group_access.mask)
1854 init_sec_ace(&ace_list[num_acls++], &group_sid, grp_acl_type,
1855 group_access, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_INHERIT_ONLY);
1857 if(other_access.mask)
1858 init_sec_ace(&ace_list[num_acls++], &global_sid_World, other_acl_type,
1859 other_access, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_INHERIT_ONLY);
1863 if((psa = make_sec_acl( 3, num_acls, ace_list)) == NULL) {
1864 DEBUG(0,("get_nt_acl: Unable to malloc space for acl.\n"));
1869 *ppdesc = make_standard_sec_desc( &owner_sid, &group_sid, psa, &sec_desc_size);
1872 DEBUG(0,("get_nt_acl: Unable to malloc space for security descriptor.\n"));
1878 return sec_desc_size;
1881 /****************************************************************************
1882 Reply to query a security descriptor - currently this is not implemented (it
1883 is planned to be though). Right now it just returns the same thing NT would
1884 when queried on a FAT filesystem. JRA.
1885 ****************************************************************************/
1887 static int call_nt_transact_query_security_desc(connection_struct *conn,
1888 char *inbuf, char *outbuf,
1889 int length, int bufsize,
1890 char **ppsetup, char **ppparams, char **ppdata)
1892 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1893 char *params = *ppparams;
1894 char *data = *ppdata;
1897 size_t sec_desc_size;
1899 files_struct *fsp = file_fsp(params,0);
1902 return(ERROR(ERRDOS,ERRbadfid));
1904 DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1906 params = *ppparams = Realloc(*ppparams, 4);
1908 return(ERROR(ERRDOS,ERRnomem));
1911 * Get the permissions to return.
1914 if((sec_desc_size = get_nt_acl(fsp, &psd)) == 0)
1915 return(UNIXERROR(ERRDOS,ERRnoaccess));
1917 DEBUG(3,("call_nt_transact_query_security_desc: sec_desc_size = %d.\n",(int)sec_desc_size));
1919 SIVAL(params,0,(uint32)sec_desc_size);
1921 if(max_data_count < sec_desc_size) {
1923 free_sec_desc(&psd);
1925 send_nt_replies(inbuf, outbuf, bufsize, 0xC0000000|NT_STATUS_BUFFER_TOO_SMALL,
1926 params, 4, *ppdata, 0);
1931 * Allocate the data we will point this at.
1934 data = *ppdata = Realloc(*ppdata, sec_desc_size);
1936 free_sec_desc(&psd);
1937 return(ERROR(ERRDOS,ERRnomem));
1940 memset(data, '\0', sec_desc_size);
1943 * Init the parse struct we will marshall into.
1946 prs_init(&pd, 0, 4, MARSHALL);
1949 * Setup the prs_struct to point at the memory we just
1953 prs_give_memory( &pd, data, (uint32)sec_desc_size, False);
1956 * Finally, linearize into the outgoing buffer.
1959 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1960 free_sec_desc(&psd);
1961 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1962 security descriptor.\n"));
1964 * Return access denied for want of a better error message..
1966 return(UNIXERROR(ERRDOS,ERRnoaccess));
1970 * Now we can delete the security descriptor.
1973 free_sec_desc(&psd);
1975 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 4, data, (int)sec_desc_size);
1979 /****************************************************************************
1981 ****************************************************************************/
1983 static BOOL validate_unix_sid( DOM_SID *psid, uint32 *prid, DOM_SID *sd_sid)
1985 extern DOM_SID global_sam_sid;
1989 DEBUG(5,("validate_unix_sid: sid missing.\n"));
1993 sid_copy(psid, sd_sid);
1994 sid_copy(&sid, sd_sid);
1996 if(!sid_split_rid(&sid, prid)) {
1997 DEBUG(5,("validate_unix_sid: cannot get RID from sid.\n"));
2001 if(!sid_equal( &sid, &global_sam_sid)) {
2002 DEBUG(5,("validate_unix_sid: sid is not ours.\n"));
2009 /****************************************************************************
2010 Map NT perms to UNIX.
2011 ****************************************************************************/
2013 #define FILE_SPECIFIC_READ_BITS (FILE_READ_DATA|FILE_READ_EA|FILE_READ_ATTRIBUTES)
2014 #define FILE_SPECIFIC_WRITE_BITS (FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_WRITE_EA|FILE_WRITE_ATTRIBUTES)
2015 #define FILE_SPECIFIC_EXECUTE_BITS (FILE_EXECUTE)
2017 static mode_t map_nt_perms( SEC_ACCESS sec_access, int type)
2023 if(sec_access.mask & GENERIC_ALL_ACCESS)
2024 mode = S_IRUSR|S_IWUSR|S_IXUSR;
2026 mode |= (sec_access.mask & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRUSR : 0;
2027 mode |= (sec_access.mask & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWUSR : 0;
2028 mode |= (sec_access.mask & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXUSR : 0;
2032 if(sec_access.mask & GENERIC_ALL_ACCESS)
2033 mode = S_IRGRP|S_IWGRP|S_IXGRP;
2035 mode |= (sec_access.mask & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRGRP : 0;
2036 mode |= (sec_access.mask & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWGRP : 0;
2037 mode |= (sec_access.mask & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXGRP : 0;
2041 if(sec_access.mask & GENERIC_ALL_ACCESS)
2042 mode = S_IROTH|S_IWOTH|S_IXOTH;
2044 mode |= (sec_access.mask & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IROTH : 0;
2045 mode |= (sec_access.mask & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWOTH : 0;
2046 mode |= (sec_access.mask & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXOTH : 0;
2054 /****************************************************************************
2055 Unpack a SEC_DESC into a owner, group and set of UNIX permissions.
2056 ****************************************************************************/
2058 static BOOL unpack_nt_permissions(uid_t *puser, gid_t *pgrp, mode_t *pmode, uint32 security_info_sent,
2059 SEC_DESC *psd, BOOL is_directory)
2061 extern DOM_SID global_sid_World;
2066 SEC_ACL *dacl = psd->dacl;
2073 if(security_info_sent == 0) {
2074 DEBUG(0,("unpack_unix_permissions: no security info sent !\n"));
2079 * Validate the owner and group SID's.
2082 memset(&owner_sid, '\0', sizeof(owner_sid));
2083 memset(&grp_sid, '\0', sizeof(grp_sid));
2085 DEBUG(5,("unpack_unix_permissions: validating owner_sid.\n"));
2088 * Don't immediately fail if the owner sid cannot be validated.
2089 * This may be a group chown only set.
2092 if(!validate_unix_sid( &owner_sid, &owner_rid, psd->owner_sid))
2093 DEBUG(3,("unpack_unix_permissions: unable to validate owner sid.\n"));
2094 else if(security_info_sent & OWNER_SECURITY_INFORMATION)
2095 *puser = pdb_user_rid_to_uid(owner_rid);
2098 * Don't immediately fail if the group sid cannot be validated.
2099 * This may be an owner chown only set.
2102 if(!validate_unix_sid( &grp_sid, &grp_rid, psd->grp_sid))
2103 DEBUG(3,("unpack_unix_permissions: unable to validate group sid.\n"));
2104 else if(security_info_sent & GROUP_SECURITY_INFORMATION)
2105 *pgrp = pdb_user_rid_to_gid(grp_rid);
2108 * If no DACL then this is a chown only security descriptor.
2111 if(!(security_info_sent & DACL_SECURITY_INFORMATION) || !dacl) {
2117 * Now go through the DACL and ensure that
2118 * any owner/group sids match.
2121 for(i = 0; i < dacl->num_aces; i++) {
2123 SEC_ACE *psa = &dacl->ace_list[i];
2125 if((psa->type != SEC_ACE_TYPE_ACCESS_ALLOWED) &&
2126 (psa->type != SEC_ACE_TYPE_ACCESS_DENIED)) {
2127 DEBUG(3,("unpack_unix_permissions: unable to set anything but an ALLOW or DENY ACE.\n"));
2132 * Ignore or remove bits we don't care about on a directory ACE.
2136 if(psa->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
2137 DEBUG(3,("unpack_unix_permissions: ignoring inherit only ACE.\n"));
2141 psa->flags &= ~(SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT);
2144 if(psa->flags != 0) {
2145 DEBUG(1,("unpack_unix_permissions: unable to set ACE flags (%x).\n",
2146 (unsigned int)psa->flags));
2151 * The security mask may be UNIX_ACCESS_NONE which should map into
2152 * no permissions (we overload the WRITE_OWNER bit for this) or it
2153 * should be one of the ALL/EXECUTE/READ/WRITE bits. Arrange for this
2154 * to be so. Any other bits override the UNIX_ACCESS_NONE bit.
2157 psa->info.mask &= (GENERIC_ALL_ACCESS|GENERIC_EXECUTE_ACCESS|GENERIC_WRITE_ACCESS|
2158 GENERIC_READ_ACCESS|UNIX_ACCESS_NONE|FILE_ALL_ATTRIBUTES);
2160 if(psa->info.mask != UNIX_ACCESS_NONE)
2161 psa->info.mask &= ~UNIX_ACCESS_NONE;
2163 sid_copy(&ace_sid, &psa->sid);
2165 if(sid_equal(&ace_sid, &owner_sid)) {
2167 * Map the desired permissions into owner perms.
2170 if(psa->type == SEC_ACE_TYPE_ACCESS_ALLOWED)
2171 *pmode |= map_nt_perms( psa->info, S_IRUSR);
2173 *pmode &= ~(map_nt_perms( psa->info, S_IRUSR));
2175 } else if( sid_equal(&ace_sid, &grp_sid)) {
2177 * Map the desired permissions into group perms.
2180 if(psa->type == SEC_ACE_TYPE_ACCESS_ALLOWED)
2181 *pmode |= map_nt_perms( psa->info, S_IRGRP);
2183 *pmode &= ~(map_nt_perms( psa->info, S_IRGRP));
2185 } else if( sid_equal(&ace_sid, &global_sid_World)) {
2187 * Map the desired permissions into other perms.
2190 if(psa->type == SEC_ACE_TYPE_ACCESS_ALLOWED)
2191 *pmode |= map_nt_perms( psa->info, S_IROTH);
2193 *pmode &= ~(map_nt_perms( psa->info, S_IROTH));
2196 DEBUG(0,("unpack_unix_permissions: unknown SID used in ACL.\n"));
2204 /****************************************************************************
2205 Reply to set a security descriptor. Map to UNIX perms.
2206 ****************************************************************************/
2208 static int call_nt_transact_set_security_desc(connection_struct *conn,
2209 char *inbuf, char *outbuf, int length,
2210 int bufsize, char **ppsetup,
2211 char **ppparams, char **ppdata)
2213 uint32 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
2214 char *params= *ppparams;
2215 char *data = *ppdata;
2217 SEC_DESC *psd = NULL;
2218 uint32 total_data_count = (uint32)IVAL(inbuf, smb_nts_TotalDataCount);
2219 uid_t user = (uid_t)-1;
2220 gid_t grp = (gid_t)-1;
2222 SMB_STRUCT_STAT sbuf;
2223 files_struct *fsp = NULL;
2224 uint32 security_info_sent = 0;
2225 BOOL got_dacl = False;
2227 if(!lp_nt_acl_support())
2228 return(UNIXERROR(ERRDOS,ERRnoaccess));
2230 if(total_parameter_count < 8)
2231 return(ERROR(ERRDOS,ERRbadfunc));
2233 if((fsp = file_fsp(params,0)) == NULL)
2234 return(ERROR(ERRDOS,ERRbadfid));
2236 security_info_sent = IVAL(params,4);
2238 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2239 (unsigned int)security_info_sent ));
2242 * Init the parse struct we will unmarshall from.
2245 prs_init(&pd, 0, 4, UNMARSHALL);
2248 * Setup the prs_struct to point at the memory we just
2252 prs_give_memory( &pd, data, total_data_count, False);
2255 * Finally, unmarshall from the data buffer.
2258 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2259 free_sec_desc(&psd);
2260 DEBUG(0,("call_nt_transact_set_security_desc: Error in unmarshalling \
2261 security descriptor.\n"));
2263 * Return access denied for want of a better error message..
2265 return(UNIXERROR(ERRDOS,ERRnoaccess));
2269 * Unpack the user/group/world id's and permissions.
2272 if(!unpack_nt_permissions( &user, &grp, &perms, security_info_sent, psd, fsp->is_directory)) {
2273 free_sec_desc(&psd);
2274 return(UNIXERROR(ERRDOS,ERRnoaccess));
2277 if (psd->dacl != NULL)
2280 free_sec_desc(&psd);
2283 * Get the current state of the file.
2286 if(fsp->is_directory) {
2287 if(dos_stat(fsp->fsp_name, &sbuf) != 0) {
2288 return(UNIXERROR(ERRDOS,ERRnoaccess));
2294 if(fsp->fd_ptr == NULL)
2295 ret = conn->vfs_ops.stat(dos_to_unix(fsp->fsp_name,False), &sbuf);
2297 ret = conn->vfs_ops.fstat(fsp->fd_ptr->fd,&sbuf);
2300 return(UNIXERROR(ERRDOS,ERRnoaccess));
2305 * Do we need to chown ?
2308 if((user != (uid_t)-1 || grp != (uid_t)-1) && (sbuf.st_uid != user || sbuf.st_gid != grp)) {
2310 DEBUG(3,("call_nt_transact_set_security_desc: chown %s. uid = %u, gid = %u.\n",
2311 fsp->fsp_name, (unsigned int)user, (unsigned int)grp ));
2313 if(dos_chown( fsp->fsp_name, user, grp) == -1) {
2314 DEBUG(3,("call_nt_transact_set_security_desc: chown %s, %u, %u failed. Error = %s.\n",
2315 fsp->fsp_name, (unsigned int)user, (unsigned int)grp, strerror(errno) ));
2316 return(UNIXERROR(ERRDOS,ERRnoaccess));
2320 * Recheck the current state of the file, which may have changed.
2321 * (suid/sgid bits, for instance)
2324 if(fsp->is_directory) {
2325 if(dos_stat(fsp->fsp_name, &sbuf) != 0) {
2326 return(UNIXERROR(ERRDOS,ERRnoaccess));
2332 if(fsp->fd_ptr == NULL)
2333 ret = conn->vfs_ops.stat(dos_to_unix(fsp->fsp_name,False), &sbuf);
2335 ret = conn->vfs_ops.fstat(fsp->fd_ptr->fd,&sbuf);
2338 return(UNIXERROR(ERRDOS,ERRnoaccess));
2343 * Only change security if we got a DACL.
2346 if((security_info_sent & DACL_SECURITY_INFORMATION) && got_dacl) {
2349 * Check to see if we need to change anything.
2350 * Enforce limits on modified bits *only*. Don't enforce masks
2351 * on bits not changed by the user.
2354 if(fsp->is_directory) {
2356 perms &= (lp_dir_security_mask(SNUM(conn)) | sbuf.st_mode);
2357 perms |= (lp_force_dir_security_mode(SNUM(conn)) & ( perms ^ sbuf.st_mode ));
2361 perms &= (lp_security_mask(SNUM(conn)) | sbuf.st_mode);
2362 perms |= (lp_force_security_mode(SNUM(conn)) & ( perms ^ sbuf.st_mode ));
2367 * Preserve special bits.
2370 perms |= (sbuf.st_mode & ~0777);
2373 * Do we need to chmod ?
2376 if(sbuf.st_mode != perms) {
2378 DEBUG(3,("call_nt_transact_set_security_desc: chmod %s. perms = 0%o.\n",
2379 fsp->fsp_name, (unsigned int)perms ));
2381 if(conn->vfs_ops.chmod(dos_to_unix(fsp->fsp_name, False), perms) == -1) {
2382 DEBUG(3,("call_nt_transact_set_security_desc: chmod %s, 0%o failed. Error = %s.\n",
2383 fsp->fsp_name, (unsigned int)perms, strerror(errno) ));
2384 return(UNIXERROR(ERRDOS,ERRnoaccess));
2389 send_nt_replies(inbuf, outbuf, bufsize, 0, NULL, 0, NULL, 0);
2393 /****************************************************************************
2394 Reply to IOCTL - not implemented - no plans.
2395 ****************************************************************************/
2396 static int call_nt_transact_ioctl(connection_struct *conn,
2397 char *inbuf, char *outbuf, int length,
2399 char **ppsetup, char **ppparams, char **ppdata)
2401 static BOOL logged_message = False;
2403 if(!logged_message) {
2404 DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
2405 logged_message = True; /* Only print this once... */
2407 return(ERROR(ERRSRV,ERRnosupport));
2410 /****************************************************************************
2411 Reply to a SMBNTtrans.
2412 ****************************************************************************/
2413 int reply_nttrans(connection_struct *conn,
2414 char *inbuf,char *outbuf,int length,int bufsize)
2417 #if 0 /* Not used. */
2418 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
2419 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
2420 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
2421 #endif /* Not used. */
2422 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
2423 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
2424 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
2425 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
2426 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
2427 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
2428 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
2429 uint16 function_code = SVAL( inbuf, smb_nt_Function);
2430 char *params = NULL, *data = NULL, *setup = NULL;
2431 uint32 num_params_sofar, num_data_sofar;
2433 if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
2435 * Queue this open message as we are the process of an oplock break.
2438 DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
2439 due to being in oplock break state.\n" ));
2441 push_oplock_pending_smb_message( inbuf, length);
2445 outsize = set_message(outbuf,0,0,True);
2448 * All nttrans messages we handle have smb_wct == 19 + setup_count.
2449 * Ensure this is so as a sanity check.
2452 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
2453 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2454 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
2455 return(ERROR(ERRSRV,ERRerror));
2458 /* Allocate the space for the setup, the maximum needed parameters and data */
2461 setup = (char *)malloc(setup_count);
2462 if (total_parameter_count > 0)
2463 params = (char *)malloc(total_parameter_count);
2464 if (total_data_count > 0)
2465 data = (char *)malloc(total_data_count);
2467 if ((total_parameter_count && !params) || (total_data_count && !data) ||
2468 (setup_count && !setup)) {
2469 DEBUG(0,("reply_nttrans : Out of memory\n"));
2470 return(ERROR(ERRDOS,ERRnomem));
2473 /* Copy the param and data bytes sent with this request into
2474 the params buffer */
2475 num_params_sofar = parameter_count;
2476 num_data_sofar = data_count;
2478 if (parameter_count > total_parameter_count || data_count > total_data_count)
2479 exit_server("reply_nttrans: invalid sizes in packet.\n");
2482 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
2483 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
2484 dump_data(10, setup, setup_count);
2487 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
2488 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
2489 dump_data(10, params, parameter_count);
2492 memcpy( data, smb_base(inbuf) + data_offset, data_count);
2493 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
2494 dump_data(10, data, data_count);
2497 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2498 /* We need to send an interim response then receive the rest
2499 of the parameter/data bytes */
2500 outsize = set_message(outbuf,0,0,True);
2501 send_smb(Client,outbuf);
2503 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2506 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
2508 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
2509 outsize = set_message(outbuf,0,0,True);
2511 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
2513 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
2514 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
2522 return(ERROR(ERRSRV,ERRerror));
2525 /* Revise total_params and total_data in case they have changed downwards */
2526 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
2527 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
2528 num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
2529 num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
2530 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
2531 exit_server("reply_nttrans2: data overflow in secondary nttrans packet\n");
2533 memcpy( ¶ms[ IVAL(inbuf, smb_nts_ParameterDisplacement)],
2534 smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
2535 memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
2536 smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
2540 if (Protocol >= PROTOCOL_NT1) {
2541 uint16 flg2 = SVAL(outbuf,smb_flg2);
2542 SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
2545 /* Now we must call the relevant NT_TRANS function */
2546 switch(function_code) {
2547 case NT_TRANSACT_CREATE:
2548 outsize = call_nt_transact_create(conn, inbuf, outbuf, length, bufsize,
2549 &setup, ¶ms, &data);
2551 case NT_TRANSACT_IOCTL:
2552 outsize = call_nt_transact_ioctl(conn,
2553 inbuf, outbuf, length, bufsize,
2554 &setup, ¶ms, &data);
2556 case NT_TRANSACT_SET_SECURITY_DESC:
2557 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
2559 &setup, ¶ms, &data);
2561 case NT_TRANSACT_NOTIFY_CHANGE:
2562 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
2564 &setup, ¶ms, &data);
2566 case NT_TRANSACT_RENAME:
2567 outsize = call_nt_transact_rename(conn, inbuf, outbuf, length,
2569 &setup, ¶ms, &data);
2572 case NT_TRANSACT_QUERY_SECURITY_DESC:
2573 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
2575 &setup, ¶ms, &data);
2578 /* Error in request */
2579 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
2586 return (ERROR(ERRSRV,ERRerror));
2589 /* As we do not know how many data packets will need to be
2590 returned here the various call_nt_transact_xxxx calls
2591 must send their own. Thus a call_nt_transact_xxxx routine only
2592 returns a value other than -1 when it wants to send
2602 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
2603 calls have already sent it. If outsize != -1 then it is
2604 returning an error packet. */