must add one to the extra_data size to transfer the 0 string terminator.
[Samba/gebeck_regimport.git] / source3 / smbd / nttrans.c
blob1f3bbd488e577446f429f72a03be7db992ba1d93
1 /*
2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 extern int Protocol;
24 extern int smb_read_error;
25 extern int global_oplock_break;
26 extern BOOL case_sensitive;
27 extern BOOL case_preserve;
28 extern BOOL short_case_preserve;
30 static char *known_nt_pipes[] = {
31 "\\LANMAN",
32 "\\srvsvc",
33 "\\samr",
34 "\\wkssvc",
35 "\\NETLOGON",
36 "\\ntlsa",
37 "\\ntsvcs",
38 "\\lsass",
39 "\\lsarpc",
40 "\\winreg",
41 "\\spoolss",
42 "\\netdfs",
43 NULL
46 /* Map generic permissions to file object specific permissions */
48 struct generic_mapping file_generic_mapping = {
49 FILE_GENERIC_READ,
50 FILE_GENERIC_WRITE,
51 FILE_GENERIC_EXECUTE,
52 FILE_GENERIC_ALL
55 /****************************************************************************
56 Send the required number of replies back.
57 We assume all fields other than the data fields are
58 set correctly for the type of call.
59 HACK ! Always assumes smb_setup field is zero.
60 ****************************************************************************/
62 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, NTSTATUS nt_error, char *params,
63 int paramsize, char *pdata, int datasize)
65 extern int max_send;
66 int data_to_send = datasize;
67 int params_to_send = paramsize;
68 int useable_space;
69 char *pp = params;
70 char *pd = pdata;
71 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
72 int alignment_offset = 3;
73 int data_alignment_offset = 0;
76 * Initially set the wcnt area to be 18 - this is true for all
77 * transNT replies.
80 set_message(outbuf,18,0,True);
82 if (NT_STATUS_V(nt_error))
83 ERROR_NT(nt_error);
85 /*
86 * If there genuinely are no parameters or data to send just send
87 * the empty packet.
90 if(params_to_send == 0 && data_to_send == 0) {
91 if (!send_smb(smbd_server_fd(),outbuf))
92 exit_server("send_nt_replies: send_smb failed.");
93 return 0;
97 * When sending params and data ensure that both are nicely aligned.
98 * Only do this alignment when there is also data to send - else
99 * can cause NT redirector problems.
102 if (((params_to_send % 4) != 0) && (data_to_send != 0))
103 data_alignment_offset = 4 - (params_to_send % 4);
106 * Space is bufsize minus Netbios over TCP header minus SMB header.
107 * The alignment_offset is to align the param bytes on a four byte
108 * boundary (2 bytes for data len, one byte pad).
109 * NT needs this to work correctly.
112 useable_space = bufsize - ((smb_buf(outbuf)+
113 alignment_offset+data_alignment_offset) -
114 outbuf);
117 * useable_space can never be more than max_send minus the
118 * alignment offset.
121 useable_space = MIN(useable_space,
122 max_send - (alignment_offset+data_alignment_offset));
125 while (params_to_send || data_to_send) {
128 * Calculate whether we will totally or partially fill this packet.
131 total_sent_thistime = params_to_send + data_to_send +
132 alignment_offset + data_alignment_offset;
135 * We can never send more than useable_space.
138 total_sent_thistime = MIN(total_sent_thistime, useable_space);
140 set_message(outbuf, 18, total_sent_thistime, True);
143 * Set total params and data to be sent.
146 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
147 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
150 * Calculate how many parameters and data we can fit into
151 * this packet. Parameters get precedence.
154 params_sent_thistime = MIN(params_to_send,useable_space);
155 data_sent_thistime = useable_space - params_sent_thistime;
156 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
158 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
160 if(params_sent_thistime == 0) {
161 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
162 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
163 } else {
165 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
166 * parameter bytes, however the first 4 bytes of outbuf are
167 * the Netbios over TCP header. Thus use smb_base() to subtract
168 * them from the calculation.
171 SIVAL(outbuf,smb_ntr_ParameterOffset,
172 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
174 * Absolute displacement of param bytes sent in this packet.
177 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
181 * Deal with the data portion.
184 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
186 if(data_sent_thistime == 0) {
187 SIVAL(outbuf,smb_ntr_DataOffset,0);
188 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
189 } else {
191 * The offset of the data bytes is the offset of the
192 * parameter bytes plus the number of parameters being sent this time.
195 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
196 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
197 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
201 * Copy the param bytes into the packet.
204 if(params_sent_thistime)
205 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
208 * Copy in the data bytes
211 if(data_sent_thistime)
212 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
213 data_alignment_offset,pd,data_sent_thistime);
215 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
216 params_sent_thistime, data_sent_thistime, useable_space));
217 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
218 params_to_send, data_to_send, paramsize, datasize));
220 /* Send the packet */
221 if (!send_smb(smbd_server_fd(),outbuf))
222 exit_server("send_nt_replies: send_smb failed.");
224 pp += params_sent_thistime;
225 pd += data_sent_thistime;
227 params_to_send -= params_sent_thistime;
228 data_to_send -= data_sent_thistime;
231 * Sanity check
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));
237 return -1;
241 return 0;
244 /****************************************************************************
245 Save case statics.
246 ****************************************************************************/
248 static BOOL saved_case_sensitive;
249 static BOOL saved_case_preserve;
250 static BOOL saved_short_case_preserve;
252 /****************************************************************************
253 Save case semantics.
254 ****************************************************************************/
256 static void set_posix_case_semantics(uint32 file_attributes)
258 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
259 return;
261 saved_case_sensitive = case_sensitive;
262 saved_case_preserve = case_preserve;
263 saved_short_case_preserve = short_case_preserve;
265 /* Set to POSIX. */
266 case_sensitive = True;
267 case_preserve = True;
268 short_case_preserve = True;
271 /****************************************************************************
272 Restore case semantics.
273 ****************************************************************************/
275 static void restore_case_semantics(uint32 file_attributes)
277 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
278 return;
280 case_sensitive = saved_case_sensitive;
281 case_preserve = saved_case_preserve;
282 short_case_preserve = saved_short_case_preserve;
285 /****************************************************************************
286 Utility function to map create disposition.
287 ****************************************************************************/
289 static int map_create_disposition( uint32 create_disposition)
291 int ret;
293 switch( create_disposition ) {
294 case FILE_CREATE:
295 /* create if not exist, fail if exist */
296 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL);
297 break;
298 case FILE_SUPERSEDE:
299 case FILE_OVERWRITE_IF:
300 /* create if not exist, trunc if exist */
301 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
302 break;
303 case FILE_OPEN:
304 /* fail if not exist, open if exists */
305 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN);
306 break;
307 case FILE_OPEN_IF:
308 /* create if not exist, open if exists */
309 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_OPEN);
310 break;
311 case FILE_OVERWRITE:
312 /* fail if not exist, truncate if exists */
313 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
314 break;
315 default:
316 DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
317 create_disposition ));
318 return -1;
321 DEBUG(10,("map_create_disposition: Mapped create_disposition 0x%lx to 0x%x\n",
322 (unsigned long)create_disposition, ret ));
324 return ret;
327 /****************************************************************************
328 Utility function to map share modes.
329 ****************************************************************************/
331 static int map_share_mode( char *fname, uint32 create_options,
332 uint32 *desired_access, uint32 share_access, uint32 file_attributes)
334 int smb_open_mode = -1;
337 * Convert GENERIC bits to specific bits.
340 se_map_generic(desired_access, &file_generic_mapping);
342 switch( *desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
343 case FILE_READ_DATA:
344 smb_open_mode = DOS_OPEN_RDONLY;
345 break;
346 case FILE_WRITE_DATA:
347 case FILE_APPEND_DATA:
348 case FILE_WRITE_DATA|FILE_APPEND_DATA:
349 smb_open_mode = DOS_OPEN_WRONLY;
350 break;
351 case FILE_READ_DATA|FILE_WRITE_DATA:
352 case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
353 case FILE_READ_DATA|FILE_APPEND_DATA:
354 smb_open_mode = DOS_OPEN_RDWR;
355 break;
359 * NB. For DELETE_ACCESS we should really check the
360 * directory permissions, as that is what controls
361 * delete, and for WRITE_DAC_ACCESS we should really
362 * check the ownership, as that is what controls the
363 * chmod. Note that this is *NOT* a security hole (this
364 * note is for you, Andrew) as we are not *allowing*
365 * the access at this point, the actual unlink or
366 * chown or chmod call would do this. We are just helping
367 * clients out by telling them if they have a hope
368 * of any of this succeeding. POSIX acls may still
369 * deny the real call. JRA.
372 if (smb_open_mode == -1) {
374 if(*desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS|
375 FILE_EXECUTE|FILE_READ_ATTRIBUTES|
376 FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
377 FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS)) {
378 smb_open_mode = DOS_OPEN_RDONLY;
379 } else if(*desired_access == 0) {
382 * JRA - NT seems to sometimes send desired_access as zero. play it safe
383 * and map to a stat open.
386 smb_open_mode = DOS_OPEN_RDONLY;
388 } else {
389 DEBUG(0,("map_share_mode: Incorrect value 0x%lx for desired_access to file %s\n",
390 (unsigned long)*desired_access, fname));
391 return -1;
396 * Set the special bit that means allow share delete.
397 * This is held outside the normal share mode bits at 1<<15.
398 * JRA.
401 if(share_access & FILE_SHARE_DELETE) {
402 smb_open_mode |= ALLOW_SHARE_DELETE;
403 DEBUG(10,("map_share_mode: FILE_SHARE_DELETE requested. open_mode = 0x%x\n", smb_open_mode));
407 * We need to store the intent to open for Delete. This
408 * is what determines if a delete on close flag can be set.
409 * This is the wrong way (and place) to store this, but for 2.2 this
410 * is the only practical way. JRA.
413 if(*desired_access & DELETE_ACCESS) {
414 DEBUG(10,("map_share_mode: DELETE_ACCESS requested. open_mode = 0x%x\n", smb_open_mode));
417 if (create_options & FILE_DELETE_ON_CLOSE) {
418 /* Implicit delete access is *NOT* requested... */
419 smb_open_mode |= DELETE_ON_CLOSE_FLAG;
420 DEBUG(10,("map_share_mode: FILE_DELETE_ON_CLOSE requested. open_mode = 0x%x\n", smb_open_mode));
423 /* Add in the requested share mode. */
424 switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
425 case FILE_SHARE_READ:
426 smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
427 break;
428 case FILE_SHARE_WRITE:
429 smb_open_mode |= SET_DENY_MODE(DENY_READ);
430 break;
431 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
432 smb_open_mode |= SET_DENY_MODE(DENY_NONE);
433 break;
434 case FILE_SHARE_NONE:
435 smb_open_mode |= SET_DENY_MODE(DENY_ALL);
436 break;
440 * Handle an O_SYNC request.
443 if(file_attributes & FILE_FLAG_WRITE_THROUGH)
444 smb_open_mode |= FILE_SYNC_OPENMODE;
446 DEBUG(10,("map_share_mode: Mapped desired access 0x%lx, share access 0x%lx, file attributes 0x%lx \
447 to open_mode 0x%x\n", (unsigned long)*desired_access, (unsigned long)share_access,
448 (unsigned long)file_attributes, smb_open_mode ));
450 return smb_open_mode;
453 /****************************************************************************
454 Reply to an NT create and X call on a pipe.
455 ****************************************************************************/
457 static int nt_open_pipe(char *fname, connection_struct *conn,
458 char *inbuf, char *outbuf, int *ppnum)
460 smb_np_struct *p = NULL;
462 uint16 vuid = SVAL(inbuf, smb_uid);
463 int i;
465 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
467 /* See if it is one we want to handle. */
469 if (lp_disable_spoolss() && strequal(fname, "\\spoolss"))
470 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
472 for( i = 0; known_nt_pipes[i]; i++ )
473 if( strequal(fname,known_nt_pipes[i]))
474 break;
476 if ( known_nt_pipes[i] == NULL )
477 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
479 /* Strip \\ off the name. */
480 fname++;
482 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
484 p = open_rpc_pipe_p(fname, conn, vuid);
485 if (!p)
486 return(ERROR_DOS(ERRSRV,ERRnofids));
488 *ppnum = p->pnum;
490 return 0;
493 /****************************************************************************
494 Reply to an NT create and X call for pipes.
495 ****************************************************************************/
497 static int do_ntcreate_pipe_open(connection_struct *conn,
498 char *inbuf,char *outbuf,int length,int bufsize)
500 pstring fname;
501 int ret;
502 int pnum = -1;
503 char *p = NULL;
505 srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
507 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
508 return ret;
511 * Deal with pipe return.
514 set_message(outbuf,34,0,True);
516 p = outbuf + smb_vwv2;
517 p++;
518 SSVAL(p,0,pnum);
519 p += 2;
520 SIVAL(p,0,FILE_WAS_OPENED);
521 p += 4;
522 p += 32;
523 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
524 p += 20;
525 /* File type. */
526 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
527 /* Device state. */
528 SSVAL(p,2, 0x5FF); /* ? */
530 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
532 return chain_reply(inbuf,outbuf,length,bufsize);
535 /****************************************************************************
536 Reply to an NT create and X call.
537 ****************************************************************************/
539 int reply_ntcreate_and_X(connection_struct *conn,
540 char *inbuf,char *outbuf,int length,int bufsize)
542 int result;
543 pstring fname;
544 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
545 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
546 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
547 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
548 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
549 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
550 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
551 SMB_OFF_T allocation_size = 0;
552 int smb_ofun;
553 int smb_open_mode;
554 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
555 /* Breakout the oplock request bits so we can set the
556 reply bits separately. */
557 int oplock_request = 0;
558 mode_t unixmode;
559 int fmode=0,rmode=0;
560 SMB_OFF_T file_len = 0;
561 SMB_STRUCT_STAT sbuf;
562 int smb_action = 0;
563 BOOL bad_path = False;
564 files_struct *fsp=NULL;
565 char *p = NULL;
566 time_t c_time;
567 BOOL extended_oplock_granted = False;
569 START_PROFILE(SMBntcreateX);
571 DEBUG(10,("reply_ntcreateX: flags = 0x%x, desired_access = 0x%x \
572 file_attributes = 0x%x, share_access = 0x%x, create_disposition = 0x%x \
573 create_options = 0x%x root_dir_fid = 0x%x\n", flags, desired_access, file_attributes,
574 share_access, create_disposition,
575 create_options, root_dir_fid ));
577 /* If it's an IPC, use the pipe handler. */
579 if (IS_IPC(conn)) {
580 if (lp_nt_pipe_support()) {
581 END_PROFILE(SMBntcreateX);
582 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
583 } else {
584 END_PROFILE(SMBntcreateX);
585 return(ERROR_DOS(ERRDOS,ERRnoaccess));
589 if (create_options & FILE_OPEN_BY_FILE_ID) {
590 END_PROFILE(SMBntcreateX);
591 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
595 * We need to construct the open_and_X ofun value from the
596 * NT values, as that's what our code is structured to accept.
599 if((smb_ofun = map_create_disposition( create_disposition )) == -1) {
600 END_PROFILE(SMBntcreateX);
601 return(ERROR_DOS(ERRDOS,ERRnoaccess));
605 * Get the file name.
608 if(root_dir_fid != 0) {
610 * This filename is relative to a directory fid.
612 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
613 size_t dir_name_len;
615 if(!dir_fsp) {
616 END_PROFILE(SMBntcreateX);
617 return(ERROR_DOS(ERRDOS,ERRbadfid));
620 if(!dir_fsp->is_directory) {
622 srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
625 * Check to see if this is a mac fork of some kind.
628 if( strchr_m(fname, ':')) {
629 END_PROFILE(SMBntcreateX);
630 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
632 END_PROFILE(SMBntcreateX);
633 return(ERROR_DOS(ERRDOS,ERRbadfid));
637 * Copy in the base directory name.
640 pstrcpy( fname, dir_fsp->fsp_name );
641 dir_name_len = strlen(fname);
644 * Ensure it ends in a '\'.
647 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
648 pstrcat(fname, "\\");
649 dir_name_len++;
652 srvstr_pull_buf(inbuf, &fname[dir_name_len], smb_buf(inbuf), sizeof(fname)-dir_name_len, STR_TERMINATE);
653 } else {
654 srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
657 * Check to see if this is a mac fork of some kind.
660 if( strchr_m(fname, ':')) {
661 END_PROFILE(SMBntcreateX);
662 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
667 * Now contruct the smb_open_mode value from the filename,
668 * desired access and the share access.
670 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
672 if((smb_open_mode = map_share_mode(fname, create_options, &desired_access,
673 share_access,
674 file_attributes)) == -1) {
675 END_PROFILE(SMBntcreateX);
676 return ERROR_DOS(ERRDOS,ERRnoaccess);
679 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
680 if (oplock_request) {
681 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
685 * Ordinary file or directory.
689 * Check if POSIX semantics are wanted.
692 set_posix_case_semantics(file_attributes);
694 unix_convert(fname,conn,0,&bad_path,&sbuf);
696 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
699 * If it's a request for a directory open, deal with it separately.
702 if(create_options & FILE_DIRECTORY_FILE) {
703 oplock_request = 0;
705 /* Can't open a temp directory. IFS kit test. */
706 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
707 END_PROFILE(SMBntcreateX);
708 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
711 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
713 restore_case_semantics(file_attributes);
715 if(!fsp) {
716 set_bad_path_error(errno, bad_path);
717 END_PROFILE(SMBntcreateX);
718 return(UNIXERROR(ERRDOS,ERRnoaccess));
720 } else {
722 * Ordinary file case.
725 /* NB. We have a potential bug here. If we
726 * cause an oplock break to ourselves, then we
727 * could end up processing filename related
728 * SMB requests whilst we await the oplock
729 * break response. As we may have changed the
730 * filename case semantics to be POSIX-like,
731 * this could mean a filename request could
732 * fail when it should succeed. This is a rare
733 * condition, but eventually we must arrange
734 * to restore the correct case semantics
735 * before issuing an oplock break request to
736 * our client. JRA. */
738 fsp = open_file_shared1(conn,fname,&sbuf,
739 desired_access,
740 smb_open_mode,
741 smb_ofun,unixmode, oplock_request,
742 &rmode,&smb_action);
744 if (!fsp) {
745 /* We cheat here. There are two cases we
746 * care about. One is a directory rename,
747 * where the NT client will attempt to
748 * open the source directory for
749 * DELETE access. Note that when the
750 * NT client does this it does *not*
751 * set the directory bit in the
752 * request packet. This is translated
753 * into a read/write open
754 * request. POSIX states that any open
755 * for write request on a directory
756 * will generate an EISDIR error, so
757 * we can catch this here and open a
758 * pseudo handle that is flagged as a
759 * directory. The second is an open
760 * for a permissions read only, which
761 * we handle in the open_file_stat case. JRA.
764 if(errno == EISDIR) {
767 * Fail the open if it was explicitly a non-directory file.
770 if (create_options & FILE_NON_DIRECTORY_FILE) {
771 restore_case_semantics(file_attributes);
772 SSVAL(outbuf, smb_flg2,
773 SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
774 END_PROFILE(SMBntcreateX);
775 return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
778 oplock_request = 0;
779 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
781 if(!fsp) {
782 restore_case_semantics(file_attributes);
783 set_bad_path_error(errno, bad_path);
784 END_PROFILE(SMBntcreateX);
785 return(UNIXERROR(ERRDOS,ERRnoaccess));
787 } else {
789 restore_case_semantics(file_attributes);
790 set_bad_path_error(errno, bad_path);
791 END_PROFILE(SMBntcreateX);
792 return(UNIXERROR(ERRDOS,ERRnoaccess));
797 restore_case_semantics(file_attributes);
799 file_len = sbuf.st_size;
800 fmode = dos_mode(conn,fname,&sbuf);
801 if(fmode == 0)
802 fmode = FILE_ATTRIBUTE_NORMAL;
803 if (!fsp->is_directory && (fmode & aDIR)) {
804 close_file(fsp,False);
805 END_PROFILE(SMBntcreateX);
806 return ERROR_DOS(ERRDOS,ERRnoaccess);
809 /* Save the requested allocation size. */
810 allocation_size = IVAL(inbuf,smb_ntcreate_AllocationSize);
811 #ifdef LARGE_SMB_OFF_T
812 allocation_size |= (((SMB_OFF_T)IVAL(inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
813 #endif
814 if (allocation_size && (allocation_size > file_len)) {
815 fsp->initial_allocation_size = SMB_ROUNDUP(allocation_size,SMB_ROUNDUP_ALLOCATION_SIZE);
816 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
817 close_file(fsp,False);
818 END_PROFILE(SMBntcreateX);
819 return ERROR_NT(NT_STATUS_DISK_FULL);
821 } else {
822 fsp->initial_allocation_size = SMB_ROUNDUP(file_len,SMB_ROUNDUP_ALLOCATION_SIZE);
826 * If the caller set the extended oplock request bit
827 * and we granted one (by whatever means) - set the
828 * correct bit for extended oplock reply.
831 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
832 extended_oplock_granted = True;
834 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
835 extended_oplock_granted = True;
837 #if 0
838 /* W2K sends back 42 words here ! If we do the same it breaks offline sync. Go figure... ? JRA. */
839 set_message(outbuf,42,0,True);
840 #else
841 set_message(outbuf,34,0,True);
842 #endif
844 p = outbuf + smb_vwv2;
847 * Currently as we don't support level II oplocks we just report
848 * exclusive & batch here.
851 if (extended_oplock_granted) {
852 if (flags & REQUEST_BATCH_OPLOCK) {
853 SCVAL(p,0, BATCH_OPLOCK_RETURN);
854 } else {
855 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
857 } else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
858 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
859 } else {
860 SCVAL(p,0,NO_OPLOCK_RETURN);
863 p++;
864 SSVAL(p,0,fsp->fnum);
865 p += 2;
866 if ((create_disposition == FILE_SUPERSEDE) && (smb_action == FILE_WAS_OVERWRITTEN))
867 SIVAL(p,0,FILE_WAS_SUPERSEDED);
868 else
869 SIVAL(p,0,smb_action);
870 p += 4;
872 /* Create time. */
873 c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
875 if (lp_dos_filetime_resolution(SNUM(conn))) {
876 c_time &= ~1;
877 sbuf.st_atime &= ~1;
878 sbuf.st_mtime &= ~1;
879 sbuf.st_mtime &= ~1;
882 put_long_date(p,c_time);
883 p += 8;
884 put_long_date(p,sbuf.st_atime); /* access time */
885 p += 8;
886 put_long_date(p,sbuf.st_mtime); /* write time */
887 p += 8;
888 put_long_date(p,sbuf.st_mtime); /* change time */
889 p += 8;
890 SIVAL(p,0,fmode); /* File Attributes. */
891 p += 4;
892 SOFF_T(p, 0, get_allocation_size(fsp,&sbuf));
893 p += 8;
894 SOFF_T(p,0,file_len);
895 p += 12;
896 SCVAL(p,0,fsp->is_directory ? 1 : 0);
898 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
900 result = chain_reply(inbuf,outbuf,length,bufsize);
901 END_PROFILE(SMBntcreateX);
902 return result;
905 /****************************************************************************
906 Reply to a NT_TRANSACT_CREATE call to open a pipe.
907 ****************************************************************************/
909 static int do_nt_transact_create_pipe( connection_struct *conn,
910 char *inbuf, char *outbuf, int length,
911 int bufsize, char **ppsetup, char **ppparams,
912 char **ppdata)
914 pstring fname;
915 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
916 char *params = *ppparams;
917 int ret;
918 int pnum = -1;
919 char *p = NULL;
922 * Ensure minimum number of parameters sent.
925 if(total_parameter_count < 54) {
926 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
927 return ERROR_DOS(ERRDOS,ERRnoaccess);
930 srvstr_pull(inbuf, fname, params+53, sizeof(fname), total_parameter_count-53, STR_TERMINATE);
932 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
933 return ret;
935 /* Realloc the size of parameters and data we will return */
936 params = Realloc(*ppparams, 69);
937 if(params == NULL)
938 return ERROR_DOS(ERRDOS,ERRnomem);
940 *ppparams = params;
942 memset((char *)params,'\0',69);
944 p = params;
945 SCVAL(p,0,NO_OPLOCK_RETURN);
947 p += 2;
948 SSVAL(p,0,pnum);
949 p += 2;
950 SIVAL(p,0,FILE_WAS_OPENED);
951 p += 8;
953 p += 32;
954 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
955 p += 20;
956 /* File type. */
957 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
958 /* Device state. */
959 SSVAL(p,2, 0x5FF); /* ? */
961 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
963 /* Send the required number of replies */
964 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
966 return -1;
969 /****************************************************************************
970 Internal fn to set security descriptors.
971 ****************************************************************************/
973 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
975 prs_struct pd;
976 SEC_DESC *psd = NULL;
977 TALLOC_CTX *mem_ctx;
978 BOOL ret;
980 if (sd_len == 0) {
981 return NT_STATUS_OK;
985 * Init the parse struct we will unmarshall from.
988 if ((mem_ctx = talloc_init()) == NULL) {
989 DEBUG(0,("set_sd: talloc_init failed.\n"));
990 return NT_STATUS_NO_MEMORY;
993 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
996 * Setup the prs_struct to point at the memory we just
997 * allocated.
1000 prs_give_memory( &pd, data, sd_len, False);
1003 * Finally, unmarshall from the data buffer.
1006 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1007 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1009 * Return access denied for want of a better error message..
1011 talloc_destroy(mem_ctx);
1012 return NT_STATUS_NO_MEMORY;
1015 if (psd->off_owner_sid==0)
1016 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1017 if (psd->off_grp_sid==0)
1018 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1019 if (psd->off_sacl==0)
1020 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1021 if (psd->off_dacl==0)
1022 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1024 ret = fsp->conn->vfs_ops.fset_nt_acl( fsp, fsp->fd, security_info_sent, psd);
1026 if (!ret) {
1027 talloc_destroy(mem_ctx);
1028 return NT_STATUS_ACCESS_DENIED;
1031 talloc_destroy(mem_ctx);
1033 return NT_STATUS_OK;
1036 /****************************************************************************
1037 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1038 ****************************************************************************/
1040 static int call_nt_transact_create(connection_struct *conn,
1041 char *inbuf, char *outbuf, int length,
1042 int bufsize, char **ppsetup, char **ppparams,
1043 char **ppdata)
1045 pstring fname;
1046 char *params = *ppparams;
1047 char *data = *ppdata;
1048 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
1049 /* Breakout the oplock request bits so we can set the reply bits separately. */
1050 int oplock_request = 0;
1051 mode_t unixmode;
1052 int fmode=0,rmode=0;
1053 SMB_OFF_T file_len = 0;
1054 SMB_STRUCT_STAT sbuf;
1055 int smb_action = 0;
1056 BOOL bad_path = False;
1057 files_struct *fsp = NULL;
1058 char *p = NULL;
1059 BOOL extended_oplock_granted = False;
1060 uint32 flags;
1061 uint32 desired_access;
1062 uint32 file_attributes;
1063 uint32 share_access;
1064 uint32 create_disposition;
1065 uint32 create_options;
1066 uint32 sd_len;
1067 uint16 root_dir_fid;
1068 SMB_OFF_T allocation_size = 0;
1069 int smb_ofun;
1070 int smb_open_mode;
1071 int smb_attr;
1072 time_t c_time;
1073 NTSTATUS nt_status;
1075 DEBUG(5,("call_nt_transact_create\n"));
1078 * If it's an IPC, use the pipe handler.
1081 if (IS_IPC(conn)) {
1082 if (lp_nt_pipe_support())
1083 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1084 bufsize, ppsetup, ppparams, ppdata);
1085 else
1086 return ERROR_DOS(ERRDOS,ERRnoaccess);
1090 * Ensure minimum number of parameters sent.
1093 if(total_parameter_count < 54) {
1094 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1095 return ERROR_DOS(ERRDOS,ERRnoaccess);
1098 flags = IVAL(params,0);
1099 desired_access = IVAL(params,8);
1100 file_attributes = IVAL(params,20);
1101 share_access = IVAL(params,24);
1102 create_disposition = IVAL(params,28);
1103 create_options = IVAL(params,32);
1104 sd_len = IVAL(params,36);
1105 root_dir_fid = (uint16)IVAL(params,4);
1106 smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
1108 if (create_options & FILE_OPEN_BY_FILE_ID) {
1109 END_PROFILE(SMBntcreateX);
1110 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1114 * We need to construct the open_and_X ofun value from the
1115 * NT values, as that's what our code is structured to accept.
1118 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
1119 return ERROR_DOS(ERRDOS,ERRbadmem);
1122 * Get the file name.
1125 if(root_dir_fid != 0) {
1127 * This filename is relative to a directory fid.
1130 files_struct *dir_fsp = file_fsp(params,4);
1131 size_t dir_name_len;
1133 if(!dir_fsp)
1134 return ERROR_DOS(ERRDOS,ERRbadfid);
1136 if(!dir_fsp->is_directory) {
1138 srvstr_pull(inbuf, fname, params+53, sizeof(fname), total_parameter_count-53, STR_TERMINATE);
1141 * Check to see if this is a mac fork of some kind.
1144 if( strchr_m(fname, ':'))
1145 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1147 return ERROR_DOS(ERRDOS,ERRbadfid);
1151 * Copy in the base directory name.
1154 pstrcpy( fname, dir_fsp->fsp_name );
1155 dir_name_len = strlen(fname);
1158 * Ensure it ends in a '\'.
1161 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1162 pstrcat(fname, "\\");
1163 dir_name_len++;
1166 srvstr_pull(inbuf, &fname[dir_name_len], params+53, sizeof(fname)-dir_name_len,
1167 total_parameter_count-53, STR_TERMINATE);
1168 } else {
1169 srvstr_pull(inbuf, fname, params+53, sizeof(fname), total_parameter_count-53, STR_TERMINATE);
1172 * Check to see if this is a mac fork of some kind.
1175 if( strchr_m(fname, ':'))
1176 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1180 * Now contruct the smb_open_mode value from the desired access
1181 * and the share access.
1184 if((smb_open_mode = map_share_mode( fname, create_options, &desired_access,
1185 share_access, file_attributes)) == -1)
1186 return ERROR_DOS(ERRDOS,ERRnoaccess);
1188 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1189 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1192 * Check if POSIX semantics are wanted.
1195 set_posix_case_semantics(file_attributes);
1197 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1199 unix_convert(fname,conn,0,&bad_path,&sbuf);
1201 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
1204 * If it's a request for a directory open, deal with it separately.
1207 if(create_options & FILE_DIRECTORY_FILE) {
1209 /* Can't open a temp directory. IFS kit test. */
1210 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1211 END_PROFILE(SMBntcreateX);
1212 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1215 oplock_request = 0;
1218 * We will get a create directory here if the Win32
1219 * app specified a security descriptor in the
1220 * CreateDirectory() call.
1223 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
1225 if(!fsp) {
1226 restore_case_semantics(file_attributes);
1227 set_bad_path_error(errno, bad_path);
1228 return(UNIXERROR(ERRDOS,ERRnoaccess));
1231 } else {
1234 * Ordinary file case.
1237 fsp = open_file_shared1(conn,fname,&sbuf,desired_access,
1238 smb_open_mode,smb_ofun,unixmode,
1239 oplock_request,&rmode,&smb_action);
1241 if (!fsp) {
1243 if(errno == EISDIR) {
1246 * Fail the open if it was explicitly a non-directory file.
1249 if (create_options & FILE_NON_DIRECTORY_FILE) {
1250 restore_case_semantics(file_attributes);
1251 SSVAL(outbuf, smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1252 return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1255 oplock_request = 0;
1256 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
1258 if(!fsp) {
1259 restore_case_semantics(file_attributes);
1260 set_bad_path_error(errno, bad_path);
1261 return(UNIXERROR(ERRDOS,ERRnoaccess));
1263 } else {
1264 restore_case_semantics(file_attributes);
1265 set_bad_path_error(errno, bad_path);
1266 return(UNIXERROR(ERRDOS,ERRnoaccess));
1270 file_len = sbuf.st_size;
1271 fmode = dos_mode(conn,fname,&sbuf);
1272 if(fmode == 0)
1273 fmode = FILE_ATTRIBUTE_NORMAL;
1275 if (fmode & aDIR) {
1276 close_file(fsp,False);
1277 restore_case_semantics(file_attributes);
1278 return ERROR_DOS(ERRDOS,ERRnoaccess);
1282 * If the caller set the extended oplock request bit
1283 * and we granted one (by whatever means) - set the
1284 * correct bit for extended oplock reply.
1287 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1288 extended_oplock_granted = True;
1290 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1291 extended_oplock_granted = True;
1295 * Now try and apply the desired SD.
1298 if (sd_len && !NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION))) {
1299 close_file(fsp,False);
1300 restore_case_semantics(file_attributes);
1301 return ERROR_NT(nt_status);
1304 restore_case_semantics(file_attributes);
1306 /* Save the requested allocation size. */
1307 allocation_size = IVAL(params,12);
1308 #ifdef LARGE_SMB_OFF_T
1309 allocation_size |= (((SMB_OFF_T)IVAL(params,16)) << 32);
1310 #endif
1311 if (allocation_size && (allocation_size > file_len)) {
1312 fsp->initial_allocation_size = SMB_ROUNDUP(allocation_size,SMB_ROUNDUP_ALLOCATION_SIZE);
1313 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1314 close_file(fsp,False);
1315 END_PROFILE(SMBntcreateX);
1316 return ERROR_NT(NT_STATUS_DISK_FULL);
1318 } else {
1319 fsp->initial_allocation_size = SMB_ROUNDUP(file_len,SMB_ROUNDUP_ALLOCATION_SIZE);
1322 /* Realloc the size of parameters and data we will return */
1323 params = Realloc(*ppparams, 69);
1324 if(params == NULL)
1325 return ERROR_DOS(ERRDOS,ERRnomem);
1327 *ppparams = params;
1329 memset((char *)params,'\0',69);
1331 p = params;
1332 if (extended_oplock_granted)
1333 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1334 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1335 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1336 else
1337 SCVAL(p,0,NO_OPLOCK_RETURN);
1339 p += 2;
1340 SSVAL(p,0,fsp->fnum);
1341 p += 2;
1342 if ((create_disposition == FILE_SUPERSEDE) && (smb_action == FILE_WAS_OVERWRITTEN))
1343 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1344 else
1345 SIVAL(p,0,smb_action);
1346 p += 8;
1348 /* Create time. */
1349 c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1351 if (lp_dos_filetime_resolution(SNUM(conn))) {
1352 c_time &= ~1;
1353 sbuf.st_atime &= ~1;
1354 sbuf.st_mtime &= ~1;
1355 sbuf.st_mtime &= ~1;
1358 put_long_date(p,c_time);
1359 p += 8;
1360 put_long_date(p,sbuf.st_atime); /* access time */
1361 p += 8;
1362 put_long_date(p,sbuf.st_mtime); /* write time */
1363 p += 8;
1364 put_long_date(p,sbuf.st_mtime); /* change time */
1365 p += 8;
1366 SIVAL(p,0,fmode); /* File Attributes. */
1367 p += 4;
1368 SOFF_T(p, 0, get_allocation_size(fsp,&sbuf));
1369 p += 8;
1370 SOFF_T(p,0,file_len);
1372 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1374 /* Send the required number of replies */
1375 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1377 return -1;
1380 /****************************************************************************
1381 Reply to a NT CANCEL request.
1382 ****************************************************************************/
1384 int reply_ntcancel(connection_struct *conn,
1385 char *inbuf,char *outbuf,int length,int bufsize)
1388 * Go through and cancel any pending change notifies.
1391 int mid = SVAL(inbuf,smb_mid);
1392 START_PROFILE(SMBntcancel);
1393 remove_pending_change_notify_requests_by_mid(mid);
1394 remove_pending_lock_requests_by_mid(mid);
1396 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1398 END_PROFILE(SMBntcancel);
1399 return(-1);
1402 /****************************************************************************
1403 Reply to an unsolicited SMBNTtranss - just ignore it!
1404 ****************************************************************************/
1406 int reply_nttranss(connection_struct *conn,
1407 char *inbuf,char *outbuf,int length,int bufsize)
1409 START_PROFILE(SMBnttranss);
1410 DEBUG(4,("Ignoring nttranss of length %d\n",length));
1411 END_PROFILE(SMBnttranss);
1412 return(-1);
1415 /****************************************************************************
1416 Reply to a notify change - queue the request and
1417 don't allow a directory to be opened.
1418 ****************************************************************************/
1420 static int call_nt_transact_notify_change(connection_struct *conn,
1421 char *inbuf, char *outbuf, int length,
1422 int bufsize,
1423 char **ppsetup,
1424 char **ppparams, char **ppdata)
1426 char *setup = *ppsetup;
1427 files_struct *fsp;
1428 uint32 flags;
1430 fsp = file_fsp(setup,4);
1431 flags = IVAL(setup, 0);
1433 DEBUG(3,("call_nt_transact_notify_change\n"));
1435 if(!fsp)
1436 return ERROR_DOS(ERRDOS,ERRbadfid);
1438 if((!fsp->is_directory) || (conn != fsp->conn))
1439 return ERROR_DOS(ERRDOS,ERRbadfid);
1441 if (!change_notify_set(inbuf, fsp, conn, flags))
1442 return(UNIXERROR(ERRDOS,ERRbadfid));
1444 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1445 name = %s\n", fsp->fsp_name ));
1447 return -1;
1450 /****************************************************************************
1451 Reply to an NT transact rename command.
1452 ****************************************************************************/
1454 static int call_nt_transact_rename(connection_struct *conn,
1455 char *inbuf, char *outbuf, int length,
1456 int bufsize,
1457 char **ppsetup, char **ppparams, char **ppdata)
1459 char *params = *ppparams;
1460 pstring new_name;
1461 files_struct *fsp = file_fsp(params, 0);
1462 BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1463 NTSTATUS status;
1465 CHECK_FSP(fsp, conn);
1466 srvstr_pull(inbuf, new_name, params+4, sizeof(new_name), -1, STR_TERMINATE);
1468 status = rename_internals(conn, fsp->fsp_name,
1469 new_name, replace_if_exists);
1470 if (!NT_STATUS_IS_OK(status))
1471 return ERROR_NT(status);
1474 * Rename was successful.
1476 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1478 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1479 fsp->fsp_name, new_name));
1482 * Win2k needs a changenotify request response before it will
1483 * update after a rename..
1486 process_pending_change_notify_queue((time_t)0);
1488 return -1;
1491 /******************************************************************************
1492 Fake up a completely empty SD.
1493 *******************************************************************************/
1495 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1497 extern DOM_SID global_sid_World;
1498 size_t sd_size;
1500 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1501 if(!*ppsd) {
1502 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1503 sd_size = 0;
1506 return sd_size;
1509 /****************************************************************************
1510 Reply to query a security descriptor - currently this is not implemented (it
1511 is planned to be though). Right now it just returns the same thing NT would
1512 when queried on a FAT filesystem. JRA.
1513 ****************************************************************************/
1515 static int call_nt_transact_query_security_desc(connection_struct *conn,
1516 char *inbuf, char *outbuf,
1517 int length, int bufsize,
1518 char **ppsetup, char **ppparams, char **ppdata)
1520 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1521 char *params = *ppparams;
1522 char *data = *ppdata;
1523 prs_struct pd;
1524 SEC_DESC *psd = NULL;
1525 size_t sd_size;
1526 TALLOC_CTX *mem_ctx;
1528 files_struct *fsp = file_fsp(params,0);
1530 if(!fsp)
1531 return ERROR_DOS(ERRDOS,ERRbadfid);
1533 DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1535 params = Realloc(*ppparams, 4);
1536 if(params == NULL)
1537 return ERROR_DOS(ERRDOS,ERRnomem);
1539 *ppparams = params;
1541 if ((mem_ctx = talloc_init()) == NULL) {
1542 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1543 return ERROR_DOS(ERRDOS,ERRnomem);
1547 * Get the permissions to return.
1550 if (!lp_nt_acl_support(SNUM(conn)))
1551 sd_size = get_null_nt_acl(mem_ctx, &psd);
1552 else
1553 sd_size = conn->vfs_ops.fget_nt_acl(fsp, fsp->fd, &psd);
1555 if (sd_size == 0) {
1556 talloc_destroy(mem_ctx);
1557 return(UNIXERROR(ERRDOS,ERRnoaccess));
1560 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %d.\n",(int)sd_size));
1562 SIVAL(params,0,(uint32)sd_size);
1564 if(max_data_count < sd_size) {
1566 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1567 params, 4, *ppdata, 0);
1568 talloc_destroy(mem_ctx);
1569 return -1;
1573 * Allocate the data we will point this at.
1576 data = Realloc(*ppdata, sd_size);
1577 if(data == NULL) {
1578 talloc_destroy(mem_ctx);
1579 return ERROR_DOS(ERRDOS,ERRnomem);
1582 *ppdata = data;
1584 memset(data, '\0', sd_size);
1587 * Init the parse struct we will marshall into.
1590 prs_init(&pd, 0, mem_ctx, MARSHALL);
1593 * Setup the prs_struct to point at the memory we just
1594 * allocated.
1597 prs_give_memory( &pd, data, (uint32)sd_size, False);
1600 * Finally, linearize into the outgoing buffer.
1603 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1604 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1605 security descriptor.\n"));
1607 * Return access denied for want of a better error message..
1609 talloc_destroy(mem_ctx);
1610 return(UNIXERROR(ERRDOS,ERRnoaccess));
1614 * Now we can delete the security descriptor.
1617 talloc_destroy(mem_ctx);
1619 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data, (int)sd_size);
1620 return -1;
1623 /****************************************************************************
1624 Reply to set a security descriptor. Map to UNIX perms.
1625 ****************************************************************************/
1627 static int call_nt_transact_set_security_desc(connection_struct *conn,
1628 char *inbuf, char *outbuf, int length,
1629 int bufsize, char **ppsetup,
1630 char **ppparams, char **ppdata)
1632 uint32 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1633 char *params= *ppparams;
1634 char *data = *ppdata;
1635 uint32 total_data_count = (uint32)IVAL(inbuf, smb_nts_TotalDataCount);
1636 files_struct *fsp = NULL;
1637 uint32 security_info_sent = 0;
1638 NTSTATUS nt_status;
1640 if(total_parameter_count < 8)
1641 return ERROR_DOS(ERRDOS,ERRbadfunc);
1643 if((fsp = file_fsp(params,0)) == NULL)
1644 return ERROR_DOS(ERRDOS,ERRbadfid);
1646 if(!lp_nt_acl_support(SNUM(conn)))
1647 goto done;
1649 security_info_sent = IVAL(params,4);
1651 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1652 (unsigned int)security_info_sent ));
1654 if (total_data_count == 0)
1655 return ERROR_DOS(ERRDOS, ERRnoaccess);
1657 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, total_data_count, security_info_sent)))
1658 return ERROR_NT(nt_status);
1660 done:
1662 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1663 return -1;
1666 /****************************************************************************
1667 Reply to NT IOCTL
1668 ****************************************************************************/
1669 static int call_nt_transact_ioctl(connection_struct *conn,
1670 char *inbuf, char *outbuf, int length,
1671 int bufsize,
1672 char **ppsetup, int setup_count,
1673 char **ppparams, int parameter_count,
1674 char **ppdata, int data_count)
1676 unsigned fnum, control;
1677 static BOOL logged_message;
1679 if (setup_count != 8) {
1680 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1681 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1684 fnum = SVAL(*ppsetup, 4);
1685 control = IVAL(*ppsetup, 0);
1687 DEBUG(6,("call_nt_transact_ioctl: fnum=%d control=0x%x\n",
1688 fnum, control));
1690 switch (control) {
1691 case NTIOCTL_SET_SPARSE:
1692 /* pretend this succeeded - tho strictly we should
1693 mark the file sparse (if the local fs supports it)
1694 so we can know if we need to pre-allocate or not */
1695 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1696 return -1;
1698 default:
1699 if (!logged_message) {
1700 logged_message = True; /* Only print this once... */
1701 DEBUG(3,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
1702 control));
1706 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1709 /****************************************************************************
1710 Reply to a SMBNTtrans.
1711 ****************************************************************************/
1713 int reply_nttrans(connection_struct *conn,
1714 char *inbuf,char *outbuf,int length,int bufsize)
1716 int outsize = 0;
1717 #if 0 /* Not used. */
1718 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
1719 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
1720 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1721 #endif /* Not used. */
1722 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
1723 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
1724 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
1725 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
1726 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
1727 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
1728 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
1729 uint16 function_code = SVAL( inbuf, smb_nt_Function);
1730 char *params = NULL, *data = NULL, *setup = NULL;
1731 uint32 num_params_sofar, num_data_sofar;
1732 START_PROFILE(SMBnttrans);
1734 if(global_oplock_break &&
1735 ((function_code == NT_TRANSACT_CREATE) ||
1736 (function_code == NT_TRANSACT_RENAME))) {
1738 * Queue this open message as we are the process of an oplock break.
1741 DEBUG(2,("reply_nttrans: queueing message code 0x%x \
1742 due to being in oplock break state.\n", (unsigned int)function_code ));
1744 push_oplock_pending_smb_message( inbuf, length);
1745 END_PROFILE(SMBnttrans);
1746 return -1;
1749 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
1750 END_PROFILE(SMBnttrans);
1751 return ERROR_DOS(ERRSRV,ERRaccess);
1754 outsize = set_message(outbuf,0,0,True);
1757 * All nttrans messages we handle have smb_wct == 19 + setup_count.
1758 * Ensure this is so as a sanity check.
1761 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
1762 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
1763 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
1764 END_PROFILE(SMBnttrans);
1765 return ERROR_DOS(ERRSRV,ERRerror);
1768 /* Allocate the space for the setup, the maximum needed parameters and data */
1770 if(setup_count > 0)
1771 setup = (char *)malloc(setup_count);
1772 if (total_parameter_count > 0)
1773 params = (char *)malloc(total_parameter_count);
1774 if (total_data_count > 0)
1775 data = (char *)malloc(total_data_count);
1777 if ((total_parameter_count && !params) || (total_data_count && !data) ||
1778 (setup_count && !setup)) {
1779 SAFE_FREE(setup);
1780 SAFE_FREE(params);
1781 SAFE_FREE(data);
1782 DEBUG(0,("reply_nttrans : Out of memory\n"));
1783 END_PROFILE(SMBnttrans);
1784 return ERROR_DOS(ERRDOS,ERRnomem);
1787 /* Copy the param and data bytes sent with this request into the params buffer */
1788 num_params_sofar = parameter_count;
1789 num_data_sofar = data_count;
1791 if (parameter_count > total_parameter_count || data_count > total_data_count)
1792 exit_server("reply_nttrans: invalid sizes in packet.");
1794 if(setup) {
1795 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
1796 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
1797 dump_data(10, setup, setup_count);
1799 if(params) {
1800 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
1801 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
1802 dump_data(10, params, parameter_count);
1804 if(data) {
1805 memcpy( data, smb_base(inbuf) + data_offset, data_count);
1806 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
1807 dump_data(10, data, data_count);
1810 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1811 /* We need to send an interim response then receive the rest
1812 of the parameter/data bytes */
1813 outsize = set_message(outbuf,0,0,True);
1814 if (!send_smb(smbd_server_fd(),outbuf))
1815 exit_server("reply_nttrans: send_smb failed.");
1817 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
1818 BOOL ret;
1820 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
1822 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
1823 outsize = set_message(outbuf,0,0,True);
1824 if(ret) {
1825 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
1826 } else {
1827 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
1828 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
1830 SAFE_FREE(params);
1831 SAFE_FREE(data);
1832 SAFE_FREE(setup);
1833 END_PROFILE(SMBnttrans);
1834 return ERROR_DOS(ERRSRV,ERRerror);
1837 /* Revise total_params and total_data in case they have changed downwards */
1838 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1839 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
1840 num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
1841 num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
1842 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
1843 exit_server("reply_nttrans2: data overflow in secondary nttrans packet");
1845 memcpy( &params[ IVAL(inbuf, smb_nts_ParameterDisplacement)],
1846 smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
1847 memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
1848 smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
1852 if (Protocol >= PROTOCOL_NT1)
1853 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | FLAGS2_IS_LONG_NAME);
1855 /* Now we must call the relevant NT_TRANS function */
1856 switch(function_code) {
1857 case NT_TRANSACT_CREATE:
1858 START_PROFILE_NESTED(NT_transact_create);
1859 outsize = call_nt_transact_create(conn, inbuf, outbuf,
1860 length, bufsize,
1861 &setup, &params, &data);
1862 END_PROFILE_NESTED(NT_transact_create);
1863 break;
1864 case NT_TRANSACT_IOCTL:
1865 START_PROFILE_NESTED(NT_transact_ioctl);
1866 outsize = call_nt_transact_ioctl(conn, inbuf, outbuf,
1867 length, bufsize,
1868 &setup, setup_count,
1869 &params, parameter_count,
1870 &data, data_count);
1871 END_PROFILE_NESTED(NT_transact_ioctl);
1872 break;
1873 case NT_TRANSACT_SET_SECURITY_DESC:
1874 START_PROFILE_NESTED(NT_transact_set_security_desc);
1875 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
1876 length, bufsize,
1877 &setup, &params, &data);
1878 END_PROFILE_NESTED(NT_transact_set_security_desc);
1879 break;
1880 case NT_TRANSACT_NOTIFY_CHANGE:
1881 START_PROFILE_NESTED(NT_transact_notify_change);
1882 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
1883 length, bufsize,
1884 &setup, &params, &data);
1885 END_PROFILE_NESTED(NT_transact_notify_change);
1886 break;
1887 case NT_TRANSACT_RENAME:
1888 START_PROFILE_NESTED(NT_transact_rename);
1889 outsize = call_nt_transact_rename(conn, inbuf, outbuf,
1890 length, bufsize,
1891 &setup, &params, &data);
1892 END_PROFILE_NESTED(NT_transact_rename);
1893 break;
1895 case NT_TRANSACT_QUERY_SECURITY_DESC:
1896 START_PROFILE_NESTED(NT_transact_query_security_desc);
1897 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
1898 length, bufsize,
1899 &setup, &params, &data);
1900 END_PROFILE_NESTED(NT_transact_query_security_desc);
1901 break;
1902 default:
1903 /* Error in request */
1904 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
1905 SAFE_FREE(setup);
1906 SAFE_FREE(params);
1907 SAFE_FREE(data);
1908 END_PROFILE(SMBnttrans);
1909 return ERROR_DOS(ERRSRV,ERRerror);
1912 /* As we do not know how many data packets will need to be
1913 returned here the various call_nt_transact_xxxx calls
1914 must send their own. Thus a call_nt_transact_xxxx routine only
1915 returns a value other than -1 when it wants to send
1916 an error packet.
1919 SAFE_FREE(setup);
1920 SAFE_FREE(params);
1921 SAFE_FREE(data);
1922 END_PROFILE(SMBnttrans);
1923 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
1924 calls have already sent it. If outsize != -1 then it is
1925 returning an error packet. */