s4:smbtorture: Fix samba3.smb.dir on btrfs
[samba4-gss.git] / source3 / smbd / smb1_nttrans.c
blob554059059d4702b99da221754bed168c50046ee4
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 "smbprofile.h"
31 #include "libsmb/libsmb.h"
32 #include "lib/util_ea.h"
33 #include "librpc/gen_ndr/ndr_quota.h"
34 #include "librpc/gen_ndr/ndr_security.h"
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 static 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 smbXsrv_connection *xconn = req->xconn;
71 int max_send = xconn->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_smb1_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 (!smb1_srv_send(xconn,
87 (char *)req->outbuf,
88 true,
89 req->seqnum + 1,
90 IS_CONN_ENCRYPTED(conn))) {
91 exit_server_cleanly("send_nt_replies: smb1_srv_send 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_smb1_outbuf(req, 18,
143 total_sent_thistime + alignment_offset
144 + data_alignment_offset);
147 * Set total params and data to be sent.
150 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
151 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
154 * Calculate how many parameters and data we can fit into
155 * this packet. Parameters get precedence.
158 params_sent_thistime = MIN(params_to_send,useable_space);
159 data_sent_thistime = useable_space - params_sent_thistime;
160 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
162 SIVAL(req->outbuf, smb_ntr_ParameterCount,
163 params_sent_thistime);
165 if(params_sent_thistime == 0) {
166 SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
167 SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
168 } else {
170 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
171 * parameter bytes, however the first 4 bytes of outbuf are
172 * the Netbios over TCP header. Thus use smb_base() to subtract
173 * them from the calculation.
176 SIVAL(req->outbuf,smb_ntr_ParameterOffset,
177 ((smb_buf(req->outbuf)+alignment_offset)
178 - smb_base(req->outbuf)));
180 * Absolute displacement of param bytes sent in this packet.
183 SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
184 pp - params);
188 * Deal with the data portion.
191 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
193 if(data_sent_thistime == 0) {
194 SIVAL(req->outbuf,smb_ntr_DataOffset,0);
195 SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
196 } else {
198 * The offset of the data bytes is the offset of the
199 * parameter bytes plus the number of parameters being sent this time.
202 SIVAL(req->outbuf, smb_ntr_DataOffset,
203 ((smb_buf(req->outbuf)+alignment_offset) -
204 smb_base(req->outbuf))
205 + params_sent_thistime + data_alignment_offset);
206 SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
210 * Copy the param bytes into the packet.
213 if(params_sent_thistime) {
214 if (alignment_offset != 0) {
215 memset(smb_buf(req->outbuf), 0,
216 alignment_offset);
218 memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
219 params_sent_thistime);
223 * Copy in the data bytes
226 if(data_sent_thistime) {
227 if (data_alignment_offset != 0) {
228 memset((smb_buf(req->outbuf)+alignment_offset+
229 params_sent_thistime), 0,
230 data_alignment_offset);
232 memcpy(smb_buf(req->outbuf)+alignment_offset
233 +params_sent_thistime+data_alignment_offset,
234 pd,data_sent_thistime);
237 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
238 params_sent_thistime, data_sent_thistime, useable_space));
239 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
240 params_to_send, data_to_send, paramsize, datasize));
242 if (NT_STATUS_V(nt_error)) {
243 error_packet_set((char *)req->outbuf,
244 0, 0, nt_error,
245 __LINE__,__FILE__);
248 /* Send the packet */
249 show_msg((char *)req->outbuf);
250 if (!smb1_srv_send(xconn,
251 (char *)req->outbuf,
252 true,
253 req->seqnum + 1,
254 IS_CONN_ENCRYPTED(conn))) {
255 exit_server_cleanly("send_nt_replies: smb1_srv_send failed.");
258 TALLOC_FREE(req->outbuf);
260 pp += params_sent_thistime;
261 pd += data_sent_thistime;
263 params_to_send -= params_sent_thistime;
264 data_to_send -= data_sent_thistime;
267 * Sanity check
270 if(params_to_send < 0 || data_to_send < 0) {
271 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
272 params_to_send, data_to_send));
273 exit_server_cleanly("send_nt_replies: internal error");
278 /****************************************************************************
279 Reply to an NT create and X call on a pipe
280 ****************************************************************************/
282 static void nt_open_pipe(char *fname, connection_struct *conn,
283 struct smb_request *req, uint16_t *ppnum)
285 files_struct *fsp;
286 NTSTATUS status;
288 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
290 /* Strip \\ off the name if present. */
291 while (fname[0] == '\\') {
292 fname++;
295 status = open_np_file(req, fname, &fsp);
296 if (!NT_STATUS_IS_OK(status)) {
297 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
298 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
299 ERRDOS, ERRbadpipe);
300 return;
302 reply_nterror(req, status);
303 return;
306 *ppnum = fsp->fnum;
307 return;
310 /****************************************************************************
311 Reply to an NT create and X call for pipes.
312 ****************************************************************************/
314 static void do_ntcreate_pipe_open(connection_struct *conn,
315 struct smb_request *req)
317 char *fname = NULL;
318 uint16_t pnum = FNUM_FIELD_INVALID;
319 char *p = NULL;
320 uint32_t flags = IVAL(req->vwv+3, 1);
321 TALLOC_CTX *ctx = talloc_tos();
323 srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
325 if (!fname) {
326 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
327 ERRDOS, ERRbadpipe);
328 return;
330 nt_open_pipe(fname, conn, req, &pnum);
332 if (req->outbuf) {
333 /* error reply */
334 return;
338 * Deal with pipe return.
341 if (flags & EXTENDED_RESPONSE_REQUIRED) {
342 /* This is very strange. We
343 * return 50 words, but only set
344 * the wcnt to 42 ? It's definitely
345 * what happens on the wire....
347 reply_smb1_outbuf(req, 50, 0);
348 SCVAL(req->outbuf,smb_wct,42);
349 } else {
350 reply_smb1_outbuf(req, 34, 0);
353 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
354 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
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));
385 struct case_semantics_state {
386 connection_struct *conn;
387 bool case_sensitive;
388 bool case_preserve;
389 bool short_case_preserve;
392 /****************************************************************************
393 Restore case semantics.
394 ****************************************************************************/
396 static int restore_case_semantics(struct case_semantics_state *state)
398 state->conn->case_sensitive = state->case_sensitive;
399 state->conn->case_preserve = state->case_preserve;
400 state->conn->short_case_preserve = state->short_case_preserve;
401 return 0;
404 /****************************************************************************
405 Save case semantics.
406 ****************************************************************************/
408 static struct case_semantics_state *set_posix_case_semantics(TALLOC_CTX *mem_ctx,
409 connection_struct *conn)
411 struct case_semantics_state *result;
413 if (!(result = talloc(mem_ctx, struct case_semantics_state))) {
414 return NULL;
417 result->conn = conn;
418 result->case_sensitive = conn->case_sensitive;
419 result->case_preserve = conn->case_preserve;
420 result->short_case_preserve = conn->short_case_preserve;
422 /* Set to POSIX. */
423 conn->case_sensitive = True;
424 conn->case_preserve = True;
425 conn->short_case_preserve = True;
427 talloc_set_destructor(result, restore_case_semantics);
429 return result;
433 * Calculate the full path name given a relative fid.
435 static NTSTATUS get_relative_fid_filename(connection_struct *conn,
436 struct smb_request *req,
437 uint16_t root_dir_fid,
438 char *path,
439 char **path_out)
441 struct files_struct *dir_fsp = NULL;
442 char *new_path = NULL;
444 if (root_dir_fid == 0 || path == NULL) {
445 return NT_STATUS_INTERNAL_ERROR;
448 dir_fsp = file_fsp(req, root_dir_fid);
449 if (dir_fsp == NULL) {
450 return NT_STATUS_INVALID_HANDLE;
453 if (fsp_is_alternate_stream(dir_fsp)) {
454 return NT_STATUS_INVALID_HANDLE;
457 if (!dir_fsp->fsp_flags.is_directory) {
459 * Check to see if this is a mac fork of some kind.
461 if (conn->fs_capabilities & FILE_NAMED_STREAMS) {
462 char *stream = NULL;
464 stream = strchr_m(path, ':');
465 if (stream != NULL) {
466 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
471 * We need to handle the case when we get a relative open
472 * relative to a file and the pathname is blank - this is a
473 * reopen! (hint from demyn plantenberg)
475 return NT_STATUS_INVALID_HANDLE;
478 if (ISDOT(dir_fsp->fsp_name->base_name)) {
480 * We're at the toplevel dir, the final file name
481 * must not contain ./, as this is filtered out
482 * normally by srvstr_get_path and unix_convert
483 * explicitly rejects paths containing ./.
485 new_path = talloc_strdup(talloc_tos(), path);
486 } else {
488 * Copy in the base directory name.
491 new_path = talloc_asprintf(talloc_tos(),
492 "%s/%s",
493 dir_fsp->fsp_name->base_name,
494 path);
496 if (new_path == NULL) {
497 return NT_STATUS_NO_MEMORY;
500 *path_out = new_path;
501 return NT_STATUS_OK;
504 /****************************************************************************
505 Reply to an NT create and X call.
506 ****************************************************************************/
508 void reply_ntcreate_and_X(struct smb_request *req)
510 connection_struct *conn = req->conn;
511 struct files_struct *dirfsp = NULL;
512 struct smb_filename *smb_fname = NULL;
513 char *fname = NULL;
514 uint32_t flags;
515 uint32_t access_mask;
516 uint32_t file_attributes;
517 uint32_t share_access;
518 uint32_t create_disposition;
519 uint32_t create_options;
520 uint16_t root_dir_fid;
521 uint64_t allocation_size;
522 /* Breakout the oplock request bits so we can set the
523 reply bits separately. */
524 uint32_t fattr=0;
525 off_t file_len = 0;
526 int info = 0;
527 files_struct *fsp = NULL;
528 char *p = NULL;
529 struct timespec create_timespec;
530 struct timespec c_timespec;
531 struct timespec a_timespec;
532 struct timespec m_timespec;
533 NTSTATUS status;
534 int oplock_request;
535 uint8_t oplock_granted = NO_OPLOCK_RETURN;
536 struct case_semantics_state *case_state = NULL;
537 uint32_t ucf_flags;
538 NTTIME twrp = 0;
539 TALLOC_CTX *ctx = talloc_tos();
541 START_PROFILE(SMBntcreateX);
543 if (req->wct < 24) {
544 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
545 goto out;
548 flags = IVAL(req->vwv+3, 1);
549 access_mask = IVAL(req->vwv+7, 1);
550 file_attributes = IVAL(req->vwv+13, 1);
551 share_access = IVAL(req->vwv+15, 1);
552 create_disposition = IVAL(req->vwv+17, 1);
553 create_options = IVAL(req->vwv+19, 1);
554 root_dir_fid = (uint16_t)IVAL(req->vwv+5, 1);
556 allocation_size = BVAL(req->vwv+9, 1);
558 srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
559 STR_TERMINATE, &status);
561 if (!NT_STATUS_IS_OK(status)) {
562 reply_nterror(req, status);
563 goto out;
566 DBG_DEBUG("flags = 0x%" PRIx32 ", access_mask = 0x%" PRIx32
567 ", file_attributes = 0x%" PRIx32
568 ", share_access = 0x%" PRIx32
569 ", create_disposition = 0x%" PRIx32
570 ", create_options = 0x%" PRIx32 ", root_dir_fid = 0x%" PRIx32
571 ", fname = %s\n",
572 flags,
573 access_mask,
574 file_attributes,
575 share_access,
576 create_disposition,
577 create_options,
578 root_dir_fid,
579 fname);
582 * we need to remove ignored bits when they come directly from the client
583 * because we reuse some of them for internal stuff
585 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
588 * If it's an IPC, use the pipe handler.
591 if (IS_IPC(conn)) {
592 if (lp_nt_pipe_support()) {
593 do_ntcreate_pipe_open(conn, req);
594 goto out;
596 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
597 goto out;
600 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
601 if (oplock_request) {
602 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
603 ? BATCH_OPLOCK : 0;
606 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
607 case_state = set_posix_case_semantics(ctx, conn);
608 if (!case_state) {
609 reply_nterror(req, NT_STATUS_NO_MEMORY);
610 goto out;
614 if (root_dir_fid != 0) {
615 char *new_fname = NULL;
617 status = get_relative_fid_filename(conn,
618 req,
619 root_dir_fid,
620 fname,
621 &new_fname);
622 if (!NT_STATUS_IS_OK(status)) {
623 reply_nterror(req, status);
624 goto out;
626 fname = new_fname;
629 ucf_flags = filename_create_ucf_flags(req,
630 create_disposition,
631 create_options);
632 if (ucf_flags & UCF_GMT_PATHNAME) {
633 extract_snapshot_token(fname, &twrp);
635 status = smb1_strip_dfs_path(ctx, &ucf_flags, &fname);
636 if (!NT_STATUS_IS_OK(status)) {
637 reply_nterror(req, status);
638 goto out;
641 status = filename_convert_dirfsp(
642 ctx, conn, fname, ucf_flags, twrp, &dirfsp, &smb_fname);
644 TALLOC_FREE(case_state);
646 if (!NT_STATUS_IS_OK(status)) {
647 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
648 reply_botherror(req,
649 NT_STATUS_PATH_NOT_COVERED,
650 ERRSRV, ERRbadpath);
651 goto out;
653 reply_nterror(req, status);
654 goto out;
658 * Bug #6898 - clients using Windows opens should
659 * never be able to set this attribute into the
660 * VFS.
662 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
664 status = SMB_VFS_CREATE_FILE(
665 conn, /* conn */
666 req, /* req */
667 dirfsp, /* dirfsp */
668 smb_fname, /* fname */
669 access_mask, /* access_mask */
670 share_access, /* share_access */
671 create_disposition, /* create_disposition*/
672 create_options, /* create_options */
673 file_attributes, /* file_attributes */
674 oplock_request, /* oplock_request */
675 NULL, /* lease */
676 allocation_size, /* allocation_size */
677 0, /* private_flags */
678 NULL, /* sd */
679 NULL, /* ea_list */
680 &fsp, /* result */
681 &info, /* pinfo */
682 NULL, NULL); /* create context */
684 if (!NT_STATUS_IS_OK(status)) {
685 if (open_was_deferred(req->xconn, req->mid)) {
686 /* We have re-scheduled this call, no error. */
687 goto out;
689 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
690 bool ok = defer_smb1_sharing_violation(req);
691 if (ok) {
692 goto out;
695 reply_openerror(req, status);
696 goto out;
699 /* Ensure we're pointing at the correct stat struct. */
700 smb_fname = fsp->fsp_name;
703 * If the caller set the extended oplock request bit
704 * and we granted one (by whatever means) - set the
705 * correct bit for extended oplock reply.
708 if (oplock_request &&
709 (lp_fake_oplocks(SNUM(conn))
710 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
713 * Exclusive oplock granted
716 if (flags & REQUEST_BATCH_OPLOCK) {
717 oplock_granted = BATCH_OPLOCK_RETURN;
718 } else {
719 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
721 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
722 oplock_granted = LEVEL_II_OPLOCK_RETURN;
723 } else {
724 oplock_granted = NO_OPLOCK_RETURN;
727 file_len = smb_fname->st.st_ex_size;
729 if (flags & EXTENDED_RESPONSE_REQUIRED) {
730 /* This is very strange. We
731 * return 50 words, but only set
732 * the wcnt to 42 ? It's definitely
733 * what happens on the wire....
735 reply_smb1_outbuf(req, 50, 0);
736 SCVAL(req->outbuf,smb_wct,42);
737 } else {
738 reply_smb1_outbuf(req, 34, 0);
741 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
742 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
744 p = (char *)req->outbuf + smb_vwv2;
746 SCVAL(p, 0, oplock_granted);
748 p++;
749 SSVAL(p,0,fsp->fnum);
750 p += 2;
751 if ((create_disposition == FILE_SUPERSEDE)
752 && (info == FILE_WAS_OVERWRITTEN)) {
753 SIVAL(p,0,FILE_WAS_SUPERSEDED);
754 } else {
755 SIVAL(p,0,info);
757 p += 4;
759 fattr = fdos_mode(fsp);
760 if (fattr == 0) {
761 fattr = FILE_ATTRIBUTE_NORMAL;
764 /* Create time. */
765 create_timespec = get_create_timespec(conn, fsp, smb_fname);
766 a_timespec = smb_fname->st.st_ex_atime;
767 m_timespec = smb_fname->st.st_ex_mtime;
768 c_timespec = get_change_timespec(conn, fsp, smb_fname);
770 if (lp_dos_filetime_resolution(SNUM(conn))) {
771 dos_filetime_timespec(&create_timespec);
772 dos_filetime_timespec(&a_timespec);
773 dos_filetime_timespec(&m_timespec);
774 dos_filetime_timespec(&c_timespec);
777 put_long_date_full_timespec(conn->ts_res, p, &create_timespec); /* create time. */
778 p += 8;
779 put_long_date_full_timespec(conn->ts_res, p, &a_timespec); /* access time */
780 p += 8;
781 put_long_date_full_timespec(conn->ts_res, p, &m_timespec); /* write time */
782 p += 8;
783 put_long_date_full_timespec(conn->ts_res, p, &c_timespec); /* change time */
784 p += 8;
785 SIVAL(p,0,fattr); /* File Attributes. */
786 p += 4;
787 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
788 p += 8;
789 SOFF_T(p,0,file_len);
790 p += 8;
791 if (flags & EXTENDED_RESPONSE_REQUIRED) {
792 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
793 unsigned int num_streams = 0;
794 struct stream_struct *streams = NULL;
796 if (lp_ea_support(SNUM(conn))) {
797 size_t num_names = 0;
798 /* Do we have any EA's ? */
799 status = get_ea_names_from_fsp(
800 ctx, smb_fname->fsp, NULL, &num_names);
801 if (NT_STATUS_IS_OK(status) && num_names) {
802 file_status &= ~NO_EAS;
806 status = vfs_fstreaminfo(smb_fname->fsp, ctx,
807 &num_streams, &streams);
808 /* There is always one stream, ::$DATA. */
809 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
810 file_status &= ~NO_SUBSTREAMS;
812 TALLOC_FREE(streams);
813 SSVAL(p,2,file_status);
815 p += 4;
816 SCVAL(p,0,fsp->fsp_flags.is_directory ? 1 : 0);
818 if (flags & EXTENDED_RESPONSE_REQUIRED) {
819 uint32_t perms = 0;
820 p += 25;
821 if (fsp->fsp_flags.is_directory ||
822 fsp->fsp_flags.can_write ||
823 can_write_to_fsp(fsp))
825 perms = FILE_GENERIC_ALL;
826 } else {
827 perms = FILE_GENERIC_READ|FILE_EXECUTE;
829 SIVAL(p,0,perms);
832 DEBUG(5,("reply_ntcreate_and_X: %s, open name = %s\n",
833 fsp_fnum_dbg(fsp), smb_fname_str_dbg(smb_fname)));
835 out:
836 END_PROFILE(SMBntcreateX);
837 return;
840 /****************************************************************************
841 Reply to a NT_TRANSACT_CREATE call to open a pipe.
842 ****************************************************************************/
844 static void do_nt_transact_create_pipe(connection_struct *conn,
845 struct smb_request *req,
846 uint16_t **ppsetup, uint32_t setup_count,
847 char **ppparams, uint32_t parameter_count,
848 char **ppdata, uint32_t data_count)
850 char *fname = NULL;
851 char *params = *ppparams;
852 uint16_t pnum = FNUM_FIELD_INVALID;
853 char *p = NULL;
854 NTSTATUS status;
855 size_t param_len;
856 uint32_t flags;
857 TALLOC_CTX *ctx = talloc_tos();
860 * Ensure minimum number of parameters sent.
863 if(parameter_count < 54) {
864 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
865 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
866 return;
869 flags = IVAL(params,0);
871 if (req->posix_pathnames) {
872 srvstr_get_path_posix(ctx,
873 params,
874 req->flags2,
875 &fname,
876 params+53,
877 parameter_count-53,
878 STR_TERMINATE,
879 &status);
880 } else {
881 srvstr_get_path(ctx,
882 params,
883 req->flags2,
884 &fname,
885 params+53,
886 parameter_count-53,
887 STR_TERMINATE,
888 &status);
890 if (!NT_STATUS_IS_OK(status)) {
891 reply_nterror(req, status);
892 return;
895 nt_open_pipe(fname, conn, req, &pnum);
897 if (req->outbuf) {
898 /* Error return */
899 return;
902 /* Realloc the size of parameters and data we will return */
903 if (flags & EXTENDED_RESPONSE_REQUIRED) {
904 /* Extended response is 32 more byyes. */
905 param_len = 101;
906 } else {
907 param_len = 69;
909 params = nttrans_realloc(ppparams, param_len);
910 if(params == NULL) {
911 reply_nterror(req, NT_STATUS_NO_MEMORY);
912 return;
915 p = params;
916 SCVAL(p,0,NO_OPLOCK_RETURN);
918 p += 2;
919 SSVAL(p,0,pnum);
920 p += 2;
921 SIVAL(p,0,FILE_WAS_OPENED);
922 p += 8;
924 p += 32;
925 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
926 p += 20;
927 /* File type. */
928 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
929 /* Device state. */
930 SSVAL(p,2, 0x5FF); /* ? */
931 p += 4;
933 if (flags & EXTENDED_RESPONSE_REQUIRED) {
934 p += 25;
935 SIVAL(p,0,FILE_GENERIC_ALL);
937 * For pipes W2K3 seems to return
938 * 0x12019B next.
939 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
941 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
944 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
946 /* Send the required number of replies */
947 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
949 return;
952 /****************************************************************************
953 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
954 ****************************************************************************/
956 static void call_nt_transact_create(connection_struct *conn,
957 struct smb_request *req,
958 uint16_t **ppsetup, uint32_t setup_count,
959 char **ppparams, uint32_t parameter_count,
960 char **ppdata, uint32_t data_count,
961 uint32_t max_data_count)
963 struct smb_filename *smb_fname = NULL;
964 char *fname = NULL;
965 char *params = *ppparams;
966 char *data = *ppdata;
967 /* Breakout the oplock request bits so we can set the reply bits separately. */
968 uint32_t fattr=0;
969 off_t file_len = 0;
970 int info = 0;
971 struct files_struct *dirfsp = NULL;
972 files_struct *fsp = NULL;
973 char *p = NULL;
974 uint32_t flags;
975 uint32_t access_mask;
976 uint32_t file_attributes;
977 uint32_t share_access;
978 uint32_t create_disposition;
979 uint32_t create_options;
980 uint32_t sd_len;
981 struct security_descriptor *sd = NULL;
982 uint32_t ea_len;
983 uint16_t root_dir_fid;
984 struct timespec create_timespec;
985 struct timespec c_timespec;
986 struct timespec a_timespec;
987 struct timespec m_timespec;
988 struct ea_list *ea_list = NULL;
989 NTSTATUS status;
990 size_t param_len;
991 uint64_t allocation_size;
992 int oplock_request;
993 uint8_t oplock_granted;
994 struct case_semantics_state *case_state = NULL;
995 uint32_t ucf_flags;
996 NTTIME twrp = 0;
997 TALLOC_CTX *ctx = talloc_tos();
999 DEBUG(5,("call_nt_transact_create\n"));
1002 * If it's an IPC, use the pipe handler.
1005 if (IS_IPC(conn)) {
1006 if (lp_nt_pipe_support()) {
1007 do_nt_transact_create_pipe(
1008 conn, req,
1009 ppsetup, setup_count,
1010 ppparams, parameter_count,
1011 ppdata, data_count);
1012 goto out;
1014 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1015 goto out;
1019 * Ensure minimum number of parameters sent.
1022 if(parameter_count < 54) {
1023 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1024 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1025 goto out;
1028 flags = IVAL(params,0);
1029 access_mask = IVAL(params,8);
1030 file_attributes = IVAL(params,20);
1031 share_access = IVAL(params,24);
1032 create_disposition = IVAL(params,28);
1033 create_options = IVAL(params,32);
1034 sd_len = IVAL(params,36);
1035 ea_len = IVAL(params,40);
1036 root_dir_fid = (uint16_t)IVAL(params,4);
1037 allocation_size = BVAL(params,12);
1040 * we need to remove ignored bits when they come directly from the client
1041 * because we reuse some of them for internal stuff
1043 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1045 if (req->posix_pathnames) {
1046 srvstr_get_path_posix(ctx,
1047 params,
1048 req->flags2,
1049 &fname,
1050 params+53,
1051 parameter_count-53,
1052 STR_TERMINATE,
1053 &status);
1054 } else {
1055 srvstr_get_path(ctx,
1056 params,
1057 req->flags2,
1058 &fname,
1059 params+53,
1060 parameter_count-53,
1061 STR_TERMINATE,
1062 &status);
1064 if (!NT_STATUS_IS_OK(status)) {
1065 reply_nterror(req, status);
1066 goto out;
1069 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1070 case_state = set_posix_case_semantics(ctx, conn);
1071 if (!case_state) {
1072 reply_nterror(req, NT_STATUS_NO_MEMORY);
1073 goto out;
1077 if (root_dir_fid != 0) {
1078 char *new_fname = NULL;
1080 status = get_relative_fid_filename(conn,
1081 req,
1082 root_dir_fid,
1083 fname,
1084 &new_fname);
1085 if (!NT_STATUS_IS_OK(status)) {
1086 reply_nterror(req, status);
1087 goto out;
1089 fname = new_fname;
1092 ucf_flags = filename_create_ucf_flags(req,
1093 create_disposition,
1094 create_options);
1095 if (ucf_flags & UCF_GMT_PATHNAME) {
1096 extract_snapshot_token(fname, &twrp);
1098 status = smb1_strip_dfs_path(ctx, &ucf_flags, &fname);
1099 if (!NT_STATUS_IS_OK(status)) {
1100 reply_nterror(req, status);
1101 goto out;
1104 status = filename_convert_dirfsp(ctx,
1105 conn,
1106 fname,
1107 ucf_flags,
1108 twrp,
1109 &dirfsp,
1110 &smb_fname);
1112 TALLOC_FREE(case_state);
1114 if (!NT_STATUS_IS_OK(status)) {
1115 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1116 reply_botherror(req,
1117 NT_STATUS_PATH_NOT_COVERED,
1118 ERRSRV, ERRbadpath);
1119 goto out;
1121 reply_nterror(req, status);
1122 goto out;
1125 /* Ensure the data_len is correct for the sd and ea values given. */
1126 if ((ea_len + sd_len > data_count)
1127 || (ea_len > data_count) || (sd_len > data_count)
1128 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1129 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1130 "%u, data_count = %u\n", (unsigned int)ea_len,
1131 (unsigned int)sd_len, (unsigned int)data_count));
1132 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1133 goto out;
1136 if (sd_len) {
1137 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1138 sd_len));
1140 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1141 &sd);
1142 if (!NT_STATUS_IS_OK(status)) {
1143 DEBUG(10, ("call_nt_transact_create: "
1144 "unmarshall_sec_desc failed: %s\n",
1145 nt_errstr(status)));
1146 reply_nterror(req, status);
1147 goto out;
1151 if (ea_len) {
1152 if (!lp_ea_support(SNUM(conn))) {
1153 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1154 "EA's not supported.\n",
1155 (unsigned int)ea_len));
1156 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1157 goto out;
1160 if (ea_len < 10) {
1161 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1162 "too small (should be more than 10)\n",
1163 (unsigned int)ea_len ));
1164 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1165 goto out;
1168 /* We have already checked that ea_len <= data_count here. */
1169 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1170 ea_len);
1171 if (ea_list == NULL) {
1172 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1173 goto out;
1176 if (!req->posix_pathnames &&
1177 ea_list_has_invalid_name(ea_list)) {
1178 /* Realloc the size of parameters and data we will return */
1179 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1180 /* Extended response is 32 more bytes. */
1181 param_len = 101;
1182 } else {
1183 param_len = 69;
1185 params = nttrans_realloc(ppparams, param_len);
1186 if(params == NULL) {
1187 reply_nterror(req, NT_STATUS_NO_MEMORY);
1188 goto out;
1191 memset(params, '\0', param_len);
1192 send_nt_replies(conn, req, STATUS_INVALID_EA_NAME,
1193 params, param_len, NULL, 0);
1194 goto out;
1198 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1199 if (oplock_request) {
1200 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1201 ? BATCH_OPLOCK : 0;
1205 * Bug #6898 - clients using Windows opens should
1206 * never be able to set this attribute into the
1207 * VFS.
1209 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1211 status = SMB_VFS_CREATE_FILE(
1212 conn, /* conn */
1213 req, /* req */
1214 dirfsp, /* dirfsp */
1215 smb_fname, /* fname */
1216 access_mask, /* access_mask */
1217 share_access, /* share_access */
1218 create_disposition, /* create_disposition*/
1219 create_options, /* create_options */
1220 file_attributes, /* file_attributes */
1221 oplock_request, /* oplock_request */
1222 NULL, /* lease */
1223 allocation_size, /* allocation_size */
1224 0, /* private_flags */
1225 sd, /* sd */
1226 ea_list, /* ea_list */
1227 &fsp, /* result */
1228 &info, /* pinfo */
1229 NULL, NULL); /* create context */
1231 if(!NT_STATUS_IS_OK(status)) {
1232 if (open_was_deferred(req->xconn, req->mid)) {
1233 /* We have re-scheduled this call, no error. */
1234 return;
1236 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
1237 bool ok = defer_smb1_sharing_violation(req);
1238 if (ok) {
1239 return;
1242 reply_openerror(req, status);
1243 goto out;
1246 /* Ensure we're pointing at the correct stat struct. */
1247 TALLOC_FREE(smb_fname);
1248 smb_fname = fsp->fsp_name;
1251 * If the caller set the extended oplock request bit
1252 * and we granted one (by whatever means) - set the
1253 * correct bit for extended oplock reply.
1256 if (oplock_request &&
1257 (lp_fake_oplocks(SNUM(conn))
1258 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1261 * Exclusive oplock granted
1264 if (flags & REQUEST_BATCH_OPLOCK) {
1265 oplock_granted = BATCH_OPLOCK_RETURN;
1266 } else {
1267 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1269 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1270 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1271 } else {
1272 oplock_granted = NO_OPLOCK_RETURN;
1275 file_len = smb_fname->st.st_ex_size;
1277 /* Realloc the size of parameters and data we will return */
1278 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1279 /* Extended response is 32 more byyes. */
1280 param_len = 101;
1281 } else {
1282 param_len = 69;
1284 params = nttrans_realloc(ppparams, param_len);
1285 if(params == NULL) {
1286 reply_nterror(req, NT_STATUS_NO_MEMORY);
1287 goto out;
1290 p = params;
1291 SCVAL(p, 0, oplock_granted);
1293 p += 2;
1294 SSVAL(p,0,fsp->fnum);
1295 p += 2;
1296 if ((create_disposition == FILE_SUPERSEDE)
1297 && (info == FILE_WAS_OVERWRITTEN)) {
1298 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1299 } else {
1300 SIVAL(p,0,info);
1302 p += 8;
1304 fattr = fdos_mode(fsp);
1305 if (fattr == 0) {
1306 fattr = FILE_ATTRIBUTE_NORMAL;
1309 /* Create time. */
1310 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1311 a_timespec = smb_fname->st.st_ex_atime;
1312 m_timespec = smb_fname->st.st_ex_mtime;
1313 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1315 if (lp_dos_filetime_resolution(SNUM(conn))) {
1316 dos_filetime_timespec(&create_timespec);
1317 dos_filetime_timespec(&a_timespec);
1318 dos_filetime_timespec(&m_timespec);
1319 dos_filetime_timespec(&c_timespec);
1322 put_long_date_full_timespec(conn->ts_res, p, &create_timespec); /* create time. */
1323 p += 8;
1324 put_long_date_full_timespec(conn->ts_res, p, &a_timespec); /* access time */
1325 p += 8;
1326 put_long_date_full_timespec(conn->ts_res, p, &m_timespec); /* write time */
1327 p += 8;
1328 put_long_date_full_timespec(conn->ts_res, p, &c_timespec); /* change time */
1329 p += 8;
1330 SIVAL(p,0,fattr); /* File Attributes. */
1331 p += 4;
1332 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1333 p += 8;
1334 SOFF_T(p,0,file_len);
1335 p += 8;
1336 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1337 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1338 unsigned int num_streams = 0;
1339 struct stream_struct *streams = NULL;
1341 if (lp_ea_support(SNUM(conn))) {
1342 size_t num_names = 0;
1343 /* Do we have any EA's ? */
1344 status = get_ea_names_from_fsp(
1345 ctx, smb_fname->fsp, NULL, &num_names);
1346 if (NT_STATUS_IS_OK(status) && num_names) {
1347 file_status &= ~NO_EAS;
1351 status = vfs_fstreaminfo(smb_fname->fsp, ctx,
1352 &num_streams, &streams);
1353 /* There is always one stream, ::$DATA. */
1354 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1355 file_status &= ~NO_SUBSTREAMS;
1357 TALLOC_FREE(streams);
1358 SSVAL(p,2,file_status);
1360 p += 4;
1361 SCVAL(p,0,fsp->fsp_flags.is_directory ? 1 : 0);
1363 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1364 uint32_t perms = 0;
1365 p += 25;
1366 if (fsp->fsp_flags.is_directory ||
1367 fsp->fsp_flags.can_write ||
1368 can_write_to_fsp(fsp))
1370 perms = FILE_GENERIC_ALL;
1371 } else {
1372 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1374 SIVAL(p,0,perms);
1377 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1378 smb_fname_str_dbg(smb_fname)));
1380 /* Send the required number of replies */
1381 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1382 out:
1383 return;
1386 /****************************************************************************
1387 Reply to a NT CANCEL request.
1388 conn POINTER CAN BE NULL HERE !
1389 ****************************************************************************/
1391 void reply_ntcancel(struct smb_request *req)
1393 struct smbXsrv_connection *xconn = req->xconn;
1394 struct smbd_server_connection *sconn = req->sconn;
1395 bool found;
1398 * Go through and cancel any pending change notifies.
1401 START_PROFILE(SMBntcancel);
1402 smb1_srv_cancel_sign_response(xconn);
1403 found = remove_pending_change_notify_requests_by_mid(sconn, req->mid);
1404 if (!found) {
1405 smbd_smb1_brl_finish_by_mid(sconn, req->mid);
1408 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1409 (unsigned long long)req->mid));
1411 END_PROFILE(SMBntcancel);
1412 return;
1415 /****************************************************************************
1416 Reply to a NT rename request.
1417 ****************************************************************************/
1419 void reply_ntrename(struct smb_request *req)
1421 connection_struct *conn = req->conn;
1422 struct files_struct *src_dirfsp = NULL;
1423 struct smb_filename *smb_fname_old = NULL;
1424 struct files_struct *dst_dirfsp = NULL;
1425 struct smb_filename *smb_fname_new = NULL;
1426 char *oldname = NULL;
1427 char *newname = NULL;
1428 const char *dst_original_lcomp = NULL;
1429 const char *p;
1430 NTSTATUS status;
1431 uint32_t attrs;
1432 uint32_t ucf_flags_src = ucf_flags_from_smb_request(req);
1433 NTTIME src_twrp = 0;
1434 uint32_t ucf_flags_dst = ucf_flags_from_smb_request(req);
1435 NTTIME dst_twrp = 0;
1436 uint16_t rename_type;
1437 TALLOC_CTX *ctx = talloc_tos();
1438 bool stream_rename = false;
1440 START_PROFILE(SMBntrename);
1442 if (req->wct < 4) {
1443 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1444 goto out;
1447 attrs = SVAL(req->vwv+0, 0);
1448 rename_type = SVAL(req->vwv+1, 0);
1450 p = (const char *)req->buf + 1;
1451 p += srvstr_get_path_req(ctx, req, &oldname, p, STR_TERMINATE,
1452 &status);
1453 if (!NT_STATUS_IS_OK(status)) {
1454 reply_nterror(req, status);
1455 goto out;
1458 if (!req->posix_pathnames && ms_has_wild(oldname)) {
1459 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1460 goto out;
1463 p++;
1464 p += srvstr_get_path_req(ctx, req, &newname, p, STR_TERMINATE,
1465 &status);
1466 if (!NT_STATUS_IS_OK(status)) {
1467 reply_nterror(req, status);
1468 goto out;
1471 if (!req->posix_pathnames && ms_has_wild(newname)) {
1472 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1473 goto out;
1476 if (!req->posix_pathnames) {
1477 /* The newname must begin with a ':' if the
1478 oldname contains a ':'. */
1479 if (strchr_m(oldname, ':')) {
1480 if (newname[0] != ':') {
1481 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1482 goto out;
1484 stream_rename = true;
1488 if (ucf_flags_src & UCF_GMT_PATHNAME) {
1489 extract_snapshot_token(oldname, &src_twrp);
1491 status = smb1_strip_dfs_path(ctx, &ucf_flags_src, &oldname);
1492 if (!NT_STATUS_IS_OK(status)) {
1493 reply_nterror(req, status);
1494 goto out;
1497 status = filename_convert_dirfsp(ctx,
1498 conn,
1499 oldname,
1500 ucf_flags_src,
1501 src_twrp,
1502 &src_dirfsp,
1503 &smb_fname_old);
1504 if (!NT_STATUS_IS_OK(status)) {
1505 if (NT_STATUS_EQUAL(status,
1506 NT_STATUS_PATH_NOT_COVERED)) {
1507 reply_botherror(req,
1508 NT_STATUS_PATH_NOT_COVERED,
1509 ERRSRV, ERRbadpath);
1510 goto out;
1512 reply_nterror(req, status);
1513 goto out;
1516 if (stream_rename) {
1518 * No point in calling filename_convert()
1519 * on a raw stream name. It can never find
1520 * the file anyway. Use the same logic as
1521 * SMB2_FILE_RENAME_INFORMATION_INTERNAL
1522 * and generate smb_fname_new directly.
1524 smb_fname_new = synthetic_smb_fname(talloc_tos(),
1525 smb_fname_old->base_name,
1526 newname,
1527 NULL,
1528 smb_fname_old->twrp,
1529 smb_fname_old->flags);
1530 if (smb_fname_new == NULL) {
1531 reply_nterror(req, NT_STATUS_NO_MEMORY);
1532 goto out;
1534 } else {
1535 if (ucf_flags_dst & UCF_GMT_PATHNAME) {
1536 extract_snapshot_token(newname,
1537 &dst_twrp);
1539 status = smb1_strip_dfs_path(ctx, &ucf_flags_dst, &newname);
1540 if (!NT_STATUS_IS_OK(status)) {
1541 reply_nterror(req, status);
1542 goto out;
1544 status = filename_convert_dirfsp(ctx,
1545 conn,
1546 newname,
1547 ucf_flags_dst,
1548 dst_twrp,
1549 &dst_dirfsp,
1550 &smb_fname_new);
1551 if (!NT_STATUS_IS_OK(status)) {
1552 if (NT_STATUS_EQUAL(status,
1553 NT_STATUS_PATH_NOT_COVERED)) {
1554 reply_botherror(req,
1555 NT_STATUS_PATH_NOT_COVERED,
1556 ERRSRV, ERRbadpath);
1557 goto out;
1559 reply_nterror(req, status);
1560 goto out;
1564 /* Get the last component of the destination for rename_internals(). */
1565 dst_original_lcomp = get_original_lcomp(ctx,
1566 conn,
1567 newname,
1568 ucf_flags_dst);
1569 if (dst_original_lcomp == NULL) {
1570 reply_nterror(req, NT_STATUS_NO_MEMORY);
1571 goto out;
1575 DEBUG(3,("reply_ntrename: %s -> %s\n",
1576 smb_fname_str_dbg(smb_fname_old),
1577 smb_fname_str_dbg(smb_fname_new)));
1579 switch(rename_type) {
1580 case RENAME_FLAG_RENAME:
1581 status = rename_internals(ctx,
1582 conn,
1583 req,
1584 src_dirfsp,
1585 smb_fname_old,
1586 smb_fname_new,
1587 dst_original_lcomp,
1588 attrs,
1589 false,
1590 DELETE_ACCESS);
1591 break;
1592 case RENAME_FLAG_HARD_LINK:
1593 status = hardlink_internals(ctx,
1594 conn,
1595 req,
1596 false,
1597 smb_fname_old,
1598 smb_fname_new);
1599 break;
1600 case RENAME_FLAG_COPY:
1601 status = copy_internals(ctx,
1602 conn,
1603 req,
1604 src_dirfsp,
1605 smb_fname_old,
1606 dst_dirfsp,
1607 smb_fname_new,
1608 attrs);
1609 break;
1610 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1611 status = NT_STATUS_INVALID_PARAMETER;
1612 break;
1613 default:
1614 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1615 break;
1618 if (!NT_STATUS_IS_OK(status)) {
1619 if (open_was_deferred(req->xconn, req->mid)) {
1620 /* We have re-scheduled this call. */
1621 goto out;
1623 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
1624 bool ok = defer_smb1_sharing_violation(req);
1625 if (ok) {
1626 goto out;
1630 reply_nterror(req, status);
1631 goto out;
1634 reply_smb1_outbuf(req, 0, 0);
1635 out:
1636 END_PROFILE(SMBntrename);
1637 return;
1640 /****************************************************************************
1641 Reply to a notify change - queue the request and
1642 don't allow a directory to be opened.
1643 ****************************************************************************/
1645 static void smbd_smb1_notify_reply(struct smb_request *req,
1646 NTSTATUS error_code,
1647 uint8_t *buf, size_t len)
1649 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1652 static void call_nt_transact_notify_change(connection_struct *conn,
1653 struct smb_request *req,
1654 uint16_t **ppsetup,
1655 uint32_t setup_count,
1656 char **ppparams,
1657 uint32_t parameter_count,
1658 char **ppdata, uint32_t data_count,
1659 uint32_t max_data_count,
1660 uint32_t max_param_count)
1662 uint16_t *setup = *ppsetup;
1663 files_struct *fsp;
1664 uint32_t filter;
1665 NTSTATUS status;
1666 bool recursive;
1668 if(setup_count < 6) {
1669 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1670 return;
1673 fsp = file_fsp(req, SVAL(setup,4));
1674 filter = IVAL(setup, 0);
1675 recursive = (SVAL(setup, 6) != 0) ? True : False;
1677 DEBUG(3,("call_nt_transact_notify_change\n"));
1679 if(!fsp) {
1680 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1681 return;
1684 DBG_NOTICE("notify change called on %s, filter = %s, recursive = %d\n",
1685 fsp_str_dbg(fsp),
1686 notify_filter_string(talloc_tos(), filter),
1687 recursive);
1689 if((!fsp->fsp_flags.is_directory) || (conn != fsp->conn)) {
1690 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1691 return;
1694 if (fsp->notify == NULL) {
1696 status = change_notify_create(fsp,
1697 max_param_count,
1698 filter,
1699 recursive);
1700 if (!NT_STATUS_IS_OK(status)) {
1701 DEBUG(10, ("change_notify_create returned %s\n",
1702 nt_errstr(status)));
1703 reply_nterror(req, status);
1704 return;
1708 if (change_notify_fsp_has_changes(fsp)) {
1711 * We've got changes pending, respond immediately
1715 * TODO: write a torture test to check the filtering behaviour
1716 * here.
1719 change_notify_reply(req,
1720 NT_STATUS_OK,
1721 max_param_count,
1722 fsp->notify,
1723 smbd_smb1_notify_reply);
1726 * change_notify_reply() above has independently sent its
1727 * results
1729 return;
1733 * No changes pending, queue the request
1736 status = change_notify_add_request(req,
1737 max_param_count,
1738 filter,
1739 recursive, fsp,
1740 smbd_smb1_notify_reply);
1741 if (!NT_STATUS_IS_OK(status)) {
1742 reply_nterror(req, status);
1744 return;
1747 /****************************************************************************
1748 Reply to an NT transact rename command.
1749 ****************************************************************************/
1751 static void call_nt_transact_rename(connection_struct *conn,
1752 struct smb_request *req,
1753 uint16_t **ppsetup, uint32_t setup_count,
1754 char **ppparams, uint32_t parameter_count,
1755 char **ppdata, uint32_t data_count,
1756 uint32_t max_data_count)
1758 char *params = *ppparams;
1759 char *new_name = NULL;
1760 files_struct *fsp = NULL;
1761 NTSTATUS status;
1762 TALLOC_CTX *ctx = talloc_tos();
1764 if(parameter_count < 5) {
1765 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1766 return;
1769 fsp = file_fsp(req, SVAL(params, 0));
1770 if (!check_fsp(conn, req, fsp)) {
1771 return;
1773 if (req->posix_pathnames) {
1774 srvstr_get_path_posix(ctx,
1775 params,
1776 req->flags2,
1777 &new_name,
1778 params+4,
1779 parameter_count - 4,
1780 STR_TERMINATE,
1781 &status);
1782 } else {
1783 srvstr_get_path(ctx,
1784 params,
1785 req->flags2,
1786 &new_name,
1787 params+4,
1788 parameter_count - 4,
1789 STR_TERMINATE,
1790 &status);
1793 if (!NT_STATUS_IS_OK(status)) {
1794 reply_nterror(req, status);
1795 return;
1799 * W2K3 ignores this request as the RAW-RENAME test
1800 * demonstrates, so we do.
1802 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1804 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1805 fsp_str_dbg(fsp), new_name));
1807 return;
1810 /****************************************************************************
1811 SMB1 reply to query a security descriptor.
1812 ****************************************************************************/
1814 static void call_nt_transact_query_security_desc(connection_struct *conn,
1815 struct smb_request *req,
1816 uint16_t **ppsetup,
1817 uint32_t setup_count,
1818 char **ppparams,
1819 uint32_t parameter_count,
1820 char **ppdata,
1821 uint32_t data_count,
1822 uint32_t max_data_count)
1824 char *params = *ppparams;
1825 char *data = *ppdata;
1826 size_t sd_size = 0;
1827 uint32_t security_info_wanted;
1828 files_struct *fsp = NULL;
1829 NTSTATUS status;
1830 uint8_t *marshalled_sd = NULL;
1832 if(parameter_count < 8) {
1833 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1834 return;
1837 fsp = file_fsp(req, SVAL(params,0));
1838 if(!fsp) {
1839 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1840 return;
1843 security_info_wanted = IVAL(params,4);
1845 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1846 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1847 (unsigned int)security_info_wanted));
1849 params = nttrans_realloc(ppparams, 4);
1850 if(params == NULL) {
1851 reply_nterror(req, NT_STATUS_NO_MEMORY);
1852 return;
1856 * Get the permissions to return.
1859 status = smbd_do_query_security_desc(conn,
1860 talloc_tos(),
1861 fsp,
1862 security_info_wanted &
1863 SMB_SUPPORTED_SECINFO_FLAGS,
1864 max_data_count,
1865 &marshalled_sd,
1866 &sd_size);
1868 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
1869 SIVAL(params,0,(uint32_t)sd_size);
1870 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1871 params, 4, NULL, 0);
1872 return;
1875 if (!NT_STATUS_IS_OK(status)) {
1876 reply_nterror(req, status);
1877 return;
1880 SMB_ASSERT(sd_size > 0);
1882 SIVAL(params,0,(uint32_t)sd_size);
1884 if (max_data_count < sd_size) {
1885 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1886 params, 4, NULL, 0);
1887 return;
1891 * Allocate the data we will return.
1894 data = nttrans_realloc(ppdata, sd_size);
1895 if(data == NULL) {
1896 reply_nterror(req, NT_STATUS_NO_MEMORY);
1897 return;
1900 memcpy(data, marshalled_sd, sd_size);
1902 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1904 return;
1907 /****************************************************************************
1908 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1909 ****************************************************************************/
1911 static void call_nt_transact_set_security_desc(connection_struct *conn,
1912 struct smb_request *req,
1913 uint16_t **ppsetup,
1914 uint32_t setup_count,
1915 char **ppparams,
1916 uint32_t parameter_count,
1917 char **ppdata,
1918 uint32_t data_count,
1919 uint32_t max_data_count)
1921 char *params= *ppparams;
1922 char *data = *ppdata;
1923 files_struct *fsp = NULL;
1924 uint32_t security_info_sent = 0;
1925 NTSTATUS status;
1927 if(parameter_count < 8) {
1928 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1929 return;
1932 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1933 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1934 return;
1937 if (!CAN_WRITE(fsp->conn)) {
1938 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1939 return;
1942 if(!lp_nt_acl_support(SNUM(conn))) {
1943 goto done;
1946 security_info_sent = IVAL(params,4);
1948 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
1949 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
1951 if (data_count == 0) {
1952 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1953 return;
1956 status = set_sd_blob(fsp, (uint8_t *)data, data_count,
1957 security_info_sent & SMB_SUPPORTED_SECINFO_FLAGS);
1958 if (!NT_STATUS_IS_OK(status)) {
1959 reply_nterror(req, status);
1960 return;
1963 done:
1964 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1965 return;
1968 /****************************************************************************
1969 Reply to NT IOCTL
1970 ****************************************************************************/
1972 static void call_nt_transact_ioctl(connection_struct *conn,
1973 struct smb_request *req,
1974 uint16_t **ppsetup, uint32_t setup_count,
1975 char **ppparams, uint32_t parameter_count,
1976 char **ppdata, uint32_t data_count,
1977 uint32_t max_data_count)
1979 NTSTATUS status;
1980 uint32_t function;
1981 uint16_t fidnum;
1982 files_struct *fsp;
1983 uint8_t isFSctl;
1984 uint8_t compfilter;
1985 char *out_data = NULL;
1986 uint32_t out_data_len = 0;
1987 char *pdata = *ppdata;
1988 TALLOC_CTX *ctx = talloc_tos();
1990 if (setup_count != 8) {
1991 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1992 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1993 return;
1996 function = IVAL(*ppsetup, 0);
1997 fidnum = SVAL(*ppsetup, 4);
1998 isFSctl = CVAL(*ppsetup, 6);
1999 compfilter = CVAL(*ppsetup, 7);
2001 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
2002 function, fidnum, isFSctl, compfilter));
2004 fsp=file_fsp(req, fidnum);
2007 * We don't really implement IOCTLs, especially on files.
2009 if (!isFSctl) {
2010 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2011 isFSctl));
2012 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2013 return;
2016 /* Has to be for an open file! */
2017 if (!check_fsp_open(conn, req, fsp)) {
2018 return;
2022 * out_data might be allocated by the VFS module, but talloc should be
2023 * used, and should be cleaned up when the request ends.
2025 status = SMB_VFS_FSCTL(fsp,
2026 ctx,
2027 function,
2028 req->flags2,
2029 (uint8_t *)pdata,
2030 data_count,
2031 (uint8_t **)&out_data,
2032 max_data_count,
2033 &out_data_len);
2034 if (!NT_STATUS_IS_OK(status)) {
2035 reply_nterror(req, status);
2036 } else {
2037 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2042 #ifdef HAVE_SYS_QUOTAS
2043 /****************************************************************************
2044 Reply to get user quota
2045 ****************************************************************************/
2047 static void call_nt_transact_get_user_quota(connection_struct *conn,
2048 struct smb_request *req,
2049 uint16_t **ppsetup,
2050 uint32_t setup_count,
2051 char **ppparams,
2052 uint32_t parameter_count,
2053 char **ppdata,
2054 uint32_t data_count,
2055 uint32_t max_data_count)
2057 const struct loadparm_substitution *lp_sub =
2058 loadparm_s3_global_substitution();
2059 NTSTATUS nt_status = NT_STATUS_OK;
2060 char *params = *ppparams;
2061 char *pdata = *ppdata;
2062 int data_len = 0;
2063 int param_len = 0;
2064 files_struct *fsp = NULL;
2065 DATA_BLOB blob = data_blob_null;
2066 struct nttrans_query_quota_params info = {0};
2067 enum ndr_err_code err;
2068 TALLOC_CTX *tmp_ctx = NULL;
2069 uint32_t resp_len = 0;
2070 uint8_t *resp_data = 0;
2072 tmp_ctx = talloc_init("ntquota_list");
2073 if (!tmp_ctx) {
2074 nt_status = NT_STATUS_NO_MEMORY;
2075 goto error;
2078 /* access check */
2079 if (get_current_uid(conn) != sec_initial_uid()) {
2080 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2081 "[%s]\n", lp_servicename(talloc_tos(), lp_sub, SNUM(conn)),
2082 conn->session_info->unix_info->unix_name));
2083 nt_status = NT_STATUS_ACCESS_DENIED;
2084 goto error;
2087 blob.data = (uint8_t*)params;
2088 blob.length = parameter_count;
2090 err = ndr_pull_struct_blob(&blob, tmp_ctx, &info,
2091 (ndr_pull_flags_fn_t)ndr_pull_nttrans_query_quota_params);
2093 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
2094 DEBUG(0,("TRANSACT_GET_USER_QUOTA: failed to pull "
2095 "query_quota_params.\n"));
2096 nt_status = NT_STATUS_INVALID_PARAMETER;
2097 goto error;
2099 DBG_DEBUG("info.return_single_entry = %u, info.restart_scan = %u, "
2100 "info.sid_list_length = %u, info.start_sid_length = %u, "
2101 "info.start_sid_offset = %u\n",
2102 (unsigned int)info.return_single_entry,
2103 (unsigned int)info.restart_scan,
2104 (unsigned int)info.sid_list_length,
2105 (unsigned int)info.start_sid_length,
2106 (unsigned int)info.start_sid_offset);
2108 /* set blob to point at data for further parsing */
2109 blob.data = (uint8_t*)pdata;
2110 blob.length = data_count;
2112 * Although MS-SMB ref is ambiguous here, a microsoft client will
2113 * only ever send a start sid (as part of a list) with
2114 * sid_list_length & start_sid_offset both set to the actual list
2115 * length. Note: Only a single result is returned in this case
2116 * In the case where either start_sid_offset or start_sid_length
2117 * are set alone or if both set (but have different values) then
2118 * it seems windows will return a number of entries from the start
2119 * of the list of users with quotas set. This behaviour is undocumented
2120 * and windows clients do not send messages of that type. As such we
2121 * currently will reject these requests.
2123 if (info.start_sid_length
2124 || (info.sid_list_length != info.start_sid_offset)) {
2125 DBG_ERR("TRANSACT_GET_USER_QUOTA: unsupported single or "
2126 "compound sid format\n");
2127 nt_status = NT_STATUS_INVALID_PARAMETER;
2128 goto error;
2131 /* maybe we can check the quota_fnum */
2132 fsp = file_fsp(req, info.fid);
2133 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2134 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2135 nt_status = NT_STATUS_INVALID_HANDLE;
2136 goto error;
2138 nt_status = smbd_do_query_getinfo_quota(tmp_ctx,
2139 fsp,
2140 info.restart_scan,
2141 info.return_single_entry,
2142 info.sid_list_length,
2143 &blob,
2144 max_data_count,
2145 &resp_data,
2146 &resp_len);
2147 if (!NT_STATUS_IS_OK(nt_status)) {
2148 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MORE_ENTRIES)) {
2149 goto error;
2151 nt_status = NT_STATUS_OK;
2154 param_len = 4;
2155 params = nttrans_realloc(ppparams, param_len);
2156 if(params == NULL) {
2157 nt_status = NT_STATUS_NO_MEMORY;
2158 goto error;
2161 data_len = resp_len;
2162 SIVAL(params, 0, data_len);
2163 pdata = nttrans_realloc(ppdata, data_len);
2164 memcpy(pdata, resp_data, data_len);
2166 TALLOC_FREE(tmp_ctx);
2167 send_nt_replies(conn, req, nt_status, params, param_len,
2168 pdata, data_len);
2169 return;
2170 error:
2171 TALLOC_FREE(tmp_ctx);
2172 reply_nterror(req, nt_status);
2175 /****************************************************************************
2176 Reply to set user quota
2177 ****************************************************************************/
2179 static void call_nt_transact_set_user_quota(connection_struct *conn,
2180 struct smb_request *req,
2181 uint16_t **ppsetup,
2182 uint32_t setup_count,
2183 char **ppparams,
2184 uint32_t parameter_count,
2185 char **ppdata,
2186 uint32_t data_count,
2187 uint32_t max_data_count)
2189 const struct loadparm_substitution *lp_sub =
2190 loadparm_s3_global_substitution();
2191 char *params = *ppparams;
2192 char *pdata = *ppdata;
2193 int data_len=0,param_len=0;
2194 SMB_NTQUOTA_STRUCT qt;
2195 struct file_quota_information info = {0};
2196 enum ndr_err_code err;
2197 struct dom_sid sid;
2198 DATA_BLOB inblob;
2199 files_struct *fsp = NULL;
2200 TALLOC_CTX *ctx = NULL;
2201 NTSTATUS status = NT_STATUS_OK;
2202 ZERO_STRUCT(qt);
2204 /* access check */
2205 if (get_current_uid(conn) != sec_initial_uid()) {
2206 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2207 "[%s]\n", lp_servicename(talloc_tos(), lp_sub, SNUM(conn)),
2208 conn->session_info->unix_info->unix_name));
2209 status = NT_STATUS_ACCESS_DENIED;
2210 goto error;
2214 * Ensure minimum number of parameters sent.
2217 if (parameter_count < 2) {
2218 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2219 status = NT_STATUS_INVALID_PARAMETER;
2220 goto error;
2223 /* maybe we can check the quota_fnum */
2224 fsp = file_fsp(req, SVAL(params,0));
2225 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2226 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2227 status = NT_STATUS_INVALID_HANDLE;
2228 goto error;
2231 ctx = talloc_init("set_user_quota");
2232 if (!ctx) {
2233 status = NT_STATUS_NO_MEMORY;
2234 goto error;
2236 inblob.data = (uint8_t*)pdata;
2237 inblob.length = data_count;
2239 err = ndr_pull_struct_blob(
2240 &inblob,
2241 ctx,
2242 &info,
2243 (ndr_pull_flags_fn_t)ndr_pull_file_quota_information);
2245 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
2246 DEBUG(0,("TRANSACT_SET_USER_QUOTA: failed to pull "
2247 "file_quota_information\n"));
2248 status = NT_STATUS_INVALID_PARAMETER;
2249 goto error;
2251 qt.usedspace = info.quota_used;
2253 qt.softlim = info.quota_threshold;
2255 qt.hardlim = info.quota_limit;
2257 sid = info.sid;
2259 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2260 status = NT_STATUS_INTERNAL_ERROR;
2261 goto error;
2264 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2265 pdata, data_len);
2266 TALLOC_FREE(ctx);
2267 return;
2268 error:
2269 TALLOC_FREE(ctx);
2270 reply_nterror(req, status);
2272 #endif /* HAVE_SYS_QUOTAS */
2274 static void handle_nttrans(connection_struct *conn,
2275 struct trans_state *state,
2276 struct smb_request *req)
2278 struct smbXsrv_connection *xconn = req->xconn;
2280 if (xconn->protocol >= PROTOCOL_NT1) {
2281 req->flags2 |= 0x40; /* IS_LONG_NAME */
2282 SSVAL(discard_const_p(uint8_t, req->inbuf),smb_flg2,req->flags2);
2286 /* Now we must call the relevant NT_TRANS function */
2287 switch(state->call) {
2288 case NT_TRANSACT_CREATE:
2290 START_PROFILE(NT_transact_create);
2291 call_nt_transact_create(
2292 conn, req,
2293 &state->setup, state->setup_count,
2294 &state->param, state->total_param,
2295 &state->data, state->total_data,
2296 state->max_data_return);
2297 END_PROFILE(NT_transact_create);
2298 break;
2301 case NT_TRANSACT_IOCTL:
2303 START_PROFILE(NT_transact_ioctl);
2304 call_nt_transact_ioctl(
2305 conn, req,
2306 &state->setup, state->setup_count,
2307 &state->param, state->total_param,
2308 &state->data, state->total_data,
2309 state->max_data_return);
2310 END_PROFILE(NT_transact_ioctl);
2311 break;
2314 case NT_TRANSACT_SET_SECURITY_DESC:
2316 START_PROFILE(NT_transact_set_security_desc);
2317 call_nt_transact_set_security_desc(
2318 conn, req,
2319 &state->setup, state->setup_count,
2320 &state->param, state->total_param,
2321 &state->data, state->total_data,
2322 state->max_data_return);
2323 END_PROFILE(NT_transact_set_security_desc);
2324 break;
2327 case NT_TRANSACT_NOTIFY_CHANGE:
2329 START_PROFILE(NT_transact_notify_change);
2330 call_nt_transact_notify_change(
2331 conn, req,
2332 &state->setup, state->setup_count,
2333 &state->param, state->total_param,
2334 &state->data, state->total_data,
2335 state->max_data_return,
2336 state->max_param_return);
2337 END_PROFILE(NT_transact_notify_change);
2338 break;
2341 case NT_TRANSACT_RENAME:
2343 START_PROFILE(NT_transact_rename);
2344 call_nt_transact_rename(
2345 conn, req,
2346 &state->setup, state->setup_count,
2347 &state->param, state->total_param,
2348 &state->data, state->total_data,
2349 state->max_data_return);
2350 END_PROFILE(NT_transact_rename);
2351 break;
2354 case NT_TRANSACT_QUERY_SECURITY_DESC:
2356 START_PROFILE(NT_transact_query_security_desc);
2357 call_nt_transact_query_security_desc(
2358 conn, req,
2359 &state->setup, state->setup_count,
2360 &state->param, state->total_param,
2361 &state->data, state->total_data,
2362 state->max_data_return);
2363 END_PROFILE(NT_transact_query_security_desc);
2364 break;
2367 #ifdef HAVE_SYS_QUOTAS
2368 case NT_TRANSACT_GET_USER_QUOTA:
2370 START_PROFILE(NT_transact_get_user_quota);
2371 call_nt_transact_get_user_quota(
2372 conn, req,
2373 &state->setup, state->setup_count,
2374 &state->param, state->total_param,
2375 &state->data, state->total_data,
2376 state->max_data_return);
2377 END_PROFILE(NT_transact_get_user_quota);
2378 break;
2381 case NT_TRANSACT_SET_USER_QUOTA:
2383 START_PROFILE(NT_transact_set_user_quota);
2384 call_nt_transact_set_user_quota(
2385 conn, req,
2386 &state->setup, state->setup_count,
2387 &state->param, state->total_param,
2388 &state->data, state->total_data,
2389 state->max_data_return);
2390 END_PROFILE(NT_transact_set_user_quota);
2391 break;
2393 #endif /* HAVE_SYS_QUOTAS */
2395 default:
2396 /* Error in request */
2397 DEBUG(0,("handle_nttrans: Unknown request %d in "
2398 "nttrans call\n", state->call));
2399 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2400 return;
2402 return;
2405 /****************************************************************************
2406 Reply to a SMBNTtrans.
2407 ****************************************************************************/
2409 void reply_nttrans(struct smb_request *req)
2411 connection_struct *conn = req->conn;
2412 uint32_t pscnt;
2413 uint32_t psoff;
2414 uint32_t dscnt;
2415 uint32_t dsoff;
2416 uint16_t function_code;
2417 NTSTATUS result;
2418 struct trans_state *state;
2420 START_PROFILE(SMBnttrans);
2422 if (req->wct < 19) {
2423 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2424 END_PROFILE(SMBnttrans);
2425 return;
2428 pscnt = IVAL(req->vwv+9, 1);
2429 psoff = IVAL(req->vwv+11, 1);
2430 dscnt = IVAL(req->vwv+13, 1);
2431 dsoff = IVAL(req->vwv+15, 1);
2432 function_code = SVAL(req->vwv+18, 0);
2434 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2435 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2436 END_PROFILE(SMBnttrans);
2437 return;
2440 result = allow_new_trans(conn->pending_trans, req->mid);
2441 if (!NT_STATUS_IS_OK(result)) {
2442 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2443 reply_nterror(req, result);
2444 END_PROFILE(SMBnttrans);
2445 return;
2448 if ((state = talloc(conn, struct trans_state)) == NULL) {
2449 reply_nterror(req, NT_STATUS_NO_MEMORY);
2450 END_PROFILE(SMBnttrans);
2451 return;
2454 state->cmd = SMBnttrans;
2456 state->mid = req->mid;
2457 state->vuid = req->vuid;
2458 state->total_data = IVAL(req->vwv+3, 1);
2459 state->data = NULL;
2460 state->total_param = IVAL(req->vwv+1, 1);
2461 state->param = NULL;
2462 state->max_data_return = IVAL(req->vwv+7, 1);
2463 state->max_param_return = IVAL(req->vwv+5, 1);
2465 /* setup count is in *words* */
2466 state->setup_count = 2*CVAL(req->vwv+17, 1);
2467 state->setup = NULL;
2468 state->call = function_code;
2470 DEBUG(10, ("num_setup=%u, "
2471 "param_total=%u, this_param=%u, max_param=%u, "
2472 "data_total=%u, this_data=%u, max_data=%u, "
2473 "param_offset=%u, data_offset=%u\n",
2474 (unsigned)state->setup_count,
2475 (unsigned)state->total_param, (unsigned)pscnt,
2476 (unsigned)state->max_param_return,
2477 (unsigned)state->total_data, (unsigned)dscnt,
2478 (unsigned)state->max_data_return,
2479 (unsigned)psoff, (unsigned)dsoff));
2482 * All nttrans messages we handle have smb_wct == 19 +
2483 * state->setup_count. Ensure this is so as a sanity check.
2486 if(req->wct != 19 + (state->setup_count/2)) {
2487 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2488 req->wct, 19 + (state->setup_count/2)));
2489 goto bad_param;
2492 /* Don't allow more than 128mb for each value. */
2493 if ((state->total_data > (1024*1024*128)) ||
2494 (state->total_param > (1024*1024*128))) {
2495 reply_nterror(req, NT_STATUS_NO_MEMORY);
2496 END_PROFILE(SMBnttrans);
2497 return;
2500 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2501 goto bad_param;
2503 if (state->total_data) {
2505 if (smb_buffer_oob(state->total_data, 0, dscnt)
2506 || smb_buffer_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2507 goto bad_param;
2510 /* Can't use talloc here, the core routines do realloc on the
2511 * params and data. */
2512 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2513 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2514 "bytes !\n", (unsigned int)state->total_data));
2515 TALLOC_FREE(state);
2516 reply_nterror(req, NT_STATUS_NO_MEMORY);
2517 END_PROFILE(SMBnttrans);
2518 return;
2521 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2524 if (state->total_param) {
2526 if (smb_buffer_oob(state->total_param, 0, pscnt)
2527 || smb_buffer_oob(smb_len(req->inbuf), psoff, pscnt)) {
2528 goto bad_param;
2531 /* Can't use talloc here, the core routines do realloc on the
2532 * params and data. */
2533 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2534 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2535 "bytes !\n", (unsigned int)state->total_param));
2536 SAFE_FREE(state->data);
2537 TALLOC_FREE(state);
2538 reply_nterror(req, NT_STATUS_NO_MEMORY);
2539 END_PROFILE(SMBnttrans);
2540 return;
2543 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2546 state->received_data = dscnt;
2547 state->received_param = pscnt;
2549 if(state->setup_count > 0) {
2550 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2551 state->setup_count));
2554 * No overflow possible here, state->setup_count is an
2555 * unsigned int, being filled by a single byte from
2556 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2557 * below is not necessary, it's here to clarify things. The
2558 * validity of req->vwv and req->wct has been checked in
2559 * init_smb1_request already.
2561 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2562 goto bad_param;
2565 state->setup = (uint16_t *)TALLOC(state, state->setup_count);
2566 if (state->setup == NULL) {
2567 DEBUG(0,("reply_nttrans : Out of memory\n"));
2568 SAFE_FREE(state->data);
2569 SAFE_FREE(state->param);
2570 TALLOC_FREE(state);
2571 reply_nterror(req, NT_STATUS_NO_MEMORY);
2572 END_PROFILE(SMBnttrans);
2573 return;
2576 memcpy(state->setup, req->vwv+19, state->setup_count);
2577 dump_data(10, (uint8_t *)state->setup, state->setup_count);
2580 if ((state->received_data == state->total_data) &&
2581 (state->received_param == state->total_param)) {
2582 handle_nttrans(conn, state, req);
2583 SAFE_FREE(state->param);
2584 SAFE_FREE(state->data);
2585 TALLOC_FREE(state);
2586 END_PROFILE(SMBnttrans);
2587 return;
2590 DLIST_ADD(conn->pending_trans, state);
2592 /* We need to send an interim response then receive the rest
2593 of the parameter/data bytes */
2594 reply_smb1_outbuf(req, 0, 0);
2595 show_msg((char *)req->outbuf);
2596 END_PROFILE(SMBnttrans);
2597 return;
2599 bad_param:
2601 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2602 SAFE_FREE(state->data);
2603 SAFE_FREE(state->param);
2604 TALLOC_FREE(state);
2605 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2606 END_PROFILE(SMBnttrans);
2607 return;
2610 /****************************************************************************
2611 Reply to a SMBnttranss
2612 ****************************************************************************/
2614 void reply_nttranss(struct smb_request *req)
2616 connection_struct *conn = req->conn;
2617 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2618 struct trans_state *state;
2620 START_PROFILE(SMBnttranss);
2622 show_msg((const char *)req->inbuf);
2624 /* Windows clients expect all replies to
2625 an NT transact secondary (SMBnttranss 0xA1)
2626 to have a command code of NT transact
2627 (SMBnttrans 0xA0). See bug #8989 for details. */
2628 req->cmd = SMBnttrans;
2630 if (req->wct < 18) {
2631 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2632 END_PROFILE(SMBnttranss);
2633 return;
2636 for (state = conn->pending_trans; state != NULL;
2637 state = state->next) {
2638 if (state->mid == req->mid) {
2639 break;
2643 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2644 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2645 END_PROFILE(SMBnttranss);
2646 return;
2649 /* Revise state->total_param and state->total_data in case they have
2650 changed downwards */
2651 if (IVAL(req->vwv+1, 1) < state->total_param) {
2652 state->total_param = IVAL(req->vwv+1, 1);
2654 if (IVAL(req->vwv+3, 1) < state->total_data) {
2655 state->total_data = IVAL(req->vwv+3, 1);
2658 pcnt = IVAL(req->vwv+5, 1);
2659 poff = IVAL(req->vwv+7, 1);
2660 pdisp = IVAL(req->vwv+9, 1);
2662 dcnt = IVAL(req->vwv+11, 1);
2663 doff = IVAL(req->vwv+13, 1);
2664 ddisp = IVAL(req->vwv+15, 1);
2666 state->received_param += pcnt;
2667 state->received_data += dcnt;
2669 if ((state->received_data > state->total_data) ||
2670 (state->received_param > state->total_param))
2671 goto bad_param;
2673 if (pcnt) {
2674 if (smb_buffer_oob(state->total_param, pdisp, pcnt)
2675 || smb_buffer_oob(smb_len(req->inbuf), poff, pcnt)) {
2676 goto bad_param;
2678 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
2681 if (dcnt) {
2682 if (smb_buffer_oob(state->total_data, ddisp, dcnt)
2683 || smb_buffer_oob(smb_len(req->inbuf), doff, dcnt)) {
2684 goto bad_param;
2686 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
2689 if ((state->received_param < state->total_param) ||
2690 (state->received_data < state->total_data)) {
2691 END_PROFILE(SMBnttranss);
2692 return;
2695 handle_nttrans(conn, state, req);
2697 DLIST_REMOVE(conn->pending_trans, state);
2698 SAFE_FREE(state->data);
2699 SAFE_FREE(state->param);
2700 TALLOC_FREE(state);
2701 END_PROFILE(SMBnttranss);
2702 return;
2704 bad_param:
2706 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2707 DLIST_REMOVE(conn->pending_trans, state);
2708 SAFE_FREE(state->data);
2709 SAFE_FREE(state->param);
2710 TALLOC_FREE(state);
2711 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2712 END_PROFILE(SMBnttranss);
2713 return;