r23206: remove unreachable code
[Samba.git] / source / smbd / nttrans.c
bloba1edc99bceb55881371ee51b89d07015ae6591e9
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 max_send;
25 extern enum protocol_types Protocol;
26 extern int smb_read_error;
27 extern struct current_user current_user;
29 static const char *known_nt_pipes[] = {
30 "\\LANMAN",
31 "\\srvsvc",
32 "\\samr",
33 "\\wkssvc",
34 "\\NETLOGON",
35 "\\ntlsa",
36 "\\ntsvcs",
37 "\\lsass",
38 "\\lsarpc",
39 "\\winreg",
40 "\\initshutdown",
41 "\\spoolss",
42 "\\netdfs",
43 "\\rpcecho",
44 "\\svcctl",
45 "\\eventlog",
46 "\\unixinfo",
47 NULL
50 static char *nttrans_realloc(char **ptr, size_t size)
52 if (ptr==NULL) {
53 smb_panic("nttrans_realloc() called with NULL ptr\n");
56 *ptr = (char *)SMB_REALLOC(*ptr, size);
57 if(*ptr == NULL) {
58 return NULL;
60 memset(*ptr,'\0',size);
61 return *ptr;
64 /****************************************************************************
65 Send the required number of replies back.
66 We assume all fields other than the data fields are
67 set correctly for the type of call.
68 HACK ! Always assumes smb_setup field is zero.
69 ****************************************************************************/
71 int send_nt_replies(const char *inbuf,
72 char *outbuf,
73 int bufsize,
74 NTSTATUS nt_error,
75 char *params,
76 int paramsize,
77 char *pdata,
78 int datasize)
80 int data_to_send = datasize;
81 int params_to_send = paramsize;
82 int useable_space;
83 char *pp = params;
84 char *pd = pdata;
85 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
86 int alignment_offset = 3;
87 int data_alignment_offset = 0;
90 * Initially set the wcnt area to be 18 - this is true for all
91 * transNT replies.
94 set_message(inbuf,outbuf,18,0,True);
96 if (NT_STATUS_V(nt_error)) {
97 ERROR_NT(nt_error);
101 * If there genuinely are no parameters or data to send just send
102 * the empty packet.
105 if(params_to_send == 0 && data_to_send == 0) {
106 show_msg(outbuf);
107 if (!send_smb(smbd_server_fd(),outbuf)) {
108 exit_server_cleanly("send_nt_replies: send_smb failed.");
110 return 0;
114 * When sending params and data ensure that both are nicely aligned.
115 * Only do this alignment when there is also data to send - else
116 * can cause NT redirector problems.
119 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
120 data_alignment_offset = 4 - (params_to_send % 4);
124 * Space is bufsize minus Netbios over TCP header minus SMB header.
125 * The alignment_offset is to align the param bytes on a four byte
126 * boundary (2 bytes for data len, one byte pad).
127 * NT needs this to work correctly.
130 useable_space = bufsize - ((smb_buf(outbuf)+
131 alignment_offset+data_alignment_offset) -
132 outbuf);
135 * useable_space can never be more than max_send minus the
136 * alignment offset.
139 useable_space = MIN(useable_space,
140 max_send - (alignment_offset+data_alignment_offset));
143 while (params_to_send || data_to_send) {
146 * Calculate whether we will totally or partially fill this packet.
149 total_sent_thistime = params_to_send + data_to_send +
150 alignment_offset + data_alignment_offset;
153 * We can never send more than useable_space.
156 total_sent_thistime = MIN(total_sent_thistime, useable_space);
158 set_message(inbuf,outbuf, 18, total_sent_thistime, True);
161 * Set total params and data to be sent.
164 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
165 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
168 * Calculate how many parameters and data we can fit into
169 * this packet. Parameters get precedence.
172 params_sent_thistime = MIN(params_to_send,useable_space);
173 data_sent_thistime = useable_space - params_sent_thistime;
174 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
176 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
178 if(params_sent_thistime == 0) {
179 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
180 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
181 } else {
183 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
184 * parameter bytes, however the first 4 bytes of outbuf are
185 * the Netbios over TCP header. Thus use smb_base() to subtract
186 * them from the calculation.
189 SIVAL(outbuf,smb_ntr_ParameterOffset,
190 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
192 * Absolute displacement of param bytes sent in this packet.
195 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
199 * Deal with the data portion.
202 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
204 if(data_sent_thistime == 0) {
205 SIVAL(outbuf,smb_ntr_DataOffset,0);
206 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
207 } else {
209 * The offset of the data bytes is the offset of the
210 * parameter bytes plus the number of parameters being sent this time.
213 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
214 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
215 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
219 * Copy the param bytes into the packet.
222 if(params_sent_thistime) {
223 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
227 * Copy in the data bytes
230 if(data_sent_thistime) {
231 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
232 data_alignment_offset,pd,data_sent_thistime);
235 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
236 params_sent_thistime, data_sent_thistime, useable_space));
237 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
238 params_to_send, data_to_send, paramsize, datasize));
240 /* Send the packet */
241 show_msg(outbuf);
242 if (!send_smb(smbd_server_fd(),outbuf)) {
243 exit_server_cleanly("send_nt_replies: send_smb failed.");
246 pp += params_sent_thistime;
247 pd += data_sent_thistime;
249 params_to_send -= params_sent_thistime;
250 data_to_send -= data_sent_thistime;
253 * Sanity check
256 if(params_to_send < 0 || data_to_send < 0) {
257 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
258 params_to_send, data_to_send));
259 return -1;
263 return 0;
266 /****************************************************************************
267 Is it an NTFS stream name ?
268 ****************************************************************************/
270 BOOL is_ntfs_stream_name(const char *fname)
272 if (lp_posix_pathnames()) {
273 return False;
275 return (strchr_m(fname, ':') != NULL) ? True : False;
278 /****************************************************************************
279 Save case statics.
280 ****************************************************************************/
282 static BOOL saved_case_sensitive;
283 static BOOL saved_case_preserve;
284 static BOOL saved_short_case_preserve;
286 /****************************************************************************
287 Save case semantics.
288 ****************************************************************************/
290 static uint32 set_posix_case_semantics(connection_struct *conn, uint32 file_attributes)
292 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
293 return file_attributes;
296 saved_case_sensitive = conn->case_sensitive;
297 saved_case_preserve = conn->case_preserve;
298 saved_short_case_preserve = conn->short_case_preserve;
300 /* Set to POSIX. */
301 conn->case_sensitive = True;
302 conn->case_preserve = True;
303 conn->short_case_preserve = True;
305 return (file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
308 /****************************************************************************
309 Restore case semantics.
310 ****************************************************************************/
312 static void restore_case_semantics(connection_struct *conn, uint32 file_attributes)
314 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
315 return;
318 conn->case_sensitive = saved_case_sensitive;
319 conn->case_preserve = saved_case_preserve;
320 conn->short_case_preserve = saved_short_case_preserve;
323 /****************************************************************************
324 Reply to an NT create and X call on a pipe.
325 ****************************************************************************/
327 static int nt_open_pipe(char *fname, connection_struct *conn,
328 char *inbuf, char *outbuf, int *ppnum)
330 smb_np_struct *p = NULL;
331 uint16 vuid = SVAL(inbuf, smb_uid);
332 int i;
334 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
336 /* See if it is one we want to handle. */
338 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
339 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
342 for( i = 0; known_nt_pipes[i]; i++ ) {
343 if( strequal(fname,known_nt_pipes[i])) {
344 break;
348 if ( known_nt_pipes[i] == NULL ) {
349 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
352 /* Strip \\ off the name. */
353 fname++;
355 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
357 p = open_rpc_pipe_p(fname, conn, vuid);
358 if (!p) {
359 return(ERROR_DOS(ERRSRV,ERRnofids));
362 /* TODO: Add pipe to db */
364 if ( !store_pipe_opendb( p ) ) {
365 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
368 *ppnum = p->pnum;
369 return 0;
372 /****************************************************************************
373 Reply to an NT create and X call for pipes.
374 ****************************************************************************/
376 static int do_ntcreate_pipe_open(connection_struct *conn,
377 char *inbuf,char *outbuf,int length,int bufsize)
379 pstring fname;
380 int ret;
381 int pnum = -1;
382 char *p = NULL;
383 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
385 srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
387 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0) {
388 return ret;
392 * Deal with pipe return.
395 if (flags & EXTENDED_RESPONSE_REQUIRED) {
396 /* This is very strange. We
397 * return 50 words, but only set
398 * the wcnt to 42 ? It's definately
399 * what happens on the wire....
401 set_message(inbuf,outbuf,50,0,True);
402 SCVAL(outbuf,smb_wct,42);
403 } else {
404 set_message(inbuf,outbuf,34,0,True);
407 p = outbuf + smb_vwv2;
408 p++;
409 SSVAL(p,0,pnum);
410 p += 2;
411 SIVAL(p,0,FILE_WAS_OPENED);
412 p += 4;
413 p += 32;
414 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
415 p += 20;
416 /* File type. */
417 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
418 /* Device state. */
419 SSVAL(p,2, 0x5FF); /* ? */
420 p += 4;
422 if (flags & EXTENDED_RESPONSE_REQUIRED) {
423 p += 26;
424 SIVAL(p,0,FILE_GENERIC_ALL);
426 * For pipes W2K3 seems to return
427 * 0x12019B next.
428 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
430 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
433 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
435 return chain_reply(inbuf,outbuf,length,bufsize);
438 /****************************************************************************
439 Reply to an NT create and X call for a quota file.
440 ****************************************************************************/
442 int reply_ntcreate_and_X_quota(connection_struct *conn,
443 char *inbuf,
444 char *outbuf,
445 int length,
446 int bufsize,
447 enum FAKE_FILE_TYPE fake_file_type,
448 const char *fname)
450 int result;
451 char *p;
452 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
453 files_struct *fsp;
454 NTSTATUS status;
456 status = open_fake_file(conn, fake_file_type, fname, desired_access,
457 &fsp);
459 if (!NT_STATUS_IS_OK(status)) {
460 return ERROR_NT(status);
463 set_message(inbuf,outbuf,34,0,True);
465 p = outbuf + smb_vwv2;
467 /* SCVAL(p,0,NO_OPLOCK_RETURN); */
468 p++;
469 SSVAL(p,0,fsp->fnum);
471 DEBUG(5,("reply_ntcreate_and_X_quota: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
473 result = chain_reply(inbuf,outbuf,length,bufsize);
474 return result;
477 /****************************************************************************
478 Reply to an NT create and X call.
479 ****************************************************************************/
481 int reply_ntcreate_and_X(connection_struct *conn,
482 char *inbuf,char *outbuf,int length,int bufsize)
484 int result;
485 pstring fname;
486 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
487 uint32 access_mask = IVAL(inbuf,smb_ntcreate_DesiredAccess);
488 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
489 uint32 new_file_attributes;
490 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
491 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
492 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
493 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
494 /* Breakout the oplock request bits so we can set the
495 reply bits separately. */
496 int oplock_request = 0;
497 uint32 fattr=0;
498 SMB_OFF_T file_len = 0;
499 SMB_STRUCT_STAT sbuf;
500 int info = 0;
501 files_struct *fsp = NULL;
502 char *p = NULL;
503 struct timespec c_timespec;
504 struct timespec a_timespec;
505 struct timespec m_timespec;
506 BOOL extended_oplock_granted = False;
507 NTSTATUS status;
509 START_PROFILE(SMBntcreateX);
511 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
512 "file_attributes = 0x%x, share_access = 0x%x, "
513 "create_disposition = 0x%x create_options = 0x%x "
514 "root_dir_fid = 0x%x\n",
515 (unsigned int)flags,
516 (unsigned int)access_mask,
517 (unsigned int)file_attributes,
518 (unsigned int)share_access,
519 (unsigned int)create_disposition,
520 (unsigned int)create_options,
521 (unsigned int)root_dir_fid ));
524 * If it's an IPC, use the pipe handler.
527 if (IS_IPC(conn)) {
528 if (lp_nt_pipe_support()) {
529 END_PROFILE(SMBntcreateX);
530 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
531 } else {
532 END_PROFILE(SMBntcreateX);
533 return(ERROR_DOS(ERRDOS,ERRnoaccess));
537 if (create_options & FILE_OPEN_BY_FILE_ID) {
538 END_PROFILE(SMBntcreateX);
539 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
543 * Get the file name.
546 if(root_dir_fid != 0) {
548 * This filename is relative to a directory fid.
550 pstring rel_fname;
551 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
552 size_t dir_name_len;
554 if(!dir_fsp) {
555 END_PROFILE(SMBntcreateX);
556 return ERROR_DOS(ERRDOS,ERRbadfid);
559 if(!dir_fsp->is_directory) {
561 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
562 if (!NT_STATUS_IS_OK(status)) {
563 END_PROFILE(SMBntcreateX);
564 return ERROR_NT(status);
568 * Check to see if this is a mac fork of some kind.
571 if( is_ntfs_stream_name(fname)) {
572 END_PROFILE(SMBntcreateX);
573 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
577 we need to handle the case when we get a
578 relative open relative to a file and the
579 pathname is blank - this is a reopen!
580 (hint from demyn plantenberg)
583 END_PROFILE(SMBntcreateX);
584 return(ERROR_DOS(ERRDOS,ERRbadfid));
588 * Copy in the base directory name.
591 pstrcpy( fname, dir_fsp->fsp_name );
592 dir_name_len = strlen(fname);
595 * Ensure it ends in a '\'.
598 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
599 pstrcat(fname, "/");
600 dir_name_len++;
603 srvstr_get_path(inbuf, rel_fname, smb_buf(inbuf), sizeof(rel_fname), 0, STR_TERMINATE, &status);
604 if (!NT_STATUS_IS_OK(status)) {
605 END_PROFILE(SMBntcreateX);
606 return ERROR_NT(status);
608 pstrcat(fname, rel_fname);
609 } else {
610 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status);
611 if (!NT_STATUS_IS_OK(status)) {
612 END_PROFILE(SMBntcreateX);
613 return ERROR_NT(status);
617 * Check to see if this is a mac fork of some kind.
620 if( is_ntfs_stream_name(fname)) {
621 enum FAKE_FILE_TYPE fake_file_type = is_fake_file(fname);
622 if (fake_file_type!=FAKE_FILE_TYPE_NONE) {
624 * Here we go! support for changing the disk quotas --metze
626 * We need to fake up to open this MAGIC QUOTA file
627 * and return a valid FID.
629 * w2k close this file directly after openening
630 * xp also tries a QUERY_FILE_INFO on the file and then close it
632 result = reply_ntcreate_and_X_quota(conn, inbuf, outbuf, length, bufsize,
633 fake_file_type, fname);
634 END_PROFILE(SMBntcreateX);
635 return result;
636 } else {
637 END_PROFILE(SMBntcreateX);
638 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
644 * Now contruct the smb_open_mode value from the filename,
645 * desired access and the share access.
647 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, fname);
648 if (!NT_STATUS_IS_OK(status)) {
649 END_PROFILE(SMBntcreateX);
650 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
651 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
653 return ERROR_NT(status);
656 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
657 if (oplock_request) {
658 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
662 * Ordinary file or directory.
666 * Check if POSIX semantics are wanted.
669 new_file_attributes = set_posix_case_semantics(conn, file_attributes);
671 status = unix_convert(conn, fname, False, NULL, &sbuf);
672 if (!NT_STATUS_IS_OK(status)) {
673 restore_case_semantics(conn, file_attributes);
674 END_PROFILE(SMBntcreateX);
675 return ERROR_NT(status);
677 /* All file access must go through check_name() */
678 status = check_name(conn, fname);
679 if (!NT_STATUS_IS_OK(status)) {
680 restore_case_semantics(conn, file_attributes);
681 END_PROFILE(SMBntcreateX);
682 return ERROR_NT(status);
685 /* This is the correct thing to do (check every time) but can_delete is
686 expensive (it may have to read the parent directory permissions). So
687 for now we're not doing it unless we have a strong hint the client
688 is really going to delete this file. If the client is forcing FILE_CREATE
689 let the filesystem take care of the permissions. */
691 /* Setting FILE_SHARE_DELETE is the hint. */
693 if (lp_acl_check_permissions(SNUM(conn))
694 && (create_disposition != FILE_CREATE)
695 && (share_access & FILE_SHARE_DELETE)
696 && (access_mask & DELETE_ACCESS)) {
697 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
698 !can_delete_file_in_directory(conn, fname)) {
699 restore_case_semantics(conn, file_attributes);
700 END_PROFILE(SMBntcreateX);
701 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
706 * If it's a request for a directory open, deal with it separately.
709 if(create_options & FILE_DIRECTORY_FILE) {
711 /* Can't open a temp directory. IFS kit test. */
712 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
713 END_PROFILE(SMBntcreateX);
714 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
717 oplock_request = 0;
718 status = open_directory(conn, fname, &sbuf,
719 access_mask,
720 share_access,
721 create_disposition,
722 create_options,
723 new_file_attributes,
724 &info, &fsp);
726 restore_case_semantics(conn, file_attributes);
728 if(!NT_STATUS_IS_OK(status)) {
729 if (!use_nt_status() && NT_STATUS_EQUAL(
730 status, NT_STATUS_OBJECT_NAME_COLLISION)) {
731 status = NT_STATUS_DOS(ERRDOS, ERRfilexists);
733 END_PROFILE(SMBntcreateX);
734 return ERROR_NT(status);
737 } else {
740 * Ordinary file case.
743 /* NB. We have a potential bug here. If we
744 * cause an oplock break to ourselves, then we
745 * could end up processing filename related
746 * SMB requests whilst we await the oplock
747 * break response. As we may have changed the
748 * filename case semantics to be POSIX-like,
749 * this could mean a filename request could
750 * fail when it should succeed. This is a rare
751 * condition, but eventually we must arrange
752 * to restore the correct case semantics
753 * before issuing an oplock break request to
754 * our client. JRA. */
756 status = open_file_ntcreate(conn,fname,&sbuf,
757 access_mask,
758 share_access,
759 create_disposition,
760 create_options,
761 new_file_attributes,
762 oplock_request,
763 &info, &fsp);
765 if (!NT_STATUS_IS_OK(status)) {
766 /* We cheat here. There are two cases we
767 * care about. One is a directory rename,
768 * where the NT client will attempt to
769 * open the source directory for
770 * DELETE access. Note that when the
771 * NT client does this it does *not*
772 * set the directory bit in the
773 * request packet. This is translated
774 * into a read/write open
775 * request. POSIX states that any open
776 * for write request on a directory
777 * will generate an EISDIR error, so
778 * we can catch this here and open a
779 * pseudo handle that is flagged as a
780 * directory. The second is an open
781 * for a permissions read only, which
782 * we handle in the open_file_stat case. JRA.
785 if (NT_STATUS_EQUAL(status,
786 NT_STATUS_FILE_IS_A_DIRECTORY)) {
789 * Fail the open if it was explicitly a non-directory file.
792 if (create_options & FILE_NON_DIRECTORY_FILE) {
793 restore_case_semantics(conn, file_attributes);
794 END_PROFILE(SMBntcreateX);
795 return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
798 oplock_request = 0;
799 status = open_directory(conn, fname, &sbuf,
800 access_mask,
801 share_access,
802 create_disposition,
803 create_options,
804 new_file_attributes,
805 &info, &fsp);
807 if(!NT_STATUS_IS_OK(status)) {
808 restore_case_semantics(conn, file_attributes);
809 if (!use_nt_status() && NT_STATUS_EQUAL(
810 status, NT_STATUS_OBJECT_NAME_COLLISION)) {
811 status = NT_STATUS_DOS(ERRDOS, ERRfilexists);
813 END_PROFILE(SMBntcreateX);
814 return ERROR_NT(status);
816 } else {
817 restore_case_semantics(conn, file_attributes);
818 END_PROFILE(SMBntcreateX);
819 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
820 /* We have re-scheduled this call. */
821 return -1;
823 return ERROR_NT(status);
828 restore_case_semantics(conn, file_attributes);
830 file_len = sbuf.st_size;
831 fattr = dos_mode(conn,fname,&sbuf);
832 if(fattr == 0) {
833 fattr = FILE_ATTRIBUTE_NORMAL;
835 if (!fsp->is_directory && (fattr & aDIR)) {
836 close_file(fsp,ERROR_CLOSE);
837 END_PROFILE(SMBntcreateX);
838 return ERROR_DOS(ERRDOS,ERRnoaccess);
841 /* Save the requested allocation size. */
842 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
843 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize);
844 #ifdef LARGE_SMB_OFF_T
845 allocation_size |= (((SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
846 #endif
847 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
848 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
849 if (fsp->is_directory) {
850 close_file(fsp,ERROR_CLOSE);
851 END_PROFILE(SMBntcreateX);
852 /* Can't set allocation size on a directory. */
853 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
855 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
856 close_file(fsp,ERROR_CLOSE);
857 END_PROFILE(SMBntcreateX);
858 return ERROR_NT(NT_STATUS_DISK_FULL);
860 } else {
861 fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)file_len);
866 * If the caller set the extended oplock request bit
867 * and we granted one (by whatever means) - set the
868 * correct bit for extended oplock reply.
871 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
872 extended_oplock_granted = True;
875 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
876 extended_oplock_granted = True;
879 if (flags & EXTENDED_RESPONSE_REQUIRED) {
880 /* This is very strange. We
881 * return 50 words, but only set
882 * the wcnt to 42 ? It's definately
883 * what happens on the wire....
885 set_message(inbuf,outbuf,50,0,True);
886 SCVAL(outbuf,smb_wct,42);
887 } else {
888 set_message(inbuf,outbuf,34,0,True);
891 p = outbuf + smb_vwv2;
894 * Currently as we don't support level II oplocks we just report
895 * exclusive & batch here.
898 if (extended_oplock_granted) {
899 if (flags & REQUEST_BATCH_OPLOCK) {
900 SCVAL(p,0, BATCH_OPLOCK_RETURN);
901 } else {
902 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
904 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
905 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
906 } else {
907 SCVAL(p,0,NO_OPLOCK_RETURN);
910 p++;
911 SSVAL(p,0,fsp->fnum);
912 p += 2;
913 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
914 SIVAL(p,0,FILE_WAS_SUPERSEDED);
915 } else {
916 SIVAL(p,0,info);
918 p += 4;
920 /* Create time. */
921 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
922 a_timespec = get_atimespec(&sbuf);
923 m_timespec = get_mtimespec(&sbuf);
925 if (lp_dos_filetime_resolution(SNUM(conn))) {
926 dos_filetime_timespec(&c_timespec);
927 dos_filetime_timespec(&a_timespec);
928 dos_filetime_timespec(&m_timespec);
931 put_long_date_timespec(p, c_timespec); /* create time. */
932 p += 8;
933 put_long_date_timespec(p, a_timespec); /* access time */
934 p += 8;
935 put_long_date_timespec(p, m_timespec); /* write time */
936 p += 8;
937 put_long_date_timespec(p, m_timespec); /* change time */
938 p += 8;
939 SIVAL(p,0,fattr); /* File Attributes. */
940 p += 4;
941 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
942 p += 8;
943 SOFF_T(p,0,file_len);
944 p += 8;
945 if (flags & EXTENDED_RESPONSE_REQUIRED) {
946 SSVAL(p,2,0x7);
948 p += 4;
949 SCVAL(p,0,fsp->is_directory ? 1 : 0);
951 if (flags & EXTENDED_RESPONSE_REQUIRED) {
952 uint32 perms = 0;
953 p += 26;
954 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
955 perms = FILE_GENERIC_ALL;
956 } else {
957 perms = FILE_GENERIC_READ|FILE_EXECUTE;
959 SIVAL(p,0,perms);
962 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
964 result = chain_reply(inbuf,outbuf,length,bufsize);
965 END_PROFILE(SMBntcreateX);
966 return result;
969 /****************************************************************************
970 Reply to a NT_TRANSACT_CREATE call to open a pipe.
971 ****************************************************************************/
973 static int do_nt_transact_create_pipe( connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
974 uint16 **ppsetup, uint32 setup_count,
975 char **ppparams, uint32 parameter_count,
976 char **ppdata, uint32 data_count)
978 pstring fname;
979 char *params = *ppparams;
980 int ret;
981 int pnum = -1;
982 char *p = NULL;
983 NTSTATUS status;
984 size_t param_len;
985 uint32 flags;
988 * Ensure minimum number of parameters sent.
991 if(parameter_count < 54) {
992 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
993 return ERROR_DOS(ERRDOS,ERRnoaccess);
996 flags = IVAL(params,0);
998 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
999 if (!NT_STATUS_IS_OK(status)) {
1000 return ERROR_NT(status);
1003 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0) {
1004 return ret;
1007 /* Realloc the size of parameters and data we will return */
1008 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1009 /* Extended response is 32 more byyes. */
1010 param_len = 101;
1011 } else {
1012 param_len = 69;
1014 params = nttrans_realloc(ppparams, param_len);
1015 if(params == NULL) {
1016 return ERROR_DOS(ERRDOS,ERRnomem);
1019 p = params;
1020 SCVAL(p,0,NO_OPLOCK_RETURN);
1022 p += 2;
1023 SSVAL(p,0,pnum);
1024 p += 2;
1025 SIVAL(p,0,FILE_WAS_OPENED);
1026 p += 8;
1028 p += 32;
1029 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1030 p += 20;
1031 /* File type. */
1032 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1033 /* Device state. */
1034 SSVAL(p,2, 0x5FF); /* ? */
1035 p += 4;
1037 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1038 p += 26;
1039 SIVAL(p,0,FILE_GENERIC_ALL);
1041 * For pipes W2K3 seems to return
1042 * 0x12019B next.
1043 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
1045 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
1048 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1050 /* Send the required number of replies */
1051 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, *ppdata, 0);
1053 return -1;
1056 /****************************************************************************
1057 Internal fn to set security descriptors.
1058 ****************************************************************************/
1060 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1062 prs_struct pd;
1063 SEC_DESC *psd = NULL;
1064 TALLOC_CTX *mem_ctx;
1065 BOOL ret;
1067 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
1068 return NT_STATUS_OK;
1072 * Init the parse struct we will unmarshall from.
1075 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1076 DEBUG(0,("set_sd: talloc_init failed.\n"));
1077 return NT_STATUS_NO_MEMORY;
1080 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1083 * Setup the prs_struct to point at the memory we just
1084 * allocated.
1087 prs_give_memory( &pd, data, sd_len, False);
1090 * Finally, unmarshall from the data buffer.
1093 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1094 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1096 * Return access denied for want of a better error message..
1098 talloc_destroy(mem_ctx);
1099 return NT_STATUS_NO_MEMORY;
1102 if (psd->owner_sid==0) {
1103 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1105 if (psd->group_sid==0) {
1106 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1108 if (psd->sacl==0) {
1109 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1111 if (psd->dacl==0) {
1112 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1115 ret = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
1117 if (!ret) {
1118 talloc_destroy(mem_ctx);
1119 return NT_STATUS_ACCESS_DENIED;
1122 talloc_destroy(mem_ctx);
1124 return NT_STATUS_OK;
1127 /****************************************************************************
1128 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1129 ****************************************************************************/
1131 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
1133 struct ea_list *ea_list_head = NULL;
1134 size_t offset = 0;
1136 if (data_size < 4) {
1137 return NULL;
1140 while (offset + 4 <= data_size) {
1141 size_t next_offset = IVAL(pdata,offset);
1142 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
1144 if (!eal) {
1145 return NULL;
1148 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
1149 if (next_offset == 0) {
1150 break;
1152 offset += next_offset;
1155 return ea_list_head;
1158 /****************************************************************************
1159 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1160 ****************************************************************************/
1162 static int call_nt_transact_create(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1163 uint16 **ppsetup, uint32 setup_count,
1164 char **ppparams, uint32 parameter_count,
1165 char **ppdata, uint32 data_count, uint32 max_data_count)
1167 pstring fname;
1168 char *params = *ppparams;
1169 char *data = *ppdata;
1170 /* Breakout the oplock request bits so we can set the reply bits separately. */
1171 int oplock_request = 0;
1172 uint32 fattr=0;
1173 SMB_OFF_T file_len = 0;
1174 SMB_STRUCT_STAT sbuf;
1175 int info = 0;
1176 files_struct *fsp = NULL;
1177 char *p = NULL;
1178 BOOL extended_oplock_granted = False;
1179 uint32 flags;
1180 uint32 access_mask;
1181 uint32 file_attributes;
1182 uint32 new_file_attributes;
1183 uint32 share_access;
1184 uint32 create_disposition;
1185 uint32 create_options;
1186 uint32 sd_len;
1187 uint32 ea_len;
1188 uint16 root_dir_fid;
1189 struct timespec c_timespec;
1190 struct timespec a_timespec;
1191 struct timespec m_timespec;
1192 struct ea_list *ea_list = NULL;
1193 TALLOC_CTX *ctx = NULL;
1194 char *pdata = NULL;
1195 NTSTATUS status;
1196 size_t param_len;
1198 DEBUG(5,("call_nt_transact_create\n"));
1201 * If it's an IPC, use the pipe handler.
1204 if (IS_IPC(conn)) {
1205 if (lp_nt_pipe_support()) {
1206 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1207 bufsize,
1208 ppsetup, setup_count,
1209 ppparams, parameter_count,
1210 ppdata, data_count);
1211 } else {
1212 return ERROR_DOS(ERRDOS,ERRnoaccess);
1217 * Ensure minimum number of parameters sent.
1220 if(parameter_count < 54) {
1221 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1222 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1225 flags = IVAL(params,0);
1226 access_mask = IVAL(params,8);
1227 file_attributes = IVAL(params,20);
1228 share_access = IVAL(params,24);
1229 create_disposition = IVAL(params,28);
1230 create_options = IVAL(params,32);
1231 sd_len = IVAL(params,36);
1232 ea_len = IVAL(params,40);
1233 root_dir_fid = (uint16)IVAL(params,4);
1235 /* Ensure the data_len is correct for the sd and ea values given. */
1236 if ((ea_len + sd_len > data_count) ||
1237 (ea_len > data_count) || (sd_len > data_count) ||
1238 (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1239 DEBUG(10,("call_nt_transact_create - ea_len = %u, sd_len = %u, data_count = %u\n",
1240 (unsigned int)ea_len, (unsigned int)sd_len, (unsigned int)data_count ));
1241 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1244 if (ea_len) {
1245 if (!lp_ea_support(SNUM(conn))) {
1246 DEBUG(10,("call_nt_transact_create - ea_len = %u but EA's not supported.\n",
1247 (unsigned int)ea_len ));
1248 return ERROR_NT(NT_STATUS_EAS_NOT_SUPPORTED);
1251 if (ea_len < 10) {
1252 DEBUG(10,("call_nt_transact_create - ea_len = %u - too small (should be more than 10)\n",
1253 (unsigned int)ea_len ));
1254 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1258 if (create_options & FILE_OPEN_BY_FILE_ID) {
1259 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1263 * Get the file name.
1266 if(root_dir_fid != 0) {
1268 * This filename is relative to a directory fid.
1270 files_struct *dir_fsp = file_fsp(params,4);
1271 size_t dir_name_len;
1273 if(!dir_fsp) {
1274 return ERROR_DOS(ERRDOS,ERRbadfid);
1277 if(!dir_fsp->is_directory) {
1278 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
1279 if (!NT_STATUS_IS_OK(status)) {
1280 return ERROR_NT(status);
1284 * Check to see if this is a mac fork of some kind.
1287 if( is_ntfs_stream_name(fname)) {
1288 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1291 return ERROR_DOS(ERRDOS,ERRbadfid);
1295 * Copy in the base directory name.
1298 pstrcpy( fname, dir_fsp->fsp_name );
1299 dir_name_len = strlen(fname);
1302 * Ensure it ends in a '\'.
1305 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1306 pstrcat(fname, "/");
1307 dir_name_len++;
1311 pstring tmpname;
1312 srvstr_get_path(inbuf, tmpname, params+53, sizeof(tmpname), parameter_count-53, STR_TERMINATE, &status);
1313 if (!NT_STATUS_IS_OK(status)) {
1314 return ERROR_NT(status);
1316 pstrcat(fname, tmpname);
1318 } else {
1319 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status);
1320 if (!NT_STATUS_IS_OK(status)) {
1321 return ERROR_NT(status);
1325 * Check to see if this is a mac fork of some kind.
1328 if( is_ntfs_stream_name(fname)) {
1329 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1333 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1334 if (oplock_request) {
1335 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1339 * Ordinary file or directory.
1343 * Check if POSIX semantics are wanted.
1346 new_file_attributes = set_posix_case_semantics(conn, file_attributes);
1348 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, fname);
1349 if (!NT_STATUS_IS_OK(status)) {
1350 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1351 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1353 return ERROR_NT(status);
1356 status = unix_convert(conn, fname, False, NULL, &sbuf);
1357 if (!NT_STATUS_IS_OK(status)) {
1358 restore_case_semantics(conn, file_attributes);
1359 return ERROR_NT(status);
1361 /* All file access must go through check_name() */
1362 status = check_name(conn, fname);
1363 if (!NT_STATUS_IS_OK(status)) {
1364 restore_case_semantics(conn, file_attributes);
1365 return ERROR_NT(status);
1368 /* This is the correct thing to do (check every time) but can_delete is
1369 expensive (it may have to read the parent directory permissions). So
1370 for now we're not doing it unless we have a strong hint the client
1371 is really going to delete this file. If the client is forcing FILE_CREATE
1372 let the filesystem take care of the permissions. */
1374 /* Setting FILE_SHARE_DELETE is the hint. */
1376 if (lp_acl_check_permissions(SNUM(conn))
1377 && (create_disposition != FILE_CREATE)
1378 && (share_access & FILE_SHARE_DELETE)
1379 && (access_mask & DELETE_ACCESS)) {
1380 if ((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY) ||
1381 !can_delete_file_in_directory(conn, fname)) {
1382 restore_case_semantics(conn, file_attributes);
1383 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1387 if (ea_len) {
1388 pdata = data + sd_len;
1390 /* We have already checked that ea_len <= data_count here. */
1391 ea_list = read_nttrans_ea_list(tmp_talloc_ctx(), pdata,
1392 ea_len);
1393 if (!ea_list ) {
1394 restore_case_semantics(conn, file_attributes);
1395 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1400 * If it's a request for a directory open, deal with it separately.
1403 if(create_options & FILE_DIRECTORY_FILE) {
1405 /* Can't open a temp directory. IFS kit test. */
1406 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1407 restore_case_semantics(conn, file_attributes);
1408 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1412 * We will get a create directory here if the Win32
1413 * app specified a security descriptor in the
1414 * CreateDirectory() call.
1417 oplock_request = 0;
1418 status = open_directory(conn, fname, &sbuf,
1419 access_mask,
1420 share_access,
1421 create_disposition,
1422 create_options,
1423 new_file_attributes,
1424 &info, &fsp);
1425 if(!NT_STATUS_IS_OK(status)) {
1426 restore_case_semantics(conn, file_attributes);
1427 return ERROR_NT(status);
1430 } else {
1433 * Ordinary file case.
1436 status = open_file_ntcreate(conn,fname,&sbuf,
1437 access_mask,
1438 share_access,
1439 create_disposition,
1440 create_options,
1441 new_file_attributes,
1442 oplock_request,
1443 &info, &fsp);
1445 if (!NT_STATUS_IS_OK(status)) {
1446 if (NT_STATUS_EQUAL(status,
1447 NT_STATUS_FILE_IS_A_DIRECTORY)) {
1450 * Fail the open if it was explicitly a non-directory file.
1453 if (create_options & FILE_NON_DIRECTORY_FILE) {
1454 restore_case_semantics(conn, file_attributes);
1455 return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1458 oplock_request = 0;
1459 status = open_directory(conn, fname, &sbuf,
1460 access_mask,
1461 share_access,
1462 create_disposition,
1463 create_options,
1464 new_file_attributes,
1465 &info, &fsp);
1466 if(!NT_STATUS_IS_OK(status)) {
1467 restore_case_semantics(conn, file_attributes);
1468 return ERROR_NT(status);
1470 } else {
1471 restore_case_semantics(conn, file_attributes);
1472 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1473 /* We have re-scheduled this call. */
1474 return -1;
1476 return ERROR_NT(status);
1482 * According to the MS documentation, the only time the security
1483 * descriptor is applied to the opened file is iff we *created* the
1484 * file; an existing file stays the same.
1486 * Also, it seems (from observation) that you can open the file with
1487 * any access mask but you can still write the sd. We need to override
1488 * the granted access before we call set_sd
1489 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1492 if (lp_nt_acl_support(SNUM(conn)) && sd_len && info == FILE_WAS_CREATED) {
1493 uint32 saved_access_mask = fsp->access_mask;
1495 /* We have already checked that sd_len <= data_count here. */
1497 fsp->access_mask = FILE_GENERIC_ALL;
1499 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1500 if (!NT_STATUS_IS_OK(status)) {
1501 talloc_destroy(ctx);
1502 close_file(fsp,ERROR_CLOSE);
1503 restore_case_semantics(conn, file_attributes);
1504 return ERROR_NT(status);
1506 fsp->access_mask = saved_access_mask;
1509 if (ea_len && (info == FILE_WAS_CREATED)) {
1510 status = set_ea(conn, fsp, fname, ea_list);
1511 if (!NT_STATUS_IS_OK(status)) {
1512 close_file(fsp,ERROR_CLOSE);
1513 restore_case_semantics(conn, file_attributes);
1514 return ERROR_NT(status);
1518 restore_case_semantics(conn, file_attributes);
1520 file_len = sbuf.st_size;
1521 fattr = dos_mode(conn,fname,&sbuf);
1522 if(fattr == 0) {
1523 fattr = FILE_ATTRIBUTE_NORMAL;
1525 if (!fsp->is_directory && (fattr & aDIR)) {
1526 close_file(fsp,ERROR_CLOSE);
1527 return ERROR_DOS(ERRDOS,ERRnoaccess);
1530 /* Save the requested allocation size. */
1531 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1532 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1533 #ifdef LARGE_SMB_OFF_T
1534 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1535 #endif
1536 if (allocation_size && (allocation_size > file_len)) {
1537 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1538 if (fsp->is_directory) {
1539 close_file(fsp,ERROR_CLOSE);
1540 /* Can't set allocation size on a directory. */
1541 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1543 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1544 close_file(fsp,ERROR_CLOSE);
1545 return ERROR_NT(NT_STATUS_DISK_FULL);
1547 } else {
1548 fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1553 * If the caller set the extended oplock request bit
1554 * and we granted one (by whatever means) - set the
1555 * correct bit for extended oplock reply.
1558 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1559 extended_oplock_granted = True;
1562 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1563 extended_oplock_granted = True;
1566 /* Realloc the size of parameters and data we will return */
1567 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1568 /* Extended response is 32 more byyes. */
1569 param_len = 101;
1570 } else {
1571 param_len = 69;
1573 params = nttrans_realloc(ppparams, param_len);
1574 if(params == NULL) {
1575 return ERROR_DOS(ERRDOS,ERRnomem);
1578 p = params;
1579 if (extended_oplock_granted) {
1580 if (flags & REQUEST_BATCH_OPLOCK) {
1581 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1582 } else {
1583 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
1585 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1586 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1587 } else {
1588 SCVAL(p,0,NO_OPLOCK_RETURN);
1591 p += 2;
1592 SSVAL(p,0,fsp->fnum);
1593 p += 2;
1594 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1595 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1596 } else {
1597 SIVAL(p,0,info);
1599 p += 8;
1601 /* Create time. */
1602 c_timespec = get_create_timespec(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1603 a_timespec = get_atimespec(&sbuf);
1604 m_timespec = get_mtimespec(&sbuf);
1606 if (lp_dos_filetime_resolution(SNUM(conn))) {
1607 dos_filetime_timespec(&c_timespec);
1608 dos_filetime_timespec(&a_timespec);
1609 dos_filetime_timespec(&m_timespec);
1612 put_long_date_timespec(p, c_timespec); /* create time. */
1613 p += 8;
1614 put_long_date_timespec(p, a_timespec); /* access time */
1615 p += 8;
1616 put_long_date_timespec(p, m_timespec); /* write time */
1617 p += 8;
1618 put_long_date_timespec(p, m_timespec); /* change time */
1619 p += 8;
1620 SIVAL(p,0,fattr); /* File Attributes. */
1621 p += 4;
1622 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1623 p += 8;
1624 SOFF_T(p,0,file_len);
1625 p += 8;
1626 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1627 SSVAL(p,2,0x7);
1629 p += 4;
1630 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1632 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1633 uint32 perms = 0;
1634 p += 26;
1635 if (fsp->is_directory || can_write_to_file(conn, fname, &sbuf)) {
1636 perms = FILE_GENERIC_ALL;
1637 } else {
1638 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1640 SIVAL(p,0,perms);
1643 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1645 /* Send the required number of replies */
1646 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, *ppdata, 0);
1648 return -1;
1651 /****************************************************************************
1652 Reply to a NT CANCEL request.
1653 conn POINTER CAN BE NULL HERE !
1654 ****************************************************************************/
1656 int reply_ntcancel(connection_struct *conn,
1657 char *inbuf,char *outbuf,int length,int bufsize)
1660 * Go through and cancel any pending change notifies.
1663 int mid = SVAL(inbuf,smb_mid);
1664 START_PROFILE(SMBntcancel);
1665 remove_pending_change_notify_requests_by_mid(mid);
1666 remove_pending_lock_requests_by_mid(mid);
1667 srv_cancel_sign_response(mid);
1669 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1671 END_PROFILE(SMBntcancel);
1672 return(-1);
1675 /****************************************************************************
1676 Copy a file.
1677 ****************************************************************************/
1679 static NTSTATUS copy_internals(connection_struct *conn, char *oldname, char *newname, uint32 attrs)
1681 SMB_STRUCT_STAT sbuf1, sbuf2;
1682 pstring last_component_oldname;
1683 pstring last_component_newname;
1684 files_struct *fsp1,*fsp2;
1685 uint32 fattr;
1686 int info;
1687 SMB_OFF_T ret=-1;
1688 NTSTATUS status = NT_STATUS_OK;
1690 ZERO_STRUCT(sbuf1);
1691 ZERO_STRUCT(sbuf2);
1693 if (!CAN_WRITE(conn)) {
1694 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1697 status = unix_convert(conn, oldname, False, last_component_oldname, &sbuf1);
1698 if (!NT_STATUS_IS_OK(status)) {
1699 return status;
1702 status = check_name(conn, oldname);
1703 if (!NT_STATUS_IS_OK(status)) {
1704 return status;
1707 /* Source must already exist. */
1708 if (!VALID_STAT(sbuf1)) {
1709 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1711 /* Ensure attributes match. */
1712 fattr = dos_mode(conn,oldname,&sbuf1);
1713 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1714 return NT_STATUS_NO_SUCH_FILE;
1717 status = unix_convert(conn, newname, False, last_component_newname, &sbuf2);
1718 if (!NT_STATUS_IS_OK(status)) {
1719 return status;
1722 status = check_name(conn, newname);
1723 if (!NT_STATUS_IS_OK(status)) {
1724 return status;
1727 /* Disallow if newname already exists. */
1728 if (VALID_STAT(sbuf2)) {
1729 return NT_STATUS_OBJECT_NAME_COLLISION;
1732 /* No links from a directory. */
1733 if (S_ISDIR(sbuf1.st_mode)) {
1734 return NT_STATUS_FILE_IS_A_DIRECTORY;
1737 /* Ensure this is within the share. */
1738 status = reduce_name(conn, oldname);
1739 if (!NT_STATUS_IS_OK(status)) {
1740 return status;
1743 DEBUG(10,("copy_internals: doing file copy %s to %s\n", oldname, newname));
1745 status = open_file_ntcreate(conn,oldname,&sbuf1,
1746 FILE_READ_DATA, /* Read-only. */
1747 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1748 FILE_OPEN,
1749 0, /* No create options. */
1750 FILE_ATTRIBUTE_NORMAL,
1751 NO_OPLOCK,
1752 &info, &fsp1);
1754 if (!NT_STATUS_IS_OK(status)) {
1755 return status;
1758 status = open_file_ntcreate(conn,newname,&sbuf2,
1759 FILE_WRITE_DATA, /* Read-only. */
1760 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1761 FILE_CREATE,
1762 0, /* No create options. */
1763 fattr,
1764 NO_OPLOCK,
1765 &info, &fsp2);
1767 if (!NT_STATUS_IS_OK(status)) {
1768 close_file(fsp1,ERROR_CLOSE);
1769 return status;
1772 if (sbuf1.st_size) {
1773 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1777 * As we are opening fsp1 read-only we only expect
1778 * an error on close on fsp2 if we are out of space.
1779 * Thus we don't look at the error return from the
1780 * close of fsp1.
1782 close_file(fsp1,NORMAL_CLOSE);
1784 /* Ensure the modtime is set correctly on the destination file. */
1785 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1787 status = close_file(fsp2,NORMAL_CLOSE);
1789 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1790 creates the file. This isn't the correct thing to do in the copy
1791 case. JRA */
1792 file_set_dosmode(conn, newname, fattr, &sbuf2,
1793 parent_dirname(newname));
1795 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1796 return NT_STATUS_DISK_FULL;
1799 if (!NT_STATUS_IS_OK(status)) {
1800 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1801 nt_errstr(status), oldname, newname));
1803 return status;
1806 /****************************************************************************
1807 Reply to a NT rename request.
1808 ****************************************************************************/
1810 int reply_ntrename(connection_struct *conn,
1811 char *inbuf,char *outbuf,int length,int bufsize)
1813 int outsize = 0;
1814 pstring oldname;
1815 pstring newname;
1816 char *p;
1817 NTSTATUS status;
1818 BOOL src_has_wcard = False;
1819 BOOL dest_has_wcard = False;
1820 uint32 attrs = SVAL(inbuf,smb_vwv0);
1821 uint16 rename_type = SVAL(inbuf,smb_vwv1);
1823 START_PROFILE(SMBntrename);
1825 p = smb_buf(inbuf) + 1;
1826 p += srvstr_get_path_wcard(inbuf, oldname, p, sizeof(oldname), 0, STR_TERMINATE, &status, &src_has_wcard);
1827 if (!NT_STATUS_IS_OK(status)) {
1828 END_PROFILE(SMBntrename);
1829 return ERROR_NT(status);
1832 if( is_ntfs_stream_name(oldname)) {
1833 /* Can't rename a stream. */
1834 END_PROFILE(SMBntrename);
1835 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1838 if (ms_has_wild(oldname)) {
1839 END_PROFILE(SMBntrename);
1840 return ERROR_NT(NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1843 p++;
1844 p += srvstr_get_path_wcard(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status, &dest_has_wcard);
1845 if (!NT_STATUS_IS_OK(status)) {
1846 END_PROFILE(SMBntrename);
1847 return ERROR_NT(status);
1850 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, oldname);
1851 if (!NT_STATUS_IS_OK(status)) {
1852 END_PROFILE(SMBntrename);
1853 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1854 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1856 return ERROR_NT(status);
1859 status = resolve_dfspath(conn, SVAL(inbuf,smb_flg2) & FLAGS2_DFS_PATHNAMES, newname);
1860 if (!NT_STATUS_IS_OK(status)) {
1861 END_PROFILE(SMBntrename);
1862 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1863 return ERROR_BOTH(NT_STATUS_PATH_NOT_COVERED, ERRSRV, ERRbadpath);
1865 return ERROR_NT(status);
1868 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1870 switch(rename_type) {
1871 case RENAME_FLAG_RENAME:
1872 status = rename_internals(conn, oldname, newname, attrs, False, src_has_wcard, dest_has_wcard);
1873 break;
1874 case RENAME_FLAG_HARD_LINK:
1875 if (src_has_wcard || dest_has_wcard) {
1876 /* No wildcards. */
1877 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1878 } else {
1879 status = hardlink_internals(conn, oldname, newname);
1881 break;
1882 case RENAME_FLAG_COPY:
1883 if (src_has_wcard || dest_has_wcard) {
1884 /* No wildcards. */
1885 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1886 } else {
1887 status = copy_internals(conn, oldname, newname, attrs);
1889 break;
1890 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1891 status = NT_STATUS_INVALID_PARAMETER;
1892 break;
1893 default:
1894 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1895 break;
1898 if (!NT_STATUS_IS_OK(status)) {
1899 END_PROFILE(SMBntrename);
1900 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1901 /* We have re-scheduled this call. */
1902 return -1;
1904 return ERROR_NT(status);
1907 outsize = set_message(inbuf,outbuf,0,0,False);
1909 END_PROFILE(SMBntrename);
1910 return(outsize);
1913 /****************************************************************************
1914 Reply to a notify change - queue the request and
1915 don't allow a directory to be opened.
1916 ****************************************************************************/
1918 static int call_nt_transact_notify_change(connection_struct *conn, char *inbuf,
1919 char *outbuf, int length,
1920 int bufsize,
1921 uint16 **ppsetup, uint32 setup_count,
1922 char **ppparams,
1923 uint32 parameter_count,
1924 char **ppdata, uint32 data_count,
1925 uint32 max_data_count,
1926 uint32 max_param_count)
1928 uint16 *setup = *ppsetup;
1929 files_struct *fsp;
1930 uint32 filter;
1931 NTSTATUS status;
1932 BOOL recursive;
1934 if(setup_count < 6) {
1935 return ERROR_DOS(ERRDOS,ERRbadfunc);
1938 fsp = file_fsp((char *)setup,4);
1939 filter = IVAL(setup, 0);
1940 recursive = (SVAL(setup, 6) != 0) ? True : False;
1942 DEBUG(3,("call_nt_transact_notify_change\n"));
1944 if(!fsp) {
1945 return ERROR_DOS(ERRDOS,ERRbadfid);
1949 char *filter_string;
1951 if (!(filter_string = notify_filter_string(NULL, filter))) {
1952 return ERROR_NT(NT_STATUS_NO_MEMORY);
1955 DEBUG(3,("call_nt_transact_notify_change: notify change "
1956 "called on %s, filter = %s, recursive = %d\n",
1957 fsp->fsp_name, filter_string, recursive));
1959 TALLOC_FREE(filter_string);
1962 if((!fsp->is_directory) || (conn != fsp->conn)) {
1963 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1966 if (fsp->notify == NULL) {
1968 status = change_notify_create(fsp, filter, recursive);
1970 if (!NT_STATUS_IS_OK(status)) {
1971 DEBUG(10, ("change_notify_create returned %s\n",
1972 nt_errstr(status)));
1973 return ERROR_NT(status);
1977 if (fsp->notify->num_changes != 0) {
1980 * We've got changes pending, respond immediately
1984 * TODO: write a torture test to check the filtering behaviour
1985 * here.
1988 change_notify_reply(inbuf, max_param_count,
1989 fsp->notify);
1992 * change_notify_reply() above has independently sent its
1993 * results
1995 return -1;
1999 * No changes pending, queue the request
2002 status = change_notify_add_request(inbuf, max_param_count, filter,
2003 recursive, fsp);
2004 if (!NT_STATUS_IS_OK(status)) {
2005 return ERROR_NT(status);
2008 return -1;
2011 /****************************************************************************
2012 Reply to an NT transact rename command.
2013 ****************************************************************************/
2015 static int call_nt_transact_rename(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2016 uint16 **ppsetup, uint32 setup_count,
2017 char **ppparams, uint32 parameter_count,
2018 char **ppdata, uint32 data_count, uint32 max_data_count)
2020 char *params = *ppparams;
2021 pstring new_name;
2022 files_struct *fsp = NULL;
2023 BOOL replace_if_exists = False;
2024 BOOL dest_has_wcard = False;
2025 NTSTATUS status;
2027 if(parameter_count < 5) {
2028 return ERROR_DOS(ERRDOS,ERRbadfunc);
2031 fsp = file_fsp(params, 0);
2032 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
2033 CHECK_FSP(fsp, conn);
2034 srvstr_get_path_wcard(inbuf, new_name, params+4, sizeof(new_name), parameter_count - 4,
2035 STR_TERMINATE, &status, &dest_has_wcard);
2036 if (!NT_STATUS_IS_OK(status)) {
2037 return ERROR_NT(status);
2040 status = rename_internals(conn, fsp->fsp_name,
2041 new_name, 0, replace_if_exists, False, dest_has_wcard);
2043 if (!NT_STATUS_IS_OK(status)) {
2044 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
2045 /* We have re-scheduled this call. */
2046 return -1;
2048 return ERROR_NT(status);
2052 * Rename was successful.
2054 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2056 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
2057 fsp->fsp_name, new_name));
2059 return -1;
2062 /******************************************************************************
2063 Fake up a completely empty SD.
2064 *******************************************************************************/
2066 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
2068 size_t sd_size;
2070 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
2071 if(!*ppsd) {
2072 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
2073 sd_size = 0;
2076 return sd_size;
2079 /****************************************************************************
2080 Reply to query a security descriptor.
2081 ****************************************************************************/
2083 static int call_nt_transact_query_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2084 uint16 **ppsetup, uint32 setup_count,
2085 char **ppparams, uint32 parameter_count,
2086 char **ppdata, uint32 data_count, uint32 max_data_count)
2088 char *params = *ppparams;
2089 char *data = *ppdata;
2090 prs_struct pd;
2091 SEC_DESC *psd = NULL;
2092 size_t sd_size;
2093 uint32 security_info_wanted;
2094 TALLOC_CTX *mem_ctx;
2095 files_struct *fsp = NULL;
2097 if(parameter_count < 8) {
2098 return ERROR_DOS(ERRDOS,ERRbadfunc);
2101 fsp = file_fsp(params,0);
2102 if(!fsp) {
2103 return ERROR_DOS(ERRDOS,ERRbadfid);
2106 security_info_wanted = IVAL(params,4);
2108 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
2109 (unsigned int)security_info_wanted ));
2111 params = nttrans_realloc(ppparams, 4);
2112 if(params == NULL) {
2113 return ERROR_DOS(ERRDOS,ERRnomem);
2116 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
2117 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2118 return ERROR_DOS(ERRDOS,ERRnomem);
2122 * Get the permissions to return.
2125 if (!lp_nt_acl_support(SNUM(conn))) {
2126 sd_size = get_null_nt_acl(mem_ctx, &psd);
2127 } else {
2128 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd, security_info_wanted, &psd);
2131 if (sd_size == 0) {
2132 talloc_destroy(mem_ctx);
2133 return(UNIXERROR(ERRDOS,ERRnoaccess));
2136 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
2138 SIVAL(params,0,(uint32)sd_size);
2140 if(max_data_count < sd_size) {
2142 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
2143 params, 4, *ppdata, 0);
2144 talloc_destroy(mem_ctx);
2145 return -1;
2149 * Allocate the data we will point this at.
2152 data = nttrans_realloc(ppdata, sd_size);
2153 if(data == NULL) {
2154 talloc_destroy(mem_ctx);
2155 return ERROR_DOS(ERRDOS,ERRnomem);
2159 * Init the parse struct we will marshall into.
2162 prs_init(&pd, 0, mem_ctx, MARSHALL);
2165 * Setup the prs_struct to point at the memory we just
2166 * allocated.
2169 prs_give_memory( &pd, data, (uint32)sd_size, False);
2172 * Finally, linearize into the outgoing buffer.
2175 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2176 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2177 security descriptor.\n"));
2179 * Return access denied for want of a better error message..
2181 talloc_destroy(mem_ctx);
2182 return(UNIXERROR(ERRDOS,ERRnoaccess));
2186 * Now we can delete the security descriptor.
2189 talloc_destroy(mem_ctx);
2191 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data,
2192 (int)sd_size);
2193 return -1;
2196 /****************************************************************************
2197 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2198 ****************************************************************************/
2200 static int call_nt_transact_set_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2201 uint16 **ppsetup, uint32 setup_count,
2202 char **ppparams, uint32 parameter_count,
2203 char **ppdata, uint32 data_count, uint32 max_data_count)
2205 char *params= *ppparams;
2206 char *data = *ppdata;
2207 files_struct *fsp = NULL;
2208 uint32 security_info_sent = 0;
2209 NTSTATUS nt_status;
2211 if(parameter_count < 8) {
2212 return ERROR_DOS(ERRDOS,ERRbadfunc);
2215 if((fsp = file_fsp(params,0)) == NULL) {
2216 return ERROR_DOS(ERRDOS,ERRbadfid);
2219 if(!lp_nt_acl_support(SNUM(conn))) {
2220 goto done;
2223 security_info_sent = IVAL(params,4);
2225 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2226 (unsigned int)security_info_sent ));
2228 if (data_count == 0) {
2229 return ERROR_DOS(ERRDOS, ERRnoaccess);
2232 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2233 return ERROR_NT(nt_status);
2236 done:
2238 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2239 return -1;
2242 /****************************************************************************
2243 Reply to NT IOCTL
2244 ****************************************************************************/
2246 static int call_nt_transact_ioctl(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2247 uint16 **ppsetup, uint32 setup_count,
2248 char **ppparams, uint32 parameter_count,
2249 char **ppdata, uint32 data_count, uint32 max_data_count)
2251 uint32 function;
2252 uint16 fidnum;
2253 files_struct *fsp;
2254 uint8 isFSctl;
2255 uint8 compfilter;
2256 static BOOL logged_message;
2257 char *pdata = *ppdata;
2259 if (setup_count != 8) {
2260 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2261 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2264 function = IVAL(*ppsetup, 0);
2265 fidnum = SVAL(*ppsetup, 4);
2266 isFSctl = CVAL(*ppsetup, 6);
2267 compfilter = CVAL(*ppsetup, 7);
2269 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2270 function, fidnum, isFSctl, compfilter));
2272 fsp=file_fsp((char *)*ppsetup, 4);
2273 /* this check is done in each implemented function case for now
2274 because I don't want to break anything... --metze
2275 FSP_BELONGS_CONN(fsp,conn);*/
2277 switch (function) {
2278 case FSCTL_SET_SPARSE:
2279 /* pretend this succeeded - tho strictly we should
2280 mark the file sparse (if the local fs supports it)
2281 so we can know if we need to pre-allocate or not */
2283 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2284 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL,
2286 return -1;
2288 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2290 unsigned char objid[16];
2292 /* This should return the object-id on this file.
2293 * I think I'll make this be the inode+dev. JRA.
2296 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2298 data_count = 64;
2299 pdata = nttrans_realloc(ppdata, data_count);
2300 if (pdata == NULL) {
2301 return ERROR_NT(NT_STATUS_NO_MEMORY);
2303 push_file_id_16(pdata, &fsp->file_id);
2304 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2305 push_file_id_16(pdata+32, &fsp->file_id);
2306 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, pdata, data_count);
2307 return -1;
2310 case FSCTL_GET_REPARSE_POINT:
2311 /* pretend this fail - my winXP does it like this
2312 * --metze
2315 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2316 return ERROR_NT(NT_STATUS_NOT_A_REPARSE_POINT);
2318 case FSCTL_SET_REPARSE_POINT:
2319 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2320 * --metze
2323 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2324 return ERROR_NT(NT_STATUS_NOT_A_REPARSE_POINT);
2326 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2329 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2330 * and return their volume names. If max_data_count is 16, then it is just
2331 * asking for the number of volumes and length of the combined names.
2333 * pdata is the data allocated by our caller, but that uses
2334 * total_data_count (which is 0 in our case) rather than max_data_count.
2335 * Allocate the correct amount and return the pointer to let
2336 * it be deallocated when we return.
2338 SHADOW_COPY_DATA *shadow_data = NULL;
2339 TALLOC_CTX *shadow_mem_ctx = NULL;
2340 BOOL labels = False;
2341 uint32 labels_data_count = 0;
2342 uint32 i;
2343 char *cur_pdata;
2345 FSP_BELONGS_CONN(fsp,conn);
2347 if (max_data_count < 16) {
2348 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2349 max_data_count));
2350 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2353 if (max_data_count > 16) {
2354 labels = True;
2357 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2358 if (shadow_mem_ctx == NULL) {
2359 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2360 return ERROR_NT(NT_STATUS_NO_MEMORY);
2363 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2364 if (shadow_data == NULL) {
2365 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2366 talloc_destroy(shadow_mem_ctx);
2367 return ERROR_NT(NT_STATUS_NO_MEMORY);
2370 shadow_data->mem_ctx = shadow_mem_ctx;
2373 * Call the VFS routine to actually do the work.
2375 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2376 talloc_destroy(shadow_data->mem_ctx);
2377 if (errno == ENOSYS) {
2378 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2379 conn->connectpath));
2380 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2381 } else {
2382 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2383 conn->connectpath));
2384 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
2388 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2390 if (!labels) {
2391 data_count = 16;
2392 } else {
2393 data_count = 12+labels_data_count+4;
2396 if (max_data_count<data_count) {
2397 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2398 max_data_count,data_count));
2399 talloc_destroy(shadow_data->mem_ctx);
2400 return ERROR_NT(NT_STATUS_BUFFER_TOO_SMALL);
2403 pdata = nttrans_realloc(ppdata, data_count);
2404 if (pdata == NULL) {
2405 talloc_destroy(shadow_data->mem_ctx);
2406 return ERROR_NT(NT_STATUS_NO_MEMORY);
2409 cur_pdata = pdata;
2411 /* num_volumes 4 bytes */
2412 SIVAL(pdata,0,shadow_data->num_volumes);
2414 if (labels) {
2415 /* num_labels 4 bytes */
2416 SIVAL(pdata,4,shadow_data->num_volumes);
2419 /* needed_data_count 4 bytes */
2420 SIVAL(pdata,8,labels_data_count);
2422 cur_pdata+=12;
2424 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2425 shadow_data->num_volumes,fsp->fsp_name));
2426 if (labels && shadow_data->labels) {
2427 for (i=0;i<shadow_data->num_volumes;i++) {
2428 srvstr_push(outbuf, cur_pdata, shadow_data->labels[i], 2*sizeof(SHADOW_COPY_LABEL), STR_UNICODE|STR_TERMINATE);
2429 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2430 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2434 talloc_destroy(shadow_data->mem_ctx);
2436 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0,
2437 pdata, data_count);
2439 return -1;
2442 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2444 /* pretend this succeeded -
2446 * we have to send back a list with all files owned by this SID
2448 * but I have to check that --metze
2450 DOM_SID sid;
2451 uid_t uid;
2452 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2454 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2456 FSP_BELONGS_CONN(fsp,conn);
2458 /* unknown 4 bytes: this is not the length of the sid :-( */
2459 /*unknown = IVAL(pdata,0);*/
2461 sid_parse(pdata+4,sid_len,&sid);
2462 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2464 if (!sid_to_uid(&sid, &uid)) {
2465 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2466 sid_string_static(&sid),(unsigned long)sid_len));
2467 uid = (-1);
2470 /* we can take a look at the find source :-)
2472 * find ./ -uid $uid -name '*' is what we need here
2475 * and send 4bytes len and then NULL terminated unicode strings
2476 * for each file
2478 * but I don't know how to deal with the paged results
2479 * (maybe we can hang the result anywhere in the fsp struct)
2481 * we don't send all files at once
2482 * and at the next we should *not* start from the beginning,
2483 * so we have to cache the result
2485 * --metze
2488 /* this works for now... */
2489 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0,
2490 NULL, 0);
2491 return -1;
2493 default:
2494 if (!logged_message) {
2495 logged_message = True; /* Only print this once... */
2496 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2497 function));
2501 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2505 #ifdef HAVE_SYS_QUOTAS
2506 /****************************************************************************
2507 Reply to get user quota
2508 ****************************************************************************/
2510 static int call_nt_transact_get_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2511 uint16 **ppsetup, uint32 setup_count,
2512 char **ppparams, uint32 parameter_count,
2513 char **ppdata, uint32 data_count, uint32 max_data_count)
2515 NTSTATUS nt_status = NT_STATUS_OK;
2516 char *params = *ppparams;
2517 char *pdata = *ppdata;
2518 char *entry;
2519 int data_len=0,param_len=0;
2520 int qt_len=0;
2521 int entry_len = 0;
2522 files_struct *fsp = NULL;
2523 uint16 level = 0;
2524 size_t sid_len;
2525 DOM_SID sid;
2526 BOOL start_enum = True;
2527 SMB_NTQUOTA_STRUCT qt;
2528 SMB_NTQUOTA_LIST *tmp_list;
2529 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2531 ZERO_STRUCT(qt);
2533 /* access check */
2534 if (current_user.ut.uid != 0) {
2535 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2536 lp_servicename(SNUM(conn)),conn->user));
2537 return ERROR_DOS(ERRDOS,ERRnoaccess);
2541 * Ensure minimum number of parameters sent.
2544 if (parameter_count < 4) {
2545 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2546 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2549 /* maybe we can check the quota_fnum */
2550 fsp = file_fsp(params,0);
2551 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2552 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2553 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2556 /* the NULL pointer checking for fsp->fake_file_handle->pd
2557 * is done by CHECK_NTQUOTA_HANDLE_OK()
2559 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2561 level = SVAL(params,2);
2563 /* unknown 12 bytes leading in params */
2565 switch (level) {
2566 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2567 /* seems that we should continue with the enum here --metze */
2569 if (qt_handle->quota_list!=NULL &&
2570 qt_handle->tmp_list==NULL) {
2572 /* free the list */
2573 free_ntquota_list(&(qt_handle->quota_list));
2575 /* Realloc the size of parameters and data we will return */
2576 param_len = 4;
2577 params = nttrans_realloc(ppparams, param_len);
2578 if(params == NULL) {
2579 return ERROR_DOS(ERRDOS,ERRnomem);
2582 data_len = 0;
2583 SIVAL(params,0,data_len);
2585 break;
2588 start_enum = False;
2590 case TRANSACT_GET_USER_QUOTA_LIST_START:
2592 if (qt_handle->quota_list==NULL &&
2593 qt_handle->tmp_list==NULL) {
2594 start_enum = True;
2597 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0)
2598 return ERROR_DOS(ERRSRV,ERRerror);
2600 /* Realloc the size of parameters and data we will return */
2601 param_len = 4;
2602 params = nttrans_realloc(ppparams, param_len);
2603 if(params == NULL) {
2604 return ERROR_DOS(ERRDOS,ERRnomem);
2607 /* we should not trust the value in max_data_count*/
2608 max_data_count = MIN(max_data_count,2048);
2610 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2611 if(pdata == NULL) {
2612 return ERROR_DOS(ERRDOS,ERRnomem);
2615 entry = pdata;
2617 /* set params Size of returned Quota Data 4 bytes*/
2618 /* but set it later when we know it */
2620 /* for each entry push the data */
2622 if (start_enum) {
2623 qt_handle->tmp_list = qt_handle->quota_list;
2626 tmp_list = qt_handle->tmp_list;
2628 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2629 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2631 sid_len = sid_size(&tmp_list->quotas->sid);
2632 entry_len = 40 + sid_len;
2634 /* nextoffset entry 4 bytes */
2635 SIVAL(entry,0,entry_len);
2637 /* then the len of the SID 4 bytes */
2638 SIVAL(entry,4,sid_len);
2640 /* unknown data 8 bytes SMB_BIG_UINT */
2641 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2643 /* the used disk space 8 bytes SMB_BIG_UINT */
2644 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2646 /* the soft quotas 8 bytes SMB_BIG_UINT */
2647 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2649 /* the hard quotas 8 bytes SMB_BIG_UINT */
2650 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2652 /* and now the SID */
2653 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2656 qt_handle->tmp_list = tmp_list;
2658 /* overwrite the offset of the last entry */
2659 SIVAL(entry-entry_len,0,0);
2661 data_len = 4+qt_len;
2662 /* overwrite the params quota_data_len */
2663 SIVAL(params,0,data_len);
2665 break;
2667 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2669 /* unknown 4 bytes IVAL(pdata,0) */
2671 if (data_count < 8) {
2672 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2673 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2676 sid_len = IVAL(pdata,4);
2677 /* Ensure this is less than 1mb. */
2678 if (sid_len > (1024*1024)) {
2679 return ERROR_DOS(ERRDOS,ERRnomem);
2682 if (data_count < 8+sid_len) {
2683 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2684 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2687 data_len = 4+40+sid_len;
2689 if (max_data_count < data_len) {
2690 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2691 max_data_count, data_len));
2692 param_len = 4;
2693 SIVAL(params,0,data_len);
2694 data_len = 0;
2695 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2696 break;
2699 sid_parse(pdata+8,sid_len,&sid);
2701 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2702 ZERO_STRUCT(qt);
2704 * we have to return zero's in all fields
2705 * instead of returning an error here
2706 * --metze
2710 /* Realloc the size of parameters and data we will return */
2711 param_len = 4;
2712 params = nttrans_realloc(ppparams, param_len);
2713 if(params == NULL) {
2714 return ERROR_DOS(ERRDOS,ERRnomem);
2717 pdata = nttrans_realloc(ppdata, data_len);
2718 if(pdata == NULL) {
2719 return ERROR_DOS(ERRDOS,ERRnomem);
2722 entry = pdata;
2724 /* set params Size of returned Quota Data 4 bytes*/
2725 SIVAL(params,0,data_len);
2727 /* nextoffset entry 4 bytes */
2728 SIVAL(entry,0,0);
2730 /* then the len of the SID 4 bytes */
2731 SIVAL(entry,4,sid_len);
2733 /* unknown data 8 bytes SMB_BIG_UINT */
2734 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2736 /* the used disk space 8 bytes SMB_BIG_UINT */
2737 SBIG_UINT(entry,16,qt.usedspace);
2739 /* the soft quotas 8 bytes SMB_BIG_UINT */
2740 SBIG_UINT(entry,24,qt.softlim);
2742 /* the hard quotas 8 bytes SMB_BIG_UINT */
2743 SBIG_UINT(entry,32,qt.hardlim);
2745 /* and now the SID */
2746 sid_linearize(entry+40, sid_len, &sid);
2748 break;
2750 default:
2751 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2752 return ERROR_DOS(ERRSRV,ERRerror);
2753 break;
2756 send_nt_replies(inbuf, outbuf, bufsize, nt_status, params, param_len,
2757 pdata, data_len);
2759 return -1;
2762 /****************************************************************************
2763 Reply to set user quota
2764 ****************************************************************************/
2766 static int call_nt_transact_set_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2767 uint16 **ppsetup, uint32 setup_count,
2768 char **ppparams, uint32 parameter_count,
2769 char **ppdata, uint32 data_count, uint32 max_data_count)
2771 char *params = *ppparams;
2772 char *pdata = *ppdata;
2773 int data_len=0,param_len=0;
2774 SMB_NTQUOTA_STRUCT qt;
2775 size_t sid_len;
2776 DOM_SID sid;
2777 files_struct *fsp = NULL;
2779 ZERO_STRUCT(qt);
2781 /* access check */
2782 if (current_user.ut.uid != 0) {
2783 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2784 lp_servicename(SNUM(conn)),conn->user));
2785 return ERROR_DOS(ERRDOS,ERRnoaccess);
2789 * Ensure minimum number of parameters sent.
2792 if (parameter_count < 2) {
2793 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2794 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2797 /* maybe we can check the quota_fnum */
2798 fsp = file_fsp(params,0);
2799 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2800 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2801 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2804 if (data_count < 40) {
2805 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2806 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2809 /* offset to next quota record.
2810 * 4 bytes IVAL(pdata,0)
2811 * unused here...
2814 /* sid len */
2815 sid_len = IVAL(pdata,4);
2817 if (data_count < 40+sid_len) {
2818 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2819 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2822 /* unknown 8 bytes in pdata
2823 * maybe its the change time in NTTIME
2826 /* the used space 8 bytes (SMB_BIG_UINT)*/
2827 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2828 #ifdef LARGE_SMB_OFF_T
2829 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2830 #else /* LARGE_SMB_OFF_T */
2831 if ((IVAL(pdata,20) != 0)&&
2832 ((qt.usedspace != 0xFFFFFFFF)||
2833 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2834 /* more than 32 bits? */
2835 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2837 #endif /* LARGE_SMB_OFF_T */
2839 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2840 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2841 #ifdef LARGE_SMB_OFF_T
2842 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2843 #else /* LARGE_SMB_OFF_T */
2844 if ((IVAL(pdata,28) != 0)&&
2845 ((qt.softlim != 0xFFFFFFFF)||
2846 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2847 /* more than 32 bits? */
2848 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2850 #endif /* LARGE_SMB_OFF_T */
2852 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2853 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2854 #ifdef LARGE_SMB_OFF_T
2855 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2856 #else /* LARGE_SMB_OFF_T */
2857 if ((IVAL(pdata,36) != 0)&&
2858 ((qt.hardlim != 0xFFFFFFFF)||
2859 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2860 /* more than 32 bits? */
2861 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2863 #endif /* LARGE_SMB_OFF_T */
2865 sid_parse(pdata+40,sid_len,&sid);
2866 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2868 /* 44 unknown bytes left... */
2870 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2871 return ERROR_DOS(ERRSRV,ERRerror);
2874 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len,
2875 pdata, data_len);
2877 return -1;
2879 #endif /* HAVE_SYS_QUOTAS */
2881 static int handle_nttrans(connection_struct *conn,
2882 struct trans_state *state,
2883 char *inbuf, char *outbuf, int size, int bufsize)
2885 int outsize;
2887 if (Protocol >= PROTOCOL_NT1) {
2888 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | 0x40); /* IS_LONG_NAME */
2891 /* Now we must call the relevant NT_TRANS function */
2892 switch(state->call) {
2893 case NT_TRANSACT_CREATE:
2895 START_PROFILE(NT_transact_create);
2896 outsize = call_nt_transact_create(conn, inbuf, outbuf,
2897 size, bufsize,
2898 &state->setup, state->setup_count,
2899 &state->param, state->total_param,
2900 &state->data, state->total_data,
2901 state->max_data_return);
2902 END_PROFILE(NT_transact_create);
2903 break;
2906 case NT_TRANSACT_IOCTL:
2908 START_PROFILE(NT_transact_ioctl);
2909 outsize = call_nt_transact_ioctl(conn, inbuf, outbuf,
2910 size, bufsize,
2911 &state->setup, state->setup_count,
2912 &state->param, state->total_param,
2913 &state->data, state->total_data, state->max_data_return);
2914 END_PROFILE(NT_transact_ioctl);
2915 break;
2918 case NT_TRANSACT_SET_SECURITY_DESC:
2920 START_PROFILE(NT_transact_set_security_desc);
2921 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
2922 size, bufsize,
2923 &state->setup, state->setup_count,
2924 &state->param, state->total_param,
2925 &state->data, state->total_data, state->max_data_return);
2926 END_PROFILE(NT_transact_set_security_desc);
2927 break;
2930 case NT_TRANSACT_NOTIFY_CHANGE:
2932 START_PROFILE(NT_transact_notify_change);
2933 outsize = call_nt_transact_notify_change(
2934 conn, inbuf, outbuf, size, bufsize,
2935 &state->setup, state->setup_count,
2936 &state->param, state->total_param,
2937 &state->data, state->total_data,
2938 state->max_data_return,
2939 state->max_param_return);
2940 END_PROFILE(NT_transact_notify_change);
2941 break;
2944 case NT_TRANSACT_RENAME:
2946 START_PROFILE(NT_transact_rename);
2947 outsize = call_nt_transact_rename(conn, inbuf, outbuf,
2948 size, bufsize,
2949 &state->setup, state->setup_count,
2950 &state->param, state->total_param,
2951 &state->data, state->total_data, state->max_data_return);
2952 END_PROFILE(NT_transact_rename);
2953 break;
2956 case NT_TRANSACT_QUERY_SECURITY_DESC:
2958 START_PROFILE(NT_transact_query_security_desc);
2959 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
2960 size, bufsize,
2961 &state->setup, state->setup_count,
2962 &state->param, state->total_param,
2963 &state->data, state->total_data, state->max_data_return);
2964 END_PROFILE(NT_transact_query_security_desc);
2965 break;
2968 #ifdef HAVE_SYS_QUOTAS
2969 case NT_TRANSACT_GET_USER_QUOTA:
2971 START_PROFILE(NT_transact_get_user_quota);
2972 outsize = call_nt_transact_get_user_quota(conn, inbuf, outbuf,
2973 size, bufsize,
2974 &state->setup, state->setup_count,
2975 &state->param, state->total_param,
2976 &state->data, state->total_data, state->max_data_return);
2977 END_PROFILE(NT_transact_get_user_quota);
2978 break;
2981 case NT_TRANSACT_SET_USER_QUOTA:
2983 START_PROFILE(NT_transact_set_user_quota);
2984 outsize = call_nt_transact_set_user_quota(conn, inbuf, outbuf,
2985 size, bufsize,
2986 &state->setup, state->setup_count,
2987 &state->param, state->total_param,
2988 &state->data, state->total_data, state->max_data_return);
2989 END_PROFILE(NT_transact_set_user_quota);
2990 break;
2992 #endif /* HAVE_SYS_QUOTAS */
2994 default:
2995 /* Error in request */
2996 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n",
2997 state->call));
2998 return ERROR_DOS(ERRSRV,ERRerror);
3000 return outsize;
3003 /****************************************************************************
3004 Reply to a SMBNTtrans.
3005 ****************************************************************************/
3007 int reply_nttrans(connection_struct *conn,
3008 char *inbuf,char *outbuf,int size,int bufsize)
3010 int outsize = 0;
3011 uint32 pscnt = IVAL(inbuf,smb_nt_ParameterCount);
3012 uint32 psoff = IVAL(inbuf,smb_nt_ParameterOffset);
3013 uint32 dscnt = IVAL(inbuf,smb_nt_DataCount);
3014 uint32 dsoff = IVAL(inbuf,smb_nt_DataOffset);
3016 uint16 function_code = SVAL( inbuf, smb_nt_Function);
3017 NTSTATUS result;
3018 struct trans_state *state;
3020 START_PROFILE(SMBnttrans);
3022 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3023 END_PROFILE(SMBnttrans);
3024 return ERROR_DOS(ERRSRV,ERRaccess);
3027 result = allow_new_trans(conn->pending_trans, SVAL(inbuf, smb_mid));
3028 if (!NT_STATUS_IS_OK(result)) {
3029 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3030 END_PROFILE(SMBnttrans);
3031 return ERROR_NT(result);
3034 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
3035 END_PROFILE(SMBnttrans);
3036 return ERROR_DOS(ERRSRV,ERRaccess);
3039 state->cmd = SMBnttrans;
3041 state->mid = SVAL(inbuf,smb_mid);
3042 state->vuid = SVAL(inbuf,smb_uid);
3043 state->total_data = IVAL(inbuf, smb_nt_TotalDataCount);
3044 state->data = NULL;
3045 state->total_param = IVAL(inbuf, smb_nt_TotalParameterCount);
3046 state->param = NULL;
3047 state->max_data_return = IVAL(inbuf,smb_nt_MaxDataCount);
3048 state->max_param_return = IVAL(inbuf,smb_nt_MaxParameterCount);
3050 /* setup count is in *words* */
3051 state->setup_count = 2*CVAL(inbuf,smb_nt_SetupCount);
3052 state->setup = NULL;
3053 state->call = function_code;
3056 * All nttrans messages we handle have smb_wct == 19 +
3057 * state->setup_count. Ensure this is so as a sanity check.
3060 if(CVAL(inbuf, smb_wct) != 19 + (state->setup_count/2)) {
3061 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3062 CVAL(inbuf, smb_wct), 19 + (state->setup_count/2)));
3063 goto bad_param;
3066 /* Don't allow more than 128mb for each value. */
3067 if ((state->total_data > (1024*1024*128)) ||
3068 (state->total_param > (1024*1024*128))) {
3069 END_PROFILE(SMBnttrans);
3070 return ERROR_DOS(ERRDOS,ERRnomem);
3073 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3074 goto bad_param;
3076 if (state->total_data) {
3077 /* Can't use talloc here, the core routines do realloc on the
3078 * params and data. */
3079 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3080 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3081 "bytes !\n", (unsigned int)state->total_data));
3082 TALLOC_FREE(state);
3083 END_PROFILE(SMBnttrans);
3084 return(ERROR_DOS(ERRDOS,ERRnomem));
3086 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
3087 goto bad_param;
3088 if ((smb_base(inbuf)+dsoff+dscnt > inbuf + size) ||
3089 (smb_base(inbuf)+dsoff+dscnt < smb_base(inbuf)))
3090 goto bad_param;
3092 memcpy(state->data,smb_base(inbuf)+dsoff,dscnt);
3095 if (state->total_param) {
3096 /* Can't use talloc here, the core routines do realloc on the
3097 * params and data. */
3098 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3099 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3100 "bytes !\n", (unsigned int)state->total_param));
3101 SAFE_FREE(state->data);
3102 TALLOC_FREE(state);
3103 END_PROFILE(SMBnttrans);
3104 return(ERROR_DOS(ERRDOS,ERRnomem));
3106 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
3107 goto bad_param;
3108 if ((smb_base(inbuf)+psoff+pscnt > inbuf + size) ||
3109 (smb_base(inbuf)+psoff+pscnt < smb_base(inbuf)))
3110 goto bad_param;
3112 memcpy(state->param,smb_base(inbuf)+psoff,pscnt);
3115 state->received_data = dscnt;
3116 state->received_param = pscnt;
3118 if(state->setup_count > 0) {
3119 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3120 state->setup_count));
3121 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3122 if (state->setup == NULL) {
3123 DEBUG(0,("reply_nttrans : Out of memory\n"));
3124 SAFE_FREE(state->data);
3125 SAFE_FREE(state->param);
3126 TALLOC_FREE(state);
3127 END_PROFILE(SMBnttrans);
3128 return ERROR_DOS(ERRDOS,ERRnomem);
3131 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
3132 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
3133 goto bad_param;
3135 if (smb_nt_SetupStart + state->setup_count > size) {
3136 goto bad_param;
3139 memcpy( state->setup, &inbuf[smb_nt_SetupStart], state->setup_count);
3140 dump_data(10, (uint8 *)state->setup, state->setup_count);
3143 if ((state->received_data == state->total_data) &&
3144 (state->received_param == state->total_param)) {
3145 outsize = handle_nttrans(conn, state, inbuf, outbuf,
3146 size, bufsize);
3147 SAFE_FREE(state->param);
3148 SAFE_FREE(state->data);
3149 TALLOC_FREE(state);
3150 END_PROFILE(SMBnttrans);
3151 return outsize;
3154 DLIST_ADD(conn->pending_trans, state);
3156 /* We need to send an interim response then receive the rest
3157 of the parameter/data bytes */
3158 outsize = set_message(inbuf,outbuf,0,0,False);
3159 show_msg(outbuf);
3160 END_PROFILE(SMBnttrans);
3161 return outsize;
3163 bad_param:
3165 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3166 SAFE_FREE(state->data);
3167 SAFE_FREE(state->param);
3168 TALLOC_FREE(state);
3169 END_PROFILE(SMBnttrans);
3170 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
3173 /****************************************************************************
3174 Reply to a SMBnttranss
3175 ****************************************************************************/
3177 int reply_nttranss(connection_struct *conn, char *inbuf,char *outbuf,
3178 int size,int bufsize)
3180 int outsize = 0;
3181 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
3182 struct trans_state *state;
3184 START_PROFILE(SMBnttranss);
3186 show_msg(inbuf);
3188 for (state = conn->pending_trans; state != NULL;
3189 state = state->next) {
3190 if (state->mid == SVAL(inbuf,smb_mid)) {
3191 break;
3195 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3196 END_PROFILE(SMBnttranss);
3197 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
3200 /* Revise state->total_param and state->total_data in case they have
3201 changed downwards */
3202 if (IVAL(inbuf, smb_nts_TotalParameterCount) < state->total_param) {
3203 state->total_param = IVAL(inbuf, smb_nts_TotalParameterCount);
3205 if (IVAL(inbuf, smb_nts_TotalDataCount) < state->total_data) {
3206 state->total_data = IVAL(inbuf, smb_nts_TotalDataCount);
3209 pcnt = IVAL(inbuf,smb_nts_ParameterCount);
3210 poff = IVAL(inbuf, smb_nts_ParameterOffset);
3211 pdisp = IVAL(inbuf, smb_nts_ParameterDisplacement);
3213 dcnt = IVAL(inbuf, smb_nts_DataCount);
3214 ddisp = IVAL(inbuf, smb_nts_DataDisplacement);
3215 doff = IVAL(inbuf, smb_nts_DataOffset);
3217 state->received_param += pcnt;
3218 state->received_data += dcnt;
3220 if ((state->received_data > state->total_data) ||
3221 (state->received_param > state->total_param))
3222 goto bad_param;
3224 if (pcnt) {
3225 if (pdisp+pcnt > state->total_param)
3226 goto bad_param;
3227 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3228 goto bad_param;
3229 if (pdisp > state->total_param)
3230 goto bad_param;
3231 if ((smb_base(inbuf) + poff + pcnt > inbuf + size) ||
3232 (smb_base(inbuf) + poff + pcnt < smb_base(inbuf)))
3233 goto bad_param;
3234 if (state->param + pdisp < state->param)
3235 goto bad_param;
3237 memcpy(state->param+pdisp,smb_base(inbuf)+poff,
3238 pcnt);
3241 if (dcnt) {
3242 if (ddisp+dcnt > state->total_data)
3243 goto bad_param;
3244 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
3245 goto bad_param;
3246 if (ddisp > state->total_data)
3247 goto bad_param;
3248 if ((smb_base(inbuf) + doff + dcnt > inbuf + size) ||
3249 (smb_base(inbuf) + doff + dcnt < smb_base(inbuf)))
3250 goto bad_param;
3251 if (state->data + ddisp < state->data)
3252 goto bad_param;
3254 memcpy(state->data+ddisp, smb_base(inbuf)+doff,
3255 dcnt);
3258 if ((state->received_param < state->total_param) ||
3259 (state->received_data < state->total_data)) {
3260 END_PROFILE(SMBnttranss);
3261 return -1;
3264 /* construct_reply_common has done us the favor to pre-fill the
3265 * command field with SMBnttranss which is wrong :-)
3267 SCVAL(outbuf,smb_com,SMBnttrans);
3269 outsize = handle_nttrans(conn, state, inbuf, outbuf,
3270 size, bufsize);
3272 DLIST_REMOVE(conn->pending_trans, state);
3273 SAFE_FREE(state->data);
3274 SAFE_FREE(state->param);
3275 TALLOC_FREE(state);
3277 if (outsize == 0) {
3278 END_PROFILE(SMBnttranss);
3279 return(ERROR_DOS(ERRSRV,ERRnosupport));
3282 END_PROFILE(SMBnttranss);
3283 return(outsize);
3285 bad_param:
3287 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3288 DLIST_REMOVE(conn->pending_trans, state);
3289 SAFE_FREE(state->data);
3290 SAFE_FREE(state->param);
3291 TALLOC_FREE(state);
3292 END_PROFILE(SMBnttranss);
3293 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);