smbd: use smb1_strip_dfs_path() in reply_ntcreate_and_X()
[Samba.git] / source3 / smbd / smb1_nttrans.c
blob4c025fe9149c5133884ef4658df68eefcf58d06e
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, req->seqnum+1,
89 IS_CONN_ENCRYPTED(conn),
90 &req->pcd)) {
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, req->seqnum+1,
253 IS_CONN_ENCRYPTED(conn),
254 &req->pcd)) {
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 DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
567 "file_attributes = 0x%x, share_access = 0x%x, "
568 "create_disposition = 0x%x create_options = 0x%x "
569 "root_dir_fid = 0x%x, fname = %s\n",
570 (unsigned int)flags,
571 (unsigned int)access_mask,
572 (unsigned int)file_attributes,
573 (unsigned int)share_access,
574 (unsigned int)create_disposition,
575 (unsigned int)create_options,
576 (unsigned int)root_dir_fid,
577 fname));
580 * we need to remove ignored bits when they come directly from the client
581 * because we reuse some of them for internal stuff
583 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
586 * If it's an IPC, use the pipe handler.
589 if (IS_IPC(conn)) {
590 if (lp_nt_pipe_support()) {
591 do_ntcreate_pipe_open(conn, req);
592 goto out;
594 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
595 goto out;
598 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
599 if (oplock_request) {
600 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
601 ? BATCH_OPLOCK : 0;
604 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
605 case_state = set_posix_case_semantics(ctx, conn);
606 if (!case_state) {
607 reply_nterror(req, NT_STATUS_NO_MEMORY);
608 goto out;
612 if (root_dir_fid != 0) {
613 char *new_fname = NULL;
615 status = get_relative_fid_filename(conn,
616 req,
617 root_dir_fid,
618 fname,
619 &new_fname);
620 if (!NT_STATUS_IS_OK(status)) {
621 reply_nterror(req, status);
622 goto out;
624 fname = new_fname;
627 ucf_flags = filename_create_ucf_flags(req, create_disposition);
628 if (ucf_flags & UCF_GMT_PATHNAME) {
629 extract_snapshot_token(fname, &twrp);
631 status = smb1_strip_dfs_path(ctx, &ucf_flags, &fname);
632 if (!NT_STATUS_IS_OK(status)) {
633 reply_nterror(req, status);
634 goto out;
637 status = filename_convert_dirfsp(
638 ctx, conn, fname, ucf_flags, twrp, &dirfsp, &smb_fname);
640 TALLOC_FREE(case_state);
642 if (!NT_STATUS_IS_OK(status)) {
643 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
644 reply_botherror(req,
645 NT_STATUS_PATH_NOT_COVERED,
646 ERRSRV, ERRbadpath);
647 goto out;
649 reply_nterror(req, status);
650 goto out;
654 * Bug #6898 - clients using Windows opens should
655 * never be able to set this attribute into the
656 * VFS.
658 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
660 status = SMB_VFS_CREATE_FILE(
661 conn, /* conn */
662 req, /* req */
663 dirfsp, /* dirfsp */
664 smb_fname, /* fname */
665 access_mask, /* access_mask */
666 share_access, /* share_access */
667 create_disposition, /* create_disposition*/
668 create_options, /* create_options */
669 file_attributes, /* file_attributes */
670 oplock_request, /* oplock_request */
671 NULL, /* lease */
672 allocation_size, /* allocation_size */
673 0, /* private_flags */
674 NULL, /* sd */
675 NULL, /* ea_list */
676 &fsp, /* result */
677 &info, /* pinfo */
678 NULL, NULL); /* create context */
680 if (!NT_STATUS_IS_OK(status)) {
681 if (open_was_deferred(req->xconn, req->mid)) {
682 /* We have re-scheduled this call, no error. */
683 goto out;
685 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
686 bool ok = defer_smb1_sharing_violation(req);
687 if (ok) {
688 goto out;
691 reply_openerror(req, status);
692 goto out;
695 /* Ensure we're pointing at the correct stat struct. */
696 smb_fname = fsp->fsp_name;
699 * If the caller set the extended oplock request bit
700 * and we granted one (by whatever means) - set the
701 * correct bit for extended oplock reply.
704 if (oplock_request &&
705 (lp_fake_oplocks(SNUM(conn))
706 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
709 * Exclusive oplock granted
712 if (flags & REQUEST_BATCH_OPLOCK) {
713 oplock_granted = BATCH_OPLOCK_RETURN;
714 } else {
715 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
717 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
718 oplock_granted = LEVEL_II_OPLOCK_RETURN;
719 } else {
720 oplock_granted = NO_OPLOCK_RETURN;
723 file_len = smb_fname->st.st_ex_size;
725 if (flags & EXTENDED_RESPONSE_REQUIRED) {
726 /* This is very strange. We
727 * return 50 words, but only set
728 * the wcnt to 42 ? It's definitely
729 * what happens on the wire....
731 reply_smb1_outbuf(req, 50, 0);
732 SCVAL(req->outbuf,smb_wct,42);
733 } else {
734 reply_smb1_outbuf(req, 34, 0);
737 SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
738 SSVAL(req->outbuf, smb_vwv1, 0); /* no andx offset */
740 p = (char *)req->outbuf + smb_vwv2;
742 SCVAL(p, 0, oplock_granted);
744 p++;
745 SSVAL(p,0,fsp->fnum);
746 p += 2;
747 if ((create_disposition == FILE_SUPERSEDE)
748 && (info == FILE_WAS_OVERWRITTEN)) {
749 SIVAL(p,0,FILE_WAS_SUPERSEDED);
750 } else {
751 SIVAL(p,0,info);
753 p += 4;
755 fattr = fdos_mode(fsp);
756 if (fattr == 0) {
757 fattr = FILE_ATTRIBUTE_NORMAL;
760 /* Create time. */
761 create_timespec = get_create_timespec(conn, fsp, smb_fname);
762 a_timespec = smb_fname->st.st_ex_atime;
763 m_timespec = smb_fname->st.st_ex_mtime;
764 c_timespec = get_change_timespec(conn, fsp, smb_fname);
766 if (lp_dos_filetime_resolution(SNUM(conn))) {
767 dos_filetime_timespec(&create_timespec);
768 dos_filetime_timespec(&a_timespec);
769 dos_filetime_timespec(&m_timespec);
770 dos_filetime_timespec(&c_timespec);
773 put_long_date_full_timespec(conn->ts_res, p, &create_timespec); /* create time. */
774 p += 8;
775 put_long_date_full_timespec(conn->ts_res, p, &a_timespec); /* access time */
776 p += 8;
777 put_long_date_full_timespec(conn->ts_res, p, &m_timespec); /* write time */
778 p += 8;
779 put_long_date_full_timespec(conn->ts_res, p, &c_timespec); /* change time */
780 p += 8;
781 SIVAL(p,0,fattr); /* File Attributes. */
782 p += 4;
783 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
784 p += 8;
785 SOFF_T(p,0,file_len);
786 p += 8;
787 if (flags & EXTENDED_RESPONSE_REQUIRED) {
788 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
789 unsigned int num_streams = 0;
790 struct stream_struct *streams = NULL;
792 if (lp_ea_support(SNUM(conn))) {
793 size_t num_names = 0;
794 /* Do we have any EA's ? */
795 status = get_ea_names_from_fsp(
796 ctx, smb_fname->fsp, NULL, &num_names);
797 if (NT_STATUS_IS_OK(status) && num_names) {
798 file_status &= ~NO_EAS;
802 status = vfs_fstreaminfo(smb_fname->fsp, ctx,
803 &num_streams, &streams);
804 /* There is always one stream, ::$DATA. */
805 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
806 file_status &= ~NO_SUBSTREAMS;
808 TALLOC_FREE(streams);
809 SSVAL(p,2,file_status);
811 p += 4;
812 SCVAL(p,0,fsp->fsp_flags.is_directory ? 1 : 0);
814 if (flags & EXTENDED_RESPONSE_REQUIRED) {
815 uint32_t perms = 0;
816 p += 25;
817 if (fsp->fsp_flags.is_directory ||
818 fsp->fsp_flags.can_write ||
819 can_write_to_fsp(fsp))
821 perms = FILE_GENERIC_ALL;
822 } else {
823 perms = FILE_GENERIC_READ|FILE_EXECUTE;
825 SIVAL(p,0,perms);
828 DEBUG(5,("reply_ntcreate_and_X: %s, open name = %s\n",
829 fsp_fnum_dbg(fsp), smb_fname_str_dbg(smb_fname)));
831 out:
832 END_PROFILE(SMBntcreateX);
833 return;
836 /****************************************************************************
837 Reply to a NT_TRANSACT_CREATE call to open a pipe.
838 ****************************************************************************/
840 static void do_nt_transact_create_pipe(connection_struct *conn,
841 struct smb_request *req,
842 uint16_t **ppsetup, uint32_t setup_count,
843 char **ppparams, uint32_t parameter_count,
844 char **ppdata, uint32_t data_count)
846 char *fname = NULL;
847 char *params = *ppparams;
848 uint16_t pnum = FNUM_FIELD_INVALID;
849 char *p = NULL;
850 NTSTATUS status;
851 size_t param_len;
852 uint32_t flags;
853 TALLOC_CTX *ctx = talloc_tos();
856 * Ensure minimum number of parameters sent.
859 if(parameter_count < 54) {
860 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
861 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
862 return;
865 flags = IVAL(params,0);
867 if (req->posix_pathnames) {
868 srvstr_get_path_posix(ctx,
869 params,
870 req->flags2,
871 &fname,
872 params+53,
873 parameter_count-53,
874 STR_TERMINATE,
875 &status);
876 } else {
877 srvstr_get_path(ctx,
878 params,
879 req->flags2,
880 &fname,
881 params+53,
882 parameter_count-53,
883 STR_TERMINATE,
884 &status);
886 if (!NT_STATUS_IS_OK(status)) {
887 reply_nterror(req, status);
888 return;
891 nt_open_pipe(fname, conn, req, &pnum);
893 if (req->outbuf) {
894 /* Error return */
895 return;
898 /* Realloc the size of parameters and data we will return */
899 if (flags & EXTENDED_RESPONSE_REQUIRED) {
900 /* Extended response is 32 more byyes. */
901 param_len = 101;
902 } else {
903 param_len = 69;
905 params = nttrans_realloc(ppparams, param_len);
906 if(params == NULL) {
907 reply_nterror(req, NT_STATUS_NO_MEMORY);
908 return;
911 p = params;
912 SCVAL(p,0,NO_OPLOCK_RETURN);
914 p += 2;
915 SSVAL(p,0,pnum);
916 p += 2;
917 SIVAL(p,0,FILE_WAS_OPENED);
918 p += 8;
920 p += 32;
921 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
922 p += 20;
923 /* File type. */
924 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
925 /* Device state. */
926 SSVAL(p,2, 0x5FF); /* ? */
927 p += 4;
929 if (flags & EXTENDED_RESPONSE_REQUIRED) {
930 p += 25;
931 SIVAL(p,0,FILE_GENERIC_ALL);
933 * For pipes W2K3 seems to return
934 * 0x12019B next.
935 * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
937 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
940 DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
942 /* Send the required number of replies */
943 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
945 return;
948 /****************************************************************************
949 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
950 ****************************************************************************/
952 static void call_nt_transact_create(connection_struct *conn,
953 struct smb_request *req,
954 uint16_t **ppsetup, uint32_t setup_count,
955 char **ppparams, uint32_t parameter_count,
956 char **ppdata, uint32_t data_count,
957 uint32_t max_data_count)
959 struct smb_filename *smb_fname = NULL;
960 char *fname = NULL;
961 char *params = *ppparams;
962 char *data = *ppdata;
963 /* Breakout the oplock request bits so we can set the reply bits separately. */
964 uint32_t fattr=0;
965 off_t file_len = 0;
966 int info = 0;
967 struct files_struct *dirfsp = NULL;
968 files_struct *fsp = NULL;
969 char *p = NULL;
970 uint32_t flags;
971 uint32_t access_mask;
972 uint32_t file_attributes;
973 uint32_t share_access;
974 uint32_t create_disposition;
975 uint32_t create_options;
976 uint32_t sd_len;
977 struct security_descriptor *sd = NULL;
978 uint32_t ea_len;
979 uint16_t root_dir_fid;
980 struct timespec create_timespec;
981 struct timespec c_timespec;
982 struct timespec a_timespec;
983 struct timespec m_timespec;
984 struct ea_list *ea_list = NULL;
985 NTSTATUS status;
986 size_t param_len;
987 uint64_t allocation_size;
988 int oplock_request;
989 uint8_t oplock_granted;
990 struct case_semantics_state *case_state = NULL;
991 uint32_t ucf_flags;
992 NTTIME twrp = 0;
993 TALLOC_CTX *ctx = talloc_tos();
995 DEBUG(5,("call_nt_transact_create\n"));
998 * If it's an IPC, use the pipe handler.
1001 if (IS_IPC(conn)) {
1002 if (lp_nt_pipe_support()) {
1003 do_nt_transact_create_pipe(
1004 conn, req,
1005 ppsetup, setup_count,
1006 ppparams, parameter_count,
1007 ppdata, data_count);
1008 goto out;
1010 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1011 goto out;
1015 * Ensure minimum number of parameters sent.
1018 if(parameter_count < 54) {
1019 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
1020 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1021 goto out;
1024 flags = IVAL(params,0);
1025 access_mask = IVAL(params,8);
1026 file_attributes = IVAL(params,20);
1027 share_access = IVAL(params,24);
1028 create_disposition = IVAL(params,28);
1029 create_options = IVAL(params,32);
1030 sd_len = IVAL(params,36);
1031 ea_len = IVAL(params,40);
1032 root_dir_fid = (uint16_t)IVAL(params,4);
1033 allocation_size = BVAL(params,12);
1036 * we need to remove ignored bits when they come directly from the client
1037 * because we reuse some of them for internal stuff
1039 create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
1041 if (req->posix_pathnames) {
1042 srvstr_get_path_posix(ctx,
1043 params,
1044 req->flags2,
1045 &fname,
1046 params+53,
1047 parameter_count-53,
1048 STR_TERMINATE,
1049 &status);
1050 } else {
1051 srvstr_get_path(ctx,
1052 params,
1053 req->flags2,
1054 &fname,
1055 params+53,
1056 parameter_count-53,
1057 STR_TERMINATE,
1058 &status);
1060 if (!NT_STATUS_IS_OK(status)) {
1061 reply_nterror(req, status);
1062 goto out;
1065 if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
1066 case_state = set_posix_case_semantics(ctx, conn);
1067 if (!case_state) {
1068 reply_nterror(req, NT_STATUS_NO_MEMORY);
1069 goto out;
1073 if (root_dir_fid != 0) {
1074 char *new_fname = NULL;
1076 status = get_relative_fid_filename(conn,
1077 req,
1078 root_dir_fid,
1079 fname,
1080 &new_fname);
1081 if (!NT_STATUS_IS_OK(status)) {
1082 reply_nterror(req, status);
1083 goto out;
1085 fname = new_fname;
1088 ucf_flags = filename_create_ucf_flags(req, create_disposition);
1089 if (ucf_flags & UCF_GMT_PATHNAME) {
1090 extract_snapshot_token(fname, &twrp);
1092 status = filename_convert_dirfsp(ctx,
1093 conn,
1094 fname,
1095 ucf_flags,
1096 twrp,
1097 &dirfsp,
1098 &smb_fname);
1100 TALLOC_FREE(case_state);
1102 if (!NT_STATUS_IS_OK(status)) {
1103 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1104 reply_botherror(req,
1105 NT_STATUS_PATH_NOT_COVERED,
1106 ERRSRV, ERRbadpath);
1107 goto out;
1109 reply_nterror(req, status);
1110 goto out;
1113 /* Ensure the data_len is correct for the sd and ea values given. */
1114 if ((ea_len + sd_len > data_count)
1115 || (ea_len > data_count) || (sd_len > data_count)
1116 || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
1117 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
1118 "%u, data_count = %u\n", (unsigned int)ea_len,
1119 (unsigned int)sd_len, (unsigned int)data_count));
1120 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1121 goto out;
1124 if (sd_len) {
1125 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
1126 sd_len));
1128 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
1129 &sd);
1130 if (!NT_STATUS_IS_OK(status)) {
1131 DEBUG(10, ("call_nt_transact_create: "
1132 "unmarshall_sec_desc failed: %s\n",
1133 nt_errstr(status)));
1134 reply_nterror(req, status);
1135 goto out;
1139 if (ea_len) {
1140 if (!lp_ea_support(SNUM(conn))) {
1141 DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
1142 "EA's not supported.\n",
1143 (unsigned int)ea_len));
1144 reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
1145 goto out;
1148 if (ea_len < 10) {
1149 DEBUG(10,("call_nt_transact_create - ea_len = %u - "
1150 "too small (should be more than 10)\n",
1151 (unsigned int)ea_len ));
1152 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1153 goto out;
1156 /* We have already checked that ea_len <= data_count here. */
1157 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
1158 ea_len);
1159 if (ea_list == NULL) {
1160 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1161 goto out;
1164 if (!req->posix_pathnames &&
1165 ea_list_has_invalid_name(ea_list)) {
1166 /* Realloc the size of parameters and data we will return */
1167 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1168 /* Extended response is 32 more bytes. */
1169 param_len = 101;
1170 } else {
1171 param_len = 69;
1173 params = nttrans_realloc(ppparams, param_len);
1174 if(params == NULL) {
1175 reply_nterror(req, NT_STATUS_NO_MEMORY);
1176 goto out;
1179 memset(params, '\0', param_len);
1180 send_nt_replies(conn, req, STATUS_INVALID_EA_NAME,
1181 params, param_len, NULL, 0);
1182 goto out;
1186 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1187 if (oplock_request) {
1188 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1189 ? BATCH_OPLOCK : 0;
1193 * Bug #6898 - clients using Windows opens should
1194 * never be able to set this attribute into the
1195 * VFS.
1197 file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1199 status = SMB_VFS_CREATE_FILE(
1200 conn, /* conn */
1201 req, /* req */
1202 dirfsp, /* dirfsp */
1203 smb_fname, /* fname */
1204 access_mask, /* access_mask */
1205 share_access, /* share_access */
1206 create_disposition, /* create_disposition*/
1207 create_options, /* create_options */
1208 file_attributes, /* file_attributes */
1209 oplock_request, /* oplock_request */
1210 NULL, /* lease */
1211 allocation_size, /* allocation_size */
1212 0, /* private_flags */
1213 sd, /* sd */
1214 ea_list, /* ea_list */
1215 &fsp, /* result */
1216 &info, /* pinfo */
1217 NULL, NULL); /* create context */
1219 if(!NT_STATUS_IS_OK(status)) {
1220 if (open_was_deferred(req->xconn, req->mid)) {
1221 /* We have re-scheduled this call, no error. */
1222 return;
1224 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
1225 bool ok = defer_smb1_sharing_violation(req);
1226 if (ok) {
1227 return;
1230 reply_openerror(req, status);
1231 goto out;
1234 /* Ensure we're pointing at the correct stat struct. */
1235 TALLOC_FREE(smb_fname);
1236 smb_fname = fsp->fsp_name;
1239 * If the caller set the extended oplock request bit
1240 * and we granted one (by whatever means) - set the
1241 * correct bit for extended oplock reply.
1244 if (oplock_request &&
1245 (lp_fake_oplocks(SNUM(conn))
1246 || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1249 * Exclusive oplock granted
1252 if (flags & REQUEST_BATCH_OPLOCK) {
1253 oplock_granted = BATCH_OPLOCK_RETURN;
1254 } else {
1255 oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1257 } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1258 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1259 } else {
1260 oplock_granted = NO_OPLOCK_RETURN;
1263 file_len = smb_fname->st.st_ex_size;
1265 /* Realloc the size of parameters and data we will return */
1266 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1267 /* Extended response is 32 more byyes. */
1268 param_len = 101;
1269 } else {
1270 param_len = 69;
1272 params = nttrans_realloc(ppparams, param_len);
1273 if(params == NULL) {
1274 reply_nterror(req, NT_STATUS_NO_MEMORY);
1275 goto out;
1278 p = params;
1279 SCVAL(p, 0, oplock_granted);
1281 p += 2;
1282 SSVAL(p,0,fsp->fnum);
1283 p += 2;
1284 if ((create_disposition == FILE_SUPERSEDE)
1285 && (info == FILE_WAS_OVERWRITTEN)) {
1286 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1287 } else {
1288 SIVAL(p,0,info);
1290 p += 8;
1292 fattr = fdos_mode(fsp);
1293 if (fattr == 0) {
1294 fattr = FILE_ATTRIBUTE_NORMAL;
1297 /* Create time. */
1298 create_timespec = get_create_timespec(conn, fsp, smb_fname);
1299 a_timespec = smb_fname->st.st_ex_atime;
1300 m_timespec = smb_fname->st.st_ex_mtime;
1301 c_timespec = get_change_timespec(conn, fsp, smb_fname);
1303 if (lp_dos_filetime_resolution(SNUM(conn))) {
1304 dos_filetime_timespec(&create_timespec);
1305 dos_filetime_timespec(&a_timespec);
1306 dos_filetime_timespec(&m_timespec);
1307 dos_filetime_timespec(&c_timespec);
1310 put_long_date_full_timespec(conn->ts_res, p, &create_timespec); /* create time. */
1311 p += 8;
1312 put_long_date_full_timespec(conn->ts_res, p, &a_timespec); /* access time */
1313 p += 8;
1314 put_long_date_full_timespec(conn->ts_res, p, &m_timespec); /* write time */
1315 p += 8;
1316 put_long_date_full_timespec(conn->ts_res, p, &c_timespec); /* change time */
1317 p += 8;
1318 SIVAL(p,0,fattr); /* File Attributes. */
1319 p += 4;
1320 SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1321 p += 8;
1322 SOFF_T(p,0,file_len);
1323 p += 8;
1324 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1325 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
1326 unsigned int num_streams = 0;
1327 struct stream_struct *streams = NULL;
1329 if (lp_ea_support(SNUM(conn))) {
1330 size_t num_names = 0;
1331 /* Do we have any EA's ? */
1332 status = get_ea_names_from_fsp(
1333 ctx, smb_fname->fsp, NULL, &num_names);
1334 if (NT_STATUS_IS_OK(status) && num_names) {
1335 file_status &= ~NO_EAS;
1339 status = vfs_fstreaminfo(smb_fname->fsp, ctx,
1340 &num_streams, &streams);
1341 /* There is always one stream, ::$DATA. */
1342 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
1343 file_status &= ~NO_SUBSTREAMS;
1345 TALLOC_FREE(streams);
1346 SSVAL(p,2,file_status);
1348 p += 4;
1349 SCVAL(p,0,fsp->fsp_flags.is_directory ? 1 : 0);
1351 if (flags & EXTENDED_RESPONSE_REQUIRED) {
1352 uint32_t perms = 0;
1353 p += 25;
1354 if (fsp->fsp_flags.is_directory ||
1355 fsp->fsp_flags.can_write ||
1356 can_write_to_fsp(fsp))
1358 perms = FILE_GENERIC_ALL;
1359 } else {
1360 perms = FILE_GENERIC_READ|FILE_EXECUTE;
1362 SIVAL(p,0,perms);
1365 DEBUG(5,("call_nt_transact_create: open name = %s\n",
1366 smb_fname_str_dbg(smb_fname)));
1368 /* Send the required number of replies */
1369 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1370 out:
1371 return;
1374 /****************************************************************************
1375 Reply to a NT CANCEL request.
1376 conn POINTER CAN BE NULL HERE !
1377 ****************************************************************************/
1379 void reply_ntcancel(struct smb_request *req)
1381 struct smbXsrv_connection *xconn = req->xconn;
1382 struct smbd_server_connection *sconn = req->sconn;
1383 bool found;
1386 * Go through and cancel any pending change notifies.
1389 START_PROFILE(SMBntcancel);
1390 smb1_srv_cancel_sign_response(xconn);
1391 found = remove_pending_change_notify_requests_by_mid(sconn, req->mid);
1392 if (!found) {
1393 smbd_smb1_brl_finish_by_mid(sconn, req->mid);
1396 DEBUG(3,("reply_ntcancel: cancel called on mid = %llu.\n",
1397 (unsigned long long)req->mid));
1399 END_PROFILE(SMBntcancel);
1400 return;
1403 /****************************************************************************
1404 Reply to a NT rename request.
1405 ****************************************************************************/
1407 void reply_ntrename(struct smb_request *req)
1409 connection_struct *conn = req->conn;
1410 struct files_struct *src_dirfsp = NULL;
1411 struct smb_filename *smb_fname_old = NULL;
1412 struct files_struct *dst_dirfsp = NULL;
1413 struct smb_filename *smb_fname_new = NULL;
1414 char *oldname = NULL;
1415 char *newname = NULL;
1416 const char *dst_original_lcomp = NULL;
1417 const char *p;
1418 NTSTATUS status;
1419 uint32_t attrs;
1420 uint32_t ucf_flags_src = ucf_flags_from_smb_request(req);
1421 NTTIME src_twrp = 0;
1422 uint32_t ucf_flags_dst = ucf_flags_from_smb_request(req);
1423 NTTIME dst_twrp = 0;
1424 uint16_t rename_type;
1425 TALLOC_CTX *ctx = talloc_tos();
1426 bool stream_rename = false;
1428 START_PROFILE(SMBntrename);
1430 if (req->wct < 4) {
1431 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1432 goto out;
1435 attrs = SVAL(req->vwv+0, 0);
1436 rename_type = SVAL(req->vwv+1, 0);
1438 p = (const char *)req->buf + 1;
1439 p += srvstr_get_path_req(ctx, req, &oldname, p, STR_TERMINATE,
1440 &status);
1441 if (!NT_STATUS_IS_OK(status)) {
1442 reply_nterror(req, status);
1443 goto out;
1446 if (!req->posix_pathnames && ms_has_wild(oldname)) {
1447 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1448 goto out;
1451 p++;
1452 p += srvstr_get_path_req(ctx, req, &newname, p, STR_TERMINATE,
1453 &status);
1454 if (!NT_STATUS_IS_OK(status)) {
1455 reply_nterror(req, status);
1456 goto out;
1459 if (!req->posix_pathnames && ms_has_wild(newname)) {
1460 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1461 goto out;
1464 if (!req->posix_pathnames) {
1465 /* The newname must begin with a ':' if the
1466 oldname contains a ':'. */
1467 if (strchr_m(oldname, ':')) {
1468 if (newname[0] != ':') {
1469 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1470 goto out;
1472 stream_rename = true;
1476 if (ucf_flags_src & UCF_GMT_PATHNAME) {
1477 extract_snapshot_token(oldname, &src_twrp);
1479 status = filename_convert_dirfsp(ctx,
1480 conn,
1481 oldname,
1482 ucf_flags_src,
1483 src_twrp,
1484 &src_dirfsp,
1485 &smb_fname_old);
1486 if (!NT_STATUS_IS_OK(status)) {
1487 if (NT_STATUS_EQUAL(status,
1488 NT_STATUS_PATH_NOT_COVERED)) {
1489 reply_botherror(req,
1490 NT_STATUS_PATH_NOT_COVERED,
1491 ERRSRV, ERRbadpath);
1492 goto out;
1494 reply_nterror(req, status);
1495 goto out;
1498 if (stream_rename) {
1500 * No point in calling filename_convert()
1501 * on a raw stream name. It can never find
1502 * the file anyway. Use the same logic as
1503 * SMB2_FILE_RENAME_INFORMATION_INTERNAL
1504 * and generate smb_fname_new directly.
1506 smb_fname_new = synthetic_smb_fname(talloc_tos(),
1507 smb_fname_old->base_name,
1508 newname,
1509 NULL,
1510 smb_fname_old->twrp,
1511 smb_fname_old->flags);
1512 if (smb_fname_new == NULL) {
1513 reply_nterror(req, NT_STATUS_NO_MEMORY);
1514 goto out;
1516 } else {
1517 if (ucf_flags_dst & UCF_GMT_PATHNAME) {
1518 extract_snapshot_token(newname,
1519 &dst_twrp);
1521 status = filename_convert_dirfsp(ctx,
1522 conn,
1523 newname,
1524 ucf_flags_dst,
1525 dst_twrp,
1526 &dst_dirfsp,
1527 &smb_fname_new);
1528 if (!NT_STATUS_IS_OK(status)) {
1529 if (NT_STATUS_EQUAL(status,
1530 NT_STATUS_PATH_NOT_COVERED)) {
1531 reply_botherror(req,
1532 NT_STATUS_PATH_NOT_COVERED,
1533 ERRSRV, ERRbadpath);
1534 goto out;
1536 reply_nterror(req, status);
1537 goto out;
1541 /* Get the last component of the destination for rename_internals(). */
1542 dst_original_lcomp = get_original_lcomp(ctx,
1543 conn,
1544 newname,
1545 ucf_flags_dst);
1546 if (dst_original_lcomp == NULL) {
1547 reply_nterror(req, NT_STATUS_NO_MEMORY);
1548 goto out;
1552 DEBUG(3,("reply_ntrename: %s -> %s\n",
1553 smb_fname_str_dbg(smb_fname_old),
1554 smb_fname_str_dbg(smb_fname_new)));
1556 switch(rename_type) {
1557 case RENAME_FLAG_RENAME:
1558 status = rename_internals(ctx,
1559 conn,
1560 req,
1561 src_dirfsp,
1562 smb_fname_old,
1563 dst_dirfsp,
1564 smb_fname_new,
1565 dst_original_lcomp,
1566 attrs,
1567 false,
1568 DELETE_ACCESS);
1569 break;
1570 case RENAME_FLAG_HARD_LINK:
1571 status = hardlink_internals(ctx,
1572 conn,
1573 req,
1574 false,
1575 src_dirfsp,
1576 smb_fname_old,
1577 dst_dirfsp,
1578 smb_fname_new);
1579 break;
1580 case RENAME_FLAG_COPY:
1581 status = copy_internals(ctx,
1582 conn,
1583 req,
1584 src_dirfsp,
1585 smb_fname_old,
1586 dst_dirfsp,
1587 smb_fname_new,
1588 attrs);
1589 break;
1590 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1591 status = NT_STATUS_INVALID_PARAMETER;
1592 break;
1593 default:
1594 status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1595 break;
1598 if (!NT_STATUS_IS_OK(status)) {
1599 if (open_was_deferred(req->xconn, req->mid)) {
1600 /* We have re-scheduled this call. */
1601 goto out;
1603 if (NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)) {
1604 bool ok = defer_smb1_sharing_violation(req);
1605 if (ok) {
1606 goto out;
1610 reply_nterror(req, status);
1611 goto out;
1614 reply_smb1_outbuf(req, 0, 0);
1615 out:
1616 END_PROFILE(SMBntrename);
1617 return;
1620 /****************************************************************************
1621 Reply to a notify change - queue the request and
1622 don't allow a directory to be opened.
1623 ****************************************************************************/
1625 static void smbd_smb1_notify_reply(struct smb_request *req,
1626 NTSTATUS error_code,
1627 uint8_t *buf, size_t len)
1629 send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1632 static void call_nt_transact_notify_change(connection_struct *conn,
1633 struct smb_request *req,
1634 uint16_t **ppsetup,
1635 uint32_t setup_count,
1636 char **ppparams,
1637 uint32_t parameter_count,
1638 char **ppdata, uint32_t data_count,
1639 uint32_t max_data_count,
1640 uint32_t max_param_count)
1642 uint16_t *setup = *ppsetup;
1643 files_struct *fsp;
1644 uint32_t filter;
1645 NTSTATUS status;
1646 bool recursive;
1648 if(setup_count < 6) {
1649 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1650 return;
1653 fsp = file_fsp(req, SVAL(setup,4));
1654 filter = IVAL(setup, 0);
1655 recursive = (SVAL(setup, 6) != 0) ? True : False;
1657 DEBUG(3,("call_nt_transact_notify_change\n"));
1659 if(!fsp) {
1660 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1661 return;
1665 char *filter_string;
1667 if (!(filter_string = notify_filter_string(NULL, filter))) {
1668 reply_nterror(req,NT_STATUS_NO_MEMORY);
1669 return;
1672 DEBUG(3,("call_nt_transact_notify_change: notify change "
1673 "called on %s, filter = %s, recursive = %d\n",
1674 fsp_str_dbg(fsp), filter_string, recursive));
1676 TALLOC_FREE(filter_string);
1679 if((!fsp->fsp_flags.is_directory) || (conn != fsp->conn)) {
1680 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1681 return;
1684 if (fsp->notify == NULL) {
1686 status = change_notify_create(fsp,
1687 max_param_count,
1688 filter,
1689 recursive);
1690 if (!NT_STATUS_IS_OK(status)) {
1691 DEBUG(10, ("change_notify_create returned %s\n",
1692 nt_errstr(status)));
1693 reply_nterror(req, status);
1694 return;
1698 if (change_notify_fsp_has_changes(fsp)) {
1701 * We've got changes pending, respond immediately
1705 * TODO: write a torture test to check the filtering behaviour
1706 * here.
1709 change_notify_reply(req,
1710 NT_STATUS_OK,
1711 max_param_count,
1712 fsp->notify,
1713 smbd_smb1_notify_reply);
1716 * change_notify_reply() above has independently sent its
1717 * results
1719 return;
1723 * No changes pending, queue the request
1726 status = change_notify_add_request(req,
1727 max_param_count,
1728 filter,
1729 recursive, fsp,
1730 smbd_smb1_notify_reply);
1731 if (!NT_STATUS_IS_OK(status)) {
1732 reply_nterror(req, status);
1734 return;
1737 /****************************************************************************
1738 Reply to an NT transact rename command.
1739 ****************************************************************************/
1741 static void call_nt_transact_rename(connection_struct *conn,
1742 struct smb_request *req,
1743 uint16_t **ppsetup, uint32_t setup_count,
1744 char **ppparams, uint32_t parameter_count,
1745 char **ppdata, uint32_t data_count,
1746 uint32_t max_data_count)
1748 char *params = *ppparams;
1749 char *new_name = NULL;
1750 files_struct *fsp = NULL;
1751 NTSTATUS status;
1752 TALLOC_CTX *ctx = talloc_tos();
1754 if(parameter_count < 5) {
1755 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1756 return;
1759 fsp = file_fsp(req, SVAL(params, 0));
1760 if (!check_fsp(conn, req, fsp)) {
1761 return;
1763 if (req->posix_pathnames) {
1764 srvstr_get_path_posix(ctx,
1765 params,
1766 req->flags2,
1767 &new_name,
1768 params+4,
1769 parameter_count - 4,
1770 STR_TERMINATE,
1771 &status);
1772 } else {
1773 srvstr_get_path(ctx,
1774 params,
1775 req->flags2,
1776 &new_name,
1777 params+4,
1778 parameter_count - 4,
1779 STR_TERMINATE,
1780 &status);
1783 if (!NT_STATUS_IS_OK(status)) {
1784 reply_nterror(req, status);
1785 return;
1789 * W2K3 ignores this request as the RAW-RENAME test
1790 * demonstrates, so we do.
1792 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1794 DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1795 fsp_str_dbg(fsp), new_name));
1797 return;
1800 /****************************************************************************
1801 SMB1 reply to query a security descriptor.
1802 ****************************************************************************/
1804 static void call_nt_transact_query_security_desc(connection_struct *conn,
1805 struct smb_request *req,
1806 uint16_t **ppsetup,
1807 uint32_t setup_count,
1808 char **ppparams,
1809 uint32_t parameter_count,
1810 char **ppdata,
1811 uint32_t data_count,
1812 uint32_t max_data_count)
1814 char *params = *ppparams;
1815 char *data = *ppdata;
1816 size_t sd_size = 0;
1817 uint32_t security_info_wanted;
1818 files_struct *fsp = NULL;
1819 NTSTATUS status;
1820 uint8_t *marshalled_sd = NULL;
1822 if(parameter_count < 8) {
1823 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1824 return;
1827 fsp = file_fsp(req, SVAL(params,0));
1828 if(!fsp) {
1829 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1830 return;
1833 security_info_wanted = IVAL(params,4);
1835 DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1836 "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1837 (unsigned int)security_info_wanted));
1839 params = nttrans_realloc(ppparams, 4);
1840 if(params == NULL) {
1841 reply_nterror(req, NT_STATUS_NO_MEMORY);
1842 return;
1846 * Get the permissions to return.
1849 status = smbd_do_query_security_desc(conn,
1850 talloc_tos(),
1851 fsp,
1852 security_info_wanted &
1853 SMB_SUPPORTED_SECINFO_FLAGS,
1854 max_data_count,
1855 &marshalled_sd,
1856 &sd_size);
1858 if (NT_STATUS_EQUAL(status, NT_STATUS_BUFFER_TOO_SMALL)) {
1859 SIVAL(params,0,(uint32_t)sd_size);
1860 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1861 params, 4, NULL, 0);
1862 return;
1865 if (!NT_STATUS_IS_OK(status)) {
1866 reply_nterror(req, status);
1867 return;
1870 SMB_ASSERT(sd_size > 0);
1872 SIVAL(params,0,(uint32_t)sd_size);
1874 if (max_data_count < sd_size) {
1875 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1876 params, 4, NULL, 0);
1877 return;
1881 * Allocate the data we will return.
1884 data = nttrans_realloc(ppdata, sd_size);
1885 if(data == NULL) {
1886 reply_nterror(req, NT_STATUS_NO_MEMORY);
1887 return;
1890 memcpy(data, marshalled_sd, sd_size);
1892 send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1894 return;
1897 /****************************************************************************
1898 Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1899 ****************************************************************************/
1901 static void call_nt_transact_set_security_desc(connection_struct *conn,
1902 struct smb_request *req,
1903 uint16_t **ppsetup,
1904 uint32_t setup_count,
1905 char **ppparams,
1906 uint32_t parameter_count,
1907 char **ppdata,
1908 uint32_t data_count,
1909 uint32_t max_data_count)
1911 char *params= *ppparams;
1912 char *data = *ppdata;
1913 files_struct *fsp = NULL;
1914 uint32_t security_info_sent = 0;
1915 NTSTATUS status;
1917 if(parameter_count < 8) {
1918 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1919 return;
1922 if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1923 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
1924 return;
1927 if (!CAN_WRITE(fsp->conn)) {
1928 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
1929 return;
1932 if(!lp_nt_acl_support(SNUM(conn))) {
1933 goto done;
1936 security_info_sent = IVAL(params,4);
1938 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
1939 fsp_str_dbg(fsp), (unsigned int)security_info_sent));
1941 if (data_count == 0) {
1942 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1943 return;
1946 status = set_sd_blob(fsp, (uint8_t *)data, data_count,
1947 security_info_sent & SMB_SUPPORTED_SECINFO_FLAGS);
1948 if (!NT_STATUS_IS_OK(status)) {
1949 reply_nterror(req, status);
1950 return;
1953 done:
1954 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1955 return;
1958 /****************************************************************************
1959 Reply to NT IOCTL
1960 ****************************************************************************/
1962 static void call_nt_transact_ioctl(connection_struct *conn,
1963 struct smb_request *req,
1964 uint16_t **ppsetup, uint32_t setup_count,
1965 char **ppparams, uint32_t parameter_count,
1966 char **ppdata, uint32_t data_count,
1967 uint32_t max_data_count)
1969 NTSTATUS status;
1970 uint32_t function;
1971 uint16_t fidnum;
1972 files_struct *fsp;
1973 uint8_t isFSctl;
1974 uint8_t compfilter;
1975 char *out_data = NULL;
1976 uint32_t out_data_len = 0;
1977 char *pdata = *ppdata;
1978 TALLOC_CTX *ctx = talloc_tos();
1980 if (setup_count != 8) {
1981 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1982 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1983 return;
1986 function = IVAL(*ppsetup, 0);
1987 fidnum = SVAL(*ppsetup, 4);
1988 isFSctl = CVAL(*ppsetup, 6);
1989 compfilter = CVAL(*ppsetup, 7);
1991 DEBUG(10, ("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n",
1992 function, fidnum, isFSctl, compfilter));
1994 fsp=file_fsp(req, fidnum);
1997 * We don't really implement IOCTLs, especially on files.
1999 if (!isFSctl) {
2000 DEBUG(10, ("isFSctl: 0x%02X indicates IOCTL, not FSCTL!\n",
2001 isFSctl));
2002 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2003 return;
2006 /* Has to be for an open file! */
2007 if (!check_fsp_open(conn, req, fsp)) {
2008 return;
2011 SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
2014 * out_data might be allocated by the VFS module, but talloc should be
2015 * used, and should be cleaned up when the request ends.
2017 status = SMB_VFS_FSCTL(fsp,
2018 ctx,
2019 function,
2020 req->flags2,
2021 (uint8_t *)pdata,
2022 data_count,
2023 (uint8_t **)&out_data,
2024 max_data_count,
2025 &out_data_len);
2026 if (!NT_STATUS_IS_OK(status)) {
2027 reply_nterror(req, status);
2028 } else {
2029 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, out_data, out_data_len);
2034 #ifdef HAVE_SYS_QUOTAS
2035 /****************************************************************************
2036 Reply to get user quota
2037 ****************************************************************************/
2039 static void call_nt_transact_get_user_quota(connection_struct *conn,
2040 struct smb_request *req,
2041 uint16_t **ppsetup,
2042 uint32_t setup_count,
2043 char **ppparams,
2044 uint32_t parameter_count,
2045 char **ppdata,
2046 uint32_t data_count,
2047 uint32_t max_data_count)
2049 const struct loadparm_substitution *lp_sub =
2050 loadparm_s3_global_substitution();
2051 NTSTATUS nt_status = NT_STATUS_OK;
2052 char *params = *ppparams;
2053 char *pdata = *ppdata;
2054 int data_len = 0;
2055 int param_len = 0;
2056 files_struct *fsp = NULL;
2057 DATA_BLOB blob = data_blob_null;
2058 struct nttrans_query_quota_params info = {0};
2059 enum ndr_err_code err;
2060 TALLOC_CTX *tmp_ctx = NULL;
2061 uint32_t resp_len = 0;
2062 uint8_t *resp_data = 0;
2064 tmp_ctx = talloc_init("ntquota_list");
2065 if (!tmp_ctx) {
2066 nt_status = NT_STATUS_NO_MEMORY;
2067 goto error;
2070 /* access check */
2071 if (get_current_uid(conn) != sec_initial_uid()) {
2072 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2073 "[%s]\n", lp_servicename(talloc_tos(), lp_sub, SNUM(conn)),
2074 conn->session_info->unix_info->unix_name));
2075 nt_status = NT_STATUS_ACCESS_DENIED;
2076 goto error;
2079 blob.data = (uint8_t*)params;
2080 blob.length = parameter_count;
2082 err = ndr_pull_struct_blob(&blob, tmp_ctx, &info,
2083 (ndr_pull_flags_fn_t)ndr_pull_nttrans_query_quota_params);
2085 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
2086 DEBUG(0,("TRANSACT_GET_USER_QUOTA: failed to pull "
2087 "query_quota_params."));
2088 nt_status = NT_STATUS_INVALID_PARAMETER;
2089 goto error;
2091 DBG_DEBUG("info.return_single_entry = %u, info.restart_scan = %u, "
2092 "info.sid_list_length = %u, info.start_sid_length = %u, "
2093 "info.start_sid_offset = %u\n",
2094 (unsigned int)info.return_single_entry,
2095 (unsigned int)info.restart_scan,
2096 (unsigned int)info.sid_list_length,
2097 (unsigned int)info.start_sid_length,
2098 (unsigned int)info.start_sid_offset);
2100 /* set blob to point at data for further parsing */
2101 blob.data = (uint8_t*)pdata;
2102 blob.length = data_count;
2104 * Although MS-SMB ref is ambiguous here, a microsoft client will
2105 * only ever send a start sid (as part of a list) with
2106 * sid_list_length & start_sid_offset both set to the actual list
2107 * length. Note: Only a single result is returned in this case
2108 * In the case where either start_sid_offset or start_sid_length
2109 * are set alone or if both set (but have different values) then
2110 * it seems windows will return a number of entries from the start
2111 * of the list of users with quotas set. This behaviour is undocumented
2112 * and windows clients do not send messages of that type. As such we
2113 * currently will reject these requests.
2115 if (info.start_sid_length
2116 || (info.sid_list_length != info.start_sid_offset)) {
2117 DBG_ERR("TRANSACT_GET_USER_QUOTA: unsupported single or "
2118 "compound sid format\n");
2119 nt_status = NT_STATUS_INVALID_PARAMETER;
2120 goto error;
2123 /* maybe we can check the quota_fnum */
2124 fsp = file_fsp(req, info.fid);
2125 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2126 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2127 nt_status = NT_STATUS_INVALID_HANDLE;
2128 goto error;
2130 nt_status = smbd_do_query_getinfo_quota(tmp_ctx,
2131 fsp,
2132 info.restart_scan,
2133 info.return_single_entry,
2134 info.sid_list_length,
2135 &blob,
2136 max_data_count,
2137 &resp_data,
2138 &resp_len);
2139 if (!NT_STATUS_IS_OK(nt_status)) {
2140 if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MORE_ENTRIES)) {
2141 goto error;
2143 nt_status = NT_STATUS_OK;
2146 param_len = 4;
2147 params = nttrans_realloc(ppparams, param_len);
2148 if(params == NULL) {
2149 nt_status = NT_STATUS_NO_MEMORY;
2150 goto error;
2153 data_len = resp_len;
2154 SIVAL(params, 0, data_len);
2155 pdata = nttrans_realloc(ppdata, data_len);
2156 memcpy(pdata, resp_data, data_len);
2158 TALLOC_FREE(tmp_ctx);
2159 send_nt_replies(conn, req, nt_status, params, param_len,
2160 pdata, data_len);
2161 return;
2162 error:
2163 TALLOC_FREE(tmp_ctx);
2164 reply_nterror(req, nt_status);
2167 /****************************************************************************
2168 Reply to set user quota
2169 ****************************************************************************/
2171 static void call_nt_transact_set_user_quota(connection_struct *conn,
2172 struct smb_request *req,
2173 uint16_t **ppsetup,
2174 uint32_t setup_count,
2175 char **ppparams,
2176 uint32_t parameter_count,
2177 char **ppdata,
2178 uint32_t data_count,
2179 uint32_t max_data_count)
2181 const struct loadparm_substitution *lp_sub =
2182 loadparm_s3_global_substitution();
2183 char *params = *ppparams;
2184 char *pdata = *ppdata;
2185 int data_len=0,param_len=0;
2186 SMB_NTQUOTA_STRUCT qt;
2187 struct file_quota_information info = {0};
2188 enum ndr_err_code err;
2189 struct dom_sid sid;
2190 DATA_BLOB inblob;
2191 files_struct *fsp = NULL;
2192 TALLOC_CTX *ctx = NULL;
2193 NTSTATUS status = NT_STATUS_OK;
2194 ZERO_STRUCT(qt);
2196 /* access check */
2197 if (get_current_uid(conn) != sec_initial_uid()) {
2198 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2199 "[%s]\n", lp_servicename(talloc_tos(), lp_sub, SNUM(conn)),
2200 conn->session_info->unix_info->unix_name));
2201 status = NT_STATUS_ACCESS_DENIED;
2202 goto error;
2206 * Ensure minimum number of parameters sent.
2209 if (parameter_count < 2) {
2210 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2211 status = NT_STATUS_INVALID_PARAMETER;
2212 goto error;
2215 /* maybe we can check the quota_fnum */
2216 fsp = file_fsp(req, SVAL(params,0));
2217 if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2218 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2219 status = NT_STATUS_INVALID_HANDLE;
2220 goto error;
2223 ctx = talloc_init("set_user_quota");
2224 if (!ctx) {
2225 status = NT_STATUS_NO_MEMORY;
2226 goto error;
2228 inblob.data = (uint8_t*)pdata;
2229 inblob.length = data_count;
2231 err = ndr_pull_struct_blob(
2232 &inblob,
2233 ctx,
2234 &info,
2235 (ndr_pull_flags_fn_t)ndr_pull_file_quota_information);
2237 if (!NDR_ERR_CODE_IS_SUCCESS(err)) {
2238 DEBUG(0,("TRANSACT_SET_USER_QUOTA: failed to pull "
2239 "file_quota_information\n"));
2240 status = NT_STATUS_INVALID_PARAMETER;
2241 goto error;
2243 qt.usedspace = info.quota_used;
2245 qt.softlim = info.quota_threshold;
2247 qt.hardlim = info.quota_limit;
2249 sid = info.sid;
2251 if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2252 status = NT_STATUS_INTERNAL_ERROR;
2253 goto error;
2256 send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2257 pdata, data_len);
2258 TALLOC_FREE(ctx);
2259 return;
2260 error:
2261 TALLOC_FREE(ctx);
2262 reply_nterror(req, status);
2264 #endif /* HAVE_SYS_QUOTAS */
2266 static void handle_nttrans(connection_struct *conn,
2267 struct trans_state *state,
2268 struct smb_request *req)
2270 if (get_Protocol() >= PROTOCOL_NT1) {
2271 req->flags2 |= 0x40; /* IS_LONG_NAME */
2272 SSVAL(discard_const_p(uint8_t, req->inbuf),smb_flg2,req->flags2);
2276 SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2278 /* Now we must call the relevant NT_TRANS function */
2279 switch(state->call) {
2280 case NT_TRANSACT_CREATE:
2282 START_PROFILE(NT_transact_create);
2283 call_nt_transact_create(
2284 conn, req,
2285 &state->setup, state->setup_count,
2286 &state->param, state->total_param,
2287 &state->data, state->total_data,
2288 state->max_data_return);
2289 END_PROFILE(NT_transact_create);
2290 break;
2293 case NT_TRANSACT_IOCTL:
2295 START_PROFILE(NT_transact_ioctl);
2296 call_nt_transact_ioctl(
2297 conn, req,
2298 &state->setup, state->setup_count,
2299 &state->param, state->total_param,
2300 &state->data, state->total_data,
2301 state->max_data_return);
2302 END_PROFILE(NT_transact_ioctl);
2303 break;
2306 case NT_TRANSACT_SET_SECURITY_DESC:
2308 START_PROFILE(NT_transact_set_security_desc);
2309 call_nt_transact_set_security_desc(
2310 conn, req,
2311 &state->setup, state->setup_count,
2312 &state->param, state->total_param,
2313 &state->data, state->total_data,
2314 state->max_data_return);
2315 END_PROFILE(NT_transact_set_security_desc);
2316 break;
2319 case NT_TRANSACT_NOTIFY_CHANGE:
2321 START_PROFILE(NT_transact_notify_change);
2322 call_nt_transact_notify_change(
2323 conn, req,
2324 &state->setup, state->setup_count,
2325 &state->param, state->total_param,
2326 &state->data, state->total_data,
2327 state->max_data_return,
2328 state->max_param_return);
2329 END_PROFILE(NT_transact_notify_change);
2330 break;
2333 case NT_TRANSACT_RENAME:
2335 START_PROFILE(NT_transact_rename);
2336 call_nt_transact_rename(
2337 conn, req,
2338 &state->setup, state->setup_count,
2339 &state->param, state->total_param,
2340 &state->data, state->total_data,
2341 state->max_data_return);
2342 END_PROFILE(NT_transact_rename);
2343 break;
2346 case NT_TRANSACT_QUERY_SECURITY_DESC:
2348 START_PROFILE(NT_transact_query_security_desc);
2349 call_nt_transact_query_security_desc(
2350 conn, req,
2351 &state->setup, state->setup_count,
2352 &state->param, state->total_param,
2353 &state->data, state->total_data,
2354 state->max_data_return);
2355 END_PROFILE(NT_transact_query_security_desc);
2356 break;
2359 #ifdef HAVE_SYS_QUOTAS
2360 case NT_TRANSACT_GET_USER_QUOTA:
2362 START_PROFILE(NT_transact_get_user_quota);
2363 call_nt_transact_get_user_quota(
2364 conn, req,
2365 &state->setup, state->setup_count,
2366 &state->param, state->total_param,
2367 &state->data, state->total_data,
2368 state->max_data_return);
2369 END_PROFILE(NT_transact_get_user_quota);
2370 break;
2373 case NT_TRANSACT_SET_USER_QUOTA:
2375 START_PROFILE(NT_transact_set_user_quota);
2376 call_nt_transact_set_user_quota(
2377 conn, req,
2378 &state->setup, state->setup_count,
2379 &state->param, state->total_param,
2380 &state->data, state->total_data,
2381 state->max_data_return);
2382 END_PROFILE(NT_transact_set_user_quota);
2383 break;
2385 #endif /* HAVE_SYS_QUOTAS */
2387 default:
2388 /* Error in request */
2389 DEBUG(0,("handle_nttrans: Unknown request %d in "
2390 "nttrans call\n", state->call));
2391 reply_nterror(req, NT_STATUS_INVALID_LEVEL);
2392 return;
2394 return;
2397 /****************************************************************************
2398 Reply to a SMBNTtrans.
2399 ****************************************************************************/
2401 void reply_nttrans(struct smb_request *req)
2403 connection_struct *conn = req->conn;
2404 uint32_t pscnt;
2405 uint32_t psoff;
2406 uint32_t dscnt;
2407 uint32_t dsoff;
2408 uint16_t function_code;
2409 NTSTATUS result;
2410 struct trans_state *state;
2412 START_PROFILE(SMBnttrans);
2414 if (req->wct < 19) {
2415 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2416 END_PROFILE(SMBnttrans);
2417 return;
2420 pscnt = IVAL(req->vwv+9, 1);
2421 psoff = IVAL(req->vwv+11, 1);
2422 dscnt = IVAL(req->vwv+13, 1);
2423 dsoff = IVAL(req->vwv+15, 1);
2424 function_code = SVAL(req->vwv+18, 0);
2426 if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2427 reply_nterror(req, NT_STATUS_ACCESS_DENIED);
2428 END_PROFILE(SMBnttrans);
2429 return;
2432 result = allow_new_trans(conn->pending_trans, req->mid);
2433 if (!NT_STATUS_IS_OK(result)) {
2434 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2435 reply_nterror(req, result);
2436 END_PROFILE(SMBnttrans);
2437 return;
2440 if ((state = talloc(conn, struct trans_state)) == NULL) {
2441 reply_nterror(req, NT_STATUS_NO_MEMORY);
2442 END_PROFILE(SMBnttrans);
2443 return;
2446 state->cmd = SMBnttrans;
2448 state->mid = req->mid;
2449 state->vuid = req->vuid;
2450 state->total_data = IVAL(req->vwv+3, 1);
2451 state->data = NULL;
2452 state->total_param = IVAL(req->vwv+1, 1);
2453 state->param = NULL;
2454 state->max_data_return = IVAL(req->vwv+7, 1);
2455 state->max_param_return = IVAL(req->vwv+5, 1);
2457 /* setup count is in *words* */
2458 state->setup_count = 2*CVAL(req->vwv+17, 1);
2459 state->setup = NULL;
2460 state->call = function_code;
2462 DEBUG(10, ("num_setup=%u, "
2463 "param_total=%u, this_param=%u, max_param=%u, "
2464 "data_total=%u, this_data=%u, max_data=%u, "
2465 "param_offset=%u, data_offset=%u\n",
2466 (unsigned)state->setup_count,
2467 (unsigned)state->total_param, (unsigned)pscnt,
2468 (unsigned)state->max_param_return,
2469 (unsigned)state->total_data, (unsigned)dscnt,
2470 (unsigned)state->max_data_return,
2471 (unsigned)psoff, (unsigned)dsoff));
2474 * All nttrans messages we handle have smb_wct == 19 +
2475 * state->setup_count. Ensure this is so as a sanity check.
2478 if(req->wct != 19 + (state->setup_count/2)) {
2479 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2480 req->wct, 19 + (state->setup_count/2)));
2481 goto bad_param;
2484 /* Don't allow more than 128mb for each value. */
2485 if ((state->total_data > (1024*1024*128)) ||
2486 (state->total_param > (1024*1024*128))) {
2487 reply_nterror(req, NT_STATUS_NO_MEMORY);
2488 END_PROFILE(SMBnttrans);
2489 return;
2492 if ((dscnt > state->total_data) || (pscnt > state->total_param))
2493 goto bad_param;
2495 if (state->total_data) {
2497 if (smb_buffer_oob(state->total_data, 0, dscnt)
2498 || smb_buffer_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2499 goto bad_param;
2502 /* Can't use talloc here, the core routines do realloc on the
2503 * params and data. */
2504 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2505 DEBUG(0,("reply_nttrans: data malloc fail for %u "
2506 "bytes !\n", (unsigned int)state->total_data));
2507 TALLOC_FREE(state);
2508 reply_nterror(req, NT_STATUS_NO_MEMORY);
2509 END_PROFILE(SMBnttrans);
2510 return;
2513 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2516 if (state->total_param) {
2518 if (smb_buffer_oob(state->total_param, 0, pscnt)
2519 || smb_buffer_oob(smb_len(req->inbuf), psoff, pscnt)) {
2520 goto bad_param;
2523 /* Can't use talloc here, the core routines do realloc on the
2524 * params and data. */
2525 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2526 DEBUG(0,("reply_nttrans: param malloc fail for %u "
2527 "bytes !\n", (unsigned int)state->total_param));
2528 SAFE_FREE(state->data);
2529 TALLOC_FREE(state);
2530 reply_nterror(req, NT_STATUS_NO_MEMORY);
2531 END_PROFILE(SMBnttrans);
2532 return;
2535 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2538 state->received_data = dscnt;
2539 state->received_param = pscnt;
2541 if(state->setup_count > 0) {
2542 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2543 state->setup_count));
2546 * No overflow possible here, state->setup_count is an
2547 * unsigned int, being filled by a single byte from
2548 * CVAL(req->vwv+13, 0) above. The cast in the comparison
2549 * below is not necessary, it's here to clarify things. The
2550 * validity of req->vwv and req->wct has been checked in
2551 * init_smb1_request already.
2553 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2554 goto bad_param;
2557 state->setup = (uint16_t *)TALLOC(state, state->setup_count);
2558 if (state->setup == NULL) {
2559 DEBUG(0,("reply_nttrans : Out of memory\n"));
2560 SAFE_FREE(state->data);
2561 SAFE_FREE(state->param);
2562 TALLOC_FREE(state);
2563 reply_nterror(req, NT_STATUS_NO_MEMORY);
2564 END_PROFILE(SMBnttrans);
2565 return;
2568 memcpy(state->setup, req->vwv+19, state->setup_count);
2569 dump_data(10, (uint8_t *)state->setup, state->setup_count);
2572 if ((state->received_data == state->total_data) &&
2573 (state->received_param == state->total_param)) {
2574 handle_nttrans(conn, state, req);
2575 SAFE_FREE(state->param);
2576 SAFE_FREE(state->data);
2577 TALLOC_FREE(state);
2578 END_PROFILE(SMBnttrans);
2579 return;
2582 DLIST_ADD(conn->pending_trans, state);
2584 /* We need to send an interim response then receive the rest
2585 of the parameter/data bytes */
2586 reply_smb1_outbuf(req, 0, 0);
2587 show_msg((char *)req->outbuf);
2588 END_PROFILE(SMBnttrans);
2589 return;
2591 bad_param:
2593 DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2594 SAFE_FREE(state->data);
2595 SAFE_FREE(state->param);
2596 TALLOC_FREE(state);
2597 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2598 END_PROFILE(SMBnttrans);
2599 return;
2602 /****************************************************************************
2603 Reply to a SMBnttranss
2604 ****************************************************************************/
2606 void reply_nttranss(struct smb_request *req)
2608 connection_struct *conn = req->conn;
2609 uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2610 struct trans_state *state;
2612 START_PROFILE(SMBnttranss);
2614 show_msg((const char *)req->inbuf);
2616 /* Windows clients expect all replies to
2617 an NT transact secondary (SMBnttranss 0xA1)
2618 to have a command code of NT transact
2619 (SMBnttrans 0xA0). See bug #8989 for details. */
2620 req->cmd = SMBnttrans;
2622 if (req->wct < 18) {
2623 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2624 END_PROFILE(SMBnttranss);
2625 return;
2628 for (state = conn->pending_trans; state != NULL;
2629 state = state->next) {
2630 if (state->mid == req->mid) {
2631 break;
2635 if ((state == NULL) || (state->cmd != SMBnttrans)) {
2636 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2637 END_PROFILE(SMBnttranss);
2638 return;
2641 /* Revise state->total_param and state->total_data in case they have
2642 changed downwards */
2643 if (IVAL(req->vwv+1, 1) < state->total_param) {
2644 state->total_param = IVAL(req->vwv+1, 1);
2646 if (IVAL(req->vwv+3, 1) < state->total_data) {
2647 state->total_data = IVAL(req->vwv+3, 1);
2650 pcnt = IVAL(req->vwv+5, 1);
2651 poff = IVAL(req->vwv+7, 1);
2652 pdisp = IVAL(req->vwv+9, 1);
2654 dcnt = IVAL(req->vwv+11, 1);
2655 doff = IVAL(req->vwv+13, 1);
2656 ddisp = IVAL(req->vwv+15, 1);
2658 state->received_param += pcnt;
2659 state->received_data += dcnt;
2661 if ((state->received_data > state->total_data) ||
2662 (state->received_param > state->total_param))
2663 goto bad_param;
2665 if (pcnt) {
2666 if (smb_buffer_oob(state->total_param, pdisp, pcnt)
2667 || smb_buffer_oob(smb_len(req->inbuf), poff, pcnt)) {
2668 goto bad_param;
2670 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
2673 if (dcnt) {
2674 if (smb_buffer_oob(state->total_data, ddisp, dcnt)
2675 || smb_buffer_oob(smb_len(req->inbuf), doff, dcnt)) {
2676 goto bad_param;
2678 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
2681 if ((state->received_param < state->total_param) ||
2682 (state->received_data < state->total_data)) {
2683 END_PROFILE(SMBnttranss);
2684 return;
2687 handle_nttrans(conn, state, req);
2689 DLIST_REMOVE(conn->pending_trans, state);
2690 SAFE_FREE(state->data);
2691 SAFE_FREE(state->param);
2692 TALLOC_FREE(state);
2693 END_PROFILE(SMBnttranss);
2694 return;
2696 bad_param:
2698 DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2699 DLIST_REMOVE(conn->pending_trans, state);
2700 SAFE_FREE(state->data);
2701 SAFE_FREE(state->param);
2702 TALLOC_FREE(state);
2703 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2704 END_PROFILE(SMBnttranss);
2705 return;