Prefix VFS API macros with SMB_ for consistency and to avoid problems with VFS_ macro...
[Samba/gebeck_regimport.git] / source / smbd / nttrans.c
blob3ddf191ef60fd3b1e5aaf0d35feae73081217234
1 /*
2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-1998
5 Copyright (C) Stefan (metze) Metzmacher 2003
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.
22 #include "includes.h"
24 extern int Protocol;
25 extern int smb_read_error;
26 extern int global_oplock_break;
27 extern BOOL case_sensitive;
28 extern BOOL case_preserve;
29 extern BOOL short_case_preserve;
30 extern struct current_user current_user;
32 static const char *known_nt_pipes[] = {
33 "\\LANMAN",
34 "\\srvsvc",
35 "\\samr",
36 "\\wkssvc",
37 "\\NETLOGON",
38 "\\ntlsa",
39 "\\ntsvcs",
40 "\\lsass",
41 "\\lsarpc",
42 "\\winreg",
43 "\\spoolss",
44 "\\netdfs",
45 "\\rpcecho",
46 NULL
49 /* Map generic permissions to file object specific permissions */
51 struct generic_mapping file_generic_mapping = {
52 FILE_GENERIC_READ,
53 FILE_GENERIC_WRITE,
54 FILE_GENERIC_EXECUTE,
55 FILE_GENERIC_ALL
58 char *nttrans_realloc(char **ptr, size_t size)
60 char *tptr = NULL;
61 if (ptr==NULL)
62 smb_panic("nttrans_realloc() called with NULL ptr\n");
64 tptr = Realloc_zero(*ptr, size);
65 if(tptr == NULL) {
66 *ptr = NULL;
67 return NULL;
70 *ptr = tptr;
72 return tptr;
76 /****************************************************************************
77 Send the required number of replies back.
78 We assume all fields other than the data fields are
79 set correctly for the type of call.
80 HACK ! Always assumes smb_setup field is zero.
81 ****************************************************************************/
83 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, NTSTATUS nt_error, char *params,
84 int paramsize, char *pdata, int datasize)
86 extern int max_send;
87 int data_to_send = datasize;
88 int params_to_send = paramsize;
89 int useable_space;
90 char *pp = params;
91 char *pd = pdata;
92 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
93 int alignment_offset = 3;
94 int data_alignment_offset = 0;
97 * Initially set the wcnt area to be 18 - this is true for all
98 * transNT replies.
101 set_message(outbuf,18,0,True);
103 if (NT_STATUS_V(nt_error))
104 ERROR_NT(nt_error);
107 * If there genuinely are no parameters or data to send just send
108 * the empty packet.
111 if(params_to_send == 0 && data_to_send == 0) {
112 if (!send_smb(smbd_server_fd(),outbuf))
113 exit_server("send_nt_replies: send_smb failed.");
114 return 0;
118 * When sending params and data ensure that both are nicely aligned.
119 * Only do this alignment when there is also data to send - else
120 * can cause NT redirector problems.
123 if (((params_to_send % 4) != 0) && (data_to_send != 0))
124 data_alignment_offset = 4 - (params_to_send % 4);
127 * Space is bufsize minus Netbios over TCP header minus SMB header.
128 * The alignment_offset is to align the param bytes on a four byte
129 * boundary (2 bytes for data len, one byte pad).
130 * NT needs this to work correctly.
133 useable_space = bufsize - ((smb_buf(outbuf)+
134 alignment_offset+data_alignment_offset) -
135 outbuf);
138 * useable_space can never be more than max_send minus the
139 * alignment offset.
142 useable_space = MIN(useable_space,
143 max_send - (alignment_offset+data_alignment_offset));
146 while (params_to_send || data_to_send) {
149 * Calculate whether we will totally or partially fill this packet.
152 total_sent_thistime = params_to_send + data_to_send +
153 alignment_offset + data_alignment_offset;
156 * We can never send more than useable_space.
159 total_sent_thistime = MIN(total_sent_thistime, useable_space);
161 set_message(outbuf, 18, total_sent_thistime, True);
164 * Set total params and data to be sent.
167 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
168 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
171 * Calculate how many parameters and data we can fit into
172 * this packet. Parameters get precedence.
175 params_sent_thistime = MIN(params_to_send,useable_space);
176 data_sent_thistime = useable_space - params_sent_thistime;
177 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
179 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
181 if(params_sent_thistime == 0) {
182 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
183 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
184 } else {
186 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
187 * parameter bytes, however the first 4 bytes of outbuf are
188 * the Netbios over TCP header. Thus use smb_base() to subtract
189 * them from the calculation.
192 SIVAL(outbuf,smb_ntr_ParameterOffset,
193 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
195 * Absolute displacement of param bytes sent in this packet.
198 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
202 * Deal with the data portion.
205 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
207 if(data_sent_thistime == 0) {
208 SIVAL(outbuf,smb_ntr_DataOffset,0);
209 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
210 } else {
212 * The offset of the data bytes is the offset of the
213 * parameter bytes plus the number of parameters being sent this time.
216 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
217 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
218 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
222 * Copy the param bytes into the packet.
225 if(params_sent_thistime)
226 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
229 * Copy in the data bytes
232 if(data_sent_thistime)
233 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
234 data_alignment_offset,pd,data_sent_thistime);
236 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
237 params_sent_thistime, data_sent_thistime, useable_space));
238 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
239 params_to_send, data_to_send, paramsize, datasize));
241 /* Send the packet */
242 if (!send_smb(smbd_server_fd(),outbuf))
243 exit_server("send_nt_replies: send_smb failed.");
245 pp += params_sent_thistime;
246 pd += data_sent_thistime;
248 params_to_send -= params_sent_thistime;
249 data_to_send -= data_sent_thistime;
252 * Sanity check
255 if(params_to_send < 0 || data_to_send < 0) {
256 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
257 params_to_send, data_to_send));
258 return -1;
262 return 0;
265 /****************************************************************************
266 Save case statics.
267 ****************************************************************************/
269 static BOOL saved_case_sensitive;
270 static BOOL saved_case_preserve;
271 static BOOL saved_short_case_preserve;
273 /****************************************************************************
274 Save case semantics.
275 ****************************************************************************/
277 static void set_posix_case_semantics(uint32 file_attributes)
279 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
280 return;
282 saved_case_sensitive = case_sensitive;
283 saved_case_preserve = case_preserve;
284 saved_short_case_preserve = short_case_preserve;
286 /* Set to POSIX. */
287 case_sensitive = True;
288 case_preserve = True;
289 short_case_preserve = True;
292 /****************************************************************************
293 Restore case semantics.
294 ****************************************************************************/
296 static void restore_case_semantics(uint32 file_attributes)
298 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
299 return;
301 case_sensitive = saved_case_sensitive;
302 case_preserve = saved_case_preserve;
303 short_case_preserve = saved_short_case_preserve;
306 /****************************************************************************
307 Utility function to map create disposition.
308 ****************************************************************************/
310 static int map_create_disposition( uint32 create_disposition)
312 int ret;
314 switch( create_disposition ) {
315 case FILE_CREATE:
316 /* create if not exist, fail if exist */
317 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL);
318 break;
319 case FILE_SUPERSEDE:
320 case FILE_OVERWRITE_IF:
321 /* create if not exist, trunc if exist */
322 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
323 break;
324 case FILE_OPEN:
325 /* fail if not exist, open if exists */
326 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN);
327 break;
328 case FILE_OPEN_IF:
329 /* create if not exist, open if exists */
330 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_OPEN);
331 break;
332 case FILE_OVERWRITE:
333 /* fail if not exist, truncate if exists */
334 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
335 break;
336 default:
337 DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
338 create_disposition ));
339 return -1;
342 DEBUG(10,("map_create_disposition: Mapped create_disposition 0x%lx to 0x%x\n",
343 (unsigned long)create_disposition, ret ));
345 return ret;
348 /****************************************************************************
349 Utility function to map share modes.
350 ****************************************************************************/
352 static int map_share_mode( char *fname, uint32 create_options,
353 uint32 *desired_access, uint32 share_access, uint32 file_attributes)
355 int smb_open_mode = -1;
358 * Convert GENERIC bits to specific bits.
361 se_map_generic(desired_access, &file_generic_mapping);
363 switch( *desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
364 case FILE_READ_DATA:
365 smb_open_mode = DOS_OPEN_RDONLY;
366 break;
367 case FILE_WRITE_DATA:
368 case FILE_APPEND_DATA:
369 case FILE_WRITE_DATA|FILE_APPEND_DATA:
370 smb_open_mode = DOS_OPEN_WRONLY;
371 break;
372 case FILE_READ_DATA|FILE_WRITE_DATA:
373 case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
374 case FILE_READ_DATA|FILE_APPEND_DATA:
375 smb_open_mode = DOS_OPEN_RDWR;
376 break;
380 * NB. For DELETE_ACCESS we should really check the
381 * directory permissions, as that is what controls
382 * delete, and for WRITE_DAC_ACCESS we should really
383 * check the ownership, as that is what controls the
384 * chmod. Note that this is *NOT* a security hole (this
385 * note is for you, Andrew) as we are not *allowing*
386 * the access at this point, the actual unlink or
387 * chown or chmod call would do this. We are just helping
388 * clients out by telling them if they have a hope
389 * of any of this succeeding. POSIX acls may still
390 * deny the real call. JRA.
393 if (smb_open_mode == -1) {
395 if(*desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|SYNCHRONIZE_ACCESS|
396 FILE_EXECUTE|FILE_READ_ATTRIBUTES|
397 FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
398 FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS)) {
399 smb_open_mode = DOS_OPEN_RDONLY;
400 } else if(*desired_access == 0) {
403 * JRA - NT seems to sometimes send desired_access as zero. play it safe
404 * and map to a stat open.
407 smb_open_mode = DOS_OPEN_RDONLY;
409 } else {
410 DEBUG(0,("map_share_mode: Incorrect value 0x%lx for desired_access to file %s\n",
411 (unsigned long)*desired_access, fname));
412 return -1;
417 * Set the special bit that means allow share delete.
418 * This is held outside the normal share mode bits at 1<<15.
419 * JRA.
422 if(share_access & FILE_SHARE_DELETE) {
423 smb_open_mode |= ALLOW_SHARE_DELETE;
424 DEBUG(10,("map_share_mode: FILE_SHARE_DELETE requested. open_mode = 0x%x\n", smb_open_mode));
428 * We need to store the intent to open for Delete. This
429 * is what determines if a delete on close flag can be set.
430 * This is the wrong way (and place) to store this, but for 2.2 this
431 * is the only practical way. JRA.
434 if(*desired_access & DELETE_ACCESS) {
435 DEBUG(10,("map_share_mode: DELETE_ACCESS requested. open_mode = 0x%x\n", smb_open_mode));
438 if (create_options & FILE_DELETE_ON_CLOSE) {
439 /* Implicit delete access is *NOT* requested... */
440 smb_open_mode |= DELETE_ON_CLOSE_FLAG;
441 DEBUG(10,("map_share_mode: FILE_DELETE_ON_CLOSE requested. open_mode = 0x%x\n", smb_open_mode));
444 /* Add in the requested share mode. */
445 switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
446 case FILE_SHARE_READ:
447 smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
448 break;
449 case FILE_SHARE_WRITE:
450 smb_open_mode |= SET_DENY_MODE(DENY_READ);
451 break;
452 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
453 smb_open_mode |= SET_DENY_MODE(DENY_NONE);
454 break;
455 case FILE_SHARE_NONE:
456 smb_open_mode |= SET_DENY_MODE(DENY_ALL);
457 break;
461 * Handle an O_SYNC request.
464 if(file_attributes & FILE_FLAG_WRITE_THROUGH)
465 smb_open_mode |= FILE_SYNC_OPENMODE;
467 DEBUG(10,("map_share_mode: Mapped desired access 0x%lx, share access 0x%lx, file attributes 0x%lx \
468 to open_mode 0x%x\n", (unsigned long)*desired_access, (unsigned long)share_access,
469 (unsigned long)file_attributes, smb_open_mode ));
471 return smb_open_mode;
474 /****************************************************************************
475 Reply to an NT create and X call on a pipe.
476 ****************************************************************************/
478 static int nt_open_pipe(char *fname, connection_struct *conn,
479 char *inbuf, char *outbuf, int *ppnum)
481 smb_np_struct *p = NULL;
483 uint16 vuid = SVAL(inbuf, smb_uid);
484 int i;
486 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
488 /* See if it is one we want to handle. */
490 if (lp_disable_spoolss() && strequal(fname, "\\spoolss"))
491 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
493 for( i = 0; known_nt_pipes[i]; i++ )
494 if( strequal(fname,known_nt_pipes[i]))
495 break;
497 if ( known_nt_pipes[i] == NULL )
498 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
500 /* Strip \\ off the name. */
501 fname++;
503 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
505 p = open_rpc_pipe_p(fname, conn, vuid);
506 if (!p)
507 return(ERROR_DOS(ERRSRV,ERRnofids));
509 *ppnum = p->pnum;
511 return 0;
514 /****************************************************************************
515 Reply to an NT create and X call for pipes.
516 ****************************************************************************/
518 static int do_ntcreate_pipe_open(connection_struct *conn,
519 char *inbuf,char *outbuf,int length,int bufsize)
521 pstring fname;
522 int ret;
523 int pnum = -1;
524 char *p = NULL;
526 srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
528 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
529 return ret;
532 * Deal with pipe return.
535 set_message(outbuf,34,0,True);
537 p = outbuf + smb_vwv2;
538 p++;
539 SSVAL(p,0,pnum);
540 p += 2;
541 SIVAL(p,0,FILE_WAS_OPENED);
542 p += 4;
543 p += 32;
544 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
545 p += 20;
546 /* File type. */
547 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
548 /* Device state. */
549 SSVAL(p,2, 0x5FF); /* ? */
551 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
553 return chain_reply(inbuf,outbuf,length,bufsize);
556 /****************************************************************************
557 Reply to an NT create and X call.
558 ****************************************************************************/
560 int reply_ntcreate_and_X(connection_struct *conn,
561 char *inbuf,char *outbuf,int length,int bufsize)
563 int result;
564 pstring fname;
565 enum FAKE_FILE_TYPE fake_file_type = 0;
566 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
567 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
568 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
569 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
570 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
571 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
572 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
573 SMB_BIG_UINT allocation_size = 0;
574 int smb_ofun;
575 int smb_open_mode;
576 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
577 /* Breakout the oplock request bits so we can set the
578 reply bits separately. */
579 int oplock_request = 0;
580 mode_t unixmode;
581 int fmode=0,rmode=0;
582 SMB_OFF_T file_len = 0;
583 SMB_STRUCT_STAT sbuf;
584 int smb_action = 0;
585 BOOL bad_path = False;
586 files_struct *fsp=NULL;
587 char *p = NULL;
588 time_t c_time;
589 BOOL extended_oplock_granted = False;
591 START_PROFILE(SMBntcreateX);
593 DEBUG(10,("reply_ntcreateX: flags = 0x%x, desired_access = 0x%x \
594 file_attributes = 0x%x, share_access = 0x%x, create_disposition = 0x%x \
595 create_options = 0x%x root_dir_fid = 0x%x\n", flags, desired_access, file_attributes,
596 share_access, create_disposition,
597 create_options, root_dir_fid ));
599 /* If it's an IPC, use the pipe handler. */
601 if (IS_IPC(conn)) {
602 if (lp_nt_pipe_support()) {
603 END_PROFILE(SMBntcreateX);
604 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
605 } else {
606 END_PROFILE(SMBntcreateX);
607 return(ERROR_DOS(ERRDOS,ERRnoaccess));
611 if (create_options & FILE_OPEN_BY_FILE_ID) {
612 END_PROFILE(SMBntcreateX);
613 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
617 * We need to construct the open_and_X ofun value from the
618 * NT values, as that's what our code is structured to accept.
621 if((smb_ofun = map_create_disposition( create_disposition )) == -1) {
622 END_PROFILE(SMBntcreateX);
623 return(ERROR_DOS(ERRDOS,ERRnoaccess));
627 * Get the file name.
630 if(root_dir_fid != 0) {
632 * This filename is relative to a directory fid.
634 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
635 size_t dir_name_len;
637 if(!dir_fsp) {
638 END_PROFILE(SMBntcreateX);
639 return(ERROR_DOS(ERRDOS,ERRbadfid));
642 if(!dir_fsp->is_directory) {
644 srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
647 * Check to see if this is a mac fork of some kind.
650 if( strchr_m(fname, ':')) {
651 END_PROFILE(SMBntcreateX);
652 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
657 we need to handle the case when we get a
658 relative open relative to a file and the
659 pathname is blank - this is a reopen!
660 (hint from demyn plantenberg)
664 END_PROFILE(SMBntcreateX);
665 return(ERROR_DOS(ERRDOS,ERRbadfid));
669 * Copy in the base directory name.
672 pstrcpy( fname, dir_fsp->fsp_name );
673 dir_name_len = strlen(fname);
676 * Ensure it ends in a '\'.
679 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
680 pstrcat(fname, "\\");
681 dir_name_len++;
684 srvstr_pull_buf(inbuf, &fname[dir_name_len], smb_buf(inbuf), sizeof(fname)-dir_name_len, STR_TERMINATE);
685 } else {
686 srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
689 * Check to see if this is a mac fork of some kind.
692 if( strchr_m(fname, ':')) {
694 #ifdef WITH_QUOTAS
695 if ((fake_file_type=is_fake_file(fname))!=0) {
697 * here we go! support for changing the disk quotas --metze
699 * we need to fake up to open this MAGIC QUOTA file
700 * and return a valid FID
702 * w2k close this file directly after openening
703 * xp also tries a QUERY_FILE_INFO on the file and then close it
705 } else {
706 #endif
707 END_PROFILE(SMBntcreateX);
708 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
709 #ifdef WITH_QUOTAS
711 #endif
716 * Now contruct the smb_open_mode value from the filename,
717 * desired access and the share access.
719 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
721 if((smb_open_mode = map_share_mode(fname, create_options, &desired_access,
722 share_access,
723 file_attributes)) == -1) {
724 END_PROFILE(SMBntcreateX);
725 return ERROR_DOS(ERRDOS,ERRnoaccess);
728 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
729 if (oplock_request) {
730 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
734 * Ordinary file or directory.
738 * Check if POSIX semantics are wanted.
741 set_posix_case_semantics(file_attributes);
743 unix_convert(fname,conn,0,&bad_path,&sbuf);
745 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
748 * If it's a request for a directory open, deal with it separately.
751 if(create_options & FILE_DIRECTORY_FILE) {
752 oplock_request = 0;
754 /* Can't open a temp directory. IFS kit test. */
755 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
756 END_PROFILE(SMBntcreateX);
757 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
760 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
762 restore_case_semantics(file_attributes);
764 if(!fsp) {
765 set_bad_path_error(errno, bad_path);
766 END_PROFILE(SMBntcreateX);
767 return(UNIXERROR(ERRDOS,ERRnoaccess));
769 } else {
771 * Ordinary file case.
774 /* NB. We have a potential bug here. If we
775 * cause an oplock break to ourselves, then we
776 * could end up processing filename related
777 * SMB requests whilst we await the oplock
778 * break response. As we may have changed the
779 * filename case semantics to be POSIX-like,
780 * this could mean a filename request could
781 * fail when it should succeed. This is a rare
782 * condition, but eventually we must arrange
783 * to restore the correct case semantics
784 * before issuing an oplock break request to
785 * our client. JRA. */
787 if (fake_file_type==0) {
788 fsp = open_file_shared1(conn,fname,&sbuf,
789 desired_access,
790 smb_open_mode,
791 smb_ofun,unixmode, oplock_request,
792 &rmode,&smb_action);
793 } else {
794 /* to open a fake_file --metze */
795 fsp = open_fake_file_shared1(fake_file_type,conn,fname,&sbuf,
796 desired_access,
797 smb_open_mode,
798 smb_ofun,unixmode, oplock_request,
799 &rmode,&smb_action);
802 if (!fsp) {
803 /* We cheat here. There are two cases we
804 * care about. One is a directory rename,
805 * where the NT client will attempt to
806 * open the source directory for
807 * DELETE access. Note that when the
808 * NT client does this it does *not*
809 * set the directory bit in the
810 * request packet. This is translated
811 * into a read/write open
812 * request. POSIX states that any open
813 * for write request on a directory
814 * will generate an EISDIR error, so
815 * we can catch this here and open a
816 * pseudo handle that is flagged as a
817 * directory. The second is an open
818 * for a permissions read only, which
819 * we handle in the open_file_stat case. JRA.
822 if(errno == EISDIR) {
825 * Fail the open if it was explicitly a non-directory file.
828 if (create_options & FILE_NON_DIRECTORY_FILE) {
829 restore_case_semantics(file_attributes);
830 SSVAL(outbuf, smb_flg2,
831 SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
832 END_PROFILE(SMBntcreateX);
833 return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
836 oplock_request = 0;
837 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
839 if(!fsp) {
840 restore_case_semantics(file_attributes);
841 set_bad_path_error(errno, bad_path);
842 END_PROFILE(SMBntcreateX);
843 return(UNIXERROR(ERRDOS,ERRnoaccess));
845 } else {
847 restore_case_semantics(file_attributes);
848 set_bad_path_error(errno, bad_path);
849 END_PROFILE(SMBntcreateX);
850 return(UNIXERROR(ERRDOS,ERRnoaccess));
855 restore_case_semantics(file_attributes);
857 file_len = sbuf.st_size;
858 fmode = dos_mode(conn,fname,&sbuf);
859 if(fmode == 0)
860 fmode = FILE_ATTRIBUTE_NORMAL;
861 if (!fsp->is_directory && (fmode & aDIR)) {
862 close_file(fsp,False);
863 END_PROFILE(SMBntcreateX);
864 return ERROR_DOS(ERRDOS,ERRnoaccess);
867 /* Save the requested allocation size. */
868 allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize);
869 #ifdef LARGE_SMB_OFF_T
870 allocation_size |= (((SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
871 #endif
872 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
873 fsp->initial_allocation_size = SMB_ROUNDUP(allocation_size,SMB_ROUNDUP_ALLOCATION_SIZE);
874 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
875 close_file(fsp,False);
876 END_PROFILE(SMBntcreateX);
877 return ERROR_NT(NT_STATUS_DISK_FULL);
879 } else {
880 fsp->initial_allocation_size = SMB_ROUNDUP(((SMB_BIG_UINT)file_len),SMB_ROUNDUP_ALLOCATION_SIZE);
884 * If the caller set the extended oplock request bit
885 * and we granted one (by whatever means) - set the
886 * correct bit for extended oplock reply.
889 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
890 extended_oplock_granted = True;
892 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
893 extended_oplock_granted = True;
895 #if 0
896 /* W2K sends back 42 words here ! If we do the same it breaks offline sync. Go figure... ? JRA. */
897 set_message(outbuf,42,0,True);
898 #else
899 set_message(outbuf,34,0,True);
900 #endif
902 p = outbuf + smb_vwv2;
905 * Currently as we don't support level II oplocks we just report
906 * exclusive & batch here.
909 if (extended_oplock_granted) {
910 if (flags & REQUEST_BATCH_OPLOCK) {
911 SCVAL(p,0, BATCH_OPLOCK_RETURN);
912 } else {
913 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
915 } else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
916 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
917 } else {
918 SCVAL(p,0,NO_OPLOCK_RETURN);
921 p++;
922 SSVAL(p,0,fsp->fnum);
923 p += 2;
924 if ((create_disposition == FILE_SUPERSEDE) && (smb_action == FILE_WAS_OVERWRITTEN))
925 SIVAL(p,0,FILE_WAS_SUPERSEDED);
926 else
927 SIVAL(p,0,smb_action);
928 p += 4;
930 /* Create time. */
931 c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
933 if (lp_dos_filetime_resolution(SNUM(conn))) {
934 c_time &= ~1;
935 sbuf.st_atime &= ~1;
936 sbuf.st_mtime &= ~1;
937 sbuf.st_mtime &= ~1;
940 put_long_date(p,c_time);
941 p += 8;
942 put_long_date(p,sbuf.st_atime); /* access time */
943 p += 8;
944 put_long_date(p,sbuf.st_mtime); /* write time */
945 p += 8;
946 put_long_date(p,sbuf.st_mtime); /* change time */
947 p += 8;
948 SIVAL(p,0,fmode); /* File Attributes. */
949 p += 4;
950 SOFF_T(p, 0, get_allocation_size(fsp,&sbuf));
951 p += 8;
952 SOFF_T(p,0,file_len);
953 p += 12;
954 SCVAL(p,0,fsp->is_directory ? 1 : 0);
956 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
958 result = chain_reply(inbuf,outbuf,length,bufsize);
959 END_PROFILE(SMBntcreateX);
960 return result;
963 /****************************************************************************
964 Reply to a NT_TRANSACT_CREATE call to open a pipe.
965 ****************************************************************************/
967 static int do_nt_transact_create_pipe( connection_struct *conn,
968 char *inbuf, char *outbuf, int length,
969 int bufsize, char **ppsetup, char **ppparams,
970 char **ppdata)
972 pstring fname;
973 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
974 char *params = *ppparams;
975 int ret;
976 int pnum = -1;
977 char *p = NULL;
980 * Ensure minimum number of parameters sent.
983 if(total_parameter_count < 54) {
984 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
985 return ERROR_DOS(ERRDOS,ERRnoaccess);
988 srvstr_pull(inbuf, fname, params+53, sizeof(fname), total_parameter_count-53, STR_TERMINATE);
990 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0)
991 return ret;
993 /* Realloc the size of parameters and data we will return */
994 params = nttrans_realloc(ppparams, 69);
995 if(params == NULL)
996 return ERROR_DOS(ERRDOS,ERRnomem);
998 p = params;
999 SCVAL(p,0,NO_OPLOCK_RETURN);
1001 p += 2;
1002 SSVAL(p,0,pnum);
1003 p += 2;
1004 SIVAL(p,0,FILE_WAS_OPENED);
1005 p += 8;
1007 p += 32;
1008 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1009 p += 20;
1010 /* File type. */
1011 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1012 /* Device state. */
1013 SSVAL(p,2, 0x5FF); /* ? */
1015 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1017 /* Send the required number of replies */
1018 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1020 return -1;
1023 /****************************************************************************
1024 Internal fn to set security descriptors.
1025 ****************************************************************************/
1027 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1029 prs_struct pd;
1030 SEC_DESC *psd = NULL;
1031 TALLOC_CTX *mem_ctx;
1032 BOOL ret;
1034 if (sd_len == 0) {
1035 return NT_STATUS_OK;
1039 * Init the parse struct we will unmarshall from.
1042 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1043 DEBUG(0,("set_sd: talloc_init failed.\n"));
1044 return NT_STATUS_NO_MEMORY;
1047 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1050 * Setup the prs_struct to point at the memory we just
1051 * allocated.
1054 prs_give_memory( &pd, data, sd_len, False);
1057 * Finally, unmarshall from the data buffer.
1060 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1061 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1063 * Return access denied for want of a better error message..
1065 talloc_destroy(mem_ctx);
1066 return NT_STATUS_NO_MEMORY;
1069 if (psd->off_owner_sid==0)
1070 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1071 if (psd->off_grp_sid==0)
1072 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1073 if (psd->off_sacl==0)
1074 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1075 if (psd->off_dacl==0)
1076 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1078 ret = SMB_VFS_FSET_NT_ACL( fsp, fsp->fd, security_info_sent, psd);
1080 if (!ret) {
1081 talloc_destroy(mem_ctx);
1082 return NT_STATUS_ACCESS_DENIED;
1085 talloc_destroy(mem_ctx);
1087 return NT_STATUS_OK;
1090 /****************************************************************************
1091 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1092 ****************************************************************************/
1094 static int call_nt_transact_create(connection_struct *conn,
1095 char *inbuf, char *outbuf, int length,
1096 int bufsize, char **ppsetup, char **ppparams,
1097 char **ppdata)
1099 pstring fname;
1100 char *params = *ppparams;
1101 char *data = *ppdata;
1102 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
1103 /* Breakout the oplock request bits so we can set the reply bits separately. */
1104 int oplock_request = 0;
1105 mode_t unixmode;
1106 int fmode=0,rmode=0;
1107 SMB_OFF_T file_len = 0;
1108 SMB_STRUCT_STAT sbuf;
1109 int smb_action = 0;
1110 BOOL bad_path = False;
1111 files_struct *fsp = NULL;
1112 char *p = NULL;
1113 BOOL extended_oplock_granted = False;
1114 uint32 flags;
1115 uint32 desired_access;
1116 uint32 file_attributes;
1117 uint32 share_access;
1118 uint32 create_disposition;
1119 uint32 create_options;
1120 uint32 sd_len;
1121 uint16 root_dir_fid;
1122 SMB_BIG_UINT allocation_size = 0;
1123 int smb_ofun;
1124 int smb_open_mode;
1125 int smb_attr;
1126 time_t c_time;
1127 NTSTATUS nt_status;
1129 DEBUG(5,("call_nt_transact_create\n"));
1132 * If it's an IPC, use the pipe handler.
1135 if (IS_IPC(conn)) {
1136 if (lp_nt_pipe_support())
1137 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1138 bufsize, ppsetup, ppparams, ppdata);
1139 else
1140 return ERROR_DOS(ERRDOS,ERRnoaccess);
1144 * Ensure minimum number of parameters sent.
1147 if(total_parameter_count < 54) {
1148 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
1149 return ERROR_DOS(ERRDOS,ERRnoaccess);
1152 flags = IVAL(params,0);
1153 desired_access = IVAL(params,8);
1154 file_attributes = IVAL(params,20);
1155 share_access = IVAL(params,24);
1156 create_disposition = IVAL(params,28);
1157 create_options = IVAL(params,32);
1158 sd_len = IVAL(params,36);
1159 root_dir_fid = (uint16)IVAL(params,4);
1160 smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
1162 if (create_options & FILE_OPEN_BY_FILE_ID) {
1163 END_PROFILE(SMBntcreateX);
1164 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1168 * We need to construct the open_and_X ofun value from the
1169 * NT values, as that's what our code is structured to accept.
1172 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
1173 return ERROR_DOS(ERRDOS,ERRbadmem);
1176 * Get the file name.
1179 if(root_dir_fid != 0) {
1181 * This filename is relative to a directory fid.
1184 files_struct *dir_fsp = file_fsp(params,4);
1185 size_t dir_name_len;
1187 if(!dir_fsp)
1188 return ERROR_DOS(ERRDOS,ERRbadfid);
1190 if(!dir_fsp->is_directory) {
1192 srvstr_pull(inbuf, fname, params+53, sizeof(fname), total_parameter_count-53, STR_TERMINATE);
1195 * Check to see if this is a mac fork of some kind.
1198 if( strchr_m(fname, ':'))
1199 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1201 return ERROR_DOS(ERRDOS,ERRbadfid);
1205 * Copy in the base directory name.
1208 pstrcpy( fname, dir_fsp->fsp_name );
1209 dir_name_len = strlen(fname);
1212 * Ensure it ends in a '\'.
1215 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1216 pstrcat(fname, "\\");
1217 dir_name_len++;
1220 srvstr_pull(inbuf, &fname[dir_name_len], params+53, sizeof(fname)-dir_name_len,
1221 total_parameter_count-53, STR_TERMINATE);
1222 } else {
1223 srvstr_pull(inbuf, fname, params+53, sizeof(fname), total_parameter_count-53, STR_TERMINATE);
1226 * Check to see if this is a mac fork of some kind.
1229 if( strchr_m(fname, ':'))
1230 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1234 * Now contruct the smb_open_mode value from the desired access
1235 * and the share access.
1238 if((smb_open_mode = map_share_mode( fname, create_options, &desired_access,
1239 share_access, file_attributes)) == -1)
1240 return ERROR_DOS(ERRDOS,ERRnoaccess);
1242 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1243 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1246 * Check if POSIX semantics are wanted.
1249 set_posix_case_semantics(file_attributes);
1251 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1253 unix_convert(fname,conn,0,&bad_path,&sbuf);
1255 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
1258 * If it's a request for a directory open, deal with it separately.
1261 if(create_options & FILE_DIRECTORY_FILE) {
1263 /* Can't open a temp directory. IFS kit test. */
1264 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1265 END_PROFILE(SMBntcreateX);
1266 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1269 oplock_request = 0;
1272 * We will get a create directory here if the Win32
1273 * app specified a security descriptor in the
1274 * CreateDirectory() call.
1277 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
1279 if(!fsp) {
1280 restore_case_semantics(file_attributes);
1281 set_bad_path_error(errno, bad_path);
1282 return(UNIXERROR(ERRDOS,ERRnoaccess));
1285 } else {
1288 * Ordinary file case.
1291 fsp = open_file_shared1(conn,fname,&sbuf,desired_access,
1292 smb_open_mode,smb_ofun,unixmode,
1293 oplock_request,&rmode,&smb_action);
1295 if (!fsp) {
1297 if(errno == EISDIR) {
1300 * Fail the open if it was explicitly a non-directory file.
1303 if (create_options & FILE_NON_DIRECTORY_FILE) {
1304 restore_case_semantics(file_attributes);
1305 SSVAL(outbuf, smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1306 return ERROR_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1309 oplock_request = 0;
1310 fsp = open_directory(conn, fname, &sbuf, desired_access, smb_open_mode, smb_ofun, unixmode, &smb_action);
1312 if(!fsp) {
1313 restore_case_semantics(file_attributes);
1314 set_bad_path_error(errno, bad_path);
1315 return(UNIXERROR(ERRDOS,ERRnoaccess));
1317 } else {
1318 restore_case_semantics(file_attributes);
1319 set_bad_path_error(errno, bad_path);
1320 return(UNIXERROR(ERRDOS,ERRnoaccess));
1324 file_len = sbuf.st_size;
1325 fmode = dos_mode(conn,fname,&sbuf);
1326 if(fmode == 0)
1327 fmode = FILE_ATTRIBUTE_NORMAL;
1329 if (fmode & aDIR) {
1330 close_file(fsp,False);
1331 restore_case_semantics(file_attributes);
1332 return ERROR_DOS(ERRDOS,ERRnoaccess);
1336 * If the caller set the extended oplock request bit
1337 * and we granted one (by whatever means) - set the
1338 * correct bit for extended oplock reply.
1341 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1342 extended_oplock_granted = True;
1344 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1345 extended_oplock_granted = True;
1349 * Now try and apply the desired SD.
1352 if (sd_len && !NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION))) {
1353 close_file(fsp,False);
1354 restore_case_semantics(file_attributes);
1355 return ERROR_NT(nt_status);
1358 restore_case_semantics(file_attributes);
1360 /* Save the requested allocation size. */
1361 allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1362 #ifdef LARGE_SMB_OFF_T
1363 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1364 #endif
1365 if (allocation_size && (allocation_size > file_len)) {
1366 fsp->initial_allocation_size = SMB_ROUNDUP(allocation_size,SMB_ROUNDUP_ALLOCATION_SIZE);
1367 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1368 close_file(fsp,False);
1369 END_PROFILE(SMBntcreateX);
1370 return ERROR_NT(NT_STATUS_DISK_FULL);
1372 } else {
1373 fsp->initial_allocation_size = SMB_ROUNDUP(((SMB_BIG_UINT)file_len),SMB_ROUNDUP_ALLOCATION_SIZE);
1376 /* Realloc the size of parameters and data we will return */
1377 params = nttrans_realloc(ppparams, 69);
1378 if(params == NULL)
1379 return ERROR_DOS(ERRDOS,ERRnomem);
1381 p = params;
1382 if (extended_oplock_granted)
1383 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1384 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1385 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1386 else
1387 SCVAL(p,0,NO_OPLOCK_RETURN);
1389 p += 2;
1390 SSVAL(p,0,fsp->fnum);
1391 p += 2;
1392 if ((create_disposition == FILE_SUPERSEDE) && (smb_action == FILE_WAS_OVERWRITTEN))
1393 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1394 else
1395 SIVAL(p,0,smb_action);
1396 p += 8;
1398 /* Create time. */
1399 c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1401 if (lp_dos_filetime_resolution(SNUM(conn))) {
1402 c_time &= ~1;
1403 sbuf.st_atime &= ~1;
1404 sbuf.st_mtime &= ~1;
1405 sbuf.st_mtime &= ~1;
1408 put_long_date(p,c_time);
1409 p += 8;
1410 put_long_date(p,sbuf.st_atime); /* access time */
1411 p += 8;
1412 put_long_date(p,sbuf.st_mtime); /* write time */
1413 p += 8;
1414 put_long_date(p,sbuf.st_mtime); /* change time */
1415 p += 8;
1416 SIVAL(p,0,fmode); /* File Attributes. */
1417 p += 4;
1418 SOFF_T(p, 0, get_allocation_size(fsp,&sbuf));
1419 p += 8;
1420 SOFF_T(p,0,file_len);
1422 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1424 /* Send the required number of replies */
1425 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1427 return -1;
1430 /****************************************************************************
1431 Reply to a NT CANCEL request.
1432 ****************************************************************************/
1434 int reply_ntcancel(connection_struct *conn,
1435 char *inbuf,char *outbuf,int length,int bufsize)
1438 * Go through and cancel any pending change notifies.
1441 int mid = SVAL(inbuf,smb_mid);
1442 START_PROFILE(SMBntcancel);
1443 remove_pending_change_notify_requests_by_mid(mid);
1444 remove_pending_lock_requests_by_mid(mid);
1446 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1448 END_PROFILE(SMBntcancel);
1449 return(-1);
1452 /****************************************************************************
1453 Reply to an unsolicited SMBNTtranss - just ignore it!
1454 ****************************************************************************/
1456 int reply_nttranss(connection_struct *conn,
1457 char *inbuf,char *outbuf,int length,int bufsize)
1459 START_PROFILE(SMBnttranss);
1460 DEBUG(4,("Ignoring nttranss of length %d\n",length));
1461 END_PROFILE(SMBnttranss);
1462 return(-1);
1465 /****************************************************************************
1466 Reply to a notify change - queue the request and
1467 don't allow a directory to be opened.
1468 ****************************************************************************/
1470 static int call_nt_transact_notify_change(connection_struct *conn,
1471 char *inbuf, char *outbuf, int length,
1472 int bufsize,
1473 char **ppsetup,
1474 char **ppparams, char **ppdata)
1476 char *setup = *ppsetup;
1477 files_struct *fsp;
1478 uint32 flags;
1480 fsp = file_fsp(setup,4);
1481 flags = IVAL(setup, 0);
1483 DEBUG(3,("call_nt_transact_notify_change\n"));
1485 if(!fsp)
1486 return ERROR_DOS(ERRDOS,ERRbadfid);
1488 if((!fsp->is_directory) || (conn != fsp->conn))
1489 return ERROR_DOS(ERRDOS,ERRbadfid);
1491 if (!change_notify_set(inbuf, fsp, conn, flags))
1492 return(UNIXERROR(ERRDOS,ERRbadfid));
1494 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1495 name = %s\n", fsp->fsp_name ));
1497 return -1;
1500 /****************************************************************************
1501 Reply to an NT transact rename command.
1502 ****************************************************************************/
1504 static int call_nt_transact_rename(connection_struct *conn,
1505 char *inbuf, char *outbuf, int length,
1506 int bufsize,
1507 char **ppsetup, char **ppparams, char **ppdata)
1509 char *params = *ppparams;
1510 pstring new_name;
1511 files_struct *fsp = file_fsp(params, 0);
1512 BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1513 NTSTATUS status;
1515 CHECK_FSP(fsp, conn);
1516 srvstr_pull(inbuf, new_name, params+4, sizeof(new_name), -1, STR_TERMINATE);
1518 status = rename_internals(conn, fsp->fsp_name,
1519 new_name, replace_if_exists);
1520 if (!NT_STATUS_IS_OK(status))
1521 return ERROR_NT(status);
1524 * Rename was successful.
1526 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1528 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1529 fsp->fsp_name, new_name));
1532 * Win2k needs a changenotify request response before it will
1533 * update after a rename..
1536 process_pending_change_notify_queue((time_t)0);
1538 return -1;
1541 /******************************************************************************
1542 Fake up a completely empty SD.
1543 *******************************************************************************/
1545 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1547 extern DOM_SID global_sid_World;
1548 size_t sd_size;
1550 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1551 if(!*ppsd) {
1552 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1553 sd_size = 0;
1556 return sd_size;
1559 /****************************************************************************
1560 Reply to query a security descriptor - currently this is not implemented (it
1561 is planned to be though). Right now it just returns the same thing NT would
1562 when queried on a FAT filesystem. JRA.
1563 ****************************************************************************/
1565 static int call_nt_transact_query_security_desc(connection_struct *conn,
1566 char *inbuf, char *outbuf,
1567 int length, int bufsize,
1568 char **ppsetup, char **ppparams, char **ppdata)
1570 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1571 char *params = *ppparams;
1572 char *data = *ppdata;
1573 prs_struct pd;
1574 SEC_DESC *psd = NULL;
1575 size_t sd_size;
1576 TALLOC_CTX *mem_ctx;
1578 files_struct *fsp = file_fsp(params,0);
1580 if(!fsp)
1581 return ERROR_DOS(ERRDOS,ERRbadfid);
1583 DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1585 params = nttrans_realloc(ppparams, 4);
1586 if(params == NULL)
1587 return ERROR_DOS(ERRDOS,ERRnomem);
1589 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
1590 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1591 return ERROR_DOS(ERRDOS,ERRnomem);
1595 * Get the permissions to return.
1598 if (!lp_nt_acl_support(SNUM(conn)))
1599 sd_size = get_null_nt_acl(mem_ctx, &psd);
1600 else
1601 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fd, &psd);
1603 if (sd_size == 0) {
1604 talloc_destroy(mem_ctx);
1605 return(UNIXERROR(ERRDOS,ERRnoaccess));
1608 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %d.\n",(int)sd_size));
1610 SIVAL(params,0,(uint32)sd_size);
1612 if(max_data_count < sd_size) {
1614 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1615 params, 4, *ppdata, 0);
1616 talloc_destroy(mem_ctx);
1617 return -1;
1621 * Allocate the data we will point this at.
1624 data = nttrans_realloc(ppdata, sd_size);
1625 if(data == NULL) {
1626 talloc_destroy(mem_ctx);
1627 return ERROR_DOS(ERRDOS,ERRnomem);
1631 * Init the parse struct we will marshall into.
1634 prs_init(&pd, 0, mem_ctx, MARSHALL);
1637 * Setup the prs_struct to point at the memory we just
1638 * allocated.
1641 prs_give_memory( &pd, data, (uint32)sd_size, False);
1644 * Finally, linearize into the outgoing buffer.
1647 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1648 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
1649 security descriptor.\n"));
1651 * Return access denied for want of a better error message..
1653 talloc_destroy(mem_ctx);
1654 return(UNIXERROR(ERRDOS,ERRnoaccess));
1658 * Now we can delete the security descriptor.
1661 talloc_destroy(mem_ctx);
1663 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data, (int)sd_size);
1664 return -1;
1667 /****************************************************************************
1668 Reply to set a security descriptor. Map to UNIX perms.
1669 ****************************************************************************/
1671 static int call_nt_transact_set_security_desc(connection_struct *conn,
1672 char *inbuf, char *outbuf, int length,
1673 int bufsize, char **ppsetup,
1674 char **ppparams, char **ppdata)
1676 uint32 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
1677 char *params= *ppparams;
1678 char *data = *ppdata;
1679 uint32 total_data_count = (uint32)IVAL(inbuf, smb_nts_TotalDataCount);
1680 files_struct *fsp = NULL;
1681 uint32 security_info_sent = 0;
1682 NTSTATUS nt_status;
1684 if(total_parameter_count < 8)
1685 return ERROR_DOS(ERRDOS,ERRbadfunc);
1687 if((fsp = file_fsp(params,0)) == NULL)
1688 return ERROR_DOS(ERRDOS,ERRbadfid);
1690 if(!lp_nt_acl_support(SNUM(conn)))
1691 goto done;
1693 security_info_sent = IVAL(params,4);
1695 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
1696 (unsigned int)security_info_sent ));
1698 if (total_data_count == 0)
1699 return ERROR_DOS(ERRDOS, ERRnoaccess);
1701 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, total_data_count, security_info_sent)))
1702 return ERROR_NT(nt_status);
1704 done:
1706 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1707 return -1;
1710 /****************************************************************************
1711 Reply to NT IOCTL
1712 ****************************************************************************/
1713 static int call_nt_transact_ioctl(connection_struct *conn,
1714 char *inbuf, char *outbuf, int length,
1715 int bufsize,
1716 char **ppsetup, int setup_count,
1717 char **ppparams, int parameter_count,
1718 char **ppdata, int data_count)
1720 unsigned fnum, control;
1721 static BOOL logged_message;
1722 char *pdata = *ppdata;
1724 if (setup_count != 8) {
1725 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1726 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1729 fnum = SVAL(*ppsetup, 4);
1730 control = IVAL(*ppsetup, 0);
1732 DEBUG(10,("call_nt_transact_ioctl: fnum=%d control=0x%08x\n",
1733 fnum, control));
1735 switch (control) {
1736 case FSCTL_SET_SPARSE:
1737 /* pretend this succeeded - tho strictly we should
1738 mark the file sparse (if the local fs supports it)
1739 so we can know if we need to pre-allocate or not */
1741 DEBUG(10,("FSCTL_SET_SPARSE: fnum=%d control=0x%08x\n",fnum,control));
1742 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1743 return -1;
1745 case FSCTL_0x000900C0:
1746 /* pretend this succeeded - don't know what this really is
1747 but works ok like this --metze
1750 DEBUG(1,("FSCTL_GET_REPARSE_POINT: fnum=%d control=0x%08x\n",fnum,control));
1751 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1752 return -1;
1754 case FSCTL_GET_REPARSE_POINT:
1755 /* pretend this fail - my winXP does it like this
1756 * --metze
1759 DEBUG(1,("FSCTL_GET_REPARSE_POINT: fnum=%d control=0x%08x\n",fnum,control));
1760 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
1761 return -1;
1763 case FSCTL_SET_REPARSE_POINT:
1764 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1765 * --metze
1768 DEBUG(1,("FSCTL_SET_REPARSE_POINT: fnum=%d control=0x%08x\n",fnum,control));
1769 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
1770 return -1;
1772 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
1774 /* pretend this succeeded -
1776 * we have to send back a list with all files owned by this SID
1778 * but I have to check that --metze
1781 DOM_SID sid;
1782 uid_t uid;
1783 size_t sid_len=SID_MAX_SIZE;
1785 DEBUG(1,("FSCTL_FIND_FILES_BY_SID: fnum=%d control=0x%08x\n",fnum,control));
1787 /* this is not the length of the sid :-( so unknown 4 bytes */
1788 /*sid_len = IVAL(pdata,0);
1789 DEBUGADD(0,("sid_len: (%u)\n",sid_len));*/
1791 sid_parse(pdata+4,sid_len,&sid);
1792 DEBUGADD(2,("SID: %s\n",sid_string_static(&sid)));
1794 if (NT_STATUS_IS_ERR(sid_to_uid(&sid, &uid))) {
1795 DEBUG(0,("sid_to_uid: failed, sid[%s]\n",
1796 sid_string_static(&sid)));
1797 uid = (-1);
1800 /* we can take a look at the find source :-)
1802 * find ./ -uid $uid -name '*' is what we need here
1805 * and send 4bytes len and then NULL terminated unicode strings
1806 * for each file
1808 * but I don't know how to deal with the paged results
1810 * we don't send all files at once
1811 * and at the next we should *not* start from the beginning,
1812 * so we have to cache the result
1814 * --metze
1817 /* this works for now... */
1818 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1819 return -1;
1821 default:
1822 if (!logged_message) {
1823 logged_message = True; /* Only print this once... */
1824 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
1825 control));
1829 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1833 #ifdef WITH_QUOTAS
1834 /****************************************************************************
1835 Reply to get user quota
1836 ****************************************************************************/
1838 static int call_nt_transact_get_user_quota(connection_struct *conn,
1839 char *inbuf, char *outbuf,
1840 int length, int bufsize,
1841 char **ppsetup, int setup_count,
1842 char **ppparams, int params_count,
1843 char **ppdata, int data_count)
1845 NTSTATUS nt_status = NT_STATUS_OK;
1846 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1847 char *params = *ppparams;
1848 char *pdata = *ppdata;
1849 char *entry;
1850 int data_len=0,param_len=0;
1851 int qt_len=0;
1852 int entry_len = 0;
1853 files_struct *fsp = NULL;
1854 uint16 level = 0;
1855 size_t sid_len;
1856 DOM_SID sid;
1857 BOOL start_enum = True;
1858 SMB_NTQUOTA_STRUCT qt;
1859 SMB_NTQUOTA_LIST *tmp_list;
1860 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
1862 ZERO_STRUCT(qt);
1864 /* access check */
1865 if (conn->admin_user != True) {
1866 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
1867 lp_servicename(SNUM(conn)),conn->user));
1868 return ERROR_DOS(ERRDOS,ERRnoaccess);
1872 * Ensure minimum number of parameters sent.
1875 if (params_count < 4) {
1876 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",params_count));
1877 return ERROR_DOS(ERRDOS,ERRinvalidparam);
1880 /* maybe we can check the quota_fnum */
1881 fsp = file_fsp(params,0);
1882 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
1883 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
1884 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
1887 /* the NULL pointer cheking for fsp->fake_file_handle->pd
1888 * is done by CHECK_NTQUOTA_HANDLE_OK()
1890 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
1892 level = SVAL(params,2);
1894 /* unknown 12 bytes leading in params */
1896 switch (level) {
1897 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
1898 /* seems that we should continue with the enum here --metze */
1900 if (qt_handle->quota_list!=NULL &&
1901 qt_handle->tmp_list==NULL) {
1903 /* free the list */
1904 free_ntquota_list(&(qt_handle->quota_list));
1906 /* Realloc the size of parameters and data we will return */
1907 param_len = 4;
1908 params = nttrans_realloc(ppparams, param_len);
1909 if(params == NULL)
1910 return ERROR_DOS(ERRDOS,ERRnomem);
1912 data_len = 0;
1913 SIVAL(params,0,data_len);
1915 break;
1918 start_enum = False;
1920 case TRANSACT_GET_USER_QUOTA_LIST_START:
1922 if (qt_handle->quota_list==NULL &&
1923 qt_handle->tmp_list==NULL) {
1924 start_enum = True;
1927 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0)
1928 return ERROR_DOS(ERRSRV,ERRerror);
1930 /* Realloc the size of parameters and data we will return */
1931 param_len = 4;
1932 params = nttrans_realloc(ppparams, param_len);
1933 if(params == NULL)
1934 return ERROR_DOS(ERRDOS,ERRnomem);
1936 /* we should not trust the value in max_data_count*/
1937 max_data_count = MIN(max_data_count,2048);
1939 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
1940 if(pdata == NULL)
1941 return ERROR_DOS(ERRDOS,ERRnomem);
1943 entry = pdata;
1946 /* set params Size of returned Quota Data 4 bytes*/
1947 /* but set it later when we know it */
1949 /* for each entry push the data */
1951 if (start_enum) {
1952 qt_handle->tmp_list = qt_handle->quota_list;
1955 tmp_list = qt_handle->tmp_list;
1957 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
1958 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
1960 sid_len = sid_size(&tmp_list->quotas->sid);
1961 entry_len = 40 + sid_len;
1963 /* nextoffset entry 4 bytes */
1964 SIVAL(entry,0,entry_len);
1966 /* then the len of the SID 4 bytes */
1967 SIVAL(entry,4,sid_len);
1969 /* unknown data 8 bytes SMB_BIG_UINT */
1970 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
1972 /* the used disk space 8 bytes SMB_BIG_UINT */
1973 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
1975 /* the soft quotas 8 bytes SMB_BIG_UINT */
1976 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
1978 /* the hard quotas 8 bytes SMB_BIG_UINT */
1979 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
1981 /* and now the SID */
1982 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
1985 qt_handle->tmp_list = tmp_list;
1987 /* overwrite the offset of the last entry */
1988 SIVAL(entry-entry_len,0,0);
1990 data_len = 4+qt_len;
1991 /* overwrite the params quota_data_len */
1992 SIVAL(params,0,data_len);
1994 break;
1996 case TRANSACT_GET_USER_QUOTA_FOR_SID:
1998 /* unknown 4 bytes IVAL(pdata,0) */
2000 if (data_count < 8) {
2001 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2002 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2005 sid_len = IVAL(pdata,4);
2007 if (data_count < 8+sid_len) {
2008 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8+sid_len));
2009 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2012 data_len = 4+40+sid_len;
2014 if (max_data_count < data_len) {
2015 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2016 max_data_count, data_len));
2017 param_len = 4;
2018 SIVAL(params,0,data_len);
2019 data_len = 0;
2020 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2021 break;
2024 sid_parse(pdata+8,sid_len,&sid);
2027 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2028 ZERO_STRUCT(qt);
2030 * we have to return zero's in all fields
2031 * instead of returning an error here
2032 * --metze
2036 /* Realloc the size of parameters and data we will return */
2037 param_len = 4;
2038 params = nttrans_realloc(ppparams, param_len);
2039 if(params == NULL)
2040 return ERROR_DOS(ERRDOS,ERRnomem);
2042 pdata = nttrans_realloc(ppdata, data_len);
2043 if(pdata == NULL)
2044 return ERROR_DOS(ERRDOS,ERRnomem);
2046 entry = pdata;
2048 /* set params Size of returned Quota Data 4 bytes*/
2049 SIVAL(params,0,data_len);
2051 /* nextoffset entry 4 bytes */
2052 SIVAL(entry,0,0);
2054 /* then the len of the SID 4 bytes */
2055 SIVAL(entry,4,sid_len);
2057 /* unknown data 8 bytes SMB_BIG_UINT */
2058 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2060 /* the used disk space 8 bytes SMB_BIG_UINT */
2061 SBIG_UINT(entry,16,qt.usedspace);
2063 /* the soft quotas 8 bytes SMB_BIG_UINT */
2064 SBIG_UINT(entry,24,qt.softlim);
2066 /* the hard quotas 8 bytes SMB_BIG_UINT */
2067 SBIG_UINT(entry,32,qt.hardlim);
2069 /* and now the SID */
2070 sid_linearize(entry+40, sid_len, &sid);
2072 break;
2074 default:
2075 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2076 return ERROR_DOS(ERRSRV,ERRerror);
2077 break;
2080 send_nt_replies(inbuf, outbuf, bufsize, nt_status, params, param_len, pdata, data_len);
2082 return -1;
2085 /****************************************************************************
2086 Reply to set user quota
2087 ****************************************************************************/
2089 static int call_nt_transact_set_user_quota(connection_struct *conn,
2090 char *inbuf, char *outbuf,
2091 int length, int bufsize,
2092 char **ppsetup, int setup_count,
2093 char **ppparams, int params_count,
2094 char **ppdata, int data_count)
2096 char *params = *ppparams;
2097 char *pdata = *ppdata;
2098 int data_len=0,param_len=0;
2099 SMB_NTQUOTA_STRUCT qt;
2100 size_t sid_len;
2101 DOM_SID sid;
2102 files_struct *fsp = NULL;
2104 ZERO_STRUCT(qt);
2106 /* access check */
2107 if (conn->admin_user != True) {
2108 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2109 lp_servicename(SNUM(conn)),conn->user));
2110 return ERROR_DOS(ERRDOS,ERRnoaccess);
2114 * Ensure minimum number of parameters sent.
2117 if (params_count < 2) {
2118 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",params_count));
2119 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2122 /* maybe we can check the quota_fnum */
2123 fsp = file_fsp(params,0);
2124 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2125 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2126 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2129 if (data_count < 40) {
2130 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2131 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2134 /* offset to next quota record.
2135 * 4 bytes IVAL(pdata,0)
2136 * unused here...
2139 /* sid len */
2140 sid_len = IVAL(pdata,4);
2142 if (data_count < 40+sid_len) {
2143 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40+sid_len));
2144 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2147 /* unknown 8 bytes in pdata
2148 * maybe its the change time in NTTIME
2151 /* the used space 8 bytes (SMB_BIG_UINT)*/
2152 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2153 #ifdef LARGE_SMB_OFF_T
2154 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2155 #else /* LARGE_SMB_OFF_T */
2156 if ((IVAL(pdata,20) != 0)&&
2157 ((qt.usedspace != 0xFFFFFFFF)||
2158 (IVAL(pdata,20)!=0xFFFFFFFF)))) {
2159 /* more than 32 bits? */
2160 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2162 #endif /* LARGE_SMB_OFF_T */
2164 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2165 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2166 #ifdef LARGE_SMB_OFF_T
2167 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2168 #else /* LARGE_SMB_OFF_T */
2169 if ((IVAL(pdata,28) != 0)&&
2170 ((qt.softlim != 0xFFFFFFFF)||
2171 (IVAL(pdata,28)!=0xFFFFFFFF)))) {
2172 /* more than 32 bits? */
2173 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2175 #endif /* LARGE_SMB_OFF_T */
2177 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2178 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2179 #ifdef LARGE_SMB_OFF_T
2180 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2181 #else /* LARGE_SMB_OFF_T */
2182 if ((IVAL(pdata,36) != 0)&&
2183 ((qt.hardlim != 0xFFFFFFFF)||
2184 (IVAL(pdata,36)!=0xFFFFFFFF)))) {
2185 /* more than 32 bits? */
2186 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2188 #endif /* LARGE_SMB_OFF_T */
2190 sid_parse(pdata+40,sid_len,&sid);
2191 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2193 /* 44 unknown bytes left... */
2195 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2196 return ERROR_DOS(ERRSRV,ERRerror);
2199 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, pdata, data_len);
2201 return -1;
2203 #endif /* WITH_QUOTAS */
2205 /****************************************************************************
2206 Reply to a SMBNTtrans.
2207 ****************************************************************************/
2209 int reply_nttrans(connection_struct *conn,
2210 char *inbuf,char *outbuf,int length,int bufsize)
2212 int outsize = 0;
2213 #if 0 /* Not used. */
2214 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
2215 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
2216 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
2217 #endif /* Not used. */
2218 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
2219 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
2220 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
2221 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
2222 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
2223 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
2224 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
2225 uint16 function_code = SVAL( inbuf, smb_nt_Function);
2226 char *params = NULL, *data = NULL, *setup = NULL;
2227 uint32 num_params_sofar, num_data_sofar;
2228 START_PROFILE(SMBnttrans);
2230 if(global_oplock_break &&
2231 ((function_code == NT_TRANSACT_CREATE) ||
2232 (function_code == NT_TRANSACT_RENAME))) {
2234 * Queue this open message as we are the process of an oplock break.
2237 DEBUG(2,("reply_nttrans: queueing message code 0x%x \
2238 due to being in oplock break state.\n", (unsigned int)function_code ));
2240 push_oplock_pending_smb_message( inbuf, length);
2241 END_PROFILE(SMBnttrans);
2242 return -1;
2245 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2246 END_PROFILE(SMBnttrans);
2247 return ERROR_DOS(ERRSRV,ERRaccess);
2250 outsize = set_message(outbuf,0,0,True);
2253 * All nttrans messages we handle have smb_wct == 19 + setup_count.
2254 * Ensure this is so as a sanity check.
2257 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
2258 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2259 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
2260 goto bad_param;
2263 /* Allocate the space for the setup, the maximum needed parameters and data */
2265 if(setup_count > 0)
2266 setup = (char *)malloc(setup_count);
2267 if (total_parameter_count > 0)
2268 params = (char *)malloc(total_parameter_count);
2269 if (total_data_count > 0)
2270 data = (char *)malloc(total_data_count);
2272 if ((total_parameter_count && !params) || (total_data_count && !data) ||
2273 (setup_count && !setup)) {
2274 SAFE_FREE(setup);
2275 SAFE_FREE(params);
2276 SAFE_FREE(data);
2277 DEBUG(0,("reply_nttrans : Out of memory\n"));
2278 END_PROFILE(SMBnttrans);
2279 return ERROR_DOS(ERRDOS,ERRnomem);
2282 /* Copy the param and data bytes sent with this request into the params buffer */
2283 num_params_sofar = parameter_count;
2284 num_data_sofar = data_count;
2286 if (parameter_count > total_parameter_count || data_count > total_data_count)
2287 goto bad_param;
2289 if(setup) {
2290 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
2291 if ((smb_nt_SetupStart + setup_count < smb_nt_SetupStart) ||
2292 (smb_nt_SetupStart + setup_count < setup_count))
2293 goto bad_param;
2294 if (smb_nt_SetupStart + setup_count > length)
2295 goto bad_param;
2297 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
2298 dump_data(10, setup, setup_count);
2300 if(params) {
2301 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
2302 if ((parameter_offset + parameter_count < parameter_offset) ||
2303 (parameter_offset + parameter_count < parameter_count))
2304 goto bad_param;
2305 if (smb_base(inbuf) + parameter_offset + parameter_count > inbuf + length)
2306 goto bad_param;
2308 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
2309 dump_data(10, params, parameter_count);
2311 if(data) {
2312 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
2313 if ((data_offset + data_count < data_offset) || (data_offset + data_count < data_count))
2314 goto bad_param;
2315 if (smb_base(inbuf) + data_offset + data_count > inbuf + length)
2316 goto bad_param;
2318 memcpy( data, smb_base(inbuf) + data_offset, data_count);
2319 dump_data(10, data, data_count);
2322 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2323 /* We need to send an interim response then receive the rest
2324 of the parameter/data bytes */
2325 outsize = set_message(outbuf,0,0,True);
2326 if (!send_smb(smbd_server_fd(),outbuf))
2327 exit_server("reply_nttrans: send_smb failed.");
2329 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2330 BOOL ret;
2331 uint32 parameter_displacement;
2332 uint32 data_displacement;
2334 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
2336 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
2337 outsize = set_message(outbuf,0,0,True);
2338 if(ret) {
2339 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
2340 } else {
2341 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
2342 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
2344 goto bad_param;
2347 /* Revise total_params and total_data in case they have changed downwards */
2348 if (IVAL(inbuf, smb_nts_TotalParameterCount) < total_parameter_count)
2349 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
2350 if (IVAL(inbuf, smb_nts_TotalDataCount) < total_data_count)
2351 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
2353 parameter_count = IVAL(inbuf,smb_nts_ParameterCount);
2354 parameter_offset = IVAL(inbuf, smb_nts_ParameterOffset);
2355 parameter_displacement = IVAL(inbuf, smb_nts_ParameterDisplacement);
2356 num_params_sofar += parameter_count;
2358 data_count = IVAL(inbuf, smb_nts_DataCount);
2359 data_displacement = IVAL(inbuf, smb_nts_DataDisplacement);
2360 data_offset = IVAL(inbuf, smb_nts_DataOffset);
2361 num_data_sofar += data_count;
2363 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count) {
2364 DEBUG(0,("reply_nttrans2: data overflow in secondary nttrans packet"));
2365 goto bad_param;
2368 if (parameter_count) {
2369 if (parameter_displacement + parameter_count >= total_parameter_count)
2370 goto bad_param;
2371 if ((parameter_displacement + parameter_count < parameter_displacement) ||
2372 (parameter_displacement + parameter_count < parameter_count))
2373 goto bad_param;
2374 if (smb_base(inbuf) + parameter_offset + parameter_count >= inbuf + bufsize)
2375 goto bad_param;
2376 if (parameter_displacement + params < params)
2377 goto bad_param;
2379 memcpy( &params[parameter_displacement], smb_base(inbuf) + parameter_offset, parameter_count);
2382 if (data_count) {
2383 if (data_displacement + data_count >= total_data_count)
2384 goto bad_param;
2385 if ((data_displacement + data_count < data_displacement) ||
2386 (data_displacement + data_count < data_count))
2387 goto bad_param;
2388 if (smb_base(inbuf) + data_offset + data_count >= inbuf + bufsize)
2389 goto bad_param;
2390 if (data_displacement + data < data)
2391 goto bad_param;
2393 memcpy( &data[data_displacement], smb_base(inbuf)+ data_offset, data_count);
2398 if (Protocol >= PROTOCOL_NT1)
2399 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | FLAGS2_IS_LONG_NAME);
2401 /* Now we must call the relevant NT_TRANS function */
2402 switch(function_code) {
2403 case NT_TRANSACT_CREATE:
2404 START_PROFILE_NESTED(NT_transact_create);
2405 outsize = call_nt_transact_create(conn, inbuf, outbuf,
2406 length, bufsize,
2407 &setup, &params, &data);
2408 END_PROFILE_NESTED(NT_transact_create);
2409 break;
2410 case NT_TRANSACT_IOCTL:
2411 START_PROFILE_NESTED(NT_transact_ioctl);
2412 outsize = call_nt_transact_ioctl(conn, inbuf, outbuf,
2413 length, bufsize,
2414 &setup, setup_count,
2415 &params, parameter_count,
2416 &data, data_count);
2417 END_PROFILE_NESTED(NT_transact_ioctl);
2418 break;
2419 case NT_TRANSACT_SET_SECURITY_DESC:
2420 START_PROFILE_NESTED(NT_transact_set_security_desc);
2421 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
2422 length, bufsize,
2423 &setup, &params, &data);
2424 END_PROFILE_NESTED(NT_transact_set_security_desc);
2425 break;
2426 case NT_TRANSACT_NOTIFY_CHANGE:
2427 START_PROFILE_NESTED(NT_transact_notify_change);
2428 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
2429 length, bufsize,
2430 &setup, &params, &data);
2431 END_PROFILE_NESTED(NT_transact_notify_change);
2432 break;
2433 case NT_TRANSACT_RENAME:
2434 START_PROFILE_NESTED(NT_transact_rename);
2435 outsize = call_nt_transact_rename(conn, inbuf, outbuf,
2436 length, bufsize,
2437 &setup, &params, &data);
2438 END_PROFILE_NESTED(NT_transact_rename);
2439 break;
2441 case NT_TRANSACT_QUERY_SECURITY_DESC:
2442 START_PROFILE_NESTED(NT_transact_query_security_desc);
2443 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
2444 length, bufsize,
2445 &setup, &params, &data);
2446 END_PROFILE_NESTED(NT_transact_query_security_desc);
2447 break;
2448 #ifdef WITH_QUOTAS
2449 case NT_TRANSACT_GET_USER_QUOTA:
2450 START_PROFILE_NESTED(NT_transact_get_user_quota);
2451 outsize = call_nt_transact_get_user_quota(conn, inbuf, outbuf,
2452 length, bufsize,
2453 &setup, setup_count,
2454 &params, parameter_count,
2455 &data, data_count);
2456 END_PROFILE_NESTED(NT_transact_get_user_quota);
2457 break;
2458 case NT_TRANSACT_SET_USER_QUOTA:
2459 START_PROFILE_NESTED(NT_transact_set_user_quota);
2460 outsize = call_nt_transact_set_user_quota(conn, inbuf, outbuf,
2461 length, bufsize,
2462 &setup, setup_count,
2463 &params, parameter_count,
2464 &data, data_count);
2465 END_PROFILE_NESTED(NT_transact_set_user_quota);
2466 break;
2467 #endif /* WITH_QUOTAS */
2468 default:
2469 /* Error in request */
2470 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
2471 SAFE_FREE(setup);
2472 SAFE_FREE(params);
2473 SAFE_FREE(data);
2474 END_PROFILE(SMBnttrans);
2475 return ERROR_DOS(ERRSRV,ERRerror);
2478 /* As we do not know how many data packets will need to be
2479 returned here the various call_nt_transact_xxxx calls
2480 must send their own. Thus a call_nt_transact_xxxx routine only
2481 returns a value other than -1 when it wants to send
2482 an error packet.
2485 SAFE_FREE(setup);
2486 SAFE_FREE(params);
2487 SAFE_FREE(data);
2488 END_PROFILE(SMBnttrans);
2489 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
2490 calls have already sent it. If outsize != -1 then it is
2491 returning an error packet. */
2493 bad_param:
2495 SAFE_FREE(params);
2496 SAFE_FREE(data);
2497 SAFE_FREE(setup);
2498 END_PROFILE(SMBnttrans);
2499 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);