Part 2 of fix for bug #8998 - Notify code can miss a ChDir.
[Samba.git] / source3 / smbd / nttrans.c
blob429250ee196d081ad7c519dbad15461c0580ef8f
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"
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "fake_file.h"
26 #include "../libcli/security/security.h"
27 #include "../librpc/gen_ndr/ndr_security.h"
28 #include "passdb/lookup_sid.h"
29 #include "auth.h"
30 #include "ntioctl.h"
31 #include "smbprofile.h"
32 #include "libsmb/libsmb.h"
34 extern const struct generic_mapping file_generic_mapping;
36 static char *nttrans_realloc(char **ptr, size_t size)
38 if (ptr==NULL) {
39 smb_panic("nttrans_realloc() called with NULL ptr");
42 *ptr = (char *)SMB_REALLOC(*ptr, size);
43 if(*ptr == NULL) {
44 return NULL;
46 memset(*ptr,'\0',size);
47 return *ptr;
50 /****************************************************************************
51 Send the required number of replies back.
52 We assume all fields other than the data fields are
53 set correctly for the type of call.
54 HACK ! Always assumes smb_setup field is zero.
55 ****************************************************************************/
57 void send_nt_replies(connection_struct *conn,
58 struct smb_request *req, NTSTATUS nt_error,
59 char *params, int paramsize,
60 char *pdata, int datasize)
62 int data_to_send = datasize;
63 int params_to_send = paramsize;
64 int useable_space;
65 char *pp = params;
66 char *pd = pdata;
67 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
68 int alignment_offset = 1;
69 int data_alignment_offset = 0;
70 struct smbd_server_connection *sconn = req->sconn;
71 int max_send = sconn->smb1.sessions.max_send;
74 * If there genuinely are no parameters or data to send just send
75 * the empty packet.
78 if(params_to_send == 0 && data_to_send == 0) {
79 reply_outbuf(req, 18, 0);
80 if (NT_STATUS_V(nt_error)) {
81 error_packet_set((char *)req->outbuf,
82 0, 0, nt_error,
83 __LINE__,__FILE__);
85 show_msg((char *)req->outbuf);
86 if (!srv_send_smb(sconn,
87 (char *)req->outbuf,
88 true, req->seqnum+1,
89 IS_CONN_ENCRYPTED(conn),
90 &req->pcd)) {
91 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
93 TALLOC_FREE(req->outbuf);
94 return;
98 * When sending params and data ensure that both are nicely aligned.
99 * Only do this alignment when there is also data to send - else
100 * can cause NT redirector problems.
103 if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
104 data_alignment_offset = 4 - (params_to_send % 4);
108 * Space is bufsize minus Netbios over TCP header minus SMB header.
109 * The alignment_offset is to align the param bytes on a four byte
110 * boundary (2 bytes for data len, one byte pad).
111 * NT needs this to work correctly.
114 useable_space = max_send - (smb_size
115 + 2 * 18 /* wct */
116 + alignment_offset
117 + data_alignment_offset);
119 if (useable_space < 0) {
120 char *msg = talloc_asprintf(
121 talloc_tos(),
122 "send_nt_replies failed sanity useable_space = %d!!!",
123 useable_space);
124 DEBUG(0, ("%s\n", msg));
125 exit_server_cleanly(msg);
128 while (params_to_send || data_to_send) {
131 * Calculate whether we will totally or partially fill this packet.
134 total_sent_thistime = params_to_send + data_to_send;
137 * We can never send more than useable_space.
140 total_sent_thistime = MIN(total_sent_thistime, useable_space);
142 reply_outbuf(req, 18,
143 total_sent_thistime + alignment_offset
144 + data_alignment_offset);
147 * We might have had SMBnttranss in req->inbuf, fix that.
149 SCVAL(req->outbuf, smb_com, SMBnttrans);
152 * Set total params and data to be sent.
155 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
156 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
159 * Calculate how many parameters and data we can fit into
160 * this packet. Parameters get precedence.
163 params_sent_thistime = MIN(params_to_send,useable_space);
164 data_sent_thistime = useable_space - params_sent_thistime;
165 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
167 SIVAL(req->outbuf, smb_ntr_ParameterCount,
168 params_sent_thistime);
170 if(params_sent_thistime == 0) {
171 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
172 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
173 } else {
175 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
176 * parameter bytes, however the first 4 bytes of outbuf are
177 * the Netbios over TCP header. Thus use smb_base() to subtract
178 * them from the calculation.
181 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
182 ((smb_buf(req->outbuf)+alignment_offset)
183 - smb_base(req->outbuf)));
185 * Absolute displacement of param bytes sent in this packet.
188 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
189 pp - params);
193 * Deal with the data portion.
196 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
198 if(data_sent_thistime == 0) {
199 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
200 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
201 } else {
203 * The offset of the data bytes is the offset of the
204 * parameter bytes plus the number of parameters being sent this time.
207 SIVAL(req->outbuf, smb_ntr_DataOffset,
208 ((smb_buf(req->outbuf)+alignment_offset) -
209 smb_base(req->outbuf))
210 + params_sent_thistime + data_alignment_offset);
211 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
215 * Copy the param bytes into the packet.
218 if(params_sent_thistime) {
219 if (alignment_offset != 0) {
220 memset(smb_buf(req->outbuf), 0,
221 alignment_offset);
223 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
224 params_sent_thistime);
228 * Copy in the data bytes
231 if(data_sent_thistime) {
232 if (data_alignment_offset != 0) {
233 memset((smb_buf(req->outbuf)+alignment_offset+
234 params_sent_thistime), 0,
235 data_alignment_offset);
237 memcpy(smb_buf(req->outbuf)+alignment_offset
238 +params_sent_thistime+data_alignment_offset,
239 pd,data_sent_thistime);
242 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
243 params_sent_thistime, data_sent_thistime, useable_space));
244 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
245 params_to_send, data_to_send, paramsize, datasize));
247 if (NT_STATUS_V(nt_error)) {
248 error_packet_set((char *)req->outbuf,
249 0, 0, nt_error,
250 __LINE__,__FILE__);
253 /* Send the packet */
254 show_msg((char *)req->outbuf);
255 if (!srv_send_smb(sconn,
256 (char *)req->outbuf,
257 true, req->seqnum+1,
258 IS_CONN_ENCRYPTED(conn),
259 &req->pcd)) {
260 exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
263 TALLOC_FREE(req->outbuf);
265 pp += params_sent_thistime;
266 pd += data_sent_thistime;
268 params_to_send -= params_sent_thistime;
269 data_to_send -= data_sent_thistime;
272 * Sanity check
275 if(params_to_send < 0 || data_to_send < 0) {
276 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
277 params_to_send, data_to_send));
278 exit_server_cleanly("send_nt_replies: internal error");
283 /****************************************************************************
284 Reply to an NT create and X call on a pipe
285 ****************************************************************************/
287 static void nt_open_pipe(char *fname, connection_struct *conn,
288 struct smb_request *req, int *ppnum)
290 files_struct *fsp;
291 NTSTATUS status;
293 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
295 /* Strip \\ off the name. */
296 fname++;
298 status = open_np_file(req, fname, &fsp);
299 if (!NT_STATUS_IS_OK(status)) {
300 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
301 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
302 ERRDOS, ERRbadpipe);
303 return;
305 reply_nterror(req, status);
306 return;
309 *ppnum = fsp->fnum;
310 return;
313 /****************************************************************************
314 Reply to an NT create and X call for pipes.
315 ****************************************************************************/
317 static void do_ntcreate_pipe_open(connection_struct *conn,
318 struct smb_request *req)
320 char *fname = NULL;
321 int pnum = -1;
322 char *p = NULL;
323 uint32 flags = IVAL(req->vwv+3, 1);
324 TALLOC_CTX *ctx = talloc_tos();
326 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
328 if (!fname) {
329 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
330 ERRDOS, ERRbadpipe);
331 return;
333 nt_open_pipe(fname, conn, req, &pnum);
335 if (req->outbuf) {
336 /* error reply */
337 return;
341 * Deal with pipe return.
344 if (flags & EXTENDED_RESPONSE_REQUIRED) {
345 /* This is very strange. We
346 * return 50 words, but only set
347 * the wcnt to 42 ? It's definately
348 * what happens on the wire....
350 reply_outbuf(req, 50, 0);
351 SCVAL(req->outbuf,smb_wct,42);
352 } else {
353 reply_outbuf(req, 34, 0);
356 p = (char *)req->outbuf + smb_vwv2;
357 p++;
358 SSVAL(p,0,pnum);
359 p += 2;
360 SIVAL(p,0,FILE_WAS_OPENED);
361 p += 4;
362 p += 32;
363 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
364 p += 20;
365 /* File type. */
366 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
367 /* Device state. */
368 SSVAL(p,2, 0x5FF); /* ? */
369 p += 4;
371 if (flags & EXTENDED_RESPONSE_REQUIRED) {
372 p += 25;
373 SIVAL(p,0,FILE_GENERIC_ALL);
375 * For pipes W2K3 seems to return
376 * 0x12019B next.
377 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
379 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
382 DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
384 chain_reply(req);
387 struct case_semantics_state {
388 connection_struct *conn;
389 bool case_sensitive;
390 bool case_preserve;
391 bool short_case_preserve;
394 /****************************************************************************
395 Restore case semantics.
396 ****************************************************************************/
398 static int restore_case_semantics(struct case_semantics_state *state)
400 state->conn->case_sensitive = state->case_sensitive;
401 state->conn->case_preserve = state->case_preserve;
402 state->conn->short_case_preserve = state->short_case_preserve;
403 return 0;
406 /****************************************************************************
407 Save case semantics.
408 ****************************************************************************/
410 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
411 connection_struct *conn)
413 struct case_semantics_state *result;
415 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
416 return NULL;
419 result->conn = conn;
420 result->case_sensitive = conn->case_sensitive;
421 result->case_preserve = conn->case_preserve;
422 result->short_case_preserve = conn->short_case_preserve;
424 /* Set to POSIX. */
425 conn->case_sensitive = True;
426 conn->case_preserve = True;
427 conn->short_case_preserve = True;
429 talloc_set_destructor(result, restore_case_semantics);
431 return result;
434 /****************************************************************************
435 Reply to an NT create and X call.
436 ****************************************************************************/
438 void reply_ntcreate_and_X(struct smb_request *req)
440 connection_struct *conn = req->conn;
441 struct smb_filename *smb_fname = NULL;
442 char *fname = NULL;
443 uint32 flags;
444 uint32 access_mask;
445 uint32 file_attributes;
446 uint32 share_access;
447 uint32 create_disposition;
448 uint32 create_options;
449 uint16 root_dir_fid;
450 uint64_t allocation_size;
451 /* Breakout the oplock request bits so we can set the
452 reply bits separately. */
453 uint32 fattr=0;
454 SMB_OFF_T file_len = 0;
455 int info = 0;
456 files_struct *fsp = NULL;
457 char *p = NULL;
458 struct timespec create_timespec;
459 struct timespec c_timespec;
460 struct timespec a_timespec;
461 struct timespec m_timespec;
462 struct timespec write_time_ts;
463 NTSTATUS status;
464 int oplock_request;
465 uint8_t oplock_granted = NO_OPLOCK_RETURN;
466 struct case_semantics_state *case_state = NULL;
467 TALLOC_CTX *ctx = talloc_tos();
469 START_PROFILE(SMBntcreateX);
471 if (req->wct < 24) {
472 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
473 goto out;
476 flags = IVAL(req->vwv+3, 1);
477 access_mask = IVAL(req->vwv+7, 1);
478 file_attributes = IVAL(req->vwv+13, 1);
479 share_access = IVAL(req->vwv+15, 1);
480 create_disposition = IVAL(req->vwv+17, 1);
481 create_options = IVAL(req->vwv+19, 1);
482 root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
484 allocation_size = BVAL(req->vwv+9, 1);
486 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
487 STR_TERMINATE, &status);
489 if (!NT_STATUS_IS_OK(status)) {
490 reply_nterror(req, status);
491 goto out;
494 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
495 "file_attributes = 0x%x, share_access = 0x%x, "
496 "create_disposition = 0x%x create_options = 0x%x "
497 "root_dir_fid = 0x%x, fname = %s\n",
498 (unsigned int)flags,
499 (unsigned int)access_mask,
500 (unsigned int)file_attributes,
501 (unsigned int)share_access,
502 (unsigned int)create_disposition,
503 (unsigned int)create_options,
504 (unsigned int)root_dir_fid,
505 fname));
508 * we need to remove ignored bits when they come directly from the client
509 * because we reuse some of them for internal stuff
511 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
514 * If it's an IPC, use the pipe handler.
517 if (IS_IPC(conn)) {
518 if (lp_nt_pipe_support()) {
519 do_ntcreate_pipe_open(conn, req);
520 goto out;
522 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
523 goto out;
526 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
527 if (oplock_request) {
528 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
529 ? BATCH_OPLOCK : 0;
532 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
533 case_state = set_posix_case_semantics(ctx, conn);
534 if (!case_state) {
535 reply_nterror(req, NT_STATUS_NO_MEMORY);
536 goto out;
540 status = filename_convert(ctx,
541 conn,
542 req->flags2 & FLAGS2_DFS_PATHNAMES,
543 fname,
545 NULL,
546 &smb_fname);
548 TALLOC_FREE(case_state);
550 if (!NT_STATUS_IS_OK(status)) {
551 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
552 reply_botherror(req,
553 NT_STATUS_PATH_NOT_COVERED,
554 ERRSRV, ERRbadpath);
555 goto out;
557 reply_nterror(req, status);
558 goto out;
562 * Bug #6898 - clients using Windows opens should
563 * never be able to set this attribute into the
564 * VFS.
566 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
568 status = SMB_VFS_CREATE_FILE(
569 conn, /* conn */
570 req, /* req */
571 root_dir_fid, /* root_dir_fid */
572 smb_fname, /* fname */
573 access_mask, /* access_mask */
574 share_access, /* share_access */
575 create_disposition, /* create_disposition*/
576 create_options, /* create_options */
577 file_attributes, /* file_attributes */
578 oplock_request, /* oplock_request */
579 allocation_size, /* allocation_size */
580 0, /* private_flags */
581 NULL, /* sd */
582 NULL, /* ea_list */
583 &fsp, /* result */
584 &info); /* pinfo */
586 if (!NT_STATUS_IS_OK(status)) {
587 if (open_was_deferred(req->mid)) {
588 /* We have re-scheduled this call, no error. */
589 goto out;
591 reply_openerror(req, status);
592 goto out;
595 /* Ensure we're pointing at the correct stat struct. */
596 TALLOC_FREE(smb_fname);
597 smb_fname = fsp->fsp_name;
600 * If the caller set the extended oplock request bit
601 * and we granted one (by whatever means) - set the
602 * correct bit for extended oplock reply.
605 if (oplock_request &&
606 (lp_fake_oplocks(SNUM(conn))
607 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
610 * Exclusive oplock granted
613 if (flags & REQUEST_BATCH_OPLOCK) {
614 oplock_granted = BATCH_OPLOCK_RETURN;
615 } else {
616 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
618 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
619 oplock_granted = LEVEL_II_OPLOCK_RETURN;
620 } else {
621 oplock_granted = NO_OPLOCK_RETURN;
624 file_len = smb_fname->st.st_ex_size;
626 if (flags & EXTENDED_RESPONSE_REQUIRED) {
627 /* This is very strange. We
628 * return 50 words, but only set
629 * the wcnt to 42 ? It's definately
630 * what happens on the wire....
632 reply_outbuf(req, 50, 0);
633 SCVAL(req->outbuf,smb_wct,42);
634 } else {
635 reply_outbuf(req, 34, 0);
638 p = (char *)req->outbuf + smb_vwv2;
640 SCVAL(p, 0, oplock_granted);
642 p++;
643 SSVAL(p,0,fsp->fnum);
644 p += 2;
645 if ((create_disposition == FILE_SUPERSEDE)
646 && (info == FILE_WAS_OVERWRITTEN)) {
647 SIVAL(p,0,FILE_WAS_SUPERSEDED);
648 } else {
649 SIVAL(p,0,info);
651 p += 4;
653 fattr = dos_mode(conn, smb_fname);
654 if (fattr == 0) {
655 fattr = FILE_ATTRIBUTE_NORMAL;
658 /* Deal with other possible opens having a modified
659 write time. JRA. */
660 ZERO_STRUCT(write_time_ts);
661 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
662 if (!null_timespec(write_time_ts)) {
663 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
666 /* Create time. */
667 create_timespec = get_create_timespec(conn, fsp, smb_fname);
668 a_timespec = smb_fname->st.st_ex_atime;
669 m_timespec = smb_fname->st.st_ex_mtime;
670 c_timespec = get_change_timespec(conn, fsp, smb_fname);
672 if (lp_dos_filetime_resolution(SNUM(conn))) {
673 dos_filetime_timespec(&create_timespec);
674 dos_filetime_timespec(&a_timespec);
675 dos_filetime_timespec(&m_timespec);
676 dos_filetime_timespec(&c_timespec);
679 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
680 p += 8;
681 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
682 p += 8;
683 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
684 p += 8;
685 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
686 p += 8;
687 SIVAL(p,0,fattr); /* File Attributes. */
688 p += 4;
689 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
690 p += 8;
691 SOFF_T(p,0,file_len);
692 p += 8;
693 if (flags & EXTENDED_RESPONSE_REQUIRED) {
694 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
695 size_t num_names = 0;
696 unsigned int num_streams = 0;
697 struct stream_struct *streams = NULL;
699 /* Do we have any EA's ? */
700 status = get_ea_names_from_file(ctx, conn, fsp,
701 smb_fname->base_name, NULL, &num_names);
702 if (NT_STATUS_IS_OK(status) && num_names) {
703 file_status &= ~NO_EAS;
705 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
706 &num_streams, &streams);
707 /* There is always one stream, ::$DATA. */
708 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
709 file_status &= ~NO_SUBSTREAMS;
711 TALLOC_FREE(streams);
712 SSVAL(p,2,file_status);
714 p += 4;
715 SCVAL(p,0,fsp->is_directory ? 1 : 0);
717 if (flags & EXTENDED_RESPONSE_REQUIRED) {
718 uint32 perms = 0;
719 p += 25;
720 if (fsp->is_directory ||
721 can_write_to_file(conn, smb_fname)) {
722 perms = FILE_GENERIC_ALL;
723 } else {
724 perms = FILE_GENERIC_READ|FILE_EXECUTE;
726 SIVAL(p,0,perms);
729 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
730 fsp->fnum, smb_fname_str_dbg(smb_fname)));
732 chain_reply(req);
733 out:
734 END_PROFILE(SMBntcreateX);
735 return;
738 /****************************************************************************
739 Reply to a NT_TRANSACT_CREATE call to open a pipe.
740 ****************************************************************************/
742 static void do_nt_transact_create_pipe(connection_struct *conn,
743 struct smb_request *req,
744 uint16 **ppsetup, uint32 setup_count,
745 char **ppparams, uint32 parameter_count,
746 char **ppdata, uint32 data_count)
748 char *fname = NULL;
749 char *params = *ppparams;
750 int pnum = -1;
751 char *p = NULL;
752 NTSTATUS status;
753 size_t param_len;
754 uint32 flags;
755 TALLOC_CTX *ctx = talloc_tos();
758 * Ensure minimum number of parameters sent.
761 if(parameter_count < 54) {
762 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
763 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
764 return;
767 flags = IVAL(params,0);
769 srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
770 parameter_count-53, STR_TERMINATE,
771 &status);
772 if (!NT_STATUS_IS_OK(status)) {
773 reply_nterror(req, status);
774 return;
777 nt_open_pipe(fname, conn, req, &pnum);
779 if (req->outbuf) {
780 /* Error return */
781 return;
784 /* Realloc the size of parameters and data we will return */
785 if (flags & EXTENDED_RESPONSE_REQUIRED) {
786 /* Extended response is 32 more byyes. */
787 param_len = 101;
788 } else {
789 param_len = 69;
791 params = nttrans_realloc(ppparams, param_len);
792 if(params == NULL) {
793 reply_nterror(req, NT_STATUS_NO_MEMORY);
794 return;
797 p = params;
798 SCVAL(p,0,NO_OPLOCK_RETURN);
800 p += 2;
801 SSVAL(p,0,pnum);
802 p += 2;
803 SIVAL(p,0,FILE_WAS_OPENED);
804 p += 8;
806 p += 32;
807 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
808 p += 20;
809 /* File type. */
810 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
811 /* Device state. */
812 SSVAL(p,2, 0x5FF); /* ? */
813 p += 4;
815 if (flags & EXTENDED_RESPONSE_REQUIRED) {
816 p += 25;
817 SIVAL(p,0,FILE_GENERIC_ALL);
819 * For pipes W2K3 seems to return
820 * 0x12019B next.
821 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
823 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
826 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
828 /* Send the required number of replies */
829 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
831 return;
834 /****************************************************************************
835 Internal fn to set security descriptors.
836 ****************************************************************************/
838 NTSTATUS set_sd(files_struct *fsp, uint8_t *data, uint32_t sd_len,
839 uint32_t security_info_sent)
841 struct security_descriptor *psd = NULL;
842 NTSTATUS status;
844 if (sd_len == 0) {
845 return NT_STATUS_INVALID_PARAMETER;
848 if (!CAN_WRITE(fsp->conn)) {
849 return NT_STATUS_ACCESS_DENIED;
852 if (!lp_nt_acl_support(SNUM(fsp->conn))) {
853 return NT_STATUS_OK;
856 status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
858 if (!NT_STATUS_IS_OK(status)) {
859 return status;
862 if (psd->owner_sid == NULL) {
863 security_info_sent &= ~SECINFO_OWNER;
865 if (psd->group_sid == NULL) {
866 security_info_sent &= ~SECINFO_GROUP;
869 /* Ensure we have at least one thing set. */
870 if ((security_info_sent & (SECINFO_OWNER|SECINFO_GROUP|SECINFO_DACL|SECINFO_SACL)) == 0) {
871 if (security_info_sent & SECINFO_LABEL) {
872 /* Only consider SECINFO_LABEL if no other
873 bits are set. Just like W2K3 we don't
874 store this. */
875 return NT_STATUS_OK;
877 return NT_STATUS_INVALID_PARAMETER;
880 /* Ensure we have the rights to do this. */
881 if (security_info_sent & SECINFO_OWNER) {
882 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
883 return NT_STATUS_ACCESS_DENIED;
887 if (security_info_sent & SECINFO_GROUP) {
888 if (!(fsp->access_mask & SEC_STD_WRITE_OWNER)) {
889 return NT_STATUS_ACCESS_DENIED;
893 if (security_info_sent & SECINFO_DACL) {
894 if (!(fsp->access_mask & SEC_STD_WRITE_DAC)) {
895 return NT_STATUS_ACCESS_DENIED;
897 /* Convert all the generic bits. */
898 if (psd->dacl) {
899 security_acl_map_generic(psd->dacl, &file_generic_mapping);
903 if (security_info_sent & SECINFO_SACL) {
904 if (!(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
905 return NT_STATUS_ACCESS_DENIED;
907 /* Convert all the generic bits. */
908 if (psd->sacl) {
909 security_acl_map_generic(psd->sacl, &file_generic_mapping);
913 if (DEBUGLEVEL >= 10) {
914 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
915 NDR_PRINT_DEBUG(security_descriptor, psd);
918 status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
920 TALLOC_FREE(psd);
922 return status;
925 /****************************************************************************
926 Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
927 ****************************************************************************/
929 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
931 struct ea_list *ea_list_head = NULL;
932 size_t offset = 0;
934 if (data_size < 4) {
935 return NULL;
938 while (offset + 4 <= data_size) {
939 size_t next_offset = IVAL(pdata,offset);
940 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
942 if (!eal) {
943 return NULL;
946 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
947 if (next_offset == 0) {
948 break;
950 offset += next_offset;
953 return ea_list_head;
956 /****************************************************************************
957 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
958 ****************************************************************************/
960 static void call_nt_transact_create(connection_struct *conn,
961 struct smb_request *req,
962 uint16 **ppsetup, uint32 setup_count,
963 char **ppparams, uint32 parameter_count,
964 char **ppdata, uint32 data_count,
965 uint32 max_data_count)
967 struct smb_filename *smb_fname = NULL;
968 char *fname = NULL;
969 char *params = *ppparams;
970 char *data = *ppdata;
971 /* Breakout the oplock request bits so we can set the reply bits separately. */
972 uint32 fattr=0;
973 SMB_OFF_T file_len = 0;
974 int info = 0;
975 files_struct *fsp = NULL;
976 char *p = NULL;
977 uint32 flags;
978 uint32 access_mask;
979 uint32 file_attributes;
980 uint32 share_access;
981 uint32 create_disposition;
982 uint32 create_options;
983 uint32 sd_len;
984 struct security_descriptor *sd = NULL;
985 uint32 ea_len;
986 uint16 root_dir_fid;
987 struct timespec create_timespec;
988 struct timespec c_timespec;
989 struct timespec a_timespec;
990 struct timespec m_timespec;
991 struct timespec write_time_ts;
992 struct ea_list *ea_list = NULL;
993 NTSTATUS status;
994 size_t param_len;
995 uint64_t allocation_size;
996 int oplock_request;
997 uint8_t oplock_granted;
998 struct case_semantics_state *case_state = NULL;
999 TALLOC_CTX *ctx = talloc_tos();
1001 DEBUG(5,("call_nt_transact_create\n"));
1004 * If it's an IPC, use the pipe handler.
1007 if (IS_IPC(conn)) {
1008 if (lp_nt_pipe_support()) {
1009 do_nt_transact_create_pipe(
1010 conn, req,
1011 ppsetup, setup_count,
1012 ppparams, parameter_count,
1013 ppdata, data_count);
1014 goto out;
1016 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1017 goto out;
1021 * Ensure minimum number of parameters sent.
1024 if(parameter_count < 54) {
1025 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1026 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1027 goto out;
1030 flags = IVAL(params,0);
1031 access_mask = IVAL(params,8);
1032 file_attributes = IVAL(params,20);
1033 share_access = IVAL(params,24);
1034 create_disposition = IVAL(params,28);
1035 create_options = IVAL(params,32);
1036 sd_len = IVAL(params,36);
1037 ea_len = IVAL(params,40);
1038 root_dir_fid = (uint16)IVAL(params,4);
1039 allocation_size = BVAL(params,12);
1042 * we need to remove ignored bits when they come directly from the client
1043 * because we reuse some of them for internal stuff
1045 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1047 /* Ensure the data_len is correct for the sd and ea values given. */
1048 if ((ea_len + sd_len > data_count)
1049 || (ea_len > data_count) || (sd_len > data_count)
1050 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1051 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1052 "%u, data_count = %u\n", (unsigned int)ea_len,
1053 (unsigned int)sd_len, (unsigned int)data_count));
1054 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1055 goto out;
1058 if (sd_len) {
1059 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1060 sd_len));
1062 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1063 &sd);
1064 if (!NT_STATUS_IS_OK(status)) {
1065 DEBUG(10, ("call_nt_transact_create: "
1066 "unmarshall_sec_desc failed: %s\n",
1067 nt_errstr(status)));
1068 reply_nterror(req, status);
1069 goto out;
1073 if (ea_len) {
1074 if (!lp_ea_support(SNUM(conn))) {
1075 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1076 "EA's not supported.\n",
1077 (unsigned int)ea_len));
1078 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1079 goto out;
1082 if (ea_len < 10) {
1083 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1084 "too small (should be more than 10)\n",
1085 (unsigned int)ea_len ));
1086 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1087 goto out;
1090 /* We have already checked that ea_len <= data_count here. */
1091 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1092 ea_len);
1093 if (ea_list == NULL) {
1094 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1095 goto out;
1099 srvstr_get_path(ctx, params, req->flags2, &fname,
1100 params+53, parameter_count-53,
1101 STR_TERMINATE, &status);
1102 if (!NT_STATUS_IS_OK(status)) {
1103 reply_nterror(req, status);
1104 goto out;
1107 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1108 case_state = set_posix_case_semantics(ctx, conn);
1109 if (!case_state) {
1110 reply_nterror(req, NT_STATUS_NO_MEMORY);
1111 goto out;
1115 status = filename_convert(ctx,
1116 conn,
1117 req->flags2 & FLAGS2_DFS_PATHNAMES,
1118 fname,
1120 NULL,
1121 &smb_fname);
1123 TALLOC_FREE(case_state);
1125 if (!NT_STATUS_IS_OK(status)) {
1126 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1127 reply_botherror(req,
1128 NT_STATUS_PATH_NOT_COVERED,
1129 ERRSRV, ERRbadpath);
1130 goto out;
1132 reply_nterror(req, status);
1133 goto out;
1136 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1137 if (oplock_request) {
1138 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1139 ? BATCH_OPLOCK : 0;
1143 * Bug #6898 - clients using Windows opens should
1144 * never be able to set this attribute into the
1145 * VFS.
1147 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1149 status = SMB_VFS_CREATE_FILE(
1150 conn, /* conn */
1151 req, /* req */
1152 root_dir_fid, /* root_dir_fid */
1153 smb_fname, /* fname */
1154 access_mask, /* access_mask */
1155 share_access, /* share_access */
1156 create_disposition, /* create_disposition*/
1157 create_options, /* create_options */
1158 file_attributes, /* file_attributes */
1159 oplock_request, /* oplock_request */
1160 allocation_size, /* allocation_size */
1161 0, /* private_flags */
1162 sd, /* sd */
1163 ea_list, /* ea_list */
1164 &fsp, /* result */
1165 &info); /* pinfo */
1167 if(!NT_STATUS_IS_OK(status)) {
1168 if (open_was_deferred(req->mid)) {
1169 /* We have re-scheduled this call, no error. */
1170 return;
1172 reply_openerror(req, status);
1173 goto out;
1176 /* Ensure we're pointing at the correct stat struct. */
1177 TALLOC_FREE(smb_fname);
1178 smb_fname = fsp->fsp_name;
1181 * If the caller set the extended oplock request bit
1182 * and we granted one (by whatever means) - set the
1183 * correct bit for extended oplock reply.
1186 if (oplock_request &&
1187 (lp_fake_oplocks(SNUM(conn))
1188 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1191 * Exclusive oplock granted
1194 if (flags & REQUEST_BATCH_OPLOCK) {
1195 oplock_granted = BATCH_OPLOCK_RETURN;
1196 } else {
1197 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1199 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1200 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1201 } else {
1202 oplock_granted = NO_OPLOCK_RETURN;
1205 file_len = smb_fname->st.st_ex_size;
1207 /* Realloc the size of parameters and data we will return */
1208 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1209 /* Extended response is 32 more byyes. */
1210 param_len = 101;
1211 } else {
1212 param_len = 69;
1214 params = nttrans_realloc(ppparams, param_len);
1215 if(params == NULL) {
1216 reply_nterror(req, NT_STATUS_NO_MEMORY);
1217 goto out;
1220 p = params;
1221 SCVAL(p, 0, oplock_granted);
1223 p += 2;
1224 SSVAL(p,0,fsp->fnum);
1225 p += 2;
1226 if ((create_disposition == FILE_SUPERSEDE)
1227 && (info == FILE_WAS_OVERWRITTEN)) {
1228 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1229 } else {
1230 SIVAL(p,0,info);
1232 p += 8;
1234 fattr = dos_mode(conn, smb_fname);
1235 if (fattr == 0) {
1236 fattr = FILE_ATTRIBUTE_NORMAL;
1239 /* Deal with other possible opens having a modified
1240 write time. JRA. */
1241 ZERO_STRUCT(write_time_ts);
1242 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
1243 if (!null_timespec(write_time_ts)) {
1244 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1247 /* Create time. */
1248 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1249 a_timespec = smb_fname->st.st_ex_atime;
1250 m_timespec = smb_fname->st.st_ex_mtime;
1251 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1253 if (lp_dos_filetime_resolution(SNUM(conn))) {
1254 dos_filetime_timespec(&create_timespec);
1255 dos_filetime_timespec(&a_timespec);
1256 dos_filetime_timespec(&m_timespec);
1257 dos_filetime_timespec(&c_timespec);
1260 put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1261 p += 8;
1262 put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1263 p += 8;
1264 put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1265 p += 8;
1266 put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1267 p += 8;
1268 SIVAL(p,0,fattr); /* File Attributes. */
1269 p += 4;
1270 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1271 p += 8;
1272 SOFF_T(p,0,file_len);
1273 p += 8;
1274 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1275 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1276 size_t num_names = 0;
1277 unsigned int num_streams = 0;
1278 struct stream_struct *streams = NULL;
1280 /* Do we have any EA's ? */
1281 status = get_ea_names_from_file(ctx, conn, fsp,
1282 smb_fname->base_name, NULL, &num_names);
1283 if (NT_STATUS_IS_OK(status) && num_names) {
1284 file_status &= ~NO_EAS;
1286 status = vfs_streaminfo(conn, NULL, smb_fname->base_name, ctx,
1287 &num_streams, &streams);
1288 /* There is always one stream, ::$DATA. */
1289 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1290 file_status &= ~NO_SUBSTREAMS;
1292 TALLOC_FREE(streams);
1293 SSVAL(p,2,file_status);
1295 p += 4;
1296 SCVAL(p,0,fsp->is_directory ? 1 : 0);
1298 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1299 uint32 perms = 0;
1300 p += 25;
1301 if (fsp->is_directory ||
1302 can_write_to_file(conn, smb_fname)) {
1303 perms = FILE_GENERIC_ALL;
1304 } else {
1305 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1307 SIVAL(p,0,perms);
1310 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1311 smb_fname_str_dbg(smb_fname)));
1313 /* Send the required number of replies */
1314 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1315 out:
1316 return;
1319 /****************************************************************************
1320 Reply to a NT CANCEL request.
1321 conn POINTER CAN BE NULL HERE !
1322 ****************************************************************************/
1324 void reply_ntcancel(struct smb_request *req)
1327 * Go through and cancel any pending change notifies.
1330 START_PROFILE(SMBntcancel);
1331 srv_cancel_sign_response(req->sconn);
1332 remove_pending_change_notify_requests_by_mid(req->sconn, req->mid);
1333 remove_pending_lock_requests_by_mid_smb1(req->sconn, req->mid);
1335 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1336 (unsigned long long)req->mid));
1338 END_PROFILE(SMBntcancel);
1339 return;
1342 /****************************************************************************
1343 Copy a file.
1344 ****************************************************************************/
1346 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1347 connection_struct *conn,
1348 struct smb_request *req,
1349 struct smb_filename *smb_fname_src,
1350 struct smb_filename *smb_fname_dst,
1351 uint32 attrs)
1353 files_struct *fsp1,*fsp2;
1354 uint32 fattr;
1355 int info;
1356 SMB_OFF_T ret=-1;
1357 NTSTATUS status = NT_STATUS_OK;
1358 char *parent;
1360 if (!CAN_WRITE(conn)) {
1361 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1362 goto out;
1365 /* Source must already exist. */
1366 if (!VALID_STAT(smb_fname_src->st)) {
1367 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1368 goto out;
1371 /* Ensure attributes match. */
1372 fattr = dos_mode(conn, smb_fname_src);
1373 if ((fattr & ~attrs) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
1374 status = NT_STATUS_NO_SUCH_FILE;
1375 goto out;
1378 /* Disallow if dst file already exists. */
1379 if (VALID_STAT(smb_fname_dst->st)) {
1380 status = NT_STATUS_OBJECT_NAME_COLLISION;
1381 goto out;
1384 /* No links from a directory. */
1385 if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1386 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1387 goto out;
1390 DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1391 smb_fname_str_dbg(smb_fname_src),
1392 smb_fname_str_dbg(smb_fname_dst)));
1394 status = SMB_VFS_CREATE_FILE(
1395 conn, /* conn */
1396 req, /* req */
1397 0, /* root_dir_fid */
1398 smb_fname_src, /* fname */
1399 FILE_READ_DATA|FILE_READ_ATTRIBUTES|
1400 FILE_READ_EA, /* access_mask */
1401 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1402 FILE_SHARE_DELETE),
1403 FILE_OPEN, /* create_disposition*/
1404 0, /* create_options */
1405 FILE_ATTRIBUTE_NORMAL, /* file_attributes */
1406 NO_OPLOCK, /* oplock_request */
1407 0, /* allocation_size */
1408 0, /* private_flags */
1409 NULL, /* sd */
1410 NULL, /* ea_list */
1411 &fsp1, /* result */
1412 &info); /* pinfo */
1414 if (!NT_STATUS_IS_OK(status)) {
1415 goto out;
1418 status = SMB_VFS_CREATE_FILE(
1419 conn, /* conn */
1420 req, /* req */
1421 0, /* root_dir_fid */
1422 smb_fname_dst, /* fname */
1423 FILE_WRITE_DATA|FILE_WRITE_ATTRIBUTES|
1424 FILE_WRITE_EA, /* access_mask */
1425 (FILE_SHARE_READ | FILE_SHARE_WRITE | /* share_access */
1426 FILE_SHARE_DELETE),
1427 FILE_CREATE, /* create_disposition*/
1428 0, /* create_options */
1429 fattr, /* file_attributes */
1430 NO_OPLOCK, /* oplock_request */
1431 0, /* allocation_size */
1432 0, /* private_flags */
1433 NULL, /* sd */
1434 NULL, /* ea_list */
1435 &fsp2, /* result */
1436 &info); /* pinfo */
1438 if (!NT_STATUS_IS_OK(status)) {
1439 close_file(NULL, fsp1, ERROR_CLOSE);
1440 goto out;
1443 if (smb_fname_src->st.st_ex_size) {
1444 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1448 * As we are opening fsp1 read-only we only expect
1449 * an error on close on fsp2 if we are out of space.
1450 * Thus we don't look at the error return from the
1451 * close of fsp1.
1453 close_file(NULL, fsp1, NORMAL_CLOSE);
1455 /* Ensure the modtime is set correctly on the destination file. */
1456 set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1458 status = close_file(NULL, fsp2, NORMAL_CLOSE);
1460 /* Grrr. We have to do this as open_file_ntcreate adds FILE_ATTRIBUTE_ARCHIVE when it
1461 creates the file. This isn't the correct thing to do in the copy
1462 case. JRA */
1463 if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1464 NULL)) {
1465 status = NT_STATUS_NO_MEMORY;
1466 goto out;
1468 file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1469 TALLOC_FREE(parent);
1471 if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1472 status = NT_STATUS_DISK_FULL;
1473 goto out;
1475 out:
1476 if (!NT_STATUS_IS_OK(status)) {
1477 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1478 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1479 smb_fname_str_dbg(smb_fname_dst)));
1482 return status;
1485 /****************************************************************************
1486 Reply to a NT rename request.
1487 ****************************************************************************/
1489 void reply_ntrename(struct smb_request *req)
1491 connection_struct *conn = req->conn;
1492 struct smb_filename *smb_fname_old = NULL;
1493 struct smb_filename *smb_fname_new = NULL;
1494 char *oldname = NULL;
1495 char *newname = NULL;
1496 const char *p;
1497 NTSTATUS status;
1498 bool src_has_wcard = False;
1499 bool dest_has_wcard = False;
1500 uint32 attrs;
1501 uint32_t ucf_flags_src = 0;
1502 uint32_t ucf_flags_dst = 0;
1503 uint16 rename_type;
1504 TALLOC_CTX *ctx = talloc_tos();
1505 bool stream_rename = false;
1507 START_PROFILE(SMBntrename);
1509 if (req->wct < 4) {
1510 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1511 goto out;
1514 attrs = SVAL(req->vwv+0, 0);
1515 rename_type = SVAL(req->vwv+1, 0);
1517 p = (const char *)req->buf + 1;
1518 p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1519 &status, &src_has_wcard);
1520 if (!NT_STATUS_IS_OK(status)) {
1521 reply_nterror(req, status);
1522 goto out;
1525 if (ms_has_wild(oldname)) {
1526 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1527 goto out;
1530 p++;
1531 p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1532 &status, &dest_has_wcard);
1533 if (!NT_STATUS_IS_OK(status)) {
1534 reply_nterror(req, status);
1535 goto out;
1538 if (!lp_posix_pathnames()) {
1539 /* The newname must begin with a ':' if the
1540 oldname contains a ':'. */
1541 if (strchr_m(oldname, ':')) {
1542 if (newname[0] != ':') {
1543 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1544 goto out;
1546 stream_rename = true;
1551 * If this is a rename operation, allow wildcards and save the
1552 * destination's last component.
1554 if (rename_type == RENAME_FLAG_RENAME) {
1555 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1556 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1559 /* rename_internals() calls unix_convert(), so don't call it here. */
1560 status = filename_convert(ctx, conn,
1561 req->flags2 & FLAGS2_DFS_PATHNAMES,
1562 oldname,
1563 ucf_flags_src,
1564 NULL,
1565 &smb_fname_old);
1566 if (!NT_STATUS_IS_OK(status)) {
1567 if (NT_STATUS_EQUAL(status,
1568 NT_STATUS_PATH_NOT_COVERED)) {
1569 reply_botherror(req,
1570 NT_STATUS_PATH_NOT_COVERED,
1571 ERRSRV, ERRbadpath);
1572 goto out;
1574 reply_nterror(req, status);
1575 goto out;
1578 status = filename_convert(ctx, conn,
1579 req->flags2 & FLAGS2_DFS_PATHNAMES,
1580 newname,
1581 ucf_flags_dst,
1582 &dest_has_wcard,
1583 &smb_fname_new);
1584 if (!NT_STATUS_IS_OK(status)) {
1585 if (NT_STATUS_EQUAL(status,
1586 NT_STATUS_PATH_NOT_COVERED)) {
1587 reply_botherror(req,
1588 NT_STATUS_PATH_NOT_COVERED,
1589 ERRSRV, ERRbadpath);
1590 goto out;
1592 reply_nterror(req, status);
1593 goto out;
1596 if (stream_rename) {
1597 /* smb_fname_new must be the same as smb_fname_old. */
1598 TALLOC_FREE(smb_fname_new->base_name);
1599 smb_fname_new->base_name = talloc_strdup(smb_fname_new,
1600 smb_fname_old->base_name);
1601 if (!smb_fname_new->base_name) {
1602 reply_nterror(req, NT_STATUS_NO_MEMORY);
1603 goto out;
1607 DEBUG(3,("reply_ntrename: %s -> %s\n",
1608 smb_fname_str_dbg(smb_fname_old),
1609 smb_fname_str_dbg(smb_fname_new)));
1611 switch(rename_type) {
1612 case RENAME_FLAG_RENAME:
1613 status = rename_internals(ctx, conn, req,
1614 smb_fname_old, smb_fname_new,
1615 attrs, False, src_has_wcard,
1616 dest_has_wcard,
1617 DELETE_ACCESS);
1618 break;
1619 case RENAME_FLAG_HARD_LINK:
1620 if (src_has_wcard || dest_has_wcard) {
1621 /* No wildcards. */
1622 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1623 } else {
1624 status = hardlink_internals(ctx, conn,
1625 req,
1626 false,
1627 smb_fname_old,
1628 smb_fname_new);
1630 break;
1631 case RENAME_FLAG_COPY:
1632 if (src_has_wcard || dest_has_wcard) {
1633 /* No wildcards. */
1634 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1635 } else {
1636 status = copy_internals(ctx, conn, req,
1637 smb_fname_old,
1638 smb_fname_new,
1639 attrs);
1641 break;
1642 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1643 status = NT_STATUS_INVALID_PARAMETER;
1644 break;
1645 default:
1646 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1647 break;
1650 if (!NT_STATUS_IS_OK(status)) {
1651 if (open_was_deferred(req->mid)) {
1652 /* We have re-scheduled this call. */
1653 goto out;
1656 reply_nterror(req, status);
1657 goto out;
1660 reply_outbuf(req, 0, 0);
1661 out:
1662 END_PROFILE(SMBntrename);
1663 return;
1666 /****************************************************************************
1667 Reply to a notify change - queue the request and
1668 don't allow a directory to be opened.
1669 ****************************************************************************/
1671 static void smbd_smb1_notify_reply(struct smb_request *req,
1672 NTSTATUS error_code,
1673 uint8_t *buf, size_t len)
1675 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1678 static void call_nt_transact_notify_change(connection_struct *conn,
1679 struct smb_request *req,
1680 uint16 **ppsetup,
1681 uint32 setup_count,
1682 char **ppparams,
1683 uint32 parameter_count,
1684 char **ppdata, uint32 data_count,
1685 uint32 max_data_count,
1686 uint32 max_param_count)
1688 uint16 *setup = *ppsetup;
1689 files_struct *fsp;
1690 uint32 filter;
1691 NTSTATUS status;
1692 bool recursive;
1694 if(setup_count < 6) {
1695 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1696 return;
1699 fsp = file_fsp(req, SVAL(setup,4));
1700 filter = IVAL(setup, 0);
1701 recursive = (SVAL(setup, 6) != 0) ? True : False;
1703 DEBUG(3,("call_nt_transact_notify_change\n"));
1705 if(!fsp) {
1706 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1707 return;
1711 char *filter_string;
1713 if (!(filter_string = notify_filter_string(NULL, filter))) {
1714 reply_nterror(req,NT_STATUS_NO_MEMORY);
1715 return;
1718 DEBUG(3,("call_nt_transact_notify_change: notify change "
1719 "called on %s, filter = %s, recursive = %d\n",
1720 fsp_str_dbg(fsp), filter_string, recursive));
1722 TALLOC_FREE(filter_string);
1725 if((!fsp->is_directory) || (conn != fsp->conn)) {
1726 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1727 return;
1730 if (fsp->notify == NULL) {
1732 status = change_notify_create(fsp, filter, recursive);
1734 if (!NT_STATUS_IS_OK(status)) {
1735 DEBUG(10, ("change_notify_create returned %s\n",
1736 nt_errstr(status)));
1737 reply_nterror(req, status);
1738 return;
1742 if (fsp->notify->num_changes != 0) {
1745 * We've got changes pending, respond immediately
1749 * TODO: write a torture test to check the filtering behaviour
1750 * here.
1753 change_notify_reply(req,
1754 NT_STATUS_OK,
1755 max_param_count,
1756 fsp->notify,
1757 smbd_smb1_notify_reply);
1760 * change_notify_reply() above has independently sent its
1761 * results
1763 return;
1767 * No changes pending, queue the request
1770 status = change_notify_add_request(req,
1771 max_param_count,
1772 filter,
1773 recursive, fsp,
1774 smbd_smb1_notify_reply);
1775 if (!NT_STATUS_IS_OK(status)) {
1776 reply_nterror(req, status);
1778 return;
1781 /****************************************************************************
1782 Reply to an NT transact rename command.
1783 ****************************************************************************/
1785 static void call_nt_transact_rename(connection_struct *conn,
1786 struct smb_request *req,
1787 uint16 **ppsetup, uint32 setup_count,
1788 char **ppparams, uint32 parameter_count,
1789 char **ppdata, uint32 data_count,
1790 uint32 max_data_count)
1792 char *params = *ppparams;
1793 char *new_name = NULL;
1794 files_struct *fsp = NULL;
1795 bool dest_has_wcard = False;
1796 NTSTATUS status;
1797 TALLOC_CTX *ctx = talloc_tos();
1799 if(parameter_count < 5) {
1800 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1801 return;
1804 fsp = file_fsp(req, SVAL(params, 0));
1805 if (!check_fsp(conn, req, fsp)) {
1806 return;
1808 srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1809 parameter_count - 4,
1810 STR_TERMINATE, &status, &dest_has_wcard);
1811 if (!NT_STATUS_IS_OK(status)) {
1812 reply_nterror(req, status);
1813 return;
1817 * W2K3 ignores this request as the RAW-RENAME test
1818 * demonstrates, so we do.
1820 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1822 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1823 fsp_str_dbg(fsp), new_name));
1825 return;
1828 /******************************************************************************
1829 Fake up a completely empty SD.
1830 *******************************************************************************/
1832 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, struct security_descriptor **ppsd)
1834 size_t sd_size;
1836 *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1837 if(!*ppsd) {
1838 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1839 return NT_STATUS_NO_MEMORY;
1842 return NT_STATUS_OK;
1845 /****************************************************************************
1846 Reply to query a security descriptor.
1847 Callable from SMB2 and SMB2.
1848 If it returns NT_STATUS_BUFFER_TOO_SMALL, pdata_size is initialized with
1849 the required size.
1850 ****************************************************************************/
1852 NTSTATUS smbd_do_query_security_desc(connection_struct *conn,
1853 TALLOC_CTX *mem_ctx,
1854 files_struct *fsp,
1855 uint32_t security_info_wanted,
1856 uint32_t max_data_count,
1857 uint8_t **ppmarshalled_sd,
1858 size_t *psd_size)
1860 NTSTATUS status;
1861 struct security_descriptor *psd = NULL;
1864 * Get the permissions to return.
1867 if ((security_info_wanted & SECINFO_SACL) &&
1868 !(fsp->access_mask & SEC_FLAG_SYSTEM_SECURITY)) {
1869 return NT_STATUS_ACCESS_DENIED;
1872 if ((security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|SECINFO_GROUP)) &&
1873 !(fsp->access_mask & SEC_STD_READ_CONTROL)) {
1874 return NT_STATUS_ACCESS_DENIED;
1877 if (security_info_wanted & (SECINFO_DACL|SECINFO_OWNER|
1878 SECINFO_GROUP|SECINFO_SACL)) {
1879 /* Don't return SECINFO_LABEL if anything else was
1880 requested. See bug #8458. */
1881 security_info_wanted &= ~SECINFO_LABEL;
1884 if (!lp_nt_acl_support(SNUM(conn))) {
1885 status = get_null_nt_acl(mem_ctx, &psd);
1886 } else if (security_info_wanted & SECINFO_LABEL) {
1887 /* Like W2K3 return a null object. */
1888 status = get_null_nt_acl(mem_ctx, &psd);
1889 } else {
1890 status = SMB_VFS_FGET_NT_ACL(
1891 fsp, security_info_wanted, &psd);
1893 if (!NT_STATUS_IS_OK(status)) {
1894 return status;
1897 if (!(security_info_wanted & SECINFO_OWNER)) {
1898 psd->owner_sid = NULL;
1900 if (!(security_info_wanted & SECINFO_GROUP)) {
1901 psd->group_sid = NULL;
1903 if (!(security_info_wanted & SECINFO_DACL)) {
1904 psd->type &= ~SEC_DESC_DACL_PRESENT;
1905 psd->dacl = NULL;
1907 if (!(security_info_wanted & SECINFO_SACL)) {
1908 psd->type &= ~SEC_DESC_SACL_PRESENT;
1909 psd->sacl = NULL;
1912 /* If the SACL/DACL is NULL, but was requested, we mark that it is
1913 * present in the reply to match Windows behavior */
1914 if (psd->sacl == NULL &&
1915 security_info_wanted & SECINFO_SACL)
1916 psd->type |= SEC_DESC_SACL_PRESENT;
1917 if (psd->dacl == NULL &&
1918 security_info_wanted & SECINFO_DACL)
1919 psd->type |= SEC_DESC_DACL_PRESENT;
1921 if (security_info_wanted & SECINFO_LABEL) {
1922 /* Like W2K3 return a null object. */
1923 psd->owner_sid = NULL;
1924 psd->group_sid = NULL;
1925 psd->dacl = NULL;
1926 psd->sacl = NULL;
1927 psd->type &= ~(SEC_DESC_DACL_PRESENT|SEC_DESC_SACL_PRESENT);
1930 *psd_size = ndr_size_security_descriptor(psd, 0);
1932 DEBUG(3,("smbd_do_query_security_desc: sd_size = %lu.\n",
1933 (unsigned long)*psd_size));
1935 if (DEBUGLEVEL >= 10) {
1936 DEBUG(10,("smbd_do_query_security_desc for file %s\n",
1937 fsp_str_dbg(fsp)));
1938 NDR_PRINT_DEBUG(security_descriptor, psd);
1941 if (max_data_count < *psd_size) {
1942 TALLOC_FREE(psd);
1943 return NT_STATUS_BUFFER_TOO_SMALL;
1946 status = marshall_sec_desc(mem_ctx, psd,
1947 ppmarshalled_sd, psd_size);
1949 if (!NT_STATUS_IS_OK(status)) {
1950 TALLOC_FREE(psd);
1951 return status;
1954 TALLOC_FREE(psd);
1955 return NT_STATUS_OK;
1958 /****************************************************************************
1959 SMB1 reply to query a security descriptor.
1960 ****************************************************************************/
1962 static void call_nt_transact_query_security_desc(connection_struct *conn,
1963 struct smb_request *req,
1964 uint16 **ppsetup,
1965 uint32 setup_count,
1966 char **ppparams,
1967 uint32 parameter_count,
1968 char **ppdata,
1969 uint32 data_count,
1970 uint32 max_data_count)
1972 char *params = *ppparams;
1973 char *data = *ppdata;
1974 size_t sd_size = 0;
1975 uint32 security_info_wanted;
1976 files_struct *fsp = NULL;
1977 NTSTATUS status;
1978 uint8_t *marshalled_sd = NULL;
1980 if(parameter_count < 8) {
1981 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1982 return;
1985 fsp = file_fsp(req, SVAL(params,0));
1986 if(!fsp) {
1987 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1988 return;
1991 security_info_wanted = IVAL(params,4);
1993 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1994 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1995 (unsigned int)security_info_wanted));
1997 params = nttrans_realloc(ppparams, 4);
1998 if(params == NULL) {
1999 reply_nterror(req, NT_STATUS_NO_MEMORY);
2000 return;
2004 * Get the permissions to return.
2007 status = smbd_do_query_security_desc(conn,
2008 talloc_tos(),
2009 fsp,
2010 security_info_wanted,
2011 max_data_count,
2012 &marshalled_sd,
2013 &sd_size);
2015 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
2016 SIVAL(params,0,(uint32_t)sd_size);
2017 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2018 params, 4, NULL, 0);
2019 return;
2022 if (!NT_STATUS_IS_OK(status)) {
2023 reply_nterror(req, status);
2024 return;
2027 SMB_ASSERT(sd_size > 0);
2029 SIVAL(params,0,(uint32_t)sd_size);
2031 if (max_data_count < sd_size) {
2032 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
2033 params, 4, NULL, 0);
2034 return;
2038 * Allocate the data we will return.
2041 data = nttrans_realloc(ppdata, sd_size);
2042 if(data == NULL) {
2043 reply_nterror(req, NT_STATUS_NO_MEMORY);
2044 return;
2047 memcpy(data, marshalled_sd, sd_size);
2049 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
2051 return;
2054 /****************************************************************************
2055 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
2056 ****************************************************************************/
2058 static void call_nt_transact_set_security_desc(connection_struct *conn,
2059 struct smb_request *req,
2060 uint16 **ppsetup,
2061 uint32 setup_count,
2062 char **ppparams,
2063 uint32 parameter_count,
2064 char **ppdata,
2065 uint32 data_count,
2066 uint32 max_data_count)
2068 char *params= *ppparams;
2069 char *data = *ppdata;
2070 files_struct *fsp = NULL;
2071 uint32 security_info_sent = 0;
2072 NTSTATUS status;
2074 if(parameter_count < 8) {
2075 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2076 return;
2079 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
2080 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2081 return;
2084 if (!CAN_WRITE(fsp->conn)) {
2085 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2086 return;
2089 if(!lp_nt_acl_support(SNUM(conn))) {
2090 goto done;
2093 security_info_sent = IVAL(params,4);
2095 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
2096 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
2098 if (data_count == 0) {
2099 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2100 return;
2103 status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
2105 if (!NT_STATUS_IS_OK(status)) {
2106 reply_nterror(req, status);
2107 return;
2110 done:
2111 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2112 return;
2116 * Implement the default fsctl operation.
2119 static bool vfswrap_logged_ioctl_message = false;
2122 * In 3.6 we do not have a SMB_VFS_FSCTL() function
2123 * it's just faked to make it more look like
2124 * master (4.0)
2126 NTSTATUS smb_fsctl(struct files_struct *fsp,
2127 TALLOC_CTX *ctx,
2128 uint32_t function,
2129 uint16_t req_flags, /* Needed for UNICODE ... */
2130 const uint8_t *_in_data,
2131 uint32_t in_len,
2132 uint8_t **_out_data,
2133 uint32_t max_out_len,
2134 uint32_t *out_len)
2136 const char *in_data = (const char *)_in_data;
2137 char **out_data = (char **)_out_data;
2139 switch (function) {
2140 case FSCTL_SET_SPARSE:
2142 bool set_sparse = true;
2143 NTSTATUS status;
2145 if (in_len >= 1 && in_data[0] == 0) {
2146 set_sparse = false;
2149 status = file_set_sparse(fsp->conn, fsp, set_sparse);
2151 DEBUG(NT_STATUS_IS_OK(status) ? 10 : 9,
2152 ("FSCTL_SET_SPARSE: fname[%s] set[%u] - %s\n",
2153 smb_fname_str_dbg(fsp->fsp_name), set_sparse,
2154 nt_errstr(status)));
2156 return status;
2159 case FSCTL_CREATE_OR_GET_OBJECT_ID:
2161 unsigned char objid[16];
2162 char *return_data = NULL;
2164 /* This should return the object-id on this file.
2165 * I think I'll make this be the inode+dev. JRA.
2168 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fsp->fnum));
2170 *out_len = (max_out_len >= 64) ? 64 : max_out_len;
2171 /* Hmmm, will this cause problems if less data asked for? */
2172 return_data = talloc_array(ctx, char, 64);
2173 if (return_data == NULL) {
2174 return NT_STATUS_NO_MEMORY;
2177 /* For backwards compatibility only store the dev/inode. */
2178 push_file_id_16(return_data, &fsp->file_id);
2179 memcpy(return_data+16,create_volume_objectid(fsp->conn,objid),16);
2180 push_file_id_16(return_data+32, &fsp->file_id);
2181 *out_data = return_data;
2182 return NT_STATUS_OK;
2185 case FSCTL_GET_REPARSE_POINT:
2187 /* Fail it with STATUS_NOT_A_REPARSE_POINT */
2188 DEBUG(10, ("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X] Status: NOT_IMPLEMENTED\n", fsp->fnum));
2189 return NT_STATUS_NOT_A_REPARSE_POINT;
2192 case FSCTL_SET_REPARSE_POINT:
2194 /* Fail it with STATUS_NOT_A_REPARSE_POINT */
2195 DEBUG(10, ("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X] Status: NOT_IMPLEMENTED\n", fsp->fnum));
2196 return NT_STATUS_NOT_A_REPARSE_POINT;
2199 case FSCTL_GET_SHADOW_COPY_DATA:
2202 * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
2203 * and return their volume names. If max_data_count is 16, then it is just
2204 * asking for the number of volumes and length of the combined names.
2206 * pdata is the data allocated by our caller, but that uses
2207 * total_data_count (which is 0 in our case) rather than max_data_count.
2208 * Allocate the correct amount and return the pointer to let
2209 * it be deallocated when we return.
2211 struct shadow_copy_data *shadow_data = NULL;
2212 bool labels = False;
2213 uint32 labels_data_count = 0;
2214 uint32 i;
2215 char *cur_pdata = NULL;
2217 if (max_out_len < 16) {
2218 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
2219 max_out_len));
2220 return NT_STATUS_INVALID_PARAMETER;
2223 if (max_out_len > 16) {
2224 labels = True;
2227 shadow_data = talloc_zero(ctx, struct shadow_copy_data);
2228 if (shadow_data == NULL) {
2229 DEBUG(0,("TALLOC_ZERO() failed!\n"));
2230 return NT_STATUS_NO_MEMORY;
2234 * Call the VFS routine to actually do the work.
2236 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2237 TALLOC_FREE(shadow_data);
2238 if (errno == ENOSYS) {
2239 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n",
2240 fsp->conn->connectpath));
2241 return NT_STATUS_NOT_SUPPORTED;
2242 } else {
2243 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n",
2244 fsp->conn->connectpath));
2245 return NT_STATUS_UNSUCCESSFUL;
2249 labels_data_count = (shadow_data->num_volumes * 2 *
2250 sizeof(SHADOW_COPY_LABEL)) + 2;
2252 if (!labels) {
2253 *out_len = 16;
2254 } else {
2255 *out_len = 12 + labels_data_count + 4;
2258 if (max_out_len < *out_len) {
2259 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2260 max_out_len, *out_len));
2261 TALLOC_FREE(shadow_data);
2262 return NT_STATUS_BUFFER_TOO_SMALL;
2265 cur_pdata = talloc_array(ctx, char, *out_len);
2266 if (cur_pdata == NULL) {
2267 TALLOC_FREE(shadow_data);
2268 return NT_STATUS_NO_MEMORY;
2271 *out_data = cur_pdata;
2273 /* num_volumes 4 bytes */
2274 SIVAL(cur_pdata, 0, shadow_data->num_volumes);
2276 if (labels) {
2277 /* num_labels 4 bytes */
2278 SIVAL(cur_pdata, 4, shadow_data->num_volumes);
2281 /* needed_data_count 4 bytes */
2282 SIVAL(cur_pdata, 8, labels_data_count + 4);
2284 cur_pdata += 12;
2286 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2287 shadow_data->num_volumes, fsp_str_dbg(fsp)));
2288 if (labels && shadow_data->labels) {
2289 for (i=0; i<shadow_data->num_volumes; i++) {
2290 srvstr_push(cur_pdata, req_flags,
2291 cur_pdata, shadow_data->labels[i],
2292 2 * sizeof(SHADOW_COPY_LABEL),
2293 STR_UNICODE|STR_TERMINATE);
2294 cur_pdata += 2 * sizeof(SHADOW_COPY_LABEL);
2295 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2299 TALLOC_FREE(shadow_data);
2301 return NT_STATUS_OK;
2304 case FSCTL_FIND_FILES_BY_SID:
2306 /* pretend this succeeded -
2308 * we have to send back a list with all files owned by this SID
2310 * but I have to check that --metze
2312 struct dom_sid sid;
2313 uid_t uid;
2314 size_t sid_len;
2316 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n", fsp->fnum));
2318 if (in_len < 8) {
2319 /* NT_STATUS_BUFFER_TOO_SMALL maybe? */
2320 return NT_STATUS_INVALID_PARAMETER;
2323 sid_len = MIN(in_len - 4,SID_MAX_SIZE);
2325 /* unknown 4 bytes: this is not the length of the sid :-( */
2326 /*unknown = IVAL(pdata,0);*/
2328 if (!sid_parse(in_data + 4, sid_len, &sid)) {
2329 return NT_STATUS_INVALID_PARAMETER;
2331 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2333 if (!sid_to_uid(&sid, &uid)) {
2334 DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2335 sid_string_dbg(&sid),
2336 (unsigned long)sid_len));
2337 uid = (-1);
2340 /* we can take a look at the find source :-)
2342 * find ./ -uid $uid -name '*' is what we need here
2345 * and send 4bytes len and then NULL terminated unicode strings
2346 * for each file
2348 * but I don't know how to deal with the paged results
2349 * (maybe we can hang the result anywhere in the fsp struct)
2351 * but I don't know how to deal with the paged results
2352 * (maybe we can hang the result anywhere in the fsp struct)
2354 * we don't send all files at once
2355 * and at the next we should *not* start from the beginning,
2356 * so we have to cache the result
2358 * --metze
2361 /* this works for now... */
2362 return NT_STATUS_OK;
2365 case FSCTL_QUERY_ALLOCATED_RANGES:
2367 /* FIXME: This is just a dummy reply, telling that all of the
2368 * file is allocated. MKS cp needs that.
2369 * Adding the real allocated ranges via FIEMAP on Linux
2370 * and SEEK_DATA/SEEK_HOLE on Solaris is needed to make
2371 * this FSCTL correct for sparse files.
2373 NTSTATUS status;
2374 uint64_t offset, length;
2375 char *out_data_tmp = NULL;
2377 if (in_len != 16) {
2378 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: data_count(%u) != 16 is invalid!\n",
2379 in_len));
2380 return NT_STATUS_INVALID_PARAMETER;
2383 if (max_out_len < 16) {
2384 DEBUG(0,("FSCTL_QUERY_ALLOCATED_RANGES: max_out_len (%u) < 16 is invalid!\n",
2385 max_out_len));
2386 return NT_STATUS_INVALID_PARAMETER;
2389 offset = BVAL(in_data,0);
2390 length = BVAL(in_data,8);
2392 if (offset + length < offset) {
2393 /* No 64-bit integer wrap. */
2394 return NT_STATUS_INVALID_PARAMETER;
2397 /* Shouldn't this be SMB_VFS_STAT ... ? */
2398 status = vfs_stat_fsp(fsp);
2399 if (!NT_STATUS_IS_OK(status)) {
2400 return status;
2403 *out_len = 16;
2404 out_data_tmp = talloc_array(ctx, char, *out_len);
2405 if (out_data_tmp == NULL) {
2406 DEBUG(10, ("unable to allocate memory for response\n"));
2407 return NT_STATUS_NO_MEMORY;
2410 if (offset > fsp->fsp_name->st.st_ex_size ||
2411 fsp->fsp_name->st.st_ex_size == 0 ||
2412 length == 0) {
2413 memset(out_data_tmp, 0, *out_len);
2414 } else {
2415 uint64_t end = offset + length;
2416 end = MIN(end, fsp->fsp_name->st.st_ex_size);
2417 SBVAL(out_data_tmp, 0, 0);
2418 SBVAL(out_data_tmp, 8, end);
2421 *out_data = out_data_tmp;
2423 return NT_STATUS_OK;
2426 case FSCTL_IS_VOLUME_DIRTY:
2428 DEBUG(10,("FSCTL_IS_VOLUME_DIRTY: called on FID[0x%04X] "
2429 "(but not implemented)\n", fsp->fnum));
2431 * http://msdn.microsoft.com/en-us/library/cc232128%28PROT.10%29.aspx
2432 * says we have to respond with NT_STATUS_INVALID_PARAMETER
2434 return NT_STATUS_INVALID_PARAMETER;
2437 default:
2439 * Only print once ... unfortunately there could be lots of
2440 * different FSCTLs that are called.
2442 if (!vfswrap_logged_ioctl_message) {
2443 vfswrap_logged_ioctl_message = true;
2444 DEBUG(2, ("%s (0x%x): Currently not implemented.\n",
2445 __FUNCTION__, function));
2449 return NT_STATUS_NOT_SUPPORTED;
2452 /****************************************************************************
2453 Reply to NT IOCTL
2454 ****************************************************************************/
2456 static void call_nt_transact_ioctl(connection_struct *conn,
2457 struct smb_request *req,
2458 uint16 **ppsetup, uint32 setup_count,
2459 char **ppparams, uint32 parameter_count,
2460 char **ppdata, uint32 data_count,
2461 uint32 max_data_count)
2463 NTSTATUS status;
2464 uint32 function;
2465 uint16 fidnum;
2466 files_struct *fsp;
2467 uint8 isFSctl;
2468 uint8 compfilter;
2469 char *out_data = NULL;
2470 uint32 out_data_len = 0;
2471 char *pdata = *ppdata;
2472 TALLOC_CTX *ctx = talloc_tos();
2474 if (setup_count != 8) {
2475 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
2476 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2477 return;
2480 function = IVAL(*ppsetup, 0);
2481 fidnum = SVAL(*ppsetup, 4);
2482 isFSctl = CVAL(*ppsetup, 6);
2483 compfilter = CVAL(*ppsetup, 7);
2485 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2486 function, fidnum, isFSctl, compfilter));
2488 fsp=file_fsp(req, fidnum);
2491 * We don't really implement IOCTLs, especially on files.
2493 if (!isFSctl) {
2494 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2495 isFSctl));
2496 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2497 return;
2500 /* Has to be for an open file! */
2501 if (!check_fsp_open(conn, req, fsp)) {
2502 return;
2506 * out_data might be allocated by the VFS module, but talloc should be
2507 * used, and should be cleaned up when the request ends.
2509 status = smb_fsctl(fsp,
2510 ctx,
2511 function,
2512 req->flags2,
2513 (uint8_t *)pdata,
2514 data_count,
2515 (uint8_t **)&out_data,
2516 max_data_count,
2517 &out_data_len);
2518 if (!NT_STATUS_IS_OK(status)) {
2519 reply_nterror(req, status);
2520 } else {
2521 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2526 #ifdef HAVE_SYS_QUOTAS
2527 /****************************************************************************
2528 Reply to get user quota
2529 ****************************************************************************/
2531 static void call_nt_transact_get_user_quota(connection_struct *conn,
2532 struct smb_request *req,
2533 uint16 **ppsetup,
2534 uint32 setup_count,
2535 char **ppparams,
2536 uint32 parameter_count,
2537 char **ppdata,
2538 uint32 data_count,
2539 uint32 max_data_count)
2541 NTSTATUS nt_status = NT_STATUS_OK;
2542 char *params = *ppparams;
2543 char *pdata = *ppdata;
2544 char *entry;
2545 int data_len=0,param_len=0;
2546 int qt_len=0;
2547 int entry_len = 0;
2548 files_struct *fsp = NULL;
2549 uint16 level = 0;
2550 size_t sid_len;
2551 struct dom_sid sid;
2552 bool start_enum = True;
2553 SMB_NTQUOTA_STRUCT qt;
2554 SMB_NTQUOTA_LIST *tmp_list;
2555 SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2557 ZERO_STRUCT(qt);
2559 /* access check */
2560 if (get_current_uid(conn) != 0) {
2561 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2562 "[%s]\n", lp_servicename(SNUM(conn)),
2563 conn->session_info->unix_name));
2564 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2565 return;
2569 * Ensure minimum number of parameters sent.
2572 if (parameter_count < 4) {
2573 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2574 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2575 return;
2578 /* maybe we can check the quota_fnum */
2579 fsp = file_fsp(req, SVAL(params,0));
2580 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2581 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2582 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2583 return;
2586 /* the NULL pointer checking for fsp->fake_file_handle->pd
2587 * is done by CHECK_NTQUOTA_HANDLE_OK()
2589 qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2591 level = SVAL(params,2);
2593 /* unknown 12 bytes leading in params */
2595 switch (level) {
2596 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2597 /* seems that we should continue with the enum here --metze */
2599 if (qt_handle->quota_list!=NULL &&
2600 qt_handle->tmp_list==NULL) {
2602 /* free the list */
2603 free_ntquota_list(&(qt_handle->quota_list));
2605 /* Realloc the size of parameters and data we will return */
2606 param_len = 4;
2607 params = nttrans_realloc(ppparams, param_len);
2608 if(params == NULL) {
2609 reply_nterror(req, NT_STATUS_NO_MEMORY);
2610 return;
2613 data_len = 0;
2614 SIVAL(params,0,data_len);
2616 break;
2619 start_enum = False;
2621 case TRANSACT_GET_USER_QUOTA_LIST_START:
2623 if (qt_handle->quota_list==NULL &&
2624 qt_handle->tmp_list==NULL) {
2625 start_enum = True;
2628 if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2629 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2630 return;
2633 /* Realloc the size of parameters and data we will return */
2634 param_len = 4;
2635 params = nttrans_realloc(ppparams, param_len);
2636 if(params == NULL) {
2637 reply_nterror(req, NT_STATUS_NO_MEMORY);
2638 return;
2641 /* we should not trust the value in max_data_count*/
2642 max_data_count = MIN(max_data_count,2048);
2644 pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2645 if(pdata == NULL) {
2646 reply_nterror(req, NT_STATUS_NO_MEMORY);
2647 return;
2650 entry = pdata;
2652 /* set params Size of returned Quota Data 4 bytes*/
2653 /* but set it later when we know it */
2655 /* for each entry push the data */
2657 if (start_enum) {
2658 qt_handle->tmp_list = qt_handle->quota_list;
2661 tmp_list = qt_handle->tmp_list;
2663 for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2664 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2666 sid_len = ndr_size_dom_sid(
2667 &tmp_list->quotas->sid, 0);
2668 entry_len = 40 + sid_len;
2670 /* nextoffset entry 4 bytes */
2671 SIVAL(entry,0,entry_len);
2673 /* then the len of the SID 4 bytes */
2674 SIVAL(entry,4,sid_len);
2676 /* unknown data 8 bytes uint64_t */
2677 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2679 /* the used disk space 8 bytes uint64_t */
2680 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2682 /* the soft quotas 8 bytes uint64_t */
2683 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2685 /* the hard quotas 8 bytes uint64_t */
2686 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2688 /* and now the SID */
2689 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2692 qt_handle->tmp_list = tmp_list;
2694 /* overwrite the offset of the last entry */
2695 SIVAL(entry-entry_len,0,0);
2697 data_len = 4+qt_len;
2698 /* overwrite the params quota_data_len */
2699 SIVAL(params,0,data_len);
2701 break;
2703 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2705 /* unknown 4 bytes IVAL(pdata,0) */
2707 if (data_count < 8) {
2708 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2709 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2710 return;
2713 sid_len = IVAL(pdata,4);
2714 /* Ensure this is less than 1mb. */
2715 if (sid_len > (1024*1024)) {
2716 reply_nterror(req, NT_STATUS_NO_MEMORY);
2717 return;
2720 if (data_count < 8+sid_len) {
2721 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2722 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2723 return;
2726 data_len = 4+40+sid_len;
2728 if (max_data_count < data_len) {
2729 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2730 max_data_count, data_len));
2731 param_len = 4;
2732 SIVAL(params,0,data_len);
2733 data_len = 0;
2734 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2735 break;
2738 if (!sid_parse(pdata+8,sid_len,&sid)) {
2739 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2740 return;
2743 if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2744 ZERO_STRUCT(qt);
2746 * we have to return zero's in all fields
2747 * instead of returning an error here
2748 * --metze
2752 /* Realloc the size of parameters and data we will return */
2753 param_len = 4;
2754 params = nttrans_realloc(ppparams, param_len);
2755 if(params == NULL) {
2756 reply_nterror(req, NT_STATUS_NO_MEMORY);
2757 return;
2760 pdata = nttrans_realloc(ppdata, data_len);
2761 if(pdata == NULL) {
2762 reply_nterror(req, NT_STATUS_NO_MEMORY);
2763 return;
2766 entry = pdata;
2768 /* set params Size of returned Quota Data 4 bytes*/
2769 SIVAL(params,0,data_len);
2771 /* nextoffset entry 4 bytes */
2772 SIVAL(entry,0,0);
2774 /* then the len of the SID 4 bytes */
2775 SIVAL(entry,4,sid_len);
2777 /* unknown data 8 bytes uint64_t */
2778 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2780 /* the used disk space 8 bytes uint64_t */
2781 SBIG_UINT(entry,16,qt.usedspace);
2783 /* the soft quotas 8 bytes uint64_t */
2784 SBIG_UINT(entry,24,qt.softlim);
2786 /* the hard quotas 8 bytes uint64_t */
2787 SBIG_UINT(entry,32,qt.hardlim);
2789 /* and now the SID */
2790 sid_linearize(entry+40, sid_len, &sid);
2792 break;
2794 default:
2795 DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2796 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2797 return;
2798 break;
2801 send_nt_replies(conn, req, nt_status, params, param_len,
2802 pdata, data_len);
2805 /****************************************************************************
2806 Reply to set user quota
2807 ****************************************************************************/
2809 static void call_nt_transact_set_user_quota(connection_struct *conn,
2810 struct smb_request *req,
2811 uint16 **ppsetup,
2812 uint32 setup_count,
2813 char **ppparams,
2814 uint32 parameter_count,
2815 char **ppdata,
2816 uint32 data_count,
2817 uint32 max_data_count)
2819 char *params = *ppparams;
2820 char *pdata = *ppdata;
2821 int data_len=0,param_len=0;
2822 SMB_NTQUOTA_STRUCT qt;
2823 size_t sid_len;
2824 struct dom_sid sid;
2825 files_struct *fsp = NULL;
2827 ZERO_STRUCT(qt);
2829 /* access check */
2830 if (get_current_uid(conn) != 0) {
2831 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2832 "[%s]\n", lp_servicename(SNUM(conn)),
2833 conn->session_info->unix_name));
2834 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2835 return;
2839 * Ensure minimum number of parameters sent.
2842 if (parameter_count < 2) {
2843 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2844 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2845 return;
2848 /* maybe we can check the quota_fnum */
2849 fsp = file_fsp(req, SVAL(params,0));
2850 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2851 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2852 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2853 return;
2856 if (data_count < 40) {
2857 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2858 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2859 return;
2862 /* offset to next quota record.
2863 * 4 bytes IVAL(pdata,0)
2864 * unused here...
2867 /* sid len */
2868 sid_len = IVAL(pdata,4);
2870 if (data_count < 40+sid_len || (40+sid_len < sid_len)) {
2871 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2872 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2873 return;
2876 /* unknown 8 bytes in pdata
2877 * maybe its the change time in NTTIME
2880 /* the used space 8 bytes (uint64_t)*/
2881 qt.usedspace = BVAL(pdata,16);
2883 /* the soft quotas 8 bytes (uint64_t)*/
2884 qt.softlim = BVAL(pdata,24);
2886 /* the hard quotas 8 bytes (uint64_t)*/
2887 qt.hardlim = BVAL(pdata,32);
2889 if (!sid_parse(pdata+40,sid_len,&sid)) {
2890 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2891 return;
2894 DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2896 /* 44 unknown bytes left... */
2898 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2899 reply_nterror(req, NT_STATUS_INTERNAL_ERROR);
2900 return;
2903 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2904 pdata, data_len);
2906 #endif /* HAVE_SYS_QUOTAS */
2908 static void handle_nttrans(connection_struct *conn,
2909 struct trans_state *state,
2910 struct smb_request *req)
2912 if (get_Protocol() >= PROTOCOL_NT1) {
2913 req->flags2 |= 0x40; /* IS_LONG_NAME */
2914 SSVAL(req->inbuf,smb_flg2,req->flags2);
2918 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2920 /* Now we must call the relevant NT_TRANS function */
2921 switch(state->call) {
2922 case NT_TRANSACT_CREATE:
2924 START_PROFILE(NT_transact_create);
2925 call_nt_transact_create(
2926 conn, req,
2927 &state->setup, state->setup_count,
2928 &state->param, state->total_param,
2929 &state->data, state->total_data,
2930 state->max_data_return);
2931 END_PROFILE(NT_transact_create);
2932 break;
2935 case NT_TRANSACT_IOCTL:
2937 START_PROFILE(NT_transact_ioctl);
2938 call_nt_transact_ioctl(
2939 conn, req,
2940 &state->setup, state->setup_count,
2941 &state->param, state->total_param,
2942 &state->data, state->total_data,
2943 state->max_data_return);
2944 END_PROFILE(NT_transact_ioctl);
2945 break;
2948 case NT_TRANSACT_SET_SECURITY_DESC:
2950 START_PROFILE(NT_transact_set_security_desc);
2951 call_nt_transact_set_security_desc(
2952 conn, req,
2953 &state->setup, state->setup_count,
2954 &state->param, state->total_param,
2955 &state->data, state->total_data,
2956 state->max_data_return);
2957 END_PROFILE(NT_transact_set_security_desc);
2958 break;
2961 case NT_TRANSACT_NOTIFY_CHANGE:
2963 START_PROFILE(NT_transact_notify_change);
2964 call_nt_transact_notify_change(
2965 conn, req,
2966 &state->setup, state->setup_count,
2967 &state->param, state->total_param,
2968 &state->data, state->total_data,
2969 state->max_data_return,
2970 state->max_param_return);
2971 END_PROFILE(NT_transact_notify_change);
2972 break;
2975 case NT_TRANSACT_RENAME:
2977 START_PROFILE(NT_transact_rename);
2978 call_nt_transact_rename(
2979 conn, req,
2980 &state->setup, state->setup_count,
2981 &state->param, state->total_param,
2982 &state->data, state->total_data,
2983 state->max_data_return);
2984 END_PROFILE(NT_transact_rename);
2985 break;
2988 case NT_TRANSACT_QUERY_SECURITY_DESC:
2990 START_PROFILE(NT_transact_query_security_desc);
2991 call_nt_transact_query_security_desc(
2992 conn, req,
2993 &state->setup, state->setup_count,
2994 &state->param, state->total_param,
2995 &state->data, state->total_data,
2996 state->max_data_return);
2997 END_PROFILE(NT_transact_query_security_desc);
2998 break;
3001 #ifdef HAVE_SYS_QUOTAS
3002 case NT_TRANSACT_GET_USER_QUOTA:
3004 START_PROFILE(NT_transact_get_user_quota);
3005 call_nt_transact_get_user_quota(
3006 conn, req,
3007 &state->setup, state->setup_count,
3008 &state->param, state->total_param,
3009 &state->data, state->total_data,
3010 state->max_data_return);
3011 END_PROFILE(NT_transact_get_user_quota);
3012 break;
3015 case NT_TRANSACT_SET_USER_QUOTA:
3017 START_PROFILE(NT_transact_set_user_quota);
3018 call_nt_transact_set_user_quota(
3019 conn, req,
3020 &state->setup, state->setup_count,
3021 &state->param, state->total_param,
3022 &state->data, state->total_data,
3023 state->max_data_return);
3024 END_PROFILE(NT_transact_set_user_quota);
3025 break;
3027 #endif /* HAVE_SYS_QUOTAS */
3029 default:
3030 /* Error in request */
3031 DEBUG(0,("handle_nttrans: Unknown request %d in "
3032 "nttrans call\n", state->call));
3033 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
3034 return;
3036 return;
3039 /****************************************************************************
3040 Reply to a SMBNTtrans.
3041 ****************************************************************************/
3043 void reply_nttrans(struct smb_request *req)
3045 connection_struct *conn = req->conn;
3046 uint32_t pscnt;
3047 uint32_t psoff;
3048 uint32_t dscnt;
3049 uint32_t dsoff;
3050 uint16 function_code;
3051 NTSTATUS result;
3052 struct trans_state *state;
3054 START_PROFILE(SMBnttrans);
3056 if (req->wct < 19) {
3057 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3058 END_PROFILE(SMBnttrans);
3059 return;
3062 pscnt = IVAL(req->vwv+9, 1);
3063 psoff = IVAL(req->vwv+11, 1);
3064 dscnt = IVAL(req->vwv+13, 1);
3065 dsoff = IVAL(req->vwv+15, 1);
3066 function_code = SVAL(req->vwv+18, 0);
3068 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
3069 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
3070 END_PROFILE(SMBnttrans);
3071 return;
3074 result = allow_new_trans(conn->pending_trans, req->mid);
3075 if (!NT_STATUS_IS_OK(result)) {
3076 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
3077 reply_nterror(req, result);
3078 END_PROFILE(SMBnttrans);
3079 return;
3082 if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
3083 reply_nterror(req, NT_STATUS_NO_MEMORY);
3084 END_PROFILE(SMBnttrans);
3085 return;
3088 state->cmd = SMBnttrans;
3090 state->mid = req->mid;
3091 state->vuid = req->vuid;
3092 state->total_data = IVAL(req->vwv+3, 1);
3093 state->data = NULL;
3094 state->total_param = IVAL(req->vwv+1, 1);
3095 state->param = NULL;
3096 state->max_data_return = IVAL(req->vwv+7, 1);
3097 state->max_param_return = IVAL(req->vwv+5, 1);
3099 /* setup count is in *words* */
3100 state->setup_count = 2*CVAL(req->vwv+17, 1);
3101 state->setup = NULL;
3102 state->call = function_code;
3104 DEBUG(10, ("num_setup=%u, "
3105 "param_total=%u, this_param=%u, max_param=%u, "
3106 "data_total=%u, this_data=%u, max_data=%u, "
3107 "param_offset=%u, data_offset=%u\n",
3108 (unsigned)state->setup_count,
3109 (unsigned)state->total_param, (unsigned)pscnt,
3110 (unsigned)state->max_param_return,
3111 (unsigned)state->total_data, (unsigned)dscnt,
3112 (unsigned)state->max_data_return,
3113 (unsigned)psoff, (unsigned)dsoff));
3116 * All nttrans messages we handle have smb_wct == 19 +
3117 * state->setup_count. Ensure this is so as a sanity check.
3120 if(req->wct != 19 + (state->setup_count/2)) {
3121 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
3122 req->wct, 19 + (state->setup_count/2)));
3123 goto bad_param;
3126 /* Don't allow more than 128mb for each value. */
3127 if ((state->total_data > (1024*1024*128)) ||
3128 (state->total_param > (1024*1024*128))) {
3129 reply_nterror(req, NT_STATUS_NO_MEMORY);
3130 END_PROFILE(SMBnttrans);
3131 return;
3134 if ((dscnt > state->total_data) || (pscnt > state->total_param))
3135 goto bad_param;
3137 if (state->total_data) {
3139 if (trans_oob(state->total_data, 0, dscnt)
3140 || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
3141 goto bad_param;
3144 /* Can't use talloc here, the core routines do realloc on the
3145 * params and data. */
3146 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
3147 DEBUG(0,("reply_nttrans: data malloc fail for %u "
3148 "bytes !\n", (unsigned int)state->total_data));
3149 TALLOC_FREE(state);
3150 reply_nterror(req, NT_STATUS_NO_MEMORY);
3151 END_PROFILE(SMBnttrans);
3152 return;
3155 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
3158 if (state->total_param) {
3160 if (trans_oob(state->total_param, 0, pscnt)
3161 || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
3162 goto bad_param;
3165 /* Can't use talloc here, the core routines do realloc on the
3166 * params and data. */
3167 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
3168 DEBUG(0,("reply_nttrans: param malloc fail for %u "
3169 "bytes !\n", (unsigned int)state->total_param));
3170 SAFE_FREE(state->data);
3171 TALLOC_FREE(state);
3172 reply_nterror(req, NT_STATUS_NO_MEMORY);
3173 END_PROFILE(SMBnttrans);
3174 return;
3177 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
3180 state->received_data = dscnt;
3181 state->received_param = pscnt;
3183 if(state->setup_count > 0) {
3184 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
3185 state->setup_count));
3188 * No overflow possible here, state->setup_count is an
3189 * unsigned int, being filled by a single byte from
3190 * CVAL(req->vwv+13, 0) above. The cast in the comparison
3191 * below is not necessary, it's here to clarify things. The
3192 * validity of req->vwv and req->wct has been checked in
3193 * init_smb_request already.
3195 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
3196 goto bad_param;
3199 state->setup = (uint16 *)TALLOC(state, state->setup_count);
3200 if (state->setup == NULL) {
3201 DEBUG(0,("reply_nttrans : Out of memory\n"));
3202 SAFE_FREE(state->data);
3203 SAFE_FREE(state->param);
3204 TALLOC_FREE(state);
3205 reply_nterror(req, NT_STATUS_NO_MEMORY);
3206 END_PROFILE(SMBnttrans);
3207 return;
3210 memcpy(state->setup, req->vwv+19, state->setup_count);
3211 dump_data(10, (uint8 *)state->setup, state->setup_count);
3214 if ((state->received_data == state->total_data) &&
3215 (state->received_param == state->total_param)) {
3216 handle_nttrans(conn, state, req);
3217 SAFE_FREE(state->param);
3218 SAFE_FREE(state->data);
3219 TALLOC_FREE(state);
3220 END_PROFILE(SMBnttrans);
3221 return;
3224 DLIST_ADD(conn->pending_trans, state);
3226 /* We need to send an interim response then receive the rest
3227 of the parameter/data bytes */
3228 reply_outbuf(req, 0, 0);
3229 show_msg((char *)req->outbuf);
3230 END_PROFILE(SMBnttrans);
3231 return;
3233 bad_param:
3235 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
3236 SAFE_FREE(state->data);
3237 SAFE_FREE(state->param);
3238 TALLOC_FREE(state);
3239 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3240 END_PROFILE(SMBnttrans);
3241 return;
3244 /****************************************************************************
3245 Reply to a SMBnttranss
3246 ****************************************************************************/
3248 void reply_nttranss(struct smb_request *req)
3250 connection_struct *conn = req->conn;
3251 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
3252 struct trans_state *state;
3254 START_PROFILE(SMBnttranss);
3256 show_msg((char *)req->inbuf);
3258 if (req->wct < 18) {
3259 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3260 END_PROFILE(SMBnttranss);
3261 return;
3264 for (state = conn->pending_trans; state != NULL;
3265 state = state->next) {
3266 if (state->mid == req->mid) {
3267 break;
3271 if ((state == NULL) || (state->cmd != SMBnttrans)) {
3272 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3273 END_PROFILE(SMBnttranss);
3274 return;
3277 /* Revise state->total_param and state->total_data in case they have
3278 changed downwards */
3279 if (IVAL(req->vwv+1, 1) < state->total_param) {
3280 state->total_param = IVAL(req->vwv+1, 1);
3282 if (IVAL(req->vwv+3, 1) < state->total_data) {
3283 state->total_data = IVAL(req->vwv+3, 1);
3286 pcnt = IVAL(req->vwv+5, 1);
3287 poff = IVAL(req->vwv+7, 1);
3288 pdisp = IVAL(req->vwv+9, 1);
3290 dcnt = IVAL(req->vwv+11, 1);
3291 doff = IVAL(req->vwv+13, 1);
3292 ddisp = IVAL(req->vwv+15, 1);
3294 state->received_param += pcnt;
3295 state->received_data += dcnt;
3297 if ((state->received_data > state->total_data) ||
3298 (state->received_param > state->total_param))
3299 goto bad_param;
3301 if (pcnt) {
3302 if (trans_oob(state->total_param, pdisp, pcnt)
3303 || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
3304 goto bad_param;
3306 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
3309 if (dcnt) {
3310 if (trans_oob(state->total_data, ddisp, dcnt)
3311 || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
3312 goto bad_param;
3314 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
3317 if ((state->received_param < state->total_param) ||
3318 (state->received_data < state->total_data)) {
3319 END_PROFILE(SMBnttranss);
3320 return;
3323 handle_nttrans(conn, state, req);
3325 DLIST_REMOVE(conn->pending_trans, state);
3326 SAFE_FREE(state->data);
3327 SAFE_FREE(state->param);
3328 TALLOC_FREE(state);
3329 END_PROFILE(SMBnttranss);
3330 return;
3332 bad_param:
3334 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
3335 DLIST_REMOVE(conn->pending_trans, state);
3336 SAFE_FREE(state->data);
3337 SAFE_FREE(state->param);
3338 TALLOC_FREE(state);
3339 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
3340 END_PROFILE(SMBnttranss);
3341 return;