r10909: Give better shutdown messages
[Samba/nascimento.git] / source3 / smbd / nttrans.c
blob3db55bad14d275da0adb908071a51daaa6069c80
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 "\\spoolss",
41 "\\netdfs",
42 "\\rpcecho",
43 "\\svcctl",
44 "\\eventlog",
45 "\\unixinfo",
46 NULL
49 static char *nttrans_realloc(char **ptr, size_t size)
51 char *tptr = NULL;
52 if (ptr==NULL) {
53 smb_panic("nttrans_realloc() called with NULL ptr\n");
56 tptr = SMB_REALLOC(*ptr, size);
57 if(tptr == NULL) {
58 *ptr = NULL;
59 return NULL;
61 memset(tptr,'\0',size);
63 *ptr = tptr;
65 return tptr;
68 /****************************************************************************
69 Send the required number of replies back.
70 We assume all fields other than the data fields are
71 set correctly for the type of call.
72 HACK ! Always assumes smb_setup field is zero.
73 ****************************************************************************/
75 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, NTSTATUS nt_error, char *params,
76 int paramsize, char *pdata, int datasize)
78 int data_to_send = datasize;
79 int params_to_send = paramsize;
80 int useable_space;
81 char *pp = params;
82 char *pd = pdata;
83 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
84 int alignment_offset = 3;
85 int data_alignment_offset = 0;
88 * Initially set the wcnt area to be 18 - this is true for all
89 * transNT replies.
92 set_message(outbuf,18,0,True);
94 if (NT_STATUS_V(nt_error)) {
95 ERROR_NT(nt_error);
98 /*
99 * If there genuinely are no parameters or data to send just send
100 * the empty packet.
103 if(params_to_send == 0 && data_to_send == 0) {
104 show_msg(outbuf);
105 if (!send_smb(smbd_server_fd(),outbuf)) {
106 exit_server("send_nt_replies: send_smb failed.");
108 return 0;
112 * When sending params and data ensure that both are nicely aligned.
113 * Only do this alignment when there is also data to send - else
114 * can cause NT redirector problems.
117 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
118 data_alignment_offset = 4 - (params_to_send % 4);
122 * Space is bufsize minus Netbios over TCP header minus SMB header.
123 * The alignment_offset is to align the param bytes on a four byte
124 * boundary (2 bytes for data len, one byte pad).
125 * NT needs this to work correctly.
128 useable_space = bufsize - ((smb_buf(outbuf)+
129 alignment_offset+data_alignment_offset) -
130 outbuf);
133 * useable_space can never be more than max_send minus the
134 * alignment offset.
137 useable_space = MIN(useable_space,
138 max_send - (alignment_offset+data_alignment_offset));
141 while (params_to_send || data_to_send) {
144 * Calculate whether we will totally or partially fill this packet.
147 total_sent_thistime = params_to_send + data_to_send +
148 alignment_offset + data_alignment_offset;
151 * We can never send more than useable_space.
154 total_sent_thistime = MIN(total_sent_thistime, useable_space);
156 set_message(outbuf, 18, total_sent_thistime, True);
159 * Set total params and data to be sent.
162 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
163 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
166 * Calculate how many parameters and data we can fit into
167 * this packet. Parameters get precedence.
170 params_sent_thistime = MIN(params_to_send,useable_space);
171 data_sent_thistime = useable_space - params_sent_thistime;
172 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
174 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
176 if(params_sent_thistime == 0) {
177 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
178 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
179 } else {
181 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
182 * parameter bytes, however the first 4 bytes of outbuf are
183 * the Netbios over TCP header. Thus use smb_base() to subtract
184 * them from the calculation.
187 SIVAL(outbuf,smb_ntr_ParameterOffset,
188 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
190 * Absolute displacement of param bytes sent in this packet.
193 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
197 * Deal with the data portion.
200 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
202 if(data_sent_thistime == 0) {
203 SIVAL(outbuf,smb_ntr_DataOffset,0);
204 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
205 } else {
207 * The offset of the data bytes is the offset of the
208 * parameter bytes plus the number of parameters being sent this time.
211 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
212 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
213 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
217 * Copy the param bytes into the packet.
220 if(params_sent_thistime) {
221 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
225 * Copy in the data bytes
228 if(data_sent_thistime) {
229 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
230 data_alignment_offset,pd,data_sent_thistime);
233 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
234 params_sent_thistime, data_sent_thistime, useable_space));
235 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
236 params_to_send, data_to_send, paramsize, datasize));
238 /* Send the packet */
239 show_msg(outbuf);
240 if (!send_smb(smbd_server_fd(),outbuf)) {
241 exit_server("send_nt_replies: send_smb failed.");
244 pp += params_sent_thistime;
245 pd += data_sent_thistime;
247 params_to_send -= params_sent_thistime;
248 data_to_send -= data_sent_thistime;
251 * Sanity check
254 if(params_to_send < 0 || data_to_send < 0) {
255 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
256 params_to_send, data_to_send));
257 return -1;
261 return 0;
264 /****************************************************************************
265 Is it an NTFS stream name ?
266 ****************************************************************************/
268 BOOL is_ntfs_stream_name(const char *fname)
270 if (lp_posix_pathnames()) {
271 return False;
273 return (strchr_m(fname, ':') != NULL) ? True : False;
276 /****************************************************************************
277 Save case statics.
278 ****************************************************************************/
280 static BOOL saved_case_sensitive;
281 static BOOL saved_case_preserve;
282 static BOOL saved_short_case_preserve;
284 /****************************************************************************
285 Save case semantics.
286 ****************************************************************************/
288 static void set_posix_case_semantics(connection_struct *conn, uint32 file_attributes)
290 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
291 return;
294 saved_case_sensitive = conn->case_sensitive;
295 saved_case_preserve = conn->case_preserve;
296 saved_short_case_preserve = conn->short_case_preserve;
298 /* Set to POSIX. */
299 conn->case_sensitive = True;
300 conn->case_preserve = True;
301 conn->short_case_preserve = True;
304 /****************************************************************************
305 Restore case semantics.
306 ****************************************************************************/
308 static void restore_case_semantics(connection_struct *conn, uint32 file_attributes)
310 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) {
311 return;
314 conn->case_sensitive = saved_case_sensitive;
315 conn->case_preserve = saved_case_preserve;
316 conn->short_case_preserve = saved_short_case_preserve;
319 /****************************************************************************
320 Reply to an NT create and X call on a pipe.
321 ****************************************************************************/
323 static int nt_open_pipe(char *fname, connection_struct *conn,
324 char *inbuf, char *outbuf, int *ppnum)
326 smb_np_struct *p = NULL;
327 uint16 vuid = SVAL(inbuf, smb_uid);
328 int i;
330 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
332 /* See if it is one we want to handle. */
334 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
335 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
338 for( i = 0; known_nt_pipes[i]; i++ ) {
339 if( strequal(fname,known_nt_pipes[i])) {
340 break;
344 if ( known_nt_pipes[i] == NULL ) {
345 return(ERROR_BOTH(NT_STATUS_OBJECT_NAME_NOT_FOUND,ERRDOS,ERRbadpipe));
348 /* Strip \\ off the name. */
349 fname++;
351 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
353 p = open_rpc_pipe_p(fname, conn, vuid);
354 if (!p) {
355 return(ERROR_DOS(ERRSRV,ERRnofids));
358 *ppnum = p->pnum;
359 return 0;
362 /****************************************************************************
363 Reply to an NT create and X call for pipes.
364 ****************************************************************************/
366 static int do_ntcreate_pipe_open(connection_struct *conn,
367 char *inbuf,char *outbuf,int length,int bufsize)
369 pstring fname;
370 int ret;
371 int pnum = -1;
372 char *p = NULL;
374 srvstr_pull_buf(inbuf, fname, smb_buf(inbuf), sizeof(fname), STR_TERMINATE);
376 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0) {
377 return ret;
381 * Deal with pipe return.
384 set_message(outbuf,34,0,True);
386 p = outbuf + smb_vwv2;
387 p++;
388 SSVAL(p,0,pnum);
389 p += 2;
390 SIVAL(p,0,FILE_WAS_OPENED);
391 p += 4;
392 p += 32;
393 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
394 p += 20;
395 /* File type. */
396 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
397 /* Device state. */
398 SSVAL(p,2, 0x5FF); /* ? */
400 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
402 return chain_reply(inbuf,outbuf,length,bufsize);
405 /****************************************************************************
406 Reply to an NT create and X call for a quota file.
407 ****************************************************************************/
409 int reply_ntcreate_and_X_quota(connection_struct *conn,
410 char *inbuf,
411 char *outbuf,
412 int length,
413 int bufsize,
414 enum FAKE_FILE_TYPE fake_file_type,
415 const char *fname)
417 int result;
418 char *p;
419 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
420 files_struct *fsp = open_fake_file(conn, fake_file_type, fname, desired_access);
422 if (!fsp) {
423 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
426 set_message(outbuf,34,0,True);
428 p = outbuf + smb_vwv2;
430 /* SCVAL(p,0,NO_OPLOCK_RETURN); */
431 p++;
432 SSVAL(p,0,fsp->fnum);
433 #if 0
434 p += 2;
435 SIVAL(p,0,smb_action);
436 p += 4;
438 /* Create time. */
439 put_long_date(p,c_time);
440 p += 8;
441 put_long_date(p,sbuf.st_atime); /* access time */
442 p += 8;
443 put_long_date(p,sbuf.st_mtime); /* write time */
444 p += 8;
445 put_long_date(p,sbuf.st_mtime); /* change time */
446 p += 8;
447 SIVAL(p,0,fattr); /* File Attributes. */
448 p += 4;
449 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
450 p += 8;
451 SOFF_T(p,0,file_len);
452 p += 8;
453 if (flags & EXTENDED_RESPONSE_REQUIRED)
454 SSVAL(p,2,0x7);
455 p += 4;
456 SCVAL(p,0,fsp->is_directory ? 1 : 0);
457 #endif
459 DEBUG(5,("reply_ntcreate_and_X_quota: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
461 result = chain_reply(inbuf,outbuf,length,bufsize);
462 return result;
465 /****************************************************************************
466 Reply to an NT create and X call.
467 ****************************************************************************/
469 int reply_ntcreate_and_X(connection_struct *conn,
470 char *inbuf,char *outbuf,int length,int bufsize)
472 int result;
473 pstring fname;
474 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
475 uint32 access_mask = IVAL(inbuf,smb_ntcreate_DesiredAccess);
476 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
477 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
478 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
479 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
480 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
481 /* Breakout the oplock request bits so we can set the
482 reply bits separately. */
483 int oplock_request = 0;
484 uint32 fattr=0;
485 SMB_OFF_T file_len = 0;
486 SMB_STRUCT_STAT sbuf;
487 int info = 0;
488 BOOL bad_path = False;
489 files_struct *fsp=NULL;
490 char *p = NULL;
491 time_t c_time;
492 BOOL extended_oplock_granted = False;
493 NTSTATUS status;
495 START_PROFILE(SMBntcreateX);
497 DEBUG(10,("reply_ntcreateX: flags = 0x%x, access_mask = 0x%x \
498 file_attributes = 0x%x, share_access = 0x%x, create_disposition = 0x%x \
499 create_options = 0x%x root_dir_fid = 0x%x\n",
500 (unsigned int)flags,
501 (unsigned int)access_mask,
502 (unsigned int)file_attributes,
503 (unsigned int)share_access,
504 (unsigned int)create_disposition,
505 (unsigned int)create_options,
506 (unsigned int)root_dir_fid ));
508 /* If it's an IPC, use the pipe handler. */
510 if (IS_IPC(conn)) {
511 if (lp_nt_pipe_support()) {
512 END_PROFILE(SMBntcreateX);
513 return do_ntcreate_pipe_open(conn,inbuf,outbuf,length,bufsize);
514 } else {
515 END_PROFILE(SMBntcreateX);
516 return(ERROR_DOS(ERRDOS,ERRnoaccess));
520 if (create_options & FILE_OPEN_BY_FILE_ID) {
521 END_PROFILE(SMBntcreateX);
522 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
526 * Get the file name.
529 if(root_dir_fid != 0) {
531 * This filename is relative to a directory fid.
533 pstring rel_fname;
534 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
535 size_t dir_name_len;
537 if(!dir_fsp) {
538 END_PROFILE(SMBntcreateX);
539 return(ERROR_DOS(ERRDOS,ERRbadfid));
542 if(!dir_fsp->is_directory) {
544 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status,False);
545 if (!NT_STATUS_IS_OK(status)) {
546 END_PROFILE(SMBntcreateX);
547 return ERROR_NT(status);
551 * Check to see if this is a mac fork of some kind.
554 if( is_ntfs_stream_name(fname)) {
555 END_PROFILE(SMBntcreateX);
556 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
560 we need to handle the case when we get a
561 relative open relative to a file and the
562 pathname is blank - this is a reopen!
563 (hint from demyn plantenberg)
566 END_PROFILE(SMBntcreateX);
567 return(ERROR_DOS(ERRDOS,ERRbadfid));
571 * Copy in the base directory name.
574 pstrcpy( fname, dir_fsp->fsp_name );
575 dir_name_len = strlen(fname);
578 * Ensure it ends in a '\'.
581 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
582 pstrcat(fname, "/");
583 dir_name_len++;
586 srvstr_get_path(inbuf, rel_fname, smb_buf(inbuf), sizeof(rel_fname), 0, STR_TERMINATE, &status,False);
587 if (!NT_STATUS_IS_OK(status)) {
588 END_PROFILE(SMBntcreateX);
589 return ERROR_NT(status);
591 pstrcat(fname, rel_fname);
592 } else {
593 srvstr_get_path(inbuf, fname, smb_buf(inbuf), sizeof(fname), 0, STR_TERMINATE, &status,False);
594 if (!NT_STATUS_IS_OK(status)) {
595 END_PROFILE(SMBntcreateX);
596 return ERROR_NT(status);
600 * Check to see if this is a mac fork of some kind.
603 if( is_ntfs_stream_name(fname)) {
604 enum FAKE_FILE_TYPE fake_file_type = is_fake_file(fname);
605 if (fake_file_type!=FAKE_FILE_TYPE_NONE) {
607 * Here we go! support for changing the disk quotas --metze
609 * We need to fake up to open this MAGIC QUOTA file
610 * and return a valid FID.
612 * w2k close this file directly after openening
613 * xp also tries a QUERY_FILE_INFO on the file and then close it
615 result = reply_ntcreate_and_X_quota(conn, inbuf, outbuf, length, bufsize,
616 fake_file_type, fname);
617 END_PROFILE(SMBntcreateX);
618 return result;
619 } else {
620 END_PROFILE(SMBntcreateX);
621 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
627 * Now contruct the smb_open_mode value from the filename,
628 * desired access and the share access.
630 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
632 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
633 if (oplock_request) {
634 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
638 * Ordinary file or directory.
642 * Check if POSIX semantics are wanted.
645 set_posix_case_semantics(conn, file_attributes);
647 unix_convert(fname,conn,0,&bad_path,&sbuf);
649 if (bad_path) {
650 restore_case_semantics(conn, file_attributes);
651 END_PROFILE(SMBntcreateX);
652 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
654 /* All file access must go through check_name() */
655 if (!check_name(fname,conn)) {
656 restore_case_semantics(conn, file_attributes);
657 END_PROFILE(SMBntcreateX);
658 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRbadpath);
661 #if 0
662 /* This is the correct thing to do (check every time) but can_delete is
663 expensive (it may have to read the parent directory permissions). So
664 for now we're not doing it unless we have a strong hint the client
665 is really going to delete this file. */
666 if (desired_access & DELETE_ACCESS) {
667 #else
668 /* Setting FILE_SHARE_DELETE is the hint. */
669 if (lp_acl_check_permissions(SNUM(conn)) && (share_access & FILE_SHARE_DELETE)
670 && (access_mask & DELETE_ACCESS)) {
671 #endif
672 status = can_delete(conn, fname, file_attributes, bad_path, True);
673 /* We're only going to fail here if it's access denied, as that's the
674 only error we care about for "can we delete this ?" questions. */
675 if (!NT_STATUS_IS_OK(status) && (NT_STATUS_EQUAL(status,NT_STATUS_ACCESS_DENIED) ||
676 NT_STATUS_EQUAL(status,NT_STATUS_CANNOT_DELETE))) {
677 restore_case_semantics(conn, file_attributes);
678 END_PROFILE(SMBntcreateX);
679 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
684 * If it's a request for a directory open, deal with it separately.
687 if(create_options & FILE_DIRECTORY_FILE) {
688 oplock_request = 0;
690 /* Can't open a temp directory. IFS kit test. */
691 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
692 END_PROFILE(SMBntcreateX);
693 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
696 fsp = open_directory(conn, fname, &sbuf,
697 access_mask,
698 share_access,
699 create_disposition,
700 create_options,
701 &info);
703 restore_case_semantics(conn, file_attributes);
705 if(!fsp) {
706 END_PROFILE(SMBntcreateX);
707 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
709 } else {
711 * Ordinary file case.
714 /* NB. We have a potential bug here. If we
715 * cause an oplock break to ourselves, then we
716 * could end up processing filename related
717 * SMB requests whilst we await the oplock
718 * break response. As we may have changed the
719 * filename case semantics to be POSIX-like,
720 * this could mean a filename request could
721 * fail when it should succeed. This is a rare
722 * condition, but eventually we must arrange
723 * to restore the correct case semantics
724 * before issuing an oplock break request to
725 * our client. JRA. */
727 fsp = open_file_ntcreate(conn,fname,&sbuf,
728 access_mask,
729 share_access,
730 create_disposition,
731 create_options,
732 file_attributes,
733 oplock_request,
734 &info);
735 if (!fsp) {
736 /* We cheat here. There are two cases we
737 * care about. One is a directory rename,
738 * where the NT client will attempt to
739 * open the source directory for
740 * DELETE access. Note that when the
741 * NT client does this it does *not*
742 * set the directory bit in the
743 * request packet. This is translated
744 * into a read/write open
745 * request. POSIX states that any open
746 * for write request on a directory
747 * will generate an EISDIR error, so
748 * we can catch this here and open a
749 * pseudo handle that is flagged as a
750 * directory. The second is an open
751 * for a permissions read only, which
752 * we handle in the open_file_stat case. JRA.
755 if(errno == EISDIR) {
758 * Fail the open if it was explicitly a non-directory file.
761 if (create_options & FILE_NON_DIRECTORY_FILE) {
762 restore_case_semantics(conn, file_attributes);
763 END_PROFILE(SMBntcreateX);
764 return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
767 oplock_request = 0;
768 fsp = open_directory(conn, fname, &sbuf,
769 access_mask,
770 share_access,
771 create_disposition,
772 create_options,
773 &info);
775 if(!fsp) {
776 restore_case_semantics(conn, file_attributes);
777 END_PROFILE(SMBntcreateX);
778 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
780 } else {
782 restore_case_semantics(conn, file_attributes);
783 END_PROFILE(SMBntcreateX);
784 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
785 /* We have re-scheduled this call. */
786 return -1;
788 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
793 restore_case_semantics(conn, file_attributes);
795 file_len = sbuf.st_size;
796 fattr = dos_mode(conn,fname,&sbuf);
797 if(fattr == 0) {
798 fattr = FILE_ATTRIBUTE_NORMAL;
800 if (!fsp->is_directory && (fattr & aDIR)) {
801 close_file(fsp,False);
802 END_PROFILE(SMBntcreateX);
803 return ERROR_DOS(ERRDOS,ERRnoaccess);
806 /* Save the requested allocation size. */
807 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
808 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize);
809 #ifdef LARGE_SMB_OFF_T
810 allocation_size |= (((SMB_BIG_UINT)IVAL(inbuf,smb_ntcreate_AllocationSize + 4)) << 32);
811 #endif
812 if (allocation_size && (allocation_size > (SMB_BIG_UINT)file_len)) {
813 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
814 if (fsp->is_directory) {
815 close_file(fsp,False);
816 END_PROFILE(SMBntcreateX);
817 /* Can't set allocation size on a directory. */
818 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
820 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
821 close_file(fsp,False);
822 END_PROFILE(SMBntcreateX);
823 return ERROR_NT(NT_STATUS_DISK_FULL);
825 } else {
826 fsp->initial_allocation_size = smb_roundup(fsp->conn,(SMB_BIG_UINT)file_len);
831 * If the caller set the extended oplock request bit
832 * and we granted one (by whatever means) - set the
833 * correct bit for extended oplock reply.
836 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
837 extended_oplock_granted = True;
840 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
841 extended_oplock_granted = True;
844 #if 0
845 /* W2K sends back 42 words here ! If we do the same it breaks offline sync. Go figure... ? JRA. */
846 set_message(outbuf,42,0,True);
847 #else
848 set_message(outbuf,34,0,True);
849 #endif
851 p = outbuf + smb_vwv2;
854 * Currently as we don't support level II oplocks we just report
855 * exclusive & batch here.
858 if (extended_oplock_granted) {
859 if (flags & REQUEST_BATCH_OPLOCK) {
860 SCVAL(p,0, BATCH_OPLOCK_RETURN);
861 } else {
862 SCVAL(p,0, EXCLUSIVE_OPLOCK_RETURN);
864 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
865 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
866 } else {
867 SCVAL(p,0,NO_OPLOCK_RETURN);
870 p++;
871 SSVAL(p,0,fsp->fnum);
872 p += 2;
873 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
874 SIVAL(p,0,FILE_WAS_SUPERSEDED);
875 } else {
876 SIVAL(p,0,info);
878 p += 4;
880 /* Create time. */
881 c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
883 if (lp_dos_filetime_resolution(SNUM(conn))) {
884 c_time &= ~1;
885 sbuf.st_atime &= ~1;
886 sbuf.st_mtime &= ~1;
887 sbuf.st_mtime &= ~1;
890 put_long_date(p,c_time);
891 p += 8;
892 put_long_date(p,sbuf.st_atime); /* access time */
893 p += 8;
894 put_long_date(p,sbuf.st_mtime); /* write time */
895 p += 8;
896 put_long_date(p,sbuf.st_mtime); /* change time */
897 p += 8;
898 SIVAL(p,0,fattr); /* File Attributes. */
899 p += 4;
900 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
901 p += 8;
902 SOFF_T(p,0,file_len);
903 p += 8;
904 if (flags & EXTENDED_RESPONSE_REQUIRED) {
905 SSVAL(p,2,0x7);
907 p += 4;
908 SCVAL(p,0,fsp->is_directory ? 1 : 0);
910 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
912 result = chain_reply(inbuf,outbuf,length,bufsize);
913 END_PROFILE(SMBntcreateX);
914 return result;
917 /****************************************************************************
918 Reply to a NT_TRANSACT_CREATE call to open a pipe.
919 ****************************************************************************/
921 static int do_nt_transact_create_pipe( connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
922 char **ppsetup, uint32 setup_count,
923 char **ppparams, uint32 parameter_count,
924 char **ppdata, uint32 data_count)
926 pstring fname;
927 char *params = *ppparams;
928 int ret;
929 int pnum = -1;
930 char *p = NULL;
931 NTSTATUS status;
934 * Ensure minimum number of parameters sent.
937 if(parameter_count < 54) {
938 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
939 return ERROR_DOS(ERRDOS,ERRnoaccess);
942 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status, False);
943 if (!NT_STATUS_IS_OK(status)) {
944 return ERROR_NT(status);
947 if ((ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum)) != 0) {
948 return ret;
951 /* Realloc the size of parameters and data we will return */
952 params = nttrans_realloc(ppparams, 69);
953 if(params == NULL) {
954 return ERROR_DOS(ERRDOS,ERRnomem);
957 p = params;
958 SCVAL(p,0,NO_OPLOCK_RETURN);
960 p += 2;
961 SSVAL(p,0,pnum);
962 p += 2;
963 SIVAL(p,0,FILE_WAS_OPENED);
964 p += 8;
966 p += 32;
967 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
968 p += 20;
969 /* File type. */
970 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
971 /* Device state. */
972 SSVAL(p,2, 0x5FF); /* ? */
974 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
976 /* Send the required number of replies */
977 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
979 return -1;
982 /****************************************************************************
983 Internal fn to set security descriptors.
984 ****************************************************************************/
986 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
988 prs_struct pd;
989 SEC_DESC *psd = NULL;
990 TALLOC_CTX *mem_ctx;
991 BOOL ret;
993 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
994 return NT_STATUS_OK;
998 * Init the parse struct we will unmarshall from.
1001 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1002 DEBUG(0,("set_sd: talloc_init failed.\n"));
1003 return NT_STATUS_NO_MEMORY;
1006 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1009 * Setup the prs_struct to point at the memory we just
1010 * allocated.
1013 prs_give_memory( &pd, data, sd_len, False);
1016 * Finally, unmarshall from the data buffer.
1019 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1020 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1022 * Return access denied for want of a better error message..
1024 talloc_destroy(mem_ctx);
1025 return NT_STATUS_NO_MEMORY;
1028 if (psd->off_owner_sid==0) {
1029 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1031 if (psd->off_grp_sid==0) {
1032 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1034 if (psd->off_sacl==0) {
1035 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1037 if (psd->off_dacl==0) {
1038 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1041 ret = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
1043 if (!ret) {
1044 talloc_destroy(mem_ctx);
1045 return NT_STATUS_ACCESS_DENIED;
1048 talloc_destroy(mem_ctx);
1050 return NT_STATUS_OK;
1053 /****************************************************************************
1054 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1055 ****************************************************************************/
1057 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
1059 struct ea_list *ea_list_head = NULL;
1060 size_t offset = 0;
1062 if (data_size < 4) {
1063 return NULL;
1066 while (offset + 4 <= data_size) {
1067 size_t next_offset = IVAL(pdata,offset);
1068 struct ea_list *tmp;
1069 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
1071 DLIST_ADD_END(ea_list_head, eal, tmp);
1072 if (next_offset == 0) {
1073 break;
1075 offset += next_offset;
1078 return ea_list_head;
1081 /****************************************************************************
1082 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1083 ****************************************************************************/
1085 static int call_nt_transact_create(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1086 char **ppsetup, uint32 setup_count,
1087 char **ppparams, uint32 parameter_count,
1088 char **ppdata, uint32 data_count, uint32 max_data_count)
1090 pstring fname;
1091 char *params = *ppparams;
1092 char *data = *ppdata;
1093 /* Breakout the oplock request bits so we can set the reply bits separately. */
1094 int oplock_request = 0;
1095 uint32 fattr=0;
1096 SMB_OFF_T file_len = 0;
1097 SMB_STRUCT_STAT sbuf;
1098 int info = 0;
1099 BOOL bad_path = False;
1100 files_struct *fsp = NULL;
1101 char *p = NULL;
1102 BOOL extended_oplock_granted = False;
1103 uint32 flags;
1104 uint32 access_mask;
1105 uint32 file_attributes;
1106 uint32 share_access;
1107 uint32 create_disposition;
1108 uint32 create_options;
1109 uint32 sd_len;
1110 uint32 ea_len;
1111 uint16 root_dir_fid;
1112 time_t c_time;
1113 struct ea_list *ea_list = NULL;
1114 TALLOC_CTX *ctx = NULL;
1115 char *pdata = NULL;
1116 NTSTATUS status;
1118 DEBUG(5,("call_nt_transact_create\n"));
1121 * If it's an IPC, use the pipe handler.
1124 if (IS_IPC(conn)) {
1125 if (lp_nt_pipe_support()) {
1126 return do_nt_transact_create_pipe(conn, inbuf, outbuf, length,
1127 bufsize,
1128 ppsetup, setup_count,
1129 ppparams, parameter_count,
1130 ppdata, data_count);
1131 } else {
1132 return ERROR_DOS(ERRDOS,ERRnoaccess);
1137 * Ensure minimum number of parameters sent.
1140 if(parameter_count < 54) {
1141 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1142 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1145 flags = IVAL(params,0);
1146 access_mask = IVAL(params,8);
1147 file_attributes = IVAL(params,20);
1148 share_access = IVAL(params,24);
1149 create_disposition = IVAL(params,28);
1150 create_options = IVAL(params,32);
1151 sd_len = IVAL(params,36);
1152 ea_len = IVAL(params,40);
1153 root_dir_fid = (uint16)IVAL(params,4);
1155 /* Ensure the data_len is correct for the sd and ea values given. */
1156 if ((ea_len + sd_len > data_count) ||
1157 (ea_len > data_count) || (sd_len > data_count) ||
1158 (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1159 DEBUG(10,("call_nt_transact_create - ea_len = %u, sd_len = %u, data_count = %u\n",
1160 (unsigned int)ea_len, (unsigned int)sd_len, (unsigned int)data_count ));
1161 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1164 if (ea_len) {
1165 if (!lp_ea_support(SNUM(conn))) {
1166 DEBUG(10,("call_nt_transact_create - ea_len = %u but EA's not supported.\n",
1167 (unsigned int)ea_len ));
1168 return ERROR_NT(NT_STATUS_EAS_NOT_SUPPORTED);
1171 if (ea_len < 10) {
1172 DEBUG(10,("call_nt_transact_create - ea_len = %u - too small (should be more than 10)\n",
1173 (unsigned int)ea_len ));
1174 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1178 if (create_options & FILE_OPEN_BY_FILE_ID) {
1179 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
1183 * Get the file name.
1186 if(root_dir_fid != 0) {
1188 * This filename is relative to a directory fid.
1190 files_struct *dir_fsp = file_fsp(params,4);
1191 size_t dir_name_len;
1193 if(!dir_fsp) {
1194 return ERROR_DOS(ERRDOS,ERRbadfid);
1197 if(!dir_fsp->is_directory) {
1198 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status, False);
1199 if (!NT_STATUS_IS_OK(status)) {
1200 return ERROR_NT(status);
1204 * Check to see if this is a mac fork of some kind.
1207 if( is_ntfs_stream_name(fname)) {
1208 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1211 return ERROR_DOS(ERRDOS,ERRbadfid);
1215 * Copy in the base directory name.
1218 pstrcpy( fname, dir_fsp->fsp_name );
1219 dir_name_len = strlen(fname);
1222 * Ensure it ends in a '\'.
1225 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
1226 pstrcat(fname, "/");
1227 dir_name_len++;
1231 pstring tmpname;
1232 srvstr_get_path(inbuf, tmpname, params+53, sizeof(tmpname), parameter_count-53, STR_TERMINATE, &status, False);
1233 if (!NT_STATUS_IS_OK(status)) {
1234 return ERROR_NT(status);
1236 pstrcat(fname, tmpname);
1238 } else {
1239 srvstr_get_path(inbuf, fname, params+53, sizeof(fname), parameter_count-53, STR_TERMINATE, &status, False);
1240 if (!NT_STATUS_IS_OK(status)) {
1241 return ERROR_NT(status);
1245 * Check to see if this is a mac fork of some kind.
1248 if( is_ntfs_stream_name(fname)) {
1249 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1253 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1254 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1257 * Check if POSIX semantics are wanted.
1260 set_posix_case_semantics(conn, file_attributes);
1262 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1264 unix_convert(fname,conn,0,&bad_path,&sbuf);
1265 if (bad_path) {
1266 restore_case_semantics(conn, file_attributes);
1267 return ERROR_NT(NT_STATUS_OBJECT_PATH_NOT_FOUND);
1269 /* All file access must go through check_name() */
1270 if (!check_name(fname,conn)) {
1271 restore_case_semantics(conn, file_attributes);
1272 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRbadpath);
1275 #if 0
1276 /* This is the correct thing to do (check every time) but can_delete is
1277 expensive (it may have to read the parent directory permissions). So
1278 for now we're not doing it unless we have a strong hint the client
1279 is really going to delete this file. */
1280 if (desired_access & DELETE_ACCESS) {
1281 #else
1282 /* Setting FILE_SHARE_DELETE is the hint. */
1283 if (lp_acl_check_permissions(SNUM(conn)) && (share_access & FILE_SHARE_DELETE) && (access_mask & DELETE_ACCESS)) {
1284 #endif
1285 status = can_delete(conn, fname, file_attributes, bad_path, True);
1286 /* We're only going to fail here if it's access denied, as that's the
1287 only error we care about for "can we delete this ?" questions. */
1288 if (!NT_STATUS_IS_OK(status) && (NT_STATUS_EQUAL(status,NT_STATUS_ACCESS_DENIED) ||
1289 NT_STATUS_EQUAL(status,NT_STATUS_CANNOT_DELETE))) {
1290 restore_case_semantics(conn, file_attributes);
1291 END_PROFILE(SMBntcreateX);
1292 return ERROR_NT(status);
1296 if (ea_len) {
1297 ctx = talloc_init("NTTRANS_CREATE_EA");
1298 if (!ctx) {
1299 talloc_destroy(ctx);
1300 restore_case_semantics(conn, file_attributes);
1301 return ERROR_NT(NT_STATUS_NO_MEMORY);
1304 pdata = data + sd_len;
1306 /* We have already checked that ea_len <= data_count here. */
1307 ea_list = read_nttrans_ea_list(ctx, pdata, ea_len);
1308 if (!ea_list ) {
1309 talloc_destroy(ctx);
1310 restore_case_semantics(conn, file_attributes);
1311 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1316 * If it's a request for a directory open, deal with it separately.
1319 if(create_options & FILE_DIRECTORY_FILE) {
1321 /* Can't open a temp directory. IFS kit test. */
1322 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
1323 talloc_destroy(ctx);
1324 restore_case_semantics(conn, file_attributes);
1325 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1328 oplock_request = 0;
1331 * We will get a create directory here if the Win32
1332 * app specified a security descriptor in the
1333 * CreateDirectory() call.
1336 fsp = open_directory(conn, fname, &sbuf,
1337 access_mask,
1338 share_access,
1339 create_disposition,
1340 create_options,
1341 &info);
1342 if(!fsp) {
1343 talloc_destroy(ctx);
1344 restore_case_semantics(conn, file_attributes);
1345 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1348 } else {
1351 * Ordinary file case.
1354 fsp = open_file_ntcreate(conn,fname,&sbuf,
1355 access_mask,
1356 share_access,
1357 create_disposition,
1358 create_options,
1359 file_attributes,
1360 oplock_request,
1361 &info);
1363 if (!fsp) {
1364 if(errno == EISDIR) {
1367 * Fail the open if it was explicitly a non-directory file.
1370 if (create_options & FILE_NON_DIRECTORY_FILE) {
1371 restore_case_semantics(conn, file_attributes);
1372 return ERROR_FORCE_NT(NT_STATUS_FILE_IS_A_DIRECTORY);
1375 oplock_request = 0;
1376 fsp = open_directory(conn, fname, &sbuf,
1377 access_mask,
1378 share_access,
1379 create_disposition,
1380 create_options,
1381 &info);
1382 if(!fsp) {
1383 talloc_destroy(ctx);
1384 restore_case_semantics(conn, file_attributes);
1385 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1387 } else {
1388 talloc_destroy(ctx);
1389 restore_case_semantics(conn, file_attributes);
1390 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1391 /* We have re-scheduled this call. */
1392 return -1;
1394 return set_bad_path_error(errno, bad_path, outbuf, ERRDOS,ERRnoaccess);
1400 * According to the MS documentation, the only time the security
1401 * descriptor is applied to the opened file is iff we *created* the
1402 * file; an existing file stays the same.
1404 * Also, it seems (from observation) that you can open the file with
1405 * any access mask but you can still write the sd. We need to override
1406 * the granted access before we call set_sd
1407 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
1410 if (lp_nt_acl_support(SNUM(conn)) && sd_len && info == FILE_WAS_CREATED) {
1411 uint32 saved_access_mask = fsp->access_mask;
1413 /* We have already checked that sd_len <= data_count here. */
1415 fsp->access_mask = FILE_GENERIC_ALL;
1417 status = set_sd( fsp, data, sd_len, ALL_SECURITY_INFORMATION);
1418 if (!NT_STATUS_IS_OK(status)) {
1419 talloc_destroy(ctx);
1420 close_file(fsp,False);
1421 restore_case_semantics(conn, file_attributes);
1422 return ERROR_NT(status);
1424 fsp->access_mask = saved_access_mask;
1427 if (ea_len && (info == FILE_WAS_CREATED)) {
1428 status = set_ea(conn, fsp, fname, ea_list);
1429 talloc_destroy(ctx);
1430 if (!NT_STATUS_IS_OK(status)) {
1431 close_file(fsp,False);
1432 restore_case_semantics(conn, file_attributes);
1433 return ERROR_NT(status);
1437 restore_case_semantics(conn, file_attributes);
1439 file_len = sbuf.st_size;
1440 fattr = dos_mode(conn,fname,&sbuf);
1441 if(fattr == 0) {
1442 fattr = FILE_ATTRIBUTE_NORMAL;
1444 if (!fsp->is_directory && (fattr & aDIR)) {
1445 close_file(fsp,False);
1446 return ERROR_DOS(ERRDOS,ERRnoaccess);
1449 /* Save the requested allocation size. */
1450 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
1451 SMB_BIG_UINT allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1452 #ifdef LARGE_SMB_OFF_T
1453 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1454 #endif
1455 if (allocation_size && (allocation_size > file_len)) {
1456 fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
1457 if (fsp->is_directory) {
1458 close_file(fsp,False);
1459 /* Can't set allocation size on a directory. */
1460 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1462 if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
1463 close_file(fsp,False);
1464 return ERROR_NT(NT_STATUS_DISK_FULL);
1466 } else {
1467 fsp->initial_allocation_size = smb_roundup(fsp->conn, (SMB_BIG_UINT)file_len);
1472 * If the caller set the extended oplock request bit
1473 * and we granted one (by whatever means) - set the
1474 * correct bit for extended oplock reply.
1477 if (oplock_request && lp_fake_oplocks(SNUM(conn))) {
1478 extended_oplock_granted = True;
1481 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
1482 extended_oplock_granted = True;
1485 /* Realloc the size of parameters and data we will return */
1486 params = nttrans_realloc(ppparams, 69);
1487 if(params == NULL) {
1488 return ERROR_DOS(ERRDOS,ERRnomem);
1491 p = params;
1492 if (extended_oplock_granted) {
1493 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1494 } else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type)) {
1495 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1496 } else {
1497 SCVAL(p,0,NO_OPLOCK_RETURN);
1500 p += 2;
1501 SSVAL(p,0,fsp->fnum);
1502 p += 2;
1503 if ((create_disposition == FILE_SUPERSEDE) && (info == FILE_WAS_OVERWRITTEN)) {
1504 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1505 } else {
1506 SIVAL(p,0,info);
1508 p += 8;
1510 /* Create time. */
1511 c_time = get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn)));
1513 if (lp_dos_filetime_resolution(SNUM(conn))) {
1514 c_time &= ~1;
1515 sbuf.st_atime &= ~1;
1516 sbuf.st_mtime &= ~1;
1517 sbuf.st_mtime &= ~1;
1520 put_long_date(p,c_time);
1521 p += 8;
1522 put_long_date(p,sbuf.st_atime); /* access time */
1523 p += 8;
1524 put_long_date(p,sbuf.st_mtime); /* write time */
1525 p += 8;
1526 put_long_date(p,sbuf.st_mtime); /* change time */
1527 p += 8;
1528 SIVAL(p,0,fattr); /* File Attributes. */
1529 p += 4;
1530 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1531 p += 8;
1532 SOFF_T(p,0,file_len);
1533 p += 8;
1534 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1535 SSVAL(p,2,0x7);
1537 p += 4;
1538 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1540 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1542 /* Send the required number of replies */
1543 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 69, *ppdata, 0);
1545 return -1;
1548 /****************************************************************************
1549 Reply to a NT CANCEL request.
1550 ****************************************************************************/
1552 int reply_ntcancel(connection_struct *conn,
1553 char *inbuf,char *outbuf,int length,int bufsize)
1556 * Go through and cancel any pending change notifies.
1559 int mid = SVAL(inbuf,smb_mid);
1560 START_PROFILE(SMBntcancel);
1561 remove_pending_change_notify_requests_by_mid(mid);
1562 remove_pending_lock_requests_by_mid(mid);
1563 srv_cancel_sign_response(mid);
1565 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1567 END_PROFILE(SMBntcancel);
1568 return(-1);
1571 /****************************************************************************
1572 Copy a file.
1573 ****************************************************************************/
1575 static NTSTATUS copy_internals(connection_struct *conn, char *oldname, char *newname, uint32 attrs)
1577 BOOL bad_path_oldname = False;
1578 BOOL bad_path_newname = False;
1579 SMB_STRUCT_STAT sbuf1, sbuf2;
1580 pstring last_component_oldname;
1581 pstring last_component_newname;
1582 files_struct *fsp1,*fsp2;
1583 uint32 fattr;
1584 int info;
1585 SMB_OFF_T ret=-1;
1586 int close_ret;
1587 NTSTATUS status = NT_STATUS_OK;
1589 ZERO_STRUCT(sbuf1);
1590 ZERO_STRUCT(sbuf2);
1592 /* No wildcards. */
1593 if (ms_has_wild(newname) || ms_has_wild(oldname)) {
1594 return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1597 if (!CAN_WRITE(conn))
1598 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1600 unix_convert(oldname,conn,last_component_oldname,&bad_path_oldname,&sbuf1);
1601 if (bad_path_oldname) {
1602 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1605 /* Quick check for "." and ".." */
1606 if (last_component_oldname[0] == '.') {
1607 if (!last_component_oldname[1] || (last_component_oldname[1] == '.' && !last_component_oldname[2])) {
1608 return NT_STATUS_OBJECT_NAME_INVALID;
1612 /* Source must already exist. */
1613 if (!VALID_STAT(sbuf1)) {
1614 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1616 if (!check_name(oldname,conn)) {
1617 return NT_STATUS_ACCESS_DENIED;
1620 /* Ensure attributes match. */
1621 fattr = dos_mode(conn,oldname,&sbuf1);
1622 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1623 return NT_STATUS_NO_SUCH_FILE;
1626 unix_convert(newname,conn,last_component_newname,&bad_path_newname,&sbuf2);
1627 if (bad_path_newname) {
1628 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1631 /* Quick check for "." and ".." */
1632 if (last_component_newname[0] == '.') {
1633 if (!last_component_newname[1] || (last_component_newname[1] == '.' && !last_component_newname[2])) {
1634 return NT_STATUS_OBJECT_NAME_INVALID;
1638 /* Disallow if newname already exists. */
1639 if (VALID_STAT(sbuf2)) {
1640 return NT_STATUS_OBJECT_NAME_COLLISION;
1643 if (!check_name(newname,conn)) {
1644 return NT_STATUS_ACCESS_DENIED;
1647 /* No links from a directory. */
1648 if (S_ISDIR(sbuf1.st_mode)) {
1649 return NT_STATUS_FILE_IS_A_DIRECTORY;
1652 /* Ensure this is within the share. */
1653 if (!reduce_name(conn, oldname) != 0) {
1654 return NT_STATUS_ACCESS_DENIED;
1657 DEBUG(10,("copy_internals: doing file copy %s to %s\n", oldname, newname));
1659 fsp1 = open_file_ntcreate(conn,oldname,&sbuf1,
1660 FILE_READ_DATA, /* Read-only. */
1661 0, /* No sharing. */
1662 FILE_OPEN,
1663 0, /* No create options. */
1664 FILE_ATTRIBUTE_NORMAL,
1665 INTERNAL_OPEN_ONLY,
1666 &info);
1668 if (!fsp1) {
1669 status = get_saved_ntstatus();
1670 if (NT_STATUS_IS_OK(status)) {
1671 status = NT_STATUS_ACCESS_DENIED;
1673 set_saved_ntstatus(NT_STATUS_OK);
1674 return status;
1677 fsp2 = open_file_ntcreate(conn,newname,&sbuf2,
1678 FILE_WRITE_DATA, /* Read-only. */
1679 0, /* No sharing. */
1680 FILE_CREATE,
1681 0, /* No create options. */
1682 fattr,
1683 INTERNAL_OPEN_ONLY,
1684 &info);
1686 if (!fsp2) {
1687 status = get_saved_ntstatus();
1688 if (NT_STATUS_IS_OK(status)) {
1689 status = NT_STATUS_ACCESS_DENIED;
1691 set_saved_ntstatus(NT_STATUS_OK);
1692 close_file(fsp1,False);
1693 return status;
1696 if (sbuf1.st_size) {
1697 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1701 * As we are opening fsp1 read-only we only expect
1702 * an error on close on fsp2 if we are out of space.
1703 * Thus we don't look at the error return from the
1704 * close of fsp1.
1706 close_file(fsp1,False);
1708 /* Ensure the modtime is set correctly on the destination file. */
1709 fsp_set_pending_modtime(fsp2, sbuf1.st_mtime);
1711 close_ret = close_file(fsp2,False);
1713 /* Grrr. We have to do this as open_file_shared1 adds aARCH when it
1714 creates the file. This isn't the correct thing to do in the copy case. JRA */
1715 file_set_dosmode(conn, newname, fattr, &sbuf2, True);
1717 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1718 return NT_STATUS_DISK_FULL;
1721 if (close_ret != 0) {
1722 status = map_nt_error_from_unix(close_ret);
1723 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1724 nt_errstr(status), oldname, newname));
1726 return status;
1729 /****************************************************************************
1730 Reply to a NT rename request.
1731 ****************************************************************************/
1733 int reply_ntrename(connection_struct *conn,
1734 char *inbuf,char *outbuf,int length,int bufsize)
1736 int outsize = 0;
1737 pstring oldname;
1738 pstring newname;
1739 char *p;
1740 NTSTATUS status;
1741 uint32 attrs = SVAL(inbuf,smb_vwv0);
1742 uint16 rename_type = SVAL(inbuf,smb_vwv1);
1744 START_PROFILE(SMBntrename);
1746 p = smb_buf(inbuf) + 1;
1747 p += srvstr_get_path(inbuf, oldname, p, sizeof(oldname), 0, STR_TERMINATE, &status, True);
1748 if (!NT_STATUS_IS_OK(status)) {
1749 END_PROFILE(SMBntrename);
1750 return ERROR_NT(status);
1753 if( is_ntfs_stream_name(oldname)) {
1754 /* Can't rename a stream. */
1755 END_PROFILE(SMBntrename);
1756 return ERROR_NT(NT_STATUS_ACCESS_DENIED);
1759 if (ms_has_wild(oldname)) {
1760 END_PROFILE(SMBntrename);
1761 return ERROR_NT(NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1764 p++;
1765 p += srvstr_get_path(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status, False);
1766 if (!NT_STATUS_IS_OK(status)) {
1767 END_PROFILE(SMBntrename);
1768 return ERROR_NT(status);
1771 RESOLVE_DFSPATH(oldname, conn, inbuf, outbuf);
1772 RESOLVE_DFSPATH(newname, conn, inbuf, outbuf);
1774 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1776 switch(rename_type) {
1777 case RENAME_FLAG_RENAME:
1778 status = rename_internals(conn, oldname, newname, attrs, False);
1779 break;
1780 case RENAME_FLAG_HARD_LINK:
1781 status = hardlink_internals(conn, oldname, newname);
1782 break;
1783 case RENAME_FLAG_COPY:
1784 status = copy_internals(conn, oldname, newname, attrs);
1785 break;
1786 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1787 status = NT_STATUS_INVALID_PARAMETER;
1788 break;
1789 default:
1790 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1791 break;
1794 if (!NT_STATUS_IS_OK(status)) {
1795 END_PROFILE(SMBntrename);
1796 if (open_was_deferred(SVAL(inbuf,smb_mid))) {
1797 /* We have re-scheduled this call. */
1798 return -1;
1800 return ERROR_NT(status);
1804 * Win2k needs a changenotify request response before it will
1805 * update after a rename..
1807 process_pending_change_notify_queue((time_t)0);
1808 outsize = set_message(outbuf,0,0,True);
1810 END_PROFILE(SMBntrename);
1811 return(outsize);
1814 /****************************************************************************
1815 Reply to an unsolicited SMBNTtranss - just ignore it!
1816 ****************************************************************************/
1818 int reply_nttranss(connection_struct *conn,
1819 char *inbuf,char *outbuf,int length,int bufsize)
1821 START_PROFILE(SMBnttranss);
1822 DEBUG(4,("Ignoring nttranss of length %d\n",length));
1823 END_PROFILE(SMBnttranss);
1824 return(-1);
1827 /****************************************************************************
1828 Reply to a notify change - queue the request and
1829 don't allow a directory to be opened.
1830 ****************************************************************************/
1832 static int call_nt_transact_notify_change(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1833 char **ppsetup, uint32 setup_count,
1834 char **ppparams, uint32 parameter_count,
1835 char **ppdata, uint32 data_count, uint32 max_data_count)
1837 char *setup = *ppsetup;
1838 files_struct *fsp;
1839 uint32 flags;
1841 if(setup_count < 6) {
1842 return ERROR_DOS(ERRDOS,ERRbadfunc);
1845 fsp = file_fsp(setup,4);
1846 flags = IVAL(setup, 0);
1848 DEBUG(3,("call_nt_transact_notify_change\n"));
1850 if(!fsp) {
1851 return ERROR_DOS(ERRDOS,ERRbadfid);
1854 if((!fsp->is_directory) || (conn != fsp->conn)) {
1855 return ERROR_DOS(ERRDOS,ERRbadfid);
1858 if (!change_notify_set(inbuf, fsp, conn, flags)) {
1859 return(UNIXERROR(ERRDOS,ERRbadfid));
1862 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1863 name = %s\n", fsp->fsp_name ));
1865 return -1;
1868 /****************************************************************************
1869 Reply to an NT transact rename command.
1870 ****************************************************************************/
1872 static int call_nt_transact_rename(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1873 char **ppsetup, uint32 setup_count,
1874 char **ppparams, uint32 parameter_count,
1875 char **ppdata, uint32 data_count, uint32 max_data_count)
1877 char *params = *ppparams;
1878 pstring new_name;
1879 files_struct *fsp = NULL;
1880 BOOL replace_if_exists = False;
1881 NTSTATUS status;
1883 if(parameter_count < 4) {
1884 return ERROR_DOS(ERRDOS,ERRbadfunc);
1887 fsp = file_fsp(params, 0);
1888 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1889 CHECK_FSP(fsp, conn);
1890 srvstr_get_path(inbuf, new_name, params+4, sizeof(new_name), -1, STR_TERMINATE, &status, True);
1891 if (!NT_STATUS_IS_OK(status)) {
1892 return ERROR_NT(status);
1895 status = rename_internals(conn, fsp->fsp_name,
1896 new_name, 0, replace_if_exists);
1897 if (!NT_STATUS_IS_OK(status))
1898 return ERROR_NT(status);
1901 * Rename was successful.
1903 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
1905 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1906 fsp->fsp_name, new_name));
1909 * Win2k needs a changenotify request response before it will
1910 * update after a rename..
1913 process_pending_change_notify_queue((time_t)0);
1915 return -1;
1918 /******************************************************************************
1919 Fake up a completely empty SD.
1920 *******************************************************************************/
1922 static size_t get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1924 size_t sd_size;
1926 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1927 if(!*ppsd) {
1928 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1929 sd_size = 0;
1932 return sd_size;
1935 /****************************************************************************
1936 Reply to query a security descriptor.
1937 ****************************************************************************/
1939 static int call_nt_transact_query_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
1940 char **ppsetup, uint32 setup_count,
1941 char **ppparams, uint32 parameter_count,
1942 char **ppdata, uint32 data_count, uint32 max_data_count)
1944 char *params = *ppparams;
1945 char *data = *ppdata;
1946 prs_struct pd;
1947 SEC_DESC *psd = NULL;
1948 size_t sd_size;
1949 uint32 security_info_wanted;
1950 TALLOC_CTX *mem_ctx;
1951 files_struct *fsp = NULL;
1953 if(parameter_count < 8) {
1954 return ERROR_DOS(ERRDOS,ERRbadfunc);
1957 fsp = file_fsp(params,0);
1958 if(!fsp) {
1959 return ERROR_DOS(ERRDOS,ERRbadfid);
1962 security_info_wanted = IVAL(params,4);
1964 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
1965 (unsigned int)security_info_wanted ));
1967 params = nttrans_realloc(ppparams, 4);
1968 if(params == NULL) {
1969 return ERROR_DOS(ERRDOS,ERRnomem);
1972 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
1973 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
1974 return ERROR_DOS(ERRDOS,ERRnomem);
1978 * Get the permissions to return.
1981 if (!lp_nt_acl_support(SNUM(conn))) {
1982 sd_size = get_null_nt_acl(mem_ctx, &psd);
1983 } else {
1984 sd_size = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd, security_info_wanted, &psd);
1987 if (sd_size == 0) {
1988 talloc_destroy(mem_ctx);
1989 return(UNIXERROR(ERRDOS,ERRnoaccess));
1992 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1994 SIVAL(params,0,(uint32)sd_size);
1996 if(max_data_count < sd_size) {
1998 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1999 params, 4, *ppdata, 0);
2000 talloc_destroy(mem_ctx);
2001 return -1;
2005 * Allocate the data we will point this at.
2008 data = nttrans_realloc(ppdata, sd_size);
2009 if(data == NULL) {
2010 talloc_destroy(mem_ctx);
2011 return ERROR_DOS(ERRDOS,ERRnomem);
2015 * Init the parse struct we will marshall into.
2018 prs_init(&pd, 0, mem_ctx, MARSHALL);
2021 * Setup the prs_struct to point at the memory we just
2022 * allocated.
2025 prs_give_memory( &pd, data, (uint32)sd_size, False);
2028 * Finally, linearize into the outgoing buffer.
2031 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2032 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2033 security descriptor.\n"));
2035 * Return access denied for want of a better error message..
2037 talloc_destroy(mem_ctx);
2038 return(UNIXERROR(ERRDOS,ERRnoaccess));
2042 * Now we can delete the security descriptor.
2045 talloc_destroy(mem_ctx);
2047 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, 4, data, (int)sd_size);
2048 return -1;
2051 /****************************************************************************
2052 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2053 ****************************************************************************/
2055 static int call_nt_transact_set_security_desc(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2056 char **ppsetup, uint32 setup_count,
2057 char **ppparams, uint32 parameter_count,
2058 char **ppdata, uint32 data_count, uint32 max_data_count)
2060 char *params= *ppparams;
2061 char *data = *ppdata;
2062 files_struct *fsp = NULL;
2063 uint32 security_info_sent = 0;
2064 NTSTATUS nt_status;
2066 if(parameter_count < 8) {
2067 return ERROR_DOS(ERRDOS,ERRbadfunc);
2070 if((fsp = file_fsp(params,0)) == NULL) {
2071 return ERROR_DOS(ERRDOS,ERRbadfid);
2074 if(!lp_nt_acl_support(SNUM(conn))) {
2075 goto done;
2078 security_info_sent = IVAL(params,4);
2080 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2081 (unsigned int)security_info_sent ));
2083 if (data_count == 0) {
2084 return ERROR_DOS(ERRDOS, ERRnoaccess);
2087 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2088 return ERROR_NT(nt_status);
2091 done:
2093 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2094 return -1;
2097 /****************************************************************************
2098 Reply to NT IOCTL
2099 ****************************************************************************/
2101 static int call_nt_transact_ioctl(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2102 char **ppsetup, uint32 setup_count,
2103 char **ppparams, uint32 parameter_count,
2104 char **ppdata, uint32 data_count, uint32 max_data_count)
2106 uint32 function;
2107 uint16 fidnum;
2108 files_struct *fsp;
2109 uint8 isFSctl;
2110 uint8 compfilter;
2111 static BOOL logged_message;
2112 char *pdata = *ppdata;
2114 if (setup_count != 8) {
2115 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2116 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2119 function = IVAL(*ppsetup, 0);
2120 fidnum = SVAL(*ppsetup, 4);
2121 isFSctl = CVAL(*ppsetup, 6);
2122 compfilter = CVAL(*ppsetup, 7);
2124 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2125 function, fidnum, isFSctl, compfilter));
2127 fsp=file_fsp(*ppsetup, 4);
2128 /* this check is done in each implemented function case for now
2129 because I don't want to break anything... --metze
2130 FSP_BELONGS_CONN(fsp,conn);*/
2132 switch (function) {
2133 case FSCTL_SET_SPARSE:
2134 /* pretend this succeeded - tho strictly we should
2135 mark the file sparse (if the local fs supports it)
2136 so we can know if we need to pre-allocate or not */
2138 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2139 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2140 return -1;
2142 case FSCTL_0x000900C0:
2143 /* pretend this succeeded - don't know what this really is
2144 but works ok like this --metze
2147 DEBUG(10,("FSCTL_0x000900C0: called on FID[0x%04X](but not implemented)\n",fidnum));
2148 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2149 return -1;
2151 case FSCTL_GET_REPARSE_POINT:
2152 /* pretend this fail - my winXP does it like this
2153 * --metze
2156 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2157 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
2158 return -1;
2160 case FSCTL_SET_REPARSE_POINT:
2161 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2162 * --metze
2165 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2166 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_NOT_A_REPARSE_POINT, NULL, 0, NULL, 0);
2167 return -1;
2169 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2172 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2173 * and return their volume names. If max_data_count is 16, then it is just
2174 * asking for the number of volumes and length of the combined names.
2176 * pdata is the data allocated by our caller, but that uses
2177 * total_data_count (which is 0 in our case) rather than max_data_count.
2178 * Allocate the correct amount and return the pointer to let
2179 * it be deallocated when we return.
2181 SHADOW_COPY_DATA *shadow_data = NULL;
2182 TALLOC_CTX *shadow_mem_ctx = NULL;
2183 BOOL labels = False;
2184 uint32 labels_data_count = 0;
2185 uint32 i;
2186 char *cur_pdata;
2188 FSP_BELONGS_CONN(fsp,conn);
2190 if (max_data_count < 16) {
2191 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2192 max_data_count));
2193 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
2196 if (max_data_count > 16) {
2197 labels = True;
2200 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2201 if (shadow_mem_ctx == NULL) {
2202 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2203 return ERROR_NT(NT_STATUS_NO_MEMORY);
2206 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2207 if (shadow_data == NULL) {
2208 DEBUG(0,("talloc_zero() failed!\n"));
2209 talloc_destroy(shadow_mem_ctx);
2210 return ERROR_NT(NT_STATUS_NO_MEMORY);
2213 shadow_data->mem_ctx = shadow_mem_ctx;
2216 * Call the VFS routine to actually do the work.
2218 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2219 talloc_destroy(shadow_data->mem_ctx);
2220 if (errno == ENOSYS) {
2221 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2222 conn->connectpath));
2223 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2224 } else {
2225 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2226 conn->connectpath));
2227 return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
2231 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2233 if (!labels) {
2234 data_count = 16;
2235 } else {
2236 data_count = 12+labels_data_count+4;
2239 if (max_data_count<data_count) {
2240 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2241 max_data_count,data_count));
2242 talloc_destroy(shadow_data->mem_ctx);
2243 return ERROR_NT(NT_STATUS_BUFFER_TOO_SMALL);
2246 pdata = nttrans_realloc(ppdata, data_count);
2247 if (pdata == NULL) {
2248 talloc_destroy(shadow_data->mem_ctx);
2249 return ERROR_NT(NT_STATUS_NO_MEMORY);
2252 cur_pdata = pdata;
2254 /* num_volumes 4 bytes */
2255 SIVAL(pdata,0,shadow_data->num_volumes);
2257 if (labels) {
2258 /* num_labels 4 bytes */
2259 SIVAL(pdata,4,shadow_data->num_volumes);
2262 /* needed_data_count 4 bytes */
2263 SIVAL(pdata,8,labels_data_count);
2265 cur_pdata+=12;
2267 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2268 shadow_data->num_volumes,fsp->fsp_name));
2269 if (labels && shadow_data->labels) {
2270 for (i=0;i<shadow_data->num_volumes;i++) {
2271 srvstr_push(outbuf, cur_pdata, shadow_data->labels[i], 2*sizeof(SHADOW_COPY_LABEL), STR_UNICODE|STR_TERMINATE);
2272 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2273 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2277 talloc_destroy(shadow_data->mem_ctx);
2279 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, pdata, data_count);
2281 return -1;
2284 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2286 /* pretend this succeeded -
2288 * we have to send back a list with all files owned by this SID
2290 * but I have to check that --metze
2292 DOM_SID sid;
2293 uid_t uid;
2294 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2296 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2298 FSP_BELONGS_CONN(fsp,conn);
2300 /* unknown 4 bytes: this is not the length of the sid :-( */
2301 /*unknown = IVAL(pdata,0);*/
2303 sid_parse(pdata+4,sid_len,&sid);
2304 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2306 if (!NT_STATUS_IS_OK(sid_to_uid(&sid, &uid))) {
2307 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2308 sid_string_static(&sid),(unsigned long)sid_len));
2309 uid = (-1);
2312 /* we can take a look at the find source :-)
2314 * find ./ -uid $uid -name '*' is what we need here
2317 * and send 4bytes len and then NULL terminated unicode strings
2318 * for each file
2320 * but I don't know how to deal with the paged results
2321 * (maybe we can hang the result anywhere in the fsp struct)
2323 * we don't send all files at once
2324 * and at the next we should *not* start from the beginning,
2325 * so we have to cache the result
2327 * --metze
2330 /* this works for now... */
2331 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, NULL, 0, NULL, 0);
2332 return -1;
2334 default:
2335 if (!logged_message) {
2336 logged_message = True; /* Only print this once... */
2337 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2338 function));
2342 return ERROR_NT(NT_STATUS_NOT_SUPPORTED);
2346 #ifdef HAVE_SYS_QUOTAS
2347 /****************************************************************************
2348 Reply to get user quota
2349 ****************************************************************************/
2351 static int call_nt_transact_get_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2352 char **ppsetup, uint32 setup_count,
2353 char **ppparams, uint32 parameter_count,
2354 char **ppdata, uint32 data_count, uint32 max_data_count)
2356 NTSTATUS nt_status = NT_STATUS_OK;
2357 char *params = *ppparams;
2358 char *pdata = *ppdata;
2359 char *entry;
2360 int data_len=0,param_len=0;
2361 int qt_len=0;
2362 int entry_len = 0;
2363 files_struct *fsp = NULL;
2364 uint16 level = 0;
2365 size_t sid_len;
2366 DOM_SID sid;
2367 BOOL start_enum = True;
2368 SMB_NTQUOTA_STRUCT qt;
2369 SMB_NTQUOTA_LIST *tmp_list;
2370 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2371 extern struct current_user current_user;
2373 ZERO_STRUCT(qt);
2375 /* access check */
2376 if (current_user.uid != 0) {
2377 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2378 lp_servicename(SNUM(conn)),conn->user));
2379 return ERROR_DOS(ERRDOS,ERRnoaccess);
2383 * Ensure minimum number of parameters sent.
2386 if (parameter_count < 4) {
2387 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2388 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2391 /* maybe we can check the quota_fnum */
2392 fsp = file_fsp(params,0);
2393 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2394 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2395 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2398 /* the NULL pointer cheking for fsp->fake_file_handle->pd
2399 * is done by CHECK_NTQUOTA_HANDLE_OK()
2401 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2403 level = SVAL(params,2);
2405 /* unknown 12 bytes leading in params */
2407 switch (level) {
2408 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2409 /* seems that we should continue with the enum here --metze */
2411 if (qt_handle->quota_list!=NULL &&
2412 qt_handle->tmp_list==NULL) {
2414 /* free the list */
2415 free_ntquota_list(&(qt_handle->quota_list));
2417 /* Realloc the size of parameters and data we will return */
2418 param_len = 4;
2419 params = nttrans_realloc(ppparams, param_len);
2420 if(params == NULL) {
2421 return ERROR_DOS(ERRDOS,ERRnomem);
2424 data_len = 0;
2425 SIVAL(params,0,data_len);
2427 break;
2430 start_enum = False;
2432 case TRANSACT_GET_USER_QUOTA_LIST_START:
2434 if (qt_handle->quota_list==NULL &&
2435 qt_handle->tmp_list==NULL) {
2436 start_enum = True;
2439 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0)
2440 return ERROR_DOS(ERRSRV,ERRerror);
2442 /* Realloc the size of parameters and data we will return */
2443 param_len = 4;
2444 params = nttrans_realloc(ppparams, param_len);
2445 if(params == NULL) {
2446 return ERROR_DOS(ERRDOS,ERRnomem);
2449 /* we should not trust the value in max_data_count*/
2450 max_data_count = MIN(max_data_count,2048);
2452 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2453 if(pdata == NULL) {
2454 return ERROR_DOS(ERRDOS,ERRnomem);
2457 entry = pdata;
2459 /* set params Size of returned Quota Data 4 bytes*/
2460 /* but set it later when we know it */
2462 /* for each entry push the data */
2464 if (start_enum) {
2465 qt_handle->tmp_list = qt_handle->quota_list;
2468 tmp_list = qt_handle->tmp_list;
2470 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2471 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2473 sid_len = sid_size(&tmp_list->quotas->sid);
2474 entry_len = 40 + sid_len;
2476 /* nextoffset entry 4 bytes */
2477 SIVAL(entry,0,entry_len);
2479 /* then the len of the SID 4 bytes */
2480 SIVAL(entry,4,sid_len);
2482 /* unknown data 8 bytes SMB_BIG_UINT */
2483 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2485 /* the used disk space 8 bytes SMB_BIG_UINT */
2486 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2488 /* the soft quotas 8 bytes SMB_BIG_UINT */
2489 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2491 /* the hard quotas 8 bytes SMB_BIG_UINT */
2492 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2494 /* and now the SID */
2495 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2498 qt_handle->tmp_list = tmp_list;
2500 /* overwrite the offset of the last entry */
2501 SIVAL(entry-entry_len,0,0);
2503 data_len = 4+qt_len;
2504 /* overwrite the params quota_data_len */
2505 SIVAL(params,0,data_len);
2507 break;
2509 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2511 /* unknown 4 bytes IVAL(pdata,0) */
2513 if (data_count < 8) {
2514 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2515 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2518 sid_len = IVAL(pdata,4);
2519 /* Ensure this is less than 1mb. */
2520 if (sid_len > (1024*1024)) {
2521 return ERROR_DOS(ERRDOS,ERRnomem);
2524 if (data_count < 8+sid_len) {
2525 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2526 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2529 data_len = 4+40+sid_len;
2531 if (max_data_count < data_len) {
2532 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2533 max_data_count, data_len));
2534 param_len = 4;
2535 SIVAL(params,0,data_len);
2536 data_len = 0;
2537 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2538 break;
2541 sid_parse(pdata+8,sid_len,&sid);
2543 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2544 ZERO_STRUCT(qt);
2546 * we have to return zero's in all fields
2547 * instead of returning an error here
2548 * --metze
2552 /* Realloc the size of parameters and data we will return */
2553 param_len = 4;
2554 params = nttrans_realloc(ppparams, param_len);
2555 if(params == NULL) {
2556 return ERROR_DOS(ERRDOS,ERRnomem);
2559 pdata = nttrans_realloc(ppdata, data_len);
2560 if(pdata == NULL) {
2561 return ERROR_DOS(ERRDOS,ERRnomem);
2564 entry = pdata;
2566 /* set params Size of returned Quota Data 4 bytes*/
2567 SIVAL(params,0,data_len);
2569 /* nextoffset entry 4 bytes */
2570 SIVAL(entry,0,0);
2572 /* then the len of the SID 4 bytes */
2573 SIVAL(entry,4,sid_len);
2575 /* unknown data 8 bytes SMB_BIG_UINT */
2576 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2578 /* the used disk space 8 bytes SMB_BIG_UINT */
2579 SBIG_UINT(entry,16,qt.usedspace);
2581 /* the soft quotas 8 bytes SMB_BIG_UINT */
2582 SBIG_UINT(entry,24,qt.softlim);
2584 /* the hard quotas 8 bytes SMB_BIG_UINT */
2585 SBIG_UINT(entry,32,qt.hardlim);
2587 /* and now the SID */
2588 sid_linearize(entry+40, sid_len, &sid);
2590 break;
2592 default:
2593 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2594 return ERROR_DOS(ERRSRV,ERRerror);
2595 break;
2598 send_nt_replies(inbuf, outbuf, bufsize, nt_status, params, param_len, pdata, data_len);
2600 return -1;
2603 /****************************************************************************
2604 Reply to set user quota
2605 ****************************************************************************/
2607 static int call_nt_transact_set_user_quota(connection_struct *conn, char *inbuf, char *outbuf, int length, int bufsize,
2608 char **ppsetup, uint32 setup_count,
2609 char **ppparams, uint32 parameter_count,
2610 char **ppdata, uint32 data_count, uint32 max_data_count)
2612 char *params = *ppparams;
2613 char *pdata = *ppdata;
2614 int data_len=0,param_len=0;
2615 SMB_NTQUOTA_STRUCT qt;
2616 size_t sid_len;
2617 DOM_SID sid;
2618 files_struct *fsp = NULL;
2620 ZERO_STRUCT(qt);
2622 /* access check */
2623 if (current_user.uid != 0) {
2624 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2625 lp_servicename(SNUM(conn)),conn->user));
2626 return ERROR_DOS(ERRDOS,ERRnoaccess);
2630 * Ensure minimum number of parameters sent.
2633 if (parameter_count < 2) {
2634 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2635 return ERROR_DOS(ERRDOS,ERRinvalidparam);
2638 /* maybe we can check the quota_fnum */
2639 fsp = file_fsp(params,0);
2640 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2641 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2642 return ERROR_NT(NT_STATUS_INVALID_HANDLE);
2645 if (data_count < 40) {
2646 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2647 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2650 /* offset to next quota record.
2651 * 4 bytes IVAL(pdata,0)
2652 * unused here...
2655 /* sid len */
2656 sid_len = IVAL(pdata,4);
2658 if (data_count < 40+sid_len) {
2659 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2660 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2663 /* unknown 8 bytes in pdata
2664 * maybe its the change time in NTTIME
2667 /* the used space 8 bytes (SMB_BIG_UINT)*/
2668 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2669 #ifdef LARGE_SMB_OFF_T
2670 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2671 #else /* LARGE_SMB_OFF_T */
2672 if ((IVAL(pdata,20) != 0)&&
2673 ((qt.usedspace != 0xFFFFFFFF)||
2674 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2675 /* more than 32 bits? */
2676 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2678 #endif /* LARGE_SMB_OFF_T */
2680 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2681 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2682 #ifdef LARGE_SMB_OFF_T
2683 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2684 #else /* LARGE_SMB_OFF_T */
2685 if ((IVAL(pdata,28) != 0)&&
2686 ((qt.softlim != 0xFFFFFFFF)||
2687 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2688 /* more than 32 bits? */
2689 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2691 #endif /* LARGE_SMB_OFF_T */
2693 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2694 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2695 #ifdef LARGE_SMB_OFF_T
2696 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2697 #else /* LARGE_SMB_OFF_T */
2698 if ((IVAL(pdata,36) != 0)&&
2699 ((qt.hardlim != 0xFFFFFFFF)||
2700 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2701 /* more than 32 bits? */
2702 return ERROR_DOS(ERRDOS,ERRunknownlevel);
2704 #endif /* LARGE_SMB_OFF_T */
2706 sid_parse(pdata+40,sid_len,&sid);
2707 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2709 /* 44 unknown bytes left... */
2711 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2712 return ERROR_DOS(ERRSRV,ERRerror);
2715 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_OK, params, param_len, pdata, data_len);
2717 return -1;
2719 #endif /* HAVE_SYS_QUOTAS */
2721 /****************************************************************************
2722 Reply to a SMBNTtrans.
2723 ****************************************************************************/
2725 int reply_nttrans(connection_struct *conn,
2726 char *inbuf,char *outbuf,int length,int bufsize)
2728 int outsize = 0;
2729 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
2730 #if 0 /* Not used. */
2731 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
2732 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
2733 #endif /* Not used. */
2734 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
2735 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
2736 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
2737 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
2738 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
2739 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
2740 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
2741 uint16 function_code = SVAL( inbuf, smb_nt_Function);
2742 char *params = NULL, *data = NULL, *setup = NULL;
2743 uint32 num_params_sofar, num_data_sofar;
2744 START_PROFILE(SMBnttrans);
2746 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2747 END_PROFILE(SMBnttrans);
2748 return ERROR_DOS(ERRSRV,ERRaccess);
2751 outsize = set_message(outbuf,0,0,True);
2754 * All nttrans messages we handle have smb_wct == 19 + setup_count.
2755 * Ensure this is so as a sanity check.
2758 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
2759 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2760 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
2761 goto bad_param;
2764 /* Don't allow more than 128mb for each value. */
2765 if ((total_parameter_count > (1024*1024*128)) || (total_data_count > (1024*1024*128))) {
2766 END_PROFILE(SMBnttrans);
2767 return ERROR_DOS(ERRDOS,ERRnomem);
2770 /* Allocate the space for the setup, the maximum needed parameters and data */
2772 if(setup_count > 0) {
2773 setup = (char *)SMB_MALLOC(setup_count);
2775 if (total_parameter_count > 0) {
2776 params = (char *)SMB_MALLOC(total_parameter_count);
2778 if (total_data_count > 0) {
2779 data = (char *)SMB_MALLOC(total_data_count);
2782 if ((total_parameter_count && !params) || (total_data_count && !data) ||
2783 (setup_count && !setup)) {
2784 SAFE_FREE(setup);
2785 SAFE_FREE(params);
2786 SAFE_FREE(data);
2787 DEBUG(0,("reply_nttrans : Out of memory\n"));
2788 END_PROFILE(SMBnttrans);
2789 return ERROR_DOS(ERRDOS,ERRnomem);
2792 /* Copy the param and data bytes sent with this request into the params buffer */
2793 num_params_sofar = parameter_count;
2794 num_data_sofar = data_count;
2796 if (parameter_count > total_parameter_count || data_count > total_data_count)
2797 goto bad_param;
2799 if(setup) {
2800 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
2801 if ((smb_nt_SetupStart + setup_count < smb_nt_SetupStart) ||
2802 (smb_nt_SetupStart + setup_count < setup_count)) {
2803 goto bad_param;
2805 if (smb_nt_SetupStart + setup_count > length) {
2806 goto bad_param;
2809 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
2810 dump_data(10, setup, setup_count);
2812 if(params) {
2813 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
2814 if ((parameter_offset + parameter_count < parameter_offset) ||
2815 (parameter_offset + parameter_count < parameter_count)) {
2816 goto bad_param;
2818 if ((smb_base(inbuf) + parameter_offset + parameter_count > inbuf + length)||
2819 (smb_base(inbuf) + parameter_offset + parameter_count < smb_base(inbuf))) {
2820 goto bad_param;
2823 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
2824 dump_data(10, params, parameter_count);
2826 if(data) {
2827 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
2828 if ((data_offset + data_count < data_offset) || (data_offset + data_count < data_count)) {
2829 goto bad_param;
2831 if ((smb_base(inbuf) + data_offset + data_count > inbuf + length) ||
2832 (smb_base(inbuf) + data_offset + data_count < smb_base(inbuf))) {
2833 goto bad_param;
2836 memcpy( data, smb_base(inbuf) + data_offset, data_count);
2837 dump_data(10, data, data_count);
2840 srv_signing_trans_start(SVAL(inbuf,smb_mid));
2842 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2843 /* We need to send an interim response then receive the rest
2844 of the parameter/data bytes */
2845 outsize = set_message(outbuf,0,0,True);
2846 srv_signing_trans_stop();
2847 show_msg(outbuf);
2848 if (!send_smb(smbd_server_fd(),outbuf)) {
2849 exit_server("reply_nttrans: send_smb failed.");
2852 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2853 BOOL ret;
2854 uint32 parameter_displacement;
2855 uint32 data_displacement;
2857 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
2859 /* We need to re-calcuate the new length after we've read the secondary packet. */
2860 length = smb_len(inbuf) + 4;
2863 * The sequence number for the trans reply is always
2864 * based on the last secondary received.
2867 srv_signing_trans_start(SVAL(inbuf,smb_mid));
2869 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
2870 outsize = set_message(outbuf,0,0,True);
2871 if(ret) {
2872 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
2873 } else {
2874 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
2875 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
2877 goto bad_param;
2880 /* Revise total_params and total_data in case they have changed downwards */
2881 if (IVAL(inbuf, smb_nts_TotalParameterCount) < total_parameter_count) {
2882 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
2884 if (IVAL(inbuf, smb_nts_TotalDataCount) < total_data_count) {
2885 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
2888 parameter_count = IVAL(inbuf,smb_nts_ParameterCount);
2889 parameter_offset = IVAL(inbuf, smb_nts_ParameterOffset);
2890 parameter_displacement = IVAL(inbuf, smb_nts_ParameterDisplacement);
2891 num_params_sofar += parameter_count;
2893 data_count = IVAL(inbuf, smb_nts_DataCount);
2894 data_displacement = IVAL(inbuf, smb_nts_DataDisplacement);
2895 data_offset = IVAL(inbuf, smb_nts_DataOffset);
2896 num_data_sofar += data_count;
2898 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count) {
2899 DEBUG(0,("reply_nttrans2: data overflow in secondary nttrans packet"));
2900 goto bad_param;
2903 if (parameter_count) {
2904 if (parameter_displacement + parameter_count > total_parameter_count) {
2905 goto bad_param;
2907 if ((parameter_displacement + parameter_count < parameter_displacement) ||
2908 (parameter_displacement + parameter_count < parameter_count)) {
2909 goto bad_param;
2911 if (parameter_displacement > total_parameter_count) {
2912 goto bad_param;
2914 if ((smb_base(inbuf) + parameter_offset + parameter_count > inbuf + length) ||
2915 (smb_base(inbuf) + parameter_offset + parameter_count < smb_base(inbuf))) {
2916 goto bad_param;
2918 if (parameter_displacement + params < params) {
2919 goto bad_param;
2922 memcpy( &params[parameter_displacement], smb_base(inbuf) + parameter_offset, parameter_count);
2925 if (data_count) {
2926 if (data_displacement + data_count > total_data_count) {
2927 goto bad_param;
2929 if ((data_displacement + data_count < data_displacement) ||
2930 (data_displacement + data_count < data_count)) {
2931 goto bad_param;
2933 if (data_displacement > total_data_count) {
2934 goto bad_param;
2936 if ((smb_base(inbuf) + data_offset + data_count > inbuf + length) ||
2937 (smb_base(inbuf) + data_offset + data_count < smb_base(inbuf))) {
2938 goto bad_param;
2940 if (data_displacement + data < data) {
2941 goto bad_param;
2944 memcpy( &data[data_displacement], smb_base(inbuf)+ data_offset, data_count);
2949 if (Protocol >= PROTOCOL_NT1) {
2950 SSVAL(outbuf,smb_flg2,SVAL(outbuf,smb_flg2) | FLAGS2_IS_LONG_NAME);
2953 /* Now we must call the relevant NT_TRANS function */
2954 switch(function_code) {
2955 case NT_TRANSACT_CREATE:
2956 START_PROFILE_NESTED(NT_transact_create);
2957 outsize = call_nt_transact_create(conn, inbuf, outbuf,
2958 length, bufsize,
2959 &setup, setup_count,
2960 &params, total_parameter_count,
2961 &data, total_data_count, max_data_count);
2962 END_PROFILE_NESTED(NT_transact_create);
2963 break;
2964 case NT_TRANSACT_IOCTL:
2965 START_PROFILE_NESTED(NT_transact_ioctl);
2966 outsize = call_nt_transact_ioctl(conn, inbuf, outbuf,
2967 length, bufsize,
2968 &setup, setup_count,
2969 &params, total_parameter_count,
2970 &data, total_data_count, max_data_count);
2971 END_PROFILE_NESTED(NT_transact_ioctl);
2972 break;
2973 case NT_TRANSACT_SET_SECURITY_DESC:
2974 START_PROFILE_NESTED(NT_transact_set_security_desc);
2975 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
2976 length, bufsize,
2977 &setup, setup_count,
2978 &params, total_parameter_count,
2979 &data, total_data_count, max_data_count);
2980 END_PROFILE_NESTED(NT_transact_set_security_desc);
2981 break;
2982 case NT_TRANSACT_NOTIFY_CHANGE:
2983 START_PROFILE_NESTED(NT_transact_notify_change);
2984 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
2985 length, bufsize,
2986 &setup, setup_count,
2987 &params, total_parameter_count,
2988 &data, total_data_count, max_data_count);
2989 END_PROFILE_NESTED(NT_transact_notify_change);
2990 break;
2991 case NT_TRANSACT_RENAME:
2992 START_PROFILE_NESTED(NT_transact_rename);
2993 outsize = call_nt_transact_rename(conn, inbuf, outbuf,
2994 length, bufsize,
2995 &setup, setup_count,
2996 &params, total_parameter_count,
2997 &data, total_data_count, max_data_count);
2998 END_PROFILE_NESTED(NT_transact_rename);
2999 break;
3001 case NT_TRANSACT_QUERY_SECURITY_DESC:
3002 START_PROFILE_NESTED(NT_transact_query_security_desc);
3003 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
3004 length, bufsize,
3005 &setup, setup_count,
3006 &params, total_parameter_count,
3007 &data, total_data_count, max_data_count);
3008 END_PROFILE_NESTED(NT_transact_query_security_desc);
3009 break;
3010 #ifdef HAVE_SYS_QUOTAS
3011 case NT_TRANSACT_GET_USER_QUOTA:
3012 START_PROFILE_NESTED(NT_transact_get_user_quota);
3013 outsize = call_nt_transact_get_user_quota(conn, inbuf, outbuf,
3014 length, bufsize,
3015 &setup, setup_count,
3016 &params, total_parameter_count,
3017 &data, total_data_count, max_data_count);
3018 END_PROFILE_NESTED(NT_transact_get_user_quota);
3019 break;
3020 case NT_TRANSACT_SET_USER_QUOTA:
3021 START_PROFILE_NESTED(NT_transact_set_user_quota);
3022 outsize = call_nt_transact_set_user_quota(conn, inbuf, outbuf,
3023 length, bufsize,
3024 &setup, setup_count,
3025 &params, total_parameter_count,
3026 &data, total_data_count, max_data_count);
3027 END_PROFILE_NESTED(NT_transact_set_user_quota);
3028 break;
3029 #endif /* HAVE_SYS_QUOTAS */
3030 default:
3031 /* Error in request */
3032 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
3033 SAFE_FREE(setup);
3034 SAFE_FREE(params);
3035 SAFE_FREE(data);
3036 END_PROFILE(SMBnttrans);
3037 srv_signing_trans_stop();
3038 return ERROR_DOS(ERRSRV,ERRerror);
3041 /* As we do not know how many data packets will need to be
3042 returned here the various call_nt_transact_xxxx calls
3043 must send their own. Thus a call_nt_transact_xxxx routine only
3044 returns a value other than -1 when it wants to send
3045 an error packet.
3048 srv_signing_trans_stop();
3050 SAFE_FREE(setup);
3051 SAFE_FREE(params);
3052 SAFE_FREE(data);
3053 END_PROFILE(SMBnttrans);
3054 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
3055 calls have already sent it. If outsize != -1 then it is
3056 returning an error packet. */
3058 bad_param:
3060 srv_signing_trans_stop();
3061 SAFE_FREE(params);
3062 SAFE_FREE(data);
3063 SAFE_FREE(setup);
3064 END_PROFILE(SMBnttrans);
3065 return ERROR_NT(NT_STATUS_INVALID_PARAMETER);