Fix an uninitialized variable
[Samba.git] / source / smbd / nttrans.c
blob15306ee590091a63e815779eca6509dd5aa544bb
1 /*
2 Unix SMB/CIFS implementation.
3 SMB NT transaction handling
4 Copyright (C) Jeremy Allison 1994-2007
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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
23 extern int max_send;
24 extern enum protocol_types Protocol;
25 extern struct current_user current_user;
27 static const char *known_nt_pipes[] = {
28 "\\LANMAN",
29 "\\srvsvc",
30 "\\samr",
31 "\\wkssvc",
32 "\\NETLOGON",
33 "\\ntlsa",
34 "\\ntsvcs",
35 "\\lsass",
36 "\\lsarpc",
37 "\\winreg",
38 "\\initshutdown",
39 "\\spoolss",
40 "\\netdfs",
41 "\\rpcecho",
42 "\\svcctl",
43 "\\eventlog",
44 "\\unixinfo",
45 NULL
48 static char *nttrans_realloc(char **ptr, size_t size)
50 if (ptr==NULL) {
51 smb_panic("nttrans_realloc() called with NULL ptr");
54 *ptr = (char *)SMB_REALLOC(*ptr, size);
55 if(*ptr == NULL) {
56 return NULL;
58 memset(*ptr,'\0',size);
59 return *ptr;
62 /****************************************************************************
63 Send the required number of replies back.
64 We assume all fields other than the data fields are
65 set correctly for the type of call.
66 HACK ! Always assumes smb_setup field is zero.
67 ****************************************************************************/
69 void send_nt_replies(struct smb_request *req, NTSTATUS nt_error,
70 char *params, int paramsize,
71 char *pdata, int datasize)
73 int data_to_send = datasize;
74 int params_to_send = paramsize;
75 int useable_space;
76 char *pp = params;
77 char *pd = pdata;
78 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
79 int alignment_offset = 3;
80 int data_alignment_offset = 0;
83 * If there genuinely are no parameters or data to send just send
84 * the empty packet.
87 if(params_to_send == 0 && data_to_send == 0) {
88 reply_outbuf(req, 18, 0);
89 show_msg((char *)req->outbuf);
90 return;
94 * When sending params and data ensure that both are nicely aligned.
95 * Only do this alignment when there is also data to send - else
96 * can cause NT redirector problems.
99 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
100 data_alignment_offset = 4 - (params_to_send % 4);
104 * Space is bufsize minus Netbios over TCP header minus SMB header.
105 * The alignment_offset is to align the param bytes on a four byte
106 * boundary (2 bytes for data len, one byte pad).
107 * NT needs this to work correctly.
110 useable_space = max_send - (smb_size
111 + 2 * 18 /* wct */
112 + alignment_offset
113 + data_alignment_offset);
116 * useable_space can never be more than max_send minus the
117 * alignment offset.
120 useable_space = MIN(useable_space,
121 max_send - (alignment_offset+data_alignment_offset));
124 while (params_to_send || data_to_send) {
127 * Calculate whether we will totally or partially fill this packet.
130 total_sent_thistime = params_to_send + data_to_send +
131 alignment_offset + data_alignment_offset;
134 * We can never send more than useable_space.
137 total_sent_thistime = MIN(total_sent_thistime, useable_space);
139 reply_outbuf(req, 18, total_sent_thistime);
142 * Set total params and data to be sent.
145 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
146 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
149 * Calculate how many parameters and data we can fit into
150 * this packet. Parameters get precedence.
153 params_sent_thistime = MIN(params_to_send,useable_space);
154 data_sent_thistime = useable_space - params_sent_thistime;
155 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
157 SIVAL(req->outbuf, smb_ntr_ParameterCount,
158 params_sent_thistime);
160 if(params_sent_thistime == 0) {
161 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
162 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
163 } else {
165 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
166 * parameter bytes, however the first 4 bytes of outbuf are
167 * the Netbios over TCP header. Thus use smb_base() to subtract
168 * them from the calculation.
171 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
172 ((smb_buf(req->outbuf)+alignment_offset)
173 - smb_base(req->outbuf)));
175 * Absolute displacement of param bytes sent in this packet.
178 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
179 pp - params);
183 * Deal with the data portion.
186 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
188 if(data_sent_thistime == 0) {
189 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
190 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
191 } else {
193 * The offset of the data bytes is the offset of the
194 * parameter bytes plus the number of parameters being sent this time.
197 SIVAL(req->outbuf, smb_ntr_DataOffset,
198 ((smb_buf(req->outbuf)+alignment_offset) -
199 smb_base(req->outbuf))
200 + params_sent_thistime + data_alignment_offset);
201 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
205 * Copy the param bytes into the packet.
208 if(params_sent_thistime) {
209 if (alignment_offset != 0) {
210 memset(smb_buf(req->outbuf), 0,
211 alignment_offset);
213 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
214 params_sent_thistime);
218 * Copy in the data bytes
221 if(data_sent_thistime) {
222 if (data_alignment_offset != 0) {
223 memset((smb_buf(req->outbuf)+alignment_offset+
224 params_sent_thistime), 0,
225 data_alignment_offset);
227 memcpy(smb_buf(req->outbuf)+alignment_offset
228 +params_sent_thistime+data_alignment_offset,
229 pd,data_sent_thistime);
232 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
233 params_sent_thistime, data_sent_thistime, useable_space));
234 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
235 params_to_send, data_to_send, paramsize, datasize));
237 if (NT_STATUS_V(nt_error)) {
238 error_packet_set((char *)req->outbuf,
239 0, 0, nt_error,
240 __LINE__,__FILE__);
243 /* Send the packet */
244 show_msg((char *)req->outbuf);
245 if (!send_smb(smbd_server_fd(),(char *)req->outbuf)) {
246 exit_server_cleanly("send_nt_replies: send_smb failed.");
249 TALLOC_FREE(req->outbuf);
251 pp += params_sent_thistime;
252 pd += data_sent_thistime;
254 params_to_send -= params_sent_thistime;
255 data_to_send -= data_sent_thistime;
258 * Sanity check
261 if(params_to_send < 0 || data_to_send < 0) {
262 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
263 params_to_send, data_to_send));
264 return;
269 /****************************************************************************
270 Is it an NTFS stream name ?
271 ****************************************************************************/
273 bool is_ntfs_stream_name(const char *fname)
275 if (lp_posix_pathnames()) {
276 return False;
278 return (strchr_m(fname, ':') != NULL) ? True : False;
281 struct case_semantics_state {
282 connection_struct *conn;
283 bool case_sensitive;
284 bool case_preserve;
285 bool short_case_preserve;
288 /****************************************************************************
289 Restore case semantics.
290 ****************************************************************************/
291 static int restore_case_semantics(struct case_semantics_state *state)
293 state->conn->case_sensitive = state->case_sensitive;
294 state->conn->case_preserve = state->case_preserve;
295 state->conn->short_case_preserve = state->short_case_preserve;
296 return 0;
299 /****************************************************************************
300 Save case semantics.
301 ****************************************************************************/
302 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
303 connection_struct *conn)
305 struct case_semantics_state *result;
307 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
308 DEBUG(0, ("talloc failed\n"));
309 return NULL;
312 result->conn = conn;
313 result->case_sensitive = conn->case_sensitive;
314 result->case_preserve = conn->case_preserve;
315 result->short_case_preserve = conn->short_case_preserve;
317 /* Set to POSIX. */
318 conn->case_sensitive = True;
319 conn->case_preserve = True;
320 conn->short_case_preserve = True;
322 talloc_set_destructor(result, restore_case_semantics);
324 return result;
327 /****************************************************************************
328 Reply to an NT create and X call on a pipe
329 ****************************************************************************/
331 static void nt_open_pipe(char *fname, connection_struct *conn,
332 struct smb_request *req, int *ppnum)
334 smb_np_struct *p = NULL;
335 int i;
337 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
339 /* See if it is one we want to handle. */
341 if (lp_disable_spoolss() && strequal(fname, "\\spoolss")) {
342 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
343 ERRDOS, ERRbadpipe);
344 return;
347 for( i = 0; known_nt_pipes[i]; i++ ) {
348 if( strequal(fname,known_nt_pipes[i])) {
349 break;
353 if ( known_nt_pipes[i] == NULL ) {
354 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
355 ERRDOS, ERRbadpipe);
356 return;
359 /* Strip \\ off the name. */
360 fname++;
362 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
364 p = open_rpc_pipe_p(fname, conn, req->vuid);
365 if (!p) {
366 reply_doserror(req, ERRSRV, ERRnofids);
367 return;
370 /* TODO: Add pipe to db */
372 if ( !store_pipe_opendb( p ) ) {
373 DEBUG(3,("nt_open_pipe: failed to store %s pipe open.\n", fname));
376 *ppnum = p->pnum;
377 return;
380 /****************************************************************************
381 Reply to an NT create and X call for pipes.
382 ****************************************************************************/
384 static void do_ntcreate_pipe_open(connection_struct *conn,
385 struct smb_request *req)
387 char *fname = NULL;
388 int pnum = -1;
389 char *p = NULL;
390 uint32 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
391 TALLOC_CTX *ctx = talloc_tos();
393 srvstr_pull_buf_talloc(ctx, (char *)req->inbuf, req->flags2, &fname,
394 smb_buf(req->inbuf), STR_TERMINATE);
396 if (!fname) {
397 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
398 ERRDOS, ERRbadpipe);
399 return;
401 nt_open_pipe(fname, conn, req, &pnum);
403 if (req->outbuf) {
404 /* error reply */
405 return;
409 * Deal with pipe return.
412 if (flags & EXTENDED_RESPONSE_REQUIRED) {
413 /* This is very strange. We
414 * return 50 words, but only set
415 * the wcnt to 42 ? It's definately
416 * what happens on the wire....
418 reply_outbuf(req, 50, 0);
419 SCVAL(req->outbuf,smb_wct,42);
420 } else {
421 reply_outbuf(req, 34, 0);
424 p = (char *)req->outbuf + smb_vwv2;
425 p++;
426 SSVAL(p,0,pnum);
427 p += 2;
428 SIVAL(p,0,FILE_WAS_OPENED);
429 p += 4;
430 p += 32;
431 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
432 p += 20;
433 /* File type. */
434 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
435 /* Device state. */
436 SSVAL(p,2, 0x5FF); /* ? */
437 p += 4;
439 if (flags & EXTENDED_RESPONSE_REQUIRED) {
440 p += 25;
441 SIVAL(p,0,FILE_GENERIC_ALL);
443 * For pipes W2K3 seems to return
444 * 0x12019B next.
445 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
447 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
450 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
452 chain_reply(req);
455 static NTSTATUS create_file(connection_struct *conn,
456 struct smb_request *req,
457 uint16_t root_dir_fid,
458 char *fname,
459 uint32_t flags,
460 uint32_t access_mask,
461 uint32_t file_attributes,
462 uint32_t share_access,
463 uint32_t create_disposition,
464 uint32_t create_options,
465 int oplock_request,
466 SMB_BIG_UINT allocation_size,
467 struct security_descriptor *sd,
468 struct ea_list *ea_list,
470 files_struct **result,
471 int *pinfo,
472 uint8_t *poplock_granted,
473 SMB_STRUCT_STAT *psbuf)
475 TALLOC_CTX *frame = talloc_stackframe();
476 struct case_semantics_state *case_state = NULL;
477 SMB_STRUCT_STAT sbuf;
478 int info = FILE_WAS_OPENED;
479 files_struct *fsp = NULL;
480 uint8_t oplock_granted = NO_OPLOCK_RETURN;
481 NTSTATUS status;
483 DEBUG(10,("create_file: flags = 0x%x, access_mask = 0x%x "
484 "file_attributes = 0x%x, share_access = 0x%x, "
485 "create_disposition = 0x%x create_options = 0x%x "
486 "root_dir_fid = 0x%x, ea_list = 0x%x, sd = 0x%x, "
487 "fname = %s\n",
488 (unsigned int)flags,
489 (unsigned int)access_mask,
490 (unsigned int)file_attributes,
491 (unsigned int)share_access,
492 (unsigned int)create_disposition,
493 (unsigned int)create_options,
494 (unsigned int)root_dir_fid,
495 (unsigned int)ea_list,
496 (unsigned int)sd,
497 fname));
499 SET_STAT_INVALID(sbuf);
501 if (create_options & FILE_OPEN_BY_FILE_ID) {
502 status = NT_STATUS_NOT_SUPPORTED;
503 goto fail;
507 * Get the file name.
510 if (root_dir_fid != 0) {
512 * This filename is relative to a directory fid.
514 char *parent_fname = NULL;
515 files_struct *dir_fsp = file_fsp(root_dir_fid);
517 if (dir_fsp == NULL) {
518 status = NT_STATUS_INVALID_HANDLE;
519 goto fail;
522 if (!dir_fsp->is_directory) {
525 * Check to see if this is a mac fork of some kind.
528 if (is_ntfs_stream_name(fname)) {
529 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
530 goto fail;
534 we need to handle the case when we get a
535 relative open relative to a file and the
536 pathname is blank - this is a reopen!
537 (hint from demyn plantenberg)
540 status = NT_STATUS_INVALID_HANDLE;
541 goto fail;
544 if (ISDOT(dir_fsp->fsp_name)) {
546 * We're at the toplevel dir, the final file name
547 * must not contain ./, as this is filtered out
548 * normally by srvstr_get_path and unix_convert
549 * explicitly rejects paths containing ./.
551 parent_fname = talloc_strdup(talloc_tos(), "");
552 if (parent_fname == NULL) {
553 status = NT_STATUS_NO_MEMORY;
554 goto fail;
556 } else {
557 size_t dir_name_len = strlen(dir_fsp->fsp_name);
560 * Copy in the base directory name.
563 parent_fname = TALLOC_ARRAY(talloc_tos(), char,
564 dir_name_len+2);
565 if (parent_fname == NULL) {
566 status = NT_STATUS_NO_MEMORY;
567 goto fail;
569 memcpy(parent_fname, dir_fsp->fsp_name,
570 dir_name_len+1);
573 * Ensure it ends in a '/'.
574 * We used TALLOC_SIZE +2 to add space for the '/'.
577 if(dir_name_len
578 && (parent_fname[dir_name_len-1] != '\\')
579 && (parent_fname[dir_name_len-1] != '/')) {
580 parent_fname[dir_name_len] = '/';
581 parent_fname[dir_name_len+1] = '\0';
585 fname = talloc_asprintf(talloc_tos(), "%s%s", parent_fname,
586 fname);
587 if (fname == NULL) {
588 status = NT_STATUS_NO_MEMORY;
589 goto fail;
591 } else {
593 * Check to see if this is a mac fork of some kind.
596 if (is_ntfs_stream_name(fname)) {
597 enum FAKE_FILE_TYPE fake_file_type;
599 fake_file_type = is_fake_file(fname);
601 if (fake_file_type == FAKE_FILE_TYPE_NONE) {
602 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
606 * Here we go! support for changing the disk quotas
607 * --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 xp
613 * also tries a QUERY_FILE_INFO on the file and then
614 * close it
616 status = open_fake_file(conn, fake_file_type, fname,
617 access_mask, &fsp);
618 if (!NT_STATUS_IS_OK(status)) {
619 goto fail;
622 goto done;
626 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
627 if (oplock_request) {
628 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
629 ? BATCH_OPLOCK : 0;
632 status = resolve_dfspath(
633 talloc_tos(), conn, req->flags2 & FLAGS2_DFS_PATHNAMES,
634 fname, &fname);
636 if (!NT_STATUS_IS_OK(status)) {
638 * For PATH_NOT_COVERED we had
639 * reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
640 * ERRSRV, ERRbadpath);
641 * Need to fix in callers
643 goto fail;
647 * Ordinary file or directory.
651 * Check if POSIX semantics are wanted.
654 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
655 case_state = set_posix_case_semantics(talloc_tos(), conn);
656 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
659 status = unix_convert(talloc_tos(), conn, fname, False, &fname, NULL,
660 &sbuf);
661 if (!NT_STATUS_IS_OK(status)) {
662 goto fail;
665 /* All file access must go through check_name() */
667 status = check_name(conn, fname);
668 if (!NT_STATUS_IS_OK(status)) {
669 goto fail;
672 /* This is the correct thing to do (check every time) but can_delete
673 * is expensive (it may have to read the parent directory
674 * permissions). So for now we're not doing it unless we have a strong
675 * hint the client is really going to delete this file. If the client
676 * is forcing FILE_CREATE let the filesystem take care of the
677 * permissions. */
679 /* Setting FILE_SHARE_DELETE is the hint. */
681 if (lp_acl_check_permissions(SNUM(conn))
682 && (create_disposition != FILE_CREATE)
683 && (share_access & FILE_SHARE_DELETE)
684 && (access_mask & DELETE_ACCESS)
685 && (((dos_mode(conn, fname, &sbuf) & FILE_ATTRIBUTE_READONLY)
686 && !lp_delete_readonly(SNUM(conn)))
687 || !can_delete_file_in_directory(conn, fname))) {
688 status = NT_STATUS_ACCESS_DENIED;
689 goto fail;
692 #if 0
693 /* We need to support SeSecurityPrivilege for this. */
694 if ((access_mask & SEC_RIGHT_SYSTEM_SECURITY) &&
695 !user_has_privileges(current_user.nt_user_token,
696 &se_security)) {
697 status = NT_STATUS_PRIVILEGE_NOT_HELD;
698 goto fail;
700 #endif
703 * If it's a request for a directory open, deal with it separately.
706 if (create_options & FILE_DIRECTORY_FILE) {
708 /* Can't open a temp directory. IFS kit test. */
709 if (file_attributes & FILE_ATTRIBUTE_TEMPORARY) {
710 status = NT_STATUS_INVALID_PARAMETER;
711 goto fail;
715 * We will get a create directory here if the Win32
716 * app specified a security descriptor in the
717 * CreateDirectory() call.
720 oplock_request = 0;
721 status = open_directory(
722 conn, req, fname, &sbuf, access_mask, share_access,
723 create_disposition, create_options, file_attributes,
724 &info, &fsp);
725 } else {
728 * Ordinary file case.
731 status = open_file_ntcreate(
732 conn, req, fname, &sbuf, access_mask, share_access,
733 create_disposition, create_options, file_attributes,
734 oplock_request, &info, &fsp);
736 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) {
739 * Fail the open if it was explicitly a non-directory
740 * file.
743 if (create_options & FILE_NON_DIRECTORY_FILE) {
744 status = NT_STATUS_FILE_IS_A_DIRECTORY;
745 goto fail;
748 oplock_request = 0;
749 status = open_directory(
750 conn, req, fname, &sbuf, access_mask,
751 share_access, create_disposition,
752 create_options, file_attributes,
753 &info, &fsp);
757 TALLOC_FREE(case_state);
759 if (!NT_STATUS_IS_OK(status)) {
760 goto fail;
764 * According to the MS documentation, the only time the security
765 * descriptor is applied to the opened file is iff we *created* the
766 * file; an existing file stays the same.
768 * Also, it seems (from observation) that you can open the file with
769 * any access mask but you can still write the sd. We need to override
770 * the granted access before we call set_sd
771 * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>.
774 if ((sd != NULL) && (info == FILE_WAS_CREATED)
775 && lp_nt_acl_support(SNUM(conn))) {
777 uint32_t sec_info_sent = ALL_SECURITY_INFORMATION;
778 uint32_t saved_access_mask = fsp->access_mask;
780 if (sd->owner_sid==0) {
781 sec_info_sent &= ~OWNER_SECURITY_INFORMATION;
783 if (sd->group_sid==0) {
784 sec_info_sent &= ~GROUP_SECURITY_INFORMATION;
786 if (sd->sacl==0) {
787 sec_info_sent &= ~SACL_SECURITY_INFORMATION;
789 if (sd->dacl==0) {
790 sec_info_sent &= ~DACL_SECURITY_INFORMATION;
793 fsp->access_mask = FILE_GENERIC_ALL;
795 status = SMB_VFS_FSET_NT_ACL(
796 fsp, fsp->fh->fd, sec_info_sent, sd);
798 fsp->access_mask = saved_access_mask;
800 if (!NT_STATUS_IS_OK(status)) {
801 goto fail;
805 if ((ea_list != NULL) && (info == FILE_WAS_CREATED)) {
806 status = set_ea(conn, fsp, fname, ea_list);
807 if (!NT_STATUS_IS_OK(status)) {
808 goto fail;
812 if (!fsp->is_directory && S_ISDIR(sbuf.st_mode)) {
813 status = NT_STATUS_ACCESS_DENIED;
814 goto fail;
817 /* Save the requested allocation size. */
818 if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
819 if (allocation_size
820 && (allocation_size > sbuf.st_size)) {
821 fsp->initial_allocation_size = smb_roundup(
822 fsp->conn, allocation_size);
823 if (fsp->is_directory) {
824 /* Can't set allocation size on a directory. */
825 status = NT_STATUS_ACCESS_DENIED;
826 goto fail;
828 if (vfs_allocate_file_space(
829 fsp, fsp->initial_allocation_size) == -1) {
830 status = NT_STATUS_DISK_FULL;
831 goto fail;
833 } else {
834 fsp->initial_allocation_size = smb_roundup(
835 fsp->conn, (SMB_BIG_UINT)sbuf.st_size);
840 * If the caller set the extended oplock request bit
841 * and we granted one (by whatever means) - set the
842 * correct bit for extended oplock reply.
845 if (oplock_request &&
846 (lp_fake_oplocks(SNUM(conn))
847 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
850 * Exclusive oplock granted
853 if (flags & REQUEST_BATCH_OPLOCK) {
854 oplock_granted = BATCH_OPLOCK_RETURN;
855 } else {
856 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
858 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
859 oplock_granted = LEVEL_II_OPLOCK_RETURN;
860 } else {
861 oplock_granted = NO_OPLOCK_RETURN;
864 done:
865 DEBUG(10, ("create_file: info=%d, oplock_granted=%d\n",
866 info, (int)oplock_granted));
868 *result = fsp;
869 *pinfo = info;
870 *poplock_granted = oplock_granted;
871 *psbuf = sbuf;
872 TALLOC_FREE(frame);
873 return NT_STATUS_OK;
875 fail:
876 DEBUG(10, ("create_file: %s\n", nt_errstr(status)));
878 if (fsp != NULL) {
879 close_file(fsp, ERROR_CLOSE);
880 fsp = NULL;
882 TALLOC_FREE(frame);
883 return status;
886 /****************************************************************************
887 Reply to an NT create and X call.
888 ****************************************************************************/
890 void reply_ntcreate_and_X(connection_struct *conn, struct smb_request *req)
892 char *fname = NULL;
893 uint32 flags;
894 uint32 access_mask;
895 uint32 file_attributes;
896 uint32 share_access;
897 uint32 create_disposition;
898 uint32 create_options;
899 uint16 root_dir_fid;
900 SMB_BIG_UINT allocation_size;
901 /* Breakout the oplock request bits so we can set the
902 reply bits separately. */
903 int oplock_request = 0;
904 uint32 fattr=0;
905 SMB_OFF_T file_len = 0;
906 SMB_STRUCT_STAT sbuf;
907 int info = 0;
908 files_struct *fsp = NULL;
909 char *p = NULL;
910 struct timespec c_timespec;
911 struct timespec a_timespec;
912 struct timespec m_timespec;
913 NTSTATUS status;
914 uint8_t oplock_granted = NO_OPLOCK_RETURN;
915 TALLOC_CTX *ctx = talloc_tos();
917 START_PROFILE(SMBntcreateX);
919 if (req->wct < 24) {
920 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
921 return;
924 flags = IVAL(req->inbuf,smb_ntcreate_Flags);
925 access_mask = IVAL(req->inbuf,smb_ntcreate_DesiredAccess);
926 file_attributes = IVAL(req->inbuf,smb_ntcreate_FileAttributes);
927 share_access = IVAL(req->inbuf,smb_ntcreate_ShareAccess);
928 create_disposition = IVAL(req->inbuf,smb_ntcreate_CreateDisposition);
929 create_options = IVAL(req->inbuf,smb_ntcreate_CreateOptions);
930 root_dir_fid = (uint16)IVAL(req->inbuf,smb_ntcreate_RootDirectoryFid);
932 allocation_size = (SMB_BIG_UINT)IVAL(req->inbuf,
933 smb_ntcreate_AllocationSize);
934 #ifdef LARGE_SMB_OFF_T
935 allocation_size |= (((SMB_BIG_UINT)IVAL(
936 req->inbuf,
937 smb_ntcreate_AllocationSize + 4)) << 32);
938 #endif
940 srvstr_get_path(ctx, (char *)req->inbuf, req->flags2, &fname,
941 smb_buf(req->inbuf), 0, STR_TERMINATE, &status);
943 if (!NT_STATUS_IS_OK(status)) {
944 reply_nterror(req, status);
945 END_PROFILE(SMBntcreateX);
946 return;
949 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
950 "file_attributes = 0x%x, share_access = 0x%x, "
951 "create_disposition = 0x%x create_options = 0x%x "
952 "root_dir_fid = 0x%x, fname = %s\n",
953 (unsigned int)flags,
954 (unsigned int)access_mask,
955 (unsigned int)file_attributes,
956 (unsigned int)share_access,
957 (unsigned int)create_disposition,
958 (unsigned int)create_options,
959 (unsigned int)root_dir_fid,
960 fname));
963 * If it's an IPC, use the pipe handler.
966 if (IS_IPC(conn)) {
967 if (lp_nt_pipe_support()) {
968 do_ntcreate_pipe_open(conn, req);
969 END_PROFILE(SMBntcreateX);
970 return;
971 } else {
972 reply_doserror(req, ERRDOS, ERRnoaccess);
973 END_PROFILE(SMBntcreateX);
974 return;
978 status = create_file(conn, req, root_dir_fid, fname, flags,
979 access_mask, file_attributes, share_access,
980 create_disposition, create_options,
981 oplock_request, allocation_size, NULL, NULL,
982 &fsp, &info, &oplock_granted, &sbuf);
984 if (!NT_STATUS_IS_OK(status)) {
985 if (open_was_deferred(req->mid)) {
986 /* We have re-scheduled this call, no error. */
987 END_PROFILE(SMBntcreateX);
988 return;
990 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
991 reply_botherror(req, status, ERRDOS, ERRfilexists);
993 else {
994 reply_nterror(req, status);
996 END_PROFILE(SMBntcreateX);
997 return;
1000 file_len = sbuf.st_size;
1001 fattr = dos_mode(conn,fname,&sbuf);
1002 if (fattr == 0) {
1003 fattr = FILE_ATTRIBUTE_NORMAL;
1006 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1007 /* This is very strange. We
1008 * return 50 words, but only set
1009 * the wcnt to 42 ? It's definately
1010 * what happens on the wire....
1012 reply_outbuf(req, 50, 0);
1013 SCVAL(req->outbuf,smb_wct,42);
1014 } else {
1015 reply_outbuf(req, 34, 0);
1018 p = (char *)req->outbuf + smb_vwv2;
1020 SCVAL(p, 0, oplock_granted);
1022 p++;
1023 SSVAL(p,0,fsp->fnum);
1024 p += 2;
1025 if ((create_disposition == FILE_SUPERSEDE)
1026 && (info == FILE_WAS_OVERWRITTEN)) {
1027 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1028 } else {
1029 SIVAL(p,0,info);
1031 p += 4;
1033 /* Create time. */
1034 c_timespec = get_create_timespec(
1035 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1036 a_timespec = get_atimespec(&sbuf);
1037 m_timespec = get_mtimespec(&sbuf);
1039 if (lp_dos_filetime_resolution(SNUM(conn))) {
1040 dos_filetime_timespec(&c_timespec);
1041 dos_filetime_timespec(&a_timespec);
1042 dos_filetime_timespec(&m_timespec);
1045 put_long_date_timespec(p, c_timespec); /* create time. */
1046 p += 8;
1047 put_long_date_timespec(p, a_timespec); /* access time */
1048 p += 8;
1049 put_long_date_timespec(p, m_timespec); /* write time */
1050 p += 8;
1051 put_long_date_timespec(p, m_timespec); /* change time */
1052 p += 8;
1053 SIVAL(p,0,fattr); /* File Attributes. */
1054 p += 4;
1055 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1056 p += 8;
1057 SOFF_T(p,0,file_len);
1058 p += 8;
1059 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1060 SSVAL(p,2,0x7);
1062 p += 4;
1063 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1065 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1066 uint32 perms = 0;
1067 p += 25;
1068 if (fsp->is_directory
1069 || can_write_to_file(conn, fname, &sbuf)) {
1070 perms = FILE_GENERIC_ALL;
1071 } else {
1072 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1074 SIVAL(p,0,perms);
1077 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
1078 fsp->fnum, fsp->fsp_name));
1080 chain_reply(req);
1081 END_PROFILE(SMBntcreateX);
1082 return;
1085 /****************************************************************************
1086 Reply to a NT_TRANSACT_CREATE call to open a pipe.
1087 ****************************************************************************/
1089 static void do_nt_transact_create_pipe(connection_struct *conn,
1090 struct smb_request *req,
1091 uint16 **ppsetup, uint32 setup_count,
1092 char **ppparams, uint32 parameter_count,
1093 char **ppdata, uint32 data_count)
1095 char *fname = NULL;
1096 char *params = *ppparams;
1097 int pnum = -1;
1098 char *p = NULL;
1099 NTSTATUS status;
1100 size_t param_len;
1101 uint32 flags;
1102 TALLOC_CTX *ctx = talloc_tos();
1105 * Ensure minimum number of parameters sent.
1108 if(parameter_count < 54) {
1109 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1110 reply_doserror(req, ERRDOS, ERRnoaccess);
1111 return;
1114 flags = IVAL(params,0);
1116 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
1117 parameter_count-53, STR_TERMINATE,
1118 &status);
1119 if (!NT_STATUS_IS_OK(status)) {
1120 reply_nterror(req, status);
1121 return;
1124 nt_open_pipe(fname, conn, req, &pnum);
1126 if (req->outbuf) {
1127 /* Error return */
1128 return;
1131 /* Realloc the size of parameters and data we will return */
1132 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1133 /* Extended response is 32 more byyes. */
1134 param_len = 101;
1135 } else {
1136 param_len = 69;
1138 params = nttrans_realloc(ppparams, param_len);
1139 if(params == NULL) {
1140 reply_doserror(req, ERRDOS, ERRnomem);
1141 return;
1144 p = params;
1145 SCVAL(p,0,NO_OPLOCK_RETURN);
1147 p += 2;
1148 SSVAL(p,0,pnum);
1149 p += 2;
1150 SIVAL(p,0,FILE_WAS_OPENED);
1151 p += 8;
1153 p += 32;
1154 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1155 p += 20;
1156 /* File type. */
1157 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1158 /* Device state. */
1159 SSVAL(p,2, 0x5FF); /* ? */
1160 p += 4;
1162 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1163 p += 25;
1164 SIVAL(p,0,FILE_GENERIC_ALL);
1166 * For pipes W2K3 seems to return
1167 * 0x12019B next.
1168 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
1170 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
1173 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
1175 /* Send the required number of replies */
1176 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1178 return;
1181 /****************************************************************************
1182 Internal fn to set security descriptors.
1183 ****************************************************************************/
1185 static NTSTATUS set_sd(files_struct *fsp, char *data, uint32 sd_len, uint32 security_info_sent)
1187 prs_struct pd;
1188 SEC_DESC *psd = NULL;
1189 TALLOC_CTX *mem_ctx;
1190 NTSTATUS status;
1192 if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
1193 return NT_STATUS_OK;
1197 * Init the parse struct we will unmarshall from.
1200 if ((mem_ctx = talloc_init("set_sd")) == NULL) {
1201 DEBUG(0,("set_sd: talloc_init failed.\n"));
1202 return NT_STATUS_NO_MEMORY;
1205 prs_init(&pd, 0, mem_ctx, UNMARSHALL);
1208 * Setup the prs_struct to point at the memory we just
1209 * allocated.
1212 prs_give_memory( &pd, data, sd_len, False);
1215 * Finally, unmarshall from the data buffer.
1218 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
1219 DEBUG(0,("set_sd: Error in unmarshalling security descriptor.\n"));
1221 * Return access denied for want of a better error message..
1223 talloc_destroy(mem_ctx);
1224 return NT_STATUS_NO_MEMORY;
1227 if (psd->owner_sid==0) {
1228 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
1230 if (psd->group_sid==0) {
1231 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
1233 if (psd->sacl==0) {
1234 security_info_sent &= ~SACL_SECURITY_INFORMATION;
1236 if (psd->dacl==0) {
1237 security_info_sent &= ~DACL_SECURITY_INFORMATION;
1240 status = SMB_VFS_FSET_NT_ACL( fsp, fsp->fh->fd, security_info_sent, psd);
1242 talloc_destroy(mem_ctx);
1243 return status;
1246 /****************************************************************************
1247 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
1248 ****************************************************************************/
1250 static struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
1252 struct ea_list *ea_list_head = NULL;
1253 size_t offset = 0;
1255 if (data_size < 4) {
1256 return NULL;
1259 while (offset + 4 <= data_size) {
1260 size_t next_offset = IVAL(pdata,offset);
1261 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
1263 if (!eal) {
1264 return NULL;
1267 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
1268 if (next_offset == 0) {
1269 break;
1271 offset += next_offset;
1274 return ea_list_head;
1277 /****************************************************************************
1278 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
1279 ****************************************************************************/
1281 static void call_nt_transact_create(connection_struct *conn,
1282 struct smb_request *req,
1283 uint16 **ppsetup, uint32 setup_count,
1284 char **ppparams, uint32 parameter_count,
1285 char **ppdata, uint32 data_count,
1286 uint32 max_data_count)
1288 char *fname = NULL;
1289 char *params = *ppparams;
1290 char *data = *ppdata;
1291 /* Breakout the oplock request bits so we can set the reply bits separately. */
1292 int oplock_request = 0;
1293 uint32 fattr=0;
1294 SMB_OFF_T file_len = 0;
1295 SMB_STRUCT_STAT sbuf;
1296 int info = 0;
1297 files_struct *fsp = NULL;
1298 char *p = NULL;
1299 uint32 flags;
1300 uint32 access_mask;
1301 uint32 file_attributes;
1302 uint32 share_access;
1303 uint32 create_disposition;
1304 uint32 create_options;
1305 uint32 sd_len;
1306 struct security_descriptor *sd = NULL;
1307 uint32 ea_len;
1308 uint16 root_dir_fid;
1309 struct timespec c_timespec;
1310 struct timespec a_timespec;
1311 struct timespec m_timespec;
1312 struct ea_list *ea_list = NULL;
1313 NTSTATUS status;
1314 size_t param_len;
1315 SMB_BIG_UINT allocation_size;
1316 uint8_t oplock_granted;
1317 TALLOC_CTX *ctx = talloc_tos();
1319 DEBUG(5,("call_nt_transact_create\n"));
1322 * If it's an IPC, use the pipe handler.
1325 if (IS_IPC(conn)) {
1326 if (lp_nt_pipe_support()) {
1327 do_nt_transact_create_pipe(
1328 conn, req,
1329 ppsetup, setup_count,
1330 ppparams, parameter_count,
1331 ppdata, data_count);
1332 return;
1333 } else {
1334 reply_doserror(req, ERRDOS, ERRnoaccess);
1335 return;
1340 * Ensure minimum number of parameters sent.
1343 if(parameter_count < 54) {
1344 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1345 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1346 return;
1349 flags = IVAL(params,0);
1350 access_mask = IVAL(params,8);
1351 file_attributes = IVAL(params,20);
1352 share_access = IVAL(params,24);
1353 create_disposition = IVAL(params,28);
1354 create_options = IVAL(params,32);
1355 sd_len = IVAL(params,36);
1356 ea_len = IVAL(params,40);
1357 root_dir_fid = (uint16)IVAL(params,4);
1358 allocation_size = (SMB_BIG_UINT)IVAL(params,12);
1359 #ifdef LARGE_SMB_OFF_T
1360 allocation_size |= (((SMB_BIG_UINT)IVAL(params,16)) << 32);
1361 #endif
1363 /* Ensure the data_len is correct for the sd and ea values given. */
1364 if ((ea_len + sd_len > data_count)
1365 || (ea_len > data_count) || (sd_len > data_count)
1366 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1367 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1368 "%u, data_count = %u\n", (unsigned int)ea_len,
1369 (unsigned int)sd_len, (unsigned int)data_count));
1370 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1371 return;
1374 if (sd_len) {
1375 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1376 sd_len));
1378 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1379 &sd);
1380 if (!NT_STATUS_IS_OK(status)) {
1381 DEBUG(10, ("call_nt_transact_create: "
1382 "unmarshall_sec_desc failed: %s\n",
1383 nt_errstr(status)));
1384 reply_nterror(req, status);
1385 return;
1389 if (ea_len) {
1390 if (!lp_ea_support(SNUM(conn))) {
1391 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1392 "EA's not supported.\n",
1393 (unsigned int)ea_len));
1394 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1395 return;
1398 if (ea_len < 10) {
1399 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1400 "too small (should be more than 10)\n",
1401 (unsigned int)ea_len ));
1402 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1403 return;
1406 /* We have already checked that ea_len <= data_count here. */
1407 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1408 ea_len);
1409 if (ea_list == NULL) {
1410 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1411 return;
1415 srvstr_get_path(ctx, params, req->flags2, &fname,
1416 params+53, parameter_count-53,
1417 STR_TERMINATE, &status);
1418 if (!NT_STATUS_IS_OK(status)) {
1419 reply_nterror(req, status);
1420 return;
1423 status = create_file(conn, req, root_dir_fid, fname, flags,
1424 access_mask, file_attributes, share_access,
1425 create_disposition, create_options,
1426 oplock_request, allocation_size, sd, ea_list,
1427 &fsp, &info, &oplock_granted, &sbuf);
1429 if(!NT_STATUS_IS_OK(status)) {
1430 if (open_was_deferred(req->mid)) {
1431 /* We have re-scheduled this call, no error. */
1432 return;
1434 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
1435 reply_botherror(req, status, ERRDOS, ERRfilexists);
1437 else {
1438 reply_nterror(req, status);
1440 return;
1443 file_len = sbuf.st_size;
1444 fattr = dos_mode(conn,fname,&sbuf);
1445 if (fattr == 0) {
1446 fattr = FILE_ATTRIBUTE_NORMAL;
1449 /* Realloc the size of parameters and data we will return */
1450 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1451 /* Extended response is 32 more byyes. */
1452 param_len = 101;
1453 } else {
1454 param_len = 69;
1456 params = nttrans_realloc(ppparams, param_len);
1457 if(params == NULL) {
1458 reply_doserror(req, ERRDOS, ERRnomem);
1459 return;
1462 p = params;
1463 SCVAL(p, 0, oplock_granted);
1465 p += 2;
1466 SSVAL(p,0,fsp->fnum);
1467 p += 2;
1468 if ((create_disposition == FILE_SUPERSEDE)
1469 && (info == FILE_WAS_OVERWRITTEN)) {
1470 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1471 } else {
1472 SIVAL(p,0,info);
1474 p += 8;
1476 /* Create time. */
1477 c_timespec = get_create_timespec(
1478 &sbuf,lp_fake_dir_create_times(SNUM(conn)));
1479 a_timespec = get_atimespec(&sbuf);
1480 m_timespec = get_mtimespec(&sbuf);
1482 if (lp_dos_filetime_resolution(SNUM(conn))) {
1483 dos_filetime_timespec(&c_timespec);
1484 dos_filetime_timespec(&a_timespec);
1485 dos_filetime_timespec(&m_timespec);
1488 put_long_date_timespec(p, c_timespec); /* create time. */
1489 p += 8;
1490 put_long_date_timespec(p, a_timespec); /* access time */
1491 p += 8;
1492 put_long_date_timespec(p, m_timespec); /* write time */
1493 p += 8;
1494 put_long_date_timespec(p, m_timespec); /* change time */
1495 p += 8;
1496 SIVAL(p,0,fattr); /* File Attributes. */
1497 p += 4;
1498 SOFF_T(p, 0, get_allocation_size(conn,fsp,&sbuf));
1499 p += 8;
1500 SOFF_T(p,0,file_len);
1501 p += 8;
1502 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1503 SSVAL(p,2,0x7);
1505 p += 4;
1506 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1508 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1509 uint32 perms = 0;
1510 p += 25;
1511 if (fsp->is_directory
1512 || can_write_to_file(conn, fname, &sbuf)) {
1513 perms = FILE_GENERIC_ALL;
1514 } else {
1515 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1517 SIVAL(p,0,perms);
1520 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1522 /* Send the required number of replies */
1523 send_nt_replies(req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1525 return;
1528 /****************************************************************************
1529 Reply to a NT CANCEL request.
1530 conn POINTER CAN BE NULL HERE !
1531 ****************************************************************************/
1533 void reply_ntcancel(connection_struct *conn, struct smb_request *req)
1536 * Go through and cancel any pending change notifies.
1539 START_PROFILE(SMBntcancel);
1540 remove_pending_change_notify_requests_by_mid(req->mid);
1541 remove_pending_lock_requests_by_mid(req->mid);
1542 srv_cancel_sign_response(req->mid);
1544 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1546 END_PROFILE(SMBntcancel);
1547 return;
1550 /****************************************************************************
1551 Copy a file.
1552 ****************************************************************************/
1554 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1555 connection_struct *conn,
1556 struct smb_request *req,
1557 const char *oldname_in,
1558 const char *newname_in,
1559 uint32 attrs)
1561 SMB_STRUCT_STAT sbuf1, sbuf2;
1562 char *oldname = NULL;
1563 char *newname = NULL;
1564 char *last_component_oldname = NULL;
1565 char *last_component_newname = NULL;
1566 files_struct *fsp1,*fsp2;
1567 uint32 fattr;
1568 int info;
1569 SMB_OFF_T ret=-1;
1570 NTSTATUS status = NT_STATUS_OK;
1572 ZERO_STRUCT(sbuf1);
1573 ZERO_STRUCT(sbuf2);
1575 if (!CAN_WRITE(conn)) {
1576 return NT_STATUS_MEDIA_WRITE_PROTECTED;
1579 status = unix_convert(ctx, conn, oldname_in, False, &oldname,
1580 &last_component_oldname, &sbuf1);
1581 if (!NT_STATUS_IS_OK(status)) {
1582 return status;
1585 status = check_name(conn, oldname);
1586 if (!NT_STATUS_IS_OK(status)) {
1587 return status;
1590 /* Source must already exist. */
1591 if (!VALID_STAT(sbuf1)) {
1592 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1594 /* Ensure attributes match. */
1595 fattr = dos_mode(conn,oldname,&sbuf1);
1596 if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1597 return NT_STATUS_NO_SUCH_FILE;
1600 status = unix_convert(ctx, conn, newname_in, False, &newname,
1601 &last_component_newname, &sbuf2);
1602 if (!NT_STATUS_IS_OK(status)) {
1603 return status;
1606 status = check_name(conn, newname);
1607 if (!NT_STATUS_IS_OK(status)) {
1608 return status;
1611 /* Disallow if newname already exists. */
1612 if (VALID_STAT(sbuf2)) {
1613 return NT_STATUS_OBJECT_NAME_COLLISION;
1616 /* No links from a directory. */
1617 if (S_ISDIR(sbuf1.st_mode)) {
1618 return NT_STATUS_FILE_IS_A_DIRECTORY;
1621 /* Ensure this is within the share. */
1622 status = check_reduced_name(conn, oldname);
1623 if (!NT_STATUS_IS_OK(status)) {
1624 return status;
1627 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1628 oldname, newname));
1630 status = open_file_ntcreate(conn, req, oldname, &sbuf1,
1631 FILE_READ_DATA, /* Read-only. */
1632 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1633 FILE_OPEN,
1634 0, /* No create options. */
1635 FILE_ATTRIBUTE_NORMAL,
1636 NO_OPLOCK,
1637 &info, &fsp1);
1639 if (!NT_STATUS_IS_OK(status)) {
1640 return status;
1643 status = open_file_ntcreate(conn, req, newname, &sbuf2,
1644 FILE_WRITE_DATA, /* Read-only. */
1645 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
1646 FILE_CREATE,
1647 0, /* No create options. */
1648 fattr,
1649 NO_OPLOCK,
1650 &info, &fsp2);
1652 if (!NT_STATUS_IS_OK(status)) {
1653 close_file(fsp1,ERROR_CLOSE);
1654 return status;
1657 if (sbuf1.st_size) {
1658 ret = vfs_transfer_file(fsp1, fsp2, sbuf1.st_size);
1662 * As we are opening fsp1 read-only we only expect
1663 * an error on close on fsp2 if we are out of space.
1664 * Thus we don't look at the error return from the
1665 * close of fsp1.
1667 close_file(fsp1,NORMAL_CLOSE);
1669 /* Ensure the modtime is set correctly on the destination file. */
1670 fsp_set_pending_modtime(fsp2, get_mtimespec(&sbuf1));
1672 status = close_file(fsp2,NORMAL_CLOSE);
1674 /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1675 creates the file. This isn't the correct thing to do in the copy
1676 case. JRA */
1677 file_set_dosmode(conn, newname, fattr, &sbuf2,
1678 parent_dirname(newname),false);
1680 if (ret < (SMB_OFF_T)sbuf1.st_size) {
1681 return NT_STATUS_DISK_FULL;
1684 if (!NT_STATUS_IS_OK(status)) {
1685 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1686 nt_errstr(status), oldname, newname));
1688 return status;
1691 /****************************************************************************
1692 Reply to a NT rename request.
1693 ****************************************************************************/
1695 void reply_ntrename(connection_struct *conn, struct smb_request *req)
1697 char *oldname = NULL;
1698 char *newname = NULL;
1699 char *p;
1700 NTSTATUS status;
1701 bool src_has_wcard = False;
1702 bool dest_has_wcard = False;
1703 uint32 attrs;
1704 uint16 rename_type;
1705 TALLOC_CTX *ctx = talloc_tos();
1707 START_PROFILE(SMBntrename);
1709 if (req->wct < 4) {
1710 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1711 END_PROFILE(SMBntrename);
1712 return;
1715 attrs = SVAL(req->inbuf,smb_vwv0);
1716 rename_type = SVAL(req->inbuf,smb_vwv1);
1718 p = smb_buf(req->inbuf) + 1;
1719 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &oldname, p,
1720 0, STR_TERMINATE, &status,
1721 &src_has_wcard);
1722 if (!NT_STATUS_IS_OK(status)) {
1723 reply_nterror(req, status);
1724 END_PROFILE(SMBntrename);
1725 return;
1728 if( is_ntfs_stream_name(oldname)) {
1729 /* Can't rename a stream. */
1730 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1731 END_PROFILE(SMBntrename);
1732 return;
1735 if (ms_has_wild(oldname)) {
1736 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1737 END_PROFILE(SMBntrename);
1738 return;
1741 p++;
1742 p += srvstr_get_path_wcard(ctx, (char *)req->inbuf, req->flags2, &newname, p,
1743 0, STR_TERMINATE, &status,
1744 &dest_has_wcard);
1745 if (!NT_STATUS_IS_OK(status)) {
1746 reply_nterror(req, status);
1747 END_PROFILE(SMBntrename);
1748 return;
1751 status = resolve_dfspath(ctx, conn,
1752 req->flags2 & FLAGS2_DFS_PATHNAMES,
1753 oldname,
1754 &oldname);
1755 if (!NT_STATUS_IS_OK(status)) {
1756 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1757 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1758 ERRSRV, ERRbadpath);
1759 END_PROFILE(SMBntrename);
1760 return;
1762 reply_nterror(req, status);
1763 END_PROFILE(SMBntrename);
1764 return;
1767 status = resolve_dfspath(ctx, conn,
1768 req->flags2 & FLAGS2_DFS_PATHNAMES,
1769 newname,
1770 &newname);
1771 if (!NT_STATUS_IS_OK(status)) {
1772 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1773 reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
1774 ERRSRV, ERRbadpath);
1775 END_PROFILE(SMBntrename);
1776 return;
1778 reply_nterror(req, status);
1779 END_PROFILE(SMBntrename);
1780 return;
1783 DEBUG(3,("reply_ntrename : %s -> %s\n",oldname,newname));
1785 switch(rename_type) {
1786 case RENAME_FLAG_RENAME:
1787 status = rename_internals(ctx, conn, req, oldname,
1788 newname, attrs, False, src_has_wcard,
1789 dest_has_wcard);
1790 break;
1791 case RENAME_FLAG_HARD_LINK:
1792 if (src_has_wcard || dest_has_wcard) {
1793 /* No wildcards. */
1794 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1795 } else {
1796 status = hardlink_internals(ctx,
1797 conn,
1798 oldname,
1799 newname);
1801 break;
1802 case RENAME_FLAG_COPY:
1803 if (src_has_wcard || dest_has_wcard) {
1804 /* No wildcards. */
1805 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1806 } else {
1807 status = copy_internals(ctx, conn, req, oldname,
1808 newname, attrs);
1810 break;
1811 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1812 status = NT_STATUS_INVALID_PARAMETER;
1813 break;
1814 default:
1815 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1816 break;
1819 if (!NT_STATUS_IS_OK(status)) {
1820 if (open_was_deferred(req->mid)) {
1821 /* We have re-scheduled this call. */
1822 END_PROFILE(SMBntrename);
1823 return;
1826 reply_nterror(req, status);
1827 END_PROFILE(SMBntrename);
1828 return;
1831 reply_outbuf(req, 0, 0);
1833 END_PROFILE(SMBntrename);
1834 return;
1837 /****************************************************************************
1838 Reply to a notify change - queue the request and
1839 don't allow a directory to be opened.
1840 ****************************************************************************/
1842 static void call_nt_transact_notify_change(connection_struct *conn,
1843 struct smb_request *req,
1844 uint16 **ppsetup,
1845 uint32 setup_count,
1846 char **ppparams,
1847 uint32 parameter_count,
1848 char **ppdata, uint32 data_count,
1849 uint32 max_data_count,
1850 uint32 max_param_count)
1852 uint16 *setup = *ppsetup;
1853 files_struct *fsp;
1854 uint32 filter;
1855 NTSTATUS status;
1856 bool recursive;
1858 if(setup_count < 6) {
1859 reply_doserror(req, ERRDOS, ERRbadfunc);
1860 return;
1863 fsp = file_fsp(SVAL(setup,4));
1864 filter = IVAL(setup, 0);
1865 recursive = (SVAL(setup, 6) != 0) ? True : False;
1867 DEBUG(3,("call_nt_transact_notify_change\n"));
1869 if(!fsp) {
1870 reply_doserror(req, ERRDOS, ERRbadfid);
1871 return;
1875 char *filter_string;
1877 if (!(filter_string = notify_filter_string(NULL, filter))) {
1878 reply_nterror(req,NT_STATUS_NO_MEMORY);
1879 return;
1882 DEBUG(3,("call_nt_transact_notify_change: notify change "
1883 "called on %s, filter = %s, recursive = %d\n",
1884 fsp->fsp_name, filter_string, recursive));
1886 TALLOC_FREE(filter_string);
1889 if((!fsp->is_directory) || (conn != fsp->conn)) {
1890 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1891 return;
1894 if (fsp->notify == NULL) {
1896 status = change_notify_create(fsp, filter, recursive);
1898 if (!NT_STATUS_IS_OK(status)) {
1899 DEBUG(10, ("change_notify_create returned %s\n",
1900 nt_errstr(status)));
1901 reply_nterror(req, status);
1902 return;
1906 if (fsp->notify->num_changes != 0) {
1909 * We've got changes pending, respond immediately
1913 * TODO: write a torture test to check the filtering behaviour
1914 * here.
1917 change_notify_reply(req->inbuf, max_param_count, fsp->notify);
1920 * change_notify_reply() above has independently sent its
1921 * results
1923 return;
1927 * No changes pending, queue the request
1930 status = change_notify_add_request(req->inbuf, max_param_count, filter,
1931 recursive, fsp);
1932 if (!NT_STATUS_IS_OK(status)) {
1933 reply_nterror(req, status);
1935 return;
1938 /****************************************************************************
1939 Reply to an NT transact rename command.
1940 ****************************************************************************/
1942 static void call_nt_transact_rename(connection_struct *conn,
1943 struct smb_request *req,
1944 uint16 **ppsetup, uint32 setup_count,
1945 char **ppparams, uint32 parameter_count,
1946 char **ppdata, uint32 data_count,
1947 uint32 max_data_count)
1949 char *params = *ppparams;
1950 char *new_name = NULL;
1951 files_struct *fsp = NULL;
1952 bool replace_if_exists = False;
1953 bool dest_has_wcard = False;
1954 NTSTATUS status;
1955 TALLOC_CTX *ctx = talloc_tos();
1957 if(parameter_count < 5) {
1958 reply_doserror(req, ERRDOS, ERRbadfunc);
1959 return;
1962 fsp = file_fsp(SVAL(params, 0));
1963 replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1964 if (!check_fsp(conn, req, fsp, &current_user)) {
1965 return;
1967 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1968 parameter_count - 4,
1969 STR_TERMINATE, &status, &dest_has_wcard);
1970 if (!NT_STATUS_IS_OK(status)) {
1971 reply_nterror(req, status);
1972 return;
1975 status = rename_internals(ctx,
1976 conn,
1977 req,
1978 fsp->fsp_name,
1979 new_name,
1981 replace_if_exists,
1982 False,
1983 dest_has_wcard);
1985 if (!NT_STATUS_IS_OK(status)) {
1986 if (open_was_deferred(req->mid)) {
1987 /* We have re-scheduled this call. */
1988 return;
1990 reply_nterror(req, status);
1991 return;
1995 * Rename was successful.
1997 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
1999 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
2000 fsp->fsp_name, new_name));
2002 return;
2005 /******************************************************************************
2006 Fake up a completely empty SD.
2007 *******************************************************************************/
2009 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
2011 size_t sd_size;
2013 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
2014 if(!*ppsd) {
2015 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
2016 return NT_STATUS_NO_MEMORY;
2019 return NT_STATUS_OK;
2022 /****************************************************************************
2023 Reply to query a security descriptor.
2024 ****************************************************************************/
2026 static void call_nt_transact_query_security_desc(connection_struct *conn,
2027 struct smb_request *req,
2028 uint16 **ppsetup,
2029 uint32 setup_count,
2030 char **ppparams,
2031 uint32 parameter_count,
2032 char **ppdata,
2033 uint32 data_count,
2034 uint32 max_data_count)
2036 char *params = *ppparams;
2037 char *data = *ppdata;
2038 prs_struct pd;
2039 SEC_DESC *psd = NULL;
2040 size_t sd_size;
2041 uint32 security_info_wanted;
2042 TALLOC_CTX *mem_ctx;
2043 files_struct *fsp = NULL;
2044 NTSTATUS status;
2046 if(parameter_count < 8) {
2047 reply_doserror(req, ERRDOS, ERRbadfunc);
2048 return;
2051 fsp = file_fsp(SVAL(params,0));
2052 if(!fsp) {
2053 reply_doserror(req, ERRDOS, ERRbadfid);
2054 return;
2057 security_info_wanted = IVAL(params,4);
2059 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, info_wanted = 0x%x\n", fsp->fsp_name,
2060 (unsigned int)security_info_wanted ));
2062 params = nttrans_realloc(ppparams, 4);
2063 if(params == NULL) {
2064 reply_doserror(req, ERRDOS, ERRnomem);
2065 return;
2068 if ((mem_ctx = talloc_init("call_nt_transact_query_security_desc")) == NULL) {
2069 DEBUG(0,("call_nt_transact_query_security_desc: talloc_init failed.\n"));
2070 reply_doserror(req, ERRDOS, ERRnomem);
2071 return;
2075 * Get the permissions to return.
2078 if (!lp_nt_acl_support(SNUM(conn))) {
2079 status = get_null_nt_acl(mem_ctx, &psd);
2080 } else {
2081 status = SMB_VFS_FGET_NT_ACL(fsp, fsp->fh->fd,
2082 security_info_wanted, &psd);
2085 if (!NT_STATUS_IS_OK(status)) {
2086 talloc_destroy(mem_ctx);
2087 reply_nterror(req, status);
2088 return;
2091 sd_size = sec_desc_size(psd);
2093 DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
2095 SIVAL(params,0,(uint32)sd_size);
2097 if(max_data_count < sd_size) {
2099 send_nt_replies(req, NT_STATUS_BUFFER_TOO_SMALL,
2100 params, 4, *ppdata, 0);
2101 talloc_destroy(mem_ctx);
2102 return;
2106 * Allocate the data we will point this at.
2109 data = nttrans_realloc(ppdata, sd_size);
2110 if(data == NULL) {
2111 talloc_destroy(mem_ctx);
2112 reply_doserror(req, ERRDOS, ERRnomem);
2113 return;
2117 * Init the parse struct we will marshall into.
2120 prs_init(&pd, 0, mem_ctx, MARSHALL);
2123 * Setup the prs_struct to point at the memory we just
2124 * allocated.
2127 prs_give_memory( &pd, data, (uint32)sd_size, False);
2130 * Finally, linearize into the outgoing buffer.
2133 if(!sec_io_desc( "sd data", &psd, &pd, 1)) {
2134 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2135 security descriptor.\n"));
2137 * Return access denied for want of a better error message..
2139 talloc_destroy(mem_ctx);
2140 reply_unixerror(req, ERRDOS, ERRnoaccess);
2141 return;
2145 * Now we can delete the security descriptor.
2148 talloc_destroy(mem_ctx);
2150 send_nt_replies(req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2151 return;
2154 /****************************************************************************
2155 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2156 ****************************************************************************/
2158 static void call_nt_transact_set_security_desc(connection_struct *conn,
2159 struct smb_request *req,
2160 uint16 **ppsetup,
2161 uint32 setup_count,
2162 char **ppparams,
2163 uint32 parameter_count,
2164 char **ppdata,
2165 uint32 data_count,
2166 uint32 max_data_count)
2168 char *params= *ppparams;
2169 char *data = *ppdata;
2170 files_struct *fsp = NULL;
2171 uint32 security_info_sent = 0;
2172 NTSTATUS nt_status;
2174 if(parameter_count < 8) {
2175 reply_doserror(req, ERRDOS, ERRbadfunc);
2176 return;
2179 if((fsp = file_fsp(SVAL(params,0))) == NULL) {
2180 reply_doserror(req, ERRDOS, ERRbadfid);
2181 return;
2184 if(!lp_nt_acl_support(SNUM(conn))) {
2185 goto done;
2188 security_info_sent = IVAL(params,4);
2190 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2191 (unsigned int)security_info_sent ));
2193 if (data_count == 0) {
2194 reply_doserror(req, ERRDOS, ERRnoaccess);
2195 return;
2198 if (!NT_STATUS_IS_OK(nt_status = set_sd( fsp, data, data_count, security_info_sent))) {
2199 reply_nterror(req, nt_status);
2200 return;
2203 done:
2205 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2206 return;
2209 /****************************************************************************
2210 Reply to NT IOCTL
2211 ****************************************************************************/
2213 static void call_nt_transact_ioctl(connection_struct *conn,
2214 struct smb_request *req,
2215 uint16 **ppsetup, uint32 setup_count,
2216 char **ppparams, uint32 parameter_count,
2217 char **ppdata, uint32 data_count,
2218 uint32 max_data_count)
2220 uint32 function;
2221 uint16 fidnum;
2222 files_struct *fsp;
2223 uint8 isFSctl;
2224 uint8 compfilter;
2225 static bool logged_message;
2226 char *pdata = *ppdata;
2228 if (setup_count != 8) {
2229 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2230 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2231 return;
2234 function = IVAL(*ppsetup, 0);
2235 fidnum = SVAL(*ppsetup, 4);
2236 isFSctl = CVAL(*ppsetup, 6);
2237 compfilter = CVAL(*ppsetup, 7);
2239 DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2240 function, fidnum, isFSctl, compfilter));
2242 fsp=file_fsp(fidnum);
2243 /* this check is done in each implemented function case for now
2244 because I don't want to break anything... --metze
2245 FSP_BELONGS_CONN(fsp,conn);*/
2247 switch (function) {
2248 case FSCTL_SET_SPARSE:
2249 /* pretend this succeeded - tho strictly we should
2250 mark the file sparse (if the local fs supports it)
2251 so we can know if we need to pre-allocate or not */
2253 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
2254 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2255 return;
2257 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2259 unsigned char objid[16];
2261 /* This should return the object-id on this file.
2262 * I think I'll make this be the inode+dev. JRA.
2265 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
2267 if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
2268 return;
2271 data_count = 64;
2272 pdata = nttrans_realloc(ppdata, data_count);
2273 if (pdata == NULL) {
2274 reply_nterror(req, NT_STATUS_NO_MEMORY);
2275 return;
2277 push_file_id_16(pdata, &fsp->file_id);
2278 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
2279 push_file_id_16(pdata+32, &fsp->file_id);
2280 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2281 pdata, data_count);
2282 return;
2285 case FSCTL_GET_REPARSE_POINT:
2286 /* pretend this fail - my winXP does it like this
2287 * --metze
2290 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2291 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2292 return;
2294 case FSCTL_SET_REPARSE_POINT:
2295 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
2296 * --metze
2299 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
2300 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
2301 return;
2303 case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
2306 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2307 * and return their volume names. If max_data_count is 16, then it is just
2308 * asking for the number of volumes and length of the combined names.
2310 * pdata is the data allocated by our caller, but that uses
2311 * total_data_count (which is 0 in our case) rather than max_data_count.
2312 * Allocate the correct amount and return the pointer to let
2313 * it be deallocated when we return.
2315 SHADOW_COPY_DATA *shadow_data = NULL;
2316 TALLOC_CTX *shadow_mem_ctx = NULL;
2317 bool labels = False;
2318 uint32 labels_data_count = 0;
2319 uint32 i;
2320 char *cur_pdata;
2322 if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
2323 return;
2326 if (max_data_count < 16) {
2327 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2328 max_data_count));
2329 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2330 return;
2333 if (max_data_count > 16) {
2334 labels = True;
2337 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
2338 if (shadow_mem_ctx == NULL) {
2339 DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
2340 reply_nterror(req, NT_STATUS_NO_MEMORY);
2341 return;
2344 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
2345 if (shadow_data == NULL) {
2346 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2347 talloc_destroy(shadow_mem_ctx);
2348 reply_nterror(req, NT_STATUS_NO_MEMORY);
2349 return;
2352 shadow_data->mem_ctx = shadow_mem_ctx;
2355 * Call the VFS routine to actually do the work.
2357 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2358 talloc_destroy(shadow_data->mem_ctx);
2359 if (errno == ENOSYS) {
2360 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2361 conn->connectpath));
2362 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2363 return;
2364 } else {
2365 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2366 conn->connectpath));
2367 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2368 return;
2372 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2374 if (!labels) {
2375 data_count = 16;
2376 } else {
2377 data_count = 12+labels_data_count+4;
2380 if (max_data_count<data_count) {
2381 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2382 max_data_count,data_count));
2383 talloc_destroy(shadow_data->mem_ctx);
2384 reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2385 return;
2388 pdata = nttrans_realloc(ppdata, data_count);
2389 if (pdata == NULL) {
2390 talloc_destroy(shadow_data->mem_ctx);
2391 reply_nterror(req, NT_STATUS_NO_MEMORY);
2392 return;
2395 cur_pdata = pdata;
2397 /* num_volumes 4 bytes */
2398 SIVAL(pdata,0,shadow_data->num_volumes);
2400 if (labels) {
2401 /* num_labels 4 bytes */
2402 SIVAL(pdata,4,shadow_data->num_volumes);
2405 /* needed_data_count 4 bytes */
2406 SIVAL(pdata,8,labels_data_count);
2408 cur_pdata+=12;
2410 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2411 shadow_data->num_volumes,fsp->fsp_name));
2412 if (labels && shadow_data->labels) {
2413 for (i=0;i<shadow_data->num_volumes;i++) {
2414 srvstr_push(pdata, req->flags2,
2415 cur_pdata, shadow_data->labels[i],
2416 2*sizeof(SHADOW_COPY_LABEL),
2417 STR_UNICODE|STR_TERMINATE);
2418 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2419 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2423 talloc_destroy(shadow_data->mem_ctx);
2425 send_nt_replies(req, NT_STATUS_OK, NULL, 0,
2426 pdata, data_count);
2428 return;
2431 case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2433 /* pretend this succeeded -
2435 * we have to send back a list with all files owned by this SID
2437 * but I have to check that --metze
2439 DOM_SID sid;
2440 uid_t uid;
2441 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2443 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2445 if (!fsp_belongs_conn(conn, req, fsp, &current_user)) {
2446 return;
2449 /* unknown 4 bytes: this is not the length of the sid :-( */
2450 /*unknown = IVAL(pdata,0);*/
2452 sid_parse(pdata+4,sid_len,&sid);
2453 DEBUGADD(10,("for SID: %s\n",sid_string_static(&sid)));
2455 if (!sid_to_uid(&sid, &uid)) {
2456 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2457 sid_string_static(&sid),(unsigned long)sid_len));
2458 uid = (-1);
2461 /* we can take a look at the find source :-)
2463 * find ./ -uid $uid -name '*' is what we need here
2466 * and send 4bytes len and then NULL terminated unicode strings
2467 * for each file
2469 * but I don't know how to deal with the paged results
2470 * (maybe we can hang the result anywhere in the fsp struct)
2472 * we don't send all files at once
2473 * and at the next we should *not* start from the beginning,
2474 * so we have to cache the result
2476 * --metze
2479 /* this works for now... */
2480 send_nt_replies(req, NT_STATUS_OK, NULL, 0, NULL, 0);
2481 return;
2483 default:
2484 if (!logged_message) {
2485 logged_message = True; /* Only print this once... */
2486 DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2487 function));
2491 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2495 #ifdef HAVE_SYS_QUOTAS
2496 /****************************************************************************
2497 Reply to get user quota
2498 ****************************************************************************/
2500 static void call_nt_transact_get_user_quota(connection_struct *conn,
2501 struct smb_request *req,
2502 uint16 **ppsetup,
2503 uint32 setup_count,
2504 char **ppparams,
2505 uint32 parameter_count,
2506 char **ppdata,
2507 uint32 data_count,
2508 uint32 max_data_count)
2510 NTSTATUS nt_status = NT_STATUS_OK;
2511 char *params = *ppparams;
2512 char *pdata = *ppdata;
2513 char *entry;
2514 int data_len=0,param_len=0;
2515 int qt_len=0;
2516 int entry_len = 0;
2517 files_struct *fsp = NULL;
2518 uint16 level = 0;
2519 size_t sid_len;
2520 DOM_SID sid;
2521 bool start_enum = True;
2522 SMB_NTQUOTA_STRUCT qt;
2523 SMB_NTQUOTA_LIST *tmp_list;
2524 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2526 ZERO_STRUCT(qt);
2528 /* access check */
2529 if (current_user.ut.uid != 0) {
2530 DEBUG(1,("get_user_quota: access_denied service [%s] user [%s]\n",
2531 lp_servicename(SNUM(conn)),conn->user));
2532 reply_doserror(req, ERRDOS, ERRnoaccess);
2533 return;
2537 * Ensure minimum number of parameters sent.
2540 if (parameter_count < 4) {
2541 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2542 reply_doserror(req, ERRDOS, ERRinvalidparam);
2543 return;
2546 /* maybe we can check the quota_fnum */
2547 fsp = file_fsp(SVAL(params,0));
2548 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2549 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2550 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2551 return;
2554 /* the NULL pointer checking for fsp->fake_file_handle->pd
2555 * is done by CHECK_NTQUOTA_HANDLE_OK()
2557 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->pd;
2559 level = SVAL(params,2);
2561 /* unknown 12 bytes leading in params */
2563 switch (level) {
2564 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2565 /* seems that we should continue with the enum here --metze */
2567 if (qt_handle->quota_list!=NULL &&
2568 qt_handle->tmp_list==NULL) {
2570 /* free the list */
2571 free_ntquota_list(&(qt_handle->quota_list));
2573 /* Realloc the size of parameters and data we will return */
2574 param_len = 4;
2575 params = nttrans_realloc(ppparams, param_len);
2576 if(params == NULL) {
2577 reply_doserror(req, ERRDOS, ERRnomem);
2578 return;
2581 data_len = 0;
2582 SIVAL(params,0,data_len);
2584 break;
2587 start_enum = False;
2589 case TRANSACT_GET_USER_QUOTA_LIST_START:
2591 if (qt_handle->quota_list==NULL &&
2592 qt_handle->tmp_list==NULL) {
2593 start_enum = True;
2596 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2597 reply_doserror(req, ERRSRV, ERRerror);
2598 return;
2601 /* Realloc the size of parameters and data we will return */
2602 param_len = 4;
2603 params = nttrans_realloc(ppparams, param_len);
2604 if(params == NULL) {
2605 reply_doserror(req, ERRDOS, ERRnomem);
2606 return;
2609 /* we should not trust the value in max_data_count*/
2610 max_data_count = MIN(max_data_count,2048);
2612 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2613 if(pdata == NULL) {
2614 reply_doserror(req, ERRDOS, ERRnomem);
2615 return;
2618 entry = pdata;
2620 /* set params Size of returned Quota Data 4 bytes*/
2621 /* but set it later when we know it */
2623 /* for each entry push the data */
2625 if (start_enum) {
2626 qt_handle->tmp_list = qt_handle->quota_list;
2629 tmp_list = qt_handle->tmp_list;
2631 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2632 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2634 sid_len = sid_size(&tmp_list->quotas->sid);
2635 entry_len = 40 + sid_len;
2637 /* nextoffset entry 4 bytes */
2638 SIVAL(entry,0,entry_len);
2640 /* then the len of the SID 4 bytes */
2641 SIVAL(entry,4,sid_len);
2643 /* unknown data 8 bytes SMB_BIG_UINT */
2644 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-metze*/
2646 /* the used disk space 8 bytes SMB_BIG_UINT */
2647 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2649 /* the soft quotas 8 bytes SMB_BIG_UINT */
2650 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2652 /* the hard quotas 8 bytes SMB_BIG_UINT */
2653 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2655 /* and now the SID */
2656 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2659 qt_handle->tmp_list = tmp_list;
2661 /* overwrite the offset of the last entry */
2662 SIVAL(entry-entry_len,0,0);
2664 data_len = 4+qt_len;
2665 /* overwrite the params quota_data_len */
2666 SIVAL(params,0,data_len);
2668 break;
2670 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2672 /* unknown 4 bytes IVAL(pdata,0) */
2674 if (data_count < 8) {
2675 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2676 reply_doserror(req, ERRDOS, ERRunknownlevel);
2677 return;
2680 sid_len = IVAL(pdata,4);
2681 /* Ensure this is less than 1mb. */
2682 if (sid_len > (1024*1024)) {
2683 reply_doserror(req, ERRDOS, ERRnomem);
2684 return;
2687 if (data_count < 8+sid_len) {
2688 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2689 reply_doserror(req, ERRDOS, ERRunknownlevel);
2690 return;
2693 data_len = 4+40+sid_len;
2695 if (max_data_count < data_len) {
2696 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2697 max_data_count, data_len));
2698 param_len = 4;
2699 SIVAL(params,0,data_len);
2700 data_len = 0;
2701 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2702 break;
2705 sid_parse(pdata+8,sid_len,&sid);
2707 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2708 ZERO_STRUCT(qt);
2710 * we have to return zero's in all fields
2711 * instead of returning an error here
2712 * --metze
2716 /* Realloc the size of parameters and data we will return */
2717 param_len = 4;
2718 params = nttrans_realloc(ppparams, param_len);
2719 if(params == NULL) {
2720 reply_doserror(req, ERRDOS, ERRnomem);
2721 return;
2724 pdata = nttrans_realloc(ppdata, data_len);
2725 if(pdata == NULL) {
2726 reply_doserror(req, ERRDOS, ERRnomem);
2727 return;
2730 entry = pdata;
2732 /* set params Size of returned Quota Data 4 bytes*/
2733 SIVAL(params,0,data_len);
2735 /* nextoffset entry 4 bytes */
2736 SIVAL(entry,0,0);
2738 /* then the len of the SID 4 bytes */
2739 SIVAL(entry,4,sid_len);
2741 /* unknown data 8 bytes SMB_BIG_UINT */
2742 SBIG_UINT(entry,8,(SMB_BIG_UINT)0); /* this is not 0 in windows...-mezte*/
2744 /* the used disk space 8 bytes SMB_BIG_UINT */
2745 SBIG_UINT(entry,16,qt.usedspace);
2747 /* the soft quotas 8 bytes SMB_BIG_UINT */
2748 SBIG_UINT(entry,24,qt.softlim);
2750 /* the hard quotas 8 bytes SMB_BIG_UINT */
2751 SBIG_UINT(entry,32,qt.hardlim);
2753 /* and now the SID */
2754 sid_linearize(entry+40, sid_len, &sid);
2756 break;
2758 default:
2759 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2760 reply_doserror(req, ERRSRV, ERRerror);
2761 return;
2762 break;
2765 send_nt_replies(req, nt_status, params, param_len,
2766 pdata, data_len);
2769 /****************************************************************************
2770 Reply to set user quota
2771 ****************************************************************************/
2773 static void call_nt_transact_set_user_quota(connection_struct *conn,
2774 struct smb_request *req,
2775 uint16 **ppsetup,
2776 uint32 setup_count,
2777 char **ppparams,
2778 uint32 parameter_count,
2779 char **ppdata,
2780 uint32 data_count,
2781 uint32 max_data_count)
2783 char *params = *ppparams;
2784 char *pdata = *ppdata;
2785 int data_len=0,param_len=0;
2786 SMB_NTQUOTA_STRUCT qt;
2787 size_t sid_len;
2788 DOM_SID sid;
2789 files_struct *fsp = NULL;
2791 ZERO_STRUCT(qt);
2793 /* access check */
2794 if (current_user.ut.uid != 0) {
2795 DEBUG(1,("set_user_quota: access_denied service [%s] user [%s]\n",
2796 lp_servicename(SNUM(conn)),conn->user));
2797 reply_doserror(req, ERRDOS, ERRnoaccess);
2798 return;
2802 * Ensure minimum number of parameters sent.
2805 if (parameter_count < 2) {
2806 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2807 reply_doserror(req, ERRDOS, ERRinvalidparam);
2808 return;
2811 /* maybe we can check the quota_fnum */
2812 fsp = file_fsp(SVAL(params,0));
2813 if (!CHECK_NTQUOTA_HANDLE_OK(fsp,conn)) {
2814 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2815 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2816 return;
2819 if (data_count < 40) {
2820 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2821 reply_doserror(req, ERRDOS, ERRunknownlevel);
2822 return;
2825 /* offset to next quota record.
2826 * 4 bytes IVAL(pdata,0)
2827 * unused here...
2830 /* sid len */
2831 sid_len = IVAL(pdata,4);
2833 if (data_count < 40+sid_len) {
2834 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2835 reply_doserror(req, ERRDOS, ERRunknownlevel);
2836 return;
2839 /* unknown 8 bytes in pdata
2840 * maybe its the change time in NTTIME
2843 /* the used space 8 bytes (SMB_BIG_UINT)*/
2844 qt.usedspace = (SMB_BIG_UINT)IVAL(pdata,16);
2845 #ifdef LARGE_SMB_OFF_T
2846 qt.usedspace |= (((SMB_BIG_UINT)IVAL(pdata,20)) << 32);
2847 #else /* LARGE_SMB_OFF_T */
2848 if ((IVAL(pdata,20) != 0)&&
2849 ((qt.usedspace != 0xFFFFFFFF)||
2850 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2851 /* more than 32 bits? */
2852 reply_doserror(req, ERRDOS, ERRunknownlevel);
2853 return;
2855 #endif /* LARGE_SMB_OFF_T */
2857 /* the soft quotas 8 bytes (SMB_BIG_UINT)*/
2858 qt.softlim = (SMB_BIG_UINT)IVAL(pdata,24);
2859 #ifdef LARGE_SMB_OFF_T
2860 qt.softlim |= (((SMB_BIG_UINT)IVAL(pdata,28)) << 32);
2861 #else /* LARGE_SMB_OFF_T */
2862 if ((IVAL(pdata,28) != 0)&&
2863 ((qt.softlim != 0xFFFFFFFF)||
2864 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2865 /* more than 32 bits? */
2866 reply_doserror(req, ERRDOS, ERRunknownlevel);
2867 return;
2869 #endif /* LARGE_SMB_OFF_T */
2871 /* the hard quotas 8 bytes (SMB_BIG_UINT)*/
2872 qt.hardlim = (SMB_BIG_UINT)IVAL(pdata,32);
2873 #ifdef LARGE_SMB_OFF_T
2874 qt.hardlim |= (((SMB_BIG_UINT)IVAL(pdata,36)) << 32);
2875 #else /* LARGE_SMB_OFF_T */
2876 if ((IVAL(pdata,36) != 0)&&
2877 ((qt.hardlim != 0xFFFFFFFF)||
2878 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2879 /* more than 32 bits? */
2880 reply_doserror(req, ERRDOS, ERRunknownlevel);
2881 return;
2883 #endif /* LARGE_SMB_OFF_T */
2885 sid_parse(pdata+40,sid_len,&sid);
2886 DEBUGADD(8,("SID: %s\n",sid_string_static(&sid)));
2888 /* 44 unknown bytes left... */
2890 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2891 reply_doserror(req, ERRSRV, ERRerror);
2892 return;
2895 send_nt_replies(req, NT_STATUS_OK, params, param_len,
2896 pdata, data_len);
2898 #endif /* HAVE_SYS_QUOTAS */
2900 static void handle_nttrans(connection_struct *conn,
2901 struct trans_state *state,
2902 struct smb_request *req)
2904 if (Protocol >= PROTOCOL_NT1) {
2905 req->flags2 |= 0x40; /* IS_LONG_NAME */
2906 SSVAL(req->inbuf,smb_flg2,req->flags2);
2909 /* Now we must call the relevant NT_TRANS function */
2910 switch(state->call) {
2911 case NT_TRANSACT_CREATE:
2913 START_PROFILE(NT_transact_create);
2914 call_nt_transact_create(
2915 conn, req,
2916 &state->setup, state->setup_count,
2917 &state->param, state->total_param,
2918 &state->data, state->total_data,
2919 state->max_data_return);
2920 END_PROFILE(NT_transact_create);
2921 break;
2924 case NT_TRANSACT_IOCTL:
2926 START_PROFILE(NT_transact_ioctl);
2927 call_nt_transact_ioctl(
2928 conn, req,
2929 &state->setup, state->setup_count,
2930 &state->param, state->total_param,
2931 &state->data, state->total_data,
2932 state->max_data_return);
2933 END_PROFILE(NT_transact_ioctl);
2934 break;
2937 case NT_TRANSACT_SET_SECURITY_DESC:
2939 START_PROFILE(NT_transact_set_security_desc);
2940 call_nt_transact_set_security_desc(
2941 conn, req,
2942 &state->setup, state->setup_count,
2943 &state->param, state->total_param,
2944 &state->data, state->total_data,
2945 state->max_data_return);
2946 END_PROFILE(NT_transact_set_security_desc);
2947 break;
2950 case NT_TRANSACT_NOTIFY_CHANGE:
2952 START_PROFILE(NT_transact_notify_change);
2953 call_nt_transact_notify_change(
2954 conn, req,
2955 &state->setup, state->setup_count,
2956 &state->param, state->total_param,
2957 &state->data, state->total_data,
2958 state->max_data_return,
2959 state->max_param_return);
2960 END_PROFILE(NT_transact_notify_change);
2961 break;
2964 case NT_TRANSACT_RENAME:
2966 START_PROFILE(NT_transact_rename);
2967 call_nt_transact_rename(
2968 conn, req,
2969 &state->setup, state->setup_count,
2970 &state->param, state->total_param,
2971 &state->data, state->total_data,
2972 state->max_data_return);
2973 END_PROFILE(NT_transact_rename);
2974 break;
2977 case NT_TRANSACT_QUERY_SECURITY_DESC:
2979 START_PROFILE(NT_transact_query_security_desc);
2980 call_nt_transact_query_security_desc(
2981 conn, req,
2982 &state->setup, state->setup_count,
2983 &state->param, state->total_param,
2984 &state->data, state->total_data,
2985 state->max_data_return);
2986 END_PROFILE(NT_transact_query_security_desc);
2987 break;
2990 #ifdef HAVE_SYS_QUOTAS
2991 case NT_TRANSACT_GET_USER_QUOTA:
2993 START_PROFILE(NT_transact_get_user_quota);
2994 call_nt_transact_get_user_quota(
2995 conn, req,
2996 &state->setup, state->setup_count,
2997 &state->param, state->total_param,
2998 &state->data, state->total_data,
2999 state->max_data_return);
3000 END_PROFILE(NT_transact_get_user_quota);
3001 break;
3004 case NT_TRANSACT_SET_USER_QUOTA:
3006 START_PROFILE(NT_transact_set_user_quota);
3007 call_nt_transact_set_user_quota(
3008 conn, req,
3009 &state->setup, state->setup_count,
3010 &state->param, state->total_param,
3011 &state->data, state->total_data,
3012 state->max_data_return);
3013 END_PROFILE(NT_transact_set_user_quota);
3014 break;
3016 #endif /* HAVE_SYS_QUOTAS */
3018 default:
3019 /* Error in request */
3020 DEBUG(0,("handle_nttrans: Unknown request %d in "
3021 "nttrans call\n", state->call));
3022 reply_doserror(req, ERRSRV, ERRerror);
3023 return;
3025 return;
3028 /****************************************************************************
3029 Reply to a SMBNTtrans.
3030 ****************************************************************************/
3032 void reply_nttrans(connection_struct *conn, struct smb_request *req)
3034 uint32 pscnt;
3035 uint32 psoff;
3036 uint32 dscnt;
3037 uint32 dsoff;
3038 uint16 function_code;
3039 NTSTATUS result;
3040 struct trans_state *state;
3041 int size;
3043 START_PROFILE(SMBnttrans);
3045 if (req->wct < 19) {
3046 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3047 END_PROFILE(SMBnttrans);
3048 return;
3051 size = smb_len(req->inbuf) + 4;
3052 pscnt = IVAL(req->inbuf,smb_nt_ParameterCount);
3053 psoff = IVAL(req->inbuf,smb_nt_ParameterOffset);
3054 dscnt = IVAL(req->inbuf,smb_nt_DataCount);
3055 dsoff = IVAL(req->inbuf,smb_nt_DataOffset);
3056 function_code = SVAL(req->inbuf, smb_nt_Function);
3058 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3059 reply_doserror(req, ERRSRV, ERRaccess);
3060 END_PROFILE(SMBnttrans);
3061 return;
3064 result = allow_new_trans(conn->pending_trans, req->mid);
3065 if (!NT_STATUS_IS_OK(result)) {
3066 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3067 reply_nterror(req, result);
3068 END_PROFILE(SMBnttrans);
3069 return;
3072 if ((state = TALLOC_P(conn->mem_ctx, struct trans_state)) == NULL) {
3073 reply_doserror(req, ERRSRV, ERRaccess);
3074 END_PROFILE(SMBnttrans);
3075 return;
3078 state->cmd = SMBnttrans;
3080 state->mid = req->mid;
3081 state->vuid = req->vuid;
3082 state->total_data = IVAL(req->inbuf, smb_nt_TotalDataCount);
3083 state->data = NULL;
3084 state->total_param = IVAL(req->inbuf, smb_nt_TotalParameterCount);
3085 state->param = NULL;
3086 state->max_data_return = IVAL(req->inbuf,smb_nt_MaxDataCount);
3087 state->max_param_return = IVAL(req->inbuf,smb_nt_MaxParameterCount);
3089 /* setup count is in *words* */
3090 state->setup_count = 2*CVAL(req->inbuf,smb_nt_SetupCount);
3091 state->setup = NULL;
3092 state->call = function_code;
3095 * All nttrans messages we handle have smb_wct == 19 +
3096 * state->setup_count. Ensure this is so as a sanity check.
3099 if(req->wct != 19 + (state->setup_count/2)) {
3100 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3101 req->wct, 19 + (state->setup_count/2)));
3102 goto bad_param;
3105 /* Don't allow more than 128mb for each value. */
3106 if ((state->total_data > (1024*1024*128)) ||
3107 (state->total_param > (1024*1024*128))) {
3108 reply_doserror(req, ERRDOS, ERRnomem);
3109 END_PROFILE(SMBnttrans);
3110 return;
3113 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3114 goto bad_param;
3116 if (state->total_data) {
3117 /* Can't use talloc here, the core routines do realloc on the
3118 * params and data. */
3119 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3120 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3121 "bytes !\n", (unsigned int)state->total_data));
3122 TALLOC_FREE(state);
3123 reply_doserror(req, ERRDOS, ERRnomem);
3124 END_PROFILE(SMBnttrans);
3125 return;
3127 if ((dsoff+dscnt < dsoff) || (dsoff+dscnt < dscnt))
3128 goto bad_param;
3129 if ((smb_base(req->inbuf)+dsoff+dscnt
3130 > (char *)req->inbuf + size) ||
3131 (smb_base(req->inbuf)+dsoff+dscnt < smb_base(req->inbuf)))
3132 goto bad_param;
3134 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3137 if (state->total_param) {
3138 /* Can't use talloc here, the core routines do realloc on the
3139 * params and data. */
3140 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3141 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3142 "bytes !\n", (unsigned int)state->total_param));
3143 SAFE_FREE(state->data);
3144 TALLOC_FREE(state);
3145 reply_doserror(req, ERRDOS, ERRnomem);
3146 END_PROFILE(SMBnttrans);
3147 return;
3149 if ((psoff+pscnt < psoff) || (psoff+pscnt < pscnt))
3150 goto bad_param;
3151 if ((smb_base(req->inbuf)+psoff+pscnt
3152 > (char *)req->inbuf + size) ||
3153 (smb_base(req->inbuf)+psoff+pscnt < smb_base(req->inbuf)))
3154 goto bad_param;
3156 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3159 state->received_data = dscnt;
3160 state->received_param = pscnt;
3162 if(state->setup_count > 0) {
3163 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3164 state->setup_count));
3165 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3166 if (state->setup == NULL) {
3167 DEBUG(0,("reply_nttrans : Out of memory\n"));
3168 SAFE_FREE(state->data);
3169 SAFE_FREE(state->param);
3170 TALLOC_FREE(state);
3171 reply_doserror(req, ERRDOS, ERRnomem);
3172 END_PROFILE(SMBnttrans);
3173 return;
3176 if ((smb_nt_SetupStart + state->setup_count < smb_nt_SetupStart) ||
3177 (smb_nt_SetupStart + state->setup_count < state->setup_count)) {
3178 goto bad_param;
3180 if (smb_nt_SetupStart + state->setup_count > size) {
3181 goto bad_param;
3184 memcpy( state->setup, &req->inbuf[smb_nt_SetupStart],
3185 state->setup_count);
3186 dump_data(10, (uint8 *)state->setup, state->setup_count);
3189 if ((state->received_data == state->total_data) &&
3190 (state->received_param == state->total_param)) {
3191 handle_nttrans(conn, state, req);
3192 SAFE_FREE(state->param);
3193 SAFE_FREE(state->data);
3194 TALLOC_FREE(state);
3195 END_PROFILE(SMBnttrans);
3196 return;
3199 DLIST_ADD(conn->pending_trans, state);
3201 /* We need to send an interim response then receive the rest
3202 of the parameter/data bytes */
3203 reply_outbuf(req, 0, 0);
3204 show_msg((char *)req->outbuf);
3205 END_PROFILE(SMBnttrans);
3206 return;
3208 bad_param:
3210 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3211 SAFE_FREE(state->data);
3212 SAFE_FREE(state->param);
3213 TALLOC_FREE(state);
3214 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3215 END_PROFILE(SMBnttrans);
3216 return;
3219 /****************************************************************************
3220 Reply to a SMBnttranss
3221 ****************************************************************************/
3223 void reply_nttranss(connection_struct *conn, struct smb_request *req)
3225 unsigned int pcnt,poff,dcnt,doff,pdisp,ddisp;
3226 struct trans_state *state;
3228 int size;
3230 START_PROFILE(SMBnttranss);
3232 show_msg((char *)req->inbuf);
3234 if (req->wct < 18) {
3235 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3236 END_PROFILE(SMBnttranss);
3237 return;
3240 for (state = conn->pending_trans; state != NULL;
3241 state = state->next) {
3242 if (state->mid == req->mid) {
3243 break;
3247 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3248 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3249 END_PROFILE(SMBnttranss);
3250 return;
3253 /* Revise state->total_param and state->total_data in case they have
3254 changed downwards */
3255 if (IVAL(req->inbuf, smb_nts_TotalParameterCount)
3256 < state->total_param) {
3257 state->total_param = IVAL(req->inbuf,
3258 smb_nts_TotalParameterCount);
3260 if (IVAL(req->inbuf, smb_nts_TotalDataCount) < state->total_data) {
3261 state->total_data = IVAL(req->inbuf, smb_nts_TotalDataCount);
3264 size = smb_len(req->inbuf) + 4;
3266 pcnt = IVAL(req->inbuf,smb_nts_ParameterCount);
3267 poff = IVAL(req->inbuf, smb_nts_ParameterOffset);
3268 pdisp = IVAL(req->inbuf, smb_nts_ParameterDisplacement);
3270 dcnt = IVAL(req->inbuf, smb_nts_DataCount);
3271 ddisp = IVAL(req->inbuf, smb_nts_DataDisplacement);
3272 doff = IVAL(req->inbuf, smb_nts_DataOffset);
3274 state->received_param += pcnt;
3275 state->received_data += dcnt;
3277 if ((state->received_data > state->total_data) ||
3278 (state->received_param > state->total_param))
3279 goto bad_param;
3281 if (pcnt) {
3282 if (pdisp+pcnt > state->total_param)
3283 goto bad_param;
3284 if ((pdisp+pcnt < pdisp) || (pdisp+pcnt < pcnt))
3285 goto bad_param;
3286 if (pdisp > state->total_param)
3287 goto bad_param;
3288 if ((smb_base(req->inbuf) + poff + pcnt
3289 > (char *)req->inbuf + size) ||
3290 (smb_base(req->inbuf) + poff + pcnt
3291 < smb_base(req->inbuf)))
3292 goto bad_param;
3293 if (state->param + pdisp < state->param)
3294 goto bad_param;
3296 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,
3297 pcnt);
3300 if (dcnt) {
3301 if (ddisp+dcnt > state->total_data)
3302 goto bad_param;
3303 if ((ddisp+dcnt < ddisp) || (ddisp+dcnt < dcnt))
3304 goto bad_param;
3305 if (ddisp > state->total_data)
3306 goto bad_param;
3307 if ((smb_base(req->inbuf) + doff + dcnt
3308 > (char *)req->inbuf + size) ||
3309 (smb_base(req->inbuf) + doff + dcnt
3310 < smb_base(req->inbuf)))
3311 goto bad_param;
3312 if (state->data + ddisp < state->data)
3313 goto bad_param;
3315 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,
3316 dcnt);
3319 if ((state->received_param < state->total_param) ||
3320 (state->received_data < state->total_data)) {
3321 END_PROFILE(SMBnttranss);
3322 return;
3326 * construct_reply_common will copy smb_com from inbuf to
3327 * outbuf. SMBnttranss is wrong here.
3329 SCVAL(req->inbuf,smb_com,SMBnttrans);
3331 handle_nttrans(conn, state, req);
3333 DLIST_REMOVE(conn->pending_trans, state);
3334 SAFE_FREE(state->data);
3335 SAFE_FREE(state->param);
3336 TALLOC_FREE(state);
3337 END_PROFILE(SMBnttranss);
3338 return;
3340 bad_param:
3342 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3343 DLIST_REMOVE(conn->pending_trans, state);
3344 SAFE_FREE(state->data);
3345 SAFE_FREE(state->param);
3346 TALLOC_FREE(state);
3347 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3348 END_PROFILE(SMBnttranss);
3349 return;